3 // This grammar specifies the structure of Rust's concrete syntax tree.
4 // It does not specify parsing rules (ambiguities, precedence, etc are out of scope).
5 // Tokens are processed -- contextual keywords are recognised, compound operators glued.
10 // Name = -- non-terminal definition
11 // 'ident' -- token (terminal)
13 // A | B -- alternation
14 // A* -- zero or more repetition
15 // A? -- zero or one repetition
17 // label:A -- suggested name for field of AST node
19 //*************************//
20 // Names, Paths and Macros //
21 //*************************//
27 'ident' | 'int_number' | 'self' | 'super' | 'crate' | 'Self'
33 (qualifier:Path '::')? segment:PathSegment
37 | NameRef GenericArgList?
38 | NameRef ParamList RetType?
39 | '<' PathType ('as' PathType)? '>'
42 '::'? '<' (GenericArg (',' GenericArg)* ','?)? '>'
54 NameRef GenericArgList? (':' TypeBoundList | ('=' Type | ConstArg))
63 Attr* Path '!' TokenTree ';'?
77 //*************************//
79 //*************************//
106 'macro_rules' '!' Name
111 'macro' Name args:TokenTree?
124 'extern' 'crate' NameRef Rename? ';'
134 (Path? '::')? ('*' | UseTreeList)
138 '{' (UseTree (',' UseTree)* ','?)? '}'
142 'default'? 'const'? 'async'? 'unsafe'? Abi?
143 'fn' Name GenericParamList? ParamList RetType? WhereClause?
144 (body:BlockExpr | ';')
152 | (SelfParam ',')? (Param (',' Param)* ','?)?
154 | '|' (Param (',' Param)* ','?)? '|'
158 ('&' Lifetime?)? 'mut'? Name
159 | 'mut'? Name ':' Type
175 'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause?
180 'struct' Name GenericParamList? (
181 WhereClause? (RecordFieldList | ';')
182 | TupleFieldList WhereClause? ';'
186 '{' fields:(RecordField (',' RecordField)* ','?)? '}'
193 '(' fields:(TupleField (',' TupleField)* ','?)? ')'
205 'enum' Name GenericParamList? WhereClause?
209 '{' (Variant (',' Variant)* ','?)? '}'
213 Name FieldList? ('=' Expr)?
217 'union' Name GenericParamList? WhereClause?
222 // Not used directly in the grammar, but handy to have anyway.
231 'const' (Name | '_') ':' Type
236 'static' 'mut'? Name ':' Type
242 'trait' Name GenericParamList?
244 (':' TypeBoundList?)? WhereClause? AssocItemList
245 | '=' TypeBoundList? WhereClause? ';'
249 '{' Attr* AssocItem* '}'
260 'impl' GenericParamList? ('const'? '!'? trait:Type 'for')? self_ty:Type WhereClause?
264 Attr* 'unsafe'? Abi ExternItemList
267 '{' Attr* ExternItem* '}'
276 '<' (GenericParam (',' GenericParam)* ','?)? '>'
284 Attr* Name (':' TypeBoundList?)?
285 ('=' default_type:Type)?
288 Attr* 'const' Name ':' Type
289 ('=' default_val:Expr)?
292 Attr* Lifetime (':' TypeBoundList?)?
295 'where' predicates:(WherePred (',' WherePred)* ','?)
298 ('for' GenericParamList)? (Lifetime | Type) ':' TypeBoundList?
301 'pub' ('(' 'in'? Path ')')?
304 '#' '!'? '[' Meta ']'
307 Path ('=' Expr | TokenTree)?
309 //****************************//
310 // Statements and Expressions //
311 //****************************//
320 Attr* 'let' Pat (':' Type)?
370 'int_number' | 'float_number'
371 | 'string' | 'raw_string'
372 | 'byte_string' | 'raw_byte_string'
388 Attr* '&' ('raw' | 'mut' | 'const') Expr
394 Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList
397 Attr* op:('-' | '!' | '*') Expr
404 | '==' | '!=' | '<=' | '>=' | '<' | '>'
405 | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
406 | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
414 Attr* '(' Attr* Expr ')'
418 (Expr (',' Expr)* ','?)?
423 Attr* base:Expr '[' index:Expr ']'
426 Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')'
429 Path RecordExprFieldList
431 RecordExprFieldList =
434 fields:(RecordExprField (',' RecordExprField)* ','?)?
439 Attr* (NameRef ':')? Expr
445 '(' args:(Expr (',' Expr)* ','?)? ')'
448 Attr* receiver:Expr '.' NameRef GenericArgList? ArgList
451 Attr* Expr '.' NameRef
454 Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType?
458 Attr* 'if' condition:Expr then_branch:BlockExpr
459 ('else' else_branch:(IfExpr | BlockExpr))?
466 Attr* Label? 'for' Pat 'in' iterable:Expr
470 Attr* Label? 'while' condition:Expr
477 Attr* 'break' Lifetime? Expr?
480 Attr* 'continue' Lifetime?
483 Attr* start:Expr? op:('..' | '..=') end:Expr?
486 Attr* 'match' Expr MatchArmList
495 Attr* Pat guard:MatchGuard? '=>' Expr ','?
507 Attr* 'let' Pat '=' Expr
513 Attr* Expr '.' 'await'
518 //*************************//
520 //*************************//
551 '(' fields:(Type (',' Type)* ','?)? ')'
554 '*' ('const' | 'mut') Type
557 '&' Lifetime? 'mut'? Type
560 '[' Type ';' Expr ']'
569 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType?
572 'for' GenericParamList Type
581 bounds:(TypeBound ('+' TypeBound)* '+'?)
585 | ('?' | '~' 'const')? Type
587 //************************//
589 //************************//
613 Attr* 'ref'? 'mut'? Name ('@' Pat)?
620 start:Pat op:('..' | '..=')
622 | start:Pat op:('..' | '..=') end:Pat
624 | op:('..' | '..=') end:Pat
630 Path RecordPatFieldList
634 fields:(RecordPatField (',' RecordPatField)* ','?)?
639 Attr* (NameRef ':')? Pat
642 Path '(' fields:(Pat (',' Pat)* ','?)? ')'
645 '(' fields:(Pat (',' Pat)* ','?)? ')'
651 '[' (Pat (',' Pat)* ','?)? ']'
657 (Pat ('|' Pat)* '|'?)