#[macro_export]
macro_rules! build_ast {
([$($many:tt)+]) => {
build_ast!($($many)+)
};
(regex_lit /{$pattern:expr}/{$flags:expr}) => {
RegExpLiteral {
pattern: $pattern,
flags: $flags,
loc: None,
}
};
(regex_lit /{$pattern:expr}/) => {
RegExpLiteral {
pattern: $pattern,
flags: String::new(),
loc: None,
}
};
(templ_el {$cooked:expr}) => {
build_ast!(templ_el {$cooked} {$cooked})
};
(templ_el {$cooked:expr} {$raw:expr}) => {
TemplateElement {
cooked: $cooked,
raw: $raw,
loc: None,
}
};
(this) => {
Expression::This(None)
};
(id $id:expr) => {
Expression::Identifier{ name: $id, loc: None, }
};
(null) => {
Expression::Literal {
value: Literal::NullLiteral(NullLiteral),
loc: None
}
};
(true) => {
Expression::Literal {
value: Literal::BooleanLiteral(BooleanLiteral(true)),
loc: None
}
};
(false) => {
Expression::Literal {
value: Literal::BooleanLiteral(BooleanLiteral(false)),
loc: None
}
};
(num $lit:expr) => {
Expression::Literal {
value: Literal::NumericLiteral(NumericLiteral($lit)),
loc: None
}
};
(str $lit:expr) => {
Expression::Literal {
value: Literal::StringLiteral(StringLiteral($lit)),
loc: None
}
};
(array [$($elements:tt),*]) => {
Expression::ArrayExpression {
loc: None,
elements: vec![$(build_ast!($elements)),*]
}
};
(array_item $($expression:tt)+) => {
ExpressionListItem::Expression(build_ast!($($expression)+))
};
(...$($expression:tt)+) => {
ExpressionListItem::Spread(SpreadElement::SpreadElement {
argument: build_ast!($($expression)+),
loc: None,
})
};
(object [$($properties:tt),*]) => {
Expression::ObjectExpression {
loc: None,
properties: vec![$(build_ast!($properties)),*]
}
};
([$($key:tt)+]: [$($value:tt)+]) => {
Property {
key: build_ast!($($key)+),
value: build_ast!($($value)+),
kind: PropertyKind::Init,
computed: false,
method: false,
shorthand: false,
loc: None,
}
};
(get [$($key:tt)+] [$($value:tt)+]) => {
Property {
key: build_ast!($($key)+),
value: build_ast!($($value)+),
kind: PropertyKind::Get,
computed: false,
method: false,
shorthand: false,
loc: None,
}
};
(set [$($key:tt)+] [$($value:tt)+]) => {
Property {
key: build_ast!($($key)+),
value: build_ast!($($value)+),
kind: PropertyKind::Set,
computed: false,
method: false,
shorthand: false,
loc: None,
}
};
(function [$($params:tt),*] [$($body:tt),*]) => {
Expression::FunctionExpression {
id: None,
params: vec![$(build_ast!($params)),*],
body: vec![$(build_ast!($body)),*],
generator: false,
async: false
}
};
(function * [$($params:tt),*] [$($body:tt),*]) => {
Expression::FunctionExpression {
id: None,
params: vec![$(build_ast!($params)),*],
body: vec![$(build_ast!($body)),*],
generator: true,
async: false
}
};
(async function [$($params:tt),*] [$($body:tt),*]) => {
Expression::FunctionExpression {
id: None,
params: vec![$(build_ast!($params)),*],
body: vec![$(build_ast!($body)),*],
generator: false,
async: true
}
};
(async function * [$($params:tt),*] [$($body:tt),*]) => {
Expression::FunctionExpression {
id: None,
params: vec![$(build_ast!($params)),*],
body: vec![$(build_ast!($body)),*],
generator: true,
async: true
}
};
(function [$($params:tt),*] {$body:expr}) => {
Expression::FunctionExpression {
id: None,
params: vec![$(build_ast!($params)),*],
body: $body,
generator: false,
async: false
}
};
(p_id $id:expr) => {
Pattern::Identifier(Identifier(None, $id))
};
(call [$($id:tt)+] [$($args:tt)+]) => {
Expression::Call {
callee: Box::new(build_ast!($($id)+)),
arguments: vec![$(build_ast!($args)),+]
}
};
(yield) => {
Expression::Yield {
argument: None,
delegate: false,
}
};
(<$id:ident />) => {
Expression::JsxElementExpression {
name: stringify!($id).to_string(),
attributes: Vec::new(),
children: Vec::new(),
loc: None
}
};
}