Skip to content

Commit

Permalink
Fix regex literal /[/]/ (#2277)
Browse files Browse the repository at this point in the history
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`
  • Loading branch information
tunz committed Sep 13, 2022
1 parent c1e34fd commit 43c7d77
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion boa_engine/src/syntax/lexer/regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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(
Expand Down

0 comments on commit 43c7d77

Please sign in to comment.