trebol 0.1.0 → 0.1.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.
Files changed (106) hide show
  1. package/README.md +8 -2
  2. package/dist/cli.js +55 -20
  3. package/dist/lib/core/access.d.ts +10 -0
  4. package/dist/lib/core/access.js +210 -0
  5. package/dist/lib/core/base.d.ts +4 -0
  6. package/dist/lib/core/base.js +50 -0
  7. package/dist/lib/core/bitset.d.ts +9 -0
  8. package/dist/lib/core/bitset.js +75 -0
  9. package/dist/lib/core/boot.d.ts +18 -0
  10. package/dist/lib/core/boot.js +72 -0
  11. package/dist/lib/core/compare.d.ts +9 -0
  12. package/dist/lib/core/compare.js +156 -0
  13. package/dist/lib/core/ctx.d.ts +36 -0
  14. package/dist/lib/core/ctx.js +251 -0
  15. package/dist/lib/core/dec.d.ts +28 -0
  16. package/dist/lib/core/dec.js +158 -0
  17. package/dist/lib/core/errors.d.ts +15 -0
  18. package/dist/lib/core/errors.js +44 -0
  19. package/dist/lib/core/escapes.d.ts +14 -0
  20. package/dist/lib/core/escapes.js +80 -0
  21. package/dist/lib/core/eval.d.ts +220 -0
  22. package/dist/lib/core/eval.js +666 -0
  23. package/dist/lib/core/func.d.ts +64 -0
  24. package/dist/lib/core/func.js +129 -0
  25. package/dist/lib/core/hash.d.ts +5 -0
  26. package/dist/lib/core/hash.js +155 -0
  27. package/dist/lib/core/index.d.ts +18 -0
  28. package/dist/lib/core/index.js +20 -0
  29. package/dist/lib/core/int64.d.ts +22 -0
  30. package/dist/lib/core/int64.js +113 -0
  31. package/dist/lib/core/kinds.d.ts +67 -0
  32. package/dist/lib/core/kinds.js +134 -0
  33. package/dist/lib/core/lexer.d.ts +13 -0
  34. package/dist/lib/core/lexer.js +702 -0
  35. package/dist/lib/core/mezz.d.ts +1 -0
  36. package/dist/lib/core/mezz.js +360 -0
  37. package/dist/lib/core/mold.d.ts +20 -0
  38. package/dist/lib/core/mold.js +434 -0
  39. package/dist/lib/core/natives/control.d.ts +1 -0
  40. package/dist/lib/core/natives/control.js +374 -0
  41. package/dist/lib/core/natives/csv.d.ts +15 -0
  42. package/dist/lib/core/natives/csv.js +315 -0
  43. package/dist/lib/core/natives/data.d.ts +11 -0
  44. package/dist/lib/core/natives/data.js +367 -0
  45. package/dist/lib/core/natives/define.d.ts +17 -0
  46. package/dist/lib/core/natives/define.js +47 -0
  47. package/dist/lib/core/natives/encode.d.ts +3 -0
  48. package/dist/lib/core/natives/encode.js +187 -0
  49. package/dist/lib/core/natives/json.d.ts +5 -0
  50. package/dist/lib/core/natives/json.js +273 -0
  51. package/dist/lib/core/natives/math.d.ts +3 -0
  52. package/dist/lib/core/natives/math.js +500 -0
  53. package/dist/lib/core/natives/parse.d.ts +1 -0
  54. package/dist/lib/core/natives/parse.js +600 -0
  55. package/dist/lib/core/natives/series.d.ts +4 -0
  56. package/dist/lib/core/natives/series.js +509 -0
  57. package/dist/lib/core/natives/text.d.ts +1 -0
  58. package/dist/lib/core/natives/text.js +484 -0
  59. package/dist/lib/core/natives/xml.d.ts +9 -0
  60. package/dist/lib/core/natives/xml.js +242 -0
  61. package/dist/lib/core/series.d.ts +26 -0
  62. package/dist/lib/core/series.js +73 -0
  63. package/dist/lib/core/symbols.d.ts +9 -0
  64. package/dist/lib/core/symbols.js +44 -0
  65. package/dist/lib/core/values.d.ts +206 -0
  66. package/dist/lib/core/values.js +223 -0
  67. package/dist/lib/host/browser.d.ts +4 -0
  68. package/dist/lib/host/browser.js +4081 -0
  69. package/dist/lib/host/chart.d.ts +26 -0
  70. package/dist/lib/host/chart.js +158 -0
  71. package/dist/lib/host/compress.d.ts +1 -0
  72. package/dist/lib/host/compress.js +37 -0
  73. package/dist/lib/host/draw.d.ts +31 -0
  74. package/dist/lib/host/draw.js +348 -0
  75. package/dist/lib/host/icons.d.ts +3 -0
  76. package/dist/lib/host/icons.js +97 -0
  77. package/dist/lib/host/index.d.ts +38 -0
  78. package/dist/lib/host/index.js +190 -0
  79. package/dist/lib/host/locale.d.ts +40 -0
  80. package/dist/lib/host/locale.js +241 -0
  81. package/dist/lib/host/markdown.d.ts +28 -0
  82. package/dist/lib/host/markdown.js +151 -0
  83. package/dist/lib/host/modules.d.ts +10 -0
  84. package/dist/lib/host/modules.js +262 -0
  85. package/dist/lib/host/natives.d.ts +6 -0
  86. package/dist/lib/host/natives.js +98 -0
  87. package/dist/lib/host/net.d.ts +12 -0
  88. package/dist/lib/host/net.js +541 -0
  89. package/dist/lib/host/page.d.ts +9 -0
  90. package/dist/lib/host/page.js +55 -0
  91. package/dist/lib/host/rows.d.ts +15 -0
  92. package/dist/lib/host/rows.js +80 -0
  93. package/dist/lib/host/scheduler.d.ts +45 -0
  94. package/dist/lib/host/scheduler.js +173 -0
  95. package/dist/lib/host/theme.d.ts +58 -0
  96. package/dist/lib/host/theme.js +1462 -0
  97. package/dist/lib/ui/i18n.trb +111 -0
  98. package/dist/lib/ui/ui.trb +1237 -0
  99. package/dist/trebol.js +123 -56
  100. package/package.json +17 -5
  101. package/src/host/browser.ts +274 -33
  102. package/src/host/index.ts +2 -2
  103. package/src/host/modules.ts +8 -1
  104. package/src/host/theme.ts +63 -4
  105. package/src/ui/i18n.trb +4 -0
  106. package/src/ui/ui.trb +48 -15
@@ -0,0 +1,600 @@
1
+ // PARSE: a PEG-style rule interpreter over blocks, strings and binaries.
2
+ //
3
+ // Like the evaluator, it never recurses on the JS stack: rules run on an
4
+ // explicit stack of frames that talk through one result register
5
+ // (`st.res`: true, false, or null while a frame is just starting). A paren
6
+ // action returns a `do` Step to the evaluator and the native continues
7
+ // with its value, so an action can `fetch` (await) in the middle of a parse.
8
+ //
9
+ // Rules are ordered choice (`a | b`), greedy repetition and no
10
+ // backtracking into an item that already matched, as in PEG.
11
+ //
12
+ // `remove`, `insert` and `change` edit the input in place, as in R3 and Red;
13
+ // a failing alternative does not undo an edit it made.
14
+ import { K, isAnyBlock, isAnyString, kindBit } from "../kinds.js";
15
+ import { intern, canon, spell } from "../symbols.js";
16
+ import * as I64 from "../int64.js";
17
+ import { NONE, TRUE, logic, strictEqual, } from "../values.js";
18
+ import { getWord, setWord } from "../ctx.js";
19
+ import { isEqual, foldCase } from "../compare.js";
20
+ import { bitHas, bitsFrom } from "../bitset.js";
21
+ import { itemsFor, splice } from "../series.js";
22
+ import { native, value, fail, truthy } from "./define.js";
23
+ const KW = {
24
+ Some: 0, Any: 1, Opt: 2, While: 3, Not: 4, Ahead: 5, To: 6, Thru: 7, Set: 8, Copy: 9,
25
+ Into: 10, If: 11, Collect: 12, Keep: 13, End: 14, Skip: 15, None: 16, Fail: 17, Break: 18, Quote: 19, Bar: 20,
26
+ Remove: 21, Insert: 22, Change: 23, Only: 24, Accept: 25, Reject: 26, Return: 27,
27
+ };
28
+ const KEYWORDS = new Map([
29
+ ['some', KW.Some], ['any', KW.Any], ['opt', KW.Opt], ['while', KW.While], ['not', KW.Not],
30
+ ['ahead', KW.Ahead], ['to', KW.To], ['thru', KW.Thru], ['set', KW.Set], ['copy', KW.Copy],
31
+ ['into', KW.Into], ['if', KW.If], ['collect', KW.Collect], ['keep', KW.Keep], ['end', KW.End],
32
+ ['skip', KW.Skip], ['none', KW.None], ['fail', KW.Fail], ['break', KW.Break], ['quote', KW.Quote],
33
+ ['remove', KW.Remove], ['insert', KW.Insert], ['change', KW.Change], ['only', KW.Only],
34
+ ['accept', KW.Accept], ['reject', KW.Reject], ['return', KW.Return],
35
+ ['|', KW.Bar],
36
+ ].map(([name, k]) => [canon(intern(name)), k]));
37
+ function kw(c) {
38
+ return c !== undefined && c.t === K.Word ? KEYWORDS.get(canon(c.sym)) ?? -1 : -1;
39
+ }
40
+ /** Index just past the rule item starting at `ip`, or -1 if the rules end too early. */
41
+ function itemEnd(d, ip) {
42
+ for (;;) {
43
+ const c = d[ip];
44
+ if (c === undefined)
45
+ return -1;
46
+ if (c.t === K.Integer) {
47
+ ip += d[ip + 1]?.t === K.Integer ? 2 : 1;
48
+ continue;
49
+ }
50
+ switch (kw(c)) {
51
+ case KW.Some:
52
+ case KW.Any:
53
+ case KW.Opt:
54
+ case KW.While:
55
+ case KW.Not:
56
+ case KW.Ahead:
57
+ case KW.To:
58
+ case KW.Thru:
59
+ case KW.Into:
60
+ case KW.Keep:
61
+ case KW.Remove:
62
+ case KW.Return:
63
+ ip += 1;
64
+ continue;
65
+ case KW.Insert: {
66
+ const v = ip + (kw(d[ip + 1]) === KW.Only ? 2 : 1);
67
+ return v < d.length ? v + 1 : -1;
68
+ }
69
+ case KW.Change: {
70
+ const end = itemEnd(d, ip + (kw(d[ip + 1]) === KW.Only ? 2 : 1));
71
+ return end >= 0 && end < d.length ? end + 1 : -1;
72
+ }
73
+ case KW.Set:
74
+ case KW.Copy:
75
+ ip += 2;
76
+ continue;
77
+ case KW.Collect:
78
+ ip += kw(d[ip + 1]) === KW.Set ? 3 : 1;
79
+ continue;
80
+ case KW.If:
81
+ return d[ip + 1]?.t === K.Paren ? ip + 2 : -1;
82
+ case KW.Quote:
83
+ return ip + 1 < d.length ? ip + 2 : -1;
84
+ }
85
+ return ip + 1;
86
+ }
87
+ }
88
+ // --- frames --------------------------------------------------------------------
89
+ const PF = { Block: 0, Rep: 1, Look: 2, Scan: 3, Cap: 4, Into: 5, Coll: 6, Act: 7, Edit: 8 };
90
+ /** Records a malformed-rule error; the native turns it into a Trebol error. */
91
+ function ruleErr(st, msg) {
92
+ st.err = msg;
93
+ return null;
94
+ }
95
+ const lenOf = (st) => st.input.s.data.length;
96
+ /** The matched part of the input from `from` to the current position. */
97
+ function slice(st, from) {
98
+ const d = st.input.s.data.slice(from, st.pos);
99
+ return { t: st.input.t, s: { data: d }, i: 0 };
100
+ }
101
+ function elementAt(st, i) {
102
+ const x = st.input.s.data[i];
103
+ if (x === undefined)
104
+ return NONE;
105
+ if (isAnyBlock(st.input.t))
106
+ return x;
107
+ return st.input.t === K.Binary ? { t: K.Integer, v: x } : { t: K.Char, v: x };
108
+ }
109
+ /** Matches a terminal value at the current position; returns the new position or -1. */
110
+ function term(st, v) {
111
+ const d = st.input.s.data;
112
+ const pos = st.pos;
113
+ if (isAnyBlock(st.input.t)) {
114
+ const el = d[pos];
115
+ if (el === undefined)
116
+ return -1;
117
+ switch (v.t) {
118
+ case K.Datatype: return el.t === v.k ? pos + 1 : -1;
119
+ case K.Typeset: return (v.bits & kindBit(el.t)) !== 0n ? pos + 1 : -1;
120
+ case K.LitWord:
121
+ return el.t === K.Word && canon(el.sym) === canon(v.sym) ? pos + 1 : -1;
122
+ }
123
+ return (st.cased ? strictEqual(el, v) : isEqual(el, v)) ? pos + 1 : -1;
124
+ }
125
+ const bytes = st.input.t === K.Binary;
126
+ const eq = (a, b) => (st.cased || bytes ? a === b : foldCase(a) === foldCase(b));
127
+ if (v.t === K.Bitset) {
128
+ const cp = d[pos];
129
+ return cp !== undefined && bitHas(v, cp) ? pos + 1 : -1;
130
+ }
131
+ if (v.t === K.Char || isAnyString(v.t) || v.t === K.Binary || (bytes && v.t === K.Integer)) {
132
+ const run = itemsFor(st.input.t, v, false).add;
133
+ if (pos + run.length > d.length)
134
+ return -1;
135
+ for (let k = 0; k < run.length; k++)
136
+ if (!eq(d[pos + k], run[k]))
137
+ return -1;
138
+ return pos + run.length;
139
+ }
140
+ ruleErr(st, `cannot match ${v.t === K.Integer ? 'an integer' : 'that value'} in a string`);
141
+ return -1;
142
+ }
143
+ /** Replaces the input from `from` to `to` with `v` and moves past it. */
144
+ function put(st, from, to, v, only) {
145
+ const { add, nl } = itemsFor(st.input.t, v, only);
146
+ splice(st.input.s, from, to - from, add, nl);
147
+ st.pos = from + add.length;
148
+ st.edits++;
149
+ return done(st, true);
150
+ }
151
+ /** The value an insert or change puts in: a paren is evaluated, a word gives its value. */
152
+ function putValue(st, rules, ip, from, to, only) {
153
+ const c = rules.data[ip];
154
+ if (c.t === K.Paren) {
155
+ st.stack.push({ k: PF.Act, how: 'put', from, to, only });
156
+ return { k: 'do', s: c.s, i: c.i };
157
+ }
158
+ if (c.t === K.Word) {
159
+ const v = getWord(c);
160
+ if (v === null || v.t === K.Unset)
161
+ return ruleErr(st, 'the value to put in has no value');
162
+ return put(st, from, to, v, only);
163
+ }
164
+ return put(st, from, to, c, only);
165
+ }
166
+ /** Ends the whole parse with a value, for `return`. */
167
+ function stop(st, v) {
168
+ st.ret = v;
169
+ st.stack.length = 0;
170
+ return null;
171
+ }
172
+ const spellKw = (w) => spell(w.sym);
173
+ /** Sets the result register. */
174
+ function done(st, ok) {
175
+ st.res = ok;
176
+ return null;
177
+ }
178
+ function subBlock(st, rules, ip) {
179
+ const c = rules.data[ip];
180
+ if (c !== undefined && c.t === K.Block)
181
+ return c;
182
+ if (c !== undefined && c.t === K.Word) {
183
+ const v = getWord(c);
184
+ if (v !== null && v.t === K.Block)
185
+ return v;
186
+ }
187
+ return ruleErr(st, 'into needs a rule block');
188
+ }
189
+ function pushBlock(st, b) {
190
+ st.stack.push({ k: PF.Block, rules: b.s, ip: b.i, next: 0, alt: 0, mark: null, markLen: 0, started: false });
191
+ return null;
192
+ }
193
+ /** Starts the rule item at `ip`: either finishes it at once or pushes a frame. */
194
+ function startItem(st, rules, ip) {
195
+ const d = rules.data;
196
+ const c = d[ip];
197
+ switch (c.t) {
198
+ case K.Integer: {
199
+ const min = I64.toNumber(c.v);
200
+ const second = d[ip + 1];
201
+ const two = second?.t === K.Integer;
202
+ st.stack.push({ k: PF.Rep, rules, sub: ip + (two ? 2 : 1), min, max: two ? I64.toNumber(second.v) : min, n: 0, from: st.pos, edits: st.edits, stall: false });
203
+ return null;
204
+ }
205
+ case K.SetWord:
206
+ setWord(c, { ...st.input, i: st.pos });
207
+ return done(st, true);
208
+ case K.GetWord: {
209
+ const v = getWord(c);
210
+ if (v === null || !(isAnyBlock(v.t) || isAnyString(v.t)) || v.s !== st.input.s) {
211
+ return ruleErr(st, 'a get-word in parse must refer to a position in the input');
212
+ }
213
+ st.pos = v.i;
214
+ return done(st, true);
215
+ }
216
+ case K.Paren:
217
+ st.stack.push({ k: PF.Act, how: 'action' });
218
+ return { k: 'do', s: c.s, i: c.i };
219
+ case K.Block:
220
+ return pushBlock(st, c);
221
+ case K.Word:
222
+ break;
223
+ default: {
224
+ const to = term(st, c);
225
+ if (to < 0)
226
+ return done(st, false);
227
+ st.pos = to;
228
+ return done(st, true);
229
+ }
230
+ }
231
+ const w = c;
232
+ switch (kw(w)) {
233
+ case KW.Some:
234
+ st.stack.push({ k: PF.Rep, rules, sub: ip + 1, min: 1, max: Infinity, n: 0, from: st.pos, edits: st.edits, stall: false });
235
+ return null;
236
+ case KW.Any:
237
+ case KW.While:
238
+ st.stack.push({ k: PF.Rep, rules, sub: ip + 1, min: 0, max: Infinity, n: 0, from: st.pos, edits: st.edits, stall: kw(w) === KW.While });
239
+ return null;
240
+ case KW.Opt:
241
+ st.stack.push({ k: PF.Rep, rules, sub: ip + 1, min: 0, max: 1, n: 0, from: st.pos, edits: st.edits, stall: false });
242
+ return null;
243
+ case KW.Not:
244
+ case KW.Ahead:
245
+ st.stack.push({ k: PF.Look, rules, sub: ip + 1, not: kw(w) === KW.Not, from: st.pos });
246
+ return null;
247
+ case KW.To:
248
+ case KW.Thru:
249
+ st.stack.push({ k: PF.Scan, rules, sub: ip + 1, thru: kw(w) === KW.Thru, start: st.pos, at: st.pos });
250
+ return null;
251
+ case KW.Set:
252
+ case KW.Copy: {
253
+ const word = d[ip + 1];
254
+ if (word === undefined || word.t !== K.Word)
255
+ return ruleErr(st, `${kw(w) === KW.Set ? 'set' : 'copy'} needs a word`);
256
+ st.stack.push({ k: PF.Cap, rules, sub: ip + 2, how: kw(w) === KW.Set ? 'set' : 'copy', word, from: st.pos });
257
+ return null;
258
+ }
259
+ case KW.Keep: {
260
+ const next = d[ip + 1];
261
+ if (next !== undefined && next.t === K.Paren) {
262
+ st.stack.push({ k: PF.Act, how: 'keep' });
263
+ return { k: 'do', s: next.s, i: next.i };
264
+ }
265
+ st.stack.push({ k: PF.Cap, rules, sub: ip + 1, how: 'keep', word: null, from: st.pos });
266
+ return null;
267
+ }
268
+ case KW.Collect: {
269
+ const named = kw(d[ip + 1]) === KW.Set;
270
+ const word = named ? d[ip + 2] : undefined;
271
+ if (named && (word === undefined || word.t !== K.Word))
272
+ return ruleErr(st, 'collect set needs a word');
273
+ st.stack.push({ k: PF.Coll, rules, sub: ip + (named ? 3 : 1), word: word ?? null });
274
+ return null;
275
+ }
276
+ case KW.Into: {
277
+ const el = st.input.s.data[st.pos];
278
+ const rule = subBlock(st, rules, ip + 1);
279
+ if (rule === null)
280
+ return null;
281
+ if (!isAnyBlock(st.input.t) || el === undefined || !(isAnyBlock(el.t) || isAnyString(el.t)))
282
+ return done(st, false);
283
+ st.stack.push({ k: PF.Into, outer: st.input, outerPos: st.pos });
284
+ st.input = el;
285
+ st.pos = el.i;
286
+ return pushBlock(st, rule);
287
+ }
288
+ case KW.If: {
289
+ const p = d[ip + 1];
290
+ st.stack.push({ k: PF.Act, how: 'if' });
291
+ return { k: 'do', s: p.s, i: p.i };
292
+ }
293
+ case KW.End: return done(st, st.pos >= lenOf(st));
294
+ case KW.Skip:
295
+ if (st.pos >= lenOf(st))
296
+ return done(st, false);
297
+ st.pos++;
298
+ return done(st, true);
299
+ case KW.None: return done(st, true);
300
+ case KW.Fail: return done(st, false);
301
+ case KW.Break:
302
+ case KW.Accept:
303
+ case KW.Reject: {
304
+ // As in R3: they end the innermost rule block, break and accept as a match where
305
+ // the input is, reject as a failure. When that block is the body of a repetition
306
+ // (opt too), the repetition ends: with break counting this turn, with reject not.
307
+ const reject = kw(w) === KW.Reject;
308
+ let blk = null;
309
+ while (st.stack.length > 0 && blk === null) {
310
+ const f = st.stack.pop();
311
+ if (f.k === PF.Block)
312
+ blk = f;
313
+ else if (f.k === PF.Into) {
314
+ st.input = f.outer;
315
+ st.pos = f.outerPos;
316
+ }
317
+ }
318
+ if (blk === null)
319
+ return ruleErr(st, `${spellKw(w)} outside of a rule block`);
320
+ const rep = st.stack[st.stack.length - 1];
321
+ if (rep !== undefined && rep.k === PF.Rep) {
322
+ st.stack.pop();
323
+ if (!reject)
324
+ return done(st, true);
325
+ st.pos = rep.from;
326
+ return done(st, rep.n >= rep.min);
327
+ }
328
+ if (!reject)
329
+ return done(st, true);
330
+ st.pos = blk.alt;
331
+ if (blk.mark !== null)
332
+ blk.mark.length = blk.markLen;
333
+ return done(st, false);
334
+ }
335
+ case KW.Return: {
336
+ const next = d[ip + 1];
337
+ if (next !== undefined && next.t === K.Paren) {
338
+ st.stack.push({ k: PF.Act, how: 'return' });
339
+ return { k: 'do', s: next.s, i: next.i };
340
+ }
341
+ st.stack.push({ k: PF.Cap, rules, sub: ip + 1, how: 'return', word: null, from: st.pos });
342
+ return null;
343
+ }
344
+ case KW.Quote: {
345
+ const to = term(st, d[ip + 1]);
346
+ if (to < 0)
347
+ return done(st, false);
348
+ st.pos = to;
349
+ return done(st, true);
350
+ }
351
+ case KW.Remove:
352
+ st.stack.push({ k: PF.Edit, rules, sub: ip + 1, val: -1, only: false, from: st.pos });
353
+ return null;
354
+ case KW.Insert: {
355
+ const only = kw(d[ip + 1]) === KW.Only;
356
+ return putValue(st, rules, ip + (only ? 2 : 1), st.pos, st.pos, only);
357
+ }
358
+ case KW.Change: {
359
+ const only = kw(d[ip + 1]) === KW.Only;
360
+ const sub = ip + (only ? 2 : 1);
361
+ st.stack.push({ k: PF.Edit, rules, sub, val: itemEnd(d, sub), only, from: st.pos });
362
+ return null;
363
+ }
364
+ case KW.Only:
365
+ return ruleErr(st, 'only goes after insert or change');
366
+ case KW.Bar:
367
+ return ruleErr(st, 'misplaced |');
368
+ }
369
+ const v = getWord(w);
370
+ if (v === null || v.t === K.Unset || v.t === K.None)
371
+ return ruleErr(st, `rule word has no value`);
372
+ if (v.t === K.Block)
373
+ return pushBlock(st, v);
374
+ const to = term(st, v);
375
+ if (to < 0)
376
+ return done(st, false);
377
+ st.pos = to;
378
+ return done(st, true);
379
+ }
380
+ /** Runs frames until the parse finishes (null) or an action needs the evaluator. */
381
+ function runParse(st, actionValue) {
382
+ if (actionValue !== null) {
383
+ const f = st.stack.pop();
384
+ switch (f.how) {
385
+ case 'action':
386
+ st.res = true;
387
+ break;
388
+ case 'if':
389
+ st.res = truthy(actionValue);
390
+ break;
391
+ case 'keep': {
392
+ const into = st.colls[st.colls.length - 1];
393
+ if (into === undefined)
394
+ return ruleErr(st, 'keep outside of collect');
395
+ if (actionValue.t !== K.Unset)
396
+ into.push(actionValue);
397
+ st.res = true;
398
+ break;
399
+ }
400
+ case 'put':
401
+ if (actionValue.t === K.Unset)
402
+ return ruleErr(st, 'the value to put in has no value');
403
+ put(st, f.from, f.to, actionValue, f.only);
404
+ break;
405
+ case 'return':
406
+ return stop(st, actionValue);
407
+ }
408
+ }
409
+ for (;;) {
410
+ const f = st.stack[st.stack.length - 1];
411
+ if (f === undefined)
412
+ return null;
413
+ const r = st.res;
414
+ st.res = null;
415
+ const step = frame(st, f, r);
416
+ if (step !== null || st.err !== null)
417
+ return step;
418
+ }
419
+ }
420
+ function frame(st, f, r) {
421
+ const pop = (ok) => {
422
+ st.stack.pop();
423
+ return done(st, ok);
424
+ };
425
+ switch (f.k) {
426
+ case PF.Block: {
427
+ const d = f.rules.data;
428
+ if (!f.started) {
429
+ f.started = true;
430
+ f.alt = st.pos;
431
+ f.mark = st.colls[st.colls.length - 1] ?? null;
432
+ f.markLen = f.mark?.length ?? 0;
433
+ }
434
+ else if (r === true) {
435
+ f.ip = f.next;
436
+ }
437
+ else if (r === false) {
438
+ let bar = f.ip;
439
+ while (bar < d.length && kw(d[bar]) !== KW.Bar)
440
+ bar++;
441
+ st.pos = f.alt;
442
+ if (f.mark !== null)
443
+ f.mark.length = f.markLen;
444
+ if (bar >= d.length)
445
+ return pop(false);
446
+ f.ip = bar + 1;
447
+ }
448
+ if (f.ip >= d.length || kw(d[f.ip]) === KW.Bar)
449
+ return pop(true);
450
+ f.next = itemEnd(d, f.ip);
451
+ if (f.next < 0)
452
+ return ruleErr(st, 'rule ends too early');
453
+ return startItem(st, f.rules, f.ip);
454
+ }
455
+ case PF.Rep: {
456
+ if (r === null) {
457
+ f.from = st.pos;
458
+ return startItem(st, f.rules, f.sub);
459
+ }
460
+ if (r === false) {
461
+ st.pos = f.from;
462
+ return pop(f.n >= f.min);
463
+ }
464
+ f.n++;
465
+ // a match that did not advance (nor edit) would repeat forever: stop here, unless it is while's
466
+ if ((!f.stall && st.pos === f.from && st.edits === f.edits) || f.n >= f.max)
467
+ return pop(true);
468
+ f.from = st.pos;
469
+ f.edits = st.edits;
470
+ return startItem(st, f.rules, f.sub);
471
+ }
472
+ case PF.Look: {
473
+ if (r === null) {
474
+ f.from = st.pos;
475
+ return startItem(st, f.rules, f.sub);
476
+ }
477
+ st.pos = f.from;
478
+ return pop(f.not ? !r : r);
479
+ }
480
+ case PF.Scan: {
481
+ if (r === true) {
482
+ if (!f.thru)
483
+ st.pos = f.at;
484
+ return pop(true);
485
+ }
486
+ if (r === false) {
487
+ f.at++;
488
+ if (f.at > lenOf(st)) {
489
+ st.pos = f.start;
490
+ return pop(false);
491
+ }
492
+ }
493
+ st.pos = f.at;
494
+ return startItem(st, f.rules, f.sub);
495
+ }
496
+ case PF.Cap: {
497
+ if (r === null) {
498
+ f.from = st.pos;
499
+ return startItem(st, f.rules, f.sub);
500
+ }
501
+ if (r === false)
502
+ return pop(false);
503
+ if (f.how === 'return') {
504
+ return stop(st, slice(st, f.from));
505
+ }
506
+ else if (f.how === 'set') {
507
+ setWord(f.word, st.pos > f.from ? elementAt(st, f.from) : NONE);
508
+ }
509
+ else if (f.how === 'copy') {
510
+ setWord(f.word, slice(st, f.from));
511
+ }
512
+ else {
513
+ const into = st.colls[st.colls.length - 1];
514
+ if (into === undefined)
515
+ return ruleErr(st, 'keep outside of collect');
516
+ if (st.pos - f.from === 1)
517
+ into.push(elementAt(st, f.from));
518
+ else if (st.pos > f.from)
519
+ into.push(slice(st, f.from));
520
+ }
521
+ return pop(true);
522
+ }
523
+ case PF.Into: {
524
+ const ok = r === true && st.pos >= lenOf(st);
525
+ st.input = f.outer;
526
+ st.pos = ok ? f.outerPos + 1 : f.outerPos;
527
+ return pop(ok);
528
+ }
529
+ case PF.Coll: {
530
+ if (r === null) {
531
+ st.colls.push([]);
532
+ return startItem(st, f.rules, f.sub);
533
+ }
534
+ const out = st.colls.pop();
535
+ if (r) {
536
+ const b = { t: K.Block, s: { data: out }, i: 0 };
537
+ if (f.word !== null)
538
+ setWord(f.word, b);
539
+ else if (st.colls.length > 0)
540
+ st.colls[st.colls.length - 1].push(b);
541
+ else
542
+ st.out = b;
543
+ }
544
+ return pop(r);
545
+ }
546
+ case PF.Edit: {
547
+ if (r === null) {
548
+ f.from = st.pos;
549
+ return startItem(st, f.rules, f.sub);
550
+ }
551
+ if (r === false)
552
+ return pop(false);
553
+ st.stack.pop();
554
+ if (f.val < 0) {
555
+ splice(st.input.s, f.from, st.pos - f.from, []);
556
+ st.pos = f.from;
557
+ st.edits++;
558
+ return done(st, true);
559
+ }
560
+ return putValue(st, f.rules, f.val, f.from, st.pos, f.only);
561
+ }
562
+ case PF.Act:
563
+ return ruleErr(st, 'internal: action frame without a value');
564
+ }
565
+ }
566
+ native('parse', '["Checks the input against a block of rules; true if they match all of it." input [series!] rules [block!] /case]', (_t, f, v) => {
567
+ if (f.phase === 0) {
568
+ f.phase = 1;
569
+ const input = f.args[0];
570
+ const st = { input, pos: input.i, stack: [], res: null, colls: [], out: null, cased: f.args[2] === TRUE, err: null, edits: 0, ret: null };
571
+ pushBlock(st, f.args[1]);
572
+ f.data = st;
573
+ }
574
+ const st = f.data;
575
+ const step = runParse(st, v);
576
+ if (st.err !== null)
577
+ return fail('script', 'parse', st.err);
578
+ if (step !== null)
579
+ return step;
580
+ if (st.ret !== null)
581
+ return value(st.ret);
582
+ if (st.out !== null)
583
+ return value(st.out);
584
+ return value(logic(st.res === true && st.pos >= lenOf(st)));
585
+ });
586
+ // --- bitsets -------------------------------------------------------------------
587
+ native('charset', '["Makes a bitset of characters, for parse." spec [char! integer! any-string! binary! block!]]', (_t, f) => {
588
+ const bytes = [];
589
+ const err = bitsFrom(f.args[0], bytes);
590
+ return err !== null ? fail('script', 'bad-bitset', err) : value({ t: K.Bitset, s: { data: bytes }, neg: false });
591
+ });
592
+ native('complement', '["The opposite: a bitset of everything else, a logic negated, an integer with its bits flipped." value [bitset! logic! integer!]]', (_t, f) => {
593
+ const x = f.args[0];
594
+ if (x.t === K.Bitset)
595
+ return value({ t: K.Bitset, s: { data: x.s.data.slice() }, neg: !x.neg });
596
+ if (x.t === K.Logic)
597
+ return value(logic(!x.v));
598
+ const r = I64.fromBig(~BigInt(x.v));
599
+ return value(r === null ? NONE : { t: K.Integer, v: r });
600
+ });
@@ -0,0 +1,4 @@
1
+ import { type Cell } from '../values.ts';
2
+ import { type SeriesCell } from '../series.ts';
3
+ /** The cell for element `i` of a series. */
4
+ export declare function cellAt(s: SeriesCell, i: number): Cell;