mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 05:44:52 +00:00
Separate rustpython_ast_pyo3
This commit is contained in:
parent
b81273e9bc
commit
7d384d88d0
13 changed files with 3209 additions and 3208 deletions
|
@ -1102,16 +1102,16 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
|
||||
self.emit(
|
||||
f"""
|
||||
impl ToPyo3Ast for crate::generic::{rust_name}{self.generics} {{
|
||||
impl ToPyAst for ast::{rust_name}{self.generics} {{
|
||||
#[inline]
|
||||
fn to_pyo3_ast<'py>(&self, {"_" if simple else ""}py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
fn to_py_ast<'py>(&self, {"_" if simple else ""}py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
let instance = match &self {{
|
||||
""",
|
||||
0,
|
||||
)
|
||||
for cons in sum.types:
|
||||
self.emit(
|
||||
f"crate::{rust_name}::{cons.name}(cons) => cons.to_pyo3_ast(py)?,",
|
||||
f"ast::{rust_name}::{cons.name}(cons) => cons.to_py_ast(py)?,",
|
||||
1,
|
||||
)
|
||||
self.emit(
|
||||
|
@ -1135,9 +1135,9 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
type_info = self.type_info[type.name]
|
||||
self.emit(
|
||||
f"""
|
||||
impl ToPyo3Ast for crate::{name}{self.generics} {{
|
||||
impl ToPyAst for ast::{name}{self.generics} {{
|
||||
#[inline]
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
let cache = Self::py_type_cache().get().unwrap();
|
||||
""",
|
||||
0,
|
||||
|
@ -1159,7 +1159,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
for field in cons.fields:
|
||||
if field.type == "constant":
|
||||
self.emit(
|
||||
f"{rust_field(field.name)}.to_object(py),",
|
||||
f"constant_to_object({rust_field(field.name)}, py),",
|
||||
3,
|
||||
)
|
||||
continue
|
||||
|
@ -1183,7 +1183,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
)
|
||||
continue
|
||||
self.emit(
|
||||
f"{rust_field(field.name)}.to_pyo3_ast(py)?,",
|
||||
f"{rust_field(field.name)}.to_py_ast(py)?,",
|
||||
3,
|
||||
)
|
||||
self.emit(
|
||||
|
@ -1252,7 +1252,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
def ref(self):
|
||||
return "&" if self.borrow else ""
|
||||
|
||||
def emit_class(self, name, rust_name, simple, base="super::AST"):
|
||||
def emit_class(self, name, rust_name, simple, base="super::Ast"):
|
||||
info = self.type_info[name]
|
||||
if simple:
|
||||
generics = ""
|
||||
|
@ -1264,7 +1264,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
into = f"{rust_name}"
|
||||
else:
|
||||
subclass = ""
|
||||
body = f"(pub {self.ref_def} crate::{rust_name}{generics})"
|
||||
body = f"(pub {self.ref_def} ast::{rust_name}{generics})"
|
||||
into = f"{rust_name}(node)"
|
||||
|
||||
self.emit(
|
||||
|
@ -1273,8 +1273,8 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
#[derive(Clone, Debug)]
|
||||
pub struct {rust_name} {body};
|
||||
|
||||
impl From<{self.ref_def} crate::{rust_name}{generics}> for {rust_name} {{
|
||||
fn from({"" if body else "_"}node: {self.ref_def} crate::{rust_name}{generics}) -> Self {{
|
||||
impl From<{self.ref_def} ast::{rust_name}{generics}> for {rust_name} {{
|
||||
fn from({"" if body else "_"}node: {self.ref_def} ast::{rust_name}{generics}) -> Self {{
|
||||
{into}
|
||||
}}
|
||||
}}
|
||||
|
@ -1288,7 +1288,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
impl {rust_name} {{
|
||||
#[new]
|
||||
fn new() -> PyClassInitializer<Self> {{
|
||||
PyClassInitializer::from(AST)
|
||||
PyClassInitializer::from(Ast)
|
||||
.add_subclass(Self)
|
||||
}}
|
||||
|
||||
|
@ -1303,7 +1303,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
0,
|
||||
)
|
||||
else:
|
||||
if base != "super::AST":
|
||||
if base != "super::Ast":
|
||||
add_subclass = f".add_subclass({base})"
|
||||
else:
|
||||
add_subclass = ""
|
||||
|
@ -1311,7 +1311,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
f"""
|
||||
impl ToPyObject for {rust_name} {{
|
||||
fn to_object(&self, py: Python) -> PyObject {{
|
||||
let initializer = PyClassInitializer::from(AST)
|
||||
let initializer = PyClassInitializer::from(Ast)
|
||||
{add_subclass}
|
||||
.add_subclass(self.clone());
|
||||
Py::new(py, initializer).unwrap().into_py(py)
|
||||
|
@ -1339,7 +1339,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
#[getter]
|
||||
#[inline]
|
||||
fn get_{field.name}(&self, py: Python) -> PyResult<PyObject> {{
|
||||
self.0.{rust_field(field.name)}.to_pyo3_wrapper(py)
|
||||
self.0.{rust_field(field.name)}.to_py_wrapper(py)
|
||||
}}
|
||||
""",
|
||||
3,
|
||||
|
@ -1365,7 +1365,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
|
||||
for field in owner.fields:
|
||||
self.emit(
|
||||
f'"{field.name}" => self.0.{rust_field(field.name)}.to_pyo3_wrapper(py)?,',
|
||||
f'"{field.name}" => self.0.{rust_field(field.name)}.to_py_wrapper(py)?,',
|
||||
3,
|
||||
)
|
||||
|
||||
|
@ -1383,9 +1383,9 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
def emit_wrapper(self, rust_name):
|
||||
self.emit(
|
||||
f"""
|
||||
impl ToPyo3Wrapper for crate::{rust_name}{self.generics} {{
|
||||
impl ToPyWrapper for ast::{rust_name}{self.generics} {{
|
||||
#[inline]
|
||||
fn to_pyo3_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
Ok({rust_name}(self).to_object(py))
|
||||
}}
|
||||
}}
|
||||
|
@ -1409,16 +1409,16 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
if not simple:
|
||||
self.emit(
|
||||
f"""
|
||||
impl ToPyo3Wrapper for crate::{rust_name}{self.generics} {{
|
||||
impl ToPyWrapper for ast::{rust_name}{self.generics} {{
|
||||
#[inline]
|
||||
fn to_pyo3_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
fn to_py_wrapper(&'static self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
match &self {{
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
for cons in sum.types:
|
||||
self.emit(f"Self::{cons.name}(cons) => cons.to_pyo3_wrapper(py),", 3)
|
||||
self.emit(f"Self::{cons.name}(cons) => cons.to_py_wrapper(py),", 3)
|
||||
|
||||
self.emit(
|
||||
"""
|
||||
|
@ -1447,7 +1447,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
|
||||
impl ToPyObject for {parent}{cons.name} {{
|
||||
fn to_object(&self, py: Python) -> PyObject {{
|
||||
let initializer = PyClassInitializer::from(AST)
|
||||
let initializer = PyClassInitializer::from(Ast)
|
||||
.add_subclass({parent})
|
||||
.add_subclass(Self);
|
||||
Py::new(py, initializer).unwrap().into_py(py)
|
||||
|
@ -1496,9 +1496,7 @@ class Pyo3PymoduleVisitor(EmitVisitor):
|
|||
self.emit_fields(cons.name, rust_name, simple)
|
||||
|
||||
def emit_fields(self, name, rust_name, simple):
|
||||
self.emit(
|
||||
f"super::init_type::<{rust_name}, crate::generic::{rust_name}>(py, m)?;", 1
|
||||
)
|
||||
self.emit(f"super::init_type::<{rust_name}, ast::{rust_name}>(py, m)?;", 1)
|
||||
|
||||
|
||||
class StdlibClassDefVisitor(EmitVisitor):
|
||||
|
@ -1846,7 +1844,7 @@ def write_pyo3_node(type_info, f):
|
|||
|
||||
f.write(
|
||||
f"""
|
||||
impl{generics} Pyo3Node for crate::generic::{rust_name}{generics} {{
|
||||
impl{generics} PyNode for ast::{rust_name}{generics} {{
|
||||
#[inline]
|
||||
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {{
|
||||
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
|
||||
|
@ -1876,7 +1874,7 @@ def write_to_pyo3(mod, type_info, f):
|
|||
|
||||
for info in type_info.values():
|
||||
rust_name = info.rust_sum_name
|
||||
f.write(f"cache_py_type::<crate::generic::{rust_name}>(ast_module)?;\n")
|
||||
f.write(f"cache_py_type::<ast::{rust_name}>(ast_module)?;\n")
|
||||
f.write("Ok(())\n}")
|
||||
|
||||
|
||||
|
@ -1890,15 +1888,15 @@ def write_to_pyo3_simple(type_info, f):
|
|||
rust_name = type_info.rust_sum_name
|
||||
f.write(
|
||||
f"""
|
||||
impl ToPyo3Ast for crate::generic::{rust_name} {{
|
||||
impl ToPyAst for ast::{rust_name} {{
|
||||
#[inline]
|
||||
fn to_pyo3_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||
let cell = match &self {{
|
||||
""",
|
||||
)
|
||||
for cons in type_info.type.value.types:
|
||||
f.write(
|
||||
f"""crate::{rust_name}::{cons.name} => crate::{rust_name}{cons.name}::py_type_cache(),""",
|
||||
f"""ast::{rust_name}::{cons.name} => ast::{rust_name}{cons.name}::py_type_cache(),""",
|
||||
)
|
||||
f.write(
|
||||
"""
|
||||
|
@ -1921,9 +1919,9 @@ def write_pyo3_wrapper(mod, type_info, namespace, f):
|
|||
rust_name = type_info.rust_sum_name
|
||||
f.write(
|
||||
f"""
|
||||
impl ToPyo3Wrapper for crate::generic::{rust_name} {{
|
||||
impl ToPyWrapper for ast::{rust_name} {{
|
||||
#[inline]
|
||||
fn to_pyo3_wrapper(&self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
fn to_py_wrapper(&self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
match &self {{
|
||||
""",
|
||||
)
|
||||
|
@ -1942,9 +1940,9 @@ def write_pyo3_wrapper(mod, type_info, namespace, f):
|
|||
for cons in type_info.type.value.types:
|
||||
f.write(
|
||||
f"""
|
||||
impl ToPyo3Wrapper for crate::generic::{rust_name}{cons.name} {{
|
||||
impl ToPyWrapper for ast::{rust_name}{cons.name} {{
|
||||
#[inline]
|
||||
fn to_pyo3_wrapper(&self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
fn to_py_wrapper(&self, py: Python) -> PyResult<Py<PyAny>> {{
|
||||
Ok({rust_name}{cons.name}.to_object(py))
|
||||
}}
|
||||
}}
|
||||
|
@ -1983,6 +1981,7 @@ def write_ast_mod(mod, type_info, f):
|
|||
def main(
|
||||
input_filename,
|
||||
ast_dir,
|
||||
ast_pyo3_dir,
|
||||
module_filename,
|
||||
dump_module=False,
|
||||
):
|
||||
|
@ -2005,14 +2004,20 @@ def main(
|
|||
("ranged", p(write_ranged_def, mod, type_info)),
|
||||
("located", p(write_located_def, mod, type_info)),
|
||||
("visitor", p(write_visitor_def, mod, type_info)),
|
||||
("to_pyo3", p(write_to_pyo3, mod, type_info)),
|
||||
("pyo3_wrapper_located", p(write_pyo3_wrapper, mod, type_info, "located")),
|
||||
("pyo3_wrapper_ranged", p(write_pyo3_wrapper, mod, type_info, "ranged")),
|
||||
]:
|
||||
with (ast_dir / f"{filename}.rs").open("w") as f:
|
||||
f.write(auto_gen_msg)
|
||||
write(f)
|
||||
|
||||
for filename, write in [
|
||||
("to_py_ast", p(write_to_pyo3, mod, type_info)),
|
||||
("wrapper_located", p(write_pyo3_wrapper, mod, type_info, "located")),
|
||||
("wrapper_ranged", p(write_pyo3_wrapper, mod, type_info, "ranged")),
|
||||
]:
|
||||
with (ast_pyo3_dir / f"{filename}.rs").open("w") as f:
|
||||
f.write(auto_gen_msg)
|
||||
write(f)
|
||||
|
||||
with module_filename.open("w") as module_file:
|
||||
module_file.write(auto_gen_msg)
|
||||
write_ast_mod(mod, type_info, module_file)
|
||||
|
@ -2024,6 +2029,7 @@ if __name__ == "__main__":
|
|||
parser = ArgumentParser()
|
||||
parser.add_argument("input_file", type=Path)
|
||||
parser.add_argument("-A", "--ast-dir", type=Path, required=True)
|
||||
parser.add_argument("-O", "--ast-pyo3-dir", type=Path, required=True)
|
||||
parser.add_argument("-M", "--module-file", type=Path, required=True)
|
||||
parser.add_argument("-d", "--dump-module", action="store_true")
|
||||
|
||||
|
@ -2031,6 +2037,7 @@ if __name__ == "__main__":
|
|||
main(
|
||||
args.input_file,
|
||||
args.ast_dir,
|
||||
args.ast_pyo3_dir,
|
||||
args.module_file,
|
||||
args.dump_module,
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue