Skip to content

Commit

Permalink
Lexer: Add UnfinishedComment error
Browse files Browse the repository at this point in the history
  • Loading branch information
SirLynix committed Aug 3, 2023
1 parent a3b0e77 commit 8bfe9f6
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
1 change: 1 addition & 0 deletions include/NZSL/Lang/ErrorList.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
// Lexer errors
NZSL_SHADERLANG_LEXER_ERROR(BadNumber, "bad number")
NZSL_SHADERLANG_LEXER_ERROR(NumberOutOfRange, "number is out of range")
NZSL_SHADERLANG_LEXER_ERROR(UnfinishedComment, "unfinished block comment")
NZSL_SHADERLANG_LEXER_ERROR(UnfinishedString, "unfinished string")
NZSL_SHADERLANG_LEXER_ERROR(UnrecognizedChar, "unrecognized character")
NZSL_SHADERLANG_LEXER_ERROR(UnrecognizedToken, "unrecognized token")
Expand Down
10 changes: 8 additions & 2 deletions src/NZSL/Lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ namespace nzsl
else if (next == '*')
{
// Block comment
do
for (;;)
{
currentPos++;
next = Peek();
Expand All @@ -196,8 +196,14 @@ namespace nzsl
}
else if (next == '\n')
HandleNewLine();
else if (next == '\0')
{
token.location.endColumn = token.location.startColumn + 1;
token.location.endLine = token.location.startLine;

throw LexerUnfinishedCommentError{ token.location };
}
}
while (next != '\0');
}
else if (next == '=')
{
Expand Down
10 changes: 10 additions & 0 deletions tests/src/Tests/ErrorsTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ TEST_CASE("errors", "[Shader]")
CHECK_THROWS_WITH(nzsl::Tokenize("\"Hello world"), "(1,1 -> 13): LUnfinishedString error: unfinished string");
CHECK_THROWS_WITH(nzsl::Tokenize(R"("hello \p")"), "(1,1 -> 9): LUnrecognizedChar error: unrecognized character");
CHECK_THROWS_WITH(nzsl::Tokenize("$"), "(1, 1): LUnrecognizedToken error: unrecognized token");
CHECK_THROWS_WITH(nzsl::Tokenize(R"(
[nzsl_version("1.0")]
module;
fn main()
{/*
let x = 42.0;
return;
}
)"), "(6,2 -> 3): LUnfinishedComment error: unfinished block comment");
}

SECTION("Checking parser errors")
Expand Down

0 comments on commit 8bfe9f6

Please sign in to comment.