Skip to content

Commit

Permalink
Merge pull request #18 from dsLeks/test-unhandled-character
Browse files Browse the repository at this point in the history
Add test for unhandled characters
  • Loading branch information
PiJoules authored Dec 9, 2023
2 parents 065289a + 9742521 commit 36fbb0f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 8 deletions.
15 changes: 8 additions & 7 deletions lexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ Result<Token> Lexer::getNextToken() {
}

std::string identifier;
if (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) && ch != EOF) {
while (((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z')) &&
ch != EOF) {
if (isalpha(ch)) {
while (isalpha(ch)) {
identifier += ch;
ch = getNextChar();
}
Expand All @@ -85,12 +84,14 @@ Result<Token> Lexer::getNextToken() {
lookahead_ = ch;
has_lookahead_ = true;

if (identifier == "out") {
return Token(Token::out, row, col);
}
if (identifier == "out") return Token(Token::out, row, col);

return Token(Token::identifier, row, col, identifier);
}

return Token(Token::identifier, row, col, identifier);
std::stringstream ss;
ss << "Unhandled character '" << ch << "'";
return Result<Token>::Error(ss.str());
}

} // namespace xos
9 changes: 9 additions & 0 deletions test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,13 @@ TEST(Lexer, UnfinishedArrowToken) {
ASSERT_EQ(result.getErr(), "Expected '=>' at row 1, col 1");
}

TEST(Lexer, UnhandledCharacter) {
std::stringstream ss("%");
xos::Lexer lexer(ss);

auto result = lexer.getNextToken();
ASSERT_TRUE(result.hasError());
ASSERT_EQ(result.getErr(), "Unhandled character '%'");
}

} // namespace
5 changes: 4 additions & 1 deletion xos.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ class Result {
assert(!hasError() && "Getting from an invalid result");
return result_;
}
const auto &getErr() const { return err_; }
const auto &getErr() const {
assert(hasError() && "Expected an error");
return err_;
}

private:
Result() = default;
Expand Down

0 comments on commit 36fbb0f

Please sign in to comment.