Simpliy how collecting token from src

This commit is contained in:
Edwin Cheng 2019-05-28 00:28:46 +08:00
parent c8c9230dd2
commit 98aac6b751
3 changed files with 39 additions and 72 deletions

View file

@ -11,8 +11,7 @@ struct TtToken {
}
pub(crate) struct SubtreeTokenSource<'a> {
start: Cursor<'a>,
cursor: Cell<Cursor<'a>>,
cached_cursor: Cell<Cursor<'a>>,
cached: RefCell<Vec<Option<TtToken>>>,
curr: (Token, usize),
}
@ -34,19 +33,13 @@ impl<'a> SubtreeTokenSource<'a> {
let mut res = SubtreeTokenSource {
curr: (Token { kind: EOF, is_jointed_to_next: false }, 0),
start: cursor,
cursor: Cell::new(cursor),
cached_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<tt::TokenTree> {
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 },
@ -61,7 +54,7 @@ impl<'a> SubtreeTokenSource<'a> {
}
while pos >= cached.len() {
let cursor = self.cursor.get();
let cursor = self.cached_cursor.get();
if cursor.eof() {
cached.push(None);
continue;
@ -70,16 +63,16 @@ impl<'a> SubtreeTokenSource<'a> {
match cursor.token_tree() {
Some(tt::TokenTree::Leaf(leaf)) => {
cached.push(Some(convert_leaf(&leaf)));
self.cursor.set(cursor.bump());
self.cached_cursor.set(cursor.bump());
}
Some(tt::TokenTree::Subtree(subtree)) => {
self.cursor.set(cursor.subtree().unwrap());
self.cached_cursor.set(cursor.subtree().unwrap());
cached.push(Some(convert_delim(subtree.delimiter, false)));
}
None => {
if let Some(subtree) = cursor.end() {
cached.push(Some(convert_delim(subtree.delimiter, true)));
self.cursor.set(cursor.bump());
self.cached_cursor.set(cursor.bump());
}
}
}
@ -87,48 +80,6 @@ impl<'a> SubtreeTokenSource<'a> {
return cached[pos].clone();
}
fn collect_token_trees(&self, n: usize) -> Vec<tt::TokenTree> {
let mut res = vec![];
let mut pos = 0;
let mut cursor = self.start;
let mut level = 0;
while pos < n {
if cursor.eof() {
break;
}
match cursor.token_tree() {
Some(tt::TokenTree::Leaf(leaf)) => {
if level == 0 {
res.push(leaf.into());
}
cursor = cursor.bump();
pos += 1;
}
Some(tt::TokenTree::Subtree(subtree)) => {
if level == 0 {
res.push(subtree.into());
}
pos += 1;
level += 1;
cursor = cursor.subtree().unwrap();
}
None => {
if let Some(_) = cursor.end() {
level -= 1;
pos += 1;
cursor = cursor.bump();
}
}
}
}
res
}
}
impl<'a> TokenSource for SubtreeTokenSource<'a> {
@ -147,7 +98,7 @@ impl<'a> TokenSource for SubtreeTokenSource<'a> {
return;
}
self.curr = (self.mk_token(self.curr.1 + 1), self.curr.1 + 1)
self.curr = (self.mk_token(self.curr.1 + 1), self.curr.1 + 1);
}
/// Is the current token a specified keyword?