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
73 changes: 30 additions & 43 deletions parser/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,7 @@ impl<'src> ParserImpl<'src> {
where
P: Fn(&mut Self) -> &mut Self,
{
if matches!(self.state, State::OutOfFuel) {
if matches!(self.state, State::Failure | State::OutOfFuel) {
return self;
}

Expand Down Expand Up @@ -1111,7 +1111,7 @@ impl ParserImpl<'_> {
.expect(t!(L_BRACE))
.if_next(t!(META_KW), |p| p.meta_blk())
.if_next(t!(STRINGS_KW), |p| p.patterns_blk())
.then(|p| p.condition_blk())
.condition_blk()
.expect(t!(R_BRACE))
.end()
.recover(t!(GLOBAL_KW
Expand Down Expand Up @@ -1289,7 +1289,7 @@ impl ParserImpl<'_> {
self.begin(HEX_PATTERN)
.expect(t!(L_BRACE))
.enter_hex_pattern_mode()
.then(|p| p.hex_sub_pattern())
.hex_sub_pattern()
.expect(t!(R_BRACE))
.end()
}
Expand Down Expand Up @@ -1324,8 +1324,8 @@ impl ParserImpl<'_> {
fn hex_alternative(&mut self) -> &mut Self {
self.begin(HEX_ALTERNATIVE)
.expect(t!(L_PAREN))
.then(|p| p.hex_sub_pattern())
.zero_or_more(|p| p.expect(t!(PIPE)).then(|p| p.hex_sub_pattern()))
.hex_sub_pattern()
.zero_or_more(|p| p.expect(t!(PIPE)).hex_sub_pattern())
.expect(t!(R_PAREN))
.end()
}
Expand Down Expand Up @@ -1374,8 +1374,7 @@ impl ParserImpl<'_> {
self.begin(BOOLEAN_EXPR)
.boolean_term()
.zero_or_more(|p| {
p.expect_d(t!(AND_KW | OR_KW), Some("operator"))
.then(|p| p.boolean_term())
p.expect_d(t!(AND_KW | OR_KW), Some("operator")).boolean_term()
})
.end()
}
Expand Down Expand Up @@ -1408,10 +1407,7 @@ impl ParserImpl<'_> {
)
})
.alt(|p| p.expect_d(t!(TRUE_KW | FALSE_KW), DESC))
.alt(|p| {
p.expect_d(t!(NOT_KW | DEFINED_KW), DESC)
.then(|p| p.boolean_term())
})
.alt(|p| p.expect_d(t!(NOT_KW | DEFINED_KW), DESC).boolean_term())
.alt(|p| p.for_expr())
.alt(|p| p.of_expr())
.alt(|p| p.with_expr())
Expand All @@ -1434,12 +1430,12 @@ impl ParserImpl<'_> {
| MATCHES_KW),
DESC,
)
.then(|p| p.expr())
.expr()
})
})
.alt(|p| {
p.expect_d(t!(L_PAREN), DESC)
.then(|p| p.boolean_expr())
.boolean_expr()
.expect(t!(R_PAREN))
})
.end_alt()
Expand Down Expand Up @@ -1471,7 +1467,7 @@ impl ParserImpl<'_> {
| BITWISE_XOR),
Some("operator"),
)
.then(|p| p.term())
.term()
})
.end()
})
Expand All @@ -1487,9 +1483,8 @@ impl ParserImpl<'_> {
.expect(t!(IDENT))
.expect(t!(L_PAREN))
.opt(|p| {
p.boolean_expr().zero_or_more(|p| {
p.expect(t!(COMMA)).then(|p| p.boolean_expr())
})
p.boolean_expr()
.zero_or_more(|p| p.expect(t!(COMMA)).boolean_expr())
})
.expect(t!(R_PAREN))
.end()
Expand All @@ -1503,10 +1498,10 @@ impl ParserImpl<'_> {
fn range(&mut self) -> &mut Self {
self.begin(RANGE)
.expect(t!(L_PAREN))
.then(|p| p.expr())
.expr()
.expect(t!(DOT))
.expect(t!(DOT))
.then(|p| p.expr())
.expr()
.expect(t!(R_PAREN))
.end()
}
Expand Down Expand Up @@ -1557,12 +1552,10 @@ impl ParserImpl<'_> {
p.expr().expect(t!(R_BRACKET))
})
})
.alt(|p| p.expect_d(t!(MINUS), DESC).then(|p| p.term()))
.alt(|p| p.expect_d(t!(BITWISE_NOT), DESC).then(|p| p.term()))
.alt(|p| p.expect_d(t!(MINUS), DESC).term())
.alt(|p| p.expect_d(t!(BITWISE_NOT), DESC).term())
.alt(|p| {
p.expect_d(t!(L_PAREN), DESC)
.then(|p| p.expr())
.expect(t!(R_PAREN))
p.expect_d(t!(L_PAREN), DESC).expr().expect(t!(R_PAREN))
})
.alt(|p| {
p.primary_expr().zero_or_more(|p| {
Expand Down Expand Up @@ -1609,7 +1602,7 @@ impl ParserImpl<'_> {
fn for_expr(&mut self) -> &mut Self {
self.begin(FOR_EXPR)
.expect_d(t!(FOR_KW), Some("expression"))
.then(|p| p.quantifier())
.quantifier()
.begin_alt()
.alt(|p| {
p.expect(t!(OF_KW))
Expand All @@ -1622,12 +1615,12 @@ impl ParserImpl<'_> {
p.expect(t!(IDENT))
.zero_or_more(|p| p.expect(t!(COMMA)).expect(t!(IDENT)))
.expect(t!(IN_KW))
.then(|p| p.iterable())
.iterable()
})
.end_alt()
.expect(t!(COLON))
.expect(t!(L_PAREN))
.then(|p| p.boolean_expr())
.boolean_expr()
.expect(t!(R_PAREN))
.end()
}
Expand All @@ -1642,7 +1635,7 @@ impl ParserImpl<'_> {
/// ``
fn of_expr(&mut self) -> &mut Self {
self.begin(OF_EXPR)
.then(|p| p.quantifier())
.quantifier()
.expect(t!(OF_KW))
.begin_alt()
.alt(|p| {
Expand Down Expand Up @@ -1672,10 +1665,10 @@ impl ParserImpl<'_> {
fn with_expr(&mut self) -> &mut Self {
self.begin(WITH_EXPR)
.expect_d(t!(WITH_KW), Some("expression"))
.then(|p| p.with_declarations())
.with_declarations()
.expect(t!(COLON))
.expect(t!(L_PAREN))
.then(|p| p.boolean_expr())
.boolean_expr()
.expect(t!(R_PAREN))
.end()
}
Expand All @@ -1687,10 +1680,8 @@ impl ParserImpl<'_> {
///
fn with_declarations(&mut self) -> &mut Self {
self.begin(WITH_DECLS)
.then(|p| p.with_declaration())
.zero_or_more(|p| {
p.expect(t!(COMMA)).then(|p| p.with_declaration())
})
.with_declaration()
.zero_or_more(|p| p.expect(t!(COMMA)).with_declaration())
.end()
}

Expand All @@ -1700,11 +1691,7 @@ impl ParserImpl<'_> {
/// WITH_DECL := IDENT `=` EXPR
/// ```
fn with_declaration(&mut self) -> &mut Self {
self.begin(WITH_DECL)
.expect(t!(IDENT))
.expect(t!(EQUAL))
.then(|p| p.expr())
.end()
self.begin(WITH_DECL).expect(t!(IDENT)).expect(t!(EQUAL)).expr().end()
}

/// Parses quantifier.
Expand Down Expand Up @@ -1761,8 +1748,8 @@ impl ParserImpl<'_> {
fn boolean_expr_tuple(&mut self) -> &mut Self {
self.begin(BOOLEAN_EXPR_TUPLE)
.expect(t!(L_PAREN))
.then(|p| p.boolean_expr())
.zero_or_more(|p| p.expect(t!(COMMA)).then(|p| p.boolean_expr()))
.boolean_expr()
.zero_or_more(|p| p.expect(t!(COMMA)).boolean_expr())
.expect(t!(R_PAREN))
.end()
}
Expand All @@ -1775,8 +1762,8 @@ impl ParserImpl<'_> {
fn expr_tuple(&mut self) -> &mut Self {
self.begin(EXPR_TUPLE)
.expect(t!(L_PAREN))
.then(|p| p.expr())
.zero_or_more(|p| p.expect(t!(COMMA)).then(|p| p.expr()))
.expr()
.zero_or_more(|p| p.expect(t!(COMMA)).expr())
.expect(t!(R_PAREN))
.end()
}
Expand Down
19 changes: 10 additions & 9 deletions parser/src/parser/tests/testdata/basic-error-4.cst
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
SOURCE_FILE@0..26
ERROR@0..26
RULE_DECL@0..26
RULE_KW@0..4 "rule"
L_BRACE@4..5 "{"
NEWLINE@5..6 "\n"
WHITESPACE@6..7 "\t"
CONDITION_KW@7..16 "condition"
COLON@16..17 ":"
NEWLINE@17..18 "\n"
WHITESPACE@18..20 "\t\t"
TRUE_KW@20..24 "true"
ERROR@4..24
L_BRACE@4..5 "{"
NEWLINE@5..6 "\n"
WHITESPACE@6..7 "\t"
CONDITION_KW@7..16 "condition"
COLON@16..17 ":"
NEWLINE@17..18 "\n"
WHITESPACE@18..20 "\t\t"
TRUE_KW@20..24 "true"
NEWLINE@24..25 "\n"
R_BRACE@25..26 "}"

Expand Down
6 changes: 4 additions & 2 deletions parser/src/parser/tests/testdata/basic-error-4.cststream
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Begin { kind: SOURCE_FILE, span: Span(0..26) }
Begin { kind: ERROR, span: Span(0..26) }
Begin { kind: RULE_DECL, span: Span(0..26) }
Token { kind: RULE_KW, span: Span(0..4) }
Begin { kind: ERROR, span: Span(4..24) }
Token { kind: L_BRACE, span: Span(4..5) }
Error { message: "expecting identifier, found `{`", span: Span(4..5) }
Token { kind: NEWLINE, span: Span(5..6) }
Expand All @@ -10,7 +11,8 @@ Token { kind: COLON, span: Span(16..17) }
Token { kind: NEWLINE, span: Span(17..18) }
Token { kind: WHITESPACE, span: Span(18..20) }
Token { kind: TRUE_KW, span: Span(20..24) }
End { kind: ERROR, span: Span(4..24) }
Token { kind: NEWLINE, span: Span(24..25) }
Token { kind: R_BRACE, span: Span(25..26) }
End { kind: ERROR, span: Span(0..26) }
End { kind: RULE_DECL, span: Span(0..26) }
End { kind: SOURCE_FILE, span: Span(0..26) }
26 changes: 13 additions & 13 deletions parser/src/parser/tests/testdata/rule-tags-error-2.cst
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
SOURCE_FILE@0..36
ERROR@0..9
RULE_DECL@0..36
RULE_KW@0..4 "rule"
WHITESPACE@4..5 " "
IDENT@5..9 "test"
WHITESPACE@9..10 " "
ERROR@10..36
EQUAL@10..11 "="
WHITESPACE@11..12 " "
L_BRACE@12..13 "{"
NEWLINE@13..14 "\n"
WHITESPACE@14..16 " "
CONDITION_KW@16..25 "condition"
COLON@25..26 ":"
NEWLINE@26..27 "\n"
WHITESPACE@27..30 "\t "
TRUE_KW@30..34 "true"
WHITESPACE@9..10 " "
ERROR@10..34
EQUAL@10..11 "="
WHITESPACE@11..12 " "
L_BRACE@12..13 "{"
NEWLINE@13..14 "\n"
WHITESPACE@14..16 " "
CONDITION_KW@16..25 "condition"
COLON@25..26 ":"
NEWLINE@26..27 "\n"
WHITESPACE@27..30 "\t "
TRUE_KW@30..34 "true"
NEWLINE@34..35 "\n"
R_BRACE@35..36 "}"

Expand Down
8 changes: 4 additions & 4 deletions parser/src/parser/tests/testdata/rule-tags-error-2.cststream
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
Begin { kind: SOURCE_FILE, span: Span(0..36) }
Begin { kind: ERROR, span: Span(0..9) }
Begin { kind: RULE_DECL, span: Span(0..36) }
Token { kind: RULE_KW, span: Span(0..4) }
Token { kind: WHITESPACE, span: Span(4..5) }
Token { kind: IDENT, span: Span(5..9) }
End { kind: ERROR, span: Span(0..9) }
Token { kind: WHITESPACE, span: Span(9..10) }
Begin { kind: ERROR, span: Span(10..36) }
Begin { kind: ERROR, span: Span(10..34) }
Token { kind: EQUAL, span: Span(10..11) }
Error { message: "expecting `:` or `{`, found `=`", span: Span(10..11) }
Token { kind: WHITESPACE, span: Span(11..12) }
Expand All @@ -17,7 +16,8 @@ Token { kind: COLON, span: Span(25..26) }
Token { kind: NEWLINE, span: Span(26..27) }
Token { kind: WHITESPACE, span: Span(27..30) }
Token { kind: TRUE_KW, span: Span(30..34) }
End { kind: ERROR, span: Span(10..34) }
Token { kind: NEWLINE, span: Span(34..35) }
Token { kind: R_BRACE, span: Span(35..36) }
End { kind: ERROR, span: Span(10..36) }
End { kind: RULE_DECL, span: Span(0..36) }
End { kind: SOURCE_FILE, span: Span(0..36) }
Loading