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