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,315 @@
1
+ // CSV, as Red's codec: load-csv and to-csv.
2
+ //
3
+ // Every field loads as a string!, as in Red: "01001" stays a postal code and
4
+ // the program converts what it means as numbers. Rows end at CRLF, LF or CR;
5
+ // a field in quotes may hold delimiters, quotes (doubled) and line breaks.
6
+ // Empty lines and a byte order mark at the start are skipped.
7
+ //
8
+ // With a header (or /as-columns) columns are named by the first line, or A,
9
+ // B ... Z, AA ... as in a spreadsheet; names are string keys, as in Red. The
10
+ // parser walks the code points once with no recursion.
11
+ import { K, isAnyString, isAnyWord } from "../kinds.js";
12
+ import { canon, intern, spell } from "../symbols.js";
13
+ import * as I64 from "../int64.js";
14
+ import { form } from "../mold.js";
15
+ import { formatDec } from "../dec.js";
16
+ import { toJson } from "./json.js";
17
+ import { NONE, TRUE, string, block, map, decimal, text, fromCodePoints, codePoints, } from "../values.js";
18
+ import { native, value, fail, hostCall } from "./define.js";
19
+ const used = (c) => c === TRUE;
20
+ const CR = 13;
21
+ const LF = 10;
22
+ const QUOTE = 34;
23
+ const BOM = 0xfeff;
24
+ /** The delimiter as code points: a char or a string, a comma when not given. */
25
+ function delimOf(c) {
26
+ if (c.t === K.Char)
27
+ return [c.v];
28
+ if (c.t === K.String) {
29
+ const d = c.s.data.slice(c.i);
30
+ return d.length === 0 ? null : d;
31
+ }
32
+ return [44];
33
+ }
34
+ const isSpace = (cp) => cp === 32 || cp === 9;
35
+ /** Parses CSV text into rows of fields; returns an error message when a quote never closes. */
36
+ export function parseCsv(src, o) {
37
+ const rows = [];
38
+ const n = src.length;
39
+ const d = o.delim;
40
+ const atDelim = (i) => {
41
+ for (let k = 0; k < d.length; k++)
42
+ if (src[i + k] !== d[k])
43
+ return false;
44
+ return true;
45
+ };
46
+ const atEol = (i) => src[i] === CR || src[i] === LF;
47
+ let i = src[0] === BOM ? 1 : 0;
48
+ let line = 1;
49
+ let row = [];
50
+ while (i < n) {
51
+ // an empty line is no row
52
+ if (row.length === 0 && atEol(i)) {
53
+ i += src[i] === CR && src[i + 1] === LF ? 2 : 1;
54
+ line++;
55
+ continue;
56
+ }
57
+ const field = [];
58
+ if (o.trim)
59
+ while (isSpace(src[i]))
60
+ i++;
61
+ const quoted = src[i] === o.quote;
62
+ if (quoted) {
63
+ const start = line;
64
+ i++;
65
+ for (;;) {
66
+ if (i >= n)
67
+ return `a quoted field on line ${start} has no closing quote`;
68
+ const c = src[i];
69
+ if (c === o.quote) {
70
+ if (src[i + 1] === o.quote) {
71
+ field.push(c);
72
+ i += 2;
73
+ continue;
74
+ }
75
+ i++;
76
+ break;
77
+ }
78
+ if (c === LF || (c === CR && src[i + 1] !== LF))
79
+ line++;
80
+ field.push(c);
81
+ i++;
82
+ }
83
+ if (o.trim)
84
+ while (isSpace(src[i]))
85
+ i++;
86
+ }
87
+ // unquoted text, or what follows a closing quote, runs to the delimiter
88
+ while (i < n && !atEol(i) && !atDelim(i))
89
+ field.push(src[i++]);
90
+ if (o.trim && !quoted)
91
+ while (isSpace(field[field.length - 1]))
92
+ field.pop();
93
+ row.push(fromCodePoints(field));
94
+ if (i < n && atDelim(i)) {
95
+ i += d.length;
96
+ // a delimiter at the end of the line leaves an empty last field
97
+ if (i >= n || atEol(i))
98
+ row.push('');
99
+ else
100
+ continue;
101
+ }
102
+ rows.push(row);
103
+ row = [];
104
+ if (i < n) {
105
+ i += src[i] === CR && src[i + 1] === LF ? 2 : 1;
106
+ line++;
107
+ }
108
+ }
109
+ return rows;
110
+ }
111
+ /** A spreadsheet's name for a column: A ... Z, AA, AB ... */
112
+ function letters(k) {
113
+ let s = '';
114
+ for (let x = k + 1; x > 0; x = Math.floor((x - 1) / 26))
115
+ s = String.fromCharCode(65 + ((x - 1) % 26)) + s;
116
+ return s;
117
+ }
118
+ /** Column names: the header's, then letters for columns past it; a repeated name gets " 2", " 3" ... */
119
+ function columnNames(header, count) {
120
+ const names = [];
121
+ const seen = new Set();
122
+ for (let k = 0; k < count; k++) {
123
+ const base = header !== null && k < header.length ? header[k] : letters(k);
124
+ let name = base;
125
+ for (let m = 2; seen.has(canon(intern(name))); m++)
126
+ name = `${base} ${m}`;
127
+ seen.add(canon(intern(name)));
128
+ names.push(name);
129
+ }
130
+ return names;
131
+ }
132
+ const field = (row, k) => (k < row.length ? string(row[k]) : NONE);
133
+ native('load-csv', '["Reads CSV text into a block of rows, each a block of strings." data [string! binary!] /with "Another delimiter than a comma" delimiter [char! string!] /header "The first line names the columns; gives columns unless /as-records" /as-columns "A map of columns: name and block of values" /as-records "A block of maps, one per row; implies /header" /flat "One block with every field" /trim "Leave out spaces around fields" /quote "Another quote character than a double quote" qt-char [char!]]', (_t, f) => {
134
+ const a = f.args;
135
+ const x = a[0];
136
+ const src = x.t === K.Binary
137
+ ? codePoints(new TextDecoder().decode(new Uint8Array(x.s.data.slice(x.i))))
138
+ : x.s.data.slice(x.i);
139
+ const delim = used(a[1]) ? delimOf(a[2]) : [44];
140
+ if (delim === null)
141
+ return fail('script', 'bad-delimiter', 'load-csv needs a delimiter that is not empty');
142
+ const header = used(a[3]);
143
+ const columns = used(a[4]);
144
+ const records = used(a[5]);
145
+ const flat = used(a[6]);
146
+ if (flat && (header || columns || records))
147
+ return fail('script', 'bad-refines', 'load-csv/flat cannot have a header, columns or records');
148
+ if (columns && records)
149
+ return fail('script', 'bad-refines', 'load-csv gives columns or records, not both');
150
+ const r = parseCsv(src, { delim, quote: used(a[8]) ? a[9].v : QUOTE, trim: used(a[7]) });
151
+ if (typeof r === 'string')
152
+ return fail('syntax', 'csv', r);
153
+ if (flat)
154
+ return value(block(r.flat().map(s => string(s))));
155
+ if (!header && !columns && !records)
156
+ return value(block(r.map(row => block(row.map(s => string(s))))));
157
+ const named = header || records;
158
+ const head = named ? r[0] ?? [] : null;
159
+ const body = named ? r.slice(1) : r;
160
+ const names = columnNames(head, body.reduce((m, row) => Math.max(m, row.length), head?.length ?? 0));
161
+ if (records) {
162
+ return value(block(body.map(row => map(names.flatMap((name, k) => [string(name), field(row, k)])))));
163
+ }
164
+ return value(map(names.flatMap((name, k) => [string(name), block(body.map(row => field(row, k)))])));
165
+ });
166
+ /**
167
+ * A value as the text of a field: none is empty, strings and words as they are, dates in ISO 8601
168
+ * (a space before the time, as spreadsheets read it) and amounts as plain numbers, as to-json
169
+ * writes them; the rest formed.
170
+ */
171
+ export function fieldText(c) {
172
+ if (c.t === K.None || c.t === K.Unset)
173
+ return '';
174
+ if (c.t === K.Date)
175
+ return JSON.parse(toJson(c)).replace('T', ' ');
176
+ if (c.t === K.Money)
177
+ return formatDec(c.v, 2);
178
+ if (isAnyString(c.t) && c.t !== K.Binary)
179
+ return fromCodePoints(c.s.data, c.i);
180
+ if (isAnyWord(c.t))
181
+ return spell(c.sym);
182
+ return form(c);
183
+ }
184
+ /** A field written for CSV: in quotes when it holds the delimiter, a quote, a line break or edge spaces. */
185
+ function quoteField(s, delim, q) {
186
+ const needs = s.includes(delim) || s.includes(q) || s.includes('\n') || s.includes('\r')
187
+ || (s.length > 0 && (s[0] === ' ' || s[s.length - 1] === ' '));
188
+ return needs ? q + s.split(q).join(q + q) + q : s;
189
+ }
190
+ /** The keys of a record (map or object) and its value for a key. */
191
+ function recordKeys(c) {
192
+ if (c.t === K.Map)
193
+ return c.s.data.filter((_, k) => k % 2 === 0);
194
+ if (c.t === K.Object)
195
+ return c.ctx.keys.map(sym => ({ t: K.Word, sym, bind: null }));
196
+ return [];
197
+ }
198
+ const keyCanon = (k) => canon(intern(fieldText(k)));
199
+ function recordGet(c, key) {
200
+ if (c.t === K.Map) {
201
+ const d = c.s.data;
202
+ for (let k = 0; k + 1 < d.length; k += 2)
203
+ if (keyCanon(d[k]) === key)
204
+ return d[k + 1];
205
+ return NONE;
206
+ }
207
+ if (c.t === K.Object) {
208
+ const k = c.ctx.keys.findIndex(sym => canon(sym) === key);
209
+ return k < 0 ? NONE : c.ctx.vals[k];
210
+ }
211
+ return NONE;
212
+ }
213
+ /** Rows of a header and records: the header is every key in the order first seen. */
214
+ function recordRows(recs) {
215
+ const keys = [];
216
+ const seen = new Set();
217
+ for (const r of recs) {
218
+ for (const k of recordKeys(r)) {
219
+ const c = keyCanon(k);
220
+ if (!seen.has(c)) {
221
+ seen.add(c);
222
+ keys.push(k);
223
+ }
224
+ }
225
+ }
226
+ return [keys, ...recs.map(r => keys.map(k => recordGet(r, keyCanon(k))))];
227
+ }
228
+ const isRecord = (c) => c.t === K.Map || c.t === K.Object;
229
+ const itemsOf = (c) => (c.t === K.Block ? c.s.data.slice(c.i) : [c]);
230
+ /** Values a language writes its own way; integers stay plain, so an id gains no thousands. */
231
+ const LOCAL_KINDS = new Set([K.Decimal, K.Percent, K.Money, K.Date, K.Time]);
232
+ /** The CSV text of rows whose fields are already texts. */
233
+ function csvText(texts, delim, q) {
234
+ return texts.map(row => row.map(s => quoteField(s, delim, q)).join(delim) + '\n').join('');
235
+ }
236
+ /** The rows to write: a step when the data has no shape to-csv knows. */
237
+ function rowsOf(a) {
238
+ const data = a[0];
239
+ if (isRecord(data)) {
240
+ // a map or object of columns: its keys are the header
241
+ const keys = recordKeys(data);
242
+ const cols = keys.map(k => itemsOf(recordGet(data, keyCanon(k))));
243
+ const count = cols.reduce((m, c) => Math.max(m, c.length), 0);
244
+ const rows = [keys];
245
+ for (let k = 0; k < count; k++)
246
+ rows.push(cols.map(c => c[k] ?? NONE));
247
+ return rows;
248
+ }
249
+ const items = data.s.data.slice(data.i);
250
+ if (used(a[3])) {
251
+ const size = I64.toNumber(a[4].v);
252
+ if (size < 1)
253
+ return fail('script', 'bad-skip', 'to-csv/skip needs a size of 1 or more');
254
+ const rows = [];
255
+ for (let k = 0; k < items.length; k += size)
256
+ rows.push(items.slice(k, k + size));
257
+ return rows;
258
+ }
259
+ if (items.length > 0 && items.every(isRecord))
260
+ return recordRows(items);
261
+ if (items.length > 0 && items[0].t === K.Block) {
262
+ if (!items.every(c => c.t === K.Block))
263
+ return fail('script', 'bad-rows', 'to-csv needs every row to be a block');
264
+ return items.map(c => c.s.data.slice(c.i));
265
+ }
266
+ if (items.some(c => c.t === K.Block || isRecord(c))) {
267
+ return fail('script', 'bad-rows', 'to-csv needs rows all of one kind: blocks, maps or objects');
268
+ }
269
+ // a block of plain values is one row
270
+ return items.length === 0 ? [] : [items];
271
+ }
272
+ native('to-csv', '["Writes CSV text from rows: a block of blocks, of maps or objects (a header from their keys), a flat block with /skip, or a map of columns." data [block! map! object!] /with "Another delimiter than a comma" delimiter [char! string!] /skip "A flat block, so many fields to a row" size [integer!] /quote "Another quote character than a double quote" qt-char [char!] /language "Dates and amounts as a language writes them, and a semicolon where its decimals have a comma" lang [word! string!]]', (t, f, v) => {
273
+ const a = f.args;
274
+ const q = used(a[5]) ? String.fromCodePoint(a[6].v) : '"';
275
+ let given = null;
276
+ if (used(a[1])) {
277
+ const d = delimOf(a[2]);
278
+ if (d === null)
279
+ return fail('script', 'bad-delimiter', 'to-csv needs a delimiter that is not empty');
280
+ given = fromCodePoints(d);
281
+ }
282
+ const lang = a[8];
283
+ if (f.phase === 0) {
284
+ const rows = rowsOf(a);
285
+ if (!Array.isArray(rows))
286
+ return rows;
287
+ const texts = rows.map(row => row.map(fieldText));
288
+ if (!used(a[7]))
289
+ return value(string(csvText(texts, given ?? ',', q)));
290
+ const todo = [];
291
+ rows.forEach((row, r) => row.forEach((c, k) => { if (LOCAL_KINDS.has(c.t))
292
+ todo.push([r, k]); }));
293
+ f.data = { rows, texts, todo, at: 0, delim: given };
294
+ f.phase = 1;
295
+ // how the language writes a half tells its decimal separator
296
+ return hostCall(t, 'format-local', [decimal(1.5), lang], 'write values in a language');
297
+ }
298
+ const w = f.data;
299
+ if (f.phase === 1) {
300
+ if (w.delim === null)
301
+ w.delim = text(v).includes(',') ? ';' : ',';
302
+ f.phase = 2;
303
+ }
304
+ else {
305
+ const [r, k] = w.todo[w.at++];
306
+ // a date and its time with no comma between, as spreadsheets read them
307
+ const local = text(v);
308
+ w.texts[r][k] = w.rows[r][k].t === K.Date ? local.replace(', ', ' ') : local;
309
+ }
310
+ if (w.at < w.todo.length) {
311
+ const [r, k] = w.todo[w.at];
312
+ return hostCall(t, 'format-local', [w.rows[r][k], lang], 'write values in a language');
313
+ }
314
+ return value(string(csvText(w.texts, w.delim, q)));
315
+ });
@@ -0,0 +1,11 @@
1
+ import { type BlockCell, type StringCell } from '../values.ts';
2
+ import type { Runtime, Step } from '../eval.ts';
3
+ /**
4
+ * Where the code of a script starts: after a `Trebol [...]` header if it has
5
+ * one. Returns the header block too (null without one).
6
+ */
7
+ export declare function scriptStart(code: BlockCell): {
8
+ header: BlockCell | null;
9
+ start: number;
10
+ };
11
+ export declare function loadUser(rt: Runtime, src: StringCell): Step;