mirror of
https://github.com/RustPython/Parser.git
synced 2025-08-04 02:39:22 +00:00
clean up pyo3 generation
This commit is contained in:
parent
4de0cb1827
commit
6bf6b29b4d
4 changed files with 27 additions and 23 deletions
|
@ -1653,8 +1653,8 @@ impl ToPyAst for ast::StmtPass<TextRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
@ -1665,8 +1665,8 @@ impl ToPyAst for ast::StmtBreak<TextRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
@ -1677,8 +1677,8 @@ impl ToPyAst for ast::StmtContinue<TextRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
@ -3429,8 +3429,8 @@ impl ToPyAst for ast::StmtPass<SourceRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
let cache = ast_cache();
|
let cache = ast_cache();
|
||||||
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
||||||
|
@ -3449,8 +3449,8 @@ impl ToPyAst for ast::StmtBreak<SourceRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
let cache = ast_cache();
|
let cache = ast_cache();
|
||||||
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
||||||
|
@ -3469,8 +3469,8 @@ impl ToPyAst for ast::StmtContinue<SourceRange> {
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
|
|
||||||
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
|
||||||
let Self { range: _range } = self;
|
let Self { range: _range } = self;
|
||||||
|
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
|
||||||
|
|
||||||
let cache = ast_cache();
|
let cache = ast_cache();
|
||||||
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
|
||||||
|
|
|
@ -131,6 +131,19 @@ struct AstCache {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AstCache {
|
impl AstCache {
|
||||||
|
// fn location_vec<'py>(&'static self, py: Python<'py>, range: &SourceRange) -> &'py PyDict {
|
||||||
|
// let attributes = PyDict::new(py);
|
||||||
|
// attributes.set_item(self.lineno.as_ref(py), range.start.row.get()).unwrap();
|
||||||
|
// attributes.set_item(self.col_offset.as_ref(py), range.start.column.to_zero_indexed()).unwrap();
|
||||||
|
// if let Some(end) = range.end {
|
||||||
|
// attributes.set_item(self.end_lineno.as_ref(py), end.row.get()).unwrap();
|
||||||
|
// attributes.set_item(
|
||||||
|
// self.end_col_offset.as_ref(py),
|
||||||
|
// end.column.to_zero_indexed(),
|
||||||
|
// ).unwrap();
|
||||||
|
// }
|
||||||
|
// attributes
|
||||||
|
// }
|
||||||
#[inline]
|
#[inline]
|
||||||
fn none_ref<'py>(&'static self, py: Python<'py>) -> &'py PyAny {
|
fn none_ref<'py>(&'static self, py: Python<'py>) -> &'py PyAny {
|
||||||
Py::<PyAny>::as_ref(&self.none, py)
|
Py::<PyAny>::as_ref(&self.none, py)
|
||||||
|
|
|
@ -93,7 +93,7 @@ impl<T: ToPyWrapper> ToPyWrapper for Vec<T> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[pyclass(module = "rustpython_ast", subclass)]
|
#[pyclass(module = "rustpython_ast", name = "AST", subclass)]
|
||||||
pub struct Ast;
|
pub struct Ast;
|
||||||
|
|
||||||
#[pymethods]
|
#[pymethods]
|
||||||
|
|
|
@ -1078,11 +1078,6 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
else:
|
else:
|
||||||
assert False, self.namespace
|
assert False, self.namespace
|
||||||
|
|
||||||
@property
|
|
||||||
def location(self):
|
|
||||||
# lineno, col_offset
|
|
||||||
pass
|
|
||||||
|
|
||||||
def visitModule(self, mod):
|
def visitModule(self, mod):
|
||||||
for dfn in mod.dfns:
|
for dfn in mod.dfns:
|
||||||
self.visit(dfn)
|
self.visit(dfn)
|
||||||
|
@ -1133,12 +1128,13 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
|
|
||||||
def emit_to_pyo3_with_fields(self, cons, type, name):
|
def emit_to_pyo3_with_fields(self, cons, type, name):
|
||||||
type_info = self.type_info[type.name]
|
type_info = self.type_info[type.name]
|
||||||
|
|
||||||
self.emit(
|
self.emit(
|
||||||
f"""
|
f"""
|
||||||
impl ToPyAst for ast::{name}{self.generics} {{
|
impl ToPyAst for ast::{name}{self.generics} {{
|
||||||
#[inline]
|
#[inline]
|
||||||
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
|
||||||
let cache = Self::py_type_cache().get().unwrap();
|
let cache = Self::py_type_cache().get().unwrap();
|
||||||
""",
|
""",
|
||||||
0,
|
0,
|
||||||
)
|
)
|
||||||
|
@ -1171,12 +1167,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
3,
|
3,
|
||||||
)
|
)
|
||||||
continue
|
continue
|
||||||
if field.name in (
|
if field.name == "lineno":
|
||||||
"lineno",
|
|
||||||
"col_offset",
|
|
||||||
"end_lineno",
|
|
||||||
"end_col_offset",
|
|
||||||
):
|
|
||||||
self.emit(
|
self.emit(
|
||||||
f"{rust_field(field.name)}.to_u32().to_object(py),",
|
f"{rust_field(field.name)}.to_u32().to_object(py),",
|
||||||
3,
|
3,
|
||||||
|
@ -1192,11 +1183,11 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
self.emit(
|
self.emit(
|
||||||
"let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;",
|
"let Self { range: _range } = self;",
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
self.emit(
|
self.emit(
|
||||||
"let Self { range: _range } = self;",
|
"""let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;""",
|
||||||
1,
|
1,
|
||||||
)
|
)
|
||||||
if type.value.attributes and self.namespace == "located":
|
if type.value.attributes and self.namespace == "located":
|
||||||
|
@ -1210,7 +1201,7 @@ class ToPyo3AstVisitor(EmitVisitor):
|
||||||
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
|
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
|
||||||
}
|
}
|
||||||
""",
|
""",
|
||||||
1,
|
0,
|
||||||
)
|
)
|
||||||
self.emit(
|
self.emit(
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue