Simiplify

This commit is contained in:
Aleksey Kladov 2018-01-07 19:56:33 +03:00
parent f194750a2a
commit bbf7e9bbd2

View file

@ -5,8 +5,7 @@ use syntax_kinds::{L_CURLY, R_CURLY, ERROR};
pub struct Parser<'t> { pub struct Parser<'t> {
text: &'t str, text: &'t str,
raw_tokens: &'t [Token], non_ws_tokens: Vec<(Token, TextUnit)>,
non_ws_tokens: Vec<(usize, TextUnit)>,
pos: usize, pos: usize,
events: Vec<Event>, events: Vec<Event>,
@ -19,16 +18,15 @@ impl<'t> Parser<'t> {
pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> {
let mut non_ws_tokens = Vec::new(); let mut non_ws_tokens = Vec::new();
let mut len = TextUnit::new(0); let mut len = TextUnit::new(0);
for (idx, &token) in raw_tokens.iter().enumerate() { for &token in raw_tokens.iter() {
if !is_insignificant(token.kind) { if !is_insignificant(token.kind) {
non_ws_tokens.push((idx, len)) non_ws_tokens.push((token, len))
} }
len += token.len; len += token.len;
} }
Parser { Parser {
text, text,
raw_tokens,
non_ws_tokens, non_ws_tokens,
pos: 0, pos: 0,
@ -48,8 +46,8 @@ impl<'t> Parser<'t> {
return true return true
} }
if let Some(limit) = self.curly_limit { if let Some(limit) = self.curly_limit {
let idx = self.non_ws_tokens[self.pos].0; let token = self.non_ws_tokens[self.pos].0;
return limit == self.curly_level && self.raw_tokens[idx].kind == R_CURLY; return limit == self.curly_level && token.kind == R_CURLY;
} }
false false
} }
@ -70,8 +68,8 @@ impl<'t> Parser<'t> {
if self.is_eof() { if self.is_eof() {
return None; return None;
} }
let idx = self.non_ws_tokens[self.pos].0; let token = self.non_ws_tokens[self.pos].0;
Some(self.raw_tokens[idx].kind) Some(token.kind)
} }
pub(crate) fn bump(&mut self) -> Option<SyntaxKind> { pub(crate) fn bump(&mut self) -> Option<SyntaxKind> {
@ -90,8 +88,8 @@ impl<'t> Parser<'t> {
if self.non_ws_tokens[self.pos..].len() < kinds.len() { if self.non_ws_tokens[self.pos..].len() < kinds.len() {
return false return false
} }
kinds.iter().zip(self.non_ws_tokens[self.pos..].iter()) kinds.iter().zip(self.non_ws_tokens[self.pos..].iter().map(|&(t, _)| t.kind))
.all(|(&k1, &(idx, _))| k1 == self.raw_tokens[idx].kind) .all(|(&k1, k2)| k1 == k2)
} }
pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool { pub(crate) fn curly_block<F: FnOnce(&mut Parser)>(&mut self, f: F) -> bool {