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.
- package/README.md +8 -2
- package/dist/cli.js +55 -20
- package/dist/lib/core/access.d.ts +10 -0
- package/dist/lib/core/access.js +210 -0
- package/dist/lib/core/base.d.ts +4 -0
- package/dist/lib/core/base.js +50 -0
- package/dist/lib/core/bitset.d.ts +9 -0
- package/dist/lib/core/bitset.js +75 -0
- package/dist/lib/core/boot.d.ts +18 -0
- package/dist/lib/core/boot.js +72 -0
- package/dist/lib/core/compare.d.ts +9 -0
- package/dist/lib/core/compare.js +156 -0
- package/dist/lib/core/ctx.d.ts +36 -0
- package/dist/lib/core/ctx.js +251 -0
- package/dist/lib/core/dec.d.ts +28 -0
- package/dist/lib/core/dec.js +158 -0
- package/dist/lib/core/errors.d.ts +15 -0
- package/dist/lib/core/errors.js +44 -0
- package/dist/lib/core/escapes.d.ts +14 -0
- package/dist/lib/core/escapes.js +80 -0
- package/dist/lib/core/eval.d.ts +220 -0
- package/dist/lib/core/eval.js +666 -0
- package/dist/lib/core/func.d.ts +64 -0
- package/dist/lib/core/func.js +129 -0
- package/dist/lib/core/hash.d.ts +5 -0
- package/dist/lib/core/hash.js +155 -0
- package/dist/lib/core/index.d.ts +18 -0
- package/dist/lib/core/index.js +20 -0
- package/dist/lib/core/int64.d.ts +22 -0
- package/dist/lib/core/int64.js +113 -0
- package/dist/lib/core/kinds.d.ts +67 -0
- package/dist/lib/core/kinds.js +134 -0
- package/dist/lib/core/lexer.d.ts +13 -0
- package/dist/lib/core/lexer.js +702 -0
- package/dist/lib/core/mezz.d.ts +1 -0
- package/dist/lib/core/mezz.js +360 -0
- package/dist/lib/core/mold.d.ts +20 -0
- package/dist/lib/core/mold.js +434 -0
- package/dist/lib/core/natives/control.d.ts +1 -0
- package/dist/lib/core/natives/control.js +374 -0
- package/dist/lib/core/natives/csv.d.ts +15 -0
- package/dist/lib/core/natives/csv.js +315 -0
- package/dist/lib/core/natives/data.d.ts +11 -0
- package/dist/lib/core/natives/data.js +367 -0
- package/dist/lib/core/natives/define.d.ts +17 -0
- package/dist/lib/core/natives/define.js +47 -0
- package/dist/lib/core/natives/encode.d.ts +3 -0
- package/dist/lib/core/natives/encode.js +187 -0
- package/dist/lib/core/natives/json.d.ts +5 -0
- package/dist/lib/core/natives/json.js +273 -0
- package/dist/lib/core/natives/math.d.ts +3 -0
- package/dist/lib/core/natives/math.js +500 -0
- package/dist/lib/core/natives/parse.d.ts +1 -0
- package/dist/lib/core/natives/parse.js +600 -0
- package/dist/lib/core/natives/series.d.ts +4 -0
- package/dist/lib/core/natives/series.js +509 -0
- package/dist/lib/core/natives/text.d.ts +1 -0
- package/dist/lib/core/natives/text.js +484 -0
- package/dist/lib/core/natives/xml.d.ts +9 -0
- package/dist/lib/core/natives/xml.js +242 -0
- package/dist/lib/core/series.d.ts +26 -0
- package/dist/lib/core/series.js +73 -0
- package/dist/lib/core/symbols.d.ts +9 -0
- package/dist/lib/core/symbols.js +44 -0
- package/dist/lib/core/values.d.ts +206 -0
- package/dist/lib/core/values.js +223 -0
- package/dist/lib/host/browser.d.ts +4 -0
- package/dist/lib/host/browser.js +4081 -0
- package/dist/lib/host/chart.d.ts +26 -0
- package/dist/lib/host/chart.js +158 -0
- package/dist/lib/host/compress.d.ts +1 -0
- package/dist/lib/host/compress.js +37 -0
- package/dist/lib/host/draw.d.ts +31 -0
- package/dist/lib/host/draw.js +348 -0
- package/dist/lib/host/icons.d.ts +3 -0
- package/dist/lib/host/icons.js +97 -0
- package/dist/lib/host/index.d.ts +38 -0
- package/dist/lib/host/index.js +190 -0
- package/dist/lib/host/locale.d.ts +40 -0
- package/dist/lib/host/locale.js +241 -0
- package/dist/lib/host/markdown.d.ts +28 -0
- package/dist/lib/host/markdown.js +151 -0
- package/dist/lib/host/modules.d.ts +10 -0
- package/dist/lib/host/modules.js +262 -0
- package/dist/lib/host/natives.d.ts +6 -0
- package/dist/lib/host/natives.js +98 -0
- package/dist/lib/host/net.d.ts +12 -0
- package/dist/lib/host/net.js +541 -0
- package/dist/lib/host/page.d.ts +9 -0
- package/dist/lib/host/page.js +55 -0
- package/dist/lib/host/rows.d.ts +15 -0
- package/dist/lib/host/rows.js +80 -0
- package/dist/lib/host/scheduler.d.ts +45 -0
- package/dist/lib/host/scheduler.js +173 -0
- package/dist/lib/host/theme.d.ts +58 -0
- package/dist/lib/host/theme.js +1462 -0
- package/dist/lib/ui/i18n.trb +111 -0
- package/dist/lib/ui/ui.trb +1237 -0
- package/dist/trebol.js +123 -56
- package/package.json +17 -5
- package/src/host/browser.ts +274 -33
- package/src/host/index.ts +2 -2
- package/src/host/modules.ts +8 -1
- package/src/host/theme.ts +63 -4
- package/src/ui/i18n.trb +4 -0
- package/src/ui/ui.trb +48 -15
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// XML in the shape of Red's community codec: every node is three values,
|
|
2
|
+
// name, content and attributes.
|
|
3
|
+
//
|
|
4
|
+
// <factura id="7"><cliente>Ana & Co</cliente><linea/></factura>
|
|
5
|
+
// [factura [cliente [none "Ana & Co" none] none linea [] none] #(id: "7")]
|
|
6
|
+
//
|
|
7
|
+
// A text is `none "text" none`, so `foreach [name content atts]` walks any
|
|
8
|
+
// content. Names are words when they read as one (as load-json keys) and
|
|
9
|
+
// strings otherwise (`"fe:Facturae"`); attributes are a map, or none when
|
|
10
|
+
// there are none. Entities and CDATA load as text; comments, processing
|
|
11
|
+
// instructions, the declaration, the DOCTYPE and the spaces only between tags
|
|
12
|
+
// are left out. The parser keeps its own stack, so deep XML never touches the
|
|
13
|
+
// JS stack limit; there is no DOMParser, so it works the same everywhere.
|
|
14
|
+
import { K } from "../kinds.js";
|
|
15
|
+
import { intern, spell } from "../symbols.js";
|
|
16
|
+
import { NONE, TRUE, string, block, map, fromCodePoints, } from "../values.js";
|
|
17
|
+
import { fieldText } from "./csv.js";
|
|
18
|
+
import { native, value, fail } from "./define.js";
|
|
19
|
+
const used = (c) => c === TRUE;
|
|
20
|
+
const NAME = /^[A-Za-z_][A-Za-z0-9_-]*$/;
|
|
21
|
+
/** A tag or attribute name: a word when it reads as one. */
|
|
22
|
+
function nameCell(n) {
|
|
23
|
+
return NAME.test(n) ? { t: K.Word, sym: intern(n), bind: null } : string(n);
|
|
24
|
+
}
|
|
25
|
+
const ENTITIES = { lt: '<', gt: '>', amp: '&', quot: '"', apos: "'" };
|
|
26
|
+
/** Text with its entities replaced; one XML does not know is kept as written. */
|
|
27
|
+
function decode(s) {
|
|
28
|
+
return s.replace(/&(#x[0-9A-Fa-f]+|#[0-9]+|[A-Za-z]+);/g, (all, e) => {
|
|
29
|
+
if (e[0] === '#') {
|
|
30
|
+
const n = e[1] === 'x' ? parseInt(e.slice(2), 16) : parseInt(e.slice(1), 10);
|
|
31
|
+
return n > 0 && n <= 0x10ffff ? String.fromCodePoint(n) : all;
|
|
32
|
+
}
|
|
33
|
+
return ENTITIES[e] ?? all;
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
/** Parses XML text into a block of nodes; returns an error message when it is not well formed. */
|
|
37
|
+
export function parseXml(src) {
|
|
38
|
+
let i = src.charCodeAt(0) === 0xfeff ? 1 : 0;
|
|
39
|
+
const n = src.length;
|
|
40
|
+
const stack = [];
|
|
41
|
+
const top = [];
|
|
42
|
+
let text = '';
|
|
43
|
+
const lineAt = (p) => {
|
|
44
|
+
let l = 1;
|
|
45
|
+
for (let k = 0; k < p; k++)
|
|
46
|
+
if (src.charCodeAt(k) === 10)
|
|
47
|
+
l++;
|
|
48
|
+
return l;
|
|
49
|
+
};
|
|
50
|
+
const out = () => (stack.length > 0 ? stack[stack.length - 1].out : top);
|
|
51
|
+
const flush = () => {
|
|
52
|
+
// spaces only between tags are layout, not text
|
|
53
|
+
if (text !== '' && /\S/.test(text))
|
|
54
|
+
out().push(NONE, string(text), NONE);
|
|
55
|
+
text = '';
|
|
56
|
+
};
|
|
57
|
+
const skipTo = (end, what) => {
|
|
58
|
+
const k = src.indexOf(end, i);
|
|
59
|
+
return k < 0 ? `${what} on line ${lineAt(i)} never ends` : k + end.length;
|
|
60
|
+
};
|
|
61
|
+
while (i < n) {
|
|
62
|
+
const lt = src.indexOf('<', i);
|
|
63
|
+
if (lt < 0) {
|
|
64
|
+
text += decode(src.slice(i));
|
|
65
|
+
break;
|
|
66
|
+
}
|
|
67
|
+
text += decode(src.slice(i, lt));
|
|
68
|
+
i = lt;
|
|
69
|
+
let to;
|
|
70
|
+
if (src.startsWith('<!--', i)) {
|
|
71
|
+
to = skipTo('-->', 'a comment');
|
|
72
|
+
}
|
|
73
|
+
else if (src.startsWith('<![CDATA[', i)) {
|
|
74
|
+
to = skipTo(']]>', 'a CDATA section');
|
|
75
|
+
if (typeof to === 'number')
|
|
76
|
+
text += src.slice(i + 9, to - 3);
|
|
77
|
+
}
|
|
78
|
+
else if (src.startsWith('<?', i)) {
|
|
79
|
+
to = skipTo('?>', 'a processing instruction');
|
|
80
|
+
}
|
|
81
|
+
else if (src.startsWith('<!', i)) {
|
|
82
|
+
// a DOCTYPE, with its internal subset in brackets
|
|
83
|
+
const open = src.indexOf('[', i);
|
|
84
|
+
const close = src.indexOf('>', i);
|
|
85
|
+
if (open >= 0 && close >= 0 && open < close) {
|
|
86
|
+
i = open;
|
|
87
|
+
to = skipTo(']', 'a DOCTYPE');
|
|
88
|
+
if (typeof to === 'number') {
|
|
89
|
+
i = to;
|
|
90
|
+
to = skipTo('>', 'a DOCTYPE');
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
else {
|
|
94
|
+
to = skipTo('>', 'a DOCTYPE');
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
else if (src[i + 1] === '/') {
|
|
98
|
+
const m = /^<\/([^\s>]+)\s*>/.exec(src.slice(i, i + 512));
|
|
99
|
+
if (m === null)
|
|
100
|
+
return `a closing tag on line ${lineAt(i)} is not written right`;
|
|
101
|
+
flush();
|
|
102
|
+
const open = stack.pop();
|
|
103
|
+
if (open === undefined)
|
|
104
|
+
return `</${m[1]}> on line ${lineAt(i)} closes no tag`;
|
|
105
|
+
if (open.name !== m[1])
|
|
106
|
+
return `</${m[1]}> on line ${lineAt(i)} closes <${open.name}> of line ${lineAt(open.at)}`;
|
|
107
|
+
out().push(nameCell(open.name), block(open.out), open.atts);
|
|
108
|
+
to = i + m[0].length;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const m = /^<([^\s/>=]+)/.exec(src.slice(i, i + 512));
|
|
112
|
+
if (m === null)
|
|
113
|
+
return `a < on line ${lineAt(i)} starts no tag; write it <`;
|
|
114
|
+
flush();
|
|
115
|
+
const name = m[1];
|
|
116
|
+
let k = i + m[0].length;
|
|
117
|
+
const atts = [];
|
|
118
|
+
for (;;) {
|
|
119
|
+
while (k < n && /\s/.test(src[k]))
|
|
120
|
+
k++;
|
|
121
|
+
if (k >= n)
|
|
122
|
+
return `<${name}> on line ${lineAt(i)} never ends`;
|
|
123
|
+
if (src[k] === '>' || src.startsWith('/>', k))
|
|
124
|
+
break;
|
|
125
|
+
const a = /^([^\s/>=]+)\s*=\s*("[^"]*"|'[^']*')/.exec(src.slice(k, k + 4096));
|
|
126
|
+
if (a === null)
|
|
127
|
+
return `an attribute of <${name}> on line ${lineAt(k)} is not written right`;
|
|
128
|
+
atts.push(nameCell(a[1]), string(decode(a[2].slice(1, -1))));
|
|
129
|
+
k += a[0].length;
|
|
130
|
+
}
|
|
131
|
+
const attsCell = atts.length === 0 ? NONE : map(atts);
|
|
132
|
+
if (src[k] === '/') {
|
|
133
|
+
out().push(nameCell(name), block([]), attsCell);
|
|
134
|
+
to = k + 2;
|
|
135
|
+
}
|
|
136
|
+
else {
|
|
137
|
+
stack.push({ name, atts: attsCell, out: [], at: i });
|
|
138
|
+
to = k + 1;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
if (typeof to === 'string')
|
|
142
|
+
return to;
|
|
143
|
+
i = to;
|
|
144
|
+
}
|
|
145
|
+
const open = stack.pop();
|
|
146
|
+
if (open !== undefined)
|
|
147
|
+
return `<${open.name}> on line ${lineAt(open.at)} is never closed`;
|
|
148
|
+
flush();
|
|
149
|
+
return top;
|
|
150
|
+
}
|
|
151
|
+
native('load-xml', '["Reads XML text into a block of nodes, each its name, content and attributes; a text is none, the text and none." source [string! binary!]]', (_t, f) => {
|
|
152
|
+
const x = f.args[0];
|
|
153
|
+
const src = x.t === K.Binary
|
|
154
|
+
? new TextDecoder().decode(new Uint8Array(x.s.data.slice(x.i)))
|
|
155
|
+
: fromCodePoints(x.s.data, x.i);
|
|
156
|
+
const r = parseXml(src);
|
|
157
|
+
return typeof r === 'string' ? fail('syntax', 'xml', r) : value(block(r));
|
|
158
|
+
});
|
|
159
|
+
const escapeText = (s) => s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
|
|
160
|
+
const escapeAttr = (s) => escapeText(s).replace(/"/g, '"');
|
|
161
|
+
/** A name as XML writes it; null when the value cannot be one. */
|
|
162
|
+
function nameText(c) {
|
|
163
|
+
if (c.t === K.Word || c.t === K.SetWord || c.t === K.LitWord)
|
|
164
|
+
return spell(c.sym);
|
|
165
|
+
if (c.t === K.String)
|
|
166
|
+
return fromCodePoints(c.s.data, c.i);
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
/** none as a value, or the word none as a block written in code has it. */
|
|
170
|
+
const isNone = (c) => c.t === K.None || (c.t === K.Word && spell(c.sym).toLowerCase() === 'none');
|
|
171
|
+
/** Attributes from a map, an object or a block of names and values; none values are left out. */
|
|
172
|
+
function attsText(c) {
|
|
173
|
+
const pairs = [];
|
|
174
|
+
if (isNone(c))
|
|
175
|
+
return '';
|
|
176
|
+
if (c.t === K.Map)
|
|
177
|
+
pairs.push(...c.s.data);
|
|
178
|
+
else if (c.t === K.Block)
|
|
179
|
+
pairs.push(...c.s.data.slice(c.i));
|
|
180
|
+
else if (c.t === K.Object)
|
|
181
|
+
c.ctx.keys.forEach((k, j) => pairs.push({ t: K.Word, sym: k, bind: null }, c.ctx.vals[j]));
|
|
182
|
+
else
|
|
183
|
+
return null;
|
|
184
|
+
let s = '';
|
|
185
|
+
for (let k = 0; k + 1 < pairs.length; k += 2) {
|
|
186
|
+
const name = nameText(pairs[k]);
|
|
187
|
+
if (name === null)
|
|
188
|
+
return null;
|
|
189
|
+
if (!isNone(pairs[k + 1]))
|
|
190
|
+
s += ` ${name}="${escapeAttr(fieldText(pairs[k + 1]))}"`;
|
|
191
|
+
}
|
|
192
|
+
return s;
|
|
193
|
+
}
|
|
194
|
+
/** The XML text of a block of nodes, or an error message when a node is not name, content and attributes. */
|
|
195
|
+
export function writeXml(nodes) {
|
|
196
|
+
let s = '';
|
|
197
|
+
// the nodes still to write, from where each block is, and the closing tags between them
|
|
198
|
+
const work = [{ nodes, at: 0 }];
|
|
199
|
+
while (work.length > 0) {
|
|
200
|
+
const w = work[work.length - 1];
|
|
201
|
+
if (typeof w === 'string' || w.at >= w.nodes.length) {
|
|
202
|
+
work.pop();
|
|
203
|
+
if (typeof w === 'string')
|
|
204
|
+
s += w;
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
if (w.at + 3 > w.nodes.length)
|
|
208
|
+
return { error: 'every node is three values: name, content and attributes' };
|
|
209
|
+
const [name, content, atts] = [w.nodes[w.at], w.nodes[w.at + 1], w.nodes[w.at + 2]];
|
|
210
|
+
w.at += 3;
|
|
211
|
+
if (isNone(name)) {
|
|
212
|
+
s += escapeText(fieldText(content));
|
|
213
|
+
continue;
|
|
214
|
+
}
|
|
215
|
+
const tag = nameText(name);
|
|
216
|
+
if (tag === null)
|
|
217
|
+
return { error: `a node's name is a word or a string, not ${fieldText(name)}` };
|
|
218
|
+
const a = attsText(atts);
|
|
219
|
+
if (a === null)
|
|
220
|
+
return { error: `the attributes of <${tag}> are a map, an object or a block` };
|
|
221
|
+
const inner = content.t === K.Block ? content.s.data.slice(content.i) : isNone(content) ? [] : null;
|
|
222
|
+
if (inner === null) {
|
|
223
|
+
// a value as content is its text
|
|
224
|
+
s += `<${tag}${a}>${escapeText(fieldText(content))}</${tag}>`;
|
|
225
|
+
}
|
|
226
|
+
else if (inner.length === 0) {
|
|
227
|
+
s += `<${tag}${a}/>`;
|
|
228
|
+
}
|
|
229
|
+
else {
|
|
230
|
+
s += `<${tag}${a}>`;
|
|
231
|
+
work.push(`</${tag}>`, { nodes: inner, at: 0 });
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
return { xml: s };
|
|
235
|
+
}
|
|
236
|
+
native('to-xml', '["Writes XML text from a block of nodes, each its name, content and attributes, as load-xml gives them." nodes [block!] /declaration "Start with the <?xml ...?> line a whole document needs"]', (_t, f) => {
|
|
237
|
+
const b = f.args[0];
|
|
238
|
+
const r = writeXml(b.s.data.slice(b.i));
|
|
239
|
+
if ('error' in r)
|
|
240
|
+
return fail('script', 'bad-xml', r.error);
|
|
241
|
+
return value(string((used(f.args[1]) ? '<?xml version="1.0" encoding="UTF-8"?>\n' : '') + r.xml));
|
|
242
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { type Kind } from './kinds.ts';
|
|
2
|
+
import { type Cell, type Series, type BlockCell, type StringCell } from './values.ts';
|
|
3
|
+
export type SeriesCell = BlockCell | StringCell;
|
|
4
|
+
/** Elements of a series: cells for blocks, code points for strings, bytes for binary. */
|
|
5
|
+
export type Item = Cell | number;
|
|
6
|
+
export declare function items(c: SeriesCell): Item[];
|
|
7
|
+
/** Elements from the cell's index to the tail. */
|
|
8
|
+
export declare function rest(c: SeriesCell): Item[];
|
|
9
|
+
export declare function lengthOf(c: SeriesCell): number;
|
|
10
|
+
/** Index clamped to the series bounds, as Rebol does when moving past an end. */
|
|
11
|
+
export declare function clampIndex(c: SeriesCell, i: number): number;
|
|
12
|
+
export declare function at(c: SeriesCell, i: number): SeriesCell;
|
|
13
|
+
/** Removes `del` elements at `at` and inserts `add` there, keeping block metadata aligned. */
|
|
14
|
+
export declare function splice(s: Series<Item>, at: number, del: number, add: readonly Item[], nl?: readonly boolean[]): void;
|
|
15
|
+
/** UTF-8 bytes of a JS string. */
|
|
16
|
+
export declare function utf8(text: string): number[];
|
|
17
|
+
/**
|
|
18
|
+
* The elements `value` contributes when inserted into a series of kind
|
|
19
|
+
* `target`. Blocks splice their contents unless `only`; strings take text.
|
|
20
|
+
*/
|
|
21
|
+
export declare function itemsFor(target: Kind, value: Cell, only: boolean): {
|
|
22
|
+
add: Item[];
|
|
23
|
+
nl?: boolean[];
|
|
24
|
+
};
|
|
25
|
+
/** Text a value contributes to a string: blocks join their elements' forms with no spaces. */
|
|
26
|
+
export declare function textOf(value: Cell): string;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
// Series editing helpers shared by the series natives.
|
|
2
|
+
//
|
|
3
|
+
// A series cell is (series, index). Block series also carry per-element
|
|
4
|
+
// newline markers and source positions; every edit keeps them aligned with
|
|
5
|
+
// `data` so mold and error positions stay right after changes.
|
|
6
|
+
import { K, isAnyBlock, isAnyString } from "./kinds.js";
|
|
7
|
+
import { codePoints, ownMeta } from "./values.js";
|
|
8
|
+
import { form } from "./mold.js";
|
|
9
|
+
export function items(c) {
|
|
10
|
+
return c.s.data;
|
|
11
|
+
}
|
|
12
|
+
/** Elements from the cell's index to the tail. */
|
|
13
|
+
export function rest(c) {
|
|
14
|
+
return c.s.data.slice(c.i);
|
|
15
|
+
}
|
|
16
|
+
export function lengthOf(c) {
|
|
17
|
+
return Math.max(0, c.s.data.length - c.i);
|
|
18
|
+
}
|
|
19
|
+
/** Index clamped to the series bounds, as Rebol does when moving past an end. */
|
|
20
|
+
export function clampIndex(c, i) {
|
|
21
|
+
return Math.max(0, Math.min(c.s.data.length, i));
|
|
22
|
+
}
|
|
23
|
+
export function at(c, i) {
|
|
24
|
+
return { ...c, i: clampIndex(c, i) };
|
|
25
|
+
}
|
|
26
|
+
/** Removes `del` elements at `at` and inserts `add` there, keeping block metadata aligned. */
|
|
27
|
+
export function splice(s, at, del, add, nl) {
|
|
28
|
+
ownMeta(s);
|
|
29
|
+
s.data.splice(at, del, ...add);
|
|
30
|
+
if (s.nl !== undefined)
|
|
31
|
+
s.nl.splice(at, del, ...add.map((_, k) => nl?.[k] ?? false));
|
|
32
|
+
if (s.pos !== undefined)
|
|
33
|
+
s.pos.splice(at, del, ...add.map(() => -1));
|
|
34
|
+
}
|
|
35
|
+
/** UTF-8 bytes of a JS string. */
|
|
36
|
+
export function utf8(text) {
|
|
37
|
+
return [...new TextEncoder().encode(text)];
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The elements `value` contributes when inserted into a series of kind
|
|
41
|
+
* `target`. Blocks splice their contents unless `only`; strings take text.
|
|
42
|
+
*/
|
|
43
|
+
export function itemsFor(target, value, only) {
|
|
44
|
+
if (isAnyBlock(target)) {
|
|
45
|
+
if (!only && value.t === K.Block) {
|
|
46
|
+
const b = value;
|
|
47
|
+
return { add: b.s.data.slice(b.i), nl: b.s.nl?.slice(b.i) };
|
|
48
|
+
}
|
|
49
|
+
return { add: [value] };
|
|
50
|
+
}
|
|
51
|
+
if (target === K.Binary) {
|
|
52
|
+
if (value.t === K.Binary)
|
|
53
|
+
return { add: value.s.data.slice(value.i) };
|
|
54
|
+
if (value.t === K.Integer)
|
|
55
|
+
return { add: [Number(value.v) & 255] };
|
|
56
|
+
if (value.t === K.Char)
|
|
57
|
+
return { add: utf8(String.fromCodePoint(value.v)) };
|
|
58
|
+
return { add: utf8(textOf(value)) };
|
|
59
|
+
}
|
|
60
|
+
if (value.t === K.Char)
|
|
61
|
+
return { add: [value.v] };
|
|
62
|
+
if (isAnyString(value.t) && (value.t !== K.Tag || target === K.Tag)) {
|
|
63
|
+
const s = value;
|
|
64
|
+
return { add: s.s.data.slice(s.i) };
|
|
65
|
+
}
|
|
66
|
+
return { add: codePoints(textOf(value)) };
|
|
67
|
+
}
|
|
68
|
+
/** Text a value contributes to a string: blocks join their elements' forms with no spaces. */
|
|
69
|
+
export function textOf(value) {
|
|
70
|
+
if (value.t === K.Block)
|
|
71
|
+
return value.s.data.slice(value.i).map(textOf).join('');
|
|
72
|
+
return form(value);
|
|
73
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type Sym = number;
|
|
2
|
+
export declare function intern(spelling: string): Sym;
|
|
3
|
+
/** The spelling a symbol was created with. */
|
|
4
|
+
export declare function spell(sym: Sym): string;
|
|
5
|
+
/** The canonical (lowercase) symbol for comparisons. */
|
|
6
|
+
export declare function canon(sym: Sym): Sym;
|
|
7
|
+
/** Case-insensitive symbol equality. */
|
|
8
|
+
export declare function sameSym(a: Sym, b: Sym): boolean;
|
|
9
|
+
export declare function symbolCount(): number;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// Symbol interning.
|
|
2
|
+
//
|
|
3
|
+
// Every distinct spelling gets its own symbol id, so words keep the case the
|
|
4
|
+
// user wrote ("Foo" molds back as "Foo"). Each symbol also has a canonical id
|
|
5
|
+
// (its lowercase spelling); words compare by canonical id, which makes word
|
|
6
|
+
// comparison case-insensitive as in Rebol.
|
|
7
|
+
const spellings = [];
|
|
8
|
+
const canons = [];
|
|
9
|
+
const ids = new Map();
|
|
10
|
+
export function intern(spelling) {
|
|
11
|
+
const found = ids.get(spelling);
|
|
12
|
+
if (found !== undefined)
|
|
13
|
+
return found;
|
|
14
|
+
const lower = spelling.toLowerCase();
|
|
15
|
+
const id = spellings.length;
|
|
16
|
+
spellings.push(spelling);
|
|
17
|
+
ids.set(spelling, id);
|
|
18
|
+
if (lower === spelling) {
|
|
19
|
+
canons.push(id);
|
|
20
|
+
}
|
|
21
|
+
else {
|
|
22
|
+
canons.push(-1); // placeholder: interning the canon may push more entries
|
|
23
|
+
canons[id] = intern(lower);
|
|
24
|
+
}
|
|
25
|
+
return id;
|
|
26
|
+
}
|
|
27
|
+
/** The spelling a symbol was created with. */
|
|
28
|
+
export function spell(sym) {
|
|
29
|
+
const s = spellings[sym];
|
|
30
|
+
if (s === undefined)
|
|
31
|
+
throw new RangeError(`unknown symbol ${sym}`);
|
|
32
|
+
return s;
|
|
33
|
+
}
|
|
34
|
+
/** The canonical (lowercase) symbol for comparisons. */
|
|
35
|
+
export function canon(sym) {
|
|
36
|
+
return canons[sym] ?? sym;
|
|
37
|
+
}
|
|
38
|
+
/** Case-insensitive symbol equality. */
|
|
39
|
+
export function sameSym(a, b) {
|
|
40
|
+
return a === b || canon(a) === canon(b);
|
|
41
|
+
}
|
|
42
|
+
export function symbolCount() {
|
|
43
|
+
return spellings.length;
|
|
44
|
+
}
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
import { K, type Kind } from './kinds.ts';
|
|
2
|
+
import { type Sym } from './symbols.ts';
|
|
3
|
+
import type { I64 } from './int64.ts';
|
|
4
|
+
import { type Dec } from './dec.ts';
|
|
5
|
+
/**
|
|
6
|
+
* Shared mutable storage behind series values. A cell points at a series plus
|
|
7
|
+
* an index, so `next block` is a new cell over the same series.
|
|
8
|
+
*
|
|
9
|
+
* Strings store Unicode code points (not UTF-16 units) so that indexes and
|
|
10
|
+
* lengths match Rebol and a future UTF-32 Zig implementation. Binaries and
|
|
11
|
+
* bitsets store bytes.
|
|
12
|
+
*/
|
|
13
|
+
export interface Series<T> {
|
|
14
|
+
data: T[];
|
|
15
|
+
/** Block series only: `nl[i]` is true when a newline precedes element i. */
|
|
16
|
+
nl?: boolean[];
|
|
17
|
+
/** Block series only: source position of element i, see `packPos`. */
|
|
18
|
+
pos?: number[];
|
|
19
|
+
/** Block series only: the file the block was loaded from, if any. */
|
|
20
|
+
file?: string;
|
|
21
|
+
/**
|
|
22
|
+
* Set when `nl` and `pos` are shared with the series this one was copied
|
|
23
|
+
* from (a function body copied for a call); `ownMeta` copies them before
|
|
24
|
+
* anything changes them.
|
|
25
|
+
*/
|
|
26
|
+
sharedMeta?: boolean;
|
|
27
|
+
}
|
|
28
|
+
/** Gives a series its own `nl` and `pos` before they are changed. Call it before every edit of them. */
|
|
29
|
+
export declare function ownMeta(s: Series<unknown>): void;
|
|
30
|
+
/** A binding context: objects, function frames and the user context. */
|
|
31
|
+
export interface Ctx {
|
|
32
|
+
keys: Sym[];
|
|
33
|
+
vals: Cell[];
|
|
34
|
+
parent: Ctx | null;
|
|
35
|
+
/**
|
|
36
|
+
* Lookup cache built lazily by `ctxFind`: canonical symbol to the first
|
|
37
|
+
* index with that key, covering `keys[0 .. idxN)`. Keys are only ever
|
|
38
|
+
* appended, so the cache is extended, never invalidated.
|
|
39
|
+
*/
|
|
40
|
+
idx?: Map<Sym, number>;
|
|
41
|
+
/** How many keys `idx` has seen. */
|
|
42
|
+
idxN?: number;
|
|
43
|
+
/** Set on object contexts: the word `self` bound here means the object. */
|
|
44
|
+
self?: boolean;
|
|
45
|
+
/**
|
|
46
|
+
* Set by a host that renders this context (a UI face): the first write
|
|
47
|
+
* through `ctxSet` pushes the context here and sets `dirty`, so the host
|
|
48
|
+
* repaints only what changed. The host clears `dirty` when it repaints.
|
|
49
|
+
*/
|
|
50
|
+
watch?: Ctx[];
|
|
51
|
+
dirty?: boolean;
|
|
52
|
+
}
|
|
53
|
+
export interface DateVal {
|
|
54
|
+
y: number;
|
|
55
|
+
m: number;
|
|
56
|
+
d: number;
|
|
57
|
+
/** Time of day in nanoseconds, or null for a date without time. */
|
|
58
|
+
time: number | null;
|
|
59
|
+
/** UTC offset in minutes, or null when no zone was given. */
|
|
60
|
+
zone: number | null;
|
|
61
|
+
}
|
|
62
|
+
export type StringKind = typeof K.String | typeof K.File | typeof K.Email | typeof K.Url | typeof K.Tag | typeof K.Binary;
|
|
63
|
+
export type WordKind = typeof K.Word | typeof K.SetWord | typeof K.GetWord | typeof K.LitWord | typeof K.Refinement | typeof K.Issue;
|
|
64
|
+
export type BlockKind = typeof K.Block | typeof K.Paren | typeof K.Path | typeof K.SetPath | typeof K.GetPath | typeof K.LitPath;
|
|
65
|
+
export type OpaqueKind = typeof K.Function | typeof K.Native | typeof K.Op | typeof K.Handle;
|
|
66
|
+
export interface UnsetCell {
|
|
67
|
+
readonly t: typeof K.Unset;
|
|
68
|
+
}
|
|
69
|
+
export interface NoneCell {
|
|
70
|
+
readonly t: typeof K.None;
|
|
71
|
+
}
|
|
72
|
+
export interface LogicCell {
|
|
73
|
+
readonly t: typeof K.Logic;
|
|
74
|
+
readonly v: boolean;
|
|
75
|
+
}
|
|
76
|
+
export interface IntegerCell {
|
|
77
|
+
readonly t: typeof K.Integer;
|
|
78
|
+
readonly v: I64;
|
|
79
|
+
}
|
|
80
|
+
export interface DecimalCell {
|
|
81
|
+
readonly t: typeof K.Decimal;
|
|
82
|
+
readonly v: number;
|
|
83
|
+
}
|
|
84
|
+
/** `v` is the fraction: 15% is stored as 0.15. */
|
|
85
|
+
export interface PercentCell {
|
|
86
|
+
readonly t: typeof K.Percent;
|
|
87
|
+
readonly v: number;
|
|
88
|
+
}
|
|
89
|
+
export interface MoneyCell {
|
|
90
|
+
readonly t: typeof K.Money;
|
|
91
|
+
readonly v: Dec;
|
|
92
|
+
readonly cur: string;
|
|
93
|
+
}
|
|
94
|
+
export interface CharCell {
|
|
95
|
+
readonly t: typeof K.Char;
|
|
96
|
+
readonly v: number;
|
|
97
|
+
}
|
|
98
|
+
export interface PairCell {
|
|
99
|
+
readonly t: typeof K.Pair;
|
|
100
|
+
readonly x: number;
|
|
101
|
+
readonly y: number;
|
|
102
|
+
}
|
|
103
|
+
export interface TupleCell {
|
|
104
|
+
readonly t: typeof K.Tuple;
|
|
105
|
+
readonly v: readonly number[];
|
|
106
|
+
}
|
|
107
|
+
export interface DateCell {
|
|
108
|
+
readonly t: typeof K.Date;
|
|
109
|
+
readonly v: Readonly<DateVal>;
|
|
110
|
+
}
|
|
111
|
+
/** Nanoseconds; may be negative or exceed 24 hours. */
|
|
112
|
+
export interface TimeCell {
|
|
113
|
+
readonly t: typeof K.Time;
|
|
114
|
+
readonly v: number;
|
|
115
|
+
}
|
|
116
|
+
export interface StringCell {
|
|
117
|
+
readonly t: StringKind;
|
|
118
|
+
readonly s: Series<number>;
|
|
119
|
+
readonly i: number;
|
|
120
|
+
}
|
|
121
|
+
export interface BitsetCell {
|
|
122
|
+
readonly t: typeof K.Bitset;
|
|
123
|
+
readonly s: Series<number>;
|
|
124
|
+
readonly neg: boolean;
|
|
125
|
+
}
|
|
126
|
+
export interface WordCell {
|
|
127
|
+
readonly t: WordKind;
|
|
128
|
+
readonly sym: Sym;
|
|
129
|
+
readonly bind: Ctx | null;
|
|
130
|
+
/**
|
|
131
|
+
* Where the word's key is in `bind`, when known at bind time. Keys are only
|
|
132
|
+
* ever appended, so the index never goes stale: a lookup is one array read.
|
|
133
|
+
*/
|
|
134
|
+
readonly idx?: number;
|
|
135
|
+
}
|
|
136
|
+
export interface BlockCell {
|
|
137
|
+
readonly t: BlockKind;
|
|
138
|
+
readonly s: Series<Cell>;
|
|
139
|
+
readonly i: number;
|
|
140
|
+
}
|
|
141
|
+
/** Keys and values alternate in `s`. Hashing arrives with the evaluator. */
|
|
142
|
+
export interface MapCell {
|
|
143
|
+
readonly t: typeof K.Map;
|
|
144
|
+
readonly s: Series<Cell>;
|
|
145
|
+
}
|
|
146
|
+
export interface ObjectCell {
|
|
147
|
+
readonly t: typeof K.Object | typeof K.Error;
|
|
148
|
+
readonly ctx: Ctx;
|
|
149
|
+
}
|
|
150
|
+
export interface DatatypeCell {
|
|
151
|
+
readonly t: typeof K.Datatype;
|
|
152
|
+
readonly k: Kind;
|
|
153
|
+
}
|
|
154
|
+
/** Bit `k` is set when kind `k` belongs to the typeset. */
|
|
155
|
+
export interface TypesetCell {
|
|
156
|
+
readonly t: typeof K.Typeset;
|
|
157
|
+
readonly bits: bigint;
|
|
158
|
+
}
|
|
159
|
+
export interface OpaqueCell {
|
|
160
|
+
readonly t: OpaqueKind;
|
|
161
|
+
readonly p: unknown;
|
|
162
|
+
}
|
|
163
|
+
export type Cell = UnsetCell | NoneCell | LogicCell | IntegerCell | DecimalCell | PercentCell | MoneyCell | CharCell | PairCell | TupleCell | DateCell | TimeCell | StringCell | BitsetCell | WordCell | BlockCell | MapCell | ObjectCell | DatatypeCell | TypesetCell | OpaqueCell;
|
|
164
|
+
export declare const UNSET: UnsetCell;
|
|
165
|
+
export declare const NONE: NoneCell;
|
|
166
|
+
export declare const TRUE: LogicCell;
|
|
167
|
+
export declare const FALSE: LogicCell;
|
|
168
|
+
export declare const logic: (v: boolean) => LogicCell;
|
|
169
|
+
export declare const integer: (v: I64) => IntegerCell;
|
|
170
|
+
export declare const decimal: (v: number) => DecimalCell;
|
|
171
|
+
export declare const percent: (v: number) => PercentCell;
|
|
172
|
+
export declare const money: (v: Dec, cur?: string) => MoneyCell;
|
|
173
|
+
export declare const char: (v: number) => CharCell;
|
|
174
|
+
export declare const pair: (x: number, y: number) => PairCell;
|
|
175
|
+
export declare const tuple: (v: readonly number[]) => TupleCell;
|
|
176
|
+
export declare const date: (v: DateVal) => DateCell;
|
|
177
|
+
export declare const time: (ns: number) => TimeCell;
|
|
178
|
+
export declare const datatype: (k: Kind) => DatatypeCell;
|
|
179
|
+
export declare const typeset: (bits: bigint) => TypesetCell;
|
|
180
|
+
/** Converts a JS string to an array of code points. */
|
|
181
|
+
export declare function codePoints(s: string): number[];
|
|
182
|
+
export declare function series<T>(data: T[]): Series<T>;
|
|
183
|
+
export declare function stringOf(t: StringKind, text: string): StringCell;
|
|
184
|
+
export declare const string: (text: string) => StringCell;
|
|
185
|
+
export declare function binary(bytes: number[]): StringCell;
|
|
186
|
+
export declare function bitset(bytes: number[], neg?: boolean): BitsetCell;
|
|
187
|
+
export declare function wordOf(t: WordKind, name: string | Sym, bind?: Ctx | null): WordCell;
|
|
188
|
+
export declare const word: (name: string) => WordCell;
|
|
189
|
+
export declare function blockOf(t: BlockKind, cells: Cell[], i?: number): BlockCell;
|
|
190
|
+
export declare const block: (cells: Cell[]) => BlockCell;
|
|
191
|
+
export declare function map(pairs: Cell[]): MapCell;
|
|
192
|
+
export declare function object(ctx: Ctx): ObjectCell;
|
|
193
|
+
export declare function context(keys?: Sym[], vals?: Cell[], parent?: Ctx | null): Ctx;
|
|
194
|
+
/** The JS string for a string-like cell, from its index to the tail. */
|
|
195
|
+
export declare function text(c: StringCell): string;
|
|
196
|
+
/** Builds a JS string from code points, in chunks to stay off the JS stack limit. */
|
|
197
|
+
export declare function fromCodePoints(cps: readonly number[], start?: number, end?: number): string;
|
|
198
|
+
/** Packs a source position into one number (line < 2^32, column < 2^20). */
|
|
199
|
+
export declare function packPos(line: number, col: number): number;
|
|
200
|
+
export declare function unpackPos(p: number): {
|
|
201
|
+
line: number;
|
|
202
|
+
col: number;
|
|
203
|
+
};
|
|
204
|
+
/** True if two cells are the very same value: the same series at the same position, the same context. */
|
|
205
|
+
export declare function sameValue(a: Cell, b: Cell): boolean;
|
|
206
|
+
export declare function strictEqual(a: Cell, b: Cell): boolean;
|