From e7222e3fb3097fa47f94436f43387c29a9a55233 Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 30 Nov 2022 22:44:08 -0300 Subject: [PATCH] handle invalid double values in the lexer --- src/lexer.cpp | 11 ++++++++++- src/lexer.h | 23 ++++++++++++++++++++++- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/lexer.cpp b/src/lexer.cpp index cd62d75..5627689 100644 --- a/src/lexer.cpp +++ b/src/lexer.cpp @@ -48,11 +48,20 @@ int obelisk::Lexer::getToken() return kTokenIdentifier; } - if (isdigit(lastChar) || lastChar == '.') + if (isdigit(lastChar)) { + bool firstPeriod = false; std::string numberStr; do { + if (firstPeriod && lastChar == '.') + { + throw obelisk::LexerException("invalid double value"); + } + else if (!firstPeriod && lastChar == '.') + { + firstPeriod = true; + } numberStr += lastChar; lastChar = getchar(); } diff --git a/src/lexer.h b/src/lexer.h index c82b949..9c4308d 100644 --- a/src/lexer.h +++ b/src/lexer.h @@ -3,7 +3,6 @@ #include -// TODO: add error handling namespace obelisk { class Lexer @@ -41,6 +40,28 @@ namespace obelisk const std::string& getIdentifier(); double getNumberValue(); }; + + class LexerException : public std::exception + { + private: + const std::string errorMessage_; + + public: + LexerException() : + errorMessage_("an unknown error ocurred") + { + } + + LexerException(const std::string& errorMessage) : + errorMessage_(errorMessage) + { + } + + const char* what() const noexcept + { + return errorMessage_.c_str(); + } + }; } // namespace obelisk #endif