1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 22:47:47 +00:00

LibJS: Fix lexing of the last character in a file

Before this commit the last character in a file would be swallowed.
This also fixes parsing of empty files which would previously ASSERT.
This commit is contained in:
Stephan Unverwerth 2020-03-13 21:29:32 +01:00 committed by Andreas Kling
parent 8285102b6d
commit ac524b632f

View file

@ -115,7 +115,8 @@ Lexer::Lexer(StringView source)
void Lexer::consume() void Lexer::consume()
{ {
if (is_eof()) { if (m_position >= m_source.length()) {
m_position = m_source.length() + 1;
m_current_char = EOF; m_current_char = EOF;
return; return;
} }
@ -125,7 +126,7 @@ void Lexer::consume()
bool Lexer::is_eof() const bool Lexer::is_eof() const
{ {
return m_position >= m_source.length(); return m_current_char == EOF;
} }
bool Lexer::is_identifier_start() const bool Lexer::is_identifier_start() const
@ -157,12 +158,12 @@ Token Lexer::next()
{ {
size_t trivia_start = m_position; size_t trivia_start = m_position;
// consume up whitespace and comments // consume whitespace and comments
while (true) { while (true) {
if (isspace(m_current_char)) { if (isspace(m_current_char)) {
do { do {
consume(); consume();
} while (!is_eof() && isspace(m_current_char)); } while (isspace(m_current_char));
} else if (is_line_comment_start()) { } else if (is_line_comment_start()) {
consume(); consume();
do { do {
@ -228,7 +229,7 @@ Token Lexer::next()
} }
bool found_two_char_token = false; bool found_two_char_token = false;
if (!found_three_char_token && !is_eof()) { if (!found_three_char_token && m_position < m_source.length()) {
char second_char = m_source[m_position]; char second_char = m_source[m_position];
char two_chars[] { (char)m_current_char, second_char, 0 }; char two_chars[] { (char)m_current_char, second_char, 0 };
auto it = s_two_char_tokens.find(two_chars); auto it = s_two_char_tokens.find(two_chars);