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