]> git.lizzy.rs Git - rust.git/blob - crates/ra_syntax/src/grammar.ron
Merge #1818
[rust.git] / crates / ra_syntax / src / grammar.ron
1 // Stores definitions which must be used in multiple places
2 // See `cargo gen-syntax` (defined in crates/tools/src/main.rs)
3 Grammar(
4     single_byte_tokens: [
5         (";", "SEMI"),
6         (",", "COMMA"),
7         ("(", "L_PAREN"),
8         (")", "R_PAREN"),
9         ("{", "L_CURLY"),
10         ("}", "R_CURLY"),
11         ("[", "L_BRACK"),
12         ("]", "R_BRACK"),
13         ("<", "L_ANGLE"),
14         (">", "R_ANGLE"),
15         ("@", "AT"),
16         ("#", "POUND"),
17         ("~", "TILDE"),
18         ("?", "QUESTION"),
19         ("$", "DOLLAR"),
20         ("&", "AMP"),
21         ("|", "PIPE"),
22         ("+", "PLUS"),
23         ("*", "STAR"),
24         ("/", "SLASH"),
25         ("^", "CARET"),
26         ("%", "PERCENT"),
27         ("_", "UNDERSCORE"),
28     ],
29     // Tokens for which the longest match must be chosen (e.g. `..` is a DOTDOT, but `.` is a DOT)
30     multi_byte_tokens: [
31         (".", "DOT"),
32         ("..", "DOTDOT"),
33         ("...", "DOTDOTDOT"),
34         ("..=", "DOTDOTEQ"),
35         (":", "COLON"),
36         ("::", "COLONCOLON"),
37         ("=", "EQ"),
38         ("==", "EQEQ"),
39         ("=>", "FAT_ARROW"),
40         ("!", "EXCL"),
41         ("!=", "NEQ"),
42         ("-", "MINUS"),
43         ("->", "THIN_ARROW"),
44         ("<=", "LTEQ"),
45         (">=", "GTEQ"),
46         ("+=", "PLUSEQ"),
47         ("-=", "MINUSEQ"),
48         ("|=", "PIPEEQ"),
49         ("&=", "AMPEQ"),
50         ("^=", "CARETEQ"),
51         ("/=", "SLASHEQ"),
52         ("*=", "STAREQ"),
53         ("%=", "PERCENTEQ"),
54         ("&&", "AMPAMP"),
55         ("||", "PIPEPIPE"),
56         ("<<", "SHL"),
57         (">>", "SHR"),
58         ("<<=", "SHLEQ"),
59         (">>=", "SHREQ"),
60     ],
61     keywords: [
62         "async",
63         "use",
64         "fn",
65         "struct",
66         "enum",
67         "trait",
68         "impl",
69         "dyn",
70         "true",
71         "false",
72         "as",
73         "extern",
74         "crate",
75         "mod",
76         "pub",
77         "self",
78         "super",
79         "in",
80         "where",
81         "for",
82         "loop",
83         "while",
84         "continue",
85         "break",
86         "if",
87         "else",
88         "match",
89         "const",
90         "static",
91         "mut",
92         "unsafe",
93         "type",
94         "ref",
95         "let",
96         "move",
97         "return",
98         "try",
99         "box",
100         "await"
101     ],
102     contextual_keywords: [
103         "auto",
104         "default",
105         "existential",
106         "union",
107     ],
108     literals: [
109         "INT_NUMBER",
110         "FLOAT_NUMBER",
111         "CHAR",
112         "BYTE",
113         "STRING",
114         "RAW_STRING",
115         "BYTE_STRING",
116         "RAW_BYTE_STRING",
117     ],
118     tokens: [
119         "ERROR",
120         "IDENT",
121         "WHITESPACE",
122         "LIFETIME",
123         "COMMENT",
124         "SHEBANG",
125         "L_DOLLAR",
126         "R_DOLLAR",
127     ],
128     nodes: [
129         "SOURCE_FILE",
130
131         "STRUCT_DEF",
132         "ENUM_DEF",
133         "FN_DEF",
134         "RET_TYPE",
135         "EXTERN_CRATE_ITEM",
136         "MODULE",
137         "USE_ITEM",
138         "STATIC_DEF",
139         "CONST_DEF",
140         "TRAIT_DEF",
141         "IMPL_BLOCK",
142         "TYPE_ALIAS_DEF",
143         "MACRO_CALL",
144         "TOKEN_TREE",
145
146         "PAREN_TYPE",
147         "TUPLE_TYPE",
148         "NEVER_TYPE",
149         "PATH_TYPE",
150         "POINTER_TYPE",
151         "ARRAY_TYPE",
152         "SLICE_TYPE",
153         "REFERENCE_TYPE",
154         "PLACEHOLDER_TYPE",
155         "FN_POINTER_TYPE",
156         "FOR_TYPE",
157         "IMPL_TRAIT_TYPE",
158         "DYN_TRAIT_TYPE",
159
160         "REF_PAT",
161         "BOX_PAT",
162         "BIND_PAT",
163         "PLACEHOLDER_PAT",
164         "PATH_PAT",
165         "RECORD_PAT",
166         "RECORD_FIELD_PAT_LIST",
167         "RECORD_FIELD_PAT",
168         "TUPLE_STRUCT_PAT",
169         "TUPLE_PAT",
170         "SLICE_PAT",
171         "RANGE_PAT",
172         "LITERAL_PAT",
173
174         // atoms
175         "TUPLE_EXPR",
176         "ARRAY_EXPR",
177         "PAREN_EXPR",
178         "PATH_EXPR",
179         "LAMBDA_EXPR",
180         "IF_EXPR",
181         "WHILE_EXPR",
182         "CONDITION",
183         "LOOP_EXPR",
184         "FOR_EXPR",
185         "CONTINUE_EXPR",
186         "BREAK_EXPR",
187         "LABEL",
188         "BLOCK_EXPR",
189         "RETURN_EXPR",
190         "MATCH_EXPR",
191         "MATCH_ARM_LIST",
192         "MATCH_ARM",
193         "MATCH_GUARD",
194         "RECORD_LIT",
195         "RECORD_FIELD_LIST",
196         "RECORD_FIELD",
197         "TRY_BLOCK_EXPR",
198         "BOX_EXPR",
199
200         // postfix
201         "CALL_EXPR",
202         "INDEX_EXPR",
203         "METHOD_CALL_EXPR",
204         "FIELD_EXPR",
205         "AWAIT_EXPR",
206         "TRY_EXPR",
207         "CAST_EXPR",
208
209         // unary
210         "REF_EXPR",
211         "PREFIX_EXPR",
212
213         "RANGE_EXPR", // just weird
214         "BIN_EXPR",
215
216         "BLOCK",
217         "EXTERN_BLOCK",
218         "EXTERN_ITEM_LIST",
219         "ENUM_VARIANT",
220         "RECORD_FIELD_DEF_LIST",
221         "RECORD_FIELD_DEF",
222         "TUPLE_FIELD_DEF_LIST",
223         "TUPLE_FIELD_DEF",
224         "ENUM_VARIANT_LIST",
225         "ITEM_LIST",
226         "ATTR",
227         "META_ITEM", // not an item actually
228         "USE_TREE",
229         "USE_TREE_LIST",
230         "PATH",
231         "PATH_SEGMENT",
232         "LITERAL",
233         "ALIAS",
234         "VISIBILITY",
235         "WHERE_CLAUSE",
236         "WHERE_PRED",
237         "ABI",
238         "NAME",
239         "NAME_REF",
240
241         "LET_STMT",
242         "EXPR_STMT",
243
244         "TYPE_PARAM_LIST",
245         "LIFETIME_PARAM",
246         "TYPE_PARAM",
247         "TYPE_ARG_LIST",
248         "LIFETIME_ARG",
249         "TYPE_ARG",
250         "ASSOC_TYPE_ARG",
251
252         "PARAM_LIST",
253         "PARAM",
254         "SELF_PARAM",
255         "ARG_LIST",
256         "TYPE_BOUND",
257         "TYPE_BOUND_LIST",
258
259         // macro related
260         "MACRO_ITEMS",
261         "MACRO_STMTS",
262     ],
263     ast: {
264         "SourceFile": (
265             traits: [ "ModuleItemOwner", "FnDefOwner" ],
266             collections: [
267                 ("modules", "Module"),
268             ]
269         ),
270         "FnDef": (
271             traits: [
272                 "VisibilityOwner",
273                 "NameOwner",
274                 "TypeParamsOwner",
275                 "AttrsOwner",
276                 "DocCommentsOwner"
277             ],
278             options: [ "ParamList", ["body", "BlockExpr"], "RetType" ],
279         ),
280         "RetType": (options: ["TypeRef"]),
281         "StructDef": (
282             traits: [
283                 "VisibilityOwner",
284                 "NameOwner",
285                 "TypeParamsOwner",
286                 "AttrsOwner",
287                 "DocCommentsOwner"
288             ]
289         ),
290         "RecordFieldDefList": (collections: [("fields", "RecordFieldDef")]),
291         "RecordFieldDef": (
292             traits: [
293                 "VisibilityOwner",
294                 "NameOwner",
295                 "AttrsOwner",
296                 "DocCommentsOwner",
297                 "TypeAscriptionOwner"
298             ]
299         ),
300         "TupleFieldDefList": (collections: [("fields", "TupleFieldDef")]),
301         "TupleFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
302         "EnumDef": ( traits: [
303             "VisibilityOwner",
304             "NameOwner",
305             "TypeParamsOwner",
306             "AttrsOwner",
307             "DocCommentsOwner"
308         ], options: [["variant_list", "EnumVariantList"]] ),
309         "EnumVariantList": ( collections: [("variants", "EnumVariant")] ),
310         "EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ),
311         "TraitDef": (
312             traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"],
313             options: ["ItemList"]
314         ),
315         "Module": (
316             traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner" ],
317             options: [ "ItemList" ]
318         ),
319         "ItemList": (
320             collections: [("impl_items", "ImplItem")],
321             traits: [ "FnDefOwner", "ModuleItemOwner" ],
322         ),
323         "ConstDef": (
324             traits: [
325                 "VisibilityOwner",
326                 "NameOwner",
327                 "TypeParamsOwner",
328                 "AttrsOwner",
329                 "DocCommentsOwner",
330                 "TypeAscriptionOwner",
331             ],
332             options: [ ["body","Expr"]],
333         ),
334         "StaticDef": (
335             traits: [
336                 "VisibilityOwner",
337                 "NameOwner",
338                 "TypeParamsOwner",
339                 "AttrsOwner",
340                 "DocCommentsOwner",
341                 "TypeAscriptionOwner",
342             ],
343             options: [ ["body","Expr"]],
344         ),
345         "TypeAliasDef": (
346             traits: [
347                 "VisibilityOwner",
348                 "NameOwner",
349                 "TypeParamsOwner",
350                 "AttrsOwner",
351                 "DocCommentsOwner",
352                 "TypeBoundsOwner",
353             ],
354             options: ["TypeRef"]
355         ),
356         "ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner", "AttrsOwner"]),
357
358         "ParenType": (options: ["TypeRef"]),
359         "TupleType": ( collections: [("fields", "TypeRef")] ),
360         "NeverType": (),
361         "PathType": (options: ["Path"]),
362         "PointerType": (options: ["TypeRef"]),
363         "ArrayType": ( options: ["TypeRef", "Expr"] ),
364         "SliceType": ( options: ["TypeRef"] ),
365         "ReferenceType": (options: ["TypeRef"]),
366         "PlaceholderType": (),
367         "FnPointerType": (options: ["ParamList", "RetType"]),
368         "ForType": (options: ["TypeRef"]),
369         "ImplTraitType": (
370             traits: ["TypeBoundsOwner"],
371         ),
372         "DynTraitType": (
373             traits: ["TypeBoundsOwner"],
374         ),
375
376         "TypeRef": ( enum: [
377             "ParenType",
378             "TupleType",
379             "NeverType",
380             "PathType",
381             "PointerType",
382             "ArrayType",
383             "SliceType",
384             "ReferenceType",
385             "PlaceholderType",
386             "FnPointerType",
387             "ForType",
388             "ImplTraitType",
389             "DynTraitType",
390         ]),
391
392         "NominalDef": (
393             enum: ["StructDef", "EnumDef"],
394             traits: [
395                 "NameOwner",
396                 "TypeParamsOwner",
397                 "AttrsOwner"
398             ],
399         ),
400         "ModuleItem": (
401             enum: ["StructDef", "EnumDef", "FnDef", "TraitDef", "TypeAliasDef", "ImplBlock",
402                    "UseItem", "ExternCrateItem", "ConstDef", "StaticDef", "Module" ]
403         ),
404         "ImplItem": (
405             enum: ["FnDef", "TypeAliasDef", "ConstDef"]
406         ),
407
408         "TupleExpr": (
409             collections: [("exprs", "Expr")]
410         ),
411         "ArrayExpr": (
412             collections: [("exprs", "Expr")]
413         ),
414         "ParenExpr": (options: ["Expr"]),
415         "PathExpr": (options: ["Path"]),
416         "LambdaExpr": (
417             options: [
418                 "ParamList",
419                 ["body", "Expr"],
420             ]
421         ),
422         "IfExpr": (
423             options: [ "Condition" ]
424         ),
425         "LoopExpr": (
426             traits: ["LoopBodyOwner"],
427         ),
428         "TryBlockExpr": (
429             options: [["body", "BlockExpr"]],
430         ),
431         "ForExpr": (
432             traits: ["LoopBodyOwner"],
433             options: [
434                 "Pat",
435                 ["iterable", "Expr"],
436             ]
437         ),
438         "WhileExpr": (
439             traits: ["LoopBodyOwner"],
440             options: [ "Condition" ]
441         ),
442         "ContinueExpr": (),
443         "BreakExpr": (options: ["Expr"]),
444         "Label": (),
445         "BlockExpr": (
446             options: [ "Block" ]
447         ),
448         "ReturnExpr": (options: ["Expr"]),
449         "MatchExpr": (
450             options: [ "Expr", "MatchArmList" ],
451         ),
452         "MatchArmList": (
453             collections: [ ("arms", "MatchArm") ],
454             traits: [ "AttrsOwner" ]
455         ),
456         "MatchArm": (
457             options: [
458                 [ "guard", "MatchGuard" ],
459                 "Expr",
460             ],
461             collections: [ ("pats", "Pat") ],
462             traits: [ "AttrsOwner" ]
463         ),
464         "MatchGuard": (options: ["Expr"]),
465         "RecordLit": (options: ["Path", "RecordFieldList"]),
466         "RecordFieldList": (
467             collections: [ ("fields", "RecordField") ],
468             options: [["spread", "Expr"]]
469         ),
470         "RecordField": (options: ["NameRef", "Expr"]),
471         "CallExpr": (
472             traits: ["ArgListOwner"],
473             options: [ "Expr" ],
474         ),
475         "MethodCallExpr": (
476             traits: ["ArgListOwner"],
477             options: [ "Expr", "NameRef", "TypeArgList" ],
478         ),
479         "IndexExpr": (),
480         "FieldExpr": (options: ["Expr", "NameRef"]),
481         "AwaitExpr": (options: ["Expr"]),
482         "TryExpr": (options: ["Expr"]),
483         "CastExpr": (options: ["Expr", "TypeRef"]),
484         "RefExpr": (options: ["Expr"]),
485         "PrefixExpr": (options: ["Expr"]),
486         "BoxExpr": (options: ["Expr"]),
487         "RangeExpr": (),
488         "BinExpr": (),
489
490         "Literal": (),
491
492         "Expr": (
493             enum: [
494                 "TupleExpr",
495                 "ArrayExpr",
496                 "ParenExpr",
497                 "PathExpr",
498                 "LambdaExpr",
499                 "IfExpr",
500                 "LoopExpr",
501                 "ForExpr",
502                 "WhileExpr",
503                 "ContinueExpr",
504                 "BreakExpr",
505                 "Label",
506                 "BlockExpr",
507                 "ReturnExpr",
508                 "MatchExpr",
509                 "RecordLit",
510                 "CallExpr",
511                 "IndexExpr",
512                 "MethodCallExpr",
513                 "FieldExpr",
514                 "AwaitExpr",
515                 "TryExpr",
516                 "TryBlockExpr",
517                 "CastExpr",
518                 "RefExpr",
519                 "PrefixExpr",
520                 "RangeExpr",
521                 "BinExpr",
522                 "Literal",
523                 "MacroCall",
524                 "BoxExpr",
525             ],
526         ),
527
528         "RefPat": ( options: [ "Pat" ]),
529         "BoxPat": ( options: [ "Pat" ]),
530         "BindPat": (
531             options: [ "Pat" ],
532             traits: ["NameOwner"]
533         ),
534         "PlaceholderPat": (),
535         "PathPat": ( options: [ "Path" ] ),
536         "RecordPat": ( options: ["RecordFieldPatList", "Path"] ),
537         "RecordFieldPatList": (
538             collections: [
539                 ("record_field_pats", "RecordFieldPat"),
540                 ("bind_pats", "BindPat"),
541             ]
542         ),
543         "RecordFieldPat": (
544             traits: ["NameOwner"],
545             options: ["Pat"]
546         ),
547         "TupleStructPat": (
548             options: ["Path"],
549             collections: [("args", "Pat")],
550         ),
551         "TuplePat": ( collections: [("args", "Pat")] ),
552         "SlicePat": (),
553         "RangePat": (),
554         "LiteralPat": (options: ["Literal"]),
555
556         "Pat": (
557             enum: [
558                 "RefPat",
559                 "BoxPat",
560                 "BindPat",
561                 "PlaceholderPat",
562                 "PathPat",
563                 "RecordPat",
564                 "TupleStructPat",
565                 "TuplePat",
566                 "SlicePat",
567                 "RangePat",
568                 "LiteralPat",
569             ],
570         ),
571
572         "Visibility": (),
573         "Name": (),
574         "NameRef": (),
575         "MacroCall": (
576             traits: [ "NameOwner", "AttrsOwner","DocCommentsOwner" ],
577             options: [ "TokenTree", "Path" ],
578         ),
579         "Attr": ( options: [ ["value", "TokenTree"] ] ),
580         "TokenTree": (),
581         "TypeParamList": (
582             collections: [
583                 ("type_params", "TypeParam" ),
584                 ("lifetime_params", "LifetimeParam" ),
585             ]
586         ),
587         "TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ),
588         "LifetimeParam": (
589             traits: ["AttrsOwner"],
590         ),
591         "TypeBound": (
592             options: [
593                 "TypeRef",
594             ]
595         ),
596         "TypeBoundList": (
597             collections: [
598                 ("bounds", "TypeBound"),
599             ]
600         ),
601         "WherePred": (
602             options: [
603                 "TypeRef",
604             ],
605             traits: [
606                 "TypeBoundsOwner",
607             ],
608         ),
609         "WhereClause": (
610             collections: [
611                 ("predicates", "WherePred"),
612             ],
613         ),
614         "ExprStmt": (
615             options: [ ["expr", "Expr"] ]
616         ),
617         "LetStmt": (
618             options: [
619                 ["pat", "Pat"],
620                 ["initializer", "Expr"],
621             ],
622             traits: [
623                 "TypeAscriptionOwner",
624             ]
625         ),
626         "Condition": (
627             options: [ "Pat", "Expr" ]
628         ),
629         "Stmt": (
630             enum: ["ExprStmt", "LetStmt"],
631         ),
632         "Block": (
633             options: [ "Expr" ],
634             collections: [
635                 ("statements", "Stmt"),
636             ],
637             traits: [
638                 "AttrsOwner",
639             ]
640         ),
641         "ParamList": (
642             options: [ "SelfParam" ],
643             collections: [
644                 ("params", "Param"),
645             ]
646         ),
647         "SelfParam": (
648             traits: [
649                 "TypeAscriptionOwner",
650                                 "AttrsOwner",
651             ]
652         ),
653         "Param": (
654             options: [ "Pat" ],
655             traits: [
656                 "TypeAscriptionOwner",
657                                 "AttrsOwner",
658             ]
659         ),
660         "UseItem": (
661             traits: ["AttrsOwner"],
662             options: [ "UseTree" ],
663         ),
664         "UseTree": (
665             options: [ "Path", "UseTreeList", "Alias" ]
666         ),
667         "Alias": (
668             traits: ["NameOwner"],
669         ),
670         "UseTreeList": (
671             collections: [("use_trees", "UseTree")]
672         ),
673         "ExternCrateItem": (
674             traits: ["AttrsOwner"],
675             options: ["NameRef", "Alias"],
676         ),
677         "ArgList": (
678             collections: [
679                 ("args", "Expr"),
680             ]
681         ),
682         "Path": (
683             options: [
684                 ["segment", "PathSegment"],
685                 ["qualifier", "Path"],
686             ]
687         ),
688         "PathSegment": (
689             options: [ "NameRef", "TypeArgList", "ParamList", "RetType" ]
690         ),
691         "TypeArgList": (collections: [
692             ("type_args", "TypeArg"),
693             ("lifetime_args", "LifetimeArg"),
694             ("assoc_type_args", "AssocTypeArg"),
695         ]),
696         "TypeArg": (options: ["TypeRef"]),
697         "AssocTypeArg": (options: ["NameRef", "TypeRef"]),
698         "LifetimeArg": (),
699
700         "MacroItems": (
701             traits: [ "ModuleItemOwner", "FnDefOwner" ],
702         ),
703
704         "MacroStmts" : (
705             options: [ "Expr" ],
706             collections: [
707                 ("statements", "Stmt"),
708             ],
709         )
710     },
711 )