aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/lib.rs105
1 files changed, 102 insertions, 3 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index e78bc734b..ee5c6866c 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -199,7 +199,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
199 rules.expand(&invocation_tt).unwrap() 199 rules.expand(&invocation_tt).unwrap()
200 } 200 }
201 201
202 pub(crate) fn expand_to_syntax( 202 pub(crate) fn expand_to_items(
203 rules: &MacroRules, 203 rules: &MacroRules,
204 invocation: &str, 204 invocation: &str,
205 ) -> ra_syntax::TreeArc<ast::MacroItems> { 205 ) -> ra_syntax::TreeArc<ast::MacroItems> {
@@ -207,7 +207,28 @@ impl_froms!(TokenTree: Leaf, Subtree);
207 token_tree_to_macro_items(&expanded).unwrap() 207 token_tree_to_macro_items(&expanded).unwrap()
208 } 208 }
209 209
210 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 210 #[allow(unused)]
211 pub(crate) fn expand_to_stmts(
212 rules: &MacroRules,
213 invocation: &str,
214 ) -> ra_syntax::TreeArc<ast::MacroStmts> {
215 let expanded = expand(rules, invocation);
216 token_tree_to_macro_stmts(&expanded).unwrap()
217 }
218
219 pub(crate) fn expand_to_expr(
220 rules: &MacroRules,
221 invocation: &str,
222 ) -> ra_syntax::TreeArc<ast::Expr> {
223 let expanded = expand(rules, invocation);
224 token_tree_to_expr(&expanded).unwrap()
225 }
226
227 pub(crate) fn assert_expansion(
228 rules: &MacroRules,
229 invocation: &str,
230 expansion: &str,
231 ) -> tt::Subtree {
211 let expanded = expand(rules, invocation); 232 let expanded = expand(rules, invocation);
212 assert_eq!(expanded.to_string(), expansion); 233 assert_eq!(expanded.to_string(), expansion);
213 234
@@ -224,6 +245,8 @@ impl_froms!(TokenTree: Leaf, Subtree);
224 245
225 let file = file.replace("C_C__C", "$crate"); 246 let file = file.replace("C_C__C", "$crate");
226 assert_eq!(tree, file,); 247 assert_eq!(tree, file,);
248
249 expanded
227 } 250 }
228 251
229 #[test] 252 #[test]
@@ -638,7 +661,7 @@ MACRO_ITEMS@[0; 40)
638 ); 661 );
639 662
640 assert_eq!( 663 assert_eq!(
641 expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), 664 expand_to_items(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(),
642 r#"MACRO_ITEMS@[0; 15) 665 r#"MACRO_ITEMS@[0; 15)
643 FN_DEF@[0; 15) 666 FN_DEF@[0; 15)
644 FN_KW@[0; 2) "fn" 667 FN_KW@[0; 2) "fn"
@@ -914,6 +937,82 @@ MACRO_ITEMS@[0; 40)
914"#, 937"#,
915 ); 938 );
916 assert_expansion(&rules, r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#); 939 assert_expansion(&rules, r#"vec!();"#, r#"{let mut v = Vec :: new () ; v}"#);
940 assert_expansion(
941 &rules,
942 r#"vec![1u32,2]"#,
943 r#"{let mut v = Vec :: new () ; v . push (1u32) ; v . push (2) ; v}"#,
944 );
945
946 println!("{}", expand_to_expr(&rules, r#"vec![1u32,2]"#).syntax().debug_dump().trim());
947
948 assert_eq!(
949 expand_to_expr(&rules, r#"vec![1u32,2]"#).syntax().debug_dump().trim(),
950 r#"MACRO_STMTS@[0; 45)
951 EXPR_STMT@[0; 45)
952 BLOCK_EXPR@[0; 45)
953 BLOCK@[0; 45)
954 L_CURLY@[0; 1) "{"
955 LET_STMT@[1; 20)
956 LET_KW@[1; 4) "let"
957 BIND_PAT@[4; 8)
958 MUT_KW@[4; 7) "mut"
959 NAME@[7; 8)
960 IDENT@[7; 8) "v"
961 EQ@[8; 9) "="
962 CALL_EXPR@[9; 19)
963 PATH_EXPR@[9; 17)
964 PATH@[9; 17)
965 PATH@[9; 12)
966 PATH_SEGMENT@[9; 12)
967 NAME_REF@[9; 12)
968 IDENT@[9; 12) "Vec"
969 COLONCOLON@[12; 14) "::"
970 PATH_SEGMENT@[14; 17)
971 NAME_REF@[14; 17)
972 IDENT@[14; 17) "new"
973 ARG_LIST@[17; 19)
974 L_PAREN@[17; 18) "("
975 R_PAREN@[18; 19) ")"
976 SEMI@[19; 20) ";"
977 EXPR_STMT@[20; 33)
978 METHOD_CALL_EXPR@[20; 32)
979 PATH_EXPR@[20; 21)
980 PATH@[20; 21)
981 PATH_SEGMENT@[20; 21)
982 NAME_REF@[20; 21)
983 IDENT@[20; 21) "v"
984 DOT@[21; 22) "."
985 NAME_REF@[22; 26)
986 IDENT@[22; 26) "push"
987 ARG_LIST@[26; 32)
988 L_PAREN@[26; 27) "("
989 LITERAL@[27; 31)
990 INT_NUMBER@[27; 31) "1u32"
991 R_PAREN@[31; 32) ")"
992 SEMI@[32; 33) ";"
993 EXPR_STMT@[33; 43)
994 METHOD_CALL_EXPR@[33; 42)
995 PATH_EXPR@[33; 34)
996 PATH@[33; 34)
997 PATH_SEGMENT@[33; 34)
998 NAME_REF@[33; 34)
999 IDENT@[33; 34) "v"
1000 DOT@[34; 35) "."
1001 NAME_REF@[35; 39)
1002 IDENT@[35; 39) "push"
1003 ARG_LIST@[39; 42)
1004 L_PAREN@[39; 40) "("
1005 LITERAL@[40; 41)
1006 INT_NUMBER@[40; 41) "2"
1007 R_PAREN@[41; 42) ")"
1008 SEMI@[42; 43) ";"
1009 PATH_EXPR@[43; 44)
1010 PATH@[43; 44)
1011 PATH_SEGMENT@[43; 44)
1012 NAME_REF@[43; 44)
1013 IDENT@[43; 44) "v"
1014 R_CURLY@[44; 45) "}""#
1015 );
917 } 1016 }
918 1017
919 #[test] 1018 #[test]