Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/parser/src/parser.rs')
-rw-r--r--crates/parser/src/parser.rs47
1 files changed, 19 insertions, 28 deletions
diff --git a/crates/parser/src/parser.rs b/crates/parser/src/parser.rs
index 44c5f8e12f..4c891108a6 100644
--- a/crates/parser/src/parser.rs
+++ b/crates/parser/src/parser.rs
@@ -7,9 +7,10 @@ use limit::Limit;
use crate::{
event::Event,
+ tokens::Tokens,
ParseError,
SyntaxKind::{self, EOF, ERROR, TOMBSTONE},
- TokenSet, TokenSource, T,
+ TokenSet, T,
};
/// `Parser` struct provides the low-level API for
@@ -22,7 +23,8 @@ use crate::{
/// "start expression, consume number literal,
/// finish expression". See `Event` docs for more.
pub(crate) struct Parser<'t> {
- token_source: &'t mut dyn TokenSource,
+ tokens: &'t Tokens,
+ pos: usize,
events: Vec<Event>,
steps: Cell<u32>,
}
@@ -30,8 +32,8 @@ pub(crate) struct Parser<'t> {
static PARSER_STEP_LIMIT: Limit = Limit::new(15_000_000);
impl<'t> Parser<'t> {
- pub(super) fn new(token_source: &'t mut dyn TokenSource) -> Parser<'t> {
- Parser { token_source, events: Vec::new(), steps: Cell::new(0) }
+ pub(super) fn new(tokens: &'t Tokens) -> Parser<'t> {
+ Parser { tokens, pos: 0, events: Vec::new(), steps: Cell::new(0) }
}
pub(crate) fn finish(self) -> Vec<Event> {
@@ -54,7 +56,7 @@ impl<'t> Parser<'t> {
assert!(PARSER_STEP_LIMIT.check(steps as usize).is_ok(), "the parser seems stuck");
self.steps.set(steps + 1);
- self.token_source.lookahead_nth(n).kind
+ self.tokens.kind(self.pos + n)
}
/// Checks if the current token is `kind`.
@@ -90,7 +92,7 @@ impl<'t> Parser<'t> {
T![<<=] => self.at_composite3(n, T![<], T![<], T![=]),
T![>>=] => self.at_composite3(n, T![>], T![>], T![=]),
- _ => self.token_source.lookahead_nth(n).kind == kind,
+ _ => self.tokens.kind(self.pos + n) == kind,
}
}
@@ -129,25 +131,17 @@ impl<'t> Parser<'t> {
}
fn at_composite2(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind) -> bool {
- let t1 = self.token_source.lookahead_nth(n);
- if t1.kind != k1 || !t1.is_jointed_to_next {
- return false;
- }
- let t2 = self.token_source.lookahead_nth(n + 1);
- t2.kind == k2
+ self.tokens.kind(self.pos + n) == k1
+ && self.tokens.kind(self.pos + n + 1) == k2
+ && self.tokens.is_joint(self.pos + n)
}
fn at_composite3(&self, n: usize, k1: SyntaxKind, k2: SyntaxKind, k3: SyntaxKind) -> bool {
- let t1 = self.token_source.lookahead_nth(n);
- if t1.kind != k1 || !t1.is_jointed_to_next {
- return false;
- }
- let t2 = self.token_source.lookahead_nth(n + 1);
- if t2.kind != k2 || !t2.is_jointed_to_next {
- return false;
- }
- let t3 = self.token_source.lookahead_nth(n + 2);
- t3.kind == k3
+ self.tokens.kind(self.pos + n) == k1
+ && self.tokens.kind(self.pos + n + 1) == k2
+ && self.tokens.kind(self.pos + n + 2) == k3
+ && self.tokens.is_joint(self.pos + n)
+ && self.tokens.is_joint(self.pos + n + 1)
}
/// Checks if the current token is in `kinds`.
@@ -156,8 +150,8 @@ impl<'t> Parser<'t> {
}
/// Checks if the current token is contextual keyword with text `t`.
- pub(crate) fn at_contextual_kw(&self, kw: &str) -> bool {
- self.token_source.is_keyword(kw)
+ pub(crate) fn at_contextual_kw(&self, kw: SyntaxKind) -> bool {
+ self.tokens.contextual_kind(self.pos) == kw
}
/// Starts a new node in the syntax tree. All nodes and tokens
@@ -243,10 +237,7 @@ impl<'t> Parser<'t> {
}
fn do_bump(&mut self, kind: SyntaxKind, n_raw_tokens: u8) {
- for _ in 0..n_raw_tokens {
- self.token_source.bump();
- }
-
+ self.pos += n_raw_tokens as usize;
self.push_event(Event::Token { kind, n_raw_tokens });
}