aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_syntax/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_syntax/src/lib.rs')
-rw-r--r--crates/ra_syntax/src/lib.rs12
1 files changed, 12 insertions, 0 deletions
diff --git a/crates/ra_syntax/src/lib.rs b/crates/ra_syntax/src/lib.rs
index edb6076bb..09230ccb2 100644
--- a/crates/ra_syntax/src/lib.rs
+++ b/crates/ra_syntax/src/lib.rs
@@ -295,6 +295,7 @@ fn api_walkthrough() {
295 // 1. explicitly call getter methods on AST nodes. 295 // 1. explicitly call getter methods on AST nodes.
296 // 2. use descendants and `AstNode::cast`. 296 // 2. use descendants and `AstNode::cast`.
297 // 3. use descendants and the visitor. 297 // 3. use descendants and the visitor.
298 // 4. use descendants and `match_ast!`.
298 // 299 //
299 // Here's how the first one looks like: 300 // Here's how the first one looks like:
300 let exprs_cast: Vec<String> = file 301 let exprs_cast: Vec<String> = file
@@ -319,3 +320,14 @@ fn api_walkthrough() {
319 } 320 }
320 assert_eq!(exprs_cast, exprs_visit); 321 assert_eq!(exprs_cast, exprs_visit);
321} 322}
323
324#[macro_export]
325macro_rules! match_ast {
326 (match $node:ident {
327 $( ast::$ast:ident($it:ident) => $res:block, )*
328 _ => $catch_all:expr,
329 }) => {{
330 $( if let Some($it) = ast::$ast::cast($node.clone()) $res else )*
331 { $catch_all }
332 }};
333}