aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-07-18 21:19:04 +0100
committerAleksey Kladov <[email protected]>2019-07-18 21:19:04 +0100
commitdf33e7685bdb0f63bf6aa809b9046708d563a1a7 (patch)
tree2336c03a0eeef98ac375868bd27dfe7e50668869 /crates/ra_mbe/src/syntax_bridge.rs
parenta6224f36200c768d49b6450204fd95edaa559b50 (diff)
use Parse in mbe
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs52
1 files changed, 23 insertions, 29 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 {