aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs52
-rw-r--r--crates/ra_mbe/src/tests.rs30
2 files changed, 38 insertions, 44 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 7560d215a..bfc351f81 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -2,8 +2,8 @@ use crate::subtree_source::SubtreeTokenSource;
2use crate::ExpandError; 2use crate::ExpandError;
3use ra_parser::{ParseError, TreeSink}; 3use ra_parser::{ParseError, TreeSink};
4use ra_syntax::{ 4use ra_syntax::{
5 ast, AstNode, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxTreeBuilder, 5 ast, AstNode, Parse, SmolStr, SyntaxElement, SyntaxKind, SyntaxKind::*, SyntaxNode,
6 TextRange, TextUnit, TreeArc, T, 6 SyntaxTreeBuilder, TextRange, TextUnit, T,
7}; 7};
8use tt::buffer::{Cursor, TokenBuffer}; 8use tt::buffer::{Cursor, TokenBuffer};
9 9
@@ -45,7 +45,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
45// 45//
46// 46//
47 47
48fn token_tree_to_syntax_node<F>(tt: &tt::Subtree, f: F) -> Result<TreeArc<SyntaxNode>, ExpandError> 48fn token_tree_to_syntax_node<F>(tt: &tt::Subtree, f: F) -> Result<Parse<SyntaxNode>, ExpandError>
49where 49where
50 F: Fn(&mut dyn ra_parser::TokenSource, &mut dyn ra_parser::TreeSink), 50 F: Fn(&mut dyn ra_parser::TokenSource, &mut dyn ra_parser::TreeSink),
51{ 51{
@@ -58,50 +58,44 @@ where
58 return Err(ExpandError::ConversionError); 58 return Err(ExpandError::ConversionError);
59 } 59 }
60 //FIXME: would be cool to report errors 60 //FIXME: would be cool to report errors
61 let (tree, _errors) = tree_sink.inner.finish(); 61 let parse = tree_sink.inner.finish();
62 Ok(tree) 62 Ok(parse)
63} 63}
64 64
65/// Parses the token tree (result of macro expansion) to an expression 65/// Parses the token tree (result of macro expansion) to an expression
66pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> { 66pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<Parse<ast::Expr>, ExpandError> {
67 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?; 67 let parse = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?;
68 ast::Expr::cast(&syntax) 68 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
69 .map(|m| m.to_owned())
70 .ok_or_else(|| crate::ExpandError::ConversionError)
71} 69}
72 70
73/// Parses the token tree (result of macro expansion) to a Pattern 71/// Parses the token tree (result of macro expansion) to a Pattern
74pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> { 72pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<Parse<ast::Pat>, ExpandError> {
75 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?; 73 let parse = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?;
76 ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 74 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
77} 75}
78 76
79/// Parses the token tree (result of macro expansion) to a Type 77/// Parses the token tree (result of macro expansion) to a Type
80pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> { 78pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<Parse<ast::TypeRef>, ExpandError> {
81 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?; 79 let parse = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?;
82 ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 80 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
83} 81}
84 82
85/// Parses the token tree (result of macro expansion) as a sequence of stmts 83/// Parses the token tree (result of macro expansion) as a sequence of stmts
86pub fn token_tree_to_macro_stmts( 84pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result<Parse<ast::MacroStmts>, ExpandError> {
87 tt: &tt::Subtree, 85 let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?;
88) -> Result<TreeArc<ast::MacroStmts>, ExpandError> { 86 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
89 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?;
90 ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
91} 87}
92 88
93/// Parses the token tree (result of macro expansion) as a sequence of items 89/// Parses the token tree (result of macro expansion) as a sequence of items
94pub fn token_tree_to_macro_items( 90pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> Result<Parse<ast::MacroItems>, ExpandError> {
95 tt: &tt::Subtree, 91 let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?;
96) -> Result<TreeArc<ast::MacroItems>, ExpandError> { 92 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError)
97 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?;
98 ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
99} 93}
100 94
101/// Parses the token tree (result of macro expansion) as a sequence of items 95/// Parses the token tree (result of macro expansion) as a sequence of items
102pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { 96pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> Parse<ast::SourceFile> {
103 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap(); 97 let parse = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap();
104 ast::SourceFile::cast(&syntax).unwrap().to_owned() 98 parse.cast().unwrap()
105} 99}
106 100
107impl TokenMap { 101impl TokenMap {
diff --git a/crates/ra_mbe/src/tests.rs b/crates/ra_mbe/src/tests.rs
index 5a1494fee..419b2c099 100644
--- a/crates/ra_mbe/src/tests.rs
+++ b/crates/ra_mbe/src/tests.rs
@@ -72,7 +72,7 @@ pub(crate) fn expand_to_items(
72 invocation: &str, 72 invocation: &str,
73) -> ra_syntax::TreeArc<ast::MacroItems> { 73) -> ra_syntax::TreeArc<ast::MacroItems> {
74 let expanded = expand(rules, invocation); 74 let expanded = expand(rules, invocation);
75 token_tree_to_macro_items(&expanded).unwrap() 75 token_tree_to_macro_items(&expanded).unwrap().tree().to_owned()
76} 76}
77 77
78#[allow(unused)] 78#[allow(unused)]
@@ -81,7 +81,7 @@ pub(crate) fn expand_to_stmts(
81 invocation: &str, 81 invocation: &str,
82) -> ra_syntax::TreeArc<ast::MacroStmts> { 82) -> ra_syntax::TreeArc<ast::MacroStmts> {
83 let expanded = expand(rules, invocation); 83 let expanded = expand(rules, invocation);
84 token_tree_to_macro_stmts(&expanded).unwrap() 84 token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned()
85} 85}
86 86
87pub(crate) fn expand_to_expr( 87pub(crate) fn expand_to_expr(
@@ -89,7 +89,7 @@ pub(crate) fn expand_to_expr(
89 invocation: &str, 89 invocation: &str,
90) -> ra_syntax::TreeArc<ast::Expr> { 90) -> ra_syntax::TreeArc<ast::Expr> {
91 let expanded = expand(rules, invocation); 91 let expanded = expand(rules, invocation);
92 token_tree_to_expr(&expanded).unwrap() 92 token_tree_to_expr(&expanded).unwrap().tree().to_owned()
93} 93}
94 94
95pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree { 95pub(crate) fn text_to_tokentree(text: &str) -> tt::Subtree {
@@ -164,22 +164,22 @@ pub(crate) fn assert_expansion(
164 164
165 let (expanded_tree, expected_tree) = match kind { 165 let (expanded_tree, expected_tree) = match kind {
166 MacroKind::Items => { 166 MacroKind::Items => {
167 let expanded_tree = token_tree_to_macro_items(&expanded); 167 let expanded_tree = token_tree_to_macro_items(&expanded).unwrap().tree().to_owned();
168 let expected_tree = token_tree_to_macro_items(&expected); 168 let expected_tree = token_tree_to_macro_items(&expected).unwrap().tree().to_owned();
169 169
170 ( 170 (
171 debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), 171 debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
172 debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), 172 debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(),
173 ) 173 )
174 } 174 }
175 175
176 MacroKind::Stmts => { 176 MacroKind::Stmts => {
177 let expanded_tree = token_tree_to_macro_stmts(&expanded); 177 let expanded_tree = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned();
178 let expected_tree = token_tree_to_macro_stmts(&expected); 178 let expected_tree = token_tree_to_macro_stmts(&expected).unwrap().tree().to_owned();
179 179
180 ( 180 (
181 debug_dump_ignore_spaces(expanded_tree.unwrap().syntax()).trim().to_string(), 181 debug_dump_ignore_spaces(expanded_tree.syntax()).trim().to_string(),
182 debug_dump_ignore_spaces(expected_tree.unwrap().syntax()).trim().to_string(), 182 debug_dump_ignore_spaces(expected_tree.syntax()).trim().to_string(),
183 ) 183 )
184 } 184 }
185 }; 185 };
@@ -419,9 +419,9 @@ fn test_expand_to_item_list() {
419 ", 419 ",
420 ); 420 );
421 let expansion = expand(&rules, "structs!(Foo, Bar);"); 421 let expansion = expand(&rules, "structs!(Foo, Bar);");
422 let tree = token_tree_to_macro_items(&expansion); 422 let tree = token_tree_to_macro_items(&expansion).unwrap().tree().to_owned();
423 assert_eq!( 423 assert_eq!(
424 tree.unwrap().syntax().debug_dump().trim(), 424 tree.syntax().debug_dump().trim(),
425 r#" 425 r#"
426MACRO_ITEMS@[0; 40) 426MACRO_ITEMS@[0; 40)
427 STRUCT_DEF@[0; 20) 427 STRUCT_DEF@[0; 20)
@@ -537,10 +537,10 @@ fn test_tt_to_stmts() {
537 ); 537 );
538 538
539 let expanded = expand(&rules, "foo!{}"); 539 let expanded = expand(&rules, "foo!{}");
540 let stmts = token_tree_to_macro_stmts(&expanded); 540 let stmts = token_tree_to_macro_stmts(&expanded).unwrap().tree().to_owned();
541 541
542 assert_eq!( 542 assert_eq!(
543 stmts.unwrap().syntax().debug_dump().trim(), 543 stmts.syntax().debug_dump().trim(),
544 r#"MACRO_STMTS@[0; 15) 544 r#"MACRO_STMTS@[0; 15)
545 LET_STMT@[0; 7) 545 LET_STMT@[0; 7)
546 LET_KW@[0; 3) "let" 546 LET_KW@[0; 3) "let"