1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
use crate::utils::rust_token::{Group, Ident, Punct, Token};
use either::Either;
use std::rc::Rc;
#[derive(Debug)]
pub(crate) struct PyStmt {
pub _marker: Rc<Punct>,
pub tokens: Rc<[Token]>,
}
#[derive(Debug)]
pub(crate) struct PyExpr {
pub start_marker: Rc<Punct>,
pub end_marker: Rc<Punct>,
pub tokens: Rc<[Token]>,
}
pub(crate) type IdentWithPyExpr = Vec<Either<Rc<Ident>, PyExpr>>;
#[derive(Debug)]
pub(crate) enum RustCode {
Code(Token),
Group {
group: Rc<Group>,
code: Box<[RustCode]>,
},
/// ```
/// const FOO = @u16(2 ** 10)@;
/// ```
/// TODO: better docs
PyExpr(PyExpr),
/// ```
/// const FOO_@"A" * 10@_BAR = 42;
/// ```
/// TODO: better docs
IdentWithPyExpr(IdentWithPyExpr),
}
#[derive(Debug)]
pub(crate) struct RustCodeWithBlock {
pub code: Box<[RustCode]>,
pub group: Rc<Group>,
pub block: Box<[CodeRegion]>,
}
#[derive(Debug)]
pub(crate) struct PyStmtWithIndentBlock {
pub stmt: PyStmt,
pub group: Rc<Group>,
pub block: Box<[CodeRegion]>,
}
#[derive(Debug)]
pub(crate) enum CodeRegion {
/// Some Rust code. **Can not** contain non-inline Python code.
///
/// This is turned into one `rust()` call in the generated Python code.
///
/// A continuous region of Rust code may be broken up into a number of these
/// to make the resulting Python code more readable.
RustCode(Vec<RustCode>),
/// Some Rust code followed by a block ([Group]).
/// The block may contain Python code, both inline and non-inline ones.
///
/// This is turned into a `with rust():` block in Python.
/// Using context manager allows any Python code contained within this to work properly,
/// and emit code to the correct block ([Group]).
RustCodeWithBlock(RustCodeWithBlock),
/// A Python statement.
PyStmt(PyStmt),
/// A Python statement, followed by an "indent block".
///
/// Something like:
/// ```
/// @for i in range(10):{
/// ...
/// }
/// ```
/// Or:
/// ```
/// @if foo:{
/// ...
/// } @else if bar:{
/// ...
/// } @else:{
/// ...
/// }
/// ```
///
/// In the generated Python code, the `{}` of the indent block is stripped out,
/// but explicit indentation is applied to the code within the block.
PyStmtWithIndentBlock(PyStmtWithIndentBlock),
}
macro_rules! impl_code_region_from_inner {
($name:ident, $inner:ty) => {
#[allow(unused)]
impl From<$inner> for CodeRegion {
fn from(value: $inner) -> Self {
Self::$name(value)
}
}
};
}
impl_code_region_from_inner!(RustCode, Vec<RustCode>);
impl_code_region_from_inner!(RustCodeWithBlock, RustCodeWithBlock);
impl_code_region_from_inner!(PyStmt, PyStmt);
impl_code_region_from_inner!(PyStmtWithIndentBlock, PyStmtWithIndentBlock);
pub(crate) mod parser {
use super::{CodeRegion, PyExpr, PyStmt, PyStmtWithIndentBlock, RustCode, RustCodeWithBlock};
use crate::rust_to_py::CONCAT_MARKER;
use crate::rust_to_py::utils::TokenBufferEx;
use crate::utils::match_unwrap;
use crate::utils::rust_token::{Token, TokenBuffer};
use either::Either;
use proc_macro_error2::abort;
use std::rc::Rc;
pub(crate) struct CodeRegionParser {
regions: Vec<CodeRegion>,
}
enum ParsePyResult {
Expr(PyExpr),
Stmt(PyStmt),
StmtWithIndentBlock(PyStmtWithIndentBlock),
}
impl CodeRegionParser {
pub(crate) fn new() -> Self {
Self {
regions: Vec::new(),
}
}
fn parse_py(tokens: &mut TokenBuffer) -> Option<ParsePyResult> {
if !tokens.is_py_marker_start() {
return None;
}
let start = tokens.read_one().unwrap().punct().unwrap();
let mut py_tokens = Vec::new();
loop {
if tokens.peek(-1).unwrap().eq_punct(';') {
return Some(ParsePyResult::Stmt(PyStmt {
_marker: start,
tokens: py_tokens.into(),
}));
}
if tokens.is_py_marker_end() {
let end = tokens.read_one().unwrap().punct().unwrap();
return Some(ParsePyResult::Expr(PyExpr {
start_marker: start,
end_marker: end,
tokens: py_tokens.into(),
}));
}
if tokens.is_indent_block() {
py_tokens.push(tokens.read_one().unwrap().clone());
let group = tokens.read_one().unwrap().group().unwrap();
let group_tokens = group.tokens();
return Some(ParsePyResult::StmtWithIndentBlock(PyStmtWithIndentBlock {
stmt: PyStmt {
_marker: start,
tokens: py_tokens.into(),
},
group,
block: CodeRegionParser::new().parse(group_tokens).into(),
}));
}
if tokens.is_py_marker_escape() {
py_tokens.push(Token::Punct(tokens.read_unescaped_py_marker_escape()));
} else {
let Some(token) = tokens.read_one() else {
abort!(
tokens.peek(-1).unwrap().span().inner().unwrap().end(),
"Incomplete Python segment."
);
};
py_tokens.push(token.clone())
}
}
}
fn get_or_put_rust_code_region(&mut self) -> &mut Vec<RustCode> {
if let Some(CodeRegion::RustCode(_)) = self.regions.last() {
match_unwrap!(code in CodeRegion::RustCode(code) = self.regions.last_mut().unwrap())
} else {
match_unwrap!(code in CodeRegion::RustCode(code) = self.regions.push_mut(CodeRegion::RustCode(Vec::new())))
}
}
pub(crate) fn parse(mut self, mut tokens: TokenBuffer) -> Box<[CodeRegion]> {
while !tokens.exausted() {
if let Some(py) = Self::parse_py(&mut tokens) {
match (self.regions.last_mut(), py) {
(_, ParsePyResult::Stmt(stmt)) => {
self.regions.push(CodeRegion::PyStmt(stmt))
}
(_, ParsePyResult::StmtWithIndentBlock(stmt)) => {
self.regions.push(CodeRegion::PyStmtWithIndentBlock(stmt))
}
(Some(CodeRegion::RustCode(code)), ParsePyResult::Expr(expr)) => {
match &mut code[..] {
[
..,
RustCode::IdentWithPyExpr(code),
RustCode::Code(Token::Punct(concat)),
] if concat.eq_punct(CONCAT_MARKER) => {
let _concat_marker = code.pop();
code.push(Either::Right(expr));
}
[
..,
RustCode::Code(Token::Ident(_)),
RustCode::Code(Token::Punct(concat)),
] if concat.eq_punct(CONCAT_MARKER) => {
let _concat_marker = code.pop();
let RustCode::Code(Token::Ident(ident)) = code.pop().unwrap()
else {
unreachable!()
}; // ident
code.push(RustCode::IdentWithPyExpr(vec![
Either::Left(Rc::clone(&ident)),
Either::Right(expr),
]));
}
_ => code.push(RustCode::PyExpr(expr)),
}
}
(_, ParsePyResult::Expr(expr)) => self
.regions
.push(CodeRegion::RustCode(vec![RustCode::PyExpr(expr)])),
}
} else {
let token = if tokens.is_py_marker_escape() {
&Token::Punct(tokens.read_unescaped_py_marker_escape())
} else {
tokens.read_one().unwrap()
};
match token {
Token::Group(group) => {
let group_regions = Self::new().parse(group.tokens());
if group_regions
.iter()
.all(|region| matches!(region, CodeRegion::RustCode(_)))
{
// simple region with rust code
let group_code = RustCode::Group {
group: Rc::clone(group),
code: group_regions
.into_iter()
.map(|region| {
if let CodeRegion::RustCode(code) = region {
code
} else {
unreachable!()
}
})
.flatten()
.collect(),
};
self.get_or_put_rust_code_region().push(group_code);
} else {
// a region with python statements
let code = self
.regions
.pop_if(|region| matches!(region, CodeRegion::RustCode(_)))
.map(|region| match_unwrap!(code in CodeRegion::RustCode(code) = region));
self.regions.push(CodeRegion::RustCodeWithBlock(
RustCodeWithBlock {
code: code.unwrap_or_else(Vec::new).into_boxed_slice(),
group: Rc::clone(group),
block: group_regions,
},
));
}
}
token => {
if let Token::Ident(ident) = token
&& let Some(CodeRegion::RustCode(code)) = self.regions.last_mut()
&& let [
..,
RustCode::PyExpr(_) | RustCode::IdentWithPyExpr(_),
RustCode::Code(Token::Punct(concat)),
] = &code[..]
&& concat.eq_punct(CONCAT_MARKER)
{
// Python expr followed by CONCAT_MARKER and then by the current token which is an ident => make a IdentWithPyExpr
let _concat_marker = code.pop();
// if last is PyExpr, turn it into a IdentWithPyExpr
if let Some(expr) =
code.pop_if(|expr| matches!(expr, RustCode::PyExpr(_)))
{
code.push(RustCode::IdentWithPyExpr(vec![Either::Right(
match_unwrap!(expr in RustCode::PyExpr(expr) = expr),
)]));
}
match_unwrap!(iwp in Some(RustCode::IdentWithPyExpr(iwp)) = code.last_mut())
.push(Either::Left(Rc::clone(ident)));
} else {
// a normal token
self.get_or_put_rust_code_region()
.push(RustCode::Code(token.clone()));
// start a new region on semicolons to make the resulting Python code more readable
//TODO: only do this for braces, or only when the region is too long
if token.eq_punct(';') {
self.regions.push(CodeRegion::RustCode(Vec::new()));
}
}
}
}
}
}
self.regions = self
.regions
.into_iter()
.filter(|region| !matches!(region, CodeRegion::RustCode(code) if code.is_empty()))
.collect();
self.regions.into()
}
}
}