From c0f19d70056fada5f381019694d893e0ffe8360a Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Fri, 19 Apr 2019 03:49:56 +0800 Subject: Add expr, pat, ty and macro_stmts --- crates/ra_mbe/src/lib.rs | 57 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) (limited to 'crates/ra_mbe/src/lib.rs') diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index 4cfa1f955..a29b07aba 100644 --- a/crates/ra_mbe/src/lib.rs +++ b/crates/ra_mbe/src/lib.rs @@ -44,7 +44,11 @@ pub use crate::syntax_bridge::{ ast_to_token_tree, token_tree_to_ast_item_list, syntax_node_to_token_tree, + token_tree_to_expr, + token_tree_to_pat, + token_tree_to_ty, token_tree_to_macro_items, + token_tree_to_macro_stmts, }; /// This struct contains AST for a single `macro_rules` definition. What might @@ -450,6 +454,59 @@ MACRO_ITEMS@[0; 40) assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); } + #[test] + fn test_tt_to_stmts() { + let rules = create_rules( + r#" + macro_rules! foo { + () => { + let a = 0; + a = 10 + 1; + a + } + } +"#, + ); + + let expanded = expand(&rules, "foo!{}"); + let stmts = token_tree_to_macro_stmts(&expanded); + + assert_eq!( + stmts.syntax().debug_dump().trim(), + r#"MACRO_STMTS@[0; 15) + LET_STMT@[0; 7) + LET_KW@[0; 3) "let" + BIND_PAT@[3; 4) + NAME@[3; 4) + IDENT@[3; 4) "a" + EQ@[4; 5) "=" + LITERAL@[5; 6) + INT_NUMBER@[5; 6) "0" + SEMI@[6; 7) ";" + EXPR_STMT@[7; 14) + BIN_EXPR@[7; 13) + PATH_EXPR@[7; 8) + PATH@[7; 8) + PATH_SEGMENT@[7; 8) + NAME_REF@[7; 8) + IDENT@[7; 8) "a" + EQ@[8; 9) "=" + BIN_EXPR@[9; 13) + LITERAL@[9; 11) + INT_NUMBER@[9; 11) "10" + PLUS@[11; 12) "+" + LITERAL@[12; 13) + INT_NUMBER@[12; 13) "1" + SEMI@[13; 14) ";" + EXPR_STMT@[14; 15) + PATH_EXPR@[14; 15) + PATH@[14; 15) + PATH_SEGMENT@[14; 15) + NAME_REF@[14; 15) + IDENT@[14; 15) "a""#, + ); + } + // The following tests are port from intellij-rust directly // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt -- cgit v1.2.3