@skillstech/thunderlang 1.3.0 → 1.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +87 -0
- package/dist/core.cjs +4868 -1068
- package/dist/index.cjs +3676 -189
- package/dist/learn.cjs +6444 -1275
- package/package.json +18 -1
- package/src/cli-home.mjs +1 -1
- package/src/cli.mjs +23 -0
- package/src/core.d.ts +45 -0
- package/src/core.mjs +15 -0
- package/src/emit.mjs +1 -1
- package/src/faithful.mjs +4 -1
- package/src/flow/access.mjs +1451 -0
- package/src/flow/ts-parse.mjs +772 -0
- package/src/index.d.ts +3 -0
- package/src/index.mjs +3 -0
- package/src/learn-lessons.mjs +145 -0
- package/src/learn.mjs +17 -0
- package/src/libraries.mjs +22 -1
- package/src/model.mjs +22 -1
- package/src/passport-cli.mjs +101 -0
- package/src/passport-fs.d.mts +9 -0
- package/src/passport-fs.mjs +180 -0
- package/src/passport.d.mts +46 -0
- package/src/passport.mjs +255 -0
- package/src/program.d.mts +31 -0
- package/src/program.mjs +14 -0
- package/src/react/reading.css +119 -0
- package/src/react/reading.d.mts +15 -0
- package/src/react/reading.mjs +87 -0
- package/src/read.mjs +37 -1
- package/src/reading-layout.d.mts +40 -0
- package/src/reading-layout.mjs +112 -0
- package/src/review.mjs +31 -3
- package/src/say-less.mjs +16 -2
- package/src/steps.mjs +13 -1
- package/src/things-langs.mjs +20 -3
- package/src/write-across-cli.mjs +143 -0
- package/src/write-across.mjs +178 -0
|
@@ -0,0 +1,772 @@
|
|
|
1
|
+
// A small TypeScript / JavaScript parser for access-flow analysis (src/flow/access.mjs).
|
|
2
|
+
//
|
|
3
|
+
// It covers what web handlers are written with (statements, expressions, functions, classes, objects, destructuring,
|
|
4
|
+
// async/await, optional chaining, template literals) and skips type annotations, generics, `as`/`satisfies`, and
|
|
5
|
+
// type-only declarations. It is not a full TypeScript parser: when it meets something it cannot parse it throws a
|
|
6
|
+
// ParseError with the line, and the caller says "Thunder cannot tell" for that file instead of guessing.
|
|
7
|
+
//
|
|
8
|
+
// Dependency-free and browser-safe, like the rest of the reader.
|
|
9
|
+
|
|
10
|
+
export class ParseError extends Error {
|
|
11
|
+
constructor(message, line) { super(message); this.line = line; }
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
// ── Tokens ───────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
15
|
+
|
|
16
|
+
const PUNCT = [
|
|
17
|
+
'>>>=', '...', '===', '!==', '**=', '<<=', '>>=', '>>>', '&&=', '||=', '??=',
|
|
18
|
+
'=>', '==', '!=', '<=', '>=', '&&', '||', '??', '?.', '++', '--', '+=', '-=', '*=', '/=', '%=', '&=', '|=', '^=', '**', '<<', '>>',
|
|
19
|
+
'{', '}', '(', ')', '[', ']', ';', ',', '<', '>', '+', '-', '*', '/', '%', '&', '|', '^', '!', '~', '?', ':', '=', '.', '@', '#',
|
|
20
|
+
];
|
|
21
|
+
const KEYWORDS_BEFORE_REGEX = new Set(['return', 'typeof', 'instanceof', 'in', 'of', 'new', 'delete', 'void', 'throw', 'case', 'do', 'else', 'yield', 'await']);
|
|
22
|
+
|
|
23
|
+
/** Tokens: { t: 'id'|'num'|'str'|'tpl'|'re'|'p', v, line, nl (a line break before it) }. Templates keep their parts. */
|
|
24
|
+
export function tokenize(src) {
|
|
25
|
+
const out = [];
|
|
26
|
+
let i = 0, line = 1, nl = false;
|
|
27
|
+
const n = src.length;
|
|
28
|
+
const prevAllowsRegex = () => {
|
|
29
|
+
const p = out[out.length - 1];
|
|
30
|
+
if (!p) return true;
|
|
31
|
+
if (p.t === 'num' || p.t === 'str' || p.t === 'tpl' || p.t === 're') return false;
|
|
32
|
+
if (p.t === 'id') return KEYWORDS_BEFORE_REGEX.has(p.v);
|
|
33
|
+
return !(p.v === ')' || p.v === ']' || p.v === '}');
|
|
34
|
+
};
|
|
35
|
+
const push = (t, v, extra) => { out.push({ t, v, line, nl, ...extra }); nl = false; };
|
|
36
|
+
while (i < n) {
|
|
37
|
+
const c = src[i];
|
|
38
|
+
if (c === '\n') { line++; nl = true; i++; continue; }
|
|
39
|
+
if (c === ' ' || c === '\t' || c === '\r' || c === '\f' || c === '\v' || c === '' || c === ' ') { i++; continue; }
|
|
40
|
+
if (c === '/' && src[i + 1] === '/') { while (i < n && src[i] !== '\n') i++; continue; }
|
|
41
|
+
if (c === '/' && src[i + 1] === '*') {
|
|
42
|
+
const end = src.indexOf('*/', i + 2);
|
|
43
|
+
const stop = end < 0 ? n : end + 2;
|
|
44
|
+
for (let k = i; k < stop; k++) if (src[k] === '\n') { line++; nl = true; }
|
|
45
|
+
i = stop;
|
|
46
|
+
continue;
|
|
47
|
+
}
|
|
48
|
+
if (/[A-Za-z_$À-]/.test(c)) {
|
|
49
|
+
let j = i + 1;
|
|
50
|
+
while (j < n && /[\w$À-]/.test(src[j])) j++;
|
|
51
|
+
push('id', src.slice(i, j));
|
|
52
|
+
i = j;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
if (/[0-9]/.test(c) || (c === '.' && /[0-9]/.test(src[i + 1] ?? ''))) {
|
|
56
|
+
let j = i + 1;
|
|
57
|
+
while (j < n && /[\w.]/.test(src[j])) { if ((src[j] === 'e' || src[j] === 'E') && (src[j + 1] === '-' || src[j + 1] === '+')) j++; j++; }
|
|
58
|
+
push('num', src.slice(i, j));
|
|
59
|
+
i = j;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (c === '"' || c === "'") {
|
|
63
|
+
let j = i + 1, v = '';
|
|
64
|
+
while (j < n && src[j] !== c) {
|
|
65
|
+
if (src[j] === '\\') { v += src[j + 1] ?? ''; j += 2; continue; }
|
|
66
|
+
if (src[j] === '\n') break;
|
|
67
|
+
v += src[j++];
|
|
68
|
+
}
|
|
69
|
+
push('str', v);
|
|
70
|
+
i = j + 1;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
if (c === '`') {
|
|
74
|
+
const startLine = line;
|
|
75
|
+
const parts = [];
|
|
76
|
+
let j = i + 1, text = '';
|
|
77
|
+
while (j < n && src[j] !== '`') {
|
|
78
|
+
if (src[j] === '\\') { text += src[j + 1] ?? ''; j += 2; continue; }
|
|
79
|
+
if (src[j] === '$' && src[j + 1] === '{') {
|
|
80
|
+
// Find the matching }, minding nested braces, strings and templates.
|
|
81
|
+
let depth = 1, k = j + 2;
|
|
82
|
+
while (k < n && depth) {
|
|
83
|
+
const ch = src[k];
|
|
84
|
+
if (ch === '{') depth++;
|
|
85
|
+
else if (ch === '}') depth--;
|
|
86
|
+
else if (ch === '"' || ch === "'" || ch === '`') {
|
|
87
|
+
const q = ch; k++;
|
|
88
|
+
while (k < n && src[k] !== q) { if (src[k] === '\\') k++; k++; }
|
|
89
|
+
} else if (ch === '\n') line++;
|
|
90
|
+
if (depth) k++;
|
|
91
|
+
}
|
|
92
|
+
parts.push({ text });
|
|
93
|
+
parts.push({ expr: src.slice(j + 2, k), line });
|
|
94
|
+
text = '';
|
|
95
|
+
j = k + 1;
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
if (src[j] === '\n') line++;
|
|
99
|
+
text += src[j++];
|
|
100
|
+
}
|
|
101
|
+
parts.push({ text });
|
|
102
|
+
out.push({ t: 'tpl', v: parts, line: startLine, nl });
|
|
103
|
+
nl = false;
|
|
104
|
+
i = j + 1;
|
|
105
|
+
continue;
|
|
106
|
+
}
|
|
107
|
+
if (c === '/' && prevAllowsRegex()) {
|
|
108
|
+
let j = i + 1, inClass = false;
|
|
109
|
+
while (j < n && src[j] !== '\n') {
|
|
110
|
+
if (src[j] === '\\') { j += 2; continue; }
|
|
111
|
+
if (src[j] === '[') inClass = true;
|
|
112
|
+
else if (src[j] === ']') inClass = false;
|
|
113
|
+
else if (src[j] === '/' && !inClass) break;
|
|
114
|
+
j++;
|
|
115
|
+
}
|
|
116
|
+
j++;
|
|
117
|
+
while (j < n && /[a-z]/i.test(src[j])) j++;
|
|
118
|
+
push('re', src.slice(i, j));
|
|
119
|
+
i = j;
|
|
120
|
+
continue;
|
|
121
|
+
}
|
|
122
|
+
const p = PUNCT.find((x) => src.startsWith(x, i));
|
|
123
|
+
if (!p) throw new ParseError(`an unexpected character "${c}"`, line);
|
|
124
|
+
push('p', p);
|
|
125
|
+
i += p.length;
|
|
126
|
+
}
|
|
127
|
+
out.push({ t: 'eof', v: '', line, nl: true });
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
// ── Parser ───────────────────────────────────────────────────────────────────────────────────────────────────────
|
|
132
|
+
//
|
|
133
|
+
// Nodes are plain objects with `type` and `line`. Statement types: Var, Function, Class, Return, If, Throw, Try,
|
|
134
|
+
// Block, Expr, Loop, Switch, Import, Export (wrapping), Empty. Expression types: Id, Lit, Tpl, Member, Call, New,
|
|
135
|
+
// Arrow (also function expressions), Obj, Arr, Bin, Logic, Unary, Assign, Cond, Await, Seq, Spread, Tagged, This.
|
|
136
|
+
|
|
137
|
+
export function parse(src) {
|
|
138
|
+
const toks = tokenize(src);
|
|
139
|
+
let pos = 0;
|
|
140
|
+
const peek = (k = 0) => toks[Math.min(pos + k, toks.length - 1)];
|
|
141
|
+
const at = (v, k = 0) => { const t = peek(k); return (t.t === 'p' || t.t === 'id') && t.v === v; };
|
|
142
|
+
const next = () => { if (pos >= toks.length - 1 && toks[pos]?.t === 'eof') throw new ParseError('the file ends in the middle of something', toks[toks.length - 1].line); return toks[pos++]; };
|
|
143
|
+
const fail = (what) => { throw new ParseError(`${what} (found "${typeof peek().v === 'string' ? peek().v : 'a template'}")`, peek().line); };
|
|
144
|
+
const eat = (v) => { if (!at(v)) fail(`expected "${v}"`); return next(); };
|
|
145
|
+
const opt = (v) => (at(v) ? next() : null);
|
|
146
|
+
const semi = () => { if (opt(';')) return; if (at('}') || peek().t === 'eof' || peek().nl) return; fail('expected the end of a statement'); };
|
|
147
|
+
|
|
148
|
+
// Skips balanced brackets starting at an opener.
|
|
149
|
+
const skipBalanced = () => {
|
|
150
|
+
const open = next().v;
|
|
151
|
+
const close = { '(': ')', '[': ']', '{': '}', '<': '>' }[open];
|
|
152
|
+
let depth = 1;
|
|
153
|
+
while (depth && peek().t !== 'eof') {
|
|
154
|
+
const t = next();
|
|
155
|
+
if (t.t !== 'p') continue;
|
|
156
|
+
if (t.v === open) depth++;
|
|
157
|
+
else if (t.v === close) depth--;
|
|
158
|
+
else if (open === '<' && t.v === '>>') depth -= 2;
|
|
159
|
+
else if (open === '<' && t.v === '>>>') depth -= 3;
|
|
160
|
+
else if (open === '<' && t.v === '=>') { /* arrow type inside generics */ }
|
|
161
|
+
}
|
|
162
|
+
};
|
|
163
|
+
|
|
164
|
+
// A type: skipped. Stops at a token that cannot continue a type, at depth 0.
|
|
165
|
+
const skipType = (stops = [',', ')', '=', ';', '{', '}', ']', '=>']) => {
|
|
166
|
+
let guard = 0;
|
|
167
|
+
const stopSet = new Set(stops);
|
|
168
|
+
// Arrow function types: (a: T) => U
|
|
169
|
+
for (;;) {
|
|
170
|
+
if (++guard > 10000) fail('a type Thunder cannot follow');
|
|
171
|
+
const t = peek();
|
|
172
|
+
if (t.t === 'eof') return;
|
|
173
|
+
if (t.t === 'p' && (t.v === '(' || t.v === '[' || t.v === '{' || t.v === '<')) {
|
|
174
|
+
if (t.v === '{' && stopSet.has('{') && guard > 1) return;
|
|
175
|
+
skipBalanced();
|
|
176
|
+
// (a: T) => U is a function type, unless => is where this type must stop (an arrow's return type).
|
|
177
|
+
if (at('=>') && t.v === '(' && !stopSet.has('=>')) { next(); continue; }
|
|
178
|
+
continue;
|
|
179
|
+
}
|
|
180
|
+
if (t.t === 'p' && (t.v === '|' || t.v === '&' || t.v === '.' || t.v === '?' || t.v === ':' && !stopSet.has(':'))) {
|
|
181
|
+
if (t.v === '?' && stopSet.has('?')) return;
|
|
182
|
+
next(); continue;
|
|
183
|
+
}
|
|
184
|
+
if (t.t === 'p' && t.v === '=>' ) { if (stopSet.has('=>')) return; next(); continue; }
|
|
185
|
+
if (t.t === 'id' || t.t === 'str' || t.t === 'num' || t.t === 'tpl') {
|
|
186
|
+
if (t.t === 'id' && guard > 1 && t.nl && !['extends', 'keyof', 'typeof', 'infer', 'is', 'readonly', 'unique'].includes(t.v)) {
|
|
187
|
+
const prev = toks[pos - 1];
|
|
188
|
+
if (!(prev.t === 'p' && (prev.v === '|' || prev.v === '&' || prev.v === ':' || prev.v === '.' || prev.v === '=>' || prev.v === '?'))) return;
|
|
189
|
+
}
|
|
190
|
+
next(); continue;
|
|
191
|
+
}
|
|
192
|
+
if (t.t === 'p' && t.v === '-' && peek(1).t === 'num') { next(); next(); continue; }
|
|
193
|
+
if (stopSet.has(t.v)) return;
|
|
194
|
+
return;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// ── Statements ──
|
|
199
|
+
const program = () => {
|
|
200
|
+
const body = [];
|
|
201
|
+
while (peek().t !== 'eof') { const s = statement(); if (s) body.push(s); }
|
|
202
|
+
return { type: 'Program', body };
|
|
203
|
+
};
|
|
204
|
+
|
|
205
|
+
const block = () => {
|
|
206
|
+
const line = eat('{').line;
|
|
207
|
+
const body = [];
|
|
208
|
+
while (!at('}')) { if (peek().t === 'eof') fail('expected "}"'); const s = statement(); if (s) body.push(s); }
|
|
209
|
+
next();
|
|
210
|
+
return { type: 'Block', body, line };
|
|
211
|
+
};
|
|
212
|
+
|
|
213
|
+
const isTypeDecl = () => (at('type') && peek(1).t === 'id' && (at('=', 2) || at('<', 2))) || (at('interface') && peek(1).t === 'id')
|
|
214
|
+
|| (at('declare') && !peek(1).nl) || (at('enum') && peek(1).t === 'id') || (at('const') && at('enum', 1))
|
|
215
|
+
|| (at('abstract') && at('class', 1)) || (at('namespace') && peek(1).t === 'id' && at('{', 2)) || (at('module') && peek(1).t === 'str');
|
|
216
|
+
|
|
217
|
+
const skipTypeDecl = () => {
|
|
218
|
+
const line = peek().line;
|
|
219
|
+
if (at('abstract')) { next(); return classDecl(); }
|
|
220
|
+
if (at('declare')) { next(); }
|
|
221
|
+
if (at('const')) next();
|
|
222
|
+
if (at('enum')) { next(); next(); skipBalanced(); return { type: 'Empty', line }; }
|
|
223
|
+
if (at('interface')) { next(); next(); if (at('<')) skipBalanced(); while (!at('{')) next(); skipBalanced(); return { type: 'Empty', line }; }
|
|
224
|
+
if (at('namespace') || at('module')) { next(); next(); skipBalanced(); return { type: 'Empty', line }; }
|
|
225
|
+
if (at('type')) { next(); next(); if (at('<')) skipBalanced(); eat('='); skipType([';']); semi(); return { type: 'Empty', line }; }
|
|
226
|
+
// declare function / const / class …: skip to the end of the statement.
|
|
227
|
+
while (!at(';') && !peek().nl && peek().t !== 'eof') { if (at('{') || at('(')) skipBalanced(); else next(); }
|
|
228
|
+
opt(';');
|
|
229
|
+
return { type: 'Empty', line };
|
|
230
|
+
};
|
|
231
|
+
|
|
232
|
+
const decorators = () => { while (at('@')) { next(); next(); while (at('.')) { next(); next(); } if (at('(')) skipBalanced(); } };
|
|
233
|
+
|
|
234
|
+
const statement = () => {
|
|
235
|
+
const t = peek();
|
|
236
|
+
const line = t.line;
|
|
237
|
+
decorators();
|
|
238
|
+
if (at(';')) { next(); return null; }
|
|
239
|
+
if (at('{')) return block();
|
|
240
|
+
if (isTypeDecl()) return skipTypeDecl();
|
|
241
|
+
if (at('import') && !at('(', 1) && !at('.', 1)) return importDecl();
|
|
242
|
+
if (at('export')) return exportDecl();
|
|
243
|
+
if (at('const') || at('let') || at('var') || (at('using') && peek(1).t === 'id' && !peek(1).nl)) { const d = varDecl(); semi(); return d; }
|
|
244
|
+
if (at('function') || (at('async') && at('function', 1) && !peek(1).nl)) return functionDecl();
|
|
245
|
+
if (at('class')) return classDecl();
|
|
246
|
+
if (at('return')) { next(); const arg = (at(';') || at('}') || peek().nl || peek().t === 'eof') ? null : expression(); semi(); return { type: 'Return', arg, line }; }
|
|
247
|
+
if (at('throw')) { next(); const arg = expression(); semi(); return { type: 'Throw', arg, line }; }
|
|
248
|
+
if (at('if')) {
|
|
249
|
+
next(); eat('('); const test = expression(); eat(')');
|
|
250
|
+
const cons = statement();
|
|
251
|
+
const alt = opt('else') ? statement() : null;
|
|
252
|
+
return { type: 'If', test, cons, alt, line };
|
|
253
|
+
}
|
|
254
|
+
if (at('try')) {
|
|
255
|
+
next(); const body = block();
|
|
256
|
+
let param = null, handler = null, finalizer = null;
|
|
257
|
+
if (opt('catch')) { if (opt('(')) { param = pattern(); if (opt(':')) skipType([')']); eat(')'); } handler = block(); }
|
|
258
|
+
if (opt('finally')) finalizer = block();
|
|
259
|
+
return { type: 'Try', body, param, handler, finalizer, line };
|
|
260
|
+
}
|
|
261
|
+
if (at('for')) {
|
|
262
|
+
next(); opt('await'); eat('(');
|
|
263
|
+
let init = null, iter = null, kind = 'for';
|
|
264
|
+
if (at('const') || at('let') || at('var')) {
|
|
265
|
+
const k = next().v;
|
|
266
|
+
const id = pattern();
|
|
267
|
+
if (opt(':')) skipType([';', '=', 'of', 'in']);
|
|
268
|
+
if (at('of') || at('in')) { kind = next().v; iter = expression(); init = { type: 'Var', kind: k, decls: [{ id, init: null, line }], line }; }
|
|
269
|
+
else {
|
|
270
|
+
const decls = [{ id, init: opt('=') ? assign() : null, line }];
|
|
271
|
+
while (opt(',')) { const id2 = pattern(); decls.push({ id: id2, init: opt('=') ? assign() : null, line }); }
|
|
272
|
+
init = { type: 'Var', kind: k, decls, line };
|
|
273
|
+
}
|
|
274
|
+
} else if (!at(';')) {
|
|
275
|
+
const e = expression(true);
|
|
276
|
+
if (at('of') || at('in')) { kind = next().v; iter = expression(); init = { type: 'Expr', expr: e, line }; } else init = { type: 'Expr', expr: e, line };
|
|
277
|
+
}
|
|
278
|
+
let test = null, update = null;
|
|
279
|
+
if (kind === 'for') { eat(';'); if (!at(';')) test = expression(); eat(';'); if (!at(')')) update = expression(); }
|
|
280
|
+
eat(')');
|
|
281
|
+
const body = statement();
|
|
282
|
+
return { type: 'Loop', kind, init, iter, test, update, body, line };
|
|
283
|
+
}
|
|
284
|
+
if (at('while')) { next(); eat('('); const test = expression(); eat(')'); const body = statement(); return { type: 'Loop', kind: 'while', test, body, line }; }
|
|
285
|
+
if (at('do')) { next(); const body = statement(); eat('while'); eat('('); const test = expression(); eat(')'); opt(';'); return { type: 'Loop', kind: 'do', test, body, line }; }
|
|
286
|
+
if (at('switch')) {
|
|
287
|
+
next(); eat('('); const disc = expression(); eat(')'); eat('{');
|
|
288
|
+
const cases = [];
|
|
289
|
+
while (!at('}')) {
|
|
290
|
+
let test = null;
|
|
291
|
+
if (opt('default')) eat(':'); else { eat('case'); test = expression(); eat(':'); }
|
|
292
|
+
const body = [];
|
|
293
|
+
while (!at('case') && !at('default') && !at('}')) { const s = statement(); if (s) body.push(s); }
|
|
294
|
+
cases.push({ test, body });
|
|
295
|
+
}
|
|
296
|
+
next();
|
|
297
|
+
return { type: 'Switch', disc, cases, line };
|
|
298
|
+
}
|
|
299
|
+
if ((at('break') || at('continue'))) { next(); if (peek().t === 'id' && !peek().nl) next(); semi(); return { type: 'Jump', line }; }
|
|
300
|
+
if (t.t === 'id' && at(':', 1) && !['default', 'case'].includes(t.v)) { next(); next(); return statement(); }
|
|
301
|
+
const expr = expression();
|
|
302
|
+
semi();
|
|
303
|
+
return { type: 'Expr', expr, line };
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const importDecl = () => {
|
|
307
|
+
const line = next().line;
|
|
308
|
+
if (at('type') && !at(',', 1) && !at('from', 1)) next();
|
|
309
|
+
const names = [];
|
|
310
|
+
if (peek().t === 'str') { next(); semi(); return { type: 'Import', from: null, names, line }; }
|
|
311
|
+
if (peek().t === 'id' && !at('{') && !at('*')) { names.push({ imported: 'default', local: next().v }); opt(','); }
|
|
312
|
+
if (opt('*')) { eat('as'); names.push({ imported: '*', local: next().v }); }
|
|
313
|
+
if (opt('{')) {
|
|
314
|
+
while (!at('}')) {
|
|
315
|
+
if (at('type') && peek(1).t === 'id' && !at(',', 1) && !at('}', 1) && !at('as', 1)) next();
|
|
316
|
+
const imported = next().v;
|
|
317
|
+
const local = opt('as') ? next().v : imported;
|
|
318
|
+
names.push({ imported, local });
|
|
319
|
+
if (!opt(',')) break;
|
|
320
|
+
}
|
|
321
|
+
eat('}');
|
|
322
|
+
}
|
|
323
|
+
eat('from');
|
|
324
|
+
const from = next().v;
|
|
325
|
+
if (at('assert') || at('with')) { next(); skipBalanced(); }
|
|
326
|
+
semi();
|
|
327
|
+
return { type: 'Import', from, names, line };
|
|
328
|
+
};
|
|
329
|
+
|
|
330
|
+
const exportDecl = () => {
|
|
331
|
+
const line = next().line;
|
|
332
|
+
if (at('type') && (at('{', 1) || at('*', 1))) next();
|
|
333
|
+
if (opt('default')) {
|
|
334
|
+
if (at('function') || (at('async') && at('function', 1))) { const f = functionDecl(true); return { type: 'Export', decl: f, isDefault: true, line }; }
|
|
335
|
+
if (at('class')) { const c = classDecl(true); return { type: 'Export', decl: c, isDefault: true, line }; }
|
|
336
|
+
const expr = assign(); semi();
|
|
337
|
+
return { type: 'Export', expr, isDefault: true, line };
|
|
338
|
+
}
|
|
339
|
+
if (at('{') || at('*')) {
|
|
340
|
+
const names = [];
|
|
341
|
+
if (opt('*')) { if (opt('as')) names.push({ local: '*', exported: next().v }); }
|
|
342
|
+
else {
|
|
343
|
+
next();
|
|
344
|
+
while (!at('}')) { if (at('type') && peek(1).t === 'id' && !at(',', 1) && !at('}', 1)) next(); const local = next().v; const exported = opt('as') ? next().v : local; names.push({ local, exported }); if (!opt(',')) break; }
|
|
345
|
+
eat('}');
|
|
346
|
+
}
|
|
347
|
+
const from = opt('from') ? next().v : null;
|
|
348
|
+
semi();
|
|
349
|
+
return { type: 'Export', names, from, line };
|
|
350
|
+
}
|
|
351
|
+
if (at('=')) { next(); assign(); semi(); return { type: 'Empty', line }; }
|
|
352
|
+
const decl = statement();
|
|
353
|
+
return { type: 'Export', decl, line };
|
|
354
|
+
};
|
|
355
|
+
|
|
356
|
+
const varDecl = () => {
|
|
357
|
+
const line = peek().line;
|
|
358
|
+
const kind = next().v;
|
|
359
|
+
const decls = [];
|
|
360
|
+
do {
|
|
361
|
+
const dline = peek().line;
|
|
362
|
+
const id = pattern();
|
|
363
|
+
if (opt('!')) { /* definite assignment */ }
|
|
364
|
+
if (opt(':')) skipType([',', '=', ';', '}']);
|
|
365
|
+
const init = opt('=') ? assign() : null;
|
|
366
|
+
decls.push({ id, init, line: dline });
|
|
367
|
+
} while (opt(','));
|
|
368
|
+
return { type: 'Var', kind, decls, line };
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// Binding patterns: identifiers, { a, b: c, ...rest }, [a, , b], with defaults.
|
|
372
|
+
const pattern = () => {
|
|
373
|
+
const line = peek().line;
|
|
374
|
+
if (at('{')) {
|
|
375
|
+
next();
|
|
376
|
+
const props = [];
|
|
377
|
+
while (!at('}')) {
|
|
378
|
+
if (opt('...')) { props.push({ rest: pattern() }); opt(','); continue; }
|
|
379
|
+
let key;
|
|
380
|
+
if (at('[')) { next(); key = { computed: expression() }; eat(']'); } else key = next().v;
|
|
381
|
+
let value;
|
|
382
|
+
if (opt(':')) value = pattern(); else value = { type: 'Id', name: typeof key === 'string' ? key : '?', line };
|
|
383
|
+
if (opt('=')) value = { type: 'Default', target: value, value: assign(), line };
|
|
384
|
+
props.push({ key, value });
|
|
385
|
+
if (!opt(',')) break;
|
|
386
|
+
}
|
|
387
|
+
eat('}');
|
|
388
|
+
return { type: 'ObjPat', props, line };
|
|
389
|
+
}
|
|
390
|
+
if (at('[')) {
|
|
391
|
+
next();
|
|
392
|
+
const elems = [];
|
|
393
|
+
while (!at(']')) {
|
|
394
|
+
if (at(',')) { next(); elems.push(null); continue; }
|
|
395
|
+
if (opt('...')) { elems.push({ type: 'Rest', arg: pattern(), line }); opt(','); continue; }
|
|
396
|
+
let p = pattern();
|
|
397
|
+
if (opt('=')) p = { type: 'Default', target: p, value: assign(), line };
|
|
398
|
+
elems.push(p);
|
|
399
|
+
if (!opt(',')) break;
|
|
400
|
+
}
|
|
401
|
+
eat(']');
|
|
402
|
+
return { type: 'ArrPat', elems, line };
|
|
403
|
+
}
|
|
404
|
+
const t = next();
|
|
405
|
+
if (t.t !== 'id') { pos--; fail('expected a name'); }
|
|
406
|
+
return { type: 'Id', name: t.v, line: t.line };
|
|
407
|
+
};
|
|
408
|
+
|
|
409
|
+
const params = () => {
|
|
410
|
+
eat('(');
|
|
411
|
+
const ps = [];
|
|
412
|
+
while (!at(')')) {
|
|
413
|
+
decorators();
|
|
414
|
+
while (['public', 'private', 'protected', 'readonly', 'override'].includes(peek().v) && peek(1).t === 'id') next();
|
|
415
|
+
if (opt('...')) { const p = pattern(); if (opt('?')) {} if (opt(':')) skipType([',', ')', '=']); ps.push({ type: 'Rest', arg: p }); opt(','); continue; }
|
|
416
|
+
let p = pattern();
|
|
417
|
+
opt('?');
|
|
418
|
+
if (opt(':')) skipType([',', ')', '=']);
|
|
419
|
+
if (opt('=')) p = { type: 'Default', target: p, value: assign(), line: p.line };
|
|
420
|
+
ps.push(p);
|
|
421
|
+
if (!opt(',')) break;
|
|
422
|
+
}
|
|
423
|
+
eat(')');
|
|
424
|
+
return ps;
|
|
425
|
+
};
|
|
426
|
+
|
|
427
|
+
const functionBody = () => (at('{') ? block() : null);
|
|
428
|
+
|
|
429
|
+
const functionDecl = (allowAnonymous = false) => {
|
|
430
|
+
const line = peek().line;
|
|
431
|
+
const isAsync = !!opt('async');
|
|
432
|
+
eat('function');
|
|
433
|
+
opt('*');
|
|
434
|
+
let name = null;
|
|
435
|
+
if (peek().t === 'id' && !at('(')) name = next().v;
|
|
436
|
+
else if (!allowAnonymous) fail('expected a function name');
|
|
437
|
+
if (at('<')) skipBalanced();
|
|
438
|
+
const ps = params();
|
|
439
|
+
if (opt(':')) skipType(['{', ';']);
|
|
440
|
+
const body = functionBody();
|
|
441
|
+
if (!body) { opt(';'); return { type: 'Empty', line }; }
|
|
442
|
+
return { type: 'Function', name, params: ps, body, isAsync, line };
|
|
443
|
+
};
|
|
444
|
+
|
|
445
|
+
const classDecl = (allowAnonymous = false) => {
|
|
446
|
+
const line = eat('class').line;
|
|
447
|
+
let name = null;
|
|
448
|
+
if (peek().t === 'id' && !at('extends') && !at('implements') && !at('{')) name = next().v;
|
|
449
|
+
else if (!allowAnonymous) { /* class expressions */ }
|
|
450
|
+
if (at('<')) skipBalanced();
|
|
451
|
+
let superClass = null;
|
|
452
|
+
if (opt('extends')) { superClass = postfix(primary()); if (at('<')) skipBalanced(); }
|
|
453
|
+
if (opt('implements')) { while (!at('{')) next(); }
|
|
454
|
+
eat('{');
|
|
455
|
+
const members = [];
|
|
456
|
+
while (!at('}')) {
|
|
457
|
+
if (opt(';')) continue;
|
|
458
|
+
decorators();
|
|
459
|
+
let isStatic = false;
|
|
460
|
+
const mline = peek().line;
|
|
461
|
+
while (['public', 'private', 'protected', 'readonly', 'static', 'abstract', 'override', 'declare', 'accessor'].includes(peek().v) && !at('(', 1) && !at('=', 1) && !at(':', 1) && !at(';', 1) && !at('?', 1)) { if (next().v === 'static') isStatic = true; }
|
|
462
|
+
if (at('static') && at('{', 1)) { next(); block(); continue; }
|
|
463
|
+
const isAsync = at('async') && !at('(', 1) && !peek(1).nl ? !!next() : false;
|
|
464
|
+
opt('*');
|
|
465
|
+
let kind = 'method';
|
|
466
|
+
if ((at('get') || at('set')) && !at('(', 1) && !at('=', 1) && !at(':', 1)) { kind = next().v; }
|
|
467
|
+
let key;
|
|
468
|
+
if (at('[')) { next(); expression(); eat(']'); key = '[computed]'; }
|
|
469
|
+
else if (at('#')) { next(); key = '#' + next().v; }
|
|
470
|
+
else key = next().v;
|
|
471
|
+
opt('?'); opt('!');
|
|
472
|
+
if (at('(') || at('<')) {
|
|
473
|
+
if (at('<')) skipBalanced();
|
|
474
|
+
const ps = params();
|
|
475
|
+
if (opt(':')) skipType(['{', ';']);
|
|
476
|
+
const body = functionBody();
|
|
477
|
+
if (!body) { opt(';'); continue; }
|
|
478
|
+
members.push({ kind, key, isStatic, fn: { type: 'Arrow', params: ps, body, isAsync, line: mline, isMethod: true, name: key }, line: mline });
|
|
479
|
+
} else {
|
|
480
|
+
if (opt(':')) skipType(['=', ';', '}']);
|
|
481
|
+
const value = opt('=') ? assign() : null;
|
|
482
|
+
semi();
|
|
483
|
+
members.push({ kind: 'field', key, isStatic, value, line: mline });
|
|
484
|
+
}
|
|
485
|
+
}
|
|
486
|
+
eat('}');
|
|
487
|
+
return { type: 'Class', name, superClass, members, line };
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
// ── Expressions ──
|
|
491
|
+
const expression = (noIn = false) => {
|
|
492
|
+
const line = peek().line;
|
|
493
|
+
const first = assign(noIn);
|
|
494
|
+
if (!at(',')) return first;
|
|
495
|
+
const list = [first];
|
|
496
|
+
while (opt(',')) list.push(assign(noIn));
|
|
497
|
+
return { type: 'Seq', list, line };
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
// Arrow function lookahead from "(" or "<": a balanced group followed by => (possibly after a return type).
|
|
501
|
+
const isArrowAhead = () => {
|
|
502
|
+
let k = pos;
|
|
503
|
+
if (toks[k].v === 'async' && toks[k].t === 'id') k++;
|
|
504
|
+
if (toks[k].t === 'p' && toks[k].v === '<') {
|
|
505
|
+
let d = 0;
|
|
506
|
+
for (; k < toks.length; k++) { if (toks[k].v === '<') d++; else if (toks[k].v === '>') { d--; if (!d) { k++; break; } } else if (toks[k].t === 'eof') return false; }
|
|
507
|
+
}
|
|
508
|
+
if (toks[k].t === 'id' && toks[k + 1]?.v === '=>') return true;
|
|
509
|
+
if (!(toks[k].t === 'p' && toks[k].v === '(')) return false;
|
|
510
|
+
let depth = 0;
|
|
511
|
+
for (; k < toks.length; k++) {
|
|
512
|
+
const t = toks[k];
|
|
513
|
+
if (t.t !== 'p') continue;
|
|
514
|
+
if (t.v === '(' || t.v === '[' || t.v === '{') depth++;
|
|
515
|
+
else if (t.v === ')' || t.v === ']' || t.v === '}') { depth--; if (!depth) break; }
|
|
516
|
+
if (t.t === 'eof') return false;
|
|
517
|
+
}
|
|
518
|
+
const after = toks[k + 1];
|
|
519
|
+
if (!after) return false;
|
|
520
|
+
if (after.t === 'p' && after.v === '=>') return true;
|
|
521
|
+
if (after.t === 'p' && after.v === ':') {
|
|
522
|
+
// A return type, then =>. Scan to => at depth 0 before a ; or a line that cannot be a type.
|
|
523
|
+
let d = 0;
|
|
524
|
+
for (let j = k + 2; j < toks.length && j < k + 200; j++) {
|
|
525
|
+
const t = toks[j];
|
|
526
|
+
if (t.t === 'p' && (t.v === '(' || t.v === '[' || t.v === '{' || t.v === '<')) d++;
|
|
527
|
+
else if (t.t === 'p' && (t.v === ')' || t.v === ']' || t.v === '}' || t.v === '>')) { d--; if (d < 0) return false; }
|
|
528
|
+
else if (t.t === 'p' && t.v === '=>' && d === 0) return true;
|
|
529
|
+
else if (t.t === 'p' && (t.v === ';' || t.v === ',' || t.v === '=') && d === 0) return false;
|
|
530
|
+
else if (t.t === 'eof') return false;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
return false;
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
const arrow = () => {
|
|
537
|
+
const line = peek().line;
|
|
538
|
+
const isAsync = at('async') && !at('=>', 1) ? !!next() : false;
|
|
539
|
+
if (at('<')) skipBalanced();
|
|
540
|
+
let ps;
|
|
541
|
+
if (peek().t === 'id' && at('=>', 1)) ps = [{ type: 'Id', name: next().v, line }];
|
|
542
|
+
else { ps = params(); if (opt(':')) skipType(['=>']); }
|
|
543
|
+
eat('=>');
|
|
544
|
+
const body = at('{') ? block() : assign();
|
|
545
|
+
return { type: 'Arrow', params: ps, body, isAsync, line };
|
|
546
|
+
};
|
|
547
|
+
|
|
548
|
+
const ASSIGN_OPS = new Set(['=', '+=', '-=', '*=', '/=', '%=', '**=', '<<=', '>>=', '>>>=', '&=', '|=', '^=', '&&=', '||=', '??=']);
|
|
549
|
+
const assign = (noIn = false) => {
|
|
550
|
+
if ((at('(') || at('<') || (peek().t === 'id' && at('=>', 1)) || (at('async') && (at('(', 1) || peek(1).t === 'id' || at('<', 1)))) && isArrowAhead()) return arrow();
|
|
551
|
+
if (at('yield')) { const line = next().line; opt('*'); const arg = (at(')') || at(']') || at('}') || at(',') || at(';') || peek().nl) ? null : assign(noIn); return { type: 'Await', arg, line }; }
|
|
552
|
+
const line = peek().line;
|
|
553
|
+
const left = conditional(noIn);
|
|
554
|
+
if (peek().t === 'p' && ASSIGN_OPS.has(peek().v)) {
|
|
555
|
+
const op = next().v;
|
|
556
|
+
const right = assign(noIn);
|
|
557
|
+
return { type: 'Assign', op, left, right, line };
|
|
558
|
+
}
|
|
559
|
+
return left;
|
|
560
|
+
};
|
|
561
|
+
|
|
562
|
+
const conditional = (noIn) => {
|
|
563
|
+
const line = peek().line;
|
|
564
|
+
const test = binary(0, noIn);
|
|
565
|
+
if (!at('?')) return test;
|
|
566
|
+
next();
|
|
567
|
+
const cons = assign();
|
|
568
|
+
eat(':');
|
|
569
|
+
const alt = assign(noIn);
|
|
570
|
+
return { type: 'Cond', test, cons, alt, line };
|
|
571
|
+
};
|
|
572
|
+
|
|
573
|
+
const PREC = {
|
|
574
|
+
'??': 1, '||': 2, '&&': 3, '|': 4, '^': 5, '&': 6, '==': 7, '!=': 7, '===': 7, '!==': 7,
|
|
575
|
+
'<': 8, '>': 8, '<=': 8, '>=': 8, instanceof: 8, in: 8, '<<': 9, '>>': 9, '>>>': 9, '+': 10, '-': 10, '*': 11, '/': 11, '%': 11, '**': 12,
|
|
576
|
+
};
|
|
577
|
+
const binary = (minPrec, noIn) => {
|
|
578
|
+
let left = unary();
|
|
579
|
+
for (;;) {
|
|
580
|
+
const t = peek();
|
|
581
|
+
// `as` / `satisfies`: a type follows; the value is unchanged.
|
|
582
|
+
if (t.t === 'id' && (t.v === 'as' || t.v === 'satisfies') && !t.nl) {
|
|
583
|
+
next();
|
|
584
|
+
if (at('const')) next(); else skipType([',', ')', ']', ';', '}', '=', '?', ':', '&&', '||', '??', '=>']);
|
|
585
|
+
continue;
|
|
586
|
+
}
|
|
587
|
+
const op = (t.t === 'p' || (t.t === 'id' && (t.v === 'instanceof' || (t.v === 'in' && !noIn)))) ? t.v : null;
|
|
588
|
+
const prec = op != null ? PREC[op] : undefined;
|
|
589
|
+
if (prec === undefined || prec < minPrec) return left;
|
|
590
|
+
next();
|
|
591
|
+
const right = binary(op === '**' ? prec : prec + 1, noIn);
|
|
592
|
+
left = { type: op === '&&' || op === '||' || op === '??' ? 'Logic' : 'Bin', op, left, right, line: t.line };
|
|
593
|
+
}
|
|
594
|
+
};
|
|
595
|
+
|
|
596
|
+
const unary = () => {
|
|
597
|
+
const t = peek();
|
|
598
|
+
if (t.t === 'p' && ['!', '-', '+', '~'].includes(t.v)) { next(); return { type: 'Unary', op: t.v, arg: unary(), line: t.line }; }
|
|
599
|
+
if (t.t === 'p' && (t.v === '++' || t.v === '--')) { next(); return { type: 'Unary', op: t.v, arg: unary(), line: t.line }; }
|
|
600
|
+
if (t.t === 'id' && (t.v === 'typeof' || t.v === 'void' || t.v === 'delete')) { next(); return { type: 'Unary', op: t.v, arg: unary(), line: t.line }; }
|
|
601
|
+
if (t.t === 'id' && t.v === 'await' && !(at('(', 1) && false)) {
|
|
602
|
+
const n2 = peek(1);
|
|
603
|
+
if (!(n2.t === 'p' && [')', ']', '}', ',', ';', '=', '.'].includes(n2.v))) { next(); return { type: 'Await', arg: unary(), line: t.line }; }
|
|
604
|
+
}
|
|
605
|
+
if (t.t === 'p' && t.v === '<' && !isArrowAhead()) { skipBalanced(); return unary(); } // old-style type assertion <T>x
|
|
606
|
+
return postfix(primary());
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const args = () => {
|
|
610
|
+
eat('(');
|
|
611
|
+
const list = [];
|
|
612
|
+
while (!at(')')) {
|
|
613
|
+
if (opt('...')) list.push({ type: 'Spread', arg: assign(), line: peek().line });
|
|
614
|
+
else list.push(assign());
|
|
615
|
+
if (!opt(',')) break;
|
|
616
|
+
}
|
|
617
|
+
eat(')');
|
|
618
|
+
return list;
|
|
619
|
+
};
|
|
620
|
+
|
|
621
|
+
// Generic call arguments: f<T>(x). Only when the <…> is followed by ( and holds type-like tokens.
|
|
622
|
+
const genericCallAhead = () => {
|
|
623
|
+
if (!at('<')) return false;
|
|
624
|
+
let d = 0, k = pos;
|
|
625
|
+
for (; k < toks.length && k < pos + 80; k++) {
|
|
626
|
+
const t = toks[k];
|
|
627
|
+
if (t.t === 'p' && t.v === '<') d++;
|
|
628
|
+
else if (t.t === 'p' && t.v === '>') { d--; if (!d) break; }
|
|
629
|
+
else if (t.t === 'p' && t.v === '>>') { d -= 2; if (d <= 0) break; }
|
|
630
|
+
else if (t.t === 'p' && ![',', '.', '|', '&', '[', ']', '{', '}', ':', '?', '(', ')', '=>', ';'].includes(t.v)) return false;
|
|
631
|
+
else if (t.t === 'eof') return false;
|
|
632
|
+
}
|
|
633
|
+
return toks[k + 1]?.t === 'p' && (toks[k + 1].v === '(' || toks[k + 1].v === '`');
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
const postfix = (expr) => {
|
|
637
|
+
for (;;) {
|
|
638
|
+
const t = peek();
|
|
639
|
+
if (t.t === 'p' && t.v === '.') { next(); const name = at('#') ? (next(), '#' + next().v) : next().v; expr = { type: 'Member', object: expr, prop: name, line: t.line }; continue; }
|
|
640
|
+
if (t.t === 'p' && t.v === '?.') {
|
|
641
|
+
next();
|
|
642
|
+
if (at('(')) { expr = { type: 'Call', callee: expr, args: args(), optional: true, line: t.line }; continue; }
|
|
643
|
+
if (at('[')) { next(); const p = expression(); eat(']'); expr = { type: 'Member', object: expr, computed: p, prop: p.type === 'Lit' ? String(p.value) : null, optional: true, line: t.line }; continue; }
|
|
644
|
+
expr = { type: 'Member', object: expr, prop: next().v, optional: true, line: t.line };
|
|
645
|
+
continue;
|
|
646
|
+
}
|
|
647
|
+
if (t.t === 'p' && t.v === '[' && !t.nl) { next(); const p = expression(); eat(']'); expr = { type: 'Member', object: expr, computed: p, prop: p.type === 'Lit' ? String(p.value) : null, line: t.line }; continue; }
|
|
648
|
+
if (t.t === 'p' && t.v === '(' ) { if (t.nl && false) break; expr = { type: 'Call', callee: expr, args: args(), line: t.line }; continue; }
|
|
649
|
+
if (t.t === 'p' && t.v === '<' && genericCallAhead()) { skipBalanced(); continue; }
|
|
650
|
+
if (t.t === 'tpl') { next(); expr = { type: 'Tagged', tag: expr, tpl: tplNode(t), line: t.line }; continue; }
|
|
651
|
+
if (t.t === 'p' && t.v === '!' && !t.nl && !at('=', 1) && !at('==', 1)) { next(); continue; } // non-null assertion
|
|
652
|
+
if (t.t === 'p' && (t.v === '++' || t.v === '--') && !t.nl) { next(); expr = { type: 'Unary', op: t.v, arg: expr, line: t.line }; continue; }
|
|
653
|
+
return expr;
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
const tplNode = (t) => {
|
|
658
|
+
const parts = t.v.map((p) => {
|
|
659
|
+
if (p.expr == null) return { text: p.text };
|
|
660
|
+
let e;
|
|
661
|
+
try { e = parseExpressionAt(p.expr, p.line); } catch { e = { type: 'Unknown', line: p.line }; }
|
|
662
|
+
return { expr: e };
|
|
663
|
+
});
|
|
664
|
+
return { type: 'Tpl', parts, line: t.line };
|
|
665
|
+
};
|
|
666
|
+
|
|
667
|
+
const objectLiteral = () => {
|
|
668
|
+
const line = eat('{').line;
|
|
669
|
+
const props = [];
|
|
670
|
+
while (!at('}')) {
|
|
671
|
+
const pline = peek().line;
|
|
672
|
+
if (opt('...')) { props.push({ spread: assign(), line: pline }); if (!opt(',')) break; continue; }
|
|
673
|
+
let isAsync = false, kind = 'init';
|
|
674
|
+
if (at('async') && !at(',', 1) && !at(':', 1) && !at('(', 1) && !at('}', 1)) { next(); isAsync = true; }
|
|
675
|
+
opt('*');
|
|
676
|
+
if ((at('get') || at('set')) && !at(',', 1) && !at(':', 1) && !at('(', 1) && !at('}', 1)) kind = next().v;
|
|
677
|
+
let key, computed = null;
|
|
678
|
+
if (at('[')) { next(); computed = assign(); eat(']'); key = computed.type === 'Lit' ? String(computed.value) : null; }
|
|
679
|
+
else { const kt = next(); key = kt.t === 'num' ? kt.v : kt.v; }
|
|
680
|
+
if (at('(') || at('<')) {
|
|
681
|
+
if (at('<')) skipBalanced();
|
|
682
|
+
const ps = params();
|
|
683
|
+
if (opt(':')) skipType(['{']);
|
|
684
|
+
const body = block();
|
|
685
|
+
props.push({ key, value: { type: 'Arrow', params: ps, body, isAsync, line: pline, isMethod: true, name: key }, method: true, kind, line: pline });
|
|
686
|
+
} else if (opt(':')) props.push({ key, computed, value: assign(), line: pline });
|
|
687
|
+
else if (at('=')) { next(); props.push({ key, value: { type: 'Id', name: key, line: pline }, shorthand: true, line: pline, dflt: assign() }); }
|
|
688
|
+
else props.push({ key, value: { type: 'Id', name: key, line: pline }, shorthand: true, line: pline });
|
|
689
|
+
if (!opt(',')) break;
|
|
690
|
+
}
|
|
691
|
+
eat('}');
|
|
692
|
+
return { type: 'Obj', props, line };
|
|
693
|
+
};
|
|
694
|
+
|
|
695
|
+
const primary = () => {
|
|
696
|
+
const t = peek();
|
|
697
|
+
const line = t.line;
|
|
698
|
+
if (t.t === 'num') { next(); return { type: 'Lit', value: Number(t.v.replace(/_/g, '')), raw: t.v, line }; }
|
|
699
|
+
if (t.t === 'str') { next(); return { type: 'Lit', value: t.v, line }; }
|
|
700
|
+
if (t.t === 'tpl') { next(); return tplNode(t); }
|
|
701
|
+
if (t.t === 're') { next(); return { type: 'Lit', value: t.v, regex: true, line }; }
|
|
702
|
+
if (t.t === 'p') {
|
|
703
|
+
if (t.v === '(') { next(); const e = expression(); eat(')'); return { ...e, paren: true }; }
|
|
704
|
+
if (t.v === '[') {
|
|
705
|
+
next();
|
|
706
|
+
const elems = [];
|
|
707
|
+
while (!at(']')) {
|
|
708
|
+
if (at(',')) { next(); elems.push(null); continue; }
|
|
709
|
+
if (opt('...')) elems.push({ type: 'Spread', arg: assign(), line }); else elems.push(assign());
|
|
710
|
+
if (!opt(',')) break;
|
|
711
|
+
}
|
|
712
|
+
eat(']');
|
|
713
|
+
return { type: 'Arr', elems, line };
|
|
714
|
+
}
|
|
715
|
+
if (t.v === '{') return objectLiteral();
|
|
716
|
+
if (t.v === '#') { next(); next(); return { type: 'Id', name: '#private', line }; }
|
|
717
|
+
if (t.v === '@') { decorators(); return primary(); }
|
|
718
|
+
fail('an expression Thunder cannot read');
|
|
719
|
+
}
|
|
720
|
+
if (t.t === 'id') {
|
|
721
|
+
if (t.v === 'function' || (t.v === 'async' && at('function', 1))) {
|
|
722
|
+
const f = functionDecl(true);
|
|
723
|
+
return { type: 'Arrow', params: f.params, body: f.body, isAsync: f.isAsync, line, name: f.name };
|
|
724
|
+
}
|
|
725
|
+
if (t.v === 'class') { const c = classDecl(true); return { ...c, type: 'ClassExpr' }; }
|
|
726
|
+
if (t.v === 'new') {
|
|
727
|
+
next();
|
|
728
|
+
if (at('.')) { next(); next(); return { type: 'Id', name: 'new.target', line }; }
|
|
729
|
+
let callee = primary();
|
|
730
|
+
while (at('.') || at('[')) {
|
|
731
|
+
if (opt('.')) callee = { type: 'Member', object: callee, prop: next().v, line };
|
|
732
|
+
else { next(); const p = expression(); eat(']'); callee = { type: 'Member', object: callee, computed: p, line }; }
|
|
733
|
+
}
|
|
734
|
+
if (at('<') && genericCallAhead()) skipBalanced();
|
|
735
|
+
const a = at('(') ? args() : [];
|
|
736
|
+
return { type: 'New', callee, args: a, line };
|
|
737
|
+
}
|
|
738
|
+
if (t.v === 'this') { next(); return { type: 'This', line }; }
|
|
739
|
+
if (t.v === 'super') { next(); return { type: 'Id', name: 'super', line }; }
|
|
740
|
+
if (t.v === 'true' || t.v === 'false') { next(); return { type: 'Lit', value: t.v === 'true', line }; }
|
|
741
|
+
if (t.v === 'null') { next(); return { type: 'Lit', value: null, line }; }
|
|
742
|
+
if (t.v === 'undefined') { next(); return { type: 'Lit', value: undefined, line }; }
|
|
743
|
+
if (t.v === 'import' && at('(', 1)) { next(); const a = args(); return { type: 'Call', callee: { type: 'Id', name: 'import', line }, args: a, line }; }
|
|
744
|
+
if (t.v === 'import' && at('.', 1)) { next(); next(); next(); return { type: 'Id', name: 'import.meta', line }; }
|
|
745
|
+
next();
|
|
746
|
+
return { type: 'Id', name: t.v, line };
|
|
747
|
+
}
|
|
748
|
+
fail('an expression Thunder cannot read');
|
|
749
|
+
};
|
|
750
|
+
|
|
751
|
+
return { program, expressionOnly: () => { const e = expression(); return e; } };
|
|
752
|
+
}
|
|
753
|
+
|
|
754
|
+
function parseExpressionAt(src, line) {
|
|
755
|
+
const p = parse(src);
|
|
756
|
+
const e = p.expressionOnly();
|
|
757
|
+
const shift = (n) => {
|
|
758
|
+
if (!n || typeof n !== 'object') return;
|
|
759
|
+
if (typeof n.line === 'number') n.line += line - 1;
|
|
760
|
+
for (const v of Object.values(n)) {
|
|
761
|
+
if (Array.isArray(v)) v.forEach(shift);
|
|
762
|
+
else if (v && typeof v === 'object') shift(v);
|
|
763
|
+
}
|
|
764
|
+
};
|
|
765
|
+
shift(e);
|
|
766
|
+
return e;
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
/** Parse a whole file into { type: 'Program', body }. Throws ParseError when it cannot. */
|
|
770
|
+
export function parseProgram(src) {
|
|
771
|
+
return parse(src).program();
|
|
772
|
+
}
|