#[macro_use]
extern crate serde_derive;
use std::borrow::Cow;
pub mod decl;
pub mod expr;
pub mod pat;
pub mod stmt;
pub mod serde;
use decl::Decl;
use expr::{Expr, Lit, Prop};
use pat::Pat;
use stmt::Stmt;
#[derive(Debug, Clone, PartialEq, Deserialize)]
#[serde(rename_all = "camelCase")]
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(PartialEq, Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
#[serde(tag = "sourceType", content: "body")]
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(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[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(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Dir<'a> {
pub expr: Lit<'a>,
pub dir: Cow<'a, str>,
}
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
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(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[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(PartialEq, Debug, Clone, Deserialize)]
pub struct FuncBody<'a>(pub Vec<ProgramPart<'a>>);
#[derive(PartialEq, Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Class<'a> {
pub id: Option<Ident<'a>>,
pub super_class: Option<Box<Expr<'a>>>,
pub body: 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,
}
}
}
#[derive(PartialEq, Clone, Debug, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", untagged)]
pub enum VarKind {
Var,
Let,
Const,
}
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum AssignOp {
Equal,
PlusEqual,
MinusEqual,
TimesEqual,
DivEqual,
ModEqual,
LeftShiftEqual,
RightShiftEqual,
UnsignedRightShiftEqual,
OrEqual,
XOrEqual,
AndEqual,
PowerOfEqual,
}
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum LogicalOp {
Or,
And,
}
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
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(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UpdateOp {
Increment,
Decrement,
}
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum UnaryOp {
Minus,
Plus,
Not,
Tilde,
TypeOf,
Void,
Delete,
}
#[derive(PartialEq, Debug, Clone, Copy, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PropKind {
Init,
Get,
Set,
Ctor,
Method,
}
pub mod prelude {
pub use crate::{
AssignOp,
BinaryOp,
Class,
Dir,
Func,
FuncArg,
FuncBody,
Ident,
LogicalOp,
Program,
ProgramPart,
PropKind,
UnaryOp,
UpdateOp,
VarKind,
};
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::decl::{
Decl,
VarDecl,
ModDecl,
ModImport,
ImportSpecifier,
ModExport,
NamedExportDecl,
DefaultExportDecl,
ExportSpecifier,
NormalImportSpec
};
pub use crate::stmt::{
Stmt,
WithStmt,
LabeledStmt,
IfStmt,
SwitchStmt,
SwitchCase,
BlockStmt,
TryStmt,
CatchClause,
WhileStmt,
DoWhileStmt,
ForStmt,
LoopInit,
ForInStmt,
ForOfStmt,
LoopLeft,
};
pub use crate::pat::{
ArrayPatPart,
AssignPat,
ObjPat,
ObjPatPart,
Pat,
};
}