1use strum::Display;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6use crate::cst::cst::expression::Expression;
7use crate::cst::cst::keyword::Keyword;
8use crate::token::GetPrecedence;
9use crate::token::Precedence;
10
11#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash, PartialOrd, Ord, Display)]
13#[cfg_attr(feature = "serde", derive(serde::Serialize))]
14#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
15pub enum BinaryOperator<'arena> {
16 Addition(Span), Subtraction(Span), Multiplication(Span), Division(Span), Modulo(Span), Exponentiation(Span), BitwiseAnd(Span), BitwiseOr(Span), BitwiseXor(Span), LeftShift(Span), RightShift(Span), NullCoalesce(Span), Equal(Span), NotEqual(Span), Identical(Span), NotIdentical(Span), AngledNotEqual(Span), LessThan(Span), LessThanOrEqual(Span), GreaterThan(Span), GreaterThanOrEqual(Span), Spaceship(Span), StringConcat(Span), Instanceof(Keyword<'arena>), And(Span), Or(Span), LowAnd(Keyword<'arena>), LowOr(Keyword<'arena>), LowXor(Keyword<'arena>), }
46
47#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
51#[cfg_attr(feature = "serde", derive(serde::Serialize))]
52pub struct Binary<'arena> {
53 pub lhs: &'arena Expression<'arena>,
54 pub operator: BinaryOperator<'arena>,
55 pub rhs: &'arena Expression<'arena>,
56}
57
58impl<'arena> BinaryOperator<'arena> {
59 #[inline]
60 #[must_use]
61 pub const fn is_constant(&self) -> bool {
62 !matches!(self, Self::Instanceof(_))
63 }
64
65 #[inline]
66 #[must_use]
67 pub const fn is_multiplicative(&self) -> bool {
68 matches!(self, Self::Multiplication(_) | Self::Division(_) | Self::Modulo(_))
69 }
70
71 #[inline]
72 #[must_use]
73 pub const fn is_additive(&self) -> bool {
74 matches!(self, Self::Addition(_) | Self::Subtraction(_))
75 }
76
77 #[inline]
78 #[must_use]
79 pub const fn is_arithmetic(&self) -> bool {
80 matches!(
81 self,
82 Self::Addition(_)
83 | Self::Subtraction(_)
84 | Self::Multiplication(_)
85 | Self::Division(_)
86 | Self::Modulo(_)
87 | Self::Exponentiation(_)
88 )
89 }
90
91 #[inline]
92 #[must_use]
93 pub const fn is_bit_shift(&self) -> bool {
94 matches!(self, Self::LeftShift(_) | Self::RightShift(_))
95 }
96
97 #[inline]
98 #[must_use]
99 pub const fn is_bitwise(&self) -> bool {
100 matches!(
101 self,
102 Self::BitwiseAnd(_) | Self::BitwiseOr(_) | Self::BitwiseXor(_) | Self::LeftShift(_) | Self::RightShift(_)
103 )
104 }
105
106 #[inline]
107 #[must_use]
108 pub const fn is_equality(&self) -> bool {
109 matches!(
110 self,
111 Self::Equal(_)
112 | Self::NotEqual(_)
113 | Self::Identical(_)
114 | Self::NotIdentical(_)
115 | Self::AngledNotEqual(_)
116 | Self::Spaceship(_)
117 )
118 }
119
120 #[must_use]
121 pub const fn is_negated_equality(&self) -> bool {
122 matches!(self, Self::NotEqual(_) | Self::NotIdentical(_) | Self::AngledNotEqual(_))
123 }
124
125 #[inline]
126 #[must_use]
127 pub const fn is_identity(&self) -> bool {
128 matches!(self, Self::Identical(_) | Self::NotIdentical(_))
129 }
130
131 #[inline]
132 #[must_use]
133 pub const fn is_comparison(&self) -> bool {
134 matches!(
135 self,
136 Self::Equal(_)
137 | Self::NotEqual(_)
138 | Self::Identical(_)
139 | Self::NotIdentical(_)
140 | Self::AngledNotEqual(_)
141 | Self::LessThan(_)
142 | Self::LessThanOrEqual(_)
143 | Self::GreaterThan(_)
144 | Self::GreaterThanOrEqual(_)
145 | Self::Spaceship(_)
146 )
147 }
148
149 #[inline]
150 #[must_use]
151 pub const fn is_logical(&self) -> bool {
152 matches!(self, Self::And(_) | Self::Or(_) | Self::LowAnd(_) | Self::LowOr(_) | Self::LowXor(_))
153 }
154
155 #[inline]
156 #[must_use]
157 pub const fn is_low_precedence(&self) -> bool {
158 matches!(self, Self::LowAnd(_) | Self::LowOr(_) | Self::LowXor(_))
159 }
160
161 #[inline]
162 #[must_use]
163 pub const fn is_concatenation(&self) -> bool {
164 matches!(self, Self::StringConcat(_))
165 }
166
167 #[inline]
168 #[must_use]
169 pub const fn is_null_coalesce(&self) -> bool {
170 matches!(self, Self::NullCoalesce(_))
171 }
172
173 #[inline]
174 #[must_use]
175 pub const fn is_instanceof(&self) -> bool {
176 matches!(self, Self::Instanceof(_))
177 }
178
179 #[inline]
180 #[must_use]
181 pub fn as_bytes(&self) -> &'arena [u8] {
182 match self {
183 Self::Addition(_) => b"+",
184 Self::Subtraction(_) => b"-",
185 Self::Multiplication(_) => b"*",
186 Self::Division(_) => b"/",
187 Self::Modulo(_) => b"%",
188 Self::Exponentiation(_) => b"**",
189 Self::BitwiseAnd(_) => b"&",
190 Self::BitwiseOr(_) => b"|",
191 Self::BitwiseXor(_) => b"^",
192 Self::LeftShift(_) => b"<<",
193 Self::RightShift(_) => b">>",
194 Self::NullCoalesce(_) => b"??",
195 Self::Equal(_) => b"==",
196 Self::NotEqual(_) => b"!=",
197 Self::Identical(_) => b"===",
198 Self::NotIdentical(_) => b"!==",
199 Self::AngledNotEqual(_) => b"<>",
200 Self::LessThan(_) => b"<",
201 Self::LessThanOrEqual(_) => b"<=",
202 Self::GreaterThan(_) => b">",
203 Self::GreaterThanOrEqual(_) => b">=",
204 Self::Spaceship(_) => b"<=>",
205 Self::StringConcat(_) => b".",
206 Self::And(_) => b"&&",
207 Self::Or(_) => b"||",
208 Self::Instanceof(keyword) => keyword.value,
209 Self::LowAnd(keyword) => keyword.value,
210 Self::LowOr(keyword) => keyword.value,
211 Self::LowXor(keyword) => keyword.value,
212 }
213 }
214
215 #[inline]
216 #[must_use]
217 pub const fn is_same_as(&self, other: &Self) -> bool {
218 matches!(
219 (self, other),
220 (Self::Addition(_), Self::Addition(_))
221 | (Self::Subtraction(_), Self::Subtraction(_))
222 | (Self::Multiplication(_), Self::Multiplication(_))
223 | (Self::Division(_), Self::Division(_))
224 | (Self::Modulo(_), Self::Modulo(_))
225 | (Self::Exponentiation(_), Self::Exponentiation(_))
226 | (Self::BitwiseAnd(_), Self::BitwiseAnd(_))
227 | (Self::BitwiseOr(_), Self::BitwiseOr(_))
228 | (Self::BitwiseXor(_), Self::BitwiseXor(_))
229 | (Self::LeftShift(_), Self::LeftShift(_))
230 | (Self::RightShift(_), Self::RightShift(_))
231 | (Self::NullCoalesce(_), Self::NullCoalesce(_))
232 | (Self::Equal(_), Self::Equal(_))
233 | (Self::NotEqual(_), Self::NotEqual(_))
234 | (Self::Identical(_), Self::Identical(_))
235 | (Self::NotIdentical(_), Self::NotIdentical(_))
236 | (Self::AngledNotEqual(_), Self::AngledNotEqual(_))
237 | (Self::LessThan(_), Self::LessThan(_))
238 | (Self::LessThanOrEqual(_), Self::LessThanOrEqual(_))
239 | (Self::GreaterThan(_), Self::GreaterThan(_))
240 | (Self::GreaterThanOrEqual(_), Self::GreaterThanOrEqual(_))
241 | (Self::Spaceship(_), Self::Spaceship(_))
242 | (Self::StringConcat(_), Self::StringConcat(_))
243 | (Self::Instanceof(_), Self::Instanceof(_))
244 | (Self::And(_), Self::And(_))
245 | (Self::Or(_), Self::Or(_))
246 | (Self::LowAnd(_), Self::LowAnd(_))
247 | (Self::LowOr(_), Self::LowOr(_))
248 | (Self::LowXor(_), Self::LowXor(_))
249 )
250 }
251}
252
253impl GetPrecedence for BinaryOperator<'_> {
254 #[inline]
255 fn precedence(&self) -> Precedence {
256 match self {
257 Self::Addition(_) | Self::Subtraction(_) => Precedence::AddSub,
258 Self::Multiplication(_) | Self::Division(_) | Self::Modulo(_) => Precedence::MulDivMod,
259 Self::Exponentiation(_) => Precedence::Pow,
260 Self::BitwiseAnd(_) => Precedence::BitwiseAnd,
261 Self::BitwiseOr(_) => Precedence::BitwiseOr,
262 Self::BitwiseXor(_) => Precedence::BitwiseXor,
263 Self::LeftShift(_) | Self::RightShift(_) => Precedence::BitShift,
264 Self::NullCoalesce(_) => Precedence::NullCoalesce,
265 Self::Equal(_)
266 | Self::NotEqual(_)
267 | Self::Identical(_)
268 | Self::NotIdentical(_)
269 | Self::AngledNotEqual(_)
270 | Self::Spaceship(_) => Precedence::Equality,
271 Self::LessThan(_) | Self::LessThanOrEqual(_) | Self::GreaterThan(_) | Self::GreaterThanOrEqual(_) => {
272 Precedence::Comparison
273 }
274 Self::StringConcat(_) => Precedence::Concat,
275 Self::And(_) => Precedence::And,
276 Self::Or(_) => Precedence::Or,
277 Self::LowAnd(_) => Precedence::KeyAnd,
278 Self::LowOr(_) => Precedence::KeyOr,
279 Self::LowXor(_) => Precedence::KeyXor,
280 Self::Instanceof(_) => Precedence::Instanceof,
281 }
282 }
283}
284
285impl HasSpan for BinaryOperator<'_> {
286 fn span(&self) -> Span {
287 match self {
288 Self::Addition(span) => *span,
289 Self::Subtraction(span) => *span,
290 Self::Multiplication(span) => *span,
291 Self::Division(span) => *span,
292 Self::Modulo(span) => *span,
293 Self::Exponentiation(span) => *span,
294 Self::BitwiseAnd(span) => *span,
295 Self::BitwiseOr(span) => *span,
296 Self::BitwiseXor(span) => *span,
297 Self::LeftShift(span) => *span,
298 Self::RightShift(span) => *span,
299 Self::NullCoalesce(span) => *span,
300 Self::Equal(span) => *span,
301 Self::NotEqual(span) => *span,
302 Self::Identical(span) => *span,
303 Self::NotIdentical(span) => *span,
304 Self::AngledNotEqual(span) => *span,
305 Self::LessThan(span) => *span,
306 Self::LessThanOrEqual(span) => *span,
307 Self::GreaterThan(span) => *span,
308 Self::GreaterThanOrEqual(span) => *span,
309 Self::Spaceship(span) => *span,
310 Self::StringConcat(span) => *span,
311 Self::Instanceof(keyword) => keyword.span(),
312 Self::And(span) => *span,
313 Self::Or(span) => *span,
314 Self::LowAnd(keyword) => keyword.span(),
315 Self::LowOr(keyword) => keyword.span(),
316 Self::LowXor(keyword) => keyword.span(),
317 }
318 }
319}
320
321impl HasSpan for Binary<'_> {
322 fn span(&self) -> Span {
323 fn left_edge_span(mut expression: &Expression<'_>) -> Span {
324 while let Expression::Binary(binary) = expression {
325 expression = binary.lhs;
326 }
327
328 expression.span()
329 }
330
331 fn right_edge_span(mut expression: &Expression<'_>) -> Span {
332 while let Expression::Binary(binary) = expression {
333 expression = binary.rhs;
334 }
335
336 expression.span()
337 }
338
339 left_edge_span(self.lhs).join(right_edge_span(self.rhs))
340 }
341}