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