aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_mbe/src/lib.rs11
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs27
2 files changed, 24 insertions, 14 deletions
diff --git a/crates/ra_mbe/src/lib.rs b/crates/ra_mbe/src/lib.rs
index a29b07aba..3a9db7835 100644
--- a/crates/ra_mbe/src/lib.rs
+++ b/crates/ra_mbe/src/lib.rs
@@ -204,7 +204,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
204 invocation: &str, 204 invocation: &str,
205 ) -> ra_syntax::TreeArc<ast::MacroItems> { 205 ) -> ra_syntax::TreeArc<ast::MacroItems> {
206 let expanded = expand(rules, invocation); 206 let expanded = expand(rules, invocation);
207 token_tree_to_macro_items(&expanded) 207 token_tree_to_macro_items(&expanded).unwrap()
208 } 208 }
209 209
210 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) { 210 pub(crate) fn assert_expansion(rules: &MacroRules, invocation: &str, expansion: &str) {
@@ -218,7 +218,10 @@ impl_froms!(TokenTree: Leaf, Subtree);
218 let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0; 218 let expansion = syntax_node_to_token_tree(expansion.syntax()).unwrap().0;
219 let file = token_tree_to_macro_items(&expansion); 219 let file = token_tree_to_macro_items(&expansion);
220 220
221 assert_eq!(tree.syntax().debug_dump().trim(), file.syntax().debug_dump().trim()); 221 assert_eq!(
222 tree.unwrap().syntax().debug_dump().trim(),
223 file.unwrap().syntax().debug_dump().trim()
224 );
222 } 225 }
223 226
224 #[test] 227 #[test]
@@ -358,7 +361,7 @@ impl_froms!(TokenTree: Leaf, Subtree);
358 let expansion = expand(&rules, "structs!(Foo, Bar)"); 361 let expansion = expand(&rules, "structs!(Foo, Bar)");
359 let tree = token_tree_to_macro_items(&expansion); 362 let tree = token_tree_to_macro_items(&expansion);
360 assert_eq!( 363 assert_eq!(
361 tree.syntax().debug_dump().trim(), 364 tree.unwrap().syntax().debug_dump().trim(),
362 r#" 365 r#"
363MACRO_ITEMS@[0; 40) 366MACRO_ITEMS@[0; 40)
364 STRUCT_DEF@[0; 20) 367 STRUCT_DEF@[0; 20)
@@ -472,7 +475,7 @@ MACRO_ITEMS@[0; 40)
472 let stmts = token_tree_to_macro_stmts(&expanded); 475 let stmts = token_tree_to_macro_stmts(&expanded);
473 476
474 assert_eq!( 477 assert_eq!(
475 stmts.syntax().debug_dump().trim(), 478 stmts.unwrap().syntax().debug_dump().trim(),
476 r#"MACRO_STMTS@[0; 15) 479 r#"MACRO_STMTS@[0; 15)
477 LET_STMT@[0; 7) 480 LET_STMT@[0; 7)
478 LET_KW@[0; 3) "let" 481 LET_KW@[0; 3) "let"
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 6af3b1995..5844d3f12 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -5,6 +5,7 @@ use ra_syntax::{
5}; 5};
6 6
7use crate::subtree_source::{SubtreeTokenSource, Querier}; 7use crate::subtree_source::{SubtreeTokenSource, Querier};
8use crate::ExpandError;
8 9
9/// Maps `tt::TokenId` to the relative range of the original token. 10/// Maps `tt::TokenId` to the relative range of the original token.
10#[derive(Default)] 11#[derive(Default)]
@@ -45,48 +46,54 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
45// 46//
46 47
47/// Parses the token tree (result of macro expansion) to an expression 48/// Parses the token tree (result of macro expansion) to an expression
48pub fn token_tree_to_expr(tt: &tt::Subtree) -> TreeArc<ast::Expr> { 49pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> {
49 let token_source = SubtreeTokenSource::new(tt); 50 let token_source = SubtreeTokenSource::new(tt);
50 let mut tree_sink = TtTreeSink::new(token_source.querier()); 51 let mut tree_sink = TtTreeSink::new(token_source.querier());
51 ra_parser::parse_expr(&token_source, &mut tree_sink); 52 ra_parser::parse_expr(&token_source, &mut tree_sink);
52 let syntax = tree_sink.inner.finish(); 53 let syntax = tree_sink.inner.finish();
53 ast::Expr::cast(&syntax).unwrap().to_owned() 54 ast::Expr::cast(&syntax)
55 .map(|m| m.to_owned())
56 .ok_or_else(|| crate::ExpandError::ConversionError)
54} 57}
55 58
56/// Parses the token tree (result of macro expansion) to a Pattern 59/// Parses the token tree (result of macro expansion) to a Pattern
57pub fn token_tree_to_pat(tt: &tt::Subtree) -> TreeArc<ast::Pat> { 60pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> {
58 let token_source = SubtreeTokenSource::new(tt); 61 let token_source = SubtreeTokenSource::new(tt);
59 let mut tree_sink = TtTreeSink::new(token_source.querier()); 62 let mut tree_sink = TtTreeSink::new(token_source.querier());
60 ra_parser::parse_pat(&token_source, &mut tree_sink); 63 ra_parser::parse_pat(&token_source, &mut tree_sink);
61 let syntax = tree_sink.inner.finish(); 64 let syntax = tree_sink.inner.finish();
62 ast::Pat::cast(&syntax).unwrap().to_owned() 65 ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
63} 66}
64 67
65/// Parses the token tree (result of macro expansion) to a Type 68/// Parses the token tree (result of macro expansion) to a Type
66pub fn token_tree_to_ty(tt: &tt::Subtree) -> TreeArc<ast::TypeRef> { 69pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> {
67 let token_source = SubtreeTokenSource::new(tt); 70 let token_source = SubtreeTokenSource::new(tt);
68 let mut tree_sink = TtTreeSink::new(token_source.querier()); 71 let mut tree_sink = TtTreeSink::new(token_source.querier());
69 ra_parser::parse_ty(&token_source, &mut tree_sink); 72 ra_parser::parse_ty(&token_source, &mut tree_sink);
70 let syntax = tree_sink.inner.finish(); 73 let syntax = tree_sink.inner.finish();
71 ast::TypeRef::cast(&syntax).unwrap().to_owned() 74 ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
72} 75}
73 76
74/// Parses the token tree (result of macro expansion) as a sequence of stmts 77/// 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> { 78pub fn token_tree_to_macro_stmts(
79 tt: &tt::Subtree,
80) -> Result<TreeArc<ast::MacroStmts>, ExpandError> {
76 let token_source = SubtreeTokenSource::new(tt); 81 let token_source = SubtreeTokenSource::new(tt);
77 let mut tree_sink = TtTreeSink::new(token_source.querier()); 82 let mut tree_sink = TtTreeSink::new(token_source.querier());
78 ra_parser::parse_macro_stmts(&token_source, &mut tree_sink); 83 ra_parser::parse_macro_stmts(&token_source, &mut tree_sink);
79 let syntax = tree_sink.inner.finish(); 84 let syntax = tree_sink.inner.finish();
80 ast::MacroStmts::cast(&syntax).unwrap().to_owned() 85 ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
81} 86}
82 87
83/// Parses the token tree (result of macro expansion) as a sequence of items 88/// Parses the token tree (result of macro expansion) as a sequence of items
84pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> TreeArc<ast::MacroItems> { 89pub fn token_tree_to_macro_items(
90 tt: &tt::Subtree,
91) -> Result<TreeArc<ast::MacroItems>, ExpandError> {
85 let token_source = SubtreeTokenSource::new(tt); 92 let token_source = SubtreeTokenSource::new(tt);
86 let mut tree_sink = TtTreeSink::new(token_source.querier()); 93 let mut tree_sink = TtTreeSink::new(token_source.querier());
87 ra_parser::parse_macro_items(&token_source, &mut tree_sink); 94 ra_parser::parse_macro_items(&token_source, &mut tree_sink);
88 let syntax = tree_sink.inner.finish(); 95 let syntax = tree_sink.inner.finish();
89 ast::MacroItems::cast(&syntax).unwrap().to_owned() 96 ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
90} 97}
91 98
92/// Parses the token tree (result of macro expansion) as a sequence of items 99/// Parses the token tree (result of macro expansion) as a sequence of items