@optique/core 0.8.4 → 0.8.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/dist/constructs.cjs +35 -9
- package/dist/constructs.d.cts +2 -2
- package/dist/constructs.d.ts +2 -2
- package/dist/constructs.js +35 -9
- package/dist/parser.cjs +25 -8
- package/dist/parser.js +25 -8
- package/package.json +1 -1
package/LICENSE
CHANGED
package/dist/constructs.cjs
CHANGED
|
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
|
|
|
73
73
|
*/
|
|
74
74
|
var DuplicateOptionError = class extends Error {
|
|
75
75
|
constructor(optionName$1, sources) {
|
|
76
|
-
|
|
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";
|
|
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
367
368
|
parsers = labelOrParsers;
|
|
368
369
|
options = maybeParsersOrOptions ?? {};
|
|
369
370
|
}
|
|
370
|
-
const
|
|
371
|
+
const parserKeys = Reflect.ownKeys(parsers);
|
|
372
|
+
const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
|
|
371
373
|
parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
|
|
374
|
+
const initialState = {};
|
|
375
|
+
for (const key of parserKeys) initialState[key] = parsers[key].initialState;
|
|
372
376
|
if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
|
|
373
|
-
const noMatchContext = analyzeNoMatchContext(
|
|
377
|
+
const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
|
|
374
378
|
return {
|
|
375
379
|
$valueType: [],
|
|
376
380
|
$stateType: [],
|
|
377
|
-
priority: Math.max(...
|
|
381
|
+
priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
|
|
378
382
|
usage: parserPairs.flatMap(([_, p]) => p.usage),
|
|
379
|
-
initialState
|
|
383
|
+
initialState,
|
|
380
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
|
+
}
|
|
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
|
+
};
|
|
399
|
+
}
|
|
381
400
|
let error = {
|
|
382
401
|
consumed: 0,
|
|
383
402
|
error: context.buffer.length > 0 ? (() => {
|
|
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
447
466
|
},
|
|
448
467
|
complete(state) {
|
|
449
468
|
const result = {};
|
|
450
|
-
for (const field
|
|
451
|
-
if (!(field in parsers)) continue;
|
|
469
|
+
for (const field of parserKeys) {
|
|
452
470
|
const valueResult = parsers[field].complete(state[field]);
|
|
453
471
|
if (valueResult.success) result[field] = valueResult.value;
|
|
454
472
|
else return {
|
|
@@ -760,8 +778,16 @@ function merge(...args) {
|
|
|
760
778
|
return require_suggestion.deduplicateSuggestions(suggestions);
|
|
761
779
|
},
|
|
762
780
|
getDocFragments(state, _defaultValue) {
|
|
763
|
-
const fragments = parsers.flatMap((p) => {
|
|
764
|
-
|
|
781
|
+
const fragments = parsers.flatMap((p, i) => {
|
|
782
|
+
let parserState;
|
|
783
|
+
if (p.initialState === void 0) {
|
|
784
|
+
const key = `__parser_${i}`;
|
|
785
|
+
if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
|
|
786
|
+
kind: "available",
|
|
787
|
+
state: state.state[key]
|
|
788
|
+
};
|
|
789
|
+
else parserState = { kind: "unavailable" };
|
|
790
|
+
} else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
|
|
765
791
|
kind: "available",
|
|
766
792
|
state: state.state
|
|
767
793
|
};
|
package/dist/constructs.d.cts
CHANGED
|
@@ -83,8 +83,8 @@ interface OrErrorOptions {
|
|
|
83
83
|
*/
|
|
84
84
|
declare class DuplicateOptionError extends Error {
|
|
85
85
|
readonly optionName: string;
|
|
86
|
-
readonly sources: string[];
|
|
87
|
-
constructor(optionName: string, sources: string[]);
|
|
86
|
+
readonly sources: readonly (string | symbol)[];
|
|
87
|
+
constructor(optionName: string, sources: readonly (string | symbol)[]);
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Creates a parser that combines two mutually exclusive parsers into one.
|
package/dist/constructs.d.ts
CHANGED
|
@@ -83,8 +83,8 @@ interface OrErrorOptions {
|
|
|
83
83
|
*/
|
|
84
84
|
declare class DuplicateOptionError extends Error {
|
|
85
85
|
readonly optionName: string;
|
|
86
|
-
readonly sources: string[];
|
|
87
|
-
constructor(optionName: string, sources: string[]);
|
|
86
|
+
readonly sources: readonly (string | symbol)[];
|
|
87
|
+
constructor(optionName: string, sources: readonly (string | symbol)[]);
|
|
88
88
|
}
|
|
89
89
|
/**
|
|
90
90
|
* Creates a parser that combines two mutually exclusive parsers into one.
|
package/dist/constructs.js
CHANGED
|
@@ -73,7 +73,8 @@ function analyzeNoMatchContext(parsers) {
|
|
|
73
73
|
*/
|
|
74
74
|
var DuplicateOptionError = class extends Error {
|
|
75
75
|
constructor(optionName$1, sources) {
|
|
76
|
-
|
|
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";
|
|
@@ -367,17 +368,35 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
367
368
|
parsers = labelOrParsers;
|
|
368
369
|
options = maybeParsersOrOptions ?? {};
|
|
369
370
|
}
|
|
370
|
-
const
|
|
371
|
+
const parserKeys = Reflect.ownKeys(parsers);
|
|
372
|
+
const parserPairs = parserKeys.map((k) => [k, parsers[k]]);
|
|
371
373
|
parserPairs.sort(([_, parserA], [__, parserB]) => parserB.priority - parserA.priority);
|
|
374
|
+
const initialState = {};
|
|
375
|
+
for (const key of parserKeys) initialState[key] = parsers[key].initialState;
|
|
372
376
|
if (!options.allowDuplicates) checkDuplicateOptionNames(parserPairs.map(([field, parser]) => [field, parser.usage]));
|
|
373
|
-
const noMatchContext = analyzeNoMatchContext(
|
|
377
|
+
const noMatchContext = analyzeNoMatchContext(parserKeys.map((k) => parsers[k]));
|
|
374
378
|
return {
|
|
375
379
|
$valueType: [],
|
|
376
380
|
$stateType: [],
|
|
377
|
-
priority: Math.max(...
|
|
381
|
+
priority: Math.max(...parserKeys.map((k) => parsers[k].priority)),
|
|
378
382
|
usage: parserPairs.flatMap(([_, p]) => p.usage),
|
|
379
|
-
initialState
|
|
383
|
+
initialState,
|
|
380
384
|
parse(context) {
|
|
385
|
+
if (!options.allowDuplicates) {
|
|
386
|
+
const optionNameSources = /* @__PURE__ */ new Map();
|
|
387
|
+
for (const [field, parser] of parserPairs) {
|
|
388
|
+
const names = extractOptionNames(parser.usage);
|
|
389
|
+
for (const name of names) {
|
|
390
|
+
if (!optionNameSources.has(name)) optionNameSources.set(name, []);
|
|
391
|
+
optionNameSources.get(name).push(field);
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
for (const [name, sources] of optionNameSources) if (sources.length > 1) return {
|
|
395
|
+
success: false,
|
|
396
|
+
consumed: 0,
|
|
397
|
+
error: message`Duplicate option name ${optionName(name)} found in fields: ${values(sources.map((s) => typeof s === "symbol" ? s.description ?? s.toString() : s))}. Each option name must be unique within a parser combinator.`
|
|
398
|
+
};
|
|
399
|
+
}
|
|
381
400
|
let error = {
|
|
382
401
|
consumed: 0,
|
|
383
402
|
error: context.buffer.length > 0 ? (() => {
|
|
@@ -447,8 +466,7 @@ function object(labelOrParsers, maybeParsersOrOptions, maybeOptions) {
|
|
|
447
466
|
},
|
|
448
467
|
complete(state) {
|
|
449
468
|
const result = {};
|
|
450
|
-
for (const field
|
|
451
|
-
if (!(field in parsers)) continue;
|
|
469
|
+
for (const field of parserKeys) {
|
|
452
470
|
const valueResult = parsers[field].complete(state[field]);
|
|
453
471
|
if (valueResult.success) result[field] = valueResult.value;
|
|
454
472
|
else return {
|
|
@@ -760,8 +778,16 @@ function merge(...args) {
|
|
|
760
778
|
return deduplicateSuggestions(suggestions);
|
|
761
779
|
},
|
|
762
780
|
getDocFragments(state, _defaultValue) {
|
|
763
|
-
const fragments = parsers.flatMap((p) => {
|
|
764
|
-
|
|
781
|
+
const fragments = parsers.flatMap((p, i) => {
|
|
782
|
+
let parserState;
|
|
783
|
+
if (p.initialState === void 0) {
|
|
784
|
+
const key = `__parser_${i}`;
|
|
785
|
+
if (state.kind === "available" && state.state && typeof state.state === "object" && key in state.state) parserState = {
|
|
786
|
+
kind: "available",
|
|
787
|
+
state: state.state[key]
|
|
788
|
+
};
|
|
789
|
+
else parserState = { kind: "unavailable" };
|
|
790
|
+
} else parserState = state.kind === "unavailable" ? { kind: "unavailable" } : {
|
|
765
791
|
kind: "available",
|
|
766
792
|
state: state.state
|
|
767
793
|
};
|
package/dist/parser.cjs
CHANGED
|
@@ -95,6 +95,26 @@ function suggest(parser, args) {
|
|
|
95
95
|
return Array.from(parser.suggest(context, prefix));
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
|
+
* Recursively searches for a command within nested exclusive usage terms.
|
|
99
|
+
* When the command is found, returns the expanded usage terms for that command.
|
|
100
|
+
*
|
|
101
|
+
* @param term The usage term to search in
|
|
102
|
+
* @param commandName The command name to find
|
|
103
|
+
* @returns The expanded usage terms if found, null otherwise
|
|
104
|
+
*/
|
|
105
|
+
function findCommandInExclusive(term, commandName) {
|
|
106
|
+
if (term.type !== "exclusive") return null;
|
|
107
|
+
for (const termGroup of term.terms) {
|
|
108
|
+
const firstTerm = termGroup[0];
|
|
109
|
+
if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
|
|
110
|
+
if (firstTerm?.type === "exclusive") {
|
|
111
|
+
const found = findCommandInExclusive(firstTerm, commandName);
|
|
112
|
+
if (found) return [...found, ...termGroup.slice(1)];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
98
118
|
* Generates a documentation page for a parser based on its current state after
|
|
99
119
|
* attempting to parse the provided arguments. This function is useful for
|
|
100
120
|
* creating help documentation that reflects the current parsing context.
|
|
@@ -157,14 +177,11 @@ function getDocPage(parser, args = []) {
|
|
|
157
177
|
if (i >= usage.length) break;
|
|
158
178
|
const term = usage[i];
|
|
159
179
|
if (term.type === "exclusive") {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
if (usage[i] === term) i++;
|
|
180
|
+
const found = findCommandInExclusive(term, arg);
|
|
181
|
+
if (found) {
|
|
182
|
+
usage.splice(i, 1, ...found);
|
|
183
|
+
i += found.length;
|
|
184
|
+
} else i++;
|
|
168
185
|
} else i++;
|
|
169
186
|
}
|
|
170
187
|
return {
|
package/dist/parser.js
CHANGED
|
@@ -95,6 +95,26 @@ function suggest(parser, args) {
|
|
|
95
95
|
return Array.from(parser.suggest(context, prefix));
|
|
96
96
|
}
|
|
97
97
|
/**
|
|
98
|
+
* Recursively searches for a command within nested exclusive usage terms.
|
|
99
|
+
* When the command is found, returns the expanded usage terms for that command.
|
|
100
|
+
*
|
|
101
|
+
* @param term The usage term to search in
|
|
102
|
+
* @param commandName The command name to find
|
|
103
|
+
* @returns The expanded usage terms if found, null otherwise
|
|
104
|
+
*/
|
|
105
|
+
function findCommandInExclusive(term, commandName) {
|
|
106
|
+
if (term.type !== "exclusive") return null;
|
|
107
|
+
for (const termGroup of term.terms) {
|
|
108
|
+
const firstTerm = termGroup[0];
|
|
109
|
+
if (firstTerm?.type === "command" && firstTerm.name === commandName) return termGroup;
|
|
110
|
+
if (firstTerm?.type === "exclusive") {
|
|
111
|
+
const found = findCommandInExclusive(firstTerm, commandName);
|
|
112
|
+
if (found) return [...found, ...termGroup.slice(1)];
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
return null;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
98
118
|
* Generates a documentation page for a parser based on its current state after
|
|
99
119
|
* attempting to parse the provided arguments. This function is useful for
|
|
100
120
|
* creating help documentation that reflects the current parsing context.
|
|
@@ -157,14 +177,11 @@ function getDocPage(parser, args = []) {
|
|
|
157
177
|
if (i >= usage.length) break;
|
|
158
178
|
const term = usage[i];
|
|
159
179
|
if (term.type === "exclusive") {
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
break;
|
|
166
|
-
}
|
|
167
|
-
if (usage[i] === term) i++;
|
|
180
|
+
const found = findCommandInExclusive(term, arg);
|
|
181
|
+
if (found) {
|
|
182
|
+
usage.splice(i, 1, ...found);
|
|
183
|
+
i += found.length;
|
|
184
|
+
} else i++;
|
|
168
185
|
} else i++;
|
|
169
186
|
}
|
|
170
187
|
return {
|