@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/modifiers.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Message } from "./message.cjs";
|
|
2
|
-
import {
|
|
2
|
+
import { Parser } from "./parser.cjs";
|
|
3
3
|
|
|
4
4
|
//#region src/modifiers.d.ts
|
|
5
5
|
|
|
@@ -8,14 +8,13 @@ import { Mode, Parser } from "./parser.cjs";
|
|
|
8
8
|
* without consuming input if the wrapped parser fails to match.
|
|
9
9
|
* If the wrapped parser succeeds, this returns its value.
|
|
10
10
|
* If the wrapped parser fails, this returns `undefined` without consuming input.
|
|
11
|
-
* @template M The execution mode of the parser.
|
|
12
11
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
13
12
|
* @template TState The type of the state used by the wrapped parser.
|
|
14
13
|
* @param parser The {@link Parser} to make optional.
|
|
15
14
|
* @returns A {@link Parser} that produces either the result of the wrapped parser
|
|
16
15
|
* or `undefined` if the wrapped parser fails to match.
|
|
17
16
|
*/
|
|
18
|
-
declare function optional<
|
|
17
|
+
declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>;
|
|
19
18
|
/**
|
|
20
19
|
* Options for the {@link withDefault} parser.
|
|
21
20
|
*/
|
|
@@ -71,7 +70,6 @@ declare class WithDefaultError extends Error {
|
|
|
71
70
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
72
71
|
* of returning `undefined` when the wrapped parser doesn't match, it returns
|
|
73
72
|
* a specified default value.
|
|
74
|
-
* @template M The execution mode of the parser.
|
|
75
73
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
76
74
|
* @template TState The type of the state used by the wrapped parser.
|
|
77
75
|
* @template TDefault The type of the default value.
|
|
@@ -83,13 +81,12 @@ declare class WithDefaultError extends Error {
|
|
|
83
81
|
* or the default value if the wrapped parser fails to match
|
|
84
82
|
* (union type {@link TValue} | {@link TDefault}).
|
|
85
83
|
*/
|
|
86
|
-
declare function withDefault<
|
|
84
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
87
85
|
/**
|
|
88
86
|
* Creates a parser that makes another parser use a default value when it fails
|
|
89
87
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
90
88
|
* of returning `undefined` when the wrapped parser doesn't match, it returns
|
|
91
89
|
* a specified default value.
|
|
92
|
-
* @template M The execution mode of the parser.
|
|
93
90
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
94
91
|
* @template TState The type of the state used by the wrapped parser.
|
|
95
92
|
* @template TDefault The type of the default value.
|
|
@@ -103,7 +100,7 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
103
100
|
* (union type {@link TValue} | {@link TDefault}).
|
|
104
101
|
* @since 0.5.0
|
|
105
102
|
*/
|
|
106
|
-
declare function withDefault<
|
|
103
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
107
104
|
/**
|
|
108
105
|
* Creates a parser that transforms the result value of another parser using
|
|
109
106
|
* a mapping function. This enables value transformation while preserving
|
|
@@ -115,7 +112,6 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
115
112
|
* - Computing derived values from parsed input
|
|
116
113
|
* - Creating reusable transformations that can be applied to any parser
|
|
117
114
|
*
|
|
118
|
-
* @template M The execution mode of the parser.
|
|
119
115
|
* @template T The type of the value produced by the original parser.
|
|
120
116
|
* @template U The type of the value produced by the mapping function.
|
|
121
117
|
* @template TState The type of the state used by the original parser.
|
|
@@ -138,7 +134,7 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
138
134
|
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
139
135
|
* ```
|
|
140
136
|
*/
|
|
141
|
-
declare function map<
|
|
137
|
+
declare function map<T, U, TState>(parser: Parser<T, TState>, transform: (value: T) => U): Parser<U, TState>;
|
|
142
138
|
/**
|
|
143
139
|
* Options for the {@link multiple} parser.
|
|
144
140
|
*/
|
|
@@ -181,7 +177,6 @@ interface MultipleErrorOptions {
|
|
|
181
177
|
* Creates a parser that allows multiple occurrences of a given parser.
|
|
182
178
|
* This parser can be used to parse multiple values of the same type,
|
|
183
179
|
* such as multiple command-line arguments or options.
|
|
184
|
-
* @template M The execution mode of the parser.
|
|
185
180
|
* @template TValue The type of the value that the parser produces.
|
|
186
181
|
* @template TState The type of the state used by the parser.
|
|
187
182
|
* @param parser The {@link Parser} to apply multiple times.
|
|
@@ -192,6 +187,6 @@ interface MultipleErrorOptions {
|
|
|
192
187
|
* of type {@link TValue} and an array of states
|
|
193
188
|
* of type {@link TState}.
|
|
194
189
|
*/
|
|
195
|
-
declare function multiple<
|
|
190
|
+
declare function multiple<TValue, TState>(parser: Parser<TValue, TState>, options?: MultipleOptions): Parser<readonly TValue[], readonly TState[]>;
|
|
196
191
|
//#endregion
|
|
197
192
|
export { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault };
|
package/dist/modifiers.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { Message } from "./message.js";
|
|
2
|
-
import {
|
|
2
|
+
import { Parser } from "./parser.js";
|
|
3
3
|
|
|
4
4
|
//#region src/modifiers.d.ts
|
|
5
5
|
|
|
@@ -8,14 +8,13 @@ import { Mode, Parser } from "./parser.js";
|
|
|
8
8
|
* without consuming input if the wrapped parser fails to match.
|
|
9
9
|
* If the wrapped parser succeeds, this returns its value.
|
|
10
10
|
* If the wrapped parser fails, this returns `undefined` without consuming input.
|
|
11
|
-
* @template M The execution mode of the parser.
|
|
12
11
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
13
12
|
* @template TState The type of the state used by the wrapped parser.
|
|
14
13
|
* @param parser The {@link Parser} to make optional.
|
|
15
14
|
* @returns A {@link Parser} that produces either the result of the wrapped parser
|
|
16
15
|
* or `undefined` if the wrapped parser fails to match.
|
|
17
16
|
*/
|
|
18
|
-
declare function optional<
|
|
17
|
+
declare function optional<TValue, TState>(parser: Parser<TValue, TState>): Parser<TValue | undefined, [TState] | undefined>;
|
|
19
18
|
/**
|
|
20
19
|
* Options for the {@link withDefault} parser.
|
|
21
20
|
*/
|
|
@@ -71,7 +70,6 @@ declare class WithDefaultError extends Error {
|
|
|
71
70
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
72
71
|
* of returning `undefined` when the wrapped parser doesn't match, it returns
|
|
73
72
|
* a specified default value.
|
|
74
|
-
* @template M The execution mode of the parser.
|
|
75
73
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
76
74
|
* @template TState The type of the state used by the wrapped parser.
|
|
77
75
|
* @template TDefault The type of the default value.
|
|
@@ -83,13 +81,12 @@ declare class WithDefaultError extends Error {
|
|
|
83
81
|
* or the default value if the wrapped parser fails to match
|
|
84
82
|
* (union type {@link TValue} | {@link TDefault}).
|
|
85
83
|
*/
|
|
86
|
-
declare function withDefault<
|
|
84
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault)): Parser<TValue | TDefault, [TState] | undefined>;
|
|
87
85
|
/**
|
|
88
86
|
* Creates a parser that makes another parser use a default value when it fails
|
|
89
87
|
* to match or consume input. This is similar to {@link optional}, but instead
|
|
90
88
|
* of returning `undefined` when the wrapped parser doesn't match, it returns
|
|
91
89
|
* a specified default value.
|
|
92
|
-
* @template M The execution mode of the parser.
|
|
93
90
|
* @template TValue The type of the value returned by the wrapped parser.
|
|
94
91
|
* @template TState The type of the state used by the wrapped parser.
|
|
95
92
|
* @template TDefault The type of the default value.
|
|
@@ -103,7 +100,7 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
103
100
|
* (union type {@link TValue} | {@link TDefault}).
|
|
104
101
|
* @since 0.5.0
|
|
105
102
|
*/
|
|
106
|
-
declare function withDefault<
|
|
103
|
+
declare function withDefault<TValue, TState, const TDefault = TValue>(parser: Parser<TValue, TState>, defaultValue: TDefault | (() => TDefault), options?: WithDefaultOptions): Parser<TValue | TDefault, [TState] | undefined>;
|
|
107
104
|
/**
|
|
108
105
|
* Creates a parser that transforms the result value of another parser using
|
|
109
106
|
* a mapping function. This enables value transformation while preserving
|
|
@@ -115,7 +112,6 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
115
112
|
* - Computing derived values from parsed input
|
|
116
113
|
* - Creating reusable transformations that can be applied to any parser
|
|
117
114
|
*
|
|
118
|
-
* @template M The execution mode of the parser.
|
|
119
115
|
* @template T The type of the value produced by the original parser.
|
|
120
116
|
* @template U The type of the value produced by the mapping function.
|
|
121
117
|
* @template TState The type of the state used by the original parser.
|
|
@@ -138,7 +134,7 @@ declare function withDefault<M extends Mode, TValue, TState, const TDefault = TV
|
|
|
138
134
|
* const prefixedParser = map(option("-n", integer()), n => `value: ${n}`);
|
|
139
135
|
* ```
|
|
140
136
|
*/
|
|
141
|
-
declare function map<
|
|
137
|
+
declare function map<T, U, TState>(parser: Parser<T, TState>, transform: (value: T) => U): Parser<U, TState>;
|
|
142
138
|
/**
|
|
143
139
|
* Options for the {@link multiple} parser.
|
|
144
140
|
*/
|
|
@@ -181,7 +177,6 @@ interface MultipleErrorOptions {
|
|
|
181
177
|
* Creates a parser that allows multiple occurrences of a given parser.
|
|
182
178
|
* This parser can be used to parse multiple values of the same type,
|
|
183
179
|
* such as multiple command-line arguments or options.
|
|
184
|
-
* @template M The execution mode of the parser.
|
|
185
180
|
* @template TValue The type of the value that the parser produces.
|
|
186
181
|
* @template TState The type of the state used by the parser.
|
|
187
182
|
* @param parser The {@link Parser} to apply multiple times.
|
|
@@ -192,6 +187,6 @@ interface MultipleErrorOptions {
|
|
|
192
187
|
* of type {@link TValue} and an array of states
|
|
193
188
|
* of type {@link TState}.
|
|
194
189
|
*/
|
|
195
|
-
declare function multiple<
|
|
190
|
+
declare function multiple<TValue, TState>(parser: Parser<TValue, TState>, options?: MultipleOptions): Parser<readonly TValue[], readonly TState[]>;
|
|
196
191
|
//#endregion
|
|
197
192
|
export { MultipleErrorOptions, MultipleOptions, WithDefaultError, WithDefaultOptions, map, multiple, optional, withDefault };
|
package/dist/modifiers.js
CHANGED
|
@@ -9,31 +9,12 @@ import { formatMessage, message, text } from "./message.js";
|
|
|
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 : 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 : message`Expected at least ${text(min.toLocaleString("en"))} values, but got only ${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 : message`Expected at most ${text(max.toLocaleString("en"))} values, but got ${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 : message`Expected at least ${text(min.toLocaleString("en"))} values, but got only ${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 : message`Expected at most ${text(max.toLocaleString("en"))} values, but got ${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
|