fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #103046 - JanBeh:PR_clarify_cmp_terminology, r=workingjubilee3bcad65fbf6

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