11#! /usr/bin/env python
22"""Generate Rust code from an ASDL description."""
33
4- import os
54import sys
6- import textwrap
7-
85import json
6+ import textwrap
97
108from argparse import ArgumentParser
119from pathlib import Path
1210
1311import asdl
1412
1513TABSIZE = 4
16- AUTOGEN_MESSAGE = "// File automatically generated by {}.\n \n "
14+ AUTOGEN_MESSAGE = "// File automatically generated by {}.\n "
1715
1816builtin_type_mapping = {
1917 'identifier' : 'Ident' ,
@@ -390,9 +388,9 @@ def gen_classdef(self, name, fields, attrs, depth, base="AstNode"):
390388 self .emit (f"impl { structname } {{" , depth )
391389 self .emit (f"#[extend_class]" , depth + 1 )
392390 self .emit ("fn extend_class_with_fields(ctx: &PyContext, class: &PyTypeRef) {" , depth + 1 )
393- fields = "," .join (f"ctx.new_str({ json .dumps (f .name )} )" for f in fields )
391+ fields = "," .join (f"ctx.new_str(ascii!( { json .dumps (f .name )} )).into( )" for f in fields )
394392 self .emit (f'class.set_str_attr("_fields", ctx.new_list(vec![{ fields } ]));' , depth + 2 )
395- attrs = "," .join (f"ctx.new_str({ json .dumps (attr .name )} )" for attr in attrs )
393+ attrs = "," .join (f"ctx.new_str(ascii!( { json .dumps (attr .name )} )).into( )" for attr in attrs )
396394 self .emit (f'class.set_str_attr("_attributes", ctx.new_list(vec![{ attrs } ]));' , depth + 2 )
397395 self .emit ("}" , depth + 1 )
398396 self .emit ("}" , depth )
@@ -401,7 +399,7 @@ class ExtendModuleVisitor(EmitVisitor):
401399
402400 def visitModule (self , mod ):
403401 depth = 0
404- self .emit ("pub fn extend_module_nodes(vm: &VirtualMachine, module: &crate::PyObj ) {" , depth )
402+ self .emit ("pub fn extend_module_nodes(vm: &VirtualMachine, module: &PyObject ) {" , depth )
405403 self .emit ("extend_module!(vm, module, {" , depth + 1 )
406404 for dfn in mod .dfns :
407405 self .visit (dfn , depth + 2 )
@@ -488,7 +486,7 @@ def make_node(self, variant, fields, depth):
488486 self .emit ("let _dict = _node.as_object().dict().unwrap();" , depth )
489487 for f in fields :
490488 self .emit (f"_dict.set_item({ json .dumps (f .name )} , { rust_field (f .name )} .ast_to_object(_vm), _vm).unwrap();" , depth )
491- self .emit ("_node.into_object ()" , depth )
489+ self .emit ("_node.into ()" , depth )
492490
493491 def make_pattern (self , fields ):
494492 return "," .join (rust_field (f .name ) for f in fields )
@@ -505,7 +503,7 @@ def gen_sum_fromobj(self, sum, sumname, enumname, depth):
505503 self .emit ("} else" , depth )
506504
507505 self .emit ("{" , depth )
508- msg = f'format!("expected some sort of { sumname } , but got {{}}",_vm.to_repr(&_object )?)'
506+ msg = f'format!("expected some sort of { sumname } , but got {{}}",_object.repr(_vm )?)'
509507 self .emit (f"return Err(_vm.new_type_error({ msg } ));" , depth + 1 )
510508 self .emit ("})" , depth )
511509
@@ -549,8 +547,8 @@ def visit(self, object):
549547
550548
551549def write_ast_def (mod , typeinfo , f ):
552- f .write ('pub use crate::location::Location;\n ' )
553550 f .write ('pub use crate::constant::*;\n ' )
551+ f .write ('pub use crate::location::Location;\n ' )
554552 f .write ('\n ' )
555553 f .write ('type Ident = String;\n ' )
556554 f .write ('\n ' )
@@ -574,8 +572,13 @@ def write_ast_def(mod, typeinfo, f):
574572
575573
576574def write_ast_mod (mod , f ):
577- f .write ('use super::*;\n ' )
578- f .write ('\n ' )
575+ f .write (textwrap .dedent ("""
576+ #![allow(clippy::all)]
577+
578+ use super::*;
579+ use crate::common::ascii;
580+
581+ """ ))
579582
580583 c = ChainOfVisitors (ClassDefVisitor (f ),
581584 TraitImplVisitor (f ),
0 commit comments