From d833ded3b4a12280f3e63e182ca7268a17cff90d Mon Sep 17 00:00:00 2001 From: Edwin Cheng Date: Mon, 27 May 2019 22:56:21 +0800 Subject: Remove Queier and SubtreeWalk --- crates/ra_mbe/src/subtree_source.rs | 86 +++++++++++++++---------------------- 1 file changed, 34 insertions(+), 52 deletions(-) (limited to 'crates/ra_mbe/src/subtree_source.rs') diff --git a/crates/ra_mbe/src/subtree_source.rs b/crates/ra_mbe/src/subtree_source.rs index c4f79f38a..2ba0b1601 100644 --- a/crates/ra_mbe/src/subtree_source.rs +++ b/crates/ra_mbe/src/subtree_source.rs @@ -1,13 +1,8 @@ use ra_parser::{TokenSource, Token}; use ra_syntax::{classify_literal, SmolStr, SyntaxKind, SyntaxKind::*, T}; use std::cell::{RefCell, Cell}; -use std::sync::Arc; use tt::buffer::{TokenBuffer, Cursor}; -pub(crate) trait Querier { - fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr, bool); -} - #[derive(Debug, Clone, Eq, PartialEq)] struct TtToken { pub kind: SyntaxKind, @@ -15,20 +10,47 @@ struct TtToken { pub text: SmolStr, } -// A wrapper class for ref cell -#[derive(Debug)] -pub(crate) struct SubtreeWalk<'a> { +pub(crate) struct SubtreeTokenSource<'a> { start: Cursor<'a>, cursor: Cell>, cached: RefCell>>, + curr: (Token, usize), +} + +impl<'a> SubtreeTokenSource<'a> { + // Helper function used in test + #[allow(unused)] + pub fn text(&self) -> SmolStr { + match self.get(self.curr.1) { + Some(tt) => tt.text, + _ => SmolStr::new(""), + } + } } -impl<'a> SubtreeWalk<'a> { - fn new(cursor: Cursor<'a>) -> Self { - SubtreeWalk { +impl<'a> SubtreeTokenSource<'a> { + pub fn new(buffer: &'a TokenBuffer) -> SubtreeTokenSource<'a> { + let cursor = buffer.begin(); + + let mut res = SubtreeTokenSource { + curr: (Token { kind: EOF, is_jointed_to_next: false }, 0), start: cursor, cursor: Cell::new(cursor), cached: RefCell::new(Vec::with_capacity(10)), + }; + res.curr = (res.mk_token(0), 0); + res + } + + pub(crate) fn bump_n(&mut self, parsed_tokens: usize) -> Vec { + let res = self.collect_token_trees(parsed_tokens); + res + } + + fn mk_token(&self, pos: usize) -> Token { + match self.get(pos) { + Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, + None => Token { kind: EOF, is_jointed_to_next: false }, } } @@ -109,46 +131,6 @@ impl<'a> SubtreeWalk<'a> { } } -impl<'a> Querier for SubtreeWalk<'a> { - fn token(&self, uidx: usize) -> (SyntaxKind, SmolStr, bool) { - self.get(uidx) - .map(|tkn| (tkn.kind, tkn.text, tkn.is_joint_to_next)) - .unwrap_or_else(|| (SyntaxKind::EOF, "".into(), false)) - } -} - -pub(crate) struct SubtreeTokenSource<'a> { - walker: Arc>, - curr: (Token, usize), -} - -impl<'a> SubtreeTokenSource<'a> { - pub fn new(buffer: &'a TokenBuffer) -> SubtreeTokenSource<'a> { - let mut res = SubtreeTokenSource { - walker: Arc::new(SubtreeWalk::new(buffer.begin())), - curr: (Token { kind: EOF, is_jointed_to_next: false }, 0), - }; - res.curr = (res.mk_token(0), 0); - res - } - - pub fn querier(&self) -> Arc> { - self.walker.clone() - } - - pub(crate) fn bump_n(&mut self, parsed_tokens: usize) -> Vec { - let res = self.walker.collect_token_trees(parsed_tokens); - res - } - - fn mk_token(&self, pos: usize) -> Token { - match self.walker.get(pos) { - Some(tt) => Token { kind: tt.kind, is_jointed_to_next: tt.is_joint_to_next }, - None => Token { kind: EOF, is_jointed_to_next: false }, - } - } -} - impl<'a> TokenSource for SubtreeTokenSource<'a> { fn current(&self) -> Token { self.curr.0 @@ -170,7 +152,7 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> { /// Is the current token a specified keyword? fn is_keyword(&self, kw: &str) -> bool { - match self.walker.get(self.curr.1) { + match self.get(self.curr.1) { Some(t) => t.text == *kw, _ => false, } -- cgit v1.2.3