-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtokenizer.cjs
More file actions
538 lines (470 loc) · 17.7 KB
/
tokenizer.cjs
File metadata and controls
538 lines (470 loc) · 17.7 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
const TokenType = {
Number: 'Number',
Parameter: 'Parameter',
Word: 'Word',
Function: 'Function',
Operator: 'Operator',
BracketOpen: 'BracketOpen',
BracketClose: 'BracketClose',
Assign: 'Assign',
EOL: 'EOL',
EOF: 'EOF',
OpenBracket: 'OpenBracket',
FlowControl: 'FlowControl',
LogicalOperator: 'LogicalOperator',
ComparisonOperator: 'ComparisonOperator',
Unknown: 'Unknown',
Comment: 'Comment'
};
const FLOW_CONTROL_KEYWORDS = new Set([
'IF', 'WHILE', 'SUB', 'ENDSUB', 'ENDWHILE', 'ENDIF',
'ELSEIF', 'ELSE', 'CALL', 'DO', 'RETURN', 'REPEAT',
'ENDREPEAT', 'BREAK', 'CONTINUE'
]);
const LOGICAL_OPERATORS = new Set(['AND', 'OR', 'XOR']);
const COMPARISON_OPERATORS = new Set(['LT', 'LE', 'GT', 'GE', 'EQ', 'NE']);
const MULTI_CHAR_OPS = new Set([...LOGICAL_OPERATORS, ...COMPARISON_OPERATORS, '**', 'MOD']);
const FUNCTIONS = new Set([
'ATAN', 'ABS', 'ACOS', 'ASIN', 'COS', 'EXP',
'FIX', 'FUP', 'ROUND', 'LN', 'SIN', 'SQRT', 'TAN', 'EXISTS'
]);
const DICTIONARY_TOKENS = new Set([
...FLOW_CONTROL_KEYWORDS,
...LOGICAL_OPERATORS,
...COMPARISON_OPERATORS,
...MULTI_CHAR_OPS,
...FUNCTIONS
]);
function getTokenType(word) {
const up = word.toUpperCase();
if (FUNCTIONS.has(up)) return TokenType.Function;
if (FLOW_CONTROL_KEYWORDS.has(up)) return TokenType.FlowControl;
if (LOGICAL_OPERATORS.has(up)) return TokenType.LogicalOperator;
if (COMPARISON_OPERATORS.has(up)) return TokenType.ComparisonOperator;
if (MULTI_CHAR_OPS.has(up)) {
return (up === 'MOD' ? TokenType.Operator : TokenType.ComparisonOperator);
}
return TokenType.Word;
}
class Tokenizer {
constructor(input) {
this.input = input;
this.pos = 0;
this.line = 1;
this.buffer = [];
this.pendingTokens = [];
// Track nested bracket expressions like [ ... ]
this.expressionLevel = 0;
// We'll track the most recently returned token type
this.previousTokenType = null;
// Not strictly necessary, but preserved from original
this.lineStartsWithO = false;
}
preprocessInput() {
let cleanedInput = '';
let inComment = false;
// First pass: identify whitespace-only lines
const lines = this.input.split(/\r\n|\r|\n/);
const whitespaceOnlyLines = new Set();
for (let lineIndex = 0; lineIndex < lines.length; lineIndex++) {
const line = lines[lineIndex];
if (line.length > 0 && /^\s+$/.test(line)) {
whitespaceOnlyLines.add(lineIndex);
}
}
// Second pass: process character by character, preserving whitespace-only lines
let currentLine = 0;
for (let i = 0; i < this.input.length; i++) {
const ch = this.input[i];
// Track line numbers
if (ch === '\n' || (ch === '\r' && this.input[i + 1] !== '\n')) {
currentLine++;
}
// Start comment on ';' or '(' (unless "()")
if (!inComment && (ch === ';' || (ch === '(' && this.input[i + 1] !== ')'))) {
inComment = true;
}
// End comment on newline or '\r' that isn't followed by '\n'
else if (inComment && (ch === '\n' || (ch === '\r' && this.input[i + 1] !== '\n'))) {
inComment = false;
}
// <-- Add this block to handle closing parenthesis -->
else if (inComment && ch === ')') {
inComment = false;
// We still append the ')' if you want it in the comment text
cleanedInput += ch;
continue;
}
if (inComment) {
cleanedInput += ch;
} else if (ch === '\n' || ch === '\r') {
cleanedInput += ch;
} else if (ch === '\t' || ch === ' ') {
// Preserve whitespace if this is a whitespace-only line
if (whitespaceOnlyLines.has(currentLine)) {
cleanedInput += ch;
} else {
// Otherwise skip whitespace as before
continue;
}
} else {
cleanedInput += ch;
}
}
return cleanedInput;
}
eof() {
return this.pos >= this.input.length && this.buffer.length === 0;
}
peek(offset = 0) {
if (offset < this.buffer.length) {
return this.buffer[offset];
}
const inputOffset = offset - this.buffer.length;
return this.input[this.pos + inputOffset] || '';
}
nextChar() {
if (this.buffer.length > 0) {
return this.buffer.shift();
}
const ch = this.input[this.pos++];
// Update line # if we see a newline, or a '\r' not followed by '\n'
if (ch === "\n" || (ch === "\r" && this.peek(1) === "\n")) {
this.line++;
}
return ch;
}
createToken(type, value, line) {
return { type, value, line: line || this.line };
}
tryReadEndOfLine() {
// 1) Windows-style CR+LF
if (this.peek() === '\r' && this.peek(1) === '\n') {
this.nextChar(); // consume '\r'
this.nextChar(); // consume '\n'
this.lineStartsWithO = (this.peek() === 'o' || this.peek() === 'O');
return this.createToken(TokenType.EOL, '\r\n');
}
// 2) Classic Mac-style CR
else if (this.peek() === '\r') {
this.nextChar(); // consume '\r'
this.lineStartsWithO = (this.peek() === 'o' || this.peek() === 'O');
return this.createToken(TokenType.EOL, '\r');
}
// 3) Unix-style LF
else if (this.peek() === '\n') {
this.nextChar(); // consume '\n'
this.lineStartsWithO = (this.peek() === 'o' || this.peek() === 'O');
return this.createToken(TokenType.EOL, '\n');
}
// If it's none of the above, no EOL token
return null;
}
tryReadComment() {
// Single-line comment using ';'
if (this.peek() === ';') {
let comment = this.nextChar();
while (!this.eof() && this.peek() !== "\n" && this.peek() !== "\r") {
comment += this.nextChar();
}
return this.createToken(TokenType.Comment, comment);
}
// Parenthetical comment: '(' ... ')'
else if (this.peek() === '(') {
let comment = this.nextChar();
let isCommentComplete = false;
while (!this.eof()) {
const ch = this.nextChar();
comment += ch;
if (ch === ')') {
isCommentComplete = true;
break; // End of comment reached
}
// If end of line is reached without closing parenthesis, it's an unknown comment
if (this.peek() === '\n' || this.peek() === '\r') {
break;
}
}
if (isCommentComplete) {
return this.createToken(TokenType.Comment, comment);
}
// If no closing parenthesis was found before EOL, treat as unknown
return this.createToken(TokenType.Unknown, comment);
}
return null;
}
tryReadNumber() {
if (/[0-9.]/.test(this.peek())) {
let numStr = "";
let hasDecimal = false;
while (!this.eof()) {
const ch = this.peek();
if (ch === '.') {
if (hasDecimal) break; // If we already have a decimal point, break
hasDecimal = true;
} else if (!/[0-9]/.test(ch)) {
break;
}
numStr += this.nextChar();
}
if (numStr === '.') { // If the entire number is just a dot
return this.createToken(TokenType.Unknown, '.');
}
return this.createToken(TokenType.Number, numStr);
}
return null;
}
tryReadParameter() {
// <...> or ##<...> or #<...> or ##NNN or #NNN style parameters
if (this.peek() === '<') {
let param = this.nextChar();
while (!this.eof() && this.peek() !== '>' && !/\r|\n/.test(this.peek())) {
param += this.nextChar();
}
if (this.peek() === '>') param += this.nextChar();
return this.createToken(TokenType.Parameter, param);
} else if (this.peek() === '#') {
let param = this.nextChar();
// Check for double ## (indirect/dereferencing)
if (this.peek() === '#') {
param += this.nextChar();
}
if (this.peek() === '<') {
param += this.nextChar();
while (!this.eof() && this.peek() !== '>' && !/\r|\n/.test(this.peek())) {
param += this.nextChar();
}
if (this.peek() === '>') param += this.nextChar();
} else {
while (!this.eof() && /[0-9]/.test(this.peek())) {
param += this.nextChar();
}
}
return this.createToken(TokenType.Parameter, param);
}
return null;
}
tryReadAssignment() {
if (this.peek() === '=') {
this.nextChar();
return this.createToken(TokenType.Assign, '=');
}
return null;
}
tryStartExpression() {
if (this.peek() === '[') {
this.expressionLevel++;
this.nextChar();
return this.createToken(TokenType.BracketOpen, '[');
}
return null;
}
tryEndExpression() {
if (this.peek() === ']') {
if (this.expressionLevel > 0) {
this.expressionLevel--;
this.nextChar();
return this.createToken(TokenType.BracketClose, ']');
}
}
return null;
}
tryReadOperator() {
const ch = this.peek();
// Check for '%' as a Word
if (ch === '%') {
this.nextChar();
return this.createToken(TokenType.Word, '%');
}
// Check for '+' or '-'
if (ch === '+' || ch === '-') {
this.nextChar();
return this.createToken(TokenType.Operator, ch);
}
// Check for '*'
if (ch === '*') {
// If next char is also '*', then it's '**'
if (this.peek(1) === '*') {
this.nextChar(); // consume first '*'
this.nextChar(); // consume second '*'
return this.createToken(TokenType.Operator, '**');
}
// Otherwise just a single '*'
this.nextChar();
return this.createToken(TokenType.Operator, '*');
}
// Check for '/'
if (ch === '/') {
this.nextChar();
return this.createToken(TokenType.Operator, '/');
}
return null; // Not an operator we handle here
}
readWordSegment() {
let segment = "";
while (!this.eof() && /[A-Za-z$]/.test(this.peek())) {
segment += this.nextChar();
}
return segment;
}
segmentWordSegment(segment) {
if (!segment) return [];
// Check all possible prefixes from longest to shortest
for (let len = segment.length; len >= 1; len--) {
const prefix = segment.substring(0, len);
const upperPrefix = prefix.toUpperCase();
if (DICTIONARY_TOKENS.has(upperPrefix)) {
const tokenType = getTokenType(prefix);
const token = this.createToken(tokenType, prefix);
const remaining = segment.substring(len);
const restTokens = this.segmentWordSegment(remaining);
return [token, ...restTokens];
}
}
// If no valid prefix found, split into first character and rest
const firstChar = segment[0];
const remaining = segment.substring(1);
const token = this.createToken(TokenType.Word, firstChar);
const restTokens = this.segmentWordSegment(remaining);
return [token, ...restTokens];
}
/**
* Refines a single token in-place, handling the "Function" special-case
* that might need to be split into (Word + Function).
*/
refineSingleToken(token) {
// If it's a function token...
if (token.type === TokenType.Function) {
// Are we at the start of the file, or is previous token type not
// one of the 'allowed' preceding types?
const isFirst = (this.previousTokenType === null);
const notAllowedBefore = ![
TokenType.Word,
TokenType.Operator,
TokenType.BracketOpen,
TokenType.LogicalOperator,
TokenType.Assign
].includes(this.previousTokenType);
if (isFirst || notAllowedBefore) {
// We'll split the function name into firstChar as Word, rest as Function
const funcValue = token.value; // e.g. "EXISTS"
const firstChar = funcValue[0]; // "E"
const rest = funcValue.slice(1); // "XISTS"
// If rest is empty or just 1 char, do as you wish—but typically:
const splitted = [
this.createToken(TokenType.Word, firstChar, token.line),
this.createToken(TokenType.Function, rest, token.line),
];
return splitted;
}
}
// Otherwise, no refinement
return [token];
}
/**
* Core: readNextToken() produces exactly one final token each call,
* applying refinement logic on-the-fly.
*/
readNextToken() {
// If we have leftover tokens, use those first
if (this.pendingTokens.length > 0) {
const t = this.pendingTokens.shift();
this.previousTokenType = t.type;
return t;
}
// If input is done, yield EOF
if (this.eof()) {
const eofToken = this.createToken(TokenType.EOF, null);
this.previousTokenType = eofToken.type;
return eofToken;
}
// 1) Attempt to read known token types in the usual order
let token = this.tryReadEndOfLine();
if (token) {
// EOL
// "Refine" is trivial for EOL, so we skip
this.previousTokenType = token.type;
return token;
}
token = this.tryReadComment();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryReadNumber();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryReadParameter();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryReadAssignment();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryStartExpression();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryEndExpression();
if (token) {
this.previousTokenType = token.type;
return token;
}
token = this.tryReadOperator();
if (token) {
this.previousTokenType = token.type;
return token;
}
// 2) If none of those matched, read a word segment
const segment = this.readWordSegment();
if (segment) {
// Segment might resolve to multiple tokens (e.g. "ENDWHILE" => [FlowControl], or "EXISTS" => [Function], etc.)
const segmentedTokens = this.segmentWordSegment(segment);
// We only refine the first token out of segmentedTokens right now;
// the rest we push to pendingTokens, where they'll also be refined
// before we pop them out in subsequent calls.
// Take the first token from the segmented list
let firstSegmentToken = segmentedTokens.shift();
// Potentially refine that single token
const refinedArray = this.refineSingleToken(firstSegmentToken);
// Put any leftover from the refined array and from the remainder of segmentedTokens into pending
this.pendingTokens.unshift(...segmentedTokens);
// If the refined array has more than 1 token, we also queue them (except the first).
if (refinedArray.length > 1) {
this.pendingTokens.unshift(...refinedArray.slice(1));
}
// The final token to return is the first one from refineSingleToken()
const finalToken = refinedArray[0];
this.previousTokenType = finalToken.type;
return finalToken;
}
// 3) If we got here, it's an unknown character
const unknownChar = this.nextChar();
const unknownToken = this.createToken(TokenType.Unknown, unknownChar);
this.previousTokenType = unknownToken.type;
return unknownToken;
}
/**
* The main public method: tokenize the entire input and return the array of tokens.
*/
tokenize() {
// Clean input first
this.input = this.preprocessInput();
const tokens = [];
let token = this.readNextToken();
while (token.type !== TokenType.EOF) {
tokens.push(token);
token = this.readNextToken();
}
// Push the EOF token as well
tokens.push(token);
return tokens;
}
}
module.exports = { Tokenizer, TokenType };