@xnoxs/flux-lang 3.5.3 → 4.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +31 -0
- package/README.md +3 -1
- package/dist/flux-cli.js +171 -11
- package/dist/flux.cjs.js +64 -1
- package/dist/flux.esm.js +64 -1
- package/dist/flux.min.js +20 -20
- package/package.json +1 -1
- package/src/self/codegen.flux +5 -2
- package/src/self/codegen.js +1 -794
- package/src/self/lexer.flux +1 -1
- package/src/self/lexer.js +1 -713
- package/src/self/lexer.stage2.js +700 -0
- package/src/self/parser.flux +41 -0
- package/src/self/parser.js +1 -1571
- package/src/self/pkg.flux +76 -0
- package/src/self/pkg.js +82 -0
- package/src/self/transpiler.flux +14 -4
- package/src/self/transpiler.js +1 -83
- package/src/self/type-checker.flux +12 -0
- package/src/self/type-checker.js +1 -1114
package/src/self/lexer.js
CHANGED
|
@@ -1,713 +1 @@
|
|
|
1
|
-
// ── Flux stdlib ──
|
|
2
|
-
|
|
3
|
-
function map(arr, fn) { return arr.map(fn); }
|
|
4
|
-
|
|
5
|
-
function some(arr, fn) { return arr.some(fn); }
|
|
6
|
-
|
|
7
|
-
function join(arr, sep) { return arr.join(sep != null ? sep : ','); }
|
|
8
|
-
|
|
9
|
-
function trim(s) { return String(s).trim(); }
|
|
10
|
-
|
|
11
|
-
function trimEnd(s) { return String(s).trimEnd(); }
|
|
12
|
-
// ── end stdlib ──
|
|
13
|
-
|
|
14
|
-
// Generated by Flux Transpiler v3.2.0
|
|
15
|
-
"use strict";
|
|
16
|
-
|
|
17
|
-
const T = { NUMBER: "NUMBER", STRING: "STRING", BOOL: "BOOL", NULL: "NULL", IDENT: "IDENT", VAR: "VAR", VAL: "VAL", FN: "FN", RETURN: "RETURN", IF: "IF", ELSE: "ELSE", FOR: "FOR", IN: "IN", WHILE: "WHILE", BREAK: "BREAK", CONTINUE: "CONTINUE", DO: "DO", CLASS: "CLASS", EXTENDS: "EXTENDS", SELF: "SELF", NEW: "NEW", INTERFACE: "INTERFACE", IMPLEMENTS: "IMPLEMENTS", PRIVATE: "PRIVATE", PUBLIC: "PUBLIC", PROTECTED: "PROTECTED", READONLY: "READONLY", STATIC: "STATIC", ABSTRACT: "ABSTRACT", OVERRIDE: "OVERRIDE", MATCH: "MATCH", WHEN: "WHEN", IMPORT: "IMPORT", EXPORT: "EXPORT", FROM: "FROM", AS: "AS", DEFAULT: "DEFAULT", AND: "AND", OR: "OR", NOT: "NOT", ASYNC: "ASYNC", AWAIT: "AWAIT", TRY: "TRY", CATCH: "CATCH", FINALLY: "FINALLY", THROW: "THROW", TYPEOF: "TYPEOF", INSTANCEOF: "INSTANCEOF", TYPE: "TYPE", ENUM: "ENUM", SATISFIES: "SATISFIES", IS: "IS", CONST: "CONST", PLUS: "PLUS", MINUS: "MINUS", STAR: "STAR", SLASH: "SLASH", PERCENT: "PERCENT", REGEX: "REGEX", STARSTAR: "STARSTAR", EQ: "EQ", EQEQ: "EQEQ", NEQ: "NEQ", EQEQEQ: "EQEQEQ", NEQEQ: "NEQEQ", LT: "LT", LTE: "LTE", GT: "GT", GTE: "GTE", PLUSEQ: "PLUSEQ", MINUSEQ: "MINUSEQ", STAREQ: "STAREQ", SLASHEQ: "SLASHEQ", PERCENTEQ: "PERCENTEQ", PLUSPLUS: "PLUSPLUS", MINUSMINUS: "MINUSMINUS", AMPERSAND: "AMPERSAND", ANDAND: "ANDAND", PIPEB: "PIPEB", OROR: "OROR", CARET: "CARET", TILDE: "TILDE", LSHIFT: "LSHIFT", RSHIFT: "RSHIFT", ARROW: "ARROW", FATARROW: "FATARROW", PIPE: "PIPE", DOTDOT: "DOTDOT", DOTDOTDOT: "DOTDOTDOT", WILDCARD: "WILDCARD", NULLISH: "NULLISH", QUESTIONDOT: "QUESTIONDOT", BANG: "BANG", AT: "AT", LPAREN: "LPAREN", RPAREN: "RPAREN", LBRACKET: "LBRACKET", RBRACKET: "RBRACKET", LBRACE: "LBRACE", RBRACE: "RBRACE", COMMA: "COMMA", DOT: "DOT", COLON: "COLON", QUESTION: "QUESTION", NEWLINE: "NEWLINE", INDENT: "INDENT", DEDENT: "DEDENT", EOF: "EOF" };
|
|
18
|
-
module.exports.T = T;
|
|
19
|
-
const TokenType = T;
|
|
20
|
-
module.exports.TokenType = TokenType;
|
|
21
|
-
const KEYWORDS = { var: "VAR", val: "VAL", fn: "FN", return: "RETURN", if: "IF", else: "ELSE", for: "FOR", in: "IN", while: "WHILE", break: "BREAK", continue: "CONTINUE", do: "DO", class: "CLASS", extends: "EXTENDS", self: "SELF", new: "NEW", interface: "INTERFACE", implements: "IMPLEMENTS", private: "PRIVATE", public: "PUBLIC", protected: "PROTECTED", readonly: "READONLY", static: "STATIC", abstract: "ABSTRACT", override: "OVERRIDE", match: "MATCH", when: "WHEN", import: "IMPORT", export: "EXPORT", from: "FROM", as: "AS", default: "DEFAULT", and: "AND", or: "OR", not: "NOT", async: "ASYNC", await: "AWAIT", try: "TRY", catch: "CATCH", finally: "FINALLY", throw: "THROW", typeof: "TYPEOF", instanceof: "INSTANCEOF", type: "TYPE", enum: "ENUM", satisfies: "SATISFIES", is: "IS", const: "CONST", true: "__TRUE__", false: "__FALSE__", null: "__NULL__" };
|
|
22
|
-
class Lexer {
|
|
23
|
-
constructor(src, pos, line, col, tokens, indentStack, nestDepth) {
|
|
24
|
-
this.src = src;
|
|
25
|
-
this.pos = pos;
|
|
26
|
-
this.line = line;
|
|
27
|
-
this.col = col;
|
|
28
|
-
this.tokens = tokens;
|
|
29
|
-
this.indentStack = indentStack;
|
|
30
|
-
this.nestDepth = nestDepth;
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
err(msg) {
|
|
34
|
-
throw new Error(`[Lexer ${this.line}:${this.col}] ${msg}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
ch(n = 0) {
|
|
38
|
-
return (this.src[(this.pos + n)] ?? "");
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
adv() {
|
|
42
|
-
const c = this.src[this.pos];
|
|
43
|
-
this.pos = (this.pos + 1);
|
|
44
|
-
if ((c == "\n")) {
|
|
45
|
-
this.line = (this.line + 1);
|
|
46
|
-
this.col = 1;
|
|
47
|
-
}
|
|
48
|
-
else {
|
|
49
|
-
this.col = (this.col + 1);
|
|
50
|
-
}
|
|
51
|
-
return c;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
tok(typ, value, l, c) {
|
|
55
|
-
const v = ((value != undefined) ? value : typ);
|
|
56
|
-
const ln = ((l != undefined) ? l : this.line);
|
|
57
|
-
const co = ((c != undefined) ? c : this.col);
|
|
58
|
-
this.tokens.push({ type: typ, value: v, line: ln, col: co });
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
applyIndent(indent) {
|
|
62
|
-
if ((this.nestDepth > 0)) {
|
|
63
|
-
return;
|
|
64
|
-
}
|
|
65
|
-
const top = this.indentStack[(this.indentStack.length - 1)];
|
|
66
|
-
if ((indent > top)) {
|
|
67
|
-
this.indentStack.push(indent);
|
|
68
|
-
this.tok(T.INDENT, indent, this.line, 1);
|
|
69
|
-
}
|
|
70
|
-
else if ((indent < top)) {
|
|
71
|
-
while (((this.indentStack.length > 1) && (this.indentStack[(this.indentStack.length - 1)] > indent))) {
|
|
72
|
-
this.indentStack.pop();
|
|
73
|
-
this.tok(T.DEDENT, null, this.line, 1);
|
|
74
|
-
}
|
|
75
|
-
if ((this.indentStack[(this.indentStack.length - 1)] != indent)) {
|
|
76
|
-
this.err(`Inconsistent indentation (${indent} spaces)`);
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
scanBacktick(l, c) {
|
|
82
|
-
this.adv();
|
|
83
|
-
let s = "";
|
|
84
|
-
while (((this.pos < this.src.length) && (this.ch() != "`"))) {
|
|
85
|
-
if ((this.ch() == "\\")) {
|
|
86
|
-
this.adv();
|
|
87
|
-
const e = this.adv();
|
|
88
|
-
if ((e == "n")) {
|
|
89
|
-
s += "\n";
|
|
90
|
-
}
|
|
91
|
-
else if ((e == "t")) {
|
|
92
|
-
s += "\t";
|
|
93
|
-
}
|
|
94
|
-
else if ((e == "`")) {
|
|
95
|
-
s += "`";
|
|
96
|
-
}
|
|
97
|
-
else if ((e == "\\")) {
|
|
98
|
-
s += "\\";
|
|
99
|
-
}
|
|
100
|
-
else {
|
|
101
|
-
s += ("\\" + e);
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
else {
|
|
105
|
-
s += this.adv();
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
|
-
if ((this.ch() != "`")) {
|
|
109
|
-
this.err("Unterminated backtick string");
|
|
110
|
-
}
|
|
111
|
-
this.adv();
|
|
112
|
-
const rawLines = s.split("\n");
|
|
113
|
-
const trimmed = rawLines.map((ln) => ln.trimEnd());
|
|
114
|
-
while ((trimmed.length && !trimmed[0].trim())) {
|
|
115
|
-
trimmed.shift();
|
|
116
|
-
}
|
|
117
|
-
while ((trimmed.length && !trimmed[(trimmed.length - 1)].trim())) {
|
|
118
|
-
trimmed.pop();
|
|
119
|
-
}
|
|
120
|
-
let minIndent = 999999;
|
|
121
|
-
for (const ln of trimmed) {
|
|
122
|
-
if (ln.trim()) {
|
|
123
|
-
const m = ln.match(/^(\s*)/);
|
|
124
|
-
const ind = m[1].length;
|
|
125
|
-
if ((ind < minIndent)) {
|
|
126
|
-
minIndent = ind;
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
if ((minIndent == 999999)) {
|
|
131
|
-
minIndent = 0;
|
|
132
|
-
}
|
|
133
|
-
const dedented = trimmed.map((ln) => ln.slice(minIndent));
|
|
134
|
-
this.tok(T.STRING, dedented.join("\n"), l, c);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
scanStr(l, c) {
|
|
138
|
-
this.adv();
|
|
139
|
-
const parts = [];
|
|
140
|
-
let text = "";
|
|
141
|
-
while (((this.pos < this.src.length) && (this.ch() != "\""))) {
|
|
142
|
-
if ((this.ch() == "\\")) {
|
|
143
|
-
this.adv();
|
|
144
|
-
const e = this.adv();
|
|
145
|
-
if ((e == "n")) {
|
|
146
|
-
text += "\n";
|
|
147
|
-
}
|
|
148
|
-
else if ((e == "t")) {
|
|
149
|
-
text += "\t";
|
|
150
|
-
}
|
|
151
|
-
else if ((e == "\"")) {
|
|
152
|
-
text += "\"";
|
|
153
|
-
}
|
|
154
|
-
else if ((e == "'")) {
|
|
155
|
-
text += "'";
|
|
156
|
-
}
|
|
157
|
-
else if ((e == "\\")) {
|
|
158
|
-
text += "\\";
|
|
159
|
-
}
|
|
160
|
-
else if ((e == "{")) {
|
|
161
|
-
text += "{";
|
|
162
|
-
}
|
|
163
|
-
else if ((e == "}")) {
|
|
164
|
-
text += "}";
|
|
165
|
-
}
|
|
166
|
-
else {
|
|
167
|
-
text += ("\\" + e);
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
else if ((this.ch() == "{")) {
|
|
171
|
-
parts.push({ type: "text", value: text });
|
|
172
|
-
text = "";
|
|
173
|
-
this.adv();
|
|
174
|
-
let depth = 1;
|
|
175
|
-
let expr = "";
|
|
176
|
-
while ((this.pos < this.src.length)) {
|
|
177
|
-
const cc = this.ch();
|
|
178
|
-
if ((cc == "{")) {
|
|
179
|
-
depth = (depth + 1);
|
|
180
|
-
}
|
|
181
|
-
if ((cc == "}")) {
|
|
182
|
-
depth = (depth - 1);
|
|
183
|
-
if ((depth == 0)) {
|
|
184
|
-
break;
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
if (((cc == "\\") && ((this.src[(this.pos + 1)] == "\"") || (this.src[(this.pos + 1)] == "'")))) {
|
|
188
|
-
this.adv();
|
|
189
|
-
expr += this.adv();
|
|
190
|
-
}
|
|
191
|
-
else {
|
|
192
|
-
expr += this.adv();
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
if ((this.ch() != "}")) {
|
|
196
|
-
this.err("Unclosed string interpolation");
|
|
197
|
-
}
|
|
198
|
-
this.adv();
|
|
199
|
-
parts.push({ type: "expr", value: expr.trim() });
|
|
200
|
-
}
|
|
201
|
-
else {
|
|
202
|
-
text += this.adv();
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
if ((this.ch() != "\"")) {
|
|
206
|
-
this.err("Unterminated string");
|
|
207
|
-
}
|
|
208
|
-
this.adv();
|
|
209
|
-
parts.push({ type: "text", value: text });
|
|
210
|
-
if (parts.some((p) => (p.type == "expr"))) {
|
|
211
|
-
this.tok(T.STRING, { template: true, parts }, l, c);
|
|
212
|
-
}
|
|
213
|
-
else {
|
|
214
|
-
this.tok(T.STRING, parts.map((p) => p.value).join(""), l, c);
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
|
|
218
|
-
scanRegexBody(l, c) {
|
|
219
|
-
let pattern = "";
|
|
220
|
-
let inClass = false;
|
|
221
|
-
while ((this.pos < this.src.length)) {
|
|
222
|
-
const ch = this.ch();
|
|
223
|
-
if ((ch == "\n")) {
|
|
224
|
-
this.err("Unterminated regex literal");
|
|
225
|
-
}
|
|
226
|
-
if ((ch == "\\")) {
|
|
227
|
-
pattern += this.adv();
|
|
228
|
-
if ((this.pos < this.src.length)) {
|
|
229
|
-
pattern += this.adv();
|
|
230
|
-
}
|
|
231
|
-
continue;
|
|
232
|
-
}
|
|
233
|
-
if ((ch == "[")) {
|
|
234
|
-
inClass = true;
|
|
235
|
-
pattern += this.adv();
|
|
236
|
-
continue;
|
|
237
|
-
}
|
|
238
|
-
if ((ch == "]")) {
|
|
239
|
-
inClass = false;
|
|
240
|
-
pattern += this.adv();
|
|
241
|
-
continue;
|
|
242
|
-
}
|
|
243
|
-
if (((ch == "/") && !inClass)) {
|
|
244
|
-
break;
|
|
245
|
-
}
|
|
246
|
-
pattern += this.adv();
|
|
247
|
-
}
|
|
248
|
-
if ((this.ch() != "/")) {
|
|
249
|
-
this.err("Unterminated regex literal");
|
|
250
|
-
}
|
|
251
|
-
this.adv();
|
|
252
|
-
let flags = "";
|
|
253
|
-
while (/[gimsuy]/.test(this.ch())) {
|
|
254
|
-
flags += this.adv();
|
|
255
|
-
}
|
|
256
|
-
this.tok(T.REGEX, { pattern, flags }, l, c);
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
scanBlockComment() {
|
|
260
|
-
this.adv();
|
|
261
|
-
this.adv();
|
|
262
|
-
while ((this.pos < this.src.length)) {
|
|
263
|
-
if (((this.ch() == "*") && (this.ch(1) == "/"))) {
|
|
264
|
-
this.adv();
|
|
265
|
-
this.adv();
|
|
266
|
-
return;
|
|
267
|
-
}
|
|
268
|
-
this.adv();
|
|
269
|
-
}
|
|
270
|
-
this.err("Unterminated block comment");
|
|
271
|
-
}
|
|
272
|
-
|
|
273
|
-
tokenize() {
|
|
274
|
-
let bol = true;
|
|
275
|
-
while ((this.pos < this.src.length)) {
|
|
276
|
-
if (bol) {
|
|
277
|
-
bol = false;
|
|
278
|
-
let indent = 0;
|
|
279
|
-
while (((this.ch() == " ") || (this.ch() == "\t"))) {
|
|
280
|
-
if ((this.ch() == "\t")) {
|
|
281
|
-
indent = (indent + 4);
|
|
282
|
-
}
|
|
283
|
-
else {
|
|
284
|
-
indent = (indent + 1);
|
|
285
|
-
}
|
|
286
|
-
this.adv();
|
|
287
|
-
}
|
|
288
|
-
if (((this.ch() == "\n") || !this.ch())) {
|
|
289
|
-
if ((this.ch() == "\n")) {
|
|
290
|
-
this.adv();
|
|
291
|
-
bol = true;
|
|
292
|
-
}
|
|
293
|
-
continue;
|
|
294
|
-
}
|
|
295
|
-
if (((this.ch() == "/") && (this.ch(1) == "/"))) {
|
|
296
|
-
while (((this.pos < this.src.length) && (this.ch() != "\n"))) {
|
|
297
|
-
this.adv();
|
|
298
|
-
}
|
|
299
|
-
if ((this.ch() == "\n")) {
|
|
300
|
-
this.adv();
|
|
301
|
-
bol = true;
|
|
302
|
-
}
|
|
303
|
-
continue;
|
|
304
|
-
}
|
|
305
|
-
if (((this.ch() == "/") && (this.ch(1) == "*"))) {
|
|
306
|
-
this.scanBlockComment();
|
|
307
|
-
continue;
|
|
308
|
-
}
|
|
309
|
-
const isContinuation = ((((this.ch() == "|") && (this.ch(1) == ">")) || (this.ch() == ".")) || ((this.ch() == "?") && (this.ch(1) == ".")));
|
|
310
|
-
if (isContinuation) {
|
|
311
|
-
const lastTok = this.tokens[(this.tokens.length - 1)];
|
|
312
|
-
if ((lastTok && (lastTok.type == T.NEWLINE))) {
|
|
313
|
-
this.tokens.pop();
|
|
314
|
-
}
|
|
315
|
-
}
|
|
316
|
-
else {
|
|
317
|
-
this.applyIndent(indent);
|
|
318
|
-
}
|
|
319
|
-
}
|
|
320
|
-
const l = this.line;
|
|
321
|
-
const c = this.col;
|
|
322
|
-
const cur = this.ch();
|
|
323
|
-
if ((cur == "\n")) {
|
|
324
|
-
this.adv();
|
|
325
|
-
bol = true;
|
|
326
|
-
if ((this.nestDepth == 0)) {
|
|
327
|
-
const lastTok = this.tokens[(this.tokens.length - 1)];
|
|
328
|
-
if ((((lastTok && (lastTok.type != T.NEWLINE)) && (lastTok.type != T.INDENT)) && (lastTok.type != T.DEDENT))) {
|
|
329
|
-
this.tok(T.NEWLINE, null, l, c);
|
|
330
|
-
}
|
|
331
|
-
}
|
|
332
|
-
continue;
|
|
333
|
-
}
|
|
334
|
-
if (((cur == " ") || (cur == "\t"))) {
|
|
335
|
-
this.adv();
|
|
336
|
-
continue;
|
|
337
|
-
}
|
|
338
|
-
if (((cur == "/") && (this.ch(1) == "/"))) {
|
|
339
|
-
while (((this.pos < this.src.length) && (this.ch() != "\n"))) {
|
|
340
|
-
this.adv();
|
|
341
|
-
}
|
|
342
|
-
continue;
|
|
343
|
-
}
|
|
344
|
-
if (((cur == "/") && (this.ch(1) == "*"))) {
|
|
345
|
-
this.scanBlockComment();
|
|
346
|
-
continue;
|
|
347
|
-
}
|
|
348
|
-
if (((cur >= "0") && (cur <= "9"))) {
|
|
349
|
-
if (((cur == "0") && ((this.ch(1) == "x") || (this.ch(1) == "X")))) {
|
|
350
|
-
this.adv();
|
|
351
|
-
this.adv();
|
|
352
|
-
let h = "";
|
|
353
|
-
while (/[0-9a-fA-F_]/.test(this.ch())) {
|
|
354
|
-
const hc = this.adv();
|
|
355
|
-
if ((hc != "_")) {
|
|
356
|
-
h += hc;
|
|
357
|
-
}
|
|
358
|
-
}
|
|
359
|
-
this.tok(T.NUMBER, parseInt(((h.length > 0) ? h : "0"), 16), l, c);
|
|
360
|
-
continue;
|
|
361
|
-
}
|
|
362
|
-
if (((cur == "0") && ((this.ch(1) == "b") || (this.ch(1) == "B")))) {
|
|
363
|
-
this.adv();
|
|
364
|
-
this.adv();
|
|
365
|
-
let b = "";
|
|
366
|
-
while (/[01_]/.test(this.ch())) {
|
|
367
|
-
const bc = this.adv();
|
|
368
|
-
if ((bc != "_")) {
|
|
369
|
-
b += bc;
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
this.tok(T.NUMBER, parseInt(((b.length > 0) ? b : "0"), 2), l, c);
|
|
373
|
-
continue;
|
|
374
|
-
}
|
|
375
|
-
let numStr = "";
|
|
376
|
-
while ((this.pos < this.src.length)) {
|
|
377
|
-
if (((this.ch() == ".") && (this.ch(1) == "."))) {
|
|
378
|
-
break;
|
|
379
|
-
}
|
|
380
|
-
if ((((this.ch() >= "0") && (this.ch() <= "9")) || (this.ch() == "."))) {
|
|
381
|
-
numStr += this.adv();
|
|
382
|
-
}
|
|
383
|
-
else if ((this.ch() == "_")) {
|
|
384
|
-
this.adv();
|
|
385
|
-
}
|
|
386
|
-
else if ((((this.ch() == "e") || (this.ch() == "E")) && (numStr.length > 0))) {
|
|
387
|
-
numStr += this.adv();
|
|
388
|
-
if (((this.ch() == "+") || (this.ch() == "-"))) {
|
|
389
|
-
numStr += this.adv();
|
|
390
|
-
}
|
|
391
|
-
while (((this.ch() >= "0") && (this.ch() <= "9"))) {
|
|
392
|
-
numStr += this.adv();
|
|
393
|
-
}
|
|
394
|
-
break;
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
break;
|
|
398
|
-
}
|
|
399
|
-
}
|
|
400
|
-
this.tok(T.NUMBER, parseFloat(numStr), l, c);
|
|
401
|
-
continue;
|
|
402
|
-
}
|
|
403
|
-
if ((cur == "\"")) {
|
|
404
|
-
this.scanStr(l, c);
|
|
405
|
-
continue;
|
|
406
|
-
}
|
|
407
|
-
if ((cur == "`")) {
|
|
408
|
-
this.scanBacktick(l, c);
|
|
409
|
-
continue;
|
|
410
|
-
}
|
|
411
|
-
if ((cur == "'")) {
|
|
412
|
-
this.adv();
|
|
413
|
-
let sq = "";
|
|
414
|
-
while (((this.pos < this.src.length) && (this.ch() != "'"))) {
|
|
415
|
-
if ((this.ch() == "\\")) {
|
|
416
|
-
this.adv();
|
|
417
|
-
const e = this.adv();
|
|
418
|
-
if ((e == "n")) {
|
|
419
|
-
sq += "\n";
|
|
420
|
-
}
|
|
421
|
-
else if ((e == "t")) {
|
|
422
|
-
sq += "\t";
|
|
423
|
-
}
|
|
424
|
-
else if ((e == "r")) {
|
|
425
|
-
sq += "\r";
|
|
426
|
-
}
|
|
427
|
-
else if ((e == "'")) {
|
|
428
|
-
sq += "'";
|
|
429
|
-
}
|
|
430
|
-
else if ((e == "\\")) {
|
|
431
|
-
sq += "\\";
|
|
432
|
-
}
|
|
433
|
-
else {
|
|
434
|
-
sq += ("\\" + e);
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
else {
|
|
438
|
-
sq += this.adv();
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
if ((this.ch() != "'")) {
|
|
442
|
-
this.err("Unterminated string");
|
|
443
|
-
}
|
|
444
|
-
this.adv();
|
|
445
|
-
this.tok(T.STRING, sq, l, c);
|
|
446
|
-
continue;
|
|
447
|
-
}
|
|
448
|
-
if (((((cur >= "a") && (cur <= "z")) || ((cur >= "A") && (cur <= "Z"))) || (cur == "_"))) {
|
|
449
|
-
let word = "";
|
|
450
|
-
while (/[a-zA-Z0-9_]/.test(this.ch())) {
|
|
451
|
-
word += this.adv();
|
|
452
|
-
}
|
|
453
|
-
if (((word == "_") && !/[a-zA-Z0-9_]/.test(this.ch()))) {
|
|
454
|
-
this.tok(T.WILDCARD, "_", l, c);
|
|
455
|
-
continue;
|
|
456
|
-
}
|
|
457
|
-
const kw = KEYWORDS[word];
|
|
458
|
-
if ((kw == "__TRUE__")) {
|
|
459
|
-
this.tok(T.BOOL, true, l, c);
|
|
460
|
-
}
|
|
461
|
-
else if ((kw == "__FALSE__")) {
|
|
462
|
-
this.tok(T.BOOL, false, l, c);
|
|
463
|
-
}
|
|
464
|
-
else if ((kw == "__NULL__")) {
|
|
465
|
-
this.tok(T.NULL, null, l, c);
|
|
466
|
-
}
|
|
467
|
-
else if ((kw != undefined)) {
|
|
468
|
-
this.tok(kw, word, l, c);
|
|
469
|
-
}
|
|
470
|
-
else {
|
|
471
|
-
this.tok(T.IDENT, word, l, c);
|
|
472
|
-
}
|
|
473
|
-
continue;
|
|
474
|
-
}
|
|
475
|
-
this.adv();
|
|
476
|
-
if ((cur == "+")) {
|
|
477
|
-
if ((this.ch() == "+")) {
|
|
478
|
-
this.adv();
|
|
479
|
-
this.tok(T.PLUSPLUS, "++", l, c);
|
|
480
|
-
}
|
|
481
|
-
else if ((this.ch() == "=")) {
|
|
482
|
-
this.adv();
|
|
483
|
-
this.tok(T.PLUSEQ, "+=", l, c);
|
|
484
|
-
}
|
|
485
|
-
else {
|
|
486
|
-
this.tok(T.PLUS, "+", l, c);
|
|
487
|
-
}
|
|
488
|
-
}
|
|
489
|
-
else if ((cur == "-")) {
|
|
490
|
-
if ((this.ch() == "-")) {
|
|
491
|
-
this.adv();
|
|
492
|
-
this.tok(T.MINUSMINUS, "--", l, c);
|
|
493
|
-
}
|
|
494
|
-
else if ((this.ch() == ">")) {
|
|
495
|
-
this.adv();
|
|
496
|
-
this.tok(T.ARROW, "->", l, c);
|
|
497
|
-
}
|
|
498
|
-
else if ((this.ch() == "=")) {
|
|
499
|
-
this.adv();
|
|
500
|
-
this.tok(T.MINUSEQ, "-=", l, c);
|
|
501
|
-
}
|
|
502
|
-
else {
|
|
503
|
-
this.tok(T.MINUS, "-", l, c);
|
|
504
|
-
}
|
|
505
|
-
}
|
|
506
|
-
else if ((cur == "*")) {
|
|
507
|
-
if ((this.ch() == "*")) {
|
|
508
|
-
this.adv();
|
|
509
|
-
this.tok(T.STARSTAR, "**", l, c);
|
|
510
|
-
}
|
|
511
|
-
else if ((this.ch() == "=")) {
|
|
512
|
-
this.adv();
|
|
513
|
-
this.tok(T.STAREQ, "*=", l, c);
|
|
514
|
-
}
|
|
515
|
-
else {
|
|
516
|
-
this.tok(T.STAR, "*", l, c);
|
|
517
|
-
}
|
|
518
|
-
}
|
|
519
|
-
else if ((cur == "/")) {
|
|
520
|
-
if ((this.ch() == "=")) {
|
|
521
|
-
this.adv();
|
|
522
|
-
this.tok(T.SLASHEQ, "/=", l, c);
|
|
523
|
-
}
|
|
524
|
-
else {
|
|
525
|
-
const lastTok = this.tokens[(this.tokens.length - 1)];
|
|
526
|
-
const afterVal = ((lastTok != null) && (((((((((((lastTok.type == T.IDENT) || (lastTok.type == T.NUMBER)) || (lastTok.type == T.STRING)) || (lastTok.type == T.BOOL)) || (lastTok.type == T.NULL)) || (lastTok.type == T.REGEX)) || (lastTok.type == T.RPAREN)) || (lastTok.type == T.RBRACKET)) || (lastTok.type == T.PLUSPLUS)) || (lastTok.type == T.MINUSMINUS)) || (lastTok.type == T.BANG)));
|
|
527
|
-
if (afterVal) {
|
|
528
|
-
this.tok(T.SLASH, "/", l, c);
|
|
529
|
-
}
|
|
530
|
-
else {
|
|
531
|
-
this.scanRegexBody(l, c);
|
|
532
|
-
}
|
|
533
|
-
}
|
|
534
|
-
}
|
|
535
|
-
else if ((cur == "%")) {
|
|
536
|
-
if ((this.ch() == "=")) {
|
|
537
|
-
this.adv();
|
|
538
|
-
this.tok(T.PERCENTEQ, "%=", l, c);
|
|
539
|
-
}
|
|
540
|
-
else {
|
|
541
|
-
this.tok(T.PERCENT, "%", l, c);
|
|
542
|
-
}
|
|
543
|
-
}
|
|
544
|
-
else if ((cur == "=")) {
|
|
545
|
-
if (((this.ch() == "=") && (this.src[(this.pos + 1)] == "="))) {
|
|
546
|
-
this.adv();
|
|
547
|
-
this.adv();
|
|
548
|
-
this.tok(T.EQEQEQ, "===", l, c);
|
|
549
|
-
}
|
|
550
|
-
else if ((this.ch() == "=")) {
|
|
551
|
-
this.adv();
|
|
552
|
-
this.tok(T.EQEQ, "==", l, c);
|
|
553
|
-
}
|
|
554
|
-
else if ((this.ch() == ">")) {
|
|
555
|
-
this.adv();
|
|
556
|
-
this.tok(T.FATARROW, "=>", l, c);
|
|
557
|
-
}
|
|
558
|
-
else {
|
|
559
|
-
this.tok(T.EQ, "=", l, c);
|
|
560
|
-
}
|
|
561
|
-
}
|
|
562
|
-
else if ((cur == "!")) {
|
|
563
|
-
if (((this.ch() == "=") && (this.src[(this.pos + 1)] == "="))) {
|
|
564
|
-
this.adv();
|
|
565
|
-
this.adv();
|
|
566
|
-
this.tok(T.NEQEQ, "!==", l, c);
|
|
567
|
-
}
|
|
568
|
-
else if ((this.ch() == "=")) {
|
|
569
|
-
this.adv();
|
|
570
|
-
this.tok(T.NEQ, "!=", l, c);
|
|
571
|
-
}
|
|
572
|
-
else {
|
|
573
|
-
this.tok(T.BANG, "!", l, c);
|
|
574
|
-
}
|
|
575
|
-
}
|
|
576
|
-
else if ((cur == "<")) {
|
|
577
|
-
if ((this.ch() == "<")) {
|
|
578
|
-
this.adv();
|
|
579
|
-
this.tok(T.LSHIFT, "<<", l, c);
|
|
580
|
-
}
|
|
581
|
-
else if ((this.ch() == "=")) {
|
|
582
|
-
this.adv();
|
|
583
|
-
this.tok(T.LTE, "<=", l, c);
|
|
584
|
-
}
|
|
585
|
-
else {
|
|
586
|
-
this.tok(T.LT, "<", l, c);
|
|
587
|
-
}
|
|
588
|
-
}
|
|
589
|
-
else if ((cur == ">")) {
|
|
590
|
-
if ((this.ch() == ">")) {
|
|
591
|
-
this.adv();
|
|
592
|
-
this.tok(T.RSHIFT, ">>", l, c);
|
|
593
|
-
}
|
|
594
|
-
else if ((this.ch() == "=")) {
|
|
595
|
-
this.adv();
|
|
596
|
-
this.tok(T.GTE, ">=", l, c);
|
|
597
|
-
}
|
|
598
|
-
else {
|
|
599
|
-
this.tok(T.GT, ">", l, c);
|
|
600
|
-
}
|
|
601
|
-
}
|
|
602
|
-
else if ((cur == ".")) {
|
|
603
|
-
if (((this.ch() == ".") && (this.src[(this.pos + 1)] == "."))) {
|
|
604
|
-
this.adv();
|
|
605
|
-
this.adv();
|
|
606
|
-
this.tok(T.DOTDOTDOT, "...", l, c);
|
|
607
|
-
}
|
|
608
|
-
else if ((this.ch() == ".")) {
|
|
609
|
-
this.adv();
|
|
610
|
-
this.tok(T.DOTDOT, "..", l, c);
|
|
611
|
-
}
|
|
612
|
-
else {
|
|
613
|
-
this.tok(T.DOT, ".", l, c);
|
|
614
|
-
}
|
|
615
|
-
}
|
|
616
|
-
else if ((cur == "?")) {
|
|
617
|
-
if ((this.ch() == ".")) {
|
|
618
|
-
this.adv();
|
|
619
|
-
this.tok(T.QUESTIONDOT, "?.", l, c);
|
|
620
|
-
}
|
|
621
|
-
else if ((this.ch() == "?")) {
|
|
622
|
-
this.adv();
|
|
623
|
-
this.tok(T.NULLISH, "??", l, c);
|
|
624
|
-
}
|
|
625
|
-
else {
|
|
626
|
-
this.tok(T.QUESTION, "?", l, c);
|
|
627
|
-
}
|
|
628
|
-
}
|
|
629
|
-
else if ((cur == "|")) {
|
|
630
|
-
if ((this.ch() == ">")) {
|
|
631
|
-
this.adv();
|
|
632
|
-
this.tok(T.PIPE, "|>", l, c);
|
|
633
|
-
}
|
|
634
|
-
else if ((this.ch() == "|")) {
|
|
635
|
-
this.adv();
|
|
636
|
-
this.tok(T.OROR, "||", l, c);
|
|
637
|
-
}
|
|
638
|
-
else {
|
|
639
|
-
this.tok(T.PIPEB, "|", l, c);
|
|
640
|
-
}
|
|
641
|
-
}
|
|
642
|
-
else if ((cur == "&")) {
|
|
643
|
-
if ((this.ch() == "&")) {
|
|
644
|
-
this.adv();
|
|
645
|
-
this.tok(T.ANDAND, "&&", l, c);
|
|
646
|
-
}
|
|
647
|
-
else {
|
|
648
|
-
this.tok(T.AMPERSAND, "&", l, c);
|
|
649
|
-
}
|
|
650
|
-
}
|
|
651
|
-
else if ((cur == "^")) {
|
|
652
|
-
this.tok(T.CARET, "^", l, c);
|
|
653
|
-
}
|
|
654
|
-
else if ((cur == "~")) {
|
|
655
|
-
this.tok(T.TILDE, "~", l, c);
|
|
656
|
-
}
|
|
657
|
-
else if ((cur == "@")) {
|
|
658
|
-
this.tok(T.AT, "@", l, c);
|
|
659
|
-
}
|
|
660
|
-
else if ((cur == "(")) {
|
|
661
|
-
this.nestDepth = (this.nestDepth + 1);
|
|
662
|
-
this.tok(T.LPAREN, "(", l, c);
|
|
663
|
-
}
|
|
664
|
-
else if ((cur == ")")) {
|
|
665
|
-
this.nestDepth = (this.nestDepth - 1);
|
|
666
|
-
this.tok(T.RPAREN, ")", l, c);
|
|
667
|
-
}
|
|
668
|
-
else if ((cur == "[")) {
|
|
669
|
-
this.nestDepth = (this.nestDepth + 1);
|
|
670
|
-
this.tok(T.LBRACKET, "[", l, c);
|
|
671
|
-
}
|
|
672
|
-
else if ((cur == "]")) {
|
|
673
|
-
this.nestDepth = (this.nestDepth - 1);
|
|
674
|
-
this.tok(T.RBRACKET, "]", l, c);
|
|
675
|
-
}
|
|
676
|
-
else if ((cur == "{")) {
|
|
677
|
-
this.nestDepth = (this.nestDepth + 1);
|
|
678
|
-
this.tok(T.LBRACE, "{", l, c);
|
|
679
|
-
}
|
|
680
|
-
else if ((cur == "}")) {
|
|
681
|
-
this.nestDepth = (this.nestDepth - 1);
|
|
682
|
-
this.tok(T.RBRACE, "}", l, c);
|
|
683
|
-
}
|
|
684
|
-
else if ((cur == ",")) {
|
|
685
|
-
this.tok(T.COMMA, ",", l, c);
|
|
686
|
-
}
|
|
687
|
-
else if ((cur == ":")) {
|
|
688
|
-
this.tok(T.COLON, ":", l, c);
|
|
689
|
-
}
|
|
690
|
-
else if ((cur != ";")) {
|
|
691
|
-
this.err(`Unknown character: '${cur}'`);
|
|
692
|
-
}
|
|
693
|
-
}
|
|
694
|
-
while ((this.indentStack.length > 1)) {
|
|
695
|
-
this.indentStack.pop();
|
|
696
|
-
this.tok(T.DEDENT, null, this.line, 1);
|
|
697
|
-
}
|
|
698
|
-
const lastTok = this.tokens[(this.tokens.length - 1)];
|
|
699
|
-
if (((lastTok && (lastTok.type != T.NEWLINE)) && (lastTok.type != T.DEDENT))) {
|
|
700
|
-
this.tok(T.NEWLINE, null, this.line, this.col);
|
|
701
|
-
}
|
|
702
|
-
this.tok(T.EOF, null, this.line, this.col);
|
|
703
|
-
return this.tokens;
|
|
704
|
-
}
|
|
705
|
-
|
|
706
|
-
}
|
|
707
|
-
|
|
708
|
-
module.exports.Lexer = Lexer;
|
|
709
|
-
function lexerize(source) {
|
|
710
|
-
const processed = source.replace(/\r\n/g, "\n").replace(/\r/g, "\n");
|
|
711
|
-
return new Lexer(processed, 0, 1, 1, [], [0], 0);
|
|
712
|
-
}
|
|
713
|
-
module.exports.lexerize = lexerize;
|
|
1
|
+
function map(arr, fn) { return arr.map(fn); } function some(arr, fn) { return arr.some(fn); } function join(arr, sep) { return arr.join(sep != null ? sep : ','); } function trim(s) { return String(s).trim(); } function trimEnd(s) { return String(s).trimEnd(); } "use strict"; const T = { NUMBER: "NUMBER", STRING: "STRING", BOOL: "BOOL", NULL: "NULL", IDENT: "IDENT", VAR: "VAR", VAL: "VAL", FN: "FN", RETURN: "RETURN", IF: "IF", ELSE: "ELSE", FOR: "FOR", IN: "IN", WHILE: "WHILE", BREAK: "BREAK", CONTINUE: "CONTINUE", DO: "DO", CLASS: "CLASS", EXTENDS: "EXTENDS", SELF: "SELF", NEW: "NEW", INTERFACE: "INTERFACE", IMPLEMENTS: "IMPLEMENTS", PRIVATE: "PRIVATE", PUBLIC: "PUBLIC", PROTECTED: "PROTECTED", READONLY: "READONLY", STATIC: "STATIC", ABSTRACT: "ABSTRACT", OVERRIDE: "OVERRIDE", MATCH: "MATCH", WHEN: "WHEN", IMPORT: "IMPORT", EXPORT: "EXPORT", FROM: "FROM", AS: "AS", DEFAULT: "DEFAULT", AND: "AND", OR: "OR", NOT: "NOT", ASYNC: "ASYNC", AWAIT: "AWAIT", TRY: "TRY", CATCH: "CATCH", FINALLY: "FINALLY", THROW: "THROW", TYPEOF: "TYPEOF", INSTANCEOF: "INSTANCEOF", TYPE: "TYPE", ENUM: "ENUM", SATISFIES: "SATISFIES", IS: "IS", CONST: "CONST", PLUS: "PLUS", MINUS: "MINUS", STAR: "STAR", SLASH: "SLASH", PERCENT: "PERCENT", REGEX: "REGEX", STARSTAR: "STARSTAR", EQ: "EQ", EQEQ: "EQEQ", NEQ: "NEQ", EQEQEQ: "EQEQEQ", NEQEQ: "NEQEQ", LT: "LT", LTE: "LTE", GT: "GT", GTE: "GTE", PLUSEQ: "PLUSEQ", MINUSEQ: "MINUSEQ", STAREQ: "STAREQ", SLASHEQ: "SLASHEQ", PERCENTEQ: "PERCENTEQ", PLUSPLUS: "PLUSPLUS", MINUSMINUS: "MINUSMINUS", AMPERSAND: "AMPERSAND", ANDAND: "ANDAND", PIPEB: "PIPEB", OROR: "OROR", CARET: "CARET", TILDE: "TILDE", LSHIFT: "LSHIFT", RSHIFT: "RSHIFT", ARROW: "ARROW", FATARROW: "FATARROW", PIPE: "PIPE", DOTDOT: "DOTDOT", DOTDOTDOT: "DOTDOTDOT", WILDCARD: "WILDCARD", NULLISH: "NULLISH", QUESTIONDOT: "QUESTIONDOT", BANG: "BANG", AT: "AT", LPAREN: "LPAREN", RPAREN: "RPAREN", LBRACKET: "LBRACKET", RBRACKET: "RBRACKET", LBRACE: "LBRACE", RBRACE: "RBRACE", COMMA: "COMMA", DOT: "DOT", COLON: "COLON", QUESTION: "QUESTION", NEWLINE: "NEWLINE", INDENT: "INDENT", DEDENT: "DEDENT", EOF: "EOF" }; module.exports.T = T; const TokenType = T; module.exports.TokenType = TokenType; const _a = { var: "VAR", val: "VAL", fn: "FN", return: "RETURN", declare: "DECLARE", if: "IF", else: "ELSE", for: "FOR", in: "IN", while: "WHILE", break: "BREAK", continue: "CONTINUE", do: "DO", class: "CLASS", extends: "EXTENDS", self: "SELF", new: "NEW", interface: "INTERFACE", implements: "IMPLEMENTS", private: "PRIVATE", public: "PUBLIC", protected: "PROTECTED", readonly: "READONLY", static: "STATIC", abstract: "ABSTRACT", override: "OVERRIDE", match: "MATCH", when: "WHEN", import: "IMPORT", export: "EXPORT", from: "FROM", as: "AS", default: "DEFAULT", and: "AND", or: "OR", not: "NOT", async: "ASYNC", await: "AWAIT", try: "TRY", catch: "CATCH", finally: "FINALLY", throw: "THROW", typeof: "TYPEOF", instanceof: "INSTANCEOF", type: "TYPE", enum: "ENUM", satisfies: "SATISFIES", is: "IS", const: "CONST", true: "__TRUE__", false: "__FALSE__", null: "__NULL__" }; class Lexer { constructor(src, pos, line, col, tokens, indentStack, nestDepth) { this.src = src; this.pos = pos; this.line = line; this.col = col; this.tokens = tokens; this.indentStack = indentStack; this.nestDepth = nestDepth; } err(_b) { throw new Error(`[Lexer ${this.line}:${this.col}] ${_b}`); } ch(_c = 0) { return (this.src[(this.pos + _c)] ?? ""); } adv() { const c = this.src[this.pos]; this.pos = (this.pos + 1); if ((c == "\n")) { this.line = (this.line + 1); this.col = 1; } else { this.col = (this.col + 1); } return c; } tok(_d, _e, _f, _g) { const v = ((_e != undefined) ? _e : _d); const ln = ((_f != undefined) ? _f : this.line); const co = ((_g != undefined) ? _g : this.col); this.tokens.push({ type: _d, value: v, line: ln, col: co }); } applyIndent(_h) { if ((this.nestDepth > 0)) { return; } const top = this.indentStack[(this.indentStack.length - 1)]; if ((_h > top)) { this.indentStack.push(_h); this.tok(T.INDENT, _h, this.line, 1); } else if ((_h < top)) { while (((this.indentStack.length > 1) && (this.indentStack[(this.indentStack.length - 1)] > _h))) { this.indentStack.pop(); this.tok(T.DEDENT, null, this.line, 1); } if ((this.indentStack[(this.indentStack.length - 1)] != _h)) { this.err(`Inconsistent indentation (${_h} spaces)`); } } } scanBacktick(_f, _g) { this.adv(); let s = ""; while (((this.pos < this.src.length) && (this.ch() != "`"))) { if ((this.ch() == "\\")) { this.adv(); const e = this.adv(); if ((e == "n")) { s += "\n"; } else if ((e == "t")) { s += "\t"; } else if ((e == "`")) { s += "`"; } else if ((e == "\\")) { s += "\\"; } else { s += ("\\" + e); } } else { s += this.adv(); } } if ((this.ch() != "`")) { this.err("Unterminated backtick string"); } this.adv(); const rawLines = s.split("\n"); const trimmed = rawLines.map((_i) => _i.trimEnd()); while ((trimmed.length && !trimmed[0].trim())) { trimmed.shift(); } while ((trimmed.length && !trimmed[(trimmed.length - 1)].trim())) { trimmed.pop(); } let minIndent = 999999; for (const _i of trimmed) { if (_i.trim()) { const m = _i.match(/^(\s*)/); const ind = m[1].length; if ((ind < minIndent)) { minIndent = ind; } } } if ((minIndent == 999999)) { minIndent = 0; } const dedented = trimmed.map((_i) => _i.slice(minIndent)); this.tok(T.STRING, dedented.join("\n"), _f, _g); } scanStr(_f, _g) { this.adv(); const parts = []; let text = ""; while (((this.pos < this.src.length) && (this.ch() != "\""))) { if ((this.ch() == "\\")) { this.adv(); const e = this.adv(); if ((e == "n")) { text += "\n"; } else if ((e == "t")) { text += "\t"; } else if ((e == "\"")) { text += "\""; } else if ((e == "'")) { text += "'"; } else if ((e == "\\")) { text += "\\"; } else if ((e == "{")) { text += "{"; } else if ((e == "}")) { text += "}"; } else { text += ("\\" + e); } } else if ((this.ch() == "{")) { parts.push({ type: "text", value: text }); text = ""; this.adv(); let depth = 1; let expr = ""; while ((this.pos < this.src.length)) { const cc = this.ch(); if ((cc == "{")) { depth = (depth + 1); } if ((cc == "}")) { depth = (depth - 1); if ((depth == 0)) { break; } } if (((cc == "\\") && ((this.src[(this.pos + 1)] == "\"") || (this.src[(this.pos + 1)] == "'")))) { this.adv(); expr += this.adv(); } else { expr += this.adv(); } } if ((this.ch() != "}")) { this.err("Unclosed string interpolation"); } this.adv(); parts.push({ type: "expr", value: expr.trim() }); } else { text += this.adv(); } } if ((this.ch() != "\"")) { this.err("Unterminated string"); } this.adv(); parts.push({ type: "text", value: text }); if (parts.some((_j) => (_j.type == "expr"))) { this.tok(T.STRING, { template: true, parts }, _f, _g); } else { this.tok(T.STRING, parts.map((_j) => _j.value).join(""), _f, _g); } } scanRegexBody(_f, _g) { let pattern = ""; let inClass = false; while ((this.pos < this.src.length)) { const ch = this.ch(); if ((ch == "\n")) { this.err("Unterminated regex literal"); } if ((ch == "\\")) { pattern += this.adv(); if ((this.pos < this.src.length)) { pattern += this.adv(); } continue; } if ((ch == "[")) { inClass = true; pattern += this.adv(); continue; } if ((ch == "]")) { inClass = false; pattern += this.adv(); continue; } if (((ch == "/") && !inClass)) { break; } pattern += this.adv(); } if ((this.ch() != "/")) { this.err("Unterminated regex literal"); } this.adv(); let flags = ""; while (/[gimsuy]/.test(this.ch())) { flags += this.adv(); } this.tok(T.REGEX, { pattern, flags }, _f, _g); } scanBlockComment() { this.adv(); this.adv(); while ((this.pos < this.src.length)) { if (((this.ch() == "*") && (this.ch(1) == "/"))) { this.adv(); this.adv(); return; } this.adv(); } this.err("Unterminated block comment"); } tokenize() { let bol = true; while ((this.pos < this.src.length)) { if (bol) { bol = false; let _h = 0; while (((this.ch() == " ") || (this.ch() == "\t"))) { if ((this.ch() == "\t")) { _h = (_h + 4); } else { _h = (_h + 1); } this.adv(); } if (((this.ch() == "\n") || !this.ch())) { if ((this.ch() == "\n")) { this.adv(); bol = true; } continue; } if (((this.ch() == "/") && (this.ch(1) == "/"))) { while (((this.pos < this.src.length) && (this.ch() != "\n"))) { this.adv(); } if ((this.ch() == "\n")) { this.adv(); bol = true; } continue; } if (((this.ch() == "/") && (this.ch(1) == "*"))) { this.scanBlockComment(); continue; } const isContinuation = ((((this.ch() == "|") && (this.ch(1) == ">")) || (this.ch() == ".")) || ((this.ch() == "?") && (this.ch(1) == "."))); if (isContinuation) { const lastTok = this.tokens[(this.tokens.length - 1)]; if ((lastTok && (lastTok.type == T.NEWLINE))) { this.tokens.pop(); } } else { this.applyIndent(_h); } } const _f = this.line; const _g = this.col; const cur = this.ch(); if ((cur == "\n")) { this.adv(); bol = true; if ((this.nestDepth == 0)) { const lastTok = this.tokens[(this.tokens.length - 1)]; if ((((lastTok && (lastTok.type != T.NEWLINE)) && (lastTok.type != T.INDENT)) && (lastTok.type != T.DEDENT))) { this.tok(T.NEWLINE, null, _f, _g); } } continue; } if (((cur == " ") || (cur == "\t"))) { this.adv(); continue; } if (((cur == "/") && (this.ch(1) == "/"))) { while (((this.pos < this.src.length) && (this.ch() != "\n"))) { this.adv(); } continue; } if (((cur == "/") && (this.ch(1) == "*"))) { this.scanBlockComment(); continue; } if (((cur >= "0") && (cur <= "9"))) { if (((cur == "0") && ((this.ch(1) == "x") || (this.ch(1) == "X")))) { this.adv(); this.adv(); let h = ""; while (/[0-9a-fA-F_]/.test(this.ch())) { const hc = this.adv(); if ((hc != "_")) { h += hc; } } this.tok(T.NUMBER, parseInt(((h.length > 0) ? h : "0"), 16), _f, _g); continue; } if (((cur == "0") && ((this.ch(1) == "b") || (this.ch(1) == "B")))) { this.adv(); this.adv(); let b = ""; while (/[01_]/.test(this.ch())) { const bc = this.adv(); if ((bc != "_")) { b += bc; } } this.tok(T.NUMBER, parseInt(((b.length > 0) ? b : "0"), 2), _f, _g); continue; } let numStr = ""; while ((this.pos < this.src.length)) { if (((this.ch() == ".") && (this.ch(1) == "."))) { break; } if ((((this.ch() >= "0") && (this.ch() <= "9")) || (this.ch() == "."))) { numStr += this.adv(); } else if ((this.ch() == "_")) { this.adv(); } else if ((((this.ch() == "e") || (this.ch() == "E")) && (numStr.length > 0))) { numStr += this.adv(); if (((this.ch() == "+") || (this.ch() == "-"))) { numStr += this.adv(); } while (((this.ch() >= "0") && (this.ch() <= "9"))) { numStr += this.adv(); } break; } else { break; } } this.tok(T.NUMBER, parseFloat(numStr), _f, _g); continue; } if ((cur == "\"")) { this.scanStr(_f, _g); continue; } if ((cur == "`")) { this.scanBacktick(_f, _g); continue; } if ((cur == "'")) { this.adv(); let sq = ""; while (((this.pos < this.src.length) && (this.ch() != "'"))) { if ((this.ch() == "\\")) { this.adv(); const e = this.adv(); if ((e == "n")) { sq += "\n"; } else if ((e == "t")) { sq += "\t"; } else if ((e == "r")) { sq += "\r"; } else if ((e == "'")) { sq += "'"; } else if ((e == "\\")) { sq += "\\"; } else { sq += ("\\" + e); } } else { sq += this.adv(); } } if ((this.ch() != "'")) { this.err("Unterminated string"); } this.adv(); this.tok(T.STRING, sq, _f, _g); continue; } if (((((cur >= "a") && (cur <= "z")) || ((cur >= "A") && (cur <= "Z"))) || (cur == "_"))) { let word = ""; while (/[a-zA-Z0-9_]/.test(this.ch())) { word += this.adv(); } if (((word == "_") && !/[a-zA-Z0-9_]/.test(this.ch()))) { this.tok(T.WILDCARD, "_", _f, _g); continue; } const kw = _a[word]; if ((kw == "__TRUE__")) { this.tok(T.BOOL, true, _f, _g); } else if ((kw == "__FALSE__")) { this.tok(T.BOOL, false, _f, _g); } else if ((kw == "__NULL__")) { this.tok(T.NULL, null, _f, _g); } else if ((kw != undefined)) { this.tok(kw, word, _f, _g); } else { this.tok(T.IDENT, word, _f, _g); } continue; } this.adv(); if ((cur == "+")) { if ((this.ch() == "+")) { this.adv(); this.tok(T.PLUSPLUS, "++", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.PLUSEQ, "+=", _f, _g); } else { this.tok(T.PLUS, "+", _f, _g); } } else if ((cur == "-")) { if ((this.ch() == "-")) { this.adv(); this.tok(T.MINUSMINUS, "--", _f, _g); } else if ((this.ch() == ">")) { this.adv(); this.tok(T.ARROW, "->", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.MINUSEQ, "-=", _f, _g); } else { this.tok(T.MINUS, "-", _f, _g); } } else if ((cur == "*")) { if ((this.ch() == "*")) { this.adv(); this.tok(T.STARSTAR, "**", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.STAREQ, "*=", _f, _g); } else { this.tok(T.STAR, "*", _f, _g); } } else if ((cur == "/")) { if ((this.ch() == "=")) { this.adv(); this.tok(T.SLASHEQ, "/=", _f, _g); } else { const lastTok = this.tokens[(this.tokens.length - 1)]; const afterVal = ((lastTok != null) && (((((((((((lastTok.type == T.IDENT) || (lastTok.type == T.NUMBER)) || (lastTok.type == T.STRING)) || (lastTok.type == T.BOOL)) || (lastTok.type == T.NULL)) || (lastTok.type == T.REGEX)) || (lastTok.type == T.RPAREN)) || (lastTok.type == T.RBRACKET)) || (lastTok.type == T.PLUSPLUS)) || (lastTok.type == T.MINUSMINUS)) || (lastTok.type == T.BANG))); if (afterVal) { this.tok(T.SLASH, "/", _f, _g); } else { this.scanRegexBody(_f, _g); } } } else if ((cur == "%")) { if ((this.ch() == "=")) { this.adv(); this.tok(T.PERCENTEQ, "%=", _f, _g); } else { this.tok(T.PERCENT, "%", _f, _g); } } else if ((cur == "=")) { if (((this.ch() == "=") && (this.src[(this.pos + 1)] == "="))) { this.adv(); this.adv(); this.tok(T.EQEQEQ, "===", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.EQEQ, "==", _f, _g); } else if ((this.ch() == ">")) { this.adv(); this.tok(T.FATARROW, "=>", _f, _g); } else { this.tok(T.EQ, "=", _f, _g); } } else if ((cur == "!")) { if (((this.ch() == "=") && (this.src[(this.pos + 1)] == "="))) { this.adv(); this.adv(); this.tok(T.NEQEQ, "!==", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.NEQ, "!=", _f, _g); } else { this.tok(T.BANG, "!", _f, _g); } } else if ((cur == "<")) { if ((this.ch() == "<")) { this.adv(); this.tok(T.LSHIFT, "<<", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.LTE, "<=", _f, _g); } else { this.tok(T.LT, "<", _f, _g); } } else if ((cur == ">")) { if ((this.ch() == ">")) { this.adv(); this.tok(T.RSHIFT, ">>", _f, _g); } else if ((this.ch() == "=")) { this.adv(); this.tok(T.GTE, ">=", _f, _g); } else { this.tok(T.GT, ">", _f, _g); } } else if ((cur == ".")) { if (((this.ch() == ".") && (this.src[(this.pos + 1)] == "."))) { this.adv(); this.adv(); this.tok(T.DOTDOTDOT, "...", _f, _g); } else if ((this.ch() == ".")) { this.adv(); this.tok(T.DOTDOT, "..", _f, _g); } else { this.tok(T.DOT, ".", _f, _g); } } else if ((cur == "?")) { if ((this.ch() == ".")) { this.adv(); this.tok(T.QUESTIONDOT, "?.", _f, _g); } else if ((this.ch() == "?")) { this.adv(); this.tok(T.NULLISH, "??", _f, _g); } else { this.tok(T.QUESTION, "?", _f, _g); } } else if ((cur == "|")) { if ((this.ch() == ">")) { this.adv(); this.tok(T.PIPE, "|>", _f, _g); } else if ((this.ch() == "|")) { this.adv(); this.tok(T.OROR, "||", _f, _g); } else { this.tok(T.PIPEB, "|", _f, _g); } } else if ((cur == "&")) { if ((this.ch() == "&")) { this.adv(); this.tok(T.ANDAND, "&&", _f, _g); } else { this.tok(T.AMPERSAND, "&", _f, _g); } } else if ((cur == "^")) { this.tok(T.CARET, "^", _f, _g); } else if ((cur == "~")) { this.tok(T.TILDE, "~", _f, _g); } else if ((cur == "@")) { this.tok(T.AT, "@", _f, _g); } else if ((cur == "(")) { this.nestDepth = (this.nestDepth + 1); this.tok(T.LPAREN, "(", _f, _g); } else if ((cur == ")")) { this.nestDepth = (this.nestDepth - 1); this.tok(T.RPAREN, ")", _f, _g); } else if ((cur == "[")) { this.nestDepth = (this.nestDepth + 1); this.tok(T.LBRACKET, "[", _f, _g); } else if ((cur == "]")) { this.nestDepth = (this.nestDepth - 1); this.tok(T.RBRACKET, "]", _f, _g); } else if ((cur == "{")) { this.nestDepth = (this.nestDepth + 1); this.tok(T.LBRACE, "{", _f, _g); } else if ((cur == "}")) { this.nestDepth = (this.nestDepth - 1); this.tok(T.RBRACE, "}", _f, _g); } else if ((cur == ",")) { this.tok(T.COMMA, ",", _f, _g); } else if ((cur == ":")) { this.tok(T.COLON, ":", _f, _g); } else if ((cur != ";")) { this.err(`Unknown character: '${cur}'`); } } while ((this.indentStack.length > 1)) { this.indentStack.pop(); this.tok(T.DEDENT, null, this.line, 1); } const lastTok = this.tokens[(this.tokens.length - 1)]; if (((lastTok && (lastTok.type != T.NEWLINE)) && (lastTok.type != T.DEDENT))) { this.tok(T.NEWLINE, null, this.line, this.col); } this.tok(T.EOF, null, this.line, this.col); return this.tokens; } } module.exports.Lexer = Lexer; function lexerize(_k) { const processed = _k.replace(/\r\n/g, "\n").replace(/\r/g, "\n"); return new Lexer(processed, 0, 1, 1, [], [0], 0); } module.exports.lexerize = lexerize;
|