From be9ebc95c0d02da4bcc2af91912176905d223fe4 Mon Sep 17 00:00:00 2001 From: raskad <32105367+raskad@users.noreply.github.com> Date: Thu, 12 Jan 2023 00:12:29 +0000 Subject: [PATCH] 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. --- Cargo.lock | 1 + boa_parser/Cargo.toml | 1 + boa_parser/src/lexer/regex.rs | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/Cargo.lock b/Cargo.lock index 8f3de16a0d0..bef5c59f246 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -300,6 +300,7 @@ dependencies = [ "fast-float", "num-bigint", "num-traits", + "regress", "rustc-hash", ] diff --git a/boa_parser/Cargo.toml b/boa_parser/Cargo.toml index a4d5601ce0c..f08c366de2f 100644 --- a/boa_parser/Cargo.toml +++ b/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" diff --git a/boa_parser/src/lexer/regex.rs b/boa_parser/src/lexer/regex.rs index 41d6270dc46..d416b853a4e 100644 --- a/boa_parser/src/lexer/regex.rs +++ b/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 Tokenizer 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),