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