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.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 4126854d1..a530f3b03 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -189,6 +189,14 @@ impl_froms!(TokenTree: Leaf, Subtree);
189 rules.expand(&invocation_tt).unwrap() 189 rules.expand(&invocation_tt).unwrap()
190 } 190 }
191 191
192 pub(crate) fn expand_to_syntax(
193 rules: &MacroRules,
194 invocation: &str,
195 ) -> ra_syntax::TreeArc<ast::SourceFile> {
196 let expanded = expand(rules, invocation);
197 token_tree_to_ast_item_list(&expanded)
198 }
199
192 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 200 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
193 let expanded = expand(rules, invocation); 201 let expanded = expand(rules, invocation);
194 assert_eq!(expanded.to_string(), expansion); 202 assert_eq!(expanded.to_string(), expansion);
@@ -485,4 +493,93 @@ SOURCE_FILE@[0; 40)
485 ); 493 );
486 assert_expansion(&rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}"); 494 assert_expansion(&rules, "foo! { foo }", "fn foo () {let a = foo :: bar ;}");
487 } 495 }
496
497 #[test]
498 fn test_expr() {
499 let rules = create_rules(
500 r#"
501 macro_rules! foo {
502 ($ i:expr) => {
503 fn bar() { $ i; }
504 }
505 }
506"#,
507 );
508
509 assert_expansion(
510 &rules,
511 "foo! { 2 + 2 * baz(3).quux() }",
512 "fn bar () {2 + 2 * baz (3) . quux () ;}",
513 );
514 }
515
516 #[test]
517 fn test_expr_order() {
518 let rules = create_rules(
519 r#"
520 macro_rules! foo {
521 ($ i:expr) => {
522 fn bar() { $ i * 2; }
523 }
524 }
525"#,
526 );
527
528 assert_eq!(
529 expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(),
530 r#"SOURCE_FILE@[0; 15)
531 FN_DEF@[0; 15)
532 FN_KW@[0; 2) "fn"
533 NAME@[2; 5)
534 IDENT@[2; 5) "bar"
535 PARAM_LIST@[5; 7)
536 L_PAREN@[5; 6) "("
537 R_PAREN@[6; 7) ")"
538 BLOCK@[7; 15)
539 L_CURLY@[7; 8) "{"
540 EXPR_STMT@[8; 14)
541 BIN_EXPR@[8; 13)
542 BIN_EXPR@[8; 11)
543 LITERAL@[8; 9)
544 INT_NUMBER@[8; 9) "1"
545 PLUS@[9; 10) "+"
546 LITERAL@[10; 11)
547 INT_NUMBER@[10; 11) "1"
548 STAR@[11; 12) "*"
549 LITERAL@[12; 13)
550 INT_NUMBER@[12; 13) "2"
551 SEMI@[13; 14) ";"
552 R_CURLY@[14; 15) "}""#,
553 );
554 }
555
556 #[test]
557 fn test_ty() {
558 let rules = create_rules(
559 r#"
560 macro_rules! foo {
561 ($ i:ty) => (
562 fn bar() -> $ i { unimplemented!() }
563 )
564 }
565"#,
566 );
567 assert_expansion(
568 &rules,
569 "foo! { Baz<u8> }",
570 "fn bar () -> Baz < u8 > {unimplemented ! ()}",
571 );
572 }
573
574 #[test]
575 fn test_pat_() {
576 let rules = create_rules(
577 r#"
578 macro_rules! foo {
579 ($ i:pat) => { fn foo() { let $ i; } }
580 }
581"#,
582 );
583 assert_expansion(&rules, "foo! { (a, b) }", "fn foo () {let (a , b) ;}");
584 }
488} 585}