Browse Source

Add regex literal early errors (#2517)

This Pull Request changes the following:

- Parse regex literals via `regress` during parsing to return errors in the regex as early syntax errors.
pull/2521/head
raskad 2 years ago
parent
commit
be9ebc95c0
  1. 1
      Cargo.lock
  2. 1
      boa_parser/Cargo.toml
  3. 8
      boa_parser/src/lexer/regex.rs

1
Cargo.lock generated

@ -300,6 +300,7 @@ dependencies = [
"fast-float",
"num-bigint",
"num-traits",
"regress",
"rustc-hash",
]

1
boa_parser/Cargo.toml

@ -21,3 +21,4 @@ fast-float = "0.2.0"
num-traits = "0.2.15"
bitflags = "1.3.2"
num-bigint = "0.4.3"
regress = "0.4.1"

8
boa_parser/src/lexer/regex.rs

@ -5,6 +5,7 @@ use bitflags::bitflags;
use boa_ast::Position;
use boa_interner::{Interner, Sym};
use boa_profiler::Profiler;
use regress::Regex;
use std::{
io::{self, ErrorKind, Read},
str::{self, FromStr},
@ -120,6 +121,13 @@ impl<R> Tokenizer<R> for RegexLiteral {
let flags_str = unsafe { str::from_utf8_unchecked(flags.as_slice()) };
if let Ok(body_str) = str::from_utf8(body.as_slice()) {
if let Err(error) = Regex::with_flags(body_str, flags_str) {
return Err(Error::Syntax(
format!("Invalid regular expression literal: {error}").into(),
start_pos,
));
}
Ok(Token::new(
TokenKind::regular_expression_literal(
interner.get_or_intern(body_str),

Loading…
Cancel
Save