diff options
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; |