Refactoring (MAY BE BUGGY)
This commit is contained in:
81
src/lexer/lexer.cpp
Normal file
81
src/lexer/lexer.cpp
Normal file
@@ -0,0 +1,81 @@
|
||||
#include <vector>
|
||||
#include <string>
|
||||
#include "lexer.h"
|
||||
|
||||
using namespace std;
|
||||
|
||||
/*
|
||||
lexer function
|
||||
This function takes the raw text from the file and splits it into a format
|
||||
that the parser can understand.
|
||||
*/
|
||||
|
||||
vector<vector<string>> lexer(string in) {
|
||||
vector<vector<string>> out;
|
||||
vector<string> line;
|
||||
string buf;
|
||||
bool procString = false;
|
||||
bool procChar = false;
|
||||
bool isComment = false;
|
||||
|
||||
for (char i : in) {
|
||||
switch (i) {
|
||||
case '"':
|
||||
if (!isComment) {
|
||||
if (procChar) {
|
||||
buf.push_back(i);
|
||||
} else {
|
||||
procString = !procString;
|
||||
buf.push_back(i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '\'':
|
||||
if (!isComment) {
|
||||
if (procString) {
|
||||
buf.push_back(i);
|
||||
} else {
|
||||
procChar = !procChar;
|
||||
buf.push_back(i);
|
||||
}
|
||||
}
|
||||
break;
|
||||
case '\n':
|
||||
if (!procString && !procChar) {
|
||||
if (!buf.empty()) line.push_back(buf);
|
||||
out.push_back(line);
|
||||
buf.clear();
|
||||
line.clear();
|
||||
isComment = false;
|
||||
} else {
|
||||
if (!isComment) buf.push_back(i);
|
||||
}
|
||||
break;
|
||||
case '#':
|
||||
if (!procString && !procChar) {
|
||||
isComment = true;
|
||||
if (!buf.empty()) line.push_back(buf);
|
||||
out.push_back(line);
|
||||
buf.clear();
|
||||
line.clear();
|
||||
} else {
|
||||
buf.push_back(i);
|
||||
}
|
||||
break;
|
||||
case ' ':
|
||||
if (!procString && !procChar) {
|
||||
if (!buf.empty() && !isComment) line.push_back(buf);
|
||||
buf.clear();
|
||||
} else {
|
||||
buf.push_back(i);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
if (!isComment) buf.push_back(i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!buf.empty()) line.push_back(buf);
|
||||
out.push_back(line);
|
||||
return out;
|
||||
}
|
||||
6
src/lexer/lexer.h
Normal file
6
src/lexer/lexer.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <string>
|
||||
|
||||
std::vector<std::vector<std::string>> lexer(std::string in);
|
||||
Reference in New Issue
Block a user