diff options
Diffstat (limited to 'crates/mbe/src/syntax_bridge.rs')
-rw-r--r-- | crates/mbe/src/syntax_bridge.rs | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 9d433b3b0..9ba98f7fb 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs | |||
@@ -43,18 +43,18 @@ pub struct TokenMap { | |||
43 | 43 | ||
44 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro | 44 | /// Convert the syntax tree (what user has written) to a `TokenTree` (what macro |
45 | /// will consume). | 45 | /// will consume). |
46 | pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> { | 46 | pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> (tt::Subtree, TokenMap) { |
47 | syntax_node_to_token_tree(ast.syntax()) | 47 | syntax_node_to_token_tree(ast.syntax()) |
48 | } | 48 | } |
49 | 49 | ||
50 | /// Convert the syntax node to a `TokenTree` (what macro | 50 | /// Convert the syntax node to a `TokenTree` (what macro |
51 | /// will consume). | 51 | /// will consume). |
52 | pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> { | 52 | pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> (tt::Subtree, TokenMap) { |
53 | let global_offset = node.text_range().start(); | 53 | let global_offset = node.text_range().start(); |
54 | let mut c = Convertor::new(node, global_offset); | 54 | let mut c = Convertor::new(node, global_offset); |
55 | let subtree = c.go()?; | 55 | let subtree = c.go(); |
56 | c.id_alloc.map.entries.shrink_to_fit(); | 56 | c.id_alloc.map.entries.shrink_to_fit(); |
57 | Some((subtree, c.id_alloc.map)) | 57 | (subtree, c.id_alloc.map) |
58 | } | 58 | } |
59 | 59 | ||
60 | // The following items are what `rustc` macro can be parsed into : | 60 | // The following items are what `rustc` macro can be parsed into : |
@@ -108,7 +108,7 @@ pub fn parse_to_token_tree(text: &str) -> Option<(tt::Subtree, TokenMap)> { | |||
108 | }, | 108 | }, |
109 | }; | 109 | }; |
110 | 110 | ||
111 | let subtree = conv.go()?; | 111 | let subtree = conv.go(); |
112 | Some((subtree, conv.id_alloc.map)) | 112 | Some((subtree, conv.id_alloc.map)) |
113 | } | 113 | } |
114 | 114 | ||
@@ -319,21 +319,18 @@ trait SrcToken: std::fmt::Debug { | |||
319 | trait TokenConvertor { | 319 | trait TokenConvertor { |
320 | type Token: SrcToken; | 320 | type Token: SrcToken; |
321 | 321 | ||
322 | fn go(&mut self) -> Option<tt::Subtree> { | 322 | fn go(&mut self) -> tt::Subtree { |
323 | let mut subtree = tt::Subtree::default(); | 323 | let mut subtree = tt::Subtree::default(); |
324 | subtree.delimiter = None; | 324 | subtree.delimiter = None; |
325 | while self.peek().is_some() { | 325 | while self.peek().is_some() { |
326 | self.collect_leaf(&mut subtree.token_trees); | 326 | self.collect_leaf(&mut subtree.token_trees); |
327 | } | 327 | } |
328 | if subtree.token_trees.is_empty() { | ||
329 | return None; | ||
330 | } | ||
331 | if subtree.token_trees.len() == 1 { | 328 | if subtree.token_trees.len() == 1 { |
332 | if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { | 329 | if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { |
333 | return Some(first.clone()); | 330 | return first.clone(); |
334 | } | 331 | } |
335 | } | 332 | } |
336 | Some(subtree) | 333 | subtree |
337 | } | 334 | } |
338 | 335 | ||
339 | fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) { | 336 | fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) { |
@@ -861,7 +858,7 @@ mod tests { | |||
861 | // - T!['}'] | 858 | // - T!['}'] |
862 | // - WHITE_SPACE | 859 | // - WHITE_SPACE |
863 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); | 860 | let token_tree = ast::TokenTree::cast(token_tree).unwrap(); |
864 | let tt = ast_to_token_tree(&token_tree).unwrap().0; | 861 | let tt = ast_to_token_tree(&token_tree).0; |
865 | 862 | ||
866 | assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); | 863 | assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); |
867 | } | 864 | } |
@@ -870,7 +867,7 @@ mod tests { | |||
870 | fn test_token_tree_multi_char_punct() { | 867 | fn test_token_tree_multi_char_punct() { |
871 | let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); | 868 | let source_file = ast::SourceFile::parse("struct Foo { a: x::Y }").ok().unwrap(); |
872 | let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap(); | 869 | let struct_def = source_file.syntax().descendants().find_map(ast::Struct::cast).unwrap(); |
873 | let tt = ast_to_token_tree(&struct_def).unwrap().0; | 870 | let tt = ast_to_token_tree(&struct_def).0; |
874 | token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); | 871 | token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); |
875 | } | 872 | } |
876 | } | 873 | } |