diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-11-09 10:16:48 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-11-09 10:16:48 +0000 |
commit | defc7ad772123a449f7cc384dd46d88c3a45fb53 (patch) | |
tree | b0b01d1397542585802a3622fdf9491e20fa8c0b /crates/ra_mbe/src/syntax_bridge.rs | |
parent | 561bb979cecd786f5d311ea7bddb1e15d77a3848 (diff) | |
parent | 70f2a21b55c1b09e575798a75807e13991f2cfec (diff) |
Merge #2197
2197: Remove typed macro parsing API r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/ra_mbe/src/syntax_bridge.rs | 34 |
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 | ||
3 | use ra_parser::{ | 3 | use ra_parser::{FragmentKind, ParseError, TreeSink}; |
4 | FragmentKind::{self, *}, | ||
5 | ParseError, TreeSink, | ||
6 | }; | ||
7 | use ra_syntax::{ | 4 | use 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 | ||
58 | fn fragment_to_syntax_node( | 55 | pub 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 | ||
82 | macro_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 | |||
94 | impl_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 | |||
107 | impl TokenMap { | 79 | impl 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 | } |