pub mod decl;
pub mod expr;
pub mod pat;
pub mod ref_tree;
pub mod stmt;
use decl::Decl;
use expr::{Expr, Literal, Property};
use pat::Pat;
use stmt::Stmt;
pub type Identifier = String;
#[derive(PartialEq, Debug)]
pub enum Program {
Mod(Vec<ProgramPart>),
Script(Vec<ProgramPart>),
}
impl Program {
pub fn module(parts: Vec<ProgramPart>) -> Self {
Program::Mod(parts)
}
pub fn script(parts: Vec<ProgramPart>) -> Self {
Program::Script(parts)
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum ProgramPart {
Dir(Dir),
Decl(Decl),
Stmt(Stmt),
}
impl ProgramPart {
pub fn use_strict(double_quotes: bool) -> Self {
let dir = if double_quotes {
r#""use strict""#
} else {
"'use strict'"
};
ProgramPart::Dir(Dir::new(String::from(dir)))
}
pub fn decl(inner: Decl) -> Self {
ProgramPart::Decl(inner)
}
pub fn stmt(inner: Stmt) -> Self {
ProgramPart::Stmt(inner)
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Dir {
pub expr: Literal,
pub dir: String,
}
impl Dir {
pub fn new(orig: String) -> Self {
Self {
dir: orig.trim_matches(|c| c == '\'' || c == '"').to_string(),
expr: Literal::String(orig),
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub struct Function {
pub id: Option<Identifier>,
pub params: Vec<FunctionArg>,
pub body: FunctionBody,
pub generator: bool,
pub is_async: bool,
}
impl Function {
pub fn new(id: Option<Identifier>, params: Vec<FunctionArg>, body: FunctionBody, generator: bool, is_async: bool) -> Self {
Self {
id,
params,
body,
generator,
is_async,
}
}
}
#[derive(PartialEq, Debug, Clone)]
pub enum FunctionArg {
Expr(Expr),
Pat(Pat),
}
impl FunctionArg {
pub fn expr(expr: Expr) -> FunctionArg {
FunctionArg::Expr(expr)
}
pub fn pat(pat: Pat) -> FunctionArg {
FunctionArg::Pat(pat)
}
pub fn ident(name: &str) -> Self {
FunctionArg::Pat(Pat::Identifier(String::from(name)))
}
}
pub type FunctionBody = Vec<ProgramPart>;
#[derive(PartialEq, Debug, Clone)]
pub struct Class {
pub id: Option<Identifier>,
pub super_class: Option<Box<Expr>>,
pub body: Vec<Property>,
}
impl Class {
pub fn new(id: Option<Identifier>, super_class: Option<Expr>, body: Vec<Property>) -> Class {
Class {
id,
super_class: super_class.map(|e| Box::new(e)),
body,
}
}
}
pub mod prelude {
pub use crate::{
decl::{
Decl,
DefaultExportDecl,
ExportSpecifier,
ImportSpecifier,
ModDecl,
ModExport,
ModImport,
NamedExportDecl,
VariableDecl,
VariableKind,
},
expr::{
Expr,
ArrayExpr,
ObjectExpr,
ObjectProperty,
Property,
PropertyKey,
PropertyValue,
PropertyKind,
UnaryExpr,
UnaryOperator,
UpdateExpr,
UpdateOperator,
BinaryExpr,
BinaryOperator,
AssignmentExpr,
AssignmentLeft,
AssignmentOperator,
LogicalExpr,
LogicalOperator,
MemberExpr,
ConditionalExpr,
CallExpr,
NewExpr,
SequenceExpr,
ArrowFunctionExpr,
ArrowFunctionBody,
YieldExpr,
TaggedTemplateExpr,
TemplateLiteral,
TemplateElement,
MetaProperty,
Literal,
RegEx,
},
pat::{
Pat,
ArrayPatPart,
ObjectPat,
ObjectPatPart,
AssignmentPat,
},
stmt::{
Stmt,
WithStmt,
LabeledStmt,
IfStmt,
SwitchStmt,
SwitchCase,
BlockStmt,
TryStmt,
WhileStmt,
DoWhileStmt,
ForStmt,
LoopInit,
ForInStmt,
ForOfStmt,
LoopLeft,
CatchClause,
},
Identifier,
Program,
ProgramPart,
Dir,
Function,
FunctionArg,
FunctionBody,
Class,
};
}