@optique/core 0.9.0-dev.217 → 0.9.0-dev.224
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/constructs.cjs +602 -1332
- package/dist/constructs.d.cts +62 -182
- package/dist/constructs.d.ts +62 -182
- package/dist/constructs.js +602 -1332
- package/dist/facade.cjs +144 -188
- package/dist/facade.d.cts +3 -41
- package/dist/facade.d.ts +3 -41
- package/dist/facade.js +145 -187
- package/dist/index.cjs +1 -8
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +4 -4
- package/dist/message.cjs +50 -0
- package/dist/message.d.cts +66 -1
- package/dist/message.d.ts +66 -1
- package/dist/message.js +50 -1
- package/dist/modifiers.cjs +79 -228
- package/dist/modifiers.d.cts +6 -11
- package/dist/modifiers.d.ts +6 -11
- package/dist/modifiers.js +79 -228
- package/dist/parser.cjs +33 -217
- package/dist/parser.d.cts +18 -202
- package/dist/parser.d.ts +18 -202
- package/dist/parser.js +34 -212
- package/dist/primitives.cjs +97 -242
- package/dist/primitives.d.cts +10 -14
- package/dist/primitives.d.ts +10 -14
- package/dist/primitives.js +97 -242
- package/dist/valueparser.cjs +3 -16
- package/dist/valueparser.d.cts +16 -29
- package/dist/valueparser.d.ts +16 -29
- package/dist/valueparser.js +4 -17
- package/package.json +1 -1
package/dist/message.d.cts
CHANGED
|
@@ -210,6 +210,71 @@ declare function envVar(envVar: string): MessageTerm;
|
|
|
210
210
|
* @since 0.6.0
|
|
211
211
|
*/
|
|
212
212
|
declare function commandLine(commandLine: string): MessageTerm;
|
|
213
|
+
/**
|
|
214
|
+
* Options for the {@link valueSet} function.
|
|
215
|
+
* @since 0.9.0
|
|
216
|
+
*/
|
|
217
|
+
interface ValueSetOptions {
|
|
218
|
+
/**
|
|
219
|
+
* The locale(s) to use for list formatting. Can be a BCP 47 language tag
|
|
220
|
+
* string, an array of language tags, an `Intl.Locale` object, or an array
|
|
221
|
+
* of `Intl.Locale` objects. If not specified, the system default locale
|
|
222
|
+
* is used.
|
|
223
|
+
*/
|
|
224
|
+
readonly locale?: string | readonly string[] | Intl.Locale | readonly Intl.Locale[];
|
|
225
|
+
/**
|
|
226
|
+
* The type of list to format:
|
|
227
|
+
*
|
|
228
|
+
* - `"conjunction"`: "A, B, and C" (default)
|
|
229
|
+
* - `"disjunction"`: "A, B, or C"
|
|
230
|
+
* - `"unit"`: "A, B, C"
|
|
231
|
+
*
|
|
232
|
+
* @default `"conjunction"`
|
|
233
|
+
*/
|
|
234
|
+
readonly type?: "conjunction" | "disjunction" | "unit";
|
|
235
|
+
/**
|
|
236
|
+
* The style of the list formatting:
|
|
237
|
+
*
|
|
238
|
+
* - `"long"`: "A, B, and C" (default)
|
|
239
|
+
* - `"short"`: "A, B, & C"
|
|
240
|
+
* - `"narrow"`: "A, B, C"
|
|
241
|
+
*
|
|
242
|
+
* @default `"long"`
|
|
243
|
+
*/
|
|
244
|
+
readonly style?: "long" | "short" | "narrow";
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Creates a {@link Message} for a formatted list of values using the
|
|
248
|
+
* `Intl.ListFormat` API. This is useful for displaying choice lists
|
|
249
|
+
* in error messages with proper locale-aware formatting.
|
|
250
|
+
*
|
|
251
|
+
* Each value in the list becomes a separate value term, and the separators
|
|
252
|
+
* (commas, "and", "or", etc.) become text terms. This allows each value
|
|
253
|
+
* to be styled independently while respecting the locale's list formatting
|
|
254
|
+
* conventions.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* // English conjunction (default): "error", "warn", and "info"
|
|
259
|
+
* const msg1 = message`Expected one of ${valueSet(["error", "warn", "info"])}.`;
|
|
260
|
+
*
|
|
261
|
+
* // English disjunction: "error", "warn", or "info"
|
|
262
|
+
* const msg2 = message`Expected ${
|
|
263
|
+
* valueSet(["error", "warn", "info"], { type: "disjunction" })
|
|
264
|
+
* }.`;
|
|
265
|
+
*
|
|
266
|
+
* // Korean disjunction: "error", "warn" 또는 "info"
|
|
267
|
+
* const msg3 = message`${
|
|
268
|
+
* valueSet(["error", "warn", "info"], { locale: "ko", type: "disjunction" })
|
|
269
|
+
* } 중 하나여야 합니다.`;
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @param values The list of values to format.
|
|
273
|
+
* @param options Optional formatting options including locale and list type.
|
|
274
|
+
* @returns A {@link Message} with alternating value and text terms.
|
|
275
|
+
* @since 0.9.0
|
|
276
|
+
*/
|
|
277
|
+
declare function valueSet(values: readonly string[], options?: ValueSetOptions): Message;
|
|
213
278
|
/**
|
|
214
279
|
* Options for the {@link formatMessage} function.
|
|
215
280
|
*/
|
|
@@ -258,4 +323,4 @@ interface MessageFormatOptions {
|
|
|
258
323
|
*/
|
|
259
324
|
declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
|
|
260
325
|
//#endregion
|
|
261
|
-
export { Message, MessageFormatOptions, MessageTerm, commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
|
|
326
|
+
export { Message, MessageFormatOptions, MessageTerm, ValueSetOptions, commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, valueSet, values };
|
package/dist/message.d.ts
CHANGED
|
@@ -210,6 +210,71 @@ declare function envVar(envVar: string): MessageTerm;
|
|
|
210
210
|
* @since 0.6.0
|
|
211
211
|
*/
|
|
212
212
|
declare function commandLine(commandLine: string): MessageTerm;
|
|
213
|
+
/**
|
|
214
|
+
* Options for the {@link valueSet} function.
|
|
215
|
+
* @since 0.9.0
|
|
216
|
+
*/
|
|
217
|
+
interface ValueSetOptions {
|
|
218
|
+
/**
|
|
219
|
+
* The locale(s) to use for list formatting. Can be a BCP 47 language tag
|
|
220
|
+
* string, an array of language tags, an `Intl.Locale` object, or an array
|
|
221
|
+
* of `Intl.Locale` objects. If not specified, the system default locale
|
|
222
|
+
* is used.
|
|
223
|
+
*/
|
|
224
|
+
readonly locale?: string | readonly string[] | Intl.Locale | readonly Intl.Locale[];
|
|
225
|
+
/**
|
|
226
|
+
* The type of list to format:
|
|
227
|
+
*
|
|
228
|
+
* - `"conjunction"`: "A, B, and C" (default)
|
|
229
|
+
* - `"disjunction"`: "A, B, or C"
|
|
230
|
+
* - `"unit"`: "A, B, C"
|
|
231
|
+
*
|
|
232
|
+
* @default `"conjunction"`
|
|
233
|
+
*/
|
|
234
|
+
readonly type?: "conjunction" | "disjunction" | "unit";
|
|
235
|
+
/**
|
|
236
|
+
* The style of the list formatting:
|
|
237
|
+
*
|
|
238
|
+
* - `"long"`: "A, B, and C" (default)
|
|
239
|
+
* - `"short"`: "A, B, & C"
|
|
240
|
+
* - `"narrow"`: "A, B, C"
|
|
241
|
+
*
|
|
242
|
+
* @default `"long"`
|
|
243
|
+
*/
|
|
244
|
+
readonly style?: "long" | "short" | "narrow";
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Creates a {@link Message} for a formatted list of values using the
|
|
248
|
+
* `Intl.ListFormat` API. This is useful for displaying choice lists
|
|
249
|
+
* in error messages with proper locale-aware formatting.
|
|
250
|
+
*
|
|
251
|
+
* Each value in the list becomes a separate value term, and the separators
|
|
252
|
+
* (commas, "and", "or", etc.) become text terms. This allows each value
|
|
253
|
+
* to be styled independently while respecting the locale's list formatting
|
|
254
|
+
* conventions.
|
|
255
|
+
*
|
|
256
|
+
* @example
|
|
257
|
+
* ```typescript
|
|
258
|
+
* // English conjunction (default): "error", "warn", and "info"
|
|
259
|
+
* const msg1 = message`Expected one of ${valueSet(["error", "warn", "info"])}.`;
|
|
260
|
+
*
|
|
261
|
+
* // English disjunction: "error", "warn", or "info"
|
|
262
|
+
* const msg2 = message`Expected ${
|
|
263
|
+
* valueSet(["error", "warn", "info"], { type: "disjunction" })
|
|
264
|
+
* }.`;
|
|
265
|
+
*
|
|
266
|
+
* // Korean disjunction: "error", "warn" 또는 "info"
|
|
267
|
+
* const msg3 = message`${
|
|
268
|
+
* valueSet(["error", "warn", "info"], { locale: "ko", type: "disjunction" })
|
|
269
|
+
* } 중 하나여야 합니다.`;
|
|
270
|
+
* ```
|
|
271
|
+
*
|
|
272
|
+
* @param values The list of values to format.
|
|
273
|
+
* @param options Optional formatting options including locale and list type.
|
|
274
|
+
* @returns A {@link Message} with alternating value and text terms.
|
|
275
|
+
* @since 0.9.0
|
|
276
|
+
*/
|
|
277
|
+
declare function valueSet(values: readonly string[], options?: ValueSetOptions): Message;
|
|
213
278
|
/**
|
|
214
279
|
* Options for the {@link formatMessage} function.
|
|
215
280
|
*/
|
|
@@ -258,4 +323,4 @@ interface MessageFormatOptions {
|
|
|
258
323
|
*/
|
|
259
324
|
declare function formatMessage(msg: Message, options?: MessageFormatOptions): string;
|
|
260
325
|
//#endregion
|
|
261
|
-
export { Message, MessageFormatOptions, MessageTerm, commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
|
|
326
|
+
export { Message, MessageFormatOptions, MessageTerm, ValueSetOptions, commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, valueSet, values };
|
package/dist/message.js
CHANGED
|
@@ -139,6 +139,55 @@ function commandLine(commandLine$1) {
|
|
|
139
139
|
};
|
|
140
140
|
}
|
|
141
141
|
/**
|
|
142
|
+
* Creates a {@link Message} for a formatted list of values using the
|
|
143
|
+
* `Intl.ListFormat` API. This is useful for displaying choice lists
|
|
144
|
+
* in error messages with proper locale-aware formatting.
|
|
145
|
+
*
|
|
146
|
+
* Each value in the list becomes a separate value term, and the separators
|
|
147
|
+
* (commas, "and", "or", etc.) become text terms. This allows each value
|
|
148
|
+
* to be styled independently while respecting the locale's list formatting
|
|
149
|
+
* conventions.
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* // English conjunction (default): "error", "warn", and "info"
|
|
154
|
+
* const msg1 = message`Expected one of ${valueSet(["error", "warn", "info"])}.`;
|
|
155
|
+
*
|
|
156
|
+
* // English disjunction: "error", "warn", or "info"
|
|
157
|
+
* const msg2 = message`Expected ${
|
|
158
|
+
* valueSet(["error", "warn", "info"], { type: "disjunction" })
|
|
159
|
+
* }.`;
|
|
160
|
+
*
|
|
161
|
+
* // Korean disjunction: "error", "warn" 또는 "info"
|
|
162
|
+
* const msg3 = message`${
|
|
163
|
+
* valueSet(["error", "warn", "info"], { locale: "ko", type: "disjunction" })
|
|
164
|
+
* } 중 하나여야 합니다.`;
|
|
165
|
+
* ```
|
|
166
|
+
*
|
|
167
|
+
* @param values The list of values to format.
|
|
168
|
+
* @param options Optional formatting options including locale and list type.
|
|
169
|
+
* @returns A {@link Message} with alternating value and text terms.
|
|
170
|
+
* @since 0.9.0
|
|
171
|
+
*/
|
|
172
|
+
function valueSet(values$1, options) {
|
|
173
|
+
if (values$1.length === 0) return [];
|
|
174
|
+
const formatter = new Intl.ListFormat(options?.locale, {
|
|
175
|
+
type: options?.type,
|
|
176
|
+
style: options?.style
|
|
177
|
+
});
|
|
178
|
+
const parts = formatter.formatToParts(values$1);
|
|
179
|
+
const result = [];
|
|
180
|
+
for (const part of parts) if (part.type === "element") result.push({
|
|
181
|
+
type: "value",
|
|
182
|
+
value: part.value
|
|
183
|
+
});
|
|
184
|
+
else result.push({
|
|
185
|
+
type: "text",
|
|
186
|
+
text: part.value
|
|
187
|
+
});
|
|
188
|
+
return result;
|
|
189
|
+
}
|
|
190
|
+
/**
|
|
142
191
|
* Formats a {@link Message} into a human-readable string for
|
|
143
192
|
* the terminal.
|
|
144
193
|
* @param msg The message to format, which is an array of
|
|
@@ -266,4 +315,4 @@ function formatMessage(msg, options = {}) {
|
|
|
266
315
|
}
|
|
267
316
|
|
|
268
317
|
//#endregion
|
|
269
|
-
export { commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, values };
|
|
318
|
+
export { commandLine, envVar, formatMessage, message, metavar, optionName, optionNames, text, value, valueSet, values };
|
package/dist/modifiers.cjs
CHANGED
|
@@ -9,31 +9,12 @@ const require_message = require('./message.cjs');
|
|
|
9
9
|
* - Returning success with undefined state when inner parser fails without consuming
|
|
10
10
|
* @internal
|
|
11
11
|
*/
|
|
12
|
-
function
|
|
12
|
+
function parseOptionalStyle(context, parser) {
|
|
13
13
|
const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
|
|
14
14
|
const result = parser.parse({
|
|
15
15
|
...context,
|
|
16
16
|
state: innerState
|
|
17
17
|
});
|
|
18
|
-
return processOptionalStyleResult(result, innerState, context);
|
|
19
|
-
}
|
|
20
|
-
/**
|
|
21
|
-
* Internal async helper for optional-style parsing logic.
|
|
22
|
-
* @internal
|
|
23
|
-
*/
|
|
24
|
-
async function parseOptionalStyleAsync(context, parser) {
|
|
25
|
-
const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
|
|
26
|
-
const result = await parser.parse({
|
|
27
|
-
...context,
|
|
28
|
-
state: innerState
|
|
29
|
-
});
|
|
30
|
-
return processOptionalStyleResult(result, innerState, context);
|
|
31
|
-
}
|
|
32
|
-
/**
|
|
33
|
-
* Internal helper to process optional-style parse results.
|
|
34
|
-
* @internal
|
|
35
|
-
*/
|
|
36
|
-
function processOptionalStyleResult(result, innerState, context) {
|
|
37
18
|
if (result.success) {
|
|
38
19
|
if (result.next.state !== innerState || result.consumed.length === 0) return {
|
|
39
20
|
success: true,
|
|
@@ -64,7 +45,6 @@ function processOptionalStyleResult(result, innerState, context) {
|
|
|
64
45
|
* without consuming input if the wrapped parser fails to match.
|
|
65
46
|
* If the wrapped parser succeeds, this returns its value.
|
|
66
47
|
* If the wrapped parser fails, this returns `undefined` without consuming input.
|
|
67
|
-
* @template M The execution mode of the parser.
|
|
68
48
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
69
49
|
* @template TState The type of the state used by the wrapped parser.
|
|
70
50
|
* @param parser The {@link Parser} to make optional.
|
|
@@ -72,25 +52,7 @@ function processOptionalStyleResult(result, innerState, context) {
|
|
|
72
52
|
* or `undefined` if the wrapped parser fails to match.
|
|
73
53
|
*/
|
|
74
54
|
function optional(parser) {
|
|
75
|
-
const syncParser = parser;
|
|
76
|
-
const isAsync = parser.$mode === "async";
|
|
77
|
-
function* suggestSync(context, prefix) {
|
|
78
|
-
const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
|
|
79
|
-
yield* syncParser.suggest({
|
|
80
|
-
...context,
|
|
81
|
-
state: innerState
|
|
82
|
-
}, prefix);
|
|
83
|
-
}
|
|
84
|
-
async function* suggestAsync(context, prefix) {
|
|
85
|
-
const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
|
|
86
|
-
const suggestions = parser.suggest({
|
|
87
|
-
...context,
|
|
88
|
-
state: innerState
|
|
89
|
-
}, prefix);
|
|
90
|
-
for await (const s of suggestions) yield s;
|
|
91
|
-
}
|
|
92
55
|
return {
|
|
93
|
-
$mode: parser.$mode,
|
|
94
56
|
$valueType: [],
|
|
95
57
|
$stateType: [],
|
|
96
58
|
priority: parser.priority,
|
|
@@ -100,27 +62,28 @@ function optional(parser) {
|
|
|
100
62
|
}],
|
|
101
63
|
initialState: void 0,
|
|
102
64
|
parse(context) {
|
|
103
|
-
|
|
104
|
-
return parseOptionalStyleSync(context, syncParser);
|
|
65
|
+
return parseOptionalStyle(context, parser);
|
|
105
66
|
},
|
|
106
67
|
complete(state) {
|
|
107
68
|
if (typeof state === "undefined") return {
|
|
108
69
|
success: true,
|
|
109
70
|
value: void 0
|
|
110
71
|
};
|
|
111
|
-
|
|
112
|
-
return (async () => await parser.complete(state[0]))();
|
|
72
|
+
return parser.complete(state[0]);
|
|
113
73
|
},
|
|
114
74
|
suggest(context, prefix) {
|
|
115
|
-
|
|
116
|
-
return
|
|
75
|
+
const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
|
|
76
|
+
return parser.suggest({
|
|
77
|
+
...context,
|
|
78
|
+
state: innerState
|
|
79
|
+
}, prefix);
|
|
117
80
|
},
|
|
118
81
|
getDocFragments(state, defaultValue) {
|
|
119
82
|
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state === void 0 ? { kind: "unavailable" } : {
|
|
120
83
|
kind: "available",
|
|
121
84
|
state: state.state[0]
|
|
122
85
|
};
|
|
123
|
-
return
|
|
86
|
+
return parser.getDocFragments(innerState, defaultValue);
|
|
124
87
|
}
|
|
125
88
|
};
|
|
126
89
|
}
|
|
@@ -159,25 +122,7 @@ var WithDefaultError = class extends Error {
|
|
|
159
122
|
}
|
|
160
123
|
};
|
|
161
124
|
function withDefault(parser, defaultValue, options) {
|
|
162
|
-
const syncParser = parser;
|
|
163
|
-
const isAsync = parser.$mode === "async";
|
|
164
|
-
function* suggestSync(context, prefix) {
|
|
165
|
-
const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
|
|
166
|
-
yield* syncParser.suggest({
|
|
167
|
-
...context,
|
|
168
|
-
state: innerState
|
|
169
|
-
}, prefix);
|
|
170
|
-
}
|
|
171
|
-
async function* suggestAsync(context, prefix) {
|
|
172
|
-
const innerState = typeof context.state === "undefined" ? syncParser.initialState : context.state[0];
|
|
173
|
-
const suggestions = parser.suggest({
|
|
174
|
-
...context,
|
|
175
|
-
state: innerState
|
|
176
|
-
}, prefix);
|
|
177
|
-
for await (const s of suggestions) yield s;
|
|
178
|
-
}
|
|
179
125
|
return {
|
|
180
|
-
$mode: parser.$mode,
|
|
181
126
|
$valueType: [],
|
|
182
127
|
$stateType: [],
|
|
183
128
|
priority: parser.priority,
|
|
@@ -187,8 +132,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
187
132
|
}],
|
|
188
133
|
initialState: void 0,
|
|
189
134
|
parse(context) {
|
|
190
|
-
|
|
191
|
-
return parseOptionalStyleSync(context, syncParser);
|
|
135
|
+
return parseOptionalStyle(context, parser);
|
|
192
136
|
},
|
|
193
137
|
complete(state) {
|
|
194
138
|
if (typeof state === "undefined") try {
|
|
@@ -203,12 +147,14 @@ function withDefault(parser, defaultValue, options) {
|
|
|
203
147
|
error: error instanceof WithDefaultError ? error.errorMessage : require_message.message`${require_message.text(String(error))}`
|
|
204
148
|
};
|
|
205
149
|
}
|
|
206
|
-
|
|
207
|
-
return (async () => await parser.complete(state[0]))();
|
|
150
|
+
return parser.complete(state[0]);
|
|
208
151
|
},
|
|
209
152
|
suggest(context, prefix) {
|
|
210
|
-
|
|
211
|
-
return
|
|
153
|
+
const innerState = typeof context.state === "undefined" ? parser.initialState : context.state[0];
|
|
154
|
+
return parser.suggest({
|
|
155
|
+
...context,
|
|
156
|
+
state: innerState
|
|
157
|
+
}, prefix);
|
|
212
158
|
},
|
|
213
159
|
getDocFragments(state, upperDefaultValue) {
|
|
214
160
|
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state === void 0 ? { kind: "unavailable" } : {
|
|
@@ -216,7 +162,7 @@ function withDefault(parser, defaultValue, options) {
|
|
|
216
162
|
state: state.state[0]
|
|
217
163
|
};
|
|
218
164
|
const actualDefaultValue = upperDefaultValue != null ? upperDefaultValue : typeof defaultValue === "function" ? defaultValue() : defaultValue;
|
|
219
|
-
const fragments =
|
|
165
|
+
const fragments = parser.getDocFragments(innerState, actualDefaultValue);
|
|
220
166
|
if (options?.message) {
|
|
221
167
|
const modifiedFragments = fragments.fragments.map((fragment) => {
|
|
222
168
|
if (fragment.type === "entry") return {
|
|
@@ -245,7 +191,6 @@ function withDefault(parser, defaultValue, options) {
|
|
|
245
191
|
* - Computing derived values from parsed input
|
|
246
192
|
* - Creating reusable transformations that can be applied to any parser
|
|
247
193
|
*
|
|
248
|
-
* @template M The execution mode of the parser.
|
|
249
194
|
* @template T The type of the value produced by the original parser.
|
|
250
195
|
* @template U The type of the value produced by the mapping function.
|
|
251
196
|
* @template TState The type of the state used by the original parser.
|
|
@@ -269,68 +214,33 @@ function withDefault(parser, defaultValue, options) {
|
|
|
269
214
|
* ```
|
|
270
215
|
*/
|
|
271
216
|
function map(parser, transform) {
|
|
272
|
-
|
|
273
|
-
const isAsync = parser.$mode === "async";
|
|
274
|
-
function* suggestSync(context, prefix) {
|
|
275
|
-
yield* syncParser.suggest(context, prefix);
|
|
276
|
-
}
|
|
277
|
-
async function* suggestAsync(context, prefix) {
|
|
278
|
-
const suggestions = parser.suggest(context, prefix);
|
|
279
|
-
for await (const s of suggestions) yield s;
|
|
280
|
-
}
|
|
281
|
-
const result = {
|
|
282
|
-
$mode: "sync",
|
|
217
|
+
return {
|
|
283
218
|
$valueType: [],
|
|
284
219
|
$stateType: parser.$stateType,
|
|
285
220
|
priority: parser.priority,
|
|
286
221
|
usage: parser.usage,
|
|
287
222
|
initialState: parser.initialState,
|
|
288
|
-
parse(
|
|
289
|
-
if (isAsync) return parser.parse(context);
|
|
290
|
-
return syncParser.parse(context);
|
|
291
|
-
},
|
|
223
|
+
parse: parser.parse.bind(parser),
|
|
292
224
|
complete(state) {
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
return {
|
|
300
|
-
success: false,
|
|
301
|
-
error: innerResult.error
|
|
302
|
-
};
|
|
303
|
-
}
|
|
304
|
-
return (async () => {
|
|
305
|
-
const innerResult = await parser.complete(state);
|
|
306
|
-
if (innerResult.success) return {
|
|
307
|
-
success: true,
|
|
308
|
-
value: transform(innerResult.value)
|
|
309
|
-
};
|
|
310
|
-
return {
|
|
311
|
-
success: false,
|
|
312
|
-
error: innerResult.error
|
|
313
|
-
};
|
|
314
|
-
})();
|
|
225
|
+
const result = parser.complete(state);
|
|
226
|
+
if (result.success) return {
|
|
227
|
+
success: true,
|
|
228
|
+
value: transform(result.value)
|
|
229
|
+
};
|
|
230
|
+
return result;
|
|
315
231
|
},
|
|
316
232
|
suggest(context, prefix) {
|
|
317
|
-
|
|
318
|
-
return suggestSync(context, prefix);
|
|
233
|
+
return parser.suggest(context, prefix);
|
|
319
234
|
},
|
|
320
235
|
getDocFragments(state, _defaultValue) {
|
|
321
|
-
return
|
|
236
|
+
return parser.getDocFragments(state, void 0);
|
|
322
237
|
}
|
|
323
238
|
};
|
|
324
|
-
return {
|
|
325
|
-
...result,
|
|
326
|
-
$mode: parser.$mode
|
|
327
|
-
};
|
|
328
239
|
}
|
|
329
240
|
/**
|
|
330
241
|
* Creates a parser that allows multiple occurrences of a given parser.
|
|
331
242
|
* This parser can be used to parse multiple values of the same type,
|
|
332
243
|
* such as multiple command-line arguments or options.
|
|
333
|
-
* @template M The execution mode of the parser.
|
|
334
244
|
* @template TValue The type of the value that the parser produces.
|
|
335
245
|
* @template TState The type of the state used by the parser.
|
|
336
246
|
* @param parser The {@link Parser} to apply multiple times.
|
|
@@ -342,59 +252,8 @@ function map(parser, transform) {
|
|
|
342
252
|
* of type {@link TState}.
|
|
343
253
|
*/
|
|
344
254
|
function multiple(parser, options = {}) {
|
|
345
|
-
const syncParser = parser;
|
|
346
|
-
const isAsync = parser.$mode === "async";
|
|
347
255
|
const { min = 0, max = Infinity } = options;
|
|
348
|
-
|
|
349
|
-
let added = context.state.length < 1;
|
|
350
|
-
let result = syncParser.parse({
|
|
351
|
-
...context,
|
|
352
|
-
state: context.state.at(-1) ?? syncParser.initialState
|
|
353
|
-
});
|
|
354
|
-
if (!result.success) if (!added) {
|
|
355
|
-
result = syncParser.parse({
|
|
356
|
-
...context,
|
|
357
|
-
state: syncParser.initialState
|
|
358
|
-
});
|
|
359
|
-
if (!result.success) return result;
|
|
360
|
-
added = true;
|
|
361
|
-
} else return result;
|
|
362
|
-
return {
|
|
363
|
-
success: true,
|
|
364
|
-
next: {
|
|
365
|
-
...result.next,
|
|
366
|
-
state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
|
|
367
|
-
},
|
|
368
|
-
consumed: result.consumed
|
|
369
|
-
};
|
|
370
|
-
};
|
|
371
|
-
const parseAsync = async (context) => {
|
|
372
|
-
let added = context.state.length < 1;
|
|
373
|
-
let resultOrPromise = parser.parse({
|
|
374
|
-
...context,
|
|
375
|
-
state: context.state.at(-1) ?? parser.initialState
|
|
376
|
-
});
|
|
377
|
-
let result = await resultOrPromise;
|
|
378
|
-
if (!result.success) if (!added) {
|
|
379
|
-
resultOrPromise = parser.parse({
|
|
380
|
-
...context,
|
|
381
|
-
state: parser.initialState
|
|
382
|
-
});
|
|
383
|
-
result = await resultOrPromise;
|
|
384
|
-
if (!result.success) return result;
|
|
385
|
-
added = true;
|
|
386
|
-
} else return result;
|
|
387
|
-
return {
|
|
388
|
-
success: true,
|
|
389
|
-
next: {
|
|
390
|
-
...result.next,
|
|
391
|
-
state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
|
|
392
|
-
},
|
|
393
|
-
consumed: result.consumed
|
|
394
|
-
};
|
|
395
|
-
};
|
|
396
|
-
const resultParser = {
|
|
397
|
-
$mode: parser.$mode,
|
|
256
|
+
return {
|
|
398
257
|
$valueType: [],
|
|
399
258
|
$stateType: [],
|
|
400
259
|
priority: parser.priority,
|
|
@@ -405,79 +264,71 @@ function multiple(parser, options = {}) {
|
|
|
405
264
|
}],
|
|
406
265
|
initialState: [],
|
|
407
266
|
parse(context) {
|
|
408
|
-
|
|
409
|
-
|
|
267
|
+
let added = context.state.length < 1;
|
|
268
|
+
let result = parser.parse({
|
|
269
|
+
...context,
|
|
270
|
+
state: context.state.at(-1) ?? parser.initialState
|
|
271
|
+
});
|
|
272
|
+
if (!result.success) if (!added) {
|
|
273
|
+
result = parser.parse({
|
|
274
|
+
...context,
|
|
275
|
+
state: parser.initialState
|
|
276
|
+
});
|
|
277
|
+
if (!result.success) return result;
|
|
278
|
+
added = true;
|
|
279
|
+
} else return result;
|
|
280
|
+
return {
|
|
281
|
+
success: true,
|
|
282
|
+
next: {
|
|
283
|
+
...result.next,
|
|
284
|
+
state: [...added ? context.state : context.state.slice(0, -1), result.next.state]
|
|
285
|
+
},
|
|
286
|
+
consumed: result.consumed
|
|
287
|
+
};
|
|
410
288
|
},
|
|
411
289
|
complete(state) {
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
};
|
|
421
|
-
}
|
|
422
|
-
return validateMultipleResult(result);
|
|
290
|
+
const result = [];
|
|
291
|
+
for (const s of state) {
|
|
292
|
+
const valueResult = parser.complete(s);
|
|
293
|
+
if (valueResult.success) result.push(valueResult.value);
|
|
294
|
+
else return {
|
|
295
|
+
success: false,
|
|
296
|
+
error: valueResult.error
|
|
297
|
+
};
|
|
423
298
|
}
|
|
424
|
-
|
|
425
|
-
const
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
299
|
+
if (result.length < min) {
|
|
300
|
+
const customMessage = options.errors?.tooFew;
|
|
301
|
+
return {
|
|
302
|
+
success: false,
|
|
303
|
+
error: customMessage ? typeof customMessage === "function" ? customMessage(min, result.length) : customMessage : require_message.message`Expected at least ${require_message.text(min.toLocaleString("en"))} values, but got only ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
304
|
+
};
|
|
305
|
+
} else if (result.length > max) {
|
|
306
|
+
const customMessage = options.errors?.tooMany;
|
|
307
|
+
return {
|
|
308
|
+
success: false,
|
|
309
|
+
error: customMessage ? typeof customMessage === "function" ? customMessage(max, result.length) : customMessage : require_message.message`Expected at most ${require_message.text(max.toLocaleString("en"))} values, but got ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
310
|
+
};
|
|
311
|
+
}
|
|
312
|
+
return {
|
|
313
|
+
success: true,
|
|
314
|
+
value: result
|
|
315
|
+
};
|
|
436
316
|
},
|
|
437
317
|
suggest(context, prefix) {
|
|
438
318
|
const innerState = context.state.length > 0 ? context.state.at(-1) : parser.initialState;
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
}, prefix);
|
|
444
|
-
for await (const s of suggestions) yield s;
|
|
445
|
-
}();
|
|
446
|
-
return function* () {
|
|
447
|
-
yield* syncParser.suggest({
|
|
448
|
-
...context,
|
|
449
|
-
state: innerState
|
|
450
|
-
}, prefix);
|
|
451
|
-
}();
|
|
319
|
+
return parser.suggest({
|
|
320
|
+
...context,
|
|
321
|
+
state: innerState
|
|
322
|
+
}, prefix);
|
|
452
323
|
},
|
|
453
324
|
getDocFragments(state, defaultValue) {
|
|
454
325
|
const innerState = state.kind === "unavailable" ? { kind: "unavailable" } : state.state.length > 0 ? {
|
|
455
326
|
kind: "available",
|
|
456
327
|
state: state.state.at(-1)
|
|
457
328
|
} : { kind: "unavailable" };
|
|
458
|
-
return
|
|
329
|
+
return parser.getDocFragments(innerState, defaultValue != null && defaultValue.length > 0 ? defaultValue[0] : void 0);
|
|
459
330
|
}
|
|
460
331
|
};
|
|
461
|
-
function validateMultipleResult(result) {
|
|
462
|
-
if (result.length < min) {
|
|
463
|
-
const customMessage = options.errors?.tooFew;
|
|
464
|
-
return {
|
|
465
|
-
success: false,
|
|
466
|
-
error: customMessage ? typeof customMessage === "function" ? customMessage(min, result.length) : customMessage : require_message.message`Expected at least ${require_message.text(min.toLocaleString("en"))} values, but got only ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
467
|
-
};
|
|
468
|
-
} else if (result.length > max) {
|
|
469
|
-
const customMessage = options.errors?.tooMany;
|
|
470
|
-
return {
|
|
471
|
-
success: false,
|
|
472
|
-
error: customMessage ? typeof customMessage === "function" ? customMessage(max, result.length) : customMessage : require_message.message`Expected at most ${require_message.text(max.toLocaleString("en"))} values, but got ${require_message.text(result.length.toLocaleString("en"))}.`
|
|
473
|
-
};
|
|
474
|
-
}
|
|
475
|
-
return {
|
|
476
|
-
success: true,
|
|
477
|
-
value: result
|
|
478
|
-
};
|
|
479
|
-
}
|
|
480
|
-
return resultParser;
|
|
481
332
|
}
|
|
482
333
|
|
|
483
334
|
//#endregion
|