@optique/core 0.9.0-dev.206 → 0.9.0-dev.211

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.
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
73
73
  */
74
74
  var DuplicateOptionError = class extends Error {
75
75
  constructor(optionName$1, sources) {
76
- super(`Duplicate option name "${optionName$1}" found in fields: ${sources.join(", ")}. Each option name must be unique within a parser combinator.`);
76
+ const sourceNames = sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s);
77
+ super(`Duplicate option name "${optionName$1}" found in fields: ${sourceNames.join(", ")}. Each option name must be unique within a parser combinator.`);
77
78
  this.optionName = optionName$1;
78
79
  this.sources = sources;
79
80
  this.name = "DuplicateOptionError";
@@ -116,79 +117,45 @@ function generateNoMatchError(context) {
116
117
  * Creates a complete() method shared by or() and longestMatch().
117
118
  * @internal
118
119
  */
119
- function createExclusiveComplete(parsers, options, noMatchContext, isAsync) {
120
- const syncParsers = parsers;
120
+ function createExclusiveComplete(parsers, options, noMatchContext) {
121
121
  return (state) => {
122
122
  if (state == null) return {
123
123
  success: false,
124
124
  error: getNoMatchError(options, noMatchContext)
125
125
  };
126
126
  const [i, result] = state;
127
- if (!result.success) return {
127
+ if (result.success) return parsers[i].complete(result.next.state);
128
+ return {
128
129
  success: false,
129
130
  error: result.error
130
131
  };
131
- if (isAsync) return (async () => {
132
- const completeResult = await parsers[i].complete(result.next.state);
133
- return completeResult;
134
- })();
135
- return syncParsers[i].complete(result.next.state);
136
132
  };
137
133
  }
138
134
  /**
139
135
  * Creates a suggest() method shared by or() and longestMatch().
140
136
  * @internal
141
137
  */
142
- function createExclusiveSuggest(parsers, isAsync) {
143
- const syncParsers = parsers;
144
- if (isAsync) return (context, prefix) => {
145
- return async function* () {
146
- const suggestions = [];
147
- if (context.state == null) for (const parser of parsers) {
148
- const parserSuggestions = parser.suggest({
149
- ...context,
150
- state: parser.initialState
151
- }, prefix);
152
- if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
153
- else suggestions.push(...parserSuggestions);
154
- }
155
- else {
156
- const [index, parserResult] = context.state;
157
- if (parserResult.success) {
158
- const parser = parsers[index];
159
- const parserSuggestions = parser.suggest({
160
- ...context,
161
- state: parserResult.next.state
162
- }, prefix);
163
- if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
164
- else suggestions.push(...parserSuggestions);
165
- }
166
- }
167
- yield* require_suggestion.deduplicateSuggestions(suggestions);
168
- }();
169
- };
138
+ function createExclusiveSuggest(parsers) {
170
139
  return (context, prefix) => {
171
- return function* () {
172
- const suggestions = [];
173
- if (context.state == null) for (const parser of syncParsers) {
174
- const parserSuggestions = parser.suggest({
140
+ const suggestions = [];
141
+ if (context.state == null) for (const parser of parsers) {
142
+ const parserSuggestions = parser.suggest({
143
+ ...context,
144
+ state: parser.initialState
145
+ }, prefix);
146
+ suggestions.push(...parserSuggestions);
147
+ }
148
+ else {
149
+ const [index, parserResult] = context.state;
150
+ if (parserResult.success) {
151
+ const parserSuggestions = parsers[index].suggest({
175
152
  ...context,
176
- state: parser.initialState
153
+ state: parserResult.next.state
177
154
  }, prefix);
178
155
  suggestions.push(...parserSuggestions);
179
156
  }
180
- else {
181
- const [index, parserResult] = context.state;
182
- if (parserResult.success) {
183
- const parserSuggestions = syncParsers[index].suggest({
184
- ...context,
185
- state: parserResult.next.state
186
- }, prefix);
187
- suggestions.push(...parserSuggestions);
188
- }
189
- }
190
- yield* require_suggestion.deduplicateSuggestions(suggestions);
191
- }();
157
+ }
158
+ return require_suggestion.deduplicateSuggestions(suggestions);
192
159
  };
193
160
  }
194
161
  /**
@@ -224,80 +191,7 @@ function or(...args) {
224
191
  options = void 0;
225
192
  }
226
193
  const noMatchContext = analyzeNoMatchContext(parsers);
227
- const combinedMode = parsers.some((p) => p.$mode === "async") ? "async" : "sync";
228
- const isAsync = combinedMode === "async";
229
- const syncParsers = parsers;
230
- const getInitialError = (context) => ({
231
- consumed: 0,
232
- error: context.buffer.length < 1 ? getNoMatchError(options, noMatchContext) : createUnexpectedInputError(context.buffer[0], context.usage, options)
233
- });
234
- const parseSync = (context) => {
235
- let error = getInitialError(context);
236
- const orderedParsers = syncParsers.map((p, i) => [p, i]);
237
- orderedParsers.sort(([_, a], [__, b]) => context.state?.[0] === a ? -1 : context.state?.[0] === b ? 1 : a - b);
238
- for (const [parser, i] of orderedParsers) {
239
- const result = parser.parse({
240
- ...context,
241
- state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
242
- });
243
- if (result.success && result.consumed.length > 0) {
244
- if (context.state?.[0] !== i && context.state?.[1].success) return {
245
- success: false,
246
- consumed: context.buffer.length - result.next.buffer.length,
247
- error: require_message.message`${require_message.values(context.state[1].consumed)} and ${require_message.values(result.consumed)} cannot be used together.`
248
- };
249
- return {
250
- success: true,
251
- next: {
252
- ...context,
253
- buffer: result.next.buffer,
254
- optionsTerminated: result.next.optionsTerminated,
255
- state: [i, result]
256
- },
257
- consumed: result.consumed
258
- };
259
- } else if (!result.success && error.consumed < result.consumed) error = result;
260
- }
261
- return {
262
- ...error,
263
- success: false
264
- };
265
- };
266
- const parseAsync = async (context) => {
267
- let error = getInitialError(context);
268
- const orderedParsers = parsers.map((p, i) => [p, i]);
269
- orderedParsers.sort(([_, a], [__, b]) => context.state?.[0] === a ? -1 : context.state?.[0] === b ? 1 : a - b);
270
- for (const [parser, i] of orderedParsers) {
271
- const resultOrPromise = parser.parse({
272
- ...context,
273
- state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
274
- });
275
- const result = await resultOrPromise;
276
- if (result.success && result.consumed.length > 0) {
277
- if (context.state?.[0] !== i && context.state?.[1].success) return {
278
- success: false,
279
- consumed: context.buffer.length - result.next.buffer.length,
280
- error: require_message.message`${require_message.values(context.state[1].consumed)} and ${require_message.values(result.consumed)} cannot be used together.`
281
- };
282
- return {
283
- success: true,
284
- next: {
285
- ...context,
286
- buffer: result.next.buffer,
287
- optionsTerminated: result.next.optionsTerminated,
288
- state: [i, result]
289
- },
290
- consumed: result.consumed
291
- };
292
- } else if (!result.success && error.consumed < result.consumed) error = result;
293
- }
294
- return {
295
- ...error,
296
- success: false
297
- };
298
- };
299
194
  return {
300
- $mode: combinedMode,
301
195
  $valueType: [],
302
196
  $stateType: [],
303
197
  priority: Math.max(...parsers.map((p) => p.priority)),
@@ -306,12 +200,43 @@ function or(...args) {
306
200
  terms: parsers.map((p) => p.usage)
307
201
  }],
308
202
  initialState: void 0,
309
- complete: createExclusiveComplete(parsers, options, noMatchContext, isAsync),
203
+ complete: createExclusiveComplete(parsers, options, noMatchContext),
310
204
  parse(context) {
311
- if (isAsync) return parseAsync(context);
312
- return parseSync(context);
205
+ let error = {
206
+ consumed: 0,
207
+ error: context.buffer.length < 1 ? getNoMatchError(options, noMatchContext) : createUnexpectedInputError(context.buffer[0], context.usage, options)
208
+ };
209
+ const orderedParsers = parsers.map((p, i) => [p, i]);
210
+ orderedParsers.sort(([_, a], [__, b]) => context.state?.[0] === a ? -1 : context.state?.[0] === b ? 1 : a - b);
211
+ for (const [parser, i] of orderedParsers) {
212
+ const result = parser.parse({
213
+ ...context,
214
+ state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
215
+ });
216
+ if (result.success && result.consumed.length > 0) {
217
+ if (context.state?.[0] !== i && context.state?.[1].success) return {
218
+ success: false,
219
+ consumed: context.buffer.length - result.next.buffer.length,
220
+ error: require_message.message`${require_message.values(context.state[1].consumed)} and ${require_message.values(result.consumed)} cannot be used together.`
221
+ };
222
+ return {
223
+ success: true,
224
+ next: {
225
+ ...context,
226
+ buffer: result.next.buffer,
227
+ optionsTerminated: result.next.optionsTerminated,
228
+ state: [i, result]
229
+ },
230
+ consumed: result.consumed
231
+ };
232
+ } else if (!result.success && error.consumed < result.consumed) error = result;
233
+ }
234
+ return {
235
+ ...error,
236
+ success: false
237
+ };
313
238
  },
314
- suggest: createExclusiveSuggest(parsers, isAsync),
239
+ suggest: createExclusiveSuggest(parsers),
315
240
  getDocFragments(state, _defaultValue) {
316
241
  let description;
317
242
  let fragments;
@@ -360,82 +285,7 @@ function longestMatch(...args) {
360
285
  options = void 0;
361
286
  }
362
287
  const noMatchContext = analyzeNoMatchContext(parsers);
363
- const combinedMode = parsers.some((p) => p.$mode === "async") ? "async" : "sync";
364
- const isAsync = combinedMode === "async";
365
- const syncParsers = parsers;
366
- const getInitialError = (context) => ({
367
- consumed: 0,
368
- error: context.buffer.length < 1 ? getNoMatchError(options, noMatchContext) : createUnexpectedInputError(context.buffer[0], context.usage, options)
369
- });
370
- const parseSync = (context) => {
371
- let bestMatch = null;
372
- let error = getInitialError(context);
373
- for (let i = 0; i < syncParsers.length; i++) {
374
- const parser = syncParsers[i];
375
- const result = parser.parse({
376
- ...context,
377
- state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
378
- });
379
- if (result.success) {
380
- const consumed = context.buffer.length - result.next.buffer.length;
381
- if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
382
- index: i,
383
- result,
384
- consumed
385
- };
386
- } else if (error.consumed < result.consumed) error = result;
387
- }
388
- if (bestMatch && bestMatch.result.success) return {
389
- success: true,
390
- next: {
391
- ...context,
392
- buffer: bestMatch.result.next.buffer,
393
- optionsTerminated: bestMatch.result.next.optionsTerminated,
394
- state: [bestMatch.index, bestMatch.result]
395
- },
396
- consumed: bestMatch.result.consumed
397
- };
398
- return {
399
- ...error,
400
- success: false
401
- };
402
- };
403
- const parseAsync = async (context) => {
404
- let bestMatch = null;
405
- let error = getInitialError(context);
406
- for (let i = 0; i < parsers.length; i++) {
407
- const parser = parsers[i];
408
- const resultOrPromise = parser.parse({
409
- ...context,
410
- state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
411
- });
412
- const result = await resultOrPromise;
413
- if (result.success) {
414
- const consumed = context.buffer.length - result.next.buffer.length;
415
- if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
416
- index: i,
417
- result,
418
- consumed
419
- };
420
- } else if (error.consumed < result.consumed) error = result;
421
- }
422
- if (bestMatch && bestMatch.result.success) return {
423
- success: true,
424
- next: {
425
- ...context,
426
- buffer: bestMatch.result.next.buffer,
427
- optionsTerminated: bestMatch.result.next.optionsTerminated,
428
- state: [bestMatch.index, bestMatch.result]
429
- },
430
- consumed: bestMatch.result.consumed
431
- };
432
- return {
433
- ...error,
434
- success: false
435
- };
436
- };
437
288
  return {
438
- $mode: combinedMode,
439
289
  $valueType: [],
440
290
  $stateType: [],
441
291
  priority: Math.max(...parsers.map((p) => p.priority)),
@@ -444,12 +294,44 @@ function longestMatch(...args) {
444
294
  terms: parsers.map((p) => p.usage)
445
295
  }],
446
296
  initialState: void 0,
447
- complete: createExclusiveComplete(parsers, options, noMatchContext, isAsync),
297
+ complete: createExclusiveComplete(parsers, options, noMatchContext),
448
298
  parse(context) {
449
- if (isAsync) return parseAsync(context);
450
- return parseSync(context);
299
+ let bestMatch = null;
300
+ let error = {
301
+ consumed: 0,
302
+ error: context.buffer.length < 1 ? getNoMatchError(options, noMatchContext) : createUnexpectedInputError(context.buffer[0], context.usage, options)
303
+ };
304
+ for (let i = 0; i < parsers.length; i++) {
305
+ const parser = parsers[i];
306
+ const result = parser.parse({
307
+ ...context,
308
+ state: context.state == null || context.state[0] !== i || !context.state[1].success ? parser.initialState : context.state[1].next.state
309
+ });
310
+ if (result.success) {
311
+ const consumed = context.buffer.length - result.next.buffer.length;
312
+ if (bestMatch === null || consumed > bestMatch.consumed) bestMatch = {
313
+ index: i,
314
+ result,
315
+ consumed
316
+ };
317
+ } else if (error.consumed < result.consumed) error = result;
318
+ }
319
+ if (bestMatch && bestMatch.result.success) return {
320
+ success: true,
321
+ next: {
322
+ ...context,
323
+ buffer: bestMatch.result.next.buffer,
324
+ optionsTerminated: bestMatch.result.next.optionsTerminated,
325
+ state: [bestMatch.index, bestMatch.result]
326
+ },
327
+ consumed: bestMatch.result.consumed
328
+ };
329
+ return {
330
+ ...error,
331
+ success: false
332
+ };
451
333
  },
452
- suggest: createExclusiveSuggest(parsers, isAsync),
334
+ suggest: createExclusiveSuggest(parsers),
453
335
  getDocFragments(state, _defaultValue) {
454
336
  let description;
455
337
  let footer;
@@ -475,61 +357,6 @@ function longestMatch(...args) {
475
357
  }
476
358
  };
477
359
  }
478
- /**
479
- * Internal sync helper for object suggest functionality.
480
- * @internal
481
- */
482
- function* suggestObjectSync(context, prefix, parserPairs) {
483
- if (context.buffer.length > 0) {
484
- const lastToken = context.buffer[context.buffer.length - 1];
485
- for (const [field, parser] of parserPairs) if (isOptionRequiringValue(parser.usage, lastToken)) {
486
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
487
- yield* parser.suggest({
488
- ...context,
489
- state: fieldState
490
- }, prefix);
491
- return;
492
- }
493
- }
494
- const suggestions = [];
495
- for (const [field, parser] of parserPairs) {
496
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
497
- const fieldSuggestions = parser.suggest({
498
- ...context,
499
- state: fieldState
500
- }, prefix);
501
- suggestions.push(...fieldSuggestions);
502
- }
503
- yield* require_suggestion.deduplicateSuggestions(suggestions);
504
- }
505
- /**
506
- * Internal async helper for object suggest functionality.
507
- * @internal
508
- */
509
- async function* suggestObjectAsync(context, prefix, parserPairs) {
510
- if (context.buffer.length > 0) {
511
- const lastToken = context.buffer[context.buffer.length - 1];
512
- for (const [field, parser] of parserPairs) if (isOptionRequiringValue(parser.usage, lastToken)) {
513
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
514
- const suggestions$1 = parser.suggest({
515
- ...context,
516
- state: fieldState
517
- }, prefix);
518
- for await (const s of suggestions$1) yield s;
519
- return;
520
- }
521
- }
522
- const suggestions = [];
523
- for (const [field, parser] of parserPairs) {
524
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
525
- const fieldSuggestions = parser.suggest({
526
- ...context,
527
- state: fieldState
528
- }, prefix);
529
- for await (const s of fieldSuggestions) suggestions.push(s);
530
- }
531
- yield* require_suggestion.deduplicateSuggestions(suggestions);
532
- }
533
360
  function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
534
361
  const label = typeof labelOrParsers === "string" ? labelOrParsers : void 0;
535
362
  let parsers;
@@ -541,189 +368,138 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
541
368
  parsers = labelOrParsers;
542
369
  options = maybeParsersOrOptions ?? {};
543
370
  }
544
- const parserPairs = Object.entries(parsers);
371
+ const parserKeys = Reflect.ownKeys(parsers);
372
+ const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
545
373
  parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
374
+ const initialState = {};
375
+ for (const key of parserKeys) initialState[key] = parsers[key].initialState;
546
376
  if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
547
- const noMatchContext = analyzeNoMatchContext(Object.values(parsers));
548
- const combinedMode = Object.values(parsers).some((p) => p.$mode === "async") ? "async" : "sync";
549
- const isAsync = combinedMode === "async";
550
- const getInitialError = (context) => ({
551
- consumed: 0,
552
- error: context.buffer.length > 0 ? (() => {
553
- const token = context.buffer[0];
554
- const customMessage = options.errors?.unexpectedInput;
555
- if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
556
- const baseError = require_message.message`Unexpected option or argument: ${token}.`;
557
- return require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
558
- })() : (() => {
559
- const customEndOfInput = options.errors?.endOfInput;
560
- return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
561
- })()
562
- });
563
- const parseSync = (context) => {
564
- let error = getInitialError(context);
565
- let currentContext = context;
566
- let anySuccess = false;
567
- const allConsumed = [];
568
- let madeProgress = true;
569
- while (madeProgress && currentContext.buffer.length > 0) {
570
- madeProgress = false;
571
- for (const [field, parser] of parserPairs) {
572
- const result = parser.parse({
573
- ...currentContext,
574
- state: currentContext.state && typeof currentContext.state === "object" && field in currentContext.state ? currentContext.state[field] : parser.initialState
575
- });
576
- if (result.success && result.consumed.length > 0) {
577
- currentContext = {
578
- ...currentContext,
579
- buffer: result.next.buffer,
580
- optionsTerminated: result.next.optionsTerminated,
581
- state: {
582
- ...currentContext.state,
583
- [field]: result.next.state
584
- }
585
- };
586
- allConsumed.push(...result.consumed);
587
- anySuccess = true;
588
- madeProgress = true;
589
- break;
590
- } else if (!result.success && error.consumed < result.consumed) error = result;
591
- }
592
- }
593
- if (anySuccess) return {
594
- success: true,
595
- next: currentContext,
596
- consumed: allConsumed
597
- };
598
- if (context.buffer.length === 0) {
599
- let allCanComplete = true;
600
- for (const [field, parser] of parserPairs) {
601
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
602
- const completeResult = parser.complete(fieldState);
603
- if (!completeResult.success) {
604
- allCanComplete = false;
605
- break;
377
+ const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
378
+ return {
379
+ $valueType: [],
380
+ $stateType: [],
381
+ priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
382
+ usage: parserPairs.flatMap(([_, p]) => p.usage),
383
+ initialState,
384
+ parse(context) {
385
+ if (!options.allowDuplicates) {
386
+ const optionNameSources = /* @__PURE__ */ new Map();
387
+ for (const [field, parser] of parserPairs) {
388
+ const names = require_usage.extractOptionNames(parser.usage);
389
+ for (const name of names) {
390
+ if (!optionNameSources.has(name)) optionNameSources.set(name, []);
391
+ optionNameSources.get(name).push(field);
392
+ }
606
393
  }
394
+ for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
395
+ success: false,
396
+ consumed: 0,
397
+ error: require_message.message`Duplicate option name ${require_message.optionName(name)} found in fields: ${require_message.values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
398
+ };
607
399
  }
608
- if (allCanComplete) return {
609
- success: true,
610
- next: context,
611
- consumed: []
400
+ let error = {
401
+ consumed: 0,
402
+ error: context.buffer.length > 0 ? (() => {
403
+ const token = context.buffer[0];
404
+ const customMessage = options.errors?.unexpectedInput;
405
+ if (customMessage) return typeof customMessage === "function" ? customMessage(token) : customMessage;
406
+ const baseError = require_message.message`Unexpected option or argument: ${token}.`;
407
+ return require_suggestion.createErrorWithSuggestions(baseError, token, context.usage, "both", options.errors?.suggestions);
408
+ })() : (() => {
409
+ const customEndOfInput = options.errors?.endOfInput;
410
+ return customEndOfInput ? typeof customEndOfInput === "function" ? customEndOfInput(noMatchContext) : customEndOfInput : generateNoMatchError(noMatchContext);
411
+ })()
612
412
  };
613
- }
614
- return {
615
- ...error,
616
- success: false
617
- };
618
- };
619
- const parseAsync = async (context) => {
620
- let error = getInitialError(context);
621
- let currentContext = context;
622
- let anySuccess = false;
623
- const allConsumed = [];
624
- let madeProgress = true;
625
- while (madeProgress && currentContext.buffer.length > 0) {
626
- madeProgress = false;
627
- for (const [field, parser] of parserPairs) {
628
- const resultOrPromise = parser.parse({
629
- ...currentContext,
630
- state: currentContext.state && typeof currentContext.state === "object" && field in currentContext.state ? currentContext.state[field] : parser.initialState
631
- });
632
- const result = await resultOrPromise;
633
- if (result.success && result.consumed.length > 0) {
634
- currentContext = {
413
+ let currentContext = context;
414
+ let anySuccess = false;
415
+ const allConsumed = [];
416
+ let madeProgress = true;
417
+ while (madeProgress && currentContext.buffer.length > 0) {
418
+ madeProgress = false;
419
+ for (const [field, parser] of parserPairs) {
420
+ const result = parser.parse({
635
421
  ...currentContext,
636
- buffer: result.next.buffer,
637
- optionsTerminated: result.next.optionsTerminated,
638
- state: {
639
- ...currentContext.state,
640
- [field]: result.next.state
641
- }
642
- };
643
- allConsumed.push(...result.consumed);
644
- anySuccess = true;
645
- madeProgress = true;
646
- break;
647
- } else if (!result.success && error.consumed < result.consumed) error = result;
648
- }
649
- }
650
- if (anySuccess) return {
651
- success: true,
652
- next: currentContext,
653
- consumed: allConsumed
654
- };
655
- if (context.buffer.length === 0) {
656
- let allCanComplete = true;
657
- for (const [field, parser] of parserPairs) {
658
- const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
659
- const completeResult = await parser.complete(fieldState);
660
- if (!completeResult.success) {
661
- allCanComplete = false;
662
- break;
422
+ state: currentContext.state && typeof currentContext.state === "object" && field in currentContext.state ? currentContext.state[field] : parser.initialState
423
+ });
424
+ if (result.success && result.consumed.length > 0) {
425
+ currentContext = {
426
+ ...currentContext,
427
+ buffer: result.next.buffer,
428
+ optionsTerminated: result.next.optionsTerminated,
429
+ state: {
430
+ ...currentContext.state,
431
+ [field]: result.next.state
432
+ }
433
+ };
434
+ allConsumed.push(...result.consumed);
435
+ anySuccess = true;
436
+ madeProgress = true;
437
+ break;
438
+ } else if (!result.success && error.consumed < result.consumed) error = result;
663
439
  }
664
440
  }
665
- if (allCanComplete) return {
441
+ if (anySuccess) return {
666
442
  success: true,
667
- next: context,
668
- consumed: []
443
+ next: currentContext,
444
+ consumed: allConsumed
669
445
  };
670
- }
671
- return {
672
- ...error,
673
- success: false
674
- };
675
- };
676
- return {
677
- $mode: combinedMode,
678
- $valueType: [],
679
- $stateType: [],
680
- priority: Math.max(...Object.values(parsers).map((p) => p.priority)),
681
- usage: parserPairs.flatMap(([_, p]) => p.usage),
682
- initialState: Object.fromEntries(Object.entries(parsers).map(([key, parser]) => [key, parser.initialState])),
683
- parse(context) {
684
- if (isAsync) return parseAsync(context);
685
- return parseSync(context);
686
- },
687
- complete(state) {
688
- if (!isAsync) {
689
- const result = {};
690
- const stateRecord = state;
691
- for (const field in stateRecord) {
692
- if (!(field in parsers)) continue;
693
- const valueResult = parsers[field].complete(stateRecord[field]);
694
- if (valueResult.success) result[field] = valueResult.value;
695
- else return {
696
- success: false,
697
- error: valueResult.error
698
- };
446
+ if (context.buffer.length === 0) {
447
+ let allCanComplete = true;
448
+ for (const [field, parser] of parserPairs) {
449
+ const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
450
+ const completeResult = parser.complete(fieldState);
451
+ if (!completeResult.success) {
452
+ allCanComplete = false;
453
+ break;
454
+ }
699
455
  }
700
- return {
456
+ if (allCanComplete) return {
701
457
  success: true,
702
- value: result
458
+ next: context,
459
+ consumed: []
703
460
  };
704
461
  }
705
- return (async () => {
706
- const result = {};
707
- const stateRecord = state;
708
- for (const field in stateRecord) {
709
- if (!(field in parsers)) continue;
710
- const valueResult = await parsers[field].complete(stateRecord[field]);
711
- if (valueResult.success) result[field] = valueResult.value;
712
- else return {
713
- success: false,
714
- error: valueResult.error
715
- };
716
- }
717
- return {
718
- success: true,
719
- value: result
462
+ return {
463
+ ...error,
464
+ success: false
465
+ };
466
+ },
467
+ complete(state) {
468
+ const result = {};
469
+ for (const field of parserKeys) {
470
+ const valueResult = parsers[field].complete(state[field]);
471
+ if (valueResult.success) result[field] = valueResult.value;
472
+ else return {
473
+ success: false,
474
+ error: valueResult.error
720
475
  };
721
- })();
476
+ }
477
+ return {
478
+ success: true,
479
+ value: result
480
+ };
722
481
  },
723
482
  suggest(context, prefix) {
724
- if (isAsync) return suggestObjectAsync(context, prefix, parserPairs);
725
- const syncParserPairs = parserPairs;
726
- return suggestObjectSync(context, prefix, syncParserPairs);
483
+ const suggestions = [];
484
+ if (context.buffer.length > 0) {
485
+ const lastToken = context.buffer[context.buffer.length - 1];
486
+ for (const [field, parser] of parserPairs) if (isOptionRequiringValue(parser.usage, lastToken)) {
487
+ const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
488
+ return Array.from(parser.suggest({
489
+ ...context,
490
+ state: fieldState
491
+ }, prefix));
492
+ }
493
+ }
494
+ for (const [field, parser] of parserPairs) {
495
+ const fieldState = context.state && typeof context.state === "object" && field in context.state ? context.state[field] : parser.initialState;
496
+ const fieldSuggestions = parser.suggest({
497
+ ...context,
498
+ state: fieldState
499
+ }, prefix);
500
+ suggestions.push(...fieldSuggestions);
501
+ }
502
+ return require_suggestion.deduplicateSuggestions(suggestions);
727
503
  },
728
504
  getDocFragments(state, defaultValue) {
729
505
  const fragments = parserPairs.flatMap(([field, p]) => {
@@ -752,35 +528,6 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
752
528
  }
753
529
  };
754
530
  }
755
- function suggestTupleSync(context, prefix, parsers) {
756
- const suggestions = [];
757
- const stateArray = context.state;
758
- for (let i = 0; i < parsers.length; i++) {
759
- const parser = parsers[i];
760
- const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
761
- const parserSuggestions = parser.suggest({
762
- ...context,
763
- state: parserState
764
- }, prefix);
765
- suggestions.push(...parserSuggestions);
766
- }
767
- return require_suggestion.deduplicateSuggestions(suggestions);
768
- }
769
- async function* suggestTupleAsync(context, prefix, parsers) {
770
- const suggestions = [];
771
- const stateArray = context.state;
772
- for (let i = 0; i < parsers.length; i++) {
773
- const parser = parsers[i];
774
- const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
775
- const parserSuggestions = parser.suggest({
776
- ...context,
777
- state: parserState
778
- }, prefix);
779
- if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
780
- else suggestions.push(...parserSuggestions);
781
- }
782
- yield* require_suggestion.deduplicateSuggestions(suggestions);
783
- }
784
531
  function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
785
532
  const label = typeof labelOrParsers === "string" ? labelOrParsers : void 0;
786
533
  let parsers;
@@ -792,187 +539,102 @@ function tuple(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
792
539
  parsers = labelOrParsers;
793
540
  options = maybeParsersOrOptions ?? {};
794
541
  }
795
- const combinedMode = parsers.some((p) => p.$mode === "async") ? "async" : "sync";
796
- const isAsync = combinedMode === "async";
797
- const syncParsers = parsers;
798
542
  if (!options.allowDuplicates) checkDuplicateOptionNames(parsers.map((parser, index) => [String(index), parser.usage]));
799
- const parseSync = (context) => {
800
- let currentContext = context;
801
- const allConsumed = [];
802
- const matchedParsers = /* @__PURE__ */ new Set();
803
- while (matchedParsers.size < syncParsers.length) {
804
- let foundMatch = false;
805
- let error = {
806
- consumed: 0,
807
- error: require_message.message`No remaining parsers could match the input.`
808
- };
809
- const stateArray = currentContext.state;
810
- const remainingParsers = syncParsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
811
- for (const [parser, index] of remainingParsers) {
812
- const result = parser.parse({
813
- ...currentContext,
814
- state: stateArray[index]
815
- });
816
- if (result.success && result.consumed.length > 0) {
817
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
818
- currentContext = {
819
- ...currentContext,
820
- buffer: result.next.buffer,
821
- optionsTerminated: result.next.optionsTerminated,
822
- state: newStateArray
823
- };
824
- allConsumed.push(...result.consumed);
825
- matchedParsers.add(index);
826
- foundMatch = true;
827
- break;
828
- } else if (!result.success && error.consumed < result.consumed) error = result;
829
- }
830
- if (!foundMatch) for (const [parser, index] of remainingParsers) {
831
- const result = parser.parse({
832
- ...currentContext,
833
- state: stateArray[index]
834
- });
835
- if (result.success && result.consumed.length < 1) {
836
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
837
- currentContext = {
838
- ...currentContext,
839
- state: newStateArray
840
- };
841
- matchedParsers.add(index);
842
- foundMatch = true;
843
- break;
844
- } else if (!result.success && result.consumed < 1) {
845
- matchedParsers.add(index);
846
- foundMatch = true;
847
- break;
848
- }
849
- }
850
- if (!foundMatch) return {
851
- ...error,
852
- success: false
853
- };
854
- }
855
- return {
856
- success: true,
857
- next: currentContext,
858
- consumed: allConsumed
859
- };
860
- };
861
- const parseAsync = async (context) => {
862
- let currentContext = context;
863
- const allConsumed = [];
864
- const matchedParsers = /* @__PURE__ */ new Set();
865
- while (matchedParsers.size < parsers.length) {
866
- let foundMatch = false;
867
- let error = {
868
- consumed: 0,
869
- error: require_message.message`No remaining parsers could match the input.`
870
- };
871
- const stateArray = currentContext.state;
872
- const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
873
- for (const [parser, index] of remainingParsers) {
874
- const resultOrPromise = parser.parse({
875
- ...currentContext,
876
- state: stateArray[index]
877
- });
878
- const result = await resultOrPromise;
879
- if (result.success && result.consumed.length > 0) {
880
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
881
- currentContext = {
882
- ...currentContext,
883
- buffer: result.next.buffer,
884
- optionsTerminated: result.next.optionsTerminated,
885
- state: newStateArray
886
- };
887
- allConsumed.push(...result.consumed);
888
- matchedParsers.add(index);
889
- foundMatch = true;
890
- break;
891
- } else if (!result.success && error.consumed < result.consumed) error = result;
892
- }
893
- if (!foundMatch) for (const [parser, index] of remainingParsers) {
894
- const resultOrPromise = parser.parse({
895
- ...currentContext,
896
- state: stateArray[index]
897
- });
898
- const result = await resultOrPromise;
899
- if (result.success && result.consumed.length < 1) {
900
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
901
- currentContext = {
902
- ...currentContext,
903
- state: newStateArray
904
- };
905
- matchedParsers.add(index);
906
- foundMatch = true;
907
- break;
908
- } else if (!result.success && result.consumed < 1) {
909
- matchedParsers.add(index);
910
- foundMatch = true;
911
- break;
912
- }
913
- }
914
- if (!foundMatch) return {
915
- ...error,
916
- success: false
917
- };
918
- }
919
- return {
920
- success: true,
921
- next: currentContext,
922
- consumed: allConsumed
923
- };
924
- };
925
543
  return {
926
- $mode: combinedMode,
927
544
  $valueType: [],
928
545
  $stateType: [],
929
546
  usage: parsers.toSorted((a, b) => b.priority - a.priority).flatMap((p) => p.usage),
930
547
  priority: parsers.length > 0 ? Math.max(...parsers.map((p) => p.priority)) : 0,
931
548
  initialState: parsers.map((parser) => parser.initialState),
932
549
  parse(context) {
933
- if (isAsync) return parseAsync(context);
934
- return parseSync(context);
935
- },
936
- complete(state) {
937
- if (!isAsync) {
938
- const result = [];
939
- const stateArray = state;
940
- for (let i = 0; i < syncParsers.length; i++) {
941
- const valueResult = syncParsers[i].complete(stateArray[i]);
942
- if (valueResult.success) result[i] = valueResult.value;
943
- else return {
944
- success: false,
945
- error: valueResult.error
946
- };
550
+ let currentContext = context;
551
+ const allConsumed = [];
552
+ const matchedParsers = /* @__PURE__ */ new Set();
553
+ while (matchedParsers.size < parsers.length) {
554
+ let foundMatch = false;
555
+ let error = {
556
+ consumed: 0,
557
+ error: require_message.message`No remaining parsers could match the input.`
558
+ };
559
+ const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
560
+ for (const [parser, index] of remainingParsers) {
561
+ const result = parser.parse({
562
+ ...currentContext,
563
+ state: currentContext.state[index]
564
+ });
565
+ if (result.success && result.consumed.length > 0) {
566
+ currentContext = {
567
+ ...currentContext,
568
+ buffer: result.next.buffer,
569
+ optionsTerminated: result.next.optionsTerminated,
570
+ state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
571
+ };
572
+ allConsumed.push(...result.consumed);
573
+ matchedParsers.add(index);
574
+ foundMatch = true;
575
+ break;
576
+ } else if (!result.success && error.consumed < result.consumed) error = result;
947
577
  }
948
- return {
949
- success: true,
950
- value: result
578
+ if (!foundMatch) for (const [parser, index] of remainingParsers) {
579
+ const result = parser.parse({
580
+ ...currentContext,
581
+ state: currentContext.state[index]
582
+ });
583
+ if (result.success && result.consumed.length < 1) {
584
+ currentContext = {
585
+ ...currentContext,
586
+ state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
587
+ };
588
+ matchedParsers.add(index);
589
+ foundMatch = true;
590
+ break;
591
+ } else if (!result.success && result.consumed < 1) {
592
+ matchedParsers.add(index);
593
+ foundMatch = true;
594
+ break;
595
+ }
596
+ }
597
+ if (!foundMatch) return {
598
+ ...error,
599
+ success: false
951
600
  };
952
601
  }
953
- return (async () => {
954
- const result = [];
955
- const stateArray = state;
956
- for (let i = 0; i < parsers.length; i++) {
957
- const valueResult = await parsers[i].complete(stateArray[i]);
958
- if (valueResult.success) result[i] = valueResult.value;
959
- else return {
960
- success: false,
961
- error: valueResult.error
962
- };
963
- }
964
- return {
965
- success: true,
966
- value: result
602
+ return {
603
+ success: true,
604
+ next: currentContext,
605
+ consumed: allConsumed
606
+ };
607
+ },
608
+ complete(state) {
609
+ const result = [];
610
+ for (let i = 0; i < parsers.length; i++) {
611
+ const valueResult = parsers[i].complete(state[i]);
612
+ if (valueResult.success) result[i] = valueResult.value;
613
+ else return {
614
+ success: false,
615
+ error: valueResult.error
967
616
  };
968
- })();
617
+ }
618
+ return {
619
+ success: true,
620
+ value: result
621
+ };
969
622
  },
970
623
  suggest(context, prefix) {
971
- if (isAsync) return suggestTupleAsync(context, prefix, parsers);
972
- return suggestTupleSync(context, prefix, syncParsers);
624
+ const suggestions = [];
625
+ for (let i = 0; i < parsers.length; i++) {
626
+ const parser = parsers[i];
627
+ const parserState = context.state && Array.isArray(context.state) ? context.state[i] : parser.initialState;
628
+ const parserSuggestions = parser.suggest({
629
+ ...context,
630
+ state: parserState
631
+ }, prefix);
632
+ suggestions.push(...parserSuggestions);
633
+ }
634
+ return require_suggestion.deduplicateSuggestions(suggestions);
973
635
  },
974
636
  getDocFragments(state, defaultValue) {
975
- const fragments = syncParsers.flatMap((p, i) => {
637
+ const fragments = parsers.flatMap((p, i) => {
976
638
  const indexState = state.kind === "unavailable" ? { kind: "unavailable" } : {
977
639
  kind: "available",
978
640
  state: state.state[i]
@@ -1009,235 +671,127 @@ function merge(...args) {
1009
671
  const startIndex = typeof args[0] === "string" ? 1 : 0;
1010
672
  const endIndex = lastArg && typeof lastArg === "object" && !("parse" in lastArg) && !("complete" in lastArg) ? args.length - 1 : args.length;
1011
673
  const rawParsers = args.slice(startIndex, endIndex);
1012
- const combinedMode = rawParsers.some((p) => p.$mode === "async") ? "async" : "sync";
1013
- const isAsync = combinedMode === "async";
1014
- const syncRawParsers = rawParsers;
1015
674
  const withIndex = rawParsers.map((p, i) => [p, i]);
1016
675
  const sorted = withIndex.toSorted(([a], [b]) => b.priority - a.priority);
1017
676
  const parsers = sorted.map(([p]) => p);
1018
- const syncWithIndex = syncRawParsers.map((p, i) => [p, i]);
1019
- const syncSorted = syncWithIndex.toSorted(([a], [b]) => b.priority - a.priority);
1020
- const syncParsers = syncSorted.map(([p]) => p);
1021
677
  if (!options.allowDuplicates) checkDuplicateOptionNames(sorted.map(([parser, originalIndex]) => [String(originalIndex), parser.usage]));
1022
678
  const initialState = {};
1023
679
  for (const parser of parsers) if (parser.initialState && typeof parser.initialState === "object") for (const field in parser.initialState) initialState[field] = parser.initialState[field];
1024
- const extractParserState = (parser, context, index) => {
1025
- if (parser.initialState === void 0) {
1026
- const key = `__parser_${index}`;
1027
- if (context.state && typeof context.state === "object" && key in context.state) return context.state[key];
1028
- return void 0;
1029
- } else if (parser.initialState && typeof parser.initialState === "object") {
1030
- if (context.state && typeof context.state === "object") {
1031
- const extractedState = {};
1032
- for (const field in parser.initialState) extractedState[field] = field in context.state ? context.state[field] : parser.initialState[field];
1033
- return extractedState;
1034
- }
1035
- return parser.initialState;
1036
- }
1037
- return parser.initialState;
1038
- };
1039
- const mergeResultState = (parser, context, result, index) => {
1040
- if (parser.initialState === void 0) {
1041
- const key = `__parser_${index}`;
1042
- if (result.success) {
1043
- if (result.consumed.length > 0 || result.next.state !== void 0) return {
1044
- ...context.state,
1045
- [key]: result.next.state
1046
- };
1047
- }
1048
- return { ...context.state };
1049
- }
1050
- return result.success ? {
1051
- ...context.state,
1052
- ...result.next.state
1053
- } : { ...context.state };
1054
- };
1055
- const parseSync = (context) => {
1056
- let currentContext = context;
1057
- let zeroConsumedSuccess = null;
1058
- for (let i = 0; i < syncParsers.length; i++) {
1059
- const parser = syncParsers[i];
1060
- const parserState = extractParserState(parser, currentContext, i);
1061
- const result = parser.parse({
1062
- ...currentContext,
1063
- state: parserState
1064
- });
1065
- if (result.success) {
1066
- const newState = mergeResultState(parser, currentContext, result, i);
1067
- const newContext = {
1068
- ...currentContext,
1069
- buffer: result.next.buffer,
1070
- optionsTerminated: result.next.optionsTerminated,
1071
- state: newState
1072
- };
1073
- if (result.consumed.length > 0) return {
1074
- success: true,
1075
- next: newContext,
1076
- consumed: result.consumed
1077
- };
1078
- currentContext = newContext;
1079
- if (zeroConsumedSuccess === null) zeroConsumedSuccess = {
1080
- context: newContext,
1081
- consumed: []
1082
- };
1083
- else zeroConsumedSuccess.context = newContext;
1084
- } else if (result.consumed < 1) continue;
1085
- else return result;
1086
- }
1087
- if (zeroConsumedSuccess !== null) return {
1088
- success: true,
1089
- next: zeroConsumedSuccess.context,
1090
- consumed: zeroConsumedSuccess.consumed
1091
- };
1092
- return {
1093
- success: false,
1094
- consumed: 0,
1095
- error: require_message.message`No matching option or argument found.`
1096
- };
1097
- };
1098
- const parseAsync = async (context) => {
1099
- let currentContext = context;
1100
- let zeroConsumedSuccess = null;
1101
- for (let i = 0; i < parsers.length; i++) {
1102
- const parser = parsers[i];
1103
- const parserState = extractParserState(parser, currentContext, i);
1104
- const resultOrPromise = parser.parse({
1105
- ...currentContext,
1106
- state: parserState
1107
- });
1108
- const result = await resultOrPromise;
1109
- if (result.success) {
1110
- const newState = mergeResultState(parser, currentContext, result, i);
1111
- const newContext = {
1112
- ...currentContext,
1113
- buffer: result.next.buffer,
1114
- optionsTerminated: result.next.optionsTerminated,
1115
- state: newState
1116
- };
1117
- if (result.consumed.length > 0) return {
1118
- success: true,
1119
- next: newContext,
1120
- consumed: result.consumed
1121
- };
1122
- currentContext = newContext;
1123
- if (zeroConsumedSuccess === null) zeroConsumedSuccess = {
1124
- context: newContext,
1125
- consumed: []
1126
- };
1127
- else zeroConsumedSuccess.context = newContext;
1128
- } else if (result.consumed < 1) continue;
1129
- else return result;
1130
- }
1131
- if (zeroConsumedSuccess !== null) return {
1132
- success: true,
1133
- next: zeroConsumedSuccess.context,
1134
- consumed: zeroConsumedSuccess.consumed
1135
- };
1136
- return {
1137
- success: false,
1138
- consumed: 0,
1139
- error: require_message.message`No matching option or argument found.`
1140
- };
1141
- };
1142
680
  return {
1143
- $mode: combinedMode,
1144
681
  $valueType: [],
1145
682
  $stateType: [],
1146
683
  priority: Math.max(...parsers.map((p) => p.priority)),
1147
684
  usage: parsers.flatMap((p) => p.usage),
1148
685
  initialState,
1149
686
  parse(context) {
1150
- if (isAsync) return parseAsync(context);
1151
- return parseSync(context);
687
+ let zeroConsumedSuccess = null;
688
+ for (let i = 0; i < parsers.length; i++) {
689
+ const parser = parsers[i];
690
+ let parserState;
691
+ if (parser.initialState === void 0) {
692
+ const key = `__parser_${i}`;
693
+ if (context.state && typeof context.state === "object" && key in context.state) parserState = context.state[key];
694
+ else parserState = void 0;
695
+ } else if (parser.initialState && typeof parser.initialState === "object") if (context.state && typeof context.state === "object") {
696
+ const extractedState = {};
697
+ for (const field in parser.initialState) extractedState[field] = field in context.state ? context.state[field] : parser.initialState[field];
698
+ parserState = extractedState;
699
+ } else parserState = parser.initialState;
700
+ else parserState = parser.initialState;
701
+ const result = parser.parse({
702
+ ...context,
703
+ state: parserState
704
+ });
705
+ if (result.success) {
706
+ let newState;
707
+ if (parser.initialState === void 0) {
708
+ const key = `__parser_${i}`;
709
+ if (result.consumed.length > 0 || result.next.state !== void 0) newState = {
710
+ ...context.state,
711
+ [key]: result.next.state
712
+ };
713
+ else newState = { ...context.state };
714
+ } else newState = {
715
+ ...context.state,
716
+ ...result.next.state
717
+ };
718
+ const newContext = {
719
+ ...context,
720
+ buffer: result.next.buffer,
721
+ optionsTerminated: result.next.optionsTerminated,
722
+ state: newState
723
+ };
724
+ if (result.consumed.length > 0) return {
725
+ success: true,
726
+ next: newContext,
727
+ consumed: result.consumed
728
+ };
729
+ context = newContext;
730
+ if (zeroConsumedSuccess === null) zeroConsumedSuccess = {
731
+ context: newContext,
732
+ consumed: []
733
+ };
734
+ else zeroConsumedSuccess.context = newContext;
735
+ } else if (result.consumed < 1) continue;
736
+ else return result;
737
+ }
738
+ if (zeroConsumedSuccess !== null) return {
739
+ success: true,
740
+ next: zeroConsumedSuccess.context,
741
+ consumed: zeroConsumedSuccess.consumed
742
+ };
743
+ return {
744
+ success: false,
745
+ consumed: 0,
746
+ error: require_message.message`No matching option or argument found.`
747
+ };
1152
748
  },
1153
749
  complete(state) {
1154
- const extractCompleteState = (parser, index) => {
750
+ const object$1 = {};
751
+ for (let i = 0; i < parsers.length; i++) {
752
+ const parser = parsers[i];
753
+ let parserState;
1155
754
  if (parser.initialState === void 0) {
1156
- const key = `__parser_${index}`;
1157
- if (state && typeof state === "object" && key in state) return state[key];
1158
- return void 0;
1159
- } else if (parser.initialState && typeof parser.initialState === "object") {
1160
- if (state && typeof state === "object") {
1161
- const extractedState = {};
1162
- for (const field in parser.initialState) extractedState[field] = field in state ? state[field] : parser.initialState[field];
1163
- return extractedState;
1164
- }
1165
- return parser.initialState;
1166
- }
1167
- return parser.initialState;
1168
- };
1169
- if (!isAsync) {
1170
- const object$1 = {};
1171
- for (let i = 0; i < syncParsers.length; i++) {
1172
- const parser = syncParsers[i];
1173
- const parserState = extractCompleteState(parser, i);
1174
- const result = parser.complete(parserState);
1175
- if (!result.success) return result;
1176
- for (const field in result.value) object$1[field] = result.value[field];
1177
- }
1178
- return {
1179
- success: true,
1180
- value: object$1
1181
- };
755
+ const key = `__parser_${i}`;
756
+ if (state && typeof state === "object" && key in state) parserState = state[key];
757
+ else parserState = void 0;
758
+ } else if (parser.initialState && typeof parser.initialState === "object") if (state && typeof state === "object") {
759
+ const extractedState = {};
760
+ for (const field in parser.initialState) extractedState[field] = field in state ? state[field] : parser.initialState[field];
761
+ parserState = extractedState;
762
+ } else parserState = parser.initialState;
763
+ else parserState = parser.initialState;
764
+ const result = parser.complete(parserState);
765
+ if (!result.success) return result;
766
+ for (const field in result.value) object$1[field] = result.value[field];
1182
767
  }
1183
- return (async () => {
1184
- const object$1 = {};
1185
- for (let i = 0; i < parsers.length; i++) {
1186
- const parser = parsers[i];
1187
- const parserState = extractCompleteState(parser, i);
1188
- const result = await parser.complete(parserState);
1189
- if (!result.success) return result;
1190
- for (const field in result.value) object$1[field] = result.value[field];
1191
- }
1192
- return {
1193
- success: true,
1194
- value: object$1
1195
- };
1196
- })();
768
+ return {
769
+ success: true,
770
+ value: object$1
771
+ };
1197
772
  },
1198
773
  suggest(context, prefix) {
1199
- const extractState = (p, i) => {
1200
- if (p.initialState === void 0) {
774
+ const suggestions = [];
775
+ for (let i = 0; i < parsers.length; i++) {
776
+ const parser = parsers[i];
777
+ let parserState;
778
+ if (parser.initialState === void 0) {
1201
779
  const key = `__parser_${i}`;
1202
- if (context.state && typeof context.state === "object" && key in context.state) return context.state[key];
1203
- return void 0;
1204
- } else if (p.initialState && typeof p.initialState === "object") {
1205
- if (context.state && typeof context.state === "object") {
1206
- const extractedState = {};
1207
- for (const field in p.initialState) extractedState[field] = field in context.state ? context.state[field] : p.initialState[field];
1208
- return extractedState;
1209
- }
1210
- return p.initialState;
1211
- }
1212
- return p.initialState;
1213
- };
1214
- if (isAsync) return async function* () {
1215
- const suggestions = [];
1216
- for (let i = 0; i < parsers.length; i++) {
1217
- const parser = parsers[i];
1218
- const parserState = extractState(parser, i);
1219
- const parserSuggestions = parser.suggest({
1220
- ...context,
1221
- state: parserState
1222
- }, prefix);
1223
- if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
1224
- else suggestions.push(...parserSuggestions);
1225
- }
1226
- yield* require_suggestion.deduplicateSuggestions(suggestions);
1227
- }();
1228
- return function* () {
1229
- const suggestions = [];
1230
- for (let i = 0; i < syncParsers.length; i++) {
1231
- const parser = syncParsers[i];
1232
- const parserState = extractState(parser, i);
1233
- const parserSuggestions = parser.suggest({
1234
- ...context,
1235
- state: parserState
1236
- }, prefix);
1237
- suggestions.push(...parserSuggestions);
1238
- }
1239
- yield* require_suggestion.deduplicateSuggestions(suggestions);
1240
- }();
780
+ if (context.state && typeof context.state === "object" && key in context.state) parserState = context.state[key];
781
+ else parserState = void 0;
782
+ } else if (parser.initialState && typeof parser.initialState === "object") if (context.state && typeof context.state === "object") {
783
+ const extractedState = {};
784
+ for (const field in parser.initialState) extractedState[field] = field in context.state ? context.state[field] : parser.initialState[field];
785
+ parserState = extractedState;
786
+ } else parserState = parser.initialState;
787
+ else parserState = parser.initialState;
788
+ const parserSuggestions = parser.suggest({
789
+ ...context,
790
+ state: parserState
791
+ }, prefix);
792
+ suggestions.push(...parserSuggestions);
793
+ }
794
+ return require_suggestion.deduplicateSuggestions(suggestions);
1241
795
  },
1242
796
  getDocFragments(state, _defaultValue) {
1243
797
  const fragments = parsers.flatMap((p, i) => {
@@ -1284,213 +838,102 @@ function merge(...args) {
1284
838
  };
1285
839
  }
1286
840
  function concat(...parsers) {
1287
- const combinedMode = parsers.some((p) => p.$mode === "async") ? "async" : "sync";
1288
- const isAsync = combinedMode === "async";
1289
- const syncParsers = parsers;
1290
841
  const initialState = parsers.map((parser) => parser.initialState);
1291
- const parseSync = (context) => {
1292
- let currentContext = context;
1293
- const allConsumed = [];
1294
- const matchedParsers = /* @__PURE__ */ new Set();
1295
- while (matchedParsers.size < syncParsers.length) {
1296
- let foundMatch = false;
1297
- let error = {
1298
- consumed: 0,
1299
- error: require_message.message`No remaining parsers could match the input.`
1300
- };
1301
- const stateArray = currentContext.state;
1302
- const remainingParsers = syncParsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1303
- for (const [parser, index] of remainingParsers) {
1304
- const result = parser.parse({
1305
- ...currentContext,
1306
- state: stateArray[index]
1307
- });
1308
- if (result.success && result.consumed.length > 0) {
1309
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1310
- currentContext = {
1311
- ...currentContext,
1312
- buffer: result.next.buffer,
1313
- optionsTerminated: result.next.optionsTerminated,
1314
- state: newStateArray
1315
- };
1316
- allConsumed.push(...result.consumed);
1317
- matchedParsers.add(index);
1318
- foundMatch = true;
1319
- break;
1320
- } else if (!result.success && error.consumed < result.consumed) error = result;
1321
- }
1322
- if (!foundMatch) for (const [parser, index] of remainingParsers) {
1323
- const result = parser.parse({
1324
- ...currentContext,
1325
- state: stateArray[index]
1326
- });
1327
- if (result.success && result.consumed.length < 1) {
1328
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1329
- currentContext = {
1330
- ...currentContext,
1331
- state: newStateArray
1332
- };
1333
- matchedParsers.add(index);
1334
- foundMatch = true;
1335
- break;
1336
- } else if (!result.success && result.consumed < 1) {
1337
- matchedParsers.add(index);
1338
- foundMatch = true;
1339
- break;
1340
- }
1341
- }
1342
- if (!foundMatch) return {
1343
- ...error,
1344
- success: false
1345
- };
1346
- }
1347
- return {
1348
- success: true,
1349
- next: currentContext,
1350
- consumed: allConsumed
1351
- };
1352
- };
1353
- const parseAsync = async (context) => {
1354
- let currentContext = context;
1355
- const allConsumed = [];
1356
- const matchedParsers = /* @__PURE__ */ new Set();
1357
- while (matchedParsers.size < parsers.length) {
1358
- let foundMatch = false;
1359
- let error = {
1360
- consumed: 0,
1361
- error: require_message.message`No remaining parsers could match the input.`
1362
- };
1363
- const stateArray = currentContext.state;
1364
- const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
1365
- for (const [parser, index] of remainingParsers) {
1366
- const result = await parser.parse({
1367
- ...currentContext,
1368
- state: stateArray[index]
1369
- });
1370
- if (result.success && result.consumed.length > 0) {
1371
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1372
- currentContext = {
1373
- ...currentContext,
1374
- buffer: result.next.buffer,
1375
- optionsTerminated: result.next.optionsTerminated,
1376
- state: newStateArray
1377
- };
1378
- allConsumed.push(...result.consumed);
1379
- matchedParsers.add(index);
1380
- foundMatch = true;
1381
- break;
1382
- } else if (!result.success && error.consumed < result.consumed) error = result;
1383
- }
1384
- if (!foundMatch) for (const [parser, index] of remainingParsers) {
1385
- const result = await parser.parse({
1386
- ...currentContext,
1387
- state: stateArray[index]
1388
- });
1389
- if (result.success && result.consumed.length < 1) {
1390
- const newStateArray = stateArray.map((s, idx) => idx === index ? result.next.state : s);
1391
- currentContext = {
1392
- ...currentContext,
1393
- state: newStateArray
1394
- };
1395
- matchedParsers.add(index);
1396
- foundMatch = true;
1397
- break;
1398
- } else if (!result.success && result.consumed < 1) {
1399
- matchedParsers.add(index);
1400
- foundMatch = true;
1401
- break;
1402
- }
1403
- }
1404
- if (!foundMatch) return {
1405
- ...error,
1406
- success: false
1407
- };
1408
- }
1409
- return {
1410
- success: true,
1411
- next: currentContext,
1412
- consumed: allConsumed
1413
- };
1414
- };
1415
- const completeSync = (state) => {
1416
- const results = [];
1417
- const stateArray = state;
1418
- for (let i = 0; i < syncParsers.length; i++) {
1419
- const parser = syncParsers[i];
1420
- const parserState = stateArray[i];
1421
- const result = parser.complete(parserState);
1422
- if (!result.success) return result;
1423
- if (Array.isArray(result.value)) results.push(...result.value);
1424
- else results.push(result.value);
1425
- }
1426
- return {
1427
- success: true,
1428
- value: results
1429
- };
1430
- };
1431
- const completeAsync = async (state) => {
1432
- const results = [];
1433
- const stateArray = state;
1434
- for (let i = 0; i < parsers.length; i++) {
1435
- const parser = parsers[i];
1436
- const parserState = stateArray[i];
1437
- const result = await parser.complete(parserState);
1438
- if (!result.success) return result;
1439
- if (Array.isArray(result.value)) results.push(...result.value);
1440
- else results.push(result.value);
1441
- }
1442
- return {
1443
- success: true,
1444
- value: results
1445
- };
1446
- };
1447
842
  return {
1448
- $mode: combinedMode,
1449
843
  $valueType: [],
1450
844
  $stateType: [],
1451
845
  priority: parsers.length > 0 ? Math.max(...parsers.map((p) => p.priority)) : 0,
1452
846
  usage: parsers.flatMap((p) => p.usage),
1453
847
  initialState,
1454
848
  parse(context) {
1455
- if (isAsync) return parseAsync(context);
1456
- return parseSync(context);
849
+ let currentContext = context;
850
+ const allConsumed = [];
851
+ const matchedParsers = /* @__PURE__ */ new Set();
852
+ while (matchedParsers.size < parsers.length) {
853
+ let foundMatch = false;
854
+ let error = {
855
+ consumed: 0,
856
+ error: require_message.message`No remaining parsers could match the input.`
857
+ };
858
+ const remainingParsers = parsers.map((parser, index) => [parser, index]).filter(([_, index]) => !matchedParsers.has(index)).sort(([parserA], [parserB]) => parserB.priority - parserA.priority);
859
+ for (const [parser, index] of remainingParsers) {
860
+ const result = parser.parse({
861
+ ...currentContext,
862
+ state: currentContext.state[index]
863
+ });
864
+ if (result.success && result.consumed.length > 0) {
865
+ currentContext = {
866
+ ...currentContext,
867
+ buffer: result.next.buffer,
868
+ optionsTerminated: result.next.optionsTerminated,
869
+ state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
870
+ };
871
+ allConsumed.push(...result.consumed);
872
+ matchedParsers.add(index);
873
+ foundMatch = true;
874
+ break;
875
+ } else if (!result.success && error.consumed < result.consumed) error = result;
876
+ }
877
+ if (!foundMatch) for (const [parser, index] of remainingParsers) {
878
+ const result = parser.parse({
879
+ ...currentContext,
880
+ state: currentContext.state[index]
881
+ });
882
+ if (result.success && result.consumed.length < 1) {
883
+ currentContext = {
884
+ ...currentContext,
885
+ state: currentContext.state.map((s, idx) => idx === index ? result.next.state : s)
886
+ };
887
+ matchedParsers.add(index);
888
+ foundMatch = true;
889
+ break;
890
+ } else if (!result.success && result.consumed < 1) {
891
+ matchedParsers.add(index);
892
+ foundMatch = true;
893
+ break;
894
+ }
895
+ }
896
+ if (!foundMatch) return {
897
+ ...error,
898
+ success: false
899
+ };
900
+ }
901
+ return {
902
+ success: true,
903
+ next: currentContext,
904
+ consumed: allConsumed
905
+ };
1457
906
  },
1458
907
  complete(state) {
1459
- if (isAsync) return completeAsync(state);
1460
- return completeSync(state);
908
+ const results = [];
909
+ for (let i = 0; i < parsers.length; i++) {
910
+ const parser = parsers[i];
911
+ const parserState = state[i];
912
+ const result = parser.complete(parserState);
913
+ if (!result.success) return result;
914
+ if (Array.isArray(result.value)) results.push(...result.value);
915
+ else results.push(result.value);
916
+ }
917
+ return {
918
+ success: true,
919
+ value: results
920
+ };
1461
921
  },
1462
922
  suggest(context, prefix) {
1463
- const stateArray = context.state;
1464
- if (isAsync) return async function* () {
1465
- const suggestions = [];
1466
- for (let i = 0; i < parsers.length; i++) {
1467
- const parser = parsers[i];
1468
- const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
1469
- const parserSuggestions = parser.suggest({
1470
- ...context,
1471
- state: parserState
1472
- }, prefix);
1473
- if (parser.$mode === "async") for await (const s of parserSuggestions) suggestions.push(s);
1474
- else suggestions.push(...parserSuggestions);
1475
- }
1476
- yield* require_suggestion.deduplicateSuggestions(suggestions);
1477
- }();
1478
- return function* () {
1479
- const suggestions = [];
1480
- for (let i = 0; i < syncParsers.length; i++) {
1481
- const parser = syncParsers[i];
1482
- const parserState = stateArray && Array.isArray(stateArray) ? stateArray[i] : parser.initialState;
1483
- const parserSuggestions = parser.suggest({
1484
- ...context,
1485
- state: parserState
1486
- }, prefix);
1487
- suggestions.push(...parserSuggestions);
1488
- }
1489
- yield* require_suggestion.deduplicateSuggestions(suggestions);
1490
- }();
923
+ const suggestions = [];
924
+ for (let i = 0; i < parsers.length; i++) {
925
+ const parser = parsers[i];
926
+ const parserState = context.state && Array.isArray(context.state) ? context.state[i] : parser.initialState;
927
+ const parserSuggestions = parser.suggest({
928
+ ...context,
929
+ state: parserState
930
+ }, prefix);
931
+ suggestions.push(...parserSuggestions);
932
+ }
933
+ return require_suggestion.deduplicateSuggestions(suggestions);
1491
934
  },
1492
935
  getDocFragments(state, _defaultValue) {
1493
- const fragments = syncParsers.flatMap((p, index) => {
936
+ const fragments = parsers.flatMap((p, index) => {
1494
937
  const indexState = state.kind === "unavailable" ? { kind: "unavailable" } : {
1495
938
  kind: "available",
1496
939
  state: state.state[index]
@@ -1560,7 +1003,6 @@ function concat(...parsers) {
1560
1003
  */
1561
1004
  function group(label, parser) {
1562
1005
  return {
1563
- $mode: parser.$mode,
1564
1006
  $valueType: parser.$valueType,
1565
1007
  $stateType: parser.$stateType,
1566
1008
  priority: parser.priority,
@@ -1626,8 +1068,6 @@ function group(label, parser) {
1626
1068
  function conditional(discriminator, branches, defaultBranch, options) {
1627
1069
  const branchParsers = Object.entries(branches);
1628
1070
  const allBranchParsers = defaultBranch ? [...branchParsers.map(([_, p]) => p), defaultBranch] : branchParsers.map(([_, p]) => p);
1629
- const combinedMode = discriminator.$mode === "async" || allBranchParsers.some((p) => p.$mode === "async") ? "async" : "sync";
1630
- const isAsync = combinedMode === "async";
1631
1071
  const maxPriority = Math.max(discriminator.priority, ...allBranchParsers.map((p) => p.priority));
1632
1072
  function appendLiteralToUsage(usage$1, literalValue) {
1633
1073
  const result = [];
@@ -1665,334 +1105,164 @@ function conditional(discriminator, branches, defaultBranch, options) {
1665
1105
  selectedBranch: void 0,
1666
1106
  branchState: void 0
1667
1107
  };
1668
- const getNoMatchError$1 = () => {
1669
- const noMatchContext = analyzeNoMatchContext([discriminator, ...allBranchParsers]);
1670
- return options?.errors?.noMatch ? typeof options.errors.noMatch === "function" ? options.errors.noMatch(noMatchContext) : options.errors.noMatch : generateNoMatchError(noMatchContext);
1671
- };
1672
- const parseSync = (context) => {
1673
- const state = context.state ?? initialState;
1674
- const syncDiscriminator = discriminator;
1675
- const syncBranches = branches;
1676
- const syncDefaultBranch = defaultBranch;
1677
- if (state.selectedBranch !== void 0) {
1678
- const branchParser = state.selectedBranch.kind === "default" ? syncDefaultBranch : syncBranches[state.selectedBranch.key];
1679
- const branchResult = branchParser.parse({
1680
- ...context,
1681
- state: state.branchState,
1682
- usage: branchParser.usage
1683
- });
1684
- if (branchResult.success) return {
1685
- success: true,
1686
- next: {
1687
- ...branchResult.next,
1688
- state: {
1689
- ...state,
1690
- branchState: branchResult.next.state
1691
- }
1692
- },
1693
- consumed: branchResult.consumed
1694
- };
1695
- return branchResult;
1696
- }
1697
- const discriminatorResult = syncDiscriminator.parse({
1698
- ...context,
1699
- state: state.discriminatorState
1700
- });
1701
- if (discriminatorResult.success && discriminatorResult.consumed.length > 0) {
1702
- const completionResult = syncDiscriminator.complete(discriminatorResult.next.state);
1703
- if (completionResult.success) {
1704
- const value = completionResult.value;
1705
- const branchParser = syncBranches[value];
1706
- if (branchParser) {
1707
- const branchParseResult = branchParser.parse({
1708
- ...context,
1709
- buffer: discriminatorResult.next.buffer,
1710
- optionsTerminated: discriminatorResult.next.optionsTerminated,
1711
- state: branchParser.initialState,
1712
- usage: branchParser.usage
1713
- });
1714
- if (branchParseResult.success) return {
1715
- success: true,
1716
- next: {
1717
- ...branchParseResult.next,
1718
- state: {
1719
- discriminatorState: discriminatorResult.next.state,
1720
- discriminatorValue: value,
1721
- selectedBranch: {
1722
- kind: "branch",
1723
- key: value
1724
- },
1725
- branchState: branchParseResult.next.state
1726
- }
1727
- },
1728
- consumed: [...discriminatorResult.consumed, ...branchParseResult.consumed]
1729
- };
1730
- return {
1731
- success: true,
1732
- next: {
1733
- ...discriminatorResult.next,
1734
- state: {
1735
- discriminatorState: discriminatorResult.next.state,
1736
- discriminatorValue: value,
1737
- selectedBranch: {
1738
- kind: "branch",
1739
- key: value
1740
- },
1741
- branchState: branchParser.initialState
1742
- }
1743
- },
1744
- consumed: discriminatorResult.consumed
1745
- };
1746
- }
1108
+ return {
1109
+ $valueType: [],
1110
+ $stateType: [],
1111
+ priority: maxPriority,
1112
+ usage,
1113
+ initialState,
1114
+ parse(context) {
1115
+ const state = context.state ?? initialState;
1116
+ if (state.selectedBranch !== void 0) {
1117
+ const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1118
+ const branchResult = branchParser.parse({
1119
+ ...context,
1120
+ state: state.branchState,
1121
+ usage: branchParser.usage
1122
+ });
1123
+ if (branchResult.success) return {
1124
+ success: true,
1125
+ next: {
1126
+ ...branchResult.next,
1127
+ state: {
1128
+ ...state,
1129
+ branchState: branchResult.next.state
1130
+ }
1131
+ },
1132
+ consumed: branchResult.consumed
1133
+ };
1134
+ return branchResult;
1747
1135
  }
1748
- }
1749
- if (syncDefaultBranch !== void 0) {
1750
- const defaultResult = syncDefaultBranch.parse({
1751
- ...context,
1752
- state: state.branchState ?? syncDefaultBranch.initialState,
1753
- usage: syncDefaultBranch.usage
1754
- });
1755
- if (defaultResult.success && defaultResult.consumed.length > 0) return {
1756
- success: true,
1757
- next: {
1758
- ...defaultResult.next,
1759
- state: {
1760
- ...state,
1761
- selectedBranch: { kind: "default" },
1762
- branchState: defaultResult.next.state
1763
- }
1764
- },
1765
- consumed: defaultResult.consumed
1766
- };
1767
- }
1768
- return {
1769
- success: false,
1770
- consumed: 0,
1771
- error: getNoMatchError$1()
1772
- };
1773
- };
1774
- const parseAsync = async (context) => {
1775
- const state = context.state ?? initialState;
1776
- if (state.selectedBranch !== void 0) {
1777
- const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1778
- const branchResult = await branchParser.parse({
1136
+ const discriminatorResult = discriminator.parse({
1779
1137
  ...context,
1780
- state: state.branchState,
1781
- usage: branchParser.usage
1138
+ state: state.discriminatorState
1782
1139
  });
1783
- if (branchResult.success) return {
1784
- success: true,
1785
- next: {
1786
- ...branchResult.next,
1787
- state: {
1788
- ...state,
1789
- branchState: branchResult.next.state
1140
+ if (discriminatorResult.success && discriminatorResult.consumed.length > 0) {
1141
+ const completionResult = discriminator.complete(discriminatorResult.next.state);
1142
+ if (completionResult.success) {
1143
+ const value = completionResult.value;
1144
+ const branchParser = branches[value];
1145
+ if (branchParser) {
1146
+ const branchParseResult = branchParser.parse({
1147
+ ...context,
1148
+ buffer: discriminatorResult.next.buffer,
1149
+ optionsTerminated: discriminatorResult.next.optionsTerminated,
1150
+ state: branchParser.initialState,
1151
+ usage: branchParser.usage
1152
+ });
1153
+ if (branchParseResult.success) return {
1154
+ success: true,
1155
+ next: {
1156
+ ...branchParseResult.next,
1157
+ state: {
1158
+ discriminatorState: discriminatorResult.next.state,
1159
+ discriminatorValue: value,
1160
+ selectedBranch: {
1161
+ kind: "branch",
1162
+ key: value
1163
+ },
1164
+ branchState: branchParseResult.next.state
1165
+ }
1166
+ },
1167
+ consumed: [...discriminatorResult.consumed, ...branchParseResult.consumed]
1168
+ };
1169
+ return {
1170
+ success: true,
1171
+ next: {
1172
+ ...discriminatorResult.next,
1173
+ state: {
1174
+ discriminatorState: discriminatorResult.next.state,
1175
+ discriminatorValue: value,
1176
+ selectedBranch: {
1177
+ kind: "branch",
1178
+ key: value
1179
+ },
1180
+ branchState: branchParser.initialState
1181
+ }
1182
+ },
1183
+ consumed: discriminatorResult.consumed
1184
+ };
1790
1185
  }
1791
- },
1792
- consumed: branchResult.consumed
1793
- };
1794
- return branchResult;
1795
- }
1796
- const discriminatorResult = await discriminator.parse({
1797
- ...context,
1798
- state: state.discriminatorState
1799
- });
1800
- if (discriminatorResult.success && discriminatorResult.consumed.length > 0) {
1801
- const completionResult = await discriminator.complete(discriminatorResult.next.state);
1802
- if (completionResult.success) {
1803
- const value = completionResult.value;
1804
- const branchParser = branches[value];
1805
- if (branchParser) {
1806
- const branchParseResult = await branchParser.parse({
1807
- ...context,
1808
- buffer: discriminatorResult.next.buffer,
1809
- optionsTerminated: discriminatorResult.next.optionsTerminated,
1810
- state: branchParser.initialState,
1811
- usage: branchParser.usage
1812
- });
1813
- if (branchParseResult.success) return {
1814
- success: true,
1815
- next: {
1816
- ...branchParseResult.next,
1817
- state: {
1818
- discriminatorState: discriminatorResult.next.state,
1819
- discriminatorValue: value,
1820
- selectedBranch: {
1821
- kind: "branch",
1822
- key: value
1823
- },
1824
- branchState: branchParseResult.next.state
1825
- }
1826
- },
1827
- consumed: [...discriminatorResult.consumed, ...branchParseResult.consumed]
1828
- };
1829
- return {
1830
- success: true,
1831
- next: {
1832
- ...discriminatorResult.next,
1833
- state: {
1834
- discriminatorState: discriminatorResult.next.state,
1835
- discriminatorValue: value,
1836
- selectedBranch: {
1837
- kind: "branch",
1838
- key: value
1839
- },
1840
- branchState: branchParser.initialState
1841
- }
1842
- },
1843
- consumed: discriminatorResult.consumed
1844
- };
1845
1186
  }
1846
1187
  }
1847
- }
1848
- if (defaultBranch !== void 0) {
1849
- const defaultResult = await defaultBranch.parse({
1850
- ...context,
1851
- state: state.branchState ?? defaultBranch.initialState,
1852
- usage: defaultBranch.usage
1853
- });
1854
- if (defaultResult.success && defaultResult.consumed.length > 0) return {
1855
- success: true,
1856
- next: {
1857
- ...defaultResult.next,
1858
- state: {
1859
- ...state,
1860
- selectedBranch: { kind: "default" },
1861
- branchState: defaultResult.next.state
1862
- }
1863
- },
1864
- consumed: defaultResult.consumed
1865
- };
1866
- }
1867
- return {
1868
- success: false,
1869
- consumed: 0,
1870
- error: getNoMatchError$1()
1871
- };
1872
- };
1873
- const completeSync = (state) => {
1874
- const syncDefaultBranch = defaultBranch;
1875
- const syncBranches = branches;
1876
- if (state.selectedBranch === void 0) {
1877
- if (syncDefaultBranch !== void 0) {
1878
- const branchState = state.branchState ?? syncDefaultBranch.initialState;
1879
- const defaultResult = syncDefaultBranch.complete(branchState);
1880
- if (!defaultResult.success) return defaultResult;
1881
- return {
1188
+ if (defaultBranch !== void 0) {
1189
+ const defaultResult = defaultBranch.parse({
1190
+ ...context,
1191
+ state: state.branchState ?? defaultBranch.initialState,
1192
+ usage: defaultBranch.usage
1193
+ });
1194
+ if (defaultResult.success && defaultResult.consumed.length > 0) return {
1882
1195
  success: true,
1883
- value: [void 0, defaultResult.value]
1196
+ next: {
1197
+ ...defaultResult.next,
1198
+ state: {
1199
+ ...state,
1200
+ selectedBranch: { kind: "default" },
1201
+ branchState: defaultResult.next.state
1202
+ }
1203
+ },
1204
+ consumed: defaultResult.consumed
1884
1205
  };
1885
1206
  }
1207
+ const noMatchContext = analyzeNoMatchContext([discriminator, ...allBranchParsers]);
1208
+ const errorMessage = options?.errors?.noMatch ? typeof options.errors.noMatch === "function" ? options.errors.noMatch(noMatchContext) : options.errors.noMatch : generateNoMatchError(noMatchContext);
1886
1209
  return {
1887
1210
  success: false,
1888
- error: require_message.message`Missing required discriminator option.`
1889
- };
1890
- }
1891
- const branchParser = state.selectedBranch.kind === "default" ? syncDefaultBranch : syncBranches[state.selectedBranch.key];
1892
- const branchResult = branchParser.complete(state.branchState);
1893
- if (!branchResult.success) {
1894
- if (state.discriminatorValue !== void 0 && options?.errors?.branchError) return {
1895
- success: false,
1896
- error: options.errors.branchError(state.discriminatorValue, branchResult.error)
1211
+ consumed: 0,
1212
+ error: errorMessage
1897
1213
  };
1898
- return branchResult;
1899
- }
1900
- const discriminatorValue = state.selectedBranch.kind === "default" ? void 0 : state.selectedBranch.key;
1901
- return {
1902
- success: true,
1903
- value: [discriminatorValue, branchResult.value]
1904
- };
1905
- };
1906
- const completeAsync = async (state) => {
1907
- if (state.selectedBranch === void 0) {
1908
- if (defaultBranch !== void 0) {
1909
- const branchState = state.branchState ?? defaultBranch.initialState;
1910
- const defaultResult = await defaultBranch.complete(branchState);
1911
- if (!defaultResult.success) return defaultResult;
1214
+ },
1215
+ complete(state) {
1216
+ if (state.selectedBranch === void 0) {
1217
+ if (defaultBranch !== void 0) {
1218
+ const branchState = state.branchState ?? defaultBranch.initialState;
1219
+ const defaultResult = defaultBranch.complete(branchState);
1220
+ if (!defaultResult.success) return defaultResult;
1221
+ return {
1222
+ success: true,
1223
+ value: [void 0, defaultResult.value]
1224
+ };
1225
+ }
1912
1226
  return {
1913
- success: true,
1914
- value: [void 0, defaultResult.value]
1227
+ success: false,
1228
+ error: require_message.message`Missing required discriminator option.`
1229
+ };
1230
+ }
1231
+ const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1232
+ const branchResult = branchParser.complete(state.branchState);
1233
+ if (!branchResult.success) {
1234
+ if (state.discriminatorValue !== void 0 && options?.errors?.branchError) return {
1235
+ success: false,
1236
+ error: options.errors.branchError(state.discriminatorValue, branchResult.error)
1915
1237
  };
1238
+ return branchResult;
1916
1239
  }
1240
+ const discriminatorValue = state.selectedBranch.kind === "default" ? void 0 : state.selectedBranch.key;
1917
1241
  return {
1918
- success: false,
1919
- error: require_message.message`Missing required discriminator option.`
1920
- };
1921
- }
1922
- const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1923
- const branchResult = await branchParser.complete(state.branchState);
1924
- if (!branchResult.success) {
1925
- if (state.discriminatorValue !== void 0 && options?.errors?.branchError) return {
1926
- success: false,
1927
- error: options.errors.branchError(state.discriminatorValue, branchResult.error)
1242
+ success: true,
1243
+ value: [discriminatorValue, branchResult.value]
1928
1244
  };
1929
- return branchResult;
1930
- }
1931
- const discriminatorValue = state.selectedBranch.kind === "default" ? void 0 : state.selectedBranch.key;
1932
- return {
1933
- success: true,
1934
- value: [discriminatorValue, branchResult.value]
1935
- };
1936
- };
1937
- function* suggestSync(context, prefix) {
1938
- const state = context.state ?? initialState;
1939
- const syncDiscriminator = discriminator;
1940
- const syncBranches = branches;
1941
- const syncDefaultBranch = defaultBranch;
1942
- if (state.selectedBranch === void 0) {
1943
- yield* syncDiscriminator.suggest({
1944
- ...context,
1945
- state: state.discriminatorState
1946
- }, prefix);
1947
- if (syncDefaultBranch !== void 0) yield* syncDefaultBranch.suggest({
1948
- ...context,
1949
- state: state.branchState ?? syncDefaultBranch.initialState
1950
- }, prefix);
1951
- } else {
1952
- const branchParser = state.selectedBranch.kind === "default" ? syncDefaultBranch : syncBranches[state.selectedBranch.key];
1953
- yield* branchParser.suggest({
1954
- ...context,
1955
- state: state.branchState
1956
- }, prefix);
1957
- }
1958
- }
1959
- async function* suggestAsync(context, prefix) {
1960
- const state = context.state ?? initialState;
1961
- if (state.selectedBranch === void 0) {
1962
- yield* discriminator.suggest({
1963
- ...context,
1964
- state: state.discriminatorState
1965
- }, prefix);
1966
- if (defaultBranch !== void 0) yield* defaultBranch.suggest({
1967
- ...context,
1968
- state: state.branchState ?? defaultBranch.initialState
1969
- }, prefix);
1970
- } else {
1971
- const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1972
- yield* branchParser.suggest({
1973
- ...context,
1974
- state: state.branchState
1975
- }, prefix);
1976
- }
1977
- }
1978
- return {
1979
- $mode: combinedMode,
1980
- $valueType: [],
1981
- $stateType: [],
1982
- priority: maxPriority,
1983
- usage,
1984
- initialState,
1985
- parse(context) {
1986
- if (isAsync) return parseAsync(context);
1987
- return parseSync(context);
1988
- },
1989
- complete(state) {
1990
- if (isAsync) return completeAsync(state);
1991
- return completeSync(state);
1992
1245
  },
1993
1246
  suggest(context, prefix) {
1994
- if (isAsync) return suggestAsync(context, prefix);
1995
- return suggestSync(context, prefix);
1247
+ const state = context.state ?? initialState;
1248
+ const suggestions = [];
1249
+ if (state.selectedBranch === void 0) {
1250
+ suggestions.push(...discriminator.suggest({
1251
+ ...context,
1252
+ state: state.discriminatorState
1253
+ }, prefix));
1254
+ if (defaultBranch !== void 0) suggestions.push(...defaultBranch.suggest({
1255
+ ...context,
1256
+ state: state.branchState ?? defaultBranch.initialState
1257
+ }, prefix));
1258
+ } else {
1259
+ const branchParser = state.selectedBranch.kind === "default" ? defaultBranch : branches[state.selectedBranch.key];
1260
+ suggestions.push(...branchParser.suggest({
1261
+ ...context,
1262
+ state: state.branchState
1263
+ }, prefix));
1264
+ }
1265
+ return require_suggestion.deduplicateSuggestions(suggestions);
1996
1266
  },
1997
1267
  getDocFragments(_state, _defaultValue) {
1998
1268
  const fragments = [];