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