Debian Codegraph @

rust.ungram

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