@optique/core 0.10.7 → 1.0.0-dev.1109

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.
Files changed (56) hide show
  1. package/README.md +4 -6
  2. package/dist/annotations.cjs +209 -1
  3. package/dist/annotations.d.cts +78 -1
  4. package/dist/annotations.d.ts +78 -1
  5. package/dist/annotations.js +201 -1
  6. package/dist/completion.cjs +186 -50
  7. package/dist/completion.js +186 -50
  8. package/dist/constructs.cjs +310 -78
  9. package/dist/constructs.d.cts +525 -644
  10. package/dist/constructs.d.ts +525 -644
  11. package/dist/constructs.js +311 -79
  12. package/dist/context.cjs +43 -3
  13. package/dist/context.d.cts +113 -5
  14. package/dist/context.d.ts +113 -5
  15. package/dist/context.js +41 -3
  16. package/dist/dependency.cjs +172 -66
  17. package/dist/dependency.d.cts +22 -2
  18. package/dist/dependency.d.ts +22 -2
  19. package/dist/dependency.js +172 -66
  20. package/dist/doc.cjs +46 -1
  21. package/dist/doc.d.cts +24 -0
  22. package/dist/doc.d.ts +24 -0
  23. package/dist/doc.js +46 -1
  24. package/dist/facade.cjs +702 -322
  25. package/dist/facade.d.cts +124 -190
  26. package/dist/facade.d.ts +124 -190
  27. package/dist/facade.js +703 -323
  28. package/dist/index.cjs +5 -0
  29. package/dist/index.d.cts +5 -5
  30. package/dist/index.d.ts +5 -5
  31. package/dist/index.js +3 -3
  32. package/dist/message.cjs +7 -4
  33. package/dist/message.js +7 -4
  34. package/dist/mode-dispatch.cjs +23 -1
  35. package/dist/mode-dispatch.d.cts +55 -0
  36. package/dist/mode-dispatch.d.ts +55 -0
  37. package/dist/mode-dispatch.js +21 -1
  38. package/dist/modifiers.cjs +210 -55
  39. package/dist/modifiers.js +211 -56
  40. package/dist/parser.cjs +80 -47
  41. package/dist/parser.d.cts +18 -3
  42. package/dist/parser.d.ts +18 -3
  43. package/dist/parser.js +82 -50
  44. package/dist/primitives.cjs +102 -37
  45. package/dist/primitives.d.cts +81 -24
  46. package/dist/primitives.d.ts +81 -24
  47. package/dist/primitives.js +103 -39
  48. package/dist/usage.cjs +88 -6
  49. package/dist/usage.d.cts +51 -13
  50. package/dist/usage.d.ts +51 -13
  51. package/dist/usage.js +85 -7
  52. package/dist/valueparser.cjs +371 -99
  53. package/dist/valueparser.d.cts +56 -7
  54. package/dist/valueparser.d.ts +56 -7
  55. package/dist/valueparser.js +371 -99
  56. package/package.json +10 -1
package/README.md CHANGED
@@ -1,9 +1,6 @@
1
1
  @optique/core
2
2
  =============
3
3
 
4
- > [!WARNING]
5
- > The API is stabilizing, but may change before the 1.0 release.
6
-
7
4
  The core package of Optique which provides the shared types and parser
8
5
  combinators. It is designed to be used in universal JavaScript runtimes,
9
6
  including Node.js, Deno, Bun, edge functions, and web browsers—although
@@ -38,7 +35,8 @@ Quick example
38
35
 
39
36
  ~~~~ typescript
40
37
  import { runParser } from "@optique/core/facade";
41
- import { object, option, argument } from "@optique/core/parser";
38
+ import { option, argument } from "@optique/core/primitives";
39
+ import { object } from "@optique/core/constructs";
42
40
  import { string, integer } from "@optique/core/valueparser";
43
41
  import process from "node:process";
44
42
 
@@ -49,8 +47,8 @@ const parser = object({
49
47
  });
50
48
 
51
49
  const config = runParser(parser, "myapp", process.argv.slice(2), {
52
- help: "both",
53
- onError: process.exit,
50
+ help: { command: true, option: true },
51
+ onError: (exitCode) => process.exit(exitCode),
54
52
  });
55
53
 
56
54
  console.log(`Hello ${config.name}!`);
@@ -16,6 +16,36 @@
16
16
  */
17
17
  const annotationKey = Symbol.for("@optique/core/parser/annotation");
18
18
  /**
19
+ * Internal marker attached during the first pass of `runWith()` so wrappers
20
+ * with side effects can defer work until dynamic contexts have resolved.
21
+ *
22
+ * @internal
23
+ */
24
+ const firstPassAnnotationKey = Symbol.for("@optique/core/parser/firstPass");
25
+ /**
26
+ * Internal key for preserving primitive parser state values when annotations
27
+ * are injected into non-object states.
28
+ * @internal
29
+ */
30
+ const annotationStateValueKey = Symbol.for("@optique/core/parser/annotationStateValue");
31
+ /**
32
+ * Internal marker key that indicates annotation wrapping was injected by
33
+ * Optique internals for non-object states.
34
+ * @internal
35
+ */
36
+ const annotationWrapperKey = Symbol.for("@optique/core/parser/annotationWrapper");
37
+ /**
38
+ * Internal symbol keys that define Optique's primitive-state annotation
39
+ * wrapper shape.
40
+ * @internal
41
+ */
42
+ const annotationWrapperKeys = new Set([
43
+ annotationKey,
44
+ annotationStateValueKey,
45
+ annotationWrapperKey
46
+ ]);
47
+ const injectedAnnotationWrappers = /* @__PURE__ */ new WeakSet();
48
+ /**
19
49
  * Extracts annotations from parser state.
20
50
  *
21
51
  * @param state Parser state that may contain annotations
@@ -35,7 +65,185 @@ function getAnnotations(state) {
35
65
  if (annotations != null && typeof annotations === "object") return annotations;
36
66
  return void 0;
37
67
  }
68
+ /**
69
+ * Reattaches annotations to a freshly created array state.
70
+ *
71
+ * Array spread copies elements but drops symbol properties, so parsers that
72
+ * rebuild array states need to copy annotations back explicitly.
73
+ *
74
+ * @param source The original state that may carry annotations.
75
+ * @param target The freshly created array state.
76
+ * @returns The target array, with annotations copied when available.
77
+ * @internal
78
+ */
79
+ function annotateFreshArray(source, target) {
80
+ const annotations = getAnnotations(source);
81
+ if (annotations === void 0) return target;
82
+ const annotated = target;
83
+ annotated[annotationKey] = annotations;
84
+ return annotated;
85
+ }
86
+ /**
87
+ * Propagates annotations from one parser state to another.
88
+ *
89
+ * This is mainly used by parsers that rebuild array states with spread syntax.
90
+ * Array spread copies elements but drops custom symbol properties, so we need
91
+ * to reattach annotations explicitly when present.
92
+ *
93
+ * @param source The original state that may carry annotations.
94
+ * @param target The new state to receive annotations.
95
+ * @returns The target state, with annotations copied when available.
96
+ * @internal
97
+ */
98
+ function inheritAnnotations(source, target) {
99
+ const annotations = getAnnotations(source);
100
+ if (annotations === void 0) return target;
101
+ if (target == null || typeof target !== "object") return injectAnnotations(target, annotations);
102
+ if (isInjectedAnnotationWrapper(target)) return injectAnnotations(target, annotations);
103
+ if (Array.isArray(target)) {
104
+ const cloned$1 = [...target];
105
+ cloned$1[annotationKey] = annotations;
106
+ return cloned$1;
107
+ }
108
+ if (target instanceof Date) {
109
+ const cloned$1 = new Date(target.getTime());
110
+ cloned$1[annotationKey] = annotations;
111
+ return cloned$1;
112
+ }
113
+ if (target instanceof Map) {
114
+ const cloned$1 = new Map(target);
115
+ cloned$1[annotationKey] = annotations;
116
+ return cloned$1;
117
+ }
118
+ if (target instanceof Set) {
119
+ const cloned$1 = new Set(target);
120
+ cloned$1[annotationKey] = annotations;
121
+ return cloned$1;
122
+ }
123
+ if (target instanceof RegExp) {
124
+ const cloned$1 = new RegExp(target);
125
+ cloned$1[annotationKey] = annotations;
126
+ return cloned$1;
127
+ }
128
+ if (Object.getPrototypeOf(target) !== Object.prototype && Object.getPrototypeOf(target) !== null) return target;
129
+ const cloned = Object.create(Object.getPrototypeOf(target), Object.getOwnPropertyDescriptors(target));
130
+ cloned[annotationKey] = annotations;
131
+ return cloned;
132
+ }
133
+ /**
134
+ * Injects annotations into parser state while preserving state shape.
135
+ *
136
+ * - Primitive, null, and undefined states are wrapped with internal metadata.
137
+ * - Array states are cloned and annotated without mutating the original.
138
+ * - Plain object states are shallow-cloned with annotations attached.
139
+ * - Built-in object states (Date/Map/Set/RegExp) are cloned by constructor.
140
+ * - Other non-plain object states are cloned via prototype/descriptors.
141
+ *
142
+ * @param state The parser state to annotate.
143
+ * @param annotations The annotations to inject.
144
+ * @returns Annotated state.
145
+ * @internal
146
+ */
147
+ function injectAnnotations(state, annotations) {
148
+ if (state == null || typeof state !== "object") {
149
+ const wrapper = {};
150
+ Object.defineProperties(wrapper, {
151
+ [annotationKey]: {
152
+ value: annotations,
153
+ enumerable: true,
154
+ writable: true,
155
+ configurable: true
156
+ },
157
+ [annotationStateValueKey]: {
158
+ value: state,
159
+ enumerable: false,
160
+ writable: true,
161
+ configurable: true
162
+ },
163
+ [annotationWrapperKey]: {
164
+ value: true,
165
+ enumerable: false,
166
+ writable: true,
167
+ configurable: true
168
+ }
169
+ });
170
+ injectedAnnotationWrappers.add(wrapper);
171
+ return wrapper;
172
+ }
173
+ if (Array.isArray(state)) {
174
+ const cloned$1 = [...state];
175
+ cloned$1[annotationKey] = annotations;
176
+ return cloned$1;
177
+ }
178
+ if (isInjectedAnnotationWrapper(state)) {
179
+ state[annotationKey] = annotations;
180
+ return state;
181
+ }
182
+ if (state instanceof Date) {
183
+ const cloned$1 = new Date(state.getTime());
184
+ cloned$1[annotationKey] = annotations;
185
+ return cloned$1;
186
+ }
187
+ if (state instanceof Map) {
188
+ const cloned$1 = new Map(state);
189
+ cloned$1[annotationKey] = annotations;
190
+ return cloned$1;
191
+ }
192
+ if (state instanceof Set) {
193
+ const cloned$1 = new Set(state);
194
+ cloned$1[annotationKey] = annotations;
195
+ return cloned$1;
196
+ }
197
+ if (state instanceof RegExp) {
198
+ const cloned$1 = new RegExp(state);
199
+ cloned$1[annotationKey] = annotations;
200
+ return cloned$1;
201
+ }
202
+ const proto = Object.getPrototypeOf(state);
203
+ if (proto === Object.prototype || proto === null) return {
204
+ ...state,
205
+ [annotationKey]: annotations
206
+ };
207
+ const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
208
+ cloned[annotationKey] = annotations;
209
+ return cloned;
210
+ }
211
+ /**
212
+ * Unwraps a primitive-state annotation wrapper injected by Optique internals.
213
+ *
214
+ * @param value Value to potentially unwrap.
215
+ * @returns The unwrapped primitive value when the input is an injected wrapper;
216
+ * otherwise the original value.
217
+ * @internal
218
+ */
219
+ function unwrapInjectedAnnotationWrapper(value) {
220
+ if (value == null || typeof value !== "object") return value;
221
+ const valueRecord = value;
222
+ if (valueRecord[annotationWrapperKey] !== true) return value;
223
+ const ownKeys = Reflect.ownKeys(valueRecord);
224
+ if (ownKeys.length === 3 && ownKeys.every((key) => annotationWrapperKeys.has(key)) && isInjectedAnnotationWrapper(value)) return valueRecord[annotationStateValueKey];
225
+ return value;
226
+ }
227
+ /**
228
+ * Returns whether the given value is an internal primitive-state annotation
229
+ * wrapper that was injected by Optique.
230
+ *
231
+ * @param value Value to check.
232
+ * @returns `true` if the value is an injected internal wrapper.
233
+ * @internal
234
+ */
235
+ function isInjectedAnnotationWrapper(value) {
236
+ return value != null && typeof value === "object" && injectedAnnotationWrappers.has(value);
237
+ }
38
238
 
39
239
  //#endregion
240
+ exports.annotateFreshArray = annotateFreshArray;
40
241
  exports.annotationKey = annotationKey;
41
- exports.getAnnotations = getAnnotations;
242
+ exports.annotationStateValueKey = annotationStateValueKey;
243
+ exports.annotationWrapperKey = annotationWrapperKey;
244
+ exports.firstPassAnnotationKey = firstPassAnnotationKey;
245
+ exports.getAnnotations = getAnnotations;
246
+ exports.inheritAnnotations = inheritAnnotations;
247
+ exports.injectAnnotations = injectAnnotations;
248
+ exports.isInjectedAnnotationWrapper = isInjectedAnnotationWrapper;
249
+ exports.unwrapInjectedAnnotationWrapper = unwrapInjectedAnnotationWrapper;
@@ -14,6 +14,25 @@
14
14
  * @since 0.10.0
15
15
  */
16
16
  declare const annotationKey: unique symbol;
17
+ /**
18
+ * Internal marker attached during the first pass of `runWith()` so wrappers
19
+ * with side effects can defer work until dynamic contexts have resolved.
20
+ *
21
+ * @internal
22
+ */
23
+ declare const firstPassAnnotationKey: unique symbol;
24
+ /**
25
+ * Internal key for preserving primitive parser state values when annotations
26
+ * are injected into non-object states.
27
+ * @internal
28
+ */
29
+ declare const annotationStateValueKey: unique symbol;
30
+ /**
31
+ * Internal marker key that indicates annotation wrapping was injected by
32
+ * Optique internals for non-object states.
33
+ * @internal
34
+ */
35
+ declare const annotationWrapperKey: unique symbol;
17
36
  /**
18
37
  * Annotations that can be passed to parsers during execution.
19
38
  * Allows external packages to provide additional data that parsers can access
@@ -56,5 +75,63 @@ interface ParseOptions {
56
75
  * ```
57
76
  */
58
77
  declare function getAnnotations(state: unknown): Annotations | undefined;
78
+ /**
79
+ * Reattaches annotations to a freshly created array state.
80
+ *
81
+ * Array spread copies elements but drops symbol properties, so parsers that
82
+ * rebuild array states need to copy annotations back explicitly.
83
+ *
84
+ * @param source The original state that may carry annotations.
85
+ * @param target The freshly created array state.
86
+ * @returns The target array, with annotations copied when available.
87
+ * @internal
88
+ */
89
+ declare function annotateFreshArray<T>(source: unknown, target: readonly T[]): readonly T[];
90
+ /**
91
+ * Propagates annotations from one parser state to another.
92
+ *
93
+ * This is mainly used by parsers that rebuild array states with spread syntax.
94
+ * Array spread copies elements but drops custom symbol properties, so we need
95
+ * to reattach annotations explicitly when present.
96
+ *
97
+ * @param source The original state that may carry annotations.
98
+ * @param target The new state to receive annotations.
99
+ * @returns The target state, with annotations copied when available.
100
+ * @internal
101
+ */
102
+ declare function inheritAnnotations<T>(source: unknown, target: T): T;
103
+ /**
104
+ * Injects annotations into parser state while preserving state shape.
105
+ *
106
+ * - Primitive, null, and undefined states are wrapped with internal metadata.
107
+ * - Array states are cloned and annotated without mutating the original.
108
+ * - Plain object states are shallow-cloned with annotations attached.
109
+ * - Built-in object states (Date/Map/Set/RegExp) are cloned by constructor.
110
+ * - Other non-plain object states are cloned via prototype/descriptors.
111
+ *
112
+ * @param state The parser state to annotate.
113
+ * @param annotations The annotations to inject.
114
+ * @returns Annotated state.
115
+ * @internal
116
+ */
117
+ declare function injectAnnotations<TState>(state: TState, annotations: Annotations): TState;
118
+ /**
119
+ * Unwraps a primitive-state annotation wrapper injected by Optique internals.
120
+ *
121
+ * @param value Value to potentially unwrap.
122
+ * @returns The unwrapped primitive value when the input is an injected wrapper;
123
+ * otherwise the original value.
124
+ * @internal
125
+ */
126
+ declare function unwrapInjectedAnnotationWrapper<T>(value: T): T;
127
+ /**
128
+ * Returns whether the given value is an internal primitive-state annotation
129
+ * wrapper that was injected by Optique.
130
+ *
131
+ * @param value Value to check.
132
+ * @returns `true` if the value is an injected internal wrapper.
133
+ * @internal
134
+ */
135
+ declare function isInjectedAnnotationWrapper(value: unknown): boolean;
59
136
  //#endregion
60
- export { Annotations, ParseOptions, annotationKey, getAnnotations };
137
+ export { Annotations, ParseOptions, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper };
@@ -14,6 +14,25 @@
14
14
  * @since 0.10.0
15
15
  */
16
16
  declare const annotationKey: unique symbol;
17
+ /**
18
+ * Internal marker attached during the first pass of `runWith()` so wrappers
19
+ * with side effects can defer work until dynamic contexts have resolved.
20
+ *
21
+ * @internal
22
+ */
23
+ declare const firstPassAnnotationKey: unique symbol;
24
+ /**
25
+ * Internal key for preserving primitive parser state values when annotations
26
+ * are injected into non-object states.
27
+ * @internal
28
+ */
29
+ declare const annotationStateValueKey: unique symbol;
30
+ /**
31
+ * Internal marker key that indicates annotation wrapping was injected by
32
+ * Optique internals for non-object states.
33
+ * @internal
34
+ */
35
+ declare const annotationWrapperKey: unique symbol;
17
36
  /**
18
37
  * Annotations that can be passed to parsers during execution.
19
38
  * Allows external packages to provide additional data that parsers can access
@@ -56,5 +75,63 @@ interface ParseOptions {
56
75
  * ```
57
76
  */
58
77
  declare function getAnnotations(state: unknown): Annotations | undefined;
78
+ /**
79
+ * Reattaches annotations to a freshly created array state.
80
+ *
81
+ * Array spread copies elements but drops symbol properties, so parsers that
82
+ * rebuild array states need to copy annotations back explicitly.
83
+ *
84
+ * @param source The original state that may carry annotations.
85
+ * @param target The freshly created array state.
86
+ * @returns The target array, with annotations copied when available.
87
+ * @internal
88
+ */
89
+ declare function annotateFreshArray<T>(source: unknown, target: readonly T[]): readonly T[];
90
+ /**
91
+ * Propagates annotations from one parser state to another.
92
+ *
93
+ * This is mainly used by parsers that rebuild array states with spread syntax.
94
+ * Array spread copies elements but drops custom symbol properties, so we need
95
+ * to reattach annotations explicitly when present.
96
+ *
97
+ * @param source The original state that may carry annotations.
98
+ * @param target The new state to receive annotations.
99
+ * @returns The target state, with annotations copied when available.
100
+ * @internal
101
+ */
102
+ declare function inheritAnnotations<T>(source: unknown, target: T): T;
103
+ /**
104
+ * Injects annotations into parser state while preserving state shape.
105
+ *
106
+ * - Primitive, null, and undefined states are wrapped with internal metadata.
107
+ * - Array states are cloned and annotated without mutating the original.
108
+ * - Plain object states are shallow-cloned with annotations attached.
109
+ * - Built-in object states (Date/Map/Set/RegExp) are cloned by constructor.
110
+ * - Other non-plain object states are cloned via prototype/descriptors.
111
+ *
112
+ * @param state The parser state to annotate.
113
+ * @param annotations The annotations to inject.
114
+ * @returns Annotated state.
115
+ * @internal
116
+ */
117
+ declare function injectAnnotations<TState>(state: TState, annotations: Annotations): TState;
118
+ /**
119
+ * Unwraps a primitive-state annotation wrapper injected by Optique internals.
120
+ *
121
+ * @param value Value to potentially unwrap.
122
+ * @returns The unwrapped primitive value when the input is an injected wrapper;
123
+ * otherwise the original value.
124
+ * @internal
125
+ */
126
+ declare function unwrapInjectedAnnotationWrapper<T>(value: T): T;
127
+ /**
128
+ * Returns whether the given value is an internal primitive-state annotation
129
+ * wrapper that was injected by Optique.
130
+ *
131
+ * @param value Value to check.
132
+ * @returns `true` if the value is an injected internal wrapper.
133
+ * @internal
134
+ */
135
+ declare function isInjectedAnnotationWrapper(value: unknown): boolean;
59
136
  //#endregion
60
- export { Annotations, ParseOptions, annotationKey, getAnnotations };
137
+ export { Annotations, ParseOptions, annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper };
@@ -15,6 +15,36 @@
15
15
  */
16
16
  const annotationKey = Symbol.for("@optique/core/parser/annotation");
17
17
  /**
18
+ * Internal marker attached during the first pass of `runWith()` so wrappers
19
+ * with side effects can defer work until dynamic contexts have resolved.
20
+ *
21
+ * @internal
22
+ */
23
+ const firstPassAnnotationKey = Symbol.for("@optique/core/parser/firstPass");
24
+ /**
25
+ * Internal key for preserving primitive parser state values when annotations
26
+ * are injected into non-object states.
27
+ * @internal
28
+ */
29
+ const annotationStateValueKey = Symbol.for("@optique/core/parser/annotationStateValue");
30
+ /**
31
+ * Internal marker key that indicates annotation wrapping was injected by
32
+ * Optique internals for non-object states.
33
+ * @internal
34
+ */
35
+ const annotationWrapperKey = Symbol.for("@optique/core/parser/annotationWrapper");
36
+ /**
37
+ * Internal symbol keys that define Optique's primitive-state annotation
38
+ * wrapper shape.
39
+ * @internal
40
+ */
41
+ const annotationWrapperKeys = new Set([
42
+ annotationKey,
43
+ annotationStateValueKey,
44
+ annotationWrapperKey
45
+ ]);
46
+ const injectedAnnotationWrappers = /* @__PURE__ */ new WeakSet();
47
+ /**
18
48
  * Extracts annotations from parser state.
19
49
  *
20
50
  * @param state Parser state that may contain annotations
@@ -34,6 +64,176 @@ function getAnnotations(state) {
34
64
  if (annotations != null && typeof annotations === "object") return annotations;
35
65
  return void 0;
36
66
  }
67
+ /**
68
+ * Reattaches annotations to a freshly created array state.
69
+ *
70
+ * Array spread copies elements but drops symbol properties, so parsers that
71
+ * rebuild array states need to copy annotations back explicitly.
72
+ *
73
+ * @param source The original state that may carry annotations.
74
+ * @param target The freshly created array state.
75
+ * @returns The target array, with annotations copied when available.
76
+ * @internal
77
+ */
78
+ function annotateFreshArray(source, target) {
79
+ const annotations = getAnnotations(source);
80
+ if (annotations === void 0) return target;
81
+ const annotated = target;
82
+ annotated[annotationKey] = annotations;
83
+ return annotated;
84
+ }
85
+ /**
86
+ * Propagates annotations from one parser state to another.
87
+ *
88
+ * This is mainly used by parsers that rebuild array states with spread syntax.
89
+ * Array spread copies elements but drops custom symbol properties, so we need
90
+ * to reattach annotations explicitly when present.
91
+ *
92
+ * @param source The original state that may carry annotations.
93
+ * @param target The new state to receive annotations.
94
+ * @returns The target state, with annotations copied when available.
95
+ * @internal
96
+ */
97
+ function inheritAnnotations(source, target) {
98
+ const annotations = getAnnotations(source);
99
+ if (annotations === void 0) return target;
100
+ if (target == null || typeof target !== "object") return injectAnnotations(target, annotations);
101
+ if (isInjectedAnnotationWrapper(target)) return injectAnnotations(target, annotations);
102
+ if (Array.isArray(target)) {
103
+ const cloned$1 = [...target];
104
+ cloned$1[annotationKey] = annotations;
105
+ return cloned$1;
106
+ }
107
+ if (target instanceof Date) {
108
+ const cloned$1 = new Date(target.getTime());
109
+ cloned$1[annotationKey] = annotations;
110
+ return cloned$1;
111
+ }
112
+ if (target instanceof Map) {
113
+ const cloned$1 = new Map(target);
114
+ cloned$1[annotationKey] = annotations;
115
+ return cloned$1;
116
+ }
117
+ if (target instanceof Set) {
118
+ const cloned$1 = new Set(target);
119
+ cloned$1[annotationKey] = annotations;
120
+ return cloned$1;
121
+ }
122
+ if (target instanceof RegExp) {
123
+ const cloned$1 = new RegExp(target);
124
+ cloned$1[annotationKey] = annotations;
125
+ return cloned$1;
126
+ }
127
+ if (Object.getPrototypeOf(target) !== Object.prototype && Object.getPrototypeOf(target) !== null) return target;
128
+ const cloned = Object.create(Object.getPrototypeOf(target), Object.getOwnPropertyDescriptors(target));
129
+ cloned[annotationKey] = annotations;
130
+ return cloned;
131
+ }
132
+ /**
133
+ * Injects annotations into parser state while preserving state shape.
134
+ *
135
+ * - Primitive, null, and undefined states are wrapped with internal metadata.
136
+ * - Array states are cloned and annotated without mutating the original.
137
+ * - Plain object states are shallow-cloned with annotations attached.
138
+ * - Built-in object states (Date/Map/Set/RegExp) are cloned by constructor.
139
+ * - Other non-plain object states are cloned via prototype/descriptors.
140
+ *
141
+ * @param state The parser state to annotate.
142
+ * @param annotations The annotations to inject.
143
+ * @returns Annotated state.
144
+ * @internal
145
+ */
146
+ function injectAnnotations(state, annotations) {
147
+ if (state == null || typeof state !== "object") {
148
+ const wrapper = {};
149
+ Object.defineProperties(wrapper, {
150
+ [annotationKey]: {
151
+ value: annotations,
152
+ enumerable: true,
153
+ writable: true,
154
+ configurable: true
155
+ },
156
+ [annotationStateValueKey]: {
157
+ value: state,
158
+ enumerable: false,
159
+ writable: true,
160
+ configurable: true
161
+ },
162
+ [annotationWrapperKey]: {
163
+ value: true,
164
+ enumerable: false,
165
+ writable: true,
166
+ configurable: true
167
+ }
168
+ });
169
+ injectedAnnotationWrappers.add(wrapper);
170
+ return wrapper;
171
+ }
172
+ if (Array.isArray(state)) {
173
+ const cloned$1 = [...state];
174
+ cloned$1[annotationKey] = annotations;
175
+ return cloned$1;
176
+ }
177
+ if (isInjectedAnnotationWrapper(state)) {
178
+ state[annotationKey] = annotations;
179
+ return state;
180
+ }
181
+ if (state instanceof Date) {
182
+ const cloned$1 = new Date(state.getTime());
183
+ cloned$1[annotationKey] = annotations;
184
+ return cloned$1;
185
+ }
186
+ if (state instanceof Map) {
187
+ const cloned$1 = new Map(state);
188
+ cloned$1[annotationKey] = annotations;
189
+ return cloned$1;
190
+ }
191
+ if (state instanceof Set) {
192
+ const cloned$1 = new Set(state);
193
+ cloned$1[annotationKey] = annotations;
194
+ return cloned$1;
195
+ }
196
+ if (state instanceof RegExp) {
197
+ const cloned$1 = new RegExp(state);
198
+ cloned$1[annotationKey] = annotations;
199
+ return cloned$1;
200
+ }
201
+ const proto = Object.getPrototypeOf(state);
202
+ if (proto === Object.prototype || proto === null) return {
203
+ ...state,
204
+ [annotationKey]: annotations
205
+ };
206
+ const cloned = Object.create(proto, Object.getOwnPropertyDescriptors(state));
207
+ cloned[annotationKey] = annotations;
208
+ return cloned;
209
+ }
210
+ /**
211
+ * Unwraps a primitive-state annotation wrapper injected by Optique internals.
212
+ *
213
+ * @param value Value to potentially unwrap.
214
+ * @returns The unwrapped primitive value when the input is an injected wrapper;
215
+ * otherwise the original value.
216
+ * @internal
217
+ */
218
+ function unwrapInjectedAnnotationWrapper(value) {
219
+ if (value == null || typeof value !== "object") return value;
220
+ const valueRecord = value;
221
+ if (valueRecord[annotationWrapperKey] !== true) return value;
222
+ const ownKeys = Reflect.ownKeys(valueRecord);
223
+ if (ownKeys.length === 3 && ownKeys.every((key) => annotationWrapperKeys.has(key)) && isInjectedAnnotationWrapper(value)) return valueRecord[annotationStateValueKey];
224
+ return value;
225
+ }
226
+ /**
227
+ * Returns whether the given value is an internal primitive-state annotation
228
+ * wrapper that was injected by Optique.
229
+ *
230
+ * @param value Value to check.
231
+ * @returns `true` if the value is an injected internal wrapper.
232
+ * @internal
233
+ */
234
+ function isInjectedAnnotationWrapper(value) {
235
+ return value != null && typeof value === "object" && injectedAnnotationWrappers.has(value);
236
+ }
37
237
 
38
238
  //#endregion
39
- export { annotationKey, getAnnotations };
239
+ export { annotateFreshArray, annotationKey, annotationStateValueKey, annotationWrapperKey, firstPassAnnotationKey, getAnnotations, inheritAnnotations, injectAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper };