aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen/gen_syntax.rs
diff options
context:
space:
mode:
Diffstat (limited to 'xtask/src/codegen/gen_syntax.rs')
-rw-r--r--xtask/src/codegen/gen_syntax.rs54
1 files changed, 29 insertions, 25 deletions
diff --git a/xtask/src/codegen/gen_syntax.rs b/xtask/src/codegen/gen_syntax.rs
index d6a72ccc0..d9f358513 100644
--- a/xtask/src/codegen/gen_syntax.rs
+++ b/xtask/src/codegen/gen_syntax.rs
@@ -153,25 +153,10 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
153 quote!(impl ast::#trait_name for #name {}) 153 quote!(impl ast::#trait_name for #name {})
154 }); 154 });
155 155
156 ( 156 let ast_node = if en.name == "Stmt" {
157 quote! { 157 quote! {}
158 #[pretty_doc_comment_placeholder_workaround] 158 } else {
159 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
160 pub enum #name {
161 #(#variants(#variants),)*
162 }
163
164 #(#traits)*
165 },
166 quote! { 159 quote! {
167 #(
168 impl From<#variants> for #name {
169 fn from(node: #variants) -> #name {
170 #name::#variants(node)
171 }
172 }
173 )*
174
175 impl AstNode for #name { 160 impl AstNode for #name {
176 fn can_cast(kind: SyntaxKind) -> bool { 161 fn can_cast(kind: SyntaxKind) -> bool {
177 match kind { 162 match kind {
@@ -196,6 +181,28 @@ fn generate_nodes(kinds: KindsSrc<'_>, grammar: &AstSrc) -> Result<String> {
196 } 181 }
197 } 182 }
198 } 183 }
184 }
185 };
186
187 (
188 quote! {
189 #[pretty_doc_comment_placeholder_workaround]
190 #[derive(Debug, Clone, PartialEq, Eq, Hash)]
191 pub enum #name {
192 #(#variants(#variants),)*
193 }
194
195 #(#traits)*
196 },
197 quote! {
198 #(
199 impl From<#variants> for #name {
200 fn from(node: #variants) -> #name {
201 #name::#variants(node)
202 }
203 }
204 )*
205 #ast_node
199 }, 206 },
200 ) 207 )
201 }) 208 })
@@ -497,13 +504,7 @@ fn lower(grammar: &Grammar) -> AstSrc {
497 let mut res = AstSrc::default(); 504 let mut res = AstSrc::default();
498 res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()]; 505 res.tokens = vec!["Whitespace".into(), "Comment".into(), "String".into(), "RawString".into()];
499 506
500 let nodes = grammar 507 let nodes = grammar.iter().collect::<Vec<_>>();
501 .iter()
502 .filter(|&node| match grammar[node].rule {
503 Rule::Node(it) if it == node => false,
504 _ => true,
505 })
506 .collect::<Vec<_>>();
507 508
508 for &node in &nodes { 509 for &node in &nodes {
509 let name = grammar[node].name.clone(); 510 let name = grammar[node].name.clone();
@@ -693,6 +694,9 @@ fn extract_struct_trait(node: &mut AstNodeSrc, trait_name: &str, methods: &[&str
693 694
694fn extract_enum_traits(ast: &mut AstSrc) { 695fn extract_enum_traits(ast: &mut AstSrc) {
695 for enm in &mut ast.enums { 696 for enm in &mut ast.enums {
697 if enm.name == "Stmt" {
698 continue;
699 }
696 let nodes = &ast.nodes; 700 let nodes = &ast.nodes;
697 let mut variant_traits = enm 701 let mut variant_traits = enm
698 .variants 702 .variants