@mark1russell7/vex 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/build/index.d.ts +884 -0
- package/build/index.d.ts.map +1 -0
- package/build/index.js +1839 -0
- package/build/index.js.map +1 -0
- package/package.json +44 -3
- package/src/builder.ts +649 -0
- package/src/deps.ts +33 -0
- package/src/domain.ts +135 -0
- package/src/errors.ts +102 -0
- package/src/evaluate.ts +628 -0
- package/src/index.ts +16 -0
- package/src/ir.ts +212 -0
- package/src/json.ts +76 -0
- package/src/list.ts +35 -0
- package/src/result.ts +44 -0
- package/src/sheet.ts +141 -0
- package/src/space.ts +322 -0
- package/src/trace.ts +37 -0
- package/src/traversal.ts +126 -0
- package/README.md +0 -3
package/build/index.d.ts
ADDED
|
@@ -0,0 +1,884 @@
|
|
|
1
|
+
//#region ../../node_modules/.pnpm/@mark1russell7+optional@htt_51d63135d3a4bb347c942166e3e4ba82/node_modules/@mark1russell7/optional/dist/index.d.ts
|
|
2
|
+
/**
|
|
3
|
+
* The Optional of the family: a value that is present (`Some`) or absent (`None`). The packages render and Vex use
|
|
4
|
+
* this form. Graph uses a sentinel form, and `fromSentinel` and `toSentinel` convert between the two forms.
|
|
5
|
+
*/
|
|
6
|
+
/** The absence of a value. */
|
|
7
|
+
type None = {
|
|
8
|
+
readonly tag: "none";
|
|
9
|
+
};
|
|
10
|
+
/** A present value. */
|
|
11
|
+
type Some<T> = {
|
|
12
|
+
readonly tag: "some";
|
|
13
|
+
readonly value: T;
|
|
14
|
+
};
|
|
15
|
+
/** A value that is present (`Some`) or absent (`None`). It replaces `null`, `undefined` and exceptions. */
|
|
16
|
+
type Optional$1<T> = None | Some<T>;
|
|
17
|
+
//#endregion
|
|
18
|
+
//#region src/errors.d.ts
|
|
19
|
+
/**
|
|
20
|
+
* The error codes of Vex. The names come from the spreadsheet error values, because a Vex program is a
|
|
21
|
+
* spreadsheet formula over records.
|
|
22
|
+
*/
|
|
23
|
+
export type ErrorCode = "#REF!" | "#N/A" | "#VALUE!" | "#NAME?" | "#NUM!" | "#CALC!" | "#ARGS" | "#CYCLE!";
|
|
24
|
+
/** The cause of an error, in more detail than the code. */
|
|
25
|
+
export type ErrorKind = "unknown-key" | "not-a-pair" | "out-of-bounds" | "no-offset" | "no-grid" | "no-tree" | "missing-field" | "empty" | "not-instance" | "kind-mismatch" | "unknown-op" | "unbound" | "not-finite" | "invalid-value" | "threw" | "undefined-result" | "args" | "cycle" | "bad-expression";
|
|
26
|
+
/** A Vex error. It is a value: a program can catch it with `ifError`. */
|
|
27
|
+
export interface VexError {
|
|
28
|
+
/** The spreadsheet-style code, for example `#REF!`. */
|
|
29
|
+
readonly code: ErrorCode;
|
|
30
|
+
/** The cause in more detail. */
|
|
31
|
+
readonly kind: ErrorKind;
|
|
32
|
+
/** A sentence that tells what went wrong. */
|
|
33
|
+
readonly message: string;
|
|
34
|
+
/** The position of the failed node in the expression tree. The root is `[]`. */
|
|
35
|
+
readonly path: readonly number[];
|
|
36
|
+
/** The key where the evaluation started. */
|
|
37
|
+
readonly origin?: string;
|
|
38
|
+
/** The key where relative references read when the error occurred. */
|
|
39
|
+
readonly focus?: string;
|
|
40
|
+
/** The op name, for errors of an op. */
|
|
41
|
+
readonly op?: string;
|
|
42
|
+
/** The errors of the arguments, for the code `#ARGS`. */
|
|
43
|
+
readonly causes?: readonly VexError[];
|
|
44
|
+
/** The value that an op threw, for the kind `threw`. */
|
|
45
|
+
readonly thrown?: unknown;
|
|
46
|
+
}
|
|
47
|
+
/** The details that a caller can give to `vexError`. */
|
|
48
|
+
export interface ErrorDetails {
|
|
49
|
+
readonly path?: readonly number[];
|
|
50
|
+
readonly origin?: string;
|
|
51
|
+
readonly focus?: string;
|
|
52
|
+
readonly op?: string;
|
|
53
|
+
readonly causes?: readonly VexError[];
|
|
54
|
+
readonly thrown?: unknown;
|
|
55
|
+
}
|
|
56
|
+
/** This function gives the code of an error kind. */
|
|
57
|
+
export declare const codeOf: (kind: ErrorKind) => ErrorCode;
|
|
58
|
+
/** This function makes a `VexError`. */
|
|
59
|
+
export declare function vexError(kind: ErrorKind, message: string, details?: ErrorDetails): VexError;
|
|
60
|
+
/** This function gives a short text for an error, for example `#REF! unknown-key: there is no key "Z"`. */
|
|
61
|
+
export declare const formatError: (e: VexError) => string;
|
|
62
|
+
//#endregion
|
|
63
|
+
//#region src/result.d.ts
|
|
64
|
+
/**
|
|
65
|
+
* A value that is there, or not there. This is the public result of a Vex program. It is the Optional of the
|
|
66
|
+
* family (`@mark1russell7/optional`), so render and Vex give and take the same values. The import is a type import
|
|
67
|
+
* only: the build copies the type into the declarations, so the package has no runtime dependency.
|
|
68
|
+
*/
|
|
69
|
+
export type Optional<T> = Optional$1<T>;
|
|
70
|
+
/** A value, or the error that tells why there is no value. The interpreter uses this type. */
|
|
71
|
+
export type Result<T> = {
|
|
72
|
+
readonly ok: true;
|
|
73
|
+
readonly value: T;
|
|
74
|
+
} | {
|
|
75
|
+
readonly ok: false;
|
|
76
|
+
readonly error: VexError;
|
|
77
|
+
};
|
|
78
|
+
/** This function makes an `Optional` that holds `value`. */
|
|
79
|
+
export declare const some: <T>(value: T) => Optional<T>;
|
|
80
|
+
/** This function gives the shared empty `Optional`. */
|
|
81
|
+
export declare const none: <T = never>() => Optional<T>;
|
|
82
|
+
/** This function makes a successful `Result`. */
|
|
83
|
+
export declare const ok: <T>(value: T) => Result<T>;
|
|
84
|
+
/** This function makes a failed `Result`. */
|
|
85
|
+
export declare const fail: <T = never>(error: VexError) => Result<T>;
|
|
86
|
+
/** This function tells if an `Optional` holds a value. */
|
|
87
|
+
export declare const isSome: <T>(o: Optional<T>) => o is {
|
|
88
|
+
readonly tag: "some";
|
|
89
|
+
readonly value: T;
|
|
90
|
+
};
|
|
91
|
+
/** This function tells if an `Optional` is empty. */
|
|
92
|
+
export declare const isNone: <T>(o: Optional<T>) => o is {
|
|
93
|
+
readonly tag: "none";
|
|
94
|
+
};
|
|
95
|
+
/** This function changes a `Result` into an `Optional`. The error is lost. */
|
|
96
|
+
export declare const toOptional: <T>(r: Result<T>) => Optional<T>;
|
|
97
|
+
/** This function gives the value of an `Optional`, or `fallback` if it is empty. */
|
|
98
|
+
export declare const getOr: <T, U>(o: Optional<T>, fallback: U) => T | U;
|
|
99
|
+
/** This function applies `f` to the value of a `Result`. An error stays the same. */
|
|
100
|
+
export declare const mapResult: <T, U>(r: Result<T>, f: (value: T) => U) => Result<U>;
|
|
101
|
+
/** This function applies `f` to the value of a `Result`, and `f` gives a `Result`. An error stays the same. */
|
|
102
|
+
export declare const chainResult: <T, U>(r: Result<T>, f: (value: T) => Result<U>) => Result<U>;
|
|
103
|
+
//#endregion
|
|
104
|
+
//#region src/ir.d.ts
|
|
105
|
+
/**
|
|
106
|
+
* The expression IR of Vex. It is plain JSON data. Its first three kinds (`lit`, `ref`, `app`) have the same
|
|
107
|
+
* shape as the expressions of `render`.
|
|
108
|
+
*/
|
|
109
|
+
/** One move of an address. The moves of an address apply in order, from the focus of the evaluation. */
|
|
110
|
+
export type Move = {
|
|
111
|
+
readonly t: "key";
|
|
112
|
+
readonly key: string;
|
|
113
|
+
} | {
|
|
114
|
+
readonly t: "index";
|
|
115
|
+
readonly i: number;
|
|
116
|
+
} | {
|
|
117
|
+
readonly t: "other";
|
|
118
|
+
} | {
|
|
119
|
+
readonly t: "offset";
|
|
120
|
+
readonly d: readonly number[];
|
|
121
|
+
} | {
|
|
122
|
+
readonly t: "origin";
|
|
123
|
+
} | {
|
|
124
|
+
readonly t: "parent";
|
|
125
|
+
};
|
|
126
|
+
/** An address: a list of moves. The empty list is the focus. */
|
|
127
|
+
export type Addr = readonly Move[];
|
|
128
|
+
/** An axis: the relation between a position and the targets that a body reads. */
|
|
129
|
+
export type Axis = {
|
|
130
|
+
readonly t: "all";
|
|
131
|
+
} | {
|
|
132
|
+
readonly t: "others";
|
|
133
|
+
} | {
|
|
134
|
+
readonly t: "other";
|
|
135
|
+
} | {
|
|
136
|
+
readonly t: "neighbors";
|
|
137
|
+
readonly n: 4 | 8;
|
|
138
|
+
} | {
|
|
139
|
+
readonly t: "children";
|
|
140
|
+
} | {
|
|
141
|
+
readonly t: "ancestors";
|
|
142
|
+
} | {
|
|
143
|
+
readonly t: "descendants";
|
|
144
|
+
} | {
|
|
145
|
+
readonly t: "siblings";
|
|
146
|
+
} | {
|
|
147
|
+
readonly t: "where";
|
|
148
|
+
readonly axis: Axis;
|
|
149
|
+
readonly test: Expr;
|
|
150
|
+
};
|
|
151
|
+
/** A Vex expression. */
|
|
152
|
+
export type Expr = {
|
|
153
|
+
readonly tag: "lit";
|
|
154
|
+
readonly value: unknown;
|
|
155
|
+
} | {
|
|
156
|
+
readonly tag: "ref";
|
|
157
|
+
readonly path: readonly string[];
|
|
158
|
+
readonly at?: Addr;
|
|
159
|
+
} | {
|
|
160
|
+
readonly tag: "app";
|
|
161
|
+
readonly op: string;
|
|
162
|
+
readonly args: readonly Expr[];
|
|
163
|
+
} | {
|
|
164
|
+
readonly tag: "let";
|
|
165
|
+
readonly bind: Readonly<Record<string, Expr>>;
|
|
166
|
+
readonly body: Expr;
|
|
167
|
+
} | {
|
|
168
|
+
readonly tag: "var";
|
|
169
|
+
readonly name: string;
|
|
170
|
+
} | {
|
|
171
|
+
readonly tag: "rec";
|
|
172
|
+
readonly fields: Readonly<Record<string, Expr>>;
|
|
173
|
+
} | {
|
|
174
|
+
readonly tag: "each";
|
|
175
|
+
readonly axis: Axis;
|
|
176
|
+
readonly body: Expr;
|
|
177
|
+
} | {
|
|
178
|
+
readonly tag: "ext";
|
|
179
|
+
readonly kind: string;
|
|
180
|
+
readonly data: unknown;
|
|
181
|
+
};
|
|
182
|
+
/** The tag of an expression kind. */
|
|
183
|
+
export type ExprTag = Expr["tag"];
|
|
184
|
+
/** An expression of one kind. */
|
|
185
|
+
export type ExprOf<T extends ExprTag> = Extract<Expr, {
|
|
186
|
+
readonly tag: T;
|
|
187
|
+
}>;
|
|
188
|
+
/** This function makes a literal. */
|
|
189
|
+
export declare const lit: (value: unknown) => ExprOf<"lit">;
|
|
190
|
+
/** This function splits a dotted path, for example `"pos.x"`, into its segments. */
|
|
191
|
+
export declare const toPath: (path: string | readonly string[]) => readonly string[];
|
|
192
|
+
/** This function makes a reference to a field path. With `at`, the reference reads at that address. */
|
|
193
|
+
export declare function ref(path: string | readonly string[], at?: Addr): ExprOf<"ref">;
|
|
194
|
+
/** This function makes the application of an op to arguments. For a method op, the first argument is the receiver. */
|
|
195
|
+
export declare const app: (op: string, ...args: readonly Expr[]) => ExprOf<"app">;
|
|
196
|
+
/** This function binds names to expressions for the body. */
|
|
197
|
+
export declare const let_: (bind: Readonly<Record<string, Expr>>, body: Expr) => ExprOf<"let">;
|
|
198
|
+
/** This function makes a reference to a bound name. */
|
|
199
|
+
export declare const v: (name: string) => ExprOf<"var">;
|
|
200
|
+
/** This function makes a record from field expressions. */
|
|
201
|
+
export declare const rec: (fields: Readonly<Record<string, Expr>>) => ExprOf<"rec">;
|
|
202
|
+
/** This function evaluates `body` once for each target of `axis`. The result is a list. */
|
|
203
|
+
export declare const each: (axis: Axis, body: Expr) => ExprOf<"each">;
|
|
204
|
+
/** This function makes an expression of an extension kind. */
|
|
205
|
+
export declare const ext: (kind: string, data: unknown) => ExprOf<"ext">;
|
|
206
|
+
/** The move to an absolute key. */
|
|
207
|
+
export declare const key: (k: string) => Move;
|
|
208
|
+
/** The move to a position in the key order. */
|
|
209
|
+
export declare const index: (i: number) => Move;
|
|
210
|
+
/** The move to the other key of a space with two keys. */
|
|
211
|
+
export declare const other: Move;
|
|
212
|
+
/** The relative move in an array (one number) or a grid (two numbers). */
|
|
213
|
+
export declare const offset: (...d: readonly number[]) => Move;
|
|
214
|
+
/** The move back to the origin of the evaluation. */
|
|
215
|
+
export declare const origin: Move;
|
|
216
|
+
/** The move to the parent of the focus in a tree space. */
|
|
217
|
+
export declare const parent: Move;
|
|
218
|
+
/** The axes. */
|
|
219
|
+
export declare const axes: {
|
|
220
|
+
/** Each key of the space. */
|
|
221
|
+
readonly all: Axis;
|
|
222
|
+
/** Each key except the focus. */
|
|
223
|
+
readonly others: Axis;
|
|
224
|
+
/** The other key of a pair. */
|
|
225
|
+
readonly other: Axis;
|
|
226
|
+
/** The neighbors of the focus in a grid. */
|
|
227
|
+
readonly neighbors: (n?: 4 | 8) => Axis;
|
|
228
|
+
/** The children of the focus in a tree, in key order. */
|
|
229
|
+
readonly children: Axis;
|
|
230
|
+
/** The ancestors of the focus in a tree: the parent first, the root last. */
|
|
231
|
+
readonly ancestors: Axis;
|
|
232
|
+
/** The descendants of the focus in a tree, depth first, each parent before its children. */
|
|
233
|
+
readonly descendants: Axis;
|
|
234
|
+
/** The other children of the parent of the focus. For a root, the other roots. */
|
|
235
|
+
readonly siblings: Axis;
|
|
236
|
+
/** The targets of `axis` where `test` gives `true`. */
|
|
237
|
+
readonly where: (axis: Axis, test: Expr) => Axis;
|
|
238
|
+
};
|
|
239
|
+
/** This function tells if a value is a valid move. */
|
|
240
|
+
export declare function isMove(u: unknown): u is Move;
|
|
241
|
+
/** This function tells if a value is a valid axis. */
|
|
242
|
+
export declare function isAxis(u: unknown): u is Axis;
|
|
243
|
+
/** This function tells if a value is a valid expression. It checks the whole tree. */
|
|
244
|
+
export declare function isExpr(u: unknown): u is Expr;
|
|
245
|
+
/** This function gives the child expressions of an expression, in the order of their child index. */
|
|
246
|
+
export declare function children(e: Expr): readonly Expr[];
|
|
247
|
+
//#endregion
|
|
248
|
+
//#region src/space.d.ts
|
|
249
|
+
/** The kind of a space. The kind sets which moves and axes are valid. */
|
|
250
|
+
export type SpaceKind = "record" | "array" | "grid" | "tree";
|
|
251
|
+
/**
|
|
252
|
+
* A space: an immutable collection of records with keys. A Vex program evaluates at one key of a space.
|
|
253
|
+
* Nothing changes a space after it is made.
|
|
254
|
+
*/
|
|
255
|
+
export interface Space<K extends string = string, O = unknown> {
|
|
256
|
+
readonly kind: SpaceKind;
|
|
257
|
+
/** The keys, in their order. */
|
|
258
|
+
readonly keys: readonly K[];
|
|
259
|
+
/** This function tells if the space has the key `k`. */
|
|
260
|
+
has(k: string): k is K;
|
|
261
|
+
/** This function gives the record at the key `k`, or `undefined`. */
|
|
262
|
+
get(k: string): O | undefined;
|
|
263
|
+
/** The position of each key, for array and grid spaces. An array key has one number, a grid key has two. */
|
|
264
|
+
coords(k: string): readonly number[] | undefined;
|
|
265
|
+
/** The key at a position, or `undefined`. */
|
|
266
|
+
keyAt(coords: readonly number[]): K | undefined;
|
|
267
|
+
/** The parent of the key `k` in a tree space, or `undefined` for a root. Other spaces do not have it. */
|
|
268
|
+
parentOf?(k: string): K | undefined;
|
|
269
|
+
/** The children of the key `k` in a tree space, in key order. Other spaces do not have it. */
|
|
270
|
+
childrenOf?(k: string): readonly K[];
|
|
271
|
+
}
|
|
272
|
+
/** The key type of a grid space. */
|
|
273
|
+
export type GridKey = `${number},${number}`;
|
|
274
|
+
/** The key type of an array space. */
|
|
275
|
+
export type ArrayKey = `${number}`;
|
|
276
|
+
/** The constructors of spaces. */
|
|
277
|
+
export declare const space: {
|
|
278
|
+
/** This function makes a space from the entries of an object. The keys are the property names. */
|
|
279
|
+
readonly record: <const R extends Readonly<Record<string, unknown>>>(records: R) => Space<keyof R & string, R[keyof R]>;
|
|
280
|
+
/** This function makes a space from an array. The keys are `"0"`, `"1"` and so on. */
|
|
281
|
+
readonly array: <O>(items: readonly O[]) => Space<ArrayKey, O>;
|
|
282
|
+
/** This function makes a space from rows of cells. The key of a cell is `"row,column"`. */
|
|
283
|
+
readonly grid: <O>(rows: readonly (readonly O[])[]) => Space<GridKey, O>;
|
|
284
|
+
/**
|
|
285
|
+
* This function makes a tree space: the records by key, and the parent of each key. A key without a parent, or
|
|
286
|
+
* with the parent `null`, is a root. A parent that is not a key, or a cycle of parents, throws a `TypeError`.
|
|
287
|
+
*/
|
|
288
|
+
readonly tree: <const R extends Readonly<Record<string, unknown>>>(records: R, parents: Readonly<Partial<Record<keyof R & string, (keyof R & string) | null>>>) => Space<keyof R & string, R[keyof R]>;
|
|
289
|
+
};
|
|
290
|
+
/** The position of an evaluation: the key where it started, and the key where relative references read. */
|
|
291
|
+
export interface Position {
|
|
292
|
+
readonly origin: string;
|
|
293
|
+
readonly focus: string;
|
|
294
|
+
}
|
|
295
|
+
/** This function applies one move to the focus. It gives the new focus, or a `#REF!` error. */
|
|
296
|
+
export declare function applyMove(s: Space, at: Position, m: Move): Result<string>;
|
|
297
|
+
/** This function applies the moves of an address in order. It gives the key where the address points. */
|
|
298
|
+
export declare function resolveAddr(s: Space, at: Position, addr: Addr | undefined): Result<string>;
|
|
299
|
+
/**
|
|
300
|
+
* This function gives the targets of a base axis at a focus. It does not handle `where`, because the
|
|
301
|
+
* interpreter must evaluate the test.
|
|
302
|
+
*/
|
|
303
|
+
export declare function axisTargets(s: Space, at: Position, a: Exclude<Axis, {
|
|
304
|
+
readonly t: "where";
|
|
305
|
+
}>): Result<readonly string[]>;
|
|
306
|
+
//#endregion
|
|
307
|
+
//#region src/domain.d.ts
|
|
308
|
+
/**
|
|
309
|
+
* Domains. A domain tells which values belong to it and which ops it has. The domain object owns this data.
|
|
310
|
+
* Vex does not write to the prototypes of domain classes, and an import has no side effects.
|
|
311
|
+
*/
|
|
312
|
+
/** An algebraic law that an op claims. `@mark1russell7/vex-testkit` checks each claim with property tests. */
|
|
313
|
+
export type Law = "commutative" | "associative" | "idempotent";
|
|
314
|
+
/** The kind of a parameter. The interpreter checks the arguments of an op that declares its parameters. */
|
|
315
|
+
export type ParamKind = "domain" | "number" | "boolean" | "string" | "any";
|
|
316
|
+
/** The description of one op of a domain. */
|
|
317
|
+
export interface OpSpec<D = never> {
|
|
318
|
+
/**
|
|
319
|
+
* The implementation, for an op that is not a method of the value (static-land style). The first argument
|
|
320
|
+
* is the receiver. Without `fn`, the interpreter calls the method of the receiver with the same name.
|
|
321
|
+
*/
|
|
322
|
+
readonly fn?: (self: D, ...args: never[]) => unknown;
|
|
323
|
+
/** The laws that the op claims. */
|
|
324
|
+
readonly laws?: readonly Law[];
|
|
325
|
+
/** The identity element of the op, for reductions over an empty list. */
|
|
326
|
+
readonly identity?: () => D;
|
|
327
|
+
/** Scalar lifting: `true` lifts each number argument with `fromScalar`. An array sets it for each argument. */
|
|
328
|
+
readonly liftScalar?: boolean | readonly boolean[];
|
|
329
|
+
/** The kinds of the parameters after the receiver. When set, the interpreter checks the arguments. */
|
|
330
|
+
readonly params?: readonly ParamKind[];
|
|
331
|
+
}
|
|
332
|
+
/** The type of an op that lifts number arguments. Use it to type the ops of a domain under `isolatedDeclarations`. */
|
|
333
|
+
export type LiftedOp<D> = OpSpec<D> & {
|
|
334
|
+
readonly liftScalar: true;
|
|
335
|
+
};
|
|
336
|
+
/** The type of an op with an implementation function `F`. */
|
|
337
|
+
export type FnOp<D, F extends (self: D, ...args: never[]) => unknown> = OpSpec<D> & {
|
|
338
|
+
readonly fn: F;
|
|
339
|
+
};
|
|
340
|
+
/**
|
|
341
|
+
* The type of an op table: each name in `Names` is an op, and each name in `Lifted` lifts number arguments.
|
|
342
|
+
* Use it to type the ops of a domain under `isolatedDeclarations`.
|
|
343
|
+
*/
|
|
344
|
+
export type OpTable<D, Names extends string, Lifted extends string = never> = { readonly [N in Names]: N extends Lifted ? LiftedOp<D> : OpSpec<D>; };
|
|
345
|
+
/** The description of a domain, as a caller gives it to `defineDomain`. */
|
|
346
|
+
export interface DomainSpec<Name extends string, D, Ops extends Readonly<Record<string, OpSpec<D>>>> {
|
|
347
|
+
/** The name of the domain. */
|
|
348
|
+
readonly name: Name;
|
|
349
|
+
/** The runtime test for the values of the domain. It must not give false positives. */
|
|
350
|
+
readonly is: (u: unknown) => u is D;
|
|
351
|
+
/** The ops of the domain, with their metadata. */
|
|
352
|
+
readonly ops: Ops;
|
|
353
|
+
/** With `"all"`, each method of a value is an op, also without an entry in `ops`. The default is `"declared"`. */
|
|
354
|
+
readonly methods?: "declared" | "all";
|
|
355
|
+
/** The scalar lift: it makes a domain value from a number. */
|
|
356
|
+
readonly fromScalar?: (n: number) => D;
|
|
357
|
+
/** A check of the values that ops give. A value that fails the check gives the error `#NUM!`. */
|
|
358
|
+
readonly valid?: (d: D) => boolean;
|
|
359
|
+
/** A short text for a value, for traces and the site. */
|
|
360
|
+
readonly show?: (d: D) => string;
|
|
361
|
+
/** The JSON form of a value, for literals in serialized programs. */
|
|
362
|
+
readonly encode?: (d: D) => unknown;
|
|
363
|
+
/** The value of a JSON form that `encode` made. */
|
|
364
|
+
readonly decode?: (json: unknown) => D;
|
|
365
|
+
}
|
|
366
|
+
/** A domain. Make one with `defineDomain`. */
|
|
367
|
+
export interface Domain<Name extends string = string, D = unknown, Ops extends Readonly<Record<string, OpSpec<D>>> = Readonly<Record<string, OpSpec<D>>>> extends DomainSpec<Name, D, Ops> {
|
|
368
|
+
readonly kind: "vex.domain";
|
|
369
|
+
}
|
|
370
|
+
/** A domain with any value type. The interpreter uses this type. */
|
|
371
|
+
export type AnyDomain = Domain<string, any, Readonly<Record<string, OpSpec<any>>>>;
|
|
372
|
+
/** This function makes a domain. The result is frozen. */
|
|
373
|
+
export declare function defineDomain<const Name extends string, D, const Ops extends Readonly<Record<string, OpSpec<D>>>>(spec: DomainSpec<Name, D, Ops>): Domain<Name, D, Ops>;
|
|
374
|
+
/** This function tells if a name can be an op name. */
|
|
375
|
+
export declare const isOpName: (name: string) => boolean;
|
|
376
|
+
/**
|
|
377
|
+
* This function finds a method on a value: an own function property, or a function on the prototype chain.
|
|
378
|
+
* It stops at `Object.prototype` and `Function.prototype`, so inherited members (for example `toString`) are not ops.
|
|
379
|
+
*/
|
|
380
|
+
export declare function findMethod(self: unknown, name: string): ((...args: unknown[]) => unknown) | undefined;
|
|
381
|
+
/** An op that the interpreter found for a receiver. */
|
|
382
|
+
export interface ResolvedOp {
|
|
383
|
+
readonly domain: AnyDomain;
|
|
384
|
+
readonly spec: OpSpec<unknown>;
|
|
385
|
+
readonly call: (args: readonly unknown[]) => unknown;
|
|
386
|
+
}
|
|
387
|
+
/** This function finds the op `name` for `self` in one domain. It gives `undefined` if the domain does not have it. */
|
|
388
|
+
export declare function resolveOp(domain: AnyDomain, self: unknown, name: string): ResolvedOp | undefined;
|
|
389
|
+
/** This function gives the domain of a value: the first domain whose `is` accepts it. */
|
|
390
|
+
export declare function domainOf(domains: readonly AnyDomain[], value: unknown): AnyDomain | undefined;
|
|
391
|
+
/** This function tells if the `liftScalar` setting lifts the argument at `index`. */
|
|
392
|
+
export declare const liftsAt: (lift: OpSpec<unknown>["liftScalar"], index: number) => boolean;
|
|
393
|
+
//#endregion
|
|
394
|
+
//#region src/list.d.ts
|
|
395
|
+
/** One item of a list: the key of the target and the result of the body there. */
|
|
396
|
+
export interface ListItem<T = unknown> {
|
|
397
|
+
readonly key: string;
|
|
398
|
+
readonly result: Result<T>;
|
|
399
|
+
}
|
|
400
|
+
/** The value of an `each` expression: one item for each target of the axis. */
|
|
401
|
+
export interface VexList<T = unknown> {
|
|
402
|
+
readonly kind: "vex.list";
|
|
403
|
+
readonly items: readonly ListItem<T>[];
|
|
404
|
+
}
|
|
405
|
+
/** This function makes a list. */
|
|
406
|
+
export declare const vexList: <T>(items: readonly ListItem<T>[]) => VexList<T>;
|
|
407
|
+
/** This function tells if a value is a Vex list. */
|
|
408
|
+
export declare const isVexList: (u: unknown) => u is VexList;
|
|
409
|
+
/** The name of a core list op. */
|
|
410
|
+
export type ListOp = "count" | "sum" | "min" | "max" | "mean" | "any" | "all" | "none" | "values" | "keys" | "reduce" | "first" | "errors";
|
|
411
|
+
/** The names of the core list ops. They apply when the first argument is a list. */
|
|
412
|
+
export declare const LIST_OPS: ReadonlySet<string>;
|
|
413
|
+
/** This function tells if a name is the name of a core list op. */
|
|
414
|
+
export declare const isListOp: (op: string) => op is ListOp;
|
|
415
|
+
/** The options of a list op. */
|
|
416
|
+
export interface ListOptions {
|
|
417
|
+
/** With `true`, any error item makes the result that error. The default skips error items. */
|
|
418
|
+
readonly strict?: boolean;
|
|
419
|
+
}
|
|
420
|
+
//#endregion
|
|
421
|
+
//#region src/trace.d.ts
|
|
422
|
+
/** One field read of a reference: the key and the path that it read, and if the read found a value. */
|
|
423
|
+
export interface Read {
|
|
424
|
+
readonly key: string;
|
|
425
|
+
readonly path: readonly string[];
|
|
426
|
+
readonly ok: boolean;
|
|
427
|
+
}
|
|
428
|
+
/** The record of one node evaluation. The interpreter makes one event each time it evaluates a node. */
|
|
429
|
+
export interface TraceEvent {
|
|
430
|
+
/** The position of the node in the expression tree. */
|
|
431
|
+
readonly path: readonly number[];
|
|
432
|
+
readonly tag: ExprTag;
|
|
433
|
+
/** A short text for the node: the op name, the variable name or the field path. */
|
|
434
|
+
readonly label: string;
|
|
435
|
+
/** The key where the evaluation started. */
|
|
436
|
+
readonly origin: string;
|
|
437
|
+
/** The key where relative references read. Inside an axis, this is the target. */
|
|
438
|
+
readonly focus: string;
|
|
439
|
+
/** The field reads of a reference node. */
|
|
440
|
+
readonly reads?: readonly Read[];
|
|
441
|
+
/** The result of the node. */
|
|
442
|
+
readonly result: Result<unknown>;
|
|
443
|
+
}
|
|
444
|
+
/** A trace: the events of one evaluation, in the order that the nodes finished. */
|
|
445
|
+
export interface Trace {
|
|
446
|
+
readonly events: readonly TraceEvent[];
|
|
447
|
+
readonly result: Result<unknown>;
|
|
448
|
+
}
|
|
449
|
+
/** A receiver of trace events. */
|
|
450
|
+
export interface TraceSink {
|
|
451
|
+
readonly events: TraceEvent[];
|
|
452
|
+
}
|
|
453
|
+
//#endregion
|
|
454
|
+
//#region src/evaluate.d.ts
|
|
455
|
+
/** A free function op. The interpreter gives it the values of all arguments. */
|
|
456
|
+
export type FreeFn = (...args: readonly unknown[]) => unknown;
|
|
457
|
+
/** The context of an extension handler. */
|
|
458
|
+
export interface ExtContext {
|
|
459
|
+
readonly space: Space;
|
|
460
|
+
readonly position: Position;
|
|
461
|
+
readonly path: readonly number[];
|
|
462
|
+
}
|
|
463
|
+
/** The handler of an extension expression kind. */
|
|
464
|
+
export type ExtHandler = (data: unknown, ctx: ExtContext) => Result<unknown>;
|
|
465
|
+
/** The options of an evaluation. */
|
|
466
|
+
export interface EvalOptions {
|
|
467
|
+
/** The space that the program reads. */
|
|
468
|
+
readonly space: Space;
|
|
469
|
+
/** The key where the evaluation starts. Relative references read here. */
|
|
470
|
+
readonly origin: string;
|
|
471
|
+
/** The domains. The interpreter finds the op of a value in the first domain that accepts the value. */
|
|
472
|
+
readonly domains?: readonly AnyDomain[];
|
|
473
|
+
/** Free function ops, for ops that are not domain ops. */
|
|
474
|
+
readonly fns?: Readonly<Record<string, FreeFn>>;
|
|
475
|
+
/** The handlers of extension expression kinds. */
|
|
476
|
+
readonly extensions?: Readonly<Record<string, ExtHandler>>;
|
|
477
|
+
/** Variables that the program can read with `v(name)`. */
|
|
478
|
+
readonly vars?: Readonly<Record<string, unknown>>;
|
|
479
|
+
/** A receiver of trace events. */
|
|
480
|
+
readonly trace?: TraceSink;
|
|
481
|
+
}
|
|
482
|
+
/** The name of a special form. */
|
|
483
|
+
export type SpecialForm = "if" | "and" | "or" | "ifError";
|
|
484
|
+
/** The names of the special forms. Their arguments are not evaluated before the call. */
|
|
485
|
+
export declare const SPECIAL_FORMS: ReadonlySet<string>;
|
|
486
|
+
/** The options that a compiled program keeps: the domains, the free functions and the extension handlers. */
|
|
487
|
+
export type CompileOptions = Pick<EvalOptions, "domains" | "fns" | "extensions">;
|
|
488
|
+
/** The options of one run of a compiled program. */
|
|
489
|
+
export type RunOptions = Pick<EvalOptions, "space" | "origin" | "vars" | "trace">;
|
|
490
|
+
/**
|
|
491
|
+
* A compiled program: the expression as a tree of functions. A program evaluates many times at different origins,
|
|
492
|
+
* and over different spaces, without a new walk of the expression.
|
|
493
|
+
*/
|
|
494
|
+
export interface Program {
|
|
495
|
+
/** The expression of the program. */
|
|
496
|
+
readonly expr: Expr;
|
|
497
|
+
/** This method evaluates the program at an origin. It does not throw. */
|
|
498
|
+
run(opts: RunOptions): Result<unknown>;
|
|
499
|
+
/** This method evaluates the program at an origin, with a trace of each node. */
|
|
500
|
+
explain(opts: Omit<RunOptions, "trace">): Trace;
|
|
501
|
+
}
|
|
502
|
+
/**
|
|
503
|
+
* This function compiles an expression. The result gives the same results and the same traces as `evaluate`,
|
|
504
|
+
* because `evaluate` uses it too.
|
|
505
|
+
*/
|
|
506
|
+
export declare function compile(expr: Expr, opts?: CompileOptions): Program;
|
|
507
|
+
/** This function evaluates an expression at the origin of a space. It does not throw. */
|
|
508
|
+
export declare function evaluate(expr: Expr, opts: EvalOptions): Result<unknown>;
|
|
509
|
+
/** This function evaluates an expression and records a trace of each node. */
|
|
510
|
+
export declare function explain(expr: Expr, opts: Omit<EvalOptions, "trace">): Trace;
|
|
511
|
+
/** This function gives a short name for the type of a value, for error messages. */
|
|
512
|
+
export declare function describeType(u: unknown): string;
|
|
513
|
+
/** This function gives the label of a node, for traces. */
|
|
514
|
+
export declare function labelOf(e: Expr): string;
|
|
515
|
+
/** This function gives a short text for a value. */
|
|
516
|
+
export declare function previewValue(u: unknown): string;
|
|
517
|
+
/**
|
|
518
|
+
* This function gives a text form of a trace. Each event has one line, in finish order. The indent of a line
|
|
519
|
+
* shows the depth of the node. A domain with `show` gives the text of its values. The golden tests compare this text.
|
|
520
|
+
*/
|
|
521
|
+
export declare function formatTrace(trace: Trace, domains?: readonly AnyDomain[]): string;
|
|
522
|
+
/** This function gives the number of nodes of an expression. */
|
|
523
|
+
export declare const sizeOf: (e: Expr) => number;
|
|
524
|
+
//#endregion
|
|
525
|
+
//#region src/deps.d.ts
|
|
526
|
+
/** One static read of a program: a field path at an address, inside zero or more axes. */
|
|
527
|
+
export interface Dependency {
|
|
528
|
+
/** The address of the read. The empty address is the focus. */
|
|
529
|
+
readonly at: Addr;
|
|
530
|
+
/** The field path of the read. */
|
|
531
|
+
readonly path: readonly string[];
|
|
532
|
+
/** The axes around the read, from the outside to the inside. Inside an axis, the focus is the target. */
|
|
533
|
+
readonly axes: readonly Axis["t"][];
|
|
534
|
+
}
|
|
535
|
+
/**
|
|
536
|
+
* This function gives the static reads of an expression. A host can use them to recompute a program only
|
|
537
|
+
* when a field that it reads changes.
|
|
538
|
+
*/
|
|
539
|
+
export declare function deps(e: Expr): readonly Dependency[];
|
|
540
|
+
//#endregion
|
|
541
|
+
//#region src/json.d.ts
|
|
542
|
+
/**
|
|
543
|
+
* This function gives the JSON text of an expression. A literal that is a domain value needs a domain with
|
|
544
|
+
* `encode`. A literal that is not JSON data and has no such domain gives the error `#VALUE!`.
|
|
545
|
+
*/
|
|
546
|
+
export declare function serialize(e: Expr, domains?: readonly AnyDomain[]): Result<string>;
|
|
547
|
+
/** This function reads an expression from JSON text. It checks the whole tree. */
|
|
548
|
+
export declare function parse(text: string, domains?: readonly AnyDomain[]): Result<Expr>;
|
|
549
|
+
//#endregion
|
|
550
|
+
//#region src/traversal.d.ts
|
|
551
|
+
/** A monoid: an associative `concat` with an identity element `empty`. */
|
|
552
|
+
export interface Monoid<M> {
|
|
553
|
+
readonly empty: M;
|
|
554
|
+
readonly concat: (a: M, b: M) => M;
|
|
555
|
+
}
|
|
556
|
+
/** Standard monoids. */
|
|
557
|
+
export declare const monoids: {
|
|
558
|
+
readonly sum: Monoid<number>;
|
|
559
|
+
readonly product: Monoid<number>;
|
|
560
|
+
readonly all: Monoid<boolean>;
|
|
561
|
+
readonly any: Monoid<boolean>;
|
|
562
|
+
};
|
|
563
|
+
/** One item of a traversal: the start key and the result there. */
|
|
564
|
+
export interface TraversalItem<K extends string, T> {
|
|
565
|
+
readonly key: K;
|
|
566
|
+
readonly result: Result<T>;
|
|
567
|
+
}
|
|
568
|
+
/**
|
|
569
|
+
* A traversal: the results of one program at each start key. The reductions skip error items by default.
|
|
570
|
+
* After `strict()`, an error item makes the reduction that error.
|
|
571
|
+
*/
|
|
572
|
+
export interface Traversal<K extends string, T> {
|
|
573
|
+
readonly items: readonly TraversalItem<K, T>[];
|
|
574
|
+
/** This method gives a traversal whose reductions fail on the first error item. */
|
|
575
|
+
strict(): Traversal<K, T>;
|
|
576
|
+
/** This method gives the result at one key. */
|
|
577
|
+
get(k: K): Optional<T>;
|
|
578
|
+
/** The values of the items without errors. */
|
|
579
|
+
values(): readonly T[];
|
|
580
|
+
/** The keys of the items without errors. */
|
|
581
|
+
keys(): readonly K[];
|
|
582
|
+
/** The errors of the items with errors. */
|
|
583
|
+
errors(): readonly VexError[];
|
|
584
|
+
count(): Optional<number>;
|
|
585
|
+
sum(this: Traversal<K, number>): Optional<number>;
|
|
586
|
+
mean(this: Traversal<K, number>): Optional<number>;
|
|
587
|
+
min(this: Traversal<K, number>): Optional<number>;
|
|
588
|
+
max(this: Traversal<K, number>): Optional<number>;
|
|
589
|
+
any(this: Traversal<K, boolean>): Optional<boolean>;
|
|
590
|
+
all(this: Traversal<K, boolean>): Optional<boolean>;
|
|
591
|
+
none(this: Traversal<K, boolean>): Optional<boolean>;
|
|
592
|
+
/** This method folds the values with a domain op, from the left. */
|
|
593
|
+
reduce(op: string): Optional<T>;
|
|
594
|
+
/** This method gives the result of a reduction, with its error. */
|
|
595
|
+
reduceResult(op: "count" | "sum" | "mean" | "min" | "max" | "any" | "all" | "none" | "reduce", reduceOp?: string): Result<unknown>;
|
|
596
|
+
/** This method maps each value with `f`. If `f` throws, the item has the error `#CALC!`. */
|
|
597
|
+
map<U>(f: (value: T, key: K) => U): Traversal<K, U>;
|
|
598
|
+
/** This method folds the values with a monoid. */
|
|
599
|
+
fold<M>(monoid: Monoid<M>, f: (value: T) => M): M;
|
|
600
|
+
}
|
|
601
|
+
interface TraversalEnv {
|
|
602
|
+
readonly domains: readonly AnyDomain[];
|
|
603
|
+
readonly options?: {
|
|
604
|
+
readonly fns?: Readonly<Record<string, FreeFn>>;
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
/** This function makes a traversal from items. */
|
|
608
|
+
export declare function traversal<K extends string, T>(items: readonly TraversalItem<K, T>[], env: TraversalEnv, isStrict?: boolean): Traversal<K, T>;
|
|
609
|
+
//#endregion
|
|
610
|
+
//#region src/sheet.d.ts
|
|
611
|
+
/** The extension kind of a cell reference. */
|
|
612
|
+
export declare const CELL_KIND = "vex.cell";
|
|
613
|
+
/** The data of a cell reference: the column, and the address from the focus. */
|
|
614
|
+
export interface CellRef {
|
|
615
|
+
readonly column: string;
|
|
616
|
+
readonly at: Addr;
|
|
617
|
+
}
|
|
618
|
+
/** This function makes a cell reference: the column `column` at the address `at` from the focus. */
|
|
619
|
+
export declare const cell: (column: string, at?: Addr) => Expr;
|
|
620
|
+
/** The options of a sheet run: the evaluation options without the origin and the trace. */
|
|
621
|
+
export type SheetOptions = Omit<EvalOptions, "origin" | "trace">;
|
|
622
|
+
export declare class SheetRun {
|
|
623
|
+
#private;
|
|
624
|
+
constructor(columns: ReadonlyMap<string, Expr>, opts: SheetOptions);
|
|
625
|
+
/** This method gives the result of one cell: the column `column` at the key `key`. */
|
|
626
|
+
cell(column: string, key: string): Result<unknown>;
|
|
627
|
+
/** This method evaluates one cell with a trace of the nodes of its column. The result is the result of the cell. */
|
|
628
|
+
explain(column: string, key: string): Trace;
|
|
629
|
+
/**
|
|
630
|
+
* This method evaluates the column `column` at each key before `key`, in key order. A recurrence over the keys,
|
|
631
|
+
* for example a running total, then reads finished cells and does not go deep into the call stack.
|
|
632
|
+
*/
|
|
633
|
+
warm(column: string, key: string): void;
|
|
634
|
+
}
|
|
635
|
+
//#endregion
|
|
636
|
+
//#region src/builder.d.ts
|
|
637
|
+
declare const ARG_TYPE: unique symbol;
|
|
638
|
+
declare const CHAIN_VALUE: unique symbol;
|
|
639
|
+
/** An argument token: an expression with the static type `T` of its value. */
|
|
640
|
+
export interface Arg<T> {
|
|
641
|
+
readonly kind: "vex.arg";
|
|
642
|
+
readonly expr: Expr;
|
|
643
|
+
/** This phantom member carries the type. It is not present at run time. */
|
|
644
|
+
readonly [ARG_TYPE]?: T;
|
|
645
|
+
}
|
|
646
|
+
/** The keys of any member of a union of record types. */
|
|
647
|
+
type AllKeys<O> = O extends unknown ? keyof O : never;
|
|
648
|
+
/** The type of field `P` in the members of `O` that have it. */
|
|
649
|
+
type FieldType<O, P extends PropertyKey> = O extends unknown ? (P extends keyof O ? O[P] : never) : never;
|
|
650
|
+
/**
|
|
651
|
+
* The merged view of a union of record types: each field of any member, with the union of its types. A record
|
|
652
|
+
* without the field gives `#N/A` at run time.
|
|
653
|
+
*/
|
|
654
|
+
export type Merge<O> = { readonly [P in AllKeys<O>]: FieldType<O, P>; };
|
|
655
|
+
/** The keys of `O` whose values have the type `T`. */
|
|
656
|
+
export type KeysOfType<O, T> = { [P in keyof O]-?: [NonNullable<O[P]>] extends [never] ? never : NonNullable<O[P]> extends T ? P : never; }[keyof O] & string;
|
|
657
|
+
/** The method names of `D`. */
|
|
658
|
+
export type MethodKeys<D> = { [P in keyof D]-?: D[P] extends ((...args: never[]) => unknown) ? P : never; }[keyof D] & string;
|
|
659
|
+
/** The value type of a domain. */
|
|
660
|
+
export type DomainValue<Dm> = Dm extends Domain<string, infer D, infer _Ops> ? D : never;
|
|
661
|
+
type OpsOf<Dm> = Dm extends Domain<string, unknown, infer Ops> ? Ops : Dm extends {
|
|
662
|
+
readonly ops: infer Ops;
|
|
663
|
+
} ? Ops : never;
|
|
664
|
+
/** The op names that a chain over a value of domain `Dm` can use. */
|
|
665
|
+
export type OpNames<Dm> = (keyof OpsOf<Dm> & string) | (Dm extends {
|
|
666
|
+
readonly methods: "all";
|
|
667
|
+
} ? MethodKeys<DomainValue<Dm>> : never);
|
|
668
|
+
type FnOf<Dm, M extends string> = M extends keyof OpsOf<Dm> ? OpsOf<Dm>[M] extends {
|
|
669
|
+
readonly fn: (self: never, ...args: infer P) => infer R;
|
|
670
|
+
} ? (...args: P) => R : M extends keyof DomainValue<Dm> ? DomainValue<Dm>[M] : never : M extends keyof DomainValue<Dm> ? DomainValue<Dm>[M] : never;
|
|
671
|
+
/** The parameters of op `M` after the receiver. */
|
|
672
|
+
export type OpParams<Dm, M extends string> = FnOf<Dm, M> extends ((...args: infer P) => unknown) ? P : never;
|
|
673
|
+
/** The value type that op `M` gives. */
|
|
674
|
+
export type OpReturn<Dm, M extends string> = FnOf<Dm, M> extends ((...args: never[]) => infer R) ? R : never;
|
|
675
|
+
type Lifts<Dm, M extends string> = M extends keyof OpsOf<Dm> ? (OpsOf<Dm>[M] extends {
|
|
676
|
+
readonly liftScalar: true;
|
|
677
|
+
} ? true : false) : false;
|
|
678
|
+
/** The first domain of `Dms` whose values include `T`, or `never`. */
|
|
679
|
+
export type DomainFor<Dms extends readonly AnyDomain[], T> = Dms extends readonly [infer H, ...infer R extends readonly AnyDomain[]] ? [T] extends [never] ? never : [T] extends [DomainValue<H>] ? H : DomainFor<R, T> : never;
|
|
680
|
+
type UnionToIntersection<U> = (U extends unknown ? (x: U) => void : never) extends ((x: infer I) => void) ? I : never;
|
|
681
|
+
type LastOf<U> = UnionToIntersection<U extends unknown ? () => U : never> extends (() => infer R) ? R : never;
|
|
682
|
+
/** `true` if `other()` is permitted for the key type: a pair of keys, or keys that the compiler does not know. */
|
|
683
|
+
export type AllowsOther<K extends string> = string extends K ? true : `${number}` extends K ? true : [Exclude<K, LastOf<K>>] extends [never] ? false : [Exclude<Exclude<K, LastOf<K>>, LastOf<Exclude<K, LastOf<K>>>>] extends [never] ? true : false;
|
|
684
|
+
/**
|
|
685
|
+
* The inputs for an argument with the parameter type `P`. The input can be a literal of type `P`, but not a bare
|
|
686
|
+
* string. It can be a field of the record with type `P`, an argument token, or a chain that gives `P`. If the op
|
|
687
|
+
* lifts scalars, it can also be a number.
|
|
688
|
+
*/
|
|
689
|
+
export type ArgInput<P, Ctx extends ChainContext, Lift extends boolean> = (P extends string ? never : P) | KeysOfType<Ctx["record"], P> | Arg<P> | ChainOf<Ctx, P> | (Lift extends true ? ([P] extends [DomainValue<DomainFor<Ctx["domains"], P>>] ? number : never) : never);
|
|
690
|
+
type MapArgs<P extends readonly unknown[], Ctx extends ChainContext, Lift extends boolean> = { [I in keyof P]: ArgInput<P[I], Ctx, Lift>; };
|
|
691
|
+
/** The static context of a chain: its domains, its key type and its record type. */
|
|
692
|
+
export interface ChainContext {
|
|
693
|
+
readonly domains: readonly AnyDomain[];
|
|
694
|
+
readonly keys: string;
|
|
695
|
+
readonly record: unknown;
|
|
696
|
+
}
|
|
697
|
+
/** The ops section `._` of a chain whose value has domain `Dm`. */
|
|
698
|
+
export type OpsProxy<Ctx extends ChainContext, Dm> = { readonly [M in OpNames<Dm>]: (...args: MapArgs<OpParams<Dm, M>, Ctx, Lifts<Dm, M>>) => ChainOf<Ctx, OpReturn<Dm, M>>; };
|
|
699
|
+
/** The members of every chain. */
|
|
700
|
+
export interface ChainBase<Ctx extends ChainContext, T> {
|
|
701
|
+
readonly kind: "vex.chain";
|
|
702
|
+
/** This phantom member carries the value type. It is not present at run time. */
|
|
703
|
+
readonly [CHAIN_VALUE]?: T;
|
|
704
|
+
/** The expression of the chain, as plain data. */
|
|
705
|
+
readonly program: Expr;
|
|
706
|
+
/** This method evaluates the chain at a key. An error gives `none`. */
|
|
707
|
+
at(k: Ctx["keys"]): Optional<T>;
|
|
708
|
+
/** This method is the same as `at`. The v0.9 spec calls it `value`. */
|
|
709
|
+
value(k: Ctx["keys"]): Optional<T>;
|
|
710
|
+
/** This method evaluates the chain at a key, and gives the error if there is one. */
|
|
711
|
+
result(k: Ctx["keys"]): Result<T>;
|
|
712
|
+
/** This method evaluates the chain at a key, with a trace of each node. */
|
|
713
|
+
explain(k: Ctx["keys"]): Trace;
|
|
714
|
+
/** This method evaluates the chain at each key of the space (the start axis). */
|
|
715
|
+
all(): Traversal<Ctx["keys"], T>;
|
|
716
|
+
/** This method gives a chain whose value is a field of the record at the current address. */
|
|
717
|
+
from<P extends keyof Ctx["record"] & string>(field: P): ChainOf<Ctx, NonNullable<Ctx["record"][P]>>;
|
|
718
|
+
/** Navigation: later field references read at the key `k`. */
|
|
719
|
+
to(k: Ctx["keys"]): ChainOf<Ctx, T>;
|
|
720
|
+
/** Navigation: later field references read at the key with this position in the key order. */
|
|
721
|
+
index(i: number): ChainOf<Ctx, T>;
|
|
722
|
+
/** Navigation: later field references read at a relative position in an array or a grid. */
|
|
723
|
+
offset(...d: readonly number[]): ChainOf<Ctx, T>;
|
|
724
|
+
/** Navigation: later field references read at the origin again. */
|
|
725
|
+
origin(): ChainOf<Ctx, T>;
|
|
726
|
+
/** Navigation: later field references read at the parent of the focus, in a tree space. A root gives `#REF!`. */
|
|
727
|
+
parent(): ChainOf<Ctx, T>;
|
|
728
|
+
/** This method uses `fallback` when the chain gives an error. */
|
|
729
|
+
ifError<U>(fallback: ArgInput<U, Ctx, false> | T): ChainOf<Ctx, T | U>;
|
|
730
|
+
/** This method evaluates each branch with the current value, and gives a record of the results. */
|
|
731
|
+
fork<const B extends Readonly<Record<string, (c: ChainOf<Ctx, T>) => ChainBase<Ctx, unknown>>>>(branches: B): ChainOf<Ctx, { readonly [N in keyof B]: ValueOf<ReturnType<B[N]>>; }>;
|
|
732
|
+
/** This method binds names for the body. The body gets a variable token for each name. */
|
|
733
|
+
with<const B extends Readonly<Record<string, unknown>>, U>(binds: B, body: (c: ChainOf<Ctx, T>, vars: { readonly [N in keyof B]: Arg<ArgValue<Ctx, B[N]>>; }) => ChainBase<Ctx, U>): ChainOf<Ctx, U>;
|
|
734
|
+
/** The others axis: the body evaluates at each key except the focus. Its start value is the current value. */
|
|
735
|
+
others<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
736
|
+
/** The neighbors axis of a grid. */
|
|
737
|
+
neighbors<U>(n: 4 | 8, body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
738
|
+
/** The all axis inside the expression: the body evaluates at each key, with the current value as start value. */
|
|
739
|
+
each<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
740
|
+
/** The children axis of a tree space: the body evaluates at each child of the focus. */
|
|
741
|
+
children<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
742
|
+
/** The ancestors axis of a tree space: the parent first, the root last. */
|
|
743
|
+
ancestors<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
744
|
+
/** The descendants axis of a tree space: depth first, each parent before its children. */
|
|
745
|
+
descendants<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
746
|
+
/** The siblings axis of a tree space: the other children of the parent, or the other roots for a root. */
|
|
747
|
+
siblings<U>(body: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, U>, opts?: AxisOpts<Ctx, T>): ListChain<Ctx, U>;
|
|
748
|
+
}
|
|
749
|
+
/** The options of an axis. */
|
|
750
|
+
export interface AxisOpts<Ctx extends ChainContext, T> {
|
|
751
|
+
/** A test at each target. The body evaluates only at the targets where the test gives `true`. */
|
|
752
|
+
readonly where?: (c: ChainOf<Ctx, T>) => ChainBase<Ctx, boolean>;
|
|
753
|
+
}
|
|
754
|
+
/** The members of a chain whose value has a domain. */
|
|
755
|
+
export interface DomainMembers<Ctx extends ChainContext, Dm> {
|
|
756
|
+
/** The ops of the domain of the current value. */
|
|
757
|
+
readonly _: OpsProxy<Ctx, Dm>;
|
|
758
|
+
}
|
|
759
|
+
/** The members of a chain over a space whose keys permit `other()`. */
|
|
760
|
+
export interface OtherMembers<Ctx extends ChainContext, T> {
|
|
761
|
+
/** Navigation: later field references read at the other key of the pair. */
|
|
762
|
+
other(): ChainOf<Ctx, T>;
|
|
763
|
+
}
|
|
764
|
+
/** A chain with the current value type `T`. */
|
|
765
|
+
export type ChainOf<Ctx extends ChainContext, T> = ChainBase<Ctx, T> & ([DomainFor<Ctx["domains"], T>] extends [never] ? unknown : DomainMembers<Ctx, DomainFor<Ctx["domains"], T>>) & (AllowsOther<Ctx["keys"]> extends true ? OtherMembers<Ctx, T> : unknown);
|
|
766
|
+
/** The value type of a chain. */
|
|
767
|
+
export type ValueOf<C> = C extends {
|
|
768
|
+
readonly [CHAIN_VALUE]?: infer T;
|
|
769
|
+
} ? T : never;
|
|
770
|
+
/** The value type of an argument input. */
|
|
771
|
+
export type ArgValue<Ctx extends ChainContext, A> = A extends Arg<infer T> ? T : A extends {
|
|
772
|
+
readonly kind: "vex.chain";
|
|
773
|
+
} ? ValueOf<A> : A extends keyof Ctx["record"] & string ? NonNullable<Ctx["record"][A]> : A;
|
|
774
|
+
/** A chain whose value is a list: the result of an axis. */
|
|
775
|
+
export interface ListChain<Ctx extends ChainContext, U> {
|
|
776
|
+
readonly kind: "vex.list-chain";
|
|
777
|
+
/** The expression of the chain, as plain data. */
|
|
778
|
+
readonly program: Expr;
|
|
779
|
+
count(opts?: ListOpts): ChainOf<Ctx, number>;
|
|
780
|
+
sum(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
781
|
+
mean(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
782
|
+
min(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
783
|
+
max(this: ListChain<Ctx, number>, opts?: ListOpts): ChainOf<Ctx, number>;
|
|
784
|
+
any(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
785
|
+
all(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
786
|
+
none(this: ListChain<Ctx, boolean>, opts?: ListOpts): ChainOf<Ctx, boolean>;
|
|
787
|
+
values(opts?: ListOpts): ChainOf<Ctx, readonly U[]>;
|
|
788
|
+
first(opts?: ListOpts): ChainOf<Ctx, U>;
|
|
789
|
+
reduce(op: OpNames<DomainFor<Ctx["domains"], U>>, opts?: ListOpts): ChainOf<Ctx, U>;
|
|
790
|
+
}
|
|
791
|
+
/** The options of a list reduction. */
|
|
792
|
+
export interface ListOpts {
|
|
793
|
+
/** With `true`, an error item makes the result that error. The default skips error items. */
|
|
794
|
+
readonly strict?: boolean;
|
|
795
|
+
}
|
|
796
|
+
/** The root of the builder for one space. */
|
|
797
|
+
export interface Root<Ctx extends ChainContext> {
|
|
798
|
+
/** This method starts a chain with a field of the record at the focus. */
|
|
799
|
+
from<P extends keyof Ctx["record"] & string>(field: P): ChainOf<Ctx, NonNullable<Ctx["record"][P]>>;
|
|
800
|
+
/** This method starts a chain with a value. A string is a field reference, as in an argument. */
|
|
801
|
+
start<const T>(value: T): ChainOf<Ctx, ArgValue<Ctx, T>>;
|
|
802
|
+
/** This method makes an absolute reference: the field of the record at key `k`. */
|
|
803
|
+
of<P extends keyof Ctx["record"] & string>(k: Ctx["keys"], field: P): Arg<NonNullable<Ctx["record"][P]>>;
|
|
804
|
+
/** This method makes a reference to a dotted path at the focus, with the type that the caller gives. */
|
|
805
|
+
field<T>(path: string): Arg<T>;
|
|
806
|
+
/** This method makes a literal argument. Use it for a string or an array: a bare string is a field reference. */
|
|
807
|
+
lit<const T>(value: T): Arg<T>;
|
|
808
|
+
/** This method makes an absolute reference to a dotted path at key `k`, with the type that the caller gives. */
|
|
809
|
+
ofPath<T>(k: Ctx["keys"], path: string): Arg<T>;
|
|
810
|
+
/** This method makes a record argument from field arguments. */
|
|
811
|
+
rec<const F extends Readonly<Record<string, unknown>>>(fields: F): Arg<{ readonly [N in keyof F]: ArgValue<Ctx, F[N]>; }>;
|
|
812
|
+
/**
|
|
813
|
+
* This method makes an extension argument: the node `ext(kind, data)`. The handler of `kind` in the options of
|
|
814
|
+
* `withOptions` gives its value. The caller gives the type `T` of that value.
|
|
815
|
+
*/
|
|
816
|
+
ext<T>(kind: string, data?: unknown): Arg<T>;
|
|
817
|
+
/** This method starts a sheet over the space: named columns of formulas. Refer to `Sheet`. */
|
|
818
|
+
sheet(): Sheet<Ctx, object>;
|
|
819
|
+
/** The space of the root. */
|
|
820
|
+
readonly space: Space<Ctx["keys"], Ctx["record"]>;
|
|
821
|
+
}
|
|
822
|
+
/** The root of a column formula: the root of the builder, and references to the cells of the sheet. */
|
|
823
|
+
export interface SheetRoot<Ctx extends ChainContext, Cols> extends Root<Ctx> {
|
|
824
|
+
/**
|
|
825
|
+
* This method makes a cell reference: the column `column` at the key `at`, or at the address `at` from the focus.
|
|
826
|
+
* Without `at`, it reads at the focus. The type is the type of a column before this one.
|
|
827
|
+
*/
|
|
828
|
+
cell<N extends keyof Cols & string>(column: N, at?: Ctx["keys"] | Addr): Arg<Cols[N]>;
|
|
829
|
+
/** This method makes a cell reference to any column, also this column or a later one. The caller gives the type. */
|
|
830
|
+
cell<T>(column: string, at?: Ctx["keys"] | Addr): Arg<T>;
|
|
831
|
+
}
|
|
832
|
+
/** One row of a sheet: the key and the result of each column. */
|
|
833
|
+
export interface SheetRow<K extends string, Cols> {
|
|
834
|
+
readonly key: K;
|
|
835
|
+
readonly cells: { readonly [N in keyof Cols]: Result<Cols[N]>; };
|
|
836
|
+
}
|
|
837
|
+
/**
|
|
838
|
+
* A sheet: named columns of formulas over a space, like the computed columns of a spreadsheet. A formula reads the
|
|
839
|
+
* other columns with `cell`. Each evaluation evaluates a cell once, and each cell on a cycle of references gives
|
|
840
|
+
* `#CYCLE!`. A sheet is immutable: `column` gives a new sheet.
|
|
841
|
+
*/
|
|
842
|
+
export interface Sheet<Ctx extends ChainContext, Cols> {
|
|
843
|
+
/** This method gives a new sheet with one more column. The formula gets a root with `cell`. */
|
|
844
|
+
column<const N extends string, C extends ChainBase<Ctx, unknown>>(name: N, formula: (r: SheetRoot<Ctx, Cols>) => C): Sheet<Ctx, Cols & { readonly [P in N]: ValueOf<C>; }>;
|
|
845
|
+
/** The program of each column. */
|
|
846
|
+
readonly columns: Readonly<Record<string, Expr>>;
|
|
847
|
+
/** This method evaluates one cell, and gives the error if there is one. */
|
|
848
|
+
result<N extends keyof Cols & string>(k: Ctx["keys"], column: N): Result<Cols[N]>;
|
|
849
|
+
/** This method evaluates one cell. An error gives `none`. */
|
|
850
|
+
at<N extends keyof Cols & string>(k: Ctx["keys"], column: N): Optional<Cols[N]>;
|
|
851
|
+
/** This method evaluates one cell, with a trace of the nodes of its column. */
|
|
852
|
+
explain(k: Ctx["keys"], column: keyof Cols & string): Trace;
|
|
853
|
+
/** This method evaluates each cell, in key order, with one result for each cell. */
|
|
854
|
+
table(): readonly SheetRow<Ctx["keys"], Cols>[];
|
|
855
|
+
}
|
|
856
|
+
/** The options of the builder. */
|
|
857
|
+
export interface VexOptions {
|
|
858
|
+
/** Free function ops. */
|
|
859
|
+
readonly fns?: Readonly<Record<string, FreeFn>>;
|
|
860
|
+
/** Handlers of extension expression kinds. */
|
|
861
|
+
readonly extensions?: Readonly<Record<string, ExtHandler>>;
|
|
862
|
+
}
|
|
863
|
+
/** The entry point for a list of domains. */
|
|
864
|
+
export interface VexEntry<Dms extends readonly AnyDomain[]> {
|
|
865
|
+
/** This method gives the root of the builder for a space. */
|
|
866
|
+
over<K extends string, O>(s: Space<K, O>): Root<{
|
|
867
|
+
readonly domains: Dms;
|
|
868
|
+
readonly keys: K;
|
|
869
|
+
readonly record: Merge<O>;
|
|
870
|
+
}>;
|
|
871
|
+
/** The domains. */
|
|
872
|
+
readonly domains: Dms;
|
|
873
|
+
}
|
|
874
|
+
/**
|
|
875
|
+
* This function gives the entry point of the builder for a list of domains. The interpreter finds the op of a
|
|
876
|
+
* value in the first domain that accepts the value.
|
|
877
|
+
*/
|
|
878
|
+
export declare function vex<const Dms extends readonly AnyDomain[]>(...domains: Dms): VexEntry<Dms> & {
|
|
879
|
+
withOptions(options: VexOptions): VexEntry<Dms>;
|
|
880
|
+
};
|
|
881
|
+
/** The type of a domain value, from the domain object. */
|
|
882
|
+
export type ValueOfDomain<Dm> = DomainValue<Dm>;
|
|
883
|
+
//#endregion
|
|
884
|
+
//# sourceMappingURL=index.d.ts.map
|