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
7 changes: 3 additions & 4 deletions crates/oxc_parser/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

use oxc_allocator::{TakeIn, Vec};
use oxc_ast::ast::{Decorator, RegExpFlags};
use oxc_diagnostics::Result;
use oxc_span::{GetSpan, Span};

use crate::{
Expand Down Expand Up @@ -219,10 +218,10 @@ impl<'a> ParserImpl<'a> {
}

/// Tell lexer to read a regex
pub(crate) fn read_regex(&mut self) -> Result<(u32, RegExpFlags, bool)> {
let (token, pattern_end, flags, flags_error) = self.lexer.next_regex(self.cur_kind())?;
pub(crate) fn read_regex(&mut self) -> (u32, RegExpFlags, bool) {
let (token, pattern_end, flags, flags_error) = self.lexer.next_regex(self.cur_kind());
self.token = token;
Ok((pattern_end, flags, flags_error))
(pattern_end, flags, flags_error)
}

/// Tell lexer to read a template substitution tail
Expand Down
11 changes: 4 additions & 7 deletions crates/oxc_parser/src/js/expression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,13 +342,10 @@ impl<'a> ParserImpl<'a> {

pub(crate) fn parse_literal_regexp(&mut self) -> RegExpLiteral<'a> {
let span = self.start_span();
// split out pattern
let (pattern_end, flags, flags_error) = match self.read_regex() {
Ok(res) => res,
Err(error) => {
return self.fatal_error(error);
}
};
let (pattern_end, flags, flags_error) = self.read_regex();
if !self.lexer.errors.is_empty() {
return self.unexpected();
}
let pattern_start = self.cur_token().start + 1; // +1 to exclude left `/`
let pattern_text = &self.source_text[pattern_start as usize..pattern_end as usize];
let flags_start = pattern_end + 1; // +1 to include right `/`
Expand Down
20 changes: 11 additions & 9 deletions crates/oxc_parser/src/lexer/regex.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use oxc_diagnostics::Result;
use oxc_syntax::identifier::is_line_terminator;

use crate::diagnostics;
Expand All @@ -12,31 +11,34 @@ impl Lexer<'_> {
/// where a `RegularExpressionLiteral` is permitted
/// Which means the parser needs to re-tokenize on `PrimaryExpression`,
/// `RegularExpressionLiteral` only appear on the right hand side of `PrimaryExpression`
pub(crate) fn next_regex(&mut self, kind: Kind) -> Result<(Token, u32, RegExpFlags, bool)> {
pub(crate) fn next_regex(&mut self, kind: Kind) -> (Token, u32, RegExpFlags, bool) {
self.token.start = self.offset()
- match kind {
Kind::Slash => 1,
Kind::SlashEq => 2,
_ => unreachable!(),
};
let (pattern_end, flags, flags_error) = self.read_regex()?;
let (pattern_end, flags, flags_error) = self.read_regex();
self.lookahead.clear();
let token = self.finish_next(Kind::RegExp);
Ok((token, pattern_end, flags, flags_error))
(token, pattern_end, flags, flags_error)
}

/// 12.9.5 Regular Expression Literals
fn read_regex(&mut self) -> Result<(u32, RegExpFlags, bool)> {
fn read_regex(&mut self) -> (u32, RegExpFlags, bool) {
let mut in_escape = false;
let mut in_character_class = false;
loop {
match self.next_char() {
None => {
return Err(diagnostics::unterminated_reg_exp(self.unterminated_range()));
// return (self.offset(), RegExpFlags::empty());
self.error(diagnostics::unterminated_reg_exp(self.unterminated_range()));
self.advance_to_end();
break;
}
Some(c) if is_line_terminator(c) => {
return Err(diagnostics::unterminated_reg_exp(self.unterminated_range()));
self.error(diagnostics::unterminated_reg_exp(self.unterminated_range()));
self.advance_to_end();
break;
}
Some(c) => {
if in_escape {
Expand Down Expand Up @@ -82,6 +84,6 @@ impl Lexer<'_> {
flags |= flag;
}

Ok((pattern_end, flags, flags_error))
(pattern_end, flags, flags_error)
}
}
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ mod parser_parse {
///
/// # Errors
/// If the source code being parsed has syntax errors.
pub fn parse_expression(self) -> std::result::Result<Expression<'a>, Vec<OxcDiagnostic>> {
pub fn parse_expression(self) -> Result<Expression<'a>, Vec<OxcDiagnostic>> {
let unique = UniquePromise::new();
let parser = ParserImpl::new(
self.allocator,
Expand Down Expand Up @@ -496,7 +496,7 @@ impl<'a> ParserImpl<'a> {
}
}

pub fn parse_expression(mut self) -> std::result::Result<Expression<'a>, Vec<OxcDiagnostic>> {
pub fn parse_expression(mut self) -> Result<Expression<'a>, Vec<OxcDiagnostic>> {
// initialize cur_token and prev_token by moving onto the first token
self.bump_any();
let expr = self.parse_expr();
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_parser/src/modifiers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl Modifier {
impl TryFrom<Token> for Modifier {
type Error = <ModifierKind as TryFrom<Kind>>::Error;

fn try_from(tok: Token) -> std::result::Result<Self, Self::Error> {
fn try_from(tok: Token) -> Result<Self, Self::Error> {
ModifierKind::try_from(tok.kind).map(|kind| Self { span: tok.span(), kind })
}
}
Expand Down Expand Up @@ -261,7 +261,7 @@ impl ModifierKind {
impl TryFrom<Kind> for ModifierKind {
type Error = ();

fn try_from(kind: Kind) -> std::result::Result<Self, Self::Error> {
fn try_from(kind: Kind) -> Result<Self, Self::Error> {
match kind {
Kind::Abstract => Ok(Self::Abstract),
Kind::Declare => Ok(Self::Declare),
Expand Down
60 changes: 60 additions & 0 deletions tasks/coverage/snapshots/parser_babel.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1742,12 +1742,24 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ─
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/364/input.js:1:1]
1 │ /
· ─
╰────

× Unterminated regular expression
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/365/input.js:1:1]
1 │ /test
· ─────
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/365/input.js:1:1]
1 │ /test
· ─────
╰────

× Invalid Unicode escape sequence
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/366/input.js:1:17]
1 │ var x = /[a-z]/\ux
Expand Down Expand Up @@ -1841,6 +1853,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
2 │ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/380/input.js:1:9]
1 │ ╭─▶ var x = /
2 │ ╰─▶ /
╰────

× Unterminated string
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/381/input.js:1:9]
1 │ var x = "
Expand Down Expand Up @@ -2223,6 +2241,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
2 │ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/441/input.js:1:1]
1 │ ╭─▶ /a\
2 │ ╰─▶ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/core/uncategorised/442/input.js:3:1]
2 │
Expand Down Expand Up @@ -3960,6 +3984,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ─
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/es2015/regex/duplicate-flags/input.js:1:1]
1 │ /./gii;
· ──────
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/es2015/shorthand/reserved-word/input.js:1:17]
1 │ var x = ({ const, if, this });
Expand Down Expand Up @@ -9997,19 +10027,37 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
· ──
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0038/input.js:1:1]
1 │ /
· ──
╰────

× Unterminated regular expression
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0039/input.js:1:1]
1 │ /test
· ──────
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0039/input.js:1:1]
1 │ /test
· ──────
╰────

× Unterminated regular expression
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0040/input.js:1:1]
1 │ /test
· ──────
2 │ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0040/input.js:1:1]
1 │ ╭─▶ /test
2 │ ╰─▶ /
╰────

× Invalid Unicode escape sequence
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0041/input.js:1:17]
1 │ var x = /[a-z]/\ux
Expand Down Expand Up @@ -10184,6 +10232,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
2 │ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0062/input.js:1:9]
1 │ ╭─▶ var x = /
2 │ ╰─▶ /
╰────

× Unterminated string
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0063/input.js:1:9]
1 │ var x = "
Expand Down Expand Up @@ -10723,6 +10777,12 @@ Expect to Parse: tasks/coverage/babel/packages/babel-parser/test/fixtures/typesc
2 │ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0157/input.js:1:1]
1 │ ╭─▶ /a\
2 │ ╰─▶ /
╰────

× Unexpected token
╭─[babel/packages/babel-parser/test/fixtures/esprima/invalid-syntax/migrated_0158/input.js:3:1]
2 │
Expand Down
Loading
Loading