From 7d720ecf3ac46c641c3043dce633b71f8644fa87 Mon Sep 17 00:00:00 2001 From: Lukas Lueg Date: Thu, 8 May 2025 22:36:26 +0200 Subject: [PATCH 1/4] Compactify syntax diagrams --- src/attributes.md | 4 +--- src/expressions/array-expr.md | 3 +-- src/expressions/block-expr.md | 3 +-- src/expressions/operator-expr.md | 37 +++++--------------------------- src/inline-assembly.md | 8 +++---- src/items/external-blocks.md | 5 ++--- src/items/functions.md | 2 +- src/items/modules.md | 6 +----- src/items/use-declarations.md | 3 +-- src/macros-by-example.md | 11 +++++----- src/macros.md | 4 +--- src/paths.md | 6 ++---- src/patterns.md | 14 +++++------- src/tokens.md | 6 +++--- src/trait-bounds.md | 3 +-- src/types/tuple.md | 3 +-- src/visibility-and-privacy.md | 6 +----- 17 files changed, 37 insertions(+), 87 deletions(-) diff --git a/src/attributes.md b/src/attributes.md index d6cbed6137..df3ca82bd3 100644 --- a/src/attributes.md +++ b/src/attributes.md @@ -116,9 +116,7 @@ attributes]. It has the following grammar: r[attributes.meta.syntax] ```grammar,attributes MetaItem -> - SimplePath - | SimplePath `=` Expression - | SimplePath `(` MetaSeq? `)` + SimplePath ((`=` Expression) | (`(` MetaSeq? `)`))? MetaSeq -> MetaItemInner ( `,` MetaItemInner )* `,`? diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 04374f3f32..49ae66c6d0 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -8,8 +8,7 @@ r[expr.array.syntax] ArrayExpression -> `[` ArrayElements? `]` ArrayElements -> - Expression ( `,` Expression )* `,`? - | Expression `;` Expression + Expression (( `,` Expression )* `,`? | (`;` Expression )) ``` r[expr.array.constructor] diff --git a/src/expressions/block-expr.md b/src/expressions/block-expr.md index da0f93b368..cc971e4f98 100644 --- a/src/expressions/block-expr.md +++ b/src/expressions/block-expr.md @@ -10,8 +10,7 @@ BlockExpression -> `}` Statements -> - Statement+ - | Statement+ ExpressionWithoutBlock + Statement+ ExpressionWithoutBlock? | ExpressionWithoutBlock ``` diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 5151bd2c6b..dd37e99aa1 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -58,10 +58,7 @@ 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] @@ -291,16 +288,7 @@ 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] @@ -354,12 +342,7 @@ 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] @@ -413,8 +396,7 @@ r[expr.bool-logic] r[expr.bool-logic.syntax] ```grammar,expressions LazyBooleanExpression -> - Expression `||` Expression - | Expression `&&` Expression + Expression (`||` | `&&`) Expression ``` r[expr.bool-logic.intro] @@ -809,16 +791,7 @@ 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] diff --git a/src/inline-assembly.md b/src/inline-assembly.md index f1d19974bc..0274432f3b 100644 --- a/src/inline-assembly.md +++ b/src/inline-assembly.md @@ -75,18 +75,18 @@ AsmOption -> RegOperand -> (ParamName `=`)? ( - DirSpec `(` RegSpec `)` Expression + ((DirSpec `(` RegSpec `)`) + | `const` + ) Expression | DualDirSpec `(` RegSpec `)` DualDirSpecExpression | `sym` PathExpression - | `const` Expression | `label` `{` Statements? `}` ) ParamName -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER DualDirSpecExpression -> - Expression - | Expression `=>` Expression + Expression (`=>` Expression)? RegSpec -> RegisterClass | ExplicitRegister diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index 0053969a17..d336080e93 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -11,9 +11,8 @@ ExternBlock -> ExternalItem -> OuterAttribute* ( - MacroInvocationSemi - | Visibility? StaticItem - | Visibility? Function + Visibility? (StaticItem | Function) + | MacroInvocationSemi ) ``` diff --git a/src/items/functions.md b/src/items/functions.md index 806cacf04b..31409343e5 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -21,7 +21,7 @@ FunctionParameters -> SelfParam -> OuterAttribute* ( ShorthandSelf | TypedSelf ) -ShorthandSelf -> (`&` | `&` Lifetime)? `mut`? `self` +ShorthandSelf -> (`&` Lifetime?)? `mut`? `self` TypedSelf -> `mut`? `self` `:` Type diff --git a/src/items/modules.md b/src/items/modules.md index 295d1d7b59..7d701c3811 100644 --- a/src/items/modules.md +++ b/src/items/modules.md @@ -4,11 +4,7 @@ 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] diff --git a/src/items/use-declarations.md b/src/items/use-declarations.md index ec993d737b..998481cff9 100644 --- a/src/items/use-declarations.md +++ b/src/items/use-declarations.md @@ -6,8 +6,7 @@ r[items.use.syntax] UseDeclaration -> `use` UseTree `;` UseTree -> - (SimplePath? `::`)? `*` - | (SimplePath? `::`)? `{` (UseTree ( `,` UseTree )* `,`?)? `}` + (SimplePath? `::`)? (`*` | (`{` (UseTree ( `,` UseTree )* `,`?)? `}`)) | SimplePath ( `as` ( IDENTIFIER | `_` ) )? ``` diff --git a/src/macros-by-example.md b/src/macros-by-example.md index 9bdb93fce5..b596218efb 100644 --- a/src/macros-by-example.md +++ b/src/macros-by-example.md @@ -7,9 +7,10 @@ MacroRulesDefinition -> `macro_rules` `!` IDENTIFIER MacroRulesDef MacroRulesDef -> - `(` MacroRules `)` `;` - | `[` MacroRules `]` `;` - | `{` MacroRules `}` + ( (`(` MacroRules `)`) + | (`[` MacroRules `]`) + )`;` + | (`{` MacroRules `}`) MacroRules -> MacroRule ( `;` MacroRule )* `;`? @@ -25,8 +26,8 @@ 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` diff --git a/src/macros.md b/src/macros.md index 48555fb253..79f3b67b67 100644 --- a/src/macros.md +++ b/src/macros.md @@ -29,9 +29,7 @@ TokenTree -> Token _except [delimiters][lex.token.delim]_ | DelimTokenTree MacroInvocationSemi -> - SimplePath `!` `(` TokenTree* `)` `;` - | SimplePath `!` `[` TokenTree* `]` `;` - | SimplePath `!` `{` TokenTree* `}` + SimplePath `!` ( ( ( `(` TokenTree* `)` ) | ( `[` TokenTree* `]` )) `;` | ( `{` TokenTree* `}` )) ``` r[macro.invocation.intro] diff --git a/src/paths.md b/src/paths.md index afa5b82ec7..1dd1d615a7 100644 --- a/src/paths.md +++ b/src/paths.md @@ -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 -> diff --git a/src/patterns.md b/src/patterns.md index 2d9ddb5acb..8a6893914a 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -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] @@ -499,8 +498,7 @@ ObsoleteRangePattern -> RangePatternBound -> CHAR_LITERAL | BYTE_LITERAL - | `-`? INTEGER_LITERAL - | `-`? FLOAT_LITERAL + | `-`? (INTEGER_LITERAL | FLOAT_LITERAL) | PathExpression ``` @@ -708,7 +706,7 @@ StructPattern -> `}` StructPatternElements -> - StructPatternFields (`,` | `,` StructPatternEtCetera)? + StructPatternFields (`,` StructPatternEtCetera?)? | StructPatternEtCetera StructPatternFields -> @@ -717,8 +715,7 @@ StructPatternFields -> StructPatternField -> OuterAttribute* ( - TUPLE_INDEX `:` Pattern - | IDENTIFIER `:` Pattern + (TUPLE_INDEX | IDENTIFIER) `:` Pattern | `ref`? `mut`? IDENTIFIER ) @@ -837,9 +834,8 @@ r[patterns.tuple.syntax] TuplePattern -> `(` TuplePatternItems? `)` TuplePatternItems -> - Pattern `,` + Pattern (`,` | (`,` Pattern)+ `,`?) | RestPattern - | Pattern (`,` Pattern)+ `,`? ``` r[patterns.tuple.intro] diff --git a/src/tokens.md b/src/tokens.md index 294753e69b..70e9654dde 100644 --- a/src/tokens.md +++ b/src/tokens.md @@ -657,9 +657,9 @@ 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?) + | ((`.` DEC_LITERAL)? FLOAT_EXPONENT SUFFIX?)) FLOAT_EXPONENT -> (`e`|`E`) (`+`|`-`)? (DEC_DIGIT|`_`)* DEC_DIGIT (DEC_DIGIT|`_`)* diff --git a/src/trait-bounds.md b/src/trait-bounds.md index f31265fbc0..dfccba1be9 100644 --- a/src/trait-bounds.md +++ b/src/trait-bounds.md @@ -21,8 +21,7 @@ Lifetime -> UseBound -> `use` UseBoundGenericArgs UseBoundGenericArgs -> - `<` `>` - | `<` ( UseBoundGenericArg `,`)* UseBoundGenericArg `,`? `>` + `<` (( UseBoundGenericArg `,`)* UseBoundGenericArg `,`?)? `>` UseBoundGenericArg -> Lifetime diff --git a/src/types/tuple.md b/src/types/tuple.md index a686cfb8fc..f7c322e9dc 100644 --- a/src/types/tuple.md +++ b/src/types/tuple.md @@ -4,8 +4,7 @@ r[type.tuple] r[type.tuple.syntax] ```grammar,types TupleType -> - `(` `)` - | `(` ( Type `,` )+ Type? `)` + `(` (( Type `,` )+ Type?)? `)` ``` r[type.tuple.intro] diff --git a/src/visibility-and-privacy.md b/src/visibility-and-privacy.md index 847d469777..e5469be40b 100644 --- a/src/visibility-and-privacy.md +++ b/src/visibility-and-privacy.md @@ -4,11 +4,7 @@ r[vis] r[vis.syntax] ```grammar,items Visibility -> - `pub` - | `pub` `(` `crate` `)` - | `pub` `(` `self` `)` - | `pub` `(` `super` `)` - | `pub` `(` `in` SimplePath `)` + `pub` ( `(` (`crate` | `self` | `super` | (`in` SimplePath)) `)` )? ``` r[vis.intro] From eb020fc20d6f952374d8abc3ece6856ef1c4787a Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Mon, 12 May 2025 00:53:30 +0000 Subject: [PATCH 2/4] Add missing bypass on `BorrowExpression` In compacting grammar productions, the `BorrowExpression` production needs to preserve the case that allows `&$EXPR`, but did not. Let's fix that. --- src/expressions/operator-expr.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index dd37e99aa1..81bbc4bbef 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -58,7 +58,7 @@ r[expr.operator.borrow] r[expr.operator.borrow.syntax] ```grammar,expressions BorrowExpression -> - (`&`|`&&`) ( `mut` | (`raw` (`const` | `mut`))) Expression + (`&`|`&&`) ( `mut` | (`raw` (`const` | `mut`)))? Expression ``` r[expr.operator.borrow.intro] From 5a4d768a94428bdaf75d1a7cd98c5397fae8bcb1 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Mon, 12 May 2025 08:54:50 +0000 Subject: [PATCH 3/4] Rework grammar compactions Let's fix up the grammar compactions to follow the indentation and spacing style we use for this grammar (especially as the indentation is preserved into the rendering), remove superfluous parentheses, and add line breaks in alternations and elsewhere to add clarity. --- src/attributes.md | 5 ++++- src/expressions/array-expr.md | 5 ++++- src/expressions/block-expr.md | 2 +- src/expressions/operator-expr.md | 20 +++++++++++++++----- src/inline-assembly.md | 19 ++++++++++--------- src/items/external-blocks.md | 4 ++-- src/items/functions.md | 2 +- src/items/modules.md | 5 ++++- src/items/use-declarations.md | 5 ++++- src/macros-by-example.md | 16 ++++++++++------ src/macros.md | 8 +++++++- src/paths.md | 2 +- src/patterns.md | 10 +++++----- src/tokens.md | 8 +++++--- src/trait-bounds.md | 2 +- src/types/tuple.md | 2 +- src/visibility-and-privacy.md | 9 ++++++++- 17 files changed, 83 insertions(+), 41 deletions(-) diff --git a/src/attributes.md b/src/attributes.md index df3ca82bd3..887d184c9c 100644 --- a/src/attributes.md +++ b/src/attributes.md @@ -116,7 +116,10 @@ attributes]. It has the following grammar: r[attributes.meta.syntax] ```grammar,attributes MetaItem -> - SimplePath ((`=` Expression) | (`(` MetaSeq? `)`))? + SimplePath ( + `=` Expression + | `(` MetaSeq? `)` + )? MetaSeq -> MetaItemInner ( `,` MetaItemInner )* `,`? diff --git a/src/expressions/array-expr.md b/src/expressions/array-expr.md index 49ae66c6d0..622505f1a1 100644 --- a/src/expressions/array-expr.md +++ b/src/expressions/array-expr.md @@ -8,7 +8,10 @@ r[expr.array.syntax] ArrayExpression -> `[` ArrayElements? `]` ArrayElements -> - Expression (( `,` Expression )* `,`? | (`;` Expression )) + Expression ( + ( `,` Expression )* `,`? + | `;` Expression + ) ``` r[expr.array.constructor] diff --git a/src/expressions/block-expr.md b/src/expressions/block-expr.md index cc971e4f98..489d177c7e 100644 --- a/src/expressions/block-expr.md +++ b/src/expressions/block-expr.md @@ -10,7 +10,7 @@ BlockExpression -> `}` Statements -> - Statement+ ExpressionWithoutBlock? + Statement+ ExpressionWithoutBlock? | ExpressionWithoutBlock ``` diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 81bbc4bbef..114335c613 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -58,7 +58,9 @@ r[expr.operator.borrow] r[expr.operator.borrow.syntax] ```grammar,expressions BorrowExpression -> - (`&`|`&&`) ( `mut` | (`raw` (`const` | `mut`)))? Expression + ( `&` | `&&` ) + ( `mut` | `raw` ( `const` | `mut` ) )? + Expression ``` r[expr.operator.borrow.intro] @@ -288,7 +290,9 @@ r[expr.arith-logic] r[expr.arith-logic.syntax] ```grammar,expressions ArithmeticOrLogicalExpression -> - Expression ( `+` | `-` | `*` | `/` | `%` | `&` | `|` | `^` | `<<` | `>>` ) Expression + Expression ( + `+` | `-` | `*` | `/` | `%` | `&` | `|` | `^` | `<<` | `>>` + ) Expression ``` r[expr.arith-logic.intro] @@ -342,7 +346,9 @@ r[expr.cmp] r[expr.cmp.syntax] ```grammar,expressions ComparisonExpression -> - Expression ( `==` | `!=` | `>` | `<` | `>=` | `<=` ) Expression + Expression ( + `==` | `!=` | `>` | `<` | `>=` | `<=` + ) Expression ``` r[expr.cmp.intro] @@ -396,7 +402,7 @@ r[expr.bool-logic] r[expr.bool-logic.syntax] ```grammar,expressions LazyBooleanExpression -> - Expression (`||` | `&&`) Expression + Expression (`||` | `&&`) Expression ``` r[expr.bool-logic.intro] @@ -791,7 +797,11 @@ r[expr.compound-assign] r[expr.compound-assign.syntax] ```grammar,expressions CompoundAssignmentExpression -> - Expression (`+=` | `-=` | `*=` | `/=` | `%=` | `&=` | `|=` | `^=` | `<<=` | `>>=`) Expression + Expression ( + `+=` | `-=` | `*=` | `/=` | `%=` + | `&=` | `|=` | `^=` + | `<<=` | `>>=` + ) Expression ``` r[expr.compound-assign.intro] diff --git a/src/inline-assembly.md b/src/inline-assembly.md index 0274432f3b..33a8bfcce4 100644 --- a/src/inline-assembly.md +++ b/src/inline-assembly.md @@ -73,20 +73,21 @@ AsmOption -> | `att_syntax` | `raw` -RegOperand -> (ParamName `=`)? - ( - ((DirSpec `(` RegSpec `)`) - | `const` - ) Expression - | DualDirSpec `(` RegSpec `)` DualDirSpecExpression - | `sym` PathExpression - | `label` `{` Statements? `}` +RegOperand -> + (ParamName `=`)? ( + ( + DirSpec `(` RegSpec `)` + | `const` + ) Expression + | DualDirSpec `(` RegSpec `)` DualDirSpecExpression + | `sym` PathExpression + | `label` `{` Statements? `}` ) ParamName -> IDENTIFIER_OR_KEYWORD | RAW_IDENTIFIER DualDirSpecExpression -> - Expression (`=>` Expression)? + Expression ( `=>` Expression )? RegSpec -> RegisterClass | ExplicitRegister diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index d336080e93..4bd9e01502 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -11,8 +11,8 @@ ExternBlock -> ExternalItem -> OuterAttribute* ( - Visibility? (StaticItem | Function) - | MacroInvocationSemi + Visibility? ( StaticItem | Function ) + | MacroInvocationSemi ) ``` diff --git a/src/items/functions.md b/src/items/functions.md index 31409343e5..139e872a1c 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -21,7 +21,7 @@ FunctionParameters -> SelfParam -> OuterAttribute* ( ShorthandSelf | TypedSelf ) -ShorthandSelf -> (`&` Lifetime?)? `mut`? `self` +ShorthandSelf -> ( `&` Lifetime? )? `mut`? `self` TypedSelf -> `mut`? `self` `:` Type diff --git a/src/items/modules.md b/src/items/modules.md index 7d701c3811..7d7d1dccb1 100644 --- a/src/items/modules.md +++ b/src/items/modules.md @@ -4,7 +4,10 @@ r[items.mod] r[items.mod.syntax] ```grammar,items Module -> - `unsafe`? `mod` IDENTIFIER ( `;` | (`{` InnerAttribute* Item* `}` )) + `unsafe`? `mod` IDENTIFIER ( + `{` InnerAttribute* Item* `}` + | `;` + ) ``` r[items.mod.intro] diff --git a/src/items/use-declarations.md b/src/items/use-declarations.md index 998481cff9..c1f5dbb8dc 100644 --- a/src/items/use-declarations.md +++ b/src/items/use-declarations.md @@ -6,7 +6,10 @@ r[items.use.syntax] UseDeclaration -> `use` UseTree `;` UseTree -> - (SimplePath? `::`)? (`*` | (`{` (UseTree ( `,` UseTree )* `,`?)? `}`)) + ( SimplePath? `::` )? ( + `{` ( UseTree ( `,` UseTree )* `,`? )? `}` + | `*` + ) | SimplePath ( `as` ( IDENTIFIER | `_` ) )? ``` diff --git a/src/macros-by-example.md b/src/macros-by-example.md index b596218efb..f8c822d4b9 100644 --- a/src/macros-by-example.md +++ b/src/macros-by-example.md @@ -7,10 +7,11 @@ MacroRulesDefinition -> `macro_rules` `!` IDENTIFIER MacroRulesDef MacroRulesDef -> - ( (`(` MacroRules `)`) - | (`[` MacroRules `]`) - )`;` - | (`{` MacroRules `}`) + ( + `(` MacroRules `)` + | `[` MacroRules `]` + ) `;` + | `{` MacroRules `}` MacroRules -> MacroRule ( `;` MacroRule )* `;`? @@ -26,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` diff --git a/src/macros.md b/src/macros.md index 79f3b67b67..1e2073ee4e 100644 --- a/src/macros.md +++ b/src/macros.md @@ -29,7 +29,13 @@ TokenTree -> Token _except [delimiters][lex.token.delim]_ | DelimTokenTree MacroInvocationSemi -> - SimplePath `!` ( ( ( `(` TokenTree* `)` ) | ( `[` TokenTree* `]` )) `;` | ( `{` TokenTree* `}` )) + SimplePath `!` ( + ( + `(` TokenTree* `)` + | `[` TokenTree* `]` + ) `;` + | `{` TokenTree* `}` + ) ``` r[macro.invocation.intro] diff --git a/src/paths.md b/src/paths.md index 1dd1d615a7..e5ef466bc1 100644 --- a/src/paths.md +++ b/src/paths.md @@ -54,7 +54,7 @@ PathIdentSegment -> IDENTIFIER | `super` | `self` | `Self` | `crate` | `$crate` GenericArgs -> - `<` (( GenericArg `,` )* GenericArg `,`?)? `>` + `<` ( GenericArg ( `,` GenericArg )* `,`? )? `>` GenericArg -> Lifetime | Type | GenericArgsConst | GenericArgsBinding | GenericArgsBounds diff --git a/src/patterns.md b/src/patterns.md index 8a6893914a..ef51b974de 100644 --- a/src/patterns.md +++ b/src/patterns.md @@ -149,7 +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] @@ -498,7 +498,7 @@ ObsoleteRangePattern -> RangePatternBound -> CHAR_LITERAL | BYTE_LITERAL - | `-`? (INTEGER_LITERAL | FLOAT_LITERAL) + | `-`? ( INTEGER_LITERAL | FLOAT_LITERAL ) | PathExpression ``` @@ -706,7 +706,7 @@ StructPattern -> `}` StructPatternElements -> - StructPatternFields (`,` StructPatternEtCetera?)? + StructPatternFields ( `,` StructPatternEtCetera? )? | StructPatternEtCetera StructPatternFields -> @@ -715,7 +715,7 @@ StructPatternFields -> StructPatternField -> OuterAttribute* ( - (TUPLE_INDEX | IDENTIFIER) `:` Pattern + ( TUPLE_INDEX | IDENTIFIER ) `:` Pattern | `ref`? `mut`? IDENTIFIER ) @@ -834,7 +834,7 @@ r[patterns.tuple.syntax] TuplePattern -> `(` TuplePatternItems? `)` TuplePatternItems -> - Pattern (`,` | (`,` Pattern)+ `,`?) + Pattern ( `,` | ( `,` Pattern )+ `,`? ) | RestPattern ``` diff --git a/src/tokens.md b/src/tokens.md index 70e9654dde..d2f977e8ec 100644 --- a/src/tokens.md +++ b/src/tokens.md @@ -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 SUFFIX_NO_E?) - | ((`.` DEC_LITERAL)? FLOAT_EXPONENT SUFFIX?)) + DEC_LITERAL ( + `.` _not immediately followed by `.`, `_` or an XID_Start character_ + | `.` DEC_LITERAL SUFFIX_NO_E? + | ( `.` DEC_LITERAL )? FLOAT_EXPONENT SUFFIX? + ) FLOAT_EXPONENT -> (`e`|`E`) (`+`|`-`)? (DEC_DIGIT|`_`)* DEC_DIGIT (DEC_DIGIT|`_`)* diff --git a/src/trait-bounds.md b/src/trait-bounds.md index dfccba1be9..33829f061c 100644 --- a/src/trait-bounds.md +++ b/src/trait-bounds.md @@ -21,7 +21,7 @@ Lifetime -> UseBound -> `use` UseBoundGenericArgs UseBoundGenericArgs -> - `<` (( UseBoundGenericArg `,`)* UseBoundGenericArg `,`?)? `>` + `<` ( UseBoundGenericArg ( `,` UseBoundGenericArg )* `,`? )? `>` UseBoundGenericArg -> Lifetime diff --git a/src/types/tuple.md b/src/types/tuple.md index f7c322e9dc..51a2643b76 100644 --- a/src/types/tuple.md +++ b/src/types/tuple.md @@ -4,7 +4,7 @@ r[type.tuple] r[type.tuple.syntax] ```grammar,types TupleType -> - `(` (( Type `,` )+ Type?)? `)` + `(` ( ( Type `,` )+ Type? )? `)` ``` r[type.tuple.intro] diff --git a/src/visibility-and-privacy.md b/src/visibility-and-privacy.md index e5469be40b..e39ed5c81f 100644 --- a/src/visibility-and-privacy.md +++ b/src/visibility-and-privacy.md @@ -4,7 +4,14 @@ r[vis] r[vis.syntax] ```grammar,items Visibility -> - `pub` ( `(` (`crate` | `self` | `super` | (`in` SimplePath)) `)` )? + `pub` ( + `(` ( + `crate` + | `self` + | `super` + | `in` SimplePath + ) `)` + )? ``` r[vis.intro] From 13e008327bdc5b2e2a16522e8855934006d65e58 Mon Sep 17 00:00:00 2001 From: Travis Cross Date: Mon, 12 May 2025 08:57:39 +0000 Subject: [PATCH 4/4] 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. --- src/comments.md | 3 +-- src/expressions/operator-expr.md | 3 +-- src/items/functions.md | 6 ++++-- src/statements.md | 7 +++++-- src/tokens.md | 11 +++++++---- src/trait-bounds.md | 6 +++--- src/types/function-pointer.md | 2 +- 7 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/comments.md b/src/comments.md index 8ffa565518..c77e8b1d47 100644 --- a/src/comments.md +++ b/src/comments.md @@ -4,8 +4,7 @@ r[comments] r[comments.syntax] ```grammar,lexer @root LINE_COMMENT -> - `//` (~[`/` `!` LF] | `//`) ~LF* - | `//` + `//` ( ( ~[`/` `!` LF] | `//` ) ~LF* )? BLOCK_COMMENT -> `/*` diff --git a/src/expressions/operator-expr.md b/src/expressions/operator-expr.md index 114335c613..88d7149184 100644 --- a/src/expressions/operator-expr.md +++ b/src/expressions/operator-expr.md @@ -256,8 +256,7 @@ r[expr.negate] r[expr.negate.syntax] ```grammar,expressions NegationExpression -> - `-` Expression - | `!` Expression + ( `-` | `!`) Expression ``` r[expr.negate.intro] diff --git a/src/items/functions.md b/src/items/functions.md index 139e872a1c..873e2a2a2e 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -16,8 +16,8 @@ ItemSafety -> `safe`[^extern-safe] | `unsafe` Abi -> STRING_LITERAL | RAW_STRING_LITERAL FunctionParameters -> - SelfParam `,`? - | (SelfParam `,`)? FunctionParam (`,` FunctionParam)* `,`? + SelfParam ( `,` FunctionParams? )? + | FunctionParams SelfParam -> OuterAttribute* ( ShorthandSelf | TypedSelf ) @@ -25,6 +25,8 @@ ShorthandSelf -> ( `&` Lifetime? )? `mut`? `self` TypedSelf -> `mut`? `self` `:` Type +FunctionParams -> FunctionParam ( `,` FunctionParam )* `,`? + FunctionParam -> OuterAttribute* ( FunctionParamPattern | `...` | Type[^fn-param-2015] ) FunctionParamPattern -> PatternNoTopAlt `:` ( Type | `...` ) diff --git a/src/statements.md b/src/statements.md index 8b10f0971e..fe0c4ce5c7 100644 --- a/src/statements.md +++ b/src/statements.md @@ -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 + ) )? `;` ``` diff --git a/src/tokens.md b/src/tokens.md index d2f977e8ec..4f0675df1c 100644 --- a/src/tokens.md +++ b/src/tokens.md @@ -659,8 +659,8 @@ r[lex.token.literal.float.syntax] FLOAT_LITERAL -> DEC_LITERAL ( `.` _not immediately followed by `.`, `_` or an XID_Start character_ - | `.` DEC_LITERAL SUFFIX_NO_E? - | ( `.` DEC_LITERAL )? FLOAT_EXPONENT SUFFIX? + | `.` DEC_LITERAL ( SUFFIX_NO_E? | FLOAT_EXPONENT SUFFIX? ) + | FLOAT_EXPONENT SUFFIX? ) FLOAT_EXPONENT -> @@ -716,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` `_`* | `0o` `_`* | `0x` `_`* diff --git a/src/trait-bounds.md b/src/trait-bounds.md index 33829f061c..ffaa4b849f 100644 --- a/src/trait-bounds.md +++ b/src/trait-bounds.md @@ -7,9 +7,9 @@ TypeParamBounds -> TypeParamBound ( `+` TypeParamBound )* `+`? TypeParamBound -> Lifetime | TraitBound | UseBound -TraitBound -> - ( `?` | ForLifetimes )? TypePath - | `(` ( `?` | ForLifetimes )? TypePath `)` +TraitBound -> `(` TraitBoundNoParens `)` | TraitBoundNoParens + +TraitBoundNoParens -> ( `?` | ForLifetimes )? TypePath LifetimeBounds -> ( Lifetime `+` )* Lifetime? diff --git a/src/types/function-pointer.md b/src/types/function-pointer.md index 03c8793662..b69c5ed30b 100644 --- a/src/types/function-pointer.md +++ b/src/types/function-pointer.md @@ -21,7 +21,7 @@ MaybeNamedParam -> OuterAttribute* ( ( IDENTIFIER | `_` ) `:` )? Type MaybeNamedFunctionParametersVariadic -> - ( MaybeNamedParam `,` )* MaybeNamedParam `,` OuterAttribute* `...` + MaybeNamedParam ( `,` MaybeNamedParam )* `,` OuterAttribute* `...` ``` r[type.fn-pointer.intro]