use oxc_ast::ast::RegExpFlags;
use oxc_span::Span;
use oxc_str::JSStr;
use crate::{
ParserConfig as Config, ParserImpl, diagnostics,
lexer::{Kind, LexerCheckpoint, Token, cold_branch},
};
#[derive(Clone, Copy)]
pub struct ParserCheckpoint<'a> {
lexer: LexerCheckpoint<'a>,
cur_token: Token,
prev_token_end: u32,
errors_pos: usize,
has_fatal_error: bool,
}
impl<'a, C: Config> ParserImpl<'a, C> {
#[inline]
pub(crate) fn cur_start(&self) -> u32 {
self.token.start()
}
#[inline]
pub(crate) fn end_span(&self, start: u32) -> Span {
Span::new(start, self.prev_token_end)
}
#[inline]
pub(crate) fn cur_token(&self) -> Token {
self.token
}
#[inline]
pub(crate) fn cur_kind(&self) -> Kind {
self.token.kind()
}
#[inline]
pub(crate) fn cur_src(&self) -> &'a str {
self.token_source(&self.token)
}
#[inline]
pub(crate) fn token_source(&self, token: &Token) -> &'a str {
let span = token.span();
if cfg!(debug_assertions) {
&self.source_text[span.start as usize..span.end as usize]
} else {
unsafe { self.source_text.get_unchecked(span.start as usize..span.end as usize) }
}
}
pub(crate) fn cur_string(&self) -> &'a str {
self.lexer.get_string(self.token)
}
pub(crate) fn cur_js_string(&self) -> JSStr<'a> {
self.lexer.get_js_string(self.token)
}
pub(crate) fn cur_template_string(&self) -> Option<JSStr<'a>> {
self.lexer.get_template_string(self.token.start())
}
#[inline]
pub(crate) fn at(&self, kind: Kind) -> bool {
self.cur_kind() == kind
}
#[cold]
fn report_escaped_keyword(&mut self, span: Span) {
self.error(diagnostics::escaped_keyword(span));
}
#[inline]
pub(crate) fn advance(&mut self, kind: Kind) {
if self.token.escaped() && kind.is_any_keyword() {
self.report_escaped_keyword(self.token.span());
}
self.prev_token_end = self.token.end();
self.token = self.lexer.next_token();
}
pub(crate) fn advance_for_jsx_child(&mut self) {
self.prev_token_end = self.token.end();
self.token = self.lexer.next_jsx_child();
}
#[inline]
#[must_use = "Use `bump` instead of `eat` if you are ignoring the return value"]
pub(crate) fn eat(&mut self, kind: Kind) -> bool {
if self.at(kind) {
self.advance(kind);
return true;
}
false
}
#[inline]
pub(crate) fn bump(&mut self, kind: Kind) {
if self.at(kind) {
self.advance(kind);
}
}
#[inline]
pub(crate) fn bump_any(&mut self) {
self.advance(self.cur_kind());
}
#[inline]
pub(crate) fn eat_if_next(&mut self, accept: impl FnOnce(Token) -> bool) -> bool {
let checkpoint = self.lexer.checkpoint();
let next = self.lexer.next_token();
if !accept(next) {
self.lexer.rewind(checkpoint);
return false;
}
if self.token.escaped() && self.token.kind().is_any_keyword() {
self.report_escaped_keyword(self.token.span());
}
self.prev_token_end = self.token.end();
self.token = next;
true
}
pub(crate) fn asi(&mut self) {
if self.eat(Kind::Semicolon) || self.can_insert_semicolon() {
} else {
cold_branch(|| {
let span = Span::empty(self.prev_token_end);
let error = diagnostics::auto_semicolon_insertion(span);
self.set_fatal_error(error);
});
}
}
#[inline]
pub(crate) fn can_insert_semicolon(&self) -> bool {
let token = self.cur_token();
matches!(token.kind(), Kind::Semicolon | Kind::RCurly | Kind::Eof) || token.is_on_new_line()
}
#[cold]
#[inline(never)]
fn handle_expect_failure(&mut self, expected_kind: Kind) {
let range = self.cur_token().span();
let error =
diagnostics::expect_token(expected_kind.to_str(), self.cur_kind().to_str(), range);
self.set_fatal_error(error);
}
#[inline]
pub(crate) fn expect_without_advance(&mut self, kind: Kind) {
if !self.at(kind) {
self.handle_expect_failure(kind);
}
}
#[inline]
pub(crate) fn expect(&mut self, kind: Kind) {
if !self.at(kind) {
self.handle_expect_failure(kind);
}
self.advance(kind);
}
#[inline]
pub(crate) fn expect_closing(&mut self, kind: Kind, opening_span: Span) {
if !self.at(kind) {
let range = self.cur_token().span();
let error = diagnostics::expect_closing(
kind.to_str(),
self.cur_kind().to_str(),
range,
opening_span,
);
self.set_fatal_error(error);
}
self.advance(kind);
}
#[inline]
pub(crate) fn expect_conditional_alternative(&mut self, question_span: Span) {
if !self.at(Kind::Colon) {
let range = self.cur_token().span();
let error = diagnostics::expect_conditional_alternative(
self.cur_kind().to_str(),
range,
question_span,
);
self.set_fatal_error(error);
}
self.bump_any(); }
pub(crate) fn expect_jsx_child(&mut self, kind: Kind) {
self.expect_without_advance(kind);
self.advance_for_jsx_child();
}
pub(crate) fn advance_for_jsx_attribute_value(&mut self) {
self.prev_token_end = self.token.end();
self.token = self.lexer.next_jsx_attribute_value();
}
pub(crate) fn read_regex(&mut self) -> (u32, RegExpFlags) {
let (token, pattern_end, flags) = self.lexer.next_regex(self.cur_kind());
self.token = token;
(pattern_end, flags)
}
pub(crate) fn re_lex_template_substitution_tail(&mut self) {
if self.fatal_error.is_some() {
return;
}
if self.at(Kind::RCurly) {
self.token = self.lexer.next_template_substitution_tail();
}
}
pub(crate) fn continue_lex_jsx_identifier(&mut self) -> bool {
if let Some(token) = self.lexer.continue_lex_jsx_identifier(self.token.start()) {
self.token = token;
true
} else {
false
}
}
#[inline]
pub(crate) fn re_lex_right_angle(&mut self) -> Kind {
if self.fatal_error.is_some() {
return Kind::Eof;
}
let kind = self.cur_kind();
if kind == Kind::RAngle {
self.token = self.lexer.re_lex_right_angle();
self.token.kind()
} else {
kind
}
}
pub(crate) fn re_lex_ts_l_angle(&mut self) -> bool {
if self.fatal_error.is_some() {
return false;
}
let kind = self.cur_kind();
if kind == Kind::ShiftLeft || kind == Kind::LtEq {
self.token = self.lexer.re_lex_as_typescript_l_angle(2);
true
} else if kind == Kind::ShiftLeftEq {
self.token = self.lexer.re_lex_as_typescript_l_angle(3);
true
} else {
kind == Kind::LAngle
}
}
pub(crate) fn re_lex_ts_r_angle(&mut self) -> bool {
if self.fatal_error.is_some() {
return false;
}
let kind = self.cur_kind();
if kind == Kind::ShiftRight {
self.token = self.lexer.re_lex_as_typescript_r_angle(2);
true
} else if kind == Kind::ShiftRight3 {
self.token = self.lexer.re_lex_as_typescript_r_angle(3);
true
} else {
kind == Kind::RAngle
}
}
pub(crate) fn checkpoint(&self) -> ParserCheckpoint<'a> {
ParserCheckpoint {
lexer: self.lexer.checkpoint(),
cur_token: self.token,
prev_token_end: self.prev_token_end,
errors_pos: self.errors.len(),
has_fatal_error: self.fatal_error.is_some(),
}
}
pub(crate) fn rewind(&mut self, checkpoint: ParserCheckpoint<'a>) {
let ParserCheckpoint { lexer, cur_token, prev_token_end, errors_pos, has_fatal_error } =
checkpoint;
if !has_fatal_error
&& let Some(fatal_error) = self.fatal_error.take()
&& self.lexer.errors.len() < lexer.errors_len()
{
self.lexer.errors.push(fatal_error.error);
}
self.lexer.rewind(lexer);
self.token = cur_token;
self.prev_token_end = prev_token_end;
self.errors.truncate(errors_pos);
}
pub(crate) fn lookahead<U>(&mut self, predicate: impl Fn(&mut ParserImpl<'a, C>) -> U) -> U {
let checkpoint = self.checkpoint();
let answer = predicate(self);
self.rewind(checkpoint);
answer
}
}