Macro ecmascript::build_ast [−][src]
macro_rules! build_ast { ([$($many:tt)+]) => { ... }; (regex_lit /{$pattern:expr}/{$flags:expr}) => { ... }; (regex_lit /{$pattern:expr}/) => { ... }; (this) => { ... }; (id $id:expr) => { ... }; (null) => { ... }; (true) => { ... }; (false) => { ... }; (num $lit:expr) => { ... }; (str $lit:expr) => { ... }; (array [$($elements:tt),*]) => { ... }; (obj [$($properties:tt),+]) => { ... }; ([$($key:tt)+]: [$($value:tt)+]) => { ... }; (function [$($params:tt),+] {$body:expr}) => { ... }; (...[$($expression:tt)+]) => { ... }; (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! { function [id "my_wrapper".to_string()] ( ) { var [id "foo".to_string()] = [obj []]; } };
Conventions
- we use [] to represent recursive calls to the macro
- we use {} to accept a rust expression
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!(...) ])