handle invalid double values in the lexer

This commit is contained in:
Chris Cromer 2022-11-30 22:44:08 -03:00
parent 0dfb064bf5
commit e7222e3fb3
Signed by: cromer
GPG Key ID: FA91071797BEEEC2
2 changed files with 32 additions and 2 deletions

View File

@ -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();
}

View File

@ -3,7 +3,6 @@
#include <string>
// 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