diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ra_mbe/src/lib.rs | 101 |
1 files changed, 98 insertions, 3 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs index e78bc734b..eedc0c5dd 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,78 @@ 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 | assert_eq!( | ||
947 | expand_to_expr(&rules, r#"vec![1u32,2]"#).syntax().debug_dump().trim(), | ||
948 | r#"BLOCK_EXPR@[0; 45) | ||
949 | BLOCK@[0; 45) | ||
950 | L_CURLY@[0; 1) "{" | ||
951 | LET_STMT@[1; 20) | ||
952 | LET_KW@[1; 4) "let" | ||
953 | BIND_PAT@[4; 8) | ||
954 | MUT_KW@[4; 7) "mut" | ||
955 | NAME@[7; 8) | ||
956 | IDENT@[7; 8) "v" | ||
957 | EQ@[8; 9) "=" | ||
958 | CALL_EXPR@[9; 19) | ||
959 | PATH_EXPR@[9; 17) | ||
960 | PATH@[9; 17) | ||
961 | PATH@[9; 12) | ||
962 | PATH_SEGMENT@[9; 12) | ||
963 | NAME_REF@[9; 12) | ||
964 | IDENT@[9; 12) "Vec" | ||
965 | COLONCOLON@[12; 14) "::" | ||
966 | PATH_SEGMENT@[14; 17) | ||
967 | NAME_REF@[14; 17) | ||
968 | IDENT@[14; 17) "new" | ||
969 | ARG_LIST@[17; 19) | ||
970 | L_PAREN@[17; 18) "(" | ||
971 | R_PAREN@[18; 19) ")" | ||
972 | SEMI@[19; 20) ";" | ||
973 | EXPR_STMT@[20; 33) | ||
974 | METHOD_CALL_EXPR@[20; 32) | ||
975 | PATH_EXPR@[20; 21) | ||
976 | PATH@[20; 21) | ||
977 | PATH_SEGMENT@[20; 21) | ||
978 | NAME_REF@[20; 21) | ||
979 | IDENT@[20; 21) "v" | ||
980 | DOT@[21; 22) "." | ||
981 | NAME_REF@[22; 26) | ||
982 | IDENT@[22; 26) "push" | ||
983 | ARG_LIST@[26; 32) | ||
984 | L_PAREN@[26; 27) "(" | ||
985 | LITERAL@[27; 31) | ||
986 | INT_NUMBER@[27; 31) "1u32" | ||
987 | R_PAREN@[31; 32) ")" | ||
988 | SEMI@[32; 33) ";" | ||
989 | EXPR_STMT@[33; 43) | ||
990 | METHOD_CALL_EXPR@[33; 42) | ||
991 | PATH_EXPR@[33; 34) | ||
992 | PATH@[33; 34) | ||
993 | PATH_SEGMENT@[33; 34) | ||
994 | NAME_REF@[33; 34) | ||
995 | IDENT@[33; 34) "v" | ||
996 | DOT@[34; 35) "." | ||
997 | NAME_REF@[35; 39) | ||
998 | IDENT@[35; 39) "push" | ||
999 | ARG_LIST@[39; 42) | ||
1000 | L_PAREN@[39; 40) "(" | ||
1001 | LITERAL@[40; 41) | ||
1002 | INT_NUMBER@[40; 41) "2" | ||
1003 | R_PAREN@[41; 42) ")" | ||
1004 | SEMI@[42; 43) ";" | ||
1005 | PATH_EXPR@[43; 44) | ||
1006 | PATH@[43; 44) | ||
1007 | PATH_SEGMENT@[43; 44) | ||
1008 | NAME_REF@[43; 44) | ||
1009 | IDENT@[43; 44) "v" | ||
1010 | R_CURLY@[44; 45) "}""# | ||
1011 | ); | ||
917 | } | 1012 | } |
918 | 1013 | ||
919 | #[test] | 1014 | #[test] |