Skip to content

Compactify syntax diagrams #1818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
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
7 changes: 4 additions & 3 deletions src/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,10 @@ attributes]. It has the following grammar:
r[attributes.meta.syntax]
```grammar,attributes
MetaItem ->
SimplePath
| SimplePath `=` Expression
| SimplePath `(` MetaSeq? `)`
SimplePath (
`=` Expression
| `(` MetaSeq? `)`
)?

MetaSeq ->
MetaItemInner ( `,` MetaItemInner )* `,`?
Expand Down
3 changes: 1 addition & 2 deletions src/comments.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ r[comments]
r[comments.syntax]
```grammar,lexer
@root LINE_COMMENT ->
`//` (~[`/` `!` LF] | `//`) ~LF*
| `//`
`//` ( ( ~[`/` `!` LF] | `//` ) ~LF* )?

BLOCK_COMMENT ->
`/*`
Expand Down
6 changes: 4 additions & 2 deletions src/expressions/array-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ r[expr.array.syntax]
ArrayExpression -> `[` ArrayElements? `]`

ArrayElements ->
Expression ( `,` Expression )* `,`?
| Expression `;` Expression
Expression (
( `,` Expression )* `,`?
| `;` Expression
)
```

r[expr.array.constructor]
Expand Down
3 changes: 1 addition & 2 deletions src/expressions/block-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ BlockExpression ->
`}`

Statements ->
Statement+
| Statement+ ExpressionWithoutBlock
Statement+ ExpressionWithoutBlock?
| ExpressionWithoutBlock
```

Expand Down
50 changes: 16 additions & 34 deletions src/expressions/operator-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@ r[expr.operator.borrow]
r[expr.operator.borrow.syntax]
```grammar,expressions
BorrowExpression ->
(`&`|`&&`) Expression
| (`&`|`&&`) `mut` Expression
| (`&`|`&&`) `raw` `const` Expression
| (`&`|`&&`) `raw` `mut` Expression
( `&` | `&&` )
( `mut` | `raw` ( `const` | `mut` ) )?
Expression
```

r[expr.operator.borrow.intro]
Expand Down Expand Up @@ -257,8 +256,7 @@ r[expr.negate]
r[expr.negate.syntax]
```grammar,expressions
NegationExpression ->
`-` Expression
| `!` Expression
( `-` | `!`) Expression
```

r[expr.negate.intro]
Expand Down Expand Up @@ -291,16 +289,9 @@ r[expr.arith-logic]
r[expr.arith-logic.syntax]
```grammar,expressions
ArithmeticOrLogicalExpression ->
Expression `+` Expression
| Expression `-` Expression
| Expression `*` Expression
| Expression `/` Expression
| Expression `%` Expression
| Expression `&` Expression
| Expression `|` Expression
| Expression `^` Expression
| Expression `<<` Expression
| Expression `>>` Expression
Expression (
`+` | `-` | `*` | `/` | `%` | `&` | `|` | `^` | `<<` | `>>`
) Expression
```

r[expr.arith-logic.intro]
Expand Down Expand Up @@ -354,12 +345,9 @@ r[expr.cmp]
r[expr.cmp.syntax]
```grammar,expressions
ComparisonExpression ->
Expression `==` Expression
| Expression `!=` Expression
| Expression `>` Expression
| Expression `<` Expression
| Expression `>=` Expression
| Expression `<=` Expression
Expression (
`==` | `!=` | `>` | `<` | `>=` | `<=`
) Expression
```

r[expr.cmp.intro]
Expand Down Expand Up @@ -413,8 +401,7 @@ r[expr.bool-logic]
r[expr.bool-logic.syntax]
```grammar,expressions
LazyBooleanExpression ->
Expression `||` Expression
| Expression `&&` Expression
Expression (`||` | `&&`) Expression
```

r[expr.bool-logic.intro]
Expand Down Expand Up @@ -809,16 +796,11 @@ r[expr.compound-assign]
r[expr.compound-assign.syntax]
```grammar,expressions
CompoundAssignmentExpression ->
Expression `+=` Expression
| Expression `-=` Expression
| Expression `*=` Expression
| Expression `/=` Expression
| Expression `%=` Expression
| Expression `&=` Expression
| Expression `|=` Expression
| Expression `^=` Expression
| Expression `<<=` Expression
| Expression `>>=` Expression
Expression (
`+=` | `-=` | `*=` | `/=` | `%=`
| `&=` | `|=` | `^=`
| `<<=` | `>>=`
) Expression
```

r[expr.compound-assign.intro]
Expand Down
19 changes: 10 additions & 9 deletions src/inline-assembly.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,21 @@ AsmOption ->
| `att_syntax`
| `raw`

RegOperand -> (ParamName `=`)?
(
DirSpec `(` RegSpec `)` Expression
| DualDirSpec `(` RegSpec `)` DualDirSpecExpression
| `sym` PathExpression
| `const` Expression
| `label` `{` Statements? `}`
RegOperand ->
(ParamName `=`)? (
(
DirSpec `(` RegSpec `)`
| `const`
) Expression
| DualDirSpec `(` RegSpec `)` DualDirSpecExpression
| `sym` PathExpression
| `label` `{` Statements? `}`
Comment on lines -76 to +84
Copy link
Contributor

@traviscross traviscross May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's an example of one that I debated on for awhile, in terms of whether the transformation was worth it. The question I asked myself was whether transforming it in this way added insight.

In the end, I decided I think it does actually. E.g., we need to decide on what syntax to use for injecting constant pointers -- e.g. whether const or sym is better for that -- and in that context, syntactically, it is interesting that Expression shares both const and e.g. in(<reg>) as common prefixes, but not sym.

)

ParamName -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER

DualDirSpecExpression ->
Expression
| Expression `=>` Expression
Expression ( `=>` Expression )?

RegSpec -> RegisterClass | ExplicitRegister

Expand Down
5 changes: 2 additions & 3 deletions src/items/external-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,8 @@ ExternBlock ->

ExternalItem ->
OuterAttribute* (
MacroInvocationSemi
| Visibility? StaticItem
| Visibility? Function
Visibility? ( StaticItem | Function )
| MacroInvocationSemi
)
```

Expand Down
8 changes: 5 additions & 3 deletions src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ ItemSafety -> `safe`[^extern-safe] | `unsafe`
Abi -> STRING_LITERAL | RAW_STRING_LITERAL

FunctionParameters ->
SelfParam `,`?
| (SelfParam `,`)? FunctionParam (`,` FunctionParam)* `,`?
SelfParam ( `,` FunctionParams? )?
| FunctionParams

SelfParam -> OuterAttribute* ( ShorthandSelf | TypedSelf )

ShorthandSelf -> (`&` | `&` Lifetime)? `mut`? `self`
ShorthandSelf -> ( `&` Lifetime? )? `mut`? `self`

TypedSelf -> `mut`? `self` `:` Type

FunctionParams -> FunctionParam ( `,` FunctionParam )* `,`?

FunctionParam -> OuterAttribute* ( FunctionParamPattern | `...` | Type[^fn-param-2015] )

FunctionParamPattern -> PatternNoTopAlt `:` ( Type | `...` )
Expand Down
9 changes: 4 additions & 5 deletions src/items/modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ r[items.mod]
r[items.mod.syntax]
```grammar,items
Module ->
`unsafe`? `mod` IDENTIFIER `;`
| `unsafe`? `mod` IDENTIFIER `{`
InnerAttribute*
Item*
`}`
`unsafe`? `mod` IDENTIFIER (
`{` InnerAttribute* Item* `}`
| `;`
)
```

r[items.mod.intro]
Expand Down
6 changes: 4 additions & 2 deletions src/items/use-declarations.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ r[items.use.syntax]
UseDeclaration -> `use` UseTree `;`

UseTree ->
(SimplePath? `::`)? `*`
| (SimplePath? `::`)? `{` (UseTree ( `,` UseTree )* `,`?)? `}`
( SimplePath? `::` )? (
`{` ( UseTree ( `,` UseTree )* `,`? )? `}`
| `*`
)
| SimplePath ( `as` ( IDENTIFIER | `_` ) )?
```

Expand Down
13 changes: 9 additions & 4 deletions src/macros-by-example.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ MacroRulesDefinition ->
`macro_rules` `!` IDENTIFIER MacroRulesDef

MacroRulesDef ->
`(` MacroRules `)` `;`
| `[` MacroRules `]` `;`
(
`(` MacroRules `)`
| `[` MacroRules `]`
) `;`
| `{` MacroRules `}`

MacroRules ->
Expand All @@ -25,8 +27,11 @@ MacroMatcher ->
MacroMatch ->
Token _except `$` and [delimiters][lex.token.delim]_
| MacroMatcher
| `$` ( IDENTIFIER_OR_KEYWORD _except `crate`_ | RAW_IDENTIFIER | `_` ) `:` MacroFragSpec
| `$` `(` MacroMatch+ `)` MacroRepSep? MacroRepOp
| `$` (
( IDENTIFIER_OR_KEYWORD _except `crate`_ | RAW_IDENTIFIER | `_` )
`:` MacroFragSpec
| `(` MacroMatch+ `)` MacroRepSep? MacroRepOp
)

MacroFragSpec ->
`block` | `expr` | `expr_2021` | `ident` | `item` | `lifetime` | `literal`
Expand Down
10 changes: 7 additions & 3 deletions src/macros.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,13 @@ TokenTree ->
Token _except [delimiters][lex.token.delim]_ | DelimTokenTree

MacroInvocationSemi ->
SimplePath `!` `(` TokenTree* `)` `;`
| SimplePath `!` `[` TokenTree* `]` `;`
| SimplePath `!` `{` TokenTree* `}`
SimplePath `!` (
(
`(` TokenTree* `)`
| `[` TokenTree* `]`
) `;`
| `{` TokenTree* `}`
)
```

r[macro.invocation.intro]
Expand Down
6 changes: 2 additions & 4 deletions src/paths.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,14 @@ PathIdentSegment ->
IDENTIFIER | `super` | `self` | `Self` | `crate` | `$crate`

GenericArgs ->
`<` `>`
| `<` ( GenericArg `,` )* GenericArg `,`? `>`
`<` ( GenericArg ( `,` GenericArg )* `,`? )? `>`

GenericArg ->
Lifetime | Type | GenericArgsConst | GenericArgsBinding | GenericArgsBounds

GenericArgsConst ->
BlockExpression
| LiteralExpression
| `-` LiteralExpression
| `-`? LiteralExpression
| SimplePathSegment

GenericArgsBinding ->
Expand Down
14 changes: 5 additions & 9 deletions src/patterns.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,8 +149,7 @@ LiteralPattern ->
| RAW_BYTE_STRING_LITERAL
| C_STRING_LITERAL
| RAW_C_STRING_LITERAL
| `-`? INTEGER_LITERAL
| `-`? FLOAT_LITERAL
| `-`? ( INTEGER_LITERAL | FLOAT_LITERAL )
```

r[patterns.literal.intro]
Expand Down Expand Up @@ -499,8 +498,7 @@ ObsoleteRangePattern ->
RangePatternBound ->
CHAR_LITERAL
| BYTE_LITERAL
| `-`? INTEGER_LITERAL
| `-`? FLOAT_LITERAL
| `-`? ( INTEGER_LITERAL | FLOAT_LITERAL )
| PathExpression
```

Expand Down Expand Up @@ -708,7 +706,7 @@ StructPattern ->
`}`

StructPatternElements ->
StructPatternFields (`,` | `,` StructPatternEtCetera)?
StructPatternFields ( `,` StructPatternEtCetera? )?
| StructPatternEtCetera

StructPatternFields ->
Expand All @@ -717,8 +715,7 @@ StructPatternFields ->
StructPatternField ->
OuterAttribute*
(
TUPLE_INDEX `:` Pattern
| IDENTIFIER `:` Pattern
( TUPLE_INDEX | IDENTIFIER ) `:` Pattern
| `ref`? `mut`? IDENTIFIER
)

Expand Down Expand Up @@ -837,9 +834,8 @@ r[patterns.tuple.syntax]
TuplePattern -> `(` TuplePatternItems? `)`

TuplePatternItems ->
Pattern `,`
Pattern ( `,` | ( `,` Pattern )+ `,`? )
| RestPattern
| Pattern (`,` Pattern)+ `,`?
```

r[patterns.tuple.intro]
Expand Down
7 changes: 5 additions & 2 deletions src/statements.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,11 @@ r[statement.let.syntax]
LetStatement ->
OuterAttribute* `let` PatternNoTopAlt ( `:` Type )?
(
`=` Expression
| `=` Expression _except [LazyBooleanExpression] or end with a `}`_ `else` BlockExpression
`=` (
Expression _except [LazyBooleanExpression] or end with a `}`_
`else` BlockExpression
| Expression
)
)? `;`
```

Expand Down
15 changes: 10 additions & 5 deletions src/tokens.md
Original file line number Diff line number Diff line change
Expand Up @@ -657,9 +657,11 @@ r[lex.token.literal.float]
r[lex.token.literal.float.syntax]
```grammar,lexer
FLOAT_LITERAL ->
DEC_LITERAL `.` _not immediately followed by `.`, `_` or an XID_Start character_
| DEC_LITERAL `.` DEC_LITERAL SUFFIX_NO_E?
| DEC_LITERAL (`.` DEC_LITERAL)? FLOAT_EXPONENT SUFFIX?
DEC_LITERAL (
`.` _not immediately followed by `.`, `_` or an XID_Start character_
| `.` DEC_LITERAL ( SUFFIX_NO_E? | FLOAT_EXPONENT SUFFIX? )
| FLOAT_EXPONENT SUFFIX?
)

FLOAT_EXPONENT ->
(`e`|`E`) (`+`|`-`)? (DEC_DIGIT|`_`)* DEC_DIGIT (DEC_DIGIT|`_`)*
Expand Down Expand Up @@ -714,8 +716,11 @@ r[lex.token.literal.reserved.syntax]
RESERVED_NUMBER ->
BIN_LITERAL [`2`-`9`]
| OCT_LITERAL [`8`-`9`]
| ( BIN_LITERAL | OCT_LITERAL | HEX_LITERAL ) `.` _not immediately followed by `.`, `_` or an XID_Start character_
| ( BIN_LITERAL | OCT_LITERAL ) (`e`|`E`)
| ( BIN_LITERAL | OCT_LITERAL ) (
`.` _not immediately followed by `.`, `_` or an XID_Start character_
| (`e`|`E`)
)
| HEX_LITERAL `.` _not immediately followed by `.`, `_` or an XID_Start character_
| `0b` `_`* <end of input or not BIN_DIGIT>
| `0o` `_`* <end of input or not OCT_DIGIT>
| `0x` `_`* <end of input or not HEX_DIGIT>
Expand Down
Loading