]> git.lizzy.rs Git - rust.git/blob - crates/ra_syntax/src/grammar.ron
Merge #1377
[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     ],
100     contextual_keywords: [
101         "auto",
102         "default",
103         "union",
104     ],
105     literals: [
106         "INT_NUMBER",
107         "FLOAT_NUMBER",
108         "CHAR",
109         "BYTE",
110         "STRING",
111         "RAW_STRING",
112         "BYTE_STRING",
113         "RAW_BYTE_STRING",
114     ],
115     tokens: [
116         "ERROR",
117         "IDENT",
118         "WHITESPACE",
119         "LIFETIME",
120         "COMMENT",
121         "SHEBANG",
122         "L_DOLLAR",
123         "R_DOLLAR",
124     ],
125     nodes: [
126         "SOURCE_FILE",
127
128         "STRUCT_DEF",
129         "ENUM_DEF",
130         "FN_DEF",
131         "RET_TYPE",
132         "EXTERN_CRATE_ITEM",
133         "MODULE",
134         "USE_ITEM",
135         "STATIC_DEF",
136         "CONST_DEF",
137         "TRAIT_DEF",
138         "IMPL_BLOCK",
139         "TYPE_ALIAS_DEF",
140         "MACRO_CALL",
141         "TOKEN_TREE",
142
143         "PAREN_TYPE",
144         "TUPLE_TYPE",
145         "NEVER_TYPE",
146         "PATH_TYPE",
147         "POINTER_TYPE",
148         "ARRAY_TYPE",
149         "SLICE_TYPE",
150         "REFERENCE_TYPE",
151         "PLACEHOLDER_TYPE",
152         "FN_POINTER_TYPE",
153         "FOR_TYPE",
154         "IMPL_TRAIT_TYPE",
155         "DYN_TRAIT_TYPE",
156
157         "REF_PAT",
158         "BIND_PAT",
159         "PLACEHOLDER_PAT",
160         "PATH_PAT",
161         "STRUCT_PAT",
162         "FIELD_PAT_LIST",
163         "FIELD_PAT",
164         "TUPLE_STRUCT_PAT",
165         "TUPLE_PAT",
166         "SLICE_PAT",
167         "RANGE_PAT",
168         "LITERAL_PAT",
169
170         // atoms
171         "TUPLE_EXPR",
172         "ARRAY_EXPR",
173         "PAREN_EXPR",
174         "PATH_EXPR",
175         "LAMBDA_EXPR",
176         "IF_EXPR",
177         "WHILE_EXPR",
178         "CONDITION",
179         "LOOP_EXPR",
180         "FOR_EXPR",
181         "CONTINUE_EXPR",
182         "BREAK_EXPR",
183         "LABEL",
184         "BLOCK_EXPR",
185         "RETURN_EXPR",
186         "MATCH_EXPR",
187         "MATCH_ARM_LIST",
188         "MATCH_ARM",
189         "MATCH_GUARD",
190         "STRUCT_LIT",
191         "NAMED_FIELD_LIST",
192         "NAMED_FIELD",
193         "TRY_BLOCK_EXPR",
194
195         // postfix
196         "CALL_EXPR",
197         "INDEX_EXPR",
198         "METHOD_CALL_EXPR",
199         "FIELD_EXPR",
200         "TRY_EXPR",
201         "CAST_EXPR",
202
203         // unary
204         "REF_EXPR",
205         "PREFIX_EXPR",
206
207         "RANGE_EXPR", // just weird
208         "BIN_EXPR",
209
210         "BLOCK",
211         "EXTERN_BLOCK",
212         "EXTERN_ITEM_LIST",
213         "ENUM_VARIANT",
214         "NAMED_FIELD_DEF_LIST",
215         "NAMED_FIELD_DEF",
216         "POS_FIELD_DEF_LIST",
217         "POS_FIELD_DEF",
218         "ENUM_VARIANT_LIST",
219         "ITEM_LIST",
220         "ATTR",
221         "META_ITEM", // not an item actually
222         "USE_TREE",
223         "USE_TREE_LIST",
224         "PATH",
225         "PATH_SEGMENT",
226         "LITERAL",
227         "ALIAS",
228         "VISIBILITY",
229         "WHERE_CLAUSE",
230         "WHERE_PRED",
231         "ABI",
232         "NAME",
233         "NAME_REF",
234
235         "LET_STMT",
236         "EXPR_STMT",
237
238         "TYPE_PARAM_LIST",
239         "LIFETIME_PARAM",
240         "TYPE_PARAM",
241         "TYPE_ARG_LIST",
242         "LIFETIME_ARG",
243         "TYPE_ARG",
244         "ASSOC_TYPE_ARG",
245
246         "PARAM_LIST",
247         "PARAM",
248         "SELF_PARAM",
249         "ARG_LIST",
250         "TYPE_BOUND",
251         "TYPE_BOUND_LIST",
252
253         // macro related
254         "MACRO_ITEMS",
255         "MACRO_STMTS",
256     ],
257     ast: {
258         "SourceFile": (
259             traits: [ "ModuleItemOwner", "FnDefOwner" ],
260             collections: [
261                 ["modules", "Module"],
262             ]
263         ),
264         "FnDef": (
265             traits: [
266                 "VisibilityOwner",
267                 "NameOwner",
268                 "TypeParamsOwner",
269                 "AttrsOwner",
270                 "DocCommentsOwner"
271             ],
272             options: [ "ParamList", ["body", "Block"], "RetType" ],
273         ),
274         "RetType": (options: ["TypeRef"]),
275         "StructDef": (
276             traits: [
277                 "VisibilityOwner",
278                 "NameOwner",
279                 "TypeParamsOwner",
280                 "AttrsOwner",
281                 "DocCommentsOwner"
282             ]
283         ),
284         "NamedFieldDefList": (collections: [["fields", "NamedFieldDef"]]),
285         "NamedFieldDef": (
286             traits: [
287                 "VisibilityOwner",
288                 "NameOwner",
289                 "AttrsOwner",
290                 "DocCommentsOwner",
291                 "TypeAscriptionOwner"
292             ]
293         ),
294         "PosFieldDefList": (collections: [["fields", "PosFieldDef"]]),
295         "PosFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
296         "EnumDef": ( traits: [
297             "VisibilityOwner",
298             "NameOwner",
299             "TypeParamsOwner",
300             "AttrsOwner",
301             "DocCommentsOwner"
302         ], options: [["variant_list", "EnumVariantList"]] ),
303         "EnumVariantList": ( collections: [["variants", "EnumVariant"]] ),
304         "EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ),
305         "TraitDef": (
306             traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"],
307             options: ["ItemList"]
308         ),
309         "Module": (
310             traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner" ],
311             options: [ "ItemList" ]
312         ),
313         "ItemList": (
314             collections: [["impl_items", "ImplItem"]],
315             traits: [ "FnDefOwner", "ModuleItemOwner" ],
316         ),
317         "ConstDef": (
318             traits: [
319                 "VisibilityOwner",
320                 "NameOwner",
321                 "TypeParamsOwner",
322                 "AttrsOwner",
323                 "DocCommentsOwner",
324                 "TypeAscriptionOwner",
325             ],
326             options: [ ["body","Expr"]],
327         ),
328         "StaticDef": (
329             traits: [
330                 "VisibilityOwner",
331                 "NameOwner",
332                 "TypeParamsOwner",
333                 "AttrsOwner",
334                 "DocCommentsOwner",
335                 "TypeAscriptionOwner",
336             ],
337             options: [ ["body","Expr"]],
338         ),
339         "TypeAliasDef": (
340             traits: [
341                 "VisibilityOwner",
342                 "NameOwner",
343                 "TypeParamsOwner",
344                 "AttrsOwner",
345                 "DocCommentsOwner",
346                 "TypeBoundsOwner",
347             ],
348             options: ["TypeRef"]
349         ),
350         "ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner", "AttrsOwner"]),
351
352         "ParenType": (options: ["TypeRef"]),
353         "TupleType": ( collections: [["fields", "TypeRef"]] ),
354         "NeverType": (),
355         "PathType": (options: ["Path"]),
356         "PointerType": (options: ["TypeRef"]),
357         "ArrayType": ( options: ["TypeRef", "Expr"] ),
358         "SliceType": ( options: ["TypeRef"] ),
359         "ReferenceType": (options: ["TypeRef"]),
360         "PlaceholderType": (),
361         "FnPointerType": (options: ["ParamList", "RetType"]),
362         "ForType": (options: ["TypeRef"]),
363         "ImplTraitType": (
364             traits: ["TypeBoundsOwner"],
365         ),
366         "DynTraitType": (
367             traits: ["TypeBoundsOwner"],
368         ),
369
370         "TypeRef": ( enum: [
371             "ParenType",
372             "TupleType",
373             "NeverType",
374             "PathType",
375             "PointerType",
376             "ArrayType",
377             "SliceType",
378             "ReferenceType",
379             "PlaceholderType",
380             "FnPointerType",
381             "ForType",
382             "ImplTraitType",
383             "DynTraitType",
384         ]),
385
386         "NominalDef": (
387             enum: ["StructDef", "EnumDef"],
388             traits: [
389                 "NameOwner",
390                 "TypeParamsOwner",
391                 "AttrsOwner"
392             ],
393         ),
394         "ModuleItem": (
395             enum: ["StructDef", "EnumDef", "FnDef", "TraitDef", "TypeAliasDef", "ImplBlock",
396                    "UseItem", "ExternCrateItem", "ConstDef", "StaticDef", "Module" ]
397         ),
398         "ImplItem": (
399             enum: ["FnDef", "TypeAliasDef", "ConstDef"]
400         ),
401
402         "TupleExpr": (
403             collections: [["exprs", "Expr"]]
404         ),
405         "ArrayExpr": (
406             collections: [["exprs", "Expr"]]
407         ),
408         "ParenExpr": (options: ["Expr"]),
409         "PathExpr": (options: ["Path"]),
410         "LambdaExpr": (
411             options: [
412                 "ParamList",
413                 ["body", "Expr"],
414             ]
415         ),
416         "IfExpr": (
417             options: [ "Condition" ]
418         ),
419         "LoopExpr": (
420             traits: ["LoopBodyOwner"],
421         ),
422         "TryBlockExpr": (
423             traits: ["TryBlockBodyOwner"],
424         ),
425         "ForExpr": (
426             traits: ["LoopBodyOwner"],
427             options: [
428                 "Pat",
429                 ["iterable", "Expr"],
430             ]
431         ),
432         "WhileExpr": (
433             traits: ["LoopBodyOwner"],
434             options: [ "Condition" ]
435         ),
436         "ContinueExpr": (),
437         "BreakExpr": (options: ["Expr"]),
438         "Label": (),
439         "BlockExpr": (
440             options: [ "Block" ]
441         ),
442         "ReturnExpr": (options: ["Expr"]),
443         "MatchExpr": (
444             options: [ "Expr", "MatchArmList" ],
445         ),
446         "MatchArmList": (
447             collections: [ ["arms", "MatchArm"] ],
448             traits: [ "AttrsOwner" ]
449         ),
450         "MatchArm": (
451             options: [
452                 [ "guard", "MatchGuard" ],
453                 "Expr",
454             ],
455             collections: [ [ "pats", "Pat" ] ],
456             traits: [ "AttrsOwner" ]
457         ),
458         "MatchGuard": (options: ["Expr"]),
459         "StructLit": (options: ["Path", "NamedFieldList"]),
460         "NamedFieldList": (
461             collections: [ ["fields", "NamedField"] ],
462             options: [["spread", "Expr"]]
463         ),
464         "NamedField": (options: ["NameRef", "Expr"]),
465         "CallExpr": (
466             traits: ["ArgListOwner"],
467             options: [ "Expr" ],
468         ),
469         "MethodCallExpr": (
470             traits: ["ArgListOwner"],
471             options: [ "Expr", "NameRef", "TypeArgList" ],
472         ),
473         "IndexExpr": (),
474         "FieldExpr": (options: ["Expr", "NameRef"]),
475         "TryExpr": (options: ["Expr"]),
476         "CastExpr": (options: ["Expr", "TypeRef"]),
477         "RefExpr": (options: ["Expr"]),
478         "PrefixExpr": (options: ["Expr"]),
479         "RangeExpr": (),
480         "BinExpr": (),
481
482         "Literal": (),
483
484         "Expr": (
485             enum: [
486                 "TupleExpr",
487                 "ArrayExpr",
488                 "ParenExpr",
489                 "PathExpr",
490                 "LambdaExpr",
491                 "IfExpr",
492                 "LoopExpr",
493                 "ForExpr",
494                 "WhileExpr",
495                 "ContinueExpr",
496                 "BreakExpr",
497                 "Label",
498                 "BlockExpr",
499                 "ReturnExpr",
500                 "MatchExpr",
501                 "StructLit",
502                 "CallExpr",
503                 "IndexExpr",
504                 "MethodCallExpr",
505                 "FieldExpr",
506                 "TryExpr",
507                 "TryBlockExpr",
508                 "CastExpr",
509                 "RefExpr",
510                 "PrefixExpr",
511                 "RangeExpr",
512                 "BinExpr",
513                 "Literal",
514                 "MacroCall",
515             ],
516         ),
517
518         "RefPat": ( options: [ "Pat" ]),
519         "BindPat": (
520             options: [ "Pat" ],
521             traits: ["NameOwner"]
522         ),
523         "PlaceholderPat": (),
524         "PathPat": ( options: [ "Path" ] ),
525         "StructPat": ( options: ["FieldPatList", "Path"] ),
526         "FieldPatList": (
527             collections: [
528                 ["field_pats", "FieldPat"],
529                 ["bind_pats", "BindPat"],
530             ]
531         ),
532         "FieldPat": (
533             traits: ["NameOwner"],
534             options: ["Pat"]
535         ),
536         "TupleStructPat": (
537             options: ["Path"],
538             collections: [["args", "Pat"]],
539         ),
540         "TuplePat": ( collections: [["args", "Pat"]] ),
541         "SlicePat": (),
542         "RangePat": (),
543         "LiteralPat": (options: ["Literal"]),
544
545         "Pat": (
546             enum: [
547                 "RefPat",
548                 "BindPat",
549                 "PlaceholderPat",
550                 "PathPat",
551                 "StructPat",
552                 "TupleStructPat",
553                 "TuplePat",
554                 "SlicePat",
555                 "RangePat",
556                 "LiteralPat",
557             ],
558         ),
559
560         "Visibility": (),
561         "Name": (),
562         "NameRef": (),
563         "MacroCall": (
564             traits: [ "NameOwner", "AttrsOwner","DocCommentsOwner" ],
565             options: [ "TokenTree", "Path" ],
566         ),
567         "Attr": ( options: [ ["value", "TokenTree"] ] ),
568         "TokenTree": (),
569         "TypeParamList": (
570             collections: [
571                 ["type_params", "TypeParam" ],
572                 ["lifetime_params", "LifetimeParam" ],
573             ]
574         ),
575         "TypeParam": ( traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner", "DefaultTypeParamOwner"] ),
576         "LifetimeParam": (
577             traits: ["AttrsOwner"],
578         ),
579         "TypeBound": (
580             options: [
581                 "TypeRef",
582             ]
583         ),
584         "TypeBoundList": (
585             collections: [
586                 ["bounds", "TypeBound"],
587             ]
588         ),
589         "WherePred": (
590             options: [
591                 "TypeRef",
592             ],
593             traits: [
594                 "TypeBoundsOwner",
595             ],
596         ),
597         "WhereClause": (
598             collections: [
599                 ["predicates", "WherePred"],
600             ],
601         ),
602         "ExprStmt": (
603             options: [ ["expr", "Expr"] ]
604         ),
605         "LetStmt": (
606             options: [
607                 ["pat", "Pat"],
608                 ["initializer", "Expr"],
609             ],
610             traits: [
611                 "TypeAscriptionOwner",
612             ]
613         ),
614         "Condition": (
615             options: [ "Pat", "Expr" ]
616         ),
617         "Stmt": (
618             enum: ["ExprStmt", "LetStmt"],
619         ),
620         "Block": (
621             options: [ "Expr" ],
622             collections: [
623                 ["statements", "Stmt"],
624             ],
625             traits: [
626                 "AttrsOwner",
627             ]
628         ),
629         "ParamList": (
630             options: [ "SelfParam" ],
631             collections: [
632                 ["params", "Param"]
633             ]
634         ),
635         "SelfParam": (
636             traits: [
637                 "TypeAscriptionOwner",
638             ]
639         ),
640         "Param": (
641             options: [ "Pat" ],
642             traits: [
643                 "TypeAscriptionOwner",
644             ]
645         ),
646         "UseItem": (
647             traits: ["AttrsOwner"],
648             options: [ "UseTree" ],
649         ),
650         "UseTree": (
651             options: [ "Path", "UseTreeList", "Alias" ]
652         ),
653         "Alias": (
654             traits: ["NameOwner"],
655         ),
656         "UseTreeList": (
657             collections: [["use_trees", "UseTree"]]
658         ),
659         "ExternCrateItem": (
660             options: ["NameRef", "Alias"],
661         ),
662         "ArgList": (
663             collections: [
664                 ["args", "Expr"]
665             ]
666         ),
667         "Path": (
668             options: [
669                 ["segment", "PathSegment"],
670                 ["qualifier", "Path"],
671             ]
672         ),
673         "PathSegment": (
674             options: [ "NameRef", "TypeArgList" ]
675         ),
676         "TypeArgList": (collections: [
677             ["type_args", "TypeArg"],
678             ["lifetime_args", "LifetimeArg"],
679             ["assoc_type_args", "AssocTypeArg"],
680         ]),
681         "TypeArg": (options: ["TypeRef"]),
682         "AssocTypeArg": (options: ["NameRef", "TypeRef"]),
683         "LifetimeArg": (),
684
685         "MacroItems": (
686             traits: [ "ModuleItemOwner", "FnDefOwner" ],            
687         ),
688
689         "MacroStmts" : (
690             options: [ "Expr" ],
691             collections: [
692                 ["statements", "Stmt"],
693             ],
694         )
695     },
696 )