@cuxt/sandboxjs 0.1.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/LICENSE +21 -0
- package/README.md +186 -0
- package/build/Sandbox.d.ts +25 -0
- package/build/Sandbox.js +62 -0
- package/build/SandboxExec.d.ts +66 -0
- package/build/SandboxExec.js +214 -0
- package/build/eval.d.ts +27 -0
- package/build/eval.js +205 -0
- package/build/executor.d.ts +124 -0
- package/build/executor.js +1546 -0
- package/build/parser.d.ts +154 -0
- package/build/parser.js +1527 -0
- package/build/unraw.d.ts +11 -0
- package/build/unraw.js +168 -0
- package/build/utils.d.ts +264 -0
- package/build/utils.js +362 -0
- package/dist/Sandbox.d.ts +25 -0
- package/dist/Sandbox.js +270 -0
- package/dist/Sandbox.js.map +1 -0
- package/dist/Sandbox.min.js +2 -0
- package/dist/Sandbox.min.js.map +1 -0
- package/dist/SandboxExec.d.ts +66 -0
- package/dist/SandboxExec.js +218 -0
- package/dist/SandboxExec.js.map +1 -0
- package/dist/SandboxExec.min.js +2 -0
- package/dist/SandboxExec.min.js.map +1 -0
- package/dist/eval.d.ts +27 -0
- package/dist/executor.d.ts +124 -0
- package/dist/executor.js +1550 -0
- package/dist/executor.js.map +1 -0
- package/dist/node/Sandbox.d.ts +25 -0
- package/dist/node/Sandbox.js +277 -0
- package/dist/node/SandboxExec.d.ts +66 -0
- package/dist/node/SandboxExec.js +225 -0
- package/dist/node/eval.d.ts +27 -0
- package/dist/node/executor.d.ts +124 -0
- package/dist/node/executor.js +1567 -0
- package/dist/node/parser.d.ts +154 -0
- package/dist/node/parser.js +1704 -0
- package/dist/node/unraw.d.ts +11 -0
- package/dist/node/utils.d.ts +264 -0
- package/dist/node/utils.js +385 -0
- package/dist/parser.d.ts +154 -0
- package/dist/parser.js +1690 -0
- package/dist/parser.js.map +1 -0
- package/dist/unraw.d.ts +11 -0
- package/dist/utils.d.ts +264 -0
- package/dist/utils.js +365 -0
- package/dist/utils.js.map +1 -0
- package/package.json +68 -0
package/build/parser.js
ADDED
|
@@ -0,0 +1,1527 @@
|
|
|
1
|
+
import unraw from './unraw.js';
|
|
2
|
+
import { CodeString, isLisp, reservedWords } from './utils.js';
|
|
3
|
+
function createLisp(obj) {
|
|
4
|
+
return [obj.op, obj.a, obj.b];
|
|
5
|
+
}
|
|
6
|
+
const NullLisp = createLisp({ op: 0 /* LispType.None */, a: 0 /* LispType.None */, b: 0 /* LispType.None */ });
|
|
7
|
+
const lispTypes = new Map();
|
|
8
|
+
export class ParseError extends Error {
|
|
9
|
+
constructor(message, code) {
|
|
10
|
+
super(message + ': ' + code.substring(0, 40));
|
|
11
|
+
this.code = code;
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
let lastType;
|
|
15
|
+
let lastPart;
|
|
16
|
+
let lastLastPart;
|
|
17
|
+
let lastLastLastPart;
|
|
18
|
+
let lastLastLastLastPart;
|
|
19
|
+
const inlineIfElse = /^:/;
|
|
20
|
+
const elseIf = /^else(?![\w$])/;
|
|
21
|
+
const ifElse = /^if(?![\w$])/;
|
|
22
|
+
const space = /^\s/;
|
|
23
|
+
export const expectTypes = {
|
|
24
|
+
splitter: {
|
|
25
|
+
types: {
|
|
26
|
+
power: /^(\*\*)(?!=)/,
|
|
27
|
+
opHigh: /^(\/|\*(?!\*)|%)(?!=)/,
|
|
28
|
+
op: /^(\+(?!(\+))|-(?!(-)))(?!=)/,
|
|
29
|
+
comparitor: /^(<=|>=|<(?!<)|>(?!>)|!==|!=(?!=)|===|==)/,
|
|
30
|
+
bitwiseShift: /^(<<|>>(?!>)|>>>)(?!=)/,
|
|
31
|
+
bitwiseAnd: /^(&(?!&))(?!=)/,
|
|
32
|
+
bitwiseXor: /^(\^)(?!=)/,
|
|
33
|
+
bitwiseOr: /^(\|(?!\|))(?!=)/,
|
|
34
|
+
boolOpAnd: /^(&&)(?!=)/,
|
|
35
|
+
boolOpOr: /^(\|\|(?!=)|instanceof(?![\w$])|in(?![\w$]))/,
|
|
36
|
+
nullishCoalescing: /^\?\?(?!=)/,
|
|
37
|
+
},
|
|
38
|
+
next: ['modifier', 'value', 'prop', 'incrementerBefore'],
|
|
39
|
+
},
|
|
40
|
+
inlineIf: {
|
|
41
|
+
types: {
|
|
42
|
+
inlineIf: /^\?(?!\.(?!\d))/,
|
|
43
|
+
},
|
|
44
|
+
next: ['expEnd'],
|
|
45
|
+
},
|
|
46
|
+
assignment: {
|
|
47
|
+
types: {
|
|
48
|
+
assignModify: /^(-=|\+=|\/=|\*\*=|\*=|%=|\^=|&=|\|=|>>>=|>>=|<<=|&&=|\|\|=|\?\?=)/,
|
|
49
|
+
assign: /^(=)(?!=)/,
|
|
50
|
+
},
|
|
51
|
+
next: ['modifier', 'value', 'prop', 'incrementerBefore'],
|
|
52
|
+
},
|
|
53
|
+
incrementerBefore: {
|
|
54
|
+
types: { incrementerBefore: /^(\+\+|--)/ },
|
|
55
|
+
next: ['prop'],
|
|
56
|
+
},
|
|
57
|
+
expEdge: {
|
|
58
|
+
types: {
|
|
59
|
+
call: /^(\?\.)?[(]/,
|
|
60
|
+
incrementerAfter: /^(\+\+|--)/,
|
|
61
|
+
taggedTemplate: /^`(\d+)`/,
|
|
62
|
+
},
|
|
63
|
+
next: ['splitter', 'expEdge', 'dot', 'inlineIf', 'expEnd'],
|
|
64
|
+
},
|
|
65
|
+
modifier: {
|
|
66
|
+
types: {
|
|
67
|
+
not: /^!/,
|
|
68
|
+
inverse: /^~/,
|
|
69
|
+
negative: /^-(?!-)/,
|
|
70
|
+
positive: /^\+(?!\+)/,
|
|
71
|
+
typeof: /^typeof(?![\w$])/,
|
|
72
|
+
delete: /^delete(?![\w$])/,
|
|
73
|
+
},
|
|
74
|
+
next: ['modifier', 'value', 'prop', 'incrementerBefore'],
|
|
75
|
+
},
|
|
76
|
+
dot: {
|
|
77
|
+
types: {
|
|
78
|
+
arrayProp: /^(\?\.)?\[/,
|
|
79
|
+
dot: /^(\?)?\.(?=\s*[a-zA-Z$_])/,
|
|
80
|
+
},
|
|
81
|
+
next: ['splitter', 'assignment', 'expEdge', 'dot', 'inlineIf', 'expEnd'],
|
|
82
|
+
},
|
|
83
|
+
prop: {
|
|
84
|
+
types: {
|
|
85
|
+
prop: /^[a-zA-Z$_][a-zA-Z\d$_]*/,
|
|
86
|
+
},
|
|
87
|
+
next: ['splitter', 'assignment', 'expEdge', 'dot', 'inlineIf', 'expEnd'],
|
|
88
|
+
},
|
|
89
|
+
value: {
|
|
90
|
+
types: {
|
|
91
|
+
createObject: /^\{/,
|
|
92
|
+
createArray: /^\[/,
|
|
93
|
+
number: /^(0b[01]+(_[01]+)*|0o[0-7]+(_[0-7]+)*|0x[\da-f]+(_[\da-f]+)*|(\d+(_\d+)*(\.\d+(_\d+)*)?|\.\d+(_\d+)*))(e[+-]?\d+(_\d+)*)?(n)?(?!\d)/i,
|
|
94
|
+
string: /^"(\d+)"/,
|
|
95
|
+
literal: /^`(\d+)`/,
|
|
96
|
+
regex: /^\/(\d+)\/r(?![\w$])/,
|
|
97
|
+
boolean: /^(true|false)(?![\w$])/,
|
|
98
|
+
null: /^null(?![\w$])/,
|
|
99
|
+
und: /^undefined(?![\w$])/,
|
|
100
|
+
arrowFunctionSingle: /^(async\s+)?([a-zA-Z$_][a-zA-Z\d$_]*)\s*=>\s*({)?/,
|
|
101
|
+
arrowFunction: /^(async\s*)?\(\s*((\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*)*)?\s*\)\s*=>\s*({)?/,
|
|
102
|
+
inlineFunction: /^(async\s+)?function(\s*[a-zA-Z$_][a-zA-Z\d$_]*)?\s*\(\s*((\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*)*)?\s*\)\s*{/,
|
|
103
|
+
group: /^\(/,
|
|
104
|
+
NaN: /^NaN(?![\w$])/,
|
|
105
|
+
Infinity: /^Infinity(?![\w$])/,
|
|
106
|
+
void: /^void(?![\w$])\s*/,
|
|
107
|
+
await: /^await(?![\w$])\s*/,
|
|
108
|
+
new: /^new(?![\w$])\s*/,
|
|
109
|
+
},
|
|
110
|
+
next: ['splitter', 'expEdge', 'dot', 'inlineIf', 'expEnd'],
|
|
111
|
+
},
|
|
112
|
+
initialize: {
|
|
113
|
+
types: {
|
|
114
|
+
initialize: /^(var|let|const)\s+([a-zA-Z$_][a-zA-Z\d$_]*)\s*(=)?/,
|
|
115
|
+
return: /^return(?![\w$])/,
|
|
116
|
+
throw: /^throw(?![\w$])\s*/,
|
|
117
|
+
},
|
|
118
|
+
next: ['modifier', 'value', 'prop', 'incrementerBefore', 'expEnd'],
|
|
119
|
+
},
|
|
120
|
+
spreadObject: {
|
|
121
|
+
types: {
|
|
122
|
+
spreadObject: /^\.\.\./,
|
|
123
|
+
},
|
|
124
|
+
next: ['value', 'prop'],
|
|
125
|
+
},
|
|
126
|
+
spreadArray: {
|
|
127
|
+
types: {
|
|
128
|
+
spreadArray: /^\.\.\./,
|
|
129
|
+
},
|
|
130
|
+
next: ['value', 'prop'],
|
|
131
|
+
},
|
|
132
|
+
expEnd: { types: {}, next: [] },
|
|
133
|
+
expFunction: {
|
|
134
|
+
types: {
|
|
135
|
+
function: /^(async\s+)?function(\s*[a-zA-Z$_][a-zA-Z\d$_]*)\s*\(\s*((\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*(\s*,\s*(\.\.\.)?\s*[a-zA-Z$_][a-zA-Z\d$_]*)*)?\s*\)\s*{/,
|
|
136
|
+
},
|
|
137
|
+
next: ['expEdge', 'expEnd'],
|
|
138
|
+
},
|
|
139
|
+
expSingle: {
|
|
140
|
+
types: {
|
|
141
|
+
for: /^(([a-zA-Z$_][\w$]*)\s*:)?\s*for\s*\(/,
|
|
142
|
+
do: /^(([a-zA-Z$_][\w$]*)\s*:)?\s*do(?![\w$])\s*(\{)?/,
|
|
143
|
+
while: /^(([a-zA-Z$_][\w$]*)\s*:)?\s*while\s*\(/,
|
|
144
|
+
loopAction: /^(break|continue)(?![\w$])\s*([a-zA-Z$_][\w$]*)?/,
|
|
145
|
+
if: /^((([a-zA-Z$_][\w$]*)\s*:)?\s*)if\s*\(/,
|
|
146
|
+
try: /^try\s*{/,
|
|
147
|
+
block: /^{/,
|
|
148
|
+
switch: /^(([a-zA-Z$_][\w$]*)\s*:)?\s*switch\s*\(/,
|
|
149
|
+
},
|
|
150
|
+
next: ['expEnd'],
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
const closings = {
|
|
154
|
+
'(': ')',
|
|
155
|
+
'[': ']',
|
|
156
|
+
'{': '}',
|
|
157
|
+
"'": "'",
|
|
158
|
+
'"': '"',
|
|
159
|
+
'`': '`',
|
|
160
|
+
};
|
|
161
|
+
export function testMultiple(str, tests) {
|
|
162
|
+
let found = null;
|
|
163
|
+
for (let i = 0; i < tests.length; i++) {
|
|
164
|
+
const test = tests[i];
|
|
165
|
+
found = test.exec(str);
|
|
166
|
+
if (found)
|
|
167
|
+
break;
|
|
168
|
+
}
|
|
169
|
+
return found;
|
|
170
|
+
}
|
|
171
|
+
const emptyString = new CodeString('');
|
|
172
|
+
const okFirstChars = /^[+\-~ !]/;
|
|
173
|
+
const aNumber = expectTypes.value.types.number;
|
|
174
|
+
const wordReg = /^((if|for|else|while|do|function)(?![\w$])|[\w$]+)/;
|
|
175
|
+
const semiColon = /^;/;
|
|
176
|
+
const insertedSemicolons = new WeakMap();
|
|
177
|
+
const quoteCache = new WeakMap();
|
|
178
|
+
export function restOfExp(constants, part, tests, quote, firstOpening, closingsTests, details = {}) {
|
|
179
|
+
if (!part.length) {
|
|
180
|
+
return part;
|
|
181
|
+
}
|
|
182
|
+
details.words = details.words || [];
|
|
183
|
+
let isStart = true;
|
|
184
|
+
tests = tests || [];
|
|
185
|
+
const hasSemiTest = tests.includes(semiColon);
|
|
186
|
+
if (hasSemiTest) {
|
|
187
|
+
tests = tests.filter((a) => a !== semiColon);
|
|
188
|
+
}
|
|
189
|
+
const insertedSemis = insertedSemicolons.get(part.ref) || [];
|
|
190
|
+
const cache = quoteCache.get(part.ref) || new Map();
|
|
191
|
+
quoteCache.set(part.ref, cache);
|
|
192
|
+
if (quote && cache.has(part.start - 1)) {
|
|
193
|
+
return part.substring(0, cache.get(part.start - 1) - part.start);
|
|
194
|
+
}
|
|
195
|
+
let escape = false;
|
|
196
|
+
let done = false;
|
|
197
|
+
let lastChar = '';
|
|
198
|
+
let isOneLiner = false;
|
|
199
|
+
let i;
|
|
200
|
+
let lastInertedSemi = false;
|
|
201
|
+
let seenKeyword = false;
|
|
202
|
+
let skipNextWord = false;
|
|
203
|
+
for (i = 0; i < part.length && !done; i++) {
|
|
204
|
+
let char = part.char(i);
|
|
205
|
+
if (quote === '"' || quote === "'" || quote === '`') {
|
|
206
|
+
if (quote === '`' && char === '$' && part.char(i + 1) === '{' && !escape) {
|
|
207
|
+
const skip = restOfExp(constants, part.substring(i + 2), [], '{');
|
|
208
|
+
i += skip.length + 2;
|
|
209
|
+
}
|
|
210
|
+
else if (char === quote && !escape) {
|
|
211
|
+
return part.substring(0, i);
|
|
212
|
+
}
|
|
213
|
+
escape = !escape && char === '\\';
|
|
214
|
+
}
|
|
215
|
+
else if (closings[char]) {
|
|
216
|
+
if (!lastInertedSemi && insertedSemis[i + part.start]) {
|
|
217
|
+
lastInertedSemi = true;
|
|
218
|
+
if (hasSemiTest) {
|
|
219
|
+
break;
|
|
220
|
+
}
|
|
221
|
+
i--;
|
|
222
|
+
lastChar = ';';
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (isOneLiner && char === '{') {
|
|
226
|
+
isOneLiner = false;
|
|
227
|
+
}
|
|
228
|
+
if (char === firstOpening) {
|
|
229
|
+
done = true;
|
|
230
|
+
break;
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
const skip = restOfExp(constants, part.substring(i + 1), [], char);
|
|
234
|
+
cache.set(skip.start - 1, skip.end);
|
|
235
|
+
i += skip.length + 1;
|
|
236
|
+
isStart = false;
|
|
237
|
+
if (closingsTests) {
|
|
238
|
+
const sub = part.substring(i);
|
|
239
|
+
let found;
|
|
240
|
+
if ((found = testMultiple(sub.toString(), closingsTests))) {
|
|
241
|
+
details.regRes = found;
|
|
242
|
+
done = true;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
else if (!quote) {
|
|
248
|
+
let sub = part.substring(i).toString();
|
|
249
|
+
let foundWord;
|
|
250
|
+
let foundNumber;
|
|
251
|
+
if (closingsTests) {
|
|
252
|
+
let found;
|
|
253
|
+
if ((found = testMultiple(sub, closingsTests))) {
|
|
254
|
+
details.regRes = found;
|
|
255
|
+
i++;
|
|
256
|
+
done = true;
|
|
257
|
+
break;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
if ((foundNumber = aNumber.exec(sub))) {
|
|
261
|
+
i += foundNumber[0].length - 1;
|
|
262
|
+
sub = part.substring(i).toString();
|
|
263
|
+
if (closingsTests) {
|
|
264
|
+
let found;
|
|
265
|
+
if ((found = testMultiple(sub, closingsTests))) {
|
|
266
|
+
details.regRes = found;
|
|
267
|
+
i++;
|
|
268
|
+
done = true;
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
else if (lastChar != char) {
|
|
274
|
+
let found = null;
|
|
275
|
+
if (char === ';' || (insertedSemis[i + part.start] && !isStart && !lastInertedSemi)) {
|
|
276
|
+
if (hasSemiTest) {
|
|
277
|
+
found = [';'];
|
|
278
|
+
}
|
|
279
|
+
else if (insertedSemis[i + part.start]) {
|
|
280
|
+
lastInertedSemi = true;
|
|
281
|
+
i--;
|
|
282
|
+
lastChar = ';';
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
char = sub = ';';
|
|
286
|
+
}
|
|
287
|
+
else {
|
|
288
|
+
lastInertedSemi = false;
|
|
289
|
+
}
|
|
290
|
+
if (!found) {
|
|
291
|
+
found = testMultiple(sub, tests);
|
|
292
|
+
}
|
|
293
|
+
if (found) {
|
|
294
|
+
done = true;
|
|
295
|
+
}
|
|
296
|
+
if (!done && (foundWord = wordReg.exec(sub))) {
|
|
297
|
+
isOneLiner = true;
|
|
298
|
+
if (foundWord[2]) {
|
|
299
|
+
seenKeyword = true;
|
|
300
|
+
skipNextWord = true;
|
|
301
|
+
}
|
|
302
|
+
else if (seenKeyword) {
|
|
303
|
+
if (skipNextWord) {
|
|
304
|
+
skipNextWord = false;
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
details.bodyContentAfterKeyword = true;
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
if (foundWord[0].length > 1) {
|
|
311
|
+
details.words.push(foundWord[1]);
|
|
312
|
+
details.lastAnyWord = foundWord[1];
|
|
313
|
+
if (foundWord[2]) {
|
|
314
|
+
details.lastWord = foundWord[2];
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (foundWord[0].length > 2) {
|
|
318
|
+
i += foundWord[0].length - 2;
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
if (isStart) {
|
|
323
|
+
if (okFirstChars.test(sub)) {
|
|
324
|
+
done = false;
|
|
325
|
+
}
|
|
326
|
+
else {
|
|
327
|
+
isStart = false;
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
if (done)
|
|
331
|
+
break;
|
|
332
|
+
}
|
|
333
|
+
else if (char === closings[quote]) {
|
|
334
|
+
return part.substring(0, i);
|
|
335
|
+
}
|
|
336
|
+
lastChar = char;
|
|
337
|
+
}
|
|
338
|
+
if (quote) {
|
|
339
|
+
throw new SyntaxError("Unclosed '" + quote + "'");
|
|
340
|
+
}
|
|
341
|
+
if (details) {
|
|
342
|
+
details.oneliner = isOneLiner;
|
|
343
|
+
}
|
|
344
|
+
return part.substring(0, i);
|
|
345
|
+
}
|
|
346
|
+
restOfExp.next = ['splitter', 'expEnd', 'inlineIf'];
|
|
347
|
+
const startingExecpted = [
|
|
348
|
+
'initialize',
|
|
349
|
+
'expSingle',
|
|
350
|
+
'expFunction',
|
|
351
|
+
'value',
|
|
352
|
+
'modifier',
|
|
353
|
+
'prop',
|
|
354
|
+
'incrementerBefore',
|
|
355
|
+
'expEnd',
|
|
356
|
+
];
|
|
357
|
+
export const setLispType = (types, fn) => {
|
|
358
|
+
types.forEach((type) => {
|
|
359
|
+
lispTypes.set(type, fn);
|
|
360
|
+
});
|
|
361
|
+
};
|
|
362
|
+
const closingsCreate = {
|
|
363
|
+
createArray: /^\]/,
|
|
364
|
+
createObject: /^\}/,
|
|
365
|
+
group: /^\)/,
|
|
366
|
+
arrayProp: /^\]/,
|
|
367
|
+
call: /^\)/,
|
|
368
|
+
};
|
|
369
|
+
const typesCreate = {
|
|
370
|
+
createArray: 12 /* LispType.CreateArray */,
|
|
371
|
+
createObject: 22 /* LispType.CreateObject */,
|
|
372
|
+
group: 23 /* LispType.Group */,
|
|
373
|
+
arrayProp: 19 /* LispType.ArrayProp */,
|
|
374
|
+
call: 5 /* LispType.Call */,
|
|
375
|
+
prop: 1 /* LispType.Prop */,
|
|
376
|
+
'?prop': 20 /* LispType.PropOptional */,
|
|
377
|
+
'?call': 21 /* LispType.CallOptional */,
|
|
378
|
+
};
|
|
379
|
+
setLispType(['createArray', 'createObject', 'group', 'arrayProp', 'call'], (constants, type, part, res, expect, ctx) => {
|
|
380
|
+
let extract = emptyString;
|
|
381
|
+
const arg = [];
|
|
382
|
+
let end = false;
|
|
383
|
+
let i = res[0].length;
|
|
384
|
+
const start = i;
|
|
385
|
+
while (i < part.length && !end) {
|
|
386
|
+
extract = restOfExp(constants, part.substring(i), [closingsCreate[type], /^,/]);
|
|
387
|
+
i += extract.length;
|
|
388
|
+
if (extract.trim().length) {
|
|
389
|
+
arg.push(extract);
|
|
390
|
+
}
|
|
391
|
+
if (part.char(i) !== ',') {
|
|
392
|
+
end = true;
|
|
393
|
+
}
|
|
394
|
+
else {
|
|
395
|
+
i++;
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
const next = ['value', 'modifier', 'prop', 'incrementerBefore', 'expEnd'];
|
|
399
|
+
let l;
|
|
400
|
+
let funcFound;
|
|
401
|
+
switch (type) {
|
|
402
|
+
case 'group':
|
|
403
|
+
case 'arrayProp':
|
|
404
|
+
l = lispifyExpr(constants, part.substring(start, i));
|
|
405
|
+
break;
|
|
406
|
+
case 'call':
|
|
407
|
+
case 'createArray':
|
|
408
|
+
// @TODO: support 'empty' values
|
|
409
|
+
l = arg.map((e) => lispify(constants, e, [...next, 'spreadArray']));
|
|
410
|
+
break;
|
|
411
|
+
case 'createObject':
|
|
412
|
+
l = arg.map((str) => {
|
|
413
|
+
str = str.trimStart();
|
|
414
|
+
let value;
|
|
415
|
+
let key = '';
|
|
416
|
+
funcFound = expectTypes.expFunction.types.function.exec('function ' + str);
|
|
417
|
+
if (funcFound) {
|
|
418
|
+
key = funcFound[2].trimStart();
|
|
419
|
+
value = lispify(constants, new CodeString('function ' + str.toString().replace(key, '')));
|
|
420
|
+
}
|
|
421
|
+
else {
|
|
422
|
+
const extract = restOfExp(constants, str, [/^:/]);
|
|
423
|
+
key = lispify(constants, extract, [...next, 'spreadObject']);
|
|
424
|
+
// Check if this is a spread object
|
|
425
|
+
if (isLisp(key) && key[0] === 17 /* LispType.SpreadObject */) {
|
|
426
|
+
// For spread objects, there's no separate value
|
|
427
|
+
value = NullLisp;
|
|
428
|
+
}
|
|
429
|
+
else {
|
|
430
|
+
// Extract the property name from Prop if needed
|
|
431
|
+
if (key[0] === 1 /* LispType.Prop */) {
|
|
432
|
+
key = key[2];
|
|
433
|
+
}
|
|
434
|
+
// Check if there's a colon (property with value) or not (property shorthand)
|
|
435
|
+
if (str.length > extract.length && str.char(extract.length) === ':') {
|
|
436
|
+
// Property with explicit value: {key: value}
|
|
437
|
+
value = lispify(constants, str.substring(extract.length + 1));
|
|
438
|
+
}
|
|
439
|
+
else {
|
|
440
|
+
// Property shorthand: {key} is equivalent to {key: key}
|
|
441
|
+
// Parse the key name as a variable reference
|
|
442
|
+
value = lispify(constants, extract, next);
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
return createLisp({
|
|
447
|
+
op: 6 /* LispType.KeyVal */,
|
|
448
|
+
a: key,
|
|
449
|
+
b: value,
|
|
450
|
+
});
|
|
451
|
+
});
|
|
452
|
+
break;
|
|
453
|
+
}
|
|
454
|
+
const lisptype = (type === 'arrayProp'
|
|
455
|
+
? res[1]
|
|
456
|
+
? 20 /* LispType.PropOptional */
|
|
457
|
+
: 1 /* LispType.Prop */
|
|
458
|
+
: type === 'call'
|
|
459
|
+
? res[1]
|
|
460
|
+
? 21 /* LispType.CallOptional */
|
|
461
|
+
: 5 /* LispType.Call */
|
|
462
|
+
: typesCreate[type]);
|
|
463
|
+
ctx.lispTree = lispify(constants, part.substring(i + 1), expectTypes[expect].next, createLisp({
|
|
464
|
+
op: lisptype,
|
|
465
|
+
a: ctx.lispTree,
|
|
466
|
+
b: l,
|
|
467
|
+
}));
|
|
468
|
+
});
|
|
469
|
+
const modifierTypes = {
|
|
470
|
+
inverse: 64 /* LispType.Inverse */,
|
|
471
|
+
not: 24 /* LispType.Not */,
|
|
472
|
+
positive: 59 /* LispType.Positive */,
|
|
473
|
+
negative: 58 /* LispType.Negative */,
|
|
474
|
+
typeof: 60 /* LispType.Typeof */,
|
|
475
|
+
delete: 61 /* LispType.Delete */,
|
|
476
|
+
};
|
|
477
|
+
setLispType(['inverse', 'not', 'negative', 'positive', 'typeof', 'delete'], (constants, type, part, res, expect, ctx) => {
|
|
478
|
+
const extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s.?\w$]|\?[^.])/]);
|
|
479
|
+
// Check if the extracted expression contains an exponentiation operator at the top level
|
|
480
|
+
// ECMAScript disallows unary operators before ** without parentheses
|
|
481
|
+
const remainingAfterOperand = part.substring(extract.length + res[0].length);
|
|
482
|
+
const remainingStr = remainingAfterOperand.trim().toString();
|
|
483
|
+
if (remainingStr.startsWith('**')) {
|
|
484
|
+
throw new SyntaxError('Unary operator used immediately before exponentiation expression. Parenthesis must be used to disambiguate operator precedence');
|
|
485
|
+
}
|
|
486
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
487
|
+
op: modifierTypes[type],
|
|
488
|
+
a: ctx.lispTree,
|
|
489
|
+
b: lispify(constants, extract, expectTypes[expect].next),
|
|
490
|
+
}));
|
|
491
|
+
});
|
|
492
|
+
setLispType(['taggedTemplate'], (constants, type, part, res, expect, ctx) => {
|
|
493
|
+
// Tagged template: func`template`
|
|
494
|
+
// Build a call with the literal strings array and interpolated values
|
|
495
|
+
const literalIndex = res[1];
|
|
496
|
+
const literal = constants.literals[parseInt(literalIndex)];
|
|
497
|
+
const [, templateStr, jsExprs] = literal;
|
|
498
|
+
// Extract the string parts and expression parts
|
|
499
|
+
const stringParts = [];
|
|
500
|
+
const expressions = [];
|
|
501
|
+
let currentStr = '';
|
|
502
|
+
let i = 0;
|
|
503
|
+
while (i < templateStr.length) {
|
|
504
|
+
if (templateStr.substring(i, i + 2) === '${') {
|
|
505
|
+
// Look ahead to check if this is a valid placeholder: ${digits}
|
|
506
|
+
let j = i + 2;
|
|
507
|
+
let exprIndex = '';
|
|
508
|
+
let isValidPlaceholder = false;
|
|
509
|
+
// Extract potential index and check for closing '}'
|
|
510
|
+
while (j < templateStr.length && templateStr[j] !== '}') {
|
|
511
|
+
exprIndex += templateStr[j];
|
|
512
|
+
j++;
|
|
513
|
+
}
|
|
514
|
+
// Check if we found a closing '}' and the content is numeric
|
|
515
|
+
if (j < templateStr.length && templateStr[j] === '}' && /^\d+$/.test(exprIndex)) {
|
|
516
|
+
isValidPlaceholder = true;
|
|
517
|
+
}
|
|
518
|
+
if (isValidPlaceholder) {
|
|
519
|
+
// Valid placeholder - add current string and the expression
|
|
520
|
+
stringParts.push(currentStr);
|
|
521
|
+
currentStr = '';
|
|
522
|
+
expressions.push(jsExprs[parseInt(exprIndex)]);
|
|
523
|
+
i = j + 1; // skip past the '}'
|
|
524
|
+
}
|
|
525
|
+
else {
|
|
526
|
+
// Not a valid placeholder - treat as literal text
|
|
527
|
+
currentStr += templateStr[i];
|
|
528
|
+
i++;
|
|
529
|
+
}
|
|
530
|
+
}
|
|
531
|
+
else {
|
|
532
|
+
currentStr += templateStr[i];
|
|
533
|
+
i++;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
stringParts.push(currentStr);
|
|
537
|
+
// Create array of string literals
|
|
538
|
+
const stringsArray = stringParts.map((str) => createLisp({
|
|
539
|
+
op: 2 /* LispType.StringIndex */,
|
|
540
|
+
a: 0 /* LispType.None */,
|
|
541
|
+
b: String(constants.strings.push(str) - 1),
|
|
542
|
+
}));
|
|
543
|
+
// Create the strings array
|
|
544
|
+
const stringsArrayLisp = createLisp({
|
|
545
|
+
op: 12 /* LispType.CreateArray */,
|
|
546
|
+
a: createLisp({
|
|
547
|
+
op: 0 /* LispType.None */,
|
|
548
|
+
a: 0 /* LispType.None */,
|
|
549
|
+
b: 0 /* LispType.None */,
|
|
550
|
+
}),
|
|
551
|
+
b: stringsArray,
|
|
552
|
+
});
|
|
553
|
+
// Create call with tag function
|
|
554
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
555
|
+
op: 5 /* LispType.Call */,
|
|
556
|
+
a: ctx.lispTree,
|
|
557
|
+
b: [stringsArrayLisp, ...expressions],
|
|
558
|
+
}));
|
|
559
|
+
});
|
|
560
|
+
const incrementTypes = {
|
|
561
|
+
'++$': 25 /* LispType.IncrementBefore */,
|
|
562
|
+
'--$': 27 /* LispType.DecrementBefore */,
|
|
563
|
+
'$++': 26 /* LispType.IncrementAfter */,
|
|
564
|
+
'$--': 28 /* LispType.DecrementAfter */,
|
|
565
|
+
};
|
|
566
|
+
setLispType(['incrementerBefore'], (constants, type, part, res, expect, ctx) => {
|
|
567
|
+
const extract = restOfExp(constants, part.substring(2), [/^[^\s.\w$]/]);
|
|
568
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + 2), restOfExp.next, createLisp({
|
|
569
|
+
op: incrementTypes[res[0] + '$'],
|
|
570
|
+
a: lispify(constants, extract, expectTypes[expect].next),
|
|
571
|
+
b: 0 /* LispType.None */,
|
|
572
|
+
}));
|
|
573
|
+
});
|
|
574
|
+
setLispType(['incrementerAfter'], (constants, type, part, res, expect, ctx) => {
|
|
575
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
576
|
+
op: incrementTypes['$' + res[0]],
|
|
577
|
+
a: ctx.lispTree,
|
|
578
|
+
b: 0 /* LispType.None */,
|
|
579
|
+
}));
|
|
580
|
+
});
|
|
581
|
+
const adderTypes = {
|
|
582
|
+
'&&': 29 /* LispType.And */,
|
|
583
|
+
'||': 30 /* LispType.Or */,
|
|
584
|
+
'??': 89 /* LispType.NullishCoalescing */,
|
|
585
|
+
instanceof: 62 /* LispType.Instanceof */,
|
|
586
|
+
in: 63 /* LispType.In */,
|
|
587
|
+
'=': 9 /* LispType.Assign */,
|
|
588
|
+
'-=': 65 /* LispType.SubractEquals */,
|
|
589
|
+
'+=': 66 /* LispType.AddEquals */,
|
|
590
|
+
'/=': 67 /* LispType.DivideEquals */,
|
|
591
|
+
'**=': 68 /* LispType.PowerEquals */,
|
|
592
|
+
'*=': 69 /* LispType.MultiplyEquals */,
|
|
593
|
+
'%=': 70 /* LispType.ModulusEquals */,
|
|
594
|
+
'^=': 71 /* LispType.BitNegateEquals */,
|
|
595
|
+
'&=': 72 /* LispType.BitAndEquals */,
|
|
596
|
+
'|=': 73 /* LispType.BitOrEquals */,
|
|
597
|
+
'>>>=': 74 /* LispType.UnsignedShiftRightEquals */,
|
|
598
|
+
'<<=': 76 /* LispType.ShiftLeftEquals */,
|
|
599
|
+
'>>=': 75 /* LispType.ShiftRightEquals */,
|
|
600
|
+
'&&=': 90 /* LispType.AndEquals */,
|
|
601
|
+
'||=': 91 /* LispType.OrEquals */,
|
|
602
|
+
'??=': 92 /* LispType.NullishCoalescingEquals */,
|
|
603
|
+
};
|
|
604
|
+
setLispType(['assign', 'assignModify', 'nullishCoalescing'], (constants, type, part, res, expect, ctx) => {
|
|
605
|
+
ctx.lispTree = createLisp({
|
|
606
|
+
op: adderTypes[res[0]],
|
|
607
|
+
a: ctx.lispTree,
|
|
608
|
+
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next),
|
|
609
|
+
});
|
|
610
|
+
});
|
|
611
|
+
const opTypes = {
|
|
612
|
+
'&': 77 /* LispType.BitAnd */,
|
|
613
|
+
'|': 78 /* LispType.BitOr */,
|
|
614
|
+
'^': 79 /* LispType.BitNegate */,
|
|
615
|
+
'<<': 80 /* LispType.BitShiftLeft */,
|
|
616
|
+
'>>': 81 /* LispType.BitShiftRight */,
|
|
617
|
+
'>>>': 82 /* LispType.BitUnsignedShiftRight */,
|
|
618
|
+
'<=': 54 /* LispType.SmallerEqualThan */,
|
|
619
|
+
'>=': 55 /* LispType.LargerEqualThan */,
|
|
620
|
+
'<': 56 /* LispType.SmallerThan */,
|
|
621
|
+
'>': 57 /* LispType.LargerThan */,
|
|
622
|
+
'!==': 31 /* LispType.StrictNotEqual */,
|
|
623
|
+
'!=': 53 /* LispType.NotEqual */,
|
|
624
|
+
'===': 32 /* LispType.StrictEqual */,
|
|
625
|
+
'==': 52 /* LispType.Equal */,
|
|
626
|
+
'+': 33 /* LispType.Plus */,
|
|
627
|
+
'-': 47 /* LispType.Minus */,
|
|
628
|
+
'/': 48 /* LispType.Divide */,
|
|
629
|
+
'**': 49 /* LispType.Power */,
|
|
630
|
+
'*': 50 /* LispType.Multiply */,
|
|
631
|
+
'%': 51 /* LispType.Modulus */,
|
|
632
|
+
'&&': 29 /* LispType.And */,
|
|
633
|
+
'||': 30 /* LispType.Or */,
|
|
634
|
+
instanceof: 62 /* LispType.Instanceof */,
|
|
635
|
+
in: 63 /* LispType.In */,
|
|
636
|
+
};
|
|
637
|
+
// Combined handler for all binary operators with configurable precedence
|
|
638
|
+
setLispType([
|
|
639
|
+
'power',
|
|
640
|
+
'opHigh',
|
|
641
|
+
'op',
|
|
642
|
+
'comparitor',
|
|
643
|
+
'bitwiseShift',
|
|
644
|
+
'bitwiseAnd',
|
|
645
|
+
'bitwiseXor',
|
|
646
|
+
'bitwiseOr',
|
|
647
|
+
'boolOpAnd',
|
|
648
|
+
'boolOpOr',
|
|
649
|
+
], (constants, type, part, res, expect, ctx) => {
|
|
650
|
+
const next = [expectTypes.inlineIf.types.inlineIf, inlineIfElse];
|
|
651
|
+
switch (type) {
|
|
652
|
+
case 'power':
|
|
653
|
+
// Right-associative: don't include power in next
|
|
654
|
+
break;
|
|
655
|
+
case 'opHigh':
|
|
656
|
+
// opHigh should NOT stop at power - let it be consumed by the right operand
|
|
657
|
+
next.push(expectTypes.splitter.types.opHigh);
|
|
658
|
+
case 'op':
|
|
659
|
+
next.push(expectTypes.splitter.types.op);
|
|
660
|
+
case 'comparitor':
|
|
661
|
+
next.push(expectTypes.splitter.types.comparitor);
|
|
662
|
+
case 'bitwiseShift':
|
|
663
|
+
next.push(expectTypes.splitter.types.bitwiseShift);
|
|
664
|
+
case 'bitwiseAnd':
|
|
665
|
+
next.push(expectTypes.splitter.types.bitwiseAnd);
|
|
666
|
+
case 'bitwiseXor':
|
|
667
|
+
next.push(expectTypes.splitter.types.bitwiseXor);
|
|
668
|
+
case 'bitwiseOr':
|
|
669
|
+
next.push(expectTypes.splitter.types.bitwiseOr);
|
|
670
|
+
case 'boolOpAnd':
|
|
671
|
+
next.push(expectTypes.splitter.types.boolOpAnd);
|
|
672
|
+
case 'boolOpOr':
|
|
673
|
+
next.push(expectTypes.splitter.types.boolOpOr);
|
|
674
|
+
}
|
|
675
|
+
const extract = restOfExp(constants, part.substring(res[0].length), next);
|
|
676
|
+
ctx.lispTree = lispify(constants, part.substring(extract.length + res[0].length), restOfExp.next, createLisp({
|
|
677
|
+
op: opTypes[res[0]],
|
|
678
|
+
a: ctx.lispTree,
|
|
679
|
+
b: lispify(constants, extract, expectTypes[expect].next),
|
|
680
|
+
}));
|
|
681
|
+
});
|
|
682
|
+
setLispType(['inlineIf'], (constants, type, part, res, expect, ctx) => {
|
|
683
|
+
let found = false;
|
|
684
|
+
const extract = part.substring(0, 0);
|
|
685
|
+
let quoteCount = 1;
|
|
686
|
+
while (!found && extract.length < part.length) {
|
|
687
|
+
extract.end = restOfExp(constants, part.substring(extract.length + 1), [
|
|
688
|
+
expectTypes.inlineIf.types.inlineIf,
|
|
689
|
+
inlineIfElse,
|
|
690
|
+
]).end;
|
|
691
|
+
if (part.char(extract.length) === '?') {
|
|
692
|
+
quoteCount++;
|
|
693
|
+
}
|
|
694
|
+
else {
|
|
695
|
+
quoteCount--;
|
|
696
|
+
}
|
|
697
|
+
if (!quoteCount) {
|
|
698
|
+
found = true;
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
extract.start = part.start + 1;
|
|
702
|
+
ctx.lispTree = createLisp({
|
|
703
|
+
op: 15 /* LispType.InlineIf */,
|
|
704
|
+
a: ctx.lispTree,
|
|
705
|
+
b: createLisp({
|
|
706
|
+
op: 16 /* LispType.InlineIfCase */,
|
|
707
|
+
a: lispifyExpr(constants, extract),
|
|
708
|
+
b: lispifyExpr(constants, part.substring(res[0].length + extract.length + 1)),
|
|
709
|
+
}),
|
|
710
|
+
});
|
|
711
|
+
});
|
|
712
|
+
function extractIfElse(constants, part) {
|
|
713
|
+
let count = 0;
|
|
714
|
+
let found = part.substring(0, 0);
|
|
715
|
+
let foundElse = emptyString;
|
|
716
|
+
let foundTrue;
|
|
717
|
+
let first = true;
|
|
718
|
+
let elseReg;
|
|
719
|
+
let details = {};
|
|
720
|
+
while ((found = restOfExp(constants, part.substring(found.end - part.start), [elseIf, ifElse, semiColon], undefined, undefined, undefined, details)).length ||
|
|
721
|
+
first) {
|
|
722
|
+
first = false;
|
|
723
|
+
const f = part.substring(found.end - part.start).toString();
|
|
724
|
+
if (f.startsWith('if')) {
|
|
725
|
+
found.end++;
|
|
726
|
+
count++;
|
|
727
|
+
}
|
|
728
|
+
else if (f.startsWith('else')) {
|
|
729
|
+
foundTrue = part.substring(0, found.end - part.start);
|
|
730
|
+
found.end++;
|
|
731
|
+
count--;
|
|
732
|
+
if (!count) {
|
|
733
|
+
found.end--;
|
|
734
|
+
}
|
|
735
|
+
}
|
|
736
|
+
else if ((elseReg = /^;?\s*else(?![\w$])/.exec(f))) {
|
|
737
|
+
foundTrue = part.substring(0, found.end - part.start);
|
|
738
|
+
found.end += elseReg[0].length - 1;
|
|
739
|
+
count--;
|
|
740
|
+
if (!count) {
|
|
741
|
+
found.end -= elseReg[0].length - 1;
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
else {
|
|
745
|
+
foundTrue = foundElse.length ? foundTrue : part.substring(0, found.end - part.start);
|
|
746
|
+
break;
|
|
747
|
+
}
|
|
748
|
+
if (!count) {
|
|
749
|
+
const ie = extractIfElse(constants, part.substring(found.end - part.start + (/^;?\s*else(?![\w$])/.exec(f)?.[0].length || 0)));
|
|
750
|
+
foundElse = ie.all;
|
|
751
|
+
break;
|
|
752
|
+
}
|
|
753
|
+
details = {};
|
|
754
|
+
}
|
|
755
|
+
foundTrue = foundTrue || part.substring(0, found.end - part.start);
|
|
756
|
+
return {
|
|
757
|
+
all: part.substring(0, Math.max(foundTrue.end, foundElse.end) - part.start),
|
|
758
|
+
true: foundTrue,
|
|
759
|
+
false: foundElse,
|
|
760
|
+
};
|
|
761
|
+
}
|
|
762
|
+
setLispType(['if'], (constants, type, part, res, expect, ctx) => {
|
|
763
|
+
let condition = restOfExp(constants, part.substring(res[0].length), [], '(');
|
|
764
|
+
const ie = extractIfElse(constants, part.substring(res[1].length));
|
|
765
|
+
const startTrue = res[0].length - res[1].length + condition.length + 1;
|
|
766
|
+
let trueBlock = ie.true.substring(startTrue);
|
|
767
|
+
let elseBlock = ie.false;
|
|
768
|
+
condition = condition.trim();
|
|
769
|
+
trueBlock = trueBlock.trim();
|
|
770
|
+
elseBlock = elseBlock.trim();
|
|
771
|
+
if (trueBlock.char(0) === '{')
|
|
772
|
+
trueBlock = trueBlock.slice(1, -1);
|
|
773
|
+
if (elseBlock.char(0) === '{')
|
|
774
|
+
elseBlock = elseBlock.slice(1, -1);
|
|
775
|
+
ctx.lispTree = createLisp({
|
|
776
|
+
op: 13 /* LispType.If */,
|
|
777
|
+
a: lispifyExpr(constants, condition),
|
|
778
|
+
b: createLisp({
|
|
779
|
+
op: 14 /* LispType.IfCase */,
|
|
780
|
+
a: lispifyBlock(trueBlock, constants),
|
|
781
|
+
b: lispifyBlock(elseBlock, constants),
|
|
782
|
+
}),
|
|
783
|
+
});
|
|
784
|
+
});
|
|
785
|
+
setLispType(['switch'], (constants, type, part, res, expect, ctx) => {
|
|
786
|
+
const test = restOfExp(constants, part.substring(res[0].length), [], '(');
|
|
787
|
+
let start = part.toString().indexOf('{', res[0].length + test.length + 1);
|
|
788
|
+
if (start === -1)
|
|
789
|
+
throw new SyntaxError('Invalid switch');
|
|
790
|
+
let statement = insertSemicolons(constants, restOfExp(constants, part.substring(start + 1), [], '{'));
|
|
791
|
+
let caseFound;
|
|
792
|
+
const caseTest = /^\s*(case\s|default)\s*/;
|
|
793
|
+
const cases = [];
|
|
794
|
+
let defaultFound = false;
|
|
795
|
+
while ((caseFound = caseTest.exec(statement.toString()))) {
|
|
796
|
+
if (caseFound[1] === 'default') {
|
|
797
|
+
if (defaultFound)
|
|
798
|
+
throw new SyntaxError('Only one default switch case allowed');
|
|
799
|
+
defaultFound = true;
|
|
800
|
+
}
|
|
801
|
+
const cond = restOfExp(constants, statement.substring(caseFound[0].length), [/^:/]);
|
|
802
|
+
let found = emptyString;
|
|
803
|
+
let i = (start = caseFound[0].length + cond.length + 1);
|
|
804
|
+
const bracketFound = /^\s*\{/.exec(statement.substring(i).toString());
|
|
805
|
+
let exprs = [];
|
|
806
|
+
if (bracketFound) {
|
|
807
|
+
i += bracketFound[0].length;
|
|
808
|
+
found = restOfExp(constants, statement.substring(i), [], '{');
|
|
809
|
+
i += found.length + 1;
|
|
810
|
+
exprs = lispifyBlock(found, constants);
|
|
811
|
+
}
|
|
812
|
+
else {
|
|
813
|
+
const notEmpty = restOfExp(constants, statement.substring(i), [caseTest]);
|
|
814
|
+
if (!notEmpty.trim().length) {
|
|
815
|
+
exprs = [];
|
|
816
|
+
i += notEmpty.length;
|
|
817
|
+
}
|
|
818
|
+
else {
|
|
819
|
+
while ((found = restOfExp(constants, statement.substring(i), [semiColon])).length) {
|
|
820
|
+
i += found.length + (statement.char(i + found.length) === ';' ? 1 : 0);
|
|
821
|
+
if (caseTest.test(statement.substring(i).toString())) {
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
exprs = lispifyBlock(statement.substring(start, found.end - statement.start), constants);
|
|
826
|
+
}
|
|
827
|
+
}
|
|
828
|
+
statement = statement.substring(i);
|
|
829
|
+
cases.push(createLisp({
|
|
830
|
+
op: 41 /* LispType.SwitchCase */,
|
|
831
|
+
a: caseFound[1] === 'default' ? 0 /* LispType.None */ : lispifyExpr(constants, cond),
|
|
832
|
+
b: exprs,
|
|
833
|
+
}));
|
|
834
|
+
}
|
|
835
|
+
ctx.lispTree = createLisp({
|
|
836
|
+
op: 40 /* LispType.Switch */,
|
|
837
|
+
a: lispifyExpr(constants, test),
|
|
838
|
+
b: cases,
|
|
839
|
+
});
|
|
840
|
+
});
|
|
841
|
+
setLispType(['dot', 'prop'], (constants, type, part, res, expect, ctx) => {
|
|
842
|
+
let prop = res[0];
|
|
843
|
+
let index = res[0].length;
|
|
844
|
+
let op = 'prop';
|
|
845
|
+
if (type === 'dot') {
|
|
846
|
+
if (res[1]) {
|
|
847
|
+
op = '?prop';
|
|
848
|
+
}
|
|
849
|
+
const matches = part.substring(res[0].length).toString().match(expectTypes.prop.types.prop);
|
|
850
|
+
if (matches && matches.length) {
|
|
851
|
+
prop = matches[0];
|
|
852
|
+
index = prop.length + res[0].length;
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
throw new SyntaxError('Hanging dot');
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
else {
|
|
859
|
+
if (reservedWords.has(prop) && prop !== 'this') {
|
|
860
|
+
throw new SyntaxError(`Unexpected token '${prop}'`);
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
ctx.lispTree = lispify(constants, part.substring(index), expectTypes[expect].next, createLisp({
|
|
864
|
+
op: typesCreate[op],
|
|
865
|
+
a: ctx.lispTree,
|
|
866
|
+
b: prop,
|
|
867
|
+
}));
|
|
868
|
+
});
|
|
869
|
+
setLispType(['spreadArray', 'spreadObject'], (constants, type, part, res, expect, ctx) => {
|
|
870
|
+
ctx.lispTree = createLisp({
|
|
871
|
+
op: type === 'spreadArray' ? 18 /* LispType.SpreadArray */ : 17 /* LispType.SpreadObject */,
|
|
872
|
+
a: 0 /* LispType.None */,
|
|
873
|
+
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next),
|
|
874
|
+
});
|
|
875
|
+
});
|
|
876
|
+
setLispType(['return', 'throw'], (constants, type, part, res, expect, ctx) => {
|
|
877
|
+
ctx.lispTree = createLisp({
|
|
878
|
+
op: type === 'return' ? 8 /* LispType.Return */ : 46 /* LispType.Throw */,
|
|
879
|
+
a: 0 /* LispType.None */,
|
|
880
|
+
b: lispifyExpr(constants, part.substring(res[0].length)),
|
|
881
|
+
});
|
|
882
|
+
});
|
|
883
|
+
setLispType(['number', 'boolean', 'null', 'und', 'NaN', 'Infinity'], (constants, type, part, res, expect, ctx) => {
|
|
884
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
885
|
+
op: type === 'number' ? (res[12] ? 83 /* LispType.BigInt */ : 7 /* LispType.Number */) : 35 /* LispType.GlobalSymbol */,
|
|
886
|
+
a: 0 /* LispType.None */,
|
|
887
|
+
b: res[12] ? res[1] : res[0],
|
|
888
|
+
}));
|
|
889
|
+
});
|
|
890
|
+
setLispType(['string', 'literal', 'regex'], (constants, type, part, res, expect, ctx) => {
|
|
891
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
892
|
+
op: type === 'string'
|
|
893
|
+
? 2 /* LispType.StringIndex */
|
|
894
|
+
: type === 'literal'
|
|
895
|
+
? 84 /* LispType.LiteralIndex */
|
|
896
|
+
: 85 /* LispType.RegexIndex */,
|
|
897
|
+
a: 0 /* LispType.None */,
|
|
898
|
+
b: res[1],
|
|
899
|
+
}));
|
|
900
|
+
});
|
|
901
|
+
setLispType(['initialize'], (constants, type, part, res, expect, ctx) => {
|
|
902
|
+
const lt = res[1] === 'var' ? 34 /* LispType.Var */ : res[1] === 'let' ? 3 /* LispType.Let */ : 4 /* LispType.Const */;
|
|
903
|
+
if (!res[3]) {
|
|
904
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length), expectTypes[expect].next, createLisp({
|
|
905
|
+
op: lt,
|
|
906
|
+
a: res[2],
|
|
907
|
+
b: 0 /* LispType.None */,
|
|
908
|
+
}));
|
|
909
|
+
}
|
|
910
|
+
else {
|
|
911
|
+
ctx.lispTree = createLisp({
|
|
912
|
+
op: lt,
|
|
913
|
+
a: res[2],
|
|
914
|
+
b: lispify(constants, part.substring(res[0].length), expectTypes[expect].next),
|
|
915
|
+
});
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
setLispType(['function', 'inlineFunction', 'arrowFunction', 'arrowFunctionSingle'], (constants, type, part, res, expect, ctx) => {
|
|
919
|
+
const isArrow = type !== 'function' && type !== 'inlineFunction';
|
|
920
|
+
const isReturn = isArrow && !res[res.length - 1];
|
|
921
|
+
const argPos = isArrow ? 2 : 3;
|
|
922
|
+
const isAsync = res[1] ? 88 /* LispType.True */ : 0 /* LispType.None */;
|
|
923
|
+
const args = res[argPos] ? res[argPos].replace(/\s+/g, '').split(/,/g) : [];
|
|
924
|
+
if (!isArrow) {
|
|
925
|
+
args.unshift((res[2] || '').trimStart());
|
|
926
|
+
}
|
|
927
|
+
let ended = false;
|
|
928
|
+
args.forEach((arg) => {
|
|
929
|
+
if (ended)
|
|
930
|
+
throw new SyntaxError('Rest parameter must be last formal parameter');
|
|
931
|
+
if (arg.startsWith('...'))
|
|
932
|
+
ended = true;
|
|
933
|
+
});
|
|
934
|
+
const f = restOfExp(constants, part.substring(res[0].length), !isReturn ? [/^}/] : [/^[,)}\]]/, semiColon]);
|
|
935
|
+
const func = isReturn ? 'return ' + f : f.toString();
|
|
936
|
+
args.forEach((arg) => {
|
|
937
|
+
if (reservedWords.has(arg.replace(/^\.\.\./, ''))) {
|
|
938
|
+
throw new SyntaxError(`Unexpected token '${arg}'`);
|
|
939
|
+
}
|
|
940
|
+
});
|
|
941
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length + func.length + 1), expectTypes[expect].next, createLisp({
|
|
942
|
+
op: isArrow
|
|
943
|
+
? 11 /* LispType.ArrowFunction */
|
|
944
|
+
: type === 'function'
|
|
945
|
+
? 37 /* LispType.Function */
|
|
946
|
+
: 10 /* LispType.InlineFunction */,
|
|
947
|
+
a: [isAsync, ...args],
|
|
948
|
+
b: constants.eager ? lispifyFunction(new CodeString(func), constants) : func,
|
|
949
|
+
}));
|
|
950
|
+
});
|
|
951
|
+
const iteratorRegex = /^((let|var|const)\s+)?\s*([a-zA-Z$_][a-zA-Z\d$_]*)\s+(in|of)(?![\w$])/;
|
|
952
|
+
setLispType(['for', 'do', 'while'], (constants, type, part, res, expect, ctx) => {
|
|
953
|
+
let i = 0;
|
|
954
|
+
let startStep = 88 /* LispType.True */;
|
|
955
|
+
let startInternal = [];
|
|
956
|
+
let getIterator = 0 /* LispType.None */;
|
|
957
|
+
let beforeStep = 0 /* LispType.None */;
|
|
958
|
+
let checkFirst = 88 /* LispType.True */;
|
|
959
|
+
let condition;
|
|
960
|
+
let step = 88 /* LispType.True */;
|
|
961
|
+
let body;
|
|
962
|
+
switch (type) {
|
|
963
|
+
case 'while': {
|
|
964
|
+
i = part.toString().indexOf('(') + 1;
|
|
965
|
+
const extract = restOfExp(constants, part.substring(i), [], '(');
|
|
966
|
+
condition = lispifyReturnExpr(constants, extract);
|
|
967
|
+
body = restOfExp(constants, part.substring(i + extract.length + 1)).trim();
|
|
968
|
+
if (body.char(0) === '{')
|
|
969
|
+
body = body.slice(1, -1);
|
|
970
|
+
break;
|
|
971
|
+
}
|
|
972
|
+
case 'for': {
|
|
973
|
+
i = part.toString().indexOf('(') + 1;
|
|
974
|
+
const args = [];
|
|
975
|
+
let extract2 = emptyString;
|
|
976
|
+
for (let k = 0; k < 3; k++) {
|
|
977
|
+
extract2 = restOfExp(constants, part.substring(i), [/^[;)]/]);
|
|
978
|
+
args.push(extract2.trim());
|
|
979
|
+
i += extract2.length + 1;
|
|
980
|
+
if (part.char(i - 1) === ')')
|
|
981
|
+
break;
|
|
982
|
+
}
|
|
983
|
+
let iterator;
|
|
984
|
+
if (args.length === 1 && (iterator = iteratorRegex.exec(args[0].toString()))) {
|
|
985
|
+
if (iterator[4] === 'of') {
|
|
986
|
+
((getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length))),
|
|
987
|
+
(startInternal = [ofStart2, ofStart3]));
|
|
988
|
+
condition = ofCondition;
|
|
989
|
+
step = ofStep;
|
|
990
|
+
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$next.value'), ['initialize']);
|
|
991
|
+
}
|
|
992
|
+
else {
|
|
993
|
+
((getIterator = lispifyReturnExpr(constants, args[0].substring(iterator[0].length))),
|
|
994
|
+
(startInternal = [inStart2, inStart3]));
|
|
995
|
+
step = inStep;
|
|
996
|
+
condition = inCondition;
|
|
997
|
+
beforeStep = lispify(constants, new CodeString((iterator[1] || 'let ') + iterator[3] + ' = $$keys[$$keyIndex]'), ['initialize']);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
else if (args.length === 3) {
|
|
1001
|
+
startStep = lispifyExpr(constants, args.shift(), startingExecpted);
|
|
1002
|
+
condition = lispifyReturnExpr(constants, args.shift());
|
|
1003
|
+
step = lispifyExpr(constants, args.shift());
|
|
1004
|
+
}
|
|
1005
|
+
else {
|
|
1006
|
+
throw new SyntaxError('Invalid for loop definition');
|
|
1007
|
+
}
|
|
1008
|
+
body = restOfExp(constants, part.substring(i)).trim();
|
|
1009
|
+
if (body.char(0) === '{')
|
|
1010
|
+
body = body.slice(1, -1);
|
|
1011
|
+
break;
|
|
1012
|
+
}
|
|
1013
|
+
case 'do': {
|
|
1014
|
+
checkFirst = 0 /* LispType.None */;
|
|
1015
|
+
const isBlock = !!res[3];
|
|
1016
|
+
body = restOfExp(constants, part.substring(res[0].length), isBlock ? [/^\}/] : [semiColon]);
|
|
1017
|
+
condition = lispifyReturnExpr(constants, restOfExp(constants, part.substring(part.toString().indexOf('(', res[0].length + body.length) + 1), [], '('));
|
|
1018
|
+
break;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
const a = [
|
|
1022
|
+
checkFirst,
|
|
1023
|
+
startInternal,
|
|
1024
|
+
getIterator,
|
|
1025
|
+
startStep,
|
|
1026
|
+
step,
|
|
1027
|
+
condition,
|
|
1028
|
+
beforeStep,
|
|
1029
|
+
];
|
|
1030
|
+
ctx.lispTree = createLisp({
|
|
1031
|
+
op: 38 /* LispType.Loop */,
|
|
1032
|
+
a,
|
|
1033
|
+
b: lispifyBlock(body, constants),
|
|
1034
|
+
});
|
|
1035
|
+
});
|
|
1036
|
+
setLispType(['block'], (constants, type, part, res, expect, ctx) => {
|
|
1037
|
+
ctx.lispTree = createLisp({
|
|
1038
|
+
op: 42 /* LispType.Block */,
|
|
1039
|
+
a: lispifyBlock(restOfExp(constants, part.substring(1), [], '{'), constants),
|
|
1040
|
+
b: 0 /* LispType.None */,
|
|
1041
|
+
});
|
|
1042
|
+
});
|
|
1043
|
+
setLispType(['loopAction'], (constants, type, part, res, expect, ctx) => {
|
|
1044
|
+
ctx.lispTree = createLisp({
|
|
1045
|
+
op: 86 /* LispType.LoopAction */,
|
|
1046
|
+
a: res[1],
|
|
1047
|
+
b: 0 /* LispType.None */,
|
|
1048
|
+
});
|
|
1049
|
+
});
|
|
1050
|
+
const catchReg = /^\s*(catch\s*(\(\s*([a-zA-Z$_][a-zA-Z\d$_]*)\s*\))?|finally)\s*\{/;
|
|
1051
|
+
setLispType(['try'], (constants, type, part, res, expect, ctx) => {
|
|
1052
|
+
const body = restOfExp(constants, part.substring(res[0].length), [], '{');
|
|
1053
|
+
let catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
1054
|
+
let finallyBody;
|
|
1055
|
+
let exception = '';
|
|
1056
|
+
let catchBody;
|
|
1057
|
+
let offset = 0;
|
|
1058
|
+
if (catchRes[1].startsWith('catch')) {
|
|
1059
|
+
catchRes = catchReg.exec(part.substring(res[0].length + body.length + 1).toString());
|
|
1060
|
+
exception = catchRes[3] || ''; // Use group 3 for the identifier, or empty string if optional catch binding
|
|
1061
|
+
catchBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], '{');
|
|
1062
|
+
offset = res[0].length + body.length + 1 + catchRes[0].length + catchBody.length + 1;
|
|
1063
|
+
if ((catchRes = catchReg.exec(part.substring(offset).toString())) &&
|
|
1064
|
+
catchRes[1].startsWith('finally')) {
|
|
1065
|
+
finallyBody = restOfExp(constants, part.substring(offset + catchRes[0].length), [], '{');
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
else {
|
|
1069
|
+
finallyBody = restOfExp(constants, part.substring(res[0].length + body.length + 1 + catchRes[0].length), [], '{');
|
|
1070
|
+
}
|
|
1071
|
+
const b = [
|
|
1072
|
+
exception,
|
|
1073
|
+
lispifyBlock(insertSemicolons(constants, catchBody || emptyString), constants),
|
|
1074
|
+
lispifyBlock(insertSemicolons(constants, finallyBody || emptyString), constants),
|
|
1075
|
+
];
|
|
1076
|
+
ctx.lispTree = createLisp({
|
|
1077
|
+
op: 39 /* LispType.Try */,
|
|
1078
|
+
a: lispifyBlock(insertSemicolons(constants, body), constants),
|
|
1079
|
+
b,
|
|
1080
|
+
});
|
|
1081
|
+
});
|
|
1082
|
+
setLispType(['void', 'await'], (constants, type, part, res, expect, ctx) => {
|
|
1083
|
+
const extract = restOfExp(constants, part.substring(res[0].length), [/^([^\s.?\w$]|\?[^.])/]);
|
|
1084
|
+
ctx.lispTree = lispify(constants, part.substring(res[0].length + extract.length), expectTypes[expect].next, createLisp({
|
|
1085
|
+
op: type === 'void' ? 87 /* LispType.Void */ : 44 /* LispType.Await */,
|
|
1086
|
+
a: lispify(constants, extract),
|
|
1087
|
+
b: 0 /* LispType.None */,
|
|
1088
|
+
}));
|
|
1089
|
+
});
|
|
1090
|
+
setLispType(['new'], (constants, type, part, res, expect, ctx) => {
|
|
1091
|
+
let i = res[0].length;
|
|
1092
|
+
const obj = restOfExp(constants, part.substring(i), [], undefined, '(');
|
|
1093
|
+
i += obj.length + 1;
|
|
1094
|
+
const args = [];
|
|
1095
|
+
if (part.char(i - 1) === '(') {
|
|
1096
|
+
const argsString = restOfExp(constants, part.substring(i), [], '(');
|
|
1097
|
+
i += argsString.length + 1;
|
|
1098
|
+
let found;
|
|
1099
|
+
let j = 0;
|
|
1100
|
+
while ((found = restOfExp(constants, argsString.substring(j), [/^,/])).length) {
|
|
1101
|
+
j += found.length + 1;
|
|
1102
|
+
args.push(found.trim());
|
|
1103
|
+
}
|
|
1104
|
+
}
|
|
1105
|
+
ctx.lispTree = lispify(constants, part.substring(i), expectTypes.expEdge.next, createLisp({
|
|
1106
|
+
op: 45 /* LispType.New */,
|
|
1107
|
+
a: lispify(constants, obj, expectTypes.initialize.next),
|
|
1108
|
+
b: args.map((arg) => lispify(constants, arg, expectTypes.initialize.next)),
|
|
1109
|
+
}));
|
|
1110
|
+
});
|
|
1111
|
+
const ofStart2 = lispify(undefined, new CodeString('let $$iterator = $$obj[Symbol.iterator]()'), ['initialize']);
|
|
1112
|
+
const ofStart3 = lispify(undefined, new CodeString('let $$next = $$iterator.next()'), [
|
|
1113
|
+
'initialize',
|
|
1114
|
+
]);
|
|
1115
|
+
const ofCondition = lispify(undefined, new CodeString('return !$$next.done'), [
|
|
1116
|
+
'initialize',
|
|
1117
|
+
]);
|
|
1118
|
+
const ofStep = lispify(undefined, new CodeString('$$next = $$iterator.next()'));
|
|
1119
|
+
const inStart2 = lispify(undefined, new CodeString('let $$keys = Object.keys($$obj)'), [
|
|
1120
|
+
'initialize',
|
|
1121
|
+
]);
|
|
1122
|
+
const inStart3 = lispify(undefined, new CodeString('let $$keyIndex = 0'), ['initialize']);
|
|
1123
|
+
const inStep = lispify(undefined, new CodeString('$$keyIndex++'));
|
|
1124
|
+
const inCondition = lispify(undefined, new CodeString('return $$keyIndex < $$keys.length'), [
|
|
1125
|
+
'initialize',
|
|
1126
|
+
]);
|
|
1127
|
+
function lispify(constants, part, expected, lispTree, topLevel = false) {
|
|
1128
|
+
lispTree = lispTree || NullLisp;
|
|
1129
|
+
expected = expected || expectTypes.initialize.next;
|
|
1130
|
+
if (part === undefined)
|
|
1131
|
+
return lispTree;
|
|
1132
|
+
part = part.trimStart();
|
|
1133
|
+
const str = part.toString();
|
|
1134
|
+
if (!part.length && !expected.includes('expEnd')) {
|
|
1135
|
+
throw new SyntaxError('Unexpected end of expression');
|
|
1136
|
+
}
|
|
1137
|
+
if (!part.length)
|
|
1138
|
+
return lispTree;
|
|
1139
|
+
const ctx = { lispTree: lispTree };
|
|
1140
|
+
let res;
|
|
1141
|
+
for (const expect of expected) {
|
|
1142
|
+
if (expect === 'expEnd') {
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
for (const type in expectTypes[expect].types) {
|
|
1146
|
+
if (type === 'expEnd') {
|
|
1147
|
+
continue;
|
|
1148
|
+
}
|
|
1149
|
+
if ((res = expectTypes[expect].types[type].exec(str))) {
|
|
1150
|
+
lastType = type;
|
|
1151
|
+
lastLastLastLastPart = lastLastLastPart;
|
|
1152
|
+
lastLastLastPart = lastLastPart;
|
|
1153
|
+
lastLastPart = lastPart;
|
|
1154
|
+
lastPart = part;
|
|
1155
|
+
try {
|
|
1156
|
+
lispTypes.get(type)?.(constants, type, part, res, expect, ctx);
|
|
1157
|
+
}
|
|
1158
|
+
catch (e) {
|
|
1159
|
+
if (topLevel && e instanceof SyntaxError) {
|
|
1160
|
+
throw new ParseError(e.message, str);
|
|
1161
|
+
}
|
|
1162
|
+
throw e;
|
|
1163
|
+
}
|
|
1164
|
+
break;
|
|
1165
|
+
}
|
|
1166
|
+
}
|
|
1167
|
+
if (res)
|
|
1168
|
+
break;
|
|
1169
|
+
}
|
|
1170
|
+
if (!res && part.length) {
|
|
1171
|
+
if (topLevel) {
|
|
1172
|
+
throw new ParseError(`Unexpected token after ${lastType}: ${part.char(0)}`, str);
|
|
1173
|
+
}
|
|
1174
|
+
throw new SyntaxError(`Unexpected token after ${lastType}: ${part.char(0)}`);
|
|
1175
|
+
}
|
|
1176
|
+
return ctx.lispTree;
|
|
1177
|
+
}
|
|
1178
|
+
const startingExpectedWithoutSingle = startingExecpted.filter((r) => r !== 'expSingle');
|
|
1179
|
+
function lispifyExpr(constants, str, expected) {
|
|
1180
|
+
if (!str.trimStart().length)
|
|
1181
|
+
return NullLisp;
|
|
1182
|
+
const subExpressions = [];
|
|
1183
|
+
let sub;
|
|
1184
|
+
let pos = 0;
|
|
1185
|
+
expected = expected || expectTypes.initialize.next;
|
|
1186
|
+
if (expected.includes('expSingle')) {
|
|
1187
|
+
if (testMultiple(str.toString(), Object.values(expectTypes.expSingle.types))) {
|
|
1188
|
+
return lispify(constants, str, ['expSingle'], undefined, true);
|
|
1189
|
+
}
|
|
1190
|
+
}
|
|
1191
|
+
if (expected === startingExecpted)
|
|
1192
|
+
expected = startingExpectedWithoutSingle;
|
|
1193
|
+
while ((sub = restOfExp(constants, str.substring(pos), [/^,/])).length) {
|
|
1194
|
+
subExpressions.push(sub.trimStart());
|
|
1195
|
+
pos += sub.length + 1;
|
|
1196
|
+
}
|
|
1197
|
+
if (subExpressions.length === 1) {
|
|
1198
|
+
return lispify(constants, str, expected, undefined, true);
|
|
1199
|
+
}
|
|
1200
|
+
if (expected.includes('initialize')) {
|
|
1201
|
+
const defined = expectTypes.initialize.types.initialize.exec(subExpressions[0].toString());
|
|
1202
|
+
if (defined) {
|
|
1203
|
+
return createLisp({
|
|
1204
|
+
op: 42 /* LispType.Block */,
|
|
1205
|
+
a: subExpressions.map((str, i) => lispify(constants, i ? new CodeString(defined[1] + ' ' + str) : str, ['initialize'], undefined, true)),
|
|
1206
|
+
b: 0 /* LispType.None */,
|
|
1207
|
+
});
|
|
1208
|
+
}
|
|
1209
|
+
else if (expectTypes.initialize.types.return.exec(subExpressions[0].toString())) {
|
|
1210
|
+
return lispify(constants, str, expected, undefined, true);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
const exprs = subExpressions.map((str) => lispify(constants, str, expected, undefined, true));
|
|
1214
|
+
return createLisp({ op: 43 /* LispType.Expression */, a: exprs, b: 0 /* LispType.None */ });
|
|
1215
|
+
}
|
|
1216
|
+
export function lispifyReturnExpr(constants, str) {
|
|
1217
|
+
return createLisp({
|
|
1218
|
+
op: 8 /* LispType.Return */,
|
|
1219
|
+
a: 0 /* LispType.None */,
|
|
1220
|
+
b: lispifyExpr(constants, str),
|
|
1221
|
+
});
|
|
1222
|
+
}
|
|
1223
|
+
export function lispifyBlock(str, constants, expression = false) {
|
|
1224
|
+
str = insertSemicolons(constants, str);
|
|
1225
|
+
if (!str.trim().length)
|
|
1226
|
+
return [];
|
|
1227
|
+
const parts = [];
|
|
1228
|
+
let part;
|
|
1229
|
+
let pos = 0;
|
|
1230
|
+
let start = 0;
|
|
1231
|
+
let details = {};
|
|
1232
|
+
let skipped = false;
|
|
1233
|
+
let isInserted = false;
|
|
1234
|
+
while ((part = restOfExp(constants, str.substring(pos), [semiColon], undefined, undefined, undefined, details)).length) {
|
|
1235
|
+
isInserted = !!(str.char(pos + part.length) && str.char(pos + part.length) !== ';');
|
|
1236
|
+
pos += part.length + (isInserted ? 0 : 1);
|
|
1237
|
+
if (/^\s*else(?![\w$])/.test(str.substring(pos).toString())) {
|
|
1238
|
+
skipped = true;
|
|
1239
|
+
}
|
|
1240
|
+
else if (details['words']?.includes('do') &&
|
|
1241
|
+
/^\s*while(?![\w$])/.test(str.substring(pos).toString())) {
|
|
1242
|
+
skipped = true;
|
|
1243
|
+
}
|
|
1244
|
+
else {
|
|
1245
|
+
skipped = false;
|
|
1246
|
+
parts.push(str.substring(start, pos - (isInserted ? 0 : 1)));
|
|
1247
|
+
start = pos;
|
|
1248
|
+
}
|
|
1249
|
+
details = {};
|
|
1250
|
+
if (expression)
|
|
1251
|
+
break;
|
|
1252
|
+
}
|
|
1253
|
+
if (skipped) {
|
|
1254
|
+
parts.push(str.substring(start, pos - (isInserted ? 0 : 1)));
|
|
1255
|
+
}
|
|
1256
|
+
return parts
|
|
1257
|
+
.map((str) => str.trimStart())
|
|
1258
|
+
.filter((str) => str.length)
|
|
1259
|
+
.map((str) => {
|
|
1260
|
+
return lispifyExpr(constants, str.trimStart(), startingExecpted);
|
|
1261
|
+
});
|
|
1262
|
+
}
|
|
1263
|
+
export function lispifyFunction(str, constants, expression = false) {
|
|
1264
|
+
if (!str.trim().length)
|
|
1265
|
+
return [];
|
|
1266
|
+
const tree = lispifyBlock(str, constants, expression);
|
|
1267
|
+
hoist(tree);
|
|
1268
|
+
return tree;
|
|
1269
|
+
}
|
|
1270
|
+
function hoist(item, res = []) {
|
|
1271
|
+
if (isLisp(item)) {
|
|
1272
|
+
if (!isLisp(item))
|
|
1273
|
+
return false;
|
|
1274
|
+
const [op, a, b] = item;
|
|
1275
|
+
if (op === 39 /* LispType.Try */ ||
|
|
1276
|
+
op === 13 /* LispType.If */ ||
|
|
1277
|
+
op === 38 /* LispType.Loop */ ||
|
|
1278
|
+
op === 40 /* LispType.Switch */) {
|
|
1279
|
+
hoist(a, res);
|
|
1280
|
+
hoist(b, res);
|
|
1281
|
+
}
|
|
1282
|
+
else if (op === 34 /* LispType.Var */) {
|
|
1283
|
+
res.push(createLisp({ op: 34 /* LispType.Var */, a: a, b: 0 /* LispType.None */ }));
|
|
1284
|
+
}
|
|
1285
|
+
else if (op === 37 /* LispType.Function */ && a[1]) {
|
|
1286
|
+
res.push(item);
|
|
1287
|
+
return true;
|
|
1288
|
+
}
|
|
1289
|
+
}
|
|
1290
|
+
else if (Array.isArray(item)) {
|
|
1291
|
+
const rep = [];
|
|
1292
|
+
for (const it of item) {
|
|
1293
|
+
if (!hoist(it, res)) {
|
|
1294
|
+
rep.push(it);
|
|
1295
|
+
}
|
|
1296
|
+
}
|
|
1297
|
+
if (rep.length !== item.length) {
|
|
1298
|
+
item.length = 0;
|
|
1299
|
+
item.push(...res, ...rep);
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
return false;
|
|
1303
|
+
}
|
|
1304
|
+
const closingsNoInsertion = /^(\})\s*(catch|finally|else|while|instanceof)(?![\w$])/;
|
|
1305
|
+
// \w|)|] \n \w = 2 // \} \w|\{ = 5
|
|
1306
|
+
const colonsRegex = /^((([\w$\])"'`]|\+\+|--)\s*\r?\n\s*([\w$+\-!~]))|(\}\s*[\w$!~+\-{("'`]))/;
|
|
1307
|
+
// if () \w \n; \w == \w \n \w | last === if a
|
|
1308
|
+
// if () { }; \w == \} ^else | last === if b
|
|
1309
|
+
// if () \w \n; else \n \w \n; == \w \n \w | last === else a
|
|
1310
|
+
// if () {} else {}; \w == \} \w | last === else b
|
|
1311
|
+
// while () \n \w \n; \w == \w \n \w | last === while a
|
|
1312
|
+
// while () { }; \w == \} \w | last === while b
|
|
1313
|
+
// do \w \n; while (); \w == \w \n while | last === do a
|
|
1314
|
+
// do { } while (); \w == \) \w | last === while c
|
|
1315
|
+
// try {} catch () {}; \w == \} \w | last === catch|finally b
|
|
1316
|
+
// \w \n; \w == \w \n \w | last === none a
|
|
1317
|
+
// cb() \n \w == \) \n \w | last === none a
|
|
1318
|
+
// obj[a] \n \w == \] \n \w | last === none a
|
|
1319
|
+
// {} {} == \} \{ | last === none b
|
|
1320
|
+
export function insertSemicolons(constants, str) {
|
|
1321
|
+
let rest = str;
|
|
1322
|
+
let sub = emptyString;
|
|
1323
|
+
let details = {};
|
|
1324
|
+
let pendingDoWhile = false;
|
|
1325
|
+
const inserted = insertedSemicolons.get(str.ref) || new Array(str.ref.str.length);
|
|
1326
|
+
while ((sub = restOfExp(constants, rest, [], undefined, undefined, [colonsRegex], details)).length) {
|
|
1327
|
+
let valid = false;
|
|
1328
|
+
let part = sub;
|
|
1329
|
+
let edge = sub.length;
|
|
1330
|
+
if (details.regRes) {
|
|
1331
|
+
valid = true;
|
|
1332
|
+
const [, , a, , , b] = details.regRes;
|
|
1333
|
+
edge = details.regRes[3] === '++' || details.regRes[3] === '--' ? sub.length + 1 : sub.length;
|
|
1334
|
+
part = rest.substring(0, edge);
|
|
1335
|
+
if (b) {
|
|
1336
|
+
const res = closingsNoInsertion.exec(rest.substring(sub.length - 1).toString());
|
|
1337
|
+
if (res) {
|
|
1338
|
+
if (res[2] === 'while') {
|
|
1339
|
+
if (details.lastWord === 'do') {
|
|
1340
|
+
valid = false;
|
|
1341
|
+
pendingDoWhile = true;
|
|
1342
|
+
}
|
|
1343
|
+
else {
|
|
1344
|
+
valid = true;
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1347
|
+
else {
|
|
1348
|
+
valid = false;
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
else if (details.lastWord === 'function' &&
|
|
1352
|
+
details.regRes[5][0] === '}' &&
|
|
1353
|
+
details.regRes[5].slice(-1) === '(') {
|
|
1354
|
+
valid = false;
|
|
1355
|
+
}
|
|
1356
|
+
}
|
|
1357
|
+
else if (a) {
|
|
1358
|
+
if (pendingDoWhile && details.lastWord === 'while') {
|
|
1359
|
+
valid = true;
|
|
1360
|
+
pendingDoWhile = false;
|
|
1361
|
+
}
|
|
1362
|
+
else if (details.lastWord === 'if' ||
|
|
1363
|
+
details.lastWord === 'while' ||
|
|
1364
|
+
details.lastWord === 'for' ||
|
|
1365
|
+
details.lastWord === 'else') {
|
|
1366
|
+
valid = !!details.bodyContentAfterKeyword;
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1369
|
+
}
|
|
1370
|
+
if (valid) {
|
|
1371
|
+
inserted[part.end] = true;
|
|
1372
|
+
}
|
|
1373
|
+
rest = rest.substring(edge);
|
|
1374
|
+
details = {};
|
|
1375
|
+
}
|
|
1376
|
+
insertedSemicolons.set(str.ref, inserted);
|
|
1377
|
+
return str;
|
|
1378
|
+
}
|
|
1379
|
+
export function checkRegex(str) {
|
|
1380
|
+
let i = 1;
|
|
1381
|
+
let escape = false;
|
|
1382
|
+
let done = false;
|
|
1383
|
+
let cancel = false;
|
|
1384
|
+
while (i < str.length && !done && !cancel) {
|
|
1385
|
+
done = str[i] === '/' && !escape;
|
|
1386
|
+
escape = str[i] === '\\' && !escape;
|
|
1387
|
+
cancel = str[i] === '\n';
|
|
1388
|
+
i++;
|
|
1389
|
+
}
|
|
1390
|
+
const after = str.substring(i);
|
|
1391
|
+
cancel = cancel || !done || /^\s*\d/.test(after);
|
|
1392
|
+
if (cancel)
|
|
1393
|
+
return null;
|
|
1394
|
+
const flags = /^[a-z]*/.exec(after);
|
|
1395
|
+
if (/^\s+[\w$]/.test(str.substring(i + flags[0].length))) {
|
|
1396
|
+
return null;
|
|
1397
|
+
}
|
|
1398
|
+
return {
|
|
1399
|
+
regex: str.substring(1, i - 1),
|
|
1400
|
+
flags: (flags && flags[0]) || '',
|
|
1401
|
+
length: i + ((flags && flags[0].length) || 0),
|
|
1402
|
+
};
|
|
1403
|
+
}
|
|
1404
|
+
const notDivide = /(typeof|delete|instanceof|return|in|of|throw|new|void|do|if)$/;
|
|
1405
|
+
const possibleDivide = /^([\w$\])]|\+\+|--)[\s/]/;
|
|
1406
|
+
export function extractConstants(constants, str, currentEnclosure = '') {
|
|
1407
|
+
let quote;
|
|
1408
|
+
let extract = [];
|
|
1409
|
+
let escape = false;
|
|
1410
|
+
let regexFound;
|
|
1411
|
+
let comment = '';
|
|
1412
|
+
let commentStart = -1;
|
|
1413
|
+
let currJs = [];
|
|
1414
|
+
let char = '';
|
|
1415
|
+
const strRes = [];
|
|
1416
|
+
const enclosures = [];
|
|
1417
|
+
let isPossibleDivide = null;
|
|
1418
|
+
let i = 0;
|
|
1419
|
+
for (i = 0; i < str.length; i++) {
|
|
1420
|
+
char = str[i];
|
|
1421
|
+
if (comment) {
|
|
1422
|
+
if (char === comment) {
|
|
1423
|
+
if (comment === '*' && str[i + 1] === '/') {
|
|
1424
|
+
comment = '';
|
|
1425
|
+
i++;
|
|
1426
|
+
}
|
|
1427
|
+
else if (comment === '\n') {
|
|
1428
|
+
comment = '';
|
|
1429
|
+
strRes.push('\n');
|
|
1430
|
+
}
|
|
1431
|
+
}
|
|
1432
|
+
}
|
|
1433
|
+
else {
|
|
1434
|
+
if (escape) {
|
|
1435
|
+
escape = false;
|
|
1436
|
+
extract.push(char);
|
|
1437
|
+
continue;
|
|
1438
|
+
}
|
|
1439
|
+
if (quote) {
|
|
1440
|
+
if (quote === '`' && char === '$' && str[i + 1] === '{') {
|
|
1441
|
+
const skip = extractConstants(constants, str.substring(i + 2), '{');
|
|
1442
|
+
currJs.push(skip.str);
|
|
1443
|
+
extract.push('${', currJs.length - 1, `}`);
|
|
1444
|
+
i += skip.length + 2;
|
|
1445
|
+
}
|
|
1446
|
+
else if (quote === char) {
|
|
1447
|
+
if (quote === '`') {
|
|
1448
|
+
const li = createLisp({
|
|
1449
|
+
op: 36 /* LispType.Literal */,
|
|
1450
|
+
a: unraw(extract.join('')),
|
|
1451
|
+
b: [],
|
|
1452
|
+
});
|
|
1453
|
+
li.tempJsStrings = currJs;
|
|
1454
|
+
constants.literals.push(li);
|
|
1455
|
+
strRes.push(`\``, constants.literals.length - 1, `\``);
|
|
1456
|
+
}
|
|
1457
|
+
else {
|
|
1458
|
+
constants.strings.push(unraw(extract.join('')));
|
|
1459
|
+
strRes.push(`"`, constants.strings.length - 1, `"`);
|
|
1460
|
+
}
|
|
1461
|
+
quote = null;
|
|
1462
|
+
extract = [];
|
|
1463
|
+
}
|
|
1464
|
+
else {
|
|
1465
|
+
extract.push(char);
|
|
1466
|
+
}
|
|
1467
|
+
}
|
|
1468
|
+
else {
|
|
1469
|
+
if (char === "'" || char === '"' || char === '`') {
|
|
1470
|
+
currJs = [];
|
|
1471
|
+
quote = char;
|
|
1472
|
+
}
|
|
1473
|
+
else if (closings[currentEnclosure] === char && !enclosures.length) {
|
|
1474
|
+
return { str: strRes.join(''), length: i };
|
|
1475
|
+
}
|
|
1476
|
+
else if (closings[char]) {
|
|
1477
|
+
enclosures.push(char);
|
|
1478
|
+
strRes.push(char);
|
|
1479
|
+
}
|
|
1480
|
+
else if (closings[enclosures[enclosures.length - 1]] === char) {
|
|
1481
|
+
enclosures.pop();
|
|
1482
|
+
strRes.push(char);
|
|
1483
|
+
}
|
|
1484
|
+
else if (char === '/' && (str[i + 1] === '*' || str[i + 1] === '/')) {
|
|
1485
|
+
comment = str[i + 1] === '*' ? '*' : '\n';
|
|
1486
|
+
commentStart = i;
|
|
1487
|
+
}
|
|
1488
|
+
else if (char === '/' &&
|
|
1489
|
+
!isPossibleDivide &&
|
|
1490
|
+
(regexFound = checkRegex(str.substring(i)))) {
|
|
1491
|
+
constants.regexes.push(regexFound);
|
|
1492
|
+
strRes.push(`/`, constants.regexes.length - 1, `/r`);
|
|
1493
|
+
i += regexFound.length - 1;
|
|
1494
|
+
}
|
|
1495
|
+
else {
|
|
1496
|
+
strRes.push(char);
|
|
1497
|
+
}
|
|
1498
|
+
if (!isPossibleDivide || !space.test(char)) {
|
|
1499
|
+
if ((isPossibleDivide = possibleDivide.exec(str.substring(i)))) {
|
|
1500
|
+
if (notDivide.test(str.substring(0, i + isPossibleDivide[1].length))) {
|
|
1501
|
+
isPossibleDivide = null;
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
escape = !!(quote && char === '\\');
|
|
1507
|
+
}
|
|
1508
|
+
}
|
|
1509
|
+
if (comment) {
|
|
1510
|
+
if (comment === '*') {
|
|
1511
|
+
throw new SyntaxError(`Unclosed comment '/*': ${str.substring(commentStart)}`);
|
|
1512
|
+
}
|
|
1513
|
+
}
|
|
1514
|
+
return { str: strRes.join(''), length: i };
|
|
1515
|
+
}
|
|
1516
|
+
export default function parse(code, eager = false, expression = false) {
|
|
1517
|
+
if (typeof code !== 'string')
|
|
1518
|
+
throw new ParseError(`Cannot parse ${code}`, code);
|
|
1519
|
+
let str = ' ' + code;
|
|
1520
|
+
const constants = { strings: [], literals: [], regexes: [], eager };
|
|
1521
|
+
str = extractConstants(constants, str).str;
|
|
1522
|
+
for (const l of constants.literals) {
|
|
1523
|
+
l[2] = l.tempJsStrings.map((js) => lispifyExpr(constants, new CodeString(js)));
|
|
1524
|
+
delete l.tempJsStrings;
|
|
1525
|
+
}
|
|
1526
|
+
return { tree: lispifyFunction(new CodeString(str), constants, expression), constants };
|
|
1527
|
+
}
|