@yaks/code-mode 0.0.0-stage → 0.2.36

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/repl.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"repl.js","sources":["./repl.ts"],"names":[],"mappings":"AAAA,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAC5E,0EAA0E;AAC1E,8EAA8E;AAC9E,2EAA2E;AAC3E,kEAAkE;AAClE,SAAwB,QAAQ,oBAAmB;AACnD,SAAS,OAAO,sBAAqB;AAUrC,OAAO,IAAI,QAAiB;EAAE,QAAQ,EAAE;EAAE,cAAc,CAAC;AAAE,EAAC;AAE5D,IAAI,OAAO;AACX,IAAI,KAAK,CAAC,OAAiB,KAAK,SAAS,CAAC;AAE1C,8EAA8E,GAC9E,OAAO,IAAI,SAAS,CAAC,UACnB,KAAK,KAAK,CAAC,OAAO,CAAC,KAAK,IAAI,kCAAiC;AAE/D,0EAA0E,GAC1E,OAAO,IAAI,YAAY,CACrB;EAEA,IAAI,OAAO,OAAO,CAAC,UAAU;EAC7B,IAAI,OAAO,KAAK,KAAK,CAAC,KAAK,OAAO,CAAC,WAAW,QAAQ,MAAM;EAC5D,OAAO;IAAE,GAAG,OAAO;IAAE,CAAC,KAAK,EAAE,KAAK,SAAS,CAAC,SAAS;EAAO;AAC9D,EAAC;AAED;;;kCAGkC,GAClC,OAAO,IAAI,QAAQ,CACjB,SACA;EAEA,IAAI,OAAO,OAAO;EAClB,IAAI,MAAM,IAAI,IAAI;OAAI,KAAK,KAAK;OAAK,OAAO,IAAI,CAAC,KAAK,YAAY;GAAE;EACpE,IAAI,SAAS,QAAQ,MAAM,CAAC,MAAM,CAAC,CAAC,OAAS,CAAC,IAAI,GAAG,CAAC;EACtD,IAAI,eAAe,OAAO,OAAO,CAAC,QAAQ,YAAY,EACnD,MAAM,CAAC,CAAC,CAAC,KAAK,GAAK,CAAC,IAAI,GAAG,CAAC;E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AI,MAAM,SAAS;IACpC,IAAI,UAAU,OAAO,SAAS,SAAS;IACvC,KAAK,KAAK,CAAC,QAAQ,OAAO;IAC1B,OAAO,QAAQ,IAAI;EACrB,EAAC"}
package/repl.ts ADDED
@@ -0,0 +1,112 @@
1
+ // A REPL's cells, between the host that keeps the REPL and the cell's own
2
+ // Worker. Nothing can evaluate new code into a running isolate, so each cell
3
+ // is a Worker of its own, run as the same Durable Object facet: its storage
4
+ // (`storage.sql`, `storage.kv`) lasts as long as the REPL, and the cell's
5
+ // top-level values are kept there when it ends (./source.ts `keep`). The host
6
+ // keeps what this module needs to declare them again in the next cell: the
7
+ // names kept, and the source of each function and class declared.
8
+ import { type Declared, declared } from './scan.js'
9
+ import { CARRIED } from './source.js'
10
+
11
+ /** What a REPL carries from one cell to the next, kept by its host. */
12
+ export type Carried = {
13
+ /** the names whose values the facet's storage holds */
14
+ values: string[]
15
+ /** each function and class declared, by name */
16
+ declarations: Record<string, string>
17
+ }
18
+
19
+ export let EMPTY: Carried = { values: [], declarations: {} }
20
+
21
+ let CELL = 'cell.json'
22
+ let id = (name: string) => JSON.stringify(name)
23
+
24
+ /** What a cell's own code declares, kept beside it for `carry` and `settle`. */
25
+ export let cellOf = (modules: Record<string, string>): Declared =>
26
+ JSON.parse(modules[CELL] ?? '{"names":[],"declarations":{}}')
27
+
28
+ /** The cell's modules with `declared`'s reading of its code beside them. */
29
+ export let described = (
30
+ modules: Record<string, string>,
31
+ ): Record<string, string> => {
32
+ let user = modules['user.js']
33
+ let code = user.slice(user.indexOf(CARRIED) + CARRIED.length)
34
+ return { ...modules, [CELL]: JSON.stringify(declared(code)) }
35
+ }
36
+
37
+ /** The cell's modules as its Worker loads them, declaring first what the
38
+ * REPL carries and the cell does not declare again: each kept value by its name, then each earlier
39
+ * function and class. Its own names and the carried ones are read back when
40
+ * it ends, whichever were bound. */
41
+ export let carry = (
42
+ modules: Record<string, string>,
43
+ carried: Carried,
44
+ ): Record<string, string> => {
45
+ let cell = cellOf(modules)
46
+ let own = new Set([...cell.names, ...Object.keys(cell.declarations)])
47
+ let values = carried.values.filter((name) => !own.has(name))
48
+ let declarations = Object.entries(carried.declarations)
49
+ .filter(([name]) => !own.has(name))
50
+ let read = [...new Set([...cell.names, ...values])]
51
+ let prelude = [
52
+ `function $capture() {\n const $o = {};\n${
53
+ read.map((name) => ` try { $o[${id(name)}] = ${name}; } catch {}`)
54
+ .join('\n')
55
+ }\n return $o;\n}`,
56
+ '$carried.capture = $capture;',
57
+ ...values.map((name) => `let ${name} = $carried.values[${id(name)}];`),
58
+ ...declarations.map(([, source]) => source),
59
+ ].join('\n')
60
+ // What the cell declares is the host's to read, not a module to load.
61
+ let { [CELL]: _, ...loaded } = modules
62
+ return {
63
+ ...loaded,
64
+ 'user.js': modules['user.js'].replace(CARRIED, () => prelude),
65
+ }
66
+ }
67
+
68
+ /** What the REPL carries after a cell, and the cell's answer without its
69
+ * bookkeeping. A name the cell bound again as a value is no longer a
70
+ * declaration, and one it declared is no longer a value. */
71
+ export let settle = (
72
+ carried: Carried,
73
+ modules: Record<string, string>,
74
+ text: string,
75
+ ): { carried: Carried; text: string } => {
76
+ let { $kept, ...answer } = JSON.parse(text) as {
77
+ $kept?: string[]
78
+ [k: string]: unknown
79
+ }
80
+ if (!$kept) return { carried, text }
81
+ let cell = cellOf(modules)
82
+ let declarations = { ...carried.declarations }
83
+ for (let name of $kept) delete declarations[name]
84
+ Object.assign(declarations, cell.declarations)
85
+ return {
86
+ carried: {
87
+ values: $kept.filter((name) => !(name in cell.declarations)),
88
+ declarations,
89
+ },
90
+ text: JSON.stringify(answer),
91
+ }
92
+ }
93
+
94
+ /** Where a host keeps what its REPL carries. */
95
+ export type Keeping = {
96
+ read(): Carried | undefined
97
+ write(carried: Carried): void
98
+ }
99
+
100
+ /** How a REPL's host runs each cell: declared with what the REPL carries,
101
+ * run by `run` as the REPL's facet, and settled into what it carries next. */
102
+ export let cells = (
103
+ kept: Keeping,
104
+ run: (modules: Record<string, string>) => Promise<string>,
105
+ ) =>
106
+ async (modules: Record<string, string>): Promise<string> => {
107
+ let carried = kept.read() ?? EMPTY
108
+ let text = await run(carry(modules, carried))
109
+ let settled = settle(carried, modules, text)
110
+ kept.write(settled.carried)
111
+ return settled.text
112
+ }
package/scan.js ADDED
@@ -0,0 +1,246 @@
1
+ // What a REPL cell declares at its top level, read from its JavaScript: the
2
+ // names its `let`, `const` and `var` bind, and the source of each `function`
3
+ // and `class` it declares. A cell's wrapper keeps those names' values, and
4
+ // the next cell declares them again (./repl.ts). Nothing here evaluates code.
5
+ //
6
+ // It reads tokens, not a syntax tree: strings, templates, comments and
7
+ // regular expressions are skipped, and only what stands at the body's own
8
+ // depth counts. A name it reads that the cell never bound costs nothing (the
9
+ // wrapper's read of it fails and is skipped); a name it misses is not kept.
10
+ let IDENT_START = /^[A-Za-z_$\u0080-\uffff]/;
11
+ let IDENT = /[A-Za-z_$\u0080-\uffff][\w$\u0080-\uffff]*/y;
12
+ let NUMBER = /\d[\w.]*|\.\d[\w]*/y;
13
+ let PUNCT = />>>=|\.\.\.|===|!==|\*\*=|<<=|>>=|>>>|&&=|\|\|=|\?\?=|=>|==|!=|<=|>=|&&|\|\||\?\?|\?\.|\+\+|--|\+=|-=|\*=|\/=|%=|&=|\|=|\^=|\*\*|<<|>>|[{}()[\];,<>+\-*/%&|^!~?:=.@#]/y;
14
+ // After these, `/` starts a regular expression rather than dividing.
15
+ let BEFORE_REGEX = new Set([
16
+ 'return',
17
+ 'typeof',
18
+ 'instanceof',
19
+ 'in',
20
+ 'of',
21
+ 'new',
22
+ 'delete',
23
+ 'void',
24
+ 'throw',
25
+ 'case',
26
+ 'do',
27
+ 'else',
28
+ 'yield',
29
+ 'await'
30
+ ]);
31
+ // Whether a token ends an expression: a name, a literal (a string, a number,
32
+ // a regular expression, a template's last part) or a closing bracket.
33
+ let ends = (t)=>!!t && (IDENT_START.test(t.text) && !BEFORE_REGEX.has(t.text) || /^['"`\d]/.test(t.text) || t.text.length > 2 && t.text[0] == '/' || t.text[0] == '}' && t.text.at(-1) == '`' || t.text == ')' || t.text == ']' || t.text == '}');
34
+ /** The tokens of JavaScript source, with the depth each stands at. */ export let tokens = (src)=>{
35
+ let out = [];
36
+ let depth = 0;
37
+ // What each open bracket was, and for `${`, that a template resumes.
38
+ let open = [];
39
+ let line = false;
40
+ let i = 0;
41
+ let push = (start, end, at = depth)=>{
42
+ out.push({
43
+ text: src.slice(start, end),
44
+ start,
45
+ end,
46
+ line,
47
+ depth: at
48
+ });
49
+ line = false;
50
+ };
51
+ // The rest of a template from `i`, to its end or its next `${`.
52
+ let template = ()=>{
53
+ let start = i;
54
+ while(i < src.length){
55
+ let c = src[i];
56
+ if (c == '\\') i += 2;
57
+ else if (c == '`') {
58
+ i++;
59
+ push(start, i);
60
+ return;
61
+ } else if (c == '$' && src[i + 1] == '{') {
62
+ i += 2;
63
+ push(start, i);
64
+ open.push('${');
65
+ depth++;
66
+ return;
67
+ } else i++;
68
+ }
69
+ push(start, i);
70
+ };
71
+ while(i < src.length){
72
+ let c = src[i];
73
+ if (c == '\n') {
74
+ line = true;
75
+ i++;
76
+ } else if (/\s/.test(c)) i++;
77
+ else if (c == '/' && src[i + 1] == '/') {
78
+ while(i < src.length && src[i] != '\n')i++;
79
+ } else if (c == '/' && src[i + 1] == '*') {
80
+ let end = src.indexOf('*/', i + 2);
81
+ let skipped = src.slice(i, end < 0 ? src.length : end);
82
+ if (skipped.includes('\n')) line = true;
83
+ i = end < 0 ? src.length : end + 2;
84
+ } else if (c == '"' || c == "'") {
85
+ let start = i++;
86
+ while(i < src.length && src[i] != c && src[i] != '\n'){
87
+ i += src[i] == '\\' ? 2 : 1;
88
+ }
89
+ i++;
90
+ push(start, i);
91
+ } else if (c == '`') {
92
+ i++;
93
+ template();
94
+ } else if (c == '/' && !ends(out.at(-1))) {
95
+ let start = i++;
96
+ let klass = false;
97
+ while(i < src.length && src[i] != '\n'){
98
+ if (src[i] == '\\') i++;
99
+ else if (src[i] == '[') klass = true;
100
+ else if (src[i] == ']') klass = false;
101
+ else if (src[i] == '/' && !klass) break;
102
+ i++;
103
+ }
104
+ i++;
105
+ while(/[a-z]/.test(src[i] ?? ''))i++;
106
+ push(start, i);
107
+ } else {
108
+ let match = (re)=>{
109
+ re.lastIndex = i;
110
+ return re.exec(src)?.[0];
111
+ };
112
+ let word = match(IDENT) ?? match(NUMBER) ?? match(PUNCT) ?? c;
113
+ let start = i;
114
+ i += word.length;
115
+ if (word == '{' || word == '(' || word == '[') {
116
+ push(start, i);
117
+ open.push(word);
118
+ depth++;
119
+ } else if (word == '}' || word == ')' || word == ']') {
120
+ let was = open.pop();
121
+ depth = Math.max(0, depth - 1);
122
+ if (was == '${') template();
123
+ else push(start, i);
124
+ } else push(start, i);
125
+ }
126
+ }
127
+ return out;
128
+ };
129
+ /** What a cell's body declares at its own top level. */ export let declared = (body)=>{
130
+ let ts = tokens(body);
131
+ let names = new Set();
132
+ let declarations = {};
133
+ let at = (n)=>ts[n]?.text;
134
+ // Whether the token at n starts a statement of the body.
135
+ let starts = (n)=>{
136
+ let before = ts[n - 1];
137
+ return !before || before.text == ';' || before.text == '}' || ts[n].line && ends(before);
138
+ };
139
+ // The names a binding pattern from n binds, and where it ends.
140
+ let pattern = (n)=>{
141
+ let t = ts[n];
142
+ if (!t) return n;
143
+ if (IDENT_START.test(t.text)) {
144
+ names.add(t.text);
145
+ return n + 1;
146
+ }
147
+ if (t.text != '{' && t.text != '[') return n;
148
+ let base = t.depth;
149
+ let i = n + 1;
150
+ while(ts[i] && !(ts[i].depth == base && /^[}\]]$/.test(ts[i].text))){
151
+ let text = ts[i].text;
152
+ if (text == '=') {
153
+ // A default value: skipped to the next element of this pattern.
154
+ let level = ts[i].depth;
155
+ i++;
156
+ while(ts[i] && !(ts[i].depth == level && ts[i].text == ',') && ts[i].depth >= level && !(ts[i].depth == level - 1))i++;
157
+ continue;
158
+ }
159
+ if (IDENT_START.test(text) && /^[,}\]=]$/.test(at(i + 1) ?? '') && at(i - 1) != '.') names.add(text);
160
+ i++;
161
+ }
162
+ return i + 1;
163
+ };
164
+ // The end of a declaration's initializer at depth 0: a comma for the next
165
+ // declarator, a semicolon, or a new statement on a new line.
166
+ let initialized = (n)=>{
167
+ let i = n;
168
+ while(ts[i]){
169
+ let t = ts[i];
170
+ if (t.depth == 0 && t.text == ',') return {
171
+ next: i + 1,
172
+ more: true
173
+ };
174
+ if (t.depth == 0 && t.text == ';') return {
175
+ next: i + 1,
176
+ more: false
177
+ };
178
+ if (i > n && t.depth == 0 && t.line && ends(ts[i - 1]) && IDENT_START.test(t.text)) return {
179
+ next: i,
180
+ more: false
181
+ };
182
+ i++;
183
+ }
184
+ return {
185
+ next: i,
186
+ more: false
187
+ };
188
+ };
189
+ // The source of a declaration from n: through the brace closing its body.
190
+ let whole = (n)=>{
191
+ let i = n;
192
+ let entered = false;
193
+ while(ts[i]){
194
+ if (ts[i].depth == 0 && ts[i].text == '{') entered = true;
195
+ if (entered && ts[i].depth == 0 && ts[i].text == '}') break;
196
+ i++;
197
+ }
198
+ return {
199
+ source: body.slice(ts[n].start, ts[i]?.end),
200
+ next: i + 1
201
+ };
202
+ };
203
+ let i = 0;
204
+ while(i < ts.length){
205
+ let t = ts[i];
206
+ if (t.depth != 0 || !starts(i)) {
207
+ i++;
208
+ continue;
209
+ }
210
+ if (/^(let|const|var)$/.test(t.text) && /^[\w${[]/.test(at(i + 1) ?? '')) {
211
+ let n = i + 1;
212
+ while(true){
213
+ n = pattern(n);
214
+ let { next, more } = at(n) == '=' ? initialized(n + 1) : {
215
+ next: at(n) == ',' ? n + 1 : at(n) == ';' ? n + 1 : n,
216
+ more: at(n) == ','
217
+ };
218
+ n = next;
219
+ if (!more) break;
220
+ }
221
+ i = Math.max(n, i + 1);
222
+ continue;
223
+ }
224
+ let fn = t.text == 'async' && at(i + 1) == 'function' ? i + 1 : i;
225
+ if (at(fn) == 'function' || t.text == 'class') {
226
+ let named = at(fn + 1) == '*' ? fn + 2 : fn + 1;
227
+ let name = at(named);
228
+ if (name && IDENT_START.test(name)) {
229
+ let { source, next } = whole(i);
230
+ declarations[name] = source;
231
+ names.delete(name);
232
+ i = next;
233
+ continue;
234
+ }
235
+ }
236
+ i++;
237
+ }
238
+ for (let name of Object.keys(declarations))names.delete(name);
239
+ return {
240
+ names: [
241
+ ...names
242
+ ],
243
+ declarations
244
+ };
245
+ };
246
+ //# sourceMappingURL=scan.js.map
package/scan.js.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.js","sources":["./scan.ts"],"names":[],"mappings":"AAAA,4EAA4E;AAC5E,6EAA6E;AAC7E,2EAA2E;AAC3E,8EAA8E;AAC9E,EAAE;AACF,uEAAuE;AACvE,0EAA0E;AAC1E,6EAA6E;AAC7E,4EAA4E;AAmB5E,IAAI,cAAc;AAClB,IAAI,QAAQ;AACZ,IAAI,SAAS;AACb,IAAI,QACF;AACF,qEAAqE;AACrE,IAAI,eAAe,IAAI,IAAI;EACzB;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;CACD;AACD,6EAA6E;AAC7E,sEAAsE;AACtE,IAAI,OAAO,CAAC,IACV,CAAC,CAAC,KAAK,CAAC,YAAY,IAAI,CAAC,EAAE,IAAI,KAAK,CAAC,aAAa,GAAG,CAAC,EAAE,IAAI,KAC1D,WAAW,IAAI,CAAC,EAAE,IAAI,KAAK,EAAE,IAAI,CAAC,MAAM,GAAG,KAAK,EAAE,IAAI,CAAC,EAAE,IAAI,OAC7D,EAAE,IAAI,CAAC,EAAE,IAAI,OAAO,EAAE,IAAI,CAAC,EAAE,CAAC,CAAC,MAAM,OACrC,EAAE,IAAI,IAAI,OAAO,EAAE,IAAI,IAAI,OAAO,EAAE,IAAI,IAAI,GAAG;AAEnD,oEAAoE,GACpE,OAAO,IAAI,SAAS,CAAC;EACnB,IAAI,MAAe,EAAE;EACrB,IAAI,QAAQ;EACZ,qEAAqE;EACrE,IAAI,OAAiB,EAAE;EACv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AAI,EAAE,KAAK,IAAI,KAAK,CAAC,OAAO,IAAI;MAC9B;MACA;IACF;IACA,IAAI,oBAAoB,IAAI,CAAC,EAAE,IAAI,KAAK,WAAW,IAAI,CAAC,GAAG,IAAI,MAAM,KAAK;MACxE,IAAI,IAAI,IAAI;MACZ,MAAO,KAAM;QACX,IAAI,QAAQ;QACZ,IAAI,EAAE,IAAI,EAAE,IAAI,EAAE,GAAG,GAAG,MAAM,MAAM,YAAY,IAAI,KAAK;UACvD,MAAM,GAAG,MAAM,MAAM,IAAI,IAAI,GAAG,MAAM,MAAM,IAAI,IAAI;UACpD,MAAM,GAAG,MAAM;QACjB;QACA,IAAI;QACJ,IAAI,CAAC,MAAM;MACb;MACA,IAAI,KAAK,GAAG,CAAC,GAAG,IAAI;MACpB;IACF;IACA,IAAI,KAAK,EAAE,IAAI,IAAI,WAAW,GAAG,IAAI,MAAM,aAAa,IAAI,IAAI;IAChE,IAAI,GAAG,OAAO,cAAc,EAAE,IAAI,IAAI,SAAS;MAC7C,IAAI,QAAQ,GAAG,KAAK,MAAM,MAAM,KAAK,IAAI,KAAK;MAC9C,IAAI,OAAO,GAAG;MACd,IAAI,QAAQ,YAAY,IAAI,CAAC,OAAO;QAClC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,MAAM;QAC7B,YAAY,CAAC,KAAK,GAAG;QACrB,MAAM,MAAM,CAAC;QACb,IAAI;QACJ;MACF;IACF;IACA;EACF;EACA,KAAK,IAAI,QAAQ,OAAO,IAAI,CAAC,cAAe,MAAM,MAAM,CAAC;EACzD,OAAO;IAAE,OAAO;SAAI;KAAM;IAAE;EAAa;AAC3C,EAAC"}
package/scan.ts ADDED
@@ -0,0 +1,257 @@
1
+ // What a REPL cell declares at its top level, read from its JavaScript: the
2
+ // names its `let`, `const` and `var` bind, and the source of each `function`
3
+ // and `class` it declares. A cell's wrapper keeps those names' values, and
4
+ // the next cell declares them again (./repl.ts). Nothing here evaluates code.
5
+ //
6
+ // It reads tokens, not a syntax tree: strings, templates, comments and
7
+ // regular expressions are skipped, and only what stands at the body's own
8
+ // depth counts. A name it reads that the cell never bound costs nothing (the
9
+ // wrapper's read of it fails and is skipped); a name it misses is not kept.
10
+
11
+ export type Declared = {
12
+ /** names bound by `let`, `const` and `var` */
13
+ names: string[]
14
+ /** each `function` and `class` declaration's source, by its name */
15
+ declarations: Record<string, string>
16
+ }
17
+
18
+ type Token = {
19
+ text: string
20
+ start: number
21
+ end: number
22
+ /** a newline came before it */
23
+ line: boolean
24
+ /** the bracket depth it stands at, from 0 */
25
+ depth: number
26
+ }
27
+
28
+ let IDENT_START = /^[A-Za-z_$\u0080-\uffff]/
29
+ let IDENT = /[A-Za-z_$\u0080-\uffff][\w$\u0080-\uffff]*/y
30
+ let NUMBER = /\d[\w.]*|\.\d[\w]*/y
31
+ let PUNCT =
32
+ />>>=|\.\.\.|===|!==|\*\*=|<<=|>>=|>>>|&&=|\|\|=|\?\?=|=>|==|!=|<=|>=|&&|\|\||\?\?|\?\.|\+\+|--|\+=|-=|\*=|\/=|%=|&=|\|=|\^=|\*\*|<<|>>|[{}()[\];,<>+\-*/%&|^!~?:=.@#]/y
33
+ // After these, `/` starts a regular expression rather than dividing.
34
+ let BEFORE_REGEX = new Set([
35
+ 'return',
36
+ 'typeof',
37
+ 'instanceof',
38
+ 'in',
39
+ 'of',
40
+ 'new',
41
+ 'delete',
42
+ 'void',
43
+ 'throw',
44
+ 'case',
45
+ 'do',
46
+ 'else',
47
+ 'yield',
48
+ 'await',
49
+ ])
50
+ // Whether a token ends an expression: a name, a literal (a string, a number,
51
+ // a regular expression, a template's last part) or a closing bracket.
52
+ let ends = (t?: Token) =>
53
+ !!t && (IDENT_START.test(t.text) && !BEFORE_REGEX.has(t.text) ||
54
+ /^['"`\d]/.test(t.text) || t.text.length > 2 && t.text[0] == '/' ||
55
+ t.text[0] == '}' && t.text.at(-1) == '`' ||
56
+ t.text == ')' || t.text == ']' || t.text == '}')
57
+
58
+ /** The tokens of JavaScript source, with the depth each stands at. */
59
+ export let tokens = (src: string): Token[] => {
60
+ let out: Token[] = []
61
+ let depth = 0
62
+ // What each open bracket was, and for `${`, that a template resumes.
63
+ let open: string[] = []
64
+ let line = false
65
+ let i = 0
66
+ let push = (start: number, end: number, at = depth) => {
67
+ out.push({ text: src.slice(start, end), start, end, line, depth: at })
68
+ line = false
69
+ }
70
+ // The rest of a template from `i`, to its end or its next `${`.
71
+ let template = () => {
72
+ let start = i
73
+ while (i < src.length) {
74
+ let c = src[i]
75
+ if (c == '\\') i += 2
76
+ else if (c == '`') {
77
+ i++
78
+ push(start, i)
79
+ return
80
+ } else if (c == '$' && src[i + 1] == '{') {
81
+ i += 2
82
+ push(start, i)
83
+ open.push('${')
84
+ depth++
85
+ return
86
+ } else i++
87
+ }
88
+ push(start, i)
89
+ }
90
+ while (i < src.length) {
91
+ let c = src[i]
92
+ if (c == '\n') {
93
+ line = true
94
+ i++
95
+ } else if (/\s/.test(c)) i++
96
+ else if (c == '/' && src[i + 1] == '/') {
97
+ while (i < src.length && src[i] != '\n') i++
98
+ } else if (c == '/' && src[i + 1] == '*') {
99
+ let end = src.indexOf('*/', i + 2)
100
+ let skipped = src.slice(i, end < 0 ? src.length : end)
101
+ if (skipped.includes('\n')) line = true
102
+ i = end < 0 ? src.length : end + 2
103
+ } else if (c == '"' || c == "'") {
104
+ let start = i++
105
+ while (i < src.length && src[i] != c && src[i] != '\n') {
106
+ i += src[i] == '\\' ? 2 : 1
107
+ }
108
+ i++
109
+ push(start, i)
110
+ } else if (c == '`') {
111
+ i++
112
+ template()
113
+ } else if (c == '/' && !ends(out.at(-1))) {
114
+ let start = i++
115
+ let klass = false
116
+ while (i < src.length && src[i] != '\n') {
117
+ if (src[i] == '\\') i++
118
+ else if (src[i] == '[') klass = true
119
+ else if (src[i] == ']') klass = false
120
+ else if (src[i] == '/' && !klass) break
121
+ i++
122
+ }
123
+ i++
124
+ while (/[a-z]/.test(src[i] ?? '')) i++
125
+ push(start, i)
126
+ } else {
127
+ let match = (re: RegExp) => {
128
+ re.lastIndex = i
129
+ return re.exec(src)?.[0]
130
+ }
131
+ let word = match(IDENT) ?? match(NUMBER) ?? match(PUNCT) ?? c
132
+ let start = i
133
+ i += word.length
134
+ if (word == '{' || word == '(' || word == '[') {
135
+ push(start, i)
136
+ open.push(word)
137
+ depth++
138
+ } else if (word == '}' || word == ')' || word == ']') {
139
+ let was = open.pop()
140
+ depth = Math.max(0, depth - 1)
141
+ if (was == '${') template()
142
+ else push(start, i)
143
+ } else push(start, i)
144
+ }
145
+ }
146
+ return out
147
+ }
148
+
149
+ /** What a cell's body declares at its own top level. */
150
+ export let declared = (body: string): Declared => {
151
+ let ts = tokens(body)
152
+ let names = new Set<string>()
153
+ let declarations: Record<string, string> = {}
154
+ let at = (n: number) => ts[n]?.text
155
+ // Whether the token at n starts a statement of the body.
156
+ let starts = (n: number) => {
157
+ let before = ts[n - 1]
158
+ return !before || before.text == ';' || before.text == '}' ||
159
+ ts[n].line && ends(before)
160
+ }
161
+ // The names a binding pattern from n binds, and where it ends.
162
+ let pattern = (n: number): number => {
163
+ let t = ts[n]
164
+ if (!t) return n
165
+ if (IDENT_START.test(t.text)) {
166
+ names.add(t.text)
167
+ return n + 1
168
+ }
169
+ if (t.text != '{' && t.text != '[') return n
170
+ let base = t.depth
171
+ let i = n + 1
172
+ while (ts[i] && !(ts[i].depth == base && /^[}\]]$/.test(ts[i].text))) {
173
+ let text = ts[i].text
174
+ if (text == '=') {
175
+ // A default value: skipped to the next element of this pattern.
176
+ let level = ts[i].depth
177
+ i++
178
+ while (
179
+ ts[i] && !(ts[i].depth == level && ts[i].text == ',') &&
180
+ ts[i].depth >= level &&
181
+ !(ts[i].depth == level - 1)
182
+ ) i++
183
+ continue
184
+ }
185
+ if (
186
+ IDENT_START.test(text) &&
187
+ /^[,}\]=]$/.test(at(i + 1) ?? '') && at(i - 1) != '.'
188
+ ) names.add(text)
189
+ i++
190
+ }
191
+ return i + 1
192
+ }
193
+ // The end of a declaration's initializer at depth 0: a comma for the next
194
+ // declarator, a semicolon, or a new statement on a new line.
195
+ let initialized = (n: number): { next: number; more: boolean } => {
196
+ let i = n
197
+ while (ts[i]) {
198
+ let t = ts[i]
199
+ if (t.depth == 0 && t.text == ',') return { next: i + 1, more: true }
200
+ if (t.depth == 0 && t.text == ';') return { next: i + 1, more: false }
201
+ if (
202
+ i > n && t.depth == 0 && t.line && ends(ts[i - 1]) &&
203
+ IDENT_START.test(t.text)
204
+ ) return { next: i, more: false }
205
+ i++
206
+ }
207
+ return { next: i, more: false }
208
+ }
209
+ // The source of a declaration from n: through the brace closing its body.
210
+ let whole = (n: number) => {
211
+ let i = n
212
+ let entered = false
213
+ while (ts[i]) {
214
+ if (ts[i].depth == 0 && ts[i].text == '{') entered = true
215
+ if (entered && ts[i].depth == 0 && ts[i].text == '}') break
216
+ i++
217
+ }
218
+ return { source: body.slice(ts[n].start, ts[i]?.end), next: i + 1 }
219
+ }
220
+ let i = 0
221
+ while (i < ts.length) {
222
+ let t = ts[i]
223
+ if (t.depth != 0 || !starts(i)) {
224
+ i++
225
+ continue
226
+ }
227
+ if (/^(let|const|var)$/.test(t.text) && /^[\w${[]/.test(at(i + 1) ?? '')) {
228
+ let n = i + 1
229
+ while (true) {
230
+ n = pattern(n)
231
+ let { next, more } = at(n) == '=' ? initialized(n + 1) : {
232
+ next: at(n) == ',' ? n + 1 : at(n) == ';' ? n + 1 : n,
233
+ more: at(n) == ',',
234
+ }
235
+ n = next
236
+ if (!more) break
237
+ }
238
+ i = Math.max(n, i + 1)
239
+ continue
240
+ }
241
+ let fn = t.text == 'async' && at(i + 1) == 'function' ? i + 1 : i
242
+ if (at(fn) == 'function' || t.text == 'class') {
243
+ let named = at(fn + 1) == '*' ? fn + 2 : fn + 1
244
+ let name = at(named)
245
+ if (name && IDENT_START.test(name)) {
246
+ let { source, next } = whole(i)
247
+ declarations[name] = source
248
+ names.delete(name)
249
+ i = next
250
+ continue
251
+ }
252
+ }
253
+ i++
254
+ }
255
+ for (let name of Object.keys(declarations)) names.delete(name)
256
+ return { names: [...names], declarations }
257
+ }