Browse Source

Fix regex literal /[/]/ (#2277)

This PR fixes a case where a forward slash is located in a regex class: `let regex = /[/]/;`. In this case, the forward slash should not close the regex literal.

This fixes `test/built-ins/RegExp/regexp-class-chars.js`
pull/2280/head
Choongwoo Han 2 years ago
parent
commit
43c7d7748a
  1. 11
      boa_engine/src/syntax/lexer/regex.rs

11
boa_engine/src/syntax/lexer/regex.rs

@ -41,6 +41,7 @@ impl<R> Tokenizer<R> for RegexLiteral {
let _timer = Profiler::global().start_event("RegexLiteral", "Lexing");
let mut body = Vec::new();
let mut is_class_char = false;
// Lex RegularExpressionBody.
loop {
@ -54,7 +55,15 @@ impl<R> Tokenizer<R> for RegexLiteral {
}
Some(b) => {
match b {
b'/' => break, // RegularExpressionBody finished.
b'/' if !is_class_char => break, // RegularExpressionBody finished.
b'[' => {
is_class_char = true;
body.push(b);
}
b']' if is_class_char => {
is_class_char = false;
body.push(b);
}
b'\n' | b'\r' => {
// Not allowed in Regex literal.
return Err(Error::syntax(

Loading…
Cancel
Save