teo_parser/ast/
expression.rs

1use std::cell::RefCell;
2use std::collections::BTreeMap;
3use std::fmt::{Display, Formatter};
4use crate::ast::argument_list::ArgumentList;
5use crate::ast::arith_expr::ArithExpr;
6use crate::ast::bracket_expression::BracketExpression;
7use crate::ast::empty_pipeline::EmptyPipeline;
8use crate::ast::group::Group;
9use crate::ast::pipeline::Pipeline;
10use crate::ast::identifier::Identifier;
11use crate::ast::int_subscript::IntSubscript;
12use crate::ast::literals::{ArrayLiteral, BoolLiteral, DictionaryLiteral, EnumVariantLiteral, NullLiteral, NumericLiteral, RegexLiteral, StringLiteral, TupleLiteral};
13use crate::ast::named_expression::NamedExpression;
14use crate::ast::node::Node;
15use crate::ast::span::Span;
16use crate::ast::subscript::Subscript;
17use crate::ast::type_as_value_expression::TypeAsValueExpression;
18use crate::ast::unit::Unit;
19use crate::format::Writer;
20use crate::traits::identifiable::Identifiable;
21use crate::traits::node_trait::NodeTrait;
22use crate::traits::resolved::{Resolve, ResolveAndClone};
23use crate::traits::write::Write;
24use crate::expr::ExprInfo;
25
26#[derive(Debug)]
27pub enum ExpressionKind {
28    Group(Group),
29    ArithExpr(ArithExpr),
30    NumericLiteral(NumericLiteral),
31    StringLiteral(StringLiteral),
32    RegexLiteral(RegexLiteral),
33    BoolLiteral(BoolLiteral),
34    NullLiteral(NullLiteral),
35    EnumVariantLiteral(EnumVariantLiteral),
36    TupleLiteral(TupleLiteral),
37    ArrayLiteral(ArrayLiteral),
38    DictionaryLiteral(DictionaryLiteral),
39    Identifier(Identifier),
40    ArgumentList(ArgumentList),
41    Subscript(Subscript),
42    IntSubscript(IntSubscript),
43    Unit(Unit),
44    Pipeline(Pipeline),
45    EmptyPipeline(EmptyPipeline),
46    NamedExpression(NamedExpression),
47    BracketExpression(BracketExpression),
48    TypeAsValueExpression(TypeAsValueExpression),
49}
50
51impl ExpressionKind {
52
53    pub fn as_dyn_node_trait(&self) -> &dyn NodeTrait {
54        match self {
55            ExpressionKind::Group(n) => n,
56            ExpressionKind::ArithExpr(n) => n,
57            ExpressionKind::NumericLiteral(n) => n,
58            ExpressionKind::StringLiteral(n) => n,
59            ExpressionKind::RegexLiteral(n) => n,
60            ExpressionKind::BoolLiteral(n) => n,
61            ExpressionKind::NullLiteral(n) => n,
62            ExpressionKind::EnumVariantLiteral(n) => n,
63            ExpressionKind::TupleLiteral(n) => n,
64            ExpressionKind::ArrayLiteral(n) => n,
65            ExpressionKind::DictionaryLiteral(n) => n,
66            ExpressionKind::Identifier(n) => n,
67            ExpressionKind::ArgumentList(n) => n,
68            ExpressionKind::Subscript(n) => n,
69            ExpressionKind::IntSubscript(n) => n,
70            ExpressionKind::Unit(n) => n,
71            ExpressionKind::Pipeline(n) => n,
72            ExpressionKind::EmptyPipeline(n) => n,
73            ExpressionKind::NamedExpression(n) => n,
74            ExpressionKind::BracketExpression(n) => n,
75            ExpressionKind::TypeAsValueExpression(n) => n,
76        }
77    }
78
79    pub fn as_numeric_literal(&self) -> Option<&NumericLiteral> {
80        match self {
81            ExpressionKind::NumericLiteral(n) => Some(n),
82            _ => None,
83        }
84    }
85
86    pub fn is_numeric_literal(&self) -> bool {
87        self.as_numeric_literal().is_some()
88    }
89
90    pub fn as_string_literal(&self) -> Option<&StringLiteral> {
91        match self {
92            ExpressionKind::StringLiteral(n) => Some(n),
93            _ => None,
94        }
95    }
96
97    pub fn is_string_literal(&self) -> bool {
98        self.as_string_literal().is_some()
99    }
100
101    pub fn as_regex_literal(&self) -> Option<&RegexLiteral> {
102        match self {
103            ExpressionKind::RegexLiteral(n) => Some(n),
104            _ => None,
105        }
106    }
107
108    pub fn is_regex_literal(&self) -> bool {
109        self.as_regex_literal().is_some()
110    }
111
112    pub fn as_bool_literal(&self) -> Option<&BoolLiteral> {
113        match self {
114            ExpressionKind::BoolLiteral(n) => Some(n),
115            _ => None,
116        }
117    }
118
119    pub fn is_bool_literal(&self) -> bool {
120        self.as_bool_literal().is_some()
121    }
122
123    pub fn as_null_literal(&self) -> Option<&NullLiteral> {
124        match self {
125            ExpressionKind::NullLiteral(n) => Some(n),
126            _ => None,
127        }
128    }
129
130    pub fn is_null_literal(&self) -> bool {
131        self.as_null_literal().is_some()
132    }
133
134    pub fn as_enum_variant_literal(&self) -> Option<&EnumVariantLiteral> {
135        match self {
136            ExpressionKind::EnumVariantLiteral(n) => Some(n),
137            _ => None,
138        }
139    }
140
141    pub fn is_enum_variant_literal(&self) -> bool {
142        self.as_enum_variant_literal().is_some()
143    }
144
145    pub fn as_tuple(&self) -> Option<&TupleLiteral> {
146        match self {
147            ExpressionKind::TupleLiteral(n) => Some(n),
148            _ => None,
149        }
150    }
151
152    pub fn as_array_literal(&self) -> Option<&ArrayLiteral> {
153        match self {
154            ExpressionKind::ArrayLiteral(n) => Some(n),
155            _ => None,
156        }
157    }
158
159    pub fn is_array_literal(&self) -> bool {
160        self.as_array_literal().is_some()
161    }
162
163    pub fn as_dictionary(&self) -> Option<&DictionaryLiteral> {
164        match self {
165            ExpressionKind::DictionaryLiteral(n) => Some(n),
166            _ => None,
167        }
168    }
169
170    pub fn as_identifier(&self) -> Option<&Identifier> {
171        match self {
172            ExpressionKind::Identifier(i) => Some(i),
173            _ => None,
174        }
175    }
176
177    pub fn is_identifier(&self) -> bool {
178        self.as_identifier().is_some()
179    }
180
181    pub fn is_unit(&self) -> bool {
182        self.as_unit().is_some()
183    }
184
185    pub fn as_unit(&self) -> Option<&Unit> {
186        match self {
187            ExpressionKind::Unit(u) => Some(u),
188            _ => None,
189        }
190    }
191
192    pub fn as_argument_list(&self) -> Option<&ArgumentList> {
193        match self {
194            ExpressionKind::ArgumentList(a) => Some(a),
195            _ => None,
196        }
197    }
198
199    pub fn as_subscript(&self) -> Option<&Subscript> {
200        match self {
201            ExpressionKind::Subscript(s) => Some(s),
202            _ => None,
203        }
204    }
205
206    pub fn as_pipeline(&self) -> Option<&Pipeline> {
207        match self {
208            ExpressionKind::Pipeline(p) => Some(p),
209            _ => None,
210        }
211    }
212
213    pub fn as_arith_expr(&self) -> Option<&ArithExpr> {
214        match self {
215            ExpressionKind::ArithExpr(a) => Some(a),
216            _ => None,
217        }
218    }
219
220    pub fn as_named_expression(&self) -> Option<&NamedExpression> {
221        match self {
222            ExpressionKind::NamedExpression(p) => Some(p),
223            _ => None,
224        }
225    }
226
227    pub fn as_bracket_expression(&self) -> Option<&BracketExpression> {
228        match self {
229            ExpressionKind::BracketExpression(p) => Some(p),
230            _ => None,
231        }
232    }
233
234    pub fn as_type_as_value_expression(&self) -> Option<&TypeAsValueExpression> {
235        match self {
236            ExpressionKind::TypeAsValueExpression(p) => Some(p),
237            _ => None,
238        }
239    }
240
241    pub fn unwrap_enumerable_enum_member_strings(&self) -> Option<Vec<&str>> {
242        match self {
243            ExpressionKind::ArithExpr(a) => a.unwrap_enumerable_enum_member_strings(),
244            ExpressionKind::Unit(u) => u.unwrap_enumerable_enum_member_strings(),
245            ExpressionKind::EnumVariantLiteral(e) => e.unwrap_enumerable_enum_member_strings(),
246            ExpressionKind::ArrayLiteral(a) => a.unwrap_enumerable_enum_member_strings(),
247            _ => None,
248        }
249    }
250
251    pub fn unwrap_enumerable_enum_member_string(&self) -> Option<&str> {
252        match self {
253            ExpressionKind::ArithExpr(a) => a.unwrap_enumerable_enum_member_string(),
254            ExpressionKind::Unit(u) => u.unwrap_enumerable_enum_member_string(),
255            ExpressionKind::EnumVariantLiteral(e) => e.unwrap_enumerable_enum_member_string(),
256            ExpressionKind::ArrayLiteral(a) => a.unwrap_enumerable_enum_member_string(),
257            _ => None,
258        }
259    }
260}
261
262impl Identifiable for ExpressionKind {
263    fn path(&self) -> &Vec<usize> {
264        self.as_dyn_node_trait().path()
265    }
266}
267
268impl NodeTrait for ExpressionKind {
269
270    fn span(&self) -> Span {
271        self.as_dyn_node_trait().span()
272    }
273
274    fn children(&self) -> Option<&BTreeMap<usize, Node>> {
275        self.as_dyn_node_trait().children()
276    }
277}
278
279impl Display for ExpressionKind {
280    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
281        Display::fmt(self.as_dyn_node_trait(), f)
282    }
283}
284
285#[derive(Debug)]
286pub struct Expression {
287    pub kind: ExpressionKind,
288    pub resolved: RefCell<Option<ExprInfo>>,
289}
290
291impl Expression {
292
293    pub fn new(kind: ExpressionKind) -> Self {
294        Self { kind, resolved: RefCell::new(None) }
295    }
296
297    pub fn is_single_identifier(&self) -> bool {
298        if self.kind.is_identifier() {
299            return true;
300        }
301        if let Some(arith_expr) = self.kind.as_arith_expr() {
302            return match arith_expr {
303                ArithExpr::Expression(e) => e.is_single_identifier(),
304                _ => false,
305            };
306        }
307        if let Some(unit) = self.kind.as_unit() {
308            return if unit.expressions().count() == 1 {
309                 unit.expression_at(0).unwrap().is_single_identifier() && unit.empty_dot().is_none()
310            } else {
311                false
312            };
313        }
314        false
315    }
316
317    pub fn unwrap_enumerable_enum_member_strings(&self) -> Option<Vec<&str>> {
318        self.kind.unwrap_enumerable_enum_member_strings()
319    }
320
321    pub fn unwrap_enumerable_enum_member_string(&self) -> Option<&str> {
322        self.kind.unwrap_enumerable_enum_member_string()
323    }
324
325    pub fn named_key_without_resolving(&self) -> Option<&str> {
326        match &self.kind {
327            ExpressionKind::StringLiteral(s) => Some(s.value.as_str()),
328            ExpressionKind::Identifier(i) => Some(i.name()),
329            _ => None,
330        }
331    }
332}
333
334impl Display for Expression {
335    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
336        Display::fmt(&self.kind, f)
337    }
338}
339
340impl Identifiable for Expression {
341
342    fn path(&self) -> &Vec<usize> {
343        self.kind.path()
344    }
345}
346
347impl NodeTrait for Expression {
348    fn span(&self) -> Span {
349        self.kind.span()
350    }
351
352    fn children(&self) -> Option<&BTreeMap<usize, Node>> {
353        self.kind.children()
354    }
355}
356
357impl Resolve<ExprInfo> for Expression {
358    fn resolved_ref_cell(&self) -> &RefCell<Option<ExprInfo>> {
359        &self.resolved
360    }
361}
362
363impl Write for ExpressionKind {
364    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
365        self.as_dyn_node_trait().write(writer);
366    }
367
368    fn write_output_with_default_writer(&self) -> String {
369        self.as_dyn_node_trait().write_output_with_default_writer()
370    }
371
372    fn prefer_whitespace_before(&self) -> bool {
373        self.as_dyn_node_trait().prefer_whitespace_before()
374    }
375
376    fn prefer_whitespace_after(&self) -> bool {
377        self.as_dyn_node_trait().prefer_whitespace_after()
378    }
379
380    fn prefer_always_no_whitespace_before(&self) -> bool {
381        self.as_dyn_node_trait().prefer_always_no_whitespace_before()
382    }
383
384    fn always_start_on_new_line(&self) -> bool {
385        self.as_dyn_node_trait().always_start_on_new_line()
386    }
387
388    fn always_end_on_new_line(&self) -> bool {
389        self.as_dyn_node_trait().always_end_on_new_line()
390    }
391
392    fn is_block_start(&self) -> bool {
393        self.as_dyn_node_trait().is_block_start()
394    }
395
396    fn is_block_end(&self) -> bool {
397        self.as_dyn_node_trait().is_block_end()
398    }
399
400    fn is_block_element_delimiter(&self) -> bool {
401        self.as_dyn_node_trait().is_block_element_delimiter()
402    }
403
404    fn is_block_level_element(&self) -> bool {
405        self.as_dyn_node_trait().is_block_level_element()
406    }
407
408    fn wrap(&self, content: &str, available_length: usize) -> String {
409        self.as_dyn_node_trait().wrap(content, available_length)
410    }
411}
412
413impl Write for Expression {
414    fn write<'a>(&'a self, writer: &mut Writer<'a>) {
415        self.kind.as_dyn_node_trait().write(writer);
416    }
417
418    fn write_output_with_default_writer(&self) -> String {
419        self.kind.as_dyn_node_trait().write_output_with_default_writer()
420    }
421
422    fn prefer_whitespace_before(&self) -> bool {
423        self.kind.as_dyn_node_trait().prefer_whitespace_before()
424    }
425
426    fn prefer_whitespace_after(&self) -> bool {
427        self.kind.as_dyn_node_trait().prefer_whitespace_after()
428    }
429
430    fn prefer_always_no_whitespace_before(&self) -> bool {
431        self.kind.as_dyn_node_trait().prefer_always_no_whitespace_before()
432    }
433
434    fn always_start_on_new_line(&self) -> bool {
435        self.kind.as_dyn_node_trait().always_start_on_new_line()
436    }
437
438    fn always_end_on_new_line(&self) -> bool {
439        self.kind.as_dyn_node_trait().always_end_on_new_line()
440    }
441
442    fn is_block_start(&self) -> bool {
443        self.kind.as_dyn_node_trait().is_block_start()
444    }
445
446    fn is_block_end(&self) -> bool {
447        self.kind.as_dyn_node_trait().is_block_end()
448    }
449
450    fn is_block_element_delimiter(&self) -> bool {
451        self.kind.as_dyn_node_trait().is_block_element_delimiter()
452    }
453
454    fn is_block_level_element(&self) -> bool {
455        self.kind.as_dyn_node_trait().is_block_level_element()
456    }
457
458    fn wrap(&self, content: &str, available_length: usize) -> String {
459        self.kind.as_dyn_node_trait().wrap(content, available_length)
460    }
461}
462
463impl<'a> TryFrom<&'a Node> for &'a Expression {
464    type Error = &'static str;
465
466    fn try_from(value: &'a Node) -> Result<Self, Self::Error> {
467        match value {
468            Node::Expression(n) => Ok(n),
469            _ => Err("convert failed"),
470        }
471    }
472}
473
474impl From<Expression> for Node {
475    fn from(value: Expression) -> Self {
476        Self::Expression(value)
477    }
478}
479
480impl ResolveAndClone<ExprInfo> for Expression { }