aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src
diff options
context:
space:
mode:
authorEdwin Cheng <[email protected]>2019-04-18 19:47:29 +0100
committerEdwin Cheng <[email protected]>2019-04-18 19:47:29 +0100
commit3ff5440a503f090032136c37c3d44375d6107db1 (patch)
tree6da9635a94f8fe11c74eb06ceec89cf215dbe78c /crates/ra_mbe/src
parent403cd78baee7e9c2410d04ca0304575e7bbab16d (diff)
Add MacroItems and MacroStmts in grammer.ron
Diffstat (limited to 'crates/ra_mbe/src')
-rw-r--r--crates/ra_mbe/src/lib.rs22
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs23
2 files changed, 37 insertions, 8 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index 2f47e32d3..4cfa1f955 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -37,9 +37,15 @@ pub enum ExpandError {
37 NoMatchingRule, 37 NoMatchingRule,
38 UnexpectedToken, 38 UnexpectedToken,
39 BindingError(String), 39 BindingError(String),
40 ConversionError,
40} 41}
41 42
42pub use crate::syntax_bridge::{ast_to_token_tree, token_tree_to_ast_item_list, syntax_node_to_token_tree}; 43pub use crate::syntax_bridge::{
44 ast_to_token_tree,
45 token_tree_to_ast_item_list,
46 syntax_node_to_token_tree,
47 token_tree_to_macro_items,
48};
43 49
44/// This struct contains AST for a single `macro_rules` definition. What might 50/// 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 51/// be very confusing is that AST has almost exactly the same shape as
@@ -192,21 +198,21 @@ impl_froms!(TokenTree: Leaf, Subtree);
192 pub(crate) fn expand_to_syntax( 198 pub(crate) fn expand_to_syntax(
193 rules: &MacroRules, 199 rules: &MacroRules,
194 invocation: &str, 200 invocation: &str,
195 ) -> ra_syntax::TreeArc<ast::SourceFile> { 201 ) -> ra_syntax::TreeArc<ast::MacroItems> {
196 let expanded = expand(rules, invocation); 202 let expanded = expand(rules, invocation);
197 token_tree_to_ast_item_list(&expanded) 203 token_tree_to_macro_items(&expanded)
198 } 204 }
199 205
200 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 206 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
201 let expanded = expand(rules, invocation); 207 let expanded = expand(rules, invocation);
202 assert_eq!(expanded.to_string(), expansion); 208 assert_eq!(expanded.to_string(), expansion);
203 209
204 let tree = token_tree_to_ast_item_list(&expanded); 210 let tree = token_tree_to_macro_items(&expanded);
205 211
206 // Eat all white space by parse it back and forth 212 // Eat all white space by parse it back and forth
207 let expansion = ast::SourceFile::parse(expansion); 213 let expansion = ast::SourceFile::parse(expansion);
208 let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; 214 let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0;
209 let file = token_tree_to_ast_item_list(&expansion); 215 let file = token_tree_to_macro_items(&expansion);
210 216
211 assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim()); 217 assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim());
212 } 218 }
@@ -346,11 +352,11 @@ impl_froms!(TokenTree: Leaf, Subtree);
346 ", 352 ",
347 ); 353 );
348 let expansion = expand(&rules, "structs!(Foo, Bar)"); 354 let expansion = expand(&rules, "structs!(Foo, Bar)");
349 let tree = token_tree_to_ast_item_list(&expansion); 355 let tree = token_tree_to_macro_items(&expansion);
350 assert_eq!( 356 assert_eq!(
351 tree.syntax().debug_dump().trim(), 357 tree.syntax().debug_dump().trim(),
352 r#" 358 r#"
353SOURCE_FILE@[0; 40) 359MACRO_ITEMS@[0; 40)
354 STRUCT_DEF@[0; 20) 360 STRUCT_DEF@[0; 20)
355 STRUCT_KW@[0; 6) "struct" 361 STRUCT_KW@[0; 6) "struct"
356 NAME@[6; 9) 362 NAME@[6; 9)
@@ -527,7 +533,7 @@ SOURCE_FILE@[0; 40)
527 533
528 assert_eq!( 534 assert_eq!(
529 expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(), 535 expand_to_syntax(&rules, "foo! { 1 + 1 }").syntax().debug_dump().trim(),
530 r#"SOURCE_FILE@[0; 15) 536 r#"MACRO_ITEMS@[0; 15)
531 FN_DEF@[0; 15) 537 FN_DEF@[0; 15)
532 FN_KW@[0; 2) "fn" 538 FN_KW@[0; 2) "fn"
533 NAME@[2; 5) 539 NAME@[2; 5)
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 28ded7870..19e09be80 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -30,6 +30,29 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
30 Some((tt, token_map)) 30 Some((tt, token_map))
31} 31}
32 32
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
35// * Expr(P<ast::Expr>)
36// * Pat(P<ast::Pat>)
37// * Ty(P<ast::Ty>)
38// * Stmts(SmallVec<[ast::Stmt; 1]>)
39// * Items(SmallVec<[P<ast::Item>; 1]>)
40//
41// * TraitItems(SmallVec<[ast::TraitItem; 1]>)
42// * ImplItems(SmallVec<[ast::ImplItem; 1]>)
43// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
44//
45//
46
47/// 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> {
49 let token_source = SubtreeTokenSource::new(tt);
50 let mut tree_sink = TtTreeSink::new(token_source.querier());
51 ra_parser::parse_macro_items(&token_source, &mut tree_sink);
52 let syntax = tree_sink.inner.finish();
53 ast::MacroItems::cast(&syntax).unwrap().to_owned()
54}
55
33/// Parses the token tree (result of macro expansion) as a sequence of items 56/// Parses the token tree (result of macro expansion) as a sequence of items
34pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { 57pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> {
35 let token_source = SubtreeTokenSource::new(tt); 58 let token_source = SubtreeTokenSource::new(tt);