1use alloc::boxed::Box;
8use alloc::string::String;
9use alloc::vec::Vec;
10
11#[derive(Debug, Clone, PartialEq)]
12pub enum BinaryOp {
13 Add,
14 Sub,
15 Mul,
16 Div,
17 Mod,
18 Pow,
19 Eq,
20 NotEq,
21 StrictEq,
22 StrictNotEq,
23 Lt,
24 Gt,
25 LtEq,
26 GtEq,
27 BitAnd,
28 BitOr,
29 BitXor,
30 Shl,
31 Shr,
32 UShr,
33 InstanceOf,
34 In,
35}
36
37#[derive(Debug, Clone, PartialEq)]
38pub enum LogicalOp {
39 And, Or, Nullish, }
43
44#[derive(Debug, Clone, PartialEq)]
45pub enum UnaryOp {
46 Neg, Pos, Not, BitNot, TypeOf, Void, Delete, }
54
55#[derive(Debug, Clone, PartialEq)]
57pub struct Property {
58 pub key: String,
59 pub value: Expression,
60 pub computed: Option<Expression>,
62 pub accessor: Option<bool>,
65}
66
67pub const OBJECT_SPREAD_KEY: &str = "\u{0}spread";
69
70#[derive(Debug, Clone, PartialEq)]
71pub enum Expression {
72 Number(f64),
73 BigIntLit(String),
75 Str(String),
76 Bool(bool),
77 Null,
78 Undefined,
79 Identifier(String),
80 This,
81 TemplateLiteral {
82 quasis: Vec<String>,
84 exprs: Vec<Expression>,
85 },
86 TaggedTemplate {
90 tag: Box<Expression>,
91 quasis: Vec<String>,
92 raw: Vec<String>,
93 exprs: Vec<Expression>,
94 },
95 Array(Vec<Expression>),
96 Object(Vec<Property>),
97 Function {
98 name: Option<String>,
99 params: Vec<Param>,
100 body: Vec<Statement>,
101 is_arrow: bool,
102 is_async: bool,
103 is_generator: bool,
104 },
105 Await(Box<Expression>),
107 Yield {
109 argument: Option<Box<Expression>>,
110 delegate: bool,
111 },
112 Unary {
113 op: UnaryOp,
114 expr: Box<Expression>,
115 },
116 Update {
117 op: String, prefix: bool,
119 target: Box<Expression>,
120 },
121 Binary {
122 op: BinaryOp,
123 left: Box<Expression>,
124 right: Box<Expression>,
125 },
126 Logical {
127 op: LogicalOp,
128 left: Box<Expression>,
129 right: Box<Expression>,
130 },
131 Assign {
132 op: String, target: Box<Expression>,
134 value: Box<Expression>,
135 },
136 Conditional {
137 test: Box<Expression>,
138 consequent: Box<Expression>,
139 alternate: Box<Expression>,
140 },
141 Member {
142 object: Box<Expression>,
143 property: String,
144 optional: bool, },
146 Index {
147 object: Box<Expression>,
148 index: Box<Expression>,
149 optional: bool, },
151 Call {
152 callee: Box<Expression>,
153 arguments: Vec<Expression>,
154 optional: bool, },
156 New {
157 callee: Box<Expression>,
158 arguments: Vec<Expression>,
159 },
160 Spread(Box<Expression>),
162 Class {
164 name: Option<String>,
165 superclass: Option<Box<Expression>>,
166 members: Vec<ClassMember>,
167 },
168 Super,
170 Sequence(Vec<Expression>),
173 NewTarget,
176 Regex(String, String),
178}
179
180#[derive(Debug, Clone, PartialEq)]
182pub enum MethodKind {
183 Method,
184 Constructor,
185 Field,
188 Getter,
190 Setter,
192 StaticBlock,
195}
196
197#[derive(Debug, Clone, PartialEq)]
199pub struct ClassMember {
200 pub key: String,
201 pub computed_key: Option<Expression>,
204 pub kind: MethodKind,
205 pub params: Vec<Param>,
206 pub body: Vec<Statement>,
207 pub is_static: bool,
208 pub field_init: Option<Expression>,
210 pub is_async: bool,
212 pub is_generator: bool,
214}
215
216#[derive(Debug, Clone, PartialEq)]
217pub enum VarKind {
218 Var,
219 Let,
220 Const,
221 Using,
224 AwaitUsing,
226}
227
228#[derive(Debug, Clone, PartialEq)]
230pub enum Pattern {
231 Identifier(String),
232 Object(Vec<ObjPatProp>),
234 Array(Vec<ArrPatElem>),
236 Expr(Box<Expression>),
239}
240
241#[derive(Debug, Clone, PartialEq)]
243pub struct ObjPatProp {
244 pub key: String,
247 pub computed_key: Option<Expression>,
249 pub value: Pattern,
251 pub default: Option<Expression>,
252 pub is_rest: bool,
253}
254
255#[derive(Debug, Clone, PartialEq)]
257pub struct ArrPatElem {
258 pub pattern: Option<Pattern>,
260 pub default: Option<Expression>,
261 pub is_rest: bool,
262}
263
264#[derive(Debug, Clone, PartialEq)]
266pub struct Param {
267 pub pattern: Pattern,
268 pub default: Option<Expression>,
269 pub is_rest: bool,
270}
271
272#[derive(Debug, Clone, PartialEq)]
273pub enum Statement {
274 Expression(Expression),
275 VarDeclaration {
276 kind: VarKind,
277 decls: Vec<(Pattern, Option<Expression>)>,
279 },
280 Block(Vec<Statement>),
281 If {
282 test: Expression,
283 consequent: Box<Statement>,
284 alternate: Option<Box<Statement>>,
285 },
286 While {
287 test: Expression,
288 body: Box<Statement>,
289 },
290 DoWhile {
291 body: Box<Statement>,
292 test: Expression,
293 },
294 For {
295 init: Option<Box<Statement>>,
296 test: Option<Expression>,
297 update: Option<Expression>,
298 body: Box<Statement>,
299 },
300 ForIn {
301 decl_kind: Option<VarKind>,
302 pattern: Pattern,
303 object: Expression,
304 body: Box<Statement>,
305 of: bool, is_await: bool,
309 },
310 FunctionDeclaration {
311 name: String,
312 params: Vec<Param>,
313 body: Vec<Statement>,
314 is_async: bool,
315 is_generator: bool,
316 },
317 Return(Option<Expression>),
318 Break(Option<String>),
320 Continue(Option<String>),
322 Labeled(String, Box<Statement>),
325 Throw(Expression),
326 Switch {
327 discriminant: Expression,
328 cases: Vec<SwitchCase>,
329 },
330 Try {
331 block: Vec<Statement>,
332 catch_param: Option<Pattern>,
334 catch_block: Option<Vec<Statement>>,
335 finally_block: Option<Vec<Statement>>,
336 },
337 Import {
340 source: String,
342 default: Option<String>,
344 namespace: Option<String>,
346 named: Vec<(String, String)>,
348 side_effect_only: bool,
350 },
351 ExportNamed {
353 specifiers: Vec<(String, String)>,
355 source: Option<String>,
357 },
358 ExportDefault(Expression),
360 ExportDecl {
363 declaration: Box<Statement>,
364 names: Vec<String>,
365 },
366 ExportAll {
368 source: String,
369 },
370 Empty,
371}
372
373#[derive(Debug, Clone, PartialEq)]
375pub struct SwitchCase {
376 pub test: Option<Expression>,
377 pub body: Vec<Statement>,
378}
379
380#[derive(Debug, Clone, PartialEq)]
381pub struct Program {
382 pub body: Vec<Statement>,
383}