Skip to content

Commit 4f4e877

Browse files
committed
Fix ast/asdl_rs.py
1 parent 5553558 commit 4f4e877

File tree

5 files changed

+20
-20
lines changed

5 files changed

+20
-20
lines changed

ast/asdl_rs.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
#! /usr/bin/env python
22
"""Generate Rust code from an ASDL description."""
33

4-
import os
54
import sys
6-
import textwrap
7-
85
import json
6+
import textwrap
97

108
from argparse import ArgumentParser
119
from pathlib import Path
1210

1311
import asdl
1412

1513
TABSIZE = 4
16-
AUTOGEN_MESSAGE = "// File automatically generated by {}.\n\n"
14+
AUTOGEN_MESSAGE = "// File automatically generated by {}.\n"
1715

1816
builtin_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

551549
def 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

576574
def 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),

ast/src/ast_gen.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
// File automatically generated by ast/asdl_rs.py.
2-
32
pub use crate::constant::*;
43
pub use crate::location::Location;
54

scripts/update_asdl.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,4 @@ set -e
44
cd "$(dirname "$(dirname "$0")")"
55

66
python ast/asdl_rs.py -D ast/src/ast_gen.rs -M vm/src/stdlib/ast/gen.rs ast/Python.asdl
7-
8-
cargo fmt
7+
rustfmt ast/src/ast_gen.rs vm/src/stdlib/ast/gen.rs

vm/src/stdlib/ast.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//! This module makes use of the parser logic, and translates all ast nodes
44
//! into python ast.AST objects.
55
6+
mod gen;
7+
68
use crate::{
79
builtins::{self, PyStrRef, PyTypeRef},
810
pyclass::{PyClassImpl, StaticType},
@@ -17,11 +19,6 @@ use rustpython_compiler as compile;
1719
#[cfg(feature = "rustpython-parser")]
1820
use rustpython_parser::parser;
1921

20-
#[rustfmt::skip]
21-
#[allow(clippy::all)]
22-
mod gen;
23-
24-
2522
#[pymodule]
2623
mod _ast {
2724
use crate::{

vm/src/stdlib/ast/gen.rs

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

3+
#![allow(clippy::all)]
4+
35
use super::*;
46
use crate::common::ascii;
57

0 commit comments

Comments
 (0)