From 6955c5cb4d12a15d7e55d4e2ea2c62ef70d6f73f Mon Sep 17 00:00:00 2001 From: Username404 Date: Tue, 1 Mar 2022 19:40:30 +0100 Subject: [PATCH] Remove tok::type::EOF_ because it is useless, and fix the `next` local constant reference in Parser.hpp Signed-off-by: Username404 --- src/etc/lexer.cpp | 5 ++--- src/headers/lex.hpp | 2 +- src/headers/parsing/Parser.hpp | 9 ++++----- 3 files changed, 7 insertions(+), 9 deletions(-) diff --git a/src/etc/lexer.cpp b/src/etc/lexer.cpp index bc88f68..4a048b0 100644 --- a/src/etc/lexer.cpp +++ b/src/etc/lexer.cpp @@ -13,7 +13,7 @@ vector lex(const string& in) { vector 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]; switch (current) { @@ -22,7 +22,7 @@ vector lex(const string& in) if (current == TAG && in[i + 1] == DEFINE) { // See the IDENTIFIER case in Parser.hpp goto insertToken; } else { - while (not (in[i + 1] == EOF_ || in[i + 1] == '\n')) { + while (not (i == in.size() || in[i + 1] == '\n')) { ++i; } break; @@ -42,7 +42,6 @@ vector lex(const string& in) break; } else goto insertToken; } - [[unlikely]] case EOF_: --lineNumber; case DEFINE: case LPAR: case RPAR: case COMMA: case LBRACE: case RBRACE: case LBRACKET: case RBRACKET: case PLUS: case HYPHEN: case LCOMP: case RCOMP: diff --git a/src/headers/lex.hpp b/src/headers/lex.hpp index 4a8903d..d73e65e 100644 --- a/src/headers/lex.hpp +++ b/src/headers/lex.hpp @@ -10,7 +10,7 @@ struct tok { typedef Yerbacon::Exception LexerException; enum type: const unsigned short { UNEXPECTED = std::numeric_limits::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR, - EOF_ = '\0', DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', COMMA = ',', + DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', COMMA = ',', LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')', RBRACE = '}', RBRACKET = ']', PLUS = '+', HYPHEN = '-', DIVIDE = '/', diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index a129bc8..7edd31e 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -32,7 +32,8 @@ namespace Parser { return true; }; for (;i < lexed.size(); ++i) { - const tok& current = lexed[i], next = (current.toktype != EOF_) ? lexed[i + 1] : current; + const bool hasNext = (i + 1) < lexed.size(); + const tok& current = lexed[i], next = hasNext ? lexed[i + 1] : tok(UNEXPECTED); switch (current.toktype) { case STRING: parseTree << types::String(current.toktext.data()); break; @@ -41,7 +42,7 @@ namespace Parser { if (next.toktype == IDENTIFIER) { parseTree << Class(next.toktext); ++i; } else { - const bool isNotBlank = (not (next.toktext.empty() or next.toktype == tok::EOF_)); + const bool isNotBlank = not next.toktext.empty(); parsingError(next, isNotBlank ? " is not a valid class identifier" : "A class identifier is required", isNotBlank); } } else { @@ -67,9 +68,7 @@ namespace Parser { return it.toktype == inverseCharacter; }); if (closingCharacter != lexed.cend()) { - vector subTokens; - subTokens.reserve(distance(lexed.cbegin() + i, closingCharacter)); - subTokens.assign(lexed.cbegin() + i + 1, closingCharacter); + vector subTokens(lexed.cbegin() + i + 1, closingCharacter); if (current.toktype == LPAR || current.toktype == LBRACKET) { if (subTokens.size() >= 2 && subTokens[1].toktype != RPAR) { for (auto iterator = subTokens.cbegin(); iterator < (subTokens.cend() - 1); ++iterator) {