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