Skip to content

Commit 13e0083

Browse files
committed
Add other grammar compactions
Using a number of strategies, there are some other grammar compactions and regularizations that stand out as similarly possible, so let's do those. In doing these, we prefer to pick a factoring of the grammar that calls for the least lookahead or backtracking.
1 parent 5a4d768 commit 13e0083

File tree

7 files changed

+22
-16
lines changed

7 files changed

+22
-16
lines changed

src/comments.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ r[comments]
44
r[comments.syntax]
55
```grammar,lexer
66
@root LINE_COMMENT ->
7-
`//` (~[`/` `!` LF] | `//`) ~LF*
8-
| `//`
7+
`//` ( ( ~[`/` `!` LF] | `//` ) ~LF* )?
98
109
BLOCK_COMMENT ->
1110
`/*`

src/expressions/operator-expr.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,7 @@ r[expr.negate]
256256
r[expr.negate.syntax]
257257
```grammar,expressions
258258
NegationExpression ->
259-
`-` Expression
260-
| `!` Expression
259+
( `-` | `!`) Expression
261260
```
262261

263262
r[expr.negate.intro]

src/items/functions.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ ItemSafety -> `safe`[^extern-safe] | `unsafe`
1616
Abi -> STRING_LITERAL | RAW_STRING_LITERAL
1717
1818
FunctionParameters ->
19-
SelfParam `,`?
20-
| (SelfParam `,`)? FunctionParam (`,` FunctionParam)* `,`?
19+
SelfParam ( `,` FunctionParams? )?
20+
| FunctionParams
2121
2222
SelfParam -> OuterAttribute* ( ShorthandSelf | TypedSelf )
2323
2424
ShorthandSelf -> ( `&` Lifetime? )? `mut`? `self`
2525
2626
TypedSelf -> `mut`? `self` `:` Type
2727
28+
FunctionParams -> FunctionParam ( `,` FunctionParam )* `,`?
29+
2830
FunctionParam -> OuterAttribute* ( FunctionParamPattern | `...` | Type[^fn-param-2015] )
2931
3032
FunctionParamPattern -> PatternNoTopAlt `:` ( Type | `...` )

src/statements.md

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,11 @@ r[statement.let.syntax]
6161
LetStatement ->
6262
OuterAttribute* `let` PatternNoTopAlt ( `:` Type )?
6363
(
64-
`=` Expression
65-
| `=` Expression _except [LazyBooleanExpression] or end with a `}`_ `else` BlockExpression
64+
`=` (
65+
Expression _except [LazyBooleanExpression] or end with a `}`_
66+
`else` BlockExpression
67+
| Expression
68+
)
6669
)? `;`
6770
```
6871

src/tokens.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,8 +659,8 @@ r[lex.token.literal.float.syntax]
659659
FLOAT_LITERAL ->
660660
DEC_LITERAL (
661661
`.` _not immediately followed by `.`, `_` or an XID_Start character_
662-
| `.` DEC_LITERAL SUFFIX_NO_E?
663-
| ( `.` DEC_LITERAL )? FLOAT_EXPONENT SUFFIX?
662+
| `.` DEC_LITERAL ( SUFFIX_NO_E? | FLOAT_EXPONENT SUFFIX? )
663+
| FLOAT_EXPONENT SUFFIX?
664664
)
665665
666666
FLOAT_EXPONENT ->
@@ -716,8 +716,11 @@ r[lex.token.literal.reserved.syntax]
716716
RESERVED_NUMBER ->
717717
BIN_LITERAL [`2`-`9`]
718718
| OCT_LITERAL [`8`-`9`]
719-
| ( BIN_LITERAL | OCT_LITERAL | HEX_LITERAL ) `.` _not immediately followed by `.`, `_` or an XID_Start character_
720-
| ( BIN_LITERAL | OCT_LITERAL ) (`e`|`E`)
719+
| ( BIN_LITERAL | OCT_LITERAL ) (
720+
`.` _not immediately followed by `.`, `_` or an XID_Start character_
721+
| (`e`|`E`)
722+
)
723+
| HEX_LITERAL `.` _not immediately followed by `.`, `_` or an XID_Start character_
721724
| `0b` `_`* <end of input or not BIN_DIGIT>
722725
| `0o` `_`* <end of input or not OCT_DIGIT>
723726
| `0x` `_`* <end of input or not HEX_DIGIT>

src/trait-bounds.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ TypeParamBounds -> TypeParamBound ( `+` TypeParamBound )* `+`?
77
88
TypeParamBound -> Lifetime | TraitBound | UseBound
99
10-
TraitBound ->
11-
( `?` | ForLifetimes )? TypePath
12-
| `(` ( `?` | ForLifetimes )? TypePath `)`
10+
TraitBound -> `(` TraitBoundNoParens `)` | TraitBoundNoParens
11+
12+
TraitBoundNoParens -> ( `?` | ForLifetimes )? TypePath
1313
1414
LifetimeBounds -> ( Lifetime `+` )* Lifetime?
1515

src/types/function-pointer.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ MaybeNamedParam ->
2121
OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? Type
2222
2323
MaybeNamedFunctionParametersVariadic ->
24-
( MaybeNamedParam `,` )* MaybeNamedParam `,` OuterAttribute* `...`
24+
MaybeNamedParam ( `,` MaybeNamedParam )* `,` OuterAttribute* `...`
2525
```
2626

2727
r[type.fn-pointer.intro]

0 commit comments

Comments
 (0)