aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_mbe/src/lib.rs57
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs46
-rw-r--r--crates/ra_parser/src/grammar.rs20
-rw-r--r--crates/ra_parser/src/grammar/expressions.rs44
-rw-r--r--crates/ra_parser/src/lib.rs4
5 files changed, 156 insertions, 15 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);
diff --git a/crates/ra_parser/src/grammar.rs b/crates/ra_parser/src/grammar.rs
index 1adc27b80..e1762633e 100644
--- a/crates/ra_parser/src/grammar.rs
+++ b/crates/ra_parser/src/grammar.rs
@@ -55,6 +55,21 @@ pub(crate) fn macro_items(p: &mut Parser) {
55 m.complete(p, MACRO_ITEMS); 55 m.complete(p, MACRO_ITEMS);
56} 56}
57 57
58pub(crate) fn macro_stmts(p: &mut Parser) {
59 let m = p.start();
60
61 while !p.at(EOF) {
62 if p.current() == SEMI {
63 p.bump();
64 continue;
65 }
66
67 expressions::stmt(p, expressions::StmtWithSemi::Optional);
68 }
69
70 m.complete(p, MACRO_STMTS);
71}
72
58pub(crate) fn path(p: &mut Parser) { 73pub(crate) fn path(p: &mut Parser) {
59 paths::type_path(p); 74 paths::type_path(p);
60} 75}
@@ -72,6 +87,11 @@ pub(crate) fn pattern(p: &mut Parser) {
72} 87}
73 88
74pub(crate) fn stmt(p: &mut Parser, with_semi: bool) { 89pub(crate) fn stmt(p: &mut Parser, with_semi: bool) {
90 let with_semi = match with_semi {
91 true => expressions::StmtWithSemi::Yes,
92 false => expressions::StmtWithSemi::No,
93 };
94
75 expressions::stmt(p, with_semi) 95 expressions::stmt(p, with_semi)
76} 96}
77 97
diff --git a/crates/ra_parser/src/grammar/expressions.rs b/crates/ra_parser/src/grammar/expressions.rs
index 06f2b45b1..8df9035e9 100644
--- a/crates/ra_parser/src/grammar/expressions.rs
+++ b/crates/ra_parser/src/grammar/expressions.rs
@@ -4,6 +4,12 @@ pub(crate) use self::atom::match_arm_list;
4pub(super) use self::atom::{literal, LITERAL_FIRST}; 4pub(super) use self::atom::{literal, LITERAL_FIRST};
5use super::*; 5use super::*;
6 6
7pub(super) enum StmtWithSemi {
8 Yes,
9 No,
10 Optional,
11}
12
7const EXPR_FIRST: TokenSet = LHS_FIRST; 13const EXPR_FIRST: TokenSet = LHS_FIRST;
8 14
9pub(super) fn expr(p: &mut Parser) -> BlockLike { 15pub(super) fn expr(p: &mut Parser) -> BlockLike {
@@ -48,7 +54,7 @@ fn is_expr_stmt_attr_allowed(kind: SyntaxKind) -> bool {
48 } 54 }
49} 55}
50 56
51pub(super) fn stmt(p: &mut Parser, with_semi: bool) { 57pub(super) fn stmt(p: &mut Parser, with_semi: StmtWithSemi) {
52 // test block_items 58 // test block_items
53 // fn a() { fn b() {} } 59 // fn a() { fn b() {} }
54 let m = p.start(); 60 let m = p.start();
@@ -111,13 +117,23 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) {
111 // } 117 // }
112 // test!{} 118 // test!{}
113 // } 119 // }
114 if with_semi { 120
115 if blocklike.is_block() { 121 match with_semi {
116 p.eat(SEMI); 122 StmtWithSemi::Yes => {
117 } else { 123 if blocklike.is_block() {
118 p.expect(SEMI); 124 p.eat(SEMI);
125 } else {
126 p.expect(SEMI);
127 }
128 }
129 StmtWithSemi::No => {}
130 StmtWithSemi::Optional => {
131 if p.at(SEMI) {
132 p.eat(SEMI);
133 }
119 } 134 }
120 } 135 }
136
121 m.complete(p, EXPR_STMT); 137 m.complete(p, EXPR_STMT);
122 } 138 }
123 139
@@ -128,7 +144,7 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) {
128 // let c = 92; 144 // let c = 92;
129 // let d: i32 = 92; 145 // let d: i32 = 92;
130 // } 146 // }
131 fn let_stmt(p: &mut Parser, m: Marker, with_semi: bool) { 147 fn let_stmt(p: &mut Parser, m: Marker, with_semi: StmtWithSemi) {
132 assert!(p.at(LET_KW)); 148 assert!(p.at(LET_KW));
133 p.bump(); 149 p.bump();
134 patterns::pattern(p); 150 patterns::pattern(p);
@@ -139,8 +155,16 @@ pub(super) fn stmt(p: &mut Parser, with_semi: bool) {
139 expressions::expr(p); 155 expressions::expr(p);
140 } 156 }
141 157
142 if with_semi { 158 match with_semi {
143 p.expect(SEMI); 159 StmtWithSemi::Yes => {
160 p.expect(SEMI);
161 }
162 StmtWithSemi::No => {}
163 StmtWithSemi::Optional => {
164 if p.at(SEMI) {
165 p.eat(SEMI);
166 }
167 }
144 } 168 }
145 m.complete(p, LET_STMT); 169 m.complete(p, LET_STMT);
146 } 170 }
@@ -160,7 +184,7 @@ pub(crate) fn expr_block_contents(p: &mut Parser) {
160 continue; 184 continue;
161 } 185 }
162 186
163 stmt(p, true) 187 stmt(p, StmtWithSemi::Yes)
164 } 188 }
165} 189}
166 190
diff --git a/crates/ra_parser/src/lib.rs b/crates/ra_parser/src/lib.rs
index d6bcc4d8c..0ea942b6e 100644
--- a/crates/ra_parser/src/lib.rs
+++ b/crates/ra_parser/src/lib.rs
@@ -102,6 +102,10 @@ pub fn parse_macro_items(token_source: &dyn TokenSource, tree_sink: &mut dyn Tre
102 parse_from_tokens(token_source, tree_sink, grammar::macro_items); 102 parse_from_tokens(token_source, tree_sink, grammar::macro_items);
103} 103}
104 104
105pub fn parse_macro_stmts(token_source: &dyn TokenSource, tree_sink: &mut dyn TreeSink) {
106 parse_from_tokens(token_source, tree_sink, grammar::macro_stmts);
107}
108
105/// A parsing function for a specific braced-block. 109/// A parsing function for a specific braced-block.
106pub struct Reparser(fn(&mut parser::Parser)); 110pub struct Reparser(fn(&mut parser::Parser));
107 111