aboutsummaryrefslogtreecommitdiff
path: root/xtask
diff options
context:
space:
mode:
Diffstat (limited to 'xtask')
-rw-r--r--xtask/src/ast_src.rs12
-rw-r--r--xtask/src/codegen/gen_syntax.rs33
2 files changed, 31 insertions, 14 deletions
diff --git a/xtask/src/ast_src.rs b/xtask/src/ast_src.rs
index 74a87e900..bb97b13fe 100644
--- a/xtask/src/ast_src.rs
+++ b/xtask/src/ast_src.rs
@@ -298,7 +298,7 @@ macro_rules! ast_enums {
298 298
299pub(crate) const AST_SRC: AstSrc = AstSrc { 299pub(crate) const AST_SRC: AstSrc = AstSrc {
300 nodes: &ast_nodes! { 300 nodes: &ast_nodes! {
301 struct SourceFile: ModuleItemOwner, FnDefOwner, AttrsOwner { 301 struct SourceFile: ModuleItemOwner, AttrsOwner {
302 modules: [Module], 302 modules: [Module],
303 } 303 }
304 304
@@ -364,7 +364,7 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
364 Semi 364 Semi
365 } 365 }
366 366
367 struct ItemList: FnDefOwner, ModuleItemOwner { 367 struct ItemList: ModuleItemOwner {
368 LCurly, 368 LCurly,
369 impl_items: [ImplItem], 369 impl_items: [ImplItem],
370 RCurly 370 RCurly
@@ -604,14 +604,14 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
604 struct LifetimeArg { Lifetime } 604 struct LifetimeArg { Lifetime }
605 struct ConstArg { Literal, Eq, BlockExpr } 605 struct ConstArg { Literal, Eq, BlockExpr }
606 606
607 struct MacroItems: ModuleItemOwner, FnDefOwner { } 607 struct MacroItems: ModuleItemOwner{ }
608 608
609 struct MacroStmts { 609 struct MacroStmts {
610 statements: [Stmt], 610 statements: [Stmt],
611 Expr, 611 Expr,
612 } 612 }
613 613
614 struct ExternItemList: FnDefOwner, ModuleItemOwner { 614 struct ExternItemList: ModuleItemOwner {
615 LCurly, 615 LCurly,
616 extern_items: [ExternItem], 616 extern_items: [ExternItem],
617 RCurly 617 RCurly
@@ -814,8 +814,8 @@ pub(crate) const AST_SRC: AstSrc = AstSrc {
814 FloatNumber, 814 FloatNumber,
815 String, 815 String,
816 RawString, 816 RawString,
817 TrueKw, 817 // TrueKw,
818 FalseKw, 818 // FalseKw,
819 ByteString, 819 ByteString,
820 RawByteString, 820 RawByteString,
821 Char, 821 Char,
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