2743: Switch ast declaration from ron to a macro r=matklad a=matklad



Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
This commit is contained in:
bors[bot] 2020-01-03 20:54:42 +00:00 committed by GitHub
commit 823c15215e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 4173 additions and 4355 deletions

22
Cargo.lock generated
View file

@ -57,14 +57,6 @@ dependencies = [
"libc 0.2.66 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "base64"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"byteorder 1.3.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "base64"
version = "0.11.0"
@ -1398,16 +1390,6 @@ dependencies = [
"winapi 0.3.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "ron"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
dependencies = [
"base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)",
"bitflags 1.2.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "rowan"
version = "0.8.2"
@ -1772,8 +1754,6 @@ dependencies = [
"pico-args 0.3.0 (registry+https://github.com/rust-lang/crates.io-index)",
"proc-macro2 1.0.7 (registry+https://github.com/rust-lang/crates.io-index)",
"quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)",
"ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)",
"serde 1.0.104 (registry+https://github.com/rust-lang/crates.io-index)",
"walkdir 2.2.9 (registry+https://github.com/rust-lang/crates.io-index)",
]
@ -1794,7 +1774,6 @@ dependencies = [
"checksum autocfg 0.1.7 (registry+https://github.com/rust-lang/crates.io-index)" = "1d49d90015b3c36167a20fe2810c5cd875ad504b39cff3d4eae7977e6b7c1cb2"
"checksum backtrace 0.3.40 (registry+https://github.com/rust-lang/crates.io-index)" = "924c76597f0d9ca25d762c25a4d369d51267536465dc5064bdf0eb073ed477ea"
"checksum backtrace-sys 0.1.32 (registry+https://github.com/rust-lang/crates.io-index)" = "5d6575f128516de27e3ce99689419835fce9643a9b215a14d2b5b685be018491"
"checksum base64 0.10.1 (registry+https://github.com/rust-lang/crates.io-index)" = "0b25d992356d2eb0ed82172f5248873db5560c4721f564b13cb5193bda5e668e"
"checksum base64 0.11.0 (registry+https://github.com/rust-lang/crates.io-index)" = "b41b7ea54a0c9d92199de89e20e58d49f02f8e699814ef3fdf266f6f748d15c7"
"checksum bit-set 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "e84c238982c4b1e1ee668d136c510c67a13465279c0cb367ea6baf6310620a80"
"checksum bit-vec 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "f59bbe95d4e52a6398ec21238d31577f2b28a9d86807f06ca59d191d8440d0bb"
@ -1915,7 +1894,6 @@ dependencies = [
"checksum regex-syntax 0.6.12 (registry+https://github.com/rust-lang/crates.io-index)" = "11a7e20d1cce64ef2fed88b66d347f88bd9babb82845b2b858f3edbf59a4f716"
"checksum relative-path 1.0.0 (registry+https://github.com/rust-lang/crates.io-index)" = "bedde000f40f2921ce439ea165c9c53fd629bfa115140c72e22aceacb4a21954"
"checksum remove_dir_all 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "4a83fa3702a688b9359eccba92d153ac33fd2e8462f9e0e3fdf155239ea7792e"
"checksum ron 0.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "2ece421e0c4129b90e4a35b6f625e472e96c552136f5093a2f4fa2bbb75a62d5"
"checksum rowan 0.8.2 (registry+https://github.com/rust-lang/crates.io-index)" = "3eb10a10a48f0f809a217bcf074b85a03dcf79831bae80e7f1a043d0897463e2"
"checksum rustc-demangle 0.1.16 (registry+https://github.com/rust-lang/crates.io-index)" = "4c691c0e608126e00913e33f0ccf3727d5fc84573623b8d65b2df340b5201783"
"checksum rustc-hash 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7540fc8b0c49f096ee9c961cda096467dce8084bec6bdca2fc83895fd9b28cb8"

View file

@ -61,46 +61,46 @@ pub enum SyntaxKind {
SHR,
SHLEQ,
SHREQ,
ASYNC_KW,
USE_KW,
FN_KW,
STRUCT_KW,
ENUM_KW,
TRAIT_KW,
IMPL_KW,
DYN_KW,
TRUE_KW,
FALSE_KW,
AS_KW,
EXTERN_KW,
CRATE_KW,
MOD_KW,
PUB_KW,
SELF_KW,
SUPER_KW,
IN_KW,
WHERE_KW,
FOR_KW,
LOOP_KW,
WHILE_KW,
CONTINUE_KW,
BREAK_KW,
IF_KW,
ELSE_KW,
MATCH_KW,
CONST_KW,
STATIC_KW,
MUT_KW,
UNSAFE_KW,
TYPE_KW,
REF_KW,
LET_KW,
MOVE_KW,
RETURN_KW,
TRY_KW,
BOX_KW,
ASYNC_KW,
AWAIT_KW,
BOX_KW,
BREAK_KW,
CONST_KW,
CONTINUE_KW,
CRATE_KW,
DYN_KW,
ELSE_KW,
ENUM_KW,
EXTERN_KW,
FALSE_KW,
FN_KW,
FOR_KW,
IF_KW,
IMPL_KW,
IN_KW,
LET_KW,
LOOP_KW,
MACRO_KW,
MATCH_KW,
MOD_KW,
MOVE_KW,
MUT_KW,
PUB_KW,
REF_KW,
RETURN_KW,
SELF_KW,
STATIC_KW,
STRUCT_KW,
SUPER_KW,
TRAIT_KW,
TRUE_KW,
TRY_KW,
TYPE_KW,
UNSAFE_KW,
USE_KW,
WHERE_KW,
WHILE_KW,
AUTO_KW,
DEFAULT_KW,
EXISTENTIAL_KW,
@ -249,12 +249,12 @@ use self::SyntaxKind::*;
impl SyntaxKind {
pub fn is_keyword(self) -> bool {
match self {
ASYNC_KW | USE_KW | FN_KW | STRUCT_KW | ENUM_KW | TRAIT_KW | IMPL_KW | DYN_KW
| TRUE_KW | FALSE_KW | AS_KW | EXTERN_KW | CRATE_KW | MOD_KW | PUB_KW | SELF_KW
| SUPER_KW | IN_KW | WHERE_KW | FOR_KW | LOOP_KW | WHILE_KW | CONTINUE_KW
| BREAK_KW | IF_KW | ELSE_KW | MATCH_KW | CONST_KW | STATIC_KW | MUT_KW | UNSAFE_KW
| TYPE_KW | REF_KW | LET_KW | MOVE_KW | RETURN_KW | TRY_KW | BOX_KW | AWAIT_KW
| MACRO_KW | AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW => true,
AS_KW | ASYNC_KW | AWAIT_KW | BOX_KW | BREAK_KW | CONST_KW | CONTINUE_KW | CRATE_KW
| DYN_KW | ELSE_KW | ENUM_KW | EXTERN_KW | FALSE_KW | FN_KW | FOR_KW | IF_KW
| IMPL_KW | IN_KW | LET_KW | LOOP_KW | MACRO_KW | MATCH_KW | MOD_KW | MOVE_KW
| MUT_KW | PUB_KW | REF_KW | RETURN_KW | SELF_KW | STATIC_KW | STRUCT_KW | SUPER_KW
| TRAIT_KW | TRUE_KW | TRY_KW | TYPE_KW | UNSAFE_KW | USE_KW | WHERE_KW | WHILE_KW
| AUTO_KW | DEFAULT_KW | EXISTENTIAL_KW | UNION_KW => true,
_ => false,
}
}
@ -278,46 +278,46 @@ impl SyntaxKind {
}
pub fn from_keyword(ident: &str) -> Option<SyntaxKind> {
let kw = match ident {
"async" => ASYNC_KW,
"use" => USE_KW,
"fn" => FN_KW,
"struct" => STRUCT_KW,
"enum" => ENUM_KW,
"trait" => TRAIT_KW,
"impl" => IMPL_KW,
"dyn" => DYN_KW,
"true" => TRUE_KW,
"false" => FALSE_KW,
"as" => AS_KW,
"extern" => EXTERN_KW,
"crate" => CRATE_KW,
"mod" => MOD_KW,
"pub" => PUB_KW,
"self" => SELF_KW,
"super" => SUPER_KW,
"in" => IN_KW,
"where" => WHERE_KW,
"for" => FOR_KW,
"loop" => LOOP_KW,
"while" => WHILE_KW,
"continue" => CONTINUE_KW,
"break" => BREAK_KW,
"if" => IF_KW,
"else" => ELSE_KW,
"match" => MATCH_KW,
"const" => CONST_KW,
"static" => STATIC_KW,
"mut" => MUT_KW,
"unsafe" => UNSAFE_KW,
"type" => TYPE_KW,
"ref" => REF_KW,
"let" => LET_KW,
"move" => MOVE_KW,
"return" => RETURN_KW,
"try" => TRY_KW,
"box" => BOX_KW,
"async" => ASYNC_KW,
"await" => AWAIT_KW,
"box" => BOX_KW,
"break" => BREAK_KW,
"const" => CONST_KW,
"continue" => CONTINUE_KW,
"crate" => CRATE_KW,
"dyn" => DYN_KW,
"else" => ELSE_KW,
"enum" => ENUM_KW,
"extern" => EXTERN_KW,
"false" => FALSE_KW,
"fn" => FN_KW,
"for" => FOR_KW,
"if" => IF_KW,
"impl" => IMPL_KW,
"in" => IN_KW,
"let" => LET_KW,
"loop" => LOOP_KW,
"macro" => MACRO_KW,
"match" => MATCH_KW,
"mod" => MOD_KW,
"move" => MOVE_KW,
"mut" => MUT_KW,
"pub" => PUB_KW,
"ref" => REF_KW,
"return" => RETURN_KW,
"self" => SELF_KW,
"static" => STATIC_KW,
"struct" => STRUCT_KW,
"super" => SUPER_KW,
"trait" => TRAIT_KW,
"true" => TRUE_KW,
"try" => TRY_KW,
"type" => TYPE_KW,
"unsafe" => UNSAFE_KW,
"use" => USE_KW,
"where" => WHERE_KW,
"while" => WHILE_KW,
_ => return None,
};
Some(kw)
@ -515,126 +515,126 @@ macro_rules! T {
( >>= ) => {
$crate::SyntaxKind::SHREQ
};
( async ) => {
$crate::SyntaxKind::ASYNC_KW
};
( use ) => {
$crate::SyntaxKind::USE_KW
};
( fn ) => {
$crate::SyntaxKind::FN_KW
};
( struct ) => {
$crate::SyntaxKind::STRUCT_KW
};
( enum ) => {
$crate::SyntaxKind::ENUM_KW
};
( trait ) => {
$crate::SyntaxKind::TRAIT_KW
};
( impl ) => {
$crate::SyntaxKind::IMPL_KW
};
( dyn ) => {
$crate::SyntaxKind::DYN_KW
};
( true ) => {
$crate::SyntaxKind::TRUE_KW
};
( false ) => {
$crate::SyntaxKind::FALSE_KW
};
( as ) => {
$crate::SyntaxKind::AS_KW
};
( extern ) => {
$crate::SyntaxKind::EXTERN_KW
};
( crate ) => {
$crate::SyntaxKind::CRATE_KW
};
( mod ) => {
$crate::SyntaxKind::MOD_KW
};
( pub ) => {
$crate::SyntaxKind::PUB_KW
};
( self ) => {
$crate::SyntaxKind::SELF_KW
};
( super ) => {
$crate::SyntaxKind::SUPER_KW
};
( in ) => {
$crate::SyntaxKind::IN_KW
};
( where ) => {
$crate::SyntaxKind::WHERE_KW
};
( for ) => {
$crate::SyntaxKind::FOR_KW
};
( loop ) => {
$crate::SyntaxKind::LOOP_KW
};
( while ) => {
$crate::SyntaxKind::WHILE_KW
};
( continue ) => {
$crate::SyntaxKind::CONTINUE_KW
};
( break ) => {
$crate::SyntaxKind::BREAK_KW
};
( if ) => {
$crate::SyntaxKind::IF_KW
};
( else ) => {
$crate::SyntaxKind::ELSE_KW
};
( match ) => {
$crate::SyntaxKind::MATCH_KW
};
( const ) => {
$crate::SyntaxKind::CONST_KW
};
( static ) => {
$crate::SyntaxKind::STATIC_KW
};
( mut ) => {
$crate::SyntaxKind::MUT_KW
};
( unsafe ) => {
$crate::SyntaxKind::UNSAFE_KW
};
( type ) => {
$crate::SyntaxKind::TYPE_KW
};
( ref ) => {
$crate::SyntaxKind::REF_KW
};
( let ) => {
$crate::SyntaxKind::LET_KW
};
( move ) => {
$crate::SyntaxKind::MOVE_KW
};
( return ) => {
$crate::SyntaxKind::RETURN_KW
};
( try ) => {
$crate::SyntaxKind::TRY_KW
};
( box ) => {
$crate::SyntaxKind::BOX_KW
( async ) => {
$crate::SyntaxKind::ASYNC_KW
};
( await ) => {
$crate::SyntaxKind::AWAIT_KW
};
( box ) => {
$crate::SyntaxKind::BOX_KW
};
( break ) => {
$crate::SyntaxKind::BREAK_KW
};
( const ) => {
$crate::SyntaxKind::CONST_KW
};
( continue ) => {
$crate::SyntaxKind::CONTINUE_KW
};
( crate ) => {
$crate::SyntaxKind::CRATE_KW
};
( dyn ) => {
$crate::SyntaxKind::DYN_KW
};
( else ) => {
$crate::SyntaxKind::ELSE_KW
};
( enum ) => {
$crate::SyntaxKind::ENUM_KW
};
( extern ) => {
$crate::SyntaxKind::EXTERN_KW
};
( false ) => {
$crate::SyntaxKind::FALSE_KW
};
( fn ) => {
$crate::SyntaxKind::FN_KW
};
( for ) => {
$crate::SyntaxKind::FOR_KW
};
( if ) => {
$crate::SyntaxKind::IF_KW
};
( impl ) => {
$crate::SyntaxKind::IMPL_KW
};
( in ) => {
$crate::SyntaxKind::IN_KW
};
( let ) => {
$crate::SyntaxKind::LET_KW
};
( loop ) => {
$crate::SyntaxKind::LOOP_KW
};
( macro ) => {
$crate::SyntaxKind::MACRO_KW
};
( match ) => {
$crate::SyntaxKind::MATCH_KW
};
( mod ) => {
$crate::SyntaxKind::MOD_KW
};
( move ) => {
$crate::SyntaxKind::MOVE_KW
};
( mut ) => {
$crate::SyntaxKind::MUT_KW
};
( pub ) => {
$crate::SyntaxKind::PUB_KW
};
( ref ) => {
$crate::SyntaxKind::REF_KW
};
( return ) => {
$crate::SyntaxKind::RETURN_KW
};
( self ) => {
$crate::SyntaxKind::SELF_KW
};
( static ) => {
$crate::SyntaxKind::STATIC_KW
};
( struct ) => {
$crate::SyntaxKind::STRUCT_KW
};
( super ) => {
$crate::SyntaxKind::SUPER_KW
};
( trait ) => {
$crate::SyntaxKind::TRAIT_KW
};
( true ) => {
$crate::SyntaxKind::TRUE_KW
};
( try ) => {
$crate::SyntaxKind::TRY_KW
};
( type ) => {
$crate::SyntaxKind::TYPE_KW
};
( unsafe ) => {
$crate::SyntaxKind::UNSAFE_KW
};
( use ) => {
$crate::SyntaxKind::USE_KW
};
( where ) => {
$crate::SyntaxKind::WHERE_KW
};
( while ) => {
$crate::SyntaxKind::WHILE_KW
};
( auto ) => {
$crate::SyntaxKind::AUTO_KW
};

File diff suppressed because it is too large Load diff

View file

@ -1,736 +1,6 @@
// Stores definitions which must be used in multiple places
// See `cargo xtask codegen` (defined in xtasks/src/main.rs)
Grammar(
punct: [
(";", "SEMI"),
(",", "COMMA"),
("(", "L_PAREN"),
(")", "R_PAREN"),
("{", "L_CURLY"),
("}", "R_CURLY"),
("[", "L_BRACK"),
("]", "R_BRACK"),
("<", "L_ANGLE"),
(">", "R_ANGLE"),
("@", "AT"),
("#", "POUND"),
("~", "TILDE"),
("?", "QUESTION"),
("$", "DOLLAR"),
("&", "AMP"),
("|", "PIPE"),
("+", "PLUS"),
("*", "STAR"),
("/", "SLASH"),
("^", "CARET"),
("%", "PERCENT"),
("_", "UNDERSCORE"),
(".", "DOT"),
("..", "DOTDOT"),
("...", "DOTDOTDOT"),
("..=", "DOTDOTEQ"),
(":", "COLON"),
("::", "COLONCOLON"),
("=", "EQ"),
("==", "EQEQ"),
("=>", "FAT_ARROW"),
("!", "EXCL"),
("!=", "NEQ"),
("-", "MINUS"),
("->", "THIN_ARROW"),
("<=", "LTEQ"),
(">=", "GTEQ"),
("+=", "PLUSEQ"),
("-=", "MINUSEQ"),
("|=", "PIPEEQ"),
("&=", "AMPEQ"),
("^=", "CARETEQ"),
("/=", "SLASHEQ"),
("*=", "STAREQ"),
("%=", "PERCENTEQ"),
("&&", "AMPAMP"),
("||", "PIPEPIPE"),
("<<", "SHL"),
(">>", "SHR"),
("<<=", "SHLEQ"),
(">>=", "SHREQ"),
],
keywords: [
"async",
"use",
"fn",
"struct",
"enum",
"trait",
"impl",
"dyn",
"true",
"false",
"as",
"extern",
"crate",
"mod",
"pub",
"self",
"super",
"in",
"where",
"for",
"loop",
"while",
"continue",
"break",
"if",
"else",
"match",
"const",
"static",
"mut",
"unsafe",
"type",
"ref",
"let",
"move",
"return",
"try",
"box",
"await",
"macro"
],
contextual_keywords: [
"auto",
"default",
"existential",
"union",
],
literals: [
"INT_NUMBER",
"FLOAT_NUMBER",
"CHAR",
"BYTE",
"STRING",
"RAW_STRING",
"BYTE_STRING",
"RAW_BYTE_STRING",
],
tokens: [
"ERROR",
"IDENT",
"WHITESPACE",
"LIFETIME",
"COMMENT",
"SHEBANG",
"L_DOLLAR",
"R_DOLLAR",
],
nodes: [
"SOURCE_FILE",
"STRUCT_DEF",
"UNION_DEF",
"ENUM_DEF",
"FN_DEF",
"RET_TYPE",
"EXTERN_CRATE_ITEM",
"MODULE",
"USE_ITEM",
"STATIC_DEF",
"CONST_DEF",
"TRAIT_DEF",
"IMPL_BLOCK",
"TYPE_ALIAS_DEF",
"MACRO_CALL",
"TOKEN_TREE",
"MACRO_DEF",
"PAREN_TYPE",
"TUPLE_TYPE",
"NEVER_TYPE",
"PATH_TYPE",
"POINTER_TYPE",
"ARRAY_TYPE",
"SLICE_TYPE",
"REFERENCE_TYPE",
"PLACEHOLDER_TYPE",
"FN_POINTER_TYPE",
"FOR_TYPE",
"IMPL_TRAIT_TYPE",
"DYN_TRAIT_TYPE",
"REF_PAT",
"BOX_PAT",
"BIND_PAT",
"PLACEHOLDER_PAT",
"DOT_DOT_PAT",
"PATH_PAT",
"RECORD_PAT",
"RECORD_FIELD_PAT_LIST",
"RECORD_FIELD_PAT",
"TUPLE_STRUCT_PAT",
"TUPLE_PAT",
"SLICE_PAT",
"RANGE_PAT",
"LITERAL_PAT",
// atoms
"TUPLE_EXPR",
"ARRAY_EXPR",
"PAREN_EXPR",
"PATH_EXPR",
"LAMBDA_EXPR",
"IF_EXPR",
"WHILE_EXPR",
"CONDITION",
"LOOP_EXPR",
"FOR_EXPR",
"CONTINUE_EXPR",
"BREAK_EXPR",
"LABEL",
"BLOCK_EXPR",
"RETURN_EXPR",
"MATCH_EXPR",
"MATCH_ARM_LIST",
"MATCH_ARM",
"MATCH_GUARD",
"RECORD_LIT",
"RECORD_FIELD_LIST",
"RECORD_FIELD",
"TRY_BLOCK_EXPR",
"BOX_EXPR",
// postfix
"CALL_EXPR",
"INDEX_EXPR",
"METHOD_CALL_EXPR",
"FIELD_EXPR",
"AWAIT_EXPR",
"TRY_EXPR",
"CAST_EXPR",
// unary
"REF_EXPR",
"PREFIX_EXPR",
"RANGE_EXPR", // just weird
"BIN_EXPR",
"BLOCK",
"EXTERN_BLOCK",
"EXTERN_ITEM_LIST",
"ENUM_VARIANT",
"RECORD_FIELD_DEF_LIST",
"RECORD_FIELD_DEF",
"TUPLE_FIELD_DEF_LIST",
"TUPLE_FIELD_DEF",
"ENUM_VARIANT_LIST",
"ITEM_LIST",
"ATTR",
"META_ITEM", // not an item actually
"USE_TREE",
"USE_TREE_LIST",
"PATH",
"PATH_SEGMENT",
"LITERAL",
"ALIAS",
"VISIBILITY",
"WHERE_CLAUSE",
"WHERE_PRED",
"ABI",
"NAME",
"NAME_REF",
"LET_STMT",
"EXPR_STMT",
"TYPE_PARAM_LIST",
"LIFETIME_PARAM",
"TYPE_PARAM",
"CONST_PARAM",
"TYPE_ARG_LIST",
"LIFETIME_ARG",
"TYPE_ARG",
"ASSOC_TYPE_ARG",
"PARAM_LIST",
"PARAM",
"SELF_PARAM",
"ARG_LIST",
"TYPE_BOUND",
"TYPE_BOUND_LIST",
// macro related
"MACRO_ITEMS",
"MACRO_STMTS",
],
ast: {
"SourceFile": (
traits: [ "ModuleItemOwner", "FnDefOwner" ],
collections: [
("modules", "Module"),
]
),
"FnDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
],
options: [ "ParamList", ["body", "BlockExpr"], "RetType" ],
),
"RetType": (options: ["TypeRef"]),
"StructDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
]
),
"UnionDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
],
options: ["RecordFieldDefList"],
),
"RecordFieldDefList": (collections: [("fields", "RecordFieldDef")]),
"RecordFieldDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"AttrsOwner",
"DocCommentsOwner",
"TypeAscriptionOwner"
]
),
"TupleFieldDefList": (collections: [("fields", "TupleFieldDef")]),
"TupleFieldDef": ( traits: ["VisibilityOwner", "AttrsOwner"], options: ["TypeRef"]),
"EnumDef": ( traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner"
], options: [["variant_list", "EnumVariantList"]] ),
"EnumVariantList": ( collections: [("variants", "EnumVariant")] ),
"EnumVariant": ( traits: ["NameOwner", "DocCommentsOwner", "AttrsOwner"], options: ["Expr"] ),
"TraitDef": (
traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner", "TypeParamsOwner", "TypeBoundsOwner"],
options: ["ItemList"]
),
"Module": (
traits: ["VisibilityOwner", "NameOwner", "AttrsOwner", "DocCommentsOwner" ],
options: [ "ItemList" ]
),
"ItemList": (
collections: [("impl_items", "ImplItem")],
traits: [ "FnDefOwner", "ModuleItemOwner" ],
),
"ConstDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner",
"TypeAscriptionOwner",
],
options: [ ["body","Expr"]],
),
"StaticDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner",
"TypeAscriptionOwner",
],
options: [ ["body","Expr"]],
),
"TypeAliasDef": (
traits: [
"VisibilityOwner",
"NameOwner",
"TypeParamsOwner",
"AttrsOwner",
"DocCommentsOwner",
"TypeBoundsOwner",
],
options: ["TypeRef"]
),
"ImplBlock": (options: ["ItemList"], traits: ["TypeParamsOwner", "AttrsOwner"]),
"ParenType": (options: ["TypeRef"]),
"TupleType": ( collections: [("fields", "TypeRef")] ),
"NeverType": (),
"PathType": (options: ["Path"]),
"PointerType": (options: ["TypeRef"]),
"ArrayType": ( options: ["TypeRef", "Expr"] ),
"SliceType": ( options: ["TypeRef"] ),
"ReferenceType": (options: ["TypeRef"]),
"PlaceholderType": (),
"FnPointerType": (options: ["ParamList", "RetType"]),
"ForType": (options: ["TypeRef"]),
"ImplTraitType": (
traits: ["TypeBoundsOwner"],
),
"DynTraitType": (
traits: ["TypeBoundsOwner"],
),
"TypeRef": ( enum: [
"ParenType",
"TupleType",
"NeverType",
"PathType",
"PointerType",
"ArrayType",
"SliceType",
"ReferenceType",
"PlaceholderType",
"FnPointerType",
"ForType",
"ImplTraitType",
"DynTraitType",
]),
"NominalDef": (
enum: ["StructDef", "EnumDef", "UnionDef"],
traits: [
"NameOwner",
"TypeParamsOwner",
"AttrsOwner"
],
),
"ModuleItem": (
enum: ["StructDef", "UnionDef", "EnumDef", "FnDef", "TraitDef", "TypeAliasDef", "ImplBlock",
"UseItem", "ExternCrateItem", "ConstDef", "StaticDef", "Module" ],
traits: ["AttrsOwner", "VisibilityOwner"],
),
"ImplItem": (
enum: ["FnDef", "TypeAliasDef", "ConstDef"],
traits: ["AttrsOwner"]
),
"TupleExpr": (
collections: [("exprs", "Expr")]
),
"ArrayExpr": (
collections: [("exprs", "Expr")]
),
"ParenExpr": (options: ["Expr"]),
"PathExpr": (options: ["Path"]),
"LambdaExpr": (
options: [
"ParamList", "RetType",
["body", "Expr"],
]
),
"IfExpr": (
options: [ "Condition" ]
),
"LoopExpr": (
traits: ["LoopBodyOwner"],
),
"TryBlockExpr": (
options: [["body", "BlockExpr"]],
),
"ForExpr": (
traits: ["LoopBodyOwner"],
options: [
"Pat",
["iterable", "Expr"],
]
),
"WhileExpr": (
traits: ["LoopBodyOwner"],
options: [ "Condition" ]
),
"ContinueExpr": (),
"BreakExpr": (options: ["Expr"]),
"Label": (),
"BlockExpr": (
options: [ "Block" ]
),
"ReturnExpr": (options: ["Expr"]),
"MatchExpr": (
options: [ "Expr", "MatchArmList" ],
),
"MatchArmList": (
collections: [ ("arms", "MatchArm") ],
traits: [ "AttrsOwner" ]
),
"MatchArm": (
options: [
[ "guard", "MatchGuard" ],
"Expr",
],
collections: [ ("pats", "Pat") ],
traits: [ "AttrsOwner" ]
),
"MatchGuard": (options: ["Expr"]),
"RecordLit": (options: ["Path", "RecordFieldList"]),
"RecordFieldList": (
collections: [ ("fields", "RecordField") ],
options: [["spread", "Expr"]]
),
"RecordField": (options: ["NameRef", "Expr"]),
"CallExpr": (
traits: ["ArgListOwner"],
options: [ "Expr" ],
),
"MethodCallExpr": (
traits: ["ArgListOwner"],
options: [ "Expr", "NameRef", "TypeArgList" ],
),
"IndexExpr": (),
"FieldExpr": (options: ["Expr", "NameRef"]),
"AwaitExpr": (options: ["Expr"]),
"TryExpr": (options: ["Expr"]),
"CastExpr": (options: ["Expr", "TypeRef"]),
"RefExpr": (options: ["Expr"]),
"PrefixExpr": (options: ["Expr"]),
"BoxExpr": (options: ["Expr"]),
"RangeExpr": (),
"BinExpr": (),
"Literal": (),
"Expr": (
enum: [
"TupleExpr",
"ArrayExpr",
"ParenExpr",
"PathExpr",
"LambdaExpr",
"IfExpr",
"LoopExpr",
"ForExpr",
"WhileExpr",
"ContinueExpr",
"BreakExpr",
"Label",
"BlockExpr",
"ReturnExpr",
"MatchExpr",
"RecordLit",
"CallExpr",
"IndexExpr",
"MethodCallExpr",
"FieldExpr",
"AwaitExpr",
"TryExpr",
"TryBlockExpr",
"CastExpr",
"RefExpr",
"PrefixExpr",
"RangeExpr",
"BinExpr",
"Literal",
"MacroCall",
"BoxExpr",
],
),
"RefPat": ( options: [ "Pat" ]),
"BoxPat": ( options: [ "Pat" ]),
"BindPat": (
options: [ "Pat" ],
traits: ["NameOwner"]
),
"PlaceholderPat": (),
"DotDotPat": (),
"PathPat": ( options: [ "Path" ] ),
"RecordPat": ( options: ["RecordFieldPatList", "Path"] ),
"RecordFieldPatList": (
collections: [
("record_field_pats", "RecordFieldPat"),
("bind_pats", "BindPat"),
]
),
"RecordFieldPat": (
traits: ["NameOwner"],
options: ["Pat"]
),
"TupleStructPat": (
options: ["Path"],
collections: [("args", "Pat")],
),
"TuplePat": ( collections: [("args", "Pat")] ),
"SlicePat": (),
"RangePat": (),
"LiteralPat": (options: ["Literal"]),
"Pat": (
enum: [
"RefPat",
"BoxPat",
"BindPat",
"PlaceholderPat",
"DotDotPat",
"PathPat",
"RecordPat",
"TupleStructPat",
"TuplePat",
"SlicePat",
"RangePat",
"LiteralPat",
],
),
"Visibility": (),
"Name": (),
"NameRef": (),
"MacroCall": (
traits: [ "NameOwner", "AttrsOwner","DocCommentsOwner" ],
options: [ "TokenTree", "Path" ],
),
"AttrInput": ( enum: [ "Literal", "TokenTree" ] ),
"Attr": ( options: [ "Path", [ "input", "AttrInput" ] ] ),
"TokenTree": (),
"TypeParamList": (
collections: [
("type_params", "TypeParam" ),
("lifetime_params", "LifetimeParam" ),
]
),
"TypeParam": (
options: [("default_type", "TypeRef")],
traits: ["NameOwner", "AttrsOwner", "TypeBoundsOwner"],
),
"ConstParam": (
options: [("default_val", "Expr")],
traits: ["NameOwner", "AttrsOwner", "TypeAscriptionOwner"],
),
"LifetimeParam": (
traits: ["AttrsOwner"],
),
"TypeBound": (
options: [
"TypeRef",
]
),
"TypeBoundList": (
collections: [
("bounds", "TypeBound"),
]
),
"WherePred": (
options: [
"TypeRef",
],
traits: [
"TypeBoundsOwner",
],
),
"WhereClause": (
collections: [
("predicates", "WherePred"),
],
),
"ExprStmt": (
options: [ ["expr", "Expr"] ]
),
"LetStmt": (
options: [
["pat", "Pat"],
["initializer", "Expr"],
],
traits: [
"TypeAscriptionOwner",
]
),
"Condition": (
options: [ "Pat", "Expr" ]
),
"Stmt": (
enum: ["ExprStmt", "LetStmt"],
),
"Block": (
options: [ "Expr" ],
collections: [
("statements", "Stmt"),
],
traits: [
"AttrsOwner",
"ModuleItemOwner",
]
),
"ParamList": (
options: [ "SelfParam" ],
collections: [
("params", "Param"),
]
),
"SelfParam": (
traits: [
"TypeAscriptionOwner",
"AttrsOwner",
]
),
"Param": (
options: [ "Pat" ],
traits: [
"TypeAscriptionOwner",
"AttrsOwner",
]
),
"UseItem": (
traits: ["AttrsOwner", "VisibilityOwner"],
options: [ "UseTree" ],
),
"UseTree": (
options: [ "Path", "UseTreeList", "Alias" ]
),
"Alias": (
traits: ["NameOwner"],
),
"UseTreeList": (
collections: [("use_trees", "UseTree")]
),
"ExternCrateItem": (
traits: ["AttrsOwner", "VisibilityOwner"],
options: ["NameRef", "Alias"],
),
"ArgList": (
collections: [
("args", "Expr"),
]
),
"Path": (
options: [
["segment", "PathSegment"],
["qualifier", "Path"],
]
),
"PathSegment": (
options: [ "NameRef", "TypeArgList", "ParamList", "RetType", "PathType" ]
),
"TypeArgList": (collections: [
("type_args", "TypeArg"),
("lifetime_args", "LifetimeArg"),
("assoc_type_args", "AssocTypeArg"),
]),
"TypeArg": (options: ["TypeRef"]),
"AssocTypeArg": (options: ["NameRef", "TypeRef"]),
"LifetimeArg": (),
"MacroItems": (
traits: [ "ModuleItemOwner", "FnDefOwner" ],
),
"MacroStmts" : (
options: [ "Expr" ],
collections: [
("statements", "Stmt"),
],
)
},
)

View file

@ -13,6 +13,4 @@ walkdir = "2.1.3"
pico-args = "0.3.0"
quote = "1.0.2"
proc-macro2 = "1.0.1"
ron = "0.5.1"
serde = { version = "1.0.0", features = ["derive"] }
anyhow = "1.0.19"

618
xtask/src/ast_src.rs Normal file
View file

@ -0,0 +1,618 @@
pub(crate) struct KindsSrc<'a> {
pub(crate) punct: &'a [(&'a str, &'a str)],
pub(crate) keywords: &'a [&'a str],
pub(crate) contextual_keywords: &'a [&'a str],
pub(crate) literals: &'a [&'a str],
pub(crate) tokens: &'a [&'a str],
pub(crate) nodes: &'a [&'a str],
}
pub(crate) const KINDS_SRC: KindsSrc = KindsSrc {
punct: &[
(";", "SEMI"),
(",", "COMMA"),
("(", "L_PAREN"),
(")", "R_PAREN"),
("{", "L_CURLY"),
("}", "R_CURLY"),
("[", "L_BRACK"),
("]", "R_BRACK"),
("<", "L_ANGLE"),
(">", "R_ANGLE"),
("@", "AT"),
("#", "POUND"),
("~", "TILDE"),
("?", "QUESTION"),
("$", "DOLLAR"),
("&", "AMP"),
("|", "PIPE"),
("+", "PLUS"),
("*", "STAR"),
("/", "SLASH"),
("^", "CARET"),
("%", "PERCENT"),
("_", "UNDERSCORE"),
(".", "DOT"),
("..", "DOTDOT"),
("...", "DOTDOTDOT"),
("..=", "DOTDOTEQ"),
(":", "COLON"),
("::", "COLONCOLON"),
("=", "EQ"),
("==", "EQEQ"),
("=>", "FAT_ARROW"),
("!", "EXCL"),
("!=", "NEQ"),
("-", "MINUS"),
("->", "THIN_ARROW"),
("<=", "LTEQ"),
(">=", "GTEQ"),
("+=", "PLUSEQ"),
("-=", "MINUSEQ"),
("|=", "PIPEEQ"),
("&=", "AMPEQ"),
("^=", "CARETEQ"),
("/=", "SLASHEQ"),
("*=", "STAREQ"),
("%=", "PERCENTEQ"),
("&&", "AMPAMP"),
("||", "PIPEPIPE"),
("<<", "SHL"),
(">>", "SHR"),
("<<=", "SHLEQ"),
(">>=", "SHREQ"),
],
keywords: &[
"as", "async", "await", "box", "break", "const", "continue", "crate", "dyn", "else",
"enum", "extern", "false", "fn", "for", "if", "impl", "in", "let", "loop", "macro",
"match", "mod", "move", "mut", "pub", "ref", "return", "self", "static", "struct", "super",
"trait", "true", "try", "type", "unsafe", "use", "where", "while",
],
contextual_keywords: &["auto", "default", "existential", "union"],
literals: &[
"INT_NUMBER",
"FLOAT_NUMBER",
"CHAR",
"BYTE",
"STRING",
"RAW_STRING",
"BYTE_STRING",
"RAW_BYTE_STRING",
],
tokens: &[
"ERROR",
"IDENT",
"WHITESPACE",
"LIFETIME",
"COMMENT",
"SHEBANG",
"L_DOLLAR",
"R_DOLLAR",
],
nodes: &[
"SOURCE_FILE",
"STRUCT_DEF",
"UNION_DEF",
"ENUM_DEF",
"FN_DEF",
"RET_TYPE",
"EXTERN_CRATE_ITEM",
"MODULE",
"USE_ITEM",
"STATIC_DEF",
"CONST_DEF",
"TRAIT_DEF",
"IMPL_BLOCK",
"TYPE_ALIAS_DEF",
"MACRO_CALL",
"TOKEN_TREE",
"MACRO_DEF",
"PAREN_TYPE",
"TUPLE_TYPE",
"NEVER_TYPE",
"PATH_TYPE",
"POINTER_TYPE",
"ARRAY_TYPE",
"SLICE_TYPE",
"REFERENCE_TYPE",
"PLACEHOLDER_TYPE",
"FN_POINTER_TYPE",
"FOR_TYPE",
"IMPL_TRAIT_TYPE",
"DYN_TRAIT_TYPE",
"REF_PAT",
"BOX_PAT",
"BIND_PAT",
"PLACEHOLDER_PAT",
"DOT_DOT_PAT",
"PATH_PAT",
"RECORD_PAT",
"RECORD_FIELD_PAT_LIST",
"RECORD_FIELD_PAT",
"TUPLE_STRUCT_PAT",
"TUPLE_PAT",
"SLICE_PAT",
"RANGE_PAT",
"LITERAL_PAT",
// atoms
"TUPLE_EXPR",
"ARRAY_EXPR",
"PAREN_EXPR",
"PATH_EXPR",
"LAMBDA_EXPR",
"IF_EXPR",
"WHILE_EXPR",
"CONDITION",
"LOOP_EXPR",
"FOR_EXPR",
"CONTINUE_EXPR",
"BREAK_EXPR",
"LABEL",
"BLOCK_EXPR",
"RETURN_EXPR",
"MATCH_EXPR",
"MATCH_ARM_LIST",
"MATCH_ARM",
"MATCH_GUARD",
"RECORD_LIT",
"RECORD_FIELD_LIST",
"RECORD_FIELD",
"TRY_BLOCK_EXPR",
"BOX_EXPR",
// postfix
"CALL_EXPR",
"INDEX_EXPR",
"METHOD_CALL_EXPR",
"FIELD_EXPR",
"AWAIT_EXPR",
"TRY_EXPR",
"CAST_EXPR",
// unary
"REF_EXPR",
"PREFIX_EXPR",
"RANGE_EXPR", // just weird
"BIN_EXPR",
"BLOCK",
"EXTERN_BLOCK",
"EXTERN_ITEM_LIST",
"ENUM_VARIANT",
"RECORD_FIELD_DEF_LIST",
"RECORD_FIELD_DEF",
"TUPLE_FIELD_DEF_LIST",
"TUPLE_FIELD_DEF",
"ENUM_VARIANT_LIST",
"ITEM_LIST",
"ATTR",
"META_ITEM", // not an item actually
"USE_TREE",
"USE_TREE_LIST",
"PATH",
"PATH_SEGMENT",
"LITERAL",
"ALIAS",
"VISIBILITY",
"WHERE_CLAUSE",
"WHERE_PRED",
"ABI",
"NAME",
"NAME_REF",
"LET_STMT",
"EXPR_STMT",
"TYPE_PARAM_LIST",
"LIFETIME_PARAM",
"TYPE_PARAM",
"CONST_PARAM",
"TYPE_ARG_LIST",
"LIFETIME_ARG",
"TYPE_ARG",
"ASSOC_TYPE_ARG",
"PARAM_LIST",
"PARAM",
"SELF_PARAM",
"ARG_LIST",
"TYPE_BOUND",
"TYPE_BOUND_LIST",
// macro related
"MACRO_ITEMS",
"MACRO_STMTS",
],
};
pub(crate) struct AstSrc<'a> {
pub(crate) nodes: &'a [AstNodeSrc<'a>],
pub(crate) enums: &'a [AstEnumSrc<'a>],
}
pub(crate) struct AstNodeSrc<'a> {
pub(crate) name: &'a str,
pub(crate) traits: &'a [&'a str],
pub(crate) fields: &'a [(&'a str, FieldSrc<&'a str>)],
}
pub(crate) enum FieldSrc<T> {
Shorthand,
Optional(T),
Many(T),
}
pub(crate) struct AstEnumSrc<'a> {
pub(crate) name: &'a str,
pub(crate) traits: &'a [&'a str],
pub(crate) variants: &'a [&'a str],
}
macro_rules! ast_nodes {
($(
struct $name:ident$(: $($trait:ident),*)? {
$($field_name:ident $(: $ty:tt)?),*$(,)?
}
)*) => {
[$(
AstNodeSrc {
name: stringify!($name),
traits: &[$($(stringify!($trait)),*)?],
fields: &[$(
(stringify!($field_name), field_ty!($field_name $($ty)?))
),*],
}
),*]
};
}
macro_rules! field_ty {
($field_name:ident) => {
FieldSrc::Shorthand
};
($field_name:ident [$ty:ident]) => {
FieldSrc::Many(stringify!($ty))
};
($field_name:ident $ty:ident) => {
FieldSrc::Optional(stringify!($ty))
};
}
macro_rules! ast_enums {
($(
enum $name:ident $(: $($trait:ident),*)? {
$($variant:ident),*$(,)?
}
)*) => {
[$(
AstEnumSrc {
name: stringify!($name),
traits: &[$($(stringify!($trait)),*)?],
variants: &[$(stringify!($variant)),*],
}
),*]
};
}
pub(crate) const AST_SRC: AstSrc = AstSrc {
nodes: &ast_nodes! {
struct SourceFile: ModuleItemOwner, FnDefOwner {
modules: [Module],
}
struct FnDef: VisibilityOwner, NameOwner, TypeParamsOwner, DocCommentsOwner, AttrsOwner {
ParamList,
RetType,
body: BlockExpr,
}
struct RetType { TypeRef }
struct StructDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
}
struct UnionDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
RecordFieldDefList,
}
struct RecordFieldDefList { fields: [RecordFieldDef] }
struct RecordFieldDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner { }
struct TupleFieldDefList { fields: [TupleFieldDef] }
struct TupleFieldDef: VisibilityOwner, AttrsOwner {
TypeRef,
}
struct EnumDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner {
variant_list: EnumVariantList,
}
struct EnumVariantList {
variants: [EnumVariant],
}
struct EnumVariant: NameOwner, DocCommentsOwner, AttrsOwner {
Expr
}
struct TraitDef: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner, TypeParamsOwner, TypeBoundsOwner {
ItemList,
}
struct Module: VisibilityOwner, NameOwner, AttrsOwner, DocCommentsOwner {
ItemList,
}
struct ItemList: FnDefOwner, ModuleItemOwner {
impl_items: [ImplItem],
}
struct ConstDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
body: Expr,
}
struct StaticDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeAscriptionOwner {
body: Expr,
}
struct TypeAliasDef: VisibilityOwner, NameOwner, TypeParamsOwner, AttrsOwner, DocCommentsOwner, TypeBoundsOwner {
TypeRef,
}
struct ImplBlock: TypeParamsOwner, AttrsOwner {
ItemList,
}
struct ParenType { TypeRef }
struct TupleType { fields: [TypeRef] }
struct NeverType { }
struct PathType { Path }
struct PointerType { TypeRef }
struct ArrayType { TypeRef, Expr }
struct SliceType { TypeRef }
struct ReferenceType { TypeRef }
struct PlaceholderType { }
struct FnPointerType { ParamList, RetType }
struct ForType { TypeRef }
struct ImplTraitType: TypeBoundsOwner {}
struct DynTraitType: TypeBoundsOwner {}
struct TupleExpr { exprs: [Expr] }
struct ArrayExpr { exprs: [Expr] }
struct ParenExpr { Expr }
struct PathExpr { Path }
struct LambdaExpr {
ParamList,
RetType,
body: Expr,
}
struct IfExpr { Condition }
struct LoopExpr: LoopBodyOwner { }
struct TryBlockExpr { body: BlockExpr }
struct ForExpr: LoopBodyOwner {
Pat,
iterable: Expr,
}
struct WhileExpr: LoopBodyOwner { Condition }
struct ContinueExpr {}
struct BreakExpr { Expr }
struct Label {}
struct BlockExpr { Block }
struct ReturnExpr { Expr }
struct CallExpr: ArgListOwner { Expr }
struct MethodCallExpr: ArgListOwner {
Expr, NameRef, TypeArgList,
}
struct IndexExpr {}
struct FieldExpr { Expr, NameRef }
struct AwaitExpr { Expr }
struct TryExpr { Expr }
struct CastExpr { Expr, TypeRef }
struct RefExpr { Expr }
struct PrefixExpr { Expr }
struct BoxExpr { Expr }
struct RangeExpr {}
struct BinExpr {}
struct Literal {}
struct MatchExpr { Expr, MatchArmList }
struct MatchArmList: AttrsOwner { arms: [MatchArm] }
struct MatchArm: AttrsOwner {
pats: [Pat],
guard: MatchGuard,
Expr,
}
struct MatchGuard { Expr }
struct RecordLit { Path, RecordFieldList }
struct RecordFieldList {
fields: [RecordField],
spread: Expr,
}
struct RecordField { NameRef, Expr }
struct RefPat { Pat }
struct BoxPat { Pat }
struct BindPat: NameOwner { Pat }
struct PlaceholderPat { }
struct DotDotPat { }
struct PathPat { Path }
struct SlicePat {}
struct RangePat {}
struct LiteralPat { Literal }
struct RecordPat { RecordFieldPatList, Path }
struct RecordFieldPatList {
record_field_pats: [RecordFieldPat],
bind_pats: [BindPat],
}
struct RecordFieldPat: NameOwner { Pat }
struct TupleStructPat { Path, args: [Pat] }
struct TuplePat { args: [Pat] }
struct Visibility {}
struct Name {}
struct NameRef {}
struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner {
TokenTree, Path
}
struct Attr { Path, input: AttrInput }
struct TokenTree {}
struct TypeParamList {
type_params: [TypeParam],
lifetime_params: [LifetimeParam],
}
struct TypeParam: NameOwner, AttrsOwner, TypeBoundsOwner {
default_type: TypeRef,
}
struct ConstParam: NameOwner, AttrsOwner, TypeAscriptionOwner {
default_val: Expr,
}
struct LifetimeParam: AttrsOwner { }
struct TypeBound { TypeRef}
struct TypeBoundList { bounds: [TypeBound] }
struct WherePred: TypeBoundsOwner { TypeRef }
struct WhereClause { predicates: [WherePred] }
struct ExprStmt { Expr }
struct LetStmt: TypeAscriptionOwner {
Pat,
initializer: Expr,
}
struct Condition { Pat, Expr }
struct Block: AttrsOwner, ModuleItemOwner {
statements: [Stmt],
Expr,
}
struct ParamList {
SelfParam,
params: [Param],
}
struct SelfParam: TypeAscriptionOwner, AttrsOwner { }
struct Param: TypeAscriptionOwner, AttrsOwner {
Pat,
}
struct UseItem: AttrsOwner, VisibilityOwner {
UseTree,
}
struct UseTree {
Path, UseTreeList, Alias
}
struct Alias: NameOwner { }
struct UseTreeList { use_trees: [UseTree] }
struct ExternCrateItem: AttrsOwner, VisibilityOwner {
NameRef, Alias,
}
struct ArgList {
args: [Expr],
}
struct Path {
segment: PathSegment,
qualifier: Path,
}
struct PathSegment {
NameRef, TypeArgList, ParamList, RetType, PathType,
}
struct TypeArgList {
type_args: [TypeArg],
lifetime_args: [LifetimeArg],
assoc_type_args: [AssocTypeArg],
}
struct TypeArg { TypeRef }
struct AssocTypeArg { NameRef, TypeRef }
struct LifetimeArg {}
struct MacroItems: ModuleItemOwner, FnDefOwner { }
struct MacroStmts {
statements: [Stmt],
Expr,
}
},
enums: &ast_enums! {
enum NominalDef: NameOwner, TypeParamsOwner, AttrsOwner {
StructDef, EnumDef, UnionDef,
}
enum TypeRef {
ParenType,
TupleType,
NeverType,
PathType,
PointerType,
ArrayType,
SliceType,
ReferenceType,
PlaceholderType,
FnPointerType,
ForType,
ImplTraitType,
DynTraitType,
}
enum ModuleItem: AttrsOwner, VisibilityOwner {
StructDef,
UnionDef,
EnumDef,
FnDef,
TraitDef,
TypeAliasDef,
ImplBlock,
UseItem,
ExternCrateItem,
ConstDef,
StaticDef,
Module,
}
enum ImplItem: AttrsOwner {
FnDef, TypeAliasDef, ConstDef,
}
enum Expr {
TupleExpr,
ArrayExpr,
ParenExpr,
PathExpr,
LambdaExpr,
IfExpr,
LoopExpr,
ForExpr,
WhileExpr,
ContinueExpr,
BreakExpr,
Label,
BlockExpr,
ReturnExpr,
MatchExpr,
RecordLit,
CallExpr,
IndexExpr,
MethodCallExpr,
FieldExpr,
AwaitExpr,
TryExpr,
TryBlockExpr,
CastExpr,
RefExpr,
PrefixExpr,
RangeExpr,
BinExpr,
Literal,
MacroCall,
BoxExpr,
}
enum Pat {
RefPat,
BoxPat,
BindPat,
PlaceholderPat,
DotDotPat,
PathPat,
RecordPat,
TupleStructPat,
TuplePat,
SlicePat,
RangePat,
LiteralPat,
}
enum AttrInput { Literal, TokenTree }
enum Stmt { ExprStmt, LetStmt }
},
};

View file

@ -24,7 +24,6 @@ pub use self::{
gen_syntax::generate_syntax,
};
pub const GRAMMAR: &str = "crates/ra_syntax/src/grammar.ron";
const GRAMMAR_DIR: &str = "crates/ra_parser/src/grammar";
const OK_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/ok";
const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err";

View file

@ -3,152 +3,145 @@
//! Specifically, it generates the `SyntaxKind` enum and a number of newtype
//! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`.
use std::{collections::BTreeMap, fs};
use proc_macro2::{Punct, Spacing};
use quote::{format_ident, quote};
use ron;
use serde::Deserialize;
use crate::{
ast_src::{AstSrc, FieldSrc, KindsSrc, AST_SRC, KINDS_SRC},
codegen::{self, update, Mode},
project_root, Result,
};
pub fn generate_syntax(mode: Mode) -> Result<()> {
let grammar = project_root().join(codegen::GRAMMAR);
let grammar: Grammar = {
let text = fs::read_to_string(grammar)?;
ron::de::from_str(&text)?
};
let syntax_kinds_file = project_root().join(codegen::SYNTAX_KINDS);
let syntax_kinds = generate_syntax_kinds(&grammar)?;
let syntax_kinds = generate_syntax_kinds(KINDS_SRC)?;
update(syntax_kinds_file.as_path(), &syntax_kinds, mode)?;
let ast_file = project_root().join(codegen::AST);
let ast = generate_ast(&grammar)?;
let ast = generate_ast(AST_SRC)?;
update(ast_file.as_path(), &ast, mode)?;
Ok(())
}
fn generate_ast(grammar: &Grammar) -> Result<String> {
let nodes = grammar.ast.iter().map(|(name, ast_node)| {
let variants =
ast_node.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
let name = format_ident!("{}", name);
let adt = if variants.is_empty() {
let kind = format_ident!("{}", to_upper_snake_case(&name.to_string()));
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name {
pub(crate) syntax: SyntaxNode,
}
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#kind => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
}
} else {
let kinds = variants
.iter()
.map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
.collect::<Vec<_>>();
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum #name {
#(#variants(#variants),)*
}
#(
impl From<#variants> for #name {
fn from(node: #variants) -> #name {
#name::#variants(node)
}
}
)*
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
}
}
}
}
};
let traits = ast_node.traits.iter().map(|trait_name| {
fn generate_ast(grammar: AstSrc<'_>) -> Result<String> {
let nodes = grammar.nodes.iter().map(|node| {
let name = format_ident!("{}", node.name);
let kind = format_ident!("{}", to_upper_snake_case(&name.to_string()));
let traits = node.traits.iter().map(|trait_name| {
let trait_name = format_ident!("{}", trait_name);
quote!(impl ast::#trait_name for #name {})
});
let collections = ast_node.collections.iter().map(|(name, kind)| {
let method_name = format_ident!("{}", name);
let kind = format_ident!("{}", kind);
quote! {
pub fn #method_name(&self) -> AstChildren<#kind> {
AstChildren::new(&self.syntax)
}
}
});
let methods = node.fields.iter().map(|(name, field)| {
let method_name = match field {
FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(&name)),
_ => format_ident!("{}", name),
};
let ty = match field {
FieldSrc::Optional(ty) | FieldSrc::Many(ty) => ty,
FieldSrc::Shorthand => name,
};
let ty = format_ident!("{}", ty);
let options = ast_node.options.iter().map(|attr| {
let method_name = match attr {
Attr::Type(t) => format_ident!("{}", to_lower_snake_case(&t)),
Attr::NameType(n, _) => format_ident!("{}", n),
};
let ty = match attr {
Attr::Type(t) | Attr::NameType(_, t) => format_ident!("{}", t),
};
quote! {
pub fn #method_name(&self) -> Option<#ty> {
AstChildren::new(&self.syntax).next()
match field {
FieldSrc::Many(_) => {
quote! {
pub fn #method_name(&self) -> AstChildren<#ty> {
AstChildren::new(&self.syntax)
}
}
}
FieldSrc::Optional(_) | FieldSrc::Shorthand => {
quote! {
pub fn #method_name(&self) -> Option<#ty> {
AstChildren::new(&self.syntax).next()
}
}
}
}
});
quote! {
#adt
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct #name {
pub(crate) syntax: SyntaxNode,
}
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#kind => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None }
}
fn syntax(&self) -> &SyntaxNode { &self.syntax }
}
#(#traits)*
impl #name {
#(#collections)*
#(#options)*
#(#methods)*
}
}
});
let enums = grammar.enums.iter().map(|en| {
let variants = en.variants.iter().map(|var| format_ident!("{}", var)).collect::<Vec<_>>();
let name = format_ident!("{}", en.name);
let kinds = variants
.iter()
.map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string())))
.collect::<Vec<_>>();
let traits = en.traits.iter().map(|trait_name| {
let trait_name = format_ident!("{}", trait_name);
quote!(impl ast::#trait_name for #name {})
});
quote! {
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum #name {
#(#variants(#variants),)*
}
#(
impl From<#variants> for #name {
fn from(node: #variants) -> #name {
#name::#variants(node)
}
}
)*
impl AstNode for #name {
fn can_cast(kind: SyntaxKind) -> bool {
match kind {
#(#kinds)|* => true,
_ => false,
}
}
fn cast(syntax: SyntaxNode) -> Option<Self> {
let res = match syntax.kind() {
#(
#kinds => #name::#variants(#variants { syntax }),
)*
_ => return None,
};
Some(res)
}
fn syntax(&self) -> &SyntaxNode {
match self {
#(
#name::#variants(it) => &it.syntax,
)*
}
}
}
#(#traits)*
}
});
let ast = quote! {
use crate::{
SyntaxNode, SyntaxKind::{self, *},
@ -156,13 +149,14 @@ fn generate_ast(grammar: &Grammar) -> Result<String> {
};
#(#nodes)*
#(#enums)*
};
let pretty = codegen::reformat(ast)?;
Ok(pretty)
}
fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result<String> {
let (single_byte_tokens_values, single_byte_tokens): (Vec<_>, Vec<_>) = grammar
.punct
.iter()
@ -274,38 +268,6 @@ fn generate_syntax_kinds(grammar: &Grammar) -> Result<String> {
codegen::reformat(ast)
}
#[derive(Deserialize, Debug)]
struct Grammar {
punct: Vec<(String, String)>,
keywords: Vec<String>,
contextual_keywords: Vec<String>,
literals: Vec<String>,
tokens: Vec<String>,
nodes: Vec<String>,
ast: BTreeMap<String, AstNode>,
}
#[derive(Deserialize, Debug)]
struct AstNode {
#[serde(default)]
#[serde(rename = "enum")]
variants: Vec<String>,
#[serde(default)]
traits: Vec<String>,
#[serde(default)]
collections: Vec<(String, String)>,
#[serde(default)]
options: Vec<Attr>,
}
#[derive(Deserialize, Debug)]
#[serde(untagged)]
enum Attr {
Type(String),
NameType(String, String),
}
fn to_upper_snake_case(s: &str) -> String {
let mut buf = String::with_capacity(s.len());
let mut prev_is_upper = None;

View file

@ -1,6 +1,7 @@
//! FIXME: write short doc here
pub mod codegen;
mod ast_src;
use anyhow::Context;
pub use anyhow::Result;