aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
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
parent3ff5440a503f090032136c37c3d44375d6107db1 (diff)
Add expr, pat, ty and macro_stmts
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/lib.rs57
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs46
2 files changed, 98 insertions, 5 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
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 19e09be80..6af3b1995 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -32,11 +32,11 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
32 32
33// The following items are what `rustc` macro can be parsed into : 33// The following items are what `rustc` macro can be parsed into :
34// link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141 34// link: https://github.com/rust-lang/rust/blob/9ebf47851a357faa4cd97f4b1dc7835f6376e639/src/libsyntax/ext/expand.rs#L141
35// * Expr(P<ast::Expr>) 35// * Expr(P<ast::Expr>) -> token_tree_to_expr
36// * Pat(P<ast::Pat>) 36// * Pat(P<ast::Pat>) -> token_tree_to_pat
37// * Ty(P<ast::Ty>) 37// * Ty(P<ast::Ty>) -> token_tree_to_ty
38// * Stmts(SmallVec<[ast::Stmt; 1]>) 38// * Stmts(SmallVec<[ast::Stmt; 1]>) -> token_tree_to_stmts
39// * Items(SmallVec<[P<ast::Item>; 1]>) 39// * Items(SmallVec<[P<ast::Item>; 1]>) -> token_tree_to_items
40// 40//
41// * TraitItems(SmallVec<[ast::TraitItem; 1]>) 41// * TraitItems(SmallVec<[ast::TraitItem; 1]>)
42// * ImplItems(SmallVec<[ast::ImplItem; 1]>) 42// * ImplItems(SmallVec<[ast::ImplItem; 1]>)
@@ -44,6 +44,42 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
44// 44//
45// 45//
46 46
47/// Parses the token tree (result of macro expansion) to an expression
48pub fn token_tree_to_expr(tt: &tt::Subtree) -> TreeArc<ast::Expr> {
49 let token_source = SubtreeTokenSource::new(tt);
50 let mut tree_sink = TtTreeSink::new(token_source.querier());
51 ra_parser::parse_expr(&token_source, &mut tree_sink);
52 let syntax = tree_sink.inner.finish();
53 ast::Expr::cast(&syntax).unwrap().to_owned()
54}
55
56/// Parses the token tree (result of macro expansion) to a Pattern
57pub fn token_tree_to_pat(tt: &tt::Subtree) -> TreeArc<ast::Pat> {
58 let token_source = SubtreeTokenSource::new(tt);
59 let mut tree_sink = TtTreeSink::new(token_source.querier());
60 ra_parser::parse_pat(&token_source, &mut tree_sink);
61 let syntax = tree_sink.inner.finish();
62 ast::Pat::cast(&syntax).unwrap().to_owned()
63}
64
65/// Parses the token tree (result of macro expansion) to a Type
66pub fn token_tree_to_ty(tt: &tt::Subtree) -> TreeArc<ast::TypeRef> {
67 let token_source = SubtreeTokenSource::new(tt);
68 let mut tree_sink = TtTreeSink::new(token_source.querier());
69 ra_parser::parse_ty(&token_source, &mut tree_sink);
70 let syntax = tree_sink.inner.finish();
71 ast::TypeRef::cast(&syntax).unwrap().to_owned()
72}
73
74/// Parses the token tree (result of macro expansion) as a sequence of stmts
75pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> TreeArc<ast::MacroStmts> {
76 let token_source = SubtreeTokenSource::new(tt);
77 let mut tree_sink = TtTreeSink::new(token_source.querier());
78 ra_parser::parse_macro_stmts(&token_source, &mut tree_sink);
79 let syntax = tree_sink.inner.finish();
80 ast::MacroStmts::cast(&syntax).unwrap().to_owned()
81}
82
47/// Parses the token tree (result of macro expansion) as a sequence of items 83/// Parses the token tree (result of macro expansion) as a sequence of items
48pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc<ast::MacroItems> { 84pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc<ast::MacroItems> {
49 let token_source = SubtreeTokenSource::new(tt); 85 let token_source = SubtreeTokenSource::new(tt);