use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Program {
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Statement {
VariableDeclaration(VariableDeclaration),
FunctionDeclaration(FunctionDeclaration),
ClassDeclaration(ClassDeclaration),
InterfaceDeclaration(InterfaceDeclaration),
TypeAlias(TypeAlias),
EnumDeclaration(EnumDeclaration),
ImportDeclaration(ImportDeclaration),
ExportDeclaration(Box<ExportDeclaration>),
NamespaceDeclaration(NamespaceDeclaration),
ModuleDeclaration(ModuleDeclaration),
DeclareStatement(Box<DeclareStatement>),
BlockStatement(BlockStatement),
ExpressionStatement(ExpressionStatement),
IfStatement(Box<IfStatement>),
WhileStatement(WhileStatement),
ForStatement(ForStatement),
ReturnStatement(ReturnStatement),
BreakStatement(BreakStatement),
ContinueStatement(ContinueStatement),
ThrowStatement(ThrowStatement),
TryStatement(Box<TryStatement>),
SwitchStatement(SwitchStatement),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct VariableDeclaration {
pub keyword: crate::lexer::Keyword,
pub name: String,
pub type_annotation: Option<Type>,
pub initializer: Option<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionDeclaration {
pub name: String,
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassDeclaration {
pub name: String,
pub type_parameters: Vec<TypeParameter>,
pub extends: Option<Type>,
pub implements: Vec<Type>,
pub body: ClassBody,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterfaceDeclaration {
pub name: String,
pub type_parameters: Vec<TypeParameter>,
pub extends: Vec<Type>,
pub body: InterfaceBody,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeAlias {
pub name: String,
pub type_parameters: Vec<TypeParameter>,
pub type_definition: Type,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnumDeclaration {
pub name: String,
pub members: Vec<EnumMember>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImportDeclaration {
pub specifiers: Vec<ImportSpecifier>,
pub source: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExportDeclaration {
pub declaration: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceDeclaration {
pub name: String,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ModuleDeclaration {
pub name: String,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeclareStatement {
pub declaration: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockStatement {
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExpressionStatement {
pub expression: Expression,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IfStatement {
pub condition: Expression,
pub consequent: Box<Statement>,
pub alternate: Option<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WhileStatement {
pub condition: Expression,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ForStatement {
pub init: Option<Expression>,
pub condition: Option<Expression>,
pub update: Option<Expression>,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReturnStatement {
pub argument: Option<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BreakStatement {
pub label: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ContinueStatement {
pub label: Option<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThrowStatement {
pub argument: Expression,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TryStatement {
pub block: Box<Statement>,
pub handler: Option<CatchClause>,
pub finalizer: Option<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwitchStatement {
pub discriminant: Expression,
pub cases: Vec<SwitchCase>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Expression {
Literal(Literal),
Identifier(String),
Binary(BinaryExpression),
Unary(UnaryExpression),
Logical(LogicalExpression),
Conditional(ConditionalExpression),
Assignment(AssignmentExpression),
Call(CallExpression),
Member(MemberExpression),
Array(ArrayExpression),
Object(ObjectExpression),
Parenthesized(ParenthesizedExpression),
Arrow(Box<ArrowFunctionExpression>),
New(NewExpression),
Super(SuperExpression),
This(ThisExpression),
Yield(Box<YieldExpression>),
Await(AwaitExpression),
TypeAssertion(TypeAssertion),
AsExpression(AsExpression),
NonNull(NonNullExpression),
Optional(OptionalExpression),
Template(TemplateLiteral),
TaggedTemplate(TaggedTemplateExpression),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Literal {
String(String),
Number(f64),
Boolean(bool),
Null,
Undefined,
RegExp(String, String), BigInt(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BinaryExpression {
pub left: Box<Expression>,
pub operator: crate::lexer::Token,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UnaryExpression {
pub operator: crate::lexer::Token,
pub argument: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LogicalExpression {
pub left: Box<Expression>,
pub operator: crate::lexer::Token,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConditionalExpression {
pub test: Box<Expression>,
pub consequent: Box<Expression>,
pub alternate: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AssignmentExpression {
pub left: Box<Expression>,
pub operator: crate::lexer::Token,
pub right: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallExpression {
pub callee: Box<Expression>,
pub arguments: Vec<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MemberExpression {
pub object: Box<Expression>,
pub property: Box<Expression>,
pub computed: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArrayExpression {
pub elements: Vec<Option<Expression>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectExpression {
pub properties: Vec<ObjectProperty>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectProperty {
pub key: Expression,
pub value: Expression,
pub shorthand: bool,
pub computed: bool,
pub method: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ParenthesizedExpression {
pub expression: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ArrowFunctionExpression {
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Box<Type>>,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NewExpression {
pub callee: Box<Expression>,
pub arguments: Vec<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SuperExpression;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ThisExpression;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct YieldExpression {
pub argument: Option<Box<Expression>>,
pub delegate: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AwaitExpression {
pub argument: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeAssertion {
pub expression: Box<Expression>,
pub type_: Type,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AsExpression {
pub expression: Box<Expression>,
pub type_: Type,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NonNullExpression {
pub expression: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OptionalExpression {
pub expression: Box<Expression>,
pub optional: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateLiteral {
pub quasis: Vec<TemplateElement>,
pub expressions: Vec<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateElement {
pub value: String,
pub tail: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TaggedTemplateExpression {
pub tag: Box<Expression>,
pub quasi: TemplateLiteral,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Type {
String,
Number,
Boolean,
Any,
Void,
Never,
Unknown,
Null,
Undefined,
Object,
Symbol,
BigInt,
Named(String),
Qualified(QualifiedTypeName),
Generic(GenericType),
GenericNamed {
name: String,
type_arguments: Vec<Type>,
},
Union {
left: Box<Type>,
right: Box<Type>,
},
Intersection {
left: Box<Type>,
right: Box<Type>,
},
Array(Box<Type>),
Tuple(Vec<Type>),
Function(Box<FunctionType>),
ObjectType(ObjectType),
IndexSignature(Box<IndexSignature>),
Mapped(Box<MappedType>),
Conditional(ConditionalType),
TemplateLiteral(TemplateLiteralType),
Parenthesized(Box<Type>),
TypeQuery(Box<TypeQuery>),
Import(Box<ImportType>),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QualifiedTypeName {
pub left: Box<Type>,
pub right: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GenericType {
pub type_: Box<Type>,
pub type_arguments: Vec<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FunctionType {
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Box<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ObjectType {
pub members: Vec<ObjectTypeMember>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ObjectTypeMember {
Property(PropertySignature),
Method(MethodSignature),
Index(IndexSignature),
Call(CallSignature),
Construct(ConstructSignature),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertySignature {
pub name: String,
pub optional: bool,
pub type_: Option<Type>,
pub readonly: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MethodSignature {
pub name: String,
pub optional: bool,
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IndexSignature {
pub parameter: Box<Parameter>,
pub type_: Type,
pub readonly: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CallSignature {
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstructSignature {
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MappedType {
pub type_parameter: Box<TypeParameter>,
pub constraint: Option<Box<Type>>,
pub name_type: Option<Box<Type>>,
pub type_: Box<Type>,
pub readonly: Option<bool>,
pub optional: Option<bool>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConditionalType {
pub check_type: Box<Type>,
pub extends_type: Box<Type>,
pub true_type: Box<Type>,
pub false_type: Box<Type>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateLiteralType {
pub head: String,
pub spans: Vec<TemplateLiteralTypeSpan>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TemplateLiteralTypeSpan {
pub type_: Type,
pub literal: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeQuery {
pub expr_name: Box<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ImportType {
pub argument: Box<Type>,
pub qualifier: Option<String>,
pub type_arguments: Option<Vec<Type>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TypeParameter {
pub name: String,
pub constraint: Option<Box<Type>>,
pub default: Option<Box<Type>>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Parameter {
pub name: String,
pub optional: bool,
pub type_: Option<Box<Type>>,
pub initializer: Option<Expression>,
pub rest: bool,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClassBody {
pub members: Vec<ClassMember>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ClassMember {
Property(PropertyDeclaration),
Method(MethodDeclaration),
Constructor(ConstructorDeclaration),
Getter(GetterDeclaration),
Setter(SetterDeclaration),
Index(IndexSignature),
Decorator(String), }
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PropertyDeclaration {
pub name: String,
pub optional: bool,
pub type_: Option<Type>,
pub initializer: Option<Expression>,
pub modifiers: Vec<Modifier>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MethodDeclaration {
pub name: String,
pub optional: bool,
pub type_parameters: Vec<TypeParameter>,
pub parameters: Vec<Parameter>,
pub return_type: Option<Type>,
pub body: Option<Statement>,
pub modifiers: Vec<Modifier>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConstructorDeclaration {
pub parameters: Vec<Parameter>,
pub body: Option<Statement>,
pub modifiers: Vec<Modifier>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct GetterDeclaration {
pub name: String,
pub type_: Option<Type>,
pub body: Option<Statement>,
pub modifiers: Vec<Modifier>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SetterDeclaration {
pub name: String,
pub parameter: Parameter,
pub body: Option<Statement>,
pub modifiers: Vec<Modifier>,
pub decorators: Vec<String>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InterfaceBody {
pub members: Vec<ObjectTypeMember>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct EnumMember {
pub name: String,
pub initializer: Option<Expression>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ImportSpecifier {
Named(NamedImportSpecifier),
Default(DefaultImportSpecifier),
Namespace(NamespaceImportSpecifier),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamedImportSpecifier {
pub name: String,
pub imported: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DefaultImportSpecifier {
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct NamespaceImportSpecifier {
pub name: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CatchClause {
pub parameter: Option<Parameter>,
pub body: Box<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SwitchCase {
pub expression: Option<Expression>,
pub statements: Vec<Statement>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Modifier {
Public,
Private,
Protected,
Static,
Readonly,
Abstract,
Async,
Override,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceLocation {
pub start: Position,
pub end: Position,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Position {
pub line: usize,
pub column: usize,
pub offset: usize,
}