From 30084a56a5731343bd4cec727646a6c55900234f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 9 Apr 2020 23:35:05 +0200 Subject: Simpler acessors for keywords --- xtask/src/codegen/gen_syntax.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'xtask/src/codegen') diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs index b5594e3a9..cc98802f6 100644 --- a/xtask/src/codegen/gen_syntax.rs +++ b/xtask/src/codegen/gen_syntax.rs @@ -58,11 +58,14 @@ fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { .chain(kinds.tokens.into_iter().copied().map(|x| x.into())) .collect(); - let tokens = all_token_kinds.iter().map(|kind_str| { + 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)); - quote! { + let res = quote! { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct #name { pub(crate) syntax: SyntaxToken, @@ -81,7 +84,8 @@ fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { } fn syntax(&self) -> &SyntaxToken { &self.syntax } } - } + }; + Some(res) }); let enums = grammar.token_enums.iter().map(|en| { @@ -186,8 +190,12 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { }); let methods = node.fields.iter().map(|(name, field)| { + let is_kw = name.ends_with("Kw"); let method_name = match field { - FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(&name)), + FieldSrc::Shorthand => { + let name = if is_kw { &name[..name.len() - 2] } else { &name }; + format_ident!("{}", to_lower_snake_case(name)) + } _ => format_ident!("{}", name), }; let ty = match field { @@ -209,9 +217,18 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { 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) + if is_kw { + let token_kind = format_ident!("{}", to_upper_snake_case(name)); + quote! { + pub fn #method_name(&self) -> Option { + support::token2(&self.syntax, #token_kind) + } + } + } else { + quote! { + pub fn #method_name(&self) -> Option<#ty> { + support::token(&self.syntax) + } } } } else { @@ -332,7 +349,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result { let ast = quote! { use crate::{ - SyntaxNode, SyntaxKind::{self, *}, + SyntaxNode, SyntaxToken, SyntaxKind::{self, *}, ast::{self, AstNode, AstChildren, support}, }; -- cgit v1.2.3