From 54ea251bd40bec06c28e635371588e83cdacca9b Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 3 Jun 2019 00:54:33 +0800 Subject: Store referece in tokenbuffer --- crates/ra_mbe/src/subtree_parser.rs | 4 ++-- crates/ra_mbe/src/syntax_bridge.rs | 6 +++-- crates/ra_tt/src/buffer.rs | 44 ++++++++++++++++++------------------- 3 files changed, 28 insertions(+), 26 deletions(-) (limited to 'crates') diff --git a/crates/ra_mbe/src/subtree_parser.rs b/crates/ra_mbe/src/subtree_parser.rs index 1f12e42ef..4a6f6aa45 100644 --- a/crates/ra_mbe/src/subtree_parser.rs +++ b/crates/ra_mbe/src/subtree_parser.rs @@ -10,7 +10,7 @@ struct OffsetTokenSink<'a> { } impl<'a> OffsetTokenSink<'a> { - pub fn collect(&self, begin: Cursor<'a>) -> Vec { + pub fn collect(&self, begin: Cursor<'a>) -> Vec<&'a tt::TokenTree> { if !self.cursor.is_root() { return vec![]; } @@ -114,7 +114,7 @@ impl<'a> Parser<'a> { 1 => Some(res[0].clone()), _ => Some(tt::TokenTree::Subtree(tt::Subtree { delimiter: tt::Delimiter::None, - token_trees: res, + token_trees: res.into_iter().cloned().collect(), })), } } diff --git a/crates/ra_mbe/src/syntax_bridge.rs b/crates/ra_mbe/src/syntax_bridge.rs index dce82f33d..0edb6f9a2 100644 --- a/crates/ra_mbe/src/syntax_bridge.rs +++ b/crates/ra_mbe/src/syntax_bridge.rs @@ -49,7 +49,8 @@ fn token_tree_to_syntax_node(tt: &tt::Subtree, f: F) -> Result { // Mimicking types from proc-macro. - Subtree(Subtree, EntryId), - Leaf(Leaf), + Subtree(&'t TokenTree, EntryId), + Leaf(&'t TokenTree), // End entries contain a pointer to the entry from the containing // token tree, or None if this is the outermost level. End(Option), @@ -21,12 +21,12 @@ enum Entry { /// A token tree buffer /// The safe version of `syn` [`TokenBuffer`](https://github.com/dtolnay/syn/blob/6533607f91686545cb034d2838beea338d9d0742/src/buffer.rs#L41) #[derive(Debug)] -pub struct TokenBuffer { - buffers: Vec>, +pub struct TokenBuffer<'t> { + buffers: Vec]>>, } -impl TokenBuffer { - pub fn new(tokens: &[TokenTree]) -> TokenBuffer { +impl<'t> TokenBuffer<'t> { + pub fn new(tokens: &'t [TokenTree]) -> TokenBuffer<'t> { let mut buffers = vec![]; let idx = TokenBuffer::new_inner(tokens, &mut buffers, None); @@ -36,21 +36,21 @@ impl TokenBuffer { } fn new_inner( - tokens: &[TokenTree], - buffers: &mut Vec>, + tokens: &'t [TokenTree], + buffers: &mut Vec]>>, next: Option, ) -> usize { let mut entries = vec![]; let mut children = vec![]; - for (idx, tt) in tokens.iter().cloned().enumerate() { + for (idx, tt) in tokens.iter().enumerate() { match tt { - TokenTree::Leaf(leaf) => { - entries.push(Entry::Leaf(leaf)); + TokenTree::Leaf(_) => { + entries.push(Entry::Leaf(tt)); } TokenTree::Subtree(subtree) => { entries.push(Entry::End(None)); - children.push((idx, subtree)); + children.push((idx, (subtree, tt))); } } } @@ -59,13 +59,13 @@ impl TokenBuffer { let res = buffers.len(); buffers.push(entries.into_boxed_slice()); - for (child_idx, subtree) in children { + for (child_idx, (subtree, tt)) in children { let idx = TokenBuffer::new_inner( &subtree.token_trees, buffers, Some(EntryPtr(EntryId(res), child_idx + 1)), ); - buffers[res].as_mut()[child_idx] = Entry::Subtree(subtree, EntryId(idx)); + buffers[res].as_mut()[child_idx] = Entry::Subtree(tt, EntryId(idx)); } res @@ -86,7 +86,7 @@ impl TokenBuffer { /// A safe version of `Cursor` from `syn` crate https://github.com/dtolnay/syn/blob/6533607f91686545cb034d2838beea338d9d0742/src/buffer.rs#L125 #[derive(Copy, Clone, Debug)] pub struct Cursor<'a> { - buffer: &'a TokenBuffer, + buffer: &'a TokenBuffer<'a>, ptr: EntryPtr, } @@ -113,7 +113,7 @@ impl<'a> Cursor<'a> { match self.entry() { Some(Entry::End(Some(ptr))) => { let idx = ptr.1; - if let Some(Entry::Subtree(subtree, _)) = + if let Some(Entry::Subtree(TokenTree::Subtree(subtree), _)) = self.buffer.entry(&EntryPtr(ptr.0, idx - 1)) { return Some(subtree); @@ -125,7 +125,7 @@ impl<'a> Cursor<'a> { } } - fn entry(self) -> Option<(&'a Entry)> { + fn entry(self) -> Option<(&'a Entry<'a>)> { self.buffer.entry(&self.ptr) } @@ -141,10 +141,10 @@ impl<'a> Cursor<'a> { } /// If the cursor is pointing at a `TokenTree`, returns it - pub fn token_tree(self) -> Option<(TokenTree)> { + pub fn token_tree(self) -> Option<(&'a TokenTree)> { match self.entry() { - Some(Entry::Leaf(leaf)) => Some(leaf.clone().into()), - Some(Entry::Subtree(subtree, _)) => Some(subtree.clone().into()), + Some(Entry::Leaf(tt)) => Some(tt), + Some(Entry::Subtree(tt, _)) => Some(tt), Some(Entry::End(_)) => None, None => None, } -- cgit v1.2.3