-
Notifications
You must be signed in to change notification settings - Fork 3
/
lexer.cpp
148 lines (133 loc) · 3.19 KB
/
lexer.cpp
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#include "lexer.h"
#include <fstream>
#include <iostream>
#include <regex>
#include <string>
#include <vector>
using namespace std;
/*
tokens = [
[/^\s+/, null],
// semicolon
[/^;/, 'SEMI'],
// comments
[/^\/\/.*\/, null],
// multiline comments
[/^\/\*[\s\S]*?\*\//, null],
[/^\.\./, 'CONCAT'],
[/^[\d\.]+/, 'NUMBER'],
// literals: string
[/^"[^"]*"|^'[^']*'/, 'STRING'],
// commas
// [/^\,/,'COMMA'],
// paren
[/^\(/, 'O-PAREN'],
[/^\)/, 'C-PAREN'],
// brackets
[/^\{/, 'O-BRACE'],
[/^\}/, 'C-BRACE'],
[/^\[/, 'O-BRACK'],
[/^\]/, 'C-BRACK'],
[/^\!/, 'EXPI'],
[/^:/, 'COLON'],
[/^>/, 'GRT'],
[/^</, 'LST'],
[/^=/, 'DECLARATION'],
[/^==/, 'EQUALITY'],
[/^\,/, 'COMMA'],
[/^\+/, 'ADD'],
[/^-/, 'SUB'],
[/^\*\/, 'MULT'],
[/^\//, 'DIV'],
// words/vars
[/^[a-zA-Z][a-zA-Z\d\.]*\/, 'WORD'],
]
*/
vector<vector<string>> regtok = {
{"COMMENT", "^\\/\\/.*\\/"},
{"MCOMMENT", "^\\/\\*[\\s\\S]*?\\*\\n"}, // 24
{"NULL", "^\\s+"}, // 0
{"SEMI", "^;"},
{"NUMBER", "^[\\d\\.]+"}, // 2
{"WORD", "^[a-zA-Z_][_a-zA-Z\\d\\.]*"},
{"STRING", "^\"[^\"]*\"|^'[^']*'"}, // 4
{"O-PAREN", "^\\("},
{"C-PAREN", "^\\)"}, // 6
{"O-BRACE", "^\\{"},
{"C-BRACE", "^\\}"}, // 8
{"O-BRACK", "^\\["},
{"C-BRACK", "^\\]"}, // 10
{"EXPI", "^\\!"},
{"COLON", "^:"}, // 12
{"GRT", "^>"},
{"LST", "^<"}, // 14
{"DECLARATION", "^="},
{"EQUALITY", "^=="}, // 16
{"COMMA", "^,"},
{"OPP", "^\\->"}, // 18
{"OPP", "^\\+"}, // 18
{"OPP", "^-"},
{"STAR", "^\\*"}, // 20
{"OPP", "^\\/"},
{"OPP", "^\\.\\."}, // 22
{"AMP", "^&"}, // 22
};
// screw c++, why does it take 9 lines for a strcpy??? :m_finger:
// ^ L
Lexer::Lexer(string filename) {
Lexer::_cursor = 0;
Lexer::tokenRegex = regtok;
ifstream file(filename);
string line;
while (getline(file, line)) {
Lexer::_string += (line + "\n");
}
}
void Lexer::printTokens() {
string out = "";
for (int i = 0; i < Lexer::_tokens.size(); i++) {
out += (Lexer::_tokens[i][0] + ":" + (Lexer::_tokens[i][1]) + "\n");
}
cout << out << endl;
}
// they are really gonna make me do it from scratch, huh?
// no ^ :D
void Lexer::tokenize() {
string str = Lexer::_string;
string s = Lexer::_string;
while (s != "") {
for (auto x : regtok) {
regex regexp(x[1]);
smatch m;
regex_search(s, m, regexp);
if (m.size() != 0) {
string mz = m[0];
// cout << "token! " << x[0] << ": " << mz << endl;
s = s.substr(mz.size(), str.size() - mz.size());
if (x[0] != "NULL")
Lexer::_tokens.push_back({x[0], mz});
}
}
}
}
// uh oh.. regular expression? where's waldo??
// ^ up there buddy
/*
why subject myself to this torture?
bool Lexer::advance() {
if((Lexer::_string.size() - 1) == Lexer::_cursor) {
return true;
}else {
int i = Lexer::_cursor;
char c = Lexer::_string.at(i);
std::string s(1, c);
// vector<string> val = {"CHAR", s};
// Lexer::_tokens.push_back(val);
if(regex_match(s, regex("/^\\s+/"))) {
cout << "whitespace" << endl;
}
Lexer::_cursor++;
return false;
}
}
*/