diff options
author | Edwin Cheng <[email protected]> | 2021-01-04 16:22:42 +0000 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2021-01-04 18:00:46 +0000 |
commit | af3d75ad2e760dc885f54e6179543718ef8f141f (patch) | |
tree | 821093073f61fbfb2d3d31304dfe0eacbef0bcf0 /crates/mbe | |
parent | f1ffd14922d2dc885ba6441ca8380f7d4cb75269 (diff) |
Refactor TokenBuffer for reduc cloning
Diffstat (limited to 'crates/mbe')
-rw-r--r-- | crates/mbe/src/mbe_expander/matcher.rs | 6 | ||||
-rw-r--r-- | crates/mbe/src/subtree_source.rs | 10 | ||||
-rw-r--r-- | crates/mbe/src/syntax_bridge.rs | 21 |
3 files changed, 18 insertions, 19 deletions
diff --git a/crates/mbe/src/mbe_expander/matcher.rs b/crates/mbe/src/mbe_expander/matcher.rs index ab5f87c48..fdc8844ce 100644 --- a/crates/mbe/src/mbe_expander/matcher.rs +++ b/crates/mbe/src/mbe_expander/matcher.rs | |||
@@ -309,7 +309,7 @@ impl<'a> TtIter<'a> { | |||
309 | } | 309 | } |
310 | } | 310 | } |
311 | 311 | ||
312 | let buffer = TokenBuffer::new(&self.inner.as_slice()); | 312 | let buffer = TokenBuffer::from_tokens(&self.inner.as_slice()); |
313 | let mut src = SubtreeTokenSource::new(&buffer); | 313 | let mut src = SubtreeTokenSource::new(&buffer); |
314 | let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; | 314 | let mut sink = OffsetTokenSink { cursor: buffer.begin(), error: false }; |
315 | 315 | ||
@@ -336,11 +336,11 @@ impl<'a> TtIter<'a> { | |||
336 | err = Some(err!("no tokens consumed")); | 336 | err = Some(err!("no tokens consumed")); |
337 | } | 337 | } |
338 | let res = match res.len() { | 338 | let res = match res.len() { |
339 | 1 => Some(res[0].clone()), | 339 | 1 => Some(res[0].cloned()), |
340 | 0 => None, | 340 | 0 => None, |
341 | _ => Some(tt::TokenTree::Subtree(tt::Subtree { | 341 | _ => Some(tt::TokenTree::Subtree(tt::Subtree { |
342 | delimiter: None, | 342 | delimiter: None, |
343 | token_trees: res.into_iter().cloned().collect(), | 343 | token_trees: res.into_iter().map(|it| it.cloned()).collect(), |
344 | })), | 344 | })), |
345 | }; | 345 | }; |
346 | ExpandResult { value: res, err } | 346 | ExpandResult { value: res, err } |
diff --git a/crates/mbe/src/subtree_source.rs b/crates/mbe/src/subtree_source.rs index d10d4b70e..36d6f1038 100644 --- a/crates/mbe/src/subtree_source.rs +++ b/crates/mbe/src/subtree_source.rs | |||
@@ -53,10 +53,12 @@ impl<'a> SubtreeTokenSource<'a> { | |||
53 | fn is_lifetime(c: Cursor) -> Option<(Cursor, SmolStr)> { | 53 | fn is_lifetime(c: Cursor) -> Option<(Cursor, SmolStr)> { |
54 | let tkn = c.token_tree(); | 54 | let tkn = c.token_tree(); |
55 | 55 | ||
56 | if let Some(tt::TokenTree::Leaf(tt::Leaf::Punct(punct))) = tkn { | 56 | if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(punct), _)) = tkn { |
57 | if punct.char == '\'' { | 57 | if punct.char == '\'' { |
58 | let next = c.bump(); | 58 | let next = c.bump(); |
59 | if let Some(tt::TokenTree::Leaf(tt::Leaf::Ident(ident))) = next.token_tree() { | 59 | if let Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Ident(ident), _)) = |
60 | next.token_tree() | ||
61 | { | ||
60 | let res_cursor = next.bump(); | 62 | let res_cursor = next.bump(); |
61 | let text = SmolStr::new("'".to_string() + &ident.to_string()); | 63 | let text = SmolStr::new("'".to_string() + &ident.to_string()); |
62 | 64 | ||
@@ -94,11 +96,11 @@ impl<'a> SubtreeTokenSource<'a> { | |||
94 | } | 96 | } |
95 | 97 | ||
96 | match cursor.token_tree() { | 98 | match cursor.token_tree() { |
97 | Some(tt::TokenTree::Leaf(leaf)) => { | 99 | Some(tt::buffer::TokenTreeRef::Leaf(leaf, _)) => { |
98 | cached.push(Some(convert_leaf(&leaf))); | 100 | cached.push(Some(convert_leaf(&leaf))); |
99 | self.cached_cursor.set(cursor.bump()); | 101 | self.cached_cursor.set(cursor.bump()); |
100 | } | 102 | } |
101 | Some(tt::TokenTree::Subtree(subtree)) => { | 103 | Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) => { |
102 | self.cached_cursor.set(cursor.subtree().unwrap()); | 104 | self.cached_cursor.set(cursor.subtree().unwrap()); |
103 | cached.push(Some(convert_delim(subtree.delimiter_kind(), false))); | 105 | cached.push(Some(convert_delim(subtree.delimiter_kind(), false))); |
104 | } | 106 | } |
diff --git a/crates/mbe/src/syntax_bridge.rs b/crates/mbe/src/syntax_bridge.rs index 423a4934e..671036e1c 100644 --- a/crates/mbe/src/syntax_bridge.rs +++ b/crates/mbe/src/syntax_bridge.rs | |||
@@ -70,15 +70,12 @@ pub fn token_tree_to_syntax_node( | |||
70 | tt: &tt::Subtree, | 70 | tt: &tt::Subtree, |
71 | fragment_kind: FragmentKind, | 71 | fragment_kind: FragmentKind, |
72 | ) -> Result<(Parse<SyntaxNode>, TokenMap), ExpandError> { | 72 | ) -> Result<(Parse<SyntaxNode>, TokenMap), ExpandError> { |
73 | let tmp; | 73 | let buffer = match tt { |
74 | let tokens = match tt { | 74 | tt::Subtree { delimiter: None, token_trees } => { |
75 | tt::Subtree { delimiter: None, token_trees } => token_trees.as_slice(), | 75 | TokenBuffer::from_tokens(token_trees.as_slice()) |
76 | _ => { | ||
77 | tmp = [tt.clone().into()]; | ||
78 | &tmp[..] | ||
79 | } | 76 | } |
77 | _ => TokenBuffer::from_subtree(tt), | ||
80 | }; | 78 | }; |
81 | let buffer = TokenBuffer::new(&tokens); | ||
82 | let mut token_source = SubtreeTokenSource::new(&buffer); | 79 | let mut token_source = SubtreeTokenSource::new(&buffer); |
83 | let mut tree_sink = TtTreeSink::new(buffer.begin()); | 80 | let mut tree_sink = TtTreeSink::new(buffer.begin()); |
84 | parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind); | 81 | parser::parse_fragment(&mut token_source, &mut tree_sink, fragment_kind); |
@@ -631,7 +628,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
631 | } | 628 | } |
632 | last = self.cursor; | 629 | last = self.cursor; |
633 | let text: &str = match self.cursor.token_tree() { | 630 | let text: &str = match self.cursor.token_tree() { |
634 | Some(tt::TokenTree::Leaf(leaf)) => { | 631 | Some(tt::buffer::TokenTreeRef::Leaf(leaf, _)) => { |
635 | // Mark the range if needed | 632 | // Mark the range if needed |
636 | let (text, id) = match leaf { | 633 | let (text, id) = match leaf { |
637 | tt::Leaf::Ident(ident) => (&ident.text, ident.id), | 634 | tt::Leaf::Ident(ident) => (&ident.text, ident.id), |
@@ -650,7 +647,7 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
650 | self.cursor = self.cursor.bump(); | 647 | self.cursor = self.cursor.bump(); |
651 | text | 648 | text |
652 | } | 649 | } |
653 | Some(tt::TokenTree::Subtree(subtree)) => { | 650 | Some(tt::buffer::TokenTreeRef::Subtree(subtree, _)) => { |
654 | self.cursor = self.cursor.subtree().unwrap(); | 651 | self.cursor = self.cursor.subtree().unwrap(); |
655 | if let Some(id) = subtree.delimiter.map(|it| it.id) { | 652 | if let Some(id) = subtree.delimiter.map(|it| it.id) { |
656 | self.open_delims.insert(id, self.text_pos); | 653 | self.open_delims.insert(id, self.text_pos); |
@@ -684,8 +681,8 @@ impl<'a> TreeSink for TtTreeSink<'a> { | |||
684 | // Add whitespace between adjoint puncts | 681 | // Add whitespace between adjoint puncts |
685 | let next = last.bump(); | 682 | let next = last.bump(); |
686 | if let ( | 683 | if let ( |
687 | Some(tt::TokenTree::Leaf(tt::Leaf::Punct(curr))), | 684 | Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(curr), _)), |
688 | Some(tt::TokenTree::Leaf(tt::Leaf::Punct(_))), | 685 | Some(tt::buffer::TokenTreeRef::Leaf(tt::Leaf::Punct(_), _)), |
689 | ) = (last.token_tree(), next.token_tree()) | 686 | ) = (last.token_tree(), next.token_tree()) |
690 | { | 687 | { |
691 | // Note: We always assume the semi-colon would be the last token in | 688 | // Note: We always assume the semi-colon would be the last token in |
@@ -744,7 +741,7 @@ mod tests { | |||
744 | ) | 741 | ) |
745 | .expand_tt("literals!(foo);"); | 742 | .expand_tt("literals!(foo);"); |
746 | let tts = &[expansion.into()]; | 743 | let tts = &[expansion.into()]; |
747 | let buffer = tt::buffer::TokenBuffer::new(tts); | 744 | let buffer = tt::buffer::TokenBuffer::from_tokens(tts); |
748 | let mut tt_src = SubtreeTokenSource::new(&buffer); | 745 | let mut tt_src = SubtreeTokenSource::new(&buffer); |
749 | let mut tokens = vec![]; | 746 | let mut tokens = vec![]; |
750 | while tt_src.current().kind != EOF { | 747 | while tt_src.current().kind != EOF { |