From 5995255fc99f02a2072ea7b3c1e7f0f7d53015a5 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 16 May 2023 23:45:00 +0900 Subject: [PATCH 1/2] remove dedent --- ast/asdl_rs.py | 235 ++++++++++++++++++++++--------------------------- 1 file changed, 105 insertions(+), 130 deletions(-) diff --git a/ast/asdl_rs.py b/ast/asdl_rs.py index a16c46ec..b82ea2b9 100755 --- a/ast/asdl_rs.py +++ b/ast/asdl_rs.py @@ -338,17 +338,15 @@ def simple_sum(self, sum, type, depth): 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}> {{ - match self {{ - {rust_name}::{cons.name} => Some({rust_name}{cons.name}), - _ => None, - }} + f""" + #[inline] + pub const fn {prefix}{rust_field_name(cons.name)}(&self) -> Option<{rust_name}{cons.name}> {{ + match self {{ + {rust_name}::{cons.name} => Some({rust_name}{cons.name}), + _ => None, }} - """ - ), + }} + """, depth, ) self.emit("}", depth) @@ -415,19 +413,17 @@ def sum_subtype_struct(self, sum_type_info, t, rust_name, depth): self.emit("}", depth) field_names = [f'"{f.name}"' for f in t.fields] self.emit( - textwrap.dedent( - f""" - impl Node for {payload_name} {{ - const NAME: &'static str = "{t.name}"; - const FIELD_NAMES: &'static [&'static str] = &[{', '.join(field_names)}]; - }} - impl From<{payload_name}> for {rust_name} {{ - fn from(payload: {payload_name}) -> Self {{ - {rust_name}::{t.name}(payload) - }} + f""" + impl Node for {payload_name} {{ + const NAME: &'static str = "{t.name}"; + const FIELD_NAMES: &'static [&'static str] = &[{', '.join(field_names)}]; + }} + impl From<{payload_name}> for {rust_name} {{ + fn from(payload: {payload_name}) -> Self {{ + {rust_name}::{t.name}(payload) }} - """ - ), + }} + """, depth, ) @@ -982,12 +978,12 @@ def visitSum(self, sum, type): #[inline] fn to_pyo3_ast(&self, {"_" if simple else ""}py: Python) -> PyResult> {{ 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"crate::{rust_name}::{cons.name}(cons) => cons.to_pyo3_ast(py)?,", 1, ) self.emit( @@ -1036,9 +1032,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name): 3, ) self.emit( - """ - ))?; - """, + "))?;", 0, ) else: @@ -1119,42 +1113,38 @@ def emit_class(self, name, rust_name, simple, base="super::AST"): into = f"{rust_name}(node)" self.emit( - textwrap.dedent( - f""" - #[pyclass(module="{self.module_name}", name="_{name}", extends={base}, frozen{subclass})] - #[derive(Clone, Debug)] - pub struct {rust_name} {body}; + f""" + #[pyclass(module="{self.module_name}", name="_{name}", extends={base}, frozen{subclass})] + #[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 {{ - {into} - }} + 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 {{ + {into} }} - """ - ), + }} + """, 0, ) if subclass: self.emit( - textwrap.dedent( - f""" - #[pymethods] - impl {rust_name} {{ - #[new] - fn new() -> PyClassInitializer {{ - PyClassInitializer::from(AST) - .add_subclass(Self) - }} - + f""" + #[pymethods] + impl {rust_name} {{ + #[new] + fn new() -> PyClassInitializer {{ + PyClassInitializer::from(AST) + .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, ) else: @@ -1163,18 +1153,16 @@ def emit_class(self, name, rust_name, simple, base="super::AST"): else: add_subclass = "" self.emit( - textwrap.dedent( - f""" - impl ToPyObject for {rust_name} {{ - fn to_object(&self, py: Python) -> PyObject {{ - let initializer = PyClassInitializer::from(AST) - {add_subclass} - .add_subclass(self.clone()); - Py::new(py, initializer).unwrap().into_py(py) - }} + f""" + impl ToPyObject for {rust_name} {{ + fn to_object(&self, py: Python) -> PyObject {{ + let initializer = PyClassInitializer::from(AST) + {add_subclass} + .add_subclass(self.clone()); + Py::new(py, initializer).unwrap().into_py(py) }} - """ - ), + }} + """, 0, ) @@ -1183,48 +1171,40 @@ def emit_class(self, name, rust_name, simple, base="super::AST"): def emit_getter(self, owner, type_name): self.emit( - textwrap.dedent( - f""" - #[pymethods] - impl {type_name} {{ - """ - ), + 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 {{ - self.0.{rust_field(field.name)}.to_pyo3_wrapper(py) - }} - """ - ), + f""" + #[getter] + #[inline] + fn get_{field.name}(&self, py: Python) -> PyResult {{ + 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 {{ - let object: Py = match key {{ - """ - ), + f""" + #[pymethods] + impl {type_name} {{ + fn __getattr__(&self, py: Python, key: &str) -> PyResult {{ + let object: Py = match key {{ + """, 0, ) @@ -1235,15 +1215,13 @@ def emit_getattr(self, owner, type_name): ) self.emit( - textwrap.dedent( - """ - _ => todo!(), - }; - Ok(object) - } + """ + _ => todo!(), + }; + Ok(object) } - """ - ), + } + """, 0, ) @@ -1309,17 +1287,17 @@ def visitConstructor(self, cons, parent, simple, depth): if simple: self.emit( f""" -#[pyclass(module="{self.module_name}", name="_{cons.name}", extends={parent})] -pub struct {parent}{cons.name}; - -impl ToPyObject for {parent}{cons.name} {{ - fn to_object(&self, py: Python) -> PyObject {{ - let initializer = PyClassInitializer::from(AST) - .add_subclass({parent}) - .add_subclass(Self); - Py::new(py, initializer).unwrap().into_py(py) - }} -}} + #[pyclass(module="{self.module_name}", name="_{cons.name}", extends={parent})] + pub struct {parent}{cons.name}; + + impl ToPyObject for {parent}{cons.name} {{ + fn to_object(&self, py: Python) -> PyObject {{ + let initializer = PyClassInitializer::from(AST) + .add_subclass({parent}) + .add_subclass(Self); + Py::new(py, initializer).unwrap().into_py(py) + }} + }} """, depth, ) @@ -1659,7 +1637,8 @@ def extract_location(self, typename, depth): let row = {row}; let column = {column}; try_location(row, column) - }};""", + }}; + """, depth, ) @@ -1711,17 +1690,15 @@ def write(info: TypeInfo): generics = "" f.write( - textwrap.dedent( - f""" - impl{generics} Pyo3Node for crate::generic::{rust_name}{generics} {{ - #[inline] - fn py_type_cache() -> &'static OnceCell<(Py, Py)> {{ - static PY_TYPE: OnceCell<(Py, Py)> = OnceCell::new(); - &PY_TYPE - }} + f""" + impl{generics} Pyo3Node for crate::generic::{rust_name}{generics} {{ + #[inline] + fn py_type_cache() -> &'static OnceCell<(Py, Py)> {{ + static PY_TYPE: OnceCell<(Py, Py)> = OnceCell::new(); + &PY_TYPE }} - """ - ), + }} + """, ) 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): f.write( - textwrap.dedent( - """ - #![allow(clippy::all)] + """ + #![allow(clippy::all)] - use super::*; - use crate::common::ascii; - """ - ) + use super::*; + use crate::common::ascii; + """ ) c = ChainOfVisitors( From 5735b7026b758c368810042f08ef14c7b212e880 Mon Sep 17 00:00:00 2001 From: Jeong YunWon Date: Tue, 16 May 2023 23:46:46 +0900 Subject: [PATCH 2/2] Revert parser-pyo3 crate --- Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Cargo.toml b/Cargo.toml index 8f0741cd..8e43b189 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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", ]