aboutsummaryrefslogtreecommitdiff
path: root/crates/mbe/src/syntax_bridge.rs
diff options
context:
space:
mode:
authorJonas Schievink <[email protected]>2021-04-04 00:46:45 +0100
committerJonas Schievink <[email protected]>2021-04-04 00:46:45 +0100
commit3abcdc03ba335fb3487c62547f61746e4a199fe6 (patch)
tree8386a83d383563308a83f4491d313be043dc94d3 /crates/mbe/src/syntax_bridge.rs
parentbcf600fc881026ab018d0a8ae09eff7e54af538b (diff)
Make `ast_to_token_tree` infallible
It could never return `None`, so reflect that in the return type
Diffstat (limited to 'crates/mbe/src/syntax_bridge.rs')
-rw-r--r--crates/mbe/src/syntax_bridge.rs20
1 files changed, 10 insertions, 10 deletions
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs
index ae0780072..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).
46pub fn ast_to_token_tree(ast: &impl ast::AstNode) -> Option<(tt::Subtree, TokenMap)> { 46pub 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).
52pub fn syntax_node_to_token_tree(node: &SyntaxNode) -> Option<(tt::Subtree, TokenMap)> { 52pub 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,7 +319,7 @@ trait SrcToken: std::fmt::Debug {
319trait TokenConvertor { 319trait 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() {
@@ -327,10 +327,10 @@ trait TokenConvertor {
327 } 327 }
328 if subtree.token_trees.len() == 1 { 328 if subtree.token_trees.len() == 1 {
329 if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] { 329 if let tt::TokenTree::Subtree(first) = &subtree.token_trees[0] {
330 return Some(first.clone()); 330 return first.clone();
331 } 331 }
332 } 332 }
333 Some(subtree) 333 subtree
334 } 334 }
335 335
336 fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) { 336 fn collect_leaf(&mut self, result: &mut Vec<tt::TokenTree>) {
@@ -858,7 +858,7 @@ mod tests {
858 // - T!['}'] 858 // - T!['}']
859 // - WHITE_SPACE 859 // - WHITE_SPACE
860 let token_tree = ast::TokenTree::cast(token_tree).unwrap(); 860 let token_tree = ast::TokenTree::cast(token_tree).unwrap();
861 let tt = ast_to_token_tree(&token_tree).unwrap().0; 861 let tt = ast_to_token_tree(&token_tree).0;
862 862
863 assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace)); 863 assert_eq!(tt.delimiter_kind(), Some(tt::DelimiterKind::Brace));
864 } 864 }
@@ -867,7 +867,7 @@ mod tests {
867 fn test_token_tree_multi_char_punct() { 867 fn test_token_tree_multi_char_punct() {
868 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();
869 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();
870 let tt = ast_to_token_tree(&struct_def).unwrap().0; 870 let tt = ast_to_token_tree(&struct_def).0;
871 token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap(); 871 token_tree_to_syntax_node(&tt, FragmentKind::Item).unwrap();
872 } 872 }
873} 873}