location support

This commit is contained in:
Jeong YunWon 2023-05-16 22:33:45 +09:00
parent d9b257f63c
commit 2408498773
5 changed files with 383 additions and 294 deletions

View file

@ -1053,11 +1053,12 @@ class ToPyo3AstVisitor(EmitVisitor):
if type.value.attributes and self.namespace == "located":
self.emit(
"""
instance.setattr(py, "lineno", _range.start.row.get())?;
instance.setattr(py, "col_offset", _range.start.column.get())?;
let cache = ast_key_cache().get().unwrap();
instance.setattr(py, cache.lineno.as_ref(py), _range.start.row.get())?;
instance.setattr(py, cache.col_offset.as_ref(py), _range.start.column.get())?;
if let Some(end) = _range.end {
instance.setattr(py, "end_lineno", end.row.get())?;
instance.setattr(py, "end_col_offset", end.column.get())?;
instance.setattr(py, cache.end_lineno.as_ref(py), end.row.get())?;
instance.setattr(py, cache.end_col_offset.as_ref(py), end.column.get())?;
}
""",
1,
@ -1148,8 +1149,7 @@ class Pyo3StructVisitor(EmitVisitor):
}}
impl ToPyObject for {rust_name} {{
fn to_object(&self, py: Python) -> PyObject {{
let initializer = PyClassInitializer::from(AST)
.add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}}
}}

View file

@ -19,7 +19,7 @@ impl Mod {
}
impl ToPyObject for Mod {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -207,7 +207,7 @@ impl Stmt {
}
impl ToPyObject for Stmt {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -1481,7 +1481,7 @@ impl Expr {
}
impl ToPyObject for Expr {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2671,7 +2671,7 @@ impl ExprContext {
}
impl ToPyObject for ExprContext {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2731,7 +2731,7 @@ impl Boolop {
}
impl ToPyObject for Boolop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2779,7 +2779,7 @@ impl Operator {
}
impl ToPyObject for Operator {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2959,7 +2959,7 @@ impl Unaryop {
}
impl ToPyObject for Unaryop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3031,7 +3031,7 @@ impl Cmpop {
}
impl ToPyObject for Cmpop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3226,7 +3226,7 @@ impl Excepthandler {
}
impl ToPyObject for Excepthandler {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3582,7 +3582,7 @@ impl Pattern {
}
impl ToPyObject for Pattern {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3938,7 +3938,7 @@ impl TypeIgnore {
}
impl ToPyObject for TypeIgnore {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}

View file

@ -19,7 +19,7 @@ impl Mod {
}
impl ToPyObject for Mod {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -207,7 +207,7 @@ impl Stmt {
}
impl ToPyObject for Stmt {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -1481,7 +1481,7 @@ impl Expr {
}
impl ToPyObject for Expr {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2671,7 +2671,7 @@ impl ExprContext {
}
impl ToPyObject for ExprContext {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2731,7 +2731,7 @@ impl Boolop {
}
impl ToPyObject for Boolop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2779,7 +2779,7 @@ impl Operator {
}
impl ToPyObject for Operator {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -2959,7 +2959,7 @@ impl Unaryop {
}
impl ToPyObject for Unaryop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3031,7 +3031,7 @@ impl Cmpop {
}
impl ToPyObject for Cmpop {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3226,7 +3226,7 @@ impl Excepthandler {
}
impl ToPyObject for Excepthandler {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3582,7 +3582,7 @@ impl Pattern {
}
impl ToPyObject for Pattern {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}
@ -3938,7 +3938,7 @@ impl TypeIgnore {
}
impl ToPyObject for TypeIgnore {
fn to_object(&self, py: Python) -> PyObject {
let initializer = PyClassInitializer::from(AST).add_subclass(self.clone());
let initializer = Self::new();
Py::new(py, initializer).unwrap().into_py(py)
}
}

File diff suppressed because it is too large Load diff

View file

@ -1,8 +1,10 @@
use crate::{source_code::SourceRange, text_size::TextRange, ConversionFlag, Node};
use num_complex::Complex64;
use once_cell::sync::OnceCell;
use pyo3::prelude::*;
use pyo3::types::{PyBytes, PyList, PyTuple};
use pyo3::{
prelude::*,
types::{PyBytes, PyList, PyString, PyTuple},
};
pub trait Pyo3Node {
fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {
@ -123,7 +125,28 @@ fn cache_py_type<N: Pyo3Node + Node>(ast_module: &PyAny) -> PyResult<()> {
Ok(())
}
struct AstKeyCache {
lineno: Py<PyString>,
col_offset: Py<PyString>,
end_lineno: Py<PyString>,
end_col_offset: Py<PyString>,
}
fn ast_key_cache() -> &'static OnceCell<AstKeyCache> {
{
static PY_TYPE: OnceCell<AstKeyCache> = OnceCell::new();
&PY_TYPE
}
}
pub fn init(py: Python) -> PyResult<()> {
ast_key_cache().get_or_init(|| AstKeyCache {
lineno: pyo3::intern!(py, "lineno").into_py(py),
col_offset: pyo3::intern!(py, "col_offset").into_py(py),
end_lineno: pyo3::intern!(py, "end_lineno").into_py(py),
end_col_offset: pyo3::intern!(py, "end_col_offset").into_py(py),
});
init_types(py)
}