diff options
author | Stephan Unverwerth <s.unverwerth@gmx.de> | 2020-04-13 19:50:58 +0200 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2020-04-14 12:54:09 +0200 |
commit | 9477efe97032f15438b51bf01e2958cc76c554be (patch) | |
tree | 172eff7a0853aeaa58b8dc32ca7903eeb26e267f /Libraries/LibJS/Lexer.cpp | |
parent | 9962db5bf82a37859a018d539da55f6afe41a8d2 (diff) | |
download | serenity-9477efe97032f15438b51bf01e2958cc76c554be.zip |
LibJS: Handle HTML-style comments
Diffstat (limited to 'Libraries/LibJS/Lexer.cpp')
-rw-r--r-- | Libraries/LibJS/Lexer.cpp | 56 |
1 files changed, 40 insertions, 16 deletions
diff --git a/Libraries/LibJS/Lexer.cpp b/Libraries/LibJS/Lexer.cpp index fc16cb239e..f8bfce245a 100644 --- a/Libraries/LibJS/Lexer.cpp +++ b/Libraries/LibJS/Lexer.cpp @@ -167,6 +167,36 @@ void Lexer::consume_exponent() } } +bool Lexer::match(char a, char b) const +{ + if (m_position >= m_source.length()) + return false; + + return m_current_char == a + && m_source[m_position] == b; +} + +bool Lexer::match(char a, char b, char c) const +{ + if (m_position + 1 >= m_source.length()) + return false; + + return m_current_char == a + && m_source[m_position] == b + && m_source[m_position + 1] == c; +} + +bool Lexer::match(char a, char b, char c, char d) const +{ + if (m_position + 2 >= m_source.length()) + return false; + + return m_current_char == a + && m_source[m_position] == b + && m_source[m_position + 1] == c + && m_source[m_position + 2] == d; +} + bool Lexer::is_eof() const { return m_current_char == EOF; @@ -184,17 +214,17 @@ bool Lexer::is_identifier_middle() const bool Lexer::is_line_comment_start() const { - return m_current_char == '/' && m_position < m_source.length() && m_source[m_position] == '/'; + return match('/', '/') || match('<', '!', '-', '-') || match('-', '-', '>'); } bool Lexer::is_block_comment_start() const { - return m_current_char == '/' && m_position < m_source.length() && m_source[m_position] == '*'; + return match('/', '*'); } bool Lexer::is_block_comment_end() const { - return m_current_char == '*' && m_position < m_source.length() && m_source[m_position] == '/'; + return match('*', '/'); } bool Lexer::is_numeric_literal_start() const @@ -328,19 +358,13 @@ Token Lexer::next() } else { // There is only one four-char operator: >>>= bool found_four_char_token = false; - if (m_position + 2 < m_source.length()) { - if (m_current_char == '>' - && m_source[m_position] == '>' - && m_source[m_position + 1] == '>' - && m_source[m_position + 2] == '=') { - - found_four_char_token = true; - consume(); - consume(); - consume(); - consume(); - token_type = TokenType::UnsignedShiftRightEquals; - } + if (match('>', '>', '>', '=')) { + found_four_char_token = true; + consume(); + consume(); + consume(); + consume(); + token_type = TokenType::UnsignedShiftRightEquals; } bool found_three_char_token = false; |