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