diff options
author | Matthew Olsson <matthewcolsson@gmail.com> | 2020-06-07 19:32:25 -0700 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-06-08 09:18:27 +0200 |
commit | bc1c5567555c1e38d931979484d01f566d171e76 (patch) | |
tree | c9d28ca7abc8bbbba10df17157939555e4fc6815 | |
parent | cc7462daeb8f99224517adce0b545868d6589374 (diff) | |
download | serenity-bc1c5567555c1e38d931979484d01f566d171e76.zip |
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.
-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(); - } } } |