@optique/core 1.0.0-dev.1665 → 1.0.0-dev.1667
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constructs.cjs +78 -48
- package/dist/constructs.js +78 -48
- package/package.json +1 -1
package/dist/constructs.cjs
CHANGED
|
@@ -484,6 +484,26 @@ function generateNoMatchError(context) {
|
|
|
484
484
|
else if (hasArguments && hasCommands && !hasOptions) return require_message.message`No matching command or argument found.`;
|
|
485
485
|
else return require_message.message`No matching option, command, or argument found.`;
|
|
486
486
|
}
|
|
487
|
+
function normalizeExclusiveState(state) {
|
|
488
|
+
if (!Array.isArray(state) || state.length !== 2 || typeof state[0] !== "number") return void 0;
|
|
489
|
+
return state;
|
|
490
|
+
}
|
|
491
|
+
function annotateExclusiveParserResult(parentState, parser, result) {
|
|
492
|
+
if (!result.success) return result;
|
|
493
|
+
const annotatedState = getAnnotatedChildState(parentState, result.next.state, parser);
|
|
494
|
+
if (annotatedState === result.next.state) return result;
|
|
495
|
+
return {
|
|
496
|
+
...result,
|
|
497
|
+
next: {
|
|
498
|
+
...result.next,
|
|
499
|
+
state: annotatedState
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function createExclusiveState(parentState, index, parser, result) {
|
|
504
|
+
const annotatedResult = annotateExclusiveParserResult(parentState, parser, result);
|
|
505
|
+
return require_annotations.annotateFreshArray(parentState, [index, annotatedResult]);
|
|
506
|
+
}
|
|
487
507
|
/**
|
|
488
508
|
* Creates a complete() method shared by or() and longestMatch().
|
|
489
509
|
* @internal
|
|
@@ -491,11 +511,12 @@ function generateNoMatchError(context) {
|
|
|
491
511
|
function createExclusiveComplete(parsers, options, noMatchContext, mode) {
|
|
492
512
|
const syncParsers = parsers;
|
|
493
513
|
return (state, exec) => {
|
|
494
|
-
|
|
514
|
+
const activeState = normalizeExclusiveState(state);
|
|
515
|
+
if (activeState == null) return {
|
|
495
516
|
success: false,
|
|
496
517
|
error: getNoMatchError(options, noMatchContext)
|
|
497
518
|
};
|
|
498
|
-
const [i, result] =
|
|
519
|
+
const [i, result] = activeState;
|
|
499
520
|
if (!result.success) return {
|
|
500
521
|
success: false,
|
|
501
522
|
error: result.error
|
|
@@ -515,32 +536,35 @@ function createExclusiveSuggest(parsers, mode) {
|
|
|
515
536
|
return (context, prefix) => {
|
|
516
537
|
return require_mode_dispatch.dispatchIterableByMode(mode, function* () {
|
|
517
538
|
const suggestions = [];
|
|
518
|
-
|
|
539
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
540
|
+
if (activeState == null) for (let i = 0; i < syncParsers.length; i++) {
|
|
519
541
|
const parser = syncParsers[i];
|
|
520
|
-
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState), prefix);
|
|
542
|
+
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState, parser), prefix);
|
|
521
543
|
suggestions.push(...parserSuggestions);
|
|
522
544
|
}
|
|
523
545
|
else {
|
|
524
|
-
const [index, parserResult] =
|
|
546
|
+
const [index, parserResult] = activeState;
|
|
525
547
|
if (parserResult.success) {
|
|
526
|
-
const
|
|
548
|
+
const parser = syncParsers[index];
|
|
549
|
+
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state, parser), prefix);
|
|
527
550
|
suggestions.push(...parserSuggestions);
|
|
528
551
|
}
|
|
529
552
|
}
|
|
530
553
|
yield* require_suggestion.deduplicateSuggestions(suggestions);
|
|
531
554
|
}, async function* () {
|
|
532
555
|
const suggestions = [];
|
|
533
|
-
|
|
556
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
557
|
+
if (activeState == null) for (let i = 0; i < parsers.length; i++) {
|
|
534
558
|
const parser = parsers[i];
|
|
535
|
-
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState), prefix);
|
|
559
|
+
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState, parser), prefix);
|
|
536
560
|
if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
|
|
537
561
|
else suggestions.push(...parserSuggestions);
|
|
538
562
|
}
|
|
539
563
|
else {
|
|
540
|
-
const [index, parserResult] =
|
|
564
|
+
const [index, parserResult] = activeState;
|
|
541
565
|
if (parserResult.success) {
|
|
542
566
|
const parser = parsers[index];
|
|
543
|
-
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state), prefix);
|
|
567
|
+
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state, parser), prefix);
|
|
544
568
|
if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
|
|
545
569
|
else suggestions.push(...parserSuggestions);
|
|
546
570
|
}
|
|
@@ -550,8 +574,9 @@ function createExclusiveSuggest(parsers, mode) {
|
|
|
550
574
|
};
|
|
551
575
|
}
|
|
552
576
|
function getExclusiveSuggestRuntimeNodes(parsers, state, path) {
|
|
553
|
-
|
|
554
|
-
|
|
577
|
+
const activeState = normalizeExclusiveState(state);
|
|
578
|
+
if (activeState == null) return [];
|
|
579
|
+
const [index, parserResult] = activeState;
|
|
555
580
|
if (!parserResult?.success || index < 0 || index >= parsers.length) return [];
|
|
556
581
|
const parser = parsers[index];
|
|
557
582
|
return require_parser.getParserSuggestRuntimeNodes(parser, parserResult.next.state, [...path, index]);
|
|
@@ -614,34 +639,32 @@ function or(...args) {
|
|
|
614
639
|
});
|
|
615
640
|
const parseSync = (context) => {
|
|
616
641
|
let error = getInitialError(context);
|
|
642
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
617
643
|
const orderedParsers = syncParsers.map((p, i) => [p, i]);
|
|
618
|
-
orderedParsers.sort(([_, a], [__, b]) =>
|
|
644
|
+
orderedParsers.sort(([_, a], [__, b]) => activeState?.[0] === a ? -1 : activeState?.[0] === b ? 1 : a - b);
|
|
619
645
|
for (const [parser, i] of orderedParsers) {
|
|
620
|
-
const result = parser.parse(withChildContext(context, i,
|
|
646
|
+
const result = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
621
647
|
if (result.success && result.consumed.length > 0) {
|
|
622
|
-
if (
|
|
623
|
-
const previouslyConsumed =
|
|
648
|
+
if (activeState?.[0] !== i && activeState?.[1].success) {
|
|
649
|
+
const previouslyConsumed = activeState[1].consumed;
|
|
624
650
|
const checkResult = parser.parse({
|
|
625
|
-
...withChildContext(context, i, parser.initialState),
|
|
651
|
+
...withChildContext(context, i, parser.initialState, parser),
|
|
626
652
|
buffer: previouslyConsumed
|
|
627
653
|
});
|
|
628
654
|
const canConsumeShared = checkResult.success && checkResult.consumed.length === previouslyConsumed.length && checkResult.consumed.every((c, idx) => c === previouslyConsumed[idx]);
|
|
629
655
|
if (!canConsumeShared) return {
|
|
630
656
|
success: false,
|
|
631
657
|
consumed: context.buffer.length - result.next.buffer.length,
|
|
632
|
-
error: require_message.message`${require_message.values(
|
|
658
|
+
error: require_message.message`${require_message.values(activeState[1].consumed)} and ${require_message.values(result.consumed)} cannot be used together.`
|
|
633
659
|
};
|
|
634
660
|
const replayExec = mergeChildExec(context.exec, checkResult.next.exec);
|
|
635
|
-
const replayedResult = parser.parse({
|
|
636
|
-
...
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}, i, checkResult.next.state),
|
|
643
|
-
state: checkResult.next.state
|
|
644
|
-
});
|
|
661
|
+
const replayedResult = parser.parse(withChildContext({
|
|
662
|
+
...context,
|
|
663
|
+
...replayExec != null ? {
|
|
664
|
+
exec: replayExec,
|
|
665
|
+
dependencyRegistry: replayExec.dependencyRegistry
|
|
666
|
+
} : {}
|
|
667
|
+
}, i, checkResult.next.state, parser));
|
|
645
668
|
if (!replayedResult.success) return replayedResult;
|
|
646
669
|
const mergedExec$1 = mergeChildExec(replayExec, replayedResult.next.exec);
|
|
647
670
|
return {
|
|
@@ -650,10 +673,10 @@ function or(...args) {
|
|
|
650
673
|
...context,
|
|
651
674
|
buffer: replayedResult.next.buffer,
|
|
652
675
|
optionsTerminated: replayedResult.next.optionsTerminated,
|
|
653
|
-
state:
|
|
676
|
+
state: createExclusiveState(context.state, i, parser, {
|
|
654
677
|
...replayedResult,
|
|
655
678
|
consumed: [...previouslyConsumed, ...replayedResult.consumed]
|
|
656
|
-
}
|
|
679
|
+
}),
|
|
657
680
|
...mergedExec$1 != null ? {
|
|
658
681
|
exec: mergedExec$1,
|
|
659
682
|
dependencyRegistry: mergedExec$1.dependencyRegistry
|
|
@@ -669,7 +692,7 @@ function or(...args) {
|
|
|
669
692
|
...context,
|
|
670
693
|
buffer: result.next.buffer,
|
|
671
694
|
optionsTerminated: result.next.optionsTerminated,
|
|
672
|
-
state:
|
|
695
|
+
state: createExclusiveState(context.state, i, parser, result),
|
|
673
696
|
...mergedExec != null ? {
|
|
674
697
|
exec: mergedExec,
|
|
675
698
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -686,16 +709,17 @@ function or(...args) {
|
|
|
686
709
|
};
|
|
687
710
|
const parseAsync = async (context) => {
|
|
688
711
|
let error = getInitialError(context);
|
|
712
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
689
713
|
const orderedParsers = parsers.map((p, i) => [p, i]);
|
|
690
|
-
orderedParsers.sort(([_, a], [__, b]) =>
|
|
714
|
+
orderedParsers.sort(([_, a], [__, b]) => activeState?.[0] === a ? -1 : activeState?.[0] === b ? 1 : a - b);
|
|
691
715
|
for (const [parser, i] of orderedParsers) {
|
|
692
|
-
const resultOrPromise = parser.parse(withChildContext(context, i,
|
|
716
|
+
const resultOrPromise = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
693
717
|
const result = await resultOrPromise;
|
|
694
718
|
if (result.success && result.consumed.length > 0) {
|
|
695
|
-
if (
|
|
696
|
-
const previouslyConsumed =
|
|
719
|
+
if (activeState?.[0] !== i && activeState?.[1].success) {
|
|
720
|
+
const previouslyConsumed = activeState[1].consumed;
|
|
697
721
|
const checkResultOrPromise = parser.parse({
|
|
698
|
-
...withChildContext(context, i, parser.initialState),
|
|
722
|
+
...withChildContext(context, i, parser.initialState, parser),
|
|
699
723
|
buffer: previouslyConsumed
|
|
700
724
|
});
|
|
701
725
|
const checkResult = await checkResultOrPromise;
|
|
@@ -703,16 +727,16 @@ function or(...args) {
|
|
|
703
727
|
if (!canConsumeShared) return {
|
|
704
728
|
success: false,
|
|
705
729
|
consumed: context.buffer.length - result.next.buffer.length,
|
|
706
|
-
error: require_message.message`${require_message.values(
|
|
730
|
+
error: require_message.message`${require_message.values(activeState[1].consumed)} and ${require_message.values(result.consumed)} cannot be used together.`
|
|
707
731
|
};
|
|
708
732
|
const replayExec = mergeChildExec(context.exec, checkResult.next.exec);
|
|
709
|
-
const replayedResultOrPromise = parser.parse(
|
|
733
|
+
const replayedResultOrPromise = parser.parse(withChildContext({
|
|
710
734
|
...context,
|
|
711
735
|
...replayExec != null ? {
|
|
712
736
|
exec: replayExec,
|
|
713
737
|
dependencyRegistry: replayExec.dependencyRegistry
|
|
714
738
|
} : {}
|
|
715
|
-
}, i, checkResult.next.state
|
|
739
|
+
}, i, checkResult.next.state, parser));
|
|
716
740
|
const replayedResult = await replayedResultOrPromise;
|
|
717
741
|
if (!replayedResult.success) return replayedResult;
|
|
718
742
|
const mergedExec$1 = mergeChildExec(replayExec, replayedResult.next.exec);
|
|
@@ -722,10 +746,10 @@ function or(...args) {
|
|
|
722
746
|
...context,
|
|
723
747
|
buffer: replayedResult.next.buffer,
|
|
724
748
|
optionsTerminated: replayedResult.next.optionsTerminated,
|
|
725
|
-
state:
|
|
749
|
+
state: createExclusiveState(context.state, i, parser, {
|
|
726
750
|
...replayedResult,
|
|
727
751
|
consumed: [...previouslyConsumed, ...replayedResult.consumed]
|
|
728
|
-
}
|
|
752
|
+
}),
|
|
729
753
|
...mergedExec$1 != null ? {
|
|
730
754
|
exec: mergedExec$1,
|
|
731
755
|
dependencyRegistry: mergedExec$1.dependencyRegistry
|
|
@@ -741,7 +765,7 @@ function or(...args) {
|
|
|
741
765
|
...context,
|
|
742
766
|
buffer: result.next.buffer,
|
|
743
767
|
optionsTerminated: result.next.optionsTerminated,
|
|
744
|
-
state:
|
|
768
|
+
state: createExclusiveState(context.state, i, parser, result),
|
|
745
769
|
...mergedExec != null ? {
|
|
746
770
|
exec: mergedExec,
|
|
747
771
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -805,6 +829,7 @@ function or(...args) {
|
|
|
805
829
|
};
|
|
806
830
|
const singleDependencyMetadata = composeExclusiveDependencyMetadata(parsers);
|
|
807
831
|
if (singleDependencyMetadata != null) singleResult.dependencyMetadata = singleDependencyMetadata;
|
|
832
|
+
require_parser.defineInheritedAnnotationParser(singleResult);
|
|
808
833
|
return singleResult;
|
|
809
834
|
}
|
|
810
835
|
/**
|
|
@@ -837,13 +862,15 @@ function longestMatch(...args) {
|
|
|
837
862
|
const parseSync = (context) => {
|
|
838
863
|
let bestMatch = null;
|
|
839
864
|
let error = getInitialError(context);
|
|
865
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
840
866
|
for (let i = 0; i < syncParsers.length; i++) {
|
|
841
867
|
const parser = syncParsers[i];
|
|
842
|
-
const result = parser.parse(withChildContext(context, i,
|
|
868
|
+
const result = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
843
869
|
if (result.success) {
|
|
844
870
|
const consumed = context.buffer.length - result.next.buffer.length;
|
|
845
871
|
if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
|
|
846
872
|
index: i,
|
|
873
|
+
parser,
|
|
847
874
|
result,
|
|
848
875
|
consumed
|
|
849
876
|
};
|
|
@@ -857,7 +884,7 @@ function longestMatch(...args) {
|
|
|
857
884
|
...context,
|
|
858
885
|
buffer: bestMatch.result.next.buffer,
|
|
859
886
|
optionsTerminated: bestMatch.result.next.optionsTerminated,
|
|
860
|
-
state:
|
|
887
|
+
state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
|
|
861
888
|
...mergedExec != null ? {
|
|
862
889
|
exec: mergedExec,
|
|
863
890
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -874,14 +901,16 @@ function longestMatch(...args) {
|
|
|
874
901
|
const parseAsync = async (context) => {
|
|
875
902
|
let bestMatch = null;
|
|
876
903
|
let error = getInitialError(context);
|
|
904
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
877
905
|
for (let i = 0; i < parsers.length; i++) {
|
|
878
906
|
const parser = parsers[i];
|
|
879
|
-
const resultOrPromise = parser.parse(withChildContext(context, i,
|
|
907
|
+
const resultOrPromise = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
880
908
|
const result = await resultOrPromise;
|
|
881
909
|
if (result.success) {
|
|
882
910
|
const consumed = context.buffer.length - result.next.buffer.length;
|
|
883
911
|
if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
|
|
884
912
|
index: i,
|
|
913
|
+
parser,
|
|
885
914
|
result,
|
|
886
915
|
consumed
|
|
887
916
|
};
|
|
@@ -895,7 +924,7 @@ function longestMatch(...args) {
|
|
|
895
924
|
...context,
|
|
896
925
|
buffer: bestMatch.result.next.buffer,
|
|
897
926
|
optionsTerminated: bestMatch.result.next.optionsTerminated,
|
|
898
|
-
state:
|
|
927
|
+
state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
|
|
899
928
|
...mergedExec != null ? {
|
|
900
929
|
exec: mergedExec,
|
|
901
930
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -965,6 +994,7 @@ function longestMatch(...args) {
|
|
|
965
994
|
};
|
|
966
995
|
const multiDependencyMetadata = composeExclusiveDependencyMetadata(parsers);
|
|
967
996
|
if (multiDependencyMetadata != null) multiResult.dependencyMetadata = multiDependencyMetadata;
|
|
997
|
+
require_parser.defineInheritedAnnotationParser(multiResult);
|
|
968
998
|
return multiResult;
|
|
969
999
|
}
|
|
970
1000
|
/**
|
|
@@ -1382,7 +1412,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1382
1412
|
madeProgress = false;
|
|
1383
1413
|
const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
|
|
1384
1414
|
for (const [field, parser] of parserPairs) {
|
|
1385
|
-
const result = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
|
|
1415
|
+
const result = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser), parser));
|
|
1386
1416
|
if (result.success && result.consumed.length > 0) {
|
|
1387
1417
|
const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
|
|
1388
1418
|
currentContext = {
|
|
@@ -1443,7 +1473,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1443
1473
|
madeProgress = false;
|
|
1444
1474
|
const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
|
|
1445
1475
|
for (const [field, parser] of parserPairs) {
|
|
1446
|
-
const resultOrPromise = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
|
|
1476
|
+
const resultOrPromise = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser), parser));
|
|
1447
1477
|
const result = await resultOrPromise;
|
|
1448
1478
|
if (result.success && result.consumed.length > 0) {
|
|
1449
1479
|
const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
|
package/dist/constructs.js
CHANGED
|
@@ -484,6 +484,26 @@ function generateNoMatchError(context) {
|
|
|
484
484
|
else if (hasArguments && hasCommands && !hasOptions) return message`No matching command or argument found.`;
|
|
485
485
|
else return message`No matching option, command, or argument found.`;
|
|
486
486
|
}
|
|
487
|
+
function normalizeExclusiveState(state) {
|
|
488
|
+
if (!Array.isArray(state) || state.length !== 2 || typeof state[0] !== "number") return void 0;
|
|
489
|
+
return state;
|
|
490
|
+
}
|
|
491
|
+
function annotateExclusiveParserResult(parentState, parser, result) {
|
|
492
|
+
if (!result.success) return result;
|
|
493
|
+
const annotatedState = getAnnotatedChildState(parentState, result.next.state, parser);
|
|
494
|
+
if (annotatedState === result.next.state) return result;
|
|
495
|
+
return {
|
|
496
|
+
...result,
|
|
497
|
+
next: {
|
|
498
|
+
...result.next,
|
|
499
|
+
state: annotatedState
|
|
500
|
+
}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
function createExclusiveState(parentState, index, parser, result) {
|
|
504
|
+
const annotatedResult = annotateExclusiveParserResult(parentState, parser, result);
|
|
505
|
+
return annotateFreshArray(parentState, [index, annotatedResult]);
|
|
506
|
+
}
|
|
487
507
|
/**
|
|
488
508
|
* Creates a complete() method shared by or() and longestMatch().
|
|
489
509
|
* @internal
|
|
@@ -491,11 +511,12 @@ function generateNoMatchError(context) {
|
|
|
491
511
|
function createExclusiveComplete(parsers, options, noMatchContext, mode) {
|
|
492
512
|
const syncParsers = parsers;
|
|
493
513
|
return (state, exec) => {
|
|
494
|
-
|
|
514
|
+
const activeState = normalizeExclusiveState(state);
|
|
515
|
+
if (activeState == null) return {
|
|
495
516
|
success: false,
|
|
496
517
|
error: getNoMatchError(options, noMatchContext)
|
|
497
518
|
};
|
|
498
|
-
const [i, result] =
|
|
519
|
+
const [i, result] = activeState;
|
|
499
520
|
if (!result.success) return {
|
|
500
521
|
success: false,
|
|
501
522
|
error: result.error
|
|
@@ -515,32 +536,35 @@ function createExclusiveSuggest(parsers, mode) {
|
|
|
515
536
|
return (context, prefix) => {
|
|
516
537
|
return dispatchIterableByMode(mode, function* () {
|
|
517
538
|
const suggestions = [];
|
|
518
|
-
|
|
539
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
540
|
+
if (activeState == null) for (let i = 0; i < syncParsers.length; i++) {
|
|
519
541
|
const parser = syncParsers[i];
|
|
520
|
-
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState), prefix);
|
|
542
|
+
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState, parser), prefix);
|
|
521
543
|
suggestions.push(...parserSuggestions);
|
|
522
544
|
}
|
|
523
545
|
else {
|
|
524
|
-
const [index, parserResult] =
|
|
546
|
+
const [index, parserResult] = activeState;
|
|
525
547
|
if (parserResult.success) {
|
|
526
|
-
const
|
|
548
|
+
const parser = syncParsers[index];
|
|
549
|
+
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state, parser), prefix);
|
|
527
550
|
suggestions.push(...parserSuggestions);
|
|
528
551
|
}
|
|
529
552
|
}
|
|
530
553
|
yield* deduplicateSuggestions(suggestions);
|
|
531
554
|
}, async function* () {
|
|
532
555
|
const suggestions = [];
|
|
533
|
-
|
|
556
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
557
|
+
if (activeState == null) for (let i = 0; i < parsers.length; i++) {
|
|
534
558
|
const parser = parsers[i];
|
|
535
|
-
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState), prefix);
|
|
559
|
+
const parserSuggestions = parser.suggest(withChildContext(context, i, parser.initialState, parser), prefix);
|
|
536
560
|
if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
|
|
537
561
|
else suggestions.push(...parserSuggestions);
|
|
538
562
|
}
|
|
539
563
|
else {
|
|
540
|
-
const [index, parserResult] =
|
|
564
|
+
const [index, parserResult] = activeState;
|
|
541
565
|
if (parserResult.success) {
|
|
542
566
|
const parser = parsers[index];
|
|
543
|
-
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state), prefix);
|
|
567
|
+
const parserSuggestions = parser.suggest(withChildContext(context, index, parserResult.next.state, parser), prefix);
|
|
544
568
|
if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
|
|
545
569
|
else suggestions.push(...parserSuggestions);
|
|
546
570
|
}
|
|
@@ -550,8 +574,9 @@ function createExclusiveSuggest(parsers, mode) {
|
|
|
550
574
|
};
|
|
551
575
|
}
|
|
552
576
|
function getExclusiveSuggestRuntimeNodes(parsers, state, path) {
|
|
553
|
-
|
|
554
|
-
|
|
577
|
+
const activeState = normalizeExclusiveState(state);
|
|
578
|
+
if (activeState == null) return [];
|
|
579
|
+
const [index, parserResult] = activeState;
|
|
555
580
|
if (!parserResult?.success || index < 0 || index >= parsers.length) return [];
|
|
556
581
|
const parser = parsers[index];
|
|
557
582
|
return getParserSuggestRuntimeNodes(parser, parserResult.next.state, [...path, index]);
|
|
@@ -614,34 +639,32 @@ function or(...args) {
|
|
|
614
639
|
});
|
|
615
640
|
const parseSync = (context) => {
|
|
616
641
|
let error = getInitialError(context);
|
|
642
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
617
643
|
const orderedParsers = syncParsers.map((p, i) => [p, i]);
|
|
618
|
-
orderedParsers.sort(([_, a], [__, b]) =>
|
|
644
|
+
orderedParsers.sort(([_, a], [__, b]) => activeState?.[0] === a ? -1 : activeState?.[0] === b ? 1 : a - b);
|
|
619
645
|
for (const [parser, i] of orderedParsers) {
|
|
620
|
-
const result = parser.parse(withChildContext(context, i,
|
|
646
|
+
const result = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
621
647
|
if (result.success && result.consumed.length > 0) {
|
|
622
|
-
if (
|
|
623
|
-
const previouslyConsumed =
|
|
648
|
+
if (activeState?.[0] !== i && activeState?.[1].success) {
|
|
649
|
+
const previouslyConsumed = activeState[1].consumed;
|
|
624
650
|
const checkResult = parser.parse({
|
|
625
|
-
...withChildContext(context, i, parser.initialState),
|
|
651
|
+
...withChildContext(context, i, parser.initialState, parser),
|
|
626
652
|
buffer: previouslyConsumed
|
|
627
653
|
});
|
|
628
654
|
const canConsumeShared = checkResult.success && checkResult.consumed.length === previouslyConsumed.length && checkResult.consumed.every((c, idx) => c === previouslyConsumed[idx]);
|
|
629
655
|
if (!canConsumeShared) return {
|
|
630
656
|
success: false,
|
|
631
657
|
consumed: context.buffer.length - result.next.buffer.length,
|
|
632
|
-
error: message`${values(
|
|
658
|
+
error: message`${values(activeState[1].consumed)} and ${values(result.consumed)} cannot be used together.`
|
|
633
659
|
};
|
|
634
660
|
const replayExec = mergeChildExec(context.exec, checkResult.next.exec);
|
|
635
|
-
const replayedResult = parser.parse({
|
|
636
|
-
...
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
}, i, checkResult.next.state),
|
|
643
|
-
state: checkResult.next.state
|
|
644
|
-
});
|
|
661
|
+
const replayedResult = parser.parse(withChildContext({
|
|
662
|
+
...context,
|
|
663
|
+
...replayExec != null ? {
|
|
664
|
+
exec: replayExec,
|
|
665
|
+
dependencyRegistry: replayExec.dependencyRegistry
|
|
666
|
+
} : {}
|
|
667
|
+
}, i, checkResult.next.state, parser));
|
|
645
668
|
if (!replayedResult.success) return replayedResult;
|
|
646
669
|
const mergedExec$1 = mergeChildExec(replayExec, replayedResult.next.exec);
|
|
647
670
|
return {
|
|
@@ -650,10 +673,10 @@ function or(...args) {
|
|
|
650
673
|
...context,
|
|
651
674
|
buffer: replayedResult.next.buffer,
|
|
652
675
|
optionsTerminated: replayedResult.next.optionsTerminated,
|
|
653
|
-
state:
|
|
676
|
+
state: createExclusiveState(context.state, i, parser, {
|
|
654
677
|
...replayedResult,
|
|
655
678
|
consumed: [...previouslyConsumed, ...replayedResult.consumed]
|
|
656
|
-
}
|
|
679
|
+
}),
|
|
657
680
|
...mergedExec$1 != null ? {
|
|
658
681
|
exec: mergedExec$1,
|
|
659
682
|
dependencyRegistry: mergedExec$1.dependencyRegistry
|
|
@@ -669,7 +692,7 @@ function or(...args) {
|
|
|
669
692
|
...context,
|
|
670
693
|
buffer: result.next.buffer,
|
|
671
694
|
optionsTerminated: result.next.optionsTerminated,
|
|
672
|
-
state:
|
|
695
|
+
state: createExclusiveState(context.state, i, parser, result),
|
|
673
696
|
...mergedExec != null ? {
|
|
674
697
|
exec: mergedExec,
|
|
675
698
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -686,16 +709,17 @@ function or(...args) {
|
|
|
686
709
|
};
|
|
687
710
|
const parseAsync = async (context) => {
|
|
688
711
|
let error = getInitialError(context);
|
|
712
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
689
713
|
const orderedParsers = parsers.map((p, i) => [p, i]);
|
|
690
|
-
orderedParsers.sort(([_, a], [__, b]) =>
|
|
714
|
+
orderedParsers.sort(([_, a], [__, b]) => activeState?.[0] === a ? -1 : activeState?.[0] === b ? 1 : a - b);
|
|
691
715
|
for (const [parser, i] of orderedParsers) {
|
|
692
|
-
const resultOrPromise = parser.parse(withChildContext(context, i,
|
|
716
|
+
const resultOrPromise = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
693
717
|
const result = await resultOrPromise;
|
|
694
718
|
if (result.success && result.consumed.length > 0) {
|
|
695
|
-
if (
|
|
696
|
-
const previouslyConsumed =
|
|
719
|
+
if (activeState?.[0] !== i && activeState?.[1].success) {
|
|
720
|
+
const previouslyConsumed = activeState[1].consumed;
|
|
697
721
|
const checkResultOrPromise = parser.parse({
|
|
698
|
-
...withChildContext(context, i, parser.initialState),
|
|
722
|
+
...withChildContext(context, i, parser.initialState, parser),
|
|
699
723
|
buffer: previouslyConsumed
|
|
700
724
|
});
|
|
701
725
|
const checkResult = await checkResultOrPromise;
|
|
@@ -703,16 +727,16 @@ function or(...args) {
|
|
|
703
727
|
if (!canConsumeShared) return {
|
|
704
728
|
success: false,
|
|
705
729
|
consumed: context.buffer.length - result.next.buffer.length,
|
|
706
|
-
error: message`${values(
|
|
730
|
+
error: message`${values(activeState[1].consumed)} and ${values(result.consumed)} cannot be used together.`
|
|
707
731
|
};
|
|
708
732
|
const replayExec = mergeChildExec(context.exec, checkResult.next.exec);
|
|
709
|
-
const replayedResultOrPromise = parser.parse(
|
|
733
|
+
const replayedResultOrPromise = parser.parse(withChildContext({
|
|
710
734
|
...context,
|
|
711
735
|
...replayExec != null ? {
|
|
712
736
|
exec: replayExec,
|
|
713
737
|
dependencyRegistry: replayExec.dependencyRegistry
|
|
714
738
|
} : {}
|
|
715
|
-
}, i, checkResult.next.state
|
|
739
|
+
}, i, checkResult.next.state, parser));
|
|
716
740
|
const replayedResult = await replayedResultOrPromise;
|
|
717
741
|
if (!replayedResult.success) return replayedResult;
|
|
718
742
|
const mergedExec$1 = mergeChildExec(replayExec, replayedResult.next.exec);
|
|
@@ -722,10 +746,10 @@ function or(...args) {
|
|
|
722
746
|
...context,
|
|
723
747
|
buffer: replayedResult.next.buffer,
|
|
724
748
|
optionsTerminated: replayedResult.next.optionsTerminated,
|
|
725
|
-
state:
|
|
749
|
+
state: createExclusiveState(context.state, i, parser, {
|
|
726
750
|
...replayedResult,
|
|
727
751
|
consumed: [...previouslyConsumed, ...replayedResult.consumed]
|
|
728
|
-
}
|
|
752
|
+
}),
|
|
729
753
|
...mergedExec$1 != null ? {
|
|
730
754
|
exec: mergedExec$1,
|
|
731
755
|
dependencyRegistry: mergedExec$1.dependencyRegistry
|
|
@@ -741,7 +765,7 @@ function or(...args) {
|
|
|
741
765
|
...context,
|
|
742
766
|
buffer: result.next.buffer,
|
|
743
767
|
optionsTerminated: result.next.optionsTerminated,
|
|
744
|
-
state:
|
|
768
|
+
state: createExclusiveState(context.state, i, parser, result),
|
|
745
769
|
...mergedExec != null ? {
|
|
746
770
|
exec: mergedExec,
|
|
747
771
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -805,6 +829,7 @@ function or(...args) {
|
|
|
805
829
|
};
|
|
806
830
|
const singleDependencyMetadata = composeExclusiveDependencyMetadata(parsers);
|
|
807
831
|
if (singleDependencyMetadata != null) singleResult.dependencyMetadata = singleDependencyMetadata;
|
|
832
|
+
defineInheritedAnnotationParser(singleResult);
|
|
808
833
|
return singleResult;
|
|
809
834
|
}
|
|
810
835
|
/**
|
|
@@ -837,13 +862,15 @@ function longestMatch(...args) {
|
|
|
837
862
|
const parseSync = (context) => {
|
|
838
863
|
let bestMatch = null;
|
|
839
864
|
let error = getInitialError(context);
|
|
865
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
840
866
|
for (let i = 0; i < syncParsers.length; i++) {
|
|
841
867
|
const parser = syncParsers[i];
|
|
842
|
-
const result = parser.parse(withChildContext(context, i,
|
|
868
|
+
const result = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
843
869
|
if (result.success) {
|
|
844
870
|
const consumed = context.buffer.length - result.next.buffer.length;
|
|
845
871
|
if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
|
|
846
872
|
index: i,
|
|
873
|
+
parser,
|
|
847
874
|
result,
|
|
848
875
|
consumed
|
|
849
876
|
};
|
|
@@ -857,7 +884,7 @@ function longestMatch(...args) {
|
|
|
857
884
|
...context,
|
|
858
885
|
buffer: bestMatch.result.next.buffer,
|
|
859
886
|
optionsTerminated: bestMatch.result.next.optionsTerminated,
|
|
860
|
-
state:
|
|
887
|
+
state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
|
|
861
888
|
...mergedExec != null ? {
|
|
862
889
|
exec: mergedExec,
|
|
863
890
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -874,14 +901,16 @@ function longestMatch(...args) {
|
|
|
874
901
|
const parseAsync = async (context) => {
|
|
875
902
|
let bestMatch = null;
|
|
876
903
|
let error = getInitialError(context);
|
|
904
|
+
const activeState = normalizeExclusiveState(context.state);
|
|
877
905
|
for (let i = 0; i < parsers.length; i++) {
|
|
878
906
|
const parser = parsers[i];
|
|
879
|
-
const resultOrPromise = parser.parse(withChildContext(context, i,
|
|
907
|
+
const resultOrPromise = parser.parse(withChildContext(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
|
|
880
908
|
const result = await resultOrPromise;
|
|
881
909
|
if (result.success) {
|
|
882
910
|
const consumed = context.buffer.length - result.next.buffer.length;
|
|
883
911
|
if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
|
|
884
912
|
index: i,
|
|
913
|
+
parser,
|
|
885
914
|
result,
|
|
886
915
|
consumed
|
|
887
916
|
};
|
|
@@ -895,7 +924,7 @@ function longestMatch(...args) {
|
|
|
895
924
|
...context,
|
|
896
925
|
buffer: bestMatch.result.next.buffer,
|
|
897
926
|
optionsTerminated: bestMatch.result.next.optionsTerminated,
|
|
898
|
-
state:
|
|
927
|
+
state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
|
|
899
928
|
...mergedExec != null ? {
|
|
900
929
|
exec: mergedExec,
|
|
901
930
|
dependencyRegistry: mergedExec.dependencyRegistry
|
|
@@ -965,6 +994,7 @@ function longestMatch(...args) {
|
|
|
965
994
|
};
|
|
966
995
|
const multiDependencyMetadata = composeExclusiveDependencyMetadata(parsers);
|
|
967
996
|
if (multiDependencyMetadata != null) multiResult.dependencyMetadata = multiDependencyMetadata;
|
|
997
|
+
defineInheritedAnnotationParser(multiResult);
|
|
968
998
|
return multiResult;
|
|
969
999
|
}
|
|
970
1000
|
/**
|
|
@@ -1382,7 +1412,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1382
1412
|
madeProgress = false;
|
|
1383
1413
|
const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
|
|
1384
1414
|
for (const [field, parser] of parserPairs) {
|
|
1385
|
-
const result = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
|
|
1415
|
+
const result = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser), parser));
|
|
1386
1416
|
if (result.success && result.consumed.length > 0) {
|
|
1387
1417
|
const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
|
|
1388
1418
|
currentContext = {
|
|
@@ -1443,7 +1473,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
1443
1473
|
madeProgress = false;
|
|
1444
1474
|
const getFieldState = createFieldStateGetter(currentContext.state, getObjectParseChildState);
|
|
1445
1475
|
for (const [field, parser] of parserPairs) {
|
|
1446
|
-
const resultOrPromise = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser)));
|
|
1476
|
+
const resultOrPromise = parser.parse(withChildContext(currentContext, field, getFieldState(field, parser), parser));
|
|
1447
1477
|
const result = await resultOrPromise;
|
|
1448
1478
|
if (result.success && result.consumed.length > 0) {
|
|
1449
1479
|
const mergedExec = mergeChildExec(currentContext.exec, result.next.exec);
|