Skip to content

Commit

Permalink
implement #[span]
Browse files Browse the repository at this point in the history
  • Loading branch information
kdy1 committed Mar 4, 2018
1 parent 3321ca5 commit 077e0c5
Show file tree
Hide file tree
Showing 16 changed files with 214 additions and 75 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,8 @@ features = [ "suggestions", "color" ]


[profile.bench]
debug = true
debug = true


[patch.crates-io]
darling = { git = "https://github.com/kdy1/darling", branch = "proc-macro2-nightly" }
3 changes: 2 additions & 1 deletion ecmascript/ast/src/decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ pub enum Decl {
#[ast_node]
pub struct FnDecl {
pub ident: Ident,
#[span]
pub function: Function,
}

#[ast_node]
pub struct ClassDecl {
pub ident: Ident,

#[span]
pub class: Class,
}

Expand Down
33 changes: 28 additions & 5 deletions ecmascript/ast/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{AssignOp, BinaryOp, BlockStmt, Class, Function, Ident, Lit, Pat, Prop, UnaryOp,
UpdateOp};
use swc_common::Span;
use swc_common::{Span, Spanned};
use swc_macros::ast_node;

#[ast_node]
Expand Down Expand Up @@ -114,15 +114,15 @@ pub struct BinExpr {
#[ast_node]
pub struct FnExpr {
pub ident: Option<Ident>,

#[span]
pub function: Function,
}

/// Class expression.
#[ast_node]
pub struct ClassExpr {
pub ident: Option<Ident>,

#[span]
pub class: Class,
}

Expand All @@ -136,34 +136,42 @@ pub struct AssignExpr {

#[ast_node]
pub struct MemberExpr {
pub span: Span,
pub obj: ExprOrSuper,
pub prop: Box<Expr>,
pub computed: bool,
}
#[ast_node]
pub struct CondExpr {
#[span(lo)]
pub test: Box<Expr>,
pub cons: Box<Expr>,
#[span(hi)]
pub alt: Box<Expr>,
}

#[ast_node]
pub struct CallExpr {
pub span: Span,
pub callee: ExprOrSuper,
pub args: Vec<ExprOrSpread>,
}
#[ast_node]
pub struct NewExpr {
pub span: Span,
pub callee: Box<Expr>,
// #[code = "$( $( $args ),* )?"]
pub args: Option<(Vec<ExprOrSpread>)>,
}
#[ast_node]
pub struct SeqExpr {
/// TODO: Calculate
pub span: Span,
pub exprs: Vec<(Box<Expr>)>,
}

#[ast_node]
pub struct ArrowExpr {
pub span: Span,
pub params: Vec<Pat>,

pub body: BlockStmtOrExpr,
Expand All @@ -173,21 +181,26 @@ pub struct ArrowExpr {

#[ast_node]
pub struct YieldExpr {
pub span: Span,
pub arg: Option<(Box<Expr>)>,
pub delegate: bool,
}
#[ast_node]
pub struct MetaPropExpr {
#[span(lo)]
pub meta: Ident,
#[span(hi)]
pub prop: Ident,
}
#[ast_node]
pub struct AwaitExpr {
pub span: Span,
pub arg: Box<Expr>,
}

#[ast_node]
pub struct TplLit {
pub span: Span,
pub tag: Option<(Box<Expr>)>,

pub exprs: Vec<(Box<Expr>)>,
Expand All @@ -197,6 +210,7 @@ pub struct TplLit {

#[ast_node]
pub struct TplElement {
pub span: Span,
pub tail: bool,
pub cooked: bool,
pub raw: String,
Expand All @@ -214,11 +228,20 @@ pub enum ExprOrSuper {
Expr(Box<Expr>),
}

#[ast_node]
#[derive(Fold, Clone, Debug, PartialEq)]
pub struct ExprOrSpread {
pub spread: Option<Span>,
pub expr: Box<Expr>,
}
impl Spanned for ExprOrSpread {
fn span(&self) -> Span {
let expr = self.expr.span();
match self.spread {
Some(spread) => expr.with_lo(spread.lo()),
None => expr,
}
}
}

#[ast_node]
pub enum BlockStmtOrExpr {
Expand Down
13 changes: 8 additions & 5 deletions ecmascript/ast/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,18 @@ pub use self::class::{Class, ClassMethod, ClassMethodKind};
pub use self::decl::{ClassDecl, Decl, FnDecl, VarDecl, VarDeclKind, VarDeclarator};
pub use self::expr::{ArrayLit, ArrowExpr, AssignExpr, AwaitExpr, BinExpr, BlockStmtOrExpr,
CallExpr, ClassExpr, CondExpr, Expr, ExprOrSpread, ExprOrSuper, FnExpr,
MemberExpr, MetaPropExpr, NewExpr, ObjectLit, PatOrExpr, SeqExpr, TplElement,
TplLit, UnaryExpr, UpdateExpr, YieldExpr};
MemberExpr, MetaPropExpr, NewExpr, ObjectLit, ParenExpr, PatOrExpr, SeqExpr,
ThisExpr, TplElement, TplLit, UnaryExpr, UpdateExpr, YieldExpr};
pub use self::function::Function;
pub use self::lit::{Bool, Lit, Null, Number, Regex, RegexFlags, Str};
pub use self::module::{Module, ModuleItem};
pub use self::module_decl::{ExportDefaultDecl, ExportSpecifier, ImportSpecifier, ModuleDecl};
pub use self::module_decl::{ExportAll, ExportDefaultDecl, ExportSpecifier, ImportDecl,
ImportDefault, ImportSpecific, ImportSpecifier, ImportStarAs,
ModuleDecl, NamedExport};
pub use self::operators::{AssignOp, BinaryOp, UnaryOp, UpdateOp};
pub use self::pat::{ObjectPatProp, Pat};
pub use self::prop::{Prop, PropName};
pub use self::pat::{ArrayPat, AssignPat, AssignPatProp, KeyValuePatProp, ObjectPat, ObjectPatProp,
Pat};
pub use self::prop::{AssignProp, GetterProp, KeyValueProp, MethodProp, Prop, PropName, SetterProp};
pub use self::stmt::{BlockStmt, BreakStmt, CatchClause, ContinueStmt, DebuggerStmt, DoWhileStmt,
EmptyStmt, ForInStmt, ForOfStmt, ForStmt, IfStmt, LabeledStmt, ReturnStmt,
Stmt, SwitchCase, SwitchStmt, ThrowStmt, TryStmt, VarDeclOrExpr,
Expand Down
1 change: 1 addition & 0 deletions ecmascript/ast/src/module_decl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ pub struct ImportSpecific {

#[ast_node]
pub struct ExportSpecifier {
pub span: Span,
/// `foo` in `export { foo as bar }`
pub orig: Ident,
/// `Some(bar)` in `export { foo as bar }`
Expand Down
11 changes: 10 additions & 1 deletion ecmascript/ast/src/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub enum Pat {

Rest(Box<Pat>),

Object(Vec<ObjectPatProp>),
Object(ObjectPat),

Assign(AssignPat),

Expand All @@ -24,6 +24,12 @@ pub struct ArrayPat {
pub elems: Vec<(Option<Pat>)>,
}

#[ast_node]
pub struct ObjectPat {
pub span: Span,
pub props: Vec<ObjectPatProp>,
}

#[ast_node]
pub struct AssignPat {
pub span: Span,
Expand All @@ -40,12 +46,15 @@ pub enum ObjectPatProp {
/// `{key: value}`
#[ast_node]
pub struct KeyValuePatProp {
#[span(lo)]
pub key: PropName,
#[span(hi)]
pub value: Box<Pat>,
}
/// `{key}` or `{key = value}`
#[ast_node]
pub struct AssignPatProp {
pub span: Span,
pub key: Ident,

pub value: Option<(Box<Expr>)>,
Expand Down
12 changes: 6 additions & 6 deletions ecmascript/ast/src/prop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,18 @@ pub enum Prop {

#[ast_node]
pub struct KeyValueProp {
// #[span(lo)]
#[span(lo)]
pub key: PropName,

// #[span(hi)]
#[span(hi)]
pub value: Box<Expr>,
}

#[ast_node]
pub struct AssignProp {
// #[span(lo)]
#[span(lo)]
key: Ident,
// #[span(hi)]
#[span(hi)]
value: Box<Expr>,
}
#[ast_node]
Expand All @@ -47,9 +47,9 @@ pub struct SetterProp {
}
#[ast_node]
pub struct MethodProp {
// #[span(lo)]
#[span(lo)]
key: PropName,
// #[span(hi)]
#[span(hi)]
function: Function,
}

Expand Down
1 change: 1 addition & 0 deletions macros/ast_node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ swc_macros_common = { path = "../common" }
pmutil = "0.1"
proc-macro2 = { version = "0.2", features = ["nightly"] }
quote = "0.4"
darling = "0.3"

[dependencies.syn]
version = "0.12"
Expand Down
2 changes: 2 additions & 0 deletions macros/ast_node/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(box_syntax, proc_macro)]

#[macro_use]
extern crate darling;
#[macro_use]
extern crate pmutil;
extern crate proc_macro;
Expand Down
Loading

0 comments on commit 077e0c5

Please sign in to comment.