[rust] estree codegen cleanup
Joe Savona committed
Jul 8, 2023 at 22:59 UTC
3983e882b809ccf611ef1b0826a458543ff360fe
2 files changed
+11
-84
compiler/forget/crates/estree-codegen/src/codegen.rs
+10
-84
@@ -31,29 +31,18 @@ impl Grammar {
31
operators,
32
} = self;
33
34
- let nodelike: HashSet<String> =
35
- // nodes.keys().cloned().chain(enums.keys().cloned()).collect();
36
- Default::default();
37
-
34
let enum_names: HashSet<String> = enums.keys().cloned().collect();
35
36
let mut node_names: Vec<_> = nodes.keys().cloned().collect();
37
node_names.sort();
42
- let node_variants: Vec<_> = node_names
43
- .iter()
44
- .map(|name| {
45
- let name = format_ident!("{}", name);
46
- quote!(#name(Box<#name>))
47
- })
48
- .collect();
38
39
let objects: Vec<_> = objects
40
.iter()
52
- .map(|(name, object)| object.codegen(name, &nodelike))
41
+ .map(|(name, object)| object.codegen(name))
42
.collect();
43
let nodes: Vec<_> = nodes
44
.iter()
56
- .map(|(name, node)| node.codegen(name, &nodelike))
45
+ .map(|(name, node)| node.codegen(name))
46
.collect();
47
let enums: Vec<_> = enums
48
.iter()
@@ -76,12 +65,6 @@ impl Grammar {
65
#(#enums)*
66
67
#(#operators)*
79
-
80
- // #[derive(Serialize, Deserialize, Clone, Debug)]
81
- // #[serde(tag = "type")]
82
- // pub enum Node {
83
- // #(#node_variants),*
84
- // }
68
}
69
}
70
}
@@ -93,12 +76,12 @@ pub struct Object {
76
}
77
78
impl Object {
96
- pub fn codegen(&self, name: &str, nodes: &HashSet<String>) -> TokenStream {
79
+ pub fn codegen(&self, name: &str) -> TokenStream {
80
let name = format_ident!("{}", name);
81
let fields: Vec<_> = self
82
.fields
83
.iter()
101
- .map(|(name, field)| field.codegen(name, nodes))
84
+ .map(|(name, field)| field.codegen(name))
85
.collect();
86
87
quote! {
@@ -117,12 +100,12 @@ pub struct Node {
100
}
101
102
impl Node {
120
- pub fn codegen(&self, name: &str, nodes: &HashSet<String>) -> TokenStream {
103
+ pub fn codegen(&self, name: &str) -> TokenStream {
104
let name = format_ident!("{}", name);
105
let fields: Vec<_> = self
106
.fields
107
.iter()
125
- .map(|(name, field)| field.codegen_node(name, nodes))
108
+ .map(|(name, field)| field.codegen_node(name))
109
.collect();
110
111
quote! {
@@ -136,15 +119,6 @@ impl Node {
119
#[serde(default)]
120
pub range: Option<SourceRange>,
121
}
139
-
140
- // impl #name {
141
- // pub fn from_node(node: Node) -> Option<Box<Self>> {
142
- // match node {
143
- // Node::#name(node) => Some(node),
144
- // _ => None
145
- // }
146
- // }
147
- // }
122
}
123
}
124
}
@@ -174,16 +148,9 @@ pub struct Field {
148
}
149
150
impl Field {
177
- pub fn codegen(&self, name: &str, nodes: &HashSet<String>) -> TokenStream {
151
+ pub fn codegen(&self, name: &str) -> TokenStream {
152
let name = format_ident!("{}", name);
179
- let type_name = format_ident!(
180
- "{}",
181
- if nodes.contains(&self.type_) {
182
- "Node"
183
- } else {
184
- &self.type_
185
- }
186
- );
153
+ let type_name = format_ident!("{}", &self.type_);
154
let mut type_ = quote!(#type_name);
155
if self.plural {
156
if self.nullable_item {
@@ -221,16 +188,9 @@ impl Field {
188
field
189
}
190
224
- pub fn codegen_node(&self, name: &str, nodes: &HashSet<String>) -> TokenStream {
191
+ pub fn codegen_node(&self, name: &str) -> TokenStream {
192
let name = format_ident!("{}", name);
226
- let type_name = format_ident!(
227
- "{}",
228
- if nodes.contains(&self.type_) {
229
- "Node"
230
- } else {
231
- &self.type_
232
- }
233
- );
193
+ let type_name = format_ident!("{}", &self.type_);
194
let mut type_ = quote!(#type_name);
195
if self.plural {
196
if self.nullable_item {
@@ -265,13 +225,6 @@ impl Field {
225
#field
226
}
227
}
268
- if nodes.contains(&self.type_) {
269
- let comment = format!(" {}", &self.type_);
270
- field = quote! {
271
- #[doc = #comment]
272
- #field
273
- }
274
- }
228
field
229
}
230
}
@@ -300,26 +253,6 @@ impl Enum {
253
})
254
.collect();
255
303
- let from_node_matches: Vec<_> = sorted_variants
304
- .iter()
305
- .map(|name| {
306
- let variant = format_ident!("{}", name);
307
- if enums.contains(*name) {
308
- quote! {
309
- if let Some(node) = #variant::from_node(node) {
310
- return Some(Self::#variant(node));
311
- }
312
- }
313
- } else {
314
- quote! {
315
- if let Some(node) = #variant::from_node(node) {
316
- return Some(Self::#variant(node));
317
- }
318
- }
319
- }
320
- })
321
- .collect();
322
-
256
let enum_ = quote! {
257
pub enum #name {
258
#(#variants),*
@@ -341,13 +274,6 @@ impl Enum {
274
quote! {
275
#[derive(Serialize, Deserialize, Clone, Debug)]
276
#enum_
344
-
345
- // impl #name {
346
- // pub fn from_node(node: Node) -> Option<Self> {
347
- // #(#from_node_matches)*
348
- // None
349
- // }
350
- // }
277
}
278
}
279
}
compiler/forget/crates/estree/src/lib.rs
+1
@@ -18,6 +18,7 @@ mod tests {
18
#[test]
19
fn fixtures() {
20
glob!("fixtures/**.json", |path| {
21
+ println!("{:?}", path);
22
let input = std::fs::read_to_string(path).unwrap();
23
let ast: Program = serde_json::from_str(&input).unwrap();
24
let serialized = serde_json::to_string_pretty(&ast).unwrap();