1use serde::{Deserialize, Serialize};
4
5#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Program {
8 pub statements: Vec<Statement>,
9}
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub enum Statement {
14 VariableDeclaration(VariableDeclaration),
15 FunctionDeclaration(FunctionDeclaration),
16 ClassDeclaration(ClassDeclaration),
17 InterfaceDeclaration(InterfaceDeclaration),
18 TypeAlias(TypeAlias),
19 EnumDeclaration(EnumDeclaration),
20 ImportDeclaration(ImportDeclaration),
21 ExportDeclaration(Box<ExportDeclaration>),
22 NamespaceDeclaration(NamespaceDeclaration),
23 ModuleDeclaration(ModuleDeclaration),
24 DeclareStatement(Box<DeclareStatement>),
25 BlockStatement(BlockStatement),
26 ExpressionStatement(ExpressionStatement),
27 IfStatement(Box<IfStatement>),
28 WhileStatement(WhileStatement),
29 ForStatement(ForStatement),
30 ReturnStatement(ReturnStatement),
31 BreakStatement(BreakStatement),
32 ContinueStatement(ContinueStatement),
33 ThrowStatement(ThrowStatement),
34 TryStatement(Box<TryStatement>),
35 SwitchStatement(SwitchStatement),
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
40pub struct VariableDeclaration {
41 pub keyword: crate::lexer::Keyword,
42 pub name: String,
43 pub type_annotation: Option<Type>,
44 pub initializer: Option<Expression>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct FunctionDeclaration {
50 pub name: String,
51 pub type_parameters: Vec<TypeParameter>,
52 pub parameters: Vec<Parameter>,
53 pub return_type: Option<Type>,
54 pub body: Box<Statement>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize)]
59pub struct ClassDeclaration {
60 pub name: String,
61 pub type_parameters: Vec<TypeParameter>,
62 pub extends: Option<Type>,
63 pub implements: Vec<Type>,
64 pub body: ClassBody,
65}
66
67#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct InterfaceDeclaration {
70 pub name: String,
71 pub type_parameters: Vec<TypeParameter>,
72 pub extends: Vec<Type>,
73 pub body: InterfaceBody,
74}
75
76#[derive(Debug, Clone, Serialize, Deserialize)]
78pub struct TypeAlias {
79 pub name: String,
80 pub type_parameters: Vec<TypeParameter>,
81 pub type_definition: Type,
82}
83
84#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct EnumDeclaration {
87 pub name: String,
88 pub members: Vec<EnumMember>,
89}
90
91#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct ImportDeclaration {
94 pub specifiers: Vec<ImportSpecifier>,
95 pub source: String,
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct ExportDeclaration {
101 pub declaration: Box<Statement>,
102}
103
104#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct NamespaceDeclaration {
107 pub name: String,
108 pub body: Box<Statement>,
109}
110
111#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct ModuleDeclaration {
114 pub name: String,
115 pub body: Box<Statement>,
116}
117
118#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct DeclareStatement {
121 pub declaration: Box<Statement>,
122}
123
124#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct BlockStatement {
127 pub statements: Vec<Statement>,
128}
129
130#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct ExpressionStatement {
133 pub expression: Expression,
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
138pub struct IfStatement {
139 pub condition: Expression,
140 pub consequent: Box<Statement>,
141 pub alternate: Option<Statement>,
142}
143
144#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct WhileStatement {
147 pub condition: Expression,
148 pub body: Box<Statement>,
149}
150
151#[derive(Debug, Clone, Serialize, Deserialize)]
153pub struct ForStatement {
154 pub init: Option<Expression>,
155 pub condition: Option<Expression>,
156 pub update: Option<Expression>,
157 pub body: Box<Statement>,
158}
159
160#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct ReturnStatement {
163 pub argument: Option<Expression>,
164}
165
166#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct BreakStatement {
169 pub label: Option<String>,
170}
171
172#[derive(Debug, Clone, Serialize, Deserialize)]
174pub struct ContinueStatement {
175 pub label: Option<String>,
176}
177
178#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct ThrowStatement {
181 pub argument: Expression,
182}
183
184#[derive(Debug, Clone, Serialize, Deserialize)]
186pub struct TryStatement {
187 pub block: Box<Statement>,
188 pub handler: Option<CatchClause>,
189 pub finalizer: Option<Statement>,
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct SwitchStatement {
195 pub discriminant: Expression,
196 pub cases: Vec<SwitchCase>,
197}
198
199#[derive(Debug, Clone, Serialize, Deserialize)]
201pub enum Expression {
202 Literal(Literal),
203 Identifier(String),
204 Binary(BinaryExpression),
205 Unary(UnaryExpression),
206 Logical(LogicalExpression),
207 Conditional(ConditionalExpression),
208 Assignment(AssignmentExpression),
209 Call(CallExpression),
210 Member(MemberExpression),
211 Array(ArrayExpression),
212 Object(ObjectExpression),
213 Parenthesized(ParenthesizedExpression),
214 Arrow(Box<ArrowFunctionExpression>),
215 New(NewExpression),
216 Super(SuperExpression),
217 This(ThisExpression),
218 Yield(Box<YieldExpression>),
219 Await(AwaitExpression),
220 TypeAssertion(TypeAssertion),
221 AsExpression(AsExpression),
222 NonNull(NonNullExpression),
223 Optional(OptionalExpression),
224 Template(TemplateLiteral),
225 TaggedTemplate(TaggedTemplateExpression),
226}
227
228#[derive(Debug, Clone, Serialize, Deserialize)]
230pub enum Literal {
231 String(String),
232 Number(f64),
233 Boolean(bool),
234 Null,
235 Undefined,
236 RegExp(String, String), BigInt(String),
238}
239
240#[derive(Debug, Clone, Serialize, Deserialize)]
242pub struct BinaryExpression {
243 pub left: Box<Expression>,
244 pub operator: crate::lexer::Token,
245 pub right: Box<Expression>,
246}
247
248#[derive(Debug, Clone, Serialize, Deserialize)]
250pub struct UnaryExpression {
251 pub operator: crate::lexer::Token,
252 pub argument: Box<Expression>,
253}
254
255#[derive(Debug, Clone, Serialize, Deserialize)]
257pub struct LogicalExpression {
258 pub left: Box<Expression>,
259 pub operator: crate::lexer::Token,
260 pub right: Box<Expression>,
261}
262
263#[derive(Debug, Clone, Serialize, Deserialize)]
265pub struct ConditionalExpression {
266 pub test: Box<Expression>,
267 pub consequent: Box<Expression>,
268 pub alternate: Box<Expression>,
269}
270
271#[derive(Debug, Clone, Serialize, Deserialize)]
273pub struct AssignmentExpression {
274 pub left: Box<Expression>,
275 pub operator: crate::lexer::Token,
276 pub right: Box<Expression>,
277}
278
279#[derive(Debug, Clone, Serialize, Deserialize)]
281pub struct CallExpression {
282 pub callee: Box<Expression>,
283 pub arguments: Vec<Expression>,
284}
285
286#[derive(Debug, Clone, Serialize, Deserialize)]
288pub struct MemberExpression {
289 pub object: Box<Expression>,
290 pub property: Box<Expression>,
291 pub computed: bool,
292}
293
294#[derive(Debug, Clone, Serialize, Deserialize)]
296pub struct ArrayExpression {
297 pub elements: Vec<Option<Expression>>,
298}
299
300#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct ObjectExpression {
303 pub properties: Vec<ObjectProperty>,
304}
305
306#[derive(Debug, Clone, Serialize, Deserialize)]
308pub struct ObjectProperty {
309 pub key: Expression,
310 pub value: Expression,
311 pub shorthand: bool,
312 pub computed: bool,
313 pub method: bool,
314}
315
316#[derive(Debug, Clone, Serialize, Deserialize)]
318pub struct ParenthesizedExpression {
319 pub expression: Box<Expression>,
320}
321
322#[derive(Debug, Clone, Serialize, Deserialize)]
324pub struct ArrowFunctionExpression {
325 pub type_parameters: Vec<TypeParameter>,
326 pub parameters: Vec<Parameter>,
327 pub return_type: Option<Box<Type>>,
328 pub body: Box<Statement>,
329}
330
331#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct NewExpression {
334 pub callee: Box<Expression>,
335 pub arguments: Vec<Expression>,
336}
337
338#[derive(Debug, Clone, Serialize, Deserialize)]
340pub struct SuperExpression;
341
342#[derive(Debug, Clone, Serialize, Deserialize)]
344pub struct ThisExpression;
345
346#[derive(Debug, Clone, Serialize, Deserialize)]
348pub struct YieldExpression {
349 pub argument: Option<Box<Expression>>,
350 pub delegate: bool,
351}
352
353#[derive(Debug, Clone, Serialize, Deserialize)]
355pub struct AwaitExpression {
356 pub argument: Box<Expression>,
357}
358
359#[derive(Debug, Clone, Serialize, Deserialize)]
361pub struct TypeAssertion {
362 pub expression: Box<Expression>,
363 pub type_: Type,
364}
365
366#[derive(Debug, Clone, Serialize, Deserialize)]
368pub struct AsExpression {
369 pub expression: Box<Expression>,
370 pub type_: Type,
371}
372
373#[derive(Debug, Clone, Serialize, Deserialize)]
375pub struct NonNullExpression {
376 pub expression: Box<Expression>,
377}
378
379#[derive(Debug, Clone, Serialize, Deserialize)]
381pub struct OptionalExpression {
382 pub expression: Box<Expression>,
383 pub optional: bool,
384}
385
386#[derive(Debug, Clone, Serialize, Deserialize)]
388pub struct TemplateLiteral {
389 pub quasis: Vec<TemplateElement>,
390 pub expressions: Vec<Expression>,
391}
392
393#[derive(Debug, Clone, Serialize, Deserialize)]
395pub struct TemplateElement {
396 pub value: String,
397 pub tail: bool,
398}
399
400#[derive(Debug, Clone, Serialize, Deserialize)]
402pub struct TaggedTemplateExpression {
403 pub tag: Box<Expression>,
404 pub quasi: TemplateLiteral,
405}
406
407#[derive(Debug, Clone, Serialize, Deserialize)]
409pub enum Type {
410 String,
412 Number,
413 Boolean,
414 Any,
415 Void,
416 Never,
417 Unknown,
418 Null,
419 Undefined,
420 Object,
421 Symbol,
422 BigInt,
423
424 Named(String),
426 Qualified(QualifiedTypeName),
427
428 Generic(GenericType),
430 GenericNamed {
431 name: String,
432 type_parameters: Vec<TypeParameter>,
433 },
434
435 Union(Vec<Type>),
437 Intersection(Vec<Type>),
438
439 Array(Box<Type>),
441 Tuple(Vec<Type>),
442
443 Function(Box<FunctionType>),
445
446 ObjectType(ObjectType),
448
449 IndexSignature(Box<IndexSignature>),
451
452 Mapped(Box<MappedType>),
454
455 Conditional(ConditionalType),
457
458 TemplateLiteral(TemplateLiteralType),
460
461 Parenthesized(Box<Type>),
463
464 TypeQuery(Box<TypeQuery>),
466
467 Import(Box<ImportType>),
469}
470
471#[derive(Debug, Clone, Serialize, Deserialize)]
473pub struct QualifiedTypeName {
474 pub left: Box<Type>,
475 pub right: String,
476}
477
478#[derive(Debug, Clone, Serialize, Deserialize)]
480pub struct GenericType {
481 pub type_: Box<Type>,
482 pub type_arguments: Vec<Type>,
483}
484
485#[derive(Debug, Clone, Serialize, Deserialize)]
487pub struct FunctionType {
488 pub type_parameters: Vec<TypeParameter>,
489 pub parameters: Vec<Parameter>,
490 pub return_type: Box<Type>,
491}
492
493#[derive(Debug, Clone, Serialize, Deserialize)]
495pub struct ObjectType {
496 pub members: Vec<ObjectTypeMember>,
497}
498
499#[derive(Debug, Clone, Serialize, Deserialize)]
501pub enum ObjectTypeMember {
502 Property(PropertySignature),
503 Method(MethodSignature),
504 Index(IndexSignature),
505 Call(CallSignature),
506 Construct(ConstructSignature),
507}
508
509#[derive(Debug, Clone, Serialize, Deserialize)]
511pub struct PropertySignature {
512 pub name: String,
513 pub optional: bool,
514 pub type_: Option<Type>,
515 pub readonly: bool,
516}
517
518#[derive(Debug, Clone, Serialize, Deserialize)]
520pub struct MethodSignature {
521 pub name: String,
522 pub optional: bool,
523 pub type_parameters: Vec<TypeParameter>,
524 pub parameters: Vec<Parameter>,
525 pub return_type: Option<Type>,
526}
527
528#[derive(Debug, Clone, Serialize, Deserialize)]
530pub struct IndexSignature {
531 pub parameter: Box<Parameter>,
532 pub type_: Type,
533 pub readonly: bool,
534}
535
536#[derive(Debug, Clone, Serialize, Deserialize)]
538pub struct CallSignature {
539 pub type_parameters: Vec<TypeParameter>,
540 pub parameters: Vec<Parameter>,
541 pub return_type: Option<Type>,
542}
543
544#[derive(Debug, Clone, Serialize, Deserialize)]
546pub struct ConstructSignature {
547 pub type_parameters: Vec<TypeParameter>,
548 pub parameters: Vec<Parameter>,
549 pub return_type: Option<Type>,
550}
551
552#[derive(Debug, Clone, Serialize, Deserialize)]
554pub struct MappedType {
555 pub type_parameter: Box<TypeParameter>,
556 pub constraint: Option<Box<Type>>,
557 pub name_type: Option<Box<Type>>,
558 pub type_: Box<Type>,
559 pub readonly: Option<bool>,
560 pub optional: Option<bool>,
561}
562
563#[derive(Debug, Clone, Serialize, Deserialize)]
565pub struct ConditionalType {
566 pub check_type: Box<Type>,
567 pub extends_type: Box<Type>,
568 pub true_type: Box<Type>,
569 pub false_type: Box<Type>,
570}
571
572#[derive(Debug, Clone, Serialize, Deserialize)]
574pub struct TemplateLiteralType {
575 pub head: String,
576 pub spans: Vec<TemplateLiteralTypeSpan>,
577}
578
579#[derive(Debug, Clone, Serialize, Deserialize)]
581pub struct TemplateLiteralTypeSpan {
582 pub type_: Type,
583 pub literal: String,
584}
585
586#[derive(Debug, Clone, Serialize, Deserialize)]
588pub struct TypeQuery {
589 pub expr_name: Box<Expression>,
590}
591
592#[derive(Debug, Clone, Serialize, Deserialize)]
594pub struct ImportType {
595 pub argument: Box<Type>,
596 pub qualifier: Option<String>,
597 pub type_arguments: Option<Vec<Type>>,
598}
599
600#[derive(Debug, Clone, Serialize, Deserialize)]
602pub struct TypeParameter {
603 pub name: String,
604 pub constraint: Option<Box<Type>>,
605 pub default: Option<Box<Type>>,
606}
607
608#[derive(Debug, Clone, Serialize, Deserialize)]
610pub struct Parameter {
611 pub name: String,
612 pub optional: bool,
613 pub type_: Option<Box<Type>>,
614 pub initializer: Option<Expression>,
615 pub rest: bool,
616}
617
618#[derive(Debug, Clone, Serialize, Deserialize)]
620pub struct ClassBody {
621 pub members: Vec<ClassMember>,
622}
623
624#[derive(Debug, Clone, Serialize, Deserialize)]
626pub enum ClassMember {
627 Property(PropertyDeclaration),
628 Method(MethodDeclaration),
629 Constructor(ConstructorDeclaration),
630 Getter(GetterDeclaration),
631 Setter(SetterDeclaration),
632 Index(IndexSignature),
633}
634
635#[derive(Debug, Clone, Serialize, Deserialize)]
637pub struct PropertyDeclaration {
638 pub name: String,
639 pub optional: bool,
640 pub type_: Option<Type>,
641 pub initializer: Option<Expression>,
642 pub modifiers: Vec<Modifier>,
643}
644
645#[derive(Debug, Clone, Serialize, Deserialize)]
647pub struct MethodDeclaration {
648 pub name: String,
649 pub optional: bool,
650 pub type_parameters: Vec<TypeParameter>,
651 pub parameters: Vec<Parameter>,
652 pub return_type: Option<Type>,
653 pub body: Option<Statement>,
654 pub modifiers: Vec<Modifier>,
655}
656
657#[derive(Debug, Clone, Serialize, Deserialize)]
659pub struct ConstructorDeclaration {
660 pub parameters: Vec<Parameter>,
661 pub body: Option<Statement>,
662 pub modifiers: Vec<Modifier>,
663}
664
665#[derive(Debug, Clone, Serialize, Deserialize)]
667pub struct GetterDeclaration {
668 pub name: String,
669 pub type_: Option<Type>,
670 pub body: Option<Statement>,
671 pub modifiers: Vec<Modifier>,
672}
673
674#[derive(Debug, Clone, Serialize, Deserialize)]
676pub struct SetterDeclaration {
677 pub name: String,
678 pub parameter: Parameter,
679 pub body: Option<Statement>,
680 pub modifiers: Vec<Modifier>,
681}
682
683#[derive(Debug, Clone, Serialize, Deserialize)]
685pub struct InterfaceBody {
686 pub members: Vec<ObjectTypeMember>,
687}
688
689#[derive(Debug, Clone, Serialize, Deserialize)]
691pub struct EnumMember {
692 pub name: String,
693 pub initializer: Option<Expression>,
694}
695
696#[derive(Debug, Clone, Serialize, Deserialize)]
698pub enum ImportSpecifier {
699 Named(NamedImportSpecifier),
700 Default(DefaultImportSpecifier),
701 Namespace(NamespaceImportSpecifier),
702}
703
704#[derive(Debug, Clone, Serialize, Deserialize)]
706pub struct NamedImportSpecifier {
707 pub name: String,
708 pub imported: String,
709}
710
711#[derive(Debug, Clone, Serialize, Deserialize)]
713pub struct DefaultImportSpecifier {
714 pub name: String,
715}
716
717#[derive(Debug, Clone, Serialize, Deserialize)]
719pub struct NamespaceImportSpecifier {
720 pub name: String,
721}
722
723#[derive(Debug, Clone, Serialize, Deserialize)]
725pub struct CatchClause {
726 pub parameter: Option<Parameter>,
727 pub body: Box<Statement>,
728}
729
730#[derive(Debug, Clone, Serialize, Deserialize)]
732pub struct SwitchCase {
733 pub expression: Option<Expression>,
734 pub statements: Vec<Statement>,
735}
736
737#[derive(Debug, Clone, Serialize, Deserialize)]
739pub enum Modifier {
740 Public,
741 Private,
742 Protected,
743 Static,
744 Readonly,
745 Abstract,
746 Async,
747 Override,
748}
749
750#[derive(Debug, Clone, Serialize, Deserialize)]
752pub struct SourceLocation {
753 pub start: Position,
754 pub end: Position,
755}
756
757#[derive(Debug, Clone, Serialize, Deserialize)]
759pub struct Position {
760 pub line: usize,
761 pub column: usize,
762 pub offset: usize,
763}