Skip to content

Commit

Permalink
Introduce skipWhitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnnyMorganz committed Dec 16, 2024
1 parent a14ee98 commit bba1afc
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 6 deletions.
4 changes: 3 additions & 1 deletion Ast/include/Luau/Lexer.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ class Lexer
Lexer(const char* buffer, std::size_t bufferSize, AstNameTable& names, Position startPosition = {0, 0});

void setSkipComments(bool skip);
void setSkipWhitespace(bool skip);
void setReadNames(bool read);

const Location& previousLocation() const
Expand All @@ -165,7 +166,7 @@ class Lexer
}

const Lexeme& next();
const Lexeme& next(bool skipComments, bool updatePrevLocation);
const Lexeme& next(bool skipComments, bool skipWhitespace, bool updatePrevLocation);
void nextline();

Lexeme lookahead();
Expand Down Expand Up @@ -229,6 +230,7 @@ class Lexer
AstNameTable& names;

bool skipComments;
bool skipWhitespace;
bool readNames;

enum class BraceType
Expand Down
12 changes: 9 additions & 3 deletions Ast/src/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ Lexer::Lexer(const char* buffer, size_t bufferSize, AstNameTable& names, Positio
)
, names(names)
, skipComments(false)
, skipWhitespace(true)
, readNames(true)
{
}
Expand All @@ -326,17 +327,22 @@ void Lexer::setSkipComments(bool skip)
skipComments = skip;
}

void Lexer::setSkipWhitespace(bool skip)
{
skipWhitespace = skip;
}

void Lexer::setReadNames(bool read)
{
readNames = read;
}

const Lexeme& Lexer::next()
{
return next(this->skipComments, true);
return next(this->skipComments, this->skipWhitespace, true);
}

const Lexeme& Lexer::next(bool skipComments, bool updatePrevLocation)
const Lexeme& Lexer::next(bool skipComments, bool skipWhitespace, bool updatePrevLocation)
{
// in skipComments mode we reject valid comments
do
Expand All @@ -353,7 +359,7 @@ const Lexeme& Lexer::next(bool skipComments, bool updatePrevLocation)

lexeme = readNext();
updatePrevLocation = false;
} while (skipComments && (lexeme.type == Lexeme::Comment || lexeme.type == Lexeme::BlockComment || lexeme.type == Lexeme::Whitespace));
} while ((skipComments && (lexeme.type == Lexeme::Comment || lexeme.type == Lexeme::BlockComment)) || (skipWhitespace && lexeme.type == Lexeme::Whitespace));

return lexeme;
}
Expand Down
4 changes: 2 additions & 2 deletions Ast/src/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3572,7 +3572,7 @@ AstTypeError* Parser::reportMissingTypeError(const Location& parseErrorLocation,

void Parser::nextLexeme()
{
Lexeme::Type type = lexer.next(/* skipComments= */ false, true).type;
Lexeme::Type type = lexer.next(/* skipComments= */ false, /* skipWhitespace= */ false, true).type;

while (type == Lexeme::BrokenComment || type == Lexeme::Comment || type == Lexeme::BlockComment || type == Lexeme::Whitespace)
{
Expand All @@ -3598,7 +3598,7 @@ void Parser::nextLexeme()
hotcomments.push_back({hotcommentHeader, lexeme.location, std::string(text + 1, text + end)});
}

type = lexer.next(/* skipComments= */ false, /* updatePrevLocation= */ false).type;
type = lexer.next(/* skipComments= */ false, /* skipWhitespace= */ false, /* updatePrevLocation= */ false).type;
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Lexer.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ TEST_CASE("lexer_tokenizes_whitespace")
Luau::Allocator alloc;
AstNameTable table(alloc);
Lexer lexer(testInput.c_str(), testInput.size(), table);
lexer.setSkipWhitespace(false);

CHECK_EQ(lexer.next().type, Lexeme::ReservedLocal);
CHECK_EQ(lexer.next().type, Lexeme::Whitespace);
Expand Down Expand Up @@ -282,6 +283,7 @@ TEST_CASE("lexer_tokenizes_multiline_whitespace")
Luau::Allocator alloc;
AstNameTable table(alloc);
Lexer lexer(testInput.c_str(), testInput.size(), table);
lexer.setSkipWhitespace(false);

CHECK_EQ(lexer.next().type, Lexeme::ReservedLocal);
CHECK_EQ(lexer.next().type, Lexeme::Whitespace);
Expand Down

0 comments on commit bba1afc

Please sign in to comment.