]> git.lizzy.rs Git - rust.git/blob - src/tools/rust-analyzer/crates/syntax/rust.ungram
Rollup merge of #104793 - nicholasbishop:bishop-add-efiapi, r=JohnTitor
[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?
243   (
244     (':' TypeBoundList?)? WhereClause? AssocItemList
245     | '=' TypeBoundList? WhereClause? ';'
246   )
247
248 AssocItemList =
249   '{' Attr* AssocItem* '}'
250
251 AssocItem =
252   Const
253 | Fn
254 | MacroCall
255 | TypeAlias
256
257 Impl =
258   Attr* Visibility?
259   'default'? 'unsafe'?
260   'impl' GenericParamList? ('const'? '!'? trait:Type 'for')? self_ty:Type WhereClause?
261   AssocItemList
262
263 ExternBlock =
264   Attr* 'unsafe'? Abi ExternItemList
265
266 ExternItemList =
267   '{' Attr* ExternItem* '}'
268
269 ExternItem =
270   Fn
271 | MacroCall
272 | Static
273 | TypeAlias
274
275 GenericParamList =
276   '<' (GenericParam (',' GenericParam)* ','?)? '>'
277
278 GenericParam =
279   ConstParam
280 | LifetimeParam
281 | TypeParam
282
283 TypeParam =
284   Attr* Name (':' TypeBoundList?)?
285   ('=' default_type:Type)?
286
287 ConstParam =
288   Attr* 'const' Name ':' Type
289   ('=' default_val:Expr)?
290
291 LifetimeParam =
292   Attr* Lifetime (':' TypeBoundList?)?
293
294 WhereClause =
295   'where' predicates:(WherePred (',' WherePred)* ','?)
296
297 WherePred =
298   ('for' GenericParamList)?  (Lifetime | Type) ':' TypeBoundList?
299
300 Visibility =
301   'pub' ('(' 'in'? Path ')')?
302
303 Attr =
304   '#' '!'? '[' Meta ']'
305
306 Meta =
307   Path ('=' Expr | TokenTree)?
308
309 //****************************//
310 // Statements and Expressions //
311 //****************************//
312
313 Stmt =
314   ';'
315 | ExprStmt
316 | Item
317 | LetStmt
318
319 LetStmt =
320   Attr* 'let' Pat (':' Type)?
321   '=' initializer:Expr
322   LetElse?
323   ';'
324
325 LetElse =
326   'else' BlockExpr
327
328 ExprStmt =
329   Expr ';'?
330
331 Expr =
332   ArrayExpr
333 | AwaitExpr
334 | BinExpr
335 | BlockExpr
336 | BoxExpr
337 | BreakExpr
338 | CallExpr
339 | CastExpr
340 | ClosureExpr
341 | ContinueExpr
342 | FieldExpr
343 | ForExpr
344 | IfExpr
345 | IndexExpr
346 | Literal
347 | LoopExpr
348 | MacroExpr
349 | MatchExpr
350 | MethodCallExpr
351 | ParenExpr
352 | PathExpr
353 | PrefixExpr
354 | RangeExpr
355 | RecordExpr
356 | RefExpr
357 | ReturnExpr
358 | TryExpr
359 | TupleExpr
360 | WhileExpr
361 | YieldExpr
362 | LetExpr
363 | UnderscoreExpr
364
365 MacroExpr =
366   MacroCall
367
368 Literal =
369   Attr* value:(
370     'int_number' | 'float_number'
371   | 'string' | 'raw_string'
372   | 'byte_string' | 'raw_byte_string'
373   | 'true' | 'false'
374   | 'char' | 'byte'
375   )
376
377 PathExpr =
378   Attr* Path
379
380 StmtList =
381   '{'
382     Attr*
383     statements:Stmt*
384     tail_expr:Expr?
385   '}'
386
387 RefExpr =
388   Attr* '&' ('raw' | 'mut' | 'const') Expr
389
390 TryExpr =
391   Attr* Expr '?'
392
393 BlockExpr =
394   Attr* Label? ('try' | 'unsafe' | 'async' | 'const') StmtList
395
396 PrefixExpr =
397   Attr* op:('-' | '!' | '*') Expr
398
399 BinExpr =
400   Attr*
401   lhs:Expr
402   op:(
403     '||' | '&&'
404   | '==' | '!=' | '<=' | '>=' | '<' | '>'
405   | '+' | '*' | '-' | '/' | '%' | '<<' | '>>' | '^' | '|' | '&'
406   | '=' | '+=' | '/=' | '*=' | '%=' | '>>=' | '<<=' | '-=' | '|=' | '&=' | '^='
407   )
408   rhs:Expr
409
410 CastExpr =
411   Attr* Expr 'as' Type
412
413 ParenExpr =
414   Attr* '(' Attr* Expr ')'
415
416 ArrayExpr =
417   Attr* '[' Attr* (
418     (Expr (',' Expr)* ','?)?
419   | Expr ';' Expr
420   ) ']'
421
422 IndexExpr =
423   Attr* base:Expr '[' index:Expr ']'
424
425 TupleExpr =
426   Attr* '(' Attr* fields:(Expr (',' Expr)* ','?)? ')'
427
428 RecordExpr =
429   Path RecordExprFieldList
430
431 RecordExprFieldList =
432   '{'
433     Attr*
434     fields:(RecordExprField (',' RecordExprField)* ','?)?
435     ('..' spread:Expr?)?
436   '}'
437
438 RecordExprField =
439   Attr* (NameRef ':')? Expr
440
441 CallExpr =
442   Attr* Expr ArgList
443
444 ArgList =
445   '(' args:(Expr (',' Expr)* ','?)? ')'
446
447 MethodCallExpr =
448   Attr* receiver:Expr '.' NameRef GenericArgList? ArgList
449
450 FieldExpr =
451   Attr* Expr '.' NameRef
452
453 ClosureExpr =
454   Attr* ('for' GenericParamList)? 'static'? 'async'? 'move'?  ParamList RetType?
455   body:Expr
456
457 IfExpr =
458   Attr* 'if' condition:Expr then_branch:BlockExpr
459   ('else' else_branch:(IfExpr | BlockExpr))?
460
461 LoopExpr =
462   Attr* Label? 'loop'
463   loop_body:BlockExpr
464
465 ForExpr =
466   Attr* Label? 'for' Pat 'in' iterable:Expr
467   loop_body:BlockExpr
468
469 WhileExpr =
470   Attr* Label? 'while' condition:Expr
471   loop_body:BlockExpr
472
473 Label =
474   Lifetime ':'
475
476 BreakExpr =
477   Attr* 'break' Lifetime? Expr?
478
479 ContinueExpr =
480   Attr* 'continue' Lifetime?
481
482 RangeExpr =
483   Attr* start:Expr? op:('..' | '..=') end:Expr?
484
485 MatchExpr =
486   Attr* 'match' Expr MatchArmList
487
488 MatchArmList =
489   '{'
490     Attr*
491     arms:MatchArm*
492   '}'
493
494 MatchArm =
495   Attr* Pat guard:MatchGuard? '=>' Expr ','?
496
497 MatchGuard =
498   'if' condition:Expr
499
500 ReturnExpr =
501   Attr* 'return' Expr?
502
503 YieldExpr =
504   Attr* 'yield' Expr?
505
506 LetExpr =
507   Attr* 'let' Pat '=' Expr
508
509 UnderscoreExpr =
510   Attr* '_'
511
512 AwaitExpr =
513   Attr* Expr '.' 'await'
514
515 BoxExpr =
516   Attr* 'box' Expr
517
518 //*************************//
519 //          Types          //
520 //*************************//
521
522 Type =
523   ArrayType
524 | DynTraitType
525 | FnPtrType
526 | ForType
527 | ImplTraitType
528 | InferType
529 | MacroType
530 | NeverType
531 | ParenType
532 | PathType
533 | PtrType
534 | RefType
535 | SliceType
536 | TupleType
537
538 ParenType =
539   '(' Type ')'
540
541 NeverType =
542   '!'
543
544 MacroType =
545   MacroCall
546
547 PathType =
548   Path
549
550 TupleType =
551   '(' fields:(Type (',' Type)* ','?)? ')'
552
553 PtrType =
554   '*' ('const' | 'mut') Type
555
556 RefType =
557   '&' Lifetime? 'mut'? Type
558
559 ArrayType =
560   '[' Type ';' Expr ']'
561
562 SliceType =
563   '[' Type ']'
564
565 InferType =
566   '_'
567
568 FnPtrType =
569   'const'? 'async'? 'unsafe'? Abi? 'fn' ParamList RetType?
570
571 ForType =
572   'for' GenericParamList Type
573
574 ImplTraitType =
575   'impl' TypeBoundList
576
577 DynTraitType =
578   'dyn' TypeBoundList
579
580 TypeBoundList =
581   bounds:(TypeBound ('+' TypeBound)* '+'?)
582
583 TypeBound =
584   Lifetime
585 | ('?' | '~' 'const')? Type
586
587 //************************//
588 //        Patterns        //
589 //************************//
590
591 Pat =
592   IdentPat
593 | BoxPat
594 | RestPat
595 | LiteralPat
596 | MacroPat
597 | OrPat
598 | ParenPat
599 | PathPat
600 | WildcardPat
601 | RangePat
602 | RecordPat
603 | RefPat
604 | SlicePat
605 | TuplePat
606 | TupleStructPat
607 | ConstBlockPat
608
609 LiteralPat =
610   Literal
611
612 IdentPat =
613   Attr* 'ref'? 'mut'? Name ('@' Pat)?
614
615 WildcardPat =
616   '_'
617
618 RangePat =
619   // 1..
620   start:Pat op:('..' | '..=')
621   // 1..2
622   | start:Pat op:('..' | '..=') end:Pat
623   // ..2
624   | op:('..' | '..=') end:Pat
625
626 RefPat =
627   '&' 'mut'? Pat
628
629 RecordPat =
630   Path RecordPatFieldList
631
632 RecordPatFieldList =
633   '{'
634     fields:(RecordPatField (',' RecordPatField)* ','?)?
635     RestPat?
636   '}'
637
638 RecordPatField =
639   Attr* (NameRef ':')? Pat
640
641 TupleStructPat =
642    Path '(' fields:(Pat (',' Pat)* ','?)? ')'
643
644 TuplePat =
645    '(' fields:(Pat (',' Pat)* ','?)? ')'
646
647 ParenPat =
648   '(' Pat ')'
649
650 SlicePat =
651   '[' (Pat (',' Pat)* ','?)? ']'
652
653 PathPat =
654   Path
655
656 OrPat =
657   (Pat ('|' Pat)* '|'?)
658
659 BoxPat =
660   'box' Pat
661
662 RestPat =
663   Attr* '..'
664
665 MacroPat =
666   MacroCall
667
668 ConstBlockPat =
669   'const' BlockExpr