-
Notifications
You must be signed in to change notification settings - Fork 0
/
lex.h
102 lines (88 loc) · 2.07 KB
/
lex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
* lex.cpp by sharll-large
* parse pok codes
*/
#ifndef LEX_CPP
#define LEX_CPP
#include <iostream>
#include <vector>
#include <fstream>
#include <algorithm>
#include "builtin/functions/basic/exception.h"
using namespace std;
vector<char> alone = {'{', '}', '<', '>', '(', ')', '[', ']', ',', ';', '~', ':', '?', '-', '+', '*', '/', '%', '.', '^', '&', '!'};
vector<char> quote_like = {'\'', '\"'};
vector<char> space_like = {' ', '\t', '\n'};
char ignore_char = '#';
template<typename T>
bool find(vector<T> &set, T target) {
return any_of(set.begin(), set.end(), [&](T t){return t == target;});
}
void push_if_not_null(vector<string> &target, const string& o) {
if(!o.empty()) target.emplace_back(o);
}
vector<string> parse(vector<string> codes, char filename[]){
vector<string> result;
string current;
char in_quote = 0;
for(int x=0; x<codes.size(); x++){
string source = codes[x];
for(char i: source) {
if(!in_quote){
if(i == ignore_char) {
break;
} else if (find(space_like, i)) {
push_if_not_null(result, current);
current = "";
}
else
{
if (find(alone, i))
{
push_if_not_null(result, current);
current = "";
result.emplace_back(1, i);
}
else
{
current += i;
if(find(quote_like, i)){
in_quote = i;
}
}
}
}else{
if(i == in_quote) {
push_if_not_null(result, current + i);
current = "";
in_quote = 0;
} else {
current += i;
}
}
}
if(in_quote) {
pok_exception(filename, x+1, "quote never closed.");
return {};
}
}
return result;
}
vector<string> readfile(char *filename) {
vector<string> result;
ifstream fin;
fin.open(filename,ios::in);
if(!fin.is_open()) {
pok_exception(filename, 0, "Cannot open file!");
}
string line;
while(getline(fin, line)) {
result.emplace_back(line);
}
fin.close();
return result;
}
vector<string> parse(char filename[]) {
return parse(readfile(filename), filename);
}
#endif