@optique/core 1.0.0-dev.1611 → 1.0.0-dev.1658

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.
@@ -0,0 +1,56 @@
1
+
2
+ //#region src/input-trace.ts
3
+ const symbolKeys = /* @__PURE__ */ new WeakMap();
4
+ let symbolCounter = 0;
5
+ /** Length-prefix a raw segment so that no delimiter escaping is needed. */
6
+ function lengthPrefix(s) {
7
+ return `${s.length}:${s}`;
8
+ }
9
+ function serializeKey(key) {
10
+ if (typeof key === "string") return lengthPrefix(`s${key}`);
11
+ if (typeof key === "number") return lengthPrefix(`n${key}`);
12
+ const registeredKey = Symbol.keyFor(key);
13
+ if (registeredKey !== void 0) return lengthPrefix(`r${registeredKey}`);
14
+ let id = symbolKeys.get(key);
15
+ if (id === void 0) {
16
+ id = `y${symbolCounter++}`;
17
+ symbolKeys.set(key, id);
18
+ }
19
+ return lengthPrefix(id);
20
+ }
21
+ function serializePath(path) {
22
+ if (path.length === 0) return "";
23
+ return path.map(serializeKey).join("");
24
+ }
25
+ var InputTraceImpl = class InputTraceImpl {
26
+ #entries;
27
+ constructor(entries) {
28
+ this.#entries = entries ?? /* @__PURE__ */ new Map();
29
+ }
30
+ get(path) {
31
+ return this.#entries.get(serializePath(path));
32
+ }
33
+ set(path, entry) {
34
+ const copy = new Map(this.#entries);
35
+ copy.set(serializePath(path), entry);
36
+ return new InputTraceImpl(copy);
37
+ }
38
+ delete(path) {
39
+ const copy = new Map(this.#entries);
40
+ copy.delete(serializePath(path));
41
+ return new InputTraceImpl(copy);
42
+ }
43
+ };
44
+ /**
45
+ * Creates a new empty {@link InputTrace}.
46
+ *
47
+ * @returns An empty immutable input trace.
48
+ * @internal
49
+ * @since 1.0.0
50
+ */
51
+ function createInputTrace() {
52
+ return new InputTraceImpl();
53
+ }
54
+
55
+ //#endregion
56
+ exports.createInputTrace = createInputTrace;
@@ -0,0 +1,77 @@
1
+ import { ValueParserResult } from "./valueparser.cjs";
2
+
3
+ //#region src/input-trace.d.ts
4
+
5
+ /**
6
+ * A replayable record of raw input consumed by a single parser node.
7
+ *
8
+ * @internal
9
+ * @since 1.0.0
10
+ */
11
+ interface TraceEntry {
12
+ /** The kind of input that was consumed. */
13
+ readonly kind: "option-value" | "argument-value" | "literal" | "custom";
14
+ /** The raw input string to be replayed. */
15
+ readonly rawInput: string;
16
+ /** The tokens consumed from the buffer. */
17
+ readonly consumed: readonly string[];
18
+ /** The preliminary parse result using default dependency values. */
19
+ readonly preliminaryResult?: ValueParserResult<unknown>;
20
+ /**
21
+ * Snapshotted default dependency values captured during parse.
22
+ *
23
+ * Present when a derived parser snapshots default dependency values during
24
+ * parse, including both `derive()` and `deriveFrom()` parsers.
25
+ * This avoids re-evaluating dynamic default thunks during replay.
26
+ */
27
+ readonly defaultDependencyValues?: readonly unknown[];
28
+ /** The option names that matched (e.g., `["--env", "-e"]`). */
29
+ readonly optionNames?: readonly string[];
30
+ /** The metavar of the value parser. */
31
+ readonly metavar?: string;
32
+ }
33
+ /**
34
+ * An immutable, path-keyed store of {@link TraceEntry} records.
35
+ *
36
+ * Each entry is keyed by a path of `PropertyKey` segments (strings, numbers,
37
+ * or symbols) corresponding to the position of the parser in the parse tree.
38
+ *
39
+ * All mutation methods return a new `InputTrace` instance; the original is
40
+ * never modified.
41
+ *
42
+ * @internal
43
+ * @since 1.0.0
44
+ */
45
+ interface InputTrace {
46
+ /**
47
+ * Retrieves the trace entry at the given path.
48
+ *
49
+ * @param path The path segments identifying the entry.
50
+ * @returns The entry, or `undefined` if no entry exists at that path.
51
+ */
52
+ get(path: readonly PropertyKey[]): TraceEntry | undefined;
53
+ /**
54
+ * Returns a new trace with the entry set at the given path.
55
+ *
56
+ * @param path The path segments identifying the entry.
57
+ * @param entry The trace entry to store.
58
+ * @returns A new `InputTrace` with the entry stored.
59
+ */
60
+ set(path: readonly PropertyKey[], entry: TraceEntry): InputTrace;
61
+ /**
62
+ * Returns a new trace with the entry at the given path removed.
63
+ *
64
+ * @param path The path segments identifying the entry to remove.
65
+ * @returns A new `InputTrace` without the entry.
66
+ */
67
+ delete(path: readonly PropertyKey[]): InputTrace;
68
+ }
69
+ /**
70
+ * Creates a new empty {@link InputTrace}.
71
+ *
72
+ * @returns An empty immutable input trace.
73
+ * @internal
74
+ * @since 1.0.0
75
+ */
76
+ //#endregion
77
+ export { InputTrace };
@@ -0,0 +1,77 @@
1
+ import { ValueParserResult } from "./valueparser.js";
2
+
3
+ //#region src/input-trace.d.ts
4
+
5
+ /**
6
+ * A replayable record of raw input consumed by a single parser node.
7
+ *
8
+ * @internal
9
+ * @since 1.0.0
10
+ */
11
+ interface TraceEntry {
12
+ /** The kind of input that was consumed. */
13
+ readonly kind: "option-value" | "argument-value" | "literal" | "custom";
14
+ /** The raw input string to be replayed. */
15
+ readonly rawInput: string;
16
+ /** The tokens consumed from the buffer. */
17
+ readonly consumed: readonly string[];
18
+ /** The preliminary parse result using default dependency values. */
19
+ readonly preliminaryResult?: ValueParserResult<unknown>;
20
+ /**
21
+ * Snapshotted default dependency values captured during parse.
22
+ *
23
+ * Present when a derived parser snapshots default dependency values during
24
+ * parse, including both `derive()` and `deriveFrom()` parsers.
25
+ * This avoids re-evaluating dynamic default thunks during replay.
26
+ */
27
+ readonly defaultDependencyValues?: readonly unknown[];
28
+ /** The option names that matched (e.g., `["--env", "-e"]`). */
29
+ readonly optionNames?: readonly string[];
30
+ /** The metavar of the value parser. */
31
+ readonly metavar?: string;
32
+ }
33
+ /**
34
+ * An immutable, path-keyed store of {@link TraceEntry} records.
35
+ *
36
+ * Each entry is keyed by a path of `PropertyKey` segments (strings, numbers,
37
+ * or symbols) corresponding to the position of the parser in the parse tree.
38
+ *
39
+ * All mutation methods return a new `InputTrace` instance; the original is
40
+ * never modified.
41
+ *
42
+ * @internal
43
+ * @since 1.0.0
44
+ */
45
+ interface InputTrace {
46
+ /**
47
+ * Retrieves the trace entry at the given path.
48
+ *
49
+ * @param path The path segments identifying the entry.
50
+ * @returns The entry, or `undefined` if no entry exists at that path.
51
+ */
52
+ get(path: readonly PropertyKey[]): TraceEntry | undefined;
53
+ /**
54
+ * Returns a new trace with the entry set at the given path.
55
+ *
56
+ * @param path The path segments identifying the entry.
57
+ * @param entry The trace entry to store.
58
+ * @returns A new `InputTrace` with the entry stored.
59
+ */
60
+ set(path: readonly PropertyKey[], entry: TraceEntry): InputTrace;
61
+ /**
62
+ * Returns a new trace with the entry at the given path removed.
63
+ *
64
+ * @param path The path segments identifying the entry to remove.
65
+ * @returns A new `InputTrace` without the entry.
66
+ */
67
+ delete(path: readonly PropertyKey[]): InputTrace;
68
+ }
69
+ /**
70
+ * Creates a new empty {@link InputTrace}.
71
+ *
72
+ * @returns An empty immutable input trace.
73
+ * @internal
74
+ * @since 1.0.0
75
+ */
76
+ //#endregion
77
+ export { InputTrace };
@@ -0,0 +1,55 @@
1
+ //#region src/input-trace.ts
2
+ const symbolKeys = /* @__PURE__ */ new WeakMap();
3
+ let symbolCounter = 0;
4
+ /** Length-prefix a raw segment so that no delimiter escaping is needed. */
5
+ function lengthPrefix(s) {
6
+ return `${s.length}:${s}`;
7
+ }
8
+ function serializeKey(key) {
9
+ if (typeof key === "string") return lengthPrefix(`s${key}`);
10
+ if (typeof key === "number") return lengthPrefix(`n${key}`);
11
+ const registeredKey = Symbol.keyFor(key);
12
+ if (registeredKey !== void 0) return lengthPrefix(`r${registeredKey}`);
13
+ let id = symbolKeys.get(key);
14
+ if (id === void 0) {
15
+ id = `y${symbolCounter++}`;
16
+ symbolKeys.set(key, id);
17
+ }
18
+ return lengthPrefix(id);
19
+ }
20
+ function serializePath(path) {
21
+ if (path.length === 0) return "";
22
+ return path.map(serializeKey).join("");
23
+ }
24
+ var InputTraceImpl = class InputTraceImpl {
25
+ #entries;
26
+ constructor(entries) {
27
+ this.#entries = entries ?? /* @__PURE__ */ new Map();
28
+ }
29
+ get(path) {
30
+ return this.#entries.get(serializePath(path));
31
+ }
32
+ set(path, entry) {
33
+ const copy = new Map(this.#entries);
34
+ copy.set(serializePath(path), entry);
35
+ return new InputTraceImpl(copy);
36
+ }
37
+ delete(path) {
38
+ const copy = new Map(this.#entries);
39
+ copy.delete(serializePath(path));
40
+ return new InputTraceImpl(copy);
41
+ }
42
+ };
43
+ /**
44
+ * Creates a new empty {@link InputTrace}.
45
+ *
46
+ * @returns An empty immutable input trace.
47
+ * @internal
48
+ * @since 1.0.0
49
+ */
50
+ function createInputTrace() {
51
+ return new InputTraceImpl();
52
+ }
53
+
54
+ //#endregion
55
+ export { createInputTrace };