Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion crates/plotnik-lib/src/bytecode/dump.rs
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,13 @@ fn format_call(
.unwrap_or_else(|| format!("@{:0w$}", call.target.0, w = step_width));
// Definition name in call is blue
let content = format!("{field_part}({}{}{})", c.blue, target_name, c.reset);
let successors = format!("{} ⯇", format_step(call.next, ctx, step_width));
// Format as "target : return" with numeric IDs
let successors = format!(
"{:0w$} : {:0w$}",
call.target.get(),
call.next.get(),
w = step_width
);

let base = format!("{prefix}{content}");
builder.pad_successors(base, &successors)
Expand Down
30 changes: 26 additions & 4 deletions crates/plotnik-lib/src/bytecode/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ impl Symbol {
/// | --------------- | ------- | ----------------------------------- |
/// | Stay | (blank) | No movement, 5 spaces |
/// | Stay (epsilon) | ε | Only when no type/field constraints |
/// | StayExact | !!! | Stay at position, exact match only |
/// | StayExact | ! | Stay at position, exact match only |
/// | Down | ▽ | First child, skip any |
/// | DownSkip | !▽ | First child, skip trivia |
/// | DownExact | !!▽ | First child, exact |
Expand All @@ -99,7 +99,7 @@ impl Symbol {
pub fn nav_symbol(nav: Nav) -> Symbol {
match nav {
Nav::Stay => Symbol::EMPTY,
Nav::StayExact => Symbol::new(" ", "!!!", " "),
Nav::StayExact => Symbol::new(" ", "!", " "),
Nav::Down => Symbol::new(" ", "▽", " "),
Nav::DownSkip => Symbol::new(" !", "▽", " "),
Nav::DownExact => Symbol::new("!!", "▽", " "),
Expand Down Expand Up @@ -278,12 +278,34 @@ impl LineBuilder {
/// Ensures at least 2 spaces between content and successors.
pub fn pad_successors(&self, base: String, successors: &str) -> String {
let padding = cols::TOTAL_WIDTH
.saturating_sub(base.chars().count())
.saturating_sub(display_width(&base))
.max(2);
format!("{base}{:padding$}{successors}", "")
}
}

/// Calculate display width of a string, ignoring ANSI escape sequences.
///
/// ANSI sequences have the form `\x1b[...m` and render as zero-width.
fn display_width(s: &str) -> usize {
let mut width = 0;
let mut in_escape = false;

for c in s.chars() {
if in_escape {
if c == 'm' {
in_escape = false;
}
} else if c == '\x1b' {
in_escape = true;
} else {
width += 1;
}
}

width
}

// =============================================================================
// Effect Formatting
// =============================================================================
Expand Down Expand Up @@ -316,7 +338,7 @@ mod tests {
fn test_symbol_format() {
assert_eq!(Symbol::EMPTY.format(), " ");
assert_eq!(Symbol::EPSILON.format(), " ε ");
assert_eq!(nav_symbol(Nav::StayExact).format(), " !!! ");
assert_eq!(nav_symbol(Nav::StayExact).format(), " ! ");
assert_eq!(nav_symbol(Nav::Down).format(), " ▽ ");
assert_eq!(nav_symbol(Nav::DownSkip).format(), " !▽ ");
assert_eq!(nav_symbol(Nav::DownExact).format(), "!!▽ ");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ Test:
04 ε 09, 11
06 ▶
07 ε [EndObj] 06
09 !!! (identifier) [Node Set(M0)] 07
11 !!! (number) [Node Set(M0)] 07
09 ! (identifier) [Node Set(M0)] 07
11 ! (number) [Node Set(M0)] 07
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ Test:
06 ▶
07 ε [EndObj] 06
09 ε [EndEnum Set(M4)] 07
11 !!! (identifier) [Node Set(M0)] 09
11 ! (identifier) [Node Set(M0)] 09
13 ε [Enum(M2)] 11
15 ε [EndEnum Set(M4)] 07
17 !!! (number) [Node Set(M1)] 15
17 ! (number) [Node Set(M1)] 15
19 ε [Enum(M3)] 17
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ Test = 01 :: T6
Test:
01 ε 02
02 ε [Obj] 04
04 !!! (object) 05
04 ! (object) 05
05 ε [Arr] 07
07 ε 42, 19
09 ε [EndArr Set(M5)] 11
Expand All @@ -59,10 +59,10 @@ Test:
17 ε [EndObj] 16
19 ε [EndArr Set(M5)] 17
21 ε [EndEnum Set(M4)] 12
23 !!! (pair) [Node Set(M0)] 21
23 ! (pair) [Node Set(M0)] 21
25 ε [Enum(M2)] 23
27 ε [EndEnum Set(M4)] 12
29 !!! (shorthand_property_identifier) [Node Set(M1)] 27
29 ! (shorthand_property_identifier) [Node Set(M1)] 27
31 ε [Enum(M3)] 29
33 ε 25, 31
35 ε [Obj] 33
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Test:
02 ε 09, 15
04 ▶
05 ε [EndEnum] 04
07 !!! (identifier) [Node Set(M0)] 05
07 ! (identifier) [Node Set(M0)] 05
09 ε [Enum(M2)] 07
11 ε [EndEnum] 04
13 !!! (number) [Node Set(M1)] 11
13 ! (number) [Node Set(M1)] 11
15 ε [Enum(M3)] 13
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Test = 01 :: T1
Test:
01 ε 02
02 ε [Obj] 04
04 !!! (program) 05
04 ! (program) 05
05 ε 10, 12
07 ▶
08 ε [EndObj] 07
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Test:
04 ε 11, 15
06 ▶
07 ε [EndObj] 06
09 !!! (identifier) [Node Set(M0)] 07
09 ! (identifier) [Node Set(M0)] 07
11 ε [Null Set(M1)] 09
13 !!! (number) [Node Set(M1)] 07
13 ! (number) [Node Set(M1)] 07
15 ε [Null Set(M0)] 13
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Test = 09 :: T4
Inner:
01 ε 02
02 ε [Obj] 04
04 !!! (identifier) [Node Set(M0)] 06
04 ! (identifier) [Node Set(M0)] 06
06 ε [EndObj] 08
08 ▶

Expand All @@ -58,8 +58,8 @@ Test:
14 ▶
15 ε [EndObj] 14
17 ε [EndEnum Set(M4)] 15
19 !!! (Inner) 17 ⯇
19 ! (Inner) 01 : 17
20 ε [Enum(M2)] 19
22 ε [EndEnum Set(M4)] 15
24 !!! (number) [Node Set(M1)] 22
24 ! (number) [Node Set(M1)] 22
26 ε [Enum(M3)] 24
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ Test:
04 ε 11, 15
06 ▶
07 ε [EndObj] 06
09 !!! (identifier) [Node Set(M0)] 07
09 ! (identifier) [Node Set(M0)] 07
11 ε [Null Set(M1)] 09
13 !!! (string) [Node Set(M1)] 07
13 ! (string) [Node Set(M1)] 07
15 ε [Null Set(M0)] 13
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Test = 1 :: T0

Test:
1 ε 2
2 !!! (parent) 3
2 ! (parent) 3
3 ▽ (a) 4
4 !▷ (b) 5
5 △ 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Test = 1 :: T0

Test:
1 ε 2
2 !!! (parent) 3
2 ! (parent) 3
3 !▽ (first) 4
4 △ 5
5 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Test = 1 :: T0

Test:
1 ε 2
2 !!! (parent) 3
2 ! (parent) 3
3 ▽ (last) 4
4 !△ 5
5 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Test = 1 :: T0

Test:
1 ε 2
2 !!! (parent) 3
2 ! (parent) 3
3 ▽ (a) 4
4 ▷ (b) 5
5 △ 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Test = 1 :: T0

Test:
1 ε 2
2 !!! (parent) 3
2 ! (parent) 3
3 ▽ (+) 4
4 !!▷ (next) 5
5 △ 6
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ Test = 1 :: T1
Test:
1 ε 2
2 ε [Obj] 4
4 !!! (identifier) [Node Set(M0)] 6
4 ! (identifier) [Node Set(M0)] 6
6 ε [EndObj] 8
8 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Test = 01 :: T1
Test:
01 ε 02
02 ε [Obj] 04
04 !!! (a) [Node Set(M0)] 06
04 ! (a) [Node Set(M0)] 06
06 ▽ (b) [Node Set(M1)] 08
08 ▽ (c) [Node Set(M2)] 10
10 ▽ (d) [Node Set(M3)] 12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Test = 01 :: T1
Test:
01 ε 02
02 ε [Obj] 04
04 !!! (binary_expression) 05
04 ! (binary_expression) 05
05 ▽ (identifier) [Node Set(M0)] 07
07 ▷ (number) [Node Set(M1)] 09
09 △ 10
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Test = 01 :: T1
Test:
01 ε 02
02 ε [Obj] 04
04 !!! (a) [Node Set(M0)] 06
04 ! (a) [Node Set(M0)] 06
06 ▽ (b) [Node Set(M1)] 08
08 ▽ (c) [Node Set(M2)] 10
10 △ 11
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ Test:
09 ε [EndObj] 08
11 ε [EndObj Set(M2)] 09
13 ε [EndObj Set(M1)] 11
15 !!! (identifier) [Node Set(M0)] 13
15 ! (identifier) [Node Set(M0)] 13
17 ε [Obj] 15
19 ε [Null Set(M1)] 11
21 ▷ 24
22 ε 21, 19
24 ε 17, 22
26 !!! 24
26 ! 24
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Test:
01 ε 02
02 ε [Obj] 04
04 ε [Obj] 06
06 !!! (a) [Node Set(M0)] 08
06 ! (a) [Node Set(M0)] 08
08 ▷ (b) [Node Set(M1)] 10
10 ε [EndObj Set(M2)] 12
12 ε [EndObj] 14
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,6 @@ Test = 1 :: T2
Test:
1 ε 2
2 ε [Obj] 4
4 !!! (identifier) [Node Set(M0)] 6
4 ! (identifier) [Node Set(M0)] 6
6 ε [EndObj] 8
8 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ Test = 1 :: T1
Test:
1 ε 2
2 ε [Obj] 4
4 !!! (identifier) [Text Set(M0)] 6
4 ! (identifier) [Text Set(M0)] 6
6 ε [EndObj] 8
8 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ Test:
15 ε [EndArr Set(M3)] 13
17 ε [EndObj Set(M2)] 08
19 ▷ (number) [Node Set(M1)] 17
21 !!! (identifier) [Node Set(M0)] 19
21 ! (identifier) [Node Set(M0)] 19
23 ε [Obj] 21
25 ε [Obj] 23
27 ▷ 30
28 ε 27, 15
30 ε 25, 28
32 !!! 30
32 ! 30
33 ▷ 36
34 ε 33, 15
36 ε 25, 34
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Ident = 01 :: T02
Ident:
01 ε 02
02 ε [Obj] 04
04 !!! (identifier) [Text Set(M0)] 06
04 ! (identifier) [Text Set(M0)] 06
06 ε [EndObj] 08
08 ▶

Expand All @@ -78,17 +78,17 @@ Expression:
Assignment:
12 ε 13
13 ε [Obj] 15
15 !!! (assignment_expression) 16
15 ! (assignment_expression) 16
16 ▽ left: (identifier) [Node Set(M6)] 18
18 ▷ right: (Expression) 19 ⯇
18 ▷ right: (Expression) 09 : 19
19 ε [Set(M5)] 21
21 △ 22
22 ε [EndObj] 24
24 ▶
25 ▶
26 ε [EndEnum] 25
28 !!! (number) [Node Set(M1)] 26
28 ! (number) [Node Set(M1)] 26
30 ε [Enum(M3)] 28
32 ε [EndEnum] 25
34 !!! (identifier) [Node Set(M2)] 32
34 ! (identifier) [Node Set(M2)] 32
36 ε [Enum(M4)] 34
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ Foo = 01 :: T1
Foo:
01 ε 02
02 ε [Obj] 04
04 !!! (identifier) [Node Set(M0)] 06
04 ! (identifier) [Node Set(M0)] 06
06 ε [EndObj] 08
08 ▶

Bar:
09 ε 10
10 ε [Obj] 12
12 !!! (string) [Node Set(M1)] 14
12 ! (string) [Node Set(M1)] 14
14 ε [EndObj] 16
16 ▶
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Outer = 11 :: T4
Inner:
01 ε 02
02 ε [Obj] 04
04 !!! (call) 05
04 ! (call) 05
05 ▽ (identifier) [Node Set(M0)] 07
07 △ 08
08 ε [EndObj] 10
Expand All @@ -54,7 +54,7 @@ Inner:
Outer:
11 ε 12
12 ε [Obj] 14
14 !!! (parent) 15
14 ! (parent) 15
15 ε [Arr] 17
17 ε 41, 29
19 ε [EndArr Set(M3)] 21
Expand All @@ -65,7 +65,7 @@ Outer:
27 ε [EndObj] 26
29 ε [EndArr Set(M3)] 27
31 ε [Set(M2)] 22
33 !!! (Inner) 31 ⯇
33 ! (Inner) 01 : 31
34 ε [Obj] 33
36 ▷ 39
37 ε 36, 29
Expand Down
Loading