mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-28 04:44:57 +00:00
replace AST visitors with macro
This commit is contained in:
parent
dbf869b4d2
commit
2fc2290173
10 changed files with 334 additions and 297 deletions
|
@ -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 }
|
||||
}};
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue