Skip to main content

strata_js/engine/
ast.rs

1//! Abstract Syntax Tree (AST) definitions for JavaScript parsing and evaluation.
2
3use crate::JsValue;
4use std::collections::HashMap;
5
6#[derive(Debug, Clone, PartialEq)]
7pub enum BinaryOp {
8    Add,
9    Sub,
10    Mul,
11    Div,
12    Mod,
13    Eq,
14    StrictEq,
15    NotEq,
16    StrictNotEq,
17    Lt,
18    Lte,
19    Gt,
20    Gte,
21    And,
22    Or,
23    NullishCoalescing,
24    InstanceOf,
25    Power,
26    BitAnd,
27    BitOr,
28    BitXor,
29    Shl,
30    Shr,
31}
32
33#[derive(Debug, Clone, PartialEq)]
34pub enum UnaryOp {
35    Not,
36    Neg,
37    Pos,
38    BitNot,
39    Typeof,
40}
41
42#[derive(Debug, Clone, Copy, PartialEq, Eq)]
43pub enum UpdateOp {
44    Increment,
45    Decrement,
46}
47
48#[derive(Debug, Clone, PartialEq)]
49pub enum TemplatePart {
50    Literal(String),
51    Expr(Expr),
52}
53
54#[derive(Debug, Clone, PartialEq)]
55pub enum ArrowBody {
56    Expr(Box<Expr>),
57    Block(Vec<Stmt>),
58}
59
60#[derive(Debug, Clone, PartialEq)]
61pub enum Expr {
62    Literal(JsValue),
63    Identifier(String),
64    Assign {
65        target: Box<Expr>,
66        value: Box<Expr>,
67    },
68    Update {
69        op: UpdateOp,
70        target: Box<Expr>,
71        prefix: bool,
72    },
73    Binary {
74        op: BinaryOp,
75        left: Box<Expr>,
76        right: Box<Expr>,
77    },
78    Unary {
79        op: UnaryOp,
80        expr: Box<Expr>,
81    },
82    Member {
83        object: Box<Expr>,
84        property: String,
85        computed: Option<Box<Expr>>,
86    },
87    OptionalMember {
88        object: Box<Expr>,
89        property: String,
90        computed: Option<Box<Expr>>,
91    },
92    Call {
93        callee: Box<Expr>,
94        args: Vec<Expr>,
95    },
96    OptionalCall {
97        callee: Box<Expr>,
98        args: Vec<Expr>,
99    },
100    New {
101        callee: Box<Expr>,
102        args: Vec<Expr>,
103    },
104    Spread(Box<Expr>),
105    Array(Vec<Expr>),
106    Object(Vec<(String, Expr)>),
107    TemplateLiteral(Vec<TemplatePart>),
108    ArrowFunction {
109        params: Vec<String>,
110        body: ArrowBody,
111        is_async: bool,
112    },
113    Await(Box<Expr>),
114    RegExpLiteral {
115        pattern: String,
116        flags: String,
117    },
118    Yield(Option<Box<Expr>>),
119}
120
121#[derive(Debug, Clone, PartialEq)]
122pub enum StatementFlow {
123    Normal(JsValue),
124    Return(JsValue),
125    Throw(JsValue),
126    Break(Option<String>),
127    Continue(Option<String>),
128    Yield(JsValue),
129}
130
131#[derive(Debug, Clone, PartialEq)]
132pub struct ClassDef {
133    pub name: String,
134    pub super_class: Option<String>,
135    pub constructor: Option<(Vec<String>, Vec<Stmt>)>,
136    pub methods: HashMap<String, (Vec<String>, Vec<Stmt>)>,
137}
138
139#[derive(Debug, Clone, PartialEq)]
140pub struct SwitchCase {
141    pub test: Option<Expr>,
142    pub consequent: Vec<Stmt>,
143}
144
145#[derive(Debug, Clone, PartialEq)]
146pub struct DestructureObjectProp {
147    pub source_key: String,
148    pub target_name: String,
149    pub default_value: Option<Expr>,
150}
151
152#[derive(Debug, Clone, PartialEq)]
153pub struct DestructureArrayElem {
154    pub target_name: String,
155    pub default_value: Option<Expr>,
156}
157
158#[derive(Debug, Clone, PartialEq)]
159pub enum DestructurePattern {
160    Object {
161        properties: Vec<DestructureObjectProp>,
162        rest: Option<String>,
163    },
164    Array {
165        elements: Vec<Option<DestructureArrayElem>>,
166        rest: Option<String>,
167    },
168}
169
170#[derive(Debug, Clone, PartialEq)]
171pub enum Stmt {
172    VarDecl {
173        name: String,
174        init: Option<Expr>,
175    },
176    DestructureDecl {
177        pattern: DestructurePattern,
178        init: Expr,
179    },
180    Assign {
181        target: Expr,
182        value: Expr,
183    },
184    Expr(Expr),
185    Block(Vec<Stmt>),
186    If {
187        cond: Expr,
188        then_branch: Box<Stmt>,
189        else_branch: Option<Box<Stmt>>,
190    },
191    While {
192        cond: Expr,
193        body: Box<Stmt>,
194    },
195    For {
196        init: Option<Box<Stmt>>,
197        cond: Option<Expr>,
198        step: Option<Expr>,
199        body: Box<Stmt>,
200    },
201    Directive(String),
202    FunctionDecl {
203        name: String,
204        params: Vec<String>,
205        body: Vec<Stmt>,
206        is_async: bool,
207        is_generator: bool,
208    },
209    ClassDecl {
210        name: String,
211        super_class: Option<String>,
212        constructor: Option<(Vec<String>, Vec<Stmt>)>,
213        methods: Vec<(String, Vec<String>, Vec<Stmt>)>,
214    },
215    Return(Option<Expr>),
216    TryCatch {
217        try_block: Vec<Stmt>,
218        catch_param: Option<String>,
219        catch_block: Option<Vec<Stmt>>,
220        finally_block: Option<Vec<Stmt>>,
221    },
222    Throw(Expr),
223    Break(Option<String>),
224    Continue(Option<String>),
225    Switch {
226        discriminant: Expr,
227        cases: Vec<SwitchCase>,
228    },
229}