diff options
Diffstat (limited to 'xtask/src')
-rw-r--r-- | xtask/src/codegen/gen_syntax.rs | 54 | ||||
-rw-r--r-- | xtask/src/codegen/rust.ungram | 2 |
2 files changed, 30 insertions, 26 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 | ||
694 | fn extract_enum_traits(ast: &mut AstSrc) { | 695 | fn 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 |
diff --git a/xtask/src/codegen/rust.ungram b/xtask/src/codegen/rust.ungram index 8271509cf..17de36d7a 100644 --- a/xtask/src/codegen/rust.ungram +++ b/xtask/src/codegen/rust.ungram | |||
@@ -197,6 +197,7 @@ Attr = | |||
197 | Stmt = | 197 | Stmt = |
198 | LetStmt | 198 | LetStmt |
199 | | ExprStmt | 199 | | ExprStmt |
200 | | Item | ||
200 | 201 | ||
201 | LetStmt = | 202 | LetStmt = |
202 | Attr* 'let' Pat (':' Type)? | 203 | Attr* 'let' Pat (':' Type)? |
@@ -316,7 +317,6 @@ Label = | |||
316 | BlockExpr = | 317 | BlockExpr = |
317 | Attr* Label | 318 | Attr* Label |
318 | '{' | 319 | '{' |
319 | Item* | ||
320 | statements:Stmt* | 320 | statements:Stmt* |
321 | Expr? | 321 | Expr? |
322 | '}' | 322 | '}' |