@thomasfosterau/effect-parse 0.0.0-stage → 0.0.0
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 +21 -0
- package/README.md +550 -2
- package/dist/Ascii.d.ts +392 -0
- package/dist/Ascii.d.ts.map +1 -0
- package/dist/Ascii.js +610 -0
- package/dist/Ascii.js.map +1 -0
- package/dist/Binary.d.ts +153 -0
- package/dist/Binary.d.ts.map +1 -0
- package/dist/Binary.js +171 -0
- package/dist/Binary.js.map +1 -0
- package/dist/Bits.d.ts +60 -0
- package/dist/Bits.d.ts.map +1 -0
- package/dist/Bits.js +144 -0
- package/dist/Bits.js.map +1 -0
- package/dist/Encoding.d.ts +116 -0
- package/dist/Encoding.d.ts.map +1 -0
- package/dist/Encoding.js +85 -0
- package/dist/Encoding.js.map +1 -0
- package/dist/Expression.d.ts +124 -0
- package/dist/Expression.d.ts.map +1 -0
- package/dist/Expression.js +139 -0
- package/dist/Expression.js.map +1 -0
- package/dist/Input.d.ts +301 -0
- package/dist/Input.d.ts.map +1 -0
- package/dist/Input.js +312 -0
- package/dist/Input.js.map +1 -0
- package/dist/Nom.d.ts +518 -0
- package/dist/Nom.d.ts.map +1 -0
- package/dist/Nom.js +519 -0
- package/dist/Nom.js.map +1 -0
- package/dist/ParseError.d.ts +120 -0
- package/dist/ParseError.d.ts.map +1 -0
- package/dist/ParseError.js +301 -0
- package/dist/ParseError.js.map +1 -0
- package/dist/Parser.d.ts +1312 -0
- package/dist/Parser.d.ts.map +1 -0
- package/dist/Parser.js +1936 -0
- package/dist/Parser.js.map +1 -0
- package/dist/ParserSchema.d.ts +31 -0
- package/dist/ParserSchema.d.ts.map +1 -0
- package/dist/ParserSchema.js +56 -0
- package/dist/ParserSchema.js.map +1 -0
- package/dist/ParserStream.d.ts +127 -0
- package/dist/ParserStream.d.ts.map +1 -0
- package/dist/ParserStream.js +270 -0
- package/dist/ParserStream.js.map +1 -0
- package/dist/Text.d.ts +330 -0
- package/dist/Text.d.ts.map +1 -0
- package/dist/Text.js +529 -0
- package/dist/Text.js.map +1 -0
- package/dist/Token.d.ts +225 -0
- package/dist/Token.d.ts.map +1 -0
- package/dist/Token.js +294 -0
- package/dist/Token.js.map +1 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +17 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/compiler/closure.d.ts +25 -0
- package/dist/internal/compiler/closure.d.ts.map +1 -0
- package/dist/internal/compiler/closure.js +1668 -0
- package/dist/internal/compiler/closure.js.map +1 -0
- package/dist/internal/compiler/entries.d.ts +19 -0
- package/dist/internal/compiler/entries.d.ts.map +1 -0
- package/dist/internal/compiler/entries.js +32 -0
- package/dist/internal/compiler/entries.js.map +1 -0
- package/dist/internal/compiler/first.d.ts +33 -0
- package/dist/internal/compiler/first.d.ts.map +1 -0
- package/dist/internal/compiler/first.js +235 -0
- package/dist/internal/compiler/first.js.map +1 -0
- package/dist/internal/compiler/jit.d.ts +20 -0
- package/dist/internal/compiler/jit.d.ts.map +1 -0
- package/dist/internal/compiler/jit.js +1030 -0
- package/dist/internal/compiler/jit.js.map +1 -0
- package/dist/internal/compiler/registry.d.ts +34 -0
- package/dist/internal/compiler/registry.d.ts.map +1 -0
- package/dist/internal/compiler/registry.js +46 -0
- package/dist/internal/compiler/registry.js.map +1 -0
- package/dist/internal/compiler/sets.d.ts +29 -0
- package/dist/internal/compiler/sets.d.ts.map +1 -0
- package/dist/internal/compiler/sets.js +85 -0
- package/dist/internal/compiler/sets.js.map +1 -0
- package/dist/internal/entries.d.ts +27 -0
- package/dist/internal/entries.d.ts.map +1 -0
- package/dist/internal/entries.js +42 -0
- package/dist/internal/entries.js.map +1 -0
- package/dist/internal/node.d.ts +251 -0
- package/dist/internal/node.d.ts.map +1 -0
- package/dist/internal/node.js +73 -0
- package/dist/internal/node.js.map +1 -0
- package/dist/internal/numbers.d.ts +41 -0
- package/dist/internal/numbers.d.ts.map +1 -0
- package/dist/internal/numbers.js +302 -0
- package/dist/internal/numbers.js.map +1 -0
- package/dist/internal/resume.d.ts +39 -0
- package/dist/internal/resume.d.ts.map +1 -0
- package/dist/internal/resume.js +37 -0
- package/dist/internal/resume.js.map +1 -0
- package/dist/internal/text.d.ts +72 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +391 -0
- package/dist/internal/text.js.map +1 -0
- package/dist/internal/tokens.d.ts +93 -0
- package/dist/internal/tokens.d.ts.map +1 -0
- package/dist/internal/tokens.js +400 -0
- package/dist/internal/tokens.js.map +1 -0
- package/package.json +115 -3
- package/src/Ascii.ts +741 -0
- package/src/Binary.ts +228 -0
- package/src/Bits.ts +192 -0
- package/src/Encoding.ts +128 -0
- package/src/Expression.ts +245 -0
- package/src/Input.ts +429 -0
- package/src/Nom.ts +642 -0
- package/src/ParseError.ts +378 -0
- package/src/Parser.ts +2763 -0
- package/src/ParserSchema.ts +87 -0
- package/src/ParserStream.ts +390 -0
- package/src/Text.ts +757 -0
- package/src/Token.ts +416 -0
- package/src/index.ts +16 -0
- package/src/internal/compiler/closure.ts +1583 -0
- package/src/internal/compiler/entries.ts +36 -0
- package/src/internal/compiler/first.ts +252 -0
- package/src/internal/compiler/jit.ts +1091 -0
- package/src/internal/compiler/registry.ts +79 -0
- package/src/internal/compiler/sets.ts +100 -0
- package/src/internal/entries.ts +51 -0
- package/src/internal/node.ts +207 -0
- package/src/internal/numbers.ts +291 -0
- package/src/internal/resume.ts +55 -0
- package/src/internal/text.ts +411 -0
- package/src/internal/tokens.ts +409 -0
package/dist/Parser.d.ts
ADDED
|
@@ -0,0 +1,1312 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `Parser` data type and its general-purpose combinators.
|
|
3
|
+
*
|
|
4
|
+
* A `Parser<A, E, R, I>` consumes input of type `I` and produces a value of
|
|
5
|
+
* type `A`. It may fail with a `ParseError<E>`, where `E` is the type of
|
|
6
|
+
* user supplied error causes, and it may require services `R` from an Effect
|
|
7
|
+
* `Context`.
|
|
8
|
+
*
|
|
9
|
+
* Under the hood a parser is a function over a mutable cursor (`Input.State`),
|
|
10
|
+
* as in winnow. Failure is cheap and backtracking is a matter of resetting an
|
|
11
|
+
* offset. On the surface the API follows Effect's conventions: parsers are
|
|
12
|
+
* pipeable, combinators are dual (data-first or data-last), and parsers can
|
|
13
|
+
* be written with generators:
|
|
14
|
+
*
|
|
15
|
+
* ```ts
|
|
16
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
17
|
+
*
|
|
18
|
+
* const point = Parser.gen(function*() {
|
|
19
|
+
* yield* Token.literal("(")
|
|
20
|
+
* const x = yield* Ascii.decInt
|
|
21
|
+
* yield* Token.literal(",")
|
|
22
|
+
* const y = yield* Ascii.decInt
|
|
23
|
+
* yield* Token.literal(")")
|
|
24
|
+
* return { x, y }
|
|
25
|
+
* })
|
|
26
|
+
*
|
|
27
|
+
* Parser.parse(point, "(1,-2)") // Result.succeed({ x: 1, y: -2 })
|
|
28
|
+
* ```
|
|
29
|
+
*
|
|
30
|
+
* @since 0.1.0
|
|
31
|
+
*/
|
|
32
|
+
import * as Context from "effect/Context";
|
|
33
|
+
import * as Effect from "effect/Effect";
|
|
34
|
+
import * as Option from "effect/Option";
|
|
35
|
+
import type { Pipeable } from "effect/Pipeable";
|
|
36
|
+
import * as Result from "effect/Result";
|
|
37
|
+
import type { Contravariant, Covariant, Simplify } from "effect/Types";
|
|
38
|
+
import type { Input, Slice, Source, TraceEvent } from "./Input.js";
|
|
39
|
+
import { Fail, State } from "./Input.js";
|
|
40
|
+
import type { ErrorKind } from "./ParseError.js";
|
|
41
|
+
import * as ParseError from "./ParseError.js";
|
|
42
|
+
import type { Node } from "./internal/node.js";
|
|
43
|
+
/**
|
|
44
|
+
* @since 0.1.0
|
|
45
|
+
* @category type ids
|
|
46
|
+
*/
|
|
47
|
+
export declare const TypeId: "~effect-parse/Parser";
|
|
48
|
+
/**
|
|
49
|
+
* A parser that consumes input of type `I`, producing a value of type `A`,
|
|
50
|
+
* possibly failing with a user cause of type `E`, and requiring services `R`.
|
|
51
|
+
*
|
|
52
|
+
* @since 0.1.0
|
|
53
|
+
* @category models
|
|
54
|
+
*/
|
|
55
|
+
export interface Parser<out A, out E = never, out R = never, in I extends Input = string> extends Pipeable {
|
|
56
|
+
readonly [TypeId]: Parser.Variance<A, E, R, I>;
|
|
57
|
+
/**
|
|
58
|
+
* Runs one parsing step against a cursor. Returns the parsed value, or the
|
|
59
|
+
* `Fail` sentinel after recording the failure on the state.
|
|
60
|
+
*
|
|
61
|
+
* Calling `step` directly is how custom combinators built with
|
|
62
|
+
* {@link make} invoke other parsers.
|
|
63
|
+
*/
|
|
64
|
+
readonly step: (state: State<I>) => A | Fail;
|
|
65
|
+
/** A human-readable name, used by `toString` and tracing. */
|
|
66
|
+
readonly name: string | undefined;
|
|
67
|
+
[Symbol.iterator](): ParserIterator<Parser<A, E, R, I>>;
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* The iterator used by `yield*` inside {@link gen}.
|
|
71
|
+
*
|
|
72
|
+
* @since 0.1.0
|
|
73
|
+
* @category models
|
|
74
|
+
*/
|
|
75
|
+
export interface ParserIterator<P extends Parser.Any> {
|
|
76
|
+
next(...args: ReadonlyArray<any>): IteratorResult<P, Parser.Success<P>>;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* @since 0.1.0
|
|
80
|
+
* @category models
|
|
81
|
+
*/
|
|
82
|
+
export declare namespace Parser {
|
|
83
|
+
/**
|
|
84
|
+
* @since 0.1.0
|
|
85
|
+
*/
|
|
86
|
+
interface Variance<A, E, R, I> {
|
|
87
|
+
readonly _A: Covariant<A>;
|
|
88
|
+
readonly _E: Covariant<E>;
|
|
89
|
+
readonly _R: Covariant<R>;
|
|
90
|
+
readonly _I: Contravariant<I>;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Any parser.
|
|
94
|
+
*
|
|
95
|
+
* @since 0.1.0
|
|
96
|
+
*/
|
|
97
|
+
type Any = Parser<any, any, any, any>;
|
|
98
|
+
/**
|
|
99
|
+
* The output type of a parser.
|
|
100
|
+
*
|
|
101
|
+
* @since 0.1.0
|
|
102
|
+
*/
|
|
103
|
+
type Success<P> = P extends Parser<infer A, infer _E, infer _R, infer _I> ? A : never;
|
|
104
|
+
/**
|
|
105
|
+
* The user error type of a parser.
|
|
106
|
+
*
|
|
107
|
+
* @since 0.1.0
|
|
108
|
+
*/
|
|
109
|
+
type Error<P> = P extends Parser<infer _A, infer E, infer _R, infer _I> ? E : never;
|
|
110
|
+
/**
|
|
111
|
+
* The services required by a parser.
|
|
112
|
+
*
|
|
113
|
+
* @since 0.1.0
|
|
114
|
+
*/
|
|
115
|
+
type Services<P> = P extends Parser<infer _A, infer _E, infer R, infer _I> ? R : never;
|
|
116
|
+
/**
|
|
117
|
+
* The input accepted by a parser (or by every member of a union of
|
|
118
|
+
* parsers).
|
|
119
|
+
*
|
|
120
|
+
* @since 0.1.0
|
|
121
|
+
*/
|
|
122
|
+
type InputOf<P> = [P] extends [never] ? Input : [P] extends [Parser<infer _A, infer _E, infer _R, infer I>] ? I : never;
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* A repetition range, in the spirit of winnow's `impl Into<Range>`:
|
|
126
|
+
*
|
|
127
|
+
* - `n`: exactly `n` times
|
|
128
|
+
* - `{ min, max }`: between `min` and `max` times (inclusive); `min`
|
|
129
|
+
* defaults to 0 and `max` to unbounded.
|
|
130
|
+
*
|
|
131
|
+
* @since 0.1.0
|
|
132
|
+
* @category models
|
|
133
|
+
*/
|
|
134
|
+
export type Range = number | {
|
|
135
|
+
readonly min?: number | undefined;
|
|
136
|
+
readonly max?: number | undefined;
|
|
137
|
+
};
|
|
138
|
+
/**
|
|
139
|
+
* The output of a repetition: a non-empty array when the range has a literal
|
|
140
|
+
* positive minimum.
|
|
141
|
+
*
|
|
142
|
+
* @since 0.1.0
|
|
143
|
+
* @category models
|
|
144
|
+
*/
|
|
145
|
+
export type Repeated<A, Rg> = Rg extends number ? number extends Rg ? Array<A> : Rg extends 0 ? Array<A> : NonEmpty<A> : Rg extends {
|
|
146
|
+
readonly min: infer M extends number;
|
|
147
|
+
} ? number extends M ? Array<A> : M extends 0 ? Array<A> : NonEmpty<A> : Array<A>;
|
|
148
|
+
type NonEmpty<A> = [A, ...Array<A>];
|
|
149
|
+
/**
|
|
150
|
+
* A span of input offsets, `[start, end)`.
|
|
151
|
+
*
|
|
152
|
+
* @since 0.1.0
|
|
153
|
+
* @category models
|
|
154
|
+
*/
|
|
155
|
+
export interface Span {
|
|
156
|
+
readonly start: number;
|
|
157
|
+
readonly end: number;
|
|
158
|
+
}
|
|
159
|
+
/**
|
|
160
|
+
* Tests whether a value is a `Parser`.
|
|
161
|
+
*
|
|
162
|
+
* @since 0.1.0
|
|
163
|
+
* @category guards
|
|
164
|
+
*/
|
|
165
|
+
export declare const isParser: (u: unknown) => u is Parser<unknown, unknown, unknown, never>;
|
|
166
|
+
/**
|
|
167
|
+
* Creates a parser from a step function over the mutable {@link State}
|
|
168
|
+
* cursor. This is the escape hatch for writing low-level parsers, much like
|
|
169
|
+
* passing a plain function as a parser in winnow.
|
|
170
|
+
*
|
|
171
|
+
* The step function must either return the parsed value, having advanced
|
|
172
|
+
* `state.pos` past the consumed input, or return the result of one of
|
|
173
|
+
* `state.backtrack`, `state.cut`, `state.incomplete`, ... to signal failure.
|
|
174
|
+
*
|
|
175
|
+
* @example
|
|
176
|
+
* ```ts
|
|
177
|
+
* import { Parser } from "@thomasfosterau/effect-parse"
|
|
178
|
+
*
|
|
179
|
+
* // Parses a single ASCII uppercase letter
|
|
180
|
+
* const upper = Parser.make<string>((state) => {
|
|
181
|
+
* const c = state.input.charCodeAt(state.pos)
|
|
182
|
+
* if (state.pos < state.end && c >= 65 && c <= 90) {
|
|
183
|
+
* state.pos++
|
|
184
|
+
* return String.fromCharCode(c)
|
|
185
|
+
* }
|
|
186
|
+
* return state.pos >= state.end
|
|
187
|
+
* ? state.eoi("Token", ["uppercase letter"], 1)
|
|
188
|
+
* : state.backtrack("Token", ["uppercase letter"])
|
|
189
|
+
* })
|
|
190
|
+
* ```
|
|
191
|
+
*
|
|
192
|
+
* @since 0.1.0
|
|
193
|
+
* @category constructors
|
|
194
|
+
*/
|
|
195
|
+
export declare const make: <A, E = never, R = never, I extends Input = string>(step: (state: State<I>) => A | Fail, name?: string) => Parser<A, E, R, I>;
|
|
196
|
+
/**
|
|
197
|
+
* Creates a parser together with its description, used by the compiler.
|
|
198
|
+
*
|
|
199
|
+
* @internal
|
|
200
|
+
*/
|
|
201
|
+
export declare const described: <A, E = never, R = never, I extends Input = string>(step: (state: State<I>) => A | Fail, name: string | undefined, node: Node) => Parser<A, E, R, I>;
|
|
202
|
+
/**
|
|
203
|
+
* Gives a parser a name, used by `toString`.
|
|
204
|
+
*
|
|
205
|
+
* @since 0.1.0
|
|
206
|
+
* @category utils
|
|
207
|
+
*/
|
|
208
|
+
export declare const named: {
|
|
209
|
+
(name: string): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
210
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, name: string): Parser<A, E, R, I>;
|
|
211
|
+
};
|
|
212
|
+
/**
|
|
213
|
+
* A parser that consumes nothing and succeeds with the given value.
|
|
214
|
+
*
|
|
215
|
+
* @since 0.1.0
|
|
216
|
+
* @category constructors
|
|
217
|
+
*/
|
|
218
|
+
export declare const succeed: <A>(value: A) => Parser<A, never, never, Input>;
|
|
219
|
+
/**
|
|
220
|
+
* A parser that consumes nothing and succeeds with the result of a thunk.
|
|
221
|
+
*
|
|
222
|
+
* @since 0.1.0
|
|
223
|
+
* @category constructors
|
|
224
|
+
*/
|
|
225
|
+
export declare const sync: <A>(evaluate: () => A) => Parser<A, never, never, Input>;
|
|
226
|
+
/**
|
|
227
|
+
* A parser that consumes nothing and succeeds with `undefined` (winnow's
|
|
228
|
+
* `empty`, nom's `success(())`).
|
|
229
|
+
*
|
|
230
|
+
* @since 0.1.0
|
|
231
|
+
* @category constructors
|
|
232
|
+
*/
|
|
233
|
+
export declare const empty: Parser<void, never, never, Input>;
|
|
234
|
+
/**
|
|
235
|
+
* Alias of {@link empty}, following Effect's `Effect.void`.
|
|
236
|
+
*
|
|
237
|
+
* @since 0.1.0
|
|
238
|
+
* @category constructors
|
|
239
|
+
*/
|
|
240
|
+
export declare const void_: Parser<void, never, never, Input>;
|
|
241
|
+
export {
|
|
242
|
+
/**
|
|
243
|
+
* @since 0.1.0
|
|
244
|
+
* @category constructors
|
|
245
|
+
*/
|
|
246
|
+
void_ as void };
|
|
247
|
+
/**
|
|
248
|
+
* A parser that always fails (backtracks) with the given user error as its
|
|
249
|
+
* cause.
|
|
250
|
+
*
|
|
251
|
+
* @since 0.1.0
|
|
252
|
+
* @category constructors
|
|
253
|
+
*/
|
|
254
|
+
export declare const fail: <E>(error: E) => Parser<never, E, never, Input>;
|
|
255
|
+
/**
|
|
256
|
+
* A parser that always fails (backtracks) without a cause. This is winnow's
|
|
257
|
+
* `fail` and nom's `fail()`.
|
|
258
|
+
*
|
|
259
|
+
* @since 0.1.0
|
|
260
|
+
* @category constructors
|
|
261
|
+
*/
|
|
262
|
+
export declare const reject: Parser<never, never, never, Input>;
|
|
263
|
+
/**
|
|
264
|
+
* A parser that always fails with a message.
|
|
265
|
+
*
|
|
266
|
+
* @since 0.1.0
|
|
267
|
+
* @category constructors
|
|
268
|
+
*/
|
|
269
|
+
export declare const failMessage: (message: string) => Parser<never, never, never, Input>;
|
|
270
|
+
/**
|
|
271
|
+
* A placeholder parser that fails with an unrecoverable error (winnow's
|
|
272
|
+
* `todo`).
|
|
273
|
+
*
|
|
274
|
+
* @since 0.1.0
|
|
275
|
+
* @category constructors
|
|
276
|
+
*/
|
|
277
|
+
export declare const todo: Parser<never, never, never, Input>;
|
|
278
|
+
/**
|
|
279
|
+
* Lazily constructs a parser, which is needed to define recursive grammars.
|
|
280
|
+
* The thunk is evaluated once, on first use.
|
|
281
|
+
*
|
|
282
|
+
* @example
|
|
283
|
+
* ```ts
|
|
284
|
+
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
285
|
+
*
|
|
286
|
+
* type Nested = ReadonlyArray<Nested>
|
|
287
|
+
* const nested: Parser.Parser<Nested> = Parser.delimited(
|
|
288
|
+
* Token.literal("["),
|
|
289
|
+
* Parser.repeat(Parser.suspend(() => nested)),
|
|
290
|
+
* Token.literal("]")
|
|
291
|
+
* )
|
|
292
|
+
* ```
|
|
293
|
+
*
|
|
294
|
+
* @since 0.1.0
|
|
295
|
+
* @category constructors
|
|
296
|
+
*/
|
|
297
|
+
export declare const suspend: <A, E = never, R = never, I extends Input = string>(evaluate: () => Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
298
|
+
/**
|
|
299
|
+
* The current offset into the input (winnow's `Location`).
|
|
300
|
+
*
|
|
301
|
+
* @since 0.1.0
|
|
302
|
+
* @category constructors
|
|
303
|
+
*/
|
|
304
|
+
export declare const location: Parser<number, never, never, Input>;
|
|
305
|
+
/**
|
|
306
|
+
* Succeeds (with `undefined`) only at the end of the input; nom's and
|
|
307
|
+
* winnow's `eof`. On partial input, reaching the end of the available data
|
|
308
|
+
* is reported as `Incomplete` since more data may follow.
|
|
309
|
+
*
|
|
310
|
+
* @since 0.1.0
|
|
311
|
+
* @category constructors
|
|
312
|
+
*/
|
|
313
|
+
export declare const eof: Parser<void, never, never, Input>;
|
|
314
|
+
/**
|
|
315
|
+
* Transforms the output of a parser.
|
|
316
|
+
*
|
|
317
|
+
* @since 0.1.0
|
|
318
|
+
* @category mapping
|
|
319
|
+
*/
|
|
320
|
+
export declare const map: {
|
|
321
|
+
<A, B>(f: (a: A) => B): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E, R, I>;
|
|
322
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, f: (a: A) => B): Parser<B, E, R, I>;
|
|
323
|
+
};
|
|
324
|
+
/**
|
|
325
|
+
* Builds an object from the `[key, value]` entries a parser returns, as
|
|
326
|
+
* `Object.fromEntries` does: `fromEntries(self)` is
|
|
327
|
+
* `map(self, Object.fromEntries)`.
|
|
328
|
+
*
|
|
329
|
+
* Compiled parsers build the object directly when `self` repeats a pair
|
|
330
|
+
* (`separated` or `repeat` of `separatedPair` or a two-element `seq`,
|
|
331
|
+
* possibly inside `preceded`, `terminated`, `delimited` or `cut`), without
|
|
332
|
+
* the pairs and the array. They do the same for `map(Object.fromEntries)`.
|
|
333
|
+
*
|
|
334
|
+
* @example
|
|
335
|
+
* ```ts
|
|
336
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
337
|
+
*
|
|
338
|
+
* const entry = Parser.separatedPair(Ascii.alpha1, Token.literal("="), Ascii.decUint)
|
|
339
|
+
* const record = Parser.fromEntries(Parser.separated(entry, Token.literal(",")))
|
|
340
|
+
* Parser.parse(record, "a=1,b=2") // Result.succeed({ a: 1, b: 2 })
|
|
341
|
+
* ```
|
|
342
|
+
*
|
|
343
|
+
* @since 0.2.0
|
|
344
|
+
* @category mapping
|
|
345
|
+
*/
|
|
346
|
+
export declare const fromEntries: <K extends PropertyKey, V, E, R, I extends Input>(self: Parser<Iterable<readonly [K, V]>, E, R, I>) => Parser<{ [P in K]: V; }, E, R, I>;
|
|
347
|
+
/**
|
|
348
|
+
* Replaces the output of a parser with a constant (winnow's `value`, nom's
|
|
349
|
+
* `value`).
|
|
350
|
+
*
|
|
351
|
+
* @since 0.1.0
|
|
352
|
+
* @category mapping
|
|
353
|
+
*/
|
|
354
|
+
export declare const as: {
|
|
355
|
+
<B>(value: B): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E, R, I>;
|
|
356
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, value: B): Parser<B, E, R, I>;
|
|
357
|
+
};
|
|
358
|
+
/**
|
|
359
|
+
* Discards the output of a parser (winnow's `void`).
|
|
360
|
+
*
|
|
361
|
+
* @since 0.1.0
|
|
362
|
+
* @category mapping
|
|
363
|
+
*/
|
|
364
|
+
export declare const asVoid: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<void, E, R, I>;
|
|
365
|
+
/**
|
|
366
|
+
* Replaces the output of a parser with `Option.some(output)`.
|
|
367
|
+
*
|
|
368
|
+
* @since 0.1.0
|
|
369
|
+
* @category mapping
|
|
370
|
+
*/
|
|
371
|
+
export declare const asSome: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Option.Option<A>, E, R, I>;
|
|
372
|
+
/**
|
|
373
|
+
* Runs a side effect on the output of a parser, keeping the output.
|
|
374
|
+
*
|
|
375
|
+
* @since 0.1.0
|
|
376
|
+
* @category mapping
|
|
377
|
+
*/
|
|
378
|
+
export declare const tap: {
|
|
379
|
+
<A>(f: (a: A) => void): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
380
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, f: (a: A) => void): Parser<A, E, R, I>;
|
|
381
|
+
};
|
|
382
|
+
/**
|
|
383
|
+
* Uses the output of a parser to choose the parser that consumes the rest
|
|
384
|
+
* of the input (winnow's `flat_map`, nom's `flat_map`).
|
|
385
|
+
*
|
|
386
|
+
* @since 0.1.0
|
|
387
|
+
* @category sequencing
|
|
388
|
+
*/
|
|
389
|
+
export declare const flatMap: {
|
|
390
|
+
<A, B, E2, R2, I2 extends Input>(f: (a: A) => Parser<B, E2, R2, I2>): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | E2, R | R2, I & I2>;
|
|
391
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, f: (a: A) => Parser<B, E2, R2, I2>): Parser<B, E | E2, R | R2, I & I2>;
|
|
392
|
+
};
|
|
393
|
+
/**
|
|
394
|
+
* Applies a second parser to the *output* of the first (winnow's
|
|
395
|
+
* `and_then`, nom's `map_parser`). The output of the first parser must
|
|
396
|
+
* itself be an input, e.g. a slice produced by {@link taken} or
|
|
397
|
+
* `Token.take`.
|
|
398
|
+
*
|
|
399
|
+
* Error offsets reported by the inner parser are translated relative to the
|
|
400
|
+
* start of the outer parser.
|
|
401
|
+
*
|
|
402
|
+
* @example
|
|
403
|
+
* ```ts
|
|
404
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
405
|
+
*
|
|
406
|
+
* // Take exactly 4 characters, then require them all to be digits
|
|
407
|
+
* const year = Token.take(4).pipe(Parser.mapParser(Parser.allConsuming(Ascii.decUint)))
|
|
408
|
+
* ```
|
|
409
|
+
*
|
|
410
|
+
* @since 0.1.0
|
|
411
|
+
* @category sequencing
|
|
412
|
+
*/
|
|
413
|
+
export declare const mapParser: {
|
|
414
|
+
<A extends Input, B, E2, R2>(inner: Parser<B, E2, R2, A>): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | E2, R | R2, I>;
|
|
415
|
+
<A extends Input, E, R, I extends Input, B, E2, R2>(self: Parser<A, E, R, I>, inner: Parser<B, E2, R2, A>): Parser<B, E | E2, R | R2, I>;
|
|
416
|
+
};
|
|
417
|
+
/**
|
|
418
|
+
* Transforms the output of a parser with a function that may fail with a
|
|
419
|
+
* `Result` (winnow's `try_map`, nom's `map_res`). On failure the parser
|
|
420
|
+
* backtracks to where it started and the error becomes the `cause` of the
|
|
421
|
+
* `ParseError`.
|
|
422
|
+
*
|
|
423
|
+
* @since 0.1.0
|
|
424
|
+
* @category mapping
|
|
425
|
+
*/
|
|
426
|
+
export declare const tryMap: {
|
|
427
|
+
<A, B, E2>(f: (a: A) => Result.Result<B, E2>): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | E2, R, I>;
|
|
428
|
+
<A, E, R, I extends Input, B, E2>(self: Parser<A, E, R, I>, f: (a: A) => Result.Result<B, E2>): Parser<B, E | E2, R, I>;
|
|
429
|
+
};
|
|
430
|
+
/**
|
|
431
|
+
* Like {@link tryMap}, but the function may throw. Thrown values become the
|
|
432
|
+
* `cause` of the error.
|
|
433
|
+
*
|
|
434
|
+
* @since 0.1.0
|
|
435
|
+
* @category mapping
|
|
436
|
+
*/
|
|
437
|
+
export declare const tryMapUnknown: {
|
|
438
|
+
<A, B>(f: (a: A) => B): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | unknown, R, I>;
|
|
439
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, f: (a: A) => B): Parser<B, E | unknown, R, I>;
|
|
440
|
+
};
|
|
441
|
+
/**
|
|
442
|
+
* Transforms the output of a parser with a function returning an `Option`;
|
|
443
|
+
* `None` makes the parser backtrack (winnow's `verify_map`, nom's
|
|
444
|
+
* `map_opt`).
|
|
445
|
+
*
|
|
446
|
+
* @since 0.1.0
|
|
447
|
+
* @category filtering
|
|
448
|
+
*/
|
|
449
|
+
export declare const filterMap: {
|
|
450
|
+
<A, B>(f: (a: A) => Option.Option<B>): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E, R, I>;
|
|
451
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, f: (a: A) => Option.Option<B>): Parser<B, E, R, I>;
|
|
452
|
+
};
|
|
453
|
+
/**
|
|
454
|
+
* Keeps the output of a parser only if it satisfies a predicate, otherwise
|
|
455
|
+
* backtracks (winnow's and nom's `verify`).
|
|
456
|
+
*
|
|
457
|
+
* @since 0.1.0
|
|
458
|
+
* @category filtering
|
|
459
|
+
*/
|
|
460
|
+
export declare const filter: {
|
|
461
|
+
<A, B extends A>(refinement: (a: NoInfer<A>) => a is B): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E, R, I>;
|
|
462
|
+
<A>(predicate: (a: NoInfer<A>) => boolean): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
463
|
+
<A, E, R, I extends Input, B extends A>(self: Parser<A, E, R, I>, refinement: (a: A) => a is B): Parser<B, E, R, I>;
|
|
464
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, predicate: (a: A) => boolean): Parser<A, E, R, I>;
|
|
465
|
+
};
|
|
466
|
+
/**
|
|
467
|
+
* Keeps the output of a parser only if it satisfies a predicate, otherwise
|
|
468
|
+
* backtracks with the given user error.
|
|
469
|
+
*
|
|
470
|
+
* @since 0.1.0
|
|
471
|
+
* @category filtering
|
|
472
|
+
*/
|
|
473
|
+
export declare const filterOrFail: {
|
|
474
|
+
<A, B extends A, E2>(refinement: (a: NoInfer<A>) => a is B, orFailWith: (a: NoInfer<A>) => E2): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | E2, R, I>;
|
|
475
|
+
<A, E2>(predicate: (a: NoInfer<A>) => boolean, orFailWith: (a: NoInfer<A>) => E2): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E | E2, R, I>;
|
|
476
|
+
<A, E, R, I extends Input, B extends A, E2>(self: Parser<A, E, R, I>, refinement: (a: A) => a is B, orFailWith: (a: A) => E2): Parser<B, E | E2, R, I>;
|
|
477
|
+
<A, E, R, I extends Input, E2>(self: Parser<A, E, R, I>, predicate: (a: A) => boolean, orFailWith: (a: A) => E2): Parser<A, E | E2, R, I>;
|
|
478
|
+
};
|
|
479
|
+
/**
|
|
480
|
+
* Returns the slice of input consumed by a parser instead of its output
|
|
481
|
+
* (winnow's `take`, nom's `recognize`).
|
|
482
|
+
*
|
|
483
|
+
* @since 0.1.0
|
|
484
|
+
* @category consumed input
|
|
485
|
+
*/
|
|
486
|
+
export declare const taken: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Slice<I>, E, R, I>;
|
|
487
|
+
/**
|
|
488
|
+
* Returns both the output of a parser and the slice of input it consumed
|
|
489
|
+
* (winnow's `with_taken`; nom's `consumed` returns them in the other order).
|
|
490
|
+
*
|
|
491
|
+
* @since 0.1.0
|
|
492
|
+
* @category consumed input
|
|
493
|
+
*/
|
|
494
|
+
export declare const withTaken: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<[output: A, taken: Slice<I>], E, R, I>;
|
|
495
|
+
/**
|
|
496
|
+
* Returns the span (`[start, end)` offsets) consumed by a parser instead of
|
|
497
|
+
* its output (winnow's `span`).
|
|
498
|
+
*
|
|
499
|
+
* @since 0.1.0
|
|
500
|
+
* @category consumed input
|
|
501
|
+
*/
|
|
502
|
+
export declare const span: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Span, E, R, I>;
|
|
503
|
+
/**
|
|
504
|
+
* Returns both the output of a parser and the span it consumed (winnow's
|
|
505
|
+
* `with_span`).
|
|
506
|
+
*
|
|
507
|
+
* @since 0.1.0
|
|
508
|
+
* @category consumed input
|
|
509
|
+
*/
|
|
510
|
+
export declare const withSpan: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<[output: A, span: Span], E, R, I>;
|
|
511
|
+
/**
|
|
512
|
+
* Commits to the current branch: recoverable failures of the parser become
|
|
513
|
+
* unrecoverable, so enclosing `alt`, `opt`, `repeat`, ... do not try
|
|
514
|
+
* alternatives (winnow's `cut_err`, nom's `cut`).
|
|
515
|
+
*
|
|
516
|
+
* @example
|
|
517
|
+
* ```ts
|
|
518
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
519
|
+
*
|
|
520
|
+
* // Once we have seen "-", a number must follow
|
|
521
|
+
* const negative = Parser.preceded(Token.literal("-"), Parser.cut(Ascii.decUint))
|
|
522
|
+
* ```
|
|
523
|
+
*
|
|
524
|
+
* @since 0.1.0
|
|
525
|
+
* @category error management
|
|
526
|
+
*/
|
|
527
|
+
export declare const cut: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
528
|
+
/**
|
|
529
|
+
* Turns unrecoverable failures back into recoverable ones (winnow's
|
|
530
|
+
* `backtrack_err`).
|
|
531
|
+
*
|
|
532
|
+
* @since 0.1.0
|
|
533
|
+
* @category error management
|
|
534
|
+
*/
|
|
535
|
+
export declare const backtrack: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
536
|
+
/**
|
|
537
|
+
* Turns `Incomplete` failures into recoverable ones, treating the input as
|
|
538
|
+
* complete for this parser (winnow's `complete_err`, nom's `complete`).
|
|
539
|
+
*
|
|
540
|
+
* @since 0.1.0
|
|
541
|
+
* @category error management
|
|
542
|
+
*/
|
|
543
|
+
export declare const complete: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
544
|
+
/**
|
|
545
|
+
* Adds a context label to failures of a parser (winnow's
|
|
546
|
+
* `context(StrContext::Label(..))`, nom's `context`). Labels are rendered as
|
|
547
|
+
* "while parsing <label>" in the error message.
|
|
548
|
+
*
|
|
549
|
+
* @since 0.1.0
|
|
550
|
+
* @category error management
|
|
551
|
+
*/
|
|
552
|
+
export declare const context: {
|
|
553
|
+
(label: string): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
554
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, label: string): Parser<A, E, R, I>;
|
|
555
|
+
};
|
|
556
|
+
/**
|
|
557
|
+
* Names what a parser expects. When the parser fails without consuming any
|
|
558
|
+
* input, its expectations are replaced with `description`, so a failure is
|
|
559
|
+
* reported as `expected <description>` rather than in terms of the parser's
|
|
560
|
+
* internals (Parsec's `<?>`, winnow's `StrContext::Expected`).
|
|
561
|
+
*
|
|
562
|
+
* @since 0.1.0
|
|
563
|
+
* @category error management
|
|
564
|
+
*/
|
|
565
|
+
export declare const expected: {
|
|
566
|
+
(description: string): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
567
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, description: string): Parser<A, E, R, I>;
|
|
568
|
+
};
|
|
569
|
+
/**
|
|
570
|
+
* Transforms the user error (cause) of a parser's failures.
|
|
571
|
+
*
|
|
572
|
+
* @since 0.1.0
|
|
573
|
+
* @category error management
|
|
574
|
+
*/
|
|
575
|
+
export declare const mapError: {
|
|
576
|
+
<E, E2>(f: (e: E) => E2): <A, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E2, R, I>;
|
|
577
|
+
<A, E, R, I extends Input, E2>(self: Parser<A, E, R, I>, f: (e: E) => E2): Parser<A, E2, R, I>;
|
|
578
|
+
};
|
|
579
|
+
/**
|
|
580
|
+
* Replaces any failure of a parser with the given user error, keeping the
|
|
581
|
+
* failure's position and mode.
|
|
582
|
+
*
|
|
583
|
+
* @since 0.1.0
|
|
584
|
+
* @category error management
|
|
585
|
+
*/
|
|
586
|
+
export declare const orFail: {
|
|
587
|
+
<E2>(error: () => E2): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E2, R, I>;
|
|
588
|
+
<A, E, R, I extends Input, E2>(self: Parser<A, E, R, I>, error: () => E2): Parser<A, E2, R, I>;
|
|
589
|
+
};
|
|
590
|
+
/**
|
|
591
|
+
* If the parser fails recoverably, tries `that` from the same position.
|
|
592
|
+
* `p.pipe(Parser.orElse(() => q))` is equivalent to `Parser.alt(p, q)`.
|
|
593
|
+
*
|
|
594
|
+
* @since 0.1.0
|
|
595
|
+
* @category error management
|
|
596
|
+
*/
|
|
597
|
+
export declare const orElse: {
|
|
598
|
+
<B, E2, R2, I2 extends Input>(that: () => Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A | B, E | E2, R | R2, I & I2>;
|
|
599
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, that: () => Parser<B, E2, R2, I2>): Parser<A | B, E | E2, R | R2, I & I2>;
|
|
600
|
+
};
|
|
601
|
+
/**
|
|
602
|
+
* If the parser fails recoverably, succeeds with the given value without
|
|
603
|
+
* consuming input.
|
|
604
|
+
*
|
|
605
|
+
* @since 0.1.0
|
|
606
|
+
* @category error management
|
|
607
|
+
*/
|
|
608
|
+
export declare const orElseSucceed: {
|
|
609
|
+
<B>(value: () => B): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A | B, E, R, I>;
|
|
610
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, value: () => B): Parser<A | B, E, R, I>;
|
|
611
|
+
};
|
|
612
|
+
/**
|
|
613
|
+
* Makes a parser optional: returns `Option.none()` without consuming input if
|
|
614
|
+
* it fails recoverably (winnow's and nom's `opt`).
|
|
615
|
+
*
|
|
616
|
+
* @since 0.1.0
|
|
617
|
+
* @category control
|
|
618
|
+
*/
|
|
619
|
+
export declare const opt: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Option.Option<A>, E, R, I>;
|
|
620
|
+
/**
|
|
621
|
+
* Like {@link opt}, but returns `undefined` instead of `None`.
|
|
622
|
+
*
|
|
623
|
+
* @since 0.1.0
|
|
624
|
+
* @category control
|
|
625
|
+
*/
|
|
626
|
+
export declare const optional: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A | undefined, E, R, I>;
|
|
627
|
+
/**
|
|
628
|
+
* Runs the parser only if `condition` holds, otherwise succeeds with
|
|
629
|
+
* `Option.none()` (winnow's and nom's `cond`).
|
|
630
|
+
*
|
|
631
|
+
* @since 0.1.0
|
|
632
|
+
* @category control
|
|
633
|
+
*/
|
|
634
|
+
export declare const cond: <A, E, R, I extends Input>(condition: boolean, self: Parser<A, E, R, I>) => Parser<Option.Option<A>, E, R, I>;
|
|
635
|
+
/**
|
|
636
|
+
* Runs a parser without consuming input (winnow's and nom's `peek`).
|
|
637
|
+
*
|
|
638
|
+
* @since 0.1.0
|
|
639
|
+
* @category control
|
|
640
|
+
*/
|
|
641
|
+
export declare const peek: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
642
|
+
/**
|
|
643
|
+
* Succeeds, without consuming input, only if the parser fails recoverably
|
|
644
|
+
* (winnow's and nom's `not`).
|
|
645
|
+
*
|
|
646
|
+
* @since 0.1.0
|
|
647
|
+
* @category control
|
|
648
|
+
*/
|
|
649
|
+
export declare const not: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<void, E, R, I>;
|
|
650
|
+
/**
|
|
651
|
+
* Requires the parser to consume the whole input (nom's `all_consuming`).
|
|
652
|
+
*
|
|
653
|
+
* @since 0.1.0
|
|
654
|
+
* @category control
|
|
655
|
+
*/
|
|
656
|
+
export declare const allConsuming: <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
657
|
+
/**
|
|
658
|
+
* Runs parsers in sequence, collecting their outputs in a tuple (winnow's
|
|
659
|
+
* and nom's tuple parsers, winnow's `seq!`).
|
|
660
|
+
*
|
|
661
|
+
* @since 0.1.0
|
|
662
|
+
* @category sequencing
|
|
663
|
+
*/
|
|
664
|
+
export declare const seq: <const Ps extends ReadonlyArray<Parser.Any>>(...parsers: Ps) => Parser<{ -readonly [K in keyof Ps]: Parser.Success<Ps[K]>; }, Parser.Error<Ps[number]>, Parser.Services<Ps[number]>, Parser.InputOf<Ps[number]>>;
|
|
665
|
+
/**
|
|
666
|
+
* Runs a record of parsers in sequence (in key order), collecting their
|
|
667
|
+
* outputs in a record. This is the Effect counterpart to winnow's
|
|
668
|
+
* `seq!(Struct { .. })`.
|
|
669
|
+
*
|
|
670
|
+
* @since 0.1.0
|
|
671
|
+
* @category sequencing
|
|
672
|
+
*/
|
|
673
|
+
export declare const struct: <const Fields extends Readonly<Record<string, Parser.Any>>>(fields: Fields) => Parser<Simplify<{ -readonly [K in keyof Fields]: Parser.Success<Fields[K]>; }>, Parser.Error<Fields[keyof Fields]>, Parser.Services<Fields[keyof Fields]>, Parser.InputOf<Fields[keyof Fields]>>;
|
|
674
|
+
/**
|
|
675
|
+
* Runs two parsers in sequence and returns both outputs.
|
|
676
|
+
*
|
|
677
|
+
* @since 0.1.0
|
|
678
|
+
* @category sequencing
|
|
679
|
+
*/
|
|
680
|
+
export declare const zip: {
|
|
681
|
+
<B, E2, R2, I2 extends Input>(that: Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<[A, B], E | E2, R | R2, I & I2>;
|
|
682
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, that: Parser<B, E2, R2, I2>): Parser<[A, B], E | E2, R | R2, I & I2>;
|
|
683
|
+
};
|
|
684
|
+
/**
|
|
685
|
+
* Runs two parsers in sequence and combines their outputs.
|
|
686
|
+
*
|
|
687
|
+
* @since 0.1.0
|
|
688
|
+
* @category sequencing
|
|
689
|
+
*/
|
|
690
|
+
export declare const zipWith: {
|
|
691
|
+
<B, E2, R2, I2 extends Input, A, C>(that: Parser<B, E2, R2, I2>, f: (a: A, b: B) => C): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<C, E | E2, R | R2, I & I2>;
|
|
692
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input, C>(self: Parser<A, E, R, I>, that: Parser<B, E2, R2, I2>, f: (a: A, b: B) => C): Parser<C, E | E2, R | R2, I & I2>;
|
|
693
|
+
};
|
|
694
|
+
/**
|
|
695
|
+
* Runs two parsers in sequence, keeping the output of the first.
|
|
696
|
+
*
|
|
697
|
+
* @since 0.1.0
|
|
698
|
+
* @category sequencing
|
|
699
|
+
*/
|
|
700
|
+
export declare const zipLeft: {
|
|
701
|
+
<B, E2, R2, I2 extends Input>(that: Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E | E2, R | R2, I & I2>;
|
|
702
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, that: Parser<B, E2, R2, I2>): Parser<A, E | E2, R | R2, I & I2>;
|
|
703
|
+
};
|
|
704
|
+
/**
|
|
705
|
+
* Runs two parsers in sequence, keeping the output of the second.
|
|
706
|
+
*
|
|
707
|
+
* @since 0.1.0
|
|
708
|
+
* @category sequencing
|
|
709
|
+
*/
|
|
710
|
+
export declare const zipRight: {
|
|
711
|
+
<B, E2, R2, I2 extends Input>(that: Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E | E2, R | R2, I & I2>;
|
|
712
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, that: Parser<B, E2, R2, I2>): Parser<B, E | E2, R | R2, I & I2>;
|
|
713
|
+
};
|
|
714
|
+
/**
|
|
715
|
+
* Runs `prefix` then `parser`, keeping the output of `parser` (winnow's and
|
|
716
|
+
* nom's `preceded`).
|
|
717
|
+
*
|
|
718
|
+
* @since 0.1.0
|
|
719
|
+
* @category sequencing
|
|
720
|
+
*/
|
|
721
|
+
export declare const preceded: <A1, E1, R1, I1 extends Input, A2, E2, R2, I2 extends Input>(prefix: Parser<A1, E1, R1, I1>, parser: Parser<A2, E2, R2, I2>) => Parser<A2, E1 | E2, R1 | R2, I1 & I2>;
|
|
722
|
+
/**
|
|
723
|
+
* Runs `parser` then `suffix`, keeping the output of `parser` (winnow's and
|
|
724
|
+
* nom's `terminated`).
|
|
725
|
+
*
|
|
726
|
+
* @since 0.1.0
|
|
727
|
+
* @category sequencing
|
|
728
|
+
*/
|
|
729
|
+
export declare const terminated: <A1, E1, R1, I1 extends Input, A2, E2, R2, I2 extends Input>(parser: Parser<A1, E1, R1, I1>, suffix: Parser<A2, E2, R2, I2>) => Parser<A1, E1 | E2, R1 | R2, I1 & I2>;
|
|
730
|
+
/**
|
|
731
|
+
* Runs `open`, `parser` and `close`, keeping the output of `parser`
|
|
732
|
+
* (winnow's and nom's `delimited`).
|
|
733
|
+
*
|
|
734
|
+
* @since 0.1.0
|
|
735
|
+
* @category sequencing
|
|
736
|
+
*/
|
|
737
|
+
export declare const delimited: <A1, E1, R1, I1 extends Input, A2, E2, R2, I2 extends Input, A3, E3, R3, I3 extends Input>(open: Parser<A1, E1, R1, I1>, parser: Parser<A2, E2, R2, I2>, close: Parser<A3, E3, R3, I3>) => Parser<A2, E1 | E2 | E3, R1 | R2 | R3, I1 & I2 & I3>;
|
|
738
|
+
/**
|
|
739
|
+
* Runs `first`, `separator` and `second`, keeping the outputs of `first` and
|
|
740
|
+
* `second` (winnow's and nom's `separated_pair`).
|
|
741
|
+
*
|
|
742
|
+
* @since 0.1.0
|
|
743
|
+
* @category sequencing
|
|
744
|
+
*/
|
|
745
|
+
export declare const separatedPair: <A1, E1, R1, I1 extends Input, A2, E2, R2, I2 extends Input, A3, E3, R3, I3 extends Input>(first: Parser<A1, E1, R1, I1>, separator: Parser<A2, E2, R2, I2>, second: Parser<A3, E3, R3, I3>) => Parser<[A1, A3], E1 | E2 | E3, R1 | R2 | R3, I1 & I2 & I3>;
|
|
746
|
+
/**
|
|
747
|
+
* Writes a parser in imperative style with a generator, Effect style:
|
|
748
|
+
* `yield*` a parser to run it and get its output.
|
|
749
|
+
*
|
|
750
|
+
* @example
|
|
751
|
+
* ```ts
|
|
752
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
753
|
+
*
|
|
754
|
+
* const keyValue = Parser.gen(function*() {
|
|
755
|
+
* const key = yield* Ascii.alpha1
|
|
756
|
+
* yield* Token.literal("=")
|
|
757
|
+
* const value = yield* Ascii.decInt
|
|
758
|
+
* return [key, value] as const
|
|
759
|
+
* })
|
|
760
|
+
* ```
|
|
761
|
+
*
|
|
762
|
+
* @since 0.1.0
|
|
763
|
+
* @category sequencing
|
|
764
|
+
*/
|
|
765
|
+
export declare const gen: <Eff extends Parser.Any, AEff>(f: () => Generator<Eff, AEff, never>) => Parser<AEff, Parser.Error<Eff>, Parser.Services<Eff>, Parser.InputOf<Eff>>;
|
|
766
|
+
/**
|
|
767
|
+
* The starting point for Do notation.
|
|
768
|
+
*
|
|
769
|
+
* @example
|
|
770
|
+
* ```ts
|
|
771
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
772
|
+
*
|
|
773
|
+
* const range = Parser.Do.pipe(
|
|
774
|
+
* Parser.bind("start", () => Ascii.decUint),
|
|
775
|
+
* Parser.zipLeft(Token.literal("..")),
|
|
776
|
+
* Parser.bind("end", () => Ascii.decUint)
|
|
777
|
+
* )
|
|
778
|
+
* ```
|
|
779
|
+
*
|
|
780
|
+
* @since 0.1.0
|
|
781
|
+
* @category do notation
|
|
782
|
+
*/
|
|
783
|
+
export declare const Do: Parser<{}, never, never, Input>;
|
|
784
|
+
/**
|
|
785
|
+
* Binds the output of a parser to a field of the Do record.
|
|
786
|
+
*
|
|
787
|
+
* @since 0.1.0
|
|
788
|
+
* @category do notation
|
|
789
|
+
*/
|
|
790
|
+
export declare const bind: {
|
|
791
|
+
<N extends string, A extends object, B, E2, R2, I2 extends Input>(name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => Parser<B, E2, R2, I2>): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Simplify<Omit<A, N> & {
|
|
792
|
+
[K in N]: B;
|
|
793
|
+
}>, E | E2, R | R2, I & I2>;
|
|
794
|
+
<A extends object, E, R, I extends Input, N extends string, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => Parser<B, E2, R2, I2>): Parser<Simplify<Omit<A, N> & {
|
|
795
|
+
[K in N]: B;
|
|
796
|
+
}>, E | E2, R | R2, I & I2>;
|
|
797
|
+
};
|
|
798
|
+
/**
|
|
799
|
+
* Wraps the output of a parser in a record with a single field.
|
|
800
|
+
*
|
|
801
|
+
* @since 0.1.0
|
|
802
|
+
* @category do notation
|
|
803
|
+
*/
|
|
804
|
+
export declare const bindTo: {
|
|
805
|
+
<N extends string>(name: N): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<{
|
|
806
|
+
[K in N]: A;
|
|
807
|
+
}, E, R, I>;
|
|
808
|
+
<A, E, R, I extends Input, N extends string>(self: Parser<A, E, R, I>, name: N): Parser<{
|
|
809
|
+
[K in N]: A;
|
|
810
|
+
}, E, R, I>;
|
|
811
|
+
};
|
|
812
|
+
declare const let_: {
|
|
813
|
+
<N extends string, A extends object, B>(name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => B): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Simplify<Omit<A, N> & {
|
|
814
|
+
[K in N]: B;
|
|
815
|
+
}>, E, R, I>;
|
|
816
|
+
<A extends object, E, R, I extends Input, N extends string, B>(self: Parser<A, E, R, I>, name: Exclude<N, keyof A>, f: (a: NoInfer<A>) => B): Parser<Simplify<Omit<A, N> & {
|
|
817
|
+
[K in N]: B;
|
|
818
|
+
}>, E, R, I>;
|
|
819
|
+
};
|
|
820
|
+
export {
|
|
821
|
+
/**
|
|
822
|
+
* Adds a computed field to the Do record.
|
|
823
|
+
*
|
|
824
|
+
* @since 0.1.0
|
|
825
|
+
* @category do notation
|
|
826
|
+
*/
|
|
827
|
+
let_ as let };
|
|
828
|
+
/**
|
|
829
|
+
* Tries each parser in turn and returns the output of the first that
|
|
830
|
+
* succeeds (winnow's and nom's `alt`).
|
|
831
|
+
*
|
|
832
|
+
* Unrecoverable (`Cut`) and `Incomplete` failures stop the search
|
|
833
|
+
* immediately. If every alternative fails recoverably, the failures are
|
|
834
|
+
* merged: the one that got furthest wins, and failures at the same offset
|
|
835
|
+
* combine their expectations, e.g. `expected "true", "false" or "null"`.
|
|
836
|
+
*
|
|
837
|
+
* @since 0.1.0
|
|
838
|
+
* @category choice
|
|
839
|
+
*/
|
|
840
|
+
export declare const alt: <const Ps extends ReadonlyArray<Parser.Any>>(...parsers: Ps) => Parser<Parser.Success<Ps[number]>, Parser.Error<Ps[number]>, Parser.Services<Ps[number]>, Parser.InputOf<Ps[number]>>;
|
|
841
|
+
/**
|
|
842
|
+
* Selects the parser to run next based on the output of `selector`, like a
|
|
843
|
+
* `match` over the input (winnow's `dispatch!`). If no case matches, the
|
|
844
|
+
* parser backtracks to where it started.
|
|
845
|
+
*
|
|
846
|
+
* Cases are given either as a record keyed by the selector's output (with
|
|
847
|
+
* an optional `otherwise` parser for unlisted keys, like winnow's `_ =>`
|
|
848
|
+
* arm), or as a function returning a parser (or `undefined` for no match).
|
|
849
|
+
*
|
|
850
|
+
* @example
|
|
851
|
+
* ```ts
|
|
852
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
853
|
+
*
|
|
854
|
+
* const escape = Parser.preceded(
|
|
855
|
+
* Token.literal("\\"),
|
|
856
|
+
* Parser.dispatch(Token.any, {
|
|
857
|
+
* n: Parser.succeed("\n"),
|
|
858
|
+
* t: Parser.succeed("\t"),
|
|
859
|
+
* u: Parser.preceded(Token.literal("{"), Ascii.hexUint).pipe(
|
|
860
|
+
* Parser.zipLeft(Token.literal("}")),
|
|
861
|
+
* Parser.map((cp) => String.fromCodePoint(cp))
|
|
862
|
+
* )
|
|
863
|
+
* })
|
|
864
|
+
* )
|
|
865
|
+
* ```
|
|
866
|
+
*
|
|
867
|
+
* @since 0.1.0
|
|
868
|
+
* @category choice
|
|
869
|
+
*/
|
|
870
|
+
export declare const dispatch: {
|
|
871
|
+
<K, E, R, I extends Input, P extends Parser.Any>(selector: Parser<K, E, R, I>, cases: (key: K) => P | undefined): Parser<Parser.Success<P>, E | Parser.Error<P>, R | Parser.Services<P>, I & Parser.InputOf<P>>;
|
|
872
|
+
<K extends PropertyKey, E, R, I extends Input, const Cases extends {
|
|
873
|
+
readonly [key in K]?: Parser.Any;
|
|
874
|
+
}, Otherwise extends Parser.Any = never>(selector: Parser<K, E, R, I>, cases: Cases, otherwise?: Otherwise): Parser<Parser.Success<NonNullable<Cases[keyof Cases]> | Otherwise>, E | Parser.Error<NonNullable<Cases[keyof Cases]> | Otherwise>, R | Parser.Services<NonNullable<Cases[keyof Cases]> | Otherwise>, I & Parser.InputOf<NonNullable<Cases[keyof Cases]> | Otherwise>>;
|
|
875
|
+
};
|
|
876
|
+
/**
|
|
877
|
+
* Applies every parser exactly once, in whatever order they match, and
|
|
878
|
+
* returns their outputs in the order the parsers were given (winnow's and
|
|
879
|
+
* nom's `permutation`).
|
|
880
|
+
*
|
|
881
|
+
* @since 0.1.0
|
|
882
|
+
* @category choice
|
|
883
|
+
*/
|
|
884
|
+
export declare const permutation: <const Ps extends ReadonlyArray<Parser.Any>>(...parsers: Ps) => Parser<{ -readonly [K in keyof Ps]: Parser.Success<Ps[K]>; }, Parser.Error<Ps[number]>, Parser.Services<Ps[number]>, Parser.InputOf<Ps[number]>>;
|
|
885
|
+
/**
|
|
886
|
+
* Applies a parser repeatedly, collecting the outputs in an array (winnow's
|
|
887
|
+
* `repeat`, nom's `many0`, `many1`, `many_m_n` and `count`).
|
|
888
|
+
*
|
|
889
|
+
* The range defaults to `{ min: 0 }` (zero or more). A number repeats
|
|
890
|
+
* exactly that many times. With a literal positive `min`, the output is
|
|
891
|
+
* typed as a non-empty array.
|
|
892
|
+
*
|
|
893
|
+
* As in winnow, an unbounded repetition of a parser that succeeds without
|
|
894
|
+
* consuming input fails with an unrecoverable assertion instead of looping
|
|
895
|
+
* forever.
|
|
896
|
+
*
|
|
897
|
+
* @example
|
|
898
|
+
* ```ts
|
|
899
|
+
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
900
|
+
*
|
|
901
|
+
* const abs = Parser.repeat(Token.literal("ab"), { min: 1 })
|
|
902
|
+
* Parser.parse(abs, "ababab") // Result.succeed(["ab", "ab", "ab"])
|
|
903
|
+
* ```
|
|
904
|
+
*
|
|
905
|
+
* @since 0.1.0
|
|
906
|
+
* @category repetition
|
|
907
|
+
*/
|
|
908
|
+
export declare const repeat: {
|
|
909
|
+
<const Rg extends Range = {
|
|
910
|
+
readonly min: 0;
|
|
911
|
+
}>(range?: Rg): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Repeated<A, Rg>, E, R, I>;
|
|
912
|
+
<A, E, R, I extends Input, const Rg extends Range = {
|
|
913
|
+
readonly min: 0;
|
|
914
|
+
}>(self: Parser<A, E, R, I>, range?: Rg): Parser<Repeated<A, Rg>, E, R, I>;
|
|
915
|
+
};
|
|
916
|
+
/**
|
|
917
|
+
* Applies a parser repeatedly, folding the outputs into an accumulator
|
|
918
|
+
* (winnow's `repeat(..).fold(..)`, nom's `fold_many0`/`fold_many1`/
|
|
919
|
+
* `fold_many_m_n`).
|
|
920
|
+
*
|
|
921
|
+
* @since 0.1.0
|
|
922
|
+
* @category repetition
|
|
923
|
+
*/
|
|
924
|
+
export declare const fold: {
|
|
925
|
+
<A, B>(options: {
|
|
926
|
+
readonly init: () => B;
|
|
927
|
+
readonly f: (acc: B, a: A) => B;
|
|
928
|
+
readonly range?: Range | undefined;
|
|
929
|
+
}): <E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<B, E, R, I>;
|
|
930
|
+
<A, E, R, I extends Input, B>(self: Parser<A, E, R, I>, options: {
|
|
931
|
+
readonly init: () => B;
|
|
932
|
+
readonly f: (acc: B, a: A) => B;
|
|
933
|
+
readonly range?: Range | undefined;
|
|
934
|
+
}): Parser<B, E, R, I>;
|
|
935
|
+
};
|
|
936
|
+
/**
|
|
937
|
+
* Applies `self` repeatedly until `end` succeeds, returning the collected
|
|
938
|
+
* outputs and the output of `end` (winnow's `repeat_till`, nom's
|
|
939
|
+
* `many_till`).
|
|
940
|
+
*
|
|
941
|
+
* @since 0.1.0
|
|
942
|
+
* @category repetition
|
|
943
|
+
*/
|
|
944
|
+
export declare const repeatTill: {
|
|
945
|
+
<B, E2, R2, I2 extends Input, const Rg extends Range = {
|
|
946
|
+
readonly min: 0;
|
|
947
|
+
}>(end: Parser<B, E2, R2, I2>, range?: Rg): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<[Repeated<A, Rg>, B], E | E2, R | R2, I & I2>;
|
|
948
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input, const Rg extends Range = {
|
|
949
|
+
readonly min: 0;
|
|
950
|
+
}>(self: Parser<A, E, R, I>, end: Parser<B, E2, R2, I2>, range?: Rg): Parser<[Repeated<A, Rg>, B], E | E2, R | R2, I & I2>;
|
|
951
|
+
};
|
|
952
|
+
export declare const separated: {
|
|
953
|
+
<S, E2, R2, I2 extends Input, const Rg extends Range = {
|
|
954
|
+
readonly min: 0;
|
|
955
|
+
}>(separator: Parser<S, E2, R2, I2>, range?: Rg): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Repeated<A, Rg>, E | E2, R | R2, I & I2>;
|
|
956
|
+
<A, E, R, I extends Input, S, E2, R2, I2 extends Input, const Rg extends Range = {
|
|
957
|
+
readonly min: 0;
|
|
958
|
+
}>(self: Parser<A, E, R, I>, separator: Parser<S, E2, R2, I2>, range?: Rg): Parser<Repeated<A, Rg>, E | E2, R | R2, I & I2>;
|
|
959
|
+
};
|
|
960
|
+
/**
|
|
961
|
+
* Parses one or more `self` separated by `separator`, folding them from the
|
|
962
|
+
* left with the separator's output: `((a op b) op c)` (winnow's
|
|
963
|
+
* `separated_foldl1`). This is the classic way to parse left-associative
|
|
964
|
+
* binary operators.
|
|
965
|
+
*
|
|
966
|
+
* @example
|
|
967
|
+
* ```ts
|
|
968
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
969
|
+
*
|
|
970
|
+
* const sum = Parser.separatedFoldl1(
|
|
971
|
+
* Ascii.decInt,
|
|
972
|
+
* Parser.alt(Token.literal("+"), Token.literal("-")),
|
|
973
|
+
* (l, op, r) => op === "+" ? l + r : l - r
|
|
974
|
+
* )
|
|
975
|
+
* Parser.parse(sum, "10-3+2") // Result.succeed(9)
|
|
976
|
+
* ```
|
|
977
|
+
*
|
|
978
|
+
* @since 0.1.0
|
|
979
|
+
* @category repetition
|
|
980
|
+
*/
|
|
981
|
+
export declare const separatedFoldl1: <A, E, R, I extends Input, S, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, separator: Parser<S, E2, R2, I2>, op: (left: A, separator: S, right: A) => A) => Parser<A, E | E2, R | R2, I & I2>;
|
|
982
|
+
/**
|
|
983
|
+
* Parses one or more `self` separated by `separator`, folding them from the
|
|
984
|
+
* right with the separator's output: `(a op (b op c))` (winnow's
|
|
985
|
+
* `separated_foldr1`). Useful for right-associative operators such as
|
|
986
|
+
* exponentiation.
|
|
987
|
+
*
|
|
988
|
+
* @since 0.1.0
|
|
989
|
+
* @category repetition
|
|
990
|
+
*/
|
|
991
|
+
export declare const separatedFoldr1: <A, E, R, I extends Input, S, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, separator: Parser<S, E2, R2, I2>, op: (left: A, separator: S, right: A) => A) => Parser<A, E | E2, R | R2, I & I2>;
|
|
992
|
+
/**
|
|
993
|
+
* Accesses a service from the parser's context. Services take the place of
|
|
994
|
+
* winnow's `Stateful` input: they can hold configuration, or mutable state
|
|
995
|
+
* such as an indentation stack or a symbol table.
|
|
996
|
+
*
|
|
997
|
+
* Services are provided with {@link provideService} / {@link provide}, with
|
|
998
|
+
* the `context` option of the runners, or taken from the surrounding Effect
|
|
999
|
+
* by {@link parseEffect}.
|
|
1000
|
+
*
|
|
1001
|
+
* Mutations made to a service's state are not undone on backtracking (as
|
|
1002
|
+
* with winnow's `Stateful`).
|
|
1003
|
+
*
|
|
1004
|
+
* @example
|
|
1005
|
+
* ```ts
|
|
1006
|
+
* import { Context } from "effect"
|
|
1007
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1008
|
+
*
|
|
1009
|
+
* class Radix extends Context.Service<Radix, number>()("Radix") {}
|
|
1010
|
+
*
|
|
1011
|
+
* const number = Parser.service(Radix).pipe(
|
|
1012
|
+
* Parser.flatMap((radix) =>
|
|
1013
|
+
* Ascii.alphanumeric1.pipe(Parser.map((digits) => parseInt(digits, radix)))
|
|
1014
|
+
* )
|
|
1015
|
+
* )
|
|
1016
|
+
*
|
|
1017
|
+
* Parser.parse(Parser.provideService(number, Radix, 16), "ff") // Result.succeed(255)
|
|
1018
|
+
* ```
|
|
1019
|
+
*
|
|
1020
|
+
* @since 0.1.0
|
|
1021
|
+
* @category services
|
|
1022
|
+
*/
|
|
1023
|
+
export declare const service: <Id, S>(key: Context.Key<Id, S>) => Parser<S, never, Id, Input>;
|
|
1024
|
+
/**
|
|
1025
|
+
* Provides a service to a parser.
|
|
1026
|
+
*
|
|
1027
|
+
* @since 0.1.0
|
|
1028
|
+
* @category services
|
|
1029
|
+
*/
|
|
1030
|
+
export declare const provideService: {
|
|
1031
|
+
<Id, S>(key: Context.Key<Id, S>, service: NoInfer<S>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, Exclude<R, Id>, I>;
|
|
1032
|
+
<A, E, R, I extends Input, Id, S>(self: Parser<A, E, R, I>, key: Context.Key<Id, S>, service: NoInfer<S>): Parser<A, E, Exclude<R, Id>, I>;
|
|
1033
|
+
};
|
|
1034
|
+
/**
|
|
1035
|
+
* Provides a context of services to a parser.
|
|
1036
|
+
*
|
|
1037
|
+
* @since 0.1.0
|
|
1038
|
+
* @category services
|
|
1039
|
+
*/
|
|
1040
|
+
export declare const provide: {
|
|
1041
|
+
<R2>(context: Context.Context<R2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, Exclude<R, R2>, I>;
|
|
1042
|
+
<A, E, R, I extends Input, R2>(self: Parser<A, E, R, I>, context: Context.Context<R2>): Parser<A, E, Exclude<R, R2>, I>;
|
|
1043
|
+
};
|
|
1044
|
+
/**
|
|
1045
|
+
* Error recovery: if the parser fails, record the failure, skip input with
|
|
1046
|
+
* `recover`, and continue with `Option.none()` (winnow's `resume_after`).
|
|
1047
|
+
*
|
|
1048
|
+
* Recovery is only active when running with {@link parseRecoverable}. Under
|
|
1049
|
+
* the other runners the parser behaves like `Parser.asSome(self)`.
|
|
1050
|
+
*
|
|
1051
|
+
* @since 0.1.0
|
|
1052
|
+
* @category error recovery
|
|
1053
|
+
*/
|
|
1054
|
+
export declare const resumeAfter: {
|
|
1055
|
+
<B, E2, R2, I2 extends Input>(recover: Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<Option.Option<A>, E | E2, R | R2, I & I2>;
|
|
1056
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, recover: Parser<B, E2, R2, I2>): Parser<Option.Option<A>, E | E2, R | R2, I & I2>;
|
|
1057
|
+
};
|
|
1058
|
+
/**
|
|
1059
|
+
* Error recovery: if the parser fails, record the failure, skip input with
|
|
1060
|
+
* `recover`, and try the parser again (winnow's `retry_after`).
|
|
1061
|
+
*
|
|
1062
|
+
* Recovery is only active when running with {@link parseRecoverable}. Under
|
|
1063
|
+
* the other runners the parser behaves like `self`.
|
|
1064
|
+
*
|
|
1065
|
+
* @since 0.1.0
|
|
1066
|
+
* @category error recovery
|
|
1067
|
+
*/
|
|
1068
|
+
export declare const retryAfter: {
|
|
1069
|
+
<B, E2, R2, I2 extends Input>(recover: Parser<B, E2, R2, I2>): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E | E2, R | R2, I & I2>;
|
|
1070
|
+
<A, E, R, I extends Input, B, E2, R2, I2 extends Input>(self: Parser<A, E, R, I>, recover: Parser<B, E2, R2, I2>): Parser<A, E | E2, R | R2, I & I2>;
|
|
1071
|
+
};
|
|
1072
|
+
/**
|
|
1073
|
+
* Traces the execution of a parser (winnow's `trace`). When a tracer is
|
|
1074
|
+
* installed (see the `trace` option of {@link parse}), `Enter` and `Exit`
|
|
1075
|
+
* events are reported for each traced parser; otherwise tracing has no
|
|
1076
|
+
* effect.
|
|
1077
|
+
*
|
|
1078
|
+
* @since 0.1.0
|
|
1079
|
+
* @category debugging
|
|
1080
|
+
*/
|
|
1081
|
+
export declare const trace: {
|
|
1082
|
+
(name: string): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
1083
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, name: string): Parser<A, E, R, I>;
|
|
1084
|
+
};
|
|
1085
|
+
/**
|
|
1086
|
+
* A tracer that writes an indented trace to `console.log`, similar to
|
|
1087
|
+
* winnow's `debug` feature.
|
|
1088
|
+
*
|
|
1089
|
+
* @since 0.1.0
|
|
1090
|
+
* @category debugging
|
|
1091
|
+
*/
|
|
1092
|
+
export declare const consoleTracer: (event: TraceEvent) => void;
|
|
1093
|
+
/**
|
|
1094
|
+
* Options for {@link compile}.
|
|
1095
|
+
*
|
|
1096
|
+
* @since 0.1.0
|
|
1097
|
+
* @category compilation
|
|
1098
|
+
*/
|
|
1099
|
+
export interface CompileOptions {
|
|
1100
|
+
/**
|
|
1101
|
+
* Which compiler to use:
|
|
1102
|
+
*
|
|
1103
|
+
* - `"auto"` (the default): generated code (a JIT) where `new Function`
|
|
1104
|
+
* is allowed, otherwise specialized closures
|
|
1105
|
+
* - `"closure"`: specialized closures only. It never generates code, so
|
|
1106
|
+
* it is the choice under a Content Security Policy, where even probing
|
|
1107
|
+
* for `new Function` is reported as a violation.
|
|
1108
|
+
*/
|
|
1109
|
+
readonly backend?: "auto" | "closure" | undefined;
|
|
1110
|
+
}
|
|
1111
|
+
/**
|
|
1112
|
+
* Compiles a parser into fused, specialized code, and returns the same
|
|
1113
|
+
* parser. Grammars are written as usual; compilation only makes
|
|
1114
|
+
* {@link parse}, {@link parseSync}, {@link parsePeek} and
|
|
1115
|
+
* {@link parseEffect} faster. Compile the root of a grammar once, where it
|
|
1116
|
+
* is defined:
|
|
1117
|
+
*
|
|
1118
|
+
* ```ts
|
|
1119
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1120
|
+
*
|
|
1121
|
+
* export const list = Parser.compile(
|
|
1122
|
+
* Parser.separated(Ascii.decInt, Token.literal(","))
|
|
1123
|
+
* )
|
|
1124
|
+
* Parser.parse(list, "1,2,3") // Result.succeed([1, 2, 3]), compiled
|
|
1125
|
+
* ```
|
|
1126
|
+
*
|
|
1127
|
+
* The code is generated lazily on the first run, for each kind of input
|
|
1128
|
+
* (string, bytes or array), and cached with the parser.
|
|
1129
|
+
*
|
|
1130
|
+
* **Results and errors are unchanged.** Compiled code records no
|
|
1131
|
+
* diagnostics. When it fails, the parser runs again, interpreted, from the
|
|
1132
|
+
* start, so every `ParseError` (offset, expected, context, cause and
|
|
1133
|
+
* message) is exactly the one the interpreter reports.
|
|
1134
|
+
*
|
|
1135
|
+
* **Callbacks should be pure.** On an input that fails, the callbacks given
|
|
1136
|
+
* to `map`, `filter`, `tryMap`, `tap`, `fold`, ... may run twice, once
|
|
1137
|
+
* compiled and once interpreted. Callbacks whose results are discarded
|
|
1138
|
+
* (a `map` inside `taken` or `preceded`, for example) may not run at all,
|
|
1139
|
+
* nor those of a branch that is certain to fail, and token-set predicates
|
|
1140
|
+
* may be called once per character code and cached.
|
|
1141
|
+
*
|
|
1142
|
+
* Runs on partial input ({@link parsePartial}, `ParserStream`), with
|
|
1143
|
+
* {@link parseRecoverable} or with the `trace` option are always
|
|
1144
|
+
* interpreted. Parsers that can't be described, such as {@link make} and
|
|
1145
|
+
* {@link gen}, run interpreted inside compiled code.
|
|
1146
|
+
*
|
|
1147
|
+
* @since 0.1.0
|
|
1148
|
+
* @category compilation
|
|
1149
|
+
*/
|
|
1150
|
+
export declare const compile: {
|
|
1151
|
+
(options?: CompileOptions): <A, E, R, I extends Input>(self: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
1152
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, options?: CompileOptions): Parser<A, E, R, I>;
|
|
1153
|
+
};
|
|
1154
|
+
/**
|
|
1155
|
+
* Options accepted by the runners.
|
|
1156
|
+
*
|
|
1157
|
+
* @since 0.1.0
|
|
1158
|
+
* @category running
|
|
1159
|
+
*/
|
|
1160
|
+
export interface ParseOptions {
|
|
1161
|
+
/**
|
|
1162
|
+
* Install a tracer for {@link trace}d parsers: `true` uses
|
|
1163
|
+
* {@link consoleTracer}.
|
|
1164
|
+
*/
|
|
1165
|
+
readonly trace?: boolean | ((event: TraceEvent) => void) | undefined;
|
|
1166
|
+
}
|
|
1167
|
+
/**
|
|
1168
|
+
* Options accepted by the runners for parsers that require services.
|
|
1169
|
+
*
|
|
1170
|
+
* @since 0.1.0
|
|
1171
|
+
* @category running
|
|
1172
|
+
*/
|
|
1173
|
+
export interface ParseOptionsWithContext<R> extends ParseOptions {
|
|
1174
|
+
readonly context: Context.Context<R>;
|
|
1175
|
+
}
|
|
1176
|
+
/**
|
|
1177
|
+
* Runs a parser against a complete input, requiring the whole input to be
|
|
1178
|
+
* consumed (winnow's `Parser::parse`).
|
|
1179
|
+
*
|
|
1180
|
+
* @example
|
|
1181
|
+
* ```ts
|
|
1182
|
+
* import { Result } from "effect"
|
|
1183
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1184
|
+
*
|
|
1185
|
+
* Parser.parse(Ascii.decInt, "-42") // Result.succeed(-42)
|
|
1186
|
+
* Result.isFailure(Parser.parse(Ascii.decInt, "42x")) // true
|
|
1187
|
+
* ```
|
|
1188
|
+
*
|
|
1189
|
+
* @since 0.1.0
|
|
1190
|
+
* @category running
|
|
1191
|
+
*/
|
|
1192
|
+
export declare const parse: {
|
|
1193
|
+
<A, E, I extends Input>(self: Parser<A, E, never, I>, input: NoInfer<Source<I>>, options?: ParseOptions): Result.Result<A, ParseError.ParseError<E>>;
|
|
1194
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options: ParseOptionsWithContext<R>): Result.Result<A, ParseError.ParseError<E>>;
|
|
1195
|
+
};
|
|
1196
|
+
/**
|
|
1197
|
+
* Like {@link parse}, but throws the `ParseError` on failure.
|
|
1198
|
+
*
|
|
1199
|
+
* @since 0.1.0
|
|
1200
|
+
* @category running
|
|
1201
|
+
*/
|
|
1202
|
+
export declare const parseSync: {
|
|
1203
|
+
<A, E, I extends Input>(self: Parser<A, E, never, I>, input: NoInfer<Source<I>>, options?: ParseOptions): A;
|
|
1204
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options: ParseOptionsWithContext<R>): A;
|
|
1205
|
+
};
|
|
1206
|
+
/**
|
|
1207
|
+
* Runs a parser against the start of an input, returning the remaining
|
|
1208
|
+
* input along with the output, nom-style (winnow's `parse_peek`, nom's
|
|
1209
|
+
* `IResult`).
|
|
1210
|
+
*
|
|
1211
|
+
* @since 0.1.0
|
|
1212
|
+
* @category running
|
|
1213
|
+
*/
|
|
1214
|
+
export declare const parsePeek: {
|
|
1215
|
+
<A, E, I extends Input>(self: Parser<A, E, never, I>, input: NoInfer<Source<I>>, options?: ParseOptions): Result.Result<[remaining: Slice<I>, output: A], ParseError.ParseError<E>>;
|
|
1216
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options: ParseOptionsWithContext<R>): Result.Result<[remaining: Slice<I>, output: A], ParseError.ParseError<E>>;
|
|
1217
|
+
};
|
|
1218
|
+
/**
|
|
1219
|
+
* The outcome of parsing a partial (streaming) input.
|
|
1220
|
+
*
|
|
1221
|
+
* - `Done`: the parser succeeded, consuming input up to `offset`.
|
|
1222
|
+
* - `Incomplete`: more input is needed (`needed` tokens, if known).
|
|
1223
|
+
*
|
|
1224
|
+
* @since 0.1.0
|
|
1225
|
+
* @category running
|
|
1226
|
+
*/
|
|
1227
|
+
export type PartialResult<A> = {
|
|
1228
|
+
readonly _tag: "Done";
|
|
1229
|
+
readonly value: A;
|
|
1230
|
+
readonly offset: number;
|
|
1231
|
+
} | {
|
|
1232
|
+
readonly _tag: "Incomplete";
|
|
1233
|
+
readonly needed: Option.Option<number>;
|
|
1234
|
+
};
|
|
1235
|
+
/**
|
|
1236
|
+
* Runs a parser against a *partial* input, i.e. a chunk of a stream that may
|
|
1237
|
+
* be followed by more data (winnow's `Partial`, nom's `streaming` parsers).
|
|
1238
|
+
* Token parsers that reach the end of the chunk report `Incomplete` instead
|
|
1239
|
+
* of failing or succeeding early.
|
|
1240
|
+
*
|
|
1241
|
+
* The parser does not have to consume the whole input; the returned
|
|
1242
|
+
* `offset` says how much was consumed.
|
|
1243
|
+
*
|
|
1244
|
+
* @example
|
|
1245
|
+
* ```ts
|
|
1246
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1247
|
+
*
|
|
1248
|
+
* Parser.parsePartial(Ascii.digit1, "123")
|
|
1249
|
+
* // Result.succeed({ _tag: "Incomplete", needed: Option.some(1) })
|
|
1250
|
+
* Parser.parsePartial(Ascii.digit1, "123;")
|
|
1251
|
+
* // Result.succeed({ _tag: "Done", value: "123", offset: 3 })
|
|
1252
|
+
* ```
|
|
1253
|
+
*
|
|
1254
|
+
* @since 0.1.0
|
|
1255
|
+
* @category running
|
|
1256
|
+
*/
|
|
1257
|
+
export declare const parsePartial: {
|
|
1258
|
+
<A, E, I extends Input>(self: Parser<A, E, never, I>, input: NoInfer<Source<I>>, options?: ParseOptions): Result.Result<PartialResult<A>, ParseError.ParseError<E>>;
|
|
1259
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options: ParseOptionsWithContext<R>): Result.Result<PartialResult<A>, ParseError.ParseError<E>>;
|
|
1260
|
+
};
|
|
1261
|
+
/**
|
|
1262
|
+
* Runs a parser against a complete input as an `Effect`. Services required
|
|
1263
|
+
* by the parser are taken from the Effect's context.
|
|
1264
|
+
*
|
|
1265
|
+
* @example
|
|
1266
|
+
* ```ts
|
|
1267
|
+
* import { Effect } from "effect"
|
|
1268
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1269
|
+
*
|
|
1270
|
+
* const program = Effect.gen(function*() {
|
|
1271
|
+
* const n = yield* Parser.parseEffect(Ascii.decInt, "42")
|
|
1272
|
+
* return n + 1
|
|
1273
|
+
* })
|
|
1274
|
+
* ```
|
|
1275
|
+
*
|
|
1276
|
+
* @since 0.1.0
|
|
1277
|
+
* @category running
|
|
1278
|
+
*/
|
|
1279
|
+
export declare const parseEffect: <A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options?: ParseOptions) => Effect.Effect<A, ParseError.ParseError<E>, R>;
|
|
1280
|
+
/**
|
|
1281
|
+
* The outcome of {@link parseRecoverable}.
|
|
1282
|
+
*
|
|
1283
|
+
* @since 0.1.0
|
|
1284
|
+
* @category running
|
|
1285
|
+
*/
|
|
1286
|
+
export interface RecoverableResult<A, E> {
|
|
1287
|
+
/** The output, if parsing ultimately succeeded. */
|
|
1288
|
+
readonly value: Option.Option<A>;
|
|
1289
|
+
/** Every error encountered: recovered ones, then the fatal one (if any). */
|
|
1290
|
+
readonly errors: ReadonlyArray<ParseError.ParseError<E>>;
|
|
1291
|
+
}
|
|
1292
|
+
/**
|
|
1293
|
+
* Runs a parser with error recovery enabled (winnow's `Recoverable`
|
|
1294
|
+
* stream). Failures handled by {@link resumeAfter} and {@link retryAfter}
|
|
1295
|
+
* are collected instead of aborting the parse.
|
|
1296
|
+
*
|
|
1297
|
+
* @since 0.1.0
|
|
1298
|
+
* @category running
|
|
1299
|
+
*/
|
|
1300
|
+
export declare const parseRecoverable: {
|
|
1301
|
+
<A, E, I extends Input>(self: Parser<A, E, never, I>, input: NoInfer<Source<I>>, options?: ParseOptions): RecoverableResult<A, E>;
|
|
1302
|
+
<A, E, R, I extends Input>(self: Parser<A, E, R, I>, input: NoInfer<Source<I>>, options: ParseOptionsWithContext<R>): RecoverableResult<A, E>;
|
|
1303
|
+
};
|
|
1304
|
+
/**
|
|
1305
|
+
* Records a failure kind for use in custom parsers; re-exported for
|
|
1306
|
+
* convenience.
|
|
1307
|
+
*
|
|
1308
|
+
* @since 0.1.0
|
|
1309
|
+
* @category models
|
|
1310
|
+
*/
|
|
1311
|
+
export type { ErrorKind };
|
|
1312
|
+
//# sourceMappingURL=Parser.d.ts.map
|