Add line numbers to Yerbacon::Exception

This commit is contained in:
Username404 2021-08-18 19:31:02 +02:00
parent 989650dba6
commit fd6ce35291
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 11 additions and 7 deletions

View File

@ -12,6 +12,7 @@ tok::type getIdentifierCharType(const char& Char) {
vector<tok> lex(const string& in) vector<tok> lex(const string& in)
{ {
vector<tok> resVal; vector<tok> resVal;
unsigned long lineNumber = 1;
for (unsigned int i = 0; i < in.size(); ++i) { for (unsigned int i = 0; i < in.size(); ++i) {
const char& current = in[i]; const char& current = in[i];
@ -20,9 +21,9 @@ vector<tok> lex(const string& in)
case LBRACE: case RBRACE: case LBRACKET: case RBRACKET: case LBRACE: case RBRACE: case LBRACKET: case RBRACKET:
case PLUS: case HYPHEN: case LCOMP: case RCOMP: case PLUS: case HYPHEN: case LCOMP: case RCOMP:
case DOT: case DOLLAR_SIGN: case SQUOTE: case DOT: case DOLLAR_SIGN: case SQUOTE:
resVal.emplace_back(static_cast<tok::type>(current), string(1, current)); resVal.emplace_back(static_cast<tok::type>(current), string(1, current), lineNumber, i);
[[likely]] case ' ': case '\t': case '\r': [[likely]] case ' ': case '\t': case '\r': break;
[[likely]] case '\n': break; [[likely]] case '\n': ++lineNumber; break;
default: { default: {
tok::type type = getIdentifierCharType(current); tok::type type = getIdentifierCharType(current);
bool isTypeString = (type == STRING); bool isTypeString = (type == STRING);
@ -33,18 +34,18 @@ vector<tok> lex(const string& in)
for (;i < in.size(); ++i) { for (;i < in.size(); ++i) {
tok::type currentCharType = getIdentifierCharType(in[i]); tok::type currentCharType = getIdentifierCharType(in[i]);
bool isString = currentCharType == STRING; bool isString = currentCharType == STRING;
if (i == in.size() - 1 && not isString) throw tok::LexerException("A never ending string was found"); if (i == in.size() - 1 && not isString) throw tok::LexerException("A never ending string was found", lineNumber);
if ((currentCharType == type || isTypeString) && !isString) { if ((currentCharType == type || isTypeString) && !isString) {
formedString += string(1, in[i]); formedString += string(1, in[i]);
} else { } else {
if (not isTypeString) --i; if (not isTypeString) --i;
resVal.emplace_back(type, formedString); resVal.emplace_back(type, formedString, lineNumber, i);
break; break;
} }
} }
} }
case UNEXPECTED: break; case UNEXPECTED: break;
default: resVal.emplace_back(type, string(1, current)); default: resVal.emplace_back(type, string(1, current), lineNumber, i);
} }
break; break;
} }

View File

@ -13,6 +13,7 @@
#include <exception> #include <exception>
#include <string_view> #include <string_view>
#include <string> #include <string>
#include <optional>
namespace Yerbacon { namespace Yerbacon {
consteval const char* getVersion() noexcept { return YBCON_VERSION; } consteval const char* getVersion() noexcept { return YBCON_VERSION; }
@ -24,6 +25,7 @@ namespace Yerbacon {
} }
public: public:
explicit Exception(const std::string_view& cause): exceptionCause(cause) {} explicit Exception(const std::string_view& cause): exceptionCause(cause) {}
Exception(const std::string_view& cause, unsigned long line): exceptionCause(('L' + std::to_string(line) + ": ").append(cause)) {}
}; };
} }

View File

@ -21,7 +21,8 @@ struct tok {
}; };
const type toktype; const type toktype;
const std::string toktext; const std::string toktext;
tok(type Type, std::string_view Text): toktype(Type), toktext(Text) {} const std::pair<unsigned long, unsigned int> linePosition;
tok(type Type, std::string_view Text, auto line, auto position): toktype(Type), toktext(Text), linePosition(line, position) {}
friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; } friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; }
}; };
std::vector<tok> lex(const std::string& in); std::vector<tok> lex(const std::string& in);