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.
This commit is contained in:
Matthew Olsson 2020-06-07 19:32:25 -07:00 committed by Andreas Kling
parent cc7462daeb
commit bc1c556755
Notes: sideshowbarker 2024-07-19 05:45:46 +09:00

View file

@ -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();
}
}
}