Remove the unlikely attribute from the '\r' case, and add an EOF token type
This commit is contained in:
parent
f3322f996e
commit
6af6c04d5f
@ -21,8 +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), lineNumber);
|
[[unlikely]] case EOF_:
|
||||||
[[likely]] case ' ': case '\t': [[unlikely]] case '\r': break;
|
resVal.emplace_back(static_cast<tok::type>(current), lineNumber);
|
||||||
|
[[likely]] case ' ': case '\t': case '\r': break;
|
||||||
[[likely]] case '\n': ++lineNumber; break;
|
[[likely]] case '\n': ++lineNumber; break;
|
||||||
default: {
|
default: {
|
||||||
tok::type type = getIdentifierCharType(current);
|
tok::type type = getIdentifierCharType(current);
|
||||||
|
@ -12,7 +12,7 @@ struct tok {
|
|||||||
};
|
};
|
||||||
enum type: const unsigned short {
|
enum type: const unsigned short {
|
||||||
UNEXPECTED = std::numeric_limits<unsigned char>::max() + 1, IDENTIFIER, NUMBER, ALPHACHAR,
|
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 = ')',
|
PLUS = '+', LPAR = '(', LBRACE = '{', LBRACKET = '[', RPAR = ')',
|
||||||
RBRACE = '}', RBRACKET = ']',
|
RBRACE = '}', RBRACKET = ']',
|
||||||
HYPHEN = '-', LCOMP = '>', RCOMP = '<',
|
HYPHEN = '-', LCOMP = '>', RCOMP = '<',
|
||||||
@ -23,6 +23,8 @@ struct tok {
|
|||||||
const std::string toktext;
|
const std::string toktext;
|
||||||
const unsigned long line = 0;
|
const unsigned long line = 0;
|
||||||
tok(type Type, std::string_view Text, decltype(line) line): toktype(Type), toktext(Text), line(line) {}
|
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; }
|
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);
|
||||||
|
@ -26,7 +26,7 @@ namespace Parser {
|
|||||||
return true;
|
return true;
|
||||||
};
|
};
|
||||||
for (;i < lexed.size(); ++i) {
|
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) {
|
switch (current.toktype) {
|
||||||
case STRING: parseTree << types::String(current.toktext.data()); break;
|
case STRING: parseTree << types::String(current.toktext.data()); break;
|
||||||
@ -34,7 +34,7 @@ namespace Parser {
|
|||||||
if (current.toktext == "class") {
|
if (current.toktext == "class") {
|
||||||
if (next.toktype == IDENTIFIER) {
|
if (next.toktype == IDENTIFIER) {
|
||||||
parseTree << Class(next.toktext); ++i;
|
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 {
|
} else {
|
||||||
bool isFinalDefine = nextAre({TAG, DEFINE});
|
bool isFinalDefine = nextAre({TAG, DEFINE});
|
||||||
if (isFinalDefine || next.toktype == DEFINE) {
|
if (isFinalDefine || next.toktype == DEFINE) {
|
||||||
|
Loading…
Reference in New Issue
Block a user