Save 13 lines in lexer.cpp and remove the longlex boolean variable.

This commit is contained in:
Username404-59 2021-07-06 10:11:25 +02:00
parent 9d36279f95
commit e8c633d545
Signed by: Username404-59
GPG Key ID: 7AB361FBB257A5D1
1 changed files with 37 additions and 50 deletions

View File

@ -12,14 +12,8 @@ tok::type getIdentifierCharType(const char& Char) {
vector<tok> lex(const string& in) vector<tok> lex(const string& in)
{ {
vector<tok> resVal; vector<tok> resVal;
bool longLex = false; for (unsigned int i = 0; i < in.size(); ++i) {
pair<tok::type, string> generated; const char& current = in[i];
for (const char& current : in) {
if (!longLex) {
if (!generated.second.empty()) {
resVal.emplace_back(generated.first, generated.second);
generated.second = "";
}
switch (current) { switch (current) {
case '#': resVal.emplace_back(tok::TAG, "#"); break; case '#': resVal.emplace_back(tok::TAG, "#"); break;
case '=': resVal.emplace_back(tok::DEFINE, "="); break; case '=': resVal.emplace_back(tok::DEFINE, "="); break;
@ -40,12 +34,18 @@ vector<tok> lex(const string& in)
tok::type type = getIdentifierCharType(current); tok::type type = getIdentifierCharType(current);
switch (type) { switch (type) {
case tok::UNEXPECTED: break; case tok::UNEXPECTED: break;
case tok::STRING: ++i;
case tok::IDENTIFIER: case tok::NUMBER: { case tok::IDENTIFIER: case tok::NUMBER: {
generated.second.append(string(1, current)); string formedString;
for (;i < in.size(); ++i) {
bool isString = getIdentifierCharType(in[i]) == tok::STRING;
if ((getIdentifierCharType(in[i]) == type && !isString) || (type == tok::STRING && !isString)) {
formedString += string(1, in[i]);
} else {
resVal.emplace_back(type, formedString);
break;
}
} }
case tok::STRING: {
generated.first = type;
longLex = true;
break; break;
} }
default: resVal.emplace_back(type, string(1, current)); default: resVal.emplace_back(type, string(1, current));
@ -53,23 +53,10 @@ vector<tok> lex(const string& in)
break; break;
} }
} }
} else {
tok::type currentType = getIdentifierCharType(current);
switch (currentType) {
case tok::UNEXPECTED: if ((generated.first == currentType) || (generated.first == tok::STRING)) break;
case tok::STRING: {
longLex = false;
break;
}
default: generated.second.append(string(1, current)); break;
}
}
} }
/* Test /* Test
for (const auto& it : resVal) { for (const auto& it : resVal) {
if (it.toktype == tok::STRING) cout << '"' << it.toktext << '"' << '\n'; cout << it.toktext << ' ' << it.toktype << '\n';
if (it.toktype == tok::NUMBER) cout << it.toktext << "= number\n";
if (it.toktype == tok::IDENTIFIER) cout << it.toktext << '\n';
} }
*/ */
return resVal; return resVal;