From bc1c5567555c1e38d931979484d01f566d171e76 Mon Sep 17 00:00:00 2001 From: Matthew Olsson Date: Sun, 7 Jun 2020 19:32:25 -0700 Subject: [PATCH] LibJS: Move regex logic to main Lexer if statement This prevents a regex such as /=/ from lexing into TokenType::SlashEquals, preventing the regex logic from working. --- Libraries/LibJS/Lexer.cpp | 45 ++++++++++++++++++++------------------- 1 file 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(); - } } }