use {Token, SyntaxKind, TextUnit}; use super::{Event}; use super::super::is_insignificant; use syntax_kinds::{L_CURLY, R_CURLY, ERROR}; pub struct Parser<'t> { text: &'t str, non_ws_tokens: Vec<(Token, TextUnit)>, pos: usize, events: Vec, curly_level: i32, curly_limit: Option, } impl<'t> Parser<'t> { pub(crate) fn new(text: &'t str, raw_tokens: &'t [Token]) -> Parser<'t> { let mut non_ws_tokens = Vec::new(); let mut len = TextUnit::new(0); for &token in raw_tokens.iter() { if !is_insignificant(token.kind) { non_ws_tokens.push((token, len)) } len += token.len; } Parser { text, non_ws_tokens, pos: 0, events: Vec::new(), curly_level: 0, curly_limit: None, } } pub(crate) fn into_events(self) -> Vec { assert!(self.is_eof()); self.events } pub(crate) fn is_eof(&self) -> bool { if self.pos == self.non_ws_tokens.len() { return true } if let Some(limit) = self.curly_limit { let token = self.non_ws_tokens[self.pos].0; return limit == self.curly_level && token.kind == R_CURLY; } false } pub(crate) fn start(&mut self, kind: SyntaxKind) { self.event(Event::Start { kind }); } pub(crate) fn finish(&mut self) { self.event(Event::Finish); } pub(crate) fn error<'p>(&'p mut self) -> ErrorBuilder<'p, 't> { ErrorBuilder::new(self) } pub(crate) fn current(&self) -> Option { if self.is_eof() { return None; } let token = self.non_ws_tokens[self.pos].0; Some(token.kind) } pub(crate) fn bump(&mut self) -> Option { let kind = self.current()?; match kind { L_CURLY => self.curly_level += 1, R_CURLY => self.curly_level -= 1, _ => (), } self.pos += 1; self.event(Event::Token { kind, n_raw_tokens: 1 }); Some(kind) } pub(crate) fn lookahead(&self, kinds: &[SyntaxKind]) -> bool { if self.non_ws_tokens[self.pos..].len() < kinds.len() { return false } kinds.iter().zip(self.non_ws_tokens[self.pos..].iter().map(|&(t, _)| t.kind)) .all(|(&k1, k2)| k1 == k2) } pub(crate) fn curly_block(&mut self, f: F) -> bool { let old_level = self.curly_level; let old_limit = self.curly_limit; if !self.expect(L_CURLY) { return false } self.curly_limit = Some(self.curly_level); f(self); assert!(self.curly_level > old_level); self.curly_limit = old_limit; if !self.expect(R_CURLY) { self.start(ERROR); while self.curly_level > old_level { if self.bump().is_none() { break; } } self.finish(); } true } fn event(&mut self, event: Event) { self.events.push(event) } } pub(crate) struct ErrorBuilder<'p, 't: 'p> { message: Option, parser: &'p mut Parser<'t> } impl<'t, 'p> ErrorBuilder<'p, 't> { fn new(parser: &'p mut Parser<'t>) -> Self { ErrorBuilder { message: None, parser } } pub fn message>(mut self, m: M) -> Self { self.message = Some(m.into()); self } pub fn emit(self) { let message = self.message.expect("Error message not set"); self.parser.event(Event::Error { message }); } }