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? (':' TypeBoundList?)? WhereClause?
246 '{' Attr* AssocItem* '}'
257 'impl' GenericParamList? ('const'? '!'? trait:Type 'for')? self_ty:Type WhereClause?
261 Attr* 'unsafe'? Abi ExternItemList
264 '{' Attr* ExternItem* '}'
273 '<' (GenericParam (',' GenericParam)* ','?)? '>'
281 Attr* Name (':' TypeBoundList?)?
282 ('=' default_type:Type)?
285 Attr* 'const' Name ':' Type
286 ('=' default_val:Expr)?
289 Attr* Lifetime (':' TypeBoundList?)?
292 'where' predicates:(WherePred (',' WherePred)* ','?)
295 ('for' GenericParamList)? (Lifetime | Type) ':' TypeBoundList?
298 'pub' ('(' 'in'? Path ')')?
301 '#' '!'? '[' Meta ']'
304 Path ('=' Expr | TokenTree)?
306 //****************************//
307 // Statements and Expressions //
308 //****************************//
317 Attr* 'let' Pat (':' Type)?
367 'int_number' | 'float_number'
368 | 'string' | 'raw_string'
369 | 'byte_string' | 'raw_byte_string'
385 Attr* '&' ('raw' | 'mut' | 'const') Expr
391 Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList
394 Attr* op:('-' | '!' | '*') Expr
401 | '==' | '!=' | '<=' | '>=' | '<' | '>'
402 | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
403 | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
411 Attr* '(' Attr* Expr ')'
415 (Expr (',' Expr)* ','?)?
420 Attr* base:Expr '[' index:Expr ']'
423 Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')'
426 Path RecordExprFieldList
428 RecordExprFieldList =
431 fields:(RecordExprField (',' RecordExprField)* ','?)?
436 Attr* (NameRef ':')? Expr
442 '(' args:(Expr (',' Expr)* ','?)? ')'
445 Attr* receiver:Expr '.' NameRef GenericArgList? ArgList
448 Attr* Expr '.' NameRef
451 Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'? ParamList RetType?
455 Attr* 'if' condition:Expr then_branch:BlockExpr
456 ('else' else_branch:(IfExpr | BlockExpr))?
463 Attr* Label? 'for' Pat 'in' iterable:Expr
467 Attr* Label? 'while' condition:Expr
474 Attr* 'break' Lifetime? Expr?
477 Attr* 'continue' Lifetime?
480 Attr* start:Expr? op:('..' | '..=') end:Expr?
483 Attr* 'match' Expr MatchArmList
492 Attr* Pat guard:MatchGuard? '=>' Expr ','?
504 Attr* 'let' Pat '=' Expr
510 Attr* Expr '.' 'await'
515 //*************************//
517 //*************************//
548 '(' fields:(Type (',' Type)* ','?)? ')'
551 '*' ('const' | 'mut') Type
554 '&' Lifetime? 'mut'? Type
557 '[' Type ';' Expr ']'
566 'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType?
569 'for' GenericParamList Type
578 bounds:(TypeBound ('+' TypeBound)* '+'?)
582 | ('?' | '~' 'const')? Type
584 //************************//
586 //************************//
610 Attr* 'ref'? 'mut'? Name ('@' Pat)?
617 start:Pat op:('..' | '..=')
619 | start:Pat op:('..' | '..=') end:Pat
621 | op:('..' | '..=') end:Pat
627 Path RecordPatFieldList
631 fields:(RecordPatField (',' RecordPatField)* ','?)?
636 Attr* (NameRef ':')? Pat
639 Path '(' fields:(Pat (',' Pat)* ','?)? ')'
642 '(' fields:(Pat (',' Pat)* ','?)? ')'
648 '[' (Pat (',' Pat)* ','?)? ']'
654 (Pat ('|' Pat)* '|'?)