From 6af6c04d5f13be5fdff4660cbd6872eb8e6a3c12 Mon Sep 17 00:00:00 2001 From: Username404-59 Date: Thu, 23 Sep 2021 13:21:05 +0200 Subject: [PATCH] Remove the unlikely attribute from the '\r' case, and add an EOF token type --- src/etc/lexer.cpp | 5 +++-- src/headers/lex.hpp | 4 +++- src/headers/parsing/Parser.hpp | 4 ++-- 3 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/etc/lexer.cpp b/src/etc/lexer.cpp index 5ff64a7..09135ce 100644 --- a/src/etc/lexer.cpp +++ b/src/etc/lexer.cpp @@ -21,8 +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), lineNumber); - [[likely]] case ' ': case '\t': [[unlikely]] case '\r': break; + [[unlikely]] case EOF_: + resVal.emplace_back(static_cast(current), lineNumber); + [[likely]] case ' ': case '\t': case '\r': break; [[likely]] case '\n': ++lineNumber; break; default: { tok::type type = getIdentifierCharType(current); diff --git a/src/headers/lex.hpp b/src/headers/lex.hpp index 041163f..306eb38 100644 --- a/src/headers/lex.hpp +++ b/src/headers/lex.hpp @@ -12,7 +12,7 @@ struct tok { }; enum type: const unsigned short { UNEXPECTED = std::numeric_limits::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR, - DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', + EOF_ = '\0', DEFINE = '=', TAG = '#', DOLLAR_SIGN = '$', DOT = '.', PLUS = '+', LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')', RBRACE = '}', RBRACKET = ']', HYPHEN = '-', LCOMP = '>', RCOMP = '<', @@ -23,6 +23,8 @@ struct tok { const std::string toktext; const unsigned long line = 0; tok(type Type, std::string_view Text, decltype(line) line): toktype(Type), toktext(Text), line(line) {} + explicit tok(type Type, const char& character, decltype(line) line): tok(Type, std::string(1, character), line) {}; + inline tok(type Type, decltype(line) line): tok(Type, Type, line) {}; friend std::ostream& operator<<(std::ostream& output, const tok& it) { return output << it.toktext; } }; std::vector lex(const std::string& in); diff --git a/src/headers/parsing/Parser.hpp b/src/headers/parsing/Parser.hpp index b1056c3..708beb4 100644 --- a/src/headers/parsing/Parser.hpp +++ b/src/headers/parsing/Parser.hpp @@ -26,7 +26,7 @@ namespace Parser { return true; }; for (;i < lexed.size(); ++i) { - const tok& current = lexed[i], next = (i != lexed.size() - 1) ? lexed[i + 1] : tok(UNEXPECTED, string(), current.line); + const tok& current = lexed[i], next = (current.toktype != EOF_) ? lexed[i + 1] : current; switch (current.toktype) { case STRING: parseTree << types::String(current.toktext.data()); break; @@ -34,7 +34,7 @@ namespace Parser { if (current.toktext == "class") { if (next.toktype == IDENTIFIER) { parseTree << Class(next.toktext); ++i; - } else throw ParsingException((not next.toktext.empty()) ? '"' + next.toktext + "\" is not a valid class identifier" : "A class identifier is required", next.line); + } else throw ParsingException((not (next.toktext.empty() or next.toktype == tok::EOF_)) ? '"' + next.toktext + "\" is not a valid class identifier" : "A class identifier is required", next.line); } else { bool isFinalDefine = nextAre({TAG, DEFINE}); if (isFinalDefine || next.toktype == DEFINE) {