aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_syntax.rs33
1 files changed, 25 insertions, 8 deletions
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<String> {
58 .chain(kinds.tokens.into_iter().copied().map(|x| x.into())) 58 .chain(kinds.tokens.into_iter().copied().map(|x| x.into()))
59 .collect(); 59 .collect();
60 60
61 let tokens = all_token_kinds.iter().map(|kind_str| { 61 let tokens = all_token_kinds.iter().filter_map(|kind_str| {
62 if kind_str.ends_with("_KW") {
63 return None;
64 }
62 let kind_str = &**kind_str; 65 let kind_str = &**kind_str;
63 let kind = format_ident!("{}", kind_str); 66 let kind = format_ident!("{}", kind_str);
64 let name = format_ident!("{}", to_pascal_case(kind_str)); 67 let name = format_ident!("{}", to_pascal_case(kind_str));
65 quote! { 68 let res = quote! {
66 #[derive(Debug, Clone, PartialEq, Eq, Hash)] 69 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
67 pub struct #name { 70 pub struct #name {
68 pub(crate) syntax: SyntaxToken, 71 pub(crate) syntax: SyntaxToken,
@@ -81,7 +84,8 @@ fn generate_tokens(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
81 } 84 }
82 fn syntax(&self) -> &SyntaxToken { &self.syntax } 85 fn syntax(&self) -> &SyntaxToken { &self.syntax }
83 } 86 }
84 } 87 };
88 Some(res)
85 }); 89 });
86 90
87 let enums = grammar.token_enums.iter().map(|en| { 91 let enums = grammar.token_enums.iter().map(|en| {
@@ -186,8 +190,12 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
186 }); 190 });
187 191
188 let methods = node.fields.iter().map(|(name, field)| { 192 let methods = node.fields.iter().map(|(name, field)| {
193 let is_kw = name.ends_with("Kw");
189 let method_name = match field { 194 let method_name = match field {
190 FieldSrc::Shorthand => format_ident!("{}", to_lower_snake_case(&name)), 195 FieldSrc::Shorthand => {
196 let name = if is_kw { &name[..name.len() - 2] } else { &name };
197 format_ident!("{}", to_lower_snake_case(name))
198 }
191 _ => format_ident!("{}", name), 199 _ => format_ident!("{}", name),
192 }; 200 };
193 let ty = match field { 201 let ty = match field {
@@ -209,9 +217,18 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
209 let is_token = token_kinds.contains(&ty.to_string()); 217 let is_token = token_kinds.contains(&ty.to_string());
210 if is_token { 218 if is_token {
211 let method_name = format_ident!("{}_token", method_name); 219 let method_name = format_ident!("{}_token", method_name);
212 quote! { 220 if is_kw {
213 pub fn #method_name(&self) -> Option<#ty> { 221 let token_kind = format_ident!("{}", to_upper_snake_case(name));
214 support::token(&self.syntax) 222 quote! {
223 pub fn #method_name(&self) -> Option<SyntaxToken> {
224 support::token2(&self.syntax, #token_kind)
225 }
226 }
227 } else {
228 quote! {
229 pub fn #method_name(&self) -> Option<#ty> {
230 support::token(&self.syntax)
231 }
215 } 232 }
216 } 233 }
217 } else { 234 } else {
@@ -332,7 +349,7 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: AstSrc<'_>) -> Result<String> {
332 349
333 let ast = quote! { 350 let ast = quote! {
334 use crate::{ 351 use crate::{
335 SyntaxNode, SyntaxKind::{self, *}, 352 SyntaxNode, SyntaxToken, SyntaxKind::{self, *},
336 ast::{self, AstNode, AstChildren, support}, 353 ast::{self, AstNode, AstChildren, support},
337 }; 354 };
338 355