1// Rust Un-Grammar.
2//
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.
6//
7// Legend:
8//
9//   //          -- comment
10//   Name =      -- non-terminal definition
11//   'ident'     -- token (terminal)
12//   A B         -- sequence
13//   A | B       -- alternation
14//   A*          -- zero or more repetition
15//   A?          -- zero or one repetition
16//   (A)         -- same as A
17//   label:A     -- suggested name for field of AST node
18
19//*************************//
20// Names, Paths and Macros //
21//*************************//
22
23Name =
24  'ident' | 'self'
25
26NameRef =
27  'ident' | 'int_number' | 'self' | 'super' | 'crate'
28
29Lifetime =
30  'lifetime_ident'
31
32Path =
33  (qualifier:Path '::')? segment:PathSegment
34
35PathSegment =
36  '::'? NameRef
37| NameRef GenericArgList?
38| NameRef ParamList RetType?
39| '<' PathType ('as' PathType)? '>'
40
41GenericArgList =
42  '::'? '<' (GenericArg (',' GenericArg)* ','?)? '>'
43
44GenericArg =
45  TypeArg
46| AssocTypeArg
47| LifetimeArg
48| ConstArg
49
50TypeArg =
51  Type
52
53AssocTypeArg =
54  NameRef GenericParamList? (':' TypeBoundList | '=' Type)
55
56LifetimeArg =
57  Lifetime
58
59ConstArg =
60  Expr
61
62MacroCall =
63  Attr* Path '!' TokenTree ';'?
64
65TokenTree =
66  '(' ')'
67| '{' '}'
68| '[' ']'
69
70MacroItems =
71  Item*
72
73MacroStmts =
74  statements:Stmt*
75  Expr?
76
77//*************************//
78//          Items          //
79//*************************//
80
81SourceFile =
82  'shebang'?
83  Attr*
84  Item*
85
86Item =
87  Const
88| Enum
89| ExternBlock
90| ExternCrate
91| Fn
92| Impl
93| MacroCall
94| MacroRules
95| MacroDef
96| Module
97| Static
98| Struct
99| Trait
100| TypeAlias
101| Union
102| Use
103
104MacroRules =
105  Attr* Visibility?
106  'macro_rules' '!' Name
107  TokenTree
108
109MacroDef =
110  Attr* Visibility?
111  'macro' Name args:TokenTree?
112  body:TokenTree
113
114Module =
115  Attr* Visibility?
116  'mod' Name
117  (ItemList | ';')
118
119ItemList =
120  '{' Attr* Item* '}'
121
122ExternCrate =
123  Attr* Visibility?
124  'extern' 'crate' NameRef Rename? ';'
125
126Rename =
127  'as' (Name | '_')
128
129Use =
130  Attr* Visibility?
131  'use' UseTree ';'
132
133UseTree =
134  (Path? '::')? ('*' | UseTreeList)
135| Path Rename?
136
137UseTreeList =
138  '{' (UseTree (',' UseTree)* ','?)? '}'
139
140Fn =
141 Attr* Visibility?
142 'default'? 'const'? 'async'? 'unsafe'? Abi?
143 'fn' Name GenericParamList? ParamList RetType? WhereClause?
144 (body:BlockExpr | ';')
145
146Abi =
147  'extern' 'string'?
148
149ParamList =
150  '('(
151    SelfParam
152  | (SelfParam ',')? (Param (',' Param)* ','?)?
153  )')'
154| '|' (Param (',' Param)* ','?)? '|'
155
156SelfParam =
157  Attr* (
158    ('&' Lifetime?)? 'mut'? Name
159  | 'mut'? Name ':' Type
160  )
161
162Param =
163  Attr* (
164    Pat (':' Type)?
165  | Type
166  | '...'
167  )
168
169RetType =
170  '->' Type
171
172TypeAlias =
173  Attr* Visibility?
174  'default'?
175  'type' Name GenericParamList? (':' TypeBoundList?)? WhereClause?
176  '=' Type ';'
177
178Struct =
179  Attr* Visibility?
180  'struct' Name GenericParamList? (
181    WhereClause? (RecordFieldList | ';')
182  | TupleFieldList WhereClause? ';'
183  )
184
185RecordFieldList =
186 '{' fields:(RecordField (',' RecordField)* ','?)? '}'
187
188RecordField =
189  Attr* Visibility?
190  Name ':' Type
191
192TupleFieldList =
193  '(' fields:(TupleField (',' TupleField)* ','?)? ')'
194
195TupleField =
196  Attr* Visibility?
197  Type
198
199FieldList =
200  RecordFieldList
201| TupleFieldList
202
203Enum =
204  Attr* Visibility?
205  'enum' Name GenericParamList? WhereClause?
206  VariantList
207
208VariantList =
209 '{' (Variant (',' Variant)* ','?)? '}'
210
211Variant =
212  Attr* Visibility?
213  Name FieldList ('=' Expr)?
214
215Union =
216  Attr* Visibility?
217  'union' Name GenericParamList? WhereClause?
218  RecordFieldList
219
220// A Data Type.
221//
222// Not used directly in the grammar, but handy to have anyway.
223Adt =
224  Enum
225| Struct
226| Union
227
228Const =
229  Attr* Visibility?
230  'default'?
231  'const' (Name | '_') ':' Type
232  '=' body:Expr ';'
233
234Static =
235  Attr* Visibility?
236  'static'? 'mut'? Name ':' Type
237  '=' body:Expr ';'
238
239Trait =
240  Attr* Visibility?
241  'unsafe'? 'auto'?
242  'trait' Name GenericParamList (':' TypeBoundList?)? WhereClause
243  AssocItemList
244
245AssocItemList =
246  '{' Attr* AssocItem* '}'
247
248AssocItem =
249  Const
250| Fn
251| MacroCall
252| TypeAlias
253
254Impl =
255  Attr* Visibility?
256  'default'? 'unsafe'?
257  'impl' GenericParamList? ('const'? '!'? trait:Type 'for')? self_ty:Type WhereClause?
258  AssocItemList
259
260ExternBlock =
261  Attr* Abi ExternItemList
262
263ExternItemList =
264  '{' Attr* ExternItem* '}'
265
266ExternItem =
267  Fn
268| MacroCall
269| Static
270| TypeAlias
271
272GenericParamList =
273  '<' (GenericParam (',' GenericParam)* ','?)? '>'
274
275GenericParam =
276  ConstParam
277| LifetimeParam
278| TypeParam
279
280TypeParam =
281  Attr* Name (':' TypeBoundList?)?
282  ('=' default_type:Type)?
283
284ConstParam =
285  Attr* 'const' Name ':' Type
286  ('=' default_val:Expr)?
287
288LifetimeParam =
289  Attr* Lifetime (':' TypeBoundList?)?
290
291WhereClause =
292  'where' predicates:(WherePred (',' WherePred)* ','?)
293
294WherePred =
295  ('for' GenericParamList)?  (Lifetime | Type) ':' TypeBoundList
296
297Visibility =
298  'pub' ('(' 'in'? Path ')')?
299
300Attr =
301  '#' '!'? '[' Meta ']'
302
303Meta =
304  Path ('=' Expr | TokenTree)?
305
306//****************************//
307// Statements and Expressions //
308//****************************//
309
310Stmt =
311  ';'
312| ExprStmt
313| Item
314| LetStmt
315
316LetStmt =
317  Attr* 'let' Pat (':' Type)?
318  '=' initializer:Expr
319  LetElse?
320  ';'
321
322LetElse =
323  'else' BlockExpr
324
325ExprStmt =
326  Expr ';'?
327
328Expr =
329  ArrayExpr
330| AwaitExpr
331| BinExpr
332| BlockExpr
333| BoxExpr
334| BreakExpr
335| CallExpr
336| CastExpr
337| ClosureExpr
338| ContinueExpr
339| FieldExpr
340| ForExpr
341| IfExpr
342| IndexExpr
343| Literal
344| LoopExpr
345| MacroCall
346| MacroStmts
347| MatchExpr
348| MethodCallExpr
349| ParenExpr
350| PathExpr
351| PrefixExpr
352| RangeExpr
353| RecordExpr
354| RefExpr
355| ReturnExpr
356| TryExpr
357| TupleExpr
358| WhileExpr
359| YieldExpr
360
361Literal =
362  Attr* value:(
363    'int_number' | 'float_number'
364  | 'string' | 'raw_string'
365  | 'byte_string' | 'raw_byte_string'
366  | 'true' | 'false'
367  | 'char' | 'byte'
368  )
369
370PathExpr =
371  Attr* Path
372
373StmtList =
374  '{'
375    Attr*
376    statements:Stmt*
377    tail_expr:Expr?
378  '}'
379
380RefExpr =
381  Attr* '&' ('raw' | 'mut' | 'const') Expr
382
383TryExpr =
384  Attr* Expr '?'
385
386BlockExpr =
387  Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList
388
389PrefixExpr =
390  Attr* op:('-' | '!' | '*') Expr
391
392BinExpr =
393  Attr*
394  lhs:Expr
395  op:(
396    '||' | '&&'
397  | '==' | '!=' | '<=' | '>=' | '<' | '>'
398  | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
399  | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
400  )
401  rhs:Expr
402
403CastExpr =
404  Attr* Expr 'as' Type
405
406ParenExpr =
407  Attr* '(' Attr* Expr ')'
408
409ArrayExpr =
410  Attr* '[' Attr* (
411    (Expr (',' Expr)* ','?)?
412  | Expr ';' Expr
413  ) ']'
414
415IndexExpr =
416  Attr* base:Expr '[' index:Expr ']'
417
418TupleExpr =
419  Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')'
420
421RecordExpr =
422  Path RecordExprFieldList
423
424RecordExprFieldList =
425  '{'
426    Attr*
427    fields:(RecordExprField (',' RecordExprField)* ','?)?
428    ('..' spread:Expr)?
429  '}'
430
431RecordExprField =
432  Attr* (NameRef ':')? Expr
433
434CallExpr =
435  Attr* Expr ArgList
436
437ArgList =
438  '(' args:(Expr (',' Expr)* ','?)? ')'
439
440MethodCallExpr =
441  Attr* receiver:Expr '.' NameRef GenericArgList? ArgList
442
443FieldExpr =
444  Attr* Expr '.' NameRef
445
446ClosureExpr =
447  Attr* 'static'? 'async'? 'move'?  ParamList RetType?
448  body:Expr
449
450IfExpr =
451  Attr* 'if' Condition then_branch:BlockExpr
452  ('else' else_branch:(IfExpr | BlockExpr))?
453
454Condition =
455  'let' Pat '=' Expr
456| Expr
457
458LoopExpr =
459  Attr* Label? 'loop'
460  loop_body:BlockExpr
461
462ForExpr =
463  Attr* Label? 'for' Pat 'in' iterable:Expr
464  loop_body:BlockExpr
465
466WhileExpr =
467  Attr* Label? 'while' Condition
468  loop_body:BlockExpr
469
470Label =
471  Lifetime ':'
472
473BreakExpr =
474  Attr* 'break' Lifetime? Expr?
475
476ContinueExpr =
477  Attr* 'continue' Lifetime?
478
479RangeExpr =
480  Attr* start:Expr? op:('..' | '..=') end:Expr?
481
482MatchExpr =
483  Attr* 'match' Expr MatchArmList
484
485MatchArmList =
486  '{'
487    Attr*
488    arms:MatchArm*
489  '}'
490
491MatchArm =
492  Attr* Pat guard:MatchGuard? '=>' Expr ','?
493
494MatchGuard =
495  'if' ('let' Pat '=')? Expr
496
497ReturnExpr =
498  Attr* 'return' Expr?
499
500YieldExpr =
501  Attr* 'yield' Expr?
502
503AwaitExpr =
504  Attr* Expr '.' 'await'
505
506BoxExpr =
507  Attr* 'box' Expr
508
509//*************************//
510//          Types          //
511//*************************//
512
513Type =
514  ArrayType
515| DynTraitType
516| FnPtrType
517| ForType
518| ImplTraitType
519| InferType
520| MacroType
521| NeverType
522| ParenType
523| PathType
524| PtrType
525| RefType
526| SliceType
527| TupleType
528
529ParenType =
530  '(' Type ')'
531
532NeverType =
533  '!'
534
535MacroType =
536  MacroCall
537
538PathType =
539  Path
540
541TupleType =
542  '(' fields:(Type (',' Type)* ','?)? ')'
543
544PtrType =
545  '*' ('const' | 'mut') Type
546
547RefType =
548  '&' Lifetime? 'mut'? Type
549
550ArrayType =
551  '[' Type ';' Expr ']'
552
553SliceType =
554  '[' Type ']'
555
556InferType =
557  '_'
558
559FnPtrType =
560  'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType?
561
562ForType =
563  'for' GenericParamList Type
564
565ImplTraitType =
566  'impl' TypeBoundList
567
568DynTraitType =
569  'dyn' TypeBoundList
570
571TypeBoundList =
572  bounds:(TypeBound ('+' TypeBound)* '+'?)
573
574TypeBound =
575  Lifetime
576| ('?' | '~' 'const')? Type
577
578//************************//
579//        Patterns        //
580//************************//
581
582Pat =
583  IdentPat
584| BoxPat
585| RestPat
586| LiteralPat
587| MacroPat
588| OrPat
589| ParenPat
590| PathPat
591| WildcardPat
592| RangePat
593| RecordPat
594| RefPat
595| SlicePat
596| TuplePat
597| TupleStructPat
598| ConstBlockPat
599
600LiteralPat =
601  Literal
602
603IdentPat =
604  Attr* 'ref'? 'mut'? Name ('@' Pat)?
605
606WildcardPat =
607  '_'
608
609RangePat =
610  // 1..
611  start:Pat op:('..' | '..=')
612  // 1..2
613  | start:Pat op:('..' | '..=') end:Pat
614  // ..2
615  | op:('..' | '..=') end:Pat
616
617RefPat =
618  '&' 'mut'? Pat
619
620RecordPat =
621  Path RecordPatFieldList
622
623RecordPatFieldList =
624  '{'
625    fields:(RecordPatField (',' RecordPatField)* ','?)?
626    RestPat?
627  '}'
628
629RecordPatField =
630  Attr* (NameRef ':')? Pat
631
632TupleStructPat =
633   Path '(' fields:(Pat (',' Pat)* ','?)? ')'
634
635TuplePat =
636   '(' fields:(Pat (',' Pat)* ','?)? ')'
637
638ParenPat =
639  '(' Pat ')'
640
641SlicePat =
642  '[' (Pat (',' Pat)* ','?)? ']'
643
644PathPat =
645  Path
646
647OrPat =
648  (Pat ('|' Pat)* '|'?)
649
650BoxPat =
651  'box' Pat
652
653RestPat =
654  Attr* '..'
655
656MacroPat =
657  MacroCall
658
659ConstBlockPat =
660  'const' BlockExpr
661