diff --git a/src/etc/lexer.cpp b/src/etc/lexer.cpp index 72f374d..1918225 100644 --- a/src/etc/lexer.cpp +++ b/src/etc/lexer.cpp @@ -10,22 +10,23 @@ vector resVal; void separate() {resVal.emplace_back(tok::SEPARATOR, "");} -string generatedStr; -bool isLexingString = false; +bool longLex = false; tok::type getIdentifierCharType(const char& Char) { if (isalpha(Char)) return tok::IDENTIFIER; - else if (isalnum(Char)) return tok::NUMBER; + else if (isdigit(Char)) return tok::NUMBER; + else if (Char == '"') return tok::STRING; else return tok::UNEXPECTED; } vector& lex(const string& in) { + pair generated; for (const char& current : in) { - if (!isLexingString) { - if (!generatedStr.empty()) { - resVal.emplace_back(tok::STRING, generatedStr); - generatedStr = ""; + if (!longLex) { + if (!generated.second.empty()) { + resVal.emplace_back(generated.first, generated.second); + generated.second = ""; } switch (current) { case '#': resVal.emplace_back(tok::TAG, "#"); break; @@ -41,18 +42,29 @@ vector& lex(const string& in) case '>': resVal.emplace_back(tok::LCOMP, ">"); break; case '<': resVal.emplace_back(tok::RCOMP, "<"); break; case '\'': resVal.emplace_back(tok::SQUOTE, "\'"); break; - case '"': isLexingString = true; break; case ' ': case '\t': case '\r': case '\n': separate(); break; - default: { // TODO Lex entire identifiers and numbers + default: { tok::type type = getIdentifierCharType(current); - if (type != tok::UNEXPECTED) resVal.emplace_back(type, string(1, current)); + switch (type) { + case tok::UNEXPECTED: break; + case tok::IDENTIFIER: case tok::NUMBER: { + generated.second.append(string(1, current)); + } + case tok::STRING: { + generated.first = type; + longLex = true; + }; break; + default: resVal.emplace_back(type, string(1, current)); + } + break; } } } else { - if ((current == '"')) { // TODO Permit the usage of double quotes in strings using '\' (an example would be "\"") - isLexingString = false; - } else generatedStr.append(string(1, current)); + tok::type currentType = getIdentifierCharType(current); + if ((currentType == tok::STRING) || ((currentType == tok::UNEXPECTED) && ((generated.first == tok::IDENTIFIER) || (generated.first == tok::NUMBER)))) { + longLex = false; + } else generated.second.append(string(1, current)); } } /* Test