@northtalk/ts 0.5.0 → 0.6.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 +11 -0
- package/base64-CDO-a06R.js +29 -0
- package/{binary-value-B3knLdHD.d.ts → binary-value-C4ZiXVjv.d.ts} +1 -1
- package/{checker-DMI4LiRm.js → checker-CUhn2XaM.js} +4 -4
- package/{core-D_9OuTRE.d.ts → core-egHbNmve.d.ts} +1 -1
- package/{debug-nN5tQ_p4.d.ts → debug--Of4MSLw.d.ts} +35 -11
- package/debug.d.ts +1 -1
- package/debug.js +1 -1
- package/{driver-Bf-7Roq6.d.ts → driver-BIm1mL77.d.ts} +1 -1
- package/driver.d.ts +1 -1
- package/driver.js +1 -1
- package/{encoding-C3Q7pIC9.js → encoding-RUuES_e4.js} +2 -2
- package/{engine-BpdGfAND.js → engine-CXf5FCwi.js} +6 -6
- package/{engine-CQqt4rTB.d.ts → engine-DbDcopGM.d.ts} +2 -2
- package/{errors-BKJES3Zu.js → errors-D11zizZg.js} +1580 -1481
- package/{group-Bz2wh72R.js → group-BpzTo0k4.js} +84 -40
- package/http-fetch-DFwVIOQg.js +517 -0
- package/http.d.ts +39 -2
- package/http.js +190 -7
- package/index.d.ts +10 -660
- package/index.js +36 -49
- package/{inline-grants-Cx0Z9XAt.js → inline-grants-XbX9HL_w.js} +72 -304
- package/lean-DiS9rUcn.d.ts +741 -0
- package/lean-aCvN6y52.js +47 -0
- package/lean.d.ts +8 -0
- package/lean.js +15 -0
- package/{manifest-Dw-8Unmf.js → manifest-CCfpn0kC.js} +3 -4
- package/{manifest-DYAbIyQm.d.ts → manifest-mpivREn-.d.ts} +2 -2
- package/message-layer.d.ts +2 -2
- package/message-layer.js +2 -2
- package/notify.d.ts +1 -1
- package/notify.js +1 -1
- package/{objects-BRcpeXwZ.d.ts → objects-DYq_YvaQ.d.ts} +5 -1
- package/{objects-BC4fS0vq.js → objects-DmUc0lha.js} +1 -1
- package/{base64-Dtqaz_0A.js → operations-DnCcztkZ.js} +1632 -32
- package/package.json +36 -2
- package/{readers-L90Hoa97.d.ts → readers-8WCimMop.d.ts} +1 -1
- package/{readers-B5e5bBrY.js → readers-CsvlahLi.js} +2 -2
- package/{request-pump-binary-BjnyqXV5.js → request-pump-binary-oqjWFqZA.js} +5 -4
- package/{router-CBuMIl6i.d.ts → router-BQsO8aPV.d.ts} +2 -2
- package/{router-BCHEfs4x.js → router-BZ-aYeXA.js} +4 -4
- package/session.d.ts +2 -2
- package/session.js +55 -16
- package/setup.d.ts +1 -1
- package/setup.js +4 -4
- package/sqlite.d.ts +2 -2
- package/sqlite.js +5 -5
- package/stdlib/bytes.d.ts +6 -0
- package/stdlib/bytes.js +11 -0
- package/stdlib/date.d.ts +6 -0
- package/stdlib/date.js +11 -0
- package/stdlib/json.d.ts +6 -0
- package/stdlib/json.js +11 -0
- package/stdlib/list.d.ts +6 -0
- package/stdlib/list.js +10 -0
- package/stdlib/map.d.ts +6 -0
- package/stdlib/map.js +10 -0
- package/stdlib/text.d.ts +6 -0
- package/stdlib/text.js +10 -0
- package/stdlib/units.d.ts +6 -0
- package/stdlib/units.js +10 -0
- package/stdlib-registry-BwitI3Ox.d.ts +18 -0
- package/{store-DTQa0lyE.js → store-DHlo6fP5.js} +2 -2
- package/{store-capability-BxXgiRM7.js → store-capability-BrUUAwVS.js} +3 -4
- package/store.d.ts +2 -2
- package/store.js +2 -2
- package/{user-capability-5-IvCeZI.js → user-capability-BcRQyyEw.js} +4 -5
- package/wasm.d.ts +8 -6
- package/wasm.js +19 -15
- package/capabilities-Bi5Qm6Fr.js +0 -1320
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@northtalk/ts",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.6.0",
|
|
4
4
|
"description": "NorthTalk, a readable scripting language for embedding: the TS Core, its Session Host and Standard Capabilities.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -21,12 +21,46 @@
|
|
|
21
21
|
"engines": {
|
|
22
22
|
"node": ">=24.10"
|
|
23
23
|
},
|
|
24
|
-
"sideEffects":
|
|
24
|
+
"sideEffects": [
|
|
25
|
+
"./index.js"
|
|
26
|
+
],
|
|
25
27
|
"exports": {
|
|
26
28
|
".": {
|
|
27
29
|
"types": "./index.d.ts",
|
|
28
30
|
"default": "./index.js"
|
|
29
31
|
},
|
|
32
|
+
"./lean": {
|
|
33
|
+
"types": "./lean.d.ts",
|
|
34
|
+
"default": "./lean.js"
|
|
35
|
+
},
|
|
36
|
+
"./stdlib/bytes": {
|
|
37
|
+
"types": "./stdlib/bytes.d.ts",
|
|
38
|
+
"default": "./stdlib/bytes.js"
|
|
39
|
+
},
|
|
40
|
+
"./stdlib/date": {
|
|
41
|
+
"types": "./stdlib/date.d.ts",
|
|
42
|
+
"default": "./stdlib/date.js"
|
|
43
|
+
},
|
|
44
|
+
"./stdlib/json": {
|
|
45
|
+
"types": "./stdlib/json.d.ts",
|
|
46
|
+
"default": "./stdlib/json.js"
|
|
47
|
+
},
|
|
48
|
+
"./stdlib/list": {
|
|
49
|
+
"types": "./stdlib/list.d.ts",
|
|
50
|
+
"default": "./stdlib/list.js"
|
|
51
|
+
},
|
|
52
|
+
"./stdlib/map": {
|
|
53
|
+
"types": "./stdlib/map.d.ts",
|
|
54
|
+
"default": "./stdlib/map.js"
|
|
55
|
+
},
|
|
56
|
+
"./stdlib/text": {
|
|
57
|
+
"types": "./stdlib/text.d.ts",
|
|
58
|
+
"default": "./stdlib/text.js"
|
|
59
|
+
},
|
|
60
|
+
"./stdlib/units": {
|
|
61
|
+
"types": "./stdlib/units.d.ts",
|
|
62
|
+
"default": "./stdlib/units.js"
|
|
63
|
+
},
|
|
30
64
|
"./message-layer": {
|
|
31
65
|
"types": "./message-layer.d.ts",
|
|
32
66
|
"default": "./message-layer.js"
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { J as Value } from "./debug
|
|
2
|
+
import { J as Value } from "./debug--Of4MSLw.js";
|
|
3
3
|
//#region src/readers.d.ts
|
|
4
4
|
/** Finds the Host Object a reader names by its kind and id, or none. */
|
|
5
5
|
type ObjectResolver = (kind: string, id: string) => {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues, H as quantity, L as nothing, N as map, Q as text, T as instant, W as range,
|
|
3
|
-
import { t as fromBase64 } from "./base64-
|
|
2
|
+
import { A as listValues, Gt as parseInstant, H as quantity, Jt as assertScalarText, L as nothing, N as map, Q as text, T as instant, W as range, f as bytesOf, l as Value, m as civilDate, o as invalidValue, u as bool, v as dec, w as hiddenCodePoint } from "./errors-D11zizZg.js";
|
|
3
|
+
import { t as fromBase64 } from "./base64-CDO-a06R.js";
|
|
4
4
|
//#region src/readers.ts
|
|
5
5
|
var Reader = class {
|
|
6
6
|
source;
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { A as listValues, Bt as formatInstant, Gt as parseInstant, H as quantity, L as nothing, N as map, Q as text, R as num, S as functionValue, T as instant, V as patternValue, W as range, d as bytes, en as normalizeNFC, l as Value, m as civilDate, o as invalidValue, u as bool, v as dec, z as objectValue } from "./errors-D11zizZg.js";
|
|
3
|
+
import { at as patternSource, g as decodeUtf8 } from "./operations-DnCcztkZ.js";
|
|
4
|
+
import { _ as viewSource, t as checkSource } from "./checker-CUhn2XaM.js";
|
|
5
|
+
import { n as toBase64 } from "./base64-CDO-a06R.js";
|
|
6
|
+
import { a as fromJson, n as JsonObject, o as isJsonNumber, s as isJsonObject, t as JsonNumber } from "./readers-CsvlahLi.js";
|
|
6
7
|
//#region src/binary-value.ts
|
|
7
8
|
const coreErrors = /* @__PURE__ */ new WeakSet();
|
|
8
9
|
const max = 2n ** 64n;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { E as RestoreResult, I as TsRestoreOptions, J as Value, Mt as GrantContext, O as SaveOptions, P as TsGroup, Qt as Group, bt as Call } from "./debug
|
|
3
|
-
import { t as TsCore } from "./core-
|
|
2
|
+
import { E as RestoreResult, I as TsRestoreOptions, J as Value, Mt as GrantContext, O as SaveOptions, P as TsGroup, Qt as Group, bt as Call } from "./debug--Of4MSLw.js";
|
|
3
|
+
import { t as TsCore } from "./core-egHbNmve.js";
|
|
4
4
|
//#region src/notify/router.d.ts
|
|
5
5
|
/** A Store's four limits on its watches (Store Notifications, Limits). */
|
|
6
6
|
type WatchLimits = {
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues, N as map, Q as text, i as ScriptError, t as HostError, u as bool } from "./errors-
|
|
3
|
-
import {
|
|
4
|
-
import { t as encodeValue } from "./encoding-
|
|
2
|
+
import { A as listValues, N as map, Q as text, i as ScriptError, t as HostError, u as bool } from "./errors-D11zizZg.js";
|
|
3
|
+
import { Z as sizeOf, lt as compareText } from "./operations-DnCcztkZ.js";
|
|
4
|
+
import { t as encodeValue } from "./encoding-RUuES_e4.js";
|
|
5
5
|
import { n as sha256 } from "./sha256-9CGf5BS_.js";
|
|
6
|
-
import { n as hostScriptError } from "./store-capability-
|
|
6
|
+
import { n as hostScriptError } from "./store-capability-BrUUAwVS.js";
|
|
7
7
|
//#region src/notify/router.ts
|
|
8
8
|
/**
|
|
9
9
|
* Whether a commit changed a key's value: compared by the bytes of the Value
|
package/session.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { J as Value } from "./debug
|
|
3
|
-
import { C as Setup, _ as UserPrompt, a as ReplayOptions, c as parseTranscript, d as DebugAction, f as MOCK_ARGUMENTS, g as SourcePlacement, h as SessionHost, i as parseEnvelope, l as replayTranscript, m as SessionEnvironment, n as ObjectReference, o as Replayed, p as Mock, r as SessionObjects, s as TranscriptItem, t as Envelope, u as writeTranscript, v as Waiting } from "./objects-
|
|
2
|
+
import { J as Value } from "./debug--Of4MSLw.js";
|
|
3
|
+
import { C as Setup, _ as UserPrompt, a as ReplayOptions, c as parseTranscript, d as DebugAction, f as MOCK_ARGUMENTS, g as SourcePlacement, h as SessionHost, i as parseEnvelope, l as replayTranscript, m as SessionEnvironment, n as ObjectReference, o as Replayed, p as Mock, r as SessionObjects, s as TranscriptItem, t as Envelope, u as writeTranscript, v as Waiting } from "./objects-DYq_YvaQ.js";
|
|
4
4
|
//#region src/session/describe.d.ts
|
|
5
5
|
/** One declaration of the session source, as `:describe` reads it. */
|
|
6
6
|
type DeclaredName = {
|
package/session.js
CHANGED
|
@@ -1,18 +1,17 @@
|
|
|
1
1
|
|
|
2
|
-
import { A as listValues,
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
10
|
-
import { n as
|
|
11
|
-
import {
|
|
12
|
-
import { a as clockCapability, i as calendarCapability, o as consoleCapability, r as storeCapability } from "./store-capability-BxXgiRM7.js";
|
|
2
|
+
import { $t as lower, A as listValues, Bt as formatInstant, Gt as parseInstant, L as nothing, N as map, Q as text, R as num, W as range, Yt as characterBoundaries, an as builtins, i as ScriptError, l as Value, n as LoadError, t as HostError, u as bool } from "./errors-D11zizZg.js";
|
|
3
|
+
import { C as registeredStdlib, T as stdlibSource, a as recordLine, h as defaultLimits, i as restore, l as compileLibrary, o as traceValue } from "./group-BpzTo0k4.js";
|
|
4
|
+
import { o as stateOf, t as defineObjectKind } from "./objects-DmUc0lha.js";
|
|
5
|
+
import { H as waitUs, V as textForm$1, et as functionHead, lt as compareText, vt as defineValueCapability, wt as validStaticMessageName, x as integerValue, xt as shape } from "./operations-DnCcztkZ.js";
|
|
6
|
+
import { _ as viewSource, c as parseSource, f as declarationDocs, h as leadingDoc, m as functionDoc, s as parseEntry, t as checkSource } from "./checker-CUhn2XaM.js";
|
|
7
|
+
import { n as exportManifest, o as shapeData, t as canonicalJSON } from "./manifest-CCfpn0kC.js";
|
|
8
|
+
import { c as readDisplay, l as readOrderedJSON, r as decodeValue } from "./readers-CsvlahLi.js";
|
|
9
|
+
import { n as quoteJSON, t as encodeValue } from "./encoding-RUuES_e4.js";
|
|
10
|
+
import { n as localeCapability, t as userCapability } from "./user-capability-BcRQyyEw.js";
|
|
11
|
+
import { a as clockCapability, i as calendarCapability, o as consoleCapability, r as storeCapability } from "./store-capability-BrUUAwVS.js";
|
|
13
12
|
import { buildSetupGroup, capabilityOperations, immutableSetup, operationDeclarations, shapeOf } from "./setup.js";
|
|
14
|
-
import { a as encodeContents, i as decodeContents, o as sessionQuotas, t as StoreContentsError } from "./engine-
|
|
15
|
-
import { i as memoryStores } from "./store-
|
|
13
|
+
import { a as encodeContents, i as decodeContents, o as sessionQuotas, t as StoreContentsError } from "./engine-CXf5FCwi.js";
|
|
14
|
+
import { i as memoryStores } from "./store-DHlo6fP5.js";
|
|
16
15
|
//#region src/session/objects.ts
|
|
17
16
|
const externalCollection = () => {
|
|
18
17
|
throw new Error("external collection callback in replay");
|
|
@@ -1122,6 +1121,46 @@ const builtinDocs = {
|
|
|
1122
1121
|
"call": "random(n)",
|
|
1123
1122
|
"gives": "A whole number from 1 to `n`, the next from the Script Group's generator"
|
|
1124
1123
|
},
|
|
1124
|
+
"bitAnd": {
|
|
1125
|
+
"call": "bitAnd(a, b)",
|
|
1126
|
+
"gives": "The bits set in both `a` and `b`"
|
|
1127
|
+
},
|
|
1128
|
+
"bitOr": {
|
|
1129
|
+
"call": "bitOr(a, b)",
|
|
1130
|
+
"gives": "The bits set in `a`, `b` or both"
|
|
1131
|
+
},
|
|
1132
|
+
"bitXor": {
|
|
1133
|
+
"call": "bitXor(a, b)",
|
|
1134
|
+
"gives": "The bits set in exactly one of `a` and `b`"
|
|
1135
|
+
},
|
|
1136
|
+
"bitNot": {
|
|
1137
|
+
"call": "bitNot(x, width)",
|
|
1138
|
+
"gives": "`x` with its low `width` bits inverted"
|
|
1139
|
+
},
|
|
1140
|
+
"bitShiftLeft": {
|
|
1141
|
+
"call": "bitShiftLeft(x, n, width = nothing)",
|
|
1142
|
+
"gives": "`x` shifted left `n` bits, kept to its low `width` bits when `width` is given"
|
|
1143
|
+
},
|
|
1144
|
+
"bitShiftRight": {
|
|
1145
|
+
"call": "bitShiftRight(x, n)",
|
|
1146
|
+
"gives": "`x` shifted right `n` bits, filling with zeros"
|
|
1147
|
+
},
|
|
1148
|
+
"bitRotateLeft": {
|
|
1149
|
+
"call": "bitRotateLeft(x, n, width)",
|
|
1150
|
+
"gives": "The low `width` bits of `x` rotated left `n` places"
|
|
1151
|
+
},
|
|
1152
|
+
"bitRotateRight": {
|
|
1153
|
+
"call": "bitRotateRight(x, n, width)",
|
|
1154
|
+
"gives": "The low `width` bits of `x` rotated right `n` places"
|
|
1155
|
+
},
|
|
1156
|
+
"bitCount": {
|
|
1157
|
+
"call": "bitCount(x)",
|
|
1158
|
+
"gives": "How many bits of `x` are set"
|
|
1159
|
+
},
|
|
1160
|
+
"bitTest": {
|
|
1161
|
+
"call": "bitTest(x, i)",
|
|
1162
|
+
"gives": "Whether bit `i` of `x` is set, bit 0 being the lowest"
|
|
1163
|
+
},
|
|
1125
1164
|
"fromFloat64": {
|
|
1126
1165
|
"call": "fromFloat64(b, order = \"big\")",
|
|
1127
1166
|
"gives": "The number an IEEE 754 binary64 float in the 8 Bytes `b` holds, as its shortest round-trip decimal"
|
|
@@ -1312,7 +1351,7 @@ const declaredIn = (source, origin, includePrivate = false) => {
|
|
|
1312
1351
|
});
|
|
1313
1352
|
return out;
|
|
1314
1353
|
};
|
|
1315
|
-
const librarySource$1 = (names, library) => names.libraries.get(library) ??
|
|
1354
|
+
const librarySource$1 = (names, library) => names.libraries.get(library) ?? stdlibSource(library);
|
|
1316
1355
|
/** A public Library export, from the Library's source, imported or not. */
|
|
1317
1356
|
const libraryDeclared = (names, library, name) => {
|
|
1318
1357
|
const source = librarySource$1(names, library);
|
|
@@ -1451,7 +1490,7 @@ const apropos = (names, rest) => {
|
|
|
1451
1490
|
import: null,
|
|
1452
1491
|
docs: builtinDeclared(b.name)
|
|
1453
1492
|
});
|
|
1454
|
-
const libraries = [...
|
|
1493
|
+
const libraries = [...[...registeredStdlib].sort(byCodePoint), ...names.libraries.keys()];
|
|
1455
1494
|
for (const library of libraries) {
|
|
1456
1495
|
const source = librarySource$1(names, library);
|
|
1457
1496
|
for (const [name, docs] of declaredIn(source, library)) {
|
|
@@ -1731,7 +1770,7 @@ var Stubs = class Stubs {
|
|
|
1731
1770
|
if (!stub.error.entries().length) throw new Error(`The Stub for ${operation} fails`);
|
|
1732
1771
|
throw hostFailure(stub.error);
|
|
1733
1772
|
}
|
|
1734
|
-
return stub.value ?? nothing;
|
|
1773
|
+
return stub.value ?? stub.later?.() ?? nothing;
|
|
1735
1774
|
}
|
|
1736
1775
|
/** A suspending call takes a Stub for its charge, and for an error that fails it at once. */
|
|
1737
1776
|
start(operation, call, recorded) {
|
package/setup.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { A as shapeOf, C as Setup, D as capabilityOperations, E as buildSetupGroup, M as tsCore, O as immutableSetup, S as OperationSpec, T as ShapeSpec, b as MissingCapabilityError, j as shapeSpec, k as operationDeclarations, w as SetupGroup, x as ObjectRefSpec, y as CaseCore } from "./objects-
|
|
2
|
+
import { A as shapeOf, C as Setup, D as capabilityOperations, E as buildSetupGroup, M as tsCore, O as immutableSetup, S as OperationSpec, T as ShapeSpec, b as MissingCapabilityError, j as shapeSpec, k as operationDeclarations, w as SetupGroup, x as ObjectRefSpec, y as CaseCore } from "./objects-DYq_YvaQ.js";
|
|
3
3
|
export { CaseCore, MissingCapabilityError, ObjectRefSpec, OperationSpec, Setup, SetupGroup, ShapeSpec, buildSetupGroup, capabilityOperations, immutableSetup, operationDeclarations, shapeOf, shapeSpec, tsCore };
|
package/setup.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
|
|
2
|
-
import { n as LoadError } from "./errors-
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
2
|
+
import { n as LoadError } from "./errors-D11zizZg.js";
|
|
3
|
+
import { i as restore, l as compileLibrary, r as newGroup, u as replacementLibraries } from "./group-BpzTo0k4.js";
|
|
4
|
+
import { t as defineObjectKind } from "./objects-DmUc0lha.js";
|
|
5
|
+
import { xt as shape } from "./operations-DnCcztkZ.js";
|
|
6
6
|
//#region src/setup.ts
|
|
7
7
|
const operationDeclarations = (capabilities) => {
|
|
8
8
|
const declarations = Object.create(null);
|
package/sqlite.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/// <reference lib="esnext.disposable" />
|
|
2
|
-
import { Wt as SegmentCoordinator, bt as Call,
|
|
3
|
-
import { a as Stores, r as StoreQuotas } from "./engine-
|
|
2
|
+
import { Wt as SegmentCoordinator, bt as Call, cn as StoreImpl, dn as SqlParams, fn as SqlRows, hn as SqliteImpl, mn as SqliteBinding } from "./debug--Of4MSLw.js";
|
|
3
|
+
import { a as Stores, r as StoreQuotas } from "./engine-DbDcopGM.js";
|
|
4
4
|
//#region src/sqlite/database.d.ts
|
|
5
5
|
/** The table that keeps a database's Stores, which no `sqlite` Grant may use. */
|
|
6
6
|
export declare const STORE_TABLE = "store";
|
package/sqlite.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
|
|
2
|
-
import { Q as text, t as HostError, v as dec } from "./errors-
|
|
3
|
-
import { r as decodeValue } from "./readers-
|
|
4
|
-
import { t as encodeValue } from "./encoding-
|
|
5
|
-
import { n as hostScriptError } from "./store-capability-
|
|
6
|
-
import { r as Stores, t as StoreContentsError } from "./engine-
|
|
2
|
+
import { Q as text, t as HostError, v as dec } from "./errors-D11zizZg.js";
|
|
3
|
+
import { r as decodeValue } from "./readers-CsvlahLi.js";
|
|
4
|
+
import { t as encodeValue } from "./encoding-RUuES_e4.js";
|
|
5
|
+
import { n as hostScriptError } from "./store-capability-BrUUAwVS.js";
|
|
6
|
+
import { r as Stores, t as StoreContentsError } from "./engine-CXf5FCwi.js";
|
|
7
7
|
import { DatabaseSync } from "node:sqlite";
|
|
8
8
|
//#region src/sqlite/scan.ts
|
|
9
9
|
const isWordChar = (c) => /[\w$]/.test(c) || c.charCodeAt(0) > 127;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/bytes.d.ts
|
|
4
|
+
/** The `bytes` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const bytes: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/bytes.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import { text } from "./text.js";
|
|
3
|
+
//#region src/generated/stdlib/bytes.ts
|
|
4
|
+
/** The `bytes` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
const bytes = {
|
|
6
|
+
name: "bytes",
|
|
7
|
+
source: "-- The `bytes` Library. Chapter 7 says what each export does, and this source\n-- does exactly that (ADR 0021). It is normative Spec text: its private\n-- helpers, its Fuel and the order of its calls are fixed here.\n\nuse join from text\n\n-- Hexadecimal ----------------------------------------------------------------\n\n--| The Bytes `b` as lowercase hexadecimal text, two digits per byte\nfunction toHex b\n needBytes(b)\n put [] into parts\n repeat for each n in the bytes of b\n put character (n div 16 + 1) of hexDigits & character (n mod 16 + 1) of hexDigits after parts\n end repeat\n return join(parts)\nend toHex\n\n--| The Bytes the hexadecimal text `t` writes\nfunction fromHex t\n needText(t)\n put the characters of t into cs\n put the length of cs into n\n if n mod 2 = 1 then badEncoding(t, \"hex\", n)\n put [] into nums\n put 1 into i\n repeat while i <= n\n put hexValue(item i of cs) into hi\n if hi < 0 then badEncoding(t, \"hex\", i)\n put hexValue(item (i + 1) of cs) into lo\n if lo < 0 then badEncoding(t, \"hex\", i + 1)\n put hi * 16 + lo after nums\n add 2 to i\n end repeat\n return bytesFrom(nums)\nend fromHex\n\nprivate constant hexDigits = \"0123456789abcdef\"\n\n-- The value of one hexadecimal digit, in either case, or -1.\nprivate function hexValue c\n return offset(lower(c), hexDigits) - 1\nend hexValue\n\n-- Base64 ---------------------------------------------------------------------\n\nprivate constant base64Alphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"\nprivate constant base64UrlAlphabet = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_\"\n\n--| The Bytes `b` in the base64 alphabet of RFC 4648, padded\nfunction toBase64 b\n needBytes(b)\n return encode64(b, base64Alphabet, true)\nend toBase64\n\n--| The Bytes the padded base64 text `t` writes\nfunction fromBase64 t\n needText(t)\n return decode64(t, base64Alphabet, true, \"base64\")\nend fromBase64\n\n--| The Bytes `b` in the base64url alphabet of RFC 4648, unpadded\nfunction toBase64Url b\n needBytes(b)\n return encode64(b, base64UrlAlphabet, false)\nend toBase64Url\n\n--| The Bytes the unpadded base64url text `t` writes\nfunction fromBase64Url t\n needText(t)\n return decode64(t, base64UrlAlphabet, false, \"base64url\")\nend fromBase64Url\n\nprivate function encode64 b, alphabet, padded\n put the bytes of b into ns\n put the length of ns into n\n put [] into parts\n put 1 into i\n repeat while i + 2 <= n\n put item i of ns * 65536 + item (i + 1) of ns * 256 + item (i + 2) of ns into v\n put sextets(v, 4, alphabet) after parts\n add 3 to i\n end repeat\n if n - i = 0 then\n put sextets(item i of ns * 16, 2, alphabet) after parts\n if padded then put \"==\" after parts\n else if n - i = 1 then\n put sextets((item i of ns * 256 + item (i + 1) of ns) * 4, 3, alphabet) after parts\n if padded then put \"=\" after parts\n end if\n return join(parts)\nend encode64\n\n-- The `count` base64 digits of `v`, most significant first.\nprivate function sextets v, count, alphabet\n put [] into digits\n repeat for each k in 1..count\n put (v div 64 ^ (count - k)) mod 64 into d\n put character (d + 1) of alphabet after digits\n end repeat\n return join(digits)\nend sextets\n\n-- Reads only the canonical form: the right alphabet, length and padding, and\n-- zero in the unused trailing bits.\nprivate function decode64 t, alphabet, padded, fmt\n put the characters of t into cs\n put the length of cs into n\n if padded and n mod 4 <> 0 then badEncoding(t, fmt, n)\n if not padded and n mod 4 = 1 then badEncoding(t, fmt, n)\n put n into body\n if padded then\n repeat while body > 0 and n - body < 2 and item body of cs = \"=\"\n subtract 1 from body\n end repeat\n end if\n put [] into values\n repeat for each i in 1..body\n put offset(item i of cs, alphabet) - 1 into v\n if v < 0 or item i of cs = \"=\" then badEncoding(t, fmt, i)\n put v after values\n end repeat\n put body mod 4 into rest\n if rest = 1 then badEncoding(t, fmt, body)\n put [] into nums\n put 1 into i\n repeat while i + 3 <= body\n put ((item i of values * 64 + item (i + 1) of values) * 64 + item (i + 2) of values) * 64 + item (i + 3) of values into v\n put v div 65536 after nums\n put (v div 256) mod 256 after nums\n put v mod 256 after nums\n add 4 to i\n end repeat\n if rest = 2 then\n put item i of values * 64 + item (i + 1) of values into v\n if v mod 16 <> 0 then badEncoding(t, fmt, body)\n put v div 16 after nums\n else if rest = 3 then\n put (item i of values * 64 + item (i + 1) of values) * 64 + item (i + 2) of values into v\n if v mod 4 <> 0 then badEncoding(t, fmt, body)\n put v div 1024 after nums\n put (v div 4) mod 256 after nums\n end if\n return bytesFrom(nums)\nend decode64\n\n-- Text encodings -------------------------------------------------------------\n\nprivate constant encodings = [\"utf-8\", \"utf-16le\", \"utf-16be\", \"latin-1\", \"windows-1252\"]\n\n-- The code points of bytes 0x80 to 0x9F in the WHATWG windows-1252 index.\nprivate constant windows1252 = [0x20AC, 0x81, 0x201A, 0x192, 0x201E, 0x2026, 0x2020, 0x2021, 0x2C6, 0x2030, 0x160, 0x2039, 0x152, 0x8D, 0x17D, 0x8F, 0x90, 0x2018, 0x2019, 0x201C, 0x201D, 0x2022, 0x2013, 0x2014, 0x2DC, 0x2122, 0x161, 0x203A, 0x153, 0x9D, 0x17E, 0x178]\n\n--| The text the Bytes `b` hold in `encoding`, normalised to NFC\nfunction decodeText b, encoding = \"utf-8\", mode = \"strict\"\n needBytes(b)\n needText(encoding)\n needText(mode)\n if encoding is not in encodings then outOfDomain(\"decodeText\", encoding)\n if mode <> \"strict\" and mode <> \"replace\" then outOfDomain(\"decodeText\", mode)\n put mode = \"replace\" into replacing\n put the bytes of b into ns\n if encoding = \"utf-8\" then\n put fromUtf8(ns, replacing, b) into cps\n else if encoding = \"utf-16le\" then\n put fromUtf16(ns, true, replacing, b, encoding) into cps\n else if encoding = \"utf-16be\" then\n put fromUtf16(ns, false, replacing, b, encoding) into cps\n else if encoding = \"latin-1\" then\n put ns into cps\n else\n put [] into cps\n repeat for each n in ns\n if n >= 0x80 and n <= 0x9F then\n put item (n - 0x7F) of windows1252 after cps\n else\n put n after cps\n end if\n end repeat\n end if\n if encoding begins with \"utf\" and cps is not empty and item 1 of cps = 0xFEFF then\n put items 2..-1 of cps into cps\n end if\n put [] into parts\n repeat for each cp in cps\n put fromCodePoint(cp) after parts\n end repeat\n return join(parts)\nend decodeText\n\n--| The Bytes of the text `t` in `encoding`\nfunction encodeText t, encoding = \"utf-8\"\n needText(t)\n needText(encoding)\n if encoding is not in encodings then outOfDomain(\"encodeText\", encoding)\n put [] into nums\n repeat for each c in the code points of t\n put codePoint(c) into cp\n if encoding = \"utf-8\" then\n put ...toUtf8(cp) after nums\n else if encoding = \"utf-16le\" or encoding = \"utf-16be\" then\n repeat for each unit in toUtf16(cp)\n if encoding = \"utf-16le\" then\n put unit mod 256 after nums\n put unit div 256 after nums\n else\n put unit div 256 after nums\n put unit mod 256 after nums\n end if\n end repeat\n else if cp <= 0x7F or (cp >= 0xA0 and cp <= 0xFF) then\n put cp after nums\n else if encoding = \"latin-1\" and cp <= 0xFF then\n put cp after nums\n else if encoding = \"windows-1252\" and cp is in windows1252 then\n put 0x7F + indexIn(windows1252, cp) after nums\n else\n unencodable(t, encoding)\n end if\n end repeat\n return bytesFrom(nums)\nend encodeText\n\n-- UTF-8, with U+FFFD for each maximal ill-formed subsequence when replacing.\nprivate function fromUtf8 ns, replacing, b\n put [] into cps\n put the length of ns into n\n put 1 into i\n repeat while i <= n\n put item i of ns into b1\n if b1 < 0x80 then\n put b1 after cps\n add 1 to i\n next repeat\n end if\n -- [continuation bytes, and the range of the first one]\n put nothing into need\n if b1 >= 0xC2 and b1 <= 0xDF then\n put [1, 0x80, 0xBF] into need\n else if b1 = 0xE0 then\n put [2, 0xA0, 0xBF] into need\n else if (b1 >= 0xE1 and b1 <= 0xEC) or b1 = 0xEE or b1 = 0xEF then\n put [2, 0x80, 0xBF] into need\n else if b1 = 0xED then\n put [2, 0x80, 0x9F] into need\n else if b1 = 0xF0 then\n put [3, 0x90, 0xBF] into need\n else if b1 >= 0xF1 and b1 <= 0xF3 then\n put [3, 0x80, 0xBF] into need\n else if b1 = 0xF4 then\n put [3, 0x80, 0x8F] into need\n end if\n if need is nothing then\n if not replacing then badText(b, \"utf-8\")\n put 0xFFFD after cps\n add 1 to i\n next repeat\n end if\n put item 1 of need into count\n put b1 mod 2 ^ (6 - count) into cp\n put 1 into got\n repeat while got <= count\n if i + got > n then exit repeat\n put item (i + got) of ns into c\n put 0x80 into lo\n put 0xBF into hi\n if got = 1 then\n put item 2 of need into lo\n put item 3 of need into hi\n end if\n if c < lo or c > hi then exit repeat\n put cp * 64 + c mod 64 into cp\n add 1 to got\n end repeat\n if got <= count then\n if not replacing then badText(b, \"utf-8\")\n put 0xFFFD after cps\n add got to i\n else\n put cp after cps\n add count + 1 to i\n end if\n end repeat\n return cps\nend fromUtf8\n\n-- UTF-16, with U+FFFD for an unpaired surrogate or an odd final byte when\n-- replacing.\nprivate function fromUtf16 ns, little, replacing, b, encoding\n put [] into units\n put the length of ns into n\n put 1 into i\n repeat while i + 1 <= n\n if little then\n put item (i + 1) of ns * 256 + item i of ns after units\n else\n put item i of ns * 256 + item (i + 1) of ns after units\n end if\n add 2 to i\n end repeat\n put [] into cps\n put the length of units into count\n put 1 into k\n repeat while k <= count\n put item k of units into u\n if u < 0xD800 or u > 0xDFFF then\n put u after cps\n add 1 to k\n else if u <= 0xDBFF and k < count and item (k + 1) of units >= 0xDC00 and item (k + 1) of units <= 0xDFFF then\n put 0x10000 + (u - 0xD800) * 1024 + (item (k + 1) of units - 0xDC00) after cps\n add 2 to k\n else\n if not replacing then badText(b, encoding)\n put 0xFFFD after cps\n add 1 to k\n end if\n end repeat\n if n mod 2 = 1 then\n if not replacing then badText(b, encoding)\n put 0xFFFD after cps\n end if\n return cps\nend fromUtf16\n\nprivate function toUtf8 cp\n if cp < 0x80 then return [cp]\n if cp < 0x800 then return [0xC0 + cp div 64, 0x80 + cp mod 64]\n if cp < 0x10000 then return [0xE0 + cp div 4096, 0x80 + (cp div 64) mod 64, 0x80 + cp mod 64]\n return [0xF0 + cp div 262144, 0x80 + (cp div 4096) mod 64, 0x80 + (cp div 64) mod 64, 0x80 + cp mod 64]\nend toUtf8\n\nprivate function toUtf16 cp\n if cp < 0x10000 then return [cp]\n put cp - 0x10000 into v\n return [0xD800 + v div 1024, 0xDC00 + v mod 1024]\nend toUtf16\n\nprivate function indexIn xs, value\n put 1 into i\n repeat for each x in xs\n if x = value then return i\n add 1 to i\n end repeat\n return 0\nend indexIn\n\n-- Bytes from a list of numbers, halving so each byte is copied about log n times.\nprivate function bytesFrom nums\n put the length of nums into n\n if n = 0 then return <<>>\n if n = 1 then return << item 1 of nums >>\n put n div 2 into half\n return << bytesFrom(items 1..half of nums), bytesFrom(items (half + 1)..n of nums) >>\nend bytesFrom\n\nprivate function needBytes v\n if v is not a bytes then wrongKind(\"bytes\", v)\nend needBytes\n\nprivate function badEncoding t, fmt, at\n throw {code: \"can't convert\", value: t, to: \"bytes\", format: fmt, offset: at}\nend badEncoding\n\nprivate function badText b, encoding\n throw {code: \"can't convert\", value: b, to: \"text\", format: encoding}\nend badText\n\nprivate function unencodable t, encoding\n throw {code: \"can't convert\", value: t, to: \"bytes\", format: encoding}\nend unencodable\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n",
|
|
8
|
+
requires: [text]
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { bytes };
|
package/stdlib/date.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/date.d.ts
|
|
4
|
+
/** The `date` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const date: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/date.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import { text } from "./text.js";
|
|
3
|
+
//#region src/generated/stdlib/date.ts
|
|
4
|
+
/** The `date` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
const date = {
|
|
6
|
+
name: "date",
|
|
7
|
+
source: "-- The `date` Library. Chapter 7 says what each export does, and this source\n-- does exactly that (ADR 0021). It is normative Spec text: its private\n-- helpers, its Fuel and the order of its calls are fixed here.\n\nuse join, padLeft from text\n\n--| The Unix epoch, as an Instant\nconstant epoch = \"1970-01-01T00:00:00Z\" as instant\n\n-- Building -------------------------------------------------------------------\n\n--| The date-only Civil Date `y`-`m`-`d`\nfunction makeDate y, m, d\n needNumber(y)\n needNumber(m)\n needNumber(d)\n checkDate(y, m, d)\n return (dateText(y, m, d)) as civil date\nend makeDate\n\n--| The date-time `y`-`m`-`d` at `h`:`mi`:`s` and `us` microseconds\nfunction makeDateTime y, m, d, h, mi, s = 0, us = 0\n needNumber(y)\n needNumber(m)\n needNumber(d)\n needNumber(h)\n needNumber(mi)\n needNumber(s)\n needNumber(us)\n checkDate(y, m, d)\n checkTime(h, mi, s, us)\n put dateText(y, m, d) & \"T\" & two(h) & \":\" & two(mi) & \":\" & two(s) into written\n if us > 0 then put \".\" & padLeft(truncate(us) as text, 6, \"0\") after written\n return written as civil date\nend makeDateTime\n\n--| The date-time on the date of `d` at `h`:`mi`:`s` and `us` microseconds\nfunction atTime d, h, mi, s = 0, us = 0\n needCivil(d)\n needNumber(h)\n needNumber(mi)\n needNumber(s)\n needNumber(us)\n if hasTime(d) then outOfDomain(\"atTime\", d)\n return makeDateTime(year(d), month(d), day(d), h, mi, s, us)\nend atTime\n\n--| The date of the Civil Date `d`, without its time of day\nfunction dateOnly d\n needCivil(d)\n if not hasTime(d) then return d\n return makeDate(year(d), month(d), day(d))\nend dateOnly\n\n--| The number of days in the month of the Civil Date `d`\nfunction daysInMonth d\n needCivil(d)\n return monthLength(year(d), month(d))\nend daysInMonth\n\n--| Whether the integer `y` is a leap year in the proleptic Gregorian calendar\nfunction isLeapYear y\n needInteger(y)\n return y mod 4 = 0 and (y mod 100 <> 0 or y mod 400 = 0)\nend isLeapYear\n\nprivate function checkDate y, m, d\n checkField(\"year\", y, 1, 9999)\n checkField(\"month\", m, 1, 12)\n checkField(\"day\", d, 1, monthLength(y, m))\nend checkDate\n\nprivate function checkTime h, mi, s, us\n checkField(\"hour\", h, 0, 23)\n checkField(\"minute\", mi, 0, 59)\n checkField(\"second\", s, 0, 59)\n checkField(\"microsecond\", us, 0, 999999)\nend checkTime\n\n-- A field that isn't an integer in its range raises `out of range`, and is\n-- never normalised (ADR 0023).\nprivate function checkField name, v, low, high\n if v is not an integer or v < low or v > high then outOfRange(name, v)\nend checkField\n\nprivate function monthLength y, m\n if m = 2 and isLeapYear(y) then return 29\n if m = 2 then return 28\n if m is in [4, 6, 9, 11] then return 30\n return 31\nend monthLength\n\nprivate function dateText y, m, d\n return padLeft(truncate(y) as text, 4, \"0\") & \"-\" & two(m) & \"-\" & two(d)\nend dateText\n\n-- `truncate` drops a zero fraction, so 9.0 writes as \"09\".\nprivate function two n\n return padLeft(truncate(n) as text, 2, \"0\")\nend two\n\n-- Templates ------------------------------------------------------------------\n\n--| The Civil Date `d` written by `template`\nfunction formatDate d, template\n needCivil(d)\n needText(template)\n put readTemplate(template, \"formatDate\") into pieces\n if hasTimeToken(pieces) and not hasTime(d) then outOfDomain(\"formatDate\", d)\n put [] into parts\n repeat for each piece in pieces\n if piece is a text then\n put piece after parts\n else if the token of piece = \"fraction\" then\n put characters 1..(the width of piece) of padLeft(microsecond(d) as text, 6, \"0\") after parts\n else\n put fieldOf(d, the token of piece) as text into digits\n if the width of piece is not nothing then put padLeft(digits, the width of piece, \"0\") into digits\n put digits after parts\n end if\n end repeat\n return join(parts)\nend formatDate\n\n--| The Civil Date the text `t` writes by `template`\nfunction parseDate t, template\n needText(t)\n needText(template)\n put readTemplate(template, \"parseDate\") into pieces\n put the characters of t into cs\n put the length of cs into n\n put 1 into i\n put {} into found\n put 0 into weekdayAt\n repeat for each piece in pieces\n if piece is a text then\n repeat for each c in the characters of piece\n if i > n then noMatch(t, template, i)\n if item i of cs <> c then noMatch(t, template, i)\n add 1 to i\n end repeat\n else\n put i into start\n put \"\" into digits\n repeat while i <= n and item i of cs matches <digit>\n if the width of piece is not nothing and the length of digits = the width of piece then exit repeat\n put item i of cs after digits\n add 1 to i\n end repeat\n if digits is empty then noMatch(t, template, i)\n if the width of piece is not nothing and the length of digits < the width of piece then noMatch(t, template, i)\n if the token of piece = \"weekday\" then put start into weekdayAt\n if the token of piece = \"fraction\" then\n put (digits as number) * 10 ^ (6 - the width of piece) into the microsecond of found\n else if the length of digits > 34 then\n outOfRange(the token of piece, digits)\n else\n put digits as number into the (the token of piece) of found\n end if\n end if\n end repeat\n if i <= n then noMatch(t, template, i)\n if the hour of found is nothing then\n put makeDate(the year of found, the month of found, the day of found) into result\n else\n put 0 into s\n if the second of found is not nothing then put the second of found into s\n put 0 into us\n if the microsecond of found is not nothing then put the microsecond of found into us\n put makeDateTime(the year of found, the month of found, the day of found, the hour of found, the minute of found, s, us) into result\n end if\n if weekdayAt > 0 and weekday(result) <> the weekday of found then noMatch(t, template, weekdayAt)\n return result\nend parseDate\n\nprivate constant timeTokens = [\"hour\", \"minute\", \"second\", \"fraction\"]\n\n-- A template as a list of literal texts and {token, width} maps, or `out of\n-- domain` with the template if it isn't valid.\nprivate function readTemplate template, fn\n put the characters of template into cs\n put the length of cs into n\n put [] into pieces\n put \"\" into literal\n put [] into seen\n put 1 into i\n repeat while i <= n\n put item i of cs into c\n if c = \"$\" and i < n and item (i + 1) of cs = \"$\" then\n put \"$\" after literal\n add 2 to i\n else if c = \"$\" and i < n and item (i + 1) of cs = \"{\" then\n put i + 2 into j\n repeat while j <= n and item j of cs <> \"}\"\n add 1 to j\n end repeat\n if j > n then outOfDomain(fn, template)\n put splitAt(characters (i + 2)..(j - 1) of template, \":\") into halves\n put item 1 of halves into token\n put nothing into width\n if the length of halves > 2 then outOfDomain(fn, template)\n if the length of halves = 2 then\n if not (item 2 of halves matches <digits>) then outOfDomain(fn, template)\n put item 2 of halves as number into width\n if width < 1 then outOfDomain(fn, template)\n end if\n if token is not in [\"year\", \"month\", \"day\", \"hour\", \"minute\", \"second\", \"weekday\", \"fraction\"] then outOfDomain(fn, template)\n if token is in seen then outOfDomain(fn, template)\n if token = \"fraction\" and (width is nothing or width > 6) then outOfDomain(fn, template)\n put token after seen\n if literal is not empty then put literal after pieces\n put \"\" into literal\n put {token: token, width: width} after pieces\n put j + 1 into i\n else\n put c after literal\n add 1 to i\n end if\n end repeat\n if literal is not empty then put literal after pieces\n repeat for each token in [\"year\", \"month\", \"day\"]\n if token is not in seen then outOfDomain(fn, template)\n end repeat\n repeat for each token in timeTokens\n if token is in seen and (\"hour\" is not in seen or \"minute\" is not in seen) then outOfDomain(fn, template)\n end repeat\n return pieces\nend readTemplate\n\nprivate function hasTimeToken pieces\n repeat for each piece in pieces\n if piece is a map and the token of piece is in timeTokens then return true\n end repeat\n return false\nend hasTimeToken\n\nprivate function fieldOf d, token\n if token = \"year\" then return year(d)\n if token = \"month\" then return month(d)\n if token = \"day\" then return day(d)\n if token = \"hour\" then return hour(d)\n if token = \"minute\" then return minute(d)\n if token = \"second\" then return second(d)\n return weekday(d)\nend fieldOf\n\nprivate function splitAt t, separator\n put [] into pieces\n put \"\" into piece\n repeat for each c in the characters of t\n if c = separator then\n put piece after pieces\n put \"\" into piece\n else\n put c after piece\n end if\n end repeat\n put piece after pieces\n return pieces\nend splitAt\n\nprivate function noMatch t, template, at\n throw {code: \"can't convert\", value: t, to: \"civil date\", format: template, offset: at}\nend noMatch\n\n-- Durations ------------------------------------------------------------------\n\n--| The exact duration `d` as the map `{days, hours, minutes, seconds}`\nfunction splitDuration d\n if d is not a quantity then wrongKind(\"quantity\", d)\n if not (d can be s) then\n -- The Unit's display form is singular, so it is read from a Quantity of magnitude 1.\n put d as text into written\n put characters 1..(offset(\" \", written) - 1) of written as number into k\n if k <> 0 then put (d / abs(k)) as text into written\n put characters (offset(\" \", written) + 1)..-1 of written into unit\n throw {code: \"incompatible units\", left: unit, right: \"s\"}\n end if\n put d / 1 s into total\n put abs(total) into rest\n put rest div 86400 into days\n put rest - days * 86400 into rest\n put rest div 3600 into hours\n put rest - hours * 3600 into rest\n put rest div 60 into minutes\n put rest - minutes * 60 into seconds\n if total < 0 then return {days: -days, hours: -hours, minutes: -minutes, seconds: -seconds}\n return {days: days, hours: hours, minutes: minutes, seconds: seconds}\nend splitDuration\n\n--| The number of whole calendar months from the Civil Date `a` to `b`, negative if `b` is earlier\nfunction monthsBetween a, b\n needCivil(a)\n needCivil(b)\n if hasTime(a) <> hasTime(b) then\n throw {code: \"can't compare\", left: a, right: b}\n end if\n if b < a then return -monthsBetween(b, a)\n put (year(b) - year(a)) * 12 + month(b) - month(a) into n\n if a + n * 1 month > b then subtract 1 from n\n return n\nend monthsBetween\n\n-- Helpers --------------------------------------------------------------------\n\nprivate function needCivil v\n if v is not a civil date then wrongKind(\"civil date\", v)\nend needCivil\n\nprivate function outOfRange name, v\n throw {code: \"out of range\", field: name, value: v}\nend outOfRange\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n",
|
|
8
|
+
requires: [text]
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { date };
|
package/stdlib/json.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/json.d.ts
|
|
4
|
+
/** The `json` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const json: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/json.js
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
|
|
2
|
+
import { text } from "./text.js";
|
|
3
|
+
//#region src/generated/stdlib/json.ts
|
|
4
|
+
/** The `json` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
const json = {
|
|
6
|
+
name: "json",
|
|
7
|
+
source: "-- The `json` Library: the JSON mapping of chapter 7, which each Core's\n-- Host-side codec follows too. This source is normative Spec text (ADR 0021).\n--\n-- Decoding reads the text's code points rather than its Characters, since a\n-- combining mark after a quote would join it into one Character, and reports\n-- positions in Characters, as chapter 7 says.\n\nuse join, repeated from text\n\n-- Decoding -------------------------------------------------------------------\n\n--| The value the JSON text `t` holds\nfunction decodeJson t\n needText(t)\n put the code points of t into cps\n let [v, j] be readValue(cps, skipSpace(cps, 1), t)\n put skipSpace(cps, j) into j\n if j <= the length of cps then invalid(t, j)\n return v\nend decodeJson\n\n-- Each reader takes the code point to start at, and gives [value, where it ended + 1].\nprivate function readValue cps, i, t\n if i > the length of cps then invalid(t, i)\n put item i of cps into c\n if c = \"{\" then return readObject(cps, i, t)\n if c = \"[\" then return readArray(cps, i, t)\n if c = quote then return readString(cps, i, t)\n if c = \"-\" or isDigit(c) then return readNumber(cps, i, t)\n if c = \"t\" then return [true, literalEnd(cps, i, \"true\", t)]\n if c = \"f\" then return [false, literalEnd(cps, i, \"false\", t)]\n if c = \"n\" then return [nothing, literalEnd(cps, i, \"null\", t)]\n invalid(t, i)\nend readValue\n\nprivate function readArray cps, i, t\n put the length of cps into n\n put [] into xs\n put skipSpace(cps, i + 1) into j\n if j <= n and item j of cps = \"]\" then return [xs, j + 1]\n repeat forever\n let [v, k] be readValue(cps, j, t)\n put v after xs\n put skipSpace(cps, k) into j\n if j > n then invalid(t, j)\n if item j of cps = \"]\" then return [xs, j + 1]\n if item j of cps <> \",\" then invalid(t, j)\n put skipSpace(cps, j + 1) into j\n end repeat\nend readArray\n\nprivate function readObject cps, i, t\n put the length of cps into n\n put {} into m\n put {} into seen\n put skipSpace(cps, i + 1) into j\n if j <= n and item j of cps = \"}\" then return [m, j + 1]\n repeat forever\n if j > n then invalid(t, j)\n if item j of cps <> quote then invalid(t, j)\n let [name, k] be readString(cps, j, t)\n if the (name) of seen is true then invalid(t, j)\n put true into the (name) of seen\n put skipSpace(cps, k) into j\n if j > n then invalid(t, j)\n if item j of cps <> \":\" then invalid(t, j)\n let [v, k] be readValue(cps, skipSpace(cps, j + 1), t)\n put v into the (name) of m\n put skipSpace(cps, k) into j\n if j > n then invalid(t, j)\n if item j of cps = \"}\" then return [m, j + 1]\n if item j of cps <> \",\" then invalid(t, j)\n put skipSpace(cps, j + 1) into j\n end repeat\nend readObject\n\nprivate function readString cps, i, t\n put the length of cps into n\n put [] into parts\n put i + 1 into j\n repeat forever\n if j > n then invalid(t, j)\n put item j of cps into c\n if c = quote then return [join(parts), j + 1]\n if codePoint(c) < 0x20 then invalid(t, j)\n if c = \"\\\" then\n if j = n then invalid(t, j + 1)\n put item (j + 1) of cps into e\n if e = quote or e = \"\\\" or e = \"/\" then\n put e after parts\n add 2 to j\n else if e is in [\"b\", \"f\", \"n\", \"r\", \"t\"] then\n put fromCodePoint(item (offset(e, \"bfnrt\")) of [8, 12, 10, 13, 9]) after parts\n add 2 to j\n else if e = \"u\" then\n put hex4(cps, j + 2, t) into u\n if u >= 0xDC00 and u <= 0xDFFF then invalid(t, j)\n add 6 to j\n if u >= 0xD800 and u <= 0xDBFF then\n if j + 1 > n then invalid(t, j)\n if item j of cps <> \"\\\" or item (j + 1) of cps <> \"u\" then invalid(t, j)\n put hex4(cps, j + 2, t) into low\n if low < 0xDC00 or low > 0xDFFF then invalid(t, j)\n put 0x10000 + (u - 0xD800) * 1024 + (low - 0xDC00) into u\n add 6 to j\n end if\n put fromCodePoint(u) after parts\n else\n invalid(t, j + 1)\n end if\n else\n put c after parts\n add 1 to j\n end if\n end repeat\nend readString\n\n-- The four hexadecimal digits starting at code point `i`, as a number.\nprivate function hex4 cps, i, t\n put 0 into u\n repeat for each k in i..(i + 3)\n if k > the length of cps then invalid(t, k)\n put offset(lower(item k of cps), \"0123456789abcdef\") - 1 into d\n if d < 0 then invalid(t, k)\n put u * 16 + d into u\n end repeat\n return u\nend hex4\n\nprivate function literalEnd cps, i, lit, t\n put the code points of lit into want\n repeat for each k in 1..(the length of want)\n if i + k - 1 > the length of cps then invalid(t, i + k - 1)\n if item (i + k - 1) of cps <> item k of want then invalid(t, i + k - 1)\n end repeat\n return i + the length of want\nend literalEnd\n\n-- A number, read exactly. Its exponent is applied, and a positive exponent is\n-- rescaled to 0, so `1e5` is 100000 and `2.50e1` is 25.0 (chapter 7).\nprivate function readNumber cps, i, t\n put the length of cps into n\n put i into j\n put \"\" into sign\n if item j of cps = \"-\" then\n put \"-\" into sign\n add 1 to j\n end if\n if j > n then invalid(t, j)\n if not isDigit(item j of cps) then invalid(t, j)\n put \"\" into whole\n if item j of cps = \"0\" then\n put \"0\" into whole\n add 1 to j\n else\n repeat while j <= n and isDigit(item j of cps)\n put item j of cps after whole\n add 1 to j\n end repeat\n end if\n put \"\" into fraction\n if j <= n and item j of cps = \".\" then\n add 1 to j\n repeat while j <= n and isDigit(item j of cps)\n put item j of cps after fraction\n add 1 to j\n end repeat\n if fraction is empty then invalid(t, j)\n end if\n put \"\" into expSign\n put \"\" into expDigits\n if j <= n and (item j of cps = \"e\" or item j of cps = \"E\") then\n add 1 to j\n if j <= n and (item j of cps = \"+\" or item j of cps = \"-\") then\n put item j of cps into expSign\n add 1 to j\n end if\n repeat while j <= n and isDigit(item j of cps)\n put item j of cps after expDigits\n add 1 to j\n end repeat\n if expDigits is empty then invalid(t, j)\n end if\n return [exactNumber(sign, whole, fraction, expSign, expDigits, t, i), j]\nend readNumber\n\nprivate function exactNumber sign, whole, fraction, expSign, expDigits, t, start\n put whole & fraction into coefficient\n put \"\" into significant\n repeat for each k in 1..(the length of coefficient)\n if character k of coefficient <> \"0\" then\n put characters k..-1 of coefficient into significant\n exit repeat\n end if\n end repeat\n repeat while the length of expDigits > 1 and character 1 of expDigits = \"0\"\n put characters 2..-1 of expDigits into expDigits\n end repeat\n if the length of expDigits > 9 then\n if significant is empty and expSign <> \"-\" then return 0\n invalid(t, start)\n end if\n put 0 into e\n if expDigits is not empty then put expDigits as number into e\n if expSign = \"-\" then put -e into e\n put e - the length of fraction into q\n if significant is empty and q >= 0 then return 0\n if the length of significant > 34 then invalid(t, start)\n if q >= 0 then\n if the length of significant + q > 34 then invalid(t, start)\n return (sign & significant & repeated(\"0\", q)) as number\n end if\n if -q > 6176 then invalid(t, start)\n put coefficient into c\n if the length of c <= -q then put repeated(\"0\", -q - the length of c + 1) & c into c\n put the length of c + q into point\n return (sign & characters 1..point of c & \".\" & characters (point + 1)..-1 of c) as number\nend exactNumber\n\nprivate function skipSpace cps, i\n put i into j\n repeat while j <= the length of cps\n if codePoint(item j of cps) is not in [32, 9, 10, 13] then exit repeat\n add 1 to j\n end repeat\n return j\nend skipSpace\n\nprivate function isDigit c\n return c matches <digit>\nend isDigit\n\n-- `i` is a code point position. The error gives the Character it falls in.\nprivate function invalid t, i\n put the length of t + 1 into at\n if i <= the length of the code points of t then put the length of (code points 1..i of t) into at\n throw {code: \"can't decode\", format: \"json\", offset: at}\nend invalid\n\n-- Encoding -------------------------------------------------------------------\n\n--| The value `v` as compact JSON text\nfunction encodeJson v\n return encodeValue(v, [])\nend encodeJson\n\nprivate function encodeValue v, path\n if v is nothing then return \"null\"\n if v is a boolean or v is a number then return v as text\n if v is a text then return encodeString(v)\n if v is a list then\n put [] into parts\n put 1 into k\n repeat for each x in v\n put encodeValue(x, [...path, k]) after parts\n add 1 to k\n end repeat\n return \"[\" & join(parts, \",\") & \"]\"\n end if\n if v is a map then\n put [] into parts\n repeat for each k in the keys of v\n put encodeString(k) & \":\" & encodeValue(the (k) of v, [...path, k]) after parts\n end repeat\n return \"{\" & join(parts, \",\") & \"}\"\n end if\n put kindOf(v) into kind\n throw {code: \"not encodable\", kind: kind, path: path}\nend encodeValue\n\nprivate function encodeString s\n put [] into parts\n repeat for each c in the code points of s\n put codePoint(c) into cp\n if c = quote then\n put \"\\\" & quote after parts\n else if c = \"\\\" then\n put \"\\\\\" after parts\n else if cp is in [8, 9, 10, 12, 13] then\n put \"\\\" & character (indexIn([8, 9, 10, 12, 13], cp)) of \"btnfr\" after parts\n else if cp < 0x20 then\n put \"\\u00\" & character (cp div 16 + 1) of \"0123456789abcdef\" & character (cp mod 16 + 1) of \"0123456789abcdef\" after parts\n else\n put c after parts\n end if\n end repeat\n return quote & join(parts) & quote\nend encodeString\n\nprivate function indexIn xs, value\n put 1 into i\n repeat for each x in xs\n if x = value then return i\n add 1 to i\n end repeat\n return 0\nend indexIn\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n\n-- JSON Pointer -------------------------------------------------------------\n\n--| The Value Path the JSON Pointer text `t` locates, with each array index as a 1-based position\nfunction pointerPath t\n needText(t)\n put pointerTokens(t) into tokens\n if tokens is nothing then outOfDomain(\"pointerPath\", t)\n put [] into path\n repeat for each token in tokens\n if isIndex(token) then\n put token as number + 1 after path\n else\n put token after path\n end if\n end repeat\n return path\nend pointerPath\n\n-- The reference tokens of the JSON Pointer text `t`, unescaped, or Nothing if\n-- `t` isn't a JSON Pointer. It reads code points, as decoding does, so a\n-- combining mark can't join a slash.\nprivate function pointerTokens t\n put [] into tokens\n if t = \"\" then return tokens\n put the code points of t into cps\n if item 1 of cps <> \"/\" then return nothing\n put [] into token\n put 2 into i\n repeat while i <= the length of cps\n put item i of cps into c\n if c = \"/\" then\n put join(token) after tokens\n put [] into token\n else if c = \"~\" then\n add 1 to i\n put item i of cps into e\n if e = \"0\" then\n put \"~\" after token\n else if e = \"1\" then\n put \"/\" after token\n else\n return nothing\n end if\n else\n put c after token\n end if\n add 1 to i\n end repeat\n put join(token) after tokens\n return tokens\nend pointerTokens\n\n-- Whether a reference token is an array index: digits with no leading zero.\nprivate function isIndex token\n if token = \"\" then return false\n put the code points of token into cps\n if the length of cps > 1 and item 1 of cps = \"0\" then return false\n repeat for each c in cps\n if not isDigit(c) then return false\n end repeat\n return true\nend isIndex\n\n-- JSON Patch (ADR 0137) ------------------------------------------------------\n\n--| `doc` with the RFC 6902 JSON Patch `patch`, a list of operations, applied in order\nfunction applyPatch doc, patch\n needList(patch)\n put 1 into n\n repeat for each operation in patch\n put patchOperation(doc, operation, n) into doc\n add 1 to n\n end repeat\n return doc\nend applyPatch\n\n-- `doc` with one operation applied. Each failure raises `can't patch`.\nprivate function patchOperation doc, operation, n\n if operation is not a map then cantPatch(n, nothing, nothing, \"invalid operation\")\n put the op of operation into op\n put the path of operation into path\n if op is not in [\"add\", \"remove\", \"replace\", \"move\", \"copy\", \"test\"] then cantPatch(n, op, path, \"invalid operation\")\n if path is not a text then cantPatch(n, op, path, \"invalid operation\")\n put pointerTokens(path) into at\n if at is nothing then cantPatch(n, op, path, \"invalid pointer\")\n put nothing into v\n if op is in [\"add\", \"replace\", \"test\"] then\n if \"value\" is not in the keys of operation then cantPatch(n, op, path, \"invalid operation\")\n put the value of operation into v\n end if\n put op into edit\n if op is in [\"move\", \"copy\"] then\n put the from of operation into from\n if from is not a text then cantPatch(n, op, path, \"invalid operation\")\n put pointerTokens(from) into source\n if source is nothing then cantPatch(n, op, path, \"invalid pointer\")\n let [found, v] be valueAt(doc, source)\n if not found then cantPatch(n, op, path, \"missing target\")\n if op = \"move\" then\n if the length of source < the length of at and isPrefix(source, at) then cantPatch(n, op, path, \"move into itself\")\n let [ok, doc] be editAt(doc, source, 1, \"remove\", nothing)\n end if\n put \"add\" into edit\n end if\n if op = \"test\" then\n let [found, current] be valueAt(doc, at)\n if not found or current <> v then cantPatch(n, op, path, \"test failed\")\n return doc\n end if\n let [ok, edited] be editAt(doc, at, 1, edit, v)\n if not ok then cantPatch(n, op, path, \"missing target\")\n return edited\nend patchOperation\n\n-- [ok, `c` with `edit` (\"add\", \"remove\" or \"replace\") of `v` made at tokens\n-- `i` onward of `tokens`], as RFC 6902 makes it; ok is false when a target or\n-- parent it needs is missing.\nprivate function editAt c, tokens, i, edit, v\n if i > the length of tokens then\n if edit = \"remove\" then return [false, c]\n return [true, v]\n end if\n put item i of tokens into token\n if i < the length of tokens then\n let [found, child] be childOf(c, token)\n if not found then return [false, c]\n let [ok, child] be editAt(child, tokens, i + 1, edit, v)\n if not ok then return [false, c]\n if c is a map then\n put child into the (token) of c\n else\n put child into item (token as number + 1) of c\n end if\n return [true, c]\n end if\n if c is a map then\n put token is in the keys of c into present\n if edit = \"add\" then\n put v into the (token) of c\n else if not present then\n return [false, c]\n else if edit = \"remove\" then\n delete the (token) of c\n else\n put v into the (token) of c\n end if\n return [true, c]\n end if\n if c is not a list then return [false, c]\n if edit = \"add\" then\n if token = \"-\" then return [true, insertItem(c, the length of c + 1, v)]\n if not isIndex(token) then return [false, c]\n if token as number > the length of c then return [false, c]\n return [true, insertItem(c, token as number + 1, v)]\n end if\n if not isIndex(token) then return [false, c]\n if token as number >= the length of c then return [false, c]\n if edit = \"remove\" then\n delete item (token as number + 1) of c\n else\n put v into item (token as number + 1) of c\n end if\n return [true, c]\nend editAt\n\n-- [found, the value at `tokens` inside `v`].\nprivate function valueAt v, tokens\n repeat for each token in tokens\n let [found, v] be childOf(v, token)\n if not found then return [false, nothing]\n end repeat\n return [true, v]\nend valueAt\n\n-- [found, the value under `token` in `c`], where a map key may hold Nothing.\nprivate function childOf c, token\n if c is a map then return [token is in the keys of c, the (token) of c]\n if c is not a list then return [false, nothing]\n if not isIndex(token) then return [false, nothing]\n if token as number >= the length of c then return [false, nothing]\n return [true, item (token as number + 1) of c]\nend childOf\n\n-- `xs` with `v` inserted at position `p`, from 1 to one past its end.\nprivate function insertItem xs, p, v\n put [] into out\n put 1 into k\n repeat for each x in xs\n if k = p then put v after out\n put x after out\n add 1 to k\n end repeat\n if p > the length of xs then put v after out\n return out\nend insertItem\n\n-- Whether the tokens `a` begin the tokens `b`.\nprivate function isPrefix a, b\n if the length of a > the length of b then return false\n repeat for each k in 1..(the length of a)\n if item k of a <> item k of b then return false\n end repeat\n return true\nend isPrefix\n\nprivate function cantPatch n, op, path, reason\n throw {code: \"can't patch\", index: n, op: op, path: path, reason: reason}\nend cantPatch\n",
|
|
8
|
+
requires: [text]
|
|
9
|
+
};
|
|
10
|
+
//#endregion
|
|
11
|
+
export { json };
|
package/stdlib/list.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/list.d.ts
|
|
4
|
+
/** The `list` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const list: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/list.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/generated/stdlib/list.ts
|
|
3
|
+
/** The `list` stdlib Library, which `registerStdlib` registers. */
|
|
4
|
+
const list = {
|
|
5
|
+
name: "list",
|
|
6
|
+
source: "-- The `list` Library. Chapter 7 says what each export does, and this source\n-- does exactly that (ADR 0021). It is normative Spec text: its private\n-- helpers, its Fuel and the order of its calls are fixed here.\n\n-- Aggregates -----------------------------------------------------------------\n\n--| The items of `xs` added with `+`, left to right, or 0 for an empty list\nfunction sum xs\n needList(xs)\n if xs is empty then return 0\n put item 1 of xs into total\n repeat for each x in items 2..-1 of xs\n put total + x into total\n end repeat\n return total\nend sum\n\n--| The sum of `xs` divided by its length\nfunction average xs\n needList(xs)\n if xs is empty then outOfDomain(\"average\", xs)\n return sum(xs) / the length of xs\nend average\n\n-- Reshaping ------------------------------------------------------------------\n\n--| The list of pairs `[x, y]` of items at the same position, as long as the shorter list\nfunction zip xs, ys\n needList(xs)\n needList(ys)\n put [] into pairs\n repeat for each i in 1..(the length of xs)\n if i > the length of ys then exit repeat\n put [item i of xs, item i of ys] after pairs\n end repeat\n return pairs\nend zip\n\n--| `xs` without the items equal to an earlier item\nfunction unique xs\n needList(xs)\n put [] into kept\n repeat for each x in xs\n if x is not in kept then put x after kept\n end repeat\n return kept\nend unique\n\n--| `xs` in reverse order\nfunction reverse xs\n needList(xs)\n put [] into backwards\n put the length of xs into i\n repeat while i > 0\n put item i of xs after backwards\n subtract 1 from i\n end repeat\n return backwards\nend reverse\n\n--| `xs` with each item that is a list replaced by its items, one level deep\nfunction flatten xs\n needList(xs)\n put [] into flat\n repeat for each x in xs\n if x is a list then\n put ...x after flat\n else\n put x after flat\n end if\n end repeat\n return flat\nend flatten\n\n-- Sorting --------------------------------------------------------------------\n\n--| `xs` sorted by `<`, stably\nfunction sort xs, direction = \"ascending\"\n needList(xs)\n needText(direction)\n put isDescending(direction, \"sort\") into down\n put [] into keyed\n repeat for each x in xs\n put [x, x] after keyed\n end repeat\n return secondOfEach(mergeByKey(keyed, down))\nend sort\n\n--| `xs` sorted by `<` on the value `key` gives for each item, stably\nfunction sortBy xs, key, direction = \"ascending\"\n needList(xs)\n needFunction(key)\n needText(direction)\n put isDescending(direction, \"sortBy\") into down\n put [] into keyed\n repeat for each x in xs\n put [key(x), x] after keyed\n end repeat\n return secondOfEach(mergeByKey(keyed, down))\nend sortBy\n\n--| `xs` sorted by the two-argument comparator `compare`, stably\nfunction sortWith xs, compare, direction = \"ascending\"\n needList(xs)\n needFunction(compare)\n needText(direction)\n put isDescending(direction, \"sortWith\") into down\n return mergeWith(xs, compare, down)\nend sortWith\n\nprivate function isDescending direction, fn\n if direction = \"ascending\" then return false\n if direction = \"descending\" then return true\n outOfDomain(fn, direction)\nend isDescending\n\n-- The merge sort of chapter 7, over [key, item] pairs compared by `<`.\nprivate function mergeByKey keyed, down\n put the length of keyed into n\n if n < 2 then return keyed\n put n div 2 into half\n put mergeByKey(items 1..half of keyed, down) into left\n put mergeByKey(items (half + 1)..n of keyed, down) into right\n put [] into merged\n put 1 into i\n put 1 into j\n repeat while i <= the length of left and j <= the length of right\n put item i of left into l\n put item j of right into r\n if down then\n put item 1 of l < item 1 of r into rightFirst\n else\n put item 1 of r < item 1 of l into rightFirst\n end if\n if rightFirst then\n put r after merged\n add 1 to j\n else\n put l after merged\n add 1 to i\n end if\n end repeat\n put ...items i..-1 of left after merged\n put ...items j..-1 of right after merged\n return merged\nend mergeByKey\n\n-- The same merge sort, asking `compare(left, right)` for a number.\nprivate function mergeWith xs, compare, down\n put the length of xs into n\n if n < 2 then return xs\n put n div 2 into half\n put mergeWith(items 1..half of xs, compare, down) into left\n put mergeWith(items (half + 1)..n of xs, compare, down) into right\n put [] into merged\n put 1 into i\n put 1 into j\n repeat while i <= the length of left and j <= the length of right\n put item i of left into l\n put item j of right into r\n put compare(l, r) into order\n needNumber(order)\n if down then\n put order < 0 into rightFirst\n else\n put order > 0 into rightFirst\n end if\n if rightFirst then\n put r after merged\n add 1 to j\n else\n put l after merged\n add 1 to i\n end if\n end repeat\n put ...items i..-1 of left after merged\n put ...items j..-1 of right after merged\n return merged\nend mergeWith\n\nprivate function secondOfEach keyed\n put [] into picked\n repeat for each pair in keyed\n put item 2 of pair after picked\n end repeat\n return picked\nend secondOfEach\n\n-- Calling a Function Value on each item --------------------------------------\n\n--| The items of `xs` for which `keep` gives true, in order\nfunction filter xs, keep\n needList(xs)\n needFunction(keep)\n put [] into kept\n repeat for each x in xs\n if keep(x) then put x after kept\n end repeat\n return kept\nend filter\n\n--| The list of what `f` gives for each item of `xs`, in order\nfunction map xs, f\n needList(xs)\n needFunction(f)\n put [] into mapped\n repeat for each x in xs\n put f(x) after mapped\n end repeat\n return mapped\nend map\n\n--| `f(f(f(initial, x1), x2), …)` over the items of `xs`, or `initial` for an empty list\nfunction reduce xs, f, initial\n needList(xs)\n needFunction(f)\n put initial into acc\n repeat for each x in xs\n put f(acc, x) into acc\n end repeat\n return acc\nend reduce\n\n--| A map from each text `key` gives to the list of items that gave it, keyed in first-seen order\nfunction group xs, key\n needList(xs)\n needFunction(key)\n put {} into groups\n repeat for each x in xs\n put key(x) into k\n needText(k)\n if the (k) of groups is nothing then\n put [x] into the (k) of groups\n else\n put x after the (k) of groups\n end if\n end repeat\n return groups\nend group\n\n--| `[matching, rest]`: the items for which `keep` gives true and the others, each in order\nfunction partition xs, keep\n needList(xs)\n needFunction(keep)\n put [] into matching\n put [] into others\n repeat for each x in xs\n if keep(x) then\n put x after matching\n else\n put x after others\n end if\n end repeat\n return [matching, others]\nend partition\n\n--| Whether `test` gives true for some item, stopping at the first that does\nfunction any xs, test\n needList(xs)\n needFunction(test)\n repeat for each x in xs\n if test(x) then return true\n end repeat\n return false\nend any\n\n--| Whether `test` gives true for every item, stopping at the first that doesn't\nfunction all xs, test\n needList(xs)\n needFunction(test)\n repeat for each x in xs\n if not test(x) then return false\n end repeat\n return true\nend all\n\n--| The first item for which `test` gives true, or Nothing\nfunction find xs, test\n needList(xs)\n needFunction(test)\n repeat for each x in xs\n if test(x) then return x\n end repeat\n return nothing\nend find\n\n--| The position of the first item equal to `value`, or 0 if there is none\nfunction indexOf xs, value\n needList(xs)\n put 1 into i\n repeat for each x in xs\n if x = value then return i\n add 1 to i\n end repeat\n return 0\nend indexOf\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n",
|
|
7
|
+
requires: []
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { list };
|
package/stdlib/map.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/map.d.ts
|
|
4
|
+
/** The `map` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const map: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/map.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/generated/stdlib/map.ts
|
|
3
|
+
/** The `map` stdlib Library, which `registerStdlib` registers. */
|
|
4
|
+
const map = {
|
|
5
|
+
name: "map",
|
|
6
|
+
source: "-- The `map` Library. Chapter 7 says what each export does, and this source\n-- does exactly that (ADR 0021). It is normative Spec text: its private\n-- helpers, its Fuel and the order of its calls are fixed here.\n\n--| The map with the entries of `a` and then of `b`, where `b`'s value wins for a key in both\nfunction merge a, b\n needMap(a)\n needMap(b)\n put a into merged\n repeat for each k in the keys of b\n put the (k) of b into the (k) of merged\n end repeat\n return merged\nend merge\n\n--| `m` without the entries whose keys are in the list `keys`\nfunction without m, keys\n needMap(m)\n needKeys(keys)\n put {} into kept\n repeat for each k in the keys of m\n if k is not in keys then put the (k) of m into the (k) of kept\n end repeat\n return kept\nend without\n\n--| `m` with only the entries whose keys are in the list `keys`, in `m`'s order\nfunction pick m, keys\n needMap(m)\n needKeys(keys)\n put {} into picked\n repeat for each k in the keys of m\n if k is in keys then put the (k) of m into the (k) of picked\n end repeat\n return picked\nend pick\n\n--| The list of pairs `[key, value]` of `m`, in order\nfunction entries m\n needMap(m)\n put [] into pairs\n repeat for each k in the keys of m\n put [k, the (k) of m] after pairs\n end repeat\n return pairs\nend entries\n\n--| The map with the entries of the list of pairs `pairs`, where a later pair wins for a repeated key\nfunction fromEntries pairs\n needList(pairs)\n repeat for each pair in pairs\n needList(pair)\n if the length of pair <> 2 then outOfDomain(\"fromEntries\", pair)\n needText(item 1 of pair)\n end repeat\n put {} into built\n repeat for each pair in pairs\n put item 2 of pair into the (item 1 of pair) of built\n end repeat\n return built\nend fromEntries\n\n--| `m` with each value replaced by what `f` gives for it, keys and order kept\nfunction mapValues m, f\n needMap(m)\n needFunction(f)\n put {} into mapped\n repeat for each k in the keys of m\n put f(the (k) of m) into the (k) of mapped\n end repeat\n return mapped\nend mapValues\n\n--| The value the Value Path `path` locates inside `m`, or `default` if a key or position along it is missing or holds Nothing\nfunction lookup m, path, default = nothing\n needList(path)\n repeat for each step in path\n if step is a number then needInteger(step)\n if step is not a number then needText(step)\n end repeat\n put m into v\n put [] into taken\n repeat for each step in path\n if v is nothing then return default\n if step is a text then\n if v is not a map then wrongKindAt(\"map\", v, taken)\n put the (step) of v into v\n else\n if v is not a list then wrongKindAt(\"list\", v, taken)\n put item step of v into v\n end if\n put step after taken\n end repeat\n if v is nothing then return default\n return v\nend lookup\n\n-- A list of texts, as `without` and `pick` take.\nprivate function needKeys keys\n needList(keys)\n repeat for each k in keys\n needText(k)\n end repeat\nend needKeys\n\n-- `wrong kind` for the value `lookup` reached at the Value Path `path`.\nprivate function wrongKindAt expected, v, path\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v, path: path}\nend wrongKindAt\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n\n-- Core intrinsic entry, private to normative Standard Library code (#696).\n-- In trusted compilation only, this unary call lowers to call-restricted.\nprivate function restrictedCall f, value\n return f(value)\nend restrictedCall\n\n--| A complete fresh mapped value, or ordered diagnostics without partial output\nfunction transform value, mapping\n validateMapping(mapping)\n put {} into built\n put [] into errors\n put 0 into ruleNumber\n repeat for each rule in the rules of mapping\n add 1 to ruleNumber\n put selectMapping(value, the from of rule) into selections\n put [] into results\n put 0 into position\n put false into wrote\n repeat for each selected in selections\n add 1 to position\n put mappingLocation(ruleNumber, rule, selected, position) into location\n if \"error\" is in the keys of selected then\n put mappingFailure(location, \"source\", the error of selected) after errors\n next repeat\n end if\n if not the present of selected then\n put \"error\" into policy\n if \"missing\" is in the keys of rule then put the missing of rule into policy\n if policy = \"skip\" then next repeat\n if policy = \"error\" then\n put mappingFailure(location, \"source\", {code: \"missing selection\", step: the failedStep of selected}) after errors\n next repeat\n end if\n put the default of rule into itemValue\n else\n put the value of selected into itemValue\n end if\n if \"input\" is in the keys of rule then\n put explain(itemValue, the input of rule) into failures\n if failures is not empty then\n repeat for each failure in failures\n put mappingFailure(location, \"input\", failure) after errors\n end repeat\n next repeat\n end if\n end if\n if \"transform\" is in the keys of rule then\n try\n put restrictedCall(the transform of rule, itemValue) into itemValue\n catch failure\n put mappingFailure(location, \"transform\", failure) after errors\n next repeat\n end try\n end if\n if \"output\" is in the keys of rule then\n put explain(itemValue, the output of rule) into failures\n if failures is not empty then\n repeat for each failure in failures\n put mappingFailure(location, \"output\", failure) after errors\n end repeat\n next repeat\n end if\n end if\n put itemValue after results\n put true into wrote\n end repeat\n if hasWildcard(the from of rule) then\n put mappingWrite(built, the to of rule, results) into built\n else\n if wrote then put mappingWrite(built, the to of rule, item 1 of results) into built\n end if\n end repeat\n if errors is not empty then return {ok: false, errors: errors}\n return {ok: true, value: built}\nend transform\n\n-- Complete definition validation precedes selection and callback execution.\nprivate function validateMapping mapping\n if mapping is not a map then invalidMapping(\"not a mapping\", [], mapping)\n mappingKeys(mapping, [\"rules\"], [])\n if \"rules\" is not in the keys of mapping then invalidMapping(\"missing rules\", [], mapping)\n if the rules of mapping is not a list then invalidMapping(\"invalid rules\", [\"rules\"], the rules of mapping)\n put [] into destinations\n put 0 into number\n repeat for each rule in the rules of mapping\n add 1 to number\n put [\"rules\", number] into path\n if rule is not a map then invalidMapping(\"invalid rule\", path, rule)\n mappingKeys(rule, [\"from\", \"to\", \"input\", \"output\", \"transform\", \"missing\", \"default\"], path)\n if \"from\" is not in the keys of rule then invalidMapping(\"missing from\", path, rule)\n put [...path, \"from\"] into fromPath\n if the from of rule is not a list then invalidMapping(\"invalid source path\", fromPath, the from of rule)\n put 0 into index\n repeat for each step in the from of rule\n add 1 to index\n if not selectionStep(step) then invalidMapping(\"invalid selection step\", [...fromPath, index], step)\n end repeat\n if \"to\" is not in the keys of rule then invalidMapping(\"missing to\", path, rule)\n put [...path, \"to\"] into toPath\n if the to of rule is not a list then invalidMapping(\"invalid destination path\", toPath, the to of rule)\n if the to of rule is empty then invalidMapping(\"empty destination\", toPath, the to of rule)\n put 0 into index\n repeat for each step in the to of rule\n add 1 to index\n if step is not a text then invalidMapping(\"invalid destination step\", [...toPath, index], step)\n end repeat\n repeat for each stage in [\"input\", \"output\"]\n if stage is in the keys of rule then\n try\n put conforms(nothing, the (stage) of rule) into ignored\n catch failure\n throw {code: \"invalid mapping\", reason: \"invalid shape\", path: [...path, stage], value: the (stage) of rule, error: failure}\n end try\n end if\n end repeat\n if \"transform\" is in the keys of rule then\n if the transform of rule is not a function then invalidMapping(\"invalid transform\", [...path, \"transform\"], the transform of rule)\n if 1 is not in functionArity(the transform of rule) then invalidMapping(\"invalid transform arity\", [...path, \"transform\"], the transform of rule)\n end if\n put \"error\" into policy\n if \"missing\" is in the keys of rule then\n put the missing of rule into policy\n if policy is not in [\"error\", \"skip\", \"default\"] then invalidMapping(\"invalid missing policy\", [...path, \"missing\"], policy)\n end if\n if policy = \"default\" then\n if \"default\" is not in the keys of rule then invalidMapping(\"missing default\", path, rule)\n else\n if \"default\" is in the keys of rule then invalidMapping(\"unexpected default\", [...path, \"default\"], the default of rule)\n end if\n put 0 into previous\n repeat for each destination in destinations\n add 1 to previous\n if overlappingPaths(destination, the to of rule) then\n throw {code: \"invalid mapping\", reason: \"overlapping destination\", path: toPath, value: the to of rule, otherRule: previous}\n end if\n end repeat\n put the to of rule after destinations\n end repeat\nend validateMapping\n\nprivate function mappingKeys definition, allowed, path\n repeat for each key in the keys of definition\n if key is not in allowed then invalidMapping(\"unknown key\", [...path, key], the (key) of definition)\n end repeat\nend mappingKeys\n\nprivate function invalidMapping reason, path, value\n throw {code: \"invalid mapping\", reason: reason, path: path, value: value}\nend invalidMapping\n\nprivate function selectionStep step\n if step is a text then return true\n if step is an integer then return step <> 0\n if step is a map then return step = {each: true}\n return false\nend selectionStep\n\nprivate function hasWildcard path\n repeat for each step in path\n if step is a map then return true\n end repeat\n return false\nend hasWildcard\n\nprivate function overlappingPaths a, b\n put 1 into index\n repeat while index <= the length of a and index <= the length of b\n if item index of a <> item index of b then return false\n add 1 to index\n end repeat\n return true\nend overlappingPaths\n\n-- Explicit work stack: no Script recursion or native depth limit. Children\n-- are pushed backwards so each wildcard is visited depth-first in source order.\nprivate function selectMapping value, path\n put [{value: value, path: [], offset: 1}] into pending\n put [] into selected\n repeat while pending is not empty\n put item -1 of pending into node\n delete item -1 of pending\n put the value of node into current\n put the path of node into taken\n put the offset of node into offset\n if offset > the length of path then\n put {present: true, value: current, path: taken} after selected\n next repeat\n end if\n put item offset of path into step\n if step is a map then\n if current is a map then\n put the keys of current into keys\n else\n if current is a list then\n if current is empty then\n put [] into keys\n else\n put the items of (1..(the length of current)) into keys\n end if\n else\n put {present: false, path: taken, failedStep: step, error: {code: \"wrong kind\", expected: \"map or list\", got: kindOf(current), value: current}} after selected\n next repeat\n end if\n end if\n put the length of keys into index\n repeat while index > 0\n put item index of keys into key\n if current is a map then\n put the (key) of current into child\n else\n put item key of current into child\n end if\n put {value: child, path: [...taken, key], offset: offset + 1} after pending\n subtract 1 from index\n end repeat\n next repeat\n end if\n if step is a text then\n if current is not a map then\n put {present: false, path: taken, failedStep: step, error: {code: \"wrong kind\", expected: \"map\", got: kindOf(current), value: current}} after selected\n next repeat\n end if\n if step is not in the keys of current then\n put {present: false, path: taken, failedStep: step} after selected\n next repeat\n end if\n put the (step) of current into child\n put step into resolved\n else\n if current is not a list then\n put {present: false, path: taken, failedStep: step, error: {code: \"wrong kind\", expected: \"list\", got: kindOf(current), value: current}} after selected\n next repeat\n end if\n put step into resolved\n if step < 0 then put the length of current + step + 1 into resolved\n if resolved < 1 or resolved > the length of current then\n put {present: false, path: taken, failedStep: step} after selected\n next repeat\n end if\n put item resolved of current into child\n end if\n put {value: child, path: [...taken, resolved], offset: offset + 1} after pending\n end repeat\n return selected\nend selectMapping\n\nprivate function mappingLocation number, rule, selected, position\n put {rule: number, sourcePath: the path of selected, destinationPath: the to of rule} into location\n if hasWildcard(the from of rule) then put position into the position of location\n if \"failedStep\" is in the keys of selected then put the failedStep of selected into the failedStep of location\n return location\nend mappingLocation\n\nprivate function mappingFailure location, stage, failure\n put stage into the stage of location\n put failure into the error of location\n return location\nend mappingFailure\n\n-- Rebuild destination parents iteratively, from the leaf towards the root.\nprivate function mappingWrite built, path, value\n put [] into parents\n put built into current\n put 1 into index\n repeat while index < the length of path\n put current after parents\n put the (item index of path) of current into current\n if current is nothing then put {} into current\n add 1 to index\n end repeat\n put value into the (item index of path) of current\n subtract 1 from index\n repeat while index > 0\n put item index of parents into parent\n put current into the (item index of path) of parent\n put parent into current\n subtract 1 from index\n end repeat\n return current\nend mappingWrite\n\n-- Writing through a Value Path (ADR 0136) ----------------------------------\n\n--| `m` with `v` at the Value Path `path`\nfunction withPath m, path, v\n needPath(path)\n return placeAt(m, path, 1, v, [])\nend withPath\n\n--| `m` with the key or item at the Value Path `path` removed\nfunction withoutPath m, path\n needPath(path)\n if the length of path = 0 then outOfDomain(\"withoutPath\", path)\n return removeAt(m, path, 1, [])\nend withoutPath\n\n--| Whether the Value Path `path` locates a value inside `m` that isn't Nothing\nfunction hasPath m, path\n return lookup(m, path) is not nothing\nend hasPath\n\n-- A Value Path: a list of texts and integers, checked before any step.\nprivate function needPath path\n needList(path)\n repeat for each step in path\n if step is a number then needInteger(step)\n if step is not a number then needText(step)\n end repeat\nend needPath\n\n-- `c` with `v` written at steps `i` onward of `path`, as `put` writes. A\n-- missing parent is never made, so stepping into it raises.\nprivate function placeAt c, path, i, v, taken\n if i > the length of path then return v\n put item i of path into step\n put taken into below\n put step after below\n if step is a text then\n if c is not a map then wrongKindAt(\"map\", c, taken)\n if i < the length of path then put placeAt(the (step) of c, path, i + 1, v, below) into v\n put v into the (step) of c\n else\n if c is not a list then wrongKindAt(\"list\", c, taken)\n if i < the length of path then put placeAt(item step of c, path, i + 1, v, below) into v\n put v into item step of c\n end if\n return c\nend placeAt\n\n-- `c` with the key or item at steps `i` onward of `path` deleted, as `delete`\n-- deletes. A step that reaches Nothing leaves `c` as it was.\nprivate function removeAt c, path, i, taken\n if c is nothing then return c\n put item i of path into step\n put taken into below\n put step after below\n if step is a text then\n if c is not a map then wrongKindAt(\"map\", c, taken)\n if the (step) of c is nothing then return c\n if i = the length of path then\n delete the (step) of c\n else\n put removeAt(the (step) of c, path, i + 1, below) into the (step) of c\n end if\n else\n if c is not a list then wrongKindAt(\"list\", c, taken)\n if item step of c is nothing then return c\n if i = the length of path then\n delete item step of c\n else\n put removeAt(item step of c, path, i + 1, below) into item step of c\n end if\n end if\n return c\nend removeAt\n",
|
|
7
|
+
requires: []
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { map };
|
package/stdlib/text.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/// <reference lib="esnext.disposable" />
|
|
2
|
+
import { t as StdlibLibrary } from "../stdlib-registry-BwitI3Ox.js";
|
|
3
|
+
//#region src/generated/stdlib/text.d.ts
|
|
4
|
+
/** The `text` stdlib Library, which `registerStdlib` registers. */
|
|
5
|
+
export declare const text: StdlibLibrary;
|
|
6
|
+
//#endregion
|
package/stdlib/text.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
|
|
2
|
+
//#region src/generated/stdlib/text.ts
|
|
3
|
+
/** The `text` stdlib Library, which `registerStdlib` registers. */
|
|
4
|
+
const text = {
|
|
5
|
+
name: "text",
|
|
6
|
+
source: "-- The `text` Library. Chapter 7 says what each export does, and this source\n-- does exactly that (ADR 0021). It is normative Spec text: its private\n-- helpers, its Fuel and the order of its calls are fixed here.\n\n-- Padding and trimming -------------------------------------------------------\n\n--| `t` with `fill` added at the end until it is `width` Characters long\nfunction pad t, width, fill = \" \"\n needText(t)\n needInteger(width)\n needText(fill)\n if width < 0 then outOfDomain(\"pad\", width)\n if the length of fill <> 1 then outOfDomain(\"pad\", fill)\n if the length of t >= width then return t\n return t & repeated(fill, width - the length of t)\nend pad\n\n--| `t` with `fill` added at the start until it is `width` Characters long\nfunction padLeft t, width, fill = \" \"\n needText(t)\n needInteger(width)\n needText(fill)\n if width < 0 then outOfDomain(\"padLeft\", width)\n if the length of fill <> 1 then outOfDomain(\"padLeft\", fill)\n if the length of t >= width then return t\n return repeated(fill, width - the length of t) & t\nend padLeft\n\n--| `t` without the white space at its start and end\nfunction trim t\n needText(t)\n return trimEnd(trimStart(t))\nend trim\n\n--| `t` without the white space at its start\nfunction trimStart t\n needText(t)\n put the characters of t into cs\n repeat for each i in 1..(the length of cs)\n if not (item i of cs matches <whitespace>) then return characters i..-1 of t\n end repeat\n return \"\"\nend trimStart\n\n--| `t` without the white space at its end\nfunction trimEnd t\n needText(t)\n put the characters of t into cs\n put the length of cs into i\n repeat while i > 0\n if not (item i of cs matches <whitespace>) then return characters 1..i of t\n subtract 1 from i\n end repeat\n return \"\"\nend trimEnd\n\n-- Splitting and joining ------------------------------------------------------\n\n--| The list of the texts between the matches of `separator`, a text or a Text Pattern, in `t`\nfunction split t, separator\n needText(t)\n if separator is a text then\n if separator is empty then outOfDomain(\"split\", separator)\n put every match of <(separator)> in t into found\n else if separator is a pattern then\n put every match of separator in t into found\n else\n wrongKind(\"text or pattern\", separator)\n end if\n put [] into pieces\n put 1 into pos\n repeat for each m in found\n if the text of m is not empty then\n put startOf(m) into start\n put characters pos..(start - 1) of t after pieces\n put start + the length of the text of m into pos\n end if\n end repeat\n put characters pos..-1 of t after pieces\n return pieces\nend split\n\n--| The items of `xs`, each shown as `&` shows it, with `separator` between each pair\nfunction join xs, separator = \"\"\n needList(xs)\n needText(separator)\n put [] into parts\n repeat for each x in xs\n if parts is not empty then put separator after parts\n put \"\" & x after parts\n end repeat\n return joined(parts)\nend join\n\n--| `n` copies of `t`, joined\nfunction repeated t, n\n needText(t)\n needInteger(n)\n if n < 0 then outOfDomain(\"repeated\", n)\n put [] into parts\n repeat n times\n put t after parts\n end repeat\n return joined(parts)\nend repeated\n\n-- Searching ------------------------------------------------------------------\n\n--| The Character position where the last match of `needle`, a text or a Text Pattern, starts in `t`, or 0 if there is none\nfunction lastOffset needle, t\n if needle is a text then\n put <(needle)> into p\n else if needle is a pattern then\n put needle into p\n else\n wrongKind(\"text or pattern\", needle)\n end if\n needText(t)\n put every match of p in t into found\n if found is empty then return 0\n return startOf(item -1 of found)\nend lastOffset\n\n-- Templates ------------------------------------------------------------------\n\n--| `template` with each `${name}` replaced by the value of that key of `values`, shown as `&` shows it\nfunction format template, values\n needText(template)\n needMap(values)\n put the characters of template into cs\n put the length of cs into n\n put [] into parts\n put 1 into i\n repeat while i <= n\n put item i of cs into c\n if c = \"$\" and i < n and item (i + 1) of cs = \"$\" then\n put \"$\" after parts\n add 2 to i\n else if c = \"$\" and i < n and item (i + 1) of cs = \"{\" then\n put i + 2 into j\n repeat while j <= n and item j of cs <> \"}\"\n add 1 to j\n end repeat\n if j > n then outOfDomain(\"format\", template)\n put characters (i + 2)..(j - 1) of template into name\n if name is not in the keys of values then outOfDomain(\"format\", name)\n put \"\" & the (name) of values after parts\n put j + 1 into i\n else\n put c after parts\n add 1 to i\n end if\n end repeat\n return joined(parts)\nend format\n\n-- Numbers in a Locale --------------------------------------------------------\n\n--| The number `n` written with a Locale's `numberSymbols`, rounded to `places` digits after the point if `places` is given\nfunction formatNumber n, symbols, places = nothing\n needNumber(n)\n needMap(symbols)\n if places is not nothing then needInteger(places)\n checkSymbols(symbols, \"formatNumber\")\n if places is not nothing and places < 0 then outOfDomain(\"formatNumber\", places)\n if places is not nothing then put round(n, places) into n\n put n as text into written\n put \"\" into sign\n if n < 0 then\n put the minus of symbols into sign\n put characters 2..-1 of written into written\n end if\n put split(written, \".\") into halves\n put localDigits(grouped(item 1 of halves, symbols), symbols) into whole\n if the length of halves = 1 then return sign & whole\n return sign & whole & the decimal of symbols & localDigits(item 2 of halves, symbols)\nend formatNumber\n\n--| The number `t` writes with a Locale's `numberSymbols`\nfunction parseNumber t, symbols\n needText(t)\n needMap(symbols)\n checkSymbols(symbols, \"parseNumber\")\n put the length of t into n\n put 1 into i\n put \"\" into sign\n if startsWith(t, i, the minus of symbols) then\n put \"-\" into sign\n add the length of the minus of symbols to i\n end if\n -- The integer part: digits, and group separators with where each one stands.\n put \"\" into whole\n put [] into spots\n put [] into groups\n repeat while i <= n\n put digitAt(t, i, symbols) into d\n if d is not nothing then\n put item 1 of d after whole\n put i after spots\n add item 2 of d to i\n else if whole is not empty and startsWith(t, i, the group of symbols) then\n put [the length of whole, i] after groups\n add the length of the group of symbols to i\n else\n exit repeat\n end if\n end repeat\n if whole is empty then cantParse(t, i)\n if groups is not empty then checkGrouping(t, whole, spots, groups, symbols)\n -- The fraction: the decimal separator, then at least one digit.\n put \"\" into fraction\n if i <= n and startsWith(t, i, the decimal of symbols) then\n add the length of the decimal of symbols to i\n repeat while i <= n\n put digitAt(t, i, symbols) into d\n if d is nothing then exit repeat\n put item 1 of d after fraction\n add item 2 of d to i\n end repeat\n if fraction is empty then cantParse(t, i)\n end if\n if i <= n then cantParse(t, i)\n put sign & whole into ascii\n if fraction is not empty then put \".\" & fraction after ascii\n if not (ascii can be a number) then cantParse(t, 1)\n return ascii as number\nend parseNumber\n\n-- Every key of `numberSymbols`, with the kind it must have (chapter 7).\nprivate function checkSymbols symbols, fn\n repeat for each k in [\"decimal\", \"group\", \"minus\"]\n put the (k) of symbols into v\n if v is not a text or v is empty then outOfDomain(fn, symbols)\n end repeat\n put the digits of symbols into ds\n if ds is not a list or the length of ds <> 10 then outOfDomain(fn, symbols)\n repeat for each v in ds\n if v is not a text or v is empty then outOfDomain(fn, symbols)\n end repeat\n repeat for each k in [\"primaryGroup\", \"secondaryGroup\", \"minGrouping\"]\n put the (k) of symbols into v\n if v is not a number or v is not an integer then outOfDomain(fn, symbols)\n if v < 1 then outOfDomain(fn, symbols)\n end repeat\nend checkSymbols\n\n-- The ASCII digits `whole`, with a \",\" wherever the Locale groups them.\nprivate function grouped whole, symbols\n put the primaryGroup of symbols into primary\n put the secondaryGroup of symbols into secondary\n put the length of whole into len\n if len < primary + the minGrouping of symbols then return whole\n put [characters (len - primary + 1)..len of whole] into parts\n put len - primary into left\n repeat while left > secondary\n put characters (left - secondary + 1)..left of whole before parts\n subtract secondary from left\n end repeat\n put characters 1..left of whole before parts\n return join(parts, \",\")\nend grouped\n\n-- Writes ASCII digits, \".\" and \",\" with the Locale's digits and separators.\nprivate function localDigits ascii, symbols\n put [] into parts\n repeat for each c in the characters of ascii\n if c = \",\" then\n put the group of symbols after parts\n else if c = \".\" then\n put the decimal of symbols after parts\n else\n put item (c as number + 1) of the digits of symbols after parts\n end if\n end repeat\n return joined(parts)\nend localDigits\n\n-- The Locale digit at Character `i` of `t`, as [ASCII digit, its length], or Nothing.\nprivate function digitAt t, i, symbols\n put 0 into k\n repeat for each d in the digits of symbols\n if startsWith(t, i, d) then return [k as text, the length of d]\n add 1 to k\n end repeat\n return nothing\nend digitAt\n\n-- A group separator may stand only where `formatNumber` would put one, and\n-- then every one it would put must be there. `spots` holds where each digit\n-- starts, and each group is [digits before it, where it starts].\nprivate function checkGrouping t, whole, spots, groups, symbols\n put [] into wanted\n put 0 into before\n repeat for each piece in items 1..-2 of split(grouped(whole, symbols), \",\")\n add the length of piece to before\n put before after wanted\n end repeat\n put 1 into k\n repeat while k <= the length of groups or k <= the length of wanted\n put item k of groups into g\n put item k of wanted into w\n if g is nothing then cantParse(t, item (w + 1) of spots)\n if w is nothing then cantParse(t, item 2 of g)\n if item 1 of g < w then cantParse(t, item 2 of g)\n if item 1 of g > w then cantParse(t, item (w + 1) of spots)\n add 1 to k\n end repeat\nend checkGrouping\n\nprivate function startsWith t, i, prefix\n put i + the length of prefix - 1 into last\n if last > the length of t then return false\n return characters i..last of t = prefix\nend startsWith\n\nprivate function cantParse t, at\n throw {code: \"can't convert\", value: t, to: \"number\", offset: at}\nend cantParse\n\n-- Helpers --------------------------------------------------------------------\n\n-- Where a match starts. An empty match's range is p..p-1, which has no\n-- items, so its first end is read with `rangeStart` (ADR 0036).\nprivate function startOf m\n return rangeStart(the range of m)\nend startOf\n\n-- Joins a list of texts, halving so each Character is copied about log n times.\nprivate function joined parts\n put the length of parts into n\n if n = 0 then return \"\"\n if n = 1 then return item 1 of parts\n put n div 2 into half\n return joined(items 1..half of parts) & joined(items (half + 1)..n of parts)\nend joined\n\n-- The helpers below are the same in every stdlib Library. They raise the\n-- catalogue errors of chapter 6. The Core adds each one's `message` and\n-- points its `at` at the Script's call (ADR 0037).\n\nprivate function wrongKind expected, v\n put kindOf(v) into got\n throw {code: \"wrong kind\", expected: expected, got: got, value: v}\nend wrongKind\n\nprivate function outOfDomain fn, v\n throw {code: \"out of domain\", function: fn, value: v}\nend outOfDomain\n\nprivate function needText v\n if v is not a text then wrongKind(\"text\", v)\nend needText\n\nprivate function needNumber v\n if v is not a number then wrongKind(\"number\", v)\nend needNumber\n\nprivate function needInteger v\n if v is not a number then wrongKind(\"number\", v)\n if v is not an integer then wrongKind(\"integer\", v)\nend needInteger\n\nprivate function needList v\n if v is not a list then wrongKind(\"list\", v)\nend needList\n\nprivate function needMap v\n if v is not a map then wrongKind(\"map\", v)\nend needMap\n\nprivate function needFunction v\n if v is not a function then wrongKind(\"function\", v)\nend needFunction\n",
|
|
7
|
+
requires: []
|
|
8
|
+
};
|
|
9
|
+
//#endregion
|
|
10
|
+
export { text };
|