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,273 @@
1
+ // JSON, with our own parser so 64-bit ids survive: integers stay integer!
2
+ // (exact up to 64 bits), and only numbers with a fraction or exponent, or
3
+ // beyond 64 bits, become decimal!.
4
+ //
5
+ // Objects load as map! with word keys when the key is a plain identifier
6
+ // (so paths work: `client/name`), and string keys otherwise. Arrays load as
7
+ // block!, null as none. The parser keeps its own stack: deep JSON never
8
+ // touches the JS stack limit.
9
+ import { K, isAnyString, isAnyWord } from "../kinds.js";
10
+ import { intern, spell } from "../symbols.js";
11
+ import * as I64 from "../int64.js";
12
+ import { formatDec } from "../dec.js";
13
+ import { NONE, TRUE, FALSE, integer, decimal, string, text, } from "../values.js";
14
+ import { formatTime } from "../mold.js";
15
+ import { native, value, fail } from "./define.js";
16
+ const KEY_WORD = /^[A-Za-z_][A-Za-z0-9_-]*$/;
17
+ /** A JSON object key as a map key: a word when it reads as one. */
18
+ function keyCell(k) {
19
+ return KEY_WORD.test(k) ? { t: K.Word, sym: intern(k), bind: null } : string(k);
20
+ }
21
+ /** Parses JSON text; returns the value or an error message. */
22
+ export function parseJson(src) {
23
+ let i = 0;
24
+ const stack = [];
25
+ let result = null;
26
+ const ws = () => {
27
+ while (i < src.length && ' \t\n\r'.includes(src[i]))
28
+ i++;
29
+ };
30
+ const err = (what) => `invalid JSON at ${i + 1}: ${what}`;
31
+ const str = () => {
32
+ // src[i] is the opening quote
33
+ let out = '';
34
+ i++;
35
+ while (i < src.length) {
36
+ const c = src[i++];
37
+ if (c === '"')
38
+ return out;
39
+ if (c !== '\\') {
40
+ out += c;
41
+ continue;
42
+ }
43
+ const e = src[i++];
44
+ switch (e) {
45
+ case '"':
46
+ case '\\':
47
+ case '/':
48
+ out += e;
49
+ break;
50
+ case 'b':
51
+ out += '\b';
52
+ break;
53
+ case 'f':
54
+ out += '\f';
55
+ break;
56
+ case 'n':
57
+ out += '\n';
58
+ break;
59
+ case 'r':
60
+ out += '\r';
61
+ break;
62
+ case 't':
63
+ out += '\t';
64
+ break;
65
+ case 'u': {
66
+ const hex = src.slice(i, i + 4);
67
+ if (!/^[0-9a-fA-F]{4}$/.test(hex))
68
+ return null;
69
+ out += String.fromCharCode(parseInt(hex, 16)); // surrogate pairs join in the JS string
70
+ i += 4;
71
+ break;
72
+ }
73
+ default: return null;
74
+ }
75
+ }
76
+ return null;
77
+ };
78
+ /** Delivers a finished value to the open container, or as the result. */
79
+ const put = (v) => {
80
+ const top = stack[stack.length - 1];
81
+ if (top === undefined) {
82
+ result = v;
83
+ return null;
84
+ }
85
+ if (top.kind === 'array') {
86
+ top.out.push(v);
87
+ }
88
+ else {
89
+ top.out.push(top.key, v);
90
+ top.key = null;
91
+ }
92
+ return null;
93
+ };
94
+ for (;;) {
95
+ ws();
96
+ const top = stack[stack.length - 1];
97
+ if (top !== undefined && top.kind === 'object' && top.key === null) {
98
+ // expecting a key or the end of the object
99
+ if (src[i] === '}' && top.out.length === 0) {
100
+ i++;
101
+ stack.pop();
102
+ put({ t: K.Map, s: { data: top.out } });
103
+ }
104
+ else {
105
+ if (src[i] !== '"')
106
+ return err('expected a key');
107
+ const k = str();
108
+ if (k === null)
109
+ return err('bad string');
110
+ ws();
111
+ if (src[i] !== ':')
112
+ return err('expected :');
113
+ i++;
114
+ top.key = keyCell(k);
115
+ continue;
116
+ }
117
+ }
118
+ else if (result === null || top !== undefined) {
119
+ if (top !== undefined && top.kind === 'array' && src[i] === ']' && top.out.length === 0) {
120
+ i++;
121
+ stack.pop();
122
+ put({ t: K.Block, s: { data: top.out }, i: 0 });
123
+ }
124
+ else {
125
+ const c = src[i];
126
+ if (c === undefined)
127
+ return err('unexpected end');
128
+ if (c === '{') {
129
+ i++;
130
+ stack.push({ kind: 'object', out: [], key: null });
131
+ continue;
132
+ }
133
+ if (c === '[') {
134
+ i++;
135
+ stack.push({ kind: 'array', out: [], key: null });
136
+ continue;
137
+ }
138
+ let v;
139
+ if (c === '"') {
140
+ const s = str();
141
+ if (s === null)
142
+ return err('bad string');
143
+ v = string(s);
144
+ }
145
+ else if (src.startsWith('true', i)) {
146
+ i += 4;
147
+ v = TRUE;
148
+ }
149
+ else if (src.startsWith('false', i)) {
150
+ i += 5;
151
+ v = FALSE;
152
+ }
153
+ else if (src.startsWith('null', i)) {
154
+ i += 4;
155
+ v = NONE;
156
+ }
157
+ else {
158
+ const m = /^-?(?:0|[1-9]\d*)(\.\d+)?([eE][+-]?\d+)?/.exec(src.slice(i, i + 400));
159
+ if (m === null)
160
+ return err('unexpected character');
161
+ i += m[0].length;
162
+ const n = m[1] === undefined && m[2] === undefined ? I64.parse(m[0]) : null;
163
+ v = n !== null ? integer(n) : decimal(Number(m[0]));
164
+ }
165
+ put(v);
166
+ }
167
+ }
168
+ // after a value: a separator, a closer, or the end
169
+ for (;;) {
170
+ ws();
171
+ const open = stack[stack.length - 1];
172
+ if (open === undefined) {
173
+ if (i < src.length)
174
+ return err('extra text after the value');
175
+ return result;
176
+ }
177
+ const c = src[i];
178
+ if (c === ',') {
179
+ i++;
180
+ break;
181
+ }
182
+ if ((open.kind === 'array' && c === ']') || (open.kind === 'object' && c === '}')) {
183
+ i++;
184
+ stack.pop();
185
+ put(open.kind === 'array' ? { t: K.Block, s: { data: open.out }, i: 0 } : { t: K.Map, s: { data: open.out } });
186
+ continue;
187
+ }
188
+ return err(c === undefined ? 'unexpected end' : `expected , or ${open.kind === 'array' ? ']' : '}'}`);
189
+ }
190
+ }
191
+ }
192
+ function jsonString(s) {
193
+ return JSON.stringify(s);
194
+ }
195
+ /** Functions have no JSON form: a field holding one is left out, as JSON.stringify does. */
196
+ const isFunc = (c) => c.t === K.Function || c.t === K.Native || c.t === K.Op;
197
+ /** Serializes a value as JSON. Cycles, and functions in a block, become null. */
198
+ export function toJson(v, seen = new Set()) {
199
+ switch (v.t) {
200
+ case K.None:
201
+ case K.Unset: return 'null';
202
+ case K.Logic: return v.v ? 'true' : 'false';
203
+ case K.Integer: return I64.toString(v.v);
204
+ case K.Decimal:
205
+ case K.Percent: return Number.isFinite(v.v) ? String(v.v) : 'null';
206
+ case K.Money: return formatDec(v.v);
207
+ case K.Char: return jsonString(String.fromCodePoint(v.v));
208
+ case K.Date: {
209
+ const d = v.v;
210
+ const pad = (n, w = 2) => String(n).padStart(w, '0');
211
+ let s = `${pad(d.y, 4)}-${pad(d.m)}-${pad(d.d)}`;
212
+ if (d.time !== null) {
213
+ const secs = d.time / 1e9;
214
+ const whole = Math.floor(secs % 60);
215
+ const frac = secs % 1 === 0 ? '' : String(Number((secs % 1).toFixed(9))).slice(1);
216
+ s += `T${pad(Math.floor(secs / 3600))}:${pad(Math.floor((secs % 3600) / 60))}:${pad(whole)}${frac}`;
217
+ if (d.zone !== null) {
218
+ const z = Math.abs(d.zone);
219
+ s += d.zone === 0 ? 'Z' : `${d.zone < 0 ? '-' : '+'}${pad(Math.floor(z / 60))}:${pad(z % 60)}`;
220
+ }
221
+ }
222
+ return jsonString(s);
223
+ }
224
+ case K.Time: return jsonString(formatTime(v.v));
225
+ case K.Pair: return `[${v.x},${v.y}]`;
226
+ case K.Tuple: return `[${v.v.join(',')}]`;
227
+ case K.Map: {
228
+ if (seen.has(v.s))
229
+ return 'null';
230
+ seen.add(v.s);
231
+ const parts = [];
232
+ for (let k = 0; k + 1 < v.s.data.length; k += 2) {
233
+ const key = v.s.data[k];
234
+ if (isFunc(v.s.data[k + 1]))
235
+ continue;
236
+ const name = isAnyWord(key.t) ? spell(key.sym) : isAnyString(key.t) ? text(key) : toJson(key);
237
+ parts.push(`${jsonString(name)}:${toJson(v.s.data[k + 1], seen)}`);
238
+ }
239
+ seen.delete(v.s);
240
+ return `{${parts.join(',')}}`;
241
+ }
242
+ case K.Object:
243
+ case K.Error: {
244
+ if (seen.has(v.ctx))
245
+ return 'null';
246
+ seen.add(v.ctx);
247
+ const parts = v.ctx.keys.flatMap((key, k) => isFunc(v.ctx.vals[k]) ? [] : [`${jsonString(spell(key))}:${toJson(v.ctx.vals[k], seen)}`]);
248
+ seen.delete(v.ctx);
249
+ return `{${parts.join(',')}}`;
250
+ }
251
+ case K.Block:
252
+ case K.Paren: {
253
+ if (seen.has(v.s))
254
+ return 'null';
255
+ seen.add(v.s);
256
+ const out = `[${v.s.data.slice(v.i).map(c => toJson(c, seen)).join(',')}]`;
257
+ seen.delete(v.s);
258
+ return out;
259
+ }
260
+ }
261
+ if (isAnyString(v.t))
262
+ return jsonString(text(v));
263
+ if (isAnyWord(v.t))
264
+ return jsonString(spell(v.sym));
265
+ return 'null';
266
+ }
267
+ native('load-json', '["Reads JSON text into Trebol values: objects as maps, lists as blocks." source [string! binary!]]', (_t, f) => {
268
+ const x = f.args[0];
269
+ const src = x.t === K.Binary ? new TextDecoder().decode(new Uint8Array(x.s.data.slice(x.i))) : text(x);
270
+ const r = parseJson(src);
271
+ return typeof r === 'string' ? fail('syntax', 'json', r) : value(r);
272
+ });
273
+ native('to-json', '["Writes a value as JSON text." value [any-type!]]', (_t, f) => value(string(toJson(f.args[0]))));
@@ -0,0 +1,3 @@
1
+ import { type DateVal } from '../values.ts';
2
+ /** Nanoseconds from date `e` to date `d`, zones taken into account. */
3
+ export declare function dateDiff(d: DateVal, e: DateVal): number;