Macro ecmascript::build_ast [−][src]
macro_rules! build_ast { ([$($many:tt)+]) => { ... }; (regex_lit /{$pattern:expr}/{$flags:expr}) => { ... }; (regex_lit /{$pattern:expr}/) => { ... }; (templ_el {$cooked:expr}) => { ... }; (templ_el {$cooked:expr} {$raw:expr}) => { ... }; (this) => { ... }; (id $id:expr) => { ... }; (null) => { ... }; (true) => { ... }; (false) => { ... }; (num $lit:expr) => { ... }; (str $lit:expr) => { ... }; (array [$($elements:tt),*]) => { ... }; (array_item $($expression:tt)+) => { ... }; (...$($expression:tt)+) => { ... }; (object [$($properties:tt),*]) => { ... }; ([$($key:tt)+]: [$($value:tt)+]) => { ... }; (get [$($key:tt)+] [$($value:tt)+]) => { ... }; (set [$($key:tt)+] [$($value:tt)+]) => { ... }; (function [$($params:tt),*] [$($body:tt),*]) => { ... }; (function * [$($params:tt),*] [$($body:tt),*]) => { ... }; (async function [$($params:tt),*] [$($body:tt),*]) => { ... }; (async function * [$($params:tt),*] [$($body:tt),*]) => { ... }; (function [$($params:tt),*] {$body:expr}) => { ... }; (p_id $id:expr) => { ... }; (call [$($id:tt)+] [$($args:tt)+]) => { ... }; (yield) => { ... }; (<$id:ident />) => { ... }; }
This macro makes constructing complicated syntax trees very easy. This can be useful for re-writing parts of the AST, or deriving a a syntax tree from other trees (eg. concatenating syntax trees).
Example
let my_wrapper_func = build_ast! { [array [ [array_item true], [array_item false], [array_item null], [...[array [ [array_item num 1f64] ]]] ]] };
Conventions
- we use [] to represent recursive calls to the macro
eg:
call [id "my_func".to_string()] [ [id "a".to_string()] [true] [null] ]
recursively expands to
build_ast!(call build_ast!(...) [ build_ast!(...), build_ast!(...), build_ast!(...) ])
- we use {} to accept a rust expression