diff options
-rw-r--r-- | Libraries/LibJS/Lexer.cpp | 45 |
1 files changed, 23 insertions, 22 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp index 911dc6b808..2d3d36e42d 100644 --- a/Libraries/LibJS/Lexer.cpp +++ b/Libraries/LibJS/Lexer.cpp @@ -418,6 +418,29 @@ Token Lexer::next() consume(); token_type = TokenType::StringLiteral; } + } else if (m_current_char == '/' && !slash_means_division()) { + consume(); + token_type = TokenType::RegexLiteral; + + while (!is_eof()) { + if (m_current_char == '[') { + m_regex_is_in_character_class = true; + } else if (m_current_char == ']') { + m_regex_is_in_character_class = false; + } else if (!m_regex_is_in_character_class && m_current_char == '/') { + break; + } + + if (match('\\', '/') || match('\\', '[') || match('\\', '\\') || (m_regex_is_in_character_class && match('\\', ']'))) + consume(); + consume(); + } + + if (is_eof()) { + token_type = TokenType::UnterminatedRegexLiteral; + } else { + consume(); + } } else if (m_current_char == EOF) { token_type = TokenType::Eof; } else { @@ -473,28 +496,6 @@ Token Lexer::next() if (!found_four_char_token && !found_three_char_token && !found_two_char_token && !found_one_char_token) { consume(); token_type = TokenType::Invalid; - } else if (token_type == TokenType::Slash && !slash_means_division()) { - token_type = TokenType::RegexLiteral; - - while (!is_eof()) { - if (m_current_char == '[') { - m_regex_is_in_character_class = true; - } else if (m_current_char == ']') { - m_regex_is_in_character_class = false; - } else if (!m_regex_is_in_character_class && m_current_char == '/') { - break; - } - - if (match('\\', '/') || match('\\', '[') || match('\\', '\\') || (m_regex_is_in_character_class && match('\\', ']'))) - consume(); - consume(); - } - - if (is_eof()) { - token_type = TokenType::UnterminatedRegexLiteral; - } else { - consume(); - } } } |