1 //! Defines input for code generation process.
3 pub(crate) struct KindsSrc<'a> {
4 pub(crate) punct: &'a [(&'a str, &'a str)],
5 pub(crate) keywords: &'a [&'a str],
6 pub(crate) contextual_keywords: &'a [&'a str],
7 pub(crate) literals: &'a [&'a str],
8 pub(crate) tokens: &'a [&'a str],
9 pub(crate) nodes: &'a [&'a str],
12 pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
68 "as", "async", "await", "box", "break", "const", "continue", "crate", "dyn", "else",
69 "enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "macro",
70 "match", "mod", "move", "mut", "pub", "ref", "return", "self", "static", "struct", "super",
71 "trait", "true", "try", "type", "unsafe", "use", "where", "while",
73 contextual_keywords: &["auto", "default", "existential", "union", "raw"],
134 "RECORD_FIELD_PAT_LIST",
178 "RANGE_EXPR", // just weird
183 "RECORD_FIELD_DEF_LIST",
185 "TUPLE_FIELD_DEF_LIST",
190 "META_ITEM", // not an item actually
226 pub(crate) struct AstSrc<'a> {
227 pub(crate) tokens: &'a [&'a str],
228 pub(crate) nodes: &'a [AstNodeSrc<'a>],
229 pub(crate) enums: &'a [AstEnumSrc<'a>],
232 pub(crate) struct AstNodeSrc<'a> {
233 pub(crate) doc: &'a [&'a str],
234 pub(crate) name: &'a str,
235 pub(crate) traits: &'a [&'a str],
236 pub(crate) fields: &'a [Field<'a>],
239 pub(crate) enum Field<'a> {
241 Node { name: &'a str, src: FieldSrc<'a> },
244 pub(crate) enum FieldSrc<'a> {
250 pub(crate) struct AstEnumSrc<'a> {
251 pub(crate) doc: &'a [&'a str],
252 pub(crate) name: &'a str,
253 pub(crate) traits: &'a [&'a str],
254 pub(crate) variants: &'a [&'a str],
257 macro_rules! ast_nodes {
259 $(#[doc = $doc:expr])+
260 struct $name:ident$(: $($trait:ident),*)? {
261 $($field_name:ident $(![$token:tt])? $(: $ty:tt)?),*$(,)?
267 name: stringify!($name),
268 traits: &[$($(stringify!($trait)),*)?],
270 $(field!($(T![$token])? $field_name $($ty)?)),*
279 (T![$token:tt] T) => {
280 Field::Token(stringify!($token))
282 ($field_name:ident) => {
283 Field::Node { name: stringify!($field_name), src: FieldSrc::Shorthand }
285 ($field_name:ident [$ty:ident]) => {
286 Field::Node { name: stringify!($field_name), src: FieldSrc::Many(stringify!($ty)) }
288 ($field_name:ident $ty:ident) => {
289 Field::Node { name: stringify!($field_name), src: FieldSrc::Optional(stringify!($ty)) }
293 macro_rules! ast_enums {
295 $(#[doc = $doc:expr])+
296 enum $name:ident $(: $($trait:ident),*)? {
297 $($variant:ident),*$(,)?
303 name: stringify!($name),
304 traits: &[$($(stringify!($trait)),*)?],
305 variants: &[$(stringify!($variant)),*],
311 pub(crate) const AST_SRC: AstSrc = AstSrc {
312 tokens: &["Whitespace", "Comment", "String", "RawString"],
314 /// The entire Rust source file. Includes all top-level inner attributes and module items.
316 /// [Reference](https://doc.rust-lang.org/reference/crates-and-source-files.html)
317 struct SourceFile: ModuleItemOwner, AttrsOwner, DocCommentsOwner {
321 /// Function definition either with body or not.
322 /// Includes all of its attributes and doc comments.
328 /// pub extern "C" fn foo<T>(#[attr] Patern {p}: Pattern) -> u32
337 /// ❰ fn fn_decl(also_variadic_ffi: u32, ...) -> u32; ❱
341 /// - [Reference](https://doc.rust-lang.org/reference/items/functions.html)
342 /// - [Nomicon](https://doc.rust-lang.org/nomicon/ffi.html#variadic-functions)
343 struct FnDef: VisibilityOwner, NameOwner, TypeParamsOwner, DocCommentsOwner, AttrsOwner {
356 /// Return type annotation.
359 /// fn foo(a: u32) ❰ -> Option<u32> ❱ { Some(a) }
362 /// [Reference](https://doc.rust-lang.org/reference/items/functions.html)
363 struct RetType { T![->], TypeRef }
365 /// Struct definition.
366 /// Includes all of its attributes and doc comments.
372 /// struct Foo<T> where T: Debug {
381 /// ❰ struct Foo<T>(#[attr] T) where T: Debug; ❱
384 /// [Reference](https://doc.rust-lang.org/reference/items/structs.html)
385 struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
391 /// Union definition.
392 /// Includes all of its attributes and doc comments.
398 /// pub union Foo<T> where T: Debug {
407 /// [Reference](https://doc.rust-lang.org/reference/items/unions.html)
408 struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
413 /// Record field definition list including enclosing curly braces.
416 /// struct Foo // same for union
425 /// [Reference](https://doc.rust-lang.org/reference/items/structs.html)
426 struct RecordFieldDefList { T!['{'], fields: [RecordFieldDef], T!['}'] }
428 /// Record field definition including its attributes and doc comments.
443 /// [Reference](https://doc.rust-lang.org/reference/items/structs.html)
444 struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { }
446 /// Tuple field definition list including enclosing parens.
449 /// struct Foo ❰ (u32, String, Vec<u32>) ❱;
452 /// [Reference](https://doc.rust-lang.org/reference/items/structs.html)
453 struct TupleFieldDefList { T!['('], fields: [TupleFieldDef], T![')'] }
455 /// Tuple field definition including its attributes.
458 /// struct Foo(❰ #[attr] u32 ❱);
461 /// [Reference](https://doc.rust-lang.org/reference/items/structs.html)
462 struct TupleFieldDef: VisibilityOwner, AttrsOwner {
467 /// Includes all of its attributes and doc comments.
473 /// pub enum Foo<T> where T: Debug {
477 /// Baz(#[attr] u32),
488 /// [Reference](https://doc.rust-lang.org/reference/items/enumerations.html)
489 struct EnumDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
491 variant_list: EnumVariantList,
494 /// Enum variant definition list including enclosing curly braces.
509 /// [Reference](https://doc.rust-lang.org/reference/items/enumerations.html)
510 struct EnumVariantList {
512 variants: [EnumVariant],
516 /// Enum variant definition including its attributes and discriminant value definition.
526 /// // same for tuple and record variants
530 /// [Reference](https://doc.rust-lang.org/reference/items/enumerations.html)
531 struct EnumVariant: VisibilityOwner, NameOwner, DocCommentsOwner, AttrsOwner {
537 /// Trait definition.
538 /// Includes all of its attributes and doc comments.
544 /// pub unsafe trait Foo<T>: Debug where T: Debug {
550 /// [Reference](https://doc.rust-lang.org/reference/items/traits.html)
551 struct TraitDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeParamsOwner, TypeBoundsOwner {
558 /// Module definition either with body or not.
559 /// Includes all of its inner and outer attributes, module items, doc comments.
578 /// [Reference](https://doc.rust-lang.org/reference/items/modules.html)
579 struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner {
585 /// Item defintion list.
586 /// This is used for both top-level items and impl block items.
594 /// const BRUUH: u32 = 42;
601 /// const BAZ: u32 = 42;
606 /// [Reference](https://doc.rust-lang.org/reference/items.html)
607 struct ItemList: ModuleItemOwner {
609 assoc_items: [AssocItem],
613 /// Constant variable definition.
614 /// Includes all of its attributes and doc comments.
620 /// pub const FOO: u32 = 42;
624 /// [Reference](https://doc.rust-lang.org/reference/items/constant-items.html)
625 struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
634 /// Static variable definition.
635 /// Includes all of its attributes and doc comments.
641 /// pub static mut FOO: u32 = 42;
645 /// [Reference](https://doc.rust-lang.org/reference/items/static-items.html)
646 struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
654 /// Type alias definition.
655 /// Includes associated type clauses with type bounds.
661 /// pub type Foo<T> where T: Debug = T;
665 /// ❰ type Baz: Debug; ❱
666 /// ❰ type Bruh = String; ❱
667 /// ❰ type Bruuh: Debug = u32; ❱
671 /// [Reference](https://doc.rust-lang.org/reference/items/type-aliases.html)
672 struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner {
680 /// Inherent and trait impl definition.
681 /// Includes all of its inner and outer attributes.
686 /// unsafe impl<T> const !Foo for Bar where T: Debug {
693 /// [Reference](https://doc.rust-lang.org/reference/items/implementations.html)
694 struct ImplDef: TypeParamsOwner, AttrsOwner, DocCommentsOwner {
705 /// Parenthesized type reference.
706 /// Note: parens are only used for grouping, this is not a tuple type.
709 /// // This is effectively just `u32`.
710 /// // Single-item tuple must be defined with a trailing comma: `(u32,)`
711 /// type Foo = ❰ (u32) ❱;
713 /// let bar: &'static ❰ (dyn Debug) ❱ = "bruh";
715 struct ParenType { T!['('], TypeRef, T![')'] }
717 /// Unnamed tuple type.
720 /// let foo: ❰ (u32, bool) ❱ = (42, true);
723 /// [Reference](https://doc.rust-lang.org/reference/types/tuple.html)
724 struct TupleType { T!['('], fields: [TypeRef], T![')'] }
726 /// The never type (i.e. the exclamation point).
731 /// fn no_return() -> ❰ ! ❱ {
736 /// [Reference](https://doc.rust-lang.org/reference/types/never.html)
737 struct NeverType { T![!] }
740 /// Includes single identifier type names and elaborate paths with
741 /// generic parameters.
744 /// type Foo = ❰ String ❱;
745 /// type Bar = ❰ std::vec::Vec<T> ❱;
746 /// type Baz = ❰ ::bruh::<Bruuh as Iterator>::Item ❱;
749 /// [Reference](https://doc.rust-lang.org/reference/paths.html)
750 struct PathType { Path }
752 /// Raw pointer type.
755 /// type Foo = ❰ *const u32 ❱;
756 /// type Bar = ❰ *mut u32 ❱;
759 /// [Reference](https://doc.rust-lang.org/reference/types/pointer.html#raw-pointers-const-and-mut)
760 struct PointerType { T![*], T![const], T![mut], TypeRef }
765 /// type Foo = ❰ [u32; 24 - 3] ❱;
768 /// [Reference](https://doc.rust-lang.org/reference/types/array.html)
769 struct ArrayType { T!['['], TypeRef, T![;], Expr, T![']'] }
774 /// type Foo = ❰ [u8] ❱;
777 /// [Reference](https://doc.rust-lang.org/reference/types/slice.html)
778 struct SliceType { T!['['], TypeRef, T![']'] }
783 /// type Foo = ❰ &'static str ❱;
786 /// [Reference](https://doc.rust-lang.org/reference/types/pointer.html)
787 struct ReferenceType { T![&], T![lifetime], T![mut], TypeRef }
789 /// Placeholder type (i.e. the underscore).
792 /// let foo: ❰ _ ❱ = 42_u32;
795 /// [Reference](https://doc.rust-lang.org/reference/types/inferred.html)
796 struct PlaceholderType { T![_] }
798 /// Function pointer type (not to be confused with `Fn*` family of traits).
801 /// type Foo = ❰ async fn(#[attr] u32, named: bool) -> u32 ❱;
803 /// type Bar = ❰ extern "C" fn(variadic: u32, #[attr] ...) ❱;
806 /// [Reference](https://doc.rust-lang.org/reference/types/function-pointer.html)
807 struct FnPointerType { Abi, T![unsafe], T![fn], ParamList, RetType }
809 /// Higher order type.
812 /// type Foo = ❰ for<'a> fn(&'a str) ❱;
815 /// [Reference](https://doc.rust-lang.org/nomicon/hrtb.html)
816 struct ForType { T![for], TypeParamList, TypeRef }
818 /// Opaque `impl Trait` type.
821 /// fn foo(bar: ❰ impl Debug + Eq ❱) {}
824 /// [Reference](https://doc.rust-lang.org/reference/types/impl-trait.html)
825 struct ImplTraitType: TypeBoundsOwner { T![impl] }
827 /// Trait object type.
830 /// type Foo = ❰ dyn Debug ❱;
833 /// [Reference](https://doc.rust-lang.org/reference/types/trait-object.html)
834 struct DynTraitType: TypeBoundsOwner { T![dyn] }
842 /// [Reference](https://doc.rust-lang.org/reference/expressions/tuple-expr.html)
843 struct TupleExpr: AttrsOwner { T!['('], exprs: [Expr], T![')'] }
848 /// ❰ [#![inner_attr] true, false, true] ❱;
853 /// [Reference](https://doc.rust-lang.org/reference/expressions/array-expr.html)
854 struct ArrayExpr: AttrsOwner { T!['['], exprs: [Expr], T![;], T![']'] }
856 /// Parenthesized expression.
857 /// Note: parens are only used for grouping, this is not a tuple literal.
860 /// ❰ (#![inner_attr] 2 + 2) ❱ * 2;
863 /// [Reference](https://doc.rust-lang.org/reference/expressions/grouped-expr.html)
864 struct ParenExpr: AttrsOwner { T!['('], Expr, T![')'] }
866 /// Path to a symbol in expression context.
867 /// Includes single identifier variable names and elaborate paths with
868 /// generic parameters.
873 /// ❰ Vec::<i32>::push ❱;
874 /// ❰ <[i32]>::reverse ❱;
875 /// ❰ <String as std::borrow::Borrow<str>>::borrow ❱;
878 /// [Reference](https://doc.rust-lang.org/reference/expressions/path-expr.html)
879 struct PathExpr { Path }
881 /// Anonymous callable object literal a.k.a. closure, lambda or functor.
885 /// ❰ |a: u32| val + 1 ❱;
886 /// ❰ async |#[attr] Pattern(_): Pattern| { bar } ❱;
888 /// ❰ || -> u32 { closure_with_ret_type_annotation_requires_block_expr } ❱
891 /// [Reference](https://doc.rust-lang.org/reference/expressions/closure-expr.html)
892 struct LambdaExpr: AttrsOwner {
893 // T![static], // TODO: what's this?
901 /// If expression. Includes both regular `if` and `if let` forms.
902 /// Beware that `else if` is a special case syntax sugar, because in general
903 /// there has to be block expression after `else`.
906 /// ❰ if bool_cond { 42 } ❱
907 /// ❰ if bool_cond { 42 } else { 24 } ❱
908 /// ❰ if bool_cond { 42 } else if bool_cond2 { 42 } ❱
911 /// if let Pattern(foo) = bar {
919 /// [Reference](https://doc.rust-lang.org/reference/expressions/if-expr.html)
920 struct IfExpr: AttrsOwner { T![if], Condition }
922 /// Unconditional loop expression.
927 /// // yeah, it's that simple...
932 /// [Reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html)
933 struct LoopExpr: AttrsOwner, LoopBodyOwner { T![loop] }
935 /// Block expression with an optional prefix (label, try ketword,
936 /// unsafe keyword, async keyword...).
946 /// - [try block](https://doc.rust-lang.org/unstable-book/language-features/try-blocks.html)
947 /// - [unsafe block](https://doc.rust-lang.org/reference/expressions/block-expr.html#unsafe-blocks)
948 /// - [async block](https://doc.rust-lang.org/reference/expressions/block-expr.html#async-blocks)
949 struct EffectExpr: AttrsOwner { Label, T![try], T![unsafe], T![async], BlockExpr }
952 /// For loop expression.
953 /// Note: record struct literals are not valid as iterable expression
954 /// due to ambiguity.
958 /// for i in (0..4) {
964 /// [Reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html#iterator-loops)
965 struct ForExpr: AttrsOwner, LoopBodyOwner {
972 /// While loop expression. Includes both regular `while` and `while let` forms.
976 /// while bool_cond {
981 /// while let Pattern(foo) = bar {
987 /// [Reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops)
988 struct WhileExpr: AttrsOwner, LoopBodyOwner { T![while], Condition }
990 /// Continue expression.
993 /// while bool_cond {
999 /// ❰ continue 'outer ❱;
1005 /// [Reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html#continue-expressions)
1006 struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] }
1008 /// Break expression.
1011 /// while bool_cond {
1015 /// for foo in bar {
1016 /// ❰ break 'outer ❱;
1021 /// ❰ break 'outer 42 ❱;
1026 /// [Refernce](https://doc.rust-lang.org/reference/expressions/loop-expr.html#break-expressions)
1027 struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr }
1032 /// ❰ 'outer: ❱ loop {}
1034 /// let foo = ❰ 'bar: ❱ loop {}
1041 /// [Reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html?highlight=label#loop-labels)
1042 /// [Labels for blocks RFC](https://github.com/rust-lang/rfcs/blob/master/text/2046-label-break-value.md)
1043 struct Label { T![lifetime] }
1045 /// Block expression. Includes unsafe blocks and block labels.
1053 /// ❰ 'label: { break 'label } ❱
1058 /// [Reference](https://doc.rust-lang.org/reference/expressions/block-expr.html)
1059 /// [Labels for blocks RFC](https://github.com/rust-lang/rfcs/blob/master/text/2046-label-break-value.md)
1060 struct BlockExpr: AttrsOwner, ModuleItemOwner {
1061 T!['{'], statements: [Stmt], Expr, T!['}'],
1064 /// Return expression.
1067 /// || ❰ return 42 ❱;
1074 /// [Reference](https://doc.rust-lang.org/reference/expressions/return-expr.html)
1075 struct ReturnExpr: AttrsOwner { Expr }
1077 /// Call expression (not to be confused with method call expression, it is
1078 /// a separate ast node).
1082 /// ❰ &str::len("bar") ❱;
1083 /// ❰ <&str as PartialEq<&str>>::eq(&"", &"") ❱;
1086 /// [Reference](https://doc.rust-lang.org/reference/expressions/call-expr.html)
1087 struct CallExpr: ArgListOwner { Expr }
1089 /// Method call expression.
1092 /// ❰ receiver_expr.method() ❱;
1093 /// ❰ receiver_expr.method::<T>(42, true) ❱;
1095 /// ❰ ❰ ❰ foo.bar() ❱ .baz() ❱ .bruh() ❱;
1098 /// [Reference](https://doc.rust-lang.org/reference/expressions/method-call-expr.html)
1099 struct MethodCallExpr: AttrsOwner, ArgListOwner {
1100 Expr, T![.], NameRef, TypeArgList,
1103 /// Index expression a.k.a. subscript operator call.
1109 /// [Reference](https://doc.rust-lang.org/reference/expressions/array-expr.html)
1110 struct IndexExpr: AttrsOwner { T!['['], T![']'] }
1112 /// Field access expression.
1117 /// ❰ ❰ ❰ foo.bar ❱ .baz ❱ .bruh ❱;
1120 /// [Reference](https://doc.rust-lang.org/reference/expressions/field-expr.html)
1121 struct FieldExpr: AttrsOwner { Expr, T![.], NameRef }
1123 /// Await operator call expression.
1129 /// [Reference](https://doc.rust-lang.org/reference/expressions/await-expr.html)
1130 struct AwaitExpr: AttrsOwner { Expr, T![.], T![await] }
1132 /// The question mark operator call.
1138 /// [Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#the-question-mark-operator)
1139 struct TryExpr: AttrsOwner { Expr, T![?] }
1141 /// Type cast expression.
1147 /// [Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#type-cast-expressions)
1148 struct CastExpr: AttrsOwner { Expr, T![as], TypeRef }
1151 /// Borrow operator call.
1158 /// [Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#borrow-operators)
1159 struct RefExpr: AttrsOwner { T![&], T![raw], T![mut], Expr }
1161 /// Prefix operator call. This is either `!` or `*` or `-`.
1169 /// [Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html)
1170 struct PrefixExpr: AttrsOwner { /*PrefixOp,*/ Expr }
1172 /// Box operator call.
1178 /// [RFC](https://github.com/rust-lang/rfcs/blob/0806be4f282144cfcd55b1d20284b43f87cbe1c6/text/0809-box-and-in-for-stdlib.md)
1179 struct BoxExpr: AttrsOwner { T![box], Expr }
1181 /// Range operator call.
1192 /// [Reference](https://doc.rust-lang.org/reference/expressions/range-expr.html)
1193 struct RangeExpr: AttrsOwner { /*RangeOp*/ }
1196 /// Binary operator call.
1197 /// Includes all arithmetic, logic, bitwise and assignment operators.
1200 /// ❰ 2 + ❰ 2 * 2 ❱ ❱;
1201 /// ❰ ❰ true && false ❱ || true ❱;
1204 /// [Reference](https://doc.rust-lang.org/reference/expressions/operator-expr.html#arithmetic-and-logical-binary-operators)
1205 struct BinExpr: AttrsOwner { /*BinOp*/ }
1208 /// [Raw] string, [raw] byte string, char, byte, integer, float or bool literal.
1212 /// ❰ br##"raw byte str"## ❱;
1220 /// [Reference](https://doc.rust-lang.org/reference/expressions/literal-expr.html)
1221 struct Literal { /*LiteralToken*/ }
1223 /// Match expression.
1234 /// [Reference](https://doc.rust-lang.org/reference/expressions/match-expr.html)
1235 struct MatchExpr: AttrsOwner { T![match], Expr, MatchArmList }
1237 /// Match arm list part of match expression. Includes its inner attributes.
1250 /// [Reference](https://doc.rust-lang.org/reference/expressions/match-expr.html)
1251 struct MatchArmList: AttrsOwner { T!['{'], arms: [MatchArm], T!['}'] }
1255 /// Note: record struct literals are not valid as target match expression
1256 /// due to ambiguity.
1259 /// ❰ #[attr] Pattern(it) if bool_cond => it ❱,
1263 /// [Reference](https://doc.rust-lang.org/reference/expressions/match-expr.html)
1264 struct MatchArm: AttrsOwner {
1275 /// Pattern(it) ❰ if bool_cond ❱ => it,
1279 /// [Reference](https://doc.rust-lang.org/reference/expressions/match-expr.html#match-guards)
1280 struct MatchGuard { T![if], Expr }
1282 /// Record literal expression. The same syntax is used for structs,
1283 /// unions and record enum variants.
1296 /// [Reference](https://doc.rust-lang.org/reference/expressions/struct-expr.html)
1297 struct RecordLit { Path, RecordFieldList}
1299 /// Record field list including enclosing curly braces.
1308 /// [Reference](https://doc.rust-lang.org/reference/expressions/struct-expr.html)
1309 struct RecordFieldList {
1311 fields: [RecordField],
1321 /// ❰ #[attr] baz: 42 ❱
1325 /// [Reference](https://doc.rust-lang.org/reference/expressions/struct-expr.html)
1326 struct RecordField: AttrsOwner { NameRef, T![:], Expr }
1328 /// Disjunction of patterns.
1331 /// let ❰ Foo(it) | Bar(it) | Baz(it) ❱ = bruh;
1334 /// [Reference](https://doc.rust-lang.org/reference/patterns.html)
1335 struct OrPat { pats: [Pat] }
1337 /// Parenthesized pattern.
1338 /// Note: parens are only used for grouping, this is not a tuple pattern.
1341 /// if let ❰ &(0..=42) ❱ = foo {}
1344 /// https://doc.rust-lang.org/reference/patterns.html#grouped-patterns
1345 struct ParenPat { T!['('], Pat, T![')'] }
1347 /// Reference pattern.
1348 /// Note: this has nothing to do with `ref` keyword, the latter is used in bind patterns.
1351 /// let ❰ &mut foo ❱ = bar;
1353 /// let ❰ & ❰ &mut ❰ &_ ❱ ❱ ❱ = baz;
1356 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#reference-patterns)
1357 struct RefPat { T![&], T![mut], Pat }
1362 /// let ❰ box foo ❱ = box 42;
1365 /// [Unstable book](https://doc.rust-lang.org/unstable-book/language-features/box-patterns.html)
1366 struct BoxPat { T![box], Pat }
1372 /// Some(❰ ref mut bar ❱) => {}
1373 /// ❰ baz @ None ❱ => {}
1377 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#identifier-patterns)
1378 struct BindPat: AttrsOwner, NameOwner { T![ref], T![mut], T![@], Pat }
1380 /// Placeholder pattern a.k.a. the wildcard pattern or the underscore.
1383 /// let ❰ _ ❱ = foo;
1386 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#wildcard-pattern)
1387 struct PlaceholderPat { T![_] }
1389 /// Rest-of-the record/tuple pattern.
1390 /// Note: this is not the unbonded range pattern (even more: it doesn't exist).
1393 /// let Foo { bar, ❰ .. ❱ } = baz;
1394 /// let (❰ .. ❱, bruh) = (42, 24, 42);
1395 /// let Bruuh(❰ .. ❱) = bruuuh;
1398 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#struct-patterns)
1399 struct DotDotPat { T![..] }
1402 /// Doesn't include the underscore pattern (it is a special case, namely `PlaceholderPat`).
1405 /// let ❰ foo::bar::Baz ❱ { .. } = bruh;
1406 /// if let ❰ CONST ❱ = 42 {}
1409 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#path-patterns)
1410 struct PathPat { Path }
1415 /// let ❰ [foo, bar, baz] ❱ = [1, 2, 3];
1418 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#slice-patterns)
1419 struct SlicePat { T!['['], args: [Pat], T![']'] }
1426 /// ❰ 0..=42 ❱ => {}
1430 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#range-patterns)
1431 struct RangePat { /*RangeSeparator*/ } // TODO: where is RangeSeparator?
1433 /// Literal pattern.
1434 /// Includes only bool, number, char, and string literals.
1438 /// Number(❰ 42 ❱) => {}
1439 /// String(❰ "42" ❱) => {}
1440 /// Bool(❰ true ❱) => {}
1444 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#literal-patterns)
1445 struct LiteralPat { Literal }
1447 /// Macro invocation in pattern position.
1450 /// let ❰ foo!(my custom syntax) ❱ = baz;
1453 /// [Reference](https://doc.rust-lang.org/reference/macros.html#macro-invocation)
1454 struct MacroPat { MacroCall }
1456 /// Record literal pattern.
1459 /// let ❰ foo::Bar { baz, .. } ❱ = bruh;
1462 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#struct-patterns)
1463 struct RecordPat { RecordFieldPatList, Path }
1465 /// Record literal's field patterns list including enclosing curly braces.
1468 /// let foo::Bar ❰ { baz, bind @ bruh, .. } ❱ = bruuh;
1471 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#struct-patterns)
1472 struct RecordFieldPatList {
1474 pats: [RecordInnerPat],
1475 record_field_pats: [RecordFieldPat],
1476 bind_pats: [BindPat],
1481 /// Record literal's field pattern.
1482 /// Note: record literal can also match tuple structs.
1485 /// let Foo { ❰ bar: _ ❱ } = baz;
1486 /// let TupleStruct { ❰ 0: _ ❱ } = bruh;
1489 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#struct-patterns)
1490 struct RecordFieldPat: AttrsOwner { NameRef, T![:], Pat }
1492 /// Tuple struct literal pattern.
1495 /// let ❰ foo::Bar(baz, bruh) ❱ = bruuh;
1498 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#tuple-struct-patterns)
1499 struct TupleStructPat { Path, T!['('], args: [Pat], T![')'] }
1502 /// Note: this doesn't include tuple structs (see `TupleStructPat`)
1505 /// let ❰ (foo, bar, .., baz) ❱ = bruh;
1508 /// [Reference](https://doc.rust-lang.org/reference/patterns.html#tuple-patterns)
1509 struct TuplePat { T!['('], args: [Pat], T![')'] }
1514 /// ❰ pub mod ❱ foo;
1515 /// ❰ pub(crate) ❱ struct Bar;
1516 /// ❰ pub(self) ❱ enum Baz {}
1517 /// ❰ pub(super) ❱ fn bruh() {}
1518 /// ❰ pub(in bruuh::bruuuh) ❱ type T = u64;
1521 /// [Reference](https://doc.rust-lang.org/reference/visibility-and-privacy.html)
1522 struct Visibility { T![pub], T![super], T![self], T![crate] }
1524 /// Single identifier.
1525 /// // TODO: clarify the difference between Name and NameRef
1528 /// let ❰ foo ❱ = bar;
1530 /// fn ❰ bruh ❱() {}
1533 /// [Reference](https://doc.rust-lang.org/reference/identifiers.html)
1534 struct Name { T![ident] }
1536 /// Reference to a name.
1539 /// let foo = ❰ bar ❱(❰ Baz(❰ bruh ❱) ❱;
1542 /// [Reference](https://doc.rust-lang.org/reference/identifiers.html)
1543 struct NameRef { /*NameRefToken*/ } // TODO: where is NameRefToken?
1546 /// Includes all of its attributes and doc comments.
1552 /// macro_rules! foo { // macro rules is also a macro call
1553 /// ($bar: tt) => {}
1560 /// [Reference](https://doc.rust-lang.org/reference/macros.html)
1561 struct MacroCall: NameOwner, AttrsOwner, DocCommentsOwner {
1562 Path, T![!], TokenTree, T![;] // TODO: what is the meaning of the semicolon here?
1568 /// ❰ #![inner_attr] ❱
1571 /// ❰ #[foo = "bar"] ❱
1572 /// ❰ #[baz(bruh::bruuh = "42")] ❱
1576 /// [Reference](https://doc.rust-lang.org/reference/attributes.html)
1577 struct Attr { T![#], T![!], T!['['], Path, T![=], input: AttrInput, T![']'] }
1579 /// Stores a list of lexer tokens and other `TokenTree`s.
1580 /// It appears in attributes, macro_rules and macro call (foo!)
1583 /// macro_call! ❰ { my syntax here } ❱;
1586 /// [Reference](https://doc.rust-lang.org/reference/macros.html)
1589 /// Generic lifetime, type and constants parameters list **declaration**.
1592 /// fn foo❰ <'a, 'b, T, U, const BAR: u64> ❱() {}
1594 /// struct Baz❰ <T> ❱(T);
1596 /// impl❰ <T> ❱ Bruh<T> {}
1598 /// type Bruuh = for❰ <'a> ❱ fn(&'a str) -> &'a str;
1601 /// [Reference](https://doc.rust-lang.org/reference/items/generics.html)
1602 struct TypeParamList {
1604 generic_params: [GenericParam],
1605 type_params: [TypeParam],
1606 lifetime_params: [LifetimeParam],
1607 const_params: [ConstParam],
1611 /// Single type parameter **declaration**.
1614 /// fn foo<❰ K ❱, ❰ I ❱, ❰ E: Debug ❱, ❰ V = DefaultType ❱>() {}
1617 /// [Reference](https://doc.rust-lang.org/reference/items/generics.html)
1618 struct TypeParam: NameOwner, AttrsOwner, TypeBoundsOwner {
1620 default_type: TypeRef,
1623 /// Const generic parameter **declaration**.
1625 /// fn foo<T, U, ❰ const BAR: usize ❱, ❰ const BAZ: bool ❱>() {}
1628 /// [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md#declaring-a-const-parameter)
1629 struct ConstParam: NameOwner, AttrsOwner, TypeAscriptionOwner {
1634 /// Lifetime parameter **declaration**.
1637 /// fn foo<❰ 'a ❱, ❰ 'b ❱, V, G, D>(bar: &'a str, baz: &'b mut str) {}
1640 /// [Reference](https://doc.rust-lang.org/reference/items/generics.html)
1641 struct LifetimeParam: AttrsOwner { T![lifetime] }
1643 /// Type bound declaration clause.
1646 /// fn foo<T: ❰ ?Sized ❱ + ❰ Debug ❱>() {}
1650 /// T: ❰ Send ❱ + ❰ Sync ❱
1652 /// type Baz: ❰ !Sync ❱ + ❰ Debug ❱ + ❰ ?const Add ❱;
1656 /// [Reference](https://doc.rust-lang.org/reference/trait-bounds.html)
1657 struct TypeBound { T![lifetime], /* Question, */ T![const], /* Question, */ TypeRef }
1659 /// Type bounds list.
1663 /// fn foo<T: ❰ ?Sized + Debug ❱>() {}
1667 /// T: ❰ Send + Sync ❱
1669 /// type Baz: ❰ !Sync + Debug ❱;
1673 /// [Reference](https://doc.rust-lang.org/reference/trait-bounds.html)
1674 struct TypeBoundList { bounds: [TypeBound] }
1676 /// Single where predicate.
1679 /// trait Foo<'a, 'b, T>
1682 /// ❰ T: IntoIterator ❱,
1683 /// ❰ for<'c> <T as IntoIterator>::Item: Bar<'c> ❱
1687 /// [Reference](https://doc.rust-lang.org/reference/items/generics.html#where-clauses)
1688 struct WherePred: TypeBoundsOwner { T![lifetime], TypeRef }
1693 /// trait Foo<'a, T> ❰ where 'a: 'static, T: Debug ❱ {}
1697 /// [Reference](https://doc.rust-lang.org/reference/items/generics.html#where-clauses)
1698 struct WhereClause { T![where], predicates: [WherePred] }
1700 /// Abi declaration.
1701 /// Note: the abi string is optional.
1704 /// ❰ extern "C" ❱ {
1708 /// type Bar = ❰ extern ❱ fn() -> u32;
1710 /// type Baz = ❰ extern r#"stdcall"# ❱ fn() -> bool;
1713 /// - [Extern blocks reference](https://doc.rust-lang.org/reference/items/external-blocks.html)
1714 /// - [FFI function pointers reference](https://doc.rust-lang.org/reference/items/functions.html#functions)
1715 struct Abi { /*String*/ }
1717 /// Expression statement.
1725 /// // constructions with trailing curly brace can omit the semicolon // TODO: clarify
1726 /// ❰ if bool_cond { } ❱
1730 /// [Reference](https://doc.rust-lang.org/reference/statements.html)
1731 struct ExprStmt: AttrsOwner { Expr, T![;] }
1736 /// ❰ #[attr] let foo; ❱
1737 /// ❰ let bar: u64; ❱
1738 /// ❰ let baz = 42; ❱
1739 /// ❰ let bruh: bool = true; ❱
1742 /// [Reference](https://doc.rust-lang.org/reference/statements.html#let-statements)
1743 struct LetStmt: AttrsOwner, TypeAscriptionOwner {
1751 /// Condition of `if` or `while` expression.
1755 /// if ❰ let Pat(foo) = bar ❱ {}
1757 /// while ❰ true ❱ {}
1758 /// while ❰ let Pat(baz) = bruh ❱ {}
1761 /// [If expression reference](https://doc.rust-lang.org/reference/expressions/if-expr.html)
1762 /// [While expression reference](https://doc.rust-lang.org/reference/expressions/loop-expr.html#predicate-loops)
1763 struct Condition { T![let], Pat, T![=], Expr }
1765 // TODO: this one is used by closure expressions too, but hey use pipes instead of parens
1766 /// Parameter list **declaration**.
1769 /// fn foo❰ (a: u32, b: bool) ❱ -> u32 {}
1770 /// let bar = ❰ |a, b| ❱ {};
1773 /// fn bruh❰ (&self, a: u32) ❱ {}
1777 /// [Reference](https://doc.rust-lang.org/reference/items/functions.html)ocs to codegen script
1785 /// Self parameter **declaration**.
1789 /// fn foo(❰ self ❱) {}
1790 /// fn bar(❰ &self ❱) {}
1791 /// fn baz(❰ &mut self ❱) {}
1792 /// fn blah<'a>(❰ &'a self ❱) {}
1793 /// fn blin(❰ self: Box<Self> ❱) {}
1797 /// [Reference](https://doc.rust-lang.org/reference/items/functions.html)
1798 struct SelfParam: TypeAscriptionOwner, AttrsOwner { T![&], T![mut], T![lifetime], T![self] }
1800 /// Parameter **declaration**.
1803 /// fn foo(❰ #[attr] Pat(bar): Pat(u32) ❱, ❰ #[attr] _: bool ❱) {}
1806 /// fn bar(❰ baz: u32 ❱, ❰ ... ❱) -> u32;
1810 /// [Reference](https://doc.rust-lang.org/reference/items/functions.html)
1811 struct Param: TypeAscriptionOwner, AttrsOwner {
1816 /// Use declaration.
1819 /// ❰ #[attr] pub use foo; ❱
1820 /// ❰ use bar as baz; ❱
1821 /// ❰ use bruh::{self, bruuh}; ❱
1822 /// ❰ use { blin::blen, blah::* };
1825 /// [Reference](https://doc.rust-lang.org/reference/items/use-declarations.html)
1826 struct UseItem: AttrsOwner, VisibilityOwner {
1834 /// pub use ❰ foo::❰ * ❱ ❱;
1835 /// use ❰ bar as baz ❱;
1836 /// use ❰ bruh::bruuh::{ ❰ self ❱, ❰ blin ❱ } ❱;
1837 /// use ❰ { ❰ blin::blen ❱ } ❱
1840 /// [Reference](https://doc.rust-lang.org/reference/items/use-declarations.html)
1842 Path, T![*], UseTreeList, Alias
1846 /// Note: this is not the type alias.
1849 /// use foo ❰ as bar ❱;
1850 /// use baz::{bruh ❰ as _ ❱};
1851 /// extern crate bruuh ❰ as blin ❱;
1854 /// [Reference](https://doc.rust-lang.org/reference/items/use-declarations.html)
1855 struct Alias: NameOwner { T![as] }
1857 /// Sublist of use trees.
1860 /// use bruh::bruuh::❰ { ❰ self ❱, ❰ blin ❱ } ❱;
1861 /// use ❰ { blin::blen::❰ {} ❱ } ❱
1864 /// [Reference](https://doc.rust-lang.org/reference/items/use-declarations.html)
1865 struct UseTreeList { T!['{'], use_trees: [UseTree], T!['}'] }
1867 /// Extern crate item.
1870 /// ❰ #[attr] pub extern crate foo; ❱
1871 /// ❰ extern crate self as bar; ❱
1874 /// [Reference](https://doc.rust-lang.org/reference/items/extern-crates.html)
1875 struct ExternCrateItem: AttrsOwner, VisibilityOwner {
1876 T![extern], T![crate], NameRef, Alias,
1879 /// Call site arguments list.
1882 /// foo::<T, U>❰ (42, true) ❱;
1885 /// [Reference](https://doc.rust-lang.org/reference/expressions/call-expr.html)
1892 /// Path to a symbol. Includes single identifier names and elaborate paths with
1893 /// generic parameters.
1896 /// (0..10).❰ ❰ collect ❱ ::<Vec<_>> ❱();
1897 /// ❰ ❰ ❰ Vec ❱ ::<u8> ❱ ::with_capacity ❱(1024);
1898 /// ❰ ❰ <❰ Foo ❱ as ❰ ❰ bar ❱ ::Bar ❱> ❱ ::baz ❱();
1899 /// ❰ ❰ <❰ bruh ❱> ❱ ::bruuh ❱();
1902 /// [Reference](https://doc.rust-lang.org/reference/paths.html)
1904 segment: PathSegment,
1909 /// Segment of the path to a symbol.
1910 /// Only path segment of an absolute path holds the `::` token,
1911 /// all other `::` tokens that connect path segments reside under `Path` itself.`
1914 /// (0..10).❰ collect ❱ :: ❰ <Vec<_>> ❱();
1915 /// ❰ Vec ❱ :: ❰ <u8> ❱ :: ❰ with_capacity ❱(1024);
1916 /// ❰ <❰ Foo ❱ as ❰ bar ❱ :: ❰ Bar ❱> ❱ :: ❰ baz ❱();
1917 /// ❰ <❰ bruh ❱> ❱ :: ❰ bruuh ❱();
1919 /// // Note that only in this case `::` token is inlcuded:
1923 /// [Reference](https://doc.rust-lang.org/reference/paths.html)
1924 struct PathSegment {
1925 T![::], T![crate], T![self], T![super], T![<], NameRef, TypeArgList, ParamList, RetType, PathType, T![>]
1928 /// List of type arguments that are passed at generic instantiation site.
1931 /// type _ = Foo ❰ ::<'a, u64, Item = Bar, 42, {true}> ❱::Bar;
1933 /// Vec❰ ::<bool> ❱::();
1936 /// [Reference](https://doc.rust-lang.org/reference/paths.html#paths-in-expressions)
1937 struct TypeArgList {
1940 generic_args: [GenericArg],
1941 type_args: [TypeArg],
1942 lifetime_args: [LifetimeArg],
1943 assoc_type_args: [AssocTypeArg],
1944 const_args: [ConstArg],
1948 /// Type argument that is passed at generic instantiation site.
1951 /// type _ = Foo::<'a, ❰ u64 ❱, ❰ bool ❱, Item = Bar, 42>::Baz;
1954 /// [Reference](https://doc.rust-lang.org/reference/paths.html#paths-in-expressions)
1955 struct TypeArg { TypeRef }
1957 /// Associated type argument that is passed at generic instantiation site.
1959 /// type Foo = Bar::<'a, u64, bool, ❰ Item = Baz ❱, 42>::Bruh;
1961 /// trait Bruh<T>: Iterator<❰ Item: Debug ❱> {}
1964 struct AssocTypeArg : TypeBoundsOwner { NameRef, T![=], TypeRef }
1966 /// Lifetime argument that is passed at generic instantiation site.
1969 /// fn foo<'a>(s: &'a str) {
1970 /// bar::<❰ 'a ❱>(s);
1974 /// [Reference](https://doc.rust-lang.org/reference/paths.html#paths-in-expressions)
1975 struct LifetimeArg { T![lifetime] }
1978 // TODO: What does equal sign do here?
1979 /// Constant value argument that is passed at generic instantiation site.
1982 /// foo::<u32, ❰ { true } ❱>();
1984 /// bar::<❰ { 2 + 2} ❱>();
1987 /// [RFC](https://github.com/rust-lang/rfcs/blob/master/text/2000-const-generics.md#declaring-a-const-parameter)
1988 struct ConstArg { Literal, T![=], BlockExpr }
1991 /// FIXME: (@edwin0cheng) Remove it to use ItemList instead
1992 /// https://github.com/rust-analyzer/rust-analyzer/pull/4083#discussion_r422666243
1994 /// [Reference](https://doc.rust-lang.org/reference/macros.html)
1995 struct MacroItems: ModuleItemOwner { }
1997 /// List of items in an extern block.
2003 /// static var: u32;
2008 /// [Reference](https://doc.rust-lang.org/reference/items/external-blocks.html)
2009 struct ExternItemList: ModuleItemOwner {
2011 extern_items: [ExternItem],
2026 /// [Reference](https://doc.rust-lang.org/reference/items/external-blocks.html)
2027 struct ExternBlock {
2032 /// Meta item in an attribute.
2035 /// #[❰ bar::baz = "42" ❱]
2036 /// #[❰ bruh(bruuh("true")) ❱]
2040 /// [Reference](https://doc.rust-lang.org/reference/attributes.html?highlight=meta,item#meta-item-attribute-syntax)
2042 Path, T![=], AttrInput, nested_meta_items: [MetaItem]
2045 // TODO: is this a special case of `MacroCall` where `Name` = `macro_rules`?
2046 // It doesn't seem this ast node is used anywhere
2047 /// Macro definition.
2051 /// macro_rules! foo {
2052 /// ($bar:tt) => {$bar}
2057 /// [Reference](https://doc.rust-lang.org/reference/macros-by-example.html)
2062 enums: &ast_enums! {
2063 /// Any kind of nominal type definition.
2064 enum NominalDef: NameOwner, TypeParamsOwner, AttrsOwner {
2065 StructDef, EnumDef, UnionDef,
2068 /// Any kind of **declared** generic parameter
2075 /// Any kind of generic argument passed at instantiation site
2083 /// Any kind of construct valid in type context
2100 /// Any kind of top-level item that may appear in a module
2101 enum ModuleItem: NameOwner, AttrsOwner, VisibilityOwner {
2120 /// Any kind of item that may appear in an impl block
2122 /// // TODO: is the following a fixme?
2123 /// impl blocks can also contain MacroCall
2124 enum AssocItem: NameOwner, AttrsOwner {
2125 FnDef, TypeAliasDef, ConstDef
2128 /// Any kind of item that may appear in an extern block
2130 /// // TODO: is the following a fixme?
2131 /// extern blocks can also contain MacroCall
2132 enum ExternItem: NameOwner, AttrsOwner, VisibilityOwner {
2136 /// Any kind of expression
2137 enum Expr: AttrsOwner {
2171 /// Any kind of pattern
2190 /// Any kind of pattern that appears directly inside of the curly
2191 /// braces of a record pattern
2192 enum RecordInnerPat {
2197 /// Any kind of input to an attribute
2198 enum AttrInput { Literal, TokenTree }
2200 /// Any kind of statement
2201 /// Note: there are no empty statements, these are just represented as
2202 /// bare semicolons without a dedicated statement ast node.
2206 // macro calls are parsed as expression statements
2209 /// Any kind of fields list (record or tuple field lists)