mago_syntax/cst/cst/
assignment.rs1use strum::Display;
2
3use mago_span::HasSpan;
4use mago_span::Span;
5
6use crate::cst::cst::expression::Expression;
7
8#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord, Display)]
10#[cfg_attr(feature = "serde", derive(serde::Serialize))]
11#[cfg_attr(feature = "serde", serde(tag = "type", content = "value"))]
12pub enum AssignmentOperator {
13 Assign(Span),
14 Addition(Span),
15 Subtraction(Span),
16 Multiplication(Span),
17 Division(Span),
18 Modulo(Span),
19 Exponentiation(Span),
20 Concat(Span),
21 BitwiseAnd(Span),
22 BitwiseOr(Span),
23 BitwiseXor(Span),
24 LeftShift(Span),
25 RightShift(Span),
26 Coalesce(Span),
27}
28
29#[derive(Debug, Clone, Eq, PartialEq, Hash, PartialOrd, Ord)]
31#[cfg_attr(feature = "serde", derive(serde::Serialize))]
32pub struct Assignment<'arena> {
33 pub lhs: &'arena Expression<'arena>,
34 pub operator: AssignmentOperator,
35 pub rhs: &'arena Expression<'arena>,
36}
37
38impl AssignmentOperator {
39 #[inline]
40 #[must_use]
41 pub const fn is_assign(&self) -> bool {
42 matches!(self, Self::Assign(_))
43 }
44
45 #[inline]
46 #[must_use]
47 pub const fn is_arithmetic(&self) -> bool {
48 matches!(
49 self,
50 Self::Addition(_) | Self::Subtraction(_) | Self::Multiplication(_) | Self::Division(_) | Self::Modulo(_)
51 )
52 }
53
54 #[inline]
55 #[must_use]
56 pub const fn is_bitwise(&self) -> bool {
57 matches!(
58 self,
59 Self::BitwiseAnd(_) | Self::BitwiseOr(_) | Self::BitwiseXor(_) | Self::LeftShift(_) | Self::RightShift(_)
60 )
61 }
62
63 #[inline]
65 #[must_use]
66 pub const fn as_str(&self) -> &'static str {
67 match self {
68 Self::Assign(_) => "=",
69 Self::Addition(_) => "+=",
70 Self::Subtraction(_) => "-=",
71 Self::Multiplication(_) => "*=",
72 Self::Division(_) => "/=",
73 Self::Modulo(_) => "%=",
74 Self::Exponentiation(_) => "**=",
75 Self::Concat(_) => ".=",
76 Self::BitwiseAnd(_) => "&=",
77 Self::BitwiseOr(_) => "|=",
78 Self::BitwiseXor(_) => "^=",
79 Self::LeftShift(_) => "<<=",
80 Self::RightShift(_) => ">>=",
81 Self::Coalesce(_) => "??=",
82 }
83 }
84}
85
86impl HasSpan for AssignmentOperator {
87 fn span(&self) -> Span {
88 match self {
89 Self::Assign(span) => *span,
90 Self::Addition(span) => *span,
91 Self::Subtraction(span) => *span,
92 Self::Multiplication(span) => *span,
93 Self::Division(span) => *span,
94 Self::Modulo(span) => *span,
95 Self::Exponentiation(span) => *span,
96 Self::Concat(span) => *span,
97 Self::BitwiseAnd(span) => *span,
98 Self::BitwiseOr(span) => *span,
99 Self::BitwiseXor(span) => *span,
100 Self::LeftShift(span) => *span,
101 Self::RightShift(span) => *span,
102 Self::Coalesce(span) => *span,
103 }
104 }
105}
106
107impl HasSpan for Assignment<'_> {
108 fn span(&self) -> Span {
109 self.lhs.span().join(self.rhs.span())
110 }
111}