-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathast.rs
306 lines (286 loc) · 7.45 KB
/
ast.rs
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
use ast2str::{utils::AstToStrExt, AstToStr};
use pretty_assertions::assert_eq;
pub type Ptr<'a, T> = Box<Expr<'a, T>>;
#[derive(Debug)]
pub enum Op {
Add,
Mul,
}
pub trait Numeric:
std::fmt::Debug + Clone + Copy + PartialEq + std::ops::Add + std::ops::Mul
{
}
impl Numeric for f64 {}
#[derive(Debug, AstToStr)]
pub enum LitValue<'a, T: Numeric = f64> {
Num(#[debug] T),
Bool(#[display] bool),
Str(#[quoted] &'a str),
Null,
}
#[derive(AstToStr)]
#[allow(unused)]
pub struct Literal<'a, T: Numeric> {
#[debug]
value: LitValue<'a, T>,
#[skip]
lexeme: &'a str,
}
impl<'a, T: Numeric> Literal<'a, T> {
pub fn new(value: LitValue<'a, T>) -> Self {
Self {
value,
lexeme: "default lexeme",
}
}
}
pub struct Letters<'a> {
letters: &'a str,
}
impl<'a> IntoIterator for &'_ Letters<'a> {
type Item = &'a u8;
type IntoIter = std::slice::Iter<'a, u8>;
fn into_iter(self) -> Self::IntoIter {
self.letters.as_bytes().iter()
}
}
#[derive(AstToStr)]
pub struct Token<'a> {
#[delegate = "lexeme"]
#[rename = "lexeme"]
source: &'a str,
#[skip]
span: std::ops::Range<usize>,
}
impl<'a> Token<'a> {
pub fn lexeme(&self) -> &'a str {
&self.source[self.span.clone()]
}
}
#[derive(AstToStr)]
pub enum ExprKind<'a, T: Numeric> {
Literal(#[forward] Literal<'a, T>),
SpecialValue(#[forward] LitValue<'a, T>),
Binary(
#[rename = "left"] Ptr<'a, T>,
#[debug]
#[rename = "operator"]
Op,
#[rename = "right"] Ptr<'a, T>,
),
Maybe {
#[default = "Nothing"]
value: Option<Ptr<'a, T>>,
},
SomeIterable(#[list] Letters<'a>),
Mappable(#[list(|byte| byte.wrapping_mul(*byte))] Letters<'a>),
Summable {
#[callback(|x: &Vec<_>| x.iter().sum::<i32>())]
#[rename = "sum"]
values: Vec<i32>,
},
List(#[rename = "elements"] Vec<Expr<'a, T>>),
Variable(#[rename = "name"] Token<'a>),
Conditional {
#[skip]
skip_me_always: (),
#[skip_if = "Option::is_none"]
condition: Option<Ptr<'a, T>>,
#[rename = "values"]
#[skip_if = "Vec::is_empty"]
my_values: Vec<Expr<'a, T>>,
},
}
#[derive(AstToStr)]
pub struct Expr<'a, T>
where
T: Numeric,
{
#[forward]
pub kind: ExprKind<'a, T>,
pub some_other_field: (),
}
impl<'a, T: Numeric> Expr<'a, T> {
pub fn new(kind: ExprKind<'a, T>) -> Self {
Self {
kind,
some_other_field: (),
}
}
}
fn get_ast() -> Expr<'static, f64> {
macro_rules! expr {
(unboxed $name:ident { $($arg:ident : $value:expr),* }) => {
Expr::new(ExprKind::$name { $($arg : $value),* })
};
(unboxed $name:ident $($arg:expr),*) => {
Expr::new(ExprKind::$name($($arg),*))
};
($name:ident $($arg:expr),*) => {
Ptr::new(expr!(unboxed $name $($arg),*))
};
}
macro_rules! lit {
($name:ident $value:expr) => {
Literal::new(LitValue::$name($value))
};
($name:ident) => {
Literal::new(LitValue::$name)
};
}
expr!(unboxed List vec![
expr!(unboxed Binary
expr!(Literal lit!(Num 1.0)),
Op::Add,
expr!(Binary
expr!(Literal lit!(Bool true)),
Op::Mul,
expr!(Literal lit!(Str "a string"))
)
),
expr!(unboxed Maybe { value: None }),
expr!(unboxed Maybe { value: Some(expr!(Literal lit!(Null))) }),
expr!(unboxed SomeIterable Letters { letters: "abc" }),
expr!(unboxed Mappable Letters { letters: "def" }),
expr!(unboxed Summable { values: vec![1, 2, 3, 4] }),
expr!(unboxed SpecialValue LitValue::Num(2.0)),
expr!(unboxed SpecialValue LitValue::Bool(false)),
expr!(unboxed SpecialValue LitValue::Str("another string")),
expr!(unboxed Variable Token { source: "a variable ", span: 2..10 }),
expr!(unboxed Conditional { skip_me_always: (), condition: None, my_values: vec![] }),
expr!(unboxed Conditional { skip_me_always: (), condition: None, my_values: vec![expr!(unboxed Literal lit!(Bool false))] }),
expr!(unboxed Conditional { skip_me_always: (), condition: Some(expr!(Literal lit!(Bool true))), my_values: vec![] }),
expr!(unboxed Conditional {
skip_me_always: (),
condition: Some(expr!(Literal lit!(Bool true))),
my_values: vec![expr!(unboxed Literal lit!(Bool false))]
}),
])
}
#[test]
fn test_ast_to_str() {
let ast = get_ast();
assert_eq!(
ast.ast_to_str().trim().with_display_as_debug_wrapper(),
r#"
ExprKind::List
╰─elements=↓
├─ExprKind::Binary
│ ├─left: Literal
│ │ ╰─value: Num(1.0)
│ ├─operator: Add
│ ╰─right: ExprKind::Binary
│ ├─left: Literal
│ │ ╰─value: Bool(true)
│ ├─operator: Mul
│ ╰─right: Literal
│ ╰─value: Str("a string")
├─ExprKind::Maybe
│ ╰─value: Nothing
├─ExprKind::Maybe
│ ╰─value: Literal
│ ╰─value: Null
├─ExprKind::SomeIterable
│ ╰─field0=↓
│ ├─97
│ ├─98
│ ╰─99
├─ExprKind::Mappable
│ ╰─field0=↓
│ ├─16
│ ├─217
│ ╰─164
├─ExprKind::Summable
│ ╰─sum: 10
├─LitValue::Num
│ ╰─field0: 2.0
├─LitValue::Bool
│ ╰─field0: false
├─LitValue::Str
│ ╰─field0: `another string`
├─ExprKind::Variable
│ ╰─name: Token
│ ╰─lexeme: "variable"
├─ExprKind::Conditional
├─ExprKind::Conditional
│ ╰─values=↓
│ ╰─Literal
│ ╰─value: Bool(false)
├─ExprKind::Conditional
│ ╰─condition: Literal
│ ╰─value: Bool(true)
╰─ExprKind::Conditional
├─condition: Literal
│ ╰─value: Bool(true)
╰─values=↓
╰─Literal
╰─value: Bool(false)"#
.trim()
.with_display_as_debug_wrapper()
);
}
#[test]
fn test_ast_to_str_with_custom_symbols() {
let ast = get_ast();
assert_eq!(
ast.ast_to_str_impl(&ast2str::TestSymbols)
.trim()
.with_display_as_debug_wrapper(),
r#"
ExprKind::List
elements=
ExprKind::Binary
left: Literal
value: Num(1.0)
operator: Add
right: ExprKind::Binary
left: Literal
value: Bool(true)
operator: Mul
right: Literal
value: Str("a string")
ExprKind::Maybe
value: Nothing
ExprKind::Maybe
value: Literal
value: Null
ExprKind::SomeIterable
field0=
97
98
99
ExprKind::Mappable
field0=
16
217
164
ExprKind::Summable
sum: 10
LitValue::Num
field0: 2.0
LitValue::Bool
field0: false
LitValue::Str
field0: `another string`
ExprKind::Variable
name: Token
lexeme: "variable"
ExprKind::Conditional
ExprKind::Conditional
values=
Literal
value: Bool(false)
ExprKind::Conditional
condition: Literal
value: Bool(true)
ExprKind::Conditional
condition: Literal
value: Bool(true)
values=
Literal
value: Bool(false)
"#
.trim()
.with_display_as_debug_wrapper()
);
}