Skip to content

Commit 531aeb3

Browse files
authored
Cmpop::as_str (#72)
* clean up pyo3 generation * Cmpop::as_str
1 parent 4de0cb1 commit 531aeb3

File tree

6 files changed

+48
-37
lines changed

6 files changed

+48
-37
lines changed

ast-pyo3/src/gen/to_py_ast.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1653,8 +1653,8 @@ impl ToPyAst for ast::StmtPass<TextRange> {
16531653
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
16541654
let cache = Self::py_type_cache().get().unwrap();
16551655

1656-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16571656
let Self { range: _range } = self;
1657+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16581658

16591659
Ok(instance)
16601660
}
@@ -1665,8 +1665,8 @@ impl ToPyAst for ast::StmtBreak<TextRange> {
16651665
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
16661666
let cache = Self::py_type_cache().get().unwrap();
16671667

1668-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16691668
let Self { range: _range } = self;
1669+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16701670

16711671
Ok(instance)
16721672
}
@@ -1677,8 +1677,8 @@ impl ToPyAst for ast::StmtContinue<TextRange> {
16771677
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
16781678
let cache = Self::py_type_cache().get().unwrap();
16791679

1680-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16811680
let Self { range: _range } = self;
1681+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
16821682

16831683
Ok(instance)
16841684
}
@@ -3429,8 +3429,8 @@ impl ToPyAst for ast::StmtPass<SourceRange> {
34293429
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
34303430
let cache = Self::py_type_cache().get().unwrap();
34313431

3432-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34333432
let Self { range: _range } = self;
3433+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34343434

34353435
let cache = ast_cache();
34363436
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
@@ -3449,8 +3449,8 @@ impl ToPyAst for ast::StmtBreak<SourceRange> {
34493449
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
34503450
let cache = Self::py_type_cache().get().unwrap();
34513451

3452-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34533452
let Self { range: _range } = self;
3453+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34543454

34553455
let cache = ast_cache();
34563456
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;
@@ -3469,8 +3469,8 @@ impl ToPyAst for ast::StmtContinue<SourceRange> {
34693469
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {
34703470
let cache = Self::py_type_cache().get().unwrap();
34713471

3472-
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34733472
let Self { range: _range } = self;
3473+
let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;
34743474

34753475
let cache = ast_cache();
34763476
instance.setattr(cache.lineno.as_ref(py), _range.start.row.get())?;

ast-pyo3/src/py_ast.rs

+13
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,19 @@ struct AstCache {
131131
}
132132

133133
impl AstCache {
134+
// fn location_vec<'py>(&'static self, py: Python<'py>, range: &SourceRange) -> &'py PyDict {
135+
// let attributes = PyDict::new(py);
136+
// attributes.set_item(self.lineno.as_ref(py), range.start.row.get()).unwrap();
137+
// attributes.set_item(self.col_offset.as_ref(py), range.start.column.to_zero_indexed()).unwrap();
138+
// if let Some(end) = range.end {
139+
// attributes.set_item(self.end_lineno.as_ref(py), end.row.get()).unwrap();
140+
// attributes.set_item(
141+
// self.end_col_offset.as_ref(py),
142+
// end.column.to_zero_indexed(),
143+
// ).unwrap();
144+
// }
145+
// attributes
146+
// }
134147
#[inline]
135148
fn none_ref<'py>(&'static self, py: Python<'py>) -> &'py PyAny {
136149
Py::<PyAny>::as_ref(&self.none, py)

ast-pyo3/src/wrapper.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<T: ToPyWrapper> ToPyWrapper for Vec<T> {
9393
}
9494
}
9595

96-
#[pyclass(module = "rustpython_ast", subclass)]
96+
#[pyclass(module = "rustpython_ast", name = "AST", subclass)]
9797
pub struct Ast;
9898

9999
#[pymethods]

ast/asdl_rs.py

+7-16
Original file line numberDiff line numberDiff line change
@@ -1078,11 +1078,6 @@ def generics(self):
10781078
else:
10791079
assert False, self.namespace
10801080

1081-
@property
1082-
def location(self):
1083-
# lineno, col_offset
1084-
pass
1085-
10861081
def visitModule(self, mod):
10871082
for dfn in mod.dfns:
10881083
self.visit(dfn)
@@ -1133,12 +1128,13 @@ def visitConstructor(self, cons, type):
11331128

11341129
def emit_to_pyo3_with_fields(self, cons, type, name):
11351130
type_info = self.type_info[type.name]
1131+
11361132
self.emit(
11371133
f"""
11381134
impl ToPyAst for ast::{name}{self.generics} {{
11391135
#[inline]
1140-
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
1141-
let cache = Self::py_type_cache().get().unwrap();
1136+
fn to_py_ast<'py>(&self, py: Python<'py>) -> PyResult<&'py PyAny> {{
1137+
let cache = Self::py_type_cache().get().unwrap();
11421138
""",
11431139
0,
11441140
)
@@ -1171,12 +1167,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11711167
3,
11721168
)
11731169
continue
1174-
if field.name in (
1175-
"lineno",
1176-
"col_offset",
1177-
"end_lineno",
1178-
"end_col_offset",
1179-
):
1170+
if field.name == "lineno":
11801171
self.emit(
11811172
f"{rust_field(field.name)}.to_u32().to_object(py),",
11821173
3,
@@ -1192,11 +1183,11 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
11921183
)
11931184
else:
11941185
self.emit(
1195-
"let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;",
1186+
"let Self { range: _range } = self;",
11961187
1,
11971188
)
11981189
self.emit(
1199-
"let Self { range: _range } = self;",
1190+
"""let instance = Py::<PyAny>::as_ref(&cache.0, py).call0()?;""",
12001191
1,
12011192
)
12021193
if type.value.attributes and self.namespace == "located":
@@ -1210,7 +1201,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
12101201
instance.setattr(cache.end_col_offset.as_ref(py), end.column.get())?;
12111202
}
12121203
""",
1213-
1,
1204+
0,
12141205
)
12151206
self.emit(
12161207
"""

ast/src/generic.rs

+17
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,21 @@ impl<R> Default for EmptyRange<R> {
4545
}
4646
}
4747

48+
impl Cmpop {
49+
pub fn as_str(&self) -> &'static str {
50+
match self {
51+
Cmpop::Eq => "==",
52+
Cmpop::NotEq => "!=",
53+
Cmpop::Lt => "<",
54+
Cmpop::LtE => "<=",
55+
Cmpop::Gt => ">",
56+
Cmpop::GtE => ">=",
57+
Cmpop::Is => "is",
58+
Cmpop::IsNot => "is not",
59+
Cmpop::In => "in",
60+
Cmpop::NotIn => "not in",
61+
}
62+
}
63+
}
64+
4865
include!("gen/generic.rs");

ast/src/unparse.rs

+4-14
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::ConversionFlag;
2-
use crate::{Arg, Arguments, Boolop, Cmpop, Comprehension, Constant, Expr, Identifier, Operator};
2+
use crate::{Arg, Arguments, Boolop, Comprehension, Constant, Expr, Identifier, Operator};
33
use std::fmt;
44

55
mod precedence {
@@ -285,19 +285,9 @@ impl<'a> Unparser<'a> {
285285
let new_lvl = precedence::CMP + 1;
286286
self.unparse_expr(left, new_lvl)?;
287287
for (op, cmp) in ops.iter().zip(comparators) {
288-
let op = match op {
289-
Cmpop::Eq => " == ",
290-
Cmpop::NotEq => " != ",
291-
Cmpop::Lt => " < ",
292-
Cmpop::LtE => " <= ",
293-
Cmpop::Gt => " > ",
294-
Cmpop::GtE => " >= ",
295-
Cmpop::Is => " is ",
296-
Cmpop::IsNot => " is not ",
297-
Cmpop::In => " in ",
298-
Cmpop::NotIn => " not in ",
299-
};
300-
self.p(op)?;
288+
self.p(" ")?;
289+
self.p(op.as_str())?;
290+
self.p(" ")?;
301291
self.unparse_expr(cmp, new_lvl)?;
302292
}
303293
})

0 commit comments

Comments
 (0)