Remove the unlikely attribute from the '\r' case, and add an EOF token type

This commit is contained in:
Username404-59 2021-09-23 13:21:05 +02:00
parent f3322f996e
commit 6af6c04d5f
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
3 changed files with 8 additions and 5 deletions

View File

@ -21,8 +21,9 @@ vector<tok> 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<tok::type>(current), string(1, current), lineNumber);
[[likely]] case ' ': case '\t': [[unlikely]] case '\r': break;
[[unlikely]] case EOF_:
resVal.emplace_back(static_cast<tok::type>(current), lineNumber);
[[likely]] case ' ': case '\t': case '\r': break;
[[likely]] case '\n': ++lineNumber; break;
default: {
tok::type type = getIdentifierCharType(current);

View File

@ -12,7 +12,7 @@ struct tok {
};
enum type: const unsigned short {
UNEXPECTED = std::numeric_limits<unsigned char>::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<tok> lex(const std::string& in);

View File

@ -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) {