-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.js
306 lines (250 loc) · 6.27 KB
/
Lexer.js
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
function Pattern(name, pattern) {
this.name = name;
this.pattern = pattern;
}
// regex patterns and coresponding token name
const tokens = [
// whitespace
new Pattern(
"_WHITESPACE_",
/\s/
),
// comments
// multi-line
new Pattern(
"_WHITESPACE_",
/(##)[^#]*(#)/
),
// single-line
new Pattern(
"_WHITESPACE_",
/(#)[^\n]*/
),
// keywords
new Pattern(
"NUMTYPE",
/num/
),
new Pattern(
"LISTTYPE",
/list/
),
new Pattern(
"STRINGTYPE",
/string/
),
new Pattern(
"GET",
/get/
),
new Pattern(
"SET",
/set/
),
new Pattern(
// so as to not be confused with addition
"ADDKEYWORD",
/add/
),
new Pattern(
"WHILE",
/while/
),
new Pattern(
"METHOD_TOSTRING",
/toString\(\)/
),
// groupings
new Pattern(
"PRINT",
/print/
),
new Pattern(
"OPENPIPE",
/\[/
),
new Pattern(
"CLOSEPIPE",
/\]/
),
new Pattern(
"OPENPAREN",
/\(/
),
new Pattern(
"CLOSEPAREN",
/\)/
),
new Pattern(
"OPENCURLY",
/\{/
),
new Pattern(
"CLOSECURLY",
/\}/
),
new Pattern(
"ID",
/[a-zA-Z]+/
),
new Pattern(
"ASSIGN",
/=/
),
new Pattern(
"DOT",
/\./
),
new Pattern(
"COMMA",
/\,/
),
// types
new Pattern(
"NUM",
/[0-9]+/
),
new Pattern(
"STRING",
/(")[^"]*(")/
),
new Pattern(
"STRING",
/(')[^']*(')/
),
new Pattern(
"INCREMENTASSIGN",
/\+=/
),
// math
new Pattern(
"PLUS",
/(\+)/
),
new Pattern(
"MINUS",
/(\-)/
),
new Pattern(
"TIMES",
/(\*)/
),
new Pattern(
"DIVIDES",
/(\/)/
),
// comparison
new Pattern(
"LESSTHAN",
/</
),
new Pattern(
"GREATERTHAN",
/>/
),
new Pattern(
"SEMICOLON",
/;/
),
// error fallback
new Pattern(
"_ERROR_",
/./
)
];
const tokensWithValue = [
"NUMTYPE", "ID", "NUM", "_ERROR_", "STRING"
];
/**
*
* Go through each token and find the first the index of it's
* first match in text. If the index is less than the smallest
* index and not -1 (no match is found), and the index is less
* than the smallest index found so far, then update the smallest
* index found so far and update match (the token that coresponds
* to smallest). Respects precedence of tokens by order listed
* out in tokens array. For instance, for int as NUMTYPE to not
* count as i for an ID, because NUMTYPE is listed higher up in
* tokens array, that would update smallest before ID does. So,
* when the ID does find a match with i at same index as int for
* NUMTYPE, smallest would not be updated because the indexes are
* the same.
*
* @param text: the current substring of program looking at to find the next token
* @returns the token, as an object, of the token found
*/
function getToken(text) {
let smallest = text.length + 2;
let match = null;
for(let i = 0; i < tokens.length; i++) {
let check = text.search(tokens[i].pattern);
if((check < smallest) && (check != -1)) {
smallest = check;
match = tokens[i];
}
}
return match;
}
function Token(name, value) {
this.name = name;
this.value = value;
}
function checkForSpecialValue(tokenName, value) {
switch(tokenName) {
case "STRING":
let after = value.replace(
/["']/g,
""
);
return after;
default:
return value;
}
}
module.exports.lex = function(program) {
let tokenStream = [];
let startIndex = 0;
let lexingFailed = false;
while(startIndex < program.length) {
// current substring of text to look at to find the next token
let current = program.substring(startIndex);
// object that has a name (ie. NUMTYPE), and it's corresponding regex pattern
// regex pattern needed for match done below (also explained further below)
let token = getToken(current);
// token would only be null when even error fallback can't match to anything, basically something has gone very wrong
if(token != null ) {
// get the actual substring of what the token matches to (found using its pattern), but the return value of the match method gives extra stuff that is not needed, so use matched below to get only the actual substring.
let matchedFull = current.match(token.pattern);
let matched = matchedFull[0];
// the index at witch matched starts at in current
let tokenStart = current.indexOf(matched);
let tokenEnd = tokenStart + matched.length;
// only actually add token and, if required, it's value if not white space
if(token.name != "_WHITESPACE_") {
let value = null;
if(tokensWithValue.includes(token.name)) {
value = matched;
// some values should be something different than the literal regex match
value = checkForSpecialValue(token.name, value);
}
// if(token.name == "_ERROR_") {
tokenStream.push(new Token(token.name, value));
// }
}
// update startIndex so the next substring of program will move past the token just found
startIndex += tokenEnd;
}
// only completely fail lexing if can't match to anything, including error fallback
else {
lexingFailed = true;
console.log("ERROR: Lexing failed at index ", startIndex, "with: ", current);
break;
}
}
if(lexingFailed) {
return [null];
}
else {
return tokenStream;
}
}