Skip to content

Commit

Permalink
refactor: improve parser and parse trace visualizations
Browse files Browse the repository at this point in the history
  • Loading branch information
umut-sahin committed Sep 9, 2024
1 parent 8d5d3f8 commit 24a879c
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
Binary file modified assets/docs/demo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 16 additions & 1 deletion src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,22 @@ impl Parser {
let mut pretty_grammar = Table::new();

pretty_grammar.add_row(row![cbFy->"Grammar"]);
pretty_grammar.add_row(row![self.grammar]);
{
let mut pretty_rules = Table::new();
pretty_rules.set_format(*prettytable::format::consts::FORMAT_CLEAN);

for (rule_index, rule) in self.grammar.rules().iter().enumerate() {
pretty_rules.add_row(row![r->format!("{})", rule_index + 1), rule]);
}
if !self.grammar.regular_expressions().is_empty() {
pretty_rules.add_row(row![r->"", ""]);
}
for (regex_token, regex) in self.grammar.regular_expressions().iter() {
pretty_rules.add_row(row![r->"", format!("{} -> /{}/", regex_token, regex)]);
}

pretty_grammar.add_row(row![pretty_rules]);
}

pretty_grammar.printstd();
}
Expand Down
4 changes: 2 additions & 2 deletions src/trace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ impl<'i> Trace<'i> {
format!("Shift {}", next_state)
},
Action::Reduce { rule_index } => {
format!("Reduce {} ({})", rule_index, grammar.rules()[rule_index])
format!("Reduce {} ({})", rule_index + 1, grammar.rules()[rule_index])
},
Action::Accept { rule_index } => {
format!("Accept {} ({})", rule_index, grammar.rules()[rule_index])
format!("Accept {} ({})", rule_index + 1, grammar.rules()[rule_index])
},
};

Expand Down

0 comments on commit 24a879c

Please sign in to comment.