Skip to content

Commit 66ad9ac

Browse files
committed
refactor: simplify the parser by removing calls to then.
Many parser rules can be expressed in a simpler way by removing unnecessary calls to `then`.
1 parent a72c219 commit 66ad9ac

5 files changed

Lines changed: 61 additions & 71 deletions

File tree

parser/src/parser/mod.rs

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -844,7 +844,7 @@ impl<'src> ParserImpl<'src> {
844844
where
845845
P: Fn(&mut Self) -> &mut Self,
846846
{
847-
if matches!(self.state, State::OutOfFuel) {
847+
if matches!(self.state, State::Failure | State::OutOfFuel) {
848848
return self;
849849
}
850850

@@ -1111,7 +1111,7 @@ impl ParserImpl<'_> {
11111111
.expect(t!(L_BRACE))
11121112
.if_next(t!(META_KW), |p| p.meta_blk())
11131113
.if_next(t!(STRINGS_KW), |p| p.patterns_blk())
1114-
.then(|p| p.condition_blk())
1114+
.condition_blk()
11151115
.expect(t!(R_BRACE))
11161116
.end()
11171117
.recover(t!(GLOBAL_KW
@@ -1289,7 +1289,7 @@ impl ParserImpl<'_> {
12891289
self.begin(HEX_PATTERN)
12901290
.expect(t!(L_BRACE))
12911291
.enter_hex_pattern_mode()
1292-
.then(|p| p.hex_sub_pattern())
1292+
.hex_sub_pattern()
12931293
.expect(t!(R_BRACE))
12941294
.end()
12951295
}
@@ -1324,8 +1324,8 @@ impl ParserImpl<'_> {
13241324
fn hex_alternative(&mut self) -> &mut Self {
13251325
self.begin(HEX_ALTERNATIVE)
13261326
.expect(t!(L_PAREN))
1327-
.then(|p| p.hex_sub_pattern())
1328-
.zero_or_more(|p| p.expect(t!(PIPE)).then(|p| p.hex_sub_pattern()))
1327+
.hex_sub_pattern()
1328+
.zero_or_more(|p| p.expect(t!(PIPE)).hex_sub_pattern())
13291329
.expect(t!(R_PAREN))
13301330
.end()
13311331
}
@@ -1374,8 +1374,7 @@ impl ParserImpl<'_> {
13741374
self.begin(BOOLEAN_EXPR)
13751375
.boolean_term()
13761376
.zero_or_more(|p| {
1377-
p.expect_d(t!(AND_KW | OR_KW), Some("operator"))
1378-
.then(|p| p.boolean_term())
1377+
p.expect_d(t!(AND_KW | OR_KW), Some("operator")).boolean_term()
13791378
})
13801379
.end()
13811380
}
@@ -1408,10 +1407,7 @@ impl ParserImpl<'_> {
14081407
)
14091408
})
14101409
.alt(|p| p.expect_d(t!(TRUE_KW | FALSE_KW), DESC))
1411-
.alt(|p| {
1412-
p.expect_d(t!(NOT_KW | DEFINED_KW), DESC)
1413-
.then(|p| p.boolean_term())
1414-
})
1410+
.alt(|p| p.expect_d(t!(NOT_KW | DEFINED_KW), DESC).boolean_term())
14151411
.alt(|p| p.for_expr())
14161412
.alt(|p| p.of_expr())
14171413
.alt(|p| p.with_expr())
@@ -1434,12 +1430,12 @@ impl ParserImpl<'_> {
14341430
| MATCHES_KW),
14351431
DESC,
14361432
)
1437-
.then(|p| p.expr())
1433+
.expr()
14381434
})
14391435
})
14401436
.alt(|p| {
14411437
p.expect_d(t!(L_PAREN), DESC)
1442-
.then(|p| p.boolean_expr())
1438+
.boolean_expr()
14431439
.expect(t!(R_PAREN))
14441440
})
14451441
.end_alt()
@@ -1471,7 +1467,7 @@ impl ParserImpl<'_> {
14711467
| BITWISE_XOR),
14721468
Some("operator"),
14731469
)
1474-
.then(|p| p.term())
1470+
.term()
14751471
})
14761472
.end()
14771473
})
@@ -1487,9 +1483,8 @@ impl ParserImpl<'_> {
14871483
.expect(t!(IDENT))
14881484
.expect(t!(L_PAREN))
14891485
.opt(|p| {
1490-
p.boolean_expr().zero_or_more(|p| {
1491-
p.expect(t!(COMMA)).then(|p| p.boolean_expr())
1492-
})
1486+
p.boolean_expr()
1487+
.zero_or_more(|p| p.expect(t!(COMMA)).boolean_expr())
14931488
})
14941489
.expect(t!(R_PAREN))
14951490
.end()
@@ -1503,10 +1498,10 @@ impl ParserImpl<'_> {
15031498
fn range(&mut self) -> &mut Self {
15041499
self.begin(RANGE)
15051500
.expect(t!(L_PAREN))
1506-
.then(|p| p.expr())
1501+
.expr()
15071502
.expect(t!(DOT))
15081503
.expect(t!(DOT))
1509-
.then(|p| p.expr())
1504+
.expr()
15101505
.expect(t!(R_PAREN))
15111506
.end()
15121507
}
@@ -1557,12 +1552,10 @@ impl ParserImpl<'_> {
15571552
p.expr().expect(t!(R_BRACKET))
15581553
})
15591554
})
1560-
.alt(|p| p.expect_d(t!(MINUS), DESC).then(|p| p.term()))
1561-
.alt(|p| p.expect_d(t!(BITWISE_NOT), DESC).then(|p| p.term()))
1555+
.alt(|p| p.expect_d(t!(MINUS), DESC).term())
1556+
.alt(|p| p.expect_d(t!(BITWISE_NOT), DESC).term())
15621557
.alt(|p| {
1563-
p.expect_d(t!(L_PAREN), DESC)
1564-
.then(|p| p.expr())
1565-
.expect(t!(R_PAREN))
1558+
p.expect_d(t!(L_PAREN), DESC).expr().expect(t!(R_PAREN))
15661559
})
15671560
.alt(|p| {
15681561
p.primary_expr().zero_or_more(|p| {
@@ -1609,7 +1602,7 @@ impl ParserImpl<'_> {
16091602
fn for_expr(&mut self) -> &mut Self {
16101603
self.begin(FOR_EXPR)
16111604
.expect_d(t!(FOR_KW), Some("expression"))
1612-
.then(|p| p.quantifier())
1605+
.quantifier()
16131606
.begin_alt()
16141607
.alt(|p| {
16151608
p.expect(t!(OF_KW))
@@ -1622,12 +1615,12 @@ impl ParserImpl<'_> {
16221615
p.expect(t!(IDENT))
16231616
.zero_or_more(|p| p.expect(t!(COMMA)).expect(t!(IDENT)))
16241617
.expect(t!(IN_KW))
1625-
.then(|p| p.iterable())
1618+
.iterable()
16261619
})
16271620
.end_alt()
16281621
.expect(t!(COLON))
16291622
.expect(t!(L_PAREN))
1630-
.then(|p| p.boolean_expr())
1623+
.boolean_expr()
16311624
.expect(t!(R_PAREN))
16321625
.end()
16331626
}
@@ -1642,7 +1635,7 @@ impl ParserImpl<'_> {
16421635
/// ``
16431636
fn of_expr(&mut self) -> &mut Self {
16441637
self.begin(OF_EXPR)
1645-
.then(|p| p.quantifier())
1638+
.quantifier()
16461639
.expect(t!(OF_KW))
16471640
.begin_alt()
16481641
.alt(|p| {
@@ -1672,10 +1665,10 @@ impl ParserImpl<'_> {
16721665
fn with_expr(&mut self) -> &mut Self {
16731666
self.begin(WITH_EXPR)
16741667
.expect_d(t!(WITH_KW), Some("expression"))
1675-
.then(|p| p.with_declarations())
1668+
.with_declarations()
16761669
.expect(t!(COLON))
16771670
.expect(t!(L_PAREN))
1678-
.then(|p| p.boolean_expr())
1671+
.boolean_expr()
16791672
.expect(t!(R_PAREN))
16801673
.end()
16811674
}
@@ -1687,10 +1680,8 @@ impl ParserImpl<'_> {
16871680
///
16881681
fn with_declarations(&mut self) -> &mut Self {
16891682
self.begin(WITH_DECLS)
1690-
.then(|p| p.with_declaration())
1691-
.zero_or_more(|p| {
1692-
p.expect(t!(COMMA)).then(|p| p.with_declaration())
1693-
})
1683+
.with_declaration()
1684+
.zero_or_more(|p| p.expect(t!(COMMA)).with_declaration())
16941685
.end()
16951686
}
16961687

@@ -1700,11 +1691,7 @@ impl ParserImpl<'_> {
17001691
/// WITH_DECL := IDENT `=` EXPR
17011692
/// ```
17021693
fn with_declaration(&mut self) -> &mut Self {
1703-
self.begin(WITH_DECL)
1704-
.expect(t!(IDENT))
1705-
.expect(t!(EQUAL))
1706-
.then(|p| p.expr())
1707-
.end()
1694+
self.begin(WITH_DECL).expect(t!(IDENT)).expect(t!(EQUAL)).expr().end()
17081695
}
17091696

17101697
/// Parses quantifier.
@@ -1761,8 +1748,8 @@ impl ParserImpl<'_> {
17611748
fn boolean_expr_tuple(&mut self) -> &mut Self {
17621749
self.begin(BOOLEAN_EXPR_TUPLE)
17631750
.expect(t!(L_PAREN))
1764-
.then(|p| p.boolean_expr())
1765-
.zero_or_more(|p| p.expect(t!(COMMA)).then(|p| p.boolean_expr()))
1751+
.boolean_expr()
1752+
.zero_or_more(|p| p.expect(t!(COMMA)).boolean_expr())
17661753
.expect(t!(R_PAREN))
17671754
.end()
17681755
}
@@ -1775,8 +1762,8 @@ impl ParserImpl<'_> {
17751762
fn expr_tuple(&mut self) -> &mut Self {
17761763
self.begin(EXPR_TUPLE)
17771764
.expect(t!(L_PAREN))
1778-
.then(|p| p.expr())
1779-
.zero_or_more(|p| p.expect(t!(COMMA)).then(|p| p.expr()))
1765+
.expr()
1766+
.zero_or_more(|p| p.expect(t!(COMMA)).expr())
17801767
.expect(t!(R_PAREN))
17811768
.end()
17821769
}

parser/src/parser/tests/testdata/basic-error-4.cst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
SOURCE_FILE@0..26
2-
ERROR@0..26
2+
RULE_DECL@0..26
33
RULE_KW@0..4 "rule"
4-
L_BRACE@4..5 "{"
5-
NEWLINE@5..6 "\n"
6-
WHITESPACE@6..7 "\t"
7-
CONDITION_KW@7..16 "condition"
8-
COLON@16..17 ":"
9-
NEWLINE@17..18 "\n"
10-
WHITESPACE@18..20 "\t\t"
11-
TRUE_KW@20..24 "true"
4+
ERROR@4..24
5+
L_BRACE@4..5 "{"
6+
NEWLINE@5..6 "\n"
7+
WHITESPACE@6..7 "\t"
8+
CONDITION_KW@7..16 "condition"
9+
COLON@16..17 ":"
10+
NEWLINE@17..18 "\n"
11+
WHITESPACE@18..20 "\t\t"
12+
TRUE_KW@20..24 "true"
1213
NEWLINE@24..25 "\n"
1314
R_BRACE@25..26 "}"
1415

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
Begin { kind: SOURCE_FILE, span: Span(0..26) }
2-
Begin { kind: ERROR, span: Span(0..26) }
2+
Begin { kind: RULE_DECL, span: Span(0..26) }
33
Token { kind: RULE_KW, span: Span(0..4) }
4+
Begin { kind: ERROR, span: Span(4..24) }
45
Token { kind: L_BRACE, span: Span(4..5) }
56
Error { message: "expecting identifier, found `{`", span: Span(4..5) }
67
Token { kind: NEWLINE, span: Span(5..6) }
@@ -10,7 +11,8 @@ Token { kind: COLON, span: Span(16..17) }
1011
Token { kind: NEWLINE, span: Span(17..18) }
1112
Token { kind: WHITESPACE, span: Span(18..20) }
1213
Token { kind: TRUE_KW, span: Span(20..24) }
14+
End { kind: ERROR, span: Span(4..24) }
1315
Token { kind: NEWLINE, span: Span(24..25) }
1416
Token { kind: R_BRACE, span: Span(25..26) }
15-
End { kind: ERROR, span: Span(0..26) }
17+
End { kind: RULE_DECL, span: Span(0..26) }
1618
End { kind: SOURCE_FILE, span: Span(0..26) }

parser/src/parser/tests/testdata/rule-tags-error-2.cst

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
SOURCE_FILE@0..36
2-
ERROR@0..9
2+
RULE_DECL@0..36
33
RULE_KW@0..4 "rule"
44
WHITESPACE@4..5 " "
55
IDENT@5..9 "test"
6-
WHITESPACE@9..10 " "
7-
ERROR@10..36
8-
EQUAL@10..11 "="
9-
WHITESPACE@11..12 " "
10-
L_BRACE@12..13 "{"
11-
NEWLINE@13..14 "\n"
12-
WHITESPACE@14..16 " "
13-
CONDITION_KW@16..25 "condition"
14-
COLON@25..26 ":"
15-
NEWLINE@26..27 "\n"
16-
WHITESPACE@27..30 "\t "
17-
TRUE_KW@30..34 "true"
6+
WHITESPACE@9..10 " "
7+
ERROR@10..34
8+
EQUAL@10..11 "="
9+
WHITESPACE@11..12 " "
10+
L_BRACE@12..13 "{"
11+
NEWLINE@13..14 "\n"
12+
WHITESPACE@14..16 " "
13+
CONDITION_KW@16..25 "condition"
14+
COLON@25..26 ":"
15+
NEWLINE@26..27 "\n"
16+
WHITESPACE@27..30 "\t "
17+
TRUE_KW@30..34 "true"
1818
NEWLINE@34..35 "\n"
1919
R_BRACE@35..36 "}"
2020

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Begin { kind: SOURCE_FILE, span: Span(0..36) }
2-
Begin { kind: ERROR, span: Span(0..9) }
2+
Begin { kind: RULE_DECL, span: Span(0..36) }
33
Token { kind: RULE_KW, span: Span(0..4) }
44
Token { kind: WHITESPACE, span: Span(4..5) }
55
Token { kind: IDENT, span: Span(5..9) }
6-
End { kind: ERROR, span: Span(0..9) }
76
Token { kind: WHITESPACE, span: Span(9..10) }
8-
Begin { kind: ERROR, span: Span(10..36) }
7+
Begin { kind: ERROR, span: Span(10..34) }
98
Token { kind: EQUAL, span: Span(10..11) }
109
Error { message: "expecting `:` or `{`, found `=`", span: Span(10..11) }
1110
Token { kind: WHITESPACE, span: Span(11..12) }
@@ -17,7 +16,8 @@ Token { kind: COLON, span: Span(25..26) }
1716
Token { kind: NEWLINE, span: Span(26..27) }
1817
Token { kind: WHITESPACE, span: Span(27..30) }
1918
Token { kind: TRUE_KW, span: Span(30..34) }
19+
End { kind: ERROR, span: Span(10..34) }
2020
Token { kind: NEWLINE, span: Span(34..35) }
2121
Token { kind: R_BRACE, span: Span(35..36) }
22-
End { kind: ERROR, span: Span(10..36) }
22+
End { kind: RULE_DECL, span: Span(0..36) }
2323
End { kind: SOURCE_FILE, span: Span(0..36) }

0 commit comments

Comments
 (0)