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

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.
@@ -38,12 +38,13 @@ function mergeChildExec(parent, child) {
38
38
  excludedSourceFields: child.excludedSourceFields ?? parent.excludedSourceFields
39
39
  };
40
40
  }
41
- function withChildContext(context, segment, state, usage) {
41
+ function withChildContext(context, segment, state, parser, usage) {
42
42
  const exec = withChildExecPath(context.exec, segment);
43
43
  const dependencyRegistry = context.dependencyRegistry ?? exec?.dependencyRegistry;
44
+ const childState = parser == null ? state : getParseChildState(context.state, state, parser);
44
45
  return {
45
46
  ...context,
46
- state,
47
+ state: childState,
47
48
  ...usage != null ? { usage } : {},
48
49
  ...exec != null ? {
49
50
  exec: dependencyRegistry === exec.dependencyRegistry ? exec : {
@@ -69,6 +70,13 @@ function filterPreCompletedRuntimeNodes(nodes, preCompletedKeys) {
69
70
  return segment == null || !preCompletedKeys.has(segment);
70
71
  });
71
72
  }
73
+ function buildIndexedParserPairs(parsers) {
74
+ return parsers.map((parser, index) => [String(index), parser]);
75
+ }
76
+ function createAnnotatedArrayStateRecord(stateArray) {
77
+ const stateRecord = Object.fromEntries(stateArray.map((state, index) => [String(index), state]));
78
+ return require_annotations.inheritAnnotations(stateArray, stateRecord);
79
+ }
72
80
  /**
73
81
  * Computes the union of `leadingNames` from all given parsers.
74
82
  * Used for alternative combinators (`or()`, `longestMatch()`) where all
@@ -101,7 +109,6 @@ function sharedBufferLeadingNames(parsers) {
101
109
  }
102
110
  return names.size === 0 ? EMPTY_LEADING_NAMES : names;
103
111
  }
104
- const inheritParentAnnotationsKey = Symbol.for("@optique/core/inheritParentAnnotations");
105
112
  /**
106
113
  * Internal symbol for exposing field-level parser pairs from `object()`
107
114
  * and `merge()` parsers. This allows `merge()` to pre-complete dependency
@@ -116,9 +123,73 @@ const fieldParsersKey = Symbol("fieldParsers");
116
123
  * to consume results from parsers that still return `DependencySourceState`.
117
124
  * @internal
118
125
  */
126
+ function unwrapAnnotationView(value) {
127
+ if (value == null || typeof value !== "object") return value;
128
+ return annotationViewTargets.get(value) ?? value;
129
+ }
130
+ function containsAnnotationView(value, seen = /* @__PURE__ */ new WeakSet()) {
131
+ if (value == null || typeof value !== "object") return false;
132
+ const candidate = value;
133
+ if (annotationViewTargets.has(candidate)) return true;
134
+ const source = unwrapAnnotationView(candidate);
135
+ if (seen.has(source)) return false;
136
+ seen.add(source);
137
+ if (Array.isArray(source)) return source.some((item) => containsAnnotationView(item, seen));
138
+ const proto = Object.getPrototypeOf(source);
139
+ if (proto !== Object.prototype && proto !== null) return false;
140
+ return Reflect.ownKeys(source).some((key) => containsAnnotationView(source[key], seen));
141
+ }
142
+ function unwrapNestedAnnotationViews(value, seen = /* @__PURE__ */ new WeakMap()) {
143
+ if (value == null || typeof value !== "object") return value;
144
+ const source = unwrapAnnotationView(value);
145
+ if (seen.has(source)) return seen.get(source);
146
+ if (Array.isArray(source)) {
147
+ let changed$1 = false;
148
+ const clone$1 = [...source];
149
+ seen.set(source, clone$1);
150
+ for (let i = 0; i < source.length; i++) {
151
+ const nextValue = unwrapNestedAnnotationViews(source[i], seen);
152
+ if (nextValue !== source[i]) {
153
+ clone$1[i] = nextValue;
154
+ changed$1 = true;
155
+ }
156
+ }
157
+ return changed$1 ? clone$1 : source;
158
+ }
159
+ const proto = Object.getPrototypeOf(source);
160
+ if (proto !== Object.prototype && proto !== null) return source;
161
+ const descriptors = Object.getOwnPropertyDescriptors(source);
162
+ let changed = false;
163
+ const clone = Object.create(proto);
164
+ seen.set(source, clone);
165
+ for (const key of Reflect.ownKeys(descriptors)) {
166
+ const descriptor = descriptors[key];
167
+ if (descriptor != null && "value" in descriptor) {
168
+ const nextValue = unwrapNestedAnnotationViews(descriptor.value, seen);
169
+ if (nextValue !== descriptor.value) {
170
+ descriptors[key] = {
171
+ ...descriptor,
172
+ value: nextValue
173
+ };
174
+ changed = true;
175
+ }
176
+ }
177
+ }
178
+ if (!changed) {
179
+ seen.set(source, source);
180
+ return source;
181
+ }
182
+ Object.defineProperties(clone, descriptors);
183
+ return clone;
184
+ }
119
185
  function unwrapCompleteResult(result) {
120
- if (require_dependency.isDependencySourceState(result)) return result.result;
121
- return result;
186
+ const unwrappedResult = require_dependency.isDependencySourceState(result) ? result.result : result;
187
+ if (!unwrappedResult.success || !containsAnnotationView(unwrappedResult.value)) return unwrappedResult;
188
+ const value = unwrapNestedAnnotationViews(unwrappedResult.value);
189
+ return value === unwrappedResult.value ? unwrappedResult : {
190
+ ...unwrappedResult,
191
+ value
192
+ };
122
193
  }
123
194
  /**
124
195
  * Prepares a field state for completion by wrapping `undefined` state
@@ -139,22 +210,80 @@ function prepareStateForCompletion(fieldState, parser) {
139
210
  }
140
211
  /**
141
212
  * Returns the field state with parent annotations inherited, respecting
142
- * the parser's {@link inheritParentAnnotationsKey} flag. This is the
143
- * same logic as {@link createFieldStateGetter} inside `object()` but
144
- * without the per-state cache, for use in module-level helpers like
213
+ * the parser's annotation-inheritance contract. This is the same logic as
214
+ * {@link createFieldStateGetter} inside `object()` but without the per-state
215
+ * cache, for use in module-level helpers like
145
216
  * {@link pendingDependencyDefaults}.
146
217
  * @internal
147
218
  */
148
219
  function getAnnotatedFieldState(parentState, field, parser) {
149
220
  const sourceState = parentState != null && typeof parentState === "object" && field in parentState ? parentState[field] : parser.initialState;
221
+ return getAnnotatedChildState(parentState, sourceState, parser);
222
+ }
223
+ const annotationViewTargets = /* @__PURE__ */ new WeakMap();
224
+ function withAnnotationView(state, annotations) {
225
+ const view = new Proxy(state, {
226
+ get(target, key) {
227
+ if (key === require_annotations.annotationKey) return annotations;
228
+ const value = Reflect.get(target, key, target);
229
+ return typeof value === "function" ? value.bind(target) : value;
230
+ },
231
+ has(target, key) {
232
+ return key === require_annotations.annotationKey || Reflect.has(target, key);
233
+ }
234
+ });
235
+ annotationViewTargets.set(view, state);
236
+ return view;
237
+ }
238
+ function getParseChildState(parentState, childState, parser) {
239
+ const annotations = require_annotations.getAnnotations(parentState);
240
+ const shouldInheritAnnotations = Reflect.get(parser, require_parser.inheritParentAnnotationsKey) === true;
241
+ if (childState == null) {
242
+ if (annotations !== void 0 && shouldInheritAnnotations) return require_annotations.injectAnnotations({}, annotations);
243
+ return childState;
244
+ }
245
+ if (annotations === void 0 || typeof childState !== "object" || require_annotations.getAnnotations(childState) === annotations || !shouldInheritAnnotations) return childState;
246
+ const injectedState = require_annotations.injectAnnotations(childState, annotations);
247
+ return require_annotations.getAnnotations(injectedState) === annotations ? injectedState : childState;
248
+ }
249
+ function getObjectParseChildState(parentState, childState, _parser) {
150
250
  const annotations = require_annotations.getAnnotations(parentState);
151
- if (sourceState == null) {
152
- if (annotations !== void 0 && Reflect.get(parser, inheritParentAnnotationsKey) === true) return require_annotations.injectAnnotations({}, annotations);
153
- return sourceState;
251
+ if (annotations === void 0 || childState == null || typeof childState !== "object" || require_annotations.getAnnotations(childState) === annotations) return childState;
252
+ return require_annotations.injectAnnotations(childState, annotations);
253
+ }
254
+ function getAnnotatedChildState(parentState, childState, parser) {
255
+ const annotations = require_annotations.getAnnotations(parentState);
256
+ const shouldInheritAnnotations = Reflect.get(parser, require_parser.inheritParentAnnotationsKey) === true;
257
+ if (childState == null) {
258
+ if (annotations !== void 0 && shouldInheritAnnotations) return require_annotations.injectAnnotations({}, annotations);
259
+ return childState;
260
+ }
261
+ if (typeof childState !== "object") return childState;
262
+ if (annotations === void 0 || require_annotations.getAnnotations(childState) === annotations) return childState;
263
+ if (shouldInheritAnnotations) {
264
+ const injectedState = require_annotations.injectAnnotations(childState, annotations);
265
+ if (require_annotations.getAnnotations(injectedState) === annotations) return injectedState;
266
+ }
267
+ return withAnnotationView(childState, annotations);
268
+ }
269
+ function buildSuggestRuntimeNodesFromPairs(pairs, state, parentPath) {
270
+ const prefix = parentPath ?? [];
271
+ const nodes = [];
272
+ for (const [field, parser] of pairs) {
273
+ const fieldState = Object.hasOwn(state, field) ? state[field] : parser.initialState;
274
+ nodes.push(...require_parser.getParserSuggestRuntimeNodes(parser, getAnnotatedChildState(state, fieldState, parser), [...prefix, field]));
275
+ }
276
+ return nodes;
277
+ }
278
+ function buildSuggestRuntimeNodesFromArray(parsers, stateArray, parentPath) {
279
+ const prefix = parentPath ?? [];
280
+ const nodes = [];
281
+ for (let i = 0; i < parsers.length; i++) {
282
+ const parser = parsers[i];
283
+ const elementState = i < stateArray.length ? stateArray[i] : void 0;
284
+ nodes.push(...require_parser.getParserSuggestRuntimeNodes(parser, getAnnotatedChildState(stateArray, elementState, parser), [...prefix, i]));
154
285
  }
155
- if (typeof sourceState !== "object") return sourceState;
156
- if (annotations === void 0 || require_annotations.getAnnotations(sourceState) === annotations) return sourceState;
157
- return Reflect.get(parser, inheritParentAnnotationsKey) === true ? require_annotations.injectAnnotations(sourceState, annotations) : require_annotations.inheritAnnotations(parentState, sourceState);
286
+ return nodes;
158
287
  }
159
288
  function createUnexpectedInputErrorWithScopedSuggestions(baseError, invalidInput, parsers, customFormatter) {
160
289
  const options = /* @__PURE__ */ new Set();
@@ -918,8 +1047,10 @@ function registerCompletedDependency(completed, registry) {
918
1047
  * @see https://github.com/dahlia/optique/issues/186
919
1048
  * @internal
920
1049
  */
921
- function* pendingDependencyDefaults(context, parserPairs) {
1050
+ function* pendingDependencyDefaults(context, parserPairs, registry) {
922
1051
  for (const [field, fieldParser] of parserPairs) {
1052
+ const sourceId = fieldParser.dependencyMetadata?.source?.sourceId ?? (require_dependency.isWrappedDependencySource(fieldParser) ? fieldParser[require_dependency.wrappedDependencySourceMarker][require_dependency.dependencyId] : require_dependency.isPendingDependencySourceState(fieldParser.initialState) ? fieldParser.initialState[require_dependency.dependencyId] : void 0);
1053
+ if (sourceId != null && registry?.has(sourceId)) continue;
923
1054
  const fieldState = context.state != null && typeof context.state === "object" && field in context.state ? context.state[field] : void 0;
924
1055
  const annotatedFieldState = getAnnotatedFieldState(context.state, field, fieldParser);
925
1056
  if (fieldParser.dependencyMetadata?.source?.getMissingSourceValue != null && isUnmatchedDependencyState(fieldState, fieldParser)) {
@@ -963,7 +1094,7 @@ function* pendingDependencyDefaults(context, parserPairs) {
963
1094
  * @internal
964
1095
  */
965
1096
  function completeDependencySourceDefaults(context, parserPairs, registry, exec) {
966
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
1097
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs, registry)) {
967
1098
  const completed = parser.complete(state, exec);
968
1099
  const depState = wrapAsDependencySourceState(completed, parser);
969
1100
  if (depState) registerCompletedDependency(depState, registry);
@@ -1000,7 +1131,7 @@ function wrapAsDependencySourceState(completed, parser) {
1000
1131
  * @internal
1001
1132
  */
1002
1133
  async function completeDependencySourceDefaultsAsync(context, parserPairs, registry, exec) {
1003
- for (const { parser, state } of pendingDependencyDefaults(context, parserPairs)) {
1134
+ for (const { parser, state } of pendingDependencyDefaults(context, parserPairs, registry)) {
1004
1135
  const completed = await parser.complete(state, exec);
1005
1136
  const depState = wrapAsDependencySourceState(completed, parser);
1006
1137
  if (depState) registerCompletedDependency(depState, registry);
@@ -1194,7 +1325,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1194
1325
  return state;
1195
1326
  };
1196
1327
  const inheritedFieldStateCache = /* @__PURE__ */ new WeakMap();
1197
- const createFieldStateGetter = (parentState) => {
1328
+ const createFieldStateGetter = (parentState, annotateChildState = getAnnotatedChildState) => {
1198
1329
  return (field, parser) => {
1199
1330
  const fieldKey = field;
1200
1331
  const cache = parentState != null && typeof parentState === "object" ? inheritedFieldStateCache.get(parentState) ?? (() => {
@@ -1204,25 +1335,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1204
1335
  })() : void 0;
1205
1336
  if (cache?.has(fieldKey)) return cache.get(fieldKey);
1206
1337
  const sourceState = parentState != null && typeof parentState === "object" && fieldKey in parentState ? parentState[fieldKey] : parser.initialState;
1207
- const annotations = require_annotations.getAnnotations(parentState);
1208
- if (sourceState == null) {
1209
- if (annotations !== void 0 && Reflect.get(parser, inheritParentAnnotationsKey) === true) {
1210
- const inheritedState$1 = require_annotations.injectAnnotations({}, annotations);
1211
- cache?.set(fieldKey, inheritedState$1);
1212
- return inheritedState$1;
1213
- }
1214
- cache?.set(fieldKey, sourceState);
1215
- return sourceState;
1216
- }
1217
- if (typeof sourceState !== "object") {
1218
- cache?.set(fieldKey, sourceState);
1219
- return sourceState;
1220
- }
1221
- if (annotations === void 0 || require_annotations.getAnnotations(sourceState) === annotations) {
1222
- cache?.set(fieldKey, sourceState);
1223
- return sourceState;
1224
- }
1225
- const inheritedState = Reflect.get(parser, inheritParentAnnotationsKey) === true ? require_annotations.injectAnnotations(sourceState, annotations) : require_annotations.inheritAnnotations(parentState, sourceState);
1338
+ const inheritedState = annotateChildState(parentState, sourceState, parser);
1226
1339
  cache?.set(fieldKey, inheritedState);
1227
1340
  return inheritedState;
1228
1341
  };
@@ -1251,7 +1364,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1251
1364
  let madeProgress = true;
1252
1365
  while (madeProgress && currentContext.buffer.length > 0) {
1253
1366
  madeProgress = false;
1254
- const getFieldState = createFieldStateGetter(currentContext.state);
1367
+ const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
1255
1368
  for (const [field, parser] of parserPairs) {
1256
1369
  const result = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
1257
1370
  if (result.success && result.consumed.length > 0) {
@@ -1262,7 +1375,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1262
1375
  optionsTerminated: result.next.optionsTerminated,
1263
1376
  state: {
1264
1377
  ...currentContext.state,
1265
- [field]: result.next.state
1378
+ [field]: getAnnotatedChildState(currentContext.state, result.next.state, parser)
1266
1379
  },
1267
1380
  ...mergedExec != null ? {
1268
1381
  trace: mergedExec.trace,
@@ -1312,7 +1425,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1312
1425
  let madeProgress = true;
1313
1426
  while (madeProgress && currentContext.buffer.length > 0) {
1314
1427
  madeProgress = false;
1315
- const getFieldState = createFieldStateGetter(currentContext.state);
1428
+ const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
1316
1429
  for (const [field, parser] of parserPairs) {
1317
1430
  const resultOrPromise = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
1318
1431
  const result = await resultOrPromise;
@@ -1324,7 +1437,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1324
1437
  optionsTerminated: result.next.optionsTerminated,
1325
1438
  state: {
1326
1439
  ...currentContext.state,
1327
- [field]: result.next.state
1440
+ [field]: getAnnotatedChildState(currentContext.state, result.next.state, parser)
1328
1441
  },
1329
1442
  ...mergedExec != null ? {
1330
1443
  trace: mergedExec.trace,
@@ -1571,49 +1684,226 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1571
1684
  configurable: true,
1572
1685
  enumerable: false
1573
1686
  });
1687
+ require_parser.defineInheritedAnnotationParser(objectParser);
1574
1688
  return objectParser;
1575
1689
  }
1576
1690
  function suggestTupleSync(context, prefix, parsers) {
1577
1691
  const suggestions = [];
1578
- const stateArray = context.state;
1579
- const runtime = require_dependency_runtime.createDependencyRuntimeContext(context.dependencyRegistry?.clone());
1692
+ const advanced = advanceTupleSuggestContextSync(context, parsers);
1693
+ const advancedContext = advanced.context;
1694
+ const stateArray = advancedContext.state;
1695
+ const runtime = require_dependency_runtime.createDependencyRuntimeContext(advancedContext.dependencyRegistry?.clone());
1580
1696
  if (stateArray && Array.isArray(stateArray)) {
1581
- require_dependency_runtime.collectExplicitSourceValues(require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, context.exec?.path), runtime);
1697
+ const nodes = buildSuggestRuntimeNodesFromArray(parsers, stateArray, advancedContext.exec?.path);
1698
+ require_dependency_runtime.collectExplicitSourceValues(nodes, runtime);
1699
+ require_dependency_runtime.fillMissingSourceDefaults(nodes, runtime);
1582
1700
  require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
1701
+ completeDependencySourceDefaults({
1702
+ ...advancedContext,
1703
+ state: createAnnotatedArrayStateRecord(stateArray)
1704
+ }, buildIndexedParserPairs(parsers), runtime.registry, advancedContext.exec);
1583
1705
  }
1706
+ markFailedTupleSuggestSources(parsers, stateArray, advanced.failedParserIndexes, runtime, advancedContext.exec?.path);
1584
1707
  const contextWithRegistry = {
1585
- ...context,
1586
- dependencyRegistry: runtime.registry
1708
+ ...advancedContext,
1709
+ dependencyRegistry: runtime.registry,
1710
+ ...advancedContext.exec != null ? { exec: {
1711
+ ...advancedContext.exec,
1712
+ dependencyRuntime: runtime,
1713
+ dependencyRegistry: runtime.registry
1714
+ } } : {}
1587
1715
  };
1588
1716
  for (let i = 0; i < parsers.length; i++) {
1589
1717
  const parser = parsers[i];
1590
1718
  const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
1591
- const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState), prefix);
1719
+ const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState, parser), prefix);
1592
1720
  suggestions.push(...parserSuggestions);
1593
1721
  }
1594
1722
  return require_suggestion.deduplicateSuggestions(suggestions);
1595
1723
  }
1596
1724
  async function* suggestTupleAsync(context, prefix, parsers) {
1597
1725
  const suggestions = [];
1598
- const stateArray = context.state;
1599
- const runtime = require_dependency_runtime.createDependencyRuntimeContext(context.dependencyRegistry?.clone());
1726
+ const advanced = await advanceTupleSuggestContextAsync(context, parsers);
1727
+ const advancedContext = advanced.context;
1728
+ const stateArray = advancedContext.state;
1729
+ const runtime = require_dependency_runtime.createDependencyRuntimeContext(advancedContext.dependencyRegistry?.clone());
1600
1730
  if (stateArray && Array.isArray(stateArray)) {
1601
- await require_dependency_runtime.collectExplicitSourceValuesAsync(require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, context.exec?.path), runtime);
1731
+ const nodes = buildSuggestRuntimeNodesFromArray(parsers, stateArray, advancedContext.exec?.path);
1732
+ await require_dependency_runtime.collectExplicitSourceValuesAsync(nodes, runtime);
1733
+ await require_dependency_runtime.fillMissingSourceDefaultsAsync(nodes, runtime);
1602
1734
  require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
1735
+ await completeDependencySourceDefaultsAsync({
1736
+ ...advancedContext,
1737
+ state: createAnnotatedArrayStateRecord(stateArray)
1738
+ }, buildIndexedParserPairs(parsers), runtime.registry, advancedContext.exec);
1603
1739
  }
1740
+ markFailedTupleSuggestSources(parsers, stateArray, advanced.failedParserIndexes, runtime, advancedContext.exec?.path);
1604
1741
  const contextWithRegistry = {
1605
- ...context,
1606
- dependencyRegistry: runtime.registry
1742
+ ...advancedContext,
1743
+ dependencyRegistry: runtime.registry,
1744
+ ...advancedContext.exec != null ? { exec: {
1745
+ ...advancedContext.exec,
1746
+ dependencyRuntime: runtime,
1747
+ dependencyRegistry: runtime.registry
1748
+ } } : {}
1607
1749
  };
1608
1750
  for (let i = 0; i < parsers.length; i++) {
1609
1751
  const parser = parsers[i];
1610
1752
  const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
1611
- const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState), prefix);
1753
+ const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState, parser), prefix);
1612
1754
  if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
1613
1755
  else suggestions.push(...parserSuggestions);
1614
1756
  }
1615
1757
  yield* require_suggestion.deduplicateSuggestions(suggestions);
1616
1758
  }
1759
+ function advanceTupleSuggestContextSync(context, parsers) {
1760
+ let currentContext = context;
1761
+ const matchedParsers = /* @__PURE__ */ new Set();
1762
+ while (currentContext.buffer.length > 0 && matchedParsers.size < parsers.length) {
1763
+ let foundMatch = false;
1764
+ let failedParserIndexes = [];
1765
+ let deepestFailure = 0;
1766
+ const stateArray = Array.isArray(currentContext.state) ? [...currentContext.state] : parsers.map((parser) => parser.initialState);
1767
+ const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1768
+ for (const [parser, index] of remainingParsers) {
1769
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1770
+ if (result.success && result.consumed.length > 0) {
1771
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((state, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : state));
1772
+ const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1773
+ currentContext = {
1774
+ ...currentContext,
1775
+ buffer: result.next.buffer,
1776
+ optionsTerminated: result.next.optionsTerminated,
1777
+ state: newStateArray,
1778
+ ...mergedExec != null ? {
1779
+ exec: mergedExec,
1780
+ dependencyRegistry: mergedExec.dependencyRegistry
1781
+ } : {}
1782
+ };
1783
+ matchedParsers.add(index);
1784
+ foundMatch = true;
1785
+ break;
1786
+ } else if (!result.success && result.consumed > 0) {
1787
+ if (result.consumed > deepestFailure) {
1788
+ deepestFailure = result.consumed;
1789
+ failedParserIndexes = [index];
1790
+ } else if (result.consumed === deepestFailure) failedParserIndexes.push(index);
1791
+ }
1792
+ }
1793
+ if (!foundMatch) for (const [parser, index] of remainingParsers) {
1794
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1795
+ if (result.success && result.consumed.length < 1) {
1796
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((state, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : state));
1797
+ const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1798
+ currentContext = {
1799
+ ...currentContext,
1800
+ state: newStateArray,
1801
+ ...mergedExec != null ? {
1802
+ exec: mergedExec,
1803
+ dependencyRegistry: mergedExec.dependencyRegistry
1804
+ } : {}
1805
+ };
1806
+ matchedParsers.add(index);
1807
+ foundMatch = true;
1808
+ break;
1809
+ } else if (!result.success && result.consumed < 1) {
1810
+ matchedParsers.add(index);
1811
+ foundMatch = true;
1812
+ break;
1813
+ }
1814
+ }
1815
+ if (!foundMatch) return {
1816
+ context: currentContext,
1817
+ failedParserIndexes
1818
+ };
1819
+ }
1820
+ return {
1821
+ context: currentContext,
1822
+ failedParserIndexes: []
1823
+ };
1824
+ }
1825
+ async function advanceTupleSuggestContextAsync(context, parsers) {
1826
+ let currentContext = context;
1827
+ const matchedParsers = /* @__PURE__ */ new Set();
1828
+ while (currentContext.buffer.length > 0 && matchedParsers.size < parsers.length) {
1829
+ let foundMatch = false;
1830
+ let failedParserIndexes = [];
1831
+ let deepestFailure = 0;
1832
+ const stateArray = Array.isArray(currentContext.state) ? [...currentContext.state] : parsers.map((parser) => parser.initialState);
1833
+ const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1834
+ for (const [parser, index] of remainingParsers) {
1835
+ const result = await parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1836
+ if (result.success && result.consumed.length > 0) {
1837
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((state, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : state));
1838
+ const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1839
+ currentContext = {
1840
+ ...currentContext,
1841
+ buffer: result.next.buffer,
1842
+ optionsTerminated: result.next.optionsTerminated,
1843
+ state: newStateArray,
1844
+ ...mergedExec != null ? {
1845
+ exec: mergedExec,
1846
+ dependencyRegistry: mergedExec.dependencyRegistry
1847
+ } : {}
1848
+ };
1849
+ matchedParsers.add(index);
1850
+ foundMatch = true;
1851
+ break;
1852
+ } else if (!result.success && result.consumed > 0) {
1853
+ if (result.consumed > deepestFailure) {
1854
+ deepestFailure = result.consumed;
1855
+ failedParserIndexes = [index];
1856
+ } else if (result.consumed === deepestFailure) failedParserIndexes.push(index);
1857
+ }
1858
+ }
1859
+ if (!foundMatch) for (const [parser, index] of remainingParsers) {
1860
+ const result = await parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1861
+ if (result.success && result.consumed.length < 1) {
1862
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((state, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : state));
1863
+ const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1864
+ currentContext = {
1865
+ ...currentContext,
1866
+ state: newStateArray,
1867
+ ...mergedExec != null ? {
1868
+ exec: mergedExec,
1869
+ dependencyRegistry: mergedExec.dependencyRegistry
1870
+ } : {}
1871
+ };
1872
+ matchedParsers.add(index);
1873
+ foundMatch = true;
1874
+ break;
1875
+ } else if (!result.success && result.consumed < 1) {
1876
+ matchedParsers.add(index);
1877
+ foundMatch = true;
1878
+ break;
1879
+ }
1880
+ }
1881
+ if (!foundMatch) return {
1882
+ context: currentContext,
1883
+ failedParserIndexes
1884
+ };
1885
+ }
1886
+ return {
1887
+ context: currentContext,
1888
+ failedParserIndexes: []
1889
+ };
1890
+ }
1891
+ function markFailedTupleSuggestSources(parsers, stateArray, failedParserIndexes, runtime, parentPath) {
1892
+ if (failedParserIndexes.length < 1) return;
1893
+ const prefix = parentPath ?? [];
1894
+ const failedSourceIds = /* @__PURE__ */ new Set();
1895
+ for (const index of failedParserIndexes) {
1896
+ const parser = parsers[index];
1897
+ if (parser == null) continue;
1898
+ const parserState = stateArray && Array.isArray(stateArray) ? stateArray[index] : parser.initialState;
1899
+ const nodes = require_parser.getParserSuggestRuntimeNodes(parser, getAnnotatedChildState(stateArray, parserState, parser), [...prefix, index]);
1900
+ for (const node of nodes) {
1901
+ const sourceId = node.parser.dependencyMetadata?.source?.sourceId;
1902
+ if (sourceId != null) failedSourceIds.add(sourceId);
1903
+ }
1904
+ }
1905
+ for (const sourceId of failedSourceIds) runtime.markSourceFailed(sourceId);
1906
+ }
1617
1907
  function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1618
1908
  const label = typeof labelOrParsers === "string" ? labelOrParsers : void 0;
1619
1909
  if (label != null) require_validate.validateLabel(label);
@@ -1642,9 +1932,9 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1642
1932
  const stateArray = currentContext.state;
1643
1933
  const remainingParsers = syncParsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1644
1934
  for (const [parser, index] of remainingParsers) {
1645
- const result = parser.parse(withChildContext(currentContext, index, stateArray[index]));
1935
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1646
1936
  if (result.success && result.consumed.length > 0) {
1647
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1937
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
1648
1938
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1649
1939
  currentContext = {
1650
1940
  ...currentContext,
@@ -1663,9 +1953,9 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1663
1953
  } else if (!result.success && error.consumed < result.consumed) error = result;
1664
1954
  }
1665
1955
  if (!foundMatch) for (const [parser, index] of remainingParsers) {
1666
- const result = parser.parse(withChildContext(currentContext, index, stateArray[index]));
1956
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1667
1957
  if (result.success && result.consumed.length < 1) {
1668
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1958
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
1669
1959
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1670
1960
  currentContext = {
1671
1961
  ...currentContext,
@@ -1708,10 +1998,10 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1708
1998
  const stateArray = currentContext.state;
1709
1999
  const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1710
2000
  for (const [parser, index] of remainingParsers) {
1711
- const resultOrPromise = parser.parse(withChildContext(currentContext, index, stateArray[index]));
2001
+ const resultOrPromise = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1712
2002
  const result = await resultOrPromise;
1713
2003
  if (result.success && result.consumed.length > 0) {
1714
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
2004
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
1715
2005
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1716
2006
  currentContext = {
1717
2007
  ...currentContext,
@@ -1730,10 +2020,10 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1730
2020
  } else if (!result.success && error.consumed < result.consumed) error = result;
1731
2021
  }
1732
2022
  if (!foundMatch) for (const [parser, index] of remainingParsers) {
1733
- const resultOrPromise = parser.parse(withChildContext(currentContext, index, stateArray[index]));
2023
+ const resultOrPromise = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
1734
2024
  const result = await resultOrPromise;
1735
2025
  if (result.success && result.consumed.length < 1) {
1736
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
2026
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
1737
2027
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
1738
2028
  currentContext = {
1739
2029
  ...currentContext,
@@ -1784,8 +2074,8 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1784
2074
  ...exec,
1785
2075
  dependencyRuntime: runtime
1786
2076
  };
1787
- const tuplePairs = syncParsers.map((p, i) => [String(i), p]);
1788
- const tupleState = Object.fromEntries(stateArray.map((s, i) => [String(i), s]));
2077
+ const tuplePairs = buildIndexedParserPairs(syncParsers);
2078
+ const tupleState = createAnnotatedArrayStateRecord(stateArray);
1789
2079
  const preCompleted = preCompleteAndRegisterDependencies(tupleState, tuplePairs, runtime.registry, childExec);
1790
2080
  require_dependency_runtime.collectExplicitSourceValues(filterPreCompletedRuntimeNodes(require_dependency_runtime.buildRuntimeNodesFromArray(syncParsers, stateArray, exec?.path), new Set(preCompleted.keys())), runtime);
1791
2081
  const phase3Exec = {
@@ -1831,8 +2121,8 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1831
2121
  ...exec,
1832
2122
  dependencyRuntime: runtime
1833
2123
  };
1834
- const tuplePairs = parsers.map((p, i) => [String(i), p]);
1835
- const tupleState = Object.fromEntries(stateArray.map((s, i) => [String(i), s]));
2124
+ const tuplePairs = buildIndexedParserPairs(parsers);
2125
+ const tupleState = createAnnotatedArrayStateRecord(stateArray);
1836
2126
  const preCompleted = await preCompleteAndRegisterDependenciesAsync(tupleState, tuplePairs, runtime.registry, childExec);
1837
2127
  await require_dependency_runtime.collectExplicitSourceValuesAsync(filterPreCompletedRuntimeNodes(require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, exec?.path), new Set(preCompleted.keys())), runtime);
1838
2128
  const phase3Exec = {
@@ -1930,6 +2220,7 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1930
2220
  configurable: true,
1931
2221
  enumerable: false
1932
2222
  });
2223
+ require_parser.defineInheritedAnnotationParser(tupleParser);
1933
2224
  return tupleParser;
1934
2225
  }
1935
2226
  function merge(...args) {
@@ -2425,6 +2716,7 @@ function merge(...args) {
2425
2716
  };
2426
2717
  }
2427
2718
  };
2719
+ require_parser.defineInheritedAnnotationParser(mergeParser);
2428
2720
  return mergeParser;
2429
2721
  }
2430
2722
  /**
@@ -2437,17 +2729,26 @@ function buildSuggestRegistry(preParsedContext, parsers) {
2437
2729
  const stateArray = preParsedContext.state;
2438
2730
  const runtime = require_dependency_runtime.createDependencyRuntimeContext(preParsedContext.dependencyRegistry?.clone());
2439
2731
  if (stateArray && Array.isArray(stateArray)) {
2440
- const nodes = require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, preParsedContext.exec?.path);
2732
+ const nodes = buildSuggestRuntimeNodesFromArray(parsers, stateArray, preParsedContext.exec?.path);
2441
2733
  require_dependency_runtime.collectExplicitSourceValues(nodes, runtime);
2442
- require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
2443
2734
  require_dependency_runtime.fillMissingSourceDefaults(nodes, runtime);
2735
+ require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
2736
+ completeDependencySourceDefaults({
2737
+ ...preParsedContext,
2738
+ state: createAnnotatedArrayStateRecord(stateArray)
2739
+ }, buildIndexedParserPairs(parsers), runtime.registry, preParsedContext.exec);
2444
2740
  const prefix = preParsedContext.exec?.path ?? [];
2445
- for (let i = 0; i < parsers.length; i++) seedSuggestRuntimeFromFieldParsers(parsers[i], stateArray[i], runtime, [...prefix, i]);
2741
+ for (let i = 0; i < parsers.length; i++) seedSuggestRuntimeFromFieldParsers(parsers[i], getAnnotatedChildState(stateArray, stateArray[i], parsers[i]), runtime, [...prefix, i]);
2446
2742
  }
2447
2743
  return {
2448
2744
  context: {
2449
2745
  ...preParsedContext,
2450
- dependencyRegistry: runtime.registry
2746
+ dependencyRegistry: runtime.registry,
2747
+ ...preParsedContext.exec != null ? { exec: {
2748
+ ...preParsedContext.exec,
2749
+ dependencyRuntime: runtime,
2750
+ dependencyRegistry: runtime.registry
2751
+ } } : {}
2451
2752
  },
2452
2753
  stateArray
2453
2754
  };
@@ -2456,17 +2757,26 @@ async function buildSuggestRegistryAsync(preParsedContext, parsers) {
2456
2757
  const stateArray = preParsedContext.state;
2457
2758
  const runtime = require_dependency_runtime.createDependencyRuntimeContext(preParsedContext.dependencyRegistry?.clone());
2458
2759
  if (stateArray && Array.isArray(stateArray)) {
2459
- const nodes = require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, preParsedContext.exec?.path);
2760
+ const nodes = buildSuggestRuntimeNodesFromArray(parsers, stateArray, preParsedContext.exec?.path);
2460
2761
  await require_dependency_runtime.collectExplicitSourceValuesAsync(nodes, runtime);
2461
- require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
2462
2762
  await require_dependency_runtime.fillMissingSourceDefaultsAsync(nodes, runtime);
2763
+ require_dependency_runtime.collectSourcesFromState(stateArray, runtime);
2764
+ await completeDependencySourceDefaultsAsync({
2765
+ ...preParsedContext,
2766
+ state: createAnnotatedArrayStateRecord(stateArray)
2767
+ }, buildIndexedParserPairs(parsers), runtime.registry, preParsedContext.exec);
2463
2768
  const prefix = preParsedContext.exec?.path ?? [];
2464
- for (let i = 0; i < parsers.length; i++) await seedSuggestRuntimeFromFieldParsersAsync(parsers[i], stateArray[i], runtime, [...prefix, i]);
2769
+ for (let i = 0; i < parsers.length; i++) await seedSuggestRuntimeFromFieldParsersAsync(parsers[i], getAnnotatedChildState(stateArray, stateArray[i], parsers[i]), runtime, [...prefix, i]);
2465
2770
  }
2466
2771
  return {
2467
2772
  context: {
2468
2773
  ...preParsedContext,
2469
- dependencyRegistry: runtime.registry
2774
+ dependencyRegistry: runtime.registry,
2775
+ ...preParsedContext.exec != null ? { exec: {
2776
+ ...preParsedContext.exec,
2777
+ dependencyRuntime: runtime,
2778
+ dependencyRegistry: runtime.registry
2779
+ } } : {}
2470
2780
  },
2471
2781
  stateArray
2472
2782
  };
@@ -2477,11 +2787,31 @@ function seedSuggestRuntimeFromFieldParsers(parser, state, runtime, parentPath)
2477
2787
  const duplicateFieldNames = collectDuplicateFieldNames(rawPairs);
2478
2788
  const pairs = filterDuplicateFieldParsers(rawPairs);
2479
2789
  const stateRecord = state != null && typeof state === "object" ? state : {};
2480
- const nodes = require_dependency_runtime.buildRuntimeNodesFromPairs(pairs, stateRecord, parentPath);
2790
+ const nodes = buildSuggestRuntimeNodesFromPairs(pairs, stateRecord, parentPath);
2481
2791
  require_dependency_runtime.collectExplicitSourceValues(nodes, runtime);
2482
- require_dependency_runtime.collectSourcesFromState(state, runtime, /* @__PURE__ */ new WeakSet(), duplicateFieldNames);
2483
2792
  require_dependency_runtime.fillMissingSourceDefaults(nodes, runtime);
2484
- for (const [field, childParser] of pairs) seedSuggestRuntimeFromFieldParsers(childParser, Object.hasOwn(stateRecord, field) ? stateRecord[field] : void 0, runtime, [...parentPath, field]);
2793
+ require_dependency_runtime.collectSourcesFromState(state, runtime, /* @__PURE__ */ new WeakSet(), duplicateFieldNames);
2794
+ completeDependencySourceDefaults({
2795
+ buffer: [],
2796
+ optionsTerminated: false,
2797
+ state: stateRecord,
2798
+ usage: parser.usage,
2799
+ dependencyRegistry: runtime.registry,
2800
+ exec: {
2801
+ usage: parser.usage,
2802
+ phase: "suggest",
2803
+ path: parentPath,
2804
+ dependencyRuntime: runtime,
2805
+ dependencyRegistry: runtime.registry
2806
+ }
2807
+ }, pairs, runtime.registry, {
2808
+ usage: parser.usage,
2809
+ phase: "suggest",
2810
+ path: parentPath,
2811
+ dependencyRuntime: runtime,
2812
+ dependencyRegistry: runtime.registry
2813
+ });
2814
+ for (const [field, childParser] of pairs) seedSuggestRuntimeFromFieldParsers(childParser, getAnnotatedFieldState(stateRecord, field, childParser), runtime, [...parentPath, field]);
2485
2815
  }
2486
2816
  async function seedSuggestRuntimeFromFieldParsersAsync(parser, state, runtime, parentPath) {
2487
2817
  if (!(fieldParsersKey in parser)) return;
@@ -2489,11 +2819,31 @@ async function seedSuggestRuntimeFromFieldParsersAsync(parser, state, runtime, p
2489
2819
  const duplicateFieldNames = collectDuplicateFieldNames(rawPairs);
2490
2820
  const pairs = filterDuplicateFieldParsers(rawPairs);
2491
2821
  const stateRecord = state != null && typeof state === "object" ? state : {};
2492
- const nodes = require_dependency_runtime.buildRuntimeNodesFromPairs(pairs, stateRecord, parentPath);
2822
+ const nodes = buildSuggestRuntimeNodesFromPairs(pairs, stateRecord, parentPath);
2493
2823
  await require_dependency_runtime.collectExplicitSourceValuesAsync(nodes, runtime);
2494
- require_dependency_runtime.collectSourcesFromState(state, runtime, /* @__PURE__ */ new WeakSet(), duplicateFieldNames);
2495
2824
  await require_dependency_runtime.fillMissingSourceDefaultsAsync(nodes, runtime);
2496
- for (const [field, childParser] of pairs) await seedSuggestRuntimeFromFieldParsersAsync(childParser, Object.hasOwn(stateRecord, field) ? stateRecord[field] : void 0, runtime, [...parentPath, field]);
2825
+ require_dependency_runtime.collectSourcesFromState(state, runtime, /* @__PURE__ */ new WeakSet(), duplicateFieldNames);
2826
+ await completeDependencySourceDefaultsAsync({
2827
+ buffer: [],
2828
+ optionsTerminated: false,
2829
+ state: stateRecord,
2830
+ usage: parser.usage,
2831
+ dependencyRegistry: runtime.registry,
2832
+ exec: {
2833
+ usage: parser.usage,
2834
+ phase: "suggest",
2835
+ path: parentPath,
2836
+ dependencyRuntime: runtime,
2837
+ dependencyRegistry: runtime.registry
2838
+ }
2839
+ }, pairs, runtime.registry, {
2840
+ usage: parser.usage,
2841
+ phase: "suggest",
2842
+ path: parentPath,
2843
+ dependencyRuntime: runtime,
2844
+ dependencyRegistry: runtime.registry
2845
+ });
2846
+ for (const [field, childParser] of pairs) await seedSuggestRuntimeFromFieldParsersAsync(childParser, getAnnotatedFieldState(stateRecord, field, childParser), runtime, [...parentPath, field]);
2497
2847
  }
2498
2848
  /**
2499
2849
  * This helper replays child parsers in priority order (mirroring
@@ -2502,7 +2852,7 @@ async function seedSuggestRuntimeFromFieldParsersAsync(parser, state, runtime, p
2502
2852
  */
2503
2853
  function preParseSuggestContext(context, parsers) {
2504
2854
  if (context.buffer.length < 1 || !Array.isArray(context.state)) return context;
2505
- return preParseSuggestLoop(context, context.state.slice(), parsers);
2855
+ return preParseSuggestLoop(context, require_annotations.annotateFreshArray(context.state, context.state.slice()), parsers);
2506
2856
  }
2507
2857
  /**
2508
2858
  * Async variant of {@link preParseSuggestContext} that awaits async child
@@ -2511,7 +2861,7 @@ function preParseSuggestContext(context, parsers) {
2511
2861
  */
2512
2862
  async function preParseSuggestContextAsync(context, parsers) {
2513
2863
  if (context.buffer.length < 1 || !Array.isArray(context.state)) return context;
2514
- return await preParseSuggestLoop(context, context.state.slice(), parsers);
2864
+ return await preParseSuggestLoop(context, require_annotations.annotateFreshArray(context.state, context.state.slice()), parsers);
2515
2865
  }
2516
2866
  /**
2517
2867
  * Shared loop for sync and async concat suggest pre-parse. When a child
@@ -2547,12 +2897,12 @@ function tryParseSuggestList(context, stateArray, parsers, matchedParsers, remai
2547
2897
  for (let ri = 0; ri < remaining.length; ri++) {
2548
2898
  const [parser, index] = remaining[ri];
2549
2899
  const parserState = index < stateArray.length ? stateArray[index] : parser.initialState;
2550
- const resultOrPromise = parser.parse(withChildContext(context, index, parserState));
2900
+ const resultOrPromise = parser.parse(withChildContext(context, index, parserState, parser));
2551
2901
  if (resultOrPromise != null && typeof resultOrPromise === "object" && "then" in resultOrPromise && typeof resultOrPromise.then === "function") {
2552
2902
  const tail = remaining.slice(ri + 1);
2553
2903
  return resultOrPromise.then((result$1) => {
2554
2904
  if (result$1.success && result$1.consumed.length > 0) {
2555
- stateArray[index] = result$1.next.state;
2905
+ stateArray[index] = getAnnotatedChildState(context.state, result$1.next.state, parser);
2556
2906
  matchedParsers.add(index);
2557
2907
  const mergedExec = mergeChildExec(context.exec, result$1.next.exec);
2558
2908
  return preParseSuggestLoop({
@@ -2579,7 +2929,7 @@ function tryParseSuggestList(context, stateArray, parsers, matchedParsers, remai
2579
2929
  }
2580
2930
  const result = resultOrPromise;
2581
2931
  if (result.success && result.consumed.length > 0) {
2582
- stateArray[index] = result.next.state;
2932
+ stateArray[index] = getAnnotatedChildState(context.state, result.next.state, parser);
2583
2933
  matchedParsers.add(index);
2584
2934
  const mergedExec = mergeChildExec(context.exec, result.next.exec);
2585
2935
  return {
@@ -2616,9 +2966,9 @@ function concat(...parsers) {
2616
2966
  const stateArray = currentContext.state;
2617
2967
  const remainingParsers = syncParsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
2618
2968
  for (const [parser, index] of remainingParsers) {
2619
- const result = parser.parse(withChildContext(currentContext, index, stateArray[index]));
2969
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
2620
2970
  if (result.success && result.consumed.length > 0) {
2621
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
2971
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
2622
2972
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
2623
2973
  currentContext = {
2624
2974
  ...currentContext,
@@ -2637,9 +2987,9 @@ function concat(...parsers) {
2637
2987
  } else if (!result.success && error.consumed < result.consumed) error = result;
2638
2988
  }
2639
2989
  if (!foundMatch) for (const [parser, index] of remainingParsers) {
2640
- const result = parser.parse(withChildContext(currentContext, index, stateArray[index]));
2990
+ const result = parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
2641
2991
  if (result.success && result.consumed.length < 1) {
2642
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
2992
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
2643
2993
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
2644
2994
  currentContext = {
2645
2995
  ...currentContext,
@@ -2682,9 +3032,9 @@ function concat(...parsers) {
2682
3032
  const stateArray = currentContext.state;
2683
3033
  const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
2684
3034
  for (const [parser, index] of remainingParsers) {
2685
- const result = await parser.parse(withChildContext(currentContext, index, stateArray[index]));
3035
+ const result = await parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
2686
3036
  if (result.success && result.consumed.length > 0) {
2687
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
3037
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
2688
3038
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
2689
3039
  currentContext = {
2690
3040
  ...currentContext,
@@ -2703,9 +3053,9 @@ function concat(...parsers) {
2703
3053
  } else if (!result.success && error.consumed < result.consumed) error = result;
2704
3054
  }
2705
3055
  if (!foundMatch) for (const [parser, index] of remainingParsers) {
2706
- const result = await parser.parse(withChildContext(currentContext, index, stateArray[index]));
3056
+ const result = await parser.parse(withChildContext(currentContext, index, stateArray[index], parser));
2707
3057
  if (result.success && result.consumed.length < 1) {
2708
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
3058
+ const newStateArray = require_annotations.annotateFreshArray(currentContext.state, stateArray.map((s, idx) => idx === index ? getAnnotatedChildState(currentContext.state, result.next.state, parser) : s));
2709
3059
  const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
2710
3060
  currentContext = {
2711
3061
  ...currentContext,
@@ -2740,7 +3090,14 @@ function concat(...parsers) {
2740
3090
  const runtime = exec?.dependencyRuntime ?? require_dependency_runtime.createDependencyRuntimeContext(exec?.dependencyRegistry);
2741
3091
  const childExec = {
2742
3092
  ...exec,
2743
- dependencyRuntime: runtime,
3093
+ dependencyRuntime: runtime
3094
+ };
3095
+ const concatPairs = buildIndexedParserPairs(syncParsers);
3096
+ const concatState = createAnnotatedArrayStateRecord(stateArray);
3097
+ const preCompleted = preCompleteAndRegisterDependencies(concatState, concatPairs, runtime.registry, childExec);
3098
+ require_dependency_runtime.collectExplicitSourceValues(filterPreCompletedRuntimeNodes(require_dependency_runtime.buildRuntimeNodesFromArray(syncParsers, stateArray, exec?.path), new Set(preCompleted.keys())), runtime);
3099
+ const phase3Exec = {
3100
+ ...childExec,
2744
3101
  preCompletedByParser: void 0
2745
3102
  };
2746
3103
  const resolvedArray = require_dependency_runtime.resolveStateWithRuntime(stateArray, runtime);
@@ -2749,8 +3106,8 @@ function concat(...parsers) {
2749
3106
  let hasDeferred = false;
2750
3107
  for (let i = 0; i < syncParsers.length; i++) {
2751
3108
  const parser = syncParsers[i];
2752
- const parserState = prepareStateForCompletion(resolvedArray[i], parser);
2753
- const result = unwrapCompleteResult(parser.complete(parserState, withChildExecPath(childExec, i)));
3109
+ const preCompletedResult = preCompleted.get(String(i));
3110
+ const result = preCompletedResult !== void 0 ? unwrapCompleteResult(preCompletedResult) : unwrapCompleteResult(parser.complete(prepareStateForCompletion(resolvedArray[i], parser), withChildExecPath(phase3Exec, i)));
2754
3111
  if (!result.success) return result;
2755
3112
  const baseIndex = results.length;
2756
3113
  if (Array.isArray(result.value)) {
@@ -2783,7 +3140,14 @@ function concat(...parsers) {
2783
3140
  const runtime = exec?.dependencyRuntime ?? require_dependency_runtime.createDependencyRuntimeContext(exec?.dependencyRegistry);
2784
3141
  const childExec = {
2785
3142
  ...exec,
2786
- dependencyRuntime: runtime,
3143
+ dependencyRuntime: runtime
3144
+ };
3145
+ const concatPairs = buildIndexedParserPairs(parsers);
3146
+ const concatState = createAnnotatedArrayStateRecord(stateArray);
3147
+ const preCompleted = await preCompleteAndRegisterDependenciesAsync(concatState, concatPairs, runtime.registry, childExec);
3148
+ await require_dependency_runtime.collectExplicitSourceValuesAsync(filterPreCompletedRuntimeNodes(require_dependency_runtime.buildRuntimeNodesFromArray(parsers, stateArray, exec?.path), new Set(preCompleted.keys())), runtime);
3149
+ const phase3Exec = {
3150
+ ...childExec,
2787
3151
  preCompletedByParser: void 0
2788
3152
  };
2789
3153
  const resolvedArray = await require_dependency_runtime.resolveStateWithRuntimeAsync(stateArray, runtime);
@@ -2792,8 +3156,8 @@ function concat(...parsers) {
2792
3156
  let hasDeferred = false;
2793
3157
  for (let i = 0; i < parsers.length; i++) {
2794
3158
  const parser = parsers[i];
2795
- const parserState = prepareStateForCompletion(resolvedArray[i], parser);
2796
- const result = unwrapCompleteResult(await parser.complete(parserState, withChildExecPath(childExec, i)));
3159
+ const preCompletedResult = preCompleted.get(String(i));
3160
+ const result = preCompletedResult !== void 0 ? unwrapCompleteResult(preCompletedResult) : unwrapCompleteResult(await parser.complete(prepareStateForCompletion(resolvedArray[i], parser), withChildExecPath(phase3Exec, i)));
2797
3161
  if (!result.success) return result;
2798
3162
  const baseIndex = results.length;
2799
3163
  if (Array.isArray(result.value)) {
@@ -2821,7 +3185,7 @@ function concat(...parsers) {
2821
3185
  } : {}
2822
3186
  };
2823
3187
  };
2824
- return {
3188
+ const concatParser = {
2825
3189
  $mode: combinedMode,
2826
3190
  $valueType: [],
2827
3191
  $stateType: [],
@@ -2846,7 +3210,7 @@ function concat(...parsers) {
2846
3210
  for (let i = 0; i < parsers.length; i++) {
2847
3211
  const parser = parsers[i];
2848
3212
  const parserState = stateArray$1 && Array.isArray(stateArray$1) ? stateArray$1[i] : parser.initialState;
2849
- const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry$1, i, parserState), prefix);
3213
+ const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry$1, i, parserState, parser), prefix);
2850
3214
  if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
2851
3215
  else suggestions.push(...parserSuggestions);
2852
3216
  }
@@ -2859,7 +3223,7 @@ function concat(...parsers) {
2859
3223
  for (let i = 0; i < syncParsers.length; i++) {
2860
3224
  const parser = syncParsers[i];
2861
3225
  const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
2862
- const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState), prefix);
3226
+ const parserSuggestions = parser.suggest(withChildContext(contextWithRegistry, i, parserState, parser), prefix);
2863
3227
  suggestions.push(...parserSuggestions);
2864
3228
  }
2865
3229
  yield* require_suggestion.deduplicateSuggestions(suggestions);
@@ -2891,6 +3255,8 @@ function concat(...parsers) {
2891
3255
  return { fragments: result };
2892
3256
  }
2893
3257
  };
3258
+ require_parser.defineInheritedAnnotationParser(concatParser);
3259
+ return concatParser;
2894
3260
  }
2895
3261
  function group(label, parser, options = {}) {
2896
3262
  require_validate.validateLabel(label);
@@ -3054,7 +3420,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3054
3420
  const syncDefaultBranch = defaultBranch;
3055
3421
  if (state.selectedBranch !== void 0) {
3056
3422
  const branchParser = state.selectedBranch.kind === "default" ? syncDefaultBranch : syncBranches[state.selectedBranch.key];
3057
- const branchResult = branchParser.parse(withChildContext(context, "_branch", state.branchState, branchParser.usage));
3423
+ const branchResult = branchParser.parse(withChildContext(context, "_branch", state.branchState, branchParser, branchParser.usage));
3058
3424
  if (branchResult.success) {
3059
3425
  const mergedExec = mergeChildExec(context.exec, branchResult.next.exec);
3060
3426
  return {
@@ -3090,7 +3456,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3090
3456
  exec: discriminatorExec,
3091
3457
  dependencyRegistry: discriminatorExec.dependencyRegistry
3092
3458
  } : {}
3093
- }, "_branch", branchParser.initialState, branchParser.usage),
3459
+ }, "_branch", branchParser.initialState, branchParser, branchParser.usage),
3094
3460
  buffer: discriminatorResult.next.buffer,
3095
3461
  optionsTerminated: discriminatorResult.next.optionsTerminated
3096
3462
  });
@@ -3141,7 +3507,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3141
3507
  }
3142
3508
  }
3143
3509
  if (syncDefaultBranch !== void 0) {
3144
- const defaultResult = syncDefaultBranch.parse(withChildContext(context, "_branch", state.branchState ?? syncDefaultBranch.initialState, syncDefaultBranch.usage));
3510
+ const defaultResult = syncDefaultBranch.parse(withChildContext(context, "_branch", state.branchState ?? syncDefaultBranch.initialState, syncDefaultBranch, syncDefaultBranch.usage));
3145
3511
  if (defaultResult.success && defaultResult.consumed.length > 0) {
3146
3512
  const mergedExec = mergeChildExec(context.exec, defaultResult.next.exec);
3147
3513
  return {
@@ -3172,7 +3538,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3172
3538
  const state = context.state ?? initialState;
3173
3539
  if (state.selectedBranch !== void 0) {
3174
3540
  const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
3175
- const branchResult = await branchParser.parse(withChildContext(context, "_branch", state.branchState, branchParser.usage));
3541
+ const branchResult = await branchParser.parse(withChildContext(context, "_branch", state.branchState, branchParser, branchParser.usage));
3176
3542
  if (branchResult.success) {
3177
3543
  const mergedExec = mergeChildExec(context.exec, branchResult.next.exec);
3178
3544
  return {
@@ -3208,7 +3574,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3208
3574
  exec: discriminatorExec,
3209
3575
  dependencyRegistry: discriminatorExec.dependencyRegistry
3210
3576
  } : {}
3211
- }, "_branch", branchParser.initialState, branchParser.usage),
3577
+ }, "_branch", branchParser.initialState, branchParser, branchParser.usage),
3212
3578
  buffer: discriminatorResult.next.buffer,
3213
3579
  optionsTerminated: discriminatorResult.next.optionsTerminated
3214
3580
  });
@@ -3259,7 +3625,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3259
3625
  }
3260
3626
  }
3261
3627
  if (defaultBranch !== void 0) {
3262
- const defaultResult = await defaultBranch.parse(withChildContext(context, "_branch", state.branchState ?? defaultBranch.initialState, defaultBranch.usage));
3628
+ const defaultResult = await defaultBranch.parse(withChildContext(context, "_branch", state.branchState ?? defaultBranch.initialState, defaultBranch, defaultBranch.usage));
3263
3629
  if (defaultResult.success && defaultResult.consumed.length > 0) {
3264
3630
  const mergedExec = mergeChildExec(context.exec, defaultResult.next.exec);
3265
3631
  return {
@@ -3293,7 +3659,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3293
3659
  if (state.selectedBranch === void 0) {
3294
3660
  if (syncDefaultBranch !== void 0) {
3295
3661
  const branchState = state.branchState ?? syncDefaultBranch.initialState;
3296
- const defaultResult = syncDefaultBranch.complete(branchState, withChildExecPath(exec, "_branch"));
3662
+ const defaultResult = unwrapCompleteResult(syncDefaultBranch.complete(branchState, withChildExecPath(exec, "_branch")));
3297
3663
  if (!defaultResult.success) return defaultResult;
3298
3664
  return {
3299
3665
  success: true,
@@ -3329,7 +3695,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3329
3695
  dependencyRegistry: runtime.registry
3330
3696
  };
3331
3697
  const discriminatorCompleteResult = state.selectedBranch.kind === "default" ? void 0 : syncDiscriminator.complete(state.discriminatorState, withChildExecPath(completionExec, "_discriminator"));
3332
- const branchResult = branchParser.complete(resolvedBranchState, withChildExecPath(completionExec, "_branch"));
3698
+ const branchResult = unwrapCompleteResult(branchParser.complete(resolvedBranchState, withChildExecPath(completionExec, "_branch")));
3333
3699
  if (!branchResult.success) {
3334
3700
  if (state.discriminatorValue !== void 0 && options?.errors?.branchError) return {
3335
3701
  success: false,
@@ -3356,7 +3722,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3356
3722
  if (state.selectedBranch === void 0) {
3357
3723
  if (defaultBranch !== void 0) {
3358
3724
  const branchState = state.branchState ?? defaultBranch.initialState;
3359
- const defaultResult = await defaultBranch.complete(branchState, withChildExecPath(exec, "_branch"));
3725
+ const defaultResult = unwrapCompleteResult(await defaultBranch.complete(branchState, withChildExecPath(exec, "_branch")));
3360
3726
  if (!defaultResult.success) return defaultResult;
3361
3727
  return {
3362
3728
  success: true,
@@ -3392,7 +3758,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3392
3758
  dependencyRegistry: runtime.registry
3393
3759
  };
3394
3760
  const discriminatorCompleteResult = state.selectedBranch.kind === "default" ? void 0 : await discriminator.complete(state.discriminatorState, withChildExecPath(completionExec, "_discriminator"));
3395
- const branchResult = await branchParser.complete(resolvedBranchState, withChildExecPath(completionExec, "_branch"));
3761
+ const branchResult = unwrapCompleteResult(await branchParser.complete(resolvedBranchState, withChildExecPath(completionExec, "_branch")));
3396
3762
  if (!branchResult.success) {
3397
3763
  if (state.discriminatorValue !== void 0 && options?.errors?.branchError) return {
3398
3764
  success: false,
@@ -3506,7 +3872,7 @@ function conditional(discriminator, branches, defaultBranch, options) {
3506
3872
  yield* branchParser.suggest(withChildContext(suggestContext, "_branch", state.branchState), prefix);
3507
3873
  }
3508
3874
  }
3509
- return {
3875
+ const conditionalParser = {
3510
3876
  $mode: combinedMode,
3511
3877
  $valueType: [],
3512
3878
  $stateType: [],
@@ -3554,6 +3920,8 @@ function conditional(discriminator, branches, defaultBranch, options) {
3554
3920
  return { fragments };
3555
3921
  }
3556
3922
  };
3923
+ require_parser.defineInheritedAnnotationParser(conditionalParser);
3924
+ return conditionalParser;
3557
3925
  }
3558
3926
 
3559
3927
  //#endregion