Skip to main content

pipa/compiler/
ast.rs

1#[derive(Debug, Clone, Copy, Default)]
2pub struct SourceLocation {
3    pub start: u32,
4    pub end: u32,
5}
6
7#[derive(Debug, Clone)]
8pub struct Program {
9    pub body: Vec<ASTNode>,
10    pub lines: Vec<u32>,
11    pub source_type: SourceType,
12}
13
14#[derive(Debug, Clone, Copy, PartialEq)]
15pub enum SourceType {
16    Script,
17    Module,
18}
19
20#[derive(Debug, Clone)]
21pub enum ASTNode {
22    BlockStatement(BlockStatement),
23    VariableDeclaration(VariableDeclaration),
24    FunctionDeclaration(FunctionDeclaration),
25    ClassDeclaration(ClassDeclaration),
26    IfStatement(IfStatement),
27    ForStatement(ForStatement),
28    ForInStatement(ForInStatement),
29    ForOfStatement(ForOfStatement),
30    WhileStatement(WhileStatement),
31    DoWhileStatement(DoWhileStatement),
32    SwitchStatement(SwitchStatement),
33    TryStatement(TryStatement),
34    WithStatement(WithStatement),
35    ReturnStatement(ReturnStatement),
36    ThrowStatement(ThrowStatement),
37    BreakStatement(BreakStatement),
38    ContinueStatement(ContinueStatement),
39    LabelledStatement(LabelledStatement),
40    DebuggerStatement,
41    EmptyStatement,
42
43    ImportDeclaration(ImportDeclaration),
44    ExportNamedDeclaration(ExportNamedDeclaration),
45    ExportDefaultDeclaration(ExportDefaultDeclaration),
46    ExportAllDeclaration(ExportAllDeclaration),
47
48    ExpressionStatement(ExpressionStatement),
49}
50
51#[derive(Debug, Clone)]
52pub enum Expression {
53    Identifier(Identifier),
54    PrivateIdentifier(String),
55    Literal(Literal),
56    This,
57    Super,
58    ArrayExpression(ArrayExpression),
59    ObjectExpression(ObjectExpression),
60    FunctionExpression(FunctionExpression),
61    ClassExpression(ClassExpression),
62    TemplateLiteral(TemplateLiteral),
63    RegExpLiteral(RegExpLiteral),
64
65    UnaryExpression(UnaryExpression),
66    UpdateExpression(UpdateExpression),
67    BinaryExpression(BinaryExpression),
68    LogicalExpression(LogicalExpression),
69    ConditionalExpression(ConditionalExpression),
70    AssignmentExpression(AssignmentExpression),
71    SequenceExpression(SequenceExpression),
72
73    MemberExpression(MemberExpression),
74    CallExpression(CallExpression),
75    NewExpression(NewExpression),
76    OptionalMemberExpression(OptionalMemberExpression),
77    OptionalCallExpression(OptionalCallExpression),
78
79    ArrowFunction(ArrowFunction),
80    AwaitExpression(AwaitExpression),
81    YieldExpression(YieldExpression),
82    SpreadElement(SpreadElement),
83    TaggedTemplateExpression(TaggedTemplateExpression),
84    MetaProperty(MetaProperty),
85
86    ArrayPattern(ArrayPattern),
87    ObjectPattern(ObjectPattern),
88    RestElement(RestElement),
89    AssignmentPattern(AssignmentPattern),
90}
91
92#[derive(Debug, Clone)]
93pub struct Identifier {
94    pub name: String,
95}
96
97#[derive(Debug, Clone)]
98pub enum Literal {
99    Number(f64),
100    String(String, bool),
101    Boolean(bool),
102    Null,
103    Undefined,
104    BigInt(String),
105    LegacyOctal(i64),
106}
107
108#[derive(Debug, Clone)]
109pub struct ArrayExpression {
110    pub elements: Vec<Option<ArrayElement>>,
111}
112
113#[derive(Debug, Clone)]
114pub enum ArrayElement {
115    Expression(Expression),
116    Spread(Expression),
117}
118
119#[derive(Debug, Clone)]
120pub struct ObjectExpression {
121    pub properties: Vec<Property>,
122}
123
124#[derive(Debug, Clone)]
125pub enum Property {
126    Property {
127        key: PropertyKey,
128        value: Box<Expression>,
129        computed: bool,
130        shorthand: bool,
131        method: bool,
132        getter: bool,
133        setter: bool,
134    },
135
136    SpreadElement(Expression),
137}
138
139#[derive(Debug, Clone)]
140pub enum PropertyKey {
141    Identifier(String),
142    Literal(Literal),
143    Computed(Box<Expression>),
144    PrivateIdentifier(String),
145}
146
147#[derive(Debug, Clone)]
148pub struct FunctionExpression {
149    pub name: Option<String>,
150    pub params: Vec<Parameter>,
151    pub body: BlockStatement,
152    pub generator: bool,
153    pub is_async: bool,
154}
155
156#[derive(Debug, Clone)]
157pub struct ClassExpression {
158    pub name: Option<String>,
159    pub super_class: Option<Box<Expression>>,
160    pub body: ClassBody,
161}
162
163#[derive(Debug, Clone)]
164pub struct TemplateLiteral {
165    pub quasis: Vec<TemplateElement>,
166    pub expressions: Vec<Expression>,
167}
168
169#[derive(Debug, Clone)]
170pub struct TemplateElement {
171    pub value: String,
172    pub tail: bool,
173}
174
175#[derive(Debug, Clone)]
176pub struct RegExpLiteral {
177    pub pattern: String,
178    pub flags: String,
179}
180
181#[derive(Debug, Clone)]
182pub struct UnaryExpression {
183    pub op: UnaryOp,
184    pub argument: Box<Expression>,
185    pub prefix: bool,
186}
187
188#[derive(Debug, Clone, PartialEq)]
189pub enum UnaryOp {
190    Minus,
191    Plus,
192    Not,
193    BitNot,
194    TypeOf,
195    Void,
196    Delete,
197}
198
199#[derive(Debug, Clone)]
200pub struct UpdateExpression {
201    pub op: UpdateOp,
202    pub argument: Box<Expression>,
203    pub prefix: bool,
204}
205
206#[derive(Debug, Clone, PartialEq)]
207pub enum UpdateOp {
208    Increment,
209    Decrement,
210}
211
212#[derive(Debug, Clone)]
213pub struct BinaryExpression {
214    pub op: BinaryOp,
215    pub left: Box<Expression>,
216    pub right: Box<Expression>,
217}
218
219#[derive(Debug, Clone, PartialEq)]
220pub enum BinaryOp {
221    Add,
222    Sub,
223    Mul,
224    Div,
225    Mod,
226    Pow,
227    Eq,
228    Neq,
229    StrictEq,
230    StrictNeq,
231    Lt,
232    Lte,
233    Gt,
234    Gte,
235    BitAnd,
236    BitOr,
237    BitXor,
238    Shl,
239    Shr,
240    UShr,
241    In,
242    InstanceOf,
243}
244
245#[derive(Debug, Clone)]
246pub struct LogicalExpression {
247    pub op: LogicalOp,
248    pub left: Box<Expression>,
249    pub right: Box<Expression>,
250}
251
252#[derive(Debug, Clone)]
253pub enum LogicalOp {
254    And,
255    Or,
256    NullishCoalescing,
257}
258
259#[derive(Debug, Clone)]
260pub struct ConditionalExpression {
261    pub test: Box<Expression>,
262    pub consequent: Box<Expression>,
263    pub alternate: Box<Expression>,
264}
265
266#[derive(Debug, Clone)]
267pub struct AssignmentExpression {
268    pub op: AssignOp,
269    pub left: AssignmentTarget,
270    pub right: Box<Expression>,
271}
272
273#[derive(Debug, Clone)]
274pub enum AssignOp {
275    Assign,
276    AddAssign,
277    SubAssign,
278    MulAssign,
279    DivAssign,
280    ModAssign,
281    PowAssign,
282    BitAndAssign,
283    BitOrAssign,
284    BitXorAssign,
285    ShlAssign,
286    ShrAssign,
287    UShrAssign,
288    LogicalAndAssign,
289    LogicalOrAssign,
290    NullishAssign,
291}
292
293#[derive(Debug, Clone)]
294pub enum AssignmentTarget {
295    Identifier(String),
296    MemberExpression(MemberExpression),
297    OptionalMemberExpression(OptionalMemberExpression),
298    ComputedMember(Box<Expression>, Box<Expression>),
299    ObjectPattern(ObjectPattern),
300    ArrayPattern(ArrayPattern),
301    RestElement(RestElement),
302    AssignmentPattern(AssignmentPattern),
303}
304
305#[derive(Debug, Clone)]
306pub struct SequenceExpression {
307    pub expressions: Vec<Expression>,
308}
309
310#[derive(Debug, Clone)]
311pub struct MemberExpression {
312    pub object: Box<Expression>,
313    pub property: MemberProperty,
314    pub computed: bool,
315}
316
317#[derive(Debug, Clone)]
318pub enum MemberProperty {
319    Identifier(String),
320    Computed(Box<Expression>),
321    PrivateIdentifier(String),
322}
323
324#[derive(Debug, Clone)]
325pub struct OptionalMemberExpression {
326    pub object: Box<Expression>,
327    pub property: MemberProperty,
328    pub computed: bool,
329    pub optional: bool,
330}
331
332#[derive(Debug, Clone)]
333pub struct CallExpression {
334    pub callee: Box<Expression>,
335    pub arguments: Vec<Argument>,
336}
337
338#[derive(Debug, Clone)]
339pub enum Argument {
340    Expression(Expression),
341    Spread(Expression),
342}
343
344#[derive(Debug, Clone)]
345pub struct OptionalCallExpression {
346    pub callee: Box<Expression>,
347    pub arguments: Vec<Argument>,
348    pub optional: bool,
349}
350
351#[derive(Debug, Clone)]
352pub struct NewExpression {
353    pub callee: Box<Expression>,
354    pub arguments: Vec<Argument>,
355}
356
357#[derive(Debug, Clone)]
358pub struct ArrowFunction {
359    pub params: Vec<Parameter>,
360    pub body: ArrowBody,
361    pub is_async: bool,
362}
363
364#[derive(Debug, Clone)]
365pub enum ArrowBody {
366    Expression(Box<Expression>),
367    Block(BlockStatement),
368}
369
370#[derive(Debug, Clone)]
371pub struct AwaitExpression {
372    pub argument: Box<Expression>,
373}
374
375#[derive(Debug, Clone)]
376pub struct YieldExpression {
377    pub argument: Option<Box<Expression>>,
378    pub delegate: bool,
379}
380
381#[derive(Debug, Clone)]
382pub struct SpreadElement {
383    pub argument: Box<Expression>,
384}
385
386#[derive(Debug, Clone)]
387pub struct TaggedTemplateExpression {
388    pub tag: Box<Expression>,
389    pub quasi: TemplateLiteral,
390}
391
392#[derive(Debug, Clone)]
393pub struct MetaProperty {
394    pub meta: String,
395    pub property: String,
396}
397
398#[derive(Debug, Clone)]
399pub struct ArrayPattern {
400    pub elements: Vec<Option<PatternElement>>,
401}
402
403#[derive(Debug, Clone)]
404pub enum PatternElement {
405    Pattern(AssignmentTarget),
406    RestElement(RestElement),
407    AssignmentPattern(AssignmentPattern),
408}
409
410#[derive(Debug, Clone)]
411pub struct ObjectPattern {
412    pub properties: Vec<ObjectPatternProperty>,
413}
414
415#[derive(Debug, Clone)]
416pub enum ObjectPatternProperty {
417    Property {
418        key: PropertyKey,
419        value: AssignmentTarget,
420        computed: bool,
421        shorthand: bool,
422    },
423    RestElement(RestElement),
424}
425
426#[derive(Debug, Clone)]
427pub struct RestElement {
428    pub argument: Box<AssignmentTarget>,
429}
430
431#[derive(Debug, Clone)]
432pub struct AssignmentPattern {
433    pub left: Box<AssignmentTarget>,
434    pub right: Box<Expression>,
435}
436
437#[derive(Debug, Clone)]
438pub enum Parameter {
439    Identifier(String),
440    Pattern(BindingPattern),
441    AssignmentPattern(AssignmentPattern),
442    RestElement(RestElement),
443}
444
445#[derive(Debug, Clone)]
446pub struct ExpressionStatement {
447    pub expression: Expression,
448}
449
450#[derive(Debug, Clone)]
451pub struct BlockStatement {
452    pub body: Vec<ASTNode>,
453    pub lines: Vec<u32>,
454}
455
456#[derive(Debug, Clone)]
457pub struct VariableDeclaration {
458    pub kind: VariableKind,
459    pub declarations: Vec<VariableDeclarator>,
460}
461
462#[derive(Debug, Clone, Copy, PartialEq)]
463pub enum VariableKind {
464    Var,
465    Let,
466    Const,
467}
468
469#[derive(Debug, Clone)]
470pub struct VariableDeclarator {
471    pub id: BindingPattern,
472    pub init: Option<Expression>,
473}
474
475#[derive(Debug, Clone)]
476pub enum BindingPattern {
477    Identifier(String),
478    ArrayPattern(ArrayPattern),
479    ObjectPattern(ObjectPattern),
480    AssignmentPattern(AssignmentPattern),
481}
482
483#[derive(Debug, Clone)]
484pub struct FunctionDeclaration {
485    pub name: String,
486    pub params: Vec<Parameter>,
487    pub body: BlockStatement,
488    pub generator: bool,
489    pub is_async: bool,
490}
491
492#[derive(Debug, Clone)]
493pub struct ClassDeclaration {
494    pub name: String,
495    pub super_class: Option<Box<Expression>>,
496    pub body: ClassBody,
497}
498
499#[derive(Debug, Clone)]
500pub struct ClassBody {
501    pub elements: Vec<ClassElement>,
502}
503
504#[derive(Debug, Clone)]
505pub enum ClassElement {
506    Method(ClassMethod),
507    StaticBlock(StaticBlock),
508    Property(ClassProperty),
509}
510
511#[derive(Debug, Clone)]
512pub struct ClassMethod {
513    pub name: PropertyKey,
514    pub params: Vec<Parameter>,
515    pub body: BlockStatement,
516    pub kind: MethodKind,
517    pub is_async: bool,
518    pub generator: bool,
519    pub is_static: bool,
520    pub is_private: bool,
521}
522
523#[derive(Debug, Clone, PartialEq)]
524pub enum MethodKind {
525    Constructor,
526    Method,
527    Get,
528    Set,
529}
530
531#[derive(Debug, Clone)]
532pub struct ClassProperty {
533    pub name: PropertyKey,
534    pub value: Option<Expression>,
535    pub is_static: bool,
536    pub is_private: bool,
537}
538
539#[derive(Debug, Clone)]
540pub struct StaticBlock {
541    pub body: Vec<ASTNode>,
542    pub lines: Vec<u32>,
543}
544
545#[derive(Debug, Clone)]
546pub struct IfStatement {
547    pub test: Expression,
548    pub consequent: Box<ASTNode>,
549    pub alternate: Option<Box<ASTNode>>,
550}
551
552#[derive(Debug, Clone)]
553pub struct ForStatement {
554    pub init: Option<ForInit>,
555    pub test: Option<Expression>,
556    pub update: Option<Expression>,
557    pub body: Box<ASTNode>,
558}
559
560#[derive(Debug, Clone)]
561pub enum ForInit {
562    VariableDeclaration(VariableDeclaration),
563    Expression(Expression),
564}
565
566#[derive(Debug, Clone)]
567pub struct ForInStatement {
568    pub left: ForInOfLeft,
569    pub right: Expression,
570    pub body: Box<ASTNode>,
571}
572
573#[derive(Debug, Clone)]
574pub struct ForOfStatement {
575    pub left: ForInOfLeft,
576    pub right: Expression,
577    pub body: Box<ASTNode>,
578    pub is_await: bool,
579}
580
581#[derive(Debug, Clone)]
582pub enum ForInOfLeft {
583    VariableDeclaration(VariableDeclaration),
584    AssignmentTarget(AssignmentTarget),
585}
586
587#[derive(Debug, Clone)]
588pub struct WhileStatement {
589    pub test: Expression,
590    pub body: Box<ASTNode>,
591}
592
593#[derive(Debug, Clone)]
594pub struct DoWhileStatement {
595    pub body: Box<ASTNode>,
596    pub test: Expression,
597}
598
599#[derive(Debug, Clone)]
600pub struct SwitchStatement {
601    pub discriminant: Expression,
602    pub cases: Vec<SwitchCase>,
603}
604
605#[derive(Debug, Clone)]
606pub struct SwitchCase {
607    pub test: Option<Expression>,
608    pub consequent: Vec<ASTNode>,
609}
610
611#[derive(Debug, Clone)]
612pub struct TryStatement {
613    pub block: BlockStatement,
614    pub handler: Option<CatchClause>,
615    pub finalizer: Option<BlockStatement>,
616}
617
618#[derive(Debug, Clone)]
619pub struct CatchClause {
620    pub param: Option<BindingPattern>,
621    pub body: BlockStatement,
622}
623
624#[derive(Debug, Clone)]
625pub struct WithStatement {
626    pub object: Expression,
627    pub body: Box<ASTNode>,
628}
629
630#[derive(Debug, Clone)]
631pub struct ReturnStatement {
632    pub argument: Option<Expression>,
633}
634
635#[derive(Debug, Clone)]
636pub struct ThrowStatement {
637    pub argument: Expression,
638}
639
640#[derive(Debug, Clone)]
641pub struct BreakStatement {
642    pub label: Option<String>,
643}
644
645#[derive(Debug, Clone)]
646pub struct ContinueStatement {
647    pub label: Option<String>,
648}
649
650#[derive(Debug, Clone)]
651pub struct LabelledStatement {
652    pub label: String,
653    pub body: Box<ASTNode>,
654}
655
656#[derive(Debug, Clone)]
657pub struct ImportDeclaration {
658    pub specifiers: Vec<ImportSpecifier>,
659    pub source: String,
660}
661
662#[derive(Debug, Clone)]
663pub enum ImportSpecifier {
664    Named { imported: String, local: String },
665
666    Default { local: String },
667
668    Namespace { local: String },
669}
670
671#[derive(Debug, Clone)]
672pub struct ExportNamedDeclaration {
673    pub declaration: Option<ExportDeclaration>,
674    pub specifiers: Vec<ExportSpecifier>,
675    pub source: Option<String>,
676}
677
678#[derive(Debug, Clone)]
679pub enum ExportDeclaration {
680    Variable(VariableDeclaration),
681    Function(FunctionDeclaration),
682    Class(ClassDeclaration),
683}
684
685#[derive(Debug, Clone)]
686pub struct ExportSpecifier {
687    pub exported: String,
688    pub local: String,
689}
690
691#[derive(Debug, Clone)]
692pub struct ExportDefaultDeclaration {
693    pub declaration: ExportDefaultDeclarationKind,
694}
695
696#[derive(Debug, Clone)]
697pub enum ExportDefaultDeclarationKind {
698    Function(FunctionDeclaration),
699    Class(ClassDeclaration),
700    Expression(Expression),
701}
702
703#[derive(Debug, Clone)]
704pub struct ExportAllDeclaration {
705    pub source: String,
706    pub exported: Option<String>,
707}