teo_parser/ast/
arith_expr.rs1use std::collections::BTreeMap;
2use std::fmt::{Display, Formatter};
3use crate::ast::expression::Expression;
4use crate::ast::span::Span;
5use crate::{declare_container_node, impl_container_node_defaults, node_child_fn};
6use crate::ast::node::Node;
7use crate::format::Writer;
8use crate::traits::identifiable::Identifiable;
9use crate::traits::node_trait::NodeTrait;
10use crate::traits::write::Write;
11
12#[derive(Debug, Clone, Copy, PartialEq)]
13pub enum ArithExprOperator {
14 Neg,
15 Add,
16 Sub,
17 Mul,
18 Div,
19 Mod,
20 Not,
21 And,
22 Or,
23 BitAnd,
24 BitXor,
25 BitOr,
26 BitNeg,
27 BitLS,
28 BitRS,
29 NullishCoalescing,
30 Gt,
31 Gte,
32 Lt,
33 Lte,
34 Eq,
35 Neq,
36 RangeOpen,
37 RangeClose,
38 ForceUnwrap,
39}
40
41declare_container_node!(UnaryOperation, pub op: ArithExprOperator, pub(crate) rhs: usize);
42
43impl_container_node_defaults!(UnaryOperation);
44
45impl UnaryOperation {
46
47 node_child_fn!(rhs, ArithExpr);
48}
49
50impl Write for UnaryOperation {
51 fn write<'a>(&'a self, writer: &mut Writer<'a>) {
52 writer.write_children(self, self.children.values())
53 }
54}
55
56declare_container_node!(UnaryPostfixOperation, pub op: ArithExprOperator, pub(crate) lhs: usize);
57
58impl_container_node_defaults!(UnaryPostfixOperation);
59
60impl UnaryPostfixOperation {
61
62 node_child_fn!(lhs, ArithExpr);
63}
64
65impl Write for UnaryPostfixOperation {
66 fn write<'a>(&'a self, writer: &mut Writer<'a>) {
67 writer.write_children(self, self.children.values())
68 }
69}
70
71declare_container_node!(BinaryOperation, pub(crate) lhs: usize, pub op: ArithExprOperator, pub(crate) rhs: usize);
72
73impl_container_node_defaults!(BinaryOperation);
74
75impl BinaryOperation {
76
77 node_child_fn!(lhs, ArithExpr);
78 node_child_fn!(rhs, ArithExpr);
79}
80
81impl Write for BinaryOperation {
82 fn write<'a>(&'a self, writer: &mut Writer<'a>) {
83 writer.write_children(self, self.children.values())
84 }
85}
86
87#[derive(Debug)]
88pub enum ArithExpr {
89 Expression(Box<Expression>),
90 UnaryOperation(UnaryOperation),
91 BinaryOperation(BinaryOperation),
92 UnaryPostfixOperation(UnaryPostfixOperation),
93}
94
95impl ArithExpr {
96
97 pub fn as_dyn_node_trait(&self) -> &dyn NodeTrait {
98 match self {
99 ArithExpr::Expression(n) => n.as_ref(),
100 ArithExpr::UnaryOperation(n) => n,
101 ArithExpr::BinaryOperation(n) => n,
102 ArithExpr::UnaryPostfixOperation(n) => n,
103 }
104 }
105
106 pub fn unwrap_enumerable_enum_member_strings(&self) -> Option<Vec<&str>> {
107 match self {
108 ArithExpr::Expression(e) => e.unwrap_enumerable_enum_member_strings(),
109 _ => None,
110 }
111 }
112
113 pub fn unwrap_enumerable_enum_member_string(&self) -> Option<&str> {
114 match self {
115 ArithExpr::Expression(e) => e.unwrap_enumerable_enum_member_string(),
116 _ => None,
117 }
118 }
119}
120
121impl Identifiable for ArithExpr {
122 fn path(&self) -> &Vec<usize> {
123 self.as_dyn_node_trait().path()
124 }
125}
126
127impl NodeTrait for ArithExpr {
128 fn span(&self) -> Span {
129 self.as_dyn_node_trait().span()
130 }
131
132 fn children(&self) -> Option<&BTreeMap<usize, Node>> {
133 self.as_dyn_node_trait().children()
134 }
135}
136
137impl Write for ArithExpr {
138 fn write<'a>(&'a self, writer: &mut Writer<'a>) {
139 self.as_dyn_node_trait().write(writer);
140 }
141
142 fn write_output_with_default_writer(&self) -> String {
143 self.as_dyn_node_trait().write_output_with_default_writer()
144 }
145
146 fn prefer_whitespace_before(&self) -> bool {
147 self.as_dyn_node_trait().prefer_whitespace_before()
148 }
149
150 fn prefer_whitespace_after(&self) -> bool {
151 self.as_dyn_node_trait().prefer_whitespace_after()
152 }
153
154 fn prefer_always_no_whitespace_before(&self) -> bool {
155 self.as_dyn_node_trait().prefer_always_no_whitespace_before()
156 }
157
158 fn always_start_on_new_line(&self) -> bool {
159 self.as_dyn_node_trait().always_start_on_new_line()
160 }
161
162 fn always_end_on_new_line(&self) -> bool {
163 self.as_dyn_node_trait().always_end_on_new_line()
164 }
165
166 fn is_block_start(&self) -> bool {
167 self.as_dyn_node_trait().is_block_start()
168 }
169
170 fn is_block_end(&self) -> bool {
171 self.as_dyn_node_trait().is_block_end()
172 }
173
174 fn is_block_element_delimiter(&self) -> bool {
175 self.as_dyn_node_trait().is_block_element_delimiter()
176 }
177
178 fn is_block_level_element(&self) -> bool {
179 self.as_dyn_node_trait().is_block_level_element()
180 }
181
182 fn wrap(&self, content: &str, available_length: usize) -> String {
183 self.as_dyn_node_trait().wrap(content, available_length)
184 }
185}
186
187impl Display for ArithExpr {
188 fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
189 Display::fmt(self.as_dyn_node_trait(), f)
190 }
191}
192
193impl<'a> TryFrom<&'a Node> for &'a ArithExpr {
194 type Error = &'static str;
195
196 fn try_from(value: &'a Node) -> Result<Self, Self::Error> {
197 match value {
198 Node::ArithExpr(n) => Ok(n),
199 _ => Err("convert failed"),
200 }
201 }
202}
203
204impl From<ArithExpr> for Node {
205 fn from(value: ArithExpr) -> Self {
206 Self::ArithExpr(value)
207 }
208}