@@ -289,7 +289,40 @@ class StructVisitor(EmitVisitor):
289
289
def __init__ (self , * args , ** kw ):
290
290
super ().__init__ (* args , ** kw )
291
291
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
+
292
301
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
+
293
326
for dfn in mod .dfns :
294
327
self .visit (dfn )
295
328
@@ -313,15 +346,6 @@ def visitSum(self, sum, type, depth):
313
346
depth ,
314
347
)
315
348
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
-
325
349
def simple_sum (self , sum , type , depth ):
326
350
rust_name = rust_type_name (type .name )
327
351
self .emit_attrs (depth )
0 commit comments