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