@optique/core 1.2.1 → 1.2.3

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.
@@ -582,6 +582,35 @@ function createExclusiveState(parentState, index, parser, result) {
582
582
  return require_internal_annotations.annotateFreshArray(parentState, [index, annotatedResult]);
583
583
  }
584
584
  /**
585
+ * Detects a branch result that handled only the global options terminator.
586
+ * Such a result advances the shared parser context, but does not identify
587
+ * which exclusive branch matched.
588
+ * @internal
589
+ */
590
+ function isOptionsTerminatorOnlyResult(context, result) {
591
+ return !context.optionsTerminated && context.buffer[0] === "--" && result.success && result.next.optionsTerminated && result.consumed.length === 1 && result.consumed[0] === "--";
592
+ }
593
+ function isUnselectedBooleanOptionState(state) {
594
+ return state != null && typeof state === "object" && "success" in state && state.success === true && "value" in state && state.value === false;
595
+ }
596
+ function preserveExclusiveStateAfterOptionsTerminator(context, result) {
597
+ const mergedExec = require_execution_context.mergeChildExec(context.exec, result.next.exec);
598
+ return {
599
+ success: true,
600
+ next: {
601
+ ...context,
602
+ buffer: result.next.buffer,
603
+ optionsTerminated: true,
604
+ state: context.state,
605
+ ...mergedExec != null ? {
606
+ exec: mergedExec,
607
+ dependencyRegistry: mergedExec.dependencyRegistry
608
+ } : {}
609
+ },
610
+ consumed: result.consumed
611
+ };
612
+ }
613
+ /**
585
614
  * Creates a complete() method shared by or() and longestMatch().
586
615
  * @internal
587
616
  */
@@ -819,9 +848,15 @@ function or(...args) {
819
848
  let zeroConsumedBranch = null;
820
849
  let zeroConsumedCount = 0;
821
850
  let provisionalConsuming = null;
851
+ let optionsTerminatorResult = null;
822
852
  let provisionalAmbiguous = false;
823
853
  for (const [parser, i] of orderedParsers) {
824
854
  const result = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
855
+ if (isOptionsTerminatorOnlyResult(context, result)) {
856
+ if (activeState?.[0] === i && activeState[1].success && activeState[1].consumed.length > 0 && result.next.buffer.length < 1) return preserveExclusiveStateAfterOptionsTerminator(context, result);
857
+ optionsTerminatorResult ??= result;
858
+ continue;
859
+ }
825
860
  if (result.success && result.consumed.length > 0) {
826
861
  if (result.provisional) {
827
862
  const activeBranchLocked = activeState != null && activeState[1].success && activeState[1].consumed.length > 0;
@@ -1002,6 +1037,7 @@ function or(...args) {
1002
1037
  }
1003
1038
  }
1004
1039
  }
1040
+ if (optionsTerminatorResult != null) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorResult);
1005
1041
  return {
1006
1042
  ...error,
1007
1043
  success: false
@@ -1015,10 +1051,16 @@ function or(...args) {
1015
1051
  let zeroConsumedBranch = null;
1016
1052
  let zeroConsumedCount = 0;
1017
1053
  let provisionalConsuming = null;
1054
+ let optionsTerminatorResult = null;
1018
1055
  let provisionalAmbiguous = false;
1019
1056
  for (const [parser, i] of orderedParsers) {
1020
1057
  const resultOrPromise = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1021
1058
  const result = await resultOrPromise;
1059
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1060
+ if (activeState?.[0] === i && activeState[1].success && activeState[1].consumed.length > 0 && result.next.buffer.length < 1) return preserveExclusiveStateAfterOptionsTerminator(context, result);
1061
+ optionsTerminatorResult ??= result;
1062
+ continue;
1063
+ }
1022
1064
  if (result.success && result.consumed.length > 0) {
1023
1065
  if (result.provisional) {
1024
1066
  const activeBranchLocked = activeState != null && activeState[1].success && activeState[1].consumed.length > 0;
@@ -1201,6 +1243,7 @@ function or(...args) {
1201
1243
  }
1202
1244
  }
1203
1245
  }
1246
+ if (optionsTerminatorResult != null) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorResult);
1204
1247
  return {
1205
1248
  ...error,
1206
1249
  success: false
@@ -1302,82 +1345,122 @@ function createLongestMatch(...args) {
1302
1345
  return createUnexpectedInputErrorWithScopedSuggestions(defaultMsg, token, parsers, options?.errors?.suggestions);
1303
1346
  })()
1304
1347
  });
1305
- const parseSync = (context) => {
1348
+ const commitLongestMatch = (context, match, optionsTerminatorResult) => {
1349
+ const selectedResult = optionsTerminatorResult == null ? match.result : {
1350
+ ...match.result,
1351
+ next: {
1352
+ ...match.result.next,
1353
+ buffer: optionsTerminatorResult.next.buffer,
1354
+ optionsTerminated: true
1355
+ },
1356
+ consumed: [...optionsTerminatorResult.consumed, ...match.result.consumed]
1357
+ };
1358
+ const parser = parsers[match.index];
1359
+ const mergedExec = require_execution_context.mergeChildExec(context.exec, selectedResult.next.exec);
1360
+ return {
1361
+ success: true,
1362
+ next: {
1363
+ ...context,
1364
+ buffer: selectedResult.next.buffer,
1365
+ optionsTerminated: selectedResult.next.optionsTerminated,
1366
+ state: createExclusiveState(context.state, match.index, parser, selectedResult),
1367
+ ...mergedExec != null ? {
1368
+ exec: mergedExec,
1369
+ dependencyRegistry: mergedExec.dependencyRegistry
1370
+ } : {}
1371
+ },
1372
+ consumed: selectedResult.consumed
1373
+ };
1374
+ };
1375
+ const resolveLongestMatchOptionsTerminator = (context, bestMatch, optionsTerminatorMatches) => {
1376
+ if (optionsTerminatorMatches.length < 1) return null;
1377
+ if (bestMatch != null && bestMatch.consumed > 0) return null;
1378
+ const activeState = normalizeExclusiveState(context.state);
1379
+ if (activeState != null && activeState[1].success && activeState[1].consumed.length > 0) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorMatches[0].result);
1380
+ let selected = bestMatch == null ? null : {
1381
+ match: bestMatch,
1382
+ fromTerminator: false
1383
+ };
1384
+ for (const match of optionsTerminatorMatches) {
1385
+ if (!match.selectedAfterTerminator && (extractRequiredUsage(parsers[match.index].usage).length > 0 || isUnselectedBooleanOptionState(match.result.next.state))) continue;
1386
+ const selectedIsProvisional = selected != null && !!selected.match.result.provisional;
1387
+ const matchIsProvisional = !!match.result.provisional;
1388
+ if (selected == null || selectedIsProvisional && !matchIsProvisional || selectedIsProvisional === matchIsProvisional && match.index < selected.match.index) selected = {
1389
+ match,
1390
+ fromTerminator: true
1391
+ };
1392
+ }
1393
+ if (selected != null) return commitLongestMatch(context, selected.match, selected.fromTerminator ? void 0 : optionsTerminatorMatches[0].result);
1394
+ return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorMatches[0].result);
1395
+ };
1396
+ const parseSyncBranches = (context) => {
1306
1397
  let bestMatch = null;
1398
+ const optionsTerminatorMatches = [];
1307
1399
  let error = getInitialError(context);
1308
1400
  const activeState = normalizeExclusiveState(context.state);
1309
1401
  for (let i = 0; i < syncParsers.length; i++) {
1310
1402
  const parser = syncParsers[i];
1311
- const result = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1403
+ const childContext = withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser);
1404
+ const result = parser.parse(childContext);
1405
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1406
+ optionsTerminatorMatches.push({
1407
+ index: i,
1408
+ result,
1409
+ consumed: 1,
1410
+ ...normalizeExclusiveState(result.next.state) != null ? { selectedAfterTerminator: true } : {}
1411
+ });
1412
+ continue;
1413
+ }
1312
1414
  if (result.success) {
1313
1415
  const consumed = context.buffer.length - result.next.buffer.length;
1314
1416
  const bestIsProvisional = bestMatch != null && bestMatch.result.success && !!bestMatch.result.provisional;
1315
1417
  if (bestMatch === null || consumed > bestMatch.consumed || consumed === bestMatch.consumed && bestIsProvisional && !result.provisional) bestMatch = {
1316
1418
  index: i,
1317
- parser,
1318
1419
  result,
1319
1420
  consumed
1320
1421
  };
1321
1422
  } else if (error.consumed < result.consumed) error = result;
1322
1423
  }
1323
- if (bestMatch && bestMatch.result.success) {
1324
- const mergedExec = require_execution_context.mergeChildExec(context.exec, bestMatch.result.next.exec);
1325
- return {
1326
- success: true,
1327
- next: {
1328
- ...context,
1329
- buffer: bestMatch.result.next.buffer,
1330
- optionsTerminated: bestMatch.result.next.optionsTerminated,
1331
- state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
1332
- ...mergedExec != null ? {
1333
- exec: mergedExec,
1334
- dependencyRegistry: mergedExec.dependencyRegistry
1335
- } : {}
1336
- },
1337
- consumed: bestMatch.result.consumed
1338
- };
1339
- }
1424
+ const optionsTerminatorResolution = resolveLongestMatchOptionsTerminator(context, bestMatch, optionsTerminatorMatches);
1425
+ if (optionsTerminatorResolution != null) return optionsTerminatorResolution;
1426
+ if (bestMatch != null) return commitLongestMatch(context, bestMatch);
1340
1427
  return {
1341
1428
  ...error,
1342
1429
  success: false
1343
1430
  };
1344
1431
  };
1345
- const parseAsync = async (context) => {
1432
+ const parseAsyncBranches = async (context) => {
1346
1433
  let bestMatch = null;
1434
+ const optionsTerminatorMatches = [];
1347
1435
  let error = getInitialError(context);
1348
1436
  const activeState = normalizeExclusiveState(context.state);
1349
1437
  for (let i = 0; i < parsers.length; i++) {
1350
1438
  const parser = parsers[i];
1351
- const resultOrPromise = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1439
+ const childContext = withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser);
1440
+ const resultOrPromise = parser.parse(childContext);
1352
1441
  const result = await resultOrPromise;
1442
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1443
+ optionsTerminatorMatches.push({
1444
+ index: i,
1445
+ result,
1446
+ consumed: 1,
1447
+ ...normalizeExclusiveState(result.next.state) != null ? { selectedAfterTerminator: true } : {}
1448
+ });
1449
+ continue;
1450
+ }
1353
1451
  if (result.success) {
1354
1452
  const consumed = context.buffer.length - result.next.buffer.length;
1355
1453
  const bestIsProvisional = bestMatch != null && bestMatch.result.success && !!bestMatch.result.provisional;
1356
1454
  if (bestMatch === null || consumed > bestMatch.consumed || consumed === bestMatch.consumed && bestIsProvisional && !result.provisional) bestMatch = {
1357
1455
  index: i,
1358
- parser,
1359
1456
  result,
1360
1457
  consumed
1361
1458
  };
1362
1459
  } else if (error.consumed < result.consumed) error = result;
1363
1460
  }
1364
- if (bestMatch && bestMatch.result.success) {
1365
- const mergedExec = require_execution_context.mergeChildExec(context.exec, bestMatch.result.next.exec);
1366
- return {
1367
- success: true,
1368
- next: {
1369
- ...context,
1370
- buffer: bestMatch.result.next.buffer,
1371
- optionsTerminated: bestMatch.result.next.optionsTerminated,
1372
- state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
1373
- ...mergedExec != null ? {
1374
- exec: mergedExec,
1375
- dependencyRegistry: mergedExec.dependencyRegistry
1376
- } : {}
1377
- },
1378
- consumed: bestMatch.result.consumed
1379
- };
1380
- }
1461
+ const optionsTerminatorResolution = resolveLongestMatchOptionsTerminator(context, bestMatch, optionsTerminatorMatches);
1462
+ if (optionsTerminatorResolution != null) return optionsTerminatorResolution;
1463
+ if (bestMatch != null) return commitLongestMatch(context, bestMatch);
1381
1464
  return {
1382
1465
  ...error,
1383
1466
  success: false
@@ -1400,7 +1483,7 @@ function createLongestMatch(...args) {
1400
1483
  return extractExclusivePhase2Seed(parsers, state, exec, combinedMode);
1401
1484
  },
1402
1485
  parse(context) {
1403
- return require_mode_dispatch.dispatchByMode(combinedMode, () => parseSync(context), () => parseAsync(context));
1486
+ return require_mode_dispatch.dispatchByMode(combinedMode, () => parseSyncBranches(context), () => parseAsyncBranches(context));
1404
1487
  },
1405
1488
  getSuggestRuntimeNodes(state, path) {
1406
1489
  return getExclusiveSuggestRuntimeNodes(parsers, state, path);
@@ -1446,6 +1529,33 @@ function createLongestMatch(...args) {
1446
1529
  return require_modifiers.fluent(multiResult);
1447
1530
  }
1448
1531
  /**
1532
+ * Creates the initial parse error shared by object-like combinators.
1533
+ * @param context The current parser context.
1534
+ * @param noMatchContext The kinds of input accepted by the combinator.
1535
+ * @param errors Optional custom error formatters.
1536
+ * @returns A zero-consumption parse error.
1537
+ */
1538
+ function createObjectLikeInitialError(context, noMatchContext, errors) {
1539
+ if (context.buffer.length < 1) {
1540
+ const customEndOfInput = errors?.endOfInput;
1541
+ return {
1542
+ consumed: 0,
1543
+ error: customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext)
1544
+ };
1545
+ }
1546
+ const token = context.buffer[0];
1547
+ const customMessage = errors?.unexpectedInput;
1548
+ if (customMessage) return {
1549
+ consumed: 0,
1550
+ error: typeof customMessage === "function" ? customMessage(token) : customMessage
1551
+ };
1552
+ const baseError = require_message.message`Unexpected option or argument: ${token}.`;
1553
+ return {
1554
+ consumed: 0,
1555
+ error: require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", errors?.suggestions)
1556
+ };
1557
+ }
1558
+ /**
1449
1559
  * Internal sync helper for object suggest functionality.
1450
1560
  * @internal
1451
1561
  */
@@ -1838,19 +1948,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1838
1948
  checkDuplicateReachableLeadingCommandNames(parserPairs.map(([field, parser]) => [field, parser]));
1839
1949
  const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
1840
1950
  const combinedMode = parserKeys.some((k) => parsers[k].mode === "async") ? "async" : "sync";
1841
- const getInitialError = (context) => ({
1842
- consumed: 0,
1843
- error: context.buffer.length > 0 ? (() => {
1844
- const token = context.buffer[0];
1845
- const customMessage = options.errors?.unexpectedInput;
1846
- if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
1847
- const baseError = require_message.message`Unexpected option or argument: ${token}.`;
1848
- return require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
1849
- })() : (() => {
1850
- const customEndOfInput = options.errors?.endOfInput;
1851
- return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
1852
- })()
1853
- });
1951
+ const getInitialError = (context) => createObjectLikeInitialError(context, noMatchContext, options.errors);
1854
1952
  const parseSync = (context) => {
1855
1953
  let error = getInitialError(context);
1856
1954
  let currentContext = context;
@@ -3720,6 +3818,7 @@ function merge(...args) {
3720
3818
  const syncParsers = syncSorted.map(([p]) => p);
3721
3819
  if (!options.allowDuplicates) checkDuplicateOptionNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser.usage]));
3722
3820
  checkDuplicateReachableLeadingCommandNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser]));
3821
+ const noMatchContext = analyzeNoMatchContext(rawParsers);
3723
3822
  const mergedFieldParsers = collectChildFieldParsers(parsers);
3724
3823
  const duplicateOutputFieldNames = collectDuplicateFieldNames(mergedFieldParsers);
3725
3824
  const parserStateKey = (index) => `__parser_${index}`;
@@ -3813,8 +3912,7 @@ function merge(...args) {
3813
3912
  };
3814
3913
  return {
3815
3914
  success: false,
3816
- consumed: 0,
3817
- error: require_message.message`No matching option or argument found.`
3915
+ ...createObjectLikeInitialError(context, noMatchContext)
3818
3916
  };
3819
3917
  };
3820
3918
  const parseAsync = async (context) => {
@@ -3859,8 +3957,7 @@ function merge(...args) {
3859
3957
  };
3860
3958
  return {
3861
3959
  success: false,
3862
- consumed: 0,
3863
- error: require_message.message`No matching option or argument found.`
3960
+ ...createObjectLikeInitialError(context, noMatchContext)
3864
3961
  };
3865
3962
  };
3866
3963
  const mergeParser = {
@@ -582,6 +582,35 @@ function createExclusiveState(parentState, index, parser, result) {
582
582
  return annotateFreshArray(parentState, [index, annotatedResult]);
583
583
  }
584
584
  /**
585
+ * Detects a branch result that handled only the global options terminator.
586
+ * Such a result advances the shared parser context, but does not identify
587
+ * which exclusive branch matched.
588
+ * @internal
589
+ */
590
+ function isOptionsTerminatorOnlyResult(context, result) {
591
+ return !context.optionsTerminated && context.buffer[0] === "--" && result.success && result.next.optionsTerminated && result.consumed.length === 1 && result.consumed[0] === "--";
592
+ }
593
+ function isUnselectedBooleanOptionState(state) {
594
+ return state != null && typeof state === "object" && "success" in state && state.success === true && "value" in state && state.value === false;
595
+ }
596
+ function preserveExclusiveStateAfterOptionsTerminator(context, result) {
597
+ const mergedExec = mergeChildExec(context.exec, result.next.exec);
598
+ return {
599
+ success: true,
600
+ next: {
601
+ ...context,
602
+ buffer: result.next.buffer,
603
+ optionsTerminated: true,
604
+ state: context.state,
605
+ ...mergedExec != null ? {
606
+ exec: mergedExec,
607
+ dependencyRegistry: mergedExec.dependencyRegistry
608
+ } : {}
609
+ },
610
+ consumed: result.consumed
611
+ };
612
+ }
613
+ /**
585
614
  * Creates a complete() method shared by or() and longestMatch().
586
615
  * @internal
587
616
  */
@@ -819,9 +848,15 @@ function or(...args) {
819
848
  let zeroConsumedBranch = null;
820
849
  let zeroConsumedCount = 0;
821
850
  let provisionalConsuming = null;
851
+ let optionsTerminatorResult = null;
822
852
  let provisionalAmbiguous = false;
823
853
  for (const [parser, i] of orderedParsers) {
824
854
  const result = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
855
+ if (isOptionsTerminatorOnlyResult(context, result)) {
856
+ if (activeState?.[0] === i && activeState[1].success && activeState[1].consumed.length > 0 && result.next.buffer.length < 1) return preserveExclusiveStateAfterOptionsTerminator(context, result);
857
+ optionsTerminatorResult ??= result;
858
+ continue;
859
+ }
825
860
  if (result.success && result.consumed.length > 0) {
826
861
  if (result.provisional) {
827
862
  const activeBranchLocked = activeState != null && activeState[1].success && activeState[1].consumed.length > 0;
@@ -1002,6 +1037,7 @@ function or(...args) {
1002
1037
  }
1003
1038
  }
1004
1039
  }
1040
+ if (optionsTerminatorResult != null) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorResult);
1005
1041
  return {
1006
1042
  ...error,
1007
1043
  success: false
@@ -1015,10 +1051,16 @@ function or(...args) {
1015
1051
  let zeroConsumedBranch = null;
1016
1052
  let zeroConsumedCount = 0;
1017
1053
  let provisionalConsuming = null;
1054
+ let optionsTerminatorResult = null;
1018
1055
  let provisionalAmbiguous = false;
1019
1056
  for (const [parser, i] of orderedParsers) {
1020
1057
  const resultOrPromise = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1021
1058
  const result = await resultOrPromise;
1059
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1060
+ if (activeState?.[0] === i && activeState[1].success && activeState[1].consumed.length > 0 && result.next.buffer.length < 1) return preserveExclusiveStateAfterOptionsTerminator(context, result);
1061
+ optionsTerminatorResult ??= result;
1062
+ continue;
1063
+ }
1022
1064
  if (result.success && result.consumed.length > 0) {
1023
1065
  if (result.provisional) {
1024
1066
  const activeBranchLocked = activeState != null && activeState[1].success && activeState[1].consumed.length > 0;
@@ -1201,6 +1243,7 @@ function or(...args) {
1201
1243
  }
1202
1244
  }
1203
1245
  }
1246
+ if (optionsTerminatorResult != null) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorResult);
1204
1247
  return {
1205
1248
  ...error,
1206
1249
  success: false
@@ -1302,82 +1345,122 @@ function createLongestMatch(...args) {
1302
1345
  return createUnexpectedInputErrorWithScopedSuggestions(defaultMsg, token, parsers, options?.errors?.suggestions);
1303
1346
  })()
1304
1347
  });
1305
- const parseSync = (context) => {
1348
+ const commitLongestMatch = (context, match, optionsTerminatorResult) => {
1349
+ const selectedResult = optionsTerminatorResult == null ? match.result : {
1350
+ ...match.result,
1351
+ next: {
1352
+ ...match.result.next,
1353
+ buffer: optionsTerminatorResult.next.buffer,
1354
+ optionsTerminated: true
1355
+ },
1356
+ consumed: [...optionsTerminatorResult.consumed, ...match.result.consumed]
1357
+ };
1358
+ const parser = parsers[match.index];
1359
+ const mergedExec = mergeChildExec(context.exec, selectedResult.next.exec);
1360
+ return {
1361
+ success: true,
1362
+ next: {
1363
+ ...context,
1364
+ buffer: selectedResult.next.buffer,
1365
+ optionsTerminated: selectedResult.next.optionsTerminated,
1366
+ state: createExclusiveState(context.state, match.index, parser, selectedResult),
1367
+ ...mergedExec != null ? {
1368
+ exec: mergedExec,
1369
+ dependencyRegistry: mergedExec.dependencyRegistry
1370
+ } : {}
1371
+ },
1372
+ consumed: selectedResult.consumed
1373
+ };
1374
+ };
1375
+ const resolveLongestMatchOptionsTerminator = (context, bestMatch, optionsTerminatorMatches) => {
1376
+ if (optionsTerminatorMatches.length < 1) return null;
1377
+ if (bestMatch != null && bestMatch.consumed > 0) return null;
1378
+ const activeState = normalizeExclusiveState(context.state);
1379
+ if (activeState != null && activeState[1].success && activeState[1].consumed.length > 0) return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorMatches[0].result);
1380
+ let selected = bestMatch == null ? null : {
1381
+ match: bestMatch,
1382
+ fromTerminator: false
1383
+ };
1384
+ for (const match of optionsTerminatorMatches) {
1385
+ if (!match.selectedAfterTerminator && (extractRequiredUsage(parsers[match.index].usage).length > 0 || isUnselectedBooleanOptionState(match.result.next.state))) continue;
1386
+ const selectedIsProvisional = selected != null && !!selected.match.result.provisional;
1387
+ const matchIsProvisional = !!match.result.provisional;
1388
+ if (selected == null || selectedIsProvisional && !matchIsProvisional || selectedIsProvisional === matchIsProvisional && match.index < selected.match.index) selected = {
1389
+ match,
1390
+ fromTerminator: true
1391
+ };
1392
+ }
1393
+ if (selected != null) return commitLongestMatch(context, selected.match, selected.fromTerminator ? void 0 : optionsTerminatorMatches[0].result);
1394
+ return preserveExclusiveStateAfterOptionsTerminator(context, optionsTerminatorMatches[0].result);
1395
+ };
1396
+ const parseSyncBranches = (context) => {
1306
1397
  let bestMatch = null;
1398
+ const optionsTerminatorMatches = [];
1307
1399
  let error = getInitialError(context);
1308
1400
  const activeState = normalizeExclusiveState(context.state);
1309
1401
  for (let i = 0; i < syncParsers.length; i++) {
1310
1402
  const parser = syncParsers[i];
1311
- const result = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1403
+ const childContext = withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser);
1404
+ const result = parser.parse(childContext);
1405
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1406
+ optionsTerminatorMatches.push({
1407
+ index: i,
1408
+ result,
1409
+ consumed: 1,
1410
+ ...normalizeExclusiveState(result.next.state) != null ? { selectedAfterTerminator: true } : {}
1411
+ });
1412
+ continue;
1413
+ }
1312
1414
  if (result.success) {
1313
1415
  const consumed = context.buffer.length - result.next.buffer.length;
1314
1416
  const bestIsProvisional = bestMatch != null && bestMatch.result.success && !!bestMatch.result.provisional;
1315
1417
  if (bestMatch === null || consumed > bestMatch.consumed || consumed === bestMatch.consumed && bestIsProvisional && !result.provisional) bestMatch = {
1316
1418
  index: i,
1317
- parser,
1318
1419
  result,
1319
1420
  consumed
1320
1421
  };
1321
1422
  } else if (error.consumed < result.consumed) error = result;
1322
1423
  }
1323
- if (bestMatch && bestMatch.result.success) {
1324
- const mergedExec = mergeChildExec(context.exec, bestMatch.result.next.exec);
1325
- return {
1326
- success: true,
1327
- next: {
1328
- ...context,
1329
- buffer: bestMatch.result.next.buffer,
1330
- optionsTerminated: bestMatch.result.next.optionsTerminated,
1331
- state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
1332
- ...mergedExec != null ? {
1333
- exec: mergedExec,
1334
- dependencyRegistry: mergedExec.dependencyRegistry
1335
- } : {}
1336
- },
1337
- consumed: bestMatch.result.consumed
1338
- };
1339
- }
1424
+ const optionsTerminatorResolution = resolveLongestMatchOptionsTerminator(context, bestMatch, optionsTerminatorMatches);
1425
+ if (optionsTerminatorResolution != null) return optionsTerminatorResolution;
1426
+ if (bestMatch != null) return commitLongestMatch(context, bestMatch);
1340
1427
  return {
1341
1428
  ...error,
1342
1429
  success: false
1343
1430
  };
1344
1431
  };
1345
- const parseAsync = async (context) => {
1432
+ const parseAsyncBranches = async (context) => {
1346
1433
  let bestMatch = null;
1434
+ const optionsTerminatorMatches = [];
1347
1435
  let error = getInitialError(context);
1348
1436
  const activeState = normalizeExclusiveState(context.state);
1349
1437
  for (let i = 0; i < parsers.length; i++) {
1350
1438
  const parser = parsers[i];
1351
- const resultOrPromise = parser.parse(withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser));
1439
+ const childContext = withChildContext$1(context, i, activeState == null || activeState[0] !== i || !activeState[1].success ? parser.initialState : activeState[1].next.state, parser);
1440
+ const resultOrPromise = parser.parse(childContext);
1352
1441
  const result = await resultOrPromise;
1442
+ if (isOptionsTerminatorOnlyResult(context, result)) {
1443
+ optionsTerminatorMatches.push({
1444
+ index: i,
1445
+ result,
1446
+ consumed: 1,
1447
+ ...normalizeExclusiveState(result.next.state) != null ? { selectedAfterTerminator: true } : {}
1448
+ });
1449
+ continue;
1450
+ }
1353
1451
  if (result.success) {
1354
1452
  const consumed = context.buffer.length - result.next.buffer.length;
1355
1453
  const bestIsProvisional = bestMatch != null && bestMatch.result.success && !!bestMatch.result.provisional;
1356
1454
  if (bestMatch === null || consumed > bestMatch.consumed || consumed === bestMatch.consumed && bestIsProvisional && !result.provisional) bestMatch = {
1357
1455
  index: i,
1358
- parser,
1359
1456
  result,
1360
1457
  consumed
1361
1458
  };
1362
1459
  } else if (error.consumed < result.consumed) error = result;
1363
1460
  }
1364
- if (bestMatch && bestMatch.result.success) {
1365
- const mergedExec = mergeChildExec(context.exec, bestMatch.result.next.exec);
1366
- return {
1367
- success: true,
1368
- next: {
1369
- ...context,
1370
- buffer: bestMatch.result.next.buffer,
1371
- optionsTerminated: bestMatch.result.next.optionsTerminated,
1372
- state: createExclusiveState(context.state, bestMatch.index, bestMatch.parser, bestMatch.result),
1373
- ...mergedExec != null ? {
1374
- exec: mergedExec,
1375
- dependencyRegistry: mergedExec.dependencyRegistry
1376
- } : {}
1377
- },
1378
- consumed: bestMatch.result.consumed
1379
- };
1380
- }
1461
+ const optionsTerminatorResolution = resolveLongestMatchOptionsTerminator(context, bestMatch, optionsTerminatorMatches);
1462
+ if (optionsTerminatorResolution != null) return optionsTerminatorResolution;
1463
+ if (bestMatch != null) return commitLongestMatch(context, bestMatch);
1381
1464
  return {
1382
1465
  ...error,
1383
1466
  success: false
@@ -1400,7 +1483,7 @@ function createLongestMatch(...args) {
1400
1483
  return extractExclusivePhase2Seed(parsers, state, exec, combinedMode);
1401
1484
  },
1402
1485
  parse(context) {
1403
- return dispatchByMode(combinedMode, () => parseSync(context), () => parseAsync(context));
1486
+ return dispatchByMode(combinedMode, () => parseSyncBranches(context), () => parseAsyncBranches(context));
1404
1487
  },
1405
1488
  getSuggestRuntimeNodes(state, path) {
1406
1489
  return getExclusiveSuggestRuntimeNodes(parsers, state, path);
@@ -1446,6 +1529,33 @@ function createLongestMatch(...args) {
1446
1529
  return fluent(multiResult);
1447
1530
  }
1448
1531
  /**
1532
+ * Creates the initial parse error shared by object-like combinators.
1533
+ * @param context The current parser context.
1534
+ * @param noMatchContext The kinds of input accepted by the combinator.
1535
+ * @param errors Optional custom error formatters.
1536
+ * @returns A zero-consumption parse error.
1537
+ */
1538
+ function createObjectLikeInitialError(context, noMatchContext, errors) {
1539
+ if (context.buffer.length < 1) {
1540
+ const customEndOfInput = errors?.endOfInput;
1541
+ return {
1542
+ consumed: 0,
1543
+ error: customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext)
1544
+ };
1545
+ }
1546
+ const token = context.buffer[0];
1547
+ const customMessage = errors?.unexpectedInput;
1548
+ if (customMessage) return {
1549
+ consumed: 0,
1550
+ error: typeof customMessage === "function" ? customMessage(token) : customMessage
1551
+ };
1552
+ const baseError = message`Unexpected option or argument: ${token}.`;
1553
+ return {
1554
+ consumed: 0,
1555
+ error: createErrorWithSuggestions(baseError, token, context.usage, "both", errors?.suggestions)
1556
+ };
1557
+ }
1558
+ /**
1449
1559
  * Internal sync helper for object suggest functionality.
1450
1560
  * @internal
1451
1561
  */
@@ -1838,19 +1948,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
1838
1948
  checkDuplicateReachableLeadingCommandNames(parserPairs.map(([field, parser]) => [field, parser]));
1839
1949
  const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
1840
1950
  const combinedMode = parserKeys.some((k) => parsers[k].mode === "async") ? "async" : "sync";
1841
- const getInitialError = (context) => ({
1842
- consumed: 0,
1843
- error: context.buffer.length > 0 ? (() => {
1844
- const token = context.buffer[0];
1845
- const customMessage = options.errors?.unexpectedInput;
1846
- if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
1847
- const baseError = message`Unexpected option or argument: ${token}.`;
1848
- return createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
1849
- })() : (() => {
1850
- const customEndOfInput = options.errors?.endOfInput;
1851
- return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
1852
- })()
1853
- });
1951
+ const getInitialError = (context) => createObjectLikeInitialError(context, noMatchContext, options.errors);
1854
1952
  const parseSync = (context) => {
1855
1953
  let error = getInitialError(context);
1856
1954
  let currentContext = context;
@@ -3720,6 +3818,7 @@ function merge(...args) {
3720
3818
  const syncParsers = syncSorted.map(([p]) => p);
3721
3819
  if (!options.allowDuplicates) checkDuplicateOptionNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser.usage]));
3722
3820
  checkDuplicateReachableLeadingCommandNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser]));
3821
+ const noMatchContext = analyzeNoMatchContext(rawParsers);
3723
3822
  const mergedFieldParsers = collectChildFieldParsers(parsers);
3724
3823
  const duplicateOutputFieldNames = collectDuplicateFieldNames(mergedFieldParsers);
3725
3824
  const parserStateKey = (index) => `__parser_${index}`;
@@ -3813,8 +3912,7 @@ function merge(...args) {
3813
3912
  };
3814
3913
  return {
3815
3914
  success: false,
3816
- consumed: 0,
3817
- error: message`No matching option or argument found.`
3915
+ ...createObjectLikeInitialError(context, noMatchContext)
3818
3916
  };
3819
3917
  };
3820
3918
  const parseAsync = async (context) => {
@@ -3859,8 +3957,7 @@ function merge(...args) {
3859
3957
  };
3860
3958
  return {
3861
3959
  success: false,
3862
- consumed: 0,
3863
- error: message`No matching option or argument found.`
3960
+ ...createObjectLikeInitialError(context, noMatchContext)
3864
3961
  };
3865
3962
  };
3866
3963
  const mergeParser = {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@optique/core",
3
- "version": "1.2.1",
3
+ "version": "1.2.3",
4
4
  "description": "Type-safe combinatorial command-line interface parser",
5
5
  "keywords": [
6
6
  "CLI",
@@ -225,7 +225,7 @@
225
225
  "fast-check": "^4.7.0",
226
226
  "tsdown": "^0.13.0",
227
227
  "typescript": "^5.8.3",
228
- "@optique/env": "1.2.1"
228
+ "@optique/env": "1.2.3"
229
229
  },
230
230
  "scripts": {
231
231
  "build": "tsdown",