1#[derive(Debug, Clone, Copy, PartialEq, Eq)]
11pub enum BinOp {
12 Add,
13 Sub,
14 Mul,
15 Div,
16 Mod,
17 Pow, Lt,
20 Le,
21 Gt,
22 Ge,
23 EqEqEq, NeEqEq, EqEq, NeEq, BitAnd,
29 BitOr,
30 BitXor,
31 Shl, Shr, UShr, In,
36 InstanceOf,
37}
38
39#[derive(Debug, Clone, Copy, PartialEq, Eq)]
41pub enum LogicalOp {
42 And, Or, Nullish, }
46
47#[derive(Debug, Clone, Copy, PartialEq, Eq)]
49pub enum UnOp {
50 Neg, Pos, Not, BitNot, TypeOf, Void, Delete, }
58
59#[derive(Debug, Clone, Copy, PartialEq, Eq)]
61pub enum DeclKind {
62 Var,
63 Let,
64 Const,
65}
66
67#[derive(Debug, Clone, Copy, PartialEq, Eq)]
69pub enum UpdateOp {
70 Inc, Dec, }
73
74#[derive(Debug, Clone, PartialEq)]
76pub enum Prop {
77 KeyValue {
79 key: Expr,
80 value: Expr,
81 computed: bool,
82 },
83 Spread(Expr),
85}
86
87#[derive(Debug, Clone, PartialEq)]
89pub enum Expr {
90 Null,
91 Undefined,
92 True,
93 False,
94 Number(f64),
95 Str(String),
96 Template {
99 quasis: Vec<String>,
100 exprs: Vec<Expr>,
101 },
102
103 Ident(String),
105 This,
107
108 Array(Vec<Expr>),
109 Object(Vec<Prop>),
110 Spread(Box<Expr>),
112
113 Logical(LogicalOp, Box<Expr>, Box<Expr>),
114 Unary(UnOp, Box<Expr>),
115 Binary(BinOp, Box<Expr>, Box<Expr>),
116
117 Conditional {
119 test: Box<Expr>,
120 cons: Box<Expr>,
121 alt: Box<Expr>,
122 },
123
124 Assign {
126 target: Box<Expr>,
127 value: Box<Expr>,
128 },
129 Update {
131 op: UpdateOp,
132 prefix: bool,
133 target: Box<Expr>,
134 },
135
136 Call {
138 func: Box<Expr>,
139 args: Vec<Expr>,
140 optional: bool,
141 },
142 New {
144 callee: Box<Expr>,
145 args: Vec<Expr>,
146 },
147 Member {
149 object: Box<Expr>,
150 property: String,
151 optional: bool,
152 },
153 Index {
155 object: Box<Expr>,
156 index: Box<Expr>,
157 optional: bool,
158 },
159
160 Function {
162 params: Vec<Param>,
163 body: FnBody,
164 is_arrow: bool,
165 name: Option<String>,
166 },
167
168 Sequence(Vec<Expr>),
170}
171
172#[derive(Debug, Clone, PartialEq)]
175pub enum FnBody {
176 Block(Vec<Stmt>),
177 Expr(Box<Expr>),
178}
179
180#[derive(Debug, Clone, PartialEq)]
182pub struct Param {
183 pub pattern: Expr,
186 pub default: Option<Expr>,
188 pub rest: bool,
190}
191
192#[derive(Debug, Clone, PartialEq)]
194pub struct SwitchCase {
195 pub test: Option<Expr>,
197 pub body: Vec<Stmt>,
198}
199
200#[derive(Debug, Clone, PartialEq)]
202pub struct Declarator {
203 pub target: Expr,
204 pub init: Option<Expr>,
205}
206
207#[derive(Debug, Clone, PartialEq)]
209pub enum StmtKind {
210 Expr(Expr),
212 Decl {
214 kind: DeclKind,
215 decls: Vec<Declarator>,
216 },
217 Block(Vec<Stmt>),
219 FuncDecl {
221 name: String,
222 params: Vec<Param>,
223 body: Vec<Stmt>,
224 },
225
226 If {
227 test: Expr,
228 cons: Box<Stmt>,
229 alt: Option<Box<Stmt>>,
230 },
231 While {
232 test: Expr,
233 body: Box<Stmt>,
234 },
235 DoWhile {
236 body: Box<Stmt>,
237 test: Expr,
238 },
239 For {
241 init: Option<Box<Stmt>>,
242 test: Option<Expr>,
243 update: Option<Expr>,
244 body: Box<Stmt>,
245 },
246 ForOf {
248 decl_kind: Option<DeclKind>,
249 target: Expr,
250 iter: Expr,
251 body: Box<Stmt>,
252 },
253 ForIn {
255 decl_kind: Option<DeclKind>,
256 target: Expr,
257 object: Expr,
258 body: Box<Stmt>,
259 },
260 Switch {
261 disc: Expr,
262 cases: Vec<SwitchCase>,
263 },
264
265 Return(Option<Expr>),
266 Break(Option<String>),
267 Continue(Option<String>),
268 Throw(Expr),
269 Try {
270 block: Vec<Stmt>,
271 handler: Option<(Option<Expr>, Vec<Stmt>)>, finalizer: Option<Vec<Stmt>>,
273 },
274
275 Empty,
276}
277
278#[derive(Debug, Clone, PartialEq)]
280pub struct Stmt {
281 pub kind: StmtKind,
282 pub line: u32,
283}
284
285impl Stmt {
286 pub fn new(kind: StmtKind, line: u32) -> Stmt {
287 Stmt { kind, line }
288 }
289}
290
291impl From<StmtKind> for Stmt {
292 fn from(kind: StmtKind) -> Stmt {
294 Stmt { kind, line: 0 }
295 }
296}