aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/lib.rs
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-18 20:49:56 +0100
committerEdwin Cheng <[email protected]>2019-04-18 20:49:56 +0100
commitc0f19d70056fada5f381019694d893e0ffe8360a (patch)
tree4837619368155d49c7540f035c6eb76d8d50ea6b /crates/ra_mbe/src/lib.rs
parent3ff5440a503f090032136c37c3d44375d6107db1 (diff)
Add expr, pat, ty and macro_stmts
Diffstat (limited to 'crates/ra_mbe/src/lib.rs')
-rw-r--r--crates/ra_mbe/src/lib.rs57
1 files changed, 57 insertions, 0 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 4cfa1f955..a29b07aba 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -44,7 +44,11 @@ pub use crate::syntax_bridge::{
44 ast_to_token_tree, 44 ast_to_token_tree,
45 token_tree_to_ast_item_list, 45 token_tree_to_ast_item_list,
46 syntax_node_to_token_tree, 46 syntax_node_to_token_tree,
47 token_tree_to_expr,
48 token_tree_to_pat,
49 token_tree_to_ty,
47 token_tree_to_macro_items, 50 token_tree_to_macro_items,
51 token_tree_to_macro_stmts,
48}; 52};
49 53
50/// This struct contains AST for a single `macro_rules` definition. What might 54/// This struct contains AST for a single `macro_rules` definition. What might
@@ -450,6 +454,59 @@ MACRO_ITEMS@[0; 40)
450 assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}"); 454 assert_expansion(&rules, "foo! { foo, bar }", "fn foo () {let a = foo ; let b = bar ;}");
451 } 455 }
452 456
457 #[test]
458 fn test_tt_to_stmts() {
459 let rules = create_rules(
460 r#"
461 macro_rules! foo {
462 () => {
463 let a = 0;
464 a = 10 + 1;
465 a
466 }
467 }
468"#,
469 );
470
471 let expanded = expand(&rules, "foo!{}");
472 let stmts = token_tree_to_macro_stmts(&expanded);
473
474 assert_eq!(
475 stmts.syntax().debug_dump().trim(),
476 r#"MACRO_STMTS@[0; 15)
477 LET_STMT@[0; 7)
478 LET_KW@[0; 3) "let"
479 BIND_PAT@[3; 4)
480 NAME@[3; 4)
481 IDENT@[3; 4) "a"
482 EQ@[4; 5) "="
483 LITERAL@[5; 6)
484 INT_NUMBER@[5; 6) "0"
485 SEMI@[6; 7) ";"
486 EXPR_STMT@[7; 14)
487 BIN_EXPR@[7; 13)
488 PATH_EXPR@[7; 8)
489 PATH@[7; 8)
490 PATH_SEGMENT@[7; 8)
491 NAME_REF@[7; 8)
492 IDENT@[7; 8) "a"
493 EQ@[8; 9) "="
494 BIN_EXPR@[9; 13)
495 LITERAL@[9; 11)
496 INT_NUMBER@[9; 11) "10"
497 PLUS@[11; 12) "+"
498 LITERAL@[12; 13)
499 INT_NUMBER@[12; 13) "1"
500 SEMI@[13; 14) ";"
501 EXPR_STMT@[14; 15)
502 PATH_EXPR@[14; 15)
503 PATH@[14; 15)
504 PATH_SEGMENT@[14; 15)
505 NAME_REF@[14; 15)
506 IDENT@[14; 15) "a""#,
507 );
508 }
509
453 // The following tests are port from intellij-rust directly 510 // The following tests are port from intellij-rust directly
454 // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt 511 // https://github.com/intellij-rust/intellij-rust/blob/c4e9feee4ad46e7953b1948c112533360b6087bb/src/test/kotlin/org/rust/lang/core/macros/RsMacroExpansionTest.kt
455 512