aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r--crates/ra_mbe/src/syntax_bridge.rs63
1 files changed, 19 insertions, 44 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 0a75305b4..0a7e50c4e 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -45,17 +45,25 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
45// 45//
46// 46//
47 47
48/// Parses the token tree (result of macro expansion) to an expression 48fn token_tree_to_syntax_node<F>(tt: &tt::Subtree, f: F) -> Result<TreeArc<SyntaxNode>, ExpandError>
49pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> { 49where
50 F: Fn(&mut ra_parser::TokenSource, &mut ra_parser::TreeSink),
51{
50 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 52 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]);
51 let token_source = SubtreeTokenSource::new(&buffer); 53 let mut token_source = SubtreeTokenSource::new(&buffer);
52 let mut tree_sink = TtTreeSink::new(token_source.querier()); 54 let querier = token_source.querier();
53 ra_parser::parse_expr(&token_source, &mut tree_sink); 55 let mut tree_sink = TtTreeSink::new(querier.as_ref());
56 f(&mut token_source, &mut tree_sink);
54 if tree_sink.roots.len() != 1 { 57 if tree_sink.roots.len() != 1 {
55 return Err(ExpandError::ConversionError); 58 return Err(ExpandError::ConversionError);
56 } 59 }
57 60
58 let syntax = tree_sink.inner.finish(); 61 Ok(tree_sink.inner.finish())
62}
63
64/// Parses the token tree (result of macro expansion) to an expression
65pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, ExpandError> {
66 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?;
59 ast::Expr::cast(&syntax) 67 ast::Expr::cast(&syntax)
60 .map(|m| m.to_owned()) 68 .map(|m| m.to_owned())
61 .ok_or_else(|| crate::ExpandError::ConversionError) 69 .ok_or_else(|| crate::ExpandError::ConversionError)
@@ -63,28 +71,13 @@ pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<TreeArc<ast::Expr>, Expand
63 71
64/// Parses the token tree (result of macro expansion) to a Pattern 72/// Parses the token tree (result of macro expansion) to a Pattern
65pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> { 73pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<TreeArc<ast::Pat>, ExpandError> {
66 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 74 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?;
67 let token_source = SubtreeTokenSource::new(&buffer);
68 let mut tree_sink = TtTreeSink::new(token_source.querier());
69 ra_parser::parse_pat(&token_source, &mut tree_sink);
70 if tree_sink.roots.len() != 1 {
71 return Err(ExpandError::ConversionError);
72 }
73
74 let syntax = tree_sink.inner.finish();
75 ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 75 ast::Pat::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
76} 76}
77 77
78/// Parses the token tree (result of macro expansion) to a Type 78/// Parses the token tree (result of macro expansion) to a Type
79pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> { 79pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, ExpandError> {
80 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 80 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?;
81 let token_source = SubtreeTokenSource::new(&buffer);
82 let mut tree_sink = TtTreeSink::new(token_source.querier());
83 ra_parser::parse_ty(&token_source, &mut tree_sink);
84 if tree_sink.roots.len() != 1 {
85 return Err(ExpandError::ConversionError);
86 }
87 let syntax = tree_sink.inner.finish();
88 ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 81 ast::TypeRef::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
89} 82}
90 83
@@ -92,14 +85,7 @@ pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<TreeArc<ast::TypeRef>, Expan
92pub fn token_tree_to_macro_stmts( 85pub fn token_tree_to_macro_stmts(
93 tt: &tt::Subtree, 86 tt: &tt::Subtree,
94) -> Result<TreeArc<ast::MacroStmts>, ExpandError> { 87) -> Result<TreeArc<ast::MacroStmts>, ExpandError> {
95 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 88 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?;
96 let token_source = SubtreeTokenSource::new(&buffer);
97 let mut tree_sink = TtTreeSink::new(token_source.querier());
98 ra_parser::parse_macro_stmts(&token_source, &mut tree_sink);
99 if tree_sink.roots.len() != 1 {
100 return Err(ExpandError::ConversionError);
101 }
102 let syntax = tree_sink.inner.finish();
103 ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 89 ast::MacroStmts::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
104} 90}
105 91
@@ -107,24 +93,13 @@ pub fn token_tree_to_macro_stmts(
107pub fn token_tree_to_macro_items( 93pub fn token_tree_to_macro_items(
108 tt: &tt::Subtree, 94 tt: &tt::Subtree,
109) -> Result<TreeArc<ast::MacroItems>, ExpandError> { 95) -> Result<TreeArc<ast::MacroItems>, ExpandError> {
110 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 96 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?;
111 let token_source = SubtreeTokenSource::new(&buffer);
112 let mut tree_sink = TtTreeSink::new(token_source.querier());
113 ra_parser::parse_macro_items(&token_source, &mut tree_sink);
114 if tree_sink.roots.len() != 1 {
115 return Err(ExpandError::ConversionError);
116 }
117 let syntax = tree_sink.inner.finish();
118 ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError) 97 ast::MacroItems::cast(&syntax).map(|m| m.to_owned()).ok_or_else(|| ExpandError::ConversionError)
119} 98}
120 99
121/// Parses the token tree (result of macro expansion) as a sequence of items 100/// Parses the token tree (result of macro expansion) as a sequence of items
122pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> { 101pub fn token_tree_to_ast_item_list(tt: &tt::Subtree) -> TreeArc<ast::SourceFile> {
123 let buffer = tt::buffer::TokenBuffer::new(&[tt.clone().into()]); 102 let syntax = token_tree_to_syntax_node(tt, ra_parser::parse).unwrap();
124 let token_source = SubtreeTokenSource::new(&buffer);
125 let mut tree_sink = TtTreeSink::new(token_source.querier());
126 ra_parser::parse(&token_source, &mut tree_sink);
127 let syntax = tree_sink.inner.finish();
128 ast::SourceFile::cast(&syntax).unwrap().to_owned() 103 ast::SourceFile::cast(&syntax).unwrap().to_owned()
129} 104}
130 105