trebol 0.1.0 → 0.1.2

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 (160) hide show
  1. package/README.md +18 -3
  2. package/dist/cli.js +13790 -11417
  3. package/dist/fonts/OFL.txt +93 -0
  4. package/dist/fonts/inter-cyrillic-ext.woff2 +0 -0
  5. package/dist/fonts/inter-cyrillic.woff2 +0 -0
  6. package/dist/fonts/inter-greek-ext.woff2 +0 -0
  7. package/dist/fonts/inter-greek.woff2 +0 -0
  8. package/dist/fonts/inter-latin-ext.woff2 +0 -0
  9. package/dist/fonts/inter-latin.woff2 +0 -0
  10. package/dist/fonts/inter-vietnamese.woff2 +0 -0
  11. package/dist/lib/core/access.d.ts +10 -0
  12. package/dist/lib/core/access.js +210 -0
  13. package/dist/lib/core/base.d.ts +4 -0
  14. package/dist/lib/core/base.js +50 -0
  15. package/dist/lib/core/bitset.d.ts +9 -0
  16. package/dist/lib/core/bitset.js +75 -0
  17. package/dist/lib/core/boot.d.ts +18 -0
  18. package/dist/lib/core/boot.js +73 -0
  19. package/dist/lib/core/compare.d.ts +9 -0
  20. package/dist/lib/core/compare.js +156 -0
  21. package/dist/lib/core/ctx.d.ts +36 -0
  22. package/dist/lib/core/ctx.js +251 -0
  23. package/dist/lib/core/dec.d.ts +28 -0
  24. package/dist/lib/core/dec.js +158 -0
  25. package/dist/lib/core/errors.d.ts +15 -0
  26. package/dist/lib/core/errors.js +44 -0
  27. package/dist/lib/core/escapes.d.ts +14 -0
  28. package/dist/lib/core/escapes.js +80 -0
  29. package/dist/lib/core/eval.d.ts +220 -0
  30. package/dist/lib/core/eval.js +666 -0
  31. package/dist/lib/core/func.d.ts +68 -0
  32. package/dist/lib/core/func.js +129 -0
  33. package/dist/lib/core/hash.d.ts +5 -0
  34. package/dist/lib/core/hash.js +155 -0
  35. package/dist/lib/core/index.d.ts +18 -0
  36. package/dist/lib/core/index.js +20 -0
  37. package/dist/lib/core/int64.d.ts +22 -0
  38. package/dist/lib/core/int64.js +113 -0
  39. package/dist/lib/core/kinds.d.ts +67 -0
  40. package/dist/lib/core/kinds.js +134 -0
  41. package/dist/lib/core/lexer.d.ts +13 -0
  42. package/dist/lib/core/lexer.js +702 -0
  43. package/dist/lib/core/mezz.d.ts +1 -0
  44. package/dist/lib/core/mezz.js +360 -0
  45. package/dist/lib/core/mold.d.ts +20 -0
  46. package/dist/lib/core/mold.js +434 -0
  47. package/dist/lib/core/natives/control.d.ts +1 -0
  48. package/dist/lib/core/natives/control.js +374 -0
  49. package/dist/lib/core/natives/csv.d.ts +15 -0
  50. package/dist/lib/core/natives/csv.js +315 -0
  51. package/dist/lib/core/natives/data.d.ts +11 -0
  52. package/dist/lib/core/natives/data.js +367 -0
  53. package/dist/lib/core/natives/define.d.ts +17 -0
  54. package/dist/lib/core/natives/define.js +47 -0
  55. package/dist/lib/core/natives/encode.d.ts +3 -0
  56. package/dist/lib/core/natives/encode.js +187 -0
  57. package/dist/lib/core/natives/json.d.ts +5 -0
  58. package/dist/lib/core/natives/json.js +273 -0
  59. package/dist/lib/core/natives/math.d.ts +3 -0
  60. package/dist/lib/core/natives/math.js +500 -0
  61. package/dist/lib/core/natives/parse.d.ts +1 -0
  62. package/dist/lib/core/natives/parse.js +600 -0
  63. package/dist/lib/core/natives/series.d.ts +4 -0
  64. package/dist/lib/core/natives/series.js +519 -0
  65. package/dist/lib/core/natives/text.d.ts +1 -0
  66. package/dist/lib/core/natives/text.js +487 -0
  67. package/dist/lib/core/natives/xml.d.ts +9 -0
  68. package/dist/lib/core/natives/xml.js +242 -0
  69. package/dist/lib/core/series.d.ts +26 -0
  70. package/dist/lib/core/series.js +73 -0
  71. package/dist/lib/core/symbols.d.ts +9 -0
  72. package/dist/lib/core/symbols.js +44 -0
  73. package/dist/lib/core/values.d.ts +206 -0
  74. package/dist/lib/core/values.js +223 -0
  75. package/dist/lib/host/browser.d.ts +4 -0
  76. package/dist/lib/host/browser.js +4974 -0
  77. package/dist/lib/host/chart.d.ts +26 -0
  78. package/dist/lib/host/chart.js +158 -0
  79. package/dist/lib/host/compress.d.ts +1 -0
  80. package/dist/lib/host/compress.js +37 -0
  81. package/dist/lib/host/devlog.d.ts +11 -0
  82. package/dist/lib/host/devlog.js +13 -0
  83. package/dist/lib/host/devtools.d.ts +23 -0
  84. package/dist/lib/host/devtools.js +626 -0
  85. package/dist/lib/host/draw.d.ts +31 -0
  86. package/dist/lib/host/draw.js +348 -0
  87. package/dist/lib/host/fonts/OFL.txt +93 -0
  88. package/dist/lib/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  89. package/dist/lib/host/fonts/inter-cyrillic.woff2 +0 -0
  90. package/dist/lib/host/fonts/inter-greek-ext.woff2 +0 -0
  91. package/dist/lib/host/fonts/inter-greek.woff2 +0 -0
  92. package/dist/lib/host/fonts/inter-latin-ext.woff2 +0 -0
  93. package/dist/lib/host/fonts/inter-latin.woff2 +0 -0
  94. package/dist/lib/host/fonts/inter-vietnamese.woff2 +0 -0
  95. package/dist/lib/host/fonts.d.ts +11 -0
  96. package/dist/lib/host/fonts.js +29 -0
  97. package/dist/lib/host/icons.d.ts +3 -0
  98. package/dist/lib/host/icons.js +97 -0
  99. package/dist/lib/host/index.d.ts +40 -0
  100. package/dist/lib/host/index.js +192 -0
  101. package/dist/lib/host/js.d.ts +5 -0
  102. package/dist/lib/host/js.js +130 -0
  103. package/dist/lib/host/locale.d.ts +40 -0
  104. package/dist/lib/host/locale.js +241 -0
  105. package/dist/lib/host/markdown.d.ts +30 -0
  106. package/dist/lib/host/markdown.js +161 -0
  107. package/dist/lib/host/modules.d.ts +14 -0
  108. package/dist/lib/host/modules.js +300 -0
  109. package/dist/lib/host/natives.d.ts +6 -0
  110. package/dist/lib/host/natives.js +98 -0
  111. package/dist/lib/host/net.d.ts +12 -0
  112. package/dist/lib/host/net.js +549 -0
  113. package/dist/lib/host/orbit.d.ts +121 -0
  114. package/dist/lib/host/orbit.js +374 -0
  115. package/dist/lib/host/page.d.ts +9 -0
  116. package/dist/lib/host/page.js +55 -0
  117. package/dist/lib/host/rows.d.ts +29 -0
  118. package/dist/lib/host/rows.js +109 -0
  119. package/dist/lib/host/scheduler.d.ts +51 -0
  120. package/dist/lib/host/scheduler.js +197 -0
  121. package/dist/lib/host/theme.d.ts +58 -0
  122. package/dist/lib/host/theme.js +1667 -0
  123. package/dist/lib/ui/i18n.trb +117 -0
  124. package/dist/lib/ui/test.trb +565 -0
  125. package/dist/lib/ui/ui.trb +1678 -0
  126. package/dist/trebol.js +1427 -138
  127. package/editors/vscode/README.md +15 -0
  128. package/editors/vscode/language-configuration.json +28 -0
  129. package/editors/vscode/package.json +42 -0
  130. package/editors/vscode/syntaxes/trebol.tmLanguage.json +237 -0
  131. package/package.json +23 -7
  132. package/src/core/boot.ts +1 -1
  133. package/src/core/func.ts +4 -0
  134. package/src/core/natives/control.ts +1 -1
  135. package/src/core/natives/series.ts +9 -1
  136. package/src/core/natives/text.ts +2 -0
  137. package/src/host/browser.ts +1235 -83
  138. package/src/host/devlog.ts +16 -0
  139. package/src/host/devtools.ts +641 -0
  140. package/src/host/fonts/OFL.txt +93 -0
  141. package/src/host/fonts/inter-cyrillic-ext.woff2 +0 -0
  142. package/src/host/fonts/inter-cyrillic.woff2 +0 -0
  143. package/src/host/fonts/inter-greek-ext.woff2 +0 -0
  144. package/src/host/fonts/inter-greek.woff2 +0 -0
  145. package/src/host/fonts/inter-latin-ext.woff2 +0 -0
  146. package/src/host/fonts/inter-latin.woff2 +0 -0
  147. package/src/host/fonts/inter-vietnamese.woff2 +0 -0
  148. package/src/host/fonts.ts +39 -0
  149. package/src/host/index.ts +5 -3
  150. package/src/host/js.ts +127 -0
  151. package/src/host/markdown.ts +9 -0
  152. package/src/host/modules.ts +41 -2
  153. package/src/host/net.ts +9 -1
  154. package/src/host/orbit.ts +432 -0
  155. package/src/host/rows.ts +30 -0
  156. package/src/host/scheduler.ts +62 -36
  157. package/src/host/theme.ts +270 -6
  158. package/src/ui/i18n.trb +10 -0
  159. package/src/ui/test.trb +565 -0
  160. package/src/ui/ui.trb +564 -90
@@ -0,0 +1,702 @@
1
+ // LOAD: source text to cells.
2
+ //
3
+ // A hand-written, single-pass lexer with no dependencies. It never evaluates
4
+ // anything: words stay words, parens stay parens. Every element of every
5
+ // block records its line and column (Series.pos) and whether a newline came
6
+ // before it (Series.nl), so mold can reproduce the layout and errors can
7
+ // point at the source.
8
+ //
9
+ // Classification of atomic tokens (numbers, dates, urls...) is done by
10
+ // ordered regular expressions on the token text, written as data so the same
11
+ // table can be ported to Zig.
12
+ import { K, kindByName, isAnyBlock, isAnyString } from "./kinds.js";
13
+ import { intern, spell as symName } from "./symbols.js";
14
+ import * as I64 from "./int64.js";
15
+ import { parseDec } from "./dec.js";
16
+ import { decodeBase } from "./base.js";
17
+ import { decodeEscape } from "./escapes.js";
18
+ import { NONE, UNSET, TRUE, FALSE, packPos, codePoints, integer, decimal, percent, money, char, pair, tuple, date, time, datatype, typeset, bitset, object, context, } from "./values.js";
19
+ export class LoadError extends Error {
20
+ line;
21
+ col;
22
+ file;
23
+ constructor(message, line, col, file) {
24
+ super(`${file ? file + ':' : ''}${line}:${col}: ${message}`);
25
+ this.name = 'LoadError';
26
+ this.line = line;
27
+ this.col = col;
28
+ this.file = file;
29
+ }
30
+ }
31
+ /** Loads source text into a block. Throws LoadError on malformed input. */
32
+ export function load(src, opts = {}) {
33
+ const lx = new Lexer(src, opts.file);
34
+ return { t: K.Block, s: lx.parseSeries(null), i: 0 };
35
+ }
36
+ // --- token classification table --------------------------------------------
37
+ const RE = {
38
+ integer: /^[+-]?\d[\d']*$/,
39
+ decimal: /^[+-]?(?:\d[\d']*(?:[.,]\d*)?|[.,]\d+)(?:e[+-]?\d+)?$/i,
40
+ percent: /^([+-]?(?:\d[\d']*(?:[.,]\d*)?|[.,]\d+)(?:e[+-]?\d+)?)%$/i,
41
+ pair: /^([+-]?(?:\d[\d']*(?:[.,]\d*)?|[.,]\d+)(?:e[+-]?\d+)?)x([+-]?(?:\d[\d']*(?:[.,]\d*)?|[.,]\d+)(?:e[+-]?\d+)?)$/i,
42
+ tuple: /^\d+(?:\.\d+){2,11}$/,
43
+ time: /^([+-]?)(\d+):(\d+)(?::(\d+(?:[.,]\d+)?))?$/,
44
+ date: /^(\d+)([-/])([A-Za-z]+|\d+)\2(\d+)(?:[/T](.+))?$/,
45
+ dateTime: /^(\d+):(\d+)(?::(\d+(?:[.,]\d+)?))?(Z|[+-]\d{1,2}(?::?\d{2})?)?$/i,
46
+ money: /^([+-]?)([A-Za-z]{3})?\$([+-]?)(\d[\d']*(?:[.,]\d*)?|[.,]\d+)$/,
47
+ url: /^[A-Za-z][A-Za-z0-9+.-]*:\S+$/,
48
+ email: /^[^@\s]+@[^@\s]*$/,
49
+ // Characters that may not appear anywhere in a word.
50
+ wordBad: /[\s@#$%^,:'/\\"()[\]{};]/,
51
+ };
52
+ const MONTHS = [
53
+ 'january', 'february', 'march', 'april', 'may', 'june', 'july',
54
+ 'august', 'september', 'october', 'november', 'december',
55
+ ];
56
+ const NS_PER_SEC = 1e9;
57
+ function isDelimiter(c) {
58
+ return c === undefined || c === ' ' || c === '\t' || c === '\n' || c === '\r' ||
59
+ c === '[' || c === ']' || c === '(' || c === ')' || c === '{' || c === '}' ||
60
+ c === '"' || c === ';' || c === '\f' || c === '\v';
61
+ }
62
+ function isWord(text) {
63
+ if (text === '')
64
+ return false;
65
+ if (text === '%' || text === '%%')
66
+ return true; // remainder and modulo
67
+ if (RE.wordBad.test(text))
68
+ return false;
69
+ if (/^[+-]?\.?\d/.test(text))
70
+ return false; // 1, +1, -.5, .5 are numbers
71
+ return true;
72
+ }
73
+ function num(text) {
74
+ return Number(text.replace(/'/g, '').replace(',', '.'));
75
+ }
76
+ /** The number in `text` times 10^shift, rounded once (15% is exactly 0.15). */
77
+ function shiftDecimal(text, shift) {
78
+ const clean = text.replace(/'/g, '').replace(',', '.');
79
+ const m = /^(.*?)(?:e([+-]?\d+))?$/i.exec(clean);
80
+ return Number(`${m[1]}e${Number(m[2] ?? 0) + shift}`);
81
+ }
82
+ function daysInMonth(y, m) {
83
+ if (m === 2)
84
+ return (y % 4 === 0 && y % 100 !== 0) || y % 400 === 0 ? 29 : 28;
85
+ return [4, 6, 9, 11].includes(m) ? 30 : 31;
86
+ }
87
+ // --- the lexer ---------------------------------------------------------------
88
+ class Lexer {
89
+ i = 0;
90
+ line = 1;
91
+ lineStart = 0;
92
+ src;
93
+ file;
94
+ constructor(src, file) {
95
+ // A leading byte-order mark is not part of the source.
96
+ this.src = src.charCodeAt(0) === 0xfeff ? src.slice(1) : src;
97
+ this.file = file;
98
+ }
99
+ fail(msg, at = this.i) {
100
+ // Recompute the position of `at` from the start of its line.
101
+ let line = 1;
102
+ let start = 0;
103
+ for (let j = 0; j < at && j < this.src.length; j++) {
104
+ if (this.src[j] === '\n') {
105
+ line++;
106
+ start = j + 1;
107
+ }
108
+ }
109
+ throw new LoadError(msg, line, at - start + 1, this.file);
110
+ }
111
+ col() {
112
+ return this.i - this.lineStart + 1;
113
+ }
114
+ newline(at) {
115
+ this.line++;
116
+ this.lineStart = at + 1;
117
+ }
118
+ /** Skips spaces and comments; returns true if a newline was crossed. */
119
+ skipSpace() {
120
+ let sawNewline = false;
121
+ const s = this.src;
122
+ while (this.i < s.length) {
123
+ const c = s[this.i];
124
+ if (c === '\n') {
125
+ this.newline(this.i);
126
+ sawNewline = true;
127
+ this.i++;
128
+ }
129
+ else if (c === ' ' || c === '\t' || c === '\r' || c === '\f' || c === '\v') {
130
+ this.i++;
131
+ }
132
+ else if (c === ';') {
133
+ while (this.i < s.length && s[this.i] !== '\n')
134
+ this.i++;
135
+ }
136
+ else {
137
+ break;
138
+ }
139
+ }
140
+ return sawNewline;
141
+ }
142
+ /** Parses values until `end` (']' or ')') or end of input when `end` is null. */
143
+ parseSeries(end) {
144
+ const data = [];
145
+ const nl = [];
146
+ const pos = [];
147
+ const openAt = this.i - 1;
148
+ for (;;) {
149
+ const sawNewline = this.skipSpace();
150
+ const c = this.src[this.i];
151
+ if (c === undefined) {
152
+ if (end !== null)
153
+ this.fail(`missing ${end}`, openAt);
154
+ break;
155
+ }
156
+ if (c === ']' || c === ')') {
157
+ if (c !== end)
158
+ this.fail(`unexpected ${c}`);
159
+ this.i++;
160
+ break;
161
+ }
162
+ const p = packPos(this.line, this.col());
163
+ data.push(this.parseValue());
164
+ nl.push(sawNewline);
165
+ pos.push(p);
166
+ }
167
+ const out = { data, nl, pos };
168
+ if (this.file !== undefined)
169
+ out.file = this.file;
170
+ return out;
171
+ }
172
+ parseValue() {
173
+ const s = this.src;
174
+ const c = s[this.i];
175
+ switch (c) {
176
+ case '[':
177
+ this.i++;
178
+ return { t: K.Block, s: this.parseSeries(']'), i: 0 };
179
+ case '(':
180
+ this.i++;
181
+ return { t: K.Paren, s: this.parseSeries(')'), i: 0 };
182
+ case '"':
183
+ return this.str(K.String, this.quoted());
184
+ case '{':
185
+ return this.str(K.String, this.braced());
186
+ case '}':
187
+ return this.fail('unexpected }');
188
+ case '#':
189
+ return this.hash();
190
+ case '%':
191
+ return this.percentSign();
192
+ case '<':
193
+ if (/[A-Za-z/!?]/.test(s[this.i + 1] ?? ''))
194
+ return this.tag();
195
+ break;
196
+ }
197
+ const base = /^(2|16|64)#\{/.exec(s.slice(this.i, this.i + 4));
198
+ if (base) {
199
+ this.i += base[1].length + 1;
200
+ return this.binary(Number(base[1]));
201
+ }
202
+ return this.atom();
203
+ }
204
+ str(t, cps) {
205
+ return { t, s: { data: cps }, i: 0 };
206
+ }
207
+ // --- strings ---------------------------------------------------------------
208
+ /** "..." with caret escapes; may not span lines. Cursor on the opening quote. */
209
+ quoted() {
210
+ const s = this.src;
211
+ const start = this.i;
212
+ this.i++;
213
+ const out = [];
214
+ for (;;) {
215
+ const c = s[this.i];
216
+ if (c === undefined || c === '\n')
217
+ this.fail('missing " to close string', start);
218
+ if (c === '"') {
219
+ this.i++;
220
+ return out;
221
+ }
222
+ if (c === '^') {
223
+ const e = decodeEscape(s, this.i + 1);
224
+ if (!e)
225
+ this.fail('invalid escape in string');
226
+ out.push(e.cp);
227
+ this.i = e.next;
228
+ continue;
229
+ }
230
+ const cp = s.codePointAt(this.i);
231
+ out.push(cp);
232
+ this.i += cp > 0xffff ? 2 : 1;
233
+ }
234
+ }
235
+ /** {...} with nesting, raw newlines and caret escapes. Cursor on the brace. */
236
+ braced() {
237
+ const s = this.src;
238
+ const start = this.i;
239
+ this.i++;
240
+ const out = [];
241
+ let depth = 1;
242
+ for (;;) {
243
+ const c = s[this.i];
244
+ if (c === undefined)
245
+ this.fail('missing } to close string', start);
246
+ if (c === '^') {
247
+ const e = decodeEscape(s, this.i + 1);
248
+ if (!e)
249
+ this.fail('invalid escape in string');
250
+ out.push(e.cp);
251
+ this.i = e.next;
252
+ continue;
253
+ }
254
+ if (c === '{')
255
+ depth++;
256
+ if (c === '}' && --depth === 0) {
257
+ this.i++;
258
+ return out;
259
+ }
260
+ if (c === '\n')
261
+ this.newline(this.i);
262
+ const cp = s.codePointAt(this.i);
263
+ out.push(cp);
264
+ this.i += cp > 0xffff ? 2 : 1;
265
+ }
266
+ }
267
+ tag() {
268
+ const s = this.src;
269
+ const start = this.i;
270
+ this.i++;
271
+ let quote = null;
272
+ while (this.i < s.length) {
273
+ const c = s[this.i];
274
+ if (quote) {
275
+ if (c === quote)
276
+ quote = null;
277
+ }
278
+ else if (c === '"' || c === "'") {
279
+ quote = c;
280
+ }
281
+ else if (c === '>') {
282
+ const body = s.slice(start + 1, this.i);
283
+ this.i++;
284
+ return this.str(K.Tag, codePoints(body));
285
+ }
286
+ if (c === '\n')
287
+ this.newline(this.i);
288
+ this.i++;
289
+ }
290
+ return this.fail('missing > to close tag', start);
291
+ }
292
+ // --- # forms -----------------------------------------------------------------
293
+ hash() {
294
+ const next = this.src[this.i + 1];
295
+ if (next === '"') {
296
+ this.i++;
297
+ const start = this.i;
298
+ const cps = this.quoted();
299
+ if (cps.length !== 1)
300
+ this.fail('a char! must hold exactly one character', start);
301
+ return char(cps[0]);
302
+ }
303
+ if (next === '{') {
304
+ this.i++;
305
+ return this.binary(16);
306
+ }
307
+ if (next === '[') {
308
+ this.i += 2;
309
+ const start = this.i - 2;
310
+ const inner = this.parseSeries(']');
311
+ return this.construct(inner, start);
312
+ }
313
+ if (next === '(') {
314
+ this.i += 2;
315
+ const start = this.i - 2;
316
+ const inner = this.parseSeries(')');
317
+ return this.mapOf(inner, start);
318
+ }
319
+ const start = this.i;
320
+ this.i++;
321
+ const name = this.span();
322
+ if (name === '')
323
+ this.fail('invalid issue', start);
324
+ return { t: K.Issue, sym: intern(name), bind: null };
325
+ }
326
+ /** Binary literal with the cursor on '{'. */
327
+ binary(base) {
328
+ const s = this.src;
329
+ const start = this.i;
330
+ const close = s.indexOf('}', this.i);
331
+ if (close < 0)
332
+ this.fail('missing } to close binary', start);
333
+ const body = s.slice(this.i + 1, close);
334
+ for (let j = this.i; j < close; j++)
335
+ if (s[j] === '\n')
336
+ this.newline(j);
337
+ this.i = close + 1;
338
+ const bytes = decodeBase(body, base);
339
+ if (bytes === null)
340
+ this.fail(`invalid ${base === 16 ? 'hex' : `base-${base}`} binary`, start);
341
+ return { t: K.Binary, s: { data: bytes }, i: 0 };
342
+ }
343
+ /** #( key value ... ) — keys and values are not evaluated. */
344
+ mapOf(inner, start) {
345
+ if (inner.data.length % 2 !== 0)
346
+ this.fail('a map! needs key value pairs', start);
347
+ const data = inner.data.map((c, j) => j % 2 === 0 && c.t === K.SetWord ? { t: K.Word, sym: c.sym, bind: null } : c);
348
+ return { t: K.Map, s: { data } };
349
+ }
350
+ /**
351
+ * Construction syntax #[...], for values that have no literal form:
352
+ * #[none] #[true] #[false] #[unset]
353
+ * #[datatype! integer!] #[typeset! [integer! decimal!]]
354
+ * #[bitset! #{...}] #[bitset! not #{...}]
355
+ * #[object! [a: 1]] #[map! [a 1]]
356
+ * #[string! "abc" 2] #[block! [a b] 2] (series with an index)
357
+ */
358
+ construct(inner, start) {
359
+ const d = inner.data;
360
+ const head = d[0];
361
+ const bad = () => this.fail('invalid construction syntax', start);
362
+ if (!head || head.t !== K.Word)
363
+ return bad();
364
+ const name = spellOf(head).toLowerCase();
365
+ if (d.length === 1) {
366
+ switch (name) {
367
+ case 'none':
368
+ case 'none!': return NONE;
369
+ case 'unset':
370
+ case 'unset!': return UNSET;
371
+ case 'true': return TRUE;
372
+ case 'false': return FALSE;
373
+ }
374
+ return bad();
375
+ }
376
+ const kind = kindByName(name);
377
+ if (kind === undefined)
378
+ return bad();
379
+ const arg = d[1];
380
+ switch (kind) {
381
+ case K.Logic:
382
+ if (d.length === 2 && arg.t === K.Word) {
383
+ const v = spellOf(arg).toLowerCase();
384
+ if (v === 'true')
385
+ return TRUE;
386
+ if (v === 'false')
387
+ return FALSE;
388
+ }
389
+ return bad();
390
+ case K.Datatype: {
391
+ if (d.length !== 2 || arg.t !== K.Word)
392
+ return bad();
393
+ const k = kindByName(spellOf(arg));
394
+ return k === undefined ? bad() : datatype(k);
395
+ }
396
+ case K.Typeset: {
397
+ if (d.length !== 2 || arg.t !== K.Block)
398
+ return bad();
399
+ let bits = 0n;
400
+ for (const w of arg.s.data) {
401
+ const k = w.t === K.Word ? kindByName(spellOf(w)) : undefined;
402
+ if (k === undefined)
403
+ return bad();
404
+ bits |= 1n << BigInt(k);
405
+ }
406
+ return typeset(bits);
407
+ }
408
+ case K.Bitset: {
409
+ const neg = d.length === 3 && arg.t === K.Word && spellOf(arg).toLowerCase() === 'not';
410
+ const bin = neg ? d[2] : arg;
411
+ if (bin.t !== K.Binary || d.length !== (neg ? 3 : 2))
412
+ return bad();
413
+ return bitset([...bin.s.data], neg);
414
+ }
415
+ case K.Object: {
416
+ if (d.length !== 2 || arg.t !== K.Block)
417
+ return bad();
418
+ const ctx = context();
419
+ const body = arg.s.data;
420
+ for (let j = 0; j < body.length; j += 2) {
421
+ const key = body[j];
422
+ if (key.t !== K.SetWord || j + 1 >= body.length)
423
+ return bad();
424
+ ctx.keys.push(key.sym);
425
+ ctx.vals.push(literalValue(body[j + 1]));
426
+ }
427
+ return object(ctx);
428
+ }
429
+ case K.Map:
430
+ if (d.length !== 2 || arg.t !== K.Block)
431
+ return bad();
432
+ return this.mapOf(arg.s, start);
433
+ }
434
+ // A series at an index: #[kind! value index]
435
+ const at = d[2];
436
+ if (d.length === 3 && at !== undefined && at.t === K.Integer) {
437
+ const idx = I64.toNumber(at.v) - 1;
438
+ const ok = (isAnyString(kind) && isAnyString(arg.t)) || (isAnyBlock(kind) && isAnyBlock(arg.t));
439
+ if (!ok || idx < 0)
440
+ return bad();
441
+ const src = arg;
442
+ if (idx > src.s.data.length)
443
+ return bad();
444
+ return { ...src, t: kind, i: idx };
445
+ }
446
+ return bad();
447
+ }
448
+ // --- % forms -------------------------------------------------------------------
449
+ percentSign() {
450
+ const s = this.src;
451
+ const next = s[this.i + 1];
452
+ if (next === '"') {
453
+ this.i++;
454
+ return this.str(K.File, this.quoted());
455
+ }
456
+ let end = this.i;
457
+ while (!isDelimiter(s[end]))
458
+ end++;
459
+ if (/^%%?:?$/.test(s.slice(this.i, end))) {
460
+ return this.atom(); // the words % and %%, and their set-words
461
+ }
462
+ const start = this.i;
463
+ this.i++;
464
+ const body = this.span();
465
+ try {
466
+ return this.str(K.File, codePoints(decodeURIComponent(body)));
467
+ }
468
+ catch {
469
+ return this.fail('invalid file', start);
470
+ }
471
+ }
472
+ // --- atoms -----------------------------------------------------------------------
473
+ /** Reads characters up to the next delimiter. */
474
+ span() {
475
+ const s = this.src;
476
+ const start = this.i;
477
+ while (!isDelimiter(s[this.i]))
478
+ this.i++;
479
+ return s.slice(start, this.i);
480
+ }
481
+ atom() {
482
+ const start = this.i;
483
+ const text = this.span();
484
+ // A path whose next segment is a paren: a/(...)
485
+ if (text.endsWith('/') && this.src[this.i] === '(' && text.length > 1 && !RE.url.test(text)) {
486
+ this.i = start;
487
+ return this.path();
488
+ }
489
+ const cell = this.classify(text, start);
490
+ if (cell === 'path') {
491
+ this.i = start;
492
+ return this.path();
493
+ }
494
+ return cell;
495
+ }
496
+ /** Classifies a delimited token. Returns 'path' when it must be rescanned as one. */
497
+ classify(text, start) {
498
+ const c = text[0];
499
+ // Prefixed words and paths
500
+ if (c === "'" || c === ':') {
501
+ const rest = text.slice(1);
502
+ if (isWord(rest))
503
+ return this.wordCell(c === "'" ? K.LitWord : K.GetWord, rest);
504
+ if (rest.includes('/') && !rest.startsWith('/'))
505
+ return 'path';
506
+ return this.fail(`invalid ${c === "'" ? 'lit-word' : 'get-word'}: ${text}`, start);
507
+ }
508
+ // set-word (before urls: "a:" has nothing after the colon)
509
+ if (text.endsWith(':') && text.length > 1) {
510
+ const name = text.slice(0, -1);
511
+ if (isWord(name))
512
+ return this.wordCell(K.SetWord, name);
513
+ if (name.includes('/') && !name.startsWith('/') && !/^[+-]?\d/.test(name))
514
+ return 'path';
515
+ }
516
+ // Numbers and number-like scalars
517
+ if (/^[+-]?[\d.,$]/.test(text) && !isWord(text)) {
518
+ const v = this.number(text, start);
519
+ if (v)
520
+ return v;
521
+ }
522
+ if (RE.money.test(text))
523
+ return this.number(text, start);
524
+ if (RE.url.test(text))
525
+ return this.str(K.Url, codePoints(text));
526
+ if (RE.email.test(text) && c !== '/')
527
+ return this.str(K.Email, codePoints(text));
528
+ if (c === '/') {
529
+ if (text === '/' || text === '//')
530
+ return this.wordCell(K.Word, text);
531
+ const name = text.slice(1);
532
+ if (isWord(name))
533
+ return this.wordCell(K.Refinement, name);
534
+ return this.fail(`invalid refinement: ${text}`, start);
535
+ }
536
+ if (text.includes('/'))
537
+ return 'path';
538
+ if (isWord(text))
539
+ return this.wordCell(K.Word, text);
540
+ return this.fail(`invalid value: ${text}`, start);
541
+ }
542
+ wordCell(t, name) {
543
+ return { t, sym: intern(name), bind: null };
544
+ }
545
+ /** Numbers, money, percent, pair, tuple, time and date. Null if none matches. */
546
+ number(text, start) {
547
+ let m;
548
+ if (RE.integer.test(text)) {
549
+ const v = I64.parse(text);
550
+ if (v === null)
551
+ this.fail(`integer out of 64-bit range: ${text}`, start);
552
+ return integer(v);
553
+ }
554
+ if (RE.decimal.test(text))
555
+ return decimal(num(text));
556
+ if ((m = RE.percent.exec(text)))
557
+ return percent(shiftDecimal(m[1], -2));
558
+ if ((m = RE.money.exec(text))) {
559
+ const v = parseDec(m[4]);
560
+ const negative = (m[1] === '-') !== (m[3] === '-');
561
+ return money(negative ? { d: -v.d, e: v.e } : v, (m[2] ?? '').toUpperCase());
562
+ }
563
+ if ((m = RE.pair.exec(text)))
564
+ return pair(num(m[1]), num(m[2]));
565
+ if (RE.tuple.test(text)) {
566
+ const parts = text.split('.').map(Number);
567
+ if (parts.some(p => p > 255))
568
+ this.fail(`tuple values must be 0-255: ${text}`, start);
569
+ return tuple(parts);
570
+ }
571
+ if ((m = RE.time.exec(text))) {
572
+ const ns = timeNs(m[2], m[3], m[4]);
573
+ return time(m[1] === '-' ? -ns : ns);
574
+ }
575
+ if ((m = RE.date.exec(text)))
576
+ return this.dateCell(m, text, start);
577
+ return null;
578
+ }
579
+ dateCell(m, text, start) {
580
+ const [, a, , monthText, b, rest] = m;
581
+ let y;
582
+ let d;
583
+ if (a.length >= 3) {
584
+ y = Number(a);
585
+ d = Number(b);
586
+ }
587
+ else {
588
+ d = Number(a);
589
+ y = Number(b);
590
+ }
591
+ let mon;
592
+ if (/^\d+$/.test(monthText)) {
593
+ mon = Number(monthText);
594
+ }
595
+ else {
596
+ const lower = monthText.toLowerCase();
597
+ mon = lower.length >= 3 ? MONTHS.findIndex(n => n.startsWith(lower)) + 1 : 0;
598
+ }
599
+ if (mon < 1 || mon > 12 || d < 1 || d > daysInMonth(y, mon)) {
600
+ this.fail(`invalid date: ${text}`, start);
601
+ }
602
+ const v = { y, m: mon, d, time: null, zone: null };
603
+ if (rest !== undefined) {
604
+ const t = RE.dateTime.exec(rest);
605
+ if (!t)
606
+ this.fail(`invalid time in date: ${text}`, start);
607
+ v.time = timeNs(t[1], t[2], t[3]);
608
+ if (t[4] !== undefined)
609
+ v.zone = parseZone(t[4]);
610
+ }
611
+ return date(v);
612
+ }
613
+ // --- paths -----------------------------------------------------------------------
614
+ /** Scans a path starting at the cursor: a/b, :a/b, 'a/b, a/b:, a/(x)/c. */
615
+ path() {
616
+ const s = this.src;
617
+ const start = this.i;
618
+ let t = K.Path;
619
+ if (s[this.i] === ':') {
620
+ t = K.GetPath;
621
+ this.i++;
622
+ }
623
+ else if (s[this.i] === "'") {
624
+ t = K.LitPath;
625
+ this.i++;
626
+ }
627
+ const data = [];
628
+ for (;;) {
629
+ if (s[this.i] === '(') {
630
+ this.i++;
631
+ data.push({ t: K.Paren, s: this.parseSeries(')'), i: 0 });
632
+ }
633
+ else {
634
+ const segStart = this.i;
635
+ while (!isDelimiter(s[this.i]) && s[this.i] !== '/')
636
+ this.i++;
637
+ let seg = s.slice(segStart, this.i);
638
+ if (seg.endsWith(':') && isDelimiter(s[this.i]) && t === K.Path && data.length > 0) {
639
+ t = K.SetPath;
640
+ seg = seg.slice(0, -1);
641
+ }
642
+ data.push(this.segment(seg, segStart));
643
+ }
644
+ if (s[this.i] === '/') {
645
+ this.i++;
646
+ if (isDelimiter(s[this.i]) && s[this.i] !== '(')
647
+ this.fail('path ends with /', start);
648
+ continue;
649
+ }
650
+ if (!isDelimiter(s[this.i]) || s[this.i] === '(')
651
+ this.fail('invalid path', start);
652
+ break;
653
+ }
654
+ if (data[0].t !== K.Word && data[0].t !== K.Paren)
655
+ this.fail('a path must start with a word', start);
656
+ return { t, s: { data }, i: 0 };
657
+ }
658
+ segment(seg, at) {
659
+ if (seg === '')
660
+ this.fail('empty path segment', at);
661
+ if (RE.integer.test(seg)) {
662
+ const v = I64.parse(seg);
663
+ if (v !== null)
664
+ return integer(v);
665
+ }
666
+ if (seg[0] === ':' && isWord(seg.slice(1)))
667
+ return this.wordCell(K.GetWord, seg.slice(1));
668
+ if (seg[0] === "'" && isWord(seg.slice(1)))
669
+ return this.wordCell(K.LitWord, seg.slice(1));
670
+ if (isWord(seg))
671
+ return this.wordCell(K.Word, seg);
672
+ return this.fail(`invalid path segment: ${seg}`, at);
673
+ }
674
+ }
675
+ // --- helpers -----------------------------------------------------------------------
676
+ function spellOf(c) {
677
+ return 'sym' in c ? symName(c.sym) : '';
678
+ }
679
+ /** In #[object! [...]] the words none, true and false stand for their values. */
680
+ function literalValue(c) {
681
+ if (c.t === K.Word) {
682
+ switch (symName(c.sym).toLowerCase()) {
683
+ case 'none': return NONE;
684
+ case 'true': return TRUE;
685
+ case 'false': return FALSE;
686
+ }
687
+ }
688
+ return c;
689
+ }
690
+ function timeNs(h, m, sec) {
691
+ const seconds = Number(h) * 3600 + Number(m) * 60;
692
+ const frac = sec === undefined ? 0 : num(sec);
693
+ // Round to whole nanoseconds to keep values exact and comparable.
694
+ return Math.round(seconds * NS_PER_SEC + frac * NS_PER_SEC);
695
+ }
696
+ function parseZone(z) {
697
+ if (z === 'Z' || z === 'z')
698
+ return 0;
699
+ const m = /^([+-])(\d{1,2})(?::?(\d{2}))?$/.exec(z);
700
+ const minutes = Number(m[2]) * 60 + Number(m[3] ?? 0);
701
+ return m[1] === '-' ? -minutes : minutes;
702
+ }
@@ -0,0 +1 @@
1
+ export declare const MEZZANINES: string;