aboutsummaryrefslogtreecommitdiff
path: root/xtask/src/codegen
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2020-07-31 14:54:19 +0100
committerGitHub <[email protected]>2020-07-31 14:54:19 +0100
commitad239f6197bda31f7a9b904b5ccb25c93cbc701a (patch)
tree5e4b244c62c61824dbb63246aa9943e3b2398131 /xtask/src/codegen
parent3407d6f8a430627be333c32cb4cef02c0c8d11e3 (diff)
parentd4d986c7f850e1f535bb4c22e3a7f7fba5483628 (diff)
Merge #5623
5623: Item is a Stmt r=matklad a=matklad bors r+ 🤖 Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'xtask/src/codegen')
-rw-r--r--xtask/src/codegen/gen_syntax.rs54
-rw-r--r--xtask/src/codegen/rust.ungram2
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
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
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 =
197Stmt = 197Stmt =
198 LetStmt 198 LetStmt
199| ExprStmt 199| ExprStmt
200| Item
200 201
201LetStmt = 202LetStmt =
202 Attr* 'let' Pat (':' Type)? 203 Attr* 'let' Pat (':' Type)?
@@ -316,7 +317,6 @@ Label =
316BlockExpr = 317BlockExpr =
317 Attr* Label 318 Attr* Label
318 '{' 319 '{'
319 Item*
320 statements:Stmt* 320 statements:Stmt*
321 Expr? 321 Expr?
322 '}' 322 '}'