Skip to content

Commit 3654cf0

Browse files
authored
Add Ast as top level enum (#58)
1 parent 6c5c311 commit 3654cf0

File tree

2 files changed

+61
-9
lines changed

2 files changed

+61
-9
lines changed

ast/asdl_rs.py

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,40 @@ class StructVisitor(EmitVisitor):
289289
def __init__(self, *args, **kw):
290290
super().__init__(*args, **kw)
291291

292+
def emit_attrs(self, depth):
293+
self.emit("#[derive(Clone, Debug, PartialEq)]", depth)
294+
295+
def emit_range(self, has_attributes, depth):
296+
if has_attributes:
297+
self.emit("pub range: R,", depth + 1)
298+
else:
299+
self.emit("pub range: OptionalRange<R>,", depth + 1)
300+
292301
def visitModule(self, mod):
302+
self.emit_attrs(0)
303+
self.emit("""
304+
#[derive(is_macro::Is)]
305+
pub enum Ast<R=TextRange> {
306+
""", 0)
307+
for dfn in mod.dfns:
308+
rust_name = rust_type_name(dfn.name)
309+
generics = "" if self.type_info[dfn.name].is_simple else "<R>"
310+
if dfn.name == "mod":
311+
# This is exceptional rule to other enums.
312+
# Unlike other enums, this is justified because `Mod` is only used as
313+
# the top node of parsing result and never a child node of other nodes.
314+
# Because it will be very rarely used in very particular applications,
315+
# "ast_" prefix to everywhere seems less useful.
316+
self.emit('#[is(name = "module")]', 1)
317+
self.emit(f"{rust_name}({rust_name}{generics}),", 1)
318+
self.emit("""
319+
}
320+
impl<R> Node for Ast<R> {
321+
const NAME: &'static str = "AST";
322+
const FIELD_NAMES: &'static [&'static str] = &[];
323+
}
324+
""", 0)
325+
293326
for dfn in mod.dfns:
294327
self.visit(dfn)
295328

@@ -313,15 +346,6 @@ def visitSum(self, sum, type, depth):
313346
depth,
314347
)
315348

316-
def emit_attrs(self, depth):
317-
self.emit("#[derive(Clone, Debug, PartialEq)]", depth)
318-
319-
def emit_range(self, has_attributes, depth):
320-
if has_attributes:
321-
self.emit("pub range: R,", depth + 1)
322-
else:
323-
self.emit("pub range: OptionalRange<R>,", depth + 1)
324-
325349
def simple_sum(self, sum, type, depth):
326350
rust_name = rust_type_name(type.name)
327351
self.emit_attrs(depth)

ast/src/gen/generic.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,34 @@
11
// File automatically generated by ast/asdl_rs.py.
22

33
use crate::text_size::TextRange;
4+
#[derive(Clone, Debug, PartialEq)]
5+
#[derive(is_macro::Is)]
6+
pub enum Ast<R = TextRange> {
7+
#[is(name = "module")]
8+
Mod(Mod<R>),
9+
Stmt(Stmt<R>),
10+
Expr(Expr<R>),
11+
ExprContext(ExprContext),
12+
Boolop(Boolop),
13+
Operator(Operator),
14+
Unaryop(Unaryop),
15+
Cmpop(Cmpop),
16+
Comprehension(Comprehension<R>),
17+
Excepthandler(Excepthandler<R>),
18+
Arguments(Arguments<R>),
19+
Arg(Arg<R>),
20+
Keyword(Keyword<R>),
21+
Alias(Alias<R>),
22+
Withitem(Withitem<R>),
23+
MatchCase(MatchCase<R>),
24+
Pattern(Pattern<R>),
25+
TypeIgnore(TypeIgnore<R>),
26+
}
27+
impl<R> Node for Ast<R> {
28+
const NAME: &'static str = "AST";
29+
const FIELD_NAMES: &'static [&'static str] = &[];
30+
}
31+
432
#[derive(Clone, Debug, PartialEq)]
533
pub struct ModModule<R = TextRange> {
634
pub range: OptionalRange<R>,

0 commit comments

Comments
 (0)