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