feature/error_handling #9

Merged
cromer merged 2 commits from feature/error_handling into develop 2022-12-08 20:07:36 -03:00
2 changed files with 32 additions and 2 deletions
Showing only changes of commit e7222e3fb3 - Show all commits

View File

@ -48,11 +48,20 @@ int obelisk::Lexer::getToken()
return kTokenIdentifier; return kTokenIdentifier;
} }
if (isdigit(lastChar) || lastChar == '.') if (isdigit(lastChar))
{ {
bool firstPeriod = false;
std::string numberStr; std::string numberStr;
do do
{ {
if (firstPeriod && lastChar == '.')
{
throw obelisk::LexerException("invalid double value");
}
else if (!firstPeriod && lastChar == '.')
{
firstPeriod = true;
}
numberStr += lastChar; numberStr += lastChar;
lastChar = getchar(); lastChar = getchar();
} }

View File

@ -3,7 +3,6 @@
#include <string> #include <string>
// TODO: add error handling
namespace obelisk namespace obelisk
{ {
class Lexer class Lexer
@ -41,6 +40,28 @@ namespace obelisk
const std::string& getIdentifier(); const std::string& getIdentifier();
double getNumberValue(); 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 } // namespace obelisk
#endif #endif