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.rs34
1 files changed, 3 insertions, 31 deletions
diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs
index 9653f7fef..3f57ce3b5 100644
--- a/crates/ra_mbe/src/syntax_bridge.rs
+++ b/crates/ra_mbe/src/syntax_bridge.rs
@@ -1,9 +1,6 @@
1//! FIXME: write short doc here 1//! FIXME: write short doc here
2 2
3use ra_parser::{ 3use ra_parser::{FragmentKind, ParseError, TreeSink};
4 FragmentKind::{self, *},
5 ParseError, TreeSink,
6};
7use ra_syntax::{ 4use ra_syntax::{
8 ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, 5 ast, AstNode, AstToken, NodeOrToken, Parse, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode,
9 SyntaxTreeBuilder, TextRange, TextUnit, T, 6 SyntaxTreeBuilder, TextRange, TextUnit, T,
@@ -55,7 +52,7 @@ pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, Toke
55// * ImplItems(SmallVec<[ast::ImplItem; 1]>) 52// * ImplItems(SmallVec<[ast::ImplItem; 1]>)
56// * ForeignItems(SmallVec<[ast::ForeignItem; 1]> 53// * ForeignItems(SmallVec<[ast::ForeignItem; 1]>
57 54
58fn fragment_to_syntax_node( 55pub fn token_tree_to_syntax_node(
59 tt: &tt::Subtree, 56 tt: &tt::Subtree,
60 fragment_kind: FragmentKind, 57 fragment_kind: FragmentKind,
61) -> Result<(Parse<SyntaxNode>, RevTokenMap), ExpandError> { 58) -> Result<(Parse<SyntaxNode>, RevTokenMap), ExpandError> {
@@ -79,31 +76,6 @@ fn fragment_to_syntax_node(
79 Ok((parse, range_map)) 76 Ok((parse, range_map))
80} 77}
81 78
82macro_rules! impl_token_tree_conversions {
83 ($($(#[$attr:meta])* $name:ident => ($kind:ident, $t:ty) ),*) => {
84 $(
85 $(#[$attr])*
86 pub fn $name(tt: &tt::Subtree) -> Result<(Parse<$t>, RevTokenMap), ExpandError> {
87 let (parse, map) = fragment_to_syntax_node(tt, $kind)?;
88 parse.cast().ok_or_else(|| crate::ExpandError::ConversionError).map(|p| (p, map))
89 }
90 )*
91 }
92}
93
94impl_token_tree_conversions! {
95 /// Parses the token tree (result of macro expansion) to an expression
96 token_tree_to_expr => (Expr, ast::Expr),
97 /// Parses the token tree (result of macro expansion) to a Pattern
98 token_tree_to_pat => (Pattern, ast::Pat),
99 /// Parses the token tree (result of macro expansion) to a Type
100 token_tree_to_ty => (Type, ast::TypeRef),
101 /// Parses the token tree (result of macro expansion) as a sequence of stmts
102 token_tree_to_macro_stmts => (Statements, ast::MacroStmts),
103 /// Parses the token tree (result of macro expansion) as a sequence of items
104 token_tree_to_items => (Items, ast::MacroItems)
105}
106
107impl TokenMap { 79impl TokenMap {
108 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> { 80 pub fn relative_range_of(&self, tt: tt::TokenId) -> Option<TextRange> {
109 let idx = tt.0 as usize; 81 let idx = tt.0 as usize;
@@ -446,6 +418,6 @@ mod tests {
446 "#, 418 "#,
447 ); 419 );
448 let expansion = expand(&rules, "stmts!();"); 420 let expansion = expand(&rules, "stmts!();");
449 assert!(token_tree_to_expr(&expansion).is_err()); 421 assert!(token_tree_to_syntax_node(&expansion, FragmentKind::Expr).is_err());
450 } 422 }
451} 423}