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.rs22
1 files changed, 14 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)