mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-27 05:44:52 +00:00
parent
e820928f11
commit
fc301ab1b0
2 changed files with 105 additions and 130 deletions
|
@ -11,7 +11,7 @@ include = ["LICENSE", "Cargo.toml", "src/**/*.rs"]
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"ast", "core", "format", "literal", "parser", "parser-pyo3",
|
||||
"ast", "core", "format", "literal", "parser",
|
||||
"ruff_text_size", "ruff_source_location",
|
||||
]
|
||||
|
||||
|
|
|
@ -338,7 +338,6 @@ class StructVisitor(EmitVisitor):
|
|||
prefix = ""
|
||||
for cons in sum.types:
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[inline]
|
||||
pub const fn {prefix}{rust_field_name(cons.name)}(&self) -> Option<{rust_name}{cons.name}> {{
|
||||
|
@ -347,8 +346,7 @@ class StructVisitor(EmitVisitor):
|
|||
_ => None,
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
depth,
|
||||
)
|
||||
self.emit("}", depth)
|
||||
|
@ -415,7 +413,6 @@ class StructVisitor(EmitVisitor):
|
|||
self.emit("}", depth)
|
||||
field_names = [f'"{f.name}"' for f in t.fields]
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
impl<R> Node for {payload_name}<R> {{
|
||||
const NAME: &'static str = "{t.name}";
|
||||
|
@ -426,8 +423,7 @@ class StructVisitor(EmitVisitor):
|
|||
{rust_name}::{t.name}(payload)
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
depth,
|
||||
)
|
||||
|
||||
|
@ -987,7 +983,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
)
|
||||
for cons in sum.types:
|
||||
self.emit(
|
||||
f"""crate::{rust_name}::{cons.name}(cons) => cons.to_pyo3_ast(py)?,""",
|
||||
f"crate::{rust_name}::{cons.name}(cons) => cons.to_pyo3_ast(py)?,",
|
||||
1,
|
||||
)
|
||||
self.emit(
|
||||
|
@ -1036,9 +1032,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
|||
3,
|
||||
)
|
||||
self.emit(
|
||||
"""
|
||||
))?;
|
||||
""",
|
||||
"))?;",
|
||||
0,
|
||||
)
|
||||
else:
|
||||
|
@ -1119,7 +1113,6 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
into = f"{rust_name}(node)"
|
||||
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[pyclass(module="{self.module_name}", name="_{name}", extends={base}, frozen{subclass})]
|
||||
#[derive(Clone, Debug)]
|
||||
|
@ -1130,13 +1123,11 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
{into}
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
if subclass:
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[pymethods]
|
||||
impl {rust_name} {{
|
||||
|
@ -1153,8 +1144,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
Py::new(py, initializer).unwrap().into_py(py)
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
else:
|
||||
|
@ -1163,7 +1153,6 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
else:
|
||||
add_subclass = ""
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
impl ToPyObject for {rust_name} {{
|
||||
fn to_object(&self, py: Python) -> PyObject {{
|
||||
|
@ -1173,8 +1162,7 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
Py::new(py, initializer).unwrap().into_py(py)
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
|
@ -1183,48 +1171,40 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
|
||||
def emit_getter(self, owner, type_name):
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[pymethods]
|
||||
impl {type_name} {{
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
for field in owner.fields:
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[getter]
|
||||
#[inline]
|
||||
fn get_{field.name}(&self, py: Python) -> PyResult<PyObject> {{
|
||||
self.0.{rust_field(field.name)}.to_pyo3_wrapper(py)
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
3,
|
||||
)
|
||||
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
def emit_getattr(self, owner, type_name):
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
#[pymethods]
|
||||
impl {type_name} {{
|
||||
fn __getattr__(&self, py: Python, key: &str) -> PyResult<PyObject> {{
|
||||
let object: Py<PyAny> = match key {{
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
|
@ -1235,15 +1215,13 @@ class Pyo3StructVisitor(EmitVisitor):
|
|||
)
|
||||
|
||||
self.emit(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
_ => todo!(),
|
||||
};
|
||||
Ok(object)
|
||||
}
|
||||
}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
0,
|
||||
)
|
||||
|
||||
|
@ -1659,7 +1637,8 @@ class StdlibTraitImplVisitor(EmitVisitor):
|
|||
let row = {row};
|
||||
let column = {column};
|
||||
try_location(row, column)
|
||||
}};""",
|
||||
}};
|
||||
""",
|
||||
depth,
|
||||
)
|
||||
|
||||
|
@ -1711,7 +1690,6 @@ def write_pyo3_node(type_info, f):
|
|||
generics = "<R>"
|
||||
|
||||
f.write(
|
||||
textwrap.dedent(
|
||||
f"""
|
||||
impl{generics} Pyo3Node for crate::generic::{rust_name}{generics} {{
|
||||
#[inline]
|
||||
|
@ -1720,8 +1698,7 @@ def write_pyo3_node(type_info, f):
|
|||
&PY_TYPE
|
||||
}}
|
||||
}}
|
||||
"""
|
||||
),
|
||||
""",
|
||||
)
|
||||
|
||||
for info in type_info.values():
|
||||
|
@ -1832,7 +1809,6 @@ def write_pyo3_wrapper(mod, type_info, namespace, f):
|
|||
|
||||
def write_ast_mod(mod, type_info, f):
|
||||
f.write(
|
||||
textwrap.dedent(
|
||||
"""
|
||||
#![allow(clippy::all)]
|
||||
|
||||
|
@ -1840,7 +1816,6 @@ def write_ast_mod(mod, type_info, f):
|
|||
use crate::common::ascii;
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
c = ChainOfVisitors(
|
||||
StdlibClassDefVisitor(f, type_info),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue