aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-12 18:50:05 +0100
committerEdwin Cheng <[email protected]>2019-04-12 18:50:05 +0100
commitf66300ccd1e6ef05b633cda06c87f913d1c91a1e (patch)
treebfa2e896c1da845dd04e43bb0973a156a310bcd5 /crates/ra_mbe/src/lib.rs
parent74e846b9ecffd819af3109c50e48517b560b17cf (diff)
Remove skip Delimiter::None and handle Dollars
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs57
1 files changed, 55 insertions, 2 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index a21ea4dbc..4126854d1 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -39,7 +39,7 @@ pub enum ExpandError {
39 BindingError(String), 39 BindingError(String),
40} 40}
41 41
42pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list}; 42pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list, syntax_node_to_token_tree};
43 43
44/// This struct contains AST for a single `macro_rules` definition. What might 44/// This struct contains AST for a single `macro_rules` definition. What might
45/// be very confusing is that AST has almost exactly the same shape as 45/// be very confusing is that AST has almost exactly the same shape as
@@ -192,6 +192,15 @@ impl_froms!(TokenTree: Leaf, Subtree);
192 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 192 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
193 let expanded = expand(rules, invocation); 193 let expanded = expand(rules, invocation);
194 assert_eq!(expanded.to_string(), expansion); 194 assert_eq!(expanded.to_string(), expansion);
195
196 let tree = token_tree_to_ast_item_list(&expanded);
197
198 // Eat all white space by parse it back and forth
199 let expansion = ast::SourceFile::parse(expansion);
200 let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0;
201 let file = token_tree_to_ast_item_list(&expansion);
202
203 assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim());
195 } 204 }
196 205
197 #[test] 206 #[test]
@@ -288,6 +297,36 @@ impl_froms!(TokenTree: Leaf, Subtree);
288 } 297 }
289 298
290 #[test] 299 #[test]
300 fn test_match_group_pattern_with_multiple_defs() {
301 let rules = create_rules(
302 r#"
303 macro_rules! foo {
304 ($ ($ i:ident),*) => ( struct Bar { $ (
305 fn $ i {}
306 )*} );
307 }
308"#,
309 );
310
311 assert_expansion(&rules, "foo! { foo, bar }", "struct Bar {fn foo {} fn bar {}}");
312 }
313
314 #[test]
315 fn test_match_group_pattern_with_multiple_statement() {
316 let rules = create_rules(
317 r#"
318 macro_rules! foo {
319 ($ ($ i:ident),*) => ( fn baz { $ (
320 $ i ();
321 )*} );
322 }
323"#,
324 );
325
326 assert_expansion(&rules, "foo! { foo, bar }", "fn baz {foo () ; bar () ;}");
327 }
328
329 #[test]
291 fn expand_to_item_list() { 330 fn expand_to_item_list() {
292 let rules = create_rules( 331 let rules = create_rules(
293 " 332 "
@@ -415,7 +454,7 @@ SOURCE_FILE@[0; 40)
415 assert_expansion( 454 assert_expansion(
416 &rules, 455 &rules,
417 "foo! { bar::<u8>::baz::<u8> }", 456 "foo! { bar::<u8>::baz::<u8> }",
418 "fn foo () {let a = bar ::< u8 > ::baz ::< u8 > ;}", 457 "fn foo () {let a = bar :: < u8 > :: baz :: < u8 > ;}",
419 ); 458 );
420 } 459 }
421 460
@@ -432,4 +471,18 @@ SOURCE_FILE@[0; 40)
432 ); 471 );
433 assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); 472 assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}");
434 } 473 }
474
475 #[test]
476 fn test_path_with_path() {
477 let rules = create_rules(
478 r#"
479 macro_rules! foo {
480 ($ i:path) => {
481 fn foo() { let a = $ i :: bar; }
482 }
483 }
484"#,
485 );
486 assert_expansion(&rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}");
487 }
435} 488}