Skip to content

Commit

Permalink
When recovering from errors when parsing Pratt expressions, give the …
Browse files Browse the repository at this point in the history
…root node of the flattened expression the label `Unrecognized` instead of `Root`.
  • Loading branch information
mjoerussell committed Jan 22, 2025
1 parent 337fbfa commit 3f28258
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,19 @@ pub enum PrattElement {

impl PrattElement {
pub fn into_nodes(self) -> Vec<Edge> {
self.into_nodes_with_label(EdgeLabel::Root)
}

pub fn into_nodes_with_label(self, label: EdgeLabel) -> Vec<Edge> {
match self {
Self::Expression { nodes } => nodes,
Self::Binary { kind, nodes, .. }
| Self::Prefix { kind, nodes, .. }
| Self::Postfix { kind, nodes, .. } => {
vec![Edge::root(Node::nonterminal(kind, nodes))]
vec![Edge {
label,
node: Node::nonterminal(kind, nodes),
}]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ impl SequenceHelper {
self.result = State::Running(ParserResult::incomplete_match(
std::mem::take(&mut cur.elements)
.into_iter()
.flat_map(PrattElement::into_nodes)
.flat_map(|e| e.into_nodes_with_label(EdgeLabel::Unrecognized))
.chain(next.nodes)
.collect(),
next.expected_terminals,
Expand All @@ -99,7 +99,7 @@ impl SequenceHelper {
self.result = State::Running(ParserResult::incomplete_match(
std::mem::take(&mut cur.elements)
.into_iter()
.flat_map(PrattElement::into_nodes)
.flat_map(|e| e.into_nodes_with_label(EdgeLabel::Unrecognized))
.collect(),
next.expected_terminals,
));
Expand All @@ -116,7 +116,7 @@ impl SequenceHelper {
(ParserResult::PrattOperatorMatch(running), ParserResult::SkippedUntil(skipped)) => {
let nodes: Vec<_> = std::mem::take(&mut running.elements)
.into_iter()
.flat_map(PrattElement::into_nodes)
.flat_map(|e| e.into_nodes_with_label(EdgeLabel::Unrecognized))
.chain(skipped.nodes)
.collect();

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Errors: # 1 total
Tree:
- (root꞉ Expression): # "2 * new\n" (0..8)
- (variant꞉ DecimalNumberExpression) ► (literal꞉ DecimalLiteral): "2" # (0..1)
- (root꞉ MultiplicativeExpression): # " *" (1..3)
- (unrecognized꞉ MultiplicativeExpression): # " *" (1..3)
- (leading_trivia꞉ Whitespace): " " # (1..2)
- (operator꞉ Asterisk): "*" # (2..3)
- (variant꞉ NewExpression): # " new\n" (3..8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Errors: # 1 total
Tree:
- (root꞉ Expression): # "2 * new\n" (0..8)
- (variant꞉ DecimalNumberExpression) ► (literal꞉ DecimalLiteral): "2" # (0..1)
- (root꞉ MultiplicativeExpression): # " *" (1..3)
- (unrecognized꞉ MultiplicativeExpression): # " *" (1..3)
- (leading_trivia꞉ Whitespace): " " # (1..2)
- (operator꞉ Asterisk): "*" # (2..3)
- (variant꞉ NewExpression): # " new\n" (3..8)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Tree:
- (variant꞉ DecimalNumberExpression): # "\t\t2" (29..32)
- (leading_trivia꞉ Whitespace): "\t\t" # (29..31)
- (literal꞉ DecimalLiteral): "2" # (31..32)
- (root꞉ AdditiveExpression): # " +" (32..34)
- (unrecognized꞉ AdditiveExpression): # " +" (32..34)
- (leading_trivia꞉ Whitespace): " " # (32..33)
- (operator꞉ Plus): "+" # (33..34)
- (variant꞉ TupleExpression): # " (\n\t" (34..38)
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f28258

Please sign in to comment.