From fd6ce352911e1210b2de187028c663cc058e8b5d Mon Sep 17 00:00:00 2001 From: Username404 Date: Wed, 18 Aug 2021 19:31:02 +0200 Subject: [PATCH] Add line numbers to Yerbacon::Exception --- src/etc/lexer.cpp | 13 +++++++------ src/headers/Yerbacon.hpp | 2 ++ src/headers/lex.hpp | 3 ++- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/etc/lexer.cpp b/src/etc/lexer.cpp index 74ce992..aa2466f 100644 --- a/src/etc/lexer.cpp +++ b/src/etc/lexer.cpp @@ -12,6 +12,7 @@ tok::type getIdentifierCharType(const char& Char) { vector lex(const string& in) { vector resVal; + unsigned long lineNumber = 1; for (unsigned int i = 0; i < in.size(); ++i) { const char& current = in[i]; @@ -20,9 +21,9 @@ vector lex(const string& in) case LBRACE: case RBRACE: case LBRACKET: case RBRACKET: case PLUS: case HYPHEN: case LCOMP: case RCOMP: case DOT: case DOLLAR_SIGN: case SQUOTE: - resVal.emplace_back(static_cast(current), string(1, current)); - [[likely]] case ' ': case '\t': case '\r': - [[likely]] case '\n': break; + resVal.emplace_back(static_cast(current), string(1, current), lineNumber, i); + [[likely]] case ' ': case '\t': case '\r': break; + [[likely]] case '\n': ++lineNumber; break; default: { tok::type type = getIdentifierCharType(current); bool isTypeString = (type == STRING); @@ -33,18 +34,18 @@ vector lex(const string& in) for (;i < in.size(); ++i) { tok::type currentCharType = getIdentifierCharType(in[i]); 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) { formedString += string(1, in[i]); } else { if (not isTypeString) --i; - resVal.emplace_back(type, formedString); + resVal.emplace_back(type, formedString, lineNumber, i); break; } } } case UNEXPECTED: break; - default: resVal.emplace_back(type, string(1, current)); + default: resVal.emplace_back(type, string(1, current), lineNumber, i); } break; } diff --git a/src/headers/Yerbacon.hpp b/src/headers/Yerbacon.hpp index 69f84d7..42852e9 100644 --- a/src/headers/Yerbacon.hpp +++ b/src/headers/Yerbacon.hpp @@ -13,6 +13,7 @@ #include #include #include +#include namespace Yerbacon { consteval const char* getVersion() noexcept { return YBCON_VERSION; } @@ -24,6 +25,7 @@ namespace Yerbacon { } public: 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)) {} }; } diff --git a/src/headers/lex.hpp b/src/headers/lex.hpp index bdaa676..6c43ecd 100644 --- a/src/headers/lex.hpp +++ b/src/headers/lex.hpp @@ -21,7 +21,8 @@ struct tok { }; const type toktype; const std::string toktext; - tok(type Type, std::string_view Text): toktype(Type), toktext(Text) {} + const std::pair 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; } }; std::vector lex(const std::string& in);