From 8d71a6bf0ca51ae099a5b470afdb957bca321441 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 10 Apr 2020 11:49:13 +0200 Subject: [PATCH] Scale token generation back --- crates/ra_assists/src/handlers/add_impl.rs | 2 +- crates/ra_assists/src/handlers/add_new.rs | 3 +- crates/ra_parser/src/syntax_kind/generated.rs | 6 + crates/ra_syntax/src/ast/extensions.rs | 12 +- crates/ra_syntax/src/ast/generated/nodes.rs | 124 ++++----- crates/ra_syntax/src/ast/generated/tokens.rs | 239 ------------------ xtask/src/ast_src.rs | 114 +++------ xtask/src/codegen.rs | 1 - xtask/src/codegen/gen_syntax.rs | 182 ++----------- 9 files changed, 136 insertions(+), 547 deletions(-) diff --git a/crates/ra_assists/src/handlers/add_impl.rs b/crates/ra_assists/src/handlers/add_impl.rs index 26dfed2376..6622eadb2d 100644 --- a/crates/ra_assists/src/handlers/add_impl.rs +++ b/crates/ra_assists/src/handlers/add_impl.rs @@ -1,5 +1,5 @@ use ra_syntax::{ - ast::{self, AstNode, AstToken, NameOwner, TypeParamsOwner}, + ast::{self, AstNode, NameOwner, TypeParamsOwner}, TextUnit, }; use stdx::{format_to, SepBy}; diff --git a/crates/ra_assists/src/handlers/add_new.rs b/crates/ra_assists/src/handlers/add_new.rs index 30360af942..240b19fa37 100644 --- a/crates/ra_assists/src/handlers/add_new.rs +++ b/crates/ra_assists/src/handlers/add_new.rs @@ -1,8 +1,7 @@ use hir::Adt; use ra_syntax::{ ast::{ - self, AstNode, AstToken, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, - VisibilityOwner, + self, AstNode, NameOwner, StructKind, TypeAscriptionOwner, TypeParamsOwner, VisibilityOwner, }, TextUnit, T, }; diff --git a/crates/ra_parser/src/syntax_kind/generated.rs b/crates/ra_parser/src/syntax_kind/generated.rs index 004f4e5643..1dd4ce34a7 100644 --- a/crates/ra_parser/src/syntax_kind/generated.rs +++ b/crates/ra_parser/src/syntax_kind/generated.rs @@ -655,4 +655,10 @@ macro_rules! T { ( raw ) => { $crate::SyntaxKind::RAW_KW }; + ( lifetime ) => { + $crate::SyntaxKind::LIFETIME + }; + ( ident ) => { + $crate::SyntaxKind::IDENT + }; } diff --git a/crates/ra_syntax/src/ast/extensions.rs b/crates/ra_syntax/src/ast/extensions.rs index eee98ebc22..03d94170da 100644 --- a/crates/ra_syntax/src/ast/extensions.rs +++ b/crates/ra_syntax/src/ast/extensions.rs @@ -21,11 +21,7 @@ impl ast::NameRef { } pub fn as_tuple_field(&self) -> Option { - if let Some(ast::NameRefToken::IntNumber(token)) = self.name_ref_token_token() { - token.text().as_str().parse().ok() - } else { - None - } + self.text().parse().ok() } } @@ -315,7 +311,7 @@ pub enum TypeBoundKind { /// for<'a> ... ForType(ast::ForType), /// 'a - Lifetime(ast::Lifetime), + Lifetime(SyntaxToken), } impl ast::TypeBound { @@ -401,7 +397,7 @@ impl ast::RangePat { pub fn start(&self) -> Option { self.syntax() .children_with_tokens() - .take_while(|it| !ast::RangeSeparator::can_cast(it.kind())) + .take_while(|it| !(it.kind() == T![..] || it.kind() == T![..=])) .filter_map(|it| it.into_node()) .find_map(ast::Pat::cast) } @@ -409,7 +405,7 @@ impl ast::RangePat { pub fn end(&self) -> Option { self.syntax() .children_with_tokens() - .skip_while(|it| !ast::RangeSeparator::can_cast(it.kind())) + .skip_while(|it| !(it.kind() == T![..] || it.kind() == T![..=])) .filter_map(|it| it.into_node()) .find_map(ast::Pat::cast) } diff --git a/crates/ra_syntax/src/ast/generated/nodes.rs b/crates/ra_syntax/src/ast/generated/nodes.rs index 106a91c12b..d67d733b18 100644 --- a/crates/ra_syntax/src/ast/generated/nodes.rs +++ b/crates/ra_syntax/src/ast/generated/nodes.rs @@ -1,6 +1,5 @@ //! Generated file, do not edit by hand, see `xtask/src/codegen` -use super::tokens::*; use crate::{ ast::{self, support, AstChildren, AstNode}, SyntaxKind::{self, *}, @@ -463,7 +462,7 @@ impl ImplDef { pub fn const_token(&self) -> Option { support::token2(&self.syntax, T![const]) } pub fn unsafe_token(&self) -> Option { support::token2(&self.syntax, T![unsafe]) } pub fn impl_token(&self) -> Option { support::token2(&self.syntax, T![impl]) } - pub fn excl_token(&self) -> Option { support::token(&self.syntax) } + pub fn excl_token(&self) -> Option { support::token2(&self.syntax, T![!]) } pub fn for_token(&self) -> Option { support::token2(&self.syntax, T![for]) } pub fn item_list(&self) -> Option { support::child(&self.syntax) } } @@ -523,7 +522,7 @@ impl AstNode for NeverType { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl NeverType { - pub fn excl_token(&self) -> Option { support::token(&self.syntax) } + pub fn excl_token(&self) -> Option { support::token2(&self.syntax, T![!]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct PathType { @@ -559,7 +558,7 @@ impl AstNode for PointerType { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PointerType { - pub fn star_token(&self) -> Option { support::token(&self.syntax) } + pub fn star_token(&self) -> Option { support::token2(&self.syntax, T![*]) } pub fn const_token(&self) -> Option { support::token2(&self.syntax, T![const]) } pub fn mut_token(&self) -> Option { support::token2(&self.syntax, T![mut]) } pub fn type_ref(&self) -> Option { support::child(&self.syntax) } @@ -622,8 +621,10 @@ impl AstNode for ReferenceType { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ReferenceType { - pub fn amp_token(&self) -> Option { support::token(&self.syntax) } - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn amp_token(&self) -> Option { support::token2(&self.syntax, T![&]) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } pub fn mut_token(&self) -> Option { support::token2(&self.syntax, T![mut]) } pub fn type_ref(&self) -> Option { support::child(&self.syntax) } } @@ -643,7 +644,7 @@ impl AstNode for PlaceholderType { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PlaceholderType { - pub fn underscore_token(&self) -> Option { support::token(&self.syntax) } + pub fn underscore_token(&self) -> Option { support::token2(&self.syntax, T![_]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct FnPointerType { @@ -955,7 +956,9 @@ impl ContinueExpr { pub fn continue_token(&self) -> Option { support::token2(&self.syntax, T![continue]) } - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BreakExpr { @@ -975,7 +978,9 @@ impl AstNode for BreakExpr { impl ast::AttrsOwner for BreakExpr {} impl BreakExpr { pub fn break_token(&self) -> Option { support::token2(&self.syntax, T![break]) } - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } pub fn expr(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -994,7 +999,9 @@ impl AstNode for Label { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl Label { - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BlockExpr { @@ -1074,7 +1081,7 @@ impl ast::AttrsOwner for MethodCallExpr {} impl ast::ArgListOwner for MethodCallExpr {} impl MethodCallExpr { pub fn expr(&self) -> Option { support::child(&self.syntax) } - pub fn dot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dot_token(&self) -> Option { support::token2(&self.syntax, T![.]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } pub fn type_arg_list(&self) -> Option { support::child(&self.syntax) } } @@ -1116,7 +1123,7 @@ impl AstNode for FieldExpr { impl ast::AttrsOwner for FieldExpr {} impl FieldExpr { pub fn expr(&self) -> Option { support::child(&self.syntax) } - pub fn dot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dot_token(&self) -> Option { support::token2(&self.syntax, T![.]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1137,7 +1144,7 @@ impl AstNode for AwaitExpr { impl ast::AttrsOwner for AwaitExpr {} impl AwaitExpr { pub fn expr(&self) -> Option { support::child(&self.syntax) } - pub fn dot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dot_token(&self) -> Option { support::token2(&self.syntax, T![.]) } pub fn await_token(&self) -> Option { support::token2(&self.syntax, T![await]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1198,7 +1205,7 @@ impl AstNode for RefExpr { } impl ast::AttrsOwner for RefExpr {} impl RefExpr { - pub fn amp_token(&self) -> Option { support::token(&self.syntax) } + pub fn amp_token(&self) -> Option { support::token2(&self.syntax, T![&]) } pub fn raw_token(&self) -> Option { support::token2(&self.syntax, T![raw]) } pub fn mut_token(&self) -> Option { support::token2(&self.syntax, T![mut]) } pub fn expr(&self) -> Option { support::child(&self.syntax) } @@ -1220,7 +1227,6 @@ impl AstNode for PrefixExpr { } impl ast::AttrsOwner for PrefixExpr {} impl PrefixExpr { - pub fn prefix_op_token(&self) -> Option { support::token(&self.syntax) } pub fn expr(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1259,9 +1265,7 @@ impl AstNode for RangeExpr { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl ast::AttrsOwner for RangeExpr {} -impl RangeExpr { - pub fn range_op_token(&self) -> Option { support::token(&self.syntax) } -} +impl RangeExpr {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct BinExpr { pub(crate) syntax: SyntaxNode, @@ -1294,9 +1298,7 @@ impl AstNode for Literal { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Literal { - pub fn literal_token_token(&self) -> Option { support::token(&self.syntax) } -} +impl Literal {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MatchExpr { pub(crate) syntax: SyntaxNode, @@ -1358,7 +1360,7 @@ impl ast::AttrsOwner for MatchArm {} impl MatchArm { pub fn pat(&self) -> Option { support::child(&self.syntax) } pub fn guard(&self) -> Option { support::child(&self.syntax) } - pub fn fat_arrow_token(&self) -> Option { support::token(&self.syntax) } + pub fn fat_arrow_token(&self) -> Option { support::token2(&self.syntax, T![=>]) } pub fn expr(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1417,7 +1419,7 @@ impl AstNode for RecordFieldList { impl RecordFieldList { pub fn l_curly_token(&self) -> Option { support::token2(&self.syntax, T!['{']) } pub fn fields(&self) -> AstChildren { support::children(&self.syntax) } - pub fn dotdot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dotdot_token(&self) -> Option { support::token2(&self.syntax, T![..]) } pub fn spread(&self) -> Option { support::child(&self.syntax) } pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } @@ -1439,7 +1441,7 @@ impl AstNode for RecordField { impl ast::AttrsOwner for RecordField {} impl RecordField { pub fn name_ref(&self) -> Option { support::child(&self.syntax) } - pub fn colon_token(&self) -> Option { support::token(&self.syntax) } + pub fn colon_token(&self) -> Option { support::token2(&self.syntax, T![:]) } pub fn expr(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1496,7 +1498,7 @@ impl AstNode for RefPat { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl RefPat { - pub fn amp_token(&self) -> Option { support::token(&self.syntax) } + pub fn amp_token(&self) -> Option { support::token2(&self.syntax, T![&]) } pub fn mut_token(&self) -> Option { support::token2(&self.syntax, T![mut]) } pub fn pat(&self) -> Option { support::child(&self.syntax) } } @@ -1539,7 +1541,7 @@ impl ast::NameOwner for BindPat {} impl BindPat { pub fn ref_token(&self) -> Option { support::token2(&self.syntax, T![ref]) } pub fn mut_token(&self) -> Option { support::token2(&self.syntax, T![mut]) } - pub fn at_token(&self) -> Option { support::token(&self.syntax) } + pub fn at_token(&self) -> Option { support::token2(&self.syntax, T![@]) } pub fn pat(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1558,7 +1560,7 @@ impl AstNode for PlaceholderPat { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PlaceholderPat { - pub fn underscore_token(&self) -> Option { support::token(&self.syntax) } + pub fn underscore_token(&self) -> Option { support::token2(&self.syntax, T![_]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct DotDotPat { @@ -1576,7 +1578,7 @@ impl AstNode for DotDotPat { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl DotDotPat { - pub fn dotdot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dotdot_token(&self) -> Option { support::token2(&self.syntax, T![..]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct PathPat { @@ -1631,9 +1633,7 @@ impl AstNode for RangePat { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl RangePat { - pub fn range_separator_token(&self) -> Option { support::token(&self.syntax) } -} +impl RangePat {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct LiteralPat { pub(crate) syntax: SyntaxNode, @@ -1713,7 +1713,7 @@ impl RecordFieldPatList { support::children(&self.syntax) } pub fn bind_pats(&self) -> AstChildren { support::children(&self.syntax) } - pub fn dotdot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dotdot_token(&self) -> Option { support::token2(&self.syntax, T![..]) } pub fn r_curly_token(&self) -> Option { support::token2(&self.syntax, T!['}']) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1734,7 +1734,7 @@ impl AstNode for RecordFieldPat { impl ast::AttrsOwner for RecordFieldPat {} impl ast::NameOwner for RecordFieldPat {} impl RecordFieldPat { - pub fn colon_token(&self) -> Option { support::token(&self.syntax) } + pub fn colon_token(&self) -> Option { support::token2(&self.syntax, T![:]) } pub fn pat(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -1815,7 +1815,7 @@ impl AstNode for Name { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl Name { - pub fn ident_token(&self) -> Option { support::token(&self.syntax) } + pub fn ident_token(&self) -> Option { support::token2(&self.syntax, T![ident]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct NameRef { @@ -1832,9 +1832,7 @@ impl AstNode for NameRef { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl NameRef { - pub fn name_ref_token_token(&self) -> Option { support::token(&self.syntax) } -} +impl NameRef {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct MacroCall { pub(crate) syntax: SyntaxNode, @@ -1855,7 +1853,7 @@ impl ast::AttrsOwner for MacroCall {} impl ast::DocCommentsOwner for MacroCall {} impl MacroCall { pub fn path(&self) -> Option { support::child(&self.syntax) } - pub fn excl_token(&self) -> Option { support::token(&self.syntax) } + pub fn excl_token(&self) -> Option { support::token2(&self.syntax, T![!]) } pub fn token_tree(&self) -> Option { support::child(&self.syntax) } pub fn semicolon_token(&self) -> Option { support::token2(&self.syntax, T![;]) } } @@ -1875,8 +1873,8 @@ impl AstNode for Attr { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl Attr { - pub fn pound_token(&self) -> Option { support::token(&self.syntax) } - pub fn excl_token(&self) -> Option { support::token(&self.syntax) } + pub fn pound_token(&self) -> Option { support::token2(&self.syntax, T![#]) } + pub fn excl_token(&self) -> Option { support::token2(&self.syntax, T![!]) } pub fn l_brack_token(&self) -> Option { support::token2(&self.syntax, T!['[']) } pub fn path(&self) -> Option { support::child(&self.syntax) } pub fn eq_token(&self) -> Option { support::token2(&self.syntax, T![=]) } @@ -1915,12 +1913,12 @@ impl AstNode for TypeParamList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl TypeParamList { - pub fn l_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn l_angle_token(&self) -> Option { support::token2(&self.syntax, T![<]) } pub fn generic_params(&self) -> AstChildren { support::children(&self.syntax) } pub fn type_params(&self) -> AstChildren { support::children(&self.syntax) } pub fn lifetime_params(&self) -> AstChildren { support::children(&self.syntax) } pub fn const_params(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_angle_token(&self) -> Option { support::token2(&self.syntax, T![>]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeParam { @@ -1983,7 +1981,9 @@ impl AstNode for LifetimeParam { } impl ast::AttrsOwner for LifetimeParam {} impl LifetimeParam { - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeBound { @@ -2001,7 +2001,9 @@ impl AstNode for TypeBound { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl TypeBound { - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } pub fn const_token(&self) -> Option { support::token2(&self.syntax, T![const]) } pub fn type_ref(&self) -> Option { support::child(&self.syntax) } } @@ -2040,7 +2042,9 @@ impl AstNode for WherePred { } impl ast::TypeBoundsOwner for WherePred {} impl WherePred { - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } pub fn type_ref(&self) -> Option { support::child(&self.syntax) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -2077,9 +2081,7 @@ impl AstNode for Abi { } fn syntax(&self) -> &SyntaxNode { &self.syntax } } -impl Abi { - pub fn string_token(&self) -> Option { support::token(&self.syntax) } -} +impl Abi {} #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ExprStmt { pub(crate) syntax: SyntaxNode, @@ -2207,8 +2209,10 @@ impl AstNode for SelfParam { impl ast::TypeAscriptionOwner for SelfParam {} impl ast::AttrsOwner for SelfParam {} impl SelfParam { - pub fn amp_token(&self) -> Option { support::token(&self.syntax) } - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn amp_token(&self) -> Option { support::token2(&self.syntax, T![&]) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } pub fn self_token(&self) -> Option { support::token2(&self.syntax, T![self]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] @@ -2230,7 +2234,7 @@ impl ast::TypeAscriptionOwner for Param {} impl ast::AttrsOwner for Param {} impl Param { pub fn pat(&self) -> Option { support::child(&self.syntax) } - pub fn dotdotdot_token(&self) -> Option { support::token(&self.syntax) } + pub fn dotdotdot_token(&self) -> Option { support::token2(&self.syntax, T![...]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct UseItem { @@ -2270,7 +2274,7 @@ impl AstNode for UseTree { } impl UseTree { pub fn path(&self) -> Option { support::child(&self.syntax) } - pub fn star_token(&self) -> Option { support::token(&self.syntax) } + pub fn star_token(&self) -> Option { support::token2(&self.syntax, T![*]) } pub fn use_tree_list(&self) -> Option { support::child(&self.syntax) } pub fn alias(&self) -> Option { support::child(&self.syntax) } } @@ -2391,14 +2395,14 @@ impl AstNode for PathSegment { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl PathSegment { - pub fn coloncolon_token(&self) -> Option { support::token(&self.syntax) } - pub fn l_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn coloncolon_token(&self) -> Option { support::token2(&self.syntax, T![::]) } + pub fn l_angle_token(&self) -> Option { support::token2(&self.syntax, T![<]) } pub fn name_ref(&self) -> Option { support::child(&self.syntax) } pub fn type_arg_list(&self) -> Option { support::child(&self.syntax) } pub fn param_list(&self) -> Option { support::child(&self.syntax) } pub fn ret_type(&self) -> Option { support::child(&self.syntax) } pub fn path_type(&self) -> Option { support::child(&self.syntax) } - pub fn r_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_angle_token(&self) -> Option { support::token2(&self.syntax, T![>]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeArgList { @@ -2416,14 +2420,14 @@ impl AstNode for TypeArgList { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl TypeArgList { - pub fn coloncolon_token(&self) -> Option { support::token(&self.syntax) } - pub fn l_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn coloncolon_token(&self) -> Option { support::token2(&self.syntax, T![::]) } + pub fn l_angle_token(&self) -> Option { support::token2(&self.syntax, T![<]) } pub fn generic_args(&self) -> AstChildren { support::children(&self.syntax) } pub fn type_args(&self) -> AstChildren { support::children(&self.syntax) } pub fn lifetime_args(&self) -> AstChildren { support::children(&self.syntax) } pub fn assoc_type_args(&self) -> AstChildren { support::children(&self.syntax) } pub fn const_args(&self) -> AstChildren { support::children(&self.syntax) } - pub fn r_angle_token(&self) -> Option { support::token(&self.syntax) } + pub fn r_angle_token(&self) -> Option { support::token2(&self.syntax, T![>]) } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct TypeArg { @@ -2480,7 +2484,9 @@ impl AstNode for LifetimeArg { fn syntax(&self) -> &SyntaxNode { &self.syntax } } impl LifetimeArg { - pub fn lifetime_token(&self) -> Option { support::token(&self.syntax) } + pub fn lifetime_token(&self) -> Option { + support::token2(&self.syntax, T![lifetime]) + } } #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct ConstArg { diff --git a/crates/ra_syntax/src/ast/generated/tokens.rs b/crates/ra_syntax/src/ast/generated/tokens.rs index 7611ce4cb0..293f91c2e9 100644 --- a/crates/ra_syntax/src/ast/generated/tokens.rs +++ b/crates/ra_syntax/src/ast/generated/tokens.rs @@ -1365,242 +1365,3 @@ impl AstToken for RDollar { } fn syntax(&self) -> &SyntaxToken { &self.syntax } } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum RangeSeparator { - Dotdot(Dotdot), - Dotdotdot(Dotdotdot), - Dotdoteq(Dotdoteq), -} -impl From for RangeSeparator { - fn from(node: Dotdot) -> RangeSeparator { RangeSeparator::Dotdot(node) } -} -impl From for RangeSeparator { - fn from(node: Dotdotdot) -> RangeSeparator { RangeSeparator::Dotdotdot(node) } -} -impl From for RangeSeparator { - fn from(node: Dotdoteq) -> RangeSeparator { RangeSeparator::Dotdoteq(node) } -} -impl std::fmt::Display for RangeSeparator { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for RangeSeparator { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - DOTDOT | DOTDOTDOT | DOTDOTEQ => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - DOTDOT => RangeSeparator::Dotdot(Dotdot { syntax }), - DOTDOTDOT => RangeSeparator::Dotdotdot(Dotdotdot { syntax }), - DOTDOTEQ => RangeSeparator::Dotdoteq(Dotdoteq { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - RangeSeparator::Dotdot(it) => &it.syntax, - RangeSeparator::Dotdotdot(it) => &it.syntax, - RangeSeparator::Dotdoteq(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum PrefixOp { - Minus(Minus), - Excl(Excl), - Star(Star), -} -impl From for PrefixOp { - fn from(node: Minus) -> PrefixOp { PrefixOp::Minus(node) } -} -impl From for PrefixOp { - fn from(node: Excl) -> PrefixOp { PrefixOp::Excl(node) } -} -impl From for PrefixOp { - fn from(node: Star) -> PrefixOp { PrefixOp::Star(node) } -} -impl std::fmt::Display for PrefixOp { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for PrefixOp { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - MINUS | EXCL | STAR => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - MINUS => PrefixOp::Minus(Minus { syntax }), - EXCL => PrefixOp::Excl(Excl { syntax }), - STAR => PrefixOp::Star(Star { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - PrefixOp::Minus(it) => &it.syntax, - PrefixOp::Excl(it) => &it.syntax, - PrefixOp::Star(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum RangeOp { - Dotdot(Dotdot), - Dotdoteq(Dotdoteq), -} -impl From for RangeOp { - fn from(node: Dotdot) -> RangeOp { RangeOp::Dotdot(node) } -} -impl From for RangeOp { - fn from(node: Dotdoteq) -> RangeOp { RangeOp::Dotdoteq(node) } -} -impl std::fmt::Display for RangeOp { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for RangeOp { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - DOTDOT | DOTDOTEQ => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - DOTDOT => RangeOp::Dotdot(Dotdot { syntax }), - DOTDOTEQ => RangeOp::Dotdoteq(Dotdoteq { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - RangeOp::Dotdot(it) => &it.syntax, - RangeOp::Dotdoteq(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum LiteralToken { - IntNumber(IntNumber), - FloatNumber(FloatNumber), - String(String), - RawString(RawString), - ByteString(ByteString), - RawByteString(RawByteString), - Char(Char), - Byte(Byte), -} -impl From for LiteralToken { - fn from(node: IntNumber) -> LiteralToken { LiteralToken::IntNumber(node) } -} -impl From for LiteralToken { - fn from(node: FloatNumber) -> LiteralToken { LiteralToken::FloatNumber(node) } -} -impl From for LiteralToken { - fn from(node: String) -> LiteralToken { LiteralToken::String(node) } -} -impl From for LiteralToken { - fn from(node: RawString) -> LiteralToken { LiteralToken::RawString(node) } -} -impl From for LiteralToken { - fn from(node: ByteString) -> LiteralToken { LiteralToken::ByteString(node) } -} -impl From for LiteralToken { - fn from(node: RawByteString) -> LiteralToken { LiteralToken::RawByteString(node) } -} -impl From for LiteralToken { - fn from(node: Char) -> LiteralToken { LiteralToken::Char(node) } -} -impl From for LiteralToken { - fn from(node: Byte) -> LiteralToken { LiteralToken::Byte(node) } -} -impl std::fmt::Display for LiteralToken { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for LiteralToken { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - INT_NUMBER | FLOAT_NUMBER | STRING | RAW_STRING | BYTE_STRING | RAW_BYTE_STRING - | CHAR | BYTE => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - INT_NUMBER => LiteralToken::IntNumber(IntNumber { syntax }), - FLOAT_NUMBER => LiteralToken::FloatNumber(FloatNumber { syntax }), - STRING => LiteralToken::String(String { syntax }), - RAW_STRING => LiteralToken::RawString(RawString { syntax }), - BYTE_STRING => LiteralToken::ByteString(ByteString { syntax }), - RAW_BYTE_STRING => LiteralToken::RawByteString(RawByteString { syntax }), - CHAR => LiteralToken::Char(Char { syntax }), - BYTE => LiteralToken::Byte(Byte { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - LiteralToken::IntNumber(it) => &it.syntax, - LiteralToken::FloatNumber(it) => &it.syntax, - LiteralToken::String(it) => &it.syntax, - LiteralToken::RawString(it) => &it.syntax, - LiteralToken::ByteString(it) => &it.syntax, - LiteralToken::RawByteString(it) => &it.syntax, - LiteralToken::Char(it) => &it.syntax, - LiteralToken::Byte(it) => &it.syntax, - } - } -} -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub enum NameRefToken { - Ident(Ident), - IntNumber(IntNumber), -} -impl From for NameRefToken { - fn from(node: Ident) -> NameRefToken { NameRefToken::Ident(node) } -} -impl From for NameRefToken { - fn from(node: IntNumber) -> NameRefToken { NameRefToken::IntNumber(node) } -} -impl std::fmt::Display for NameRefToken { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } -} -impl AstToken for NameRefToken { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - IDENT | INT_NUMBER => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - IDENT => NameRefToken::Ident(Ident { syntax }), - INT_NUMBER => NameRefToken::IntNumber(IntNumber { syntax }), - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - NameRefToken::Ident(it) => &it.syntax, - NameRefToken::IntNumber(it) => &it.syntax, - } - } -} diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs index f9cd12d0d1..554afc76aa 100644 --- a/xtask/src/ast_src.rs +++ b/xtask/src/ast_src.rs @@ -227,7 +227,6 @@ pub(crate) const KINDS_SRC: KindsSrc = KindsSrc { pub(crate) struct AstSrc<'a> { pub(crate) nodes: &'a [AstNodeSrc<'a>], pub(crate) enums: &'a [AstEnumSrc<'a>], - pub(crate) token_enums: &'a [AstEnumSrc<'a>], } pub(crate) struct AstNodeSrc<'a> { @@ -415,11 +414,11 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct TupleType { T!['('], fields: [TypeRef], T![')'] } struct NeverType { T![!] } struct PathType { Path } - struct PointerType { Star, T![const], T![mut], TypeRef } + struct PointerType { T![*], T![const], T![mut], TypeRef } struct ArrayType { T!['['], TypeRef, T![;], Expr, T![']'] } struct SliceType { T!['['], TypeRef, T![']'] } - struct ReferenceType { Amp, Lifetime, T![mut], TypeRef } - struct PlaceholderType { Underscore } + struct ReferenceType { T![&], T![lifetime], T![mut], TypeRef } + struct PlaceholderType { T![_] } struct FnPointerType { Abi, T![unsafe], T![fn], ParamList, RetType } struct ForType { T![for], TypeParamList, TypeRef } struct ImplTraitType: TypeBoundsOwner { T![impl] } @@ -447,33 +446,33 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { iterable: Expr, } struct WhileExpr: AttrsOwner, LoopBodyOwner { T![while], Condition } - struct ContinueExpr: AttrsOwner { T![continue], Lifetime } - struct BreakExpr: AttrsOwner { T![break], Lifetime, Expr } - struct Label { Lifetime } + struct ContinueExpr: AttrsOwner { T![continue], T![lifetime] } + struct BreakExpr: AttrsOwner { T![break], T![lifetime], Expr } + struct Label { T![lifetime] } struct BlockExpr: AttrsOwner { Label, T![unsafe], Block } struct ReturnExpr: AttrsOwner { Expr } struct CallExpr: ArgListOwner { Expr } struct MethodCallExpr: AttrsOwner, ArgListOwner { - Expr, Dot, NameRef, TypeArgList, + Expr, T![.], NameRef, TypeArgList, } struct IndexExpr: AttrsOwner { T!['['], T![']'] } - struct FieldExpr: AttrsOwner { Expr, Dot, NameRef } - struct AwaitExpr: AttrsOwner { Expr, Dot, T![await] } + struct FieldExpr: AttrsOwner { Expr, T![.], NameRef } + struct AwaitExpr: AttrsOwner { Expr, T![.], T![await] } struct TryExpr: AttrsOwner { T![try], Expr } struct CastExpr: AttrsOwner { Expr, T![as], TypeRef } - struct RefExpr: AttrsOwner { Amp, T![raw], T![mut], Expr } - struct PrefixExpr: AttrsOwner { PrefixOp, Expr } + struct RefExpr: AttrsOwner { T![&], T![raw], T![mut], Expr } + struct PrefixExpr: AttrsOwner { /*PrefixOp,*/ Expr } struct BoxExpr: AttrsOwner { T![box], Expr } - struct RangeExpr: AttrsOwner { RangeOp } + struct RangeExpr: AttrsOwner { /*RangeOp*/ } struct BinExpr: AttrsOwner { /*BinOp*/ } - struct Literal { LiteralToken } + struct Literal { /*LiteralToken*/ } struct MatchExpr: AttrsOwner { T![match], Expr, MatchArmList } struct MatchArmList: AttrsOwner { T!['{'], arms: [MatchArm], T!['}'] } struct MatchArm: AttrsOwner { pat: Pat, guard: MatchGuard, - FatArrow, + T![=>], Expr, } struct MatchGuard { T![if], Expr } @@ -482,22 +481,22 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { struct RecordFieldList { T!['{'], fields: [RecordField], - Dotdot, + T![..], spread: Expr, T!['}'] } - struct RecordField: AttrsOwner { NameRef, Colon, Expr } + struct RecordField: AttrsOwner { NameRef, T![:], Expr } struct OrPat { pats: [Pat] } struct ParenPat { T!['('], Pat, T![')'] } - struct RefPat { Amp, T![mut], Pat } + struct RefPat { T![&], T![mut], Pat } struct BoxPat { T![box], Pat } - struct BindPat: AttrsOwner, NameOwner { T![ref], T![mut], At, Pat } - struct PlaceholderPat { Underscore } - struct DotDotPat { Dotdot } + struct BindPat: AttrsOwner, NameOwner { T![ref], T![mut], T![@], Pat } + struct PlaceholderPat { T![_] } + struct DotDotPat { T![..] } struct PathPat { Path } struct SlicePat { T!['['], args: [Pat], T![']'] } - struct RangePat { RangeSeparator } + struct RangePat { /*RangeSeparator*/ } struct LiteralPat { Literal } struct MacroPat { MacroCall } @@ -507,30 +506,30 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { pats: [RecordInnerPat], record_field_pats: [RecordFieldPat], bind_pats: [BindPat], - Dotdot, + T![..], T!['}'] } - struct RecordFieldPat: AttrsOwner, NameOwner { Colon, Pat } + struct RecordFieldPat: AttrsOwner, NameOwner { T![:], Pat } struct TupleStructPat { Path, T!['('], args: [Pat], T![')'] } struct TuplePat { T!['('], args: [Pat], T![')'] } struct Visibility { T![pub], T![super], T![self], T![crate] } - struct Name { Ident } - struct NameRef { NameRefToken } + struct Name { T![ident] } + struct NameRef { /*NameRefToken*/ } struct MacroCall: NameOwner, AttrsOwner,DocCommentsOwner { Path, T![!], TokenTree, T![;] } - struct Attr { Pound, T![!], T!['['], Path, T![=], input: AttrInput, T![']'] } + struct Attr { T![#], T![!], T!['['], Path, T![=], input: AttrInput, T![']'] } struct TokenTree {} struct TypeParamList { - LAngle, + T![<], generic_params: [GenericParam], type_params: [TypeParam], lifetime_params: [LifetimeParam], const_params: [ConstParam], - RAngle + T![>] } struct TypeParam: NameOwner, AttrsOwner, TypeBoundsOwner { T![=], @@ -540,12 +539,12 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { T![=], default_val: Expr, } - struct LifetimeParam: AttrsOwner { Lifetime} - struct TypeBound { Lifetime, /* Question, */ T![const], /* Question, */ TypeRef} + struct LifetimeParam: AttrsOwner { T![lifetime] } + struct TypeBound { T![lifetime], /* Question, */ T![const], /* Question, */ TypeRef} struct TypeBoundList { bounds: [TypeBound] } - struct WherePred: TypeBoundsOwner { Lifetime, TypeRef } + struct WherePred: TypeBoundsOwner { T![lifetime], TypeRef } struct WhereClause { T![where], predicates: [WherePred] } - struct Abi { String } + struct Abi { /*String*/ } struct ExprStmt: AttrsOwner { Expr, T![;] } struct LetStmt: AttrsOwner, TypeAscriptionOwner { T![let], @@ -567,17 +566,17 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { params: [Param], T![')'] } - struct SelfParam: TypeAscriptionOwner, AttrsOwner { Amp, Lifetime, T![self] } + struct SelfParam: TypeAscriptionOwner, AttrsOwner { T![&], T![lifetime], T![self] } struct Param: TypeAscriptionOwner, AttrsOwner { Pat, - Dotdotdot + T![...] } struct UseItem: AttrsOwner, VisibilityOwner { T![use], UseTree, } struct UseTree { - Path, Star, UseTreeList, Alias + Path, T![*], UseTreeList, Alias } struct Alias: NameOwner { T![as] } struct UseTreeList { T!['{'], use_trees: [UseTree], T!['}'] } @@ -594,21 +593,21 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { qualifier: Path, } struct PathSegment { - Coloncolon, LAngle, NameRef, TypeArgList, ParamList, RetType, PathType, RAngle + T![::], T![<], NameRef, TypeArgList, ParamList, RetType, PathType, T![>] } struct TypeArgList { - Coloncolon, - LAngle, + T![::], + T![<], generic_args: [GenericArg], type_args: [TypeArg], lifetime_args: [LifetimeArg], assoc_type_args: [AssocTypeArg], const_args: [ConstArg], - RAngle + T![>] } struct TypeArg { TypeRef } struct AssocTypeArg : TypeBoundsOwner { NameRef, T![=], TypeRef } - struct LifetimeArg { Lifetime } + struct LifetimeArg { T![lifetime] } struct ConstArg { Literal, T![=], BlockExpr } struct MacroItems: ModuleItemOwner{ } @@ -767,37 +766,4 @@ pub(crate) const AST_SRC: AstSrc = AstSrc { TupleFieldDefList, } }, - - token_enums: &ast_enums! { - enum RangeSeparator { Dotdot, Dotdotdot, Dotdoteq} - - enum PrefixOp { - Minus, - T![!], - Star - } - - enum RangeOp { - Dotdot, - Dotdoteq - } - - enum LiteralToken { - IntNumber, - FloatNumber, - String, - RawString, - // TrueKw, - // FalseKw, - ByteString, - RawByteString, - Char, - Byte - } - - enum NameRefToken { - Ident, - IntNumber - } - }, }; diff --git a/xtask/src/codegen.rs b/xtask/src/codegen.rs index 678b40133b..e6ba2009c1 100644 --- a/xtask/src/codegen.rs +++ b/xtask/src/codegen.rs @@ -24,7 +24,6 @@ const ERR_INLINE_TESTS_DIR: &str = "crates/ra_syntax/test_data/parser/inline/err const SYNTAX_KINDS: &str = "crates/ra_parser/src/syntax_kind/generated.rs"; const AST_NODES: &str = "crates/ra_syntax/src/ast/generated/nodes.rs"; -const AST_TOKENS: &str = "crates/ra_syntax/src/ast/generated/tokens.rs"; const ASSISTS_DIR: &str = "crates/ra_assists/src/handlers"; const ASSISTS_TESTS: &str = "crates/ra_assists/src/doc_tests/generated.rs"; diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index 4c9e447a30..ce18f2b8f9 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -3,10 +3,7 @@ //! Specifically, it generates the `SyntaxKind` enum and a number of newtype //! wrappers around `SyntaxNode` which implement `ra_syntax::AstNode`. -use std::{ - borrow::Cow, - collections::{BTreeSet, HashSet}, -}; +use std::collections::{BTreeSet, HashSet}; use proc_macro2::{Punct, Spacing}; use quote::{format_ident, quote}; @@ -26,10 +23,6 @@ pub fn generate_syntax(mode: Mode) -> Result<()> { let contents = generate_nodes(KINDS_SRC, AST_SRC)?; update(ast_nodes_file.as_path(), &contents, mode)?; - let ast_tokens_file = project_root().join(codegen::AST_TOKENS); - let contents = generate_tokens(KINDS_SRC, AST_SRC)?; - update(ast_tokens_file.as_path(), &contents, mode)?; - Ok(()) } @@ -40,147 +33,7 @@ struct ElementKinds { has_tokens: bool, } -fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { - let all_token_kinds: Vec<_> = kinds - .punct - .into_iter() - .map(|(_, kind)| kind) - .copied() - .map(|x| x.into()) - .chain( - kinds - .keywords - .into_iter() - .chain(kinds.contextual_keywords.into_iter()) - .map(|name| Cow::Owned(format!("{}_KW", to_upper_snake_case(&name)))), - ) - .chain(kinds.literals.into_iter().copied().map(|x| x.into())) - .chain(kinds.tokens.into_iter().copied().map(|x| x.into())) - .collect(); - - let tokens = all_token_kinds.iter().filter_map(|kind_str| { - if kind_str.ends_with("_KW") { - return None; - } - let kind_str = &**kind_str; - let kind = format_ident!("{}", kind_str); - let name = format_ident!("{}", to_pascal_case(kind_str)); - let res = quote! { - #[derive(Debug, Clone, PartialEq, Eq, Hash)] - pub struct #name { - pub(crate) syntax: SyntaxToken, - } - - impl std::fmt::Display for #name { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(&self.syntax, f) - } - } - - impl AstToken for #name { - fn can_cast(kind: SyntaxKind) -> bool { kind == #kind } - fn cast(syntax: SyntaxToken) -> Option { - if Self::can_cast(syntax.kind()) { Some(Self { syntax }) } else { None } - } - fn syntax(&self) -> &SyntaxToken { &self.syntax } - } - }; - Some(res) - }); - - let enums = grammar.token_enums.iter().map(|en| { - let variants = en.variants.iter().map(|var| format_ident!("{}", var)).collect::>(); - let name = format_ident!("{}", en.name); - let kinds = variants - .iter() - .map(|name| format_ident!("{}", to_upper_snake_case(&name.to_string()))) - .collect::>(); - assert!(en.traits.is_empty()); - - 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 std::fmt::Display for #name { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - std::fmt::Display::fmt(self.syntax(), f) - } - } - - impl AstToken for #name { - fn can_cast(kind: SyntaxKind) -> bool { - match kind { - #(#kinds)|* => true, - _ => false, - } - } - fn cast(syntax: SyntaxToken) -> Option { - let res = match syntax.kind() { - #( - #kinds => #name::#variants(#variants { syntax }), - )* - _ => return None, - }; - Some(res) - } - fn syntax(&self) -> &SyntaxToken { - match self { - #( - #name::#variants(it) => &it.syntax, - )* - } - } - } - } - }); - - crate::reformat(quote! { - use crate::{SyntaxToken, SyntaxKind::{self, *}, ast::AstToken}; - - #(#tokens)* - #(#enums)* - }) -} - fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { - let all_token_kinds: Vec<_> = kinds - .punct - .into_iter() - .map(|(_, kind)| kind) - .copied() - .map(|x| x.into()) - .chain( - kinds - .keywords - .into_iter() - .chain(kinds.contextual_keywords.into_iter()) - .map(|name| Cow::Owned(format!("{}_KW", to_upper_snake_case(&name)))), - ) - .chain(kinds.literals.into_iter().copied().map(|x| x.into())) - .chain(kinds.tokens.into_iter().copied().map(|x| x.into())) - .collect(); - - let mut token_kinds = HashSet::new(); - for kind in &all_token_kinds { - let kind = &**kind; - let name = to_pascal_case(kind); - token_kinds.insert(name); - } - - for en in grammar.token_enums { - token_kinds.insert(en.name.to_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())); @@ -207,19 +60,9 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { } } } else { - let is_token = token_kinds.contains(&ty.to_string()); - if is_token { - let method_name = format_ident!("{}_token", method_name); - quote! { - pub fn #method_name(&self) -> Option<#ty> { - support::token(&self.syntax) - } - } - } else { - quote! { - pub fn #method_name(&self) -> Option<#ty> { - support::child(&self.syntax) - } + quote! { + pub fn #method_name(&self) -> Option<#ty> { + support::child(&self.syntax) } } } @@ -338,8 +181,6 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { T, }; - use super::tokens::*; - #(#nodes)* #(#enums)* #(#displays)* @@ -456,6 +297,8 @@ fn generate_syntax_kinds(grammar: KindsSrc<'_>) -> Result { macro_rules! T { #((#punctuation_values) => { $crate::SyntaxKind::#punctuation };)* #((#all_keywords_idents) => { $crate::SyntaxKind::#all_keywords };)* + (lifetime) => { $crate::SyntaxKind::LIFETIME }; + (ident) => { $crate::SyntaxKind::IDENT }; } }; @@ -535,8 +378,21 @@ impl Field<'_> { "')'" => "r_paren", "'['" => "l_brack", "']'" => "r_brack", + "<" => "l_angle", + ">" => "r_angle", "=" => "eq", "!" => "excl", + "*" => "star", + "&" => "amp", + "_" => "underscore", + "." => "dot", + ".." => "dotdot", + "..." => "dotdotdot", + "=>" => "fat_arrow", + "@" => "at", + ":" => "colon", + "::" => "coloncolon", + "#" => "pound", _ => name, }; format_ident!("{}_token", name)