Update lexer for handling operators
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#include "lexer.h"
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <optional>
|
||||
#include <ostream>
|
||||
#include <utility>
|
||||
@@ -44,25 +43,73 @@ bool Lexer::isDelimiter(char c) {
|
||||
*/
|
||||
Lexer::Lexer(std::string in) : file(std::move(in)) {
|
||||
std::string buf;
|
||||
bool instring = false;
|
||||
bool inString = false;
|
||||
while (true) {
|
||||
std::optional<char> c = consume();
|
||||
if (c.has_value()) {
|
||||
if (c.value() == '"') {
|
||||
instring = !instring;
|
||||
if (!instring) {
|
||||
inString = !inString;
|
||||
if (!inString) {
|
||||
content.push_back(buf + '"');
|
||||
buf.clear();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (!instring && isDelimiter(c.value())) {
|
||||
if (!inString && isDelimiter(c.value())) {
|
||||
if (!buf.empty()) content.push_back(buf);
|
||||
if (c.value() != ' ') content.emplace_back(1, c.value());
|
||||
buf.clear();
|
||||
} else if (c.value() == '/' && peek(0).has_value() && peek(0).value() == '/') {
|
||||
while (c.has_value() && c.value() != '\n') {
|
||||
c = consume();
|
||||
switch (c.value()) {
|
||||
case '=':
|
||||
if (peek() == '=') {
|
||||
consume();
|
||||
content.emplace_back("==");
|
||||
} else {
|
||||
content.emplace_back("=");
|
||||
}
|
||||
break;
|
||||
case '>':
|
||||
if (peek() == '=') {
|
||||
consume();
|
||||
content.emplace_back(">=");
|
||||
} else {
|
||||
content.emplace_back(">");
|
||||
}
|
||||
break;
|
||||
case '<':
|
||||
if (peek() == '=') {
|
||||
consume();
|
||||
content.emplace_back("<=");
|
||||
} else {
|
||||
content.emplace_back("<");
|
||||
}
|
||||
break;
|
||||
case '+':
|
||||
case '-':
|
||||
case '*':
|
||||
case '^':
|
||||
if (peek() == '=') {
|
||||
consume();
|
||||
content.emplace_back(std::string(1, c.value()) + "=");
|
||||
} else {
|
||||
content.emplace_back(1, c.value());
|
||||
}
|
||||
break;
|
||||
case '/':
|
||||
if (peek() == '=') {
|
||||
consume();
|
||||
content.emplace_back("/=");
|
||||
} else if (peek() == '/') {
|
||||
while (c.has_value() && c.value() != '\n') {
|
||||
c = consume();
|
||||
}
|
||||
} else {
|
||||
content.emplace_back("/");
|
||||
}
|
||||
case ' ':
|
||||
break;
|
||||
default:
|
||||
content.emplace_back(1, c.value());
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
buf += c.value();
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
class Lexer {
|
||||
private:
|
||||
std::vector<char> delimiters = {
|
||||
'(', ')', '{', '}', '[', ']', '.', '\n', ' ', ','
|
||||
'(', ')', '{', '}', '[', ']', '.', '\n', '+', '-', '*', '/', '^', '>', '<', ' ', ','
|
||||
};
|
||||
std::string file;
|
||||
size_t incrementor = -1;
|
||||
|
||||
Reference in New Issue
Block a user