eliscript 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +674 -0
- package/README.md +135 -0
- package/bin/eliscript +16 -0
- package/bin/eliscript-bootstrap +15 -0
- package/bin/eliscript-build +15 -0
- package/bin/eliscript-check +15 -0
- package/bin/eliscript-eval +15 -0
- package/bin/eliscript-format +15 -0
- package/bin/eliscript-org +10 -0
- package/bin/eliscript-portable +17 -0
- package/bin/eliscript-seed +10 -0
- package/bin/eliscript-seed-build +10 -0
- package/bin/eliscript-watch +15 -0
- package/bootstrap/README.md +100 -0
- package/bootstrap/compiler/analyzer.eli +1426 -0
- package/bootstrap/compiler/compiler.eli +472 -0
- package/bootstrap/compiler/emitter.eli +1745 -0
- package/bootstrap/compiler/expander.eli +2522 -0
- package/bootstrap/compiler/formatter.eli +359 -0
- package/bootstrap/compiler/ir.eli +297 -0
- package/bootstrap/compiler/lower.eli +661 -0
- package/bootstrap/compiler/project.eli +1120 -0
- package/bootstrap/compiler/reader.eli +321 -0
- package/bootstrap/compiler/source-map.eli +165 -0
- package/bootstrap/compiler/symbol.eli +92 -0
- package/bootstrap/compiler/syntax.eli +54 -0
- package/bootstrap/compiler/transient-analysis.eli +825 -0
- package/bootstrap/host/build.mjs +28 -0
- package/bootstrap/host/bun.mjs +282 -0
- package/bootstrap/host/check-cli.mjs +163 -0
- package/bootstrap/host/eliscript-cli.mjs +36 -0
- package/bootstrap/host/evaluation-cli.mjs +401 -0
- package/bootstrap/host/evaluation.mjs +560 -0
- package/bootstrap/host/format-cli.mjs +139 -0
- package/bootstrap/host/project-cli.mjs +295 -0
- package/bootstrap/host/project.mjs +1080 -0
- package/bootstrap/host/watch-cli.mjs +129 -0
- package/bootstrap/host/watch.mjs +267 -0
- package/browser/host.mjs +357 -0
- package/browser/worker.mjs +60 -0
- package/compiler/README.md +118 -0
- package/compiler/eliscript-analyzer.el +716 -0
- package/compiler/eliscript-cli.el +108 -0
- package/compiler/eliscript-diagnostic.el +158 -0
- package/compiler/eliscript-emitter.el +1351 -0
- package/compiler/eliscript-evaluation.el +259 -0
- package/compiler/eliscript-expander.el +1630 -0
- package/compiler/eliscript-form.el +79 -0
- package/compiler/eliscript-ir-emitter.el +1460 -0
- package/compiler/eliscript-ir.el +356 -0
- package/compiler/eliscript-lower.el +463 -0
- package/compiler/eliscript-macro-eval.el +811 -0
- package/compiler/eliscript-parameters.el +324 -0
- package/compiler/eliscript-portable.el +492 -0
- package/compiler/eliscript-project-cli.el +156 -0
- package/compiler/eliscript-project.el +1676 -0
- package/compiler/eliscript-reader.el +421 -0
- package/compiler/eliscript-source-map.el +192 -0
- package/compiler/eliscript-symbol.el +101 -0
- package/compiler/eliscript-transient-analysis.el +817 -0
- package/compiler/eliscript.el +239 -0
- package/dist/bootstrap/analyzer.mjs +968 -0
- package/dist/bootstrap/analyzer.mjs.map +1 -0
- package/dist/bootstrap/compiler.mjs +228 -0
- package/dist/bootstrap/compiler.mjs.map +1 -0
- package/dist/bootstrap/emitter.mjs +1173 -0
- package/dist/bootstrap/emitter.mjs.map +1 -0
- package/dist/bootstrap/expander.mjs +1469 -0
- package/dist/bootstrap/expander.mjs.map +1 -0
- package/dist/bootstrap/formatter.mjs +304 -0
- package/dist/bootstrap/formatter.mjs.map +1 -0
- package/dist/bootstrap/ir.mjs +193 -0
- package/dist/bootstrap/ir.mjs.map +1 -0
- package/dist/bootstrap/lower.mjs +344 -0
- package/dist/bootstrap/lower.mjs.map +1 -0
- package/dist/bootstrap/project.mjs +585 -0
- package/dist/bootstrap/project.mjs.map +1 -0
- package/dist/bootstrap/reader.mjs +259 -0
- package/dist/bootstrap/reader.mjs.map +1 -0
- package/dist/bootstrap/source-map.mjs +154 -0
- package/dist/bootstrap/source-map.mjs.map +1 -0
- package/dist/bootstrap/symbol.mjs +74 -0
- package/dist/bootstrap/symbol.mjs.map +1 -0
- package/dist/bootstrap/syntax.mjs +38 -0
- package/dist/bootstrap/syntax.mjs.map +1 -0
- package/dist/bootstrap/transient-analysis.mjs +660 -0
- package/dist/bootstrap/transient-analysis.mjs.map +1 -0
- package/dist/browser/compiler.js +6491 -0
- package/editor/README.md +83 -0
- package/editor/eliscript-mode.el +878 -0
- package/editor/eliscript-repl.el +501 -0
- package/package.json +163 -0
- package/platform/README.md +48 -0
- package/platform/browser.mjs +204 -0
- package/platform/worker.mjs +166 -0
- package/runtime/README.md +296 -0
- package/runtime/core/collection-internals.mjs +512 -0
- package/runtime/core/collection.mjs +645 -0
- package/runtime/core/data-text.mjs +532 -0
- package/runtime/core/data.mjs +268 -0
- package/runtime/core/identifier.mjs +314 -0
- package/runtime/core/lazy-sequence.mjs +244 -0
- package/runtime/core/list-internals.mjs +18 -0
- package/runtime/core/list.mjs +273 -0
- package/runtime/core/map-internals.mjs +793 -0
- package/runtime/core/map.mjs +394 -0
- package/runtime/core/metadata-internals.mjs +14 -0
- package/runtime/core/metadata.mjs +44 -0
- package/runtime/core/object-impl.mjs +65 -0
- package/runtime/core/object-impl.mjs.map +1 -0
- package/runtime/core/object.mjs +13 -0
- package/runtime/core/order.mjs +234 -0
- package/runtime/core/persistent-kind.mjs +43 -0
- package/runtime/core/protocol-error.mjs +36 -0
- package/runtime/core/protocol-impl.mjs +326 -0
- package/runtime/core/protocol-impl.mjs.map +1 -0
- package/runtime/core/protocol.mjs +14 -0
- package/runtime/core/queue-internals.mjs +27 -0
- package/runtime/core/queue.mjs +238 -0
- package/runtime/core/record.mjs +346 -0
- package/runtime/core/regex.mjs +184 -0
- package/runtime/core/sequence.mjs +632 -0
- package/runtime/core/set-algebra.mjs +287 -0
- package/runtime/core/set-internals.mjs +33 -0
- package/runtime/core/set.mjs +378 -0
- package/runtime/core/sorted-internals.mjs +215 -0
- package/runtime/core/sorted.mjs +598 -0
- package/runtime/core/text-impl.mjs +95 -0
- package/runtime/core/text-impl.mjs.map +1 -0
- package/runtime/core/text.mjs +15 -0
- package/runtime/core/transducer.mjs +668 -0
- package/runtime/core/transient-internals.mjs +38 -0
- package/runtime/core/transient.mjs +53 -0
- package/runtime/core/truth.mjs +3 -0
- package/runtime/core/type.mjs +262 -0
- package/runtime/core/value-internals.mjs +188 -0
- package/runtime/core/value.mjs +93 -0
- package/runtime/core/vector-internals.mjs +102 -0
- package/runtime/core/vector.mjs +951 -0
- package/runtime/literals.mjs +34 -0
- package/runtime/source-mapping.mjs +220 -0
- package/runtime/testing/list.mjs +52 -0
- package/runtime/testing/map.mjs +145 -0
- package/runtime/testing/queue.mjs +49 -0
- package/runtime/testing/set.mjs +61 -0
- package/runtime/testing/sorted.mjs +105 -0
- package/runtime/testing/value.mjs +17 -0
- package/runtime/testing/vector.mjs +102 -0
- package/runtime/worker-value-codec.mjs +565 -0
- package/runtime/worker-value-stream.mjs +786 -0
- package/runtime/worker.mjs +975 -0
- package/stdlib/README.md +774 -0
- package/stdlib/bit.eli +36 -0
- package/stdlib/core/collection.eli +134 -0
- package/stdlib/core/data-text.eli +16 -0
- package/stdlib/core/data.eli +306 -0
- package/stdlib/core/identifier.eli +32 -0
- package/stdlib/core/lazy-sequence.eli +63 -0
- package/stdlib/core/metadata.eli +20 -0
- package/stdlib/core/object.eli +89 -0
- package/stdlib/core/order.eli +133 -0
- package/stdlib/core/protocol.eli +387 -0
- package/stdlib/core/record.eli +16 -0
- package/stdlib/core/regex.eli +17 -0
- package/stdlib/core/seq.eli +701 -0
- package/stdlib/core/set.eli +312 -0
- package/stdlib/core/sorted.eli +53 -0
- package/stdlib/core/text.eli +103 -0
- package/stdlib/core/transducer.eli +59 -0
- package/stdlib/core/transient.eli +24 -0
- package/stdlib/core/type.eli +19 -0
- package/stdlib/core/walk.eli +195 -0
- package/stdlib/core/zipper.eli +508 -0
- package/stdlib/data-text.eli +1092 -0
- package/stdlib/data.eli +42 -0
- package/stdlib/deferred.eli +70 -0
- package/stdlib/function.eli +120 -0
- package/stdlib/hierarchy.eli +245 -0
- package/stdlib/identifier.eli +147 -0
- package/stdlib/interop/js.eli +616 -0
- package/stdlib/json.eli +870 -0
- package/stdlib/metadata.eli +56 -0
- package/stdlib/multimethod.eli +464 -0
- package/stdlib/numeric.eli +193 -0
- package/stdlib/object.eli +96 -0
- package/stdlib/persistent-list.eli +145 -0
- package/stdlib/persistent-map.eli +724 -0
- package/stdlib/persistent-queue.eli +32 -0
- package/stdlib/persistent-set.eli +236 -0
- package/stdlib/persistent-vector.eli +342 -0
- package/stdlib/result.eli +141 -0
- package/stdlib/sequence.eli +106 -0
- package/stdlib/state/atom.eli +265 -0
- package/stdlib/text.eli +88 -0
- package/stdlib/value.eli +325 -0
|
@@ -0,0 +1,532 @@
|
|
|
1
|
+
import {
|
|
2
|
+
EliscriptSymbol,
|
|
3
|
+
Keyword,
|
|
4
|
+
eliscriptSymbol,
|
|
5
|
+
identifierName,
|
|
6
|
+
identifierNamespace,
|
|
7
|
+
keyword,
|
|
8
|
+
qualifiedIdentifierName,
|
|
9
|
+
} from "./identifier.mjs";
|
|
10
|
+
import {
|
|
11
|
+
EMPTY_MAP,
|
|
12
|
+
PersistentHashMap,
|
|
13
|
+
isPersistentHashMap,
|
|
14
|
+
} from "./map.mjs";
|
|
15
|
+
import { PersistentList } from "./list.mjs";
|
|
16
|
+
import { meta, withMeta } from "./metadata.mjs";
|
|
17
|
+
import { PersistentQueue, persistentQueue } from "./queue.mjs";
|
|
18
|
+
import {
|
|
19
|
+
defineProtocol,
|
|
20
|
+
extendProtocolCategory,
|
|
21
|
+
extendProtocolType,
|
|
22
|
+
protocolMethod,
|
|
23
|
+
} from "./protocol.mjs";
|
|
24
|
+
import {
|
|
25
|
+
EMPTY_SET,
|
|
26
|
+
PersistentHashSet,
|
|
27
|
+
} from "./set.mjs";
|
|
28
|
+
import { PersistentSortedMap, PersistentSortedSet } from "./sorted.mjs";
|
|
29
|
+
import {
|
|
30
|
+
assocBang,
|
|
31
|
+
conjBang,
|
|
32
|
+
persistentBang,
|
|
33
|
+
transient,
|
|
34
|
+
} from "./transient.mjs";
|
|
35
|
+
import {
|
|
36
|
+
EMPTY_VECTOR,
|
|
37
|
+
PersistentVector,
|
|
38
|
+
isPersistentVector,
|
|
39
|
+
} from "./vector.mjs";
|
|
40
|
+
|
|
41
|
+
const DEFAULT_MAX_DEPTH = 256;
|
|
42
|
+
const DEFAULT_MAX_LENGTH = 16 * 1024 * 1024;
|
|
43
|
+
const DEFAULT_MAX_VALUES = 1_000_000;
|
|
44
|
+
const NUMBER_PATTERN = /^-?(?:0|[1-9]\d*)(?:\.\d+)?(?:[eE][+-]?\d+)?$/;
|
|
45
|
+
const BIGINT_PATTERN = /^-?(?:0|[1-9]\d*)N$/;
|
|
46
|
+
const UNSAFE_IDENTIFIER = /[\s()[\]{}"';,@^`~\\]/u;
|
|
47
|
+
const RESERVED_SYMBOLS = new Set([
|
|
48
|
+
"nil",
|
|
49
|
+
"true",
|
|
50
|
+
"false",
|
|
51
|
+
"undefined",
|
|
52
|
+
"##NaN",
|
|
53
|
+
"##Inf",
|
|
54
|
+
"##-Inf",
|
|
55
|
+
]);
|
|
56
|
+
|
|
57
|
+
export const IPrint = defineProtocol("IPrint", ["print"]);
|
|
58
|
+
const dispatchPrint = protocolMethod(IPrint, "print");
|
|
59
|
+
|
|
60
|
+
function positiveIntegerOption(options, name, fallback) {
|
|
61
|
+
const value = options?.[name] ?? fallback;
|
|
62
|
+
if (!Number.isSafeInteger(value) || value <= 0) {
|
|
63
|
+
throw new TypeError(`${name} must be a positive safe integer`);
|
|
64
|
+
}
|
|
65
|
+
return value;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function normalizeOptions(options) {
|
|
69
|
+
if (options === undefined || options === null) {
|
|
70
|
+
return Object.freeze({
|
|
71
|
+
maxDepth: DEFAULT_MAX_DEPTH,
|
|
72
|
+
maxLength: DEFAULT_MAX_LENGTH,
|
|
73
|
+
maxValues: DEFAULT_MAX_VALUES,
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
if (typeof options !== "object" || Array.isArray(options)) {
|
|
77
|
+
throw new TypeError("data text options must be an object");
|
|
78
|
+
}
|
|
79
|
+
return Object.freeze({
|
|
80
|
+
maxDepth: positiveIntegerOption(options, "maxDepth", DEFAULT_MAX_DEPTH),
|
|
81
|
+
maxLength: positiveIntegerOption(options, "maxLength", DEFAULT_MAX_LENGTH),
|
|
82
|
+
maxValues: positiveIntegerOption(options, "maxValues", DEFAULT_MAX_VALUES),
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function compareText(left, right) {
|
|
87
|
+
return left < right ? -1 : left > right ? 1 : 0;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function safeIdentifierPart(value) {
|
|
91
|
+
return value.length > 0 && !value.includes("/") &&
|
|
92
|
+
!UNSAFE_IDENTIFIER.test(value);
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function safeBareIdentifier(value, symbol) {
|
|
96
|
+
const namespace = identifierNamespace(value);
|
|
97
|
+
const name = identifierName(value);
|
|
98
|
+
if (!safeIdentifierPart(name) ||
|
|
99
|
+
(namespace !== null && !safeIdentifierPart(namespace))) {
|
|
100
|
+
return false;
|
|
101
|
+
}
|
|
102
|
+
const qualified = qualifiedIdentifierName(value);
|
|
103
|
+
return !symbol ||
|
|
104
|
+
(!RESERVED_SYMBOLS.has(qualified) &&
|
|
105
|
+
!NUMBER_PATTERN.test(qualified) &&
|
|
106
|
+
!BIGINT_PATTERN.test(qualified) &&
|
|
107
|
+
!qualified.startsWith(":") &&
|
|
108
|
+
!qualified.startsWith("#"));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
function taggedIdentifier(tag, value) {
|
|
112
|
+
const namespace = identifierNamespace(value);
|
|
113
|
+
return `#eliscript/${tag} [${namespace === null
|
|
114
|
+
? "nil"
|
|
115
|
+
: JSON.stringify(namespace)} ${JSON.stringify(identifierName(value))}]`;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
function printNumber(value) {
|
|
119
|
+
if (Number.isNaN(value)) return "##NaN";
|
|
120
|
+
if (value === Number.POSITIVE_INFINITY) return "##Inf";
|
|
121
|
+
if (value === Number.NEGATIVE_INFINITY) return "##-Inf";
|
|
122
|
+
return Object.is(value, -0) ? "0" : String(value);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
function printKeyword(value) {
|
|
126
|
+
return safeBareIdentifier(value, false)
|
|
127
|
+
? `:${qualifiedIdentifierName(value)}`
|
|
128
|
+
: taggedIdentifier("keyword", value);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function printSymbol(value) {
|
|
132
|
+
return safeBareIdentifier(value, true)
|
|
133
|
+
? qualifiedIdentifierName(value)
|
|
134
|
+
: taggedIdentifier("symbol", value);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function printVector(value, context) {
|
|
138
|
+
return `[${[...value].map((item) => context.print(item)).join(" ")}]`;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function printList(value, context) {
|
|
142
|
+
return `(${[...value].map((item) => context.print(item)).join(" ")})`;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function printMap(value, context) {
|
|
146
|
+
const entries = [...value].map(([key, item]) => ({
|
|
147
|
+
key: context.print(key),
|
|
148
|
+
value: context.print(item),
|
|
149
|
+
}));
|
|
150
|
+
entries.sort((left, right) =>
|
|
151
|
+
compareText(left.key, right.key) || compareText(left.value, right.value));
|
|
152
|
+
return `{${entries.map(({ key, value: item }) => `${key} ${item}`).join(" ")}}`;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function printSet(value, context) {
|
|
156
|
+
const items = [...value].map((item) => context.print(item));
|
|
157
|
+
items.sort(compareText);
|
|
158
|
+
return `#{${items.join(" ")}}`;
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
function printQueue(value, context) {
|
|
162
|
+
return `#queue [${[...value].map((item) => context.print(item)).join(" ")}]`;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
extendProtocolCategory(IPrint, "null", { print: () => "nil" });
|
|
166
|
+
extendProtocolCategory(IPrint, "undefined", { print: () => "undefined" });
|
|
167
|
+
extendProtocolCategory(IPrint, "boolean", {
|
|
168
|
+
print: (value) => value ? "true" : "false",
|
|
169
|
+
});
|
|
170
|
+
extendProtocolCategory(IPrint, "number", { print: printNumber });
|
|
171
|
+
extendProtocolCategory(IPrint, "bigint", {
|
|
172
|
+
print: (value) => `${value}N`,
|
|
173
|
+
});
|
|
174
|
+
extendProtocolCategory(IPrint, "string", {
|
|
175
|
+
print: (value) => JSON.stringify(value),
|
|
176
|
+
});
|
|
177
|
+
extendProtocolType(IPrint, Keyword, { print: printKeyword });
|
|
178
|
+
extendProtocolType(IPrint, EliscriptSymbol, { print: printSymbol });
|
|
179
|
+
extendProtocolType(IPrint, PersistentList, { print: printList });
|
|
180
|
+
extendProtocolType(IPrint, PersistentVector, { print: printVector });
|
|
181
|
+
extendProtocolType(IPrint, PersistentHashMap, { print: printMap });
|
|
182
|
+
extendProtocolType(IPrint, PersistentHashSet, { print: printSet });
|
|
183
|
+
extendProtocolType(IPrint, PersistentSortedMap, { print: printMap });
|
|
184
|
+
extendProtocolType(IPrint, PersistentSortedSet, { print: printSet });
|
|
185
|
+
extendProtocolType(IPrint, PersistentQueue, { print: printQueue });
|
|
186
|
+
|
|
187
|
+
function makePrintContext(options) {
|
|
188
|
+
const state = { values: 0 };
|
|
189
|
+
|
|
190
|
+
function render(value, depth) {
|
|
191
|
+
if (depth > options.maxDepth) {
|
|
192
|
+
throw new RangeError(`data text exceeds maxDepth ${options.maxDepth}`);
|
|
193
|
+
}
|
|
194
|
+
state.values += 1;
|
|
195
|
+
if (state.values > options.maxValues) {
|
|
196
|
+
throw new RangeError(`data text exceeds maxValues ${options.maxValues}`);
|
|
197
|
+
}
|
|
198
|
+
const context = Object.freeze({
|
|
199
|
+
print: (item) => render(item, depth + 1),
|
|
200
|
+
});
|
|
201
|
+
const body = dispatchPrint(value, context);
|
|
202
|
+
if (typeof body !== "string") {
|
|
203
|
+
throw new TypeError("IPrint/print must return a string");
|
|
204
|
+
}
|
|
205
|
+
const metadata = meta(value);
|
|
206
|
+
const result = metadata === null
|
|
207
|
+
? body
|
|
208
|
+
: `^${render(metadata, depth + 1)} ${body}`;
|
|
209
|
+
if (result.length > options.maxLength) {
|
|
210
|
+
throw new RangeError(`data text exceeds maxLength ${options.maxLength}`);
|
|
211
|
+
}
|
|
212
|
+
return result;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
return (value) => render(value, 0);
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export function printValue(value, options = undefined) {
|
|
219
|
+
return makePrintContext(normalizeOptions(options))(value);
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
export class DataTextError extends SyntaxError {
|
|
223
|
+
constructor(message, source, position) {
|
|
224
|
+
super(`${message} at ${position.line}:${position.column}`);
|
|
225
|
+
this.name = "DataTextError";
|
|
226
|
+
this.code = "ELI-DATA-TEXT";
|
|
227
|
+
this.offset = position.index;
|
|
228
|
+
this.line = position.line;
|
|
229
|
+
this.column = position.column;
|
|
230
|
+
this.sourceLength = source.length;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
class Reader {
|
|
235
|
+
constructor(source, options) {
|
|
236
|
+
this.source = source;
|
|
237
|
+
this.options = options;
|
|
238
|
+
this.index = 0;
|
|
239
|
+
this.line = 1;
|
|
240
|
+
this.column = 1;
|
|
241
|
+
this.values = 0;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
position() {
|
|
245
|
+
return { index: this.index, line: this.line, column: this.column };
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
fail(message, position = this.position()) {
|
|
249
|
+
throw new DataTextError(message, this.source, position);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
peek(ahead = 0) {
|
|
253
|
+
return this.source[this.index + ahead];
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
advance() {
|
|
257
|
+
const code = this.source.codePointAt(this.index);
|
|
258
|
+
if (code === undefined) return undefined;
|
|
259
|
+
this.index += code > 0xffff ? 2 : 1;
|
|
260
|
+
if (code === 10) {
|
|
261
|
+
this.line += 1;
|
|
262
|
+
this.column = 1;
|
|
263
|
+
} else {
|
|
264
|
+
this.column += 1;
|
|
265
|
+
}
|
|
266
|
+
return code;
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
skipIgnored() {
|
|
270
|
+
while (this.index < this.source.length) {
|
|
271
|
+
const character = this.peek();
|
|
272
|
+
if (/\s/u.test(character) || character === ",") {
|
|
273
|
+
this.advance();
|
|
274
|
+
} else if (character === ";") {
|
|
275
|
+
while (this.index < this.source.length && this.peek() !== "\n") {
|
|
276
|
+
this.advance();
|
|
277
|
+
}
|
|
278
|
+
} else {
|
|
279
|
+
return;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
delimiter(character) {
|
|
285
|
+
return character === undefined || /[\s,()[\]{}";^]/u.test(character);
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
token(start) {
|
|
289
|
+
while (!this.delimiter(this.peek())) this.advance();
|
|
290
|
+
const token = this.source.slice(start.index, this.index);
|
|
291
|
+
if (token.length === 0) this.fail("expected token", start);
|
|
292
|
+
return token;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
string(start) {
|
|
296
|
+
this.advance();
|
|
297
|
+
let escaped = false;
|
|
298
|
+
while (this.index < this.source.length) {
|
|
299
|
+
const character = this.peek();
|
|
300
|
+
if (!escaped && character === "\n") {
|
|
301
|
+
this.fail("newline in string literal", start);
|
|
302
|
+
}
|
|
303
|
+
this.advance();
|
|
304
|
+
if (!escaped && character === "\"") {
|
|
305
|
+
const raw = this.source.slice(start.index, this.index);
|
|
306
|
+
try {
|
|
307
|
+
return JSON.parse(raw);
|
|
308
|
+
} catch {
|
|
309
|
+
this.fail("invalid string escape", start);
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
escaped = !escaped && character === "\\";
|
|
313
|
+
if (character !== "\\") escaped = false;
|
|
314
|
+
}
|
|
315
|
+
this.fail("unexpected end of string", start);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
vector(depth, start) {
|
|
319
|
+
this.advance();
|
|
320
|
+
const editable = transient(EMPTY_VECTOR);
|
|
321
|
+
while (true) {
|
|
322
|
+
this.skipIgnored();
|
|
323
|
+
if (this.peek() === "]") {
|
|
324
|
+
this.advance();
|
|
325
|
+
return persistentBang(editable);
|
|
326
|
+
}
|
|
327
|
+
if (this.peek() === undefined) {
|
|
328
|
+
this.fail("unexpected end of vector", start);
|
|
329
|
+
}
|
|
330
|
+
conjBang(editable, this.value(depth + 1));
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
list(depth, start) {
|
|
335
|
+
this.advance();
|
|
336
|
+
const values = [];
|
|
337
|
+
while (true) {
|
|
338
|
+
this.skipIgnored();
|
|
339
|
+
if (this.peek() === ")") {
|
|
340
|
+
this.advance();
|
|
341
|
+
return PersistentList.from(values);
|
|
342
|
+
}
|
|
343
|
+
if (this.peek() === undefined) {
|
|
344
|
+
this.fail("unexpected end of list", start);
|
|
345
|
+
}
|
|
346
|
+
values.push(this.value(depth + 1));
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
map(depth, start) {
|
|
351
|
+
this.advance();
|
|
352
|
+
const editable = transient(EMPTY_MAP);
|
|
353
|
+
let seen = EMPTY_MAP;
|
|
354
|
+
while (true) {
|
|
355
|
+
this.skipIgnored();
|
|
356
|
+
if (this.peek() === "}") {
|
|
357
|
+
this.advance();
|
|
358
|
+
return persistentBang(editable);
|
|
359
|
+
}
|
|
360
|
+
if (this.peek() === undefined) {
|
|
361
|
+
this.fail("unexpected end of map", start);
|
|
362
|
+
}
|
|
363
|
+
const keyPosition = this.position();
|
|
364
|
+
const key = this.value(depth + 1);
|
|
365
|
+
this.skipIgnored();
|
|
366
|
+
if (this.peek() === "}" || this.peek() === undefined) {
|
|
367
|
+
this.fail("map requires a value for every key", keyPosition);
|
|
368
|
+
}
|
|
369
|
+
const item = this.value(depth + 1);
|
|
370
|
+
if (seen.has(key)) this.fail("duplicate map key", keyPosition);
|
|
371
|
+
seen = seen.assoc(key, true);
|
|
372
|
+
assocBang(editable, key, item);
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
set(depth, start) {
|
|
377
|
+
this.advance();
|
|
378
|
+
const editable = transient(EMPTY_SET);
|
|
379
|
+
let seen = EMPTY_SET;
|
|
380
|
+
while (true) {
|
|
381
|
+
this.skipIgnored();
|
|
382
|
+
if (this.peek() === "}") {
|
|
383
|
+
this.advance();
|
|
384
|
+
return persistentBang(editable);
|
|
385
|
+
}
|
|
386
|
+
if (this.peek() === undefined) {
|
|
387
|
+
this.fail("unexpected end of set", start);
|
|
388
|
+
}
|
|
389
|
+
const position = this.position();
|
|
390
|
+
const item = this.value(depth + 1);
|
|
391
|
+
if (seen.has(item)) this.fail("duplicate set value", position);
|
|
392
|
+
seen = seen.conj(item);
|
|
393
|
+
conjBang(editable, item);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
taggedIdentifier(tag, depth, start) {
|
|
398
|
+
this.skipIgnored();
|
|
399
|
+
const payload = this.value(depth + 1);
|
|
400
|
+
if (!isPersistentVector(payload) || payload.count !== 2) {
|
|
401
|
+
this.fail(`${tag} tag requires [namespace name]`, start);
|
|
402
|
+
}
|
|
403
|
+
const namespace = payload.nth(0);
|
|
404
|
+
const name = payload.nth(1);
|
|
405
|
+
if ((namespace !== null && typeof namespace !== "string") ||
|
|
406
|
+
typeof name !== "string") {
|
|
407
|
+
this.fail(`${tag} tag requires a nil/string namespace and string name`, start);
|
|
408
|
+
}
|
|
409
|
+
try {
|
|
410
|
+
return tag === "keyword"
|
|
411
|
+
? keyword(namespace, name)
|
|
412
|
+
: eliscriptSymbol(namespace, name);
|
|
413
|
+
} catch (error) {
|
|
414
|
+
this.fail(error.message, start);
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
taggedQueue(depth, start) {
|
|
419
|
+
this.skipIgnored();
|
|
420
|
+
const payload = this.value(depth + 1);
|
|
421
|
+
if (!isPersistentVector(payload)) {
|
|
422
|
+
this.fail("queue tag requires a vector", start);
|
|
423
|
+
}
|
|
424
|
+
return persistentQueue(...payload);
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
dispatch(depth, start) {
|
|
428
|
+
if (this.peek(1) === "{") {
|
|
429
|
+
this.advance();
|
|
430
|
+
return this.set(depth, start);
|
|
431
|
+
}
|
|
432
|
+
const token = this.token(start);
|
|
433
|
+
if (token === "##NaN") return Number.NaN;
|
|
434
|
+
if (token === "##Inf") return Number.POSITIVE_INFINITY;
|
|
435
|
+
if (token === "##-Inf") return Number.NEGATIVE_INFINITY;
|
|
436
|
+
if (token === "#eliscript/keyword") {
|
|
437
|
+
return this.taggedIdentifier("keyword", depth, start);
|
|
438
|
+
}
|
|
439
|
+
if (token === "#eliscript/symbol") {
|
|
440
|
+
return this.taggedIdentifier("symbol", depth, start);
|
|
441
|
+
}
|
|
442
|
+
if (token === "#queue") return this.taggedQueue(depth, start);
|
|
443
|
+
this.fail(`unknown dispatch ${token}`, start);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
atom(start) {
|
|
447
|
+
const token = this.token(start);
|
|
448
|
+
if (token === "nil") return null;
|
|
449
|
+
if (token === "true") return true;
|
|
450
|
+
if (token === "false") return false;
|
|
451
|
+
if (token === "undefined") return undefined;
|
|
452
|
+
if (BIGINT_PATTERN.test(token)) return BigInt(token.slice(0, -1));
|
|
453
|
+
if (NUMBER_PATTERN.test(token)) return Number(token);
|
|
454
|
+
try {
|
|
455
|
+
return token.startsWith(":")
|
|
456
|
+
? keyword(token.slice(1))
|
|
457
|
+
: eliscriptSymbol(token);
|
|
458
|
+
} catch (error) {
|
|
459
|
+
this.fail(error.message, start);
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
metadata(depth, start) {
|
|
464
|
+
this.advance();
|
|
465
|
+
const metadata = this.value(depth + 1);
|
|
466
|
+
if (!isPersistentHashMap(metadata)) {
|
|
467
|
+
this.fail("metadata prefix requires a persistent map", start);
|
|
468
|
+
}
|
|
469
|
+
const value = this.value(depth + 1);
|
|
470
|
+
try {
|
|
471
|
+
return withMeta(value, metadata);
|
|
472
|
+
} catch (error) {
|
|
473
|
+
this.fail(error.message, start);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
value(depth) {
|
|
478
|
+
this.skipIgnored();
|
|
479
|
+
const start = this.position();
|
|
480
|
+
if (depth > this.options.maxDepth) {
|
|
481
|
+
this.fail(`data text exceeds maxDepth ${this.options.maxDepth}`, start);
|
|
482
|
+
}
|
|
483
|
+
this.values += 1;
|
|
484
|
+
if (this.values > this.options.maxValues) {
|
|
485
|
+
this.fail(`data text exceeds maxValues ${this.options.maxValues}`, start);
|
|
486
|
+
}
|
|
487
|
+
const character = this.peek();
|
|
488
|
+
if (character === undefined) this.fail("unexpected end of input", start);
|
|
489
|
+
if (character === "\"") return this.string(start);
|
|
490
|
+
if (character === "(") return this.list(depth, start);
|
|
491
|
+
if (character === "[") return this.vector(depth, start);
|
|
492
|
+
if (character === "{") return this.map(depth, start);
|
|
493
|
+
if (character === "#") return this.dispatch(depth, start);
|
|
494
|
+
if (character === "^") return this.metadata(depth, start);
|
|
495
|
+
if ("]})".includes(character)) {
|
|
496
|
+
this.fail(`unexpected closing delimiter ${character}`, start);
|
|
497
|
+
}
|
|
498
|
+
return this.atom(start);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
|
|
502
|
+
function makeReader(source, options) {
|
|
503
|
+
if (typeof source !== "string") {
|
|
504
|
+
throw new TypeError("data text source must be a string");
|
|
505
|
+
}
|
|
506
|
+
const normalized = normalizeOptions(options);
|
|
507
|
+
if (source.length > normalized.maxLength) {
|
|
508
|
+
throw new RangeError(`data text exceeds maxLength ${normalized.maxLength}`);
|
|
509
|
+
}
|
|
510
|
+
return new Reader(source, normalized);
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
export function readValue(source, options = undefined) {
|
|
514
|
+
const reader = makeReader(source, options);
|
|
515
|
+
const value = reader.value(0);
|
|
516
|
+
reader.skipIgnored();
|
|
517
|
+
if (reader.index !== source.length) {
|
|
518
|
+
reader.fail("trailing data after value");
|
|
519
|
+
}
|
|
520
|
+
return value;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
export function readValues(source, options = undefined) {
|
|
524
|
+
const reader = makeReader(source, options);
|
|
525
|
+
const values = [];
|
|
526
|
+
reader.skipIgnored();
|
|
527
|
+
while (reader.index < source.length) {
|
|
528
|
+
values.push(reader.value(0));
|
|
529
|
+
reader.skipIgnored();
|
|
530
|
+
}
|
|
531
|
+
return Object.freeze(values);
|
|
532
|
+
}
|