Skip to content

Commit daa84b9

Browse files
committed
Add more correct grammar for cfg_select!
Macros can be renamed when imported and can delimit their bodies with different tokens, so we don't want to document the outer part of the call; we just want to document the input to the macro. Doing this for `cfg_select!` is a bit tricky since an expression with a block doesn't need to be followed by a comma while an expression without a block does unless it's the last one. We don't want to handle this the way that the `match` grammar does, currently, since we want to move toward finite lookahead. So, instead, we handle this with right recursion in `CfgSelectArms`. Unlike `match`, outer attributes aren't allowed on the RHS expressions. To handle this, we need to refactor `ExpressionWithoutBlock` and `ExpressionWithBlock`. We also document that the outer braces are removed during expansion when the payload is a block expression. (We called it the arm "payload" after running out of other options.)
1 parent c5067fe commit daa84b9

File tree

2 files changed

+43
-40
lines changed

2 files changed

+43
-40
lines changed

src/conditional-compilation.md

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -471,19 +471,22 @@ r[cfg.cfg_select]
471471

472472
r[cfg.cfg_select.syntax]
473473
```grammar,configuration
474-
CfgSelect ->
475-
cfg_select! `{` CfgSelectBranch* `}`
474+
@root CfgSelect -> CfgSelectArms?
475+
476+
CfgSelectArms ->
477+
CfgSelectConfigurationPredicate `=>`
478+
(
479+
`{` ^ TokenTree `}` CfgSelectArms?
480+
| ExpressionWithBlockNoAttrs `,`? CfgSelectArms?
481+
| ExpressionWithoutBlockNoAttrs ( `,` CfgSelectArms? )?
482+
)
476483
477484
CfgSelectConfigurationPredicate ->
478485
ConfigurationPredicate | `_`
479-
480-
CfgSelectBranch ->
481-
CfgSelectConfigurationPredicate `=>` `{` TokenTree `}`
482-
| CfgSelectConfigurationPredicate `=>` TokenTree `,`
483486
```
484487

485488
r[cfg.cfg_select.general]
486-
The built-in `cfg_select` macro expands to the `TokenTree` on the right-hand side of the first configuration predicate that evaluates to `true`.
489+
The built-in `cfg_select` macro expands to the arm payload of the first configuration predicate that evaluates to `true`. If the payload is wrapped in curly braces, those are removed during expansion.
487490

488491
For example:
489492

src/expressions.md

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,41 @@ Expression ->
88
| ExpressionWithBlock
99
1010
ExpressionWithoutBlock ->
11-
OuterAttribute*
12-
(
13-
LiteralExpression
14-
| PathExpression
15-
| OperatorExpression
16-
| GroupedExpression
17-
| ArrayExpression
18-
| AwaitExpression
19-
| IndexExpression
20-
| TupleExpression
21-
| TupleIndexingExpression
22-
| StructExpression
23-
| CallExpression
24-
| MethodCallExpression
25-
| FieldExpression
26-
| ClosureExpression
27-
| AsyncBlockExpression
28-
| ContinueExpression
29-
| BreakExpression
30-
| RangeExpression
31-
| ReturnExpression
32-
| UnderscoreExpression
33-
| MacroInvocation
34-
)
11+
OuterAttribute* ExpressionWithoutBlockNoAttrs
12+
13+
ExpressionWithoutBlockNoAttrs ->
14+
LiteralExpression
15+
| PathExpression
16+
| OperatorExpression
17+
| GroupedExpression
18+
| ArrayExpression
19+
| AwaitExpression
20+
| IndexExpression
21+
| TupleExpression
22+
| TupleIndexingExpression
23+
| StructExpression
24+
| CallExpression
25+
| MethodCallExpression
26+
| FieldExpression
27+
| ClosureExpression
28+
| AsyncBlockExpression
29+
| ContinueExpression
30+
| BreakExpression
31+
| RangeExpression
32+
| ReturnExpression
33+
| UnderscoreExpression
34+
| MacroInvocation
3535
3636
ExpressionWithBlock ->
37-
OuterAttribute*
38-
(
39-
BlockExpression
40-
| ConstBlockExpression
41-
| UnsafeBlockExpression
42-
| LoopExpression
43-
| IfExpression
44-
| MatchExpression
45-
)
37+
OuterAttribute* ExpressionWithBlockNoAttrs
38+
39+
ExpressionWithBlockNoAttrs ->
40+
BlockExpression
41+
| ConstBlockExpression
42+
| UnsafeBlockExpression
43+
| LoopExpression
44+
| IfExpression
45+
| MatchExpression
4646
```
4747

4848
r[expr.intro]

0 commit comments

Comments
 (0)