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;
}
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