1use tokenizer::{tokens::CommentedToken, tokens_buffer::TokensBuffer};
2
3#[derive(Debug, Clone, PartialEq)]
4pub enum Expression<'a> {
5 Symbol(&'a CommentedToken<'a>),
6 Literal(&'a CommentedToken<'a>),
7 Comment(&'a CommentedToken<'a>),
8 Term(Box<TermExpr<'a>>),
9 Unary(&'a CommentedToken<'a>, Box<Expression<'a>>),
10 Bop(
11 &'a CommentedToken<'a>,
12 Box<Expression<'a>>,
13 Box<Expression<'a>>,
14 ),
15 Formula(&'a CommentedToken<'a>, Box<Expression<'a>>),
16 Newline(&'a CommentedToken<'a>),
17 Whitespace(&'a [&'a CommentedToken<'a>]),
18 EOF(&'a CommentedToken<'a>),
19 FunctionDef(FunctionDefinition<'a>),
20 LambdaFunction(Lambda<'a>),
21 IfExpression(IfExpression<'a>),
22 WhileExpression(WhileExpression<'a>),
23 RepeatExpression(RepeatExpression<'a>),
24 FunctionCall(FunctionCall<'a>),
25 SubsetExpression(SubsetExpression<'a>),
26 ForLoopExpression(ForLoop<'a>),
27 Break(&'a CommentedToken<'a>),
28 Continue(&'a CommentedToken<'a>),
29}
30
31impl std::fmt::Display for Expression<'_> {
32 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
33 match self {
34 Expression::Symbol(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
35 Expression::Literal(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
36 Expression::Comment(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
37 Expression::Term(term) => f.write_fmt(format_args!("{}", term)),
38 Expression::Unary(op, expr) => f.write_fmt(format_args!("{}{}", op, expr)),
39 Expression::Bop(op, left, right) => {
40 f.write_fmt(format_args!("{} {} {}", left, TokensBuffer(&[op]), right))
41 }
42 Expression::Formula(tilde, term) => write!(f, "{} {}", tilde, term),
43 Expression::Newline(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
44 Expression::Whitespace(tokens) => f.write_fmt(format_args!("{}", TokensBuffer(tokens))),
45 Expression::EOF(token) => f.write_fmt(format_args!("{}", TokensBuffer(&[token]))),
46 Expression::FunctionDef(func_def) => f.write_fmt(format_args!("{}", func_def)),
47 Expression::IfExpression(if_expression) => {
48 f.write_fmt(format_args!("{}", if_expression))
49 }
50 Expression::WhileExpression(while_expression) => {
51 f.write_fmt(format_args!("{}", while_expression))
52 }
53 Expression::RepeatExpression(repeat_expression) => {
54 f.write_fmt(format_args!("{}", repeat_expression))
55 }
56 Expression::FunctionCall(function_call) => {
57 f.write_fmt(format_args!("{}", function_call))
58 }
59 Expression::SubsetExpression(subset_expression) => {
60 f.write_fmt(format_args!("{}", subset_expression))
61 }
62 Expression::ForLoopExpression(for_loop) => f.write_fmt(format_args!("{}", for_loop)),
63 Expression::Break(token) | Expression::Continue(token) => {
64 f.write_fmt(format_args!("{}", TokensBuffer(&[token])))
65 }
66 Expression::LambdaFunction(lambda) => f.write_fmt(format_args!("{}", lambda)),
67 }
68 }
69}
70
71#[derive(Debug, Clone, PartialEq)]
72pub struct ExpressionsBuffer<'a>(pub &'a [Expression<'a>]);
73impl std::fmt::Display for ExpressionsBuffer<'_> {
74 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
75 f.write_str("Expressions: [")?;
76 f.write_fmt(format_args!(
77 "{}",
78 self.0
79 .iter()
80 .map(|x| x.to_string())
81 .collect::<Vec<String>>()
82 .join("\n")
83 ))?;
84 f.write_str("]\n")
85 }
86}
87
88#[derive(Debug, Clone, PartialEq)]
90pub struct TermExpr<'a> {
91 pub pre_delimiters: Option<&'a CommentedToken<'a>>,
92 pub term: Vec<Expression<'a>>,
93 pub post_delimiters: Option<&'a CommentedToken<'a>>,
94}
95
96impl<'a> TermExpr<'a> {
97 pub fn new(
98 pre_delimiters: Option<&'a CommentedToken<'a>>,
99 term: Vec<Expression<'a>>,
100 post_delimiters: Option<&'a CommentedToken<'a>>,
101 ) -> Self {
102 Self {
103 pre_delimiters,
104 term,
105 post_delimiters,
106 }
107 }
108}
109
110impl<'a> From<Expression<'a>> for TermExpr<'a> {
111 fn from(expr: Expression<'a>) -> Self {
112 Self::new(None, vec![expr], None)
113 }
114}
115
116impl std::fmt::Display for TermExpr<'_> {
117 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
118 f.write_fmt(format_args!(
119 "(TermExpr: {} {} {})",
120 if let Some(pre_delim) = self.pre_delimiters {
121 pre_delim.to_string()
122 } else {
123 "".to_string()
124 },
125 self.term
126 .iter()
127 .map(|e| format!("(expr: {})", e))
128 .collect::<Vec<_>>()
129 .join(" "),
130 if let Some(post_delim) = self.post_delimiters {
131 post_delim.to_string()
132 } else {
133 "".to_string()
134 },
135 ))
136 }
137}
138
139#[derive(Debug, Clone, PartialEq)]
144pub struct Arg<'a>(pub Option<Expression<'a>>, pub Option<Expression<'a>>); impl std::fmt::Display for Arg<'_> {
147 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
148 if let Some(ref xpr) = self.0 {
149 f.write_fmt(format_args!("{}", xpr))?;
150 }
151 if let Some(comma) = &self.1 {
152 f.write_fmt(format_args!("comma:{}", comma))?;
153 }
154 Ok(())
155 }
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub enum Delimiter<'a> {
160 Paren(&'a CommentedToken<'a>),
161 SingleBracket(&'a CommentedToken<'a>),
162 DoubleBracket((&'a CommentedToken<'a>, &'a CommentedToken<'a>)),
163}
164
165impl std::fmt::Display for Delimiter<'_> {
166 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
167 match self {
168 Delimiter::Paren(single) | Delimiter::SingleBracket(single) => {
169 f.write_fmt(format_args!("{}", single))
170 }
171 Delimiter::DoubleBracket((b1, b2)) => f.write_fmt(format_args!("{}{}", b1, b2)),
172 }
173 }
174}
175
176#[derive(Debug, Clone, PartialEq)]
177pub struct Args<'a> {
178 pub left_delimeter: Delimiter<'a>,
179 pub args: Vec<Arg<'a>>,
180 pub right_delimeter: Delimiter<'a>,
181}
182
183impl<'a> Args<'a> {
184 pub fn new(
185 left_delimeter: Delimiter<'a>,
186 args: Vec<Arg<'a>>,
187 right_delimeter: Delimiter<'a>,
188 ) -> Self {
189 Self {
190 left_delimeter,
191 args,
192 right_delimeter,
193 }
194 }
195}
196
197impl std::fmt::Display for Args<'_> {
198 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
199 f.write_fmt(format_args!(
200 "(Args: {} {} {})",
201 self.left_delimeter,
202 self.args
203 .iter()
204 .map(|arg| arg.to_string())
205 .collect::<Vec<String>>()
206 .join(" "),
207 self.right_delimeter,
208 ))
209 }
210}
211
212#[derive(Debug, Clone, PartialEq)]
213pub struct FunctionDefinition<'a> {
214 pub keyword: &'a CommentedToken<'a>,
215 pub arguments: Args<'a>,
216 pub body: Box<Expression<'a>>,
217}
218
219impl<'a> FunctionDefinition<'a> {
220 pub fn new(
221 keyword: &'a CommentedToken<'a>,
222 arguments: Args<'a>,
223 body: Box<Expression<'a>>,
224 ) -> Self {
225 Self {
226 keyword,
227 arguments,
228 body,
229 }
230 }
231}
232
233impl std::fmt::Display for FunctionDefinition<'_> {
234 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
235 f.write_fmt(format_args!(
236 "{} {} {}",
237 self.keyword, self.arguments, self.body
238 ))
239 }
240}
241
242#[derive(Debug, Clone, PartialEq)]
244pub struct IfConditional<'a> {
245 pub keyword: &'a CommentedToken<'a>,
246 pub left_delimiter: &'a CommentedToken<'a>,
247 pub condition: Box<Expression<'a>>,
248 pub right_delimiter: &'a CommentedToken<'a>,
249 pub body: Box<Expression<'a>>,
250}
251
252#[derive(Debug, Clone, PartialEq)]
253pub struct ElseIfConditional<'a> {
254 pub else_keyword: &'a CommentedToken<'a>,
255 pub if_conditional: IfConditional<'a>,
256}
257
258#[derive(Debug, Clone, PartialEq)]
259pub struct TrailingElse<'a> {
260 pub else_keyword: &'a CommentedToken<'a>,
261 pub body: Box<Expression<'a>>,
262}
263
264#[derive(Debug, Clone, PartialEq)]
265pub struct IfExpression<'a> {
266 pub if_conditional: IfConditional<'a>,
267 pub else_ifs: Vec<ElseIfConditional<'a>>,
268 pub trailing_else: Option<TrailingElse<'a>>,
269}
270
271impl std::fmt::Display for TrailingElse<'_> {
272 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
273 f.write_fmt(format_args!("{} {}", self.else_keyword, self.body))
274 }
275}
276
277impl std::fmt::Display for IfConditional<'_> {
278 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
279 f.write_fmt(format_args!(
280 "{} {} {} {} {}",
281 self.keyword, self.left_delimiter, self.condition, self.right_delimiter, self.body
282 ))
283 }
284}
285
286impl std::fmt::Display for ElseIfConditional<'_> {
287 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
288 f.write_fmt(format_args!(
289 "{} {}",
290 self.else_keyword, self.if_conditional
291 ))
292 }
293}
294
295impl std::fmt::Display for IfExpression<'_> {
296 fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
297 f.write_fmt(format_args!("{} ", self.if_conditional))?;
298 for else_if in &self.else_ifs {
299 f.write_fmt(format_args!("{}", else_if))?;
300 }
301 match &self.trailing_else {
302 Some(trailing_else) => f.write_fmt(format_args!("{}", trailing_else)),
303 None => Ok(()),
304 }
305 }
306}
307
308#[derive(Debug, Clone, PartialEq)]
310pub struct WhileExpression<'a> {
311 pub while_keyword: &'a CommentedToken<'a>,
312 pub condition: Box<Expression<'a>>,
313 pub body: Box<Expression<'a>>,
314}
315
316impl std::fmt::Display for WhileExpression<'_> {
317 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
318 f.write_fmt(format_args!(
319 "({} {} {})",
320 self.while_keyword, self.condition, self.body
321 ))
322 }
323}
324
325#[derive(Debug, Clone, PartialEq)]
327pub struct RepeatExpression<'a> {
328 pub repeat_keyword: &'a CommentedToken<'a>,
329 pub body: Box<Expression<'a>>,
330}
331
332impl std::fmt::Display for RepeatExpression<'_> {
333 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
334 f.write_fmt(format_args!("({} {})", self.repeat_keyword, self.body))
335 }
336}
337
338#[derive(Debug, Clone, PartialEq)]
340pub struct FunctionCall<'a> {
341 pub function_ref: Box<Expression<'a>>,
342 pub args: Args<'a>,
343}
344
345impl std::fmt::Display for FunctionCall<'_> {
346 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
347 f.write_fmt(format_args!("Call({} {})", self.function_ref, self.args))
348 }
349}
350
351#[derive(Debug, Clone, PartialEq)]
353pub struct SubsetExpression<'a> {
354 pub object_ref: Box<Expression<'a>>,
355 pub args: Args<'a>,
356}
357
358impl std::fmt::Display for SubsetExpression<'_> {
359 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
360 f.write_fmt(format_args!("{}{}", self.object_ref, self.args))
361 }
362}
363
364#[derive(Debug, Clone, PartialEq)]
366pub struct ForLoop<'a> {
367 pub keyword: &'a CommentedToken<'a>,
368 pub left_delim: Delimiter<'a>,
369 pub identifier: Box<Expression<'a>>,
370 pub in_keyword: &'a CommentedToken<'a>,
371 pub collection: Box<Expression<'a>>,
372 pub right_delim: Delimiter<'a>,
373 pub body: Box<Expression<'a>>,
374}
375
376impl std::fmt::Display for ForLoop<'_> {
377 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
378 f.write_fmt(format_args!(
379 "<{} {} {} {} {} {} {}>",
380 self.keyword,
381 self.left_delim,
382 self.identifier,
383 self.in_keyword,
384 self.collection,
385 self.right_delim,
386 self.body
387 ))
388 }
389}
390
391#[derive(Debug, Clone, PartialEq)]
393pub struct Lambda<'a> {
394 pub keyword: &'a CommentedToken<'a>,
395 pub args: Args<'a>,
396 pub body: Box<Expression<'a>>,
397}
398
399impl std::fmt::Display for Lambda<'_> {
400 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
401 f.write_fmt(format_args!("{} {} {}", self.keyword, self.args, self.body))
402 }
403}