@optique/core 1.0.0-dev.523 → 1.0.0-dev.552

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.
@@ -1,3 +1,4 @@
1
+ const require_annotations = require('./annotations.cjs');
1
2
  const require_message = require('./message.cjs');
2
3
  const require_dependency = require('./dependency.cjs');
3
4
  const require_mode_dispatch = require('./mode-dispatch.cjs');
@@ -409,50 +410,112 @@ function map(parser, transform) {
409
410
  function multiple(parser, options = {}) {
410
411
  const syncParser = parser;
411
412
  const { min = 0, max = Infinity } = options;
413
+ const unwrapInjectedWrapper = require_annotations.unwrapInjectedAnnotationWrapper;
414
+ const annotateFreshArray = (source, target) => {
415
+ const annotations = require_annotations.getAnnotations(source);
416
+ if (annotations === void 0) return target;
417
+ const annotated = target;
418
+ annotated[require_annotations.annotationKey] = annotations;
419
+ return annotated;
420
+ };
421
+ const completeSyncWithUnwrappedFallback = (state) => {
422
+ try {
423
+ return syncParser.complete(state);
424
+ } catch (error) {
425
+ if (!require_annotations.isInjectedAnnotationWrapper(state)) throw error;
426
+ return syncParser.complete(unwrapInjectedWrapper(state));
427
+ }
428
+ };
429
+ const parseSyncWithUnwrappedFallback = (context) => {
430
+ try {
431
+ const result = syncParser.parse(context);
432
+ if (result.success || result.consumed !== 0 || !require_annotations.isInjectedAnnotationWrapper(context.state)) return result;
433
+ return syncParser.parse({
434
+ ...context,
435
+ state: unwrapInjectedWrapper(context.state)
436
+ });
437
+ } catch (error) {
438
+ if (!require_annotations.isInjectedAnnotationWrapper(context.state)) throw error;
439
+ return syncParser.parse({
440
+ ...context,
441
+ state: unwrapInjectedWrapper(context.state)
442
+ });
443
+ }
444
+ };
445
+ const completeAsyncWithUnwrappedFallback = async (state) => {
446
+ try {
447
+ return await parser.complete(state);
448
+ } catch (error) {
449
+ if (!require_annotations.isInjectedAnnotationWrapper(state)) throw error;
450
+ return await parser.complete(unwrapInjectedWrapper(state));
451
+ }
452
+ };
453
+ const parseAsyncWithUnwrappedFallback = async (context) => {
454
+ try {
455
+ const result = await parser.parse(context);
456
+ if (result.success || result.consumed !== 0 || !require_annotations.isInjectedAnnotationWrapper(context.state)) return result;
457
+ return await parser.parse({
458
+ ...context,
459
+ state: unwrapInjectedWrapper(context.state)
460
+ });
461
+ } catch (error) {
462
+ if (!require_annotations.isInjectedAnnotationWrapper(context.state)) throw error;
463
+ return await parser.parse({
464
+ ...context,
465
+ state: unwrapInjectedWrapper(context.state)
466
+ });
467
+ }
468
+ };
412
469
  const parseSync = (context) => {
413
470
  let added = context.state.length < 1;
414
- let result = syncParser.parse({
471
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? require_annotations.inheritAnnotations(context.state, syncParser.initialState);
472
+ let result = parseSyncWithUnwrappedFallback({
415
473
  ...context,
416
- state: context.state.at(-1) ?? syncParser.initialState
474
+ state: currentItemStateWithAnnotations
417
475
  });
418
476
  if (!result.success) if (!added) {
419
- result = syncParser.parse({
477
+ const nextInitialState = require_annotations.inheritAnnotations(context.state, syncParser.initialState);
478
+ result = parseSyncWithUnwrappedFallback({
420
479
  ...context,
421
- state: syncParser.initialState
480
+ state: nextInitialState
422
481
  });
423
482
  if (!result.success) return result;
424
483
  added = true;
425
484
  } else return result;
485
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
486
+ const nextItemState = require_annotations.inheritAnnotations(itemAnnotationSource, result.next.state);
426
487
  return {
427
488
  success: true,
428
489
  next: {
429
490
  ...result.next,
430
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
491
+ state: annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
431
492
  },
432
493
  consumed: result.consumed
433
494
  };
434
495
  };
435
496
  const parseAsync = async (context) => {
436
497
  let added = context.state.length < 1;
437
- let resultOrPromise = parser.parse({
498
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? require_annotations.inheritAnnotations(context.state, parser.initialState);
499
+ let result = await parseAsyncWithUnwrappedFallback({
438
500
  ...context,
439
- state: context.state.at(-1) ?? parser.initialState
501
+ state: currentItemStateWithAnnotations
440
502
  });
441
- let result = await resultOrPromise;
442
503
  if (!result.success) if (!added) {
443
- resultOrPromise = parser.parse({
504
+ const nextInitialState = require_annotations.inheritAnnotations(context.state, parser.initialState);
505
+ result = await parseAsyncWithUnwrappedFallback({
444
506
  ...context,
445
- state: parser.initialState
507
+ state: nextInitialState
446
508
  });
447
- result = await resultOrPromise;
448
509
  if (!result.success) return result;
449
510
  added = true;
450
511
  } else return result;
512
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
513
+ const nextItemState = require_annotations.inheritAnnotations(itemAnnotationSource, result.next.state);
451
514
  return {
452
515
  success: true,
453
516
  next: {
454
517
  ...result.next,
455
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
518
+ state: annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
456
519
  },
457
520
  consumed: result.consumed
458
521
  };
@@ -475,8 +538,8 @@ function multiple(parser, options = {}) {
475
538
  return require_mode_dispatch.dispatchByMode(parser.$mode, () => {
476
539
  const result = [];
477
540
  for (const s of state) {
478
- const valueResult = syncParser.complete(s);
479
- if (valueResult.success) result.push(valueResult.value);
541
+ const valueResult = completeSyncWithUnwrappedFallback(s);
542
+ if (valueResult.success) result.push(unwrapInjectedWrapper(valueResult.value));
480
543
  else return {
481
544
  success: false,
482
545
  error: valueResult.error
@@ -484,9 +547,9 @@ function multiple(parser, options = {}) {
484
547
  }
485
548
  return validateMultipleResult(result);
486
549
  }, async () => {
487
- const results = await Promise.all(state.map((s) => parser.complete(s)));
550
+ const results = await Promise.all(state.map((s) => completeAsyncWithUnwrappedFallback(s)));
488
551
  const values = [];
489
- for (const valueResult of results) if (valueResult.success) values.push(valueResult.value);
552
+ for (const valueResult of results) if (valueResult.success) values.push(unwrapInjectedWrapper(valueResult.value));
490
553
  else return {
491
554
  success: false,
492
555
  error: valueResult.error
@@ -496,10 +559,13 @@ function multiple(parser, options = {}) {
496
559
  },
497
560
  suggest(context, prefix) {
498
561
  const selectedValues = /* @__PURE__ */ new Set();
562
+ const suggestInitialState = require_annotations.inheritAnnotations(context.state, parser.initialState);
563
+ const suggestFallbackState = unwrapInjectedWrapper(suggestInitialState);
564
+ const hasSuggestFallbackState = suggestFallbackState !== suggestInitialState;
499
565
  for (const s of context.state) {
500
- const completed = syncParser.complete(s);
566
+ const completed = completeSyncWithUnwrappedFallback(s);
501
567
  if (completed.success) {
502
- const valueStr = String(completed.value);
568
+ const valueStr = String(unwrapInjectedWrapper(completed.value));
503
569
  selectedValues.add(valueStr);
504
570
  }
505
571
  }
@@ -507,23 +573,80 @@ function multiple(parser, options = {}) {
507
573
  if (suggestion.kind === "literal") return !selectedValues.has(suggestion.text);
508
574
  return true;
509
575
  };
576
+ const suggestionKey = (suggestion) => {
577
+ const description = suggestion.description == null ? "" : require_message.formatMessage(suggestion.description);
578
+ if (suggestion.kind === "literal") return JSON.stringify([
579
+ "literal",
580
+ suggestion.text,
581
+ description
582
+ ]);
583
+ return JSON.stringify([
584
+ "file",
585
+ suggestion.type,
586
+ suggestion.pattern ?? "",
587
+ suggestion.includeHidden === true,
588
+ suggestion.extensions == null ? "" : suggestion.extensions.join("\0"),
589
+ description
590
+ ]);
591
+ };
510
592
  return require_mode_dispatch.dispatchIterableByMode(parser.$mode, function* () {
511
- for (const s of syncParser.suggest({
593
+ const emitted = /* @__PURE__ */ new Set();
594
+ const yieldUnique = function* (suggestions) {
595
+ for (const s of suggestions) {
596
+ const key = suggestionKey(s);
597
+ if (shouldInclude(s) && !emitted.has(key)) {
598
+ emitted.add(key);
599
+ yield s;
600
+ }
601
+ }
602
+ };
603
+ let shouldTryFallback = false;
604
+ try {
605
+ yield* yieldUnique(syncParser.suggest({
606
+ ...context,
607
+ state: suggestInitialState
608
+ }, prefix));
609
+ } catch (error) {
610
+ if (!hasSuggestFallbackState) throw error;
611
+ shouldTryFallback = true;
612
+ }
613
+ if (shouldTryFallback) yield* yieldUnique(syncParser.suggest({
512
614
  ...context,
513
- state: parser.initialState
514
- }, prefix)) if (shouldInclude(s)) yield s;
615
+ state: suggestFallbackState
616
+ }, prefix));
515
617
  }, async function* () {
516
- const suggestions = parser.suggest({
618
+ const emitted = /* @__PURE__ */ new Set();
619
+ const yieldUnique = async function* (suggestions) {
620
+ for await (const s of suggestions) {
621
+ const key = suggestionKey(s);
622
+ if (shouldInclude(s) && !emitted.has(key)) {
623
+ emitted.add(key);
624
+ yield s;
625
+ }
626
+ }
627
+ };
628
+ let shouldTryFallback = false;
629
+ try {
630
+ yield* yieldUnique(parser.suggest({
631
+ ...context,
632
+ state: suggestInitialState
633
+ }, prefix));
634
+ } catch (error) {
635
+ if (!hasSuggestFallbackState) throw error;
636
+ shouldTryFallback = true;
637
+ }
638
+ if (shouldTryFallback) yield* yieldUnique(parser.suggest({
517
639
  ...context,
518
- state: parser.initialState
519
- }, prefix);
520
- for await (const s of suggestions) if (shouldInclude(s)) yield s;
640
+ state: suggestFallbackState
641
+ }, prefix));
521
642
  });
522
643
  },
523
644
  getDocFragments(state, defaultValue) {
524
- const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
645
+ const latestState = state.kind === "available" && state.state.length > 0 ? state.state.at(-1) : void 0;
646
+ const latestInnerState = latestState != null && require_annotations.isInjectedAnnotationWrapper(latestState) ? unwrapInjectedWrapper(latestState) : latestState;
647
+ const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : latestInnerState !== void 0 ? {
525
648
  kind: "available",
526
- state: state.state.at(-1)
649
+ state: latestInnerState
527
650
  } : { kind: "unavailable" };
528
651
  return syncParser.getDocFragments(innerState, defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : void 0);
529
652
  }
package/dist/modifiers.js CHANGED
@@ -1,3 +1,4 @@
1
+ import { annotationKey, getAnnotations, inheritAnnotations, isInjectedAnnotationWrapper, unwrapInjectedAnnotationWrapper } from "./annotations.js";
1
2
  import { formatMessage, message, text } from "./message.js";
2
3
  import { createDependencySourceState, dependencyId, isDependencySourceState, isPendingDependencySourceState, isWrappedDependencySource, transformsDependencyValue, transformsDependencyValueMarker, wrappedDependencySourceMarker } from "./dependency.js";
3
4
  import { dispatchByMode, dispatchIterableByMode, mapModeValue } from "./mode-dispatch.js";
@@ -409,50 +410,112 @@ function map(parser, transform) {
409
410
  function multiple(parser, options = {}) {
410
411
  const syncParser = parser;
411
412
  const { min = 0, max = Infinity } = options;
413
+ const unwrapInjectedWrapper = unwrapInjectedAnnotationWrapper;
414
+ const annotateFreshArray = (source, target) => {
415
+ const annotations = getAnnotations(source);
416
+ if (annotations === void 0) return target;
417
+ const annotated = target;
418
+ annotated[annotationKey] = annotations;
419
+ return annotated;
420
+ };
421
+ const completeSyncWithUnwrappedFallback = (state) => {
422
+ try {
423
+ return syncParser.complete(state);
424
+ } catch (error) {
425
+ if (!isInjectedAnnotationWrapper(state)) throw error;
426
+ return syncParser.complete(unwrapInjectedWrapper(state));
427
+ }
428
+ };
429
+ const parseSyncWithUnwrappedFallback = (context) => {
430
+ try {
431
+ const result = syncParser.parse(context);
432
+ if (result.success || result.consumed !== 0 || !isInjectedAnnotationWrapper(context.state)) return result;
433
+ return syncParser.parse({
434
+ ...context,
435
+ state: unwrapInjectedWrapper(context.state)
436
+ });
437
+ } catch (error) {
438
+ if (!isInjectedAnnotationWrapper(context.state)) throw error;
439
+ return syncParser.parse({
440
+ ...context,
441
+ state: unwrapInjectedWrapper(context.state)
442
+ });
443
+ }
444
+ };
445
+ const completeAsyncWithUnwrappedFallback = async (state) => {
446
+ try {
447
+ return await parser.complete(state);
448
+ } catch (error) {
449
+ if (!isInjectedAnnotationWrapper(state)) throw error;
450
+ return await parser.complete(unwrapInjectedWrapper(state));
451
+ }
452
+ };
453
+ const parseAsyncWithUnwrappedFallback = async (context) => {
454
+ try {
455
+ const result = await parser.parse(context);
456
+ if (result.success || result.consumed !== 0 || !isInjectedAnnotationWrapper(context.state)) return result;
457
+ return await parser.parse({
458
+ ...context,
459
+ state: unwrapInjectedWrapper(context.state)
460
+ });
461
+ } catch (error) {
462
+ if (!isInjectedAnnotationWrapper(context.state)) throw error;
463
+ return await parser.parse({
464
+ ...context,
465
+ state: unwrapInjectedWrapper(context.state)
466
+ });
467
+ }
468
+ };
412
469
  const parseSync = (context) => {
413
470
  let added = context.state.length < 1;
414
- let result = syncParser.parse({
471
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? inheritAnnotations(context.state, syncParser.initialState);
472
+ let result = parseSyncWithUnwrappedFallback({
415
473
  ...context,
416
- state: context.state.at(-1) ?? syncParser.initialState
474
+ state: currentItemStateWithAnnotations
417
475
  });
418
476
  if (!result.success) if (!added) {
419
- result = syncParser.parse({
477
+ const nextInitialState = inheritAnnotations(context.state, syncParser.initialState);
478
+ result = parseSyncWithUnwrappedFallback({
420
479
  ...context,
421
- state: syncParser.initialState
480
+ state: nextInitialState
422
481
  });
423
482
  if (!result.success) return result;
424
483
  added = true;
425
484
  } else return result;
485
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
486
+ const nextItemState = inheritAnnotations(itemAnnotationSource, result.next.state);
426
487
  return {
427
488
  success: true,
428
489
  next: {
429
490
  ...result.next,
430
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
491
+ state: annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
431
492
  },
432
493
  consumed: result.consumed
433
494
  };
434
495
  };
435
496
  const parseAsync = async (context) => {
436
497
  let added = context.state.length < 1;
437
- let resultOrPromise = parser.parse({
498
+ const currentItemStateWithAnnotations = context.state.at(-1) ?? inheritAnnotations(context.state, parser.initialState);
499
+ let result = await parseAsyncWithUnwrappedFallback({
438
500
  ...context,
439
- state: context.state.at(-1) ?? parser.initialState
501
+ state: currentItemStateWithAnnotations
440
502
  });
441
- let result = await resultOrPromise;
442
503
  if (!result.success) if (!added) {
443
- resultOrPromise = parser.parse({
504
+ const nextInitialState = inheritAnnotations(context.state, parser.initialState);
505
+ result = await parseAsyncWithUnwrappedFallback({
444
506
  ...context,
445
- state: parser.initialState
507
+ state: nextInitialState
446
508
  });
447
- result = await resultOrPromise;
448
509
  if (!result.success) return result;
449
510
  added = true;
450
511
  } else return result;
512
+ const itemAnnotationSource = added ? context.state : currentItemStateWithAnnotations;
513
+ const nextItemState = inheritAnnotations(itemAnnotationSource, result.next.state);
451
514
  return {
452
515
  success: true,
453
516
  next: {
454
517
  ...result.next,
455
- state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
518
+ state: annotateFreshArray(context.state, [...added ? context.state : context.state.slice(0, -1), nextItemState])
456
519
  },
457
520
  consumed: result.consumed
458
521
  };
@@ -475,8 +538,8 @@ function multiple(parser, options = {}) {
475
538
  return dispatchByMode(parser.$mode, () => {
476
539
  const result = [];
477
540
  for (const s of state) {
478
- const valueResult = syncParser.complete(s);
479
- if (valueResult.success) result.push(valueResult.value);
541
+ const valueResult = completeSyncWithUnwrappedFallback(s);
542
+ if (valueResult.success) result.push(unwrapInjectedWrapper(valueResult.value));
480
543
  else return {
481
544
  success: false,
482
545
  error: valueResult.error
@@ -484,9 +547,9 @@ function multiple(parser, options = {}) {
484
547
  }
485
548
  return validateMultipleResult(result);
486
549
  }, async () => {
487
- const results = await Promise.all(state.map((s) => parser.complete(s)));
550
+ const results = await Promise.all(state.map((s) => completeAsyncWithUnwrappedFallback(s)));
488
551
  const values = [];
489
- for (const valueResult of results) if (valueResult.success) values.push(valueResult.value);
552
+ for (const valueResult of results) if (valueResult.success) values.push(unwrapInjectedWrapper(valueResult.value));
490
553
  else return {
491
554
  success: false,
492
555
  error: valueResult.error
@@ -496,10 +559,13 @@ function multiple(parser, options = {}) {
496
559
  },
497
560
  suggest(context, prefix) {
498
561
  const selectedValues = /* @__PURE__ */ new Set();
562
+ const suggestInitialState = inheritAnnotations(context.state, parser.initialState);
563
+ const suggestFallbackState = unwrapInjectedWrapper(suggestInitialState);
564
+ const hasSuggestFallbackState = suggestFallbackState !== suggestInitialState;
499
565
  for (const s of context.state) {
500
- const completed = syncParser.complete(s);
566
+ const completed = completeSyncWithUnwrappedFallback(s);
501
567
  if (completed.success) {
502
- const valueStr = String(completed.value);
568
+ const valueStr = String(unwrapInjectedWrapper(completed.value));
503
569
  selectedValues.add(valueStr);
504
570
  }
505
571
  }
@@ -507,23 +573,80 @@ function multiple(parser, options = {}) {
507
573
  if (suggestion.kind === "literal") return !selectedValues.has(suggestion.text);
508
574
  return true;
509
575
  };
576
+ const suggestionKey = (suggestion) => {
577
+ const description = suggestion.description == null ? "" : formatMessage(suggestion.description);
578
+ if (suggestion.kind === "literal") return JSON.stringify([
579
+ "literal",
580
+ suggestion.text,
581
+ description
582
+ ]);
583
+ return JSON.stringify([
584
+ "file",
585
+ suggestion.type,
586
+ suggestion.pattern ?? "",
587
+ suggestion.includeHidden === true,
588
+ suggestion.extensions == null ? "" : suggestion.extensions.join("\0"),
589
+ description
590
+ ]);
591
+ };
510
592
  return dispatchIterableByMode(parser.$mode, function* () {
511
- for (const s of syncParser.suggest({
593
+ const emitted = /* @__PURE__ */ new Set();
594
+ const yieldUnique = function* (suggestions) {
595
+ for (const s of suggestions) {
596
+ const key = suggestionKey(s);
597
+ if (shouldInclude(s) && !emitted.has(key)) {
598
+ emitted.add(key);
599
+ yield s;
600
+ }
601
+ }
602
+ };
603
+ let shouldTryFallback = false;
604
+ try {
605
+ yield* yieldUnique(syncParser.suggest({
606
+ ...context,
607
+ state: suggestInitialState
608
+ }, prefix));
609
+ } catch (error) {
610
+ if (!hasSuggestFallbackState) throw error;
611
+ shouldTryFallback = true;
612
+ }
613
+ if (shouldTryFallback) yield* yieldUnique(syncParser.suggest({
512
614
  ...context,
513
- state: parser.initialState
514
- }, prefix)) if (shouldInclude(s)) yield s;
615
+ state: suggestFallbackState
616
+ }, prefix));
515
617
  }, async function* () {
516
- const suggestions = parser.suggest({
618
+ const emitted = /* @__PURE__ */ new Set();
619
+ const yieldUnique = async function* (suggestions) {
620
+ for await (const s of suggestions) {
621
+ const key = suggestionKey(s);
622
+ if (shouldInclude(s) && !emitted.has(key)) {
623
+ emitted.add(key);
624
+ yield s;
625
+ }
626
+ }
627
+ };
628
+ let shouldTryFallback = false;
629
+ try {
630
+ yield* yieldUnique(parser.suggest({
631
+ ...context,
632
+ state: suggestInitialState
633
+ }, prefix));
634
+ } catch (error) {
635
+ if (!hasSuggestFallbackState) throw error;
636
+ shouldTryFallback = true;
637
+ }
638
+ if (shouldTryFallback) yield* yieldUnique(parser.suggest({
517
639
  ...context,
518
- state: parser.initialState
519
- }, prefix);
520
- for await (const s of suggestions) if (shouldInclude(s)) yield s;
640
+ state: suggestFallbackState
641
+ }, prefix));
521
642
  });
522
643
  },
523
644
  getDocFragments(state, defaultValue) {
524
- const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
645
+ const latestState = state.kind === "available" && state.state.length > 0 ? state.state.at(-1) : void 0;
646
+ const latestInnerState = latestState != null && isInjectedAnnotationWrapper(latestState) ? unwrapInjectedWrapper(latestState) : latestState;
647
+ const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : latestInnerState !== void 0 ? {
525
648
  kind: "available",
526
- state: state.state.at(-1)
649
+ state: latestInnerState
527
650
  } : { kind: "unavailable" };
528
651
  return syncParser.getDocFragments(innerState, defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : void 0);
529
652
  }
package/dist/parser.cjs CHANGED
@@ -7,6 +7,11 @@ const require_modifiers = require('./modifiers.cjs');
7
7
  const require_primitives = require('./primitives.cjs');
8
8
 
9
9
  //#region src/parser.ts
10
+ function injectAnnotationsIntoState(state, options) {
11
+ const annotations = options?.annotations;
12
+ if (annotations == null) return state;
13
+ return require_annotations.injectAnnotations(state, annotations);
14
+ }
10
15
  /**
11
16
  * Parses an array of command-line arguments using the provided combined parser.
12
17
  * This function processes the input arguments, applying the parser to each
@@ -30,11 +35,8 @@ const require_primitives = require('./primitives.cjs');
30
35
  * @since 0.10.0 Added optional `options` parameter for annotations support.
31
36
  */
32
37
  function parseSync(parser, args, options) {
33
- let initialState = parser.initialState;
34
- if (options?.annotations) initialState = {
35
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
36
- [require_annotations.annotationKey]: options.annotations
37
- };
38
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
39
+ const shouldUnwrapAnnotatedValue = options?.annotations != null || require_annotations.isInjectedAnnotationWrapper(parser.initialState);
38
40
  let context = {
39
41
  buffer: args,
40
42
  optionsTerminated: false,
@@ -57,7 +59,7 @@ function parseSync(parser, args, options) {
57
59
  const endResult = parser.complete(context.state);
58
60
  return endResult.success ? {
59
61
  success: true,
60
- value: endResult.value
62
+ value: shouldUnwrapAnnotatedValue ? require_annotations.unwrapInjectedAnnotationWrapper(endResult.value) : endResult.value
61
63
  } : {
62
64
  success: false,
63
65
  error: endResult.error
@@ -83,11 +85,8 @@ function parseSync(parser, args, options) {
83
85
  * @since 0.10.0 Added optional `options` parameter for annotations support.
84
86
  */
85
87
  async function parseAsync(parser, args, options) {
86
- let initialState = parser.initialState;
87
- if (options?.annotations) initialState = {
88
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
89
- [require_annotations.annotationKey]: options.annotations
90
- };
88
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
89
+ const shouldUnwrapAnnotatedValue = options?.annotations != null || require_annotations.isInjectedAnnotationWrapper(parser.initialState);
91
90
  let context = {
92
91
  buffer: args,
93
92
  optionsTerminated: false,
@@ -110,7 +109,7 @@ async function parseAsync(parser, args, options) {
110
109
  const endResult = await parser.complete(context.state);
111
110
  return endResult.success ? {
112
111
  success: true,
113
- value: endResult.value
112
+ value: shouldUnwrapAnnotatedValue ? require_annotations.unwrapInjectedAnnotationWrapper(endResult.value) : endResult.value
114
113
  } : {
115
114
  success: false,
116
115
  error: endResult.error
@@ -180,11 +179,7 @@ function parse(parser, args, options) {
180
179
  function suggestSync(parser, args, options) {
181
180
  const allButLast = args.slice(0, -1);
182
181
  const prefix = args[args.length - 1];
183
- let initialState = parser.initialState;
184
- if (options?.annotations) initialState = {
185
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
186
- [require_annotations.annotationKey]: options.annotations
187
- };
182
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
188
183
  let context = {
189
184
  buffer: allButLast,
190
185
  optionsTerminated: false,
@@ -223,11 +218,7 @@ function suggestSync(parser, args, options) {
223
218
  async function suggestAsync(parser, args, options) {
224
219
  const allButLast = args.slice(0, -1);
225
220
  const prefix = args[args.length - 1];
226
- let initialState = parser.initialState;
227
- if (options?.annotations) initialState = {
228
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
229
- [require_annotations.annotationKey]: options.annotations
230
- };
221
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
231
222
  let context = {
232
223
  buffer: allButLast,
233
224
  optionsTerminated: false,
@@ -338,11 +329,7 @@ function getDocPage(parser, args = [], options) {
338
329
  * Internal sync implementation of getDocPage.
339
330
  */
340
331
  function getDocPageSyncImpl(parser, args, options) {
341
- let initialState = parser.initialState;
342
- if (options?.annotations) initialState = {
343
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
344
- [require_annotations.annotationKey]: options.annotations
345
- };
332
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
346
333
  let context = {
347
334
  buffer: args,
348
335
  optionsTerminated: false,
@@ -360,11 +347,7 @@ function getDocPageSyncImpl(parser, args, options) {
360
347
  * Internal async implementation of getDocPage.
361
348
  */
362
349
  async function getDocPageAsyncImpl(parser, args, options) {
363
- let initialState = parser.initialState;
364
- if (options?.annotations) initialState = {
365
- ...typeof initialState === "object" && initialState !== null ? initialState : {},
366
- [require_annotations.annotationKey]: options.annotations
367
- };
350
+ const initialState = injectAnnotationsIntoState(parser.initialState, options);
368
351
  let context = {
369
352
  buffer: args,
370
353
  optionsTerminated: false,