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