@@ -338,17 +338,15 @@ def simple_sum(self, sum, type, depth):
338
338
prefix = ""
339
339
for cons in sum .types :
340
340
self .emit (
341
- textwrap .dedent (
342
- f"""
343
- #[inline]
344
- pub const fn { prefix } { rust_field_name (cons .name )} (&self) -> Option<{ rust_name } { cons .name } > {{
345
- match self {{
346
- { rust_name } ::{ cons .name } => Some({ rust_name } { cons .name } ),
347
- _ => None,
348
- }}
341
+ f"""
342
+ #[inline]
343
+ pub const fn { prefix } { rust_field_name (cons .name )} (&self) -> Option<{ rust_name } { cons .name } > {{
344
+ match self {{
345
+ { rust_name } ::{ cons .name } => Some({ rust_name } { cons .name } ),
346
+ _ => None,
349
347
}}
350
- """
351
- ) ,
348
+ }}
349
+ """ ,
352
350
depth ,
353
351
)
354
352
self .emit ("}" , depth )
@@ -415,19 +413,17 @@ def sum_subtype_struct(self, sum_type_info, t, rust_name, depth):
415
413
self .emit ("}" , depth )
416
414
field_names = [f'"{ f .name } "' for f in t .fields ]
417
415
self .emit (
418
- textwrap .dedent (
419
- f"""
420
- impl<R> Node for { payload_name } <R> {{
421
- const NAME: &'static str = "{ t .name } ";
422
- const FIELD_NAMES: &'static [&'static str] = &[{ ', ' .join (field_names )} ];
423
- }}
424
- impl<R> From<{ payload_name } <R>> for { rust_name } <R> {{
425
- fn from(payload: { payload_name } <R>) -> Self {{
426
- { rust_name } ::{ t .name } (payload)
427
- }}
416
+ f"""
417
+ impl<R> Node for { payload_name } <R> {{
418
+ const NAME: &'static str = "{ t .name } ";
419
+ const FIELD_NAMES: &'static [&'static str] = &[{ ', ' .join (field_names )} ];
420
+ }}
421
+ impl<R> From<{ payload_name } <R>> for { rust_name } <R> {{
422
+ fn from(payload: { payload_name } <R>) -> Self {{
423
+ { rust_name } ::{ t .name } (payload)
428
424
}}
429
- """
430
- ) ,
425
+ }}
426
+ """ ,
431
427
depth ,
432
428
)
433
429
@@ -982,12 +978,12 @@ def visitSum(self, sum, type):
982
978
#[inline]
983
979
fn to_pyo3_ast(&self, { "_" if simple else "" } py: Python) -> PyResult<Py<PyAny>> {{
984
980
let instance = match &self {{
985
- """ ,
981
+ """ ,
986
982
0 ,
987
983
)
988
984
for cons in sum .types :
989
985
self .emit (
990
- f""" crate::{ rust_name } ::{ cons .name } (cons) => cons.to_pyo3_ast(py)?,"" " ,
986
+ f"crate::{ rust_name } ::{ cons .name } (cons) => cons.to_pyo3_ast(py)?," ,
991
987
1 ,
992
988
)
993
989
self .emit (
@@ -1036,9 +1032,7 @@ def emit_to_pyo3_with_fields(self, cons, type, name):
1036
1032
3 ,
1037
1033
)
1038
1034
self .emit (
1039
- """
1040
- ))?;
1041
- """ ,
1035
+ "))?;" ,
1042
1036
0 ,
1043
1037
)
1044
1038
else :
@@ -1119,42 +1113,38 @@ def emit_class(self, name, rust_name, simple, base="super::AST"):
1119
1113
into = f"{ rust_name } (node)"
1120
1114
1121
1115
self .emit (
1122
- textwrap .dedent (
1123
- f"""
1124
- #[pyclass(module="{ self .module_name } ", name="_{ name } ", extends={ base } , frozen{ subclass } )]
1125
- #[derive(Clone, Debug)]
1126
- pub struct { rust_name } { body } ;
1116
+ f"""
1117
+ #[pyclass(module="{ self .module_name } ", name="_{ name } ", extends={ base } , frozen{ subclass } )]
1118
+ #[derive(Clone, Debug)]
1119
+ pub struct { rust_name } { body } ;
1127
1120
1128
- impl From<{ self .ref_def } crate::{ rust_name } { generics } > for { rust_name } {{
1129
- fn from({ "" if body else "_" } node: { self .ref_def } crate::{ rust_name } { generics } ) -> Self {{
1130
- { into }
1131
- }}
1121
+ impl From<{ self .ref_def } crate::{ rust_name } { generics } > for { rust_name } {{
1122
+ fn from({ "" if body else "_" } node: { self .ref_def } crate::{ rust_name } { generics } ) -> Self {{
1123
+ { into }
1132
1124
}}
1133
- """
1134
- ) ,
1125
+ }}
1126
+ """ ,
1135
1127
0 ,
1136
1128
)
1137
1129
if subclass :
1138
1130
self .emit (
1139
- textwrap .dedent (
1140
- f"""
1141
- #[pymethods]
1142
- impl { rust_name } {{
1143
- #[new]
1144
- fn new() -> PyClassInitializer<Self> {{
1145
- PyClassInitializer::from(AST)
1146
- .add_subclass(Self)
1147
- }}
1148
-
1131
+ f"""
1132
+ #[pymethods]
1133
+ impl { rust_name } {{
1134
+ #[new]
1135
+ fn new() -> PyClassInitializer<Self> {{
1136
+ PyClassInitializer::from(AST)
1137
+ .add_subclass(Self)
1149
1138
}}
1150
- impl ToPyObject for { rust_name } {{
1151
- fn to_object(&self, py: Python) -> PyObject {{
1152
- let initializer = Self::new();
1153
- Py::new(py, initializer).unwrap().into_py(py)
1154
- }}
1139
+
1140
+ }}
1141
+ impl ToPyObject for { rust_name } {{
1142
+ fn to_object(&self, py: Python) -> PyObject {{
1143
+ let initializer = Self::new();
1144
+ Py::new(py, initializer).unwrap().into_py(py)
1155
1145
}}
1156
- """
1157
- ) ,
1146
+ }}
1147
+ """ ,
1158
1148
0 ,
1159
1149
)
1160
1150
else :
@@ -1163,18 +1153,16 @@ def emit_class(self, name, rust_name, simple, base="super::AST"):
1163
1153
else :
1164
1154
add_subclass = ""
1165
1155
self .emit (
1166
- textwrap .dedent (
1167
- f"""
1168
- impl ToPyObject for { rust_name } {{
1169
- fn to_object(&self, py: Python) -> PyObject {{
1170
- let initializer = PyClassInitializer::from(AST)
1171
- { add_subclass }
1172
- .add_subclass(self.clone());
1173
- Py::new(py, initializer).unwrap().into_py(py)
1174
- }}
1156
+ f"""
1157
+ impl ToPyObject for { rust_name } {{
1158
+ fn to_object(&self, py: Python) -> PyObject {{
1159
+ let initializer = PyClassInitializer::from(AST)
1160
+ { add_subclass }
1161
+ .add_subclass(self.clone());
1162
+ Py::new(py, initializer).unwrap().into_py(py)
1175
1163
}}
1176
- """
1177
- ) ,
1164
+ }}
1165
+ """ ,
1178
1166
0 ,
1179
1167
)
1180
1168
@@ -1183,48 +1171,40 @@ def emit_class(self, name, rust_name, simple, base="super::AST"):
1183
1171
1184
1172
def emit_getter (self , owner , type_name ):
1185
1173
self .emit (
1186
- textwrap .dedent (
1187
- f"""
1188
- #[pymethods]
1189
- impl { type_name } {{
1190
- """
1191
- ),
1174
+ f"""
1175
+ #[pymethods]
1176
+ impl { type_name } {{
1177
+ """ ,
1192
1178
0 ,
1193
1179
)
1194
1180
1195
1181
for field in owner .fields :
1196
1182
self .emit (
1197
- textwrap .dedent (
1198
- f"""
1199
- #[getter]
1200
- #[inline]
1201
- fn get_{ field .name } (&self, py: Python) -> PyResult<PyObject> {{
1202
- self.0.{ rust_field (field .name )} .to_pyo3_wrapper(py)
1203
- }}
1204
- """
1205
- ),
1183
+ f"""
1184
+ #[getter]
1185
+ #[inline]
1186
+ fn get_{ field .name } (&self, py: Python) -> PyResult<PyObject> {{
1187
+ self.0.{ rust_field (field .name )} .to_pyo3_wrapper(py)
1188
+ }}
1189
+ """ ,
1206
1190
3 ,
1207
1191
)
1208
1192
1209
1193
self .emit (
1210
- textwrap .dedent (
1211
- """
1194
+ """
1212
1195
}
1213
- """
1214
- ),
1196
+ """ ,
1215
1197
0 ,
1216
1198
)
1217
1199
1218
1200
def emit_getattr (self , owner , type_name ):
1219
1201
self .emit (
1220
- textwrap .dedent (
1221
- f"""
1222
- #[pymethods]
1223
- impl { type_name } {{
1224
- fn __getattr__(&self, py: Python, key: &str) -> PyResult<PyObject> {{
1225
- let object: Py<PyAny> = match key {{
1226
- """
1227
- ),
1202
+ f"""
1203
+ #[pymethods]
1204
+ impl { type_name } {{
1205
+ fn __getattr__(&self, py: Python, key: &str) -> PyResult<PyObject> {{
1206
+ let object: Py<PyAny> = match key {{
1207
+ """ ,
1228
1208
0 ,
1229
1209
)
1230
1210
@@ -1235,15 +1215,13 @@ def emit_getattr(self, owner, type_name):
1235
1215
)
1236
1216
1237
1217
self .emit (
1238
- textwrap .dedent (
1239
- """
1240
- _ => todo!(),
1241
- };
1242
- Ok(object)
1243
- }
1218
+ """
1219
+ _ => todo!(),
1220
+ };
1221
+ Ok(object)
1244
1222
}
1245
- """
1246
- ) ,
1223
+ }
1224
+ """ ,
1247
1225
0 ,
1248
1226
)
1249
1227
@@ -1309,17 +1287,17 @@ def visitConstructor(self, cons, parent, simple, depth):
1309
1287
if simple :
1310
1288
self .emit (
1311
1289
f"""
1312
- #[pyclass(module="{ self .module_name } ", name="_{ cons .name } ", extends={ parent } )]
1313
- pub struct { parent } { cons .name } ;
1314
-
1315
- impl ToPyObject for { parent } { cons .name } {{
1316
- fn to_object(&self, py: Python) -> PyObject {{
1317
- let initializer = PyClassInitializer::from(AST)
1318
- .add_subclass({ parent } )
1319
- .add_subclass(Self);
1320
- Py::new(py, initializer).unwrap().into_py(py)
1321
- }}
1322
- }}
1290
+ #[pyclass(module="{ self .module_name } ", name="_{ cons .name } ", extends={ parent } )]
1291
+ pub struct { parent } { cons .name } ;
1292
+
1293
+ impl ToPyObject for { parent } { cons .name } {{
1294
+ fn to_object(&self, py: Python) -> PyObject {{
1295
+ let initializer = PyClassInitializer::from(AST)
1296
+ .add_subclass({ parent } )
1297
+ .add_subclass(Self);
1298
+ Py::new(py, initializer).unwrap().into_py(py)
1299
+ }}
1300
+ }}
1323
1301
""" ,
1324
1302
depth ,
1325
1303
)
@@ -1659,7 +1637,8 @@ def extract_location(self, typename, depth):
1659
1637
let row = { row } ;
1660
1638
let column = { column } ;
1661
1639
try_location(row, column)
1662
- }};""" ,
1640
+ }};
1641
+ """ ,
1663
1642
depth ,
1664
1643
)
1665
1644
@@ -1711,17 +1690,15 @@ def write(info: TypeInfo):
1711
1690
generics = "<R>"
1712
1691
1713
1692
f .write (
1714
- textwrap .dedent (
1715
- f"""
1716
- impl{ generics } Pyo3Node for crate::generic::{ rust_name } { generics } {{
1717
- #[inline]
1718
- fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {{
1719
- static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
1720
- &PY_TYPE
1721
- }}
1693
+ f"""
1694
+ impl{ generics } Pyo3Node for crate::generic::{ rust_name } { generics } {{
1695
+ #[inline]
1696
+ fn py_type_cache() -> &'static OnceCell<(Py<PyAny>, Py<PyAny>)> {{
1697
+ static PY_TYPE: OnceCell<(Py<PyAny>, Py<PyAny>)> = OnceCell::new();
1698
+ &PY_TYPE
1722
1699
}}
1723
- """
1724
- ) ,
1700
+ }}
1701
+ """ ,
1725
1702
)
1726
1703
1727
1704
for info in type_info .values ():
@@ -1832,14 +1809,12 @@ def write_pyo3_wrapper(mod, type_info, namespace, f):
1832
1809
1833
1810
def write_ast_mod (mod , type_info , f ):
1834
1811
f .write (
1835
- textwrap .dedent (
1836
- """
1837
- #![allow(clippy::all)]
1812
+ """
1813
+ #![allow(clippy::all)]
1838
1814
1839
- use super::*;
1840
- use crate::common::ascii;
1841
- """
1842
- )
1815
+ use super::*;
1816
+ use crate::common::ascii;
1817
+ """
1843
1818
)
1844
1819
1845
1820
c = ChainOfVisitors (
0 commit comments