TypeScript_Rust_Compiler/
ast.rs

1//! Abstract Syntax Tree (AST) definitions for TypeScript
2
3use serde::{Deserialize, Serialize};
4
5/// Root program node
6#[derive(Debug, Clone, Serialize, Deserialize)]
7pub struct Program {
8    pub statements: Vec<Statement>,
9}
10
11/// Statement types
12#[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/// Variable declaration
39#[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/// Function declaration
48#[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/// Class declaration
58#[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/// Interface declaration
68#[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/// Type alias
77#[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/// Enum declaration
85#[derive(Debug, Clone, Serialize, Deserialize)]
86pub struct EnumDeclaration {
87    pub name: String,
88    pub members: Vec<EnumMember>,
89}
90
91/// Import declaration
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct ImportDeclaration {
94    pub specifiers: Vec<ImportSpecifier>,
95    pub source: String,
96}
97
98/// Export declaration
99#[derive(Debug, Clone, Serialize, Deserialize)]
100pub struct ExportDeclaration {
101    pub declaration: Box<Statement>,
102}
103
104/// Namespace declaration
105#[derive(Debug, Clone, Serialize, Deserialize)]
106pub struct NamespaceDeclaration {
107    pub name: String,
108    pub body: Box<Statement>,
109}
110
111/// Module declaration
112#[derive(Debug, Clone, Serialize, Deserialize)]
113pub struct ModuleDeclaration {
114    pub name: String,
115    pub body: Box<Statement>,
116}
117
118/// Declare statement
119#[derive(Debug, Clone, Serialize, Deserialize)]
120pub struct DeclareStatement {
121    pub declaration: Box<Statement>,
122}
123
124/// Block statement
125#[derive(Debug, Clone, Serialize, Deserialize)]
126pub struct BlockStatement {
127    pub statements: Vec<Statement>,
128}
129
130/// Expression statement
131#[derive(Debug, Clone, Serialize, Deserialize)]
132pub struct ExpressionStatement {
133    pub expression: Expression,
134}
135
136/// If statement
137#[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/// While statement
145#[derive(Debug, Clone, Serialize, Deserialize)]
146pub struct WhileStatement {
147    pub condition: Expression,
148    pub body: Box<Statement>,
149}
150
151/// For statement
152#[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/// Return statement
161#[derive(Debug, Clone, Serialize, Deserialize)]
162pub struct ReturnStatement {
163    pub argument: Option<Expression>,
164}
165
166/// Break statement
167#[derive(Debug, Clone, Serialize, Deserialize)]
168pub struct BreakStatement {
169    pub label: Option<String>,
170}
171
172/// Continue statement
173#[derive(Debug, Clone, Serialize, Deserialize)]
174pub struct ContinueStatement {
175    pub label: Option<String>,
176}
177
178/// Throw statement
179#[derive(Debug, Clone, Serialize, Deserialize)]
180pub struct ThrowStatement {
181    pub argument: Expression,
182}
183
184/// Try statement
185#[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/// Switch statement
193#[derive(Debug, Clone, Serialize, Deserialize)]
194pub struct SwitchStatement {
195    pub discriminant: Expression,
196    pub cases: Vec<SwitchCase>,
197}
198
199/// Expression types
200#[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/// Literal values
229#[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), // pattern, flags
237    BigInt(String),
238}
239
240/// Binary expression
241#[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/// Unary expression
249#[derive(Debug, Clone, Serialize, Deserialize)]
250pub struct UnaryExpression {
251    pub operator: crate::lexer::Token,
252    pub argument: Box<Expression>,
253}
254
255/// Logical expression
256#[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/// Conditional expression
264#[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/// Assignment expression
272#[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/// Call expression
280#[derive(Debug, Clone, Serialize, Deserialize)]
281pub struct CallExpression {
282    pub callee: Box<Expression>,
283    pub arguments: Vec<Expression>,
284}
285
286/// Member expression
287#[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/// Array expression
295#[derive(Debug, Clone, Serialize, Deserialize)]
296pub struct ArrayExpression {
297    pub elements: Vec<Option<Expression>>,
298}
299
300/// Object expression
301#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct ObjectExpression {
303    pub properties: Vec<ObjectProperty>,
304}
305
306/// Object property
307#[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/// Parenthesized expression
317#[derive(Debug, Clone, Serialize, Deserialize)]
318pub struct ParenthesizedExpression {
319    pub expression: Box<Expression>,
320}
321
322/// Arrow function expression
323#[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/// New expression
332#[derive(Debug, Clone, Serialize, Deserialize)]
333pub struct NewExpression {
334    pub callee: Box<Expression>,
335    pub arguments: Vec<Expression>,
336}
337
338/// Super expression
339#[derive(Debug, Clone, Serialize, Deserialize)]
340pub struct SuperExpression;
341
342/// This expression
343#[derive(Debug, Clone, Serialize, Deserialize)]
344pub struct ThisExpression;
345
346/// Yield expression
347#[derive(Debug, Clone, Serialize, Deserialize)]
348pub struct YieldExpression {
349    pub argument: Option<Box<Expression>>,
350    pub delegate: bool,
351}
352
353/// Await expression
354#[derive(Debug, Clone, Serialize, Deserialize)]
355pub struct AwaitExpression {
356    pub argument: Box<Expression>,
357}
358
359/// Type assertion
360#[derive(Debug, Clone, Serialize, Deserialize)]
361pub struct TypeAssertion {
362    pub expression: Box<Expression>,
363    pub type_: Type,
364}
365
366/// As expression
367#[derive(Debug, Clone, Serialize, Deserialize)]
368pub struct AsExpression {
369    pub expression: Box<Expression>,
370    pub type_: Type,
371}
372
373/// Non-null expression
374#[derive(Debug, Clone, Serialize, Deserialize)]
375pub struct NonNullExpression {
376    pub expression: Box<Expression>,
377}
378
379/// Optional expression
380#[derive(Debug, Clone, Serialize, Deserialize)]
381pub struct OptionalExpression {
382    pub expression: Box<Expression>,
383    pub optional: bool,
384}
385
386/// Template literal
387#[derive(Debug, Clone, Serialize, Deserialize)]
388pub struct TemplateLiteral {
389    pub quasis: Vec<TemplateElement>,
390    pub expressions: Vec<Expression>,
391}
392
393/// Template element
394#[derive(Debug, Clone, Serialize, Deserialize)]
395pub struct TemplateElement {
396    pub value: String,
397    pub tail: bool,
398}
399
400/// Tagged template expression
401#[derive(Debug, Clone, Serialize, Deserialize)]
402pub struct TaggedTemplateExpression {
403    pub tag: Box<Expression>,
404    pub quasi: TemplateLiteral,
405}
406
407/// Type definitions
408#[derive(Debug, Clone, Serialize, Deserialize)]
409pub enum Type {
410    // Primitive types
411    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 types
425    Named(String),
426    Qualified(QualifiedTypeName),
427
428    // Generic types
429    Generic(GenericType),
430    GenericNamed {
431        name: String,
432        type_parameters: Vec<TypeParameter>,
433    },
434
435    // Union and intersection types
436    Union {
437        left: Box<Type>,
438        right: Box<Type>,
439    },
440    Intersection {
441        left: Box<Type>,
442        right: Box<Type>,
443    },
444
445    // Array and tuple types
446    Array(Box<Type>),
447    Tuple(Vec<Type>),
448
449    // Function types
450    Function(Box<FunctionType>),
451
452    // Object types
453    ObjectType(ObjectType),
454
455    // Index signatures
456    IndexSignature(Box<IndexSignature>),
457
458    // Mapped types
459    Mapped(Box<MappedType>),
460
461    // Conditional types
462    Conditional(ConditionalType),
463
464    // Template literal types
465    TemplateLiteral(TemplateLiteralType),
466
467    // Parenthesized types
468    Parenthesized(Box<Type>),
469
470    // Type queries
471    TypeQuery(Box<TypeQuery>),
472
473    // Import types
474    Import(Box<ImportType>),
475}
476
477/// Qualified type name
478#[derive(Debug, Clone, Serialize, Deserialize)]
479pub struct QualifiedTypeName {
480    pub left: Box<Type>,
481    pub right: String,
482}
483
484/// Generic type
485#[derive(Debug, Clone, Serialize, Deserialize)]
486pub struct GenericType {
487    pub type_: Box<Type>,
488    pub type_arguments: Vec<Type>,
489}
490
491/// Function type
492#[derive(Debug, Clone, Serialize, Deserialize)]
493pub struct FunctionType {
494    pub type_parameters: Vec<TypeParameter>,
495    pub parameters: Vec<Parameter>,
496    pub return_type: Box<Type>,
497}
498
499/// Object type
500#[derive(Debug, Clone, Serialize, Deserialize)]
501pub struct ObjectType {
502    pub members: Vec<ObjectTypeMember>,
503}
504
505/// Object type member
506#[derive(Debug, Clone, Serialize, Deserialize)]
507pub enum ObjectTypeMember {
508    Property(PropertySignature),
509    Method(MethodSignature),
510    Index(IndexSignature),
511    Call(CallSignature),
512    Construct(ConstructSignature),
513}
514
515/// Property signature
516#[derive(Debug, Clone, Serialize, Deserialize)]
517pub struct PropertySignature {
518    pub name: String,
519    pub optional: bool,
520    pub type_: Option<Type>,
521    pub readonly: bool,
522}
523
524/// Method signature
525#[derive(Debug, Clone, Serialize, Deserialize)]
526pub struct MethodSignature {
527    pub name: String,
528    pub optional: bool,
529    pub type_parameters: Vec<TypeParameter>,
530    pub parameters: Vec<Parameter>,
531    pub return_type: Option<Type>,
532}
533
534/// Index signature
535#[derive(Debug, Clone, Serialize, Deserialize)]
536pub struct IndexSignature {
537    pub parameter: Box<Parameter>,
538    pub type_: Type,
539    pub readonly: bool,
540}
541
542/// Call signature
543#[derive(Debug, Clone, Serialize, Deserialize)]
544pub struct CallSignature {
545    pub type_parameters: Vec<TypeParameter>,
546    pub parameters: Vec<Parameter>,
547    pub return_type: Option<Type>,
548}
549
550/// Construct signature
551#[derive(Debug, Clone, Serialize, Deserialize)]
552pub struct ConstructSignature {
553    pub type_parameters: Vec<TypeParameter>,
554    pub parameters: Vec<Parameter>,
555    pub return_type: Option<Type>,
556}
557
558/// Mapped type
559#[derive(Debug, Clone, Serialize, Deserialize)]
560pub struct MappedType {
561    pub type_parameter: Box<TypeParameter>,
562    pub constraint: Option<Box<Type>>,
563    pub name_type: Option<Box<Type>>,
564    pub type_: Box<Type>,
565    pub readonly: Option<bool>,
566    pub optional: Option<bool>,
567}
568
569/// Conditional type
570#[derive(Debug, Clone, Serialize, Deserialize)]
571pub struct ConditionalType {
572    pub check_type: Box<Type>,
573    pub extends_type: Box<Type>,
574    pub true_type: Box<Type>,
575    pub false_type: Box<Type>,
576}
577
578/// Template literal type
579#[derive(Debug, Clone, Serialize, Deserialize)]
580pub struct TemplateLiteralType {
581    pub head: String,
582    pub spans: Vec<TemplateLiteralTypeSpan>,
583}
584
585/// Template literal type span
586#[derive(Debug, Clone, Serialize, Deserialize)]
587pub struct TemplateLiteralTypeSpan {
588    pub type_: Type,
589    pub literal: String,
590}
591
592/// Type query
593#[derive(Debug, Clone, Serialize, Deserialize)]
594pub struct TypeQuery {
595    pub expr_name: Box<Expression>,
596}
597
598/// Import type
599#[derive(Debug, Clone, Serialize, Deserialize)]
600pub struct ImportType {
601    pub argument: Box<Type>,
602    pub qualifier: Option<String>,
603    pub type_arguments: Option<Vec<Type>>,
604}
605
606/// Type parameter
607#[derive(Debug, Clone, Serialize, Deserialize)]
608pub struct TypeParameter {
609    pub name: String,
610    pub constraint: Option<Box<Type>>,
611    pub default: Option<Box<Type>>,
612}
613
614/// Parameter
615#[derive(Debug, Clone, Serialize, Deserialize)]
616pub struct Parameter {
617    pub name: String,
618    pub optional: bool,
619    pub type_: Option<Box<Type>>,
620    pub initializer: Option<Expression>,
621    pub rest: bool,
622}
623
624/// Class body
625#[derive(Debug, Clone, Serialize, Deserialize)]
626pub struct ClassBody {
627    pub members: Vec<ClassMember>,
628}
629
630/// Class member
631#[derive(Debug, Clone, Serialize, Deserialize)]
632pub enum ClassMember {
633    Property(PropertyDeclaration),
634    Method(MethodDeclaration),
635    Constructor(ConstructorDeclaration),
636    Getter(GetterDeclaration),
637    Setter(SetterDeclaration),
638    Index(IndexSignature),
639}
640
641/// Property declaration
642#[derive(Debug, Clone, Serialize, Deserialize)]
643pub struct PropertyDeclaration {
644    pub name: String,
645    pub optional: bool,
646    pub type_: Option<Type>,
647    pub initializer: Option<Expression>,
648    pub modifiers: Vec<Modifier>,
649}
650
651/// Method declaration
652#[derive(Debug, Clone, Serialize, Deserialize)]
653pub struct MethodDeclaration {
654    pub name: String,
655    pub optional: bool,
656    pub type_parameters: Vec<TypeParameter>,
657    pub parameters: Vec<Parameter>,
658    pub return_type: Option<Type>,
659    pub body: Option<Statement>,
660    pub modifiers: Vec<Modifier>,
661}
662
663/// Constructor declaration
664#[derive(Debug, Clone, Serialize, Deserialize)]
665pub struct ConstructorDeclaration {
666    pub parameters: Vec<Parameter>,
667    pub body: Option<Statement>,
668    pub modifiers: Vec<Modifier>,
669}
670
671/// Getter declaration
672#[derive(Debug, Clone, Serialize, Deserialize)]
673pub struct GetterDeclaration {
674    pub name: String,
675    pub type_: Option<Type>,
676    pub body: Option<Statement>,
677    pub modifiers: Vec<Modifier>,
678}
679
680/// Setter declaration
681#[derive(Debug, Clone, Serialize, Deserialize)]
682pub struct SetterDeclaration {
683    pub name: String,
684    pub parameter: Parameter,
685    pub body: Option<Statement>,
686    pub modifiers: Vec<Modifier>,
687}
688
689/// Interface body
690#[derive(Debug, Clone, Serialize, Deserialize)]
691pub struct InterfaceBody {
692    pub members: Vec<ObjectTypeMember>,
693}
694
695/// Enum member
696#[derive(Debug, Clone, Serialize, Deserialize)]
697pub struct EnumMember {
698    pub name: String,
699    pub initializer: Option<Expression>,
700}
701
702/// Import specifier
703#[derive(Debug, Clone, Serialize, Deserialize)]
704pub enum ImportSpecifier {
705    Named(NamedImportSpecifier),
706    Default(DefaultImportSpecifier),
707    Namespace(NamespaceImportSpecifier),
708}
709
710/// Named import specifier
711#[derive(Debug, Clone, Serialize, Deserialize)]
712pub struct NamedImportSpecifier {
713    pub name: String,
714    pub imported: String,
715}
716
717/// Default import specifier
718#[derive(Debug, Clone, Serialize, Deserialize)]
719pub struct DefaultImportSpecifier {
720    pub name: String,
721}
722
723/// Namespace import specifier
724#[derive(Debug, Clone, Serialize, Deserialize)]
725pub struct NamespaceImportSpecifier {
726    pub name: String,
727}
728
729/// Catch clause
730#[derive(Debug, Clone, Serialize, Deserialize)]
731pub struct CatchClause {
732    pub parameter: Option<Parameter>,
733    pub body: Box<Statement>,
734}
735
736/// Switch case
737#[derive(Debug, Clone, Serialize, Deserialize)]
738pub struct SwitchCase {
739    pub expression: Option<Expression>,
740    pub statements: Vec<Statement>,
741}
742
743/// Modifier
744#[derive(Debug, Clone, Serialize, Deserialize)]
745pub enum Modifier {
746    Public,
747    Private,
748    Protected,
749    Static,
750    Readonly,
751    Abstract,
752    Async,
753    Override,
754}
755
756/// Source location information
757#[derive(Debug, Clone, Serialize, Deserialize)]
758pub struct SourceLocation {
759    pub start: Position,
760    pub end: Position,
761}
762
763/// Position in source code
764#[derive(Debug, Clone, Serialize, Deserialize)]
765pub struct Position {
766    pub line: usize,
767    pub column: usize,
768    pub offset: usize,
769}