Skip to content

Commit

Permalink
Add regex literal early errors
Browse files Browse the repository at this point in the history
  • Loading branch information
raskad committed Jan 5, 2023
1 parent 1b04c9e commit 63ca528
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions boa_parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 changes: 8 additions & 0 deletions boa_parser/src/lexer/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down Expand Up @@ -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),
Expand Down

0 comments on commit 63ca528

Please sign in to comment.