aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs63
1 files changed, 60 insertions, 3 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index cdca3cafb..907402f5f 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -24,7 +24,7 @@ use ra_syntax::SmolStr;
24 24
25pub use tt::{Delimiter, Punct}; 25pub use tt::{Delimiter, Punct};
26 26
27pub use crate::syntax_bridge::ast_to_token_tree; 27pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list};
28 28
29/// This struct contains AST for a single `macro_rules` definition. What might 29/// This struct contains AST for a single `macro_rules` definition. What might
30/// be very confusing is that AST has almost exactly the same shape as 30/// be very confusing is that AST has almost exactly the same shape as
@@ -164,14 +164,18 @@ impl_froms!(TokenTree: Leaf, Subtree);
164 crate::MacroRules::parse(&definition_tt).unwrap() 164 crate::MacroRules::parse(&definition_tt).unwrap()
165 } 165 }
166 166
167 fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 167 fn expand(rules: &MacroRules, invocation: &str) -> tt::Subtree {
168 let source_file = ast::SourceFile::parse(invocation); 168 let source_file = ast::SourceFile::parse(invocation);
169 let macro_invocation = 169 let macro_invocation =
170 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap(); 170 source_file.syntax().descendants().find_map(ast::MacroCall::cast).unwrap();
171 171
172 let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap(); 172 let (invocation_tt, _) = ast_to_token_tree(macro_invocation.token_tree().unwrap()).unwrap();
173 173
174 let expanded = rules.expand(&invocation_tt).unwrap(); 174 rules.expand(&invocation_tt).unwrap()
175 }
176
177 fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
178 let expanded = expand(rules, invocation);
175 assert_eq!(expanded.to_string(), expansion); 179 assert_eq!(expanded.to_string(), expansion);
176 } 180 }
177 181
@@ -268,4 +272,57 @@ impl_froms!(TokenTree: Leaf, Subtree);
268 assert_expansion(&rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;"); 272 assert_expansion(&rules, "foo! { Foo,# Bar }", "struct Foo ; struct Bar ;");
269 } 273 }
270 274
275 #[test]
276 fn expand_to_item_list() {
277 let rules = create_rules(
278 "
279 macro_rules! structs {
280 ($($i:ident),*) => {
281 $(struct $i { field: u32 } )*
282 }
283 }
284 ",
285 );
286 let expansion = expand(&rules, "structs!(Foo, Bar)");
287 let tree = token_tree_to_ast_item_list(&expansion);
288 assert_eq!(
289 tree.syntax().debug_dump().trim(),
290 r#"
291SOURCE_FILE@[0; 40)
292 STRUCT_DEF@[0; 20)
293 STRUCT_KW@[0; 6)
294 NAME@[6; 9)
295 IDENT@[6; 9) "Foo"
296 NAMED_FIELD_DEF_LIST@[9; 20)
297 L_CURLY@[9; 10)
298 NAMED_FIELD_DEF@[10; 19)
299 NAME@[10; 15)
300 IDENT@[10; 15) "field"
301 COLON@[15; 16)
302 PATH_TYPE@[16; 19)
303 PATH@[16; 19)
304 PATH_SEGMENT@[16; 19)
305 NAME_REF@[16; 19)
306 IDENT@[16; 19) "u32"
307 R_CURLY@[19; 20)
308 STRUCT_DEF@[20; 40)
309 STRUCT_KW@[20; 26)
310 NAME@[26; 29)
311 IDENT@[26; 29) "Bar"
312 NAMED_FIELD_DEF_LIST@[29; 40)
313 L_CURLY@[29; 30)
314 NAMED_FIELD_DEF@[30; 39)
315 NAME@[30; 35)
316 IDENT@[30; 35) "field"
317 COLON@[35; 36)
318 PATH_TYPE@[36; 39)
319 PATH@[36; 39)
320 PATH_SEGMENT@[36; 39)
321 NAME_REF@[36; 39)
322 IDENT@[36; 39) "u32"
323 R_CURLY@[39; 40)"#
324 .trim()
325 );
326 }
327
271} 328}