Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 35 additions & 47 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ impl<'a> ParserImpl<'a> {

#[inline]
pub(crate) fn end_span(&self, start: u32) -> Span {
let end = self.prev_token_end;
debug_assert!(end >= start);
Span::new(start, end)
Span::new(start, self.prev_token_end)
}

/// Get current token
Expand Down Expand Up @@ -168,16 +166,16 @@ impl<'a> ParserImpl<'a> {

/// [Automatic Semicolon Insertion](https://tc39.es/ecma262/#sec-automatic-semicolon-insertion)
/// # Errors
pub(crate) fn asi(&mut self) -> Result<()> {
pub(crate) fn asi(&mut self) {
if !self.can_insert_semicolon() {
let span = Span::new(self.prev_token_end, self.prev_token_end);
let error = diagnostics::auto_semicolon_insertion(span);
return Err(self.set_fatal_error(error));
self.set_fatal_error(error);
return;
}
if self.at(Kind::Semicolon) {
self.advance(Kind::Semicolon);
}
Ok(())
}

pub(crate) fn can_insert_semicolon(&self) -> bool {
Expand All @@ -189,39 +187,35 @@ impl<'a> ParserImpl<'a> {
}

/// # Errors
pub(crate) fn expect_without_advance(&mut self, kind: Kind) -> Result<()> {
pub(crate) fn expect_without_advance(&mut self, kind: Kind) {
if !self.at(kind) {
let range = self.cur_token().span();
let error = diagnostics::expect_token(kind.to_str(), self.cur_kind().to_str(), range);
return Err(self.set_fatal_error(error));
self.set_fatal_error(error);
}
Ok(())
}

/// Expect a `Kind` or return error
/// # Errors
#[inline]
pub(crate) fn expect(&mut self, kind: Kind) -> Result<()> {
self.expect_without_advance(kind)?;
pub(crate) fn expect(&mut self, kind: Kind) {
self.expect_without_advance(kind);
self.advance(kind);
Ok(())
}

/// Expect the next next token to be a `JsxChild`, i.e. `<` or `{` or `JSXText`
/// # Errors
pub(crate) fn expect_jsx_child(&mut self, kind: Kind) -> Result<()> {
self.expect_without_advance(kind)?;
pub(crate) fn expect_jsx_child(&mut self, kind: Kind) {
self.expect_without_advance(kind);
self.advance_for_jsx_child(kind);
Ok(())
}

/// Expect the next next token to be a `JsxString` or any other token
/// # Errors
pub(crate) fn expect_jsx_attribute_value(&mut self, kind: Kind) -> Result<()> {
pub(crate) fn expect_jsx_attribute_value(&mut self, kind: Kind) {
self.lexer.set_context(LexerContext::JsxAttributeValue);
self.expect(kind)?;
self.expect(kind);
self.lexer.set_context(LexerContext::Regular);
Ok(())
}

/// Tell lexer to read a regex
Expand Down Expand Up @@ -296,16 +290,15 @@ impl<'a> ParserImpl<'a> {
self.fatal_error = fatal_error;
}

/// # Errors
pub(crate) fn try_parse<T>(
&mut self,
func: impl FnOnce(&mut ParserImpl<'a>) -> Result<T>,
func: impl FnOnce(&mut ParserImpl<'a>) -> T,
) -> Option<T> {
let checkpoint = self.checkpoint();
let ctx = self.ctx;
let result = func(self);
if let Ok(result) = result {
Some(result)
let node = func(self);
if self.fatal_error.is_none() {
Some(node)
} else {
self.ctx = ctx;
self.rewind(checkpoint);
Expand Down Expand Up @@ -337,33 +330,28 @@ impl<'a> ParserImpl<'a> {
self.state.decorators.take_in(self.ast.allocator)
}

pub(crate) fn parse_normal_list<F, T>(
&mut self,
open: Kind,
close: Kind,
f: F,
) -> Result<Vec<'a, T>>
pub(crate) fn parse_normal_list<F, T>(&mut self, open: Kind, close: Kind, f: F) -> Vec<'a, T>
where
F: Fn(&mut Self) -> Result<Option<T>>,
F: Fn(&mut Self) -> Option<T>,
{
self.expect(open)?;
self.expect(open);
let mut list = self.ast.vec();
loop {
let kind = self.cur_kind();
if kind == close || self.has_fatal_error() {
break;
}
match f(self)? {
match f(self) {
Some(e) => {
list.push(e);
}
_ => {
None => {
break;
}
}
}
self.expect(close)?;
Ok(list)
self.expect(close);
list
}

pub(crate) fn parse_delimited_list<F, T>(
Expand All @@ -372,9 +360,9 @@ impl<'a> ParserImpl<'a> {
separator: Kind,
trailing_separator: bool,
f: F,
) -> Result<Vec<'a, T>>
) -> Vec<'a, T>
where
F: Fn(&mut Self) -> Result<T>,
F: Fn(&mut Self) -> T,
{
let mut list = self.ast.vec();
let mut first = true;
Expand All @@ -389,25 +377,25 @@ impl<'a> ParserImpl<'a> {
if !trailing_separator && self.at(separator) && self.peek_at(close) {
break;
}
self.expect(separator)?;
self.expect(separator);
if self.at(close) {
break;
}
}
list.push(f(self)?);
list.push(f(self));
}
Ok(list)
list
}

pub(crate) fn parse_delimited_list_with_rest<E, R, A, B>(
&mut self,
close: Kind,
parse_element: E,
parse_rest: R,
) -> Result<(Vec<'a, A>, Option<B>)>
) -> (Vec<'a, A>, Option<B>)
where
E: Fn(&mut Self) -> Result<A>,
R: Fn(&mut Self) -> Result<B>,
E: Fn(&mut Self) -> A,
R: Fn(&mut Self) -> B,
B: GetSpan,
{
let mut list = self.ast.vec();
Expand All @@ -421,20 +409,20 @@ impl<'a> ParserImpl<'a> {
if first {
first = false;
} else {
self.expect(Kind::Comma)?;
self.expect(Kind::Comma);
if self.at(close) {
break;
}
}

if self.at(Kind::Dot3) {
if let Some(r) = rest.replace(parse_rest(self)?) {
if let Some(r) = rest.replace(parse_rest(self)) {
self.error(diagnostics::binding_rest_element_last(r.span()));
}
} else {
list.push(parse_element(self)?);
list.push(parse_element(self));
}
}
Ok((list, rest))
(list, rest)
}
}
24 changes: 11 additions & 13 deletions crates/oxc_parser/src/error_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,32 @@ pub struct FatalError {
/// The fatal error
pub error: OxcDiagnostic,
/// Length of `errors` at time fatal error is recorded
#[expect(unused)]
pub errors_len: usize,
}

impl<'a> ParserImpl<'a> {
pub(crate) fn set_unexpected(&mut self) -> OxcDiagnostic {
pub(crate) fn set_unexpected(&mut self) {
// The lexer should have reported a more meaningful diagnostic
// when it is a undetermined kind.
if matches!(self.cur_kind(), Kind::Eof | Kind::Undetermined) {
if let Some(error) = self.lexer.errors.pop() {
return self.set_fatal_error(error);
self.set_fatal_error(error);
return;
}
}
let error = diagnostics::unexpected_token(self.cur_token().span());
self.set_fatal_error(error)
self.set_fatal_error(error);
}

/// Return error info at current token
///
/// # Panics
///
/// * The lexer did not push a diagnostic when `Kind::Undetermined` is returned
pub(crate) fn unexpected(&mut self) -> OxcDiagnostic {
self.set_unexpected()
// Dummy::dummy(self.ast.allocator)
#[must_use]
pub(crate) fn unexpected<T: Dummy<'a>>(&mut self) -> T {
self.set_unexpected();
Dummy::dummy(self.ast.allocator)
}

/// Push a Syntax Error
Expand All @@ -49,18 +50,15 @@ impl<'a> ParserImpl<'a> {
}

/// Advance lexer's cursor to end of file.
pub(crate) fn set_fatal_error(&mut self, error: OxcDiagnostic) -> OxcDiagnostic {
pub(crate) fn set_fatal_error(&mut self, error: OxcDiagnostic) {
if self.fatal_error.is_none() {
self.lexer.advance_to_end();
self.fatal_error =
Some(FatalError { error: error.clone(), errors_len: self.errors.len() });
self.fatal_error = Some(FatalError { error, errors_len: self.errors.len() });
}
error
}

#[expect(unused)]
pub(crate) fn fatal_error<T: Dummy<'a>>(&mut self, error: OxcDiagnostic) -> T {
let _ = self.set_fatal_error(error);
self.set_fatal_error(error);
Dummy::dummy(self.ast.allocator)
}

Expand Down
Loading
Loading