]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/syntax/rust.ungram
Auto merge of #102717 - beetrees:repr128-c-style-debuginfo, r=nagisa
[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 GenericArgList? (':' 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 | MatchExpr
347 | MethodCallExpr
348 | ParenExpr
349 | PathExpr
350 | PrefixExpr
351 | RangeExpr
352 | RecordExpr
353 | RefExpr
354 | ReturnExpr
355 | TryExpr
356 | TupleExpr
357 | WhileExpr
358 | YieldExpr
359 | LetExpr
360 | UnderscoreExpr
361
362 MacroExpr =
363   MacroCall
364
365 Literal =
366   Attr* value:(
367     'int_number' | 'float_number'
368   | 'string' | 'raw_string'
369   | 'byte_string' | 'raw_byte_string'
370   | 'true' | 'false'
371   | 'char' | 'byte'
372   )
373
374 PathExpr =
375   Attr* Path
376
377 StmtList =
378   '{'
379     Attr*
380     statements:Stmt*
381     tail_expr:Expr?
382   '}'
383
384 RefExpr =
385   Attr* '&' ('raw' | 'mut' | 'const') Expr
386
387 TryExpr =
388   Attr* Expr '?'
389
390 BlockExpr =
391   Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList
392
393 PrefixExpr =
394   Attr* op:('-' | '!' | '*') Expr
395
396 BinExpr =
397   Attr*
398   lhs:Expr
399   op:(
400     '||' | '&&'
401   | '==' | '!=' | '<=' | '>=' | '<' | '>'
402   | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
403   | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
404   )
405   rhs:Expr
406
407 CastExpr =
408   Attr* Expr 'as' Type
409
410 ParenExpr =
411   Attr* '(' Attr* Expr ')'
412
413 ArrayExpr =
414   Attr* '[' Attr* (
415     (Expr (',' Expr)* ','?)?
416   | Expr ';' Expr
417   ) ']'
418
419 IndexExpr =
420   Attr* base:Expr '[' index:Expr ']'
421
422 TupleExpr =
423   Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')'
424
425 RecordExpr =
426   Path RecordExprFieldList
427
428 RecordExprFieldList =
429   '{'
430     Attr*
431     fields:(RecordExprField (',' RecordExprField)* ','?)?
432     ('..' spread:Expr?)?
433   '}'
434
435 RecordExprField =
436   Attr* (NameRef ':')? Expr
437
438 CallExpr =
439   Attr* Expr ArgList
440
441 ArgList =
442   '(' args:(Expr (',' Expr)* ','?)? ')'
443
444 MethodCallExpr =
445   Attr* receiver:Expr '.' NameRef GenericArgList? ArgList
446
447 FieldExpr =
448   Attr* Expr '.' NameRef
449
450 ClosureExpr =
451   Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'?  ParamList RetType?
452   body:Expr
453
454 IfExpr =
455   Attr* 'if' condition:Expr then_branch:BlockExpr
456   ('else' else_branch:(IfExpr | BlockExpr))?
457
458 LoopExpr =
459   Attr* Label? 'loop'
460   loop_body:BlockExpr
461
462 ForExpr =
463   Attr* Label? 'for' Pat 'in' iterable:Expr
464   loop_body:BlockExpr
465
466 WhileExpr =
467   Attr* Label? 'while' condition:Expr
468   loop_body:BlockExpr
469
470 Label =
471   Lifetime ':'
472
473 BreakExpr =
474   Attr* 'break' Lifetime? Expr?
475
476 ContinueExpr =
477   Attr* 'continue' Lifetime?
478
479 RangeExpr =
480   Attr* start:Expr? op:('..' | '..=') end:Expr?
481
482 MatchExpr =
483   Attr* 'match' Expr MatchArmList
484
485 MatchArmList =
486   '{'
487     Attr*
488     arms:MatchArm*
489   '}'
490
491 MatchArm =
492   Attr* Pat guard:MatchGuard? '=>' Expr ','?
493
494 MatchGuard =
495   'if' condition:Expr
496
497 ReturnExpr =
498   Attr* 'return' Expr?
499
500 YieldExpr =
501   Attr* 'yield' Expr?
502
503 LetExpr =
504   Attr* 'let' Pat '=' Expr
505
506 UnderscoreExpr =
507   Attr* '_'
508
509 AwaitExpr =
510   Attr* Expr '.' 'await'
511
512 BoxExpr =
513   Attr* 'box' Expr
514
515 //*************************//
516 //          Types          //
517 //*************************//
518
519 Type =
520   ArrayType
521 | DynTraitType
522 | FnPtrType
523 | ForType
524 | ImplTraitType
525 | InferType
526 | MacroType
527 | NeverType
528 | ParenType
529 | PathType
530 | PtrType
531 | RefType
532 | SliceType
533 | TupleType
534
535 ParenType =
536   '(' Type ')'
537
538 NeverType =
539   '!'
540
541 MacroType =
542   MacroCall
543
544 PathType =
545   Path
546
547 TupleType =
548   '(' fields:(Type (',' Type)* ','?)? ')'
549
550 PtrType =
551   '*' ('const' | 'mut') Type
552
553 RefType =
554   '&' Lifetime? 'mut'? Type
555
556 ArrayType =
557   '[' Type ';' Expr ']'
558
559 SliceType =
560   '[' Type ']'
561
562 InferType =
563   '_'
564
565 FnPtrType =
566   'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType?
567
568 ForType =
569   'for' GenericParamList Type
570
571 ImplTraitType =
572   'impl' TypeBoundList
573
574 DynTraitType =
575   'dyn' TypeBoundList
576
577 TypeBoundList =
578   bounds:(TypeBound ('+' TypeBound)* '+'?)
579
580 TypeBound =
581   Lifetime
582 | ('?' | '~' 'const')? Type
583
584 //************************//
585 //        Patterns        //
586 //************************//
587
588 Pat =
589   IdentPat
590 | BoxPat
591 | RestPat
592 | LiteralPat
593 | MacroPat
594 | OrPat
595 | ParenPat
596 | PathPat
597 | WildcardPat
598 | RangePat
599 | RecordPat
600 | RefPat
601 | SlicePat
602 | TuplePat
603 | TupleStructPat
604 | ConstBlockPat
605
606 LiteralPat =
607   Literal
608
609 IdentPat =
610   Attr* 'ref'? 'mut'? Name ('@' Pat)?
611
612 WildcardPat =
613   '_'
614
615 RangePat =
616   // 1..
617   start:Pat op:('..' | '..=')
618   // 1..2
619   | start:Pat op:('..' | '..=') end:Pat
620   // ..2
621   | op:('..' | '..=') end:Pat
622
623 RefPat =
624   '&' 'mut'? Pat
625
626 RecordPat =
627   Path RecordPatFieldList
628
629 RecordPatFieldList =
630   '{'
631     fields:(RecordPatField (',' RecordPatField)* ','?)?
632     RestPat?
633   '}'
634
635 RecordPatField =
636   Attr* (NameRef ':')? Pat
637
638 TupleStructPat =
639    Path '(' fields:(Pat (',' Pat)* ','?)? ')'
640
641 TuplePat =
642    '(' fields:(Pat (',' Pat)* ','?)? ')'
643
644 ParenPat =
645   '(' Pat ')'
646
647 SlicePat =
648   '[' (Pat (',' Pat)* ','?)? ']'
649
650 PathPat =
651   Path
652
653 OrPat =
654   (Pat ('|' Pat)* '|'?)
655
656 BoxPat =
657   'box' Pat
658
659 RestPat =
660   Attr* '..'
661
662 MacroPat =
663   MacroCall
664
665 ConstBlockPat =
666   'const' BlockExpr