Skip to main content

ecma_syntax_cat/
operator.rs

1//! ECMAScript operator enums, factored out of the expression variants.
2
3/// Binary operators per ECMA-262 (excluding `&&`, `||`, `??` which are in
4/// [`LogicalOperator`]).
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6pub enum BinaryOperator {
7    /// `==`
8    Equal,
9    /// `!=`
10    NotEqual,
11    /// `===`
12    StrictEqual,
13    /// `!==`
14    StrictNotEqual,
15    /// `<`
16    LessThan,
17    /// `<=`
18    LessThanOrEqual,
19    /// `>`
20    GreaterThan,
21    /// `>=`
22    GreaterThanOrEqual,
23    /// `<<`
24    LeftShift,
25    /// `>>`
26    RightShift,
27    /// `>>>`
28    UnsignedRightShift,
29    /// `+`
30    Add,
31    /// `-`
32    Subtract,
33    /// `*`
34    Multiply,
35    /// `/`
36    Divide,
37    /// `%`
38    Remainder,
39    /// `**`
40    Exponentiation,
41    /// `|`
42    BitwiseOr,
43    /// `^`
44    BitwiseXor,
45    /// `&`
46    BitwiseAnd,
47    /// `in`
48    In,
49    /// `instanceof`
50    InstanceOf,
51}
52
53impl std::fmt::Display for BinaryOperator {
54    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
55        f.write_str(match self {
56            Self::Equal => "==",
57            Self::NotEqual => "!=",
58            Self::StrictEqual => "===",
59            Self::StrictNotEqual => "!==",
60            Self::LessThan => "<",
61            Self::LessThanOrEqual => "<=",
62            Self::GreaterThan => ">",
63            Self::GreaterThanOrEqual => ">=",
64            Self::LeftShift => "<<",
65            Self::RightShift => ">>",
66            Self::UnsignedRightShift => ">>>",
67            Self::Add => "+",
68            Self::Subtract => "-",
69            Self::Multiply => "*",
70            Self::Divide => "/",
71            Self::Remainder => "%",
72            Self::Exponentiation => "**",
73            Self::BitwiseOr => "|",
74            Self::BitwiseXor => "^",
75            Self::BitwiseAnd => "&",
76            Self::In => "in",
77            Self::InstanceOf => "instanceof",
78        })
79    }
80}
81
82/// Logical operators with short-circuit semantics.
83#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
84pub enum LogicalOperator {
85    /// `&&`
86    And,
87    /// `||`
88    Or,
89    /// `??`
90    NullishCoalescing,
91}
92
93impl std::fmt::Display for LogicalOperator {
94    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
95        f.write_str(match self {
96            Self::And => "&&",
97            Self::Or => "||",
98            Self::NullishCoalescing => "??",
99        })
100    }
101}
102
103/// Unary prefix operators.
104#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
105pub enum UnaryOperator {
106    /// `-`
107    Minus,
108    /// `+`
109    Plus,
110    /// `!`
111    LogicalNot,
112    /// `~`
113    BitwiseNot,
114    /// `typeof`
115    TypeOf,
116    /// `void`
117    Void,
118    /// `delete`
119    Delete,
120}
121
122impl std::fmt::Display for UnaryOperator {
123    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
124        f.write_str(match self {
125            Self::Minus => "-",
126            Self::Plus => "+",
127            Self::LogicalNot => "!",
128            Self::BitwiseNot => "~",
129            Self::TypeOf => "typeof",
130            Self::Void => "void",
131            Self::Delete => "delete",
132        })
133    }
134}
135
136/// `++` and `--`.  The `prefix: bool` field on [`Expression::Update`]
137/// distinguishes `++x` from `x++`.
138///
139/// [`Expression::Update`]: crate::expression::ExpressionKind::Update
140#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
141pub enum UpdateOperator {
142    /// `++`
143    Increment,
144    /// `--`
145    Decrement,
146}
147
148impl std::fmt::Display for UpdateOperator {
149    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
150        f.write_str(match self {
151            Self::Increment => "++",
152            Self::Decrement => "--",
153        })
154    }
155}
156
157/// Assignment operators.  `=` is plain assignment; all others are compound
158/// (e.g. `+=` is "add and assign").
159#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
160pub enum AssignmentOperator {
161    /// `=`
162    Assign,
163    /// `+=`
164    AddAssign,
165    /// `-=`
166    SubtractAssign,
167    /// `*=`
168    MultiplyAssign,
169    /// `/=`
170    DivideAssign,
171    /// `%=`
172    RemainderAssign,
173    /// `**=`
174    ExponentiationAssign,
175    /// `<<=`
176    LeftShiftAssign,
177    /// `>>=`
178    RightShiftAssign,
179    /// `>>>=`
180    UnsignedRightShiftAssign,
181    /// `|=`
182    BitwiseOrAssign,
183    /// `^=`
184    BitwiseXorAssign,
185    /// `&=`
186    BitwiseAndAssign,
187    /// `||=`
188    LogicalOrAssign,
189    /// `&&=`
190    LogicalAndAssign,
191    /// `??=`
192    NullishCoalescingAssign,
193}
194
195impl std::fmt::Display for AssignmentOperator {
196    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
197        f.write_str(match self {
198            Self::Assign => "=",
199            Self::AddAssign => "+=",
200            Self::SubtractAssign => "-=",
201            Self::MultiplyAssign => "*=",
202            Self::DivideAssign => "/=",
203            Self::RemainderAssign => "%=",
204            Self::ExponentiationAssign => "**=",
205            Self::LeftShiftAssign => "<<=",
206            Self::RightShiftAssign => ">>=",
207            Self::UnsignedRightShiftAssign => ">>>=",
208            Self::BitwiseOrAssign => "|=",
209            Self::BitwiseXorAssign => "^=",
210            Self::BitwiseAndAssign => "&=",
211            Self::LogicalOrAssign => "||=",
212            Self::LogicalAndAssign => "&&=",
213            Self::NullishCoalescingAssign => "??=",
214        })
215    }
216}