diff options
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 32 |
1 files changed, 26 insertions, 6 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index 8225759e7..a380b1cfd 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs | |||
@@ -1,4 +1,7 @@ | |||
1 | use ra_parser::{ParseError, TreeSink}; | 1 | use ra_parser::{ |
2 | FragmentKind::{self, *}, | ||
3 | ParseError, TreeSink, | ||
4 | }; | ||
2 | use ra_syntax::{ | 5 | use ra_syntax::{ |
3 | ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, | 6 | ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, |
4 | SyntaxTreeBuilder, TextRange, TextUnit, T, | 7 | SyntaxTreeBuilder, TextRange, TextUnit, T, |
@@ -63,33 +66,50 @@ where | |||
63 | Ok(parse) | 66 | Ok(parse) |
64 | } | 67 | } |
65 | 68 | ||
69 | fn fragment_to_syntax_node( | ||
70 | tt: &tt::Subtree, | ||
71 | fragment_kind: FragmentKind, | ||
72 | ) -> Result<Parse<SyntaxNode>, ExpandError> { | ||
73 | let tokens = [tt.clone().into()]; | ||
74 | let buffer = TokenBuffer::new(&tokens); | ||
75 | let mut token_source = SubtreeTokenSource::new(&buffer); | ||
76 | let mut tree_sink = TtTreeSink::new(buffer.begin()); | ||
77 | ra_parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind); | ||
78 | if tree_sink.roots.len() != 1 { | ||
79 | return Err(ExpandError::ConversionError); | ||
80 | } | ||
81 | //FIXME: would be cool to report errors | ||
82 | let parse = tree_sink.inner.finish(); | ||
83 | Ok(parse) | ||
84 | } | ||
85 | |||
66 | /// Parses the token tree (result of macro expansion) to an expression | 86 | /// Parses the token tree (result of macro expansion) to an expression |
67 | pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<Parse<ast::Expr>, ExpandError> { | 87 | pub fn token_tree_to_expr(tt: &tt::Subtree) -> Result<Parse<ast::Expr>, ExpandError> { |
68 | let parse = token_tree_to_syntax_node(tt, ra_parser::parse_expr)?; | 88 | let parse = fragment_to_syntax_node(tt, Expr)?; |
69 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) | 89 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) |
70 | } | 90 | } |
71 | 91 | ||
72 | /// Parses the token tree (result of macro expansion) to a Pattern | 92 | /// Parses the token tree (result of macro expansion) to a Pattern |
73 | pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<Parse<ast::Pat>, ExpandError> { | 93 | pub fn token_tree_to_pat(tt: &tt::Subtree) -> Result<Parse<ast::Pat>, ExpandError> { |
74 | let parse = token_tree_to_syntax_node(tt, ra_parser::parse_pat)?; | 94 | let parse = fragment_to_syntax_node(tt, Pattern)?; |
75 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) | 95 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) |
76 | } | 96 | } |
77 | 97 | ||
78 | /// Parses the token tree (result of macro expansion) to a Type | 98 | /// Parses the token tree (result of macro expansion) to a Type |
79 | pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<Parse<ast::TypeRef>, ExpandError> { | 99 | pub fn token_tree_to_ty(tt: &tt::Subtree) -> Result<Parse<ast::TypeRef>, ExpandError> { |
80 | let parse = token_tree_to_syntax_node(tt, ra_parser::parse_ty)?; | 100 | let parse = fragment_to_syntax_node(tt, Type)?; |
81 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) | 101 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) |
82 | } | 102 | } |
83 | 103 | ||
84 | /// Parses the token tree (result of macro expansion) as a sequence of stmts | 104 | /// Parses the token tree (result of macro expansion) as a sequence of stmts |
85 | pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result<Parse<ast::MacroStmts>, ExpandError> { | 105 | pub fn token_tree_to_macro_stmts(tt: &tt::Subtree) -> Result<Parse<ast::MacroStmts>, ExpandError> { |
86 | let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_stmts)?; | 106 | let parse = fragment_to_syntax_node(tt, Statements)?; |
87 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) | 107 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) |
88 | } | 108 | } |
89 | 109 | ||
90 | /// Parses the token tree (result of macro expansion) as a sequence of items | 110 | /// Parses the token tree (result of macro expansion) as a sequence of items |
91 | pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> Result<Parse<ast::MacroItems>, ExpandError> { | 111 | pub fn token_tree_to_macro_items(tt: &tt::Subtree) -> Result<Parse<ast::MacroItems>, ExpandError> { |
92 | let parse = token_tree_to_syntax_node(tt, ra_parser::parse_macro_items)?; | 112 | let parse = fragment_to_syntax_node(tt, Items)?; |
93 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) | 113 | parse.cast().ok_or_else(|| crate::ExpandError::ConversionError) |
94 | } | 114 | } |
95 | 115 | ||