-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
lexer.ts
executable file
·306 lines (283 loc) · 7.73 KB
/
lexer.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
export enum ImportType {
/**
* A normal static using any syntax variations
* import .. from 'module'
*/
Static = 1,
/**
* A dynamic import expression `import(specifier)`
* or `import(specifier, opts)`
*/
Dynamic = 2,
/**
* An import.meta expression
*/
ImportMeta = 3,
/**
* A source phase import
* import source x from 'module'
*/
StaticSourcePhase = 4,
/**
* A dynamic source phase import
* import.source('module')
*/
DynamicSourcePhase = 5,
}
export interface ImportSpecifier {
/**
* Module name
*
* To handle escape sequences in specifier strings, the .n field of imported specifiers will be provided where possible.
*
* For dynamic import expressions, this field will be empty if not a valid JS string.
*
* @example
* const [imports1, exports1] = parse(String.raw`import './\u0061\u0062.js'`);
* imports1[0].n;
* // Returns "./ab.js"
*
* const [imports2, exports2] = parse(`import("./ab.js")`);
* imports2[0].n;
* // Returns "./ab.js"
*
* const [imports3, exports3] = parse(`import("./" + "ab.js")`);
* imports3[0].n;
* // Returns undefined
*/
readonly n: string | undefined;
/**
* Type of import statement
*/
readonly t: ImportType;
/**
* Start of module specifier
*
* @example
* const source = `import { a } from 'asdf'`;
* const [imports, exports] = parse(source);
* source.substring(imports[0].s, imports[0].e);
* // Returns "asdf"
*/
readonly s: number;
/**
* End of module specifier
*/
readonly e: number;
/**
* Start of import statement
*
* @example
* const source = `import { a } from 'asdf'`;
* const [imports, exports] = parse(source);
* source.substring(imports[0].ss, imports[0].se);
* // Returns "import { a } from 'asdf';"
*/
readonly ss: number;
/**
* End of import statement
*/
readonly se: number;
/**
* If this import keyword is a dynamic import, this is the start value.
* If this import keyword is a static import, this is -1.
* If this import keyword is an import.meta expresion, this is -2.
*/
readonly d: number;
/**
* If this import has an import assertion, this is the start value.
* Otherwise this is `-1`.
*/
readonly a: number;
}
export interface ExportSpecifier {
/**
* Exported name
*
* @example
* const source = `export default []`;
* const [imports, exports] = parse(source);
* exports[0].n;
* // Returns "default"
*
* @example
* const source = `export const asdf = 42`;
* const [imports, exports] = parse(source);
* exports[0].n;
* // Returns "asdf"
*/
readonly n: string;
/**
* Local name, or undefined.
*
* @example
* const source = `export default []`;
* const [imports, exports] = parse(source);
* exports[0].ln;
* // Returns undefined
*
* @example
* const asdf = 42;
* const source = `export { asdf as a }`;
* const [imports, exports] = parse(source);
* exports[0].ln;
* // Returns "asdf"
*/
readonly ln: string | undefined;
/**
* Start of exported name
*
* @example
* const source = `export default []`;
* const [imports, exports] = parse(source);
* source.substring(exports[0].s, exports[0].e);
* // Returns "default"
*
* @example
* const source = `export { 42 as asdf }`;
* const [imports, exports] = parse(source);
* source.substring(exports[0].s, exports[0].e);
* // Returns "asdf"
*/
readonly s: number;
/**
* End of exported name
*/
readonly e: number;
/**
* Start of local name, or -1.
*
* @example
* const asdf = 42;
* const source = `export { asdf as a }`;
* const [imports, exports] = parse(source);
* source.substring(exports[0].ls, exports[0].le);
* // Returns "asdf"
*/
readonly ls: number;
/**
* End of local name, or -1.
*/
readonly le: number;
}
export interface ParseError extends Error {
idx: number
}
const isLE = new Uint8Array(new Uint16Array([1]).buffer)[0] === 1;
/**
* Outputs the list of exports and locations of import specifiers,
* including dynamic import and import meta handling.
*
* @param source Source code to parser
* @param name Optional sourcename
* @returns Tuple contaning imports list and exports list.
*/
export function parse (source: string, name = '@'): readonly [
imports: ReadonlyArray<ImportSpecifier>,
exports: ReadonlyArray<ExportSpecifier>,
facade: boolean,
hasModuleSyntax: boolean
] {
if (!wasm)
// actually returns a promise if init hasn't resolved (not type safe).
// casting to avoid a breaking type change.
return init.then(() => parse(source)) as unknown as ReturnType<typeof parse>;
const len = source.length + 1;
// need 2 bytes per code point plus analysis space so we double again
const extraMem = (wasm.__heap_base.value || wasm.__heap_base) as number + len * 4 - wasm.memory.buffer.byteLength;
if (extraMem > 0)
wasm.memory.grow(Math.ceil(extraMem / 65536));
const addr = wasm.sa(len - 1);
(isLE ? copyLE : copyBE)(source, new Uint16Array(wasm.memory.buffer, addr, len));
if (!wasm.parse())
throw Object.assign(new Error(`Parse error ${name}:${source.slice(0, wasm.e()).split('\n').length}:${wasm.e() - source.lastIndexOf('\n', wasm.e() - 1)}`), { idx: wasm.e() });
const imports: ImportSpecifier[] = [], exports: ExportSpecifier[] = [];
while (wasm.ri()) {
const s = wasm.is(), e = wasm.ie(), t = wasm.it(), a = wasm.ai(), d = wasm.id(), ss = wasm.ss(), se = wasm.se();
let n;
if (wasm.ip())
n = decode(source.slice(d === -1 ? s - 1 : s, d === -1 ? e + 1 : e));
imports.push({ n, t, s, e, ss, se, d, a });
}
while (wasm.re()) {
const s = wasm.es(), e = wasm.ee(), ls = wasm.els(), le = wasm.ele();
const n = source.slice(s, e), ch = n[0];
const ln = ls < 0 ? undefined : source.slice(ls, le), lch = ln ? ln[0] : '';
exports.push({
s, e, ls, le,
n: (ch === '"' || ch === "'") ? decode(n) : n,
ln: (lch === '"' || lch === "'") ? decode(ln) : ln,
});
}
function decode (str: string | undefined) {
try {
return (0, eval)(str as string) // eval(undefined) -> undefined
}
catch (e) {}
}
return [imports, exports, !!wasm.f(), !!wasm.ms()];
}
function copyBE (src: string, outBuf16: Uint16Array) {
const len = src.length;
let i = 0;
while (i < len) {
const ch = src.charCodeAt(i);
outBuf16[i++] = (ch & 0xff) << 8 | ch >>> 8;
}
}
function copyLE (src: string, outBuf16: Uint16Array) {
const len = src.length;
let i = 0;
while (i < len)
outBuf16[i] = src.charCodeAt(i++);
}
let wasm: {
__heap_base: {value: number} | number & {value: undefined};
memory: WebAssembly.Memory;
parse(): boolean;
/** importType */
it(): number;
/** getAssertIndex */
ai(): number;
/** getErr */
e(): number;
/** getExportEnd */
ee(): number;
/** getExportLocalEnd */
ele(): number;
/** getExportLocalStart */
els(): number;
/** getExportStart */
es(): number;
/** facade */
f(): boolean;
/** hasModuleSyntax */
ms(): boolean;
/** getImportDynamic */
id(): number;
/** getImportEnd */
ie(): number;
/** getImportSafeString */
ip(): number;
/** getImportStart */
is(): number;
/** readExport */
re(): boolean;
/** readImport */
ri(): boolean;
/** allocateSource */
sa(utf16Len: number): number;
/** getImportStatementEnd */
se(): number;
/** getImportStatementStart */
ss(): number;
};
/**
* Wait for init to resolve before calling `parse`.
*/
export const init = WebAssembly.compile(
(binary => typeof Buffer !== 'undefined' ? Buffer.from(binary, 'base64') : Uint8Array.from(atob(binary), x => x.charCodeAt(0)))
('WASM_BINARY')
)
.then(WebAssembly.instantiate)
.then(({ exports }) => { wasm = exports as typeof wasm; });