@mark1russell7/vex 0.9.0 → 0.10.0
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 +36 -1
- package/build/index.d.ts +155 -28
- package/build/index.d.ts.map +1 -1
- package/build/index.js +2124 -227
- package/build/index.js.map +1 -1
- package/package.json +5 -1
- package/src/builder.ts +54 -13
- package/src/codec.ts +104 -0
- package/src/deps.ts +174 -60
- package/src/document.ts +301 -0
- package/src/errors.ts +7 -3
- package/src/evaluate.ts +82 -28
- package/src/index.ts +5 -1
- package/src/json.ts +11 -96
- package/src/kernel.ts +897 -0
- package/src/list.ts +6 -3
- package/src/plan.ts +88 -0
- package/src/same.ts +31 -0
- package/src/sheet.ts +2 -1
- package/src/write.ts +257 -0
package/src/json.ts
CHANGED
|
@@ -1,103 +1,10 @@
|
|
|
1
|
+
import { decodeValue, encodeValue, isRecord } from "./codec.ts";
|
|
2
|
+
import { fromDocument, isDocument } from "./document.ts";
|
|
1
3
|
import type { AnyDomain } from "./domain.ts";
|
|
2
4
|
import { isStackOverflow, vexError } from "./errors.ts";
|
|
3
5
|
import { isExpr, type Expr } from "./ir.ts";
|
|
4
6
|
import { fail, ok, type Result } from "./result.ts";
|
|
5
7
|
|
|
6
|
-
/**
|
|
7
|
-
* The JSON form of the values in a literal. JSON data stays as it is, with three exceptions. Each exception is a record
|
|
8
|
-
* with the key `$vex`:
|
|
9
|
-
*
|
|
10
|
-
* - `{ "$vex": "domain", "domain": name, "value": v }`: a value of a domain, with the `encode` of the domain.
|
|
11
|
-
* - `{ "$vex": "number", "value": "-0" }`: a number that JSON has no text for: `-0`, `NaN`, `Infinity` or `-Infinity`.
|
|
12
|
-
* - `{ "$vex": "escape", "value": record }`: a record of the data that has the key `$vex` itself (IR.JSON.ESCAPE).
|
|
13
|
-
*
|
|
14
|
-
* A record with the key `$vex` and another form stays data, so a file from an older version reads as before.
|
|
15
|
-
*
|
|
16
|
-
* The functions walk the tree of the expression, so only the value of a literal gets this form. A field of a literal
|
|
17
|
-
* that looks like an expression stays data. The data of an extension is plain JSON, as before.
|
|
18
|
-
*/
|
|
19
|
-
type Encoded =
|
|
20
|
-
| { readonly $vex: "domain"; readonly domain: string; readonly value: unknown }
|
|
21
|
-
| { readonly $vex: "number"; readonly value: "-0" | "NaN" | "Infinity" | "-Infinity" }
|
|
22
|
-
| { readonly $vex: "escape"; readonly value: Readonly<Record<string, unknown>> };
|
|
23
|
-
|
|
24
|
-
const isRecord = (u: unknown): u is Readonly<Record<string, unknown>> => typeof u === "object" && u !== null && !Array.isArray(u);
|
|
25
|
-
|
|
26
|
-
const isPlainRecord = (u: unknown): u is Readonly<Record<string, unknown>> => {
|
|
27
|
-
if (!isRecord(u)) return false;
|
|
28
|
-
const proto: unknown = Object.getPrototypeOf(u);
|
|
29
|
-
return proto === Object.prototype || proto === null;
|
|
30
|
-
};
|
|
31
|
-
|
|
32
|
-
/** This function gives the JSON form of a value, or the reason why the value has no JSON form. */
|
|
33
|
-
function encodeValue(u: unknown, domains: readonly AnyDomain[]): Result<unknown> {
|
|
34
|
-
if (u === null || typeof u === "string" || typeof u === "boolean") return ok(u);
|
|
35
|
-
if (typeof u === "number") {
|
|
36
|
-
if (Object.is(u, -0)) return ok({ $vex: "number", value: "-0" } satisfies Encoded);
|
|
37
|
-
// Stryker disable next-line EqualityOperator: here u is an infinity, so u > 0 and u >= 0 are the same test
|
|
38
|
-
return Number.isFinite(u) ? ok(u) : ok({ $vex: "number", value: Number.isNaN(u) ? "NaN" : u > 0 ? "Infinity" : "-Infinity" } satisfies Encoded);
|
|
39
|
-
}
|
|
40
|
-
if (Array.isArray(u)) {
|
|
41
|
-
const out: unknown[] = [];
|
|
42
|
-
for (const x of u) {
|
|
43
|
-
const r = encodeValue(x, domains);
|
|
44
|
-
if (!r.ok) return r;
|
|
45
|
-
out.push(r.value);
|
|
46
|
-
}
|
|
47
|
-
return ok(out);
|
|
48
|
-
}
|
|
49
|
-
if (isPlainRecord(u)) {
|
|
50
|
-
const out: Record<string, unknown> = {};
|
|
51
|
-
for (const [k, x] of Object.entries(u)) {
|
|
52
|
-
const r = encodeValue(x, domains);
|
|
53
|
-
if (!r.ok) return r;
|
|
54
|
-
out[k] = r.value;
|
|
55
|
-
}
|
|
56
|
-
return ok(Object.hasOwn(u, "$vex") ? ({ $vex: "escape", value: out } satisfies Encoded) : out);
|
|
57
|
-
}
|
|
58
|
-
// A plain array or record stays data. Another object is a value of a domain, or it has no JSON form.
|
|
59
|
-
const d = domains.find((dom) => dom.encode !== undefined && dom.is(u));
|
|
60
|
-
if (d?.encode !== undefined) return ok({ $vex: "domain", domain: d.name, value: d.encode(u) } satisfies Encoded);
|
|
61
|
-
return fail(vexError("bad-expression", "a literal value is not JSON data and no domain encodes it"));
|
|
62
|
-
}
|
|
63
|
-
|
|
64
|
-
const NUMBERS: Readonly<Record<string, number>> = { "-0": -0, NaN: Number.NaN, Infinity: Number.POSITIVE_INFINITY, "-Infinity": Number.NEGATIVE_INFINITY };
|
|
65
|
-
|
|
66
|
-
/** This function reads a value from its JSON form. */
|
|
67
|
-
function decodeValue(u: unknown, domains: readonly AnyDomain[]): Result<unknown> {
|
|
68
|
-
if (Array.isArray(u)) {
|
|
69
|
-
const out: unknown[] = [];
|
|
70
|
-
for (const x of u) {
|
|
71
|
-
const r = decodeValue(x, domains);
|
|
72
|
-
if (!r.ok) return r;
|
|
73
|
-
out.push(r.value);
|
|
74
|
-
}
|
|
75
|
-
return ok(out);
|
|
76
|
-
}
|
|
77
|
-
if (!isRecord(u)) return ok(u);
|
|
78
|
-
const fields = (record: Readonly<Record<string, unknown>>): Result<unknown> => {
|
|
79
|
-
const out: Record<string, unknown> = {};
|
|
80
|
-
for (const [k, x] of Object.entries(record)) {
|
|
81
|
-
const r = decodeValue(x, domains);
|
|
82
|
-
if (!r.ok) return r;
|
|
83
|
-
out[k] = r.value;
|
|
84
|
-
}
|
|
85
|
-
return ok(out);
|
|
86
|
-
};
|
|
87
|
-
// A record without the key $vex matches none of the forms below, so it stays data too.
|
|
88
|
-
const kind = u["$vex"];
|
|
89
|
-
if (kind === "escape" && isRecord(u["value"])) return fields(u["value"]);
|
|
90
|
-
if (kind === "number" && typeof u["value"] === "string" && Object.hasOwn(NUMBERS, u["value"])) return ok(NUMBERS[u["value"]]);
|
|
91
|
-
if (kind === "domain" && typeof u["domain"] === "string") {
|
|
92
|
-
const name = u["domain"];
|
|
93
|
-
const d = domains.find((dom) => dom.name === name);
|
|
94
|
-
if (d?.decode === undefined) return fail(vexError("bad-expression", `no domain "${name}" decodes a literal`));
|
|
95
|
-
return ok(d.decode(u["value"]));
|
|
96
|
-
}
|
|
97
|
-
// A form that this version does not know stays data, so a file from an older version reads as before.
|
|
98
|
-
return fields(u);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
8
|
/** This function applies `f` to each literal value of a JSON tree of an expression. */
|
|
102
9
|
function mapValues(u: unknown, f: (value: unknown) => Result<unknown>): Result<unknown> {
|
|
103
10
|
if (!isRecord(u)) return ok(u);
|
|
@@ -218,7 +125,10 @@ export function serialize(e: Expr, domains: readonly AnyDomain[] = []): Result<s
|
|
|
218
125
|
}
|
|
219
126
|
}
|
|
220
127
|
|
|
221
|
-
/**
|
|
128
|
+
/**
|
|
129
|
+
* This function reads an expression from JSON text. It checks the whole tree. The text can also be a document: then
|
|
130
|
+
* `fromDocument` reads it (DOC.ENTRY).
|
|
131
|
+
*/
|
|
222
132
|
// Stryker disable next-line ArrayDeclaration: a list with a value that is not a domain decodes no value
|
|
223
133
|
export function parse(text: string, domains: readonly AnyDomain[] = []): Result<Expr> {
|
|
224
134
|
let raw: unknown;
|
|
@@ -227,6 +137,11 @@ export function parse(text: string, domains: readonly AnyDomain[] = []): Result<
|
|
|
227
137
|
} catch (thrown) {
|
|
228
138
|
return fail(vexError("bad-expression", "the text is not JSON", { thrown }));
|
|
229
139
|
}
|
|
140
|
+
if (isDocument(raw)) {
|
|
141
|
+
const read = fromDocument(raw, domains);
|
|
142
|
+
// The JSON form needs whole numbers in the moves and the windows, as isExpr does.
|
|
143
|
+
return !read.ok || isExpr(read.value) ? read : fail(vexError("bad-expression", "the JSON is not a valid Vex expression"));
|
|
144
|
+
}
|
|
230
145
|
try {
|
|
231
146
|
const tree = mapValues(raw, (v) => decodeValue(v, domains));
|
|
232
147
|
if (!tree.ok) return fail(tree.error);
|