#[cfg(feature = "serde")]
#[macro_use]
extern crate serde_derive;
use std::borrow::Cow;
pub mod decl;
pub mod expr;
pub mod pat;
#[cfg(feature = "esprima")]
pub mod serde;
pub mod spanned;
pub mod stmt;
use decl::Decl;
use expr::{Expr, Lit, Prop};
use pat::Pat;
use stmt::Stmt;
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct Ident<'a> {
pub name: Cow<'a, str>,
}
impl<'a> Ident<'a> {
pub fn new(s: String) -> Self {
Ident {
name: Cow::Owned(s),
}
}
pub fn from(s: &'a str) -> Self {
Ident {
name: Cow::Borrowed(s),
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum Program<'a> {
Mod(Vec<ProgramPart<'a>>),
Script(Vec<ProgramPart<'a>>),
}
impl<'a> Program<'a> {
pub fn module(parts: Vec<ProgramPart<'a>>) -> Self {
Program::Mod(parts)
}
pub fn script(parts: Vec<ProgramPart<'a>>) -> Self {
Program::Script(parts)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum ProgramPart<'a> {
Dir(Dir<'a>),
Decl(Decl<'a>),
Stmt(Stmt<'a>),
}
impl<'a> ProgramPart<'a> {
pub fn decl(inner: Decl<'a>) -> Self {
ProgramPart::Decl(inner)
}
pub fn stmt(inner: Stmt<'a>) -> Self {
ProgramPart::Stmt(inner)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct Dir<'a> {
pub expr: Lit<'a>,
pub dir: Cow<'a, str>,
}
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct Func<'a> {
pub id: Option<Ident<'a>>,
pub params: Vec<FuncArg<'a>>,
pub body: FuncBody<'a>,
pub generator: bool,
pub is_async: bool,
}
impl<'a> Func<'a> {
pub fn new(
id: Option<Ident<'a>>,
params: Vec<FuncArg<'a>>,
body: FuncBody<'a>,
generator: bool,
is_async: bool,
) -> Self {
Func {
id,
params,
body,
generator,
is_async,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
#[cfg_attr(all(feature = "serde", feature = "esprima"), serde(untagged))]
pub enum FuncArg<'a> {
Expr(Expr<'a>),
Pat(Pat<'a>),
}
impl<'a> FuncArg<'a> {
pub fn expr(expr: Expr) -> FuncArg {
FuncArg::Expr(expr)
}
pub fn pat(pat: Pat) -> FuncArg {
FuncArg::Pat(pat)
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct FuncBody<'a>(pub Vec<ProgramPart<'a>>);
#[derive(PartialEq, Debug, Clone)]
#[cfg_attr(all(feature = "serialization"), derive(Deserialize, Serialize))]
pub struct Class<'a> {
pub id: Option<Ident<'a>>,
pub super_class: Option<Box<Expr<'a>>>,
pub body: ClassBody<'a>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub struct ClassBody<'a>(pub Vec<Prop<'a>>);
impl<'a> Class<'a> {
pub fn new(
id: Option<Ident<'a>>,
super_class: Option<Expr<'a>>,
body: Vec<Prop<'a>>,
) -> Class<'a> {
Class {
id,
super_class: super_class.map(Box::new),
body: ClassBody(body),
}
}
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
#[cfg_attr(
all(feature = "serde", feature = "esprima"),
serde(rename_all = "camelCase", untagged)
)]
pub enum VarKind {
Var,
Let,
Const,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum AssignOp {
Equal,
PlusEqual,
MinusEqual,
TimesEqual,
DivEqual,
ModEqual,
LeftShiftEqual,
RightShiftEqual,
UnsignedRightShiftEqual,
OrEqual,
XOrEqual,
AndEqual,
PowerOfEqual,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum LogicalOp {
Or,
And,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum BinaryOp {
Equal,
NotEqual,
StrictEqual,
StrictNotEqual,
LessThan,
GreaterThan,
LessThanEqual,
GreaterThanEqual,
LeftShift,
RightShift,
UnsignedRightShift,
Plus,
Minus,
Times,
Over,
Mod,
Or,
XOr,
And,
In,
InstanceOf,
PowerOf,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum UpdateOp {
Increment,
Decrement,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum UnaryOp {
Minus,
Plus,
Not,
Tilde,
TypeOf,
Void,
Delete,
}
#[derive(Debug, Clone, PartialEq, Copy)]
#[cfg_attr(
all(feature = "serde", not(feature = "esprima")),
derive(Deserialize, Serialize)
)]
#[cfg_attr(all(feature = "serde", feature = "esprima"), derive(Deserialize))]
pub enum PropKind {
Init,
Get,
Set,
Ctor,
Method,
}
pub mod prelude {
pub use crate::decl::{
Decl, DefaultExportDecl, ExportSpecifier, ImportSpecifier, ModDecl, ModExport, ModImport,
NamedExportDecl, NormalImportSpec, VarDecl,
};
pub use crate::expr::{
ArrayExpr, ArrowFuncBody, ArrowFuncExpr, AssignExpr, AssignLeft, BinaryExpr, CallExpr,
ConditionalExpr, Expr, Lit, LogicalExpr, MemberExpr, MetaProp, NewExpr, ObjExpr, ObjProp,
Prop, PropKey, PropValue, RegEx, StringLit, TaggedTemplateExpr, TemplateElement,
TemplateLit, UnaryExpr, UpdateExpr, YieldExpr,
};
pub use crate::pat::{ArrayPatPart, AssignPat, ObjPat, ObjPatPart, Pat};
pub use crate::stmt::{
BlockStmt, CatchClause, DoWhileStmt, ForInStmt, ForOfStmt, ForStmt, IfStmt, LabeledStmt,
LoopInit, LoopLeft, Stmt, SwitchCase, SwitchStmt, TryStmt, WhileStmt, WithStmt,
};
pub use crate::{
AssignOp, BinaryOp, Class, ClassBody, Dir, Func, FuncArg, FuncBody, Ident, LogicalOp,
Program, ProgramPart, PropKind, UnaryOp, UpdateOp, VarKind,
};
}