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]
|
[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",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
233
ast/asdl_rs.py
233
ast/asdl_rs.py
|
@ -338,17 +338,15 @@ 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}> {{
|
match self {{
|
||||||
match self {{
|
{rust_name}::{cons.name} => Some({rust_name}{cons.name}),
|
||||||
{rust_name}::{cons.name} => Some({rust_name}{cons.name}),
|
_ => None,
|
||||||
_ => None,
|
|
||||||
}}
|
|
||||||
}}
|
}}
|
||||||
"""
|
}}
|
||||||
),
|
""",
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
self.emit("}", depth)
|
self.emit("}", depth)
|
||||||
|
@ -415,19 +413,17 @@ 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}";
|
const FIELD_NAMES: &'static [&'static str] = &[{', '.join(field_names)}];
|
||||||
const FIELD_NAMES: &'static [&'static str] = &[{', '.join(field_names)}];
|
}}
|
||||||
|
impl<R> From<{payload_name}<R>> for {rust_name}<R> {{
|
||||||
|
fn from(payload: {payload_name}<R>) -> Self {{
|
||||||
|
{rust_name}::{t.name}(payload)
|
||||||
}}
|
}}
|
||||||
impl<R> From<{payload_name}<R>> for {rust_name}<R> {{
|
}}
|
||||||
fn from(payload: {payload_name}<R>) -> Self {{
|
""",
|
||||||
{rust_name}::{t.name}(payload)
|
|
||||||
}}
|
|
||||||
}}
|
|
||||||
"""
|
|
||||||
),
|
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -982,12 +978,12 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_pyo3_ast(&self, {"_" if simple else ""}py: Python) -> PyResult<Py<PyAny>> {{
|
fn to_pyo3_ast(&self, {"_" if simple else ""}py: Python) -> PyResult<Py<PyAny>> {{
|
||||||
let instance = match &self {{
|
let instance = match &self {{
|
||||||
""",
|
""",
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
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,42 +1113,38 @@ 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)]
|
pub struct {rust_name} {body};
|
||||||
pub struct {rust_name} {body};
|
|
||||||
|
|
||||||
impl From<{self.ref_def} crate::{rust_name}{generics}> for {rust_name} {{
|
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 {{
|
fn from({"" if body else "_"}node: {self.ref_def} crate::{rust_name}{generics}) -> Self {{
|
||||||
{into}
|
{into}
|
||||||
}}
|
|
||||||
}}
|
}}
|
||||||
"""
|
}}
|
||||||
),
|
""",
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
if subclass:
|
if subclass:
|
||||||
self.emit(
|
self.emit(
|
||||||
textwrap.dedent(
|
f"""
|
||||||
f"""
|
#[pymethods]
|
||||||
#[pymethods]
|
impl {rust_name} {{
|
||||||
impl {rust_name} {{
|
#[new]
|
||||||
#[new]
|
fn new() -> PyClassInitializer<Self> {{
|
||||||
fn new() -> PyClassInitializer<Self> {{
|
PyClassInitializer::from(AST)
|
||||||
PyClassInitializer::from(AST)
|
.add_subclass(Self)
|
||||||
.add_subclass(Self)
|
}}
|
||||||
}}
|
|
||||||
|
|
||||||
|
}}
|
||||||
|
impl ToPyObject for {rust_name} {{
|
||||||
|
fn to_object(&self, py: Python) -> PyObject {{
|
||||||
|
let initializer = Self::new();
|
||||||
|
Py::new(py, initializer).unwrap().into_py(py)
|
||||||
}}
|
}}
|
||||||
impl ToPyObject for {rust_name} {{
|
}}
|
||||||
fn to_object(&self, py: Python) -> PyObject {{
|
""",
|
||||||
let initializer = Self::new();
|
|
||||||
Py::new(py, initializer).unwrap().into_py(py)
|
|
||||||
}}
|
|
||||||
}}
|
|
||||||
"""
|
|
||||||
),
|
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
|
@ -1163,18 +1153,16 @@ 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 {{
|
let initializer = PyClassInitializer::from(AST)
|
||||||
let initializer = PyClassInitializer::from(AST)
|
{add_subclass}
|
||||||
{add_subclass}
|
.add_subclass(self.clone());
|
||||||
.add_subclass(self.clone());
|
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,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -1309,17 +1287,17 @@ class Pyo3StructVisitor(EmitVisitor):
|
||||||
if simple:
|
if simple:
|
||||||
self.emit(
|
self.emit(
|
||||||
f"""
|
f"""
|
||||||
#[pyclass(module="{self.module_name}", name="_{cons.name}", extends={parent})]
|
#[pyclass(module="{self.module_name}", name="_{cons.name}", extends={parent})]
|
||||||
pub struct {parent}{cons.name};
|
pub struct {parent}{cons.name};
|
||||||
|
|
||||||
impl ToPyObject for {parent}{cons.name} {{
|
impl ToPyObject for {parent}{cons.name} {{
|
||||||
fn to_object(&self, py: Python) -> PyObject {{
|
fn to_object(&self, py: Python) -> PyObject {{
|
||||||
let initializer = PyClassInitializer::from(AST)
|
let initializer = PyClassInitializer::from(AST)
|
||||||
.add_subclass({parent})
|
.add_subclass({parent})
|
||||||
.add_subclass(Self);
|
.add_subclass(Self);
|
||||||
Py::new(py, initializer).unwrap().into_py(py)
|
Py::new(py, initializer).unwrap().into_py(py)
|
||||||
}}
|
}}
|
||||||
}}
|
}}
|
||||||
""",
|
""",
|
||||||
depth,
|
depth,
|
||||||
)
|
)
|
||||||
|
@ -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,17 +1690,15 @@ 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]
|
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {{
|
||||||
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {{
|
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
|
||||||
static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
|
&PY_TYPE
|
||||||
&PY_TYPE
|
|
||||||
}}
|
|
||||||
}}
|
}}
|
||||||
"""
|
}}
|
||||||
),
|
""",
|
||||||
)
|
)
|
||||||
|
|
||||||
for info in type_info.values():
|
for info in type_info.values():
|
||||||
|
@ -1832,14 +1809,12 @@ 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)]
|
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
use crate::common::ascii;
|
use crate::common::ascii;
|
||||||
"""
|
"""
|
||||||
)
|
|
||||||
)
|
)
|
||||||
|
|
||||||
c = ChainOfVisitors(
|
c = ChainOfVisitors(
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue