replace AST visitors with macro
This commit is contained in:
@@ -295,6 +295,7 @@ fn api_walkthrough() {
|
||||
// 1. explicitly call getter methods on AST nodes.
|
||||
// 2. use descendants and `AstNode::cast`.
|
||||
// 3. use descendants and the visitor.
|
||||
// 4. use descendants and `match_ast!`.
|
||||
//
|
||||
// Here's how the first one looks like:
|
||||
let exprs_cast: Vec<String> = file
|
||||
@@ -319,3 +320,14 @@ fn api_walkthrough() {
|
||||
}
|
||||
assert_eq!(exprs_cast, exprs_visit);
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! match_ast {
|
||||
(match $node:ident {
|
||||
$( ast::$ast:ident($it:ident) => $res:block, )*
|
||||
_ => $catch_all:expr,
|
||||
}) => {{
|
||||
$( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
|
||||
{ $catch_all }
|
||||
}};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user