@thomasfosterau/effect-parse 0.1.0 → 0.2.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/README.md +247 -230
- package/dist/Ascii.d.ts +32 -53
- package/dist/Ascii.d.ts.map +1 -1
- package/dist/Ascii.js +576 -571
- package/dist/Ascii.js.map +1 -1
- package/dist/Binary.d.ts +6 -29
- package/dist/Binary.d.ts.map +1 -1
- package/dist/Binary.js +129 -116
- package/dist/Binary.js.map +1 -1
- package/dist/Bits.d.ts +4 -2
- package/dist/Bits.d.ts.map +1 -1
- package/dist/Bits.js +121 -123
- package/dist/Bits.js.map +1 -1
- package/dist/Compiler.d.ts +39 -0
- package/dist/Compiler.d.ts.map +1 -0
- package/dist/Compiler.js +48 -0
- package/dist/Compiler.js.map +1 -0
- package/dist/Encoding.d.ts +29 -46
- package/dist/Encoding.d.ts.map +1 -1
- package/dist/Encoding.js +76 -75
- package/dist/Encoding.js.map +1 -1
- package/dist/Expression.d.ts +15 -49
- package/dist/Expression.d.ts.map +1 -1
- package/dist/Expression.js +143 -131
- package/dist/Expression.js.map +1 -1
- package/dist/Input.d.ts +206 -177
- package/dist/Input.d.ts.map +1 -1
- package/dist/Input.js +317 -299
- package/dist/Input.js.map +1 -1
- package/dist/Nom.d.ts +44 -84
- package/dist/Nom.d.ts.map +1 -1
- package/dist/Nom.js +423 -453
- package/dist/Nom.js.map +1 -1
- package/dist/ParseError.d.ts +54 -49
- package/dist/ParseError.d.ts.map +1 -1
- package/dist/ParseError.js +233 -275
- package/dist/ParseError.js.map +1 -1
- package/dist/Parser.d.ts +345 -313
- package/dist/Parser.d.ts.map +1 -1
- package/dist/Parser.js +2010 -1802
- package/dist/Parser.js.map +1 -1
- package/dist/ParserSchema.d.ts +8 -7
- package/dist/ParserSchema.d.ts.map +1 -1
- package/dist/ParserSchema.js +49 -49
- package/dist/ParserSchema.js.map +1 -1
- package/dist/ParserStream.d.ts +26 -20
- package/dist/ParserStream.d.ts.map +1 -1
- package/dist/ParserStream.js +267 -255
- package/dist/ParserStream.js.map +1 -1
- package/dist/Syntax.d.ts +401 -0
- package/dist/Syntax.d.ts.map +1 -0
- package/dist/Syntax.js +717 -0
- package/dist/Syntax.js.map +1 -0
- package/dist/Text.d.ts +98 -134
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +534 -496
- package/dist/Text.js.map +1 -1
- package/dist/Token.d.ts +60 -42
- package/dist/Token.d.ts.map +1 -1
- package/dist/Token.js +331 -265
- package/dist/Token.js.map +1 -1
- package/dist/index.d.ts +16 -17
- package/dist/index.js +16 -17
- package/dist/internal/compiler/closure.d.ts +14 -16
- package/dist/internal/compiler/closure.d.ts.map +1 -1
- package/dist/internal/compiler/closure.js +1458 -1637
- package/dist/internal/compiler/closure.js.map +1 -1
- package/dist/internal/compiler/entries.d.ts +11 -11
- package/dist/internal/compiler/entries.d.ts.map +1 -1
- package/dist/internal/compiler/entries.js +36 -26
- package/dist/internal/compiler/entries.js.map +1 -1
- package/dist/internal/compiler/first.d.ts +19 -19
- package/dist/internal/compiler/first.d.ts.map +1 -1
- package/dist/internal/compiler/first.js +251 -227
- package/dist/internal/compiler/first.js.map +1 -1
- package/dist/internal/compiler/jit.d.ts +6 -17
- package/dist/internal/compiler/jit.d.ts.map +1 -1
- package/dist/internal/compiler/jit.js +860 -988
- package/dist/internal/compiler/jit.js.map +1 -1
- package/dist/internal/compiler/registry.d.ts +20 -21
- package/dist/internal/compiler/registry.d.ts.map +1 -1
- package/dist/internal/compiler/registry.js +21 -41
- package/dist/internal/compiler/registry.js.map +1 -1
- package/dist/internal/compiler/sets.d.ts +9 -13
- package/dist/internal/compiler/sets.d.ts.map +1 -1
- package/dist/internal/compiler/sets.js +76 -73
- package/dist/internal/compiler/sets.js.map +1 -1
- package/dist/internal/entries.d.ts +3 -3
- package/dist/internal/entries.d.ts.map +1 -1
- package/dist/internal/entries.js +38 -30
- package/dist/internal/entries.js.map +1 -1
- package/dist/internal/node.d.ts +177 -173
- package/dist/internal/node.d.ts.map +1 -1
- package/dist/internal/node.js +77 -66
- package/dist/internal/node.js.map +1 -1
- package/dist/internal/numbers.d.ts +7 -8
- package/dist/internal/numbers.d.ts.map +1 -1
- package/dist/internal/numbers.js +246 -276
- package/dist/internal/numbers.js.map +1 -1
- package/dist/internal/resume.d.ts +6 -24
- package/dist/internal/resume.d.ts.map +1 -1
- package/dist/internal/resume.js +30 -29
- package/dist/internal/resume.js.map +1 -1
- package/dist/internal/run.d.ts +15 -0
- package/dist/internal/run.d.ts.map +1 -0
- package/dist/internal/run.js +36 -0
- package/dist/internal/run.js.map +1 -0
- package/dist/internal/stack.d.ts +16 -0
- package/dist/internal/stack.d.ts.map +1 -0
- package/dist/internal/stack.js +26 -0
- package/dist/internal/stack.js.map +1 -0
- package/dist/internal/syntax.d.ts +146 -0
- package/dist/internal/syntax.d.ts.map +1 -0
- package/dist/internal/syntax.js +305 -0
- package/dist/internal/syntax.js.map +1 -0
- package/dist/internal/text.d.ts +10 -2
- package/dist/internal/text.d.ts.map +1 -1
- package/dist/internal/text.js +295 -345
- package/dist/internal/text.js.map +1 -1
- package/dist/internal/tokens.d.ts +25 -30
- package/dist/internal/tokens.d.ts.map +1 -1
- package/dist/internal/tokens.js +369 -363
- package/dist/internal/tokens.js.map +1 -1
- package/package.json +42 -28
- package/src/Ascii.ts +278 -187
- package/src/Binary.ts +150 -44
- package/src/Bits.ts +6 -3
- package/src/Compiler.ts +98 -0
- package/src/Expression.ts +10 -5
- package/src/Input.ts +106 -28
- package/src/Nom.ts +4 -3
- package/src/ParseError.ts +48 -51
- package/src/Parser.ts +1163 -795
- package/src/ParserStream.ts +104 -80
- package/src/Syntax.ts +900 -0
- package/src/Text.ts +272 -187
- package/src/Token.ts +180 -87
- package/src/index.ts +2 -0
- package/src/internal/compiler/closure.ts +121 -88
- package/src/internal/compiler/entries.ts +21 -8
- package/src/internal/compiler/first.ts +17 -15
- package/src/internal/compiler/jit.ts +360 -157
- package/src/internal/compiler/registry.ts +12 -33
- package/src/internal/compiler/sets.ts +17 -11
- package/src/internal/entries.ts +4 -2
- package/src/internal/node.ts +51 -37
- package/src/internal/numbers.ts +22 -20
- package/src/internal/resume.ts +8 -4
- package/src/internal/run.ts +41 -0
- package/src/internal/stack.ts +36 -0
- package/src/internal/syntax.ts +421 -0
- package/src/internal/text.ts +32 -25
- package/src/internal/tokens.ts +96 -51
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js.map +0 -1
package/dist/Parser.js
CHANGED
|
@@ -1,1936 +1,2144 @@
|
|
|
1
|
-
|
|
2
|
-
*
|
|
3
|
-
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
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
|
-
*/
|
|
1
|
+
import * as Predicate from "effect/Predicate";
|
|
2
|
+
import * as Result from "effect/Result";
|
|
3
|
+
import { Fail, Failure, State, mergeExpected, mergeFailures } from "./Input.js";
|
|
4
|
+
import * as Resume from "./internal/resume.js";
|
|
5
|
+
import * as Registry from "./internal/compiler/registry.js";
|
|
6
|
+
import * as Option from "effect/Option";
|
|
32
7
|
import * as Context from "effect/Context";
|
|
33
8
|
import * as Effect from "effect/Effect";
|
|
34
9
|
import { dual } from "effect/Function";
|
|
35
10
|
import { NodeInspectSymbol } from "effect/Inspectable";
|
|
36
|
-
import * as Option from "effect/Option";
|
|
37
11
|
import { pipeArguments } from "effect/Pipeable";
|
|
38
|
-
import * as Result from "effect/Result";
|
|
39
|
-
import { Fail, Failure, mergeExpected, mergeFailures, State } from "./Input.js";
|
|
40
12
|
import * as ParseError from "./ParseError.js";
|
|
41
|
-
import * as Compiler from "./internal/compiler/registry.js";
|
|
42
13
|
import * as Entries from "./internal/entries.js";
|
|
43
|
-
import * as
|
|
44
|
-
import
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
14
|
+
import * as Run from "./internal/run.js";
|
|
15
|
+
import * as Stack from "./internal/stack.js";
|
|
16
|
+
import { NodeId, bounds } from "./internal/node.js";
|
|
17
|
+
//#region src/Parser.ts
|
|
18
|
+
/**
|
|
19
|
+
* The `Parser` data type and its general-purpose combinators.
|
|
20
|
+
*
|
|
21
|
+
* A `Parser<A, E, R, I>` consumes input of type `I` and produces a value of
|
|
22
|
+
* type `A`. It may fail with a `ParseError<E>`, where `E` is the type of
|
|
23
|
+
* user supplied error causes, and it may require services `R` from an Effect
|
|
24
|
+
* `Context`.
|
|
25
|
+
*
|
|
26
|
+
* Under the hood a parser is a function over a mutable cursor (`Input.State`),
|
|
27
|
+
* as in winnow. Failure is cheap and backtracking is a matter of resetting an
|
|
28
|
+
* offset. On the surface the API follows Effect's conventions: parsers are
|
|
29
|
+
* pipeable, combinators are dual (data-first or data-last), and parsers can
|
|
30
|
+
* be written with generators:
|
|
31
|
+
*
|
|
32
|
+
* ```ts
|
|
33
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
34
|
+
*
|
|
35
|
+
* const point = Parser.gen(function*() {
|
|
36
|
+
* yield* Token.literal("(")
|
|
37
|
+
* const x = yield* Ascii.decInt
|
|
38
|
+
* yield* Token.literal(",")
|
|
39
|
+
* const y = yield* Ascii.decInt
|
|
40
|
+
* yield* Token.literal(")")
|
|
41
|
+
* return { x, y }
|
|
42
|
+
* })
|
|
43
|
+
*
|
|
44
|
+
* Parser.parse(point, "(1,-2)") // Result.succeed({ x: 1, y: -2 })
|
|
45
|
+
* ```
|
|
46
|
+
*
|
|
47
|
+
* @since 0.1.0
|
|
48
|
+
*/
|
|
49
|
+
/**
|
|
50
|
+
* @since 0.1.0
|
|
51
|
+
* @category type ids
|
|
52
|
+
*/
|
|
53
|
+
const TypeId = "~effect-parse/Parser";
|
|
56
54
|
const variance = {
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
_A: (_) => _,
|
|
56
|
+
_E: (_) => _,
|
|
57
|
+
_R: (_) => _,
|
|
58
|
+
_I: (_) => {}
|
|
59
|
+
};
|
|
60
|
+
var SingleShotGen = class SingleShotGen {
|
|
61
|
+
self;
|
|
62
|
+
called = false;
|
|
63
|
+
constructor(self) {
|
|
64
|
+
this.self = self;
|
|
65
|
+
}
|
|
66
|
+
next(a) {
|
|
67
|
+
if (this.called) return {
|
|
68
|
+
value: a,
|
|
69
|
+
done: true
|
|
70
|
+
};
|
|
71
|
+
this.called = true;
|
|
72
|
+
return {
|
|
73
|
+
value: this.self,
|
|
74
|
+
done: false
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
return(a) {
|
|
78
|
+
return {
|
|
79
|
+
value: a,
|
|
80
|
+
done: true
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
throw(e) {
|
|
84
|
+
throw e;
|
|
85
|
+
}
|
|
86
|
+
[Symbol.iterator]() {
|
|
87
|
+
return new SingleShotGen(this.self);
|
|
88
|
+
}
|
|
61
89
|
};
|
|
62
|
-
class
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
return new SingleShotGen(this);
|
|
100
|
-
}
|
|
101
|
-
toString() {
|
|
102
|
-
return this.name === undefined ? "Parser" : `Parser(${this.name})`;
|
|
103
|
-
}
|
|
104
|
-
toJSON() {
|
|
105
|
-
return { _id: "Parser", name: this.name };
|
|
106
|
-
}
|
|
107
|
-
[NodeInspectSymbol]() {
|
|
108
|
-
return this.toJSON();
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
/**
|
|
112
|
-
* Tests whether a value is a `Parser`.
|
|
113
|
-
*
|
|
114
|
-
* @since 0.1.0
|
|
115
|
-
* @category guards
|
|
116
|
-
*/
|
|
117
|
-
export const isParser = (u) => typeof u === "object" && u !== null && TypeId in u;
|
|
90
|
+
var ParserImpl = class {
|
|
91
|
+
step;
|
|
92
|
+
name;
|
|
93
|
+
[TypeId] = variance;
|
|
94
|
+
/** The description used by the compiler; `undefined` for `Opaque`. */
|
|
95
|
+
[NodeId];
|
|
96
|
+
constructor(step, name, node) {
|
|
97
|
+
this.step = step;
|
|
98
|
+
this.name = name;
|
|
99
|
+
this[NodeId] = node;
|
|
100
|
+
}
|
|
101
|
+
pipe() {
|
|
102
|
+
return pipeArguments(this, arguments);
|
|
103
|
+
}
|
|
104
|
+
[Symbol.iterator]() {
|
|
105
|
+
return new SingleShotGen(this);
|
|
106
|
+
}
|
|
107
|
+
toString() {
|
|
108
|
+
return this.name === void 0 ? "Parser" : `Parser(${this.name})`;
|
|
109
|
+
}
|
|
110
|
+
toJSON() {
|
|
111
|
+
return {
|
|
112
|
+
_id: "Parser",
|
|
113
|
+
name: this.name
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
[NodeInspectSymbol]() {
|
|
117
|
+
return this.toJSON();
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
/**
|
|
121
|
+
* Tests whether a value is a `Parser`.
|
|
122
|
+
*
|
|
123
|
+
* @since 0.1.0
|
|
124
|
+
* @category guards
|
|
125
|
+
*/
|
|
126
|
+
const isParser = (u) => Predicate.isObjectOrArray(u) && "~effect-parse/Parser" in u;
|
|
118
127
|
const isParserArg = (args) => isParser(args[0]);
|
|
119
128
|
const isTwoParserArgs = (args) => isParser(args[0]) && isParser(args[1]);
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
* @internal
|
|
157
|
-
*/
|
|
158
|
-
export const described = (step, name, node) => new ParserImpl(step, name, node);
|
|
129
|
+
/**
|
|
130
|
+
* Creates a parser from a step function over the mutable {@link State}
|
|
131
|
+
* cursor. This is the escape hatch for writing low-level parsers, much like
|
|
132
|
+
* passing a plain function as a parser in winnow.
|
|
133
|
+
*
|
|
134
|
+
* The step function must either return the parsed value, having advanced
|
|
135
|
+
* `state.pos` past the consumed input, or return the result of one of
|
|
136
|
+
* `state.backtrack`, `state.cut`, `state.incomplete`, ... to signal failure.
|
|
137
|
+
*
|
|
138
|
+
* @example
|
|
139
|
+
* ```ts
|
|
140
|
+
* import { Parser } from "@thomasfosterau/effect-parse"
|
|
141
|
+
*
|
|
142
|
+
* // Parses a single ASCII uppercase letter
|
|
143
|
+
* const upper = Parser.make<string>((state) => {
|
|
144
|
+
* const c = state.input.charCodeAt(state.pos)
|
|
145
|
+
* if (state.pos < state.end && c >= 65 && c <= 90) {
|
|
146
|
+
* state.pos++
|
|
147
|
+
* return String.fromCharCode(c)
|
|
148
|
+
* }
|
|
149
|
+
* return state.pos >= state.end
|
|
150
|
+
* ? state.eoi("Token", ["uppercase letter"], 1)
|
|
151
|
+
* : state.backtrack("Token", ["uppercase letter"])
|
|
152
|
+
* })
|
|
153
|
+
* ```
|
|
154
|
+
*
|
|
155
|
+
* @since 0.1.0
|
|
156
|
+
* @category constructors
|
|
157
|
+
*/
|
|
158
|
+
const make = (step, name) => new ParserImpl(step, name, void 0);
|
|
159
|
+
/**
|
|
160
|
+
* Creates a parser together with its description, used by the compiler.
|
|
161
|
+
*
|
|
162
|
+
* @internal
|
|
163
|
+
*/
|
|
164
|
+
const described = (step, name, node) => new ParserImpl(step, name, node);
|
|
159
165
|
const nodeOfParser = (p) => p[NodeId];
|
|
160
166
|
/**
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
/**
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
167
|
+
* Gives a parser a name, used by `toString`.
|
|
168
|
+
*
|
|
169
|
+
* @since 0.1.0
|
|
170
|
+
* @category utils
|
|
171
|
+
*/
|
|
172
|
+
const named = /*#__PURE__*/ dual(2, (self, name) => new ParserImpl(self.step, name, nodeOfParser(self)));
|
|
173
|
+
/**
|
|
174
|
+
* A parser that consumes nothing and succeeds with the given value.
|
|
175
|
+
*
|
|
176
|
+
* @since 0.1.0
|
|
177
|
+
* @category constructors
|
|
178
|
+
*/
|
|
179
|
+
const succeed = (value) => described(() => value, "succeed", {
|
|
180
|
+
_tag: "Succeed",
|
|
181
|
+
value
|
|
182
|
+
});
|
|
183
|
+
/**
|
|
184
|
+
* A parser that consumes nothing and succeeds with the result of a thunk.
|
|
185
|
+
*
|
|
186
|
+
* @since 0.1.0
|
|
187
|
+
* @category constructors
|
|
188
|
+
*/
|
|
189
|
+
const sync = (evaluate) => described(() => evaluate(), "sync", {
|
|
190
|
+
_tag: "Sync",
|
|
191
|
+
evaluate
|
|
192
|
+
});
|
|
193
|
+
/**
|
|
194
|
+
* A parser that consumes nothing and succeeds with `undefined` (winnow's
|
|
195
|
+
* `empty`, nom's `success(())`).
|
|
196
|
+
*
|
|
197
|
+
* @since 0.1.0
|
|
198
|
+
* @category constructors
|
|
199
|
+
*/
|
|
200
|
+
const empty = /*#__PURE__*/ described(() => void 0, "empty", {
|
|
201
|
+
_tag: "Succeed",
|
|
202
|
+
value: void 0
|
|
191
203
|
});
|
|
192
204
|
/**
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
* @category constructors
|
|
211
|
-
*/
|
|
212
|
-
export const fail = (error) => described((state) => state.failCause("Backtrack", "Fail", error), "fail", failBacktrack);
|
|
213
|
-
const failBacktrack = { _tag: "Fail", mode: "Backtrack" };
|
|
214
|
-
/**
|
|
215
|
-
* A parser that always fails (backtracks) without a cause. This is winnow's
|
|
216
|
-
* `fail` and nom's `fail()`.
|
|
217
|
-
*
|
|
218
|
-
* @since 0.1.0
|
|
219
|
-
* @category constructors
|
|
220
|
-
*/
|
|
221
|
-
export const reject = described((state) => state.backtrack("Fail"), "reject", failBacktrack);
|
|
222
|
-
/**
|
|
223
|
-
* A parser that always fails with a message.
|
|
224
|
-
*
|
|
225
|
-
* @since 0.1.0
|
|
226
|
-
* @category constructors
|
|
227
|
-
*/
|
|
228
|
-
export const failMessage = (message) => described((state) => state.failMessage("Backtrack", "Fail", message), "failMessage", failBacktrack);
|
|
229
|
-
/**
|
|
230
|
-
* A placeholder parser that fails with an unrecoverable error (winnow's
|
|
231
|
-
* `todo`).
|
|
232
|
-
*
|
|
233
|
-
* @since 0.1.0
|
|
234
|
-
* @category constructors
|
|
235
|
-
*/
|
|
236
|
-
export const todo = described((state) => state.failMessage("Cut", "Assert", "`todo` parser reached"), "todo", { _tag: "Fail", mode: "Cut" });
|
|
237
|
-
/**
|
|
238
|
-
* Lazily constructs a parser, which is needed to define recursive grammars.
|
|
239
|
-
* The thunk is evaluated once, on first use.
|
|
240
|
-
*
|
|
241
|
-
* @example
|
|
242
|
-
* ```ts
|
|
243
|
-
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
244
|
-
*
|
|
245
|
-
* type Nested = ReadonlyArray<Nested>
|
|
246
|
-
* const nested: Parser.Parser<Nested> = Parser.delimited(
|
|
247
|
-
* Token.literal("["),
|
|
248
|
-
* Parser.repeat(Parser.suspend(() => nested)),
|
|
249
|
-
* Token.literal("]")
|
|
250
|
-
* )
|
|
251
|
-
* ```
|
|
252
|
-
*
|
|
253
|
-
* @since 0.1.0
|
|
254
|
-
* @category constructors
|
|
255
|
-
*/
|
|
256
|
-
export const suspend = (evaluate) => {
|
|
257
|
-
let parser;
|
|
258
|
-
return described((state) => (parser ??= evaluate()).step(state), "suspend", {
|
|
259
|
-
_tag: "Suspend",
|
|
260
|
-
resolve: () => parser ??= evaluate()
|
|
261
|
-
});
|
|
205
|
+
* Alias of {@link empty}, following Effect's `Effect.void`.
|
|
206
|
+
*
|
|
207
|
+
* @since 0.1.0
|
|
208
|
+
* @category constructors
|
|
209
|
+
*/
|
|
210
|
+
const void_ = empty;
|
|
211
|
+
/**
|
|
212
|
+
* A parser that always fails (backtracks) with the given user error as its
|
|
213
|
+
* cause.
|
|
214
|
+
*
|
|
215
|
+
* @since 0.1.0
|
|
216
|
+
* @category constructors
|
|
217
|
+
*/
|
|
218
|
+
const fail = (error) => described((state) => state.failCause("Backtrack", "Fail", error), "fail", failBacktrack);
|
|
219
|
+
const failBacktrack = {
|
|
220
|
+
_tag: "Fail",
|
|
221
|
+
mode: "Backtrack"
|
|
262
222
|
};
|
|
263
223
|
/**
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
224
|
+
* A parser that always fails (backtracks) without a cause. This is winnow's
|
|
225
|
+
* `fail` and nom's `fail()`.
|
|
226
|
+
*
|
|
227
|
+
* @since 0.1.0
|
|
228
|
+
* @category constructors
|
|
229
|
+
*/
|
|
230
|
+
const reject = /*#__PURE__*/ described((state) => state.backtrack("Fail"), "reject", failBacktrack);
|
|
231
|
+
/**
|
|
232
|
+
* A parser that always fails with a message.
|
|
233
|
+
*
|
|
234
|
+
* @since 0.1.0
|
|
235
|
+
* @category constructors
|
|
236
|
+
*/
|
|
237
|
+
const failMessage = (message) => described((state) => state.failMessage("Backtrack", "Fail", message), "failMessage", failBacktrack);
|
|
238
|
+
/**
|
|
239
|
+
* A placeholder parser that fails with an unrecoverable error (winnow's
|
|
240
|
+
* `todo`).
|
|
241
|
+
*
|
|
242
|
+
* @since 0.1.0
|
|
243
|
+
* @category constructors
|
|
244
|
+
*/
|
|
245
|
+
const todo = /*#__PURE__*/ described((state) => state.failMessage("Cut", "Assert", "`todo` parser reached"), "todo", {
|
|
246
|
+
_tag: "Fail",
|
|
247
|
+
mode: "Cut"
|
|
271
248
|
});
|
|
272
249
|
/**
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
250
|
+
* Lazily constructs a parser, which is needed to define recursive grammars.
|
|
251
|
+
* The thunk is evaluated once, on first use.
|
|
252
|
+
*
|
|
253
|
+
* Each level of recursion uses some of the call stack, interpreted or
|
|
254
|
+
* compiled, so input nested a few thousand levels deep exhausts it. The
|
|
255
|
+
* runners catch the overflow and fail with an unrecoverable `ParseError` of
|
|
256
|
+
* kind `"TooDeep"` at the offset reached, instead of throwing.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
261
|
+
*
|
|
262
|
+
* type Nested = ReadonlyArray<Nested>
|
|
263
|
+
* const nested: Parser.Parser<Nested> = Parser.delimited(
|
|
264
|
+
* Token.literal("["),
|
|
265
|
+
* Parser.repeat(Parser.suspend(() => nested)),
|
|
266
|
+
* Token.literal("]")
|
|
267
|
+
* )
|
|
268
|
+
* ```
|
|
269
|
+
*
|
|
270
|
+
* @since 0.1.0
|
|
271
|
+
* @category constructors
|
|
272
|
+
*/
|
|
273
|
+
const suspend = (evaluate) => {
|
|
274
|
+
let parser;
|
|
275
|
+
return described((state) => (parser ??= evaluate()).step(state), "suspend", {
|
|
276
|
+
_tag: "Suspend",
|
|
277
|
+
resolve: () => parser ??= evaluate()
|
|
278
|
+
});
|
|
279
|
+
};
|
|
280
|
+
/**
|
|
281
|
+
* The current offset into the input (winnow's `Location`).
|
|
282
|
+
*
|
|
283
|
+
* @since 0.1.0
|
|
284
|
+
* @category constructors
|
|
285
|
+
*/
|
|
286
|
+
const location = /*#__PURE__*/ described((state) => state.pos, "location", { _tag: "Location" });
|
|
287
|
+
/**
|
|
288
|
+
* Succeeds (with `undefined`) only at the end of the input; nom's and
|
|
289
|
+
* winnow's `eof`. On partial input, reaching the end of the available data
|
|
290
|
+
* is reported as `Incomplete` since more data may follow.
|
|
291
|
+
*
|
|
292
|
+
* @since 0.1.0
|
|
293
|
+
* @category constructors
|
|
294
|
+
*/
|
|
295
|
+
const eof = /*#__PURE__*/ described((state) => {
|
|
296
|
+
if (state.pos < state.end) return state.backtrack("Eof", expectedEof);
|
|
297
|
+
if (state.partial) return state.incomplete(1, expectedEof);
|
|
286
298
|
}, "eof", { _tag: "Eof" });
|
|
287
299
|
const expectedEof = ["end of input"];
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
300
|
+
/**
|
|
301
|
+
* Transforms the output of a parser.
|
|
302
|
+
*
|
|
303
|
+
* @since 0.1.0
|
|
304
|
+
* @category mapping
|
|
305
|
+
*/
|
|
306
|
+
const map = /*#__PURE__*/ dual(2, (self, f) => {
|
|
307
|
+
const step = self.step;
|
|
308
|
+
return described((state) => {
|
|
309
|
+
const a = step(state);
|
|
310
|
+
return a === Fail ? Fail : f(a);
|
|
311
|
+
}, self.name, {
|
|
312
|
+
_tag: "Map",
|
|
313
|
+
self,
|
|
314
|
+
f
|
|
315
|
+
});
|
|
303
316
|
});
|
|
304
317
|
/**
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
/**
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
318
|
+
* Builds an object from the `[key, value]` entries a parser returns, as
|
|
319
|
+
* `Object.fromEntries` does: `fromEntries(self)` is
|
|
320
|
+
* `map(self, Object.fromEntries)`.
|
|
321
|
+
*
|
|
322
|
+
* Compiled parsers build the object directly when `self` repeats a pair
|
|
323
|
+
* (`separated` or `repeat` of `separatedPair` or a two-element `seq`,
|
|
324
|
+
* possibly inside `preceded`, `terminated`, `delimited` or `cut`), without
|
|
325
|
+
* the pairs and the array. They do the same for `map(Object.fromEntries)`.
|
|
326
|
+
*
|
|
327
|
+
* @example
|
|
328
|
+
* ```ts
|
|
329
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
330
|
+
*
|
|
331
|
+
* const entry = Parser.separatedPair(Ascii.alpha1, Token.literal("="), Ascii.decUint)
|
|
332
|
+
* const record = Parser.fromEntries(Parser.separated(entry, Token.literal(",")))
|
|
333
|
+
* Parser.parse(record, "a=1,b=2") // Result.succeed({ a: 1, b: 2 })
|
|
334
|
+
* ```
|
|
335
|
+
*
|
|
336
|
+
* @since 0.2.0
|
|
337
|
+
* @category mapping
|
|
338
|
+
*/
|
|
339
|
+
const fromEntries = (self) => map(self, Entries.fromEntries);
|
|
340
|
+
/**
|
|
341
|
+
* Replaces the output of a parser with a constant (winnow's `value`, nom's
|
|
342
|
+
* `value`).
|
|
343
|
+
*
|
|
344
|
+
* @since 0.1.0
|
|
345
|
+
* @category mapping
|
|
346
|
+
*/
|
|
347
|
+
const as = /*#__PURE__*/ dual(2, (self, value) => {
|
|
348
|
+
const step = self.step;
|
|
349
|
+
return described((state) => step(state) === Fail ? Fail : value, self.name, {
|
|
350
|
+
_tag: "As",
|
|
351
|
+
self,
|
|
352
|
+
value
|
|
353
|
+
});
|
|
337
354
|
});
|
|
338
355
|
/**
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
/**
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
/**
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
356
|
+
* Discards the output of a parser (winnow's `void`).
|
|
357
|
+
*
|
|
358
|
+
* @since 0.1.0
|
|
359
|
+
* @category mapping
|
|
360
|
+
*/
|
|
361
|
+
const asVoid = (self) => as(self, void 0);
|
|
362
|
+
/**
|
|
363
|
+
* Replaces the output of a parser with `Option.some(output)`.
|
|
364
|
+
*
|
|
365
|
+
* @since 0.1.0
|
|
366
|
+
* @category mapping
|
|
367
|
+
*/
|
|
368
|
+
const asSome = (self) => map(self, Option.some);
|
|
369
|
+
/**
|
|
370
|
+
* Runs a side effect on the output of a parser, keeping the output.
|
|
371
|
+
*
|
|
372
|
+
* @since 0.1.0
|
|
373
|
+
* @category mapping
|
|
374
|
+
*/
|
|
375
|
+
const tap = /*#__PURE__*/ dual(2, (self, f) => {
|
|
376
|
+
const step = self.step;
|
|
377
|
+
return described((state) => {
|
|
378
|
+
const a = step(state);
|
|
379
|
+
if (a !== Fail) f(a);
|
|
380
|
+
return a;
|
|
381
|
+
}, self.name, {
|
|
382
|
+
_tag: "Tap",
|
|
383
|
+
self,
|
|
384
|
+
f
|
|
385
|
+
});
|
|
366
386
|
});
|
|
367
387
|
/**
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
388
|
+
* Uses the output of a parser to choose the parser that consumes the rest
|
|
389
|
+
* of the input (winnow's `flat_map`, nom's `flat_map`).
|
|
390
|
+
*
|
|
391
|
+
* @since 0.1.0
|
|
392
|
+
* @category sequencing
|
|
393
|
+
*/
|
|
394
|
+
const flatMap = /*#__PURE__*/ dual(2, (self, f) => {
|
|
395
|
+
const step = self.step;
|
|
396
|
+
return described((state) => {
|
|
397
|
+
const a = step(state);
|
|
398
|
+
return a === Fail ? Fail : f(a).step(state);
|
|
399
|
+
}, void 0, {
|
|
400
|
+
_tag: "FlatMap",
|
|
401
|
+
self,
|
|
402
|
+
f
|
|
403
|
+
});
|
|
380
404
|
});
|
|
381
405
|
/**
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
}
|
|
422
|
-
return b;
|
|
423
|
-
});
|
|
406
|
+
* Applies a second parser to the *output* of the first (winnow's
|
|
407
|
+
* `and_then`, nom's `map_parser`). The output of the first parser must
|
|
408
|
+
* itself be an input, e.g. a slice produced by {@link taken} or
|
|
409
|
+
* `Token.take`.
|
|
410
|
+
*
|
|
411
|
+
* Error offsets reported by the inner parser are translated relative to the
|
|
412
|
+
* start of the outer parser.
|
|
413
|
+
*
|
|
414
|
+
* @example
|
|
415
|
+
* ```ts
|
|
416
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
417
|
+
*
|
|
418
|
+
* // Take exactly 4 characters, then require them all to be digits
|
|
419
|
+
* const year = Token.take(4).pipe(Parser.mapParser(Parser.allConsuming(Ascii.decUint)))
|
|
420
|
+
* ```
|
|
421
|
+
*
|
|
422
|
+
* @since 0.1.0
|
|
423
|
+
* @category sequencing
|
|
424
|
+
*/
|
|
425
|
+
const mapParser = /*#__PURE__*/ dual(2, (self, inner) => {
|
|
426
|
+
const step = self.step;
|
|
427
|
+
return make((state) => {
|
|
428
|
+
const start = state.pos;
|
|
429
|
+
const a = step(state);
|
|
430
|
+
if (a === Fail) return Fail;
|
|
431
|
+
const sub = new State(a, { context: state.context });
|
|
432
|
+
sub.tracer = state.tracer;
|
|
433
|
+
sub.depth = state.depth;
|
|
434
|
+
sub.recovered = state.recovered;
|
|
435
|
+
const b = inner.step(sub);
|
|
436
|
+
if (b === Fail) {
|
|
437
|
+
const f = sub.failure;
|
|
438
|
+
if (sub.encoding !== void 0) state.encoding = sub.encoding;
|
|
439
|
+
const base = Math.max(start, state.pos - a.length);
|
|
440
|
+
const offset = Math.min(base + f.offset, state.pos);
|
|
441
|
+
return state.failWith(sub.mode === "Incomplete" ? "Backtrack" : sub.mode, new Failure(offset, f.kind, f.expected, f.context, f.hasCause, f.cause, f.message, 0));
|
|
442
|
+
}
|
|
443
|
+
return b;
|
|
444
|
+
});
|
|
424
445
|
});
|
|
425
446
|
/**
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
+
* Transforms the output of a parser with a function that may fail with a
|
|
448
|
+
* `Result` (winnow's `try_map`, nom's `map_res`). On failure the parser
|
|
449
|
+
* backtracks to where it started and the error becomes the `cause` of the
|
|
450
|
+
* `ParseError`.
|
|
451
|
+
*
|
|
452
|
+
* @since 0.1.0
|
|
453
|
+
* @category mapping
|
|
454
|
+
*/
|
|
455
|
+
const tryMap = /*#__PURE__*/ dual(2, (self, f) => {
|
|
456
|
+
const step = self.step;
|
|
457
|
+
return described((state) => {
|
|
458
|
+
const start = state.pos;
|
|
459
|
+
const a = step(state);
|
|
460
|
+
if (a === Fail) return Fail;
|
|
461
|
+
const r = f(a);
|
|
462
|
+
if (Result.isSuccess(r)) return r.success;
|
|
463
|
+
state.pos = start;
|
|
464
|
+
return state.failCause("Backtrack", "Verify", r.failure);
|
|
465
|
+
}, self.name, {
|
|
466
|
+
_tag: "TryMap",
|
|
467
|
+
self,
|
|
468
|
+
f
|
|
469
|
+
});
|
|
447
470
|
});
|
|
448
471
|
/**
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
472
|
+
* Like {@link tryMap}, but the function may throw. Thrown values become the
|
|
473
|
+
* `cause` of the error.
|
|
474
|
+
*
|
|
475
|
+
* @since 0.1.0
|
|
476
|
+
* @category mapping
|
|
477
|
+
*/
|
|
478
|
+
const tryMapUnknown = /*#__PURE__*/ dual(2, (self, f) => tryMap(self, (a) => {
|
|
479
|
+
try {
|
|
480
|
+
return Result.succeed(f(a));
|
|
481
|
+
} catch (e) {
|
|
482
|
+
if (Stack.isStackOverflow(e)) throw e;
|
|
483
|
+
return Result.fail(e);
|
|
484
|
+
}
|
|
462
485
|
}));
|
|
463
486
|
/**
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
487
|
+
* Transforms the output of a parser with a function returning an `Option`;
|
|
488
|
+
* `None` makes the parser backtrack (winnow's `verify_map`, nom's
|
|
489
|
+
* `map_opt`).
|
|
490
|
+
*
|
|
491
|
+
* @since 0.1.0
|
|
492
|
+
* @category filtering
|
|
493
|
+
*/
|
|
494
|
+
const filterMap = /*#__PURE__*/ dual(2, (self, f) => {
|
|
495
|
+
const step = self.step;
|
|
496
|
+
return described((state) => {
|
|
497
|
+
const start = state.pos;
|
|
498
|
+
const a = step(state);
|
|
499
|
+
if (a === Fail) return Fail;
|
|
500
|
+
const o = f(a);
|
|
501
|
+
if (Option.isSome(o)) return o.value;
|
|
502
|
+
state.pos = start;
|
|
503
|
+
return state.backtrack("Verify");
|
|
504
|
+
}, self.name, {
|
|
505
|
+
_tag: "FilterMap",
|
|
506
|
+
self,
|
|
507
|
+
f
|
|
508
|
+
});
|
|
484
509
|
});
|
|
485
510
|
/**
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
511
|
+
* Keeps the output of a parser only if it satisfies a predicate, otherwise
|
|
512
|
+
* backtracks (winnow's and nom's `verify`).
|
|
513
|
+
*
|
|
514
|
+
* @since 0.1.0
|
|
515
|
+
* @category filtering
|
|
516
|
+
*/
|
|
517
|
+
const filter = /*#__PURE__*/ dual(2, (self, predicate) => {
|
|
518
|
+
const step = self.step;
|
|
519
|
+
return described((state) => {
|
|
520
|
+
const start = state.pos;
|
|
521
|
+
const a = step(state);
|
|
522
|
+
if (a === Fail) return Fail;
|
|
523
|
+
if (predicate(a)) return a;
|
|
524
|
+
state.pos = start;
|
|
525
|
+
return state.backtrack("Verify");
|
|
526
|
+
}, self.name, {
|
|
527
|
+
_tag: "Filter",
|
|
528
|
+
self,
|
|
529
|
+
predicate
|
|
530
|
+
});
|
|
504
531
|
});
|
|
505
532
|
/**
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
533
|
+
* Keeps the output of a parser only if it satisfies a predicate, otherwise
|
|
534
|
+
* backtracks with the given user error.
|
|
535
|
+
*
|
|
536
|
+
* @since 0.1.0
|
|
537
|
+
* @category filtering
|
|
538
|
+
*/
|
|
539
|
+
const filterOrFail = /*#__PURE__*/ dual(3, (self, predicate, orFailWith) => {
|
|
540
|
+
const step = self.step;
|
|
541
|
+
return described((state) => {
|
|
542
|
+
const start = state.pos;
|
|
543
|
+
const a = step(state);
|
|
544
|
+
if (a === Fail) return Fail;
|
|
545
|
+
if (predicate(a)) return a;
|
|
546
|
+
state.pos = start;
|
|
547
|
+
return state.failCause("Backtrack", "Verify", orFailWith(a));
|
|
548
|
+
}, self.name, {
|
|
549
|
+
_tag: "Filter",
|
|
550
|
+
self,
|
|
551
|
+
predicate
|
|
552
|
+
});
|
|
524
553
|
});
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
554
|
+
/**
|
|
555
|
+
* Returns the slice of input consumed by a parser instead of its output
|
|
556
|
+
* (winnow's `take`, nom's `recognize`).
|
|
557
|
+
*
|
|
558
|
+
* @since 0.1.0
|
|
559
|
+
* @category consumed input
|
|
560
|
+
*/
|
|
561
|
+
const taken = (self) => {
|
|
562
|
+
const step = self.step;
|
|
563
|
+
return described((state) => {
|
|
564
|
+
const start = state.pos;
|
|
565
|
+
return step(state) === Fail ? Fail : state.slice(start, state.pos);
|
|
566
|
+
}, self.name, {
|
|
567
|
+
_tag: "Taken",
|
|
568
|
+
self,
|
|
569
|
+
encoding: void 0
|
|
570
|
+
});
|
|
541
571
|
};
|
|
542
572
|
/**
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
573
|
+
* Returns both the output of a parser and the slice of input it consumed
|
|
574
|
+
* (winnow's `with_taken`; nom's `consumed` returns them in the other order).
|
|
575
|
+
*
|
|
576
|
+
* @since 0.1.0
|
|
577
|
+
* @category consumed input
|
|
578
|
+
*/
|
|
579
|
+
const withTaken = (self) => {
|
|
580
|
+
const step = self.step;
|
|
581
|
+
return described((state) => {
|
|
582
|
+
const start = state.pos;
|
|
583
|
+
const a = step(state);
|
|
584
|
+
return a === Fail ? Fail : [a, state.slice(start, state.pos)];
|
|
585
|
+
}, self.name, {
|
|
586
|
+
_tag: "WithTaken",
|
|
587
|
+
self
|
|
588
|
+
});
|
|
556
589
|
};
|
|
557
590
|
/**
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
591
|
+
* Returns the span (`[start, end)` offsets) consumed by a parser instead of
|
|
592
|
+
* its output (winnow's `span`).
|
|
593
|
+
*
|
|
594
|
+
* @since 0.1.0
|
|
595
|
+
* @category consumed input
|
|
596
|
+
*/
|
|
597
|
+
const span = (self) => {
|
|
598
|
+
const step = self.step;
|
|
599
|
+
return described((state) => {
|
|
600
|
+
const start = state.pos;
|
|
601
|
+
return step(state) === Fail ? Fail : {
|
|
602
|
+
start,
|
|
603
|
+
end: state.pos
|
|
604
|
+
};
|
|
605
|
+
}, self.name, {
|
|
606
|
+
_tag: "Span",
|
|
607
|
+
self
|
|
608
|
+
});
|
|
570
609
|
};
|
|
571
610
|
/**
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
611
|
+
* Returns both the output of a parser and the span it consumed (winnow's
|
|
612
|
+
* `with_span`).
|
|
613
|
+
*
|
|
614
|
+
* @since 0.1.0
|
|
615
|
+
* @category consumed input
|
|
616
|
+
*/
|
|
617
|
+
const withSpan = (self) => {
|
|
618
|
+
const step = self.step;
|
|
619
|
+
return described((state) => {
|
|
620
|
+
const start = state.pos;
|
|
621
|
+
const a = step(state);
|
|
622
|
+
return a === Fail ? Fail : [a, {
|
|
623
|
+
start,
|
|
624
|
+
end: state.pos
|
|
625
|
+
}];
|
|
626
|
+
}, self.name, {
|
|
627
|
+
_tag: "WithSpan",
|
|
628
|
+
self
|
|
629
|
+
});
|
|
585
630
|
};
|
|
586
|
-
// -----------------------------------------------------------------------------
|
|
587
|
-
// Error management
|
|
588
|
-
// -----------------------------------------------------------------------------
|
|
589
631
|
const changeMode = (self, from, to) => {
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
632
|
+
const step = self.step;
|
|
633
|
+
return described((state) => {
|
|
634
|
+
const a = step(state);
|
|
635
|
+
if (a === Fail && state.mode === from) state.mode = to;
|
|
636
|
+
return a;
|
|
637
|
+
}, self.name, {
|
|
638
|
+
_tag: "Mode",
|
|
639
|
+
self,
|
|
640
|
+
from,
|
|
641
|
+
to
|
|
642
|
+
});
|
|
643
|
+
};
|
|
644
|
+
/**
|
|
645
|
+
* Commits to the current branch: recoverable failures of the parser become
|
|
646
|
+
* unrecoverable, so enclosing `alt`, `opt`, `repeat`, ... do not try
|
|
647
|
+
* alternatives (winnow's `cut_err`, nom's `cut`).
|
|
648
|
+
*
|
|
649
|
+
* @example
|
|
650
|
+
* ```ts
|
|
651
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
652
|
+
*
|
|
653
|
+
* // Once we have seen "-", a number must follow
|
|
654
|
+
* const negative = Parser.preceded(Token.literal("-"), Parser.cut(Ascii.decUint))
|
|
655
|
+
* ```
|
|
656
|
+
*
|
|
657
|
+
* @since 0.1.0
|
|
658
|
+
* @category error management
|
|
659
|
+
*/
|
|
660
|
+
const cut = (self) => {
|
|
661
|
+
const step = self.step;
|
|
662
|
+
return described((state) => {
|
|
663
|
+
const deepest = state.deepest;
|
|
664
|
+
state.deepest = void 0;
|
|
665
|
+
const a = step(state);
|
|
666
|
+
if (a === Fail && state.mode !== "Incomplete") {
|
|
667
|
+
state.mode = "Cut";
|
|
668
|
+
return a;
|
|
669
|
+
}
|
|
670
|
+
const inner = state.deepest;
|
|
671
|
+
if (deepest !== void 0 && (inner === void 0 || deepest.offset > inner.offset)) state.deepest = deepest;
|
|
672
|
+
return a;
|
|
673
|
+
}, self.name, {
|
|
674
|
+
_tag: "Mode",
|
|
675
|
+
self,
|
|
676
|
+
from: "Backtrack",
|
|
677
|
+
to: "Cut"
|
|
678
|
+
});
|
|
597
679
|
};
|
|
598
680
|
/**
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
*/
|
|
630
|
-
export const complete = (self) => {
|
|
631
|
-
const step = self.step;
|
|
632
|
-
return described((state) => {
|
|
633
|
-
const start = state.pos;
|
|
634
|
-
const a = step(state);
|
|
635
|
-
if (a === Fail && state.mode === "Incomplete") {
|
|
636
|
-
state.pos = start;
|
|
637
|
-
return state.backtrack("Complete");
|
|
638
|
-
}
|
|
639
|
-
return a;
|
|
640
|
-
}, self.name, { _tag: "Mode", self, from: "Incomplete", to: "Backtrack" });
|
|
681
|
+
* Turns unrecoverable failures back into recoverable ones (winnow's
|
|
682
|
+
* `backtrack_err`).
|
|
683
|
+
*
|
|
684
|
+
* @since 0.1.0
|
|
685
|
+
* @category error management
|
|
686
|
+
*/
|
|
687
|
+
const backtrack = (self) => changeMode(self, "Cut", "Backtrack");
|
|
688
|
+
/**
|
|
689
|
+
* Turns `Incomplete` failures into recoverable ones, treating the input as
|
|
690
|
+
* complete for this parser (winnow's `complete_err`, nom's `complete`).
|
|
691
|
+
*
|
|
692
|
+
* @since 0.1.0
|
|
693
|
+
* @category error management
|
|
694
|
+
*/
|
|
695
|
+
const complete = (self) => {
|
|
696
|
+
const step = self.step;
|
|
697
|
+
return described((state) => {
|
|
698
|
+
const start = state.pos;
|
|
699
|
+
const a = step(state);
|
|
700
|
+
if (a === Fail && state.mode === "Incomplete") {
|
|
701
|
+
state.pos = start;
|
|
702
|
+
return state.backtrack("Complete");
|
|
703
|
+
}
|
|
704
|
+
return a;
|
|
705
|
+
}, self.name, {
|
|
706
|
+
_tag: "Mode",
|
|
707
|
+
self,
|
|
708
|
+
from: "Incomplete",
|
|
709
|
+
to: "Backtrack"
|
|
710
|
+
});
|
|
641
711
|
};
|
|
642
712
|
/**
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
713
|
+
* Adds a context label to failures of a parser (winnow's
|
|
714
|
+
* `context(StrContext::Label(..))`, nom's `context`). Labels are rendered as
|
|
715
|
+
* "while parsing <label>" in the error message.
|
|
716
|
+
*
|
|
717
|
+
* @since 0.1.0
|
|
718
|
+
* @category error management
|
|
719
|
+
*/
|
|
720
|
+
const context = /*#__PURE__*/ dual(2, (self, label) => {
|
|
721
|
+
const step = self.step;
|
|
722
|
+
return described((state) => {
|
|
723
|
+
const a = step(state);
|
|
724
|
+
if (a === Fail && state.mode !== "Incomplete") {
|
|
725
|
+
const f = state.failure;
|
|
726
|
+
state.replaceFailure(new Failure(f.offset, f.kind, f.expected, [...f.context, label], f.hasCause, f.cause, f.message, f.needed));
|
|
727
|
+
}
|
|
728
|
+
return a;
|
|
729
|
+
}, self.name ?? label, {
|
|
730
|
+
_tag: "Context",
|
|
731
|
+
self,
|
|
732
|
+
label
|
|
733
|
+
});
|
|
660
734
|
});
|
|
661
735
|
/**
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
736
|
+
* Names what a parser expects. When the parser fails without consuming any
|
|
737
|
+
* input, its expectations are replaced with `description`, so a failure is
|
|
738
|
+
* reported as `expected <description>` rather than in terms of the parser's
|
|
739
|
+
* internals (Parsec's `<?>`, winnow's `StrContext::Expected`).
|
|
740
|
+
*
|
|
741
|
+
* @since 0.1.0
|
|
742
|
+
* @category error management
|
|
743
|
+
*/
|
|
744
|
+
const expected = /*#__PURE__*/ dual(2, (self, description) => {
|
|
745
|
+
const step = self.step;
|
|
746
|
+
const desc = [description];
|
|
747
|
+
return described((state) => {
|
|
748
|
+
const start = state.pos;
|
|
749
|
+
const hintOffset = state.hintOffset;
|
|
750
|
+
const hintExpected = state.hintExpected;
|
|
751
|
+
const a = step(state);
|
|
752
|
+
if (a === Fail && state.failure.offset === start) {
|
|
753
|
+
const f = state.failure;
|
|
754
|
+
const expected = hintOffset === start ? mergeExpected(hintExpected, desc) : desc;
|
|
755
|
+
state.replaceFailure(new Failure(f.offset, f.kind, expected, f.context, f.hasCause, f.cause, f.message, f.needed));
|
|
756
|
+
}
|
|
757
|
+
return a;
|
|
758
|
+
}, description, {
|
|
759
|
+
_tag: "Expected",
|
|
760
|
+
self,
|
|
761
|
+
description
|
|
762
|
+
});
|
|
685
763
|
});
|
|
686
764
|
/**
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
765
|
+
* Transforms the user error (cause) of a parser's failures.
|
|
766
|
+
*
|
|
767
|
+
* @since 0.1.0
|
|
768
|
+
* @category error management
|
|
769
|
+
*/
|
|
770
|
+
const mapError = /*#__PURE__*/ dual(2, (self, f) => {
|
|
771
|
+
const step = self.step;
|
|
772
|
+
return described((state) => {
|
|
773
|
+
const a = step(state);
|
|
774
|
+
if (a === Fail && state.failure.hasCause) {
|
|
775
|
+
const x = state.failure;
|
|
776
|
+
state.replaceFailure(new Failure(x.offset, x.kind, x.expected, x.context, true, f(x.cause), x.message, x.needed));
|
|
777
|
+
}
|
|
778
|
+
return a;
|
|
779
|
+
}, self.name, {
|
|
780
|
+
_tag: "MapError",
|
|
781
|
+
self
|
|
782
|
+
});
|
|
702
783
|
});
|
|
703
784
|
/**
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
785
|
+
* Replaces any failure of a parser with the given user error, keeping the
|
|
786
|
+
* failure's position and mode.
|
|
787
|
+
*
|
|
788
|
+
* @since 0.1.0
|
|
789
|
+
* @category error management
|
|
790
|
+
*/
|
|
791
|
+
const orFail = /*#__PURE__*/ dual(2, (self, error) => {
|
|
792
|
+
const step = self.step;
|
|
793
|
+
return described((state) => {
|
|
794
|
+
const start = state.pos;
|
|
795
|
+
const a = step(state);
|
|
796
|
+
if (a === Fail && state.mode !== "Incomplete") {
|
|
797
|
+
const x = state.failure;
|
|
798
|
+
state.replaceFailure(new Failure(start, x.kind, [], x.context, true, error(), void 0, 0));
|
|
799
|
+
}
|
|
800
|
+
return a;
|
|
801
|
+
}, self.name, {
|
|
802
|
+
_tag: "OrFail",
|
|
803
|
+
self
|
|
804
|
+
});
|
|
721
805
|
});
|
|
722
806
|
/**
|
|
723
|
-
|
|
724
|
-
|
|
725
|
-
|
|
726
|
-
|
|
727
|
-
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
/**
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
807
|
+
* If the parser fails recoverably, tries `that` from the same position.
|
|
808
|
+
* `p.pipe(Parser.orElse(() => q))` is equivalent to `Parser.alt(p, q)`.
|
|
809
|
+
*
|
|
810
|
+
* @since 0.1.0
|
|
811
|
+
* @category error management
|
|
812
|
+
*/
|
|
813
|
+
const orElse = /*#__PURE__*/ dual(2, (self, that) => alt(self, suspend(that)));
|
|
814
|
+
/**
|
|
815
|
+
* If the parser fails recoverably, succeeds with the given value without
|
|
816
|
+
* consuming input.
|
|
817
|
+
*
|
|
818
|
+
* @since 0.1.0
|
|
819
|
+
* @category error management
|
|
820
|
+
*/
|
|
821
|
+
const orElseSucceed = /*#__PURE__*/ dual(2, (self, value) => {
|
|
822
|
+
const step = self.step;
|
|
823
|
+
return described((state) => {
|
|
824
|
+
const start = state.pos;
|
|
825
|
+
const a = step(state);
|
|
826
|
+
if (a !== Fail) return a;
|
|
827
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
828
|
+
recover(state, start);
|
|
829
|
+
return value();
|
|
830
|
+
}, self.name, {
|
|
831
|
+
_tag: "OrElseSucceed",
|
|
832
|
+
self,
|
|
833
|
+
value
|
|
834
|
+
});
|
|
749
835
|
});
|
|
750
836
|
/** Rewinds after a recoverable failure, remembering it as a hint. */
|
|
751
837
|
const recover = (state, start) => {
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
838
|
+
state.pos = start;
|
|
839
|
+
const f = state.failure;
|
|
840
|
+
if (f.offset === start) {
|
|
841
|
+
state.hintOffset = start;
|
|
842
|
+
state.hintExpected = f.expected;
|
|
843
|
+
}
|
|
758
844
|
};
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
return Option.none();
|
|
780
|
-
}, self.name, { _tag: "Opt", self });
|
|
845
|
+
/**
|
|
846
|
+
* Makes a parser optional: returns `Option.none()` without consuming input if
|
|
847
|
+
* it fails recoverably (winnow's and nom's `opt`).
|
|
848
|
+
*
|
|
849
|
+
* @since 0.1.0
|
|
850
|
+
* @category control
|
|
851
|
+
*/
|
|
852
|
+
const opt = (self) => {
|
|
853
|
+
const step = self.step;
|
|
854
|
+
return described((state) => {
|
|
855
|
+
const start = state.pos;
|
|
856
|
+
const a = step(state);
|
|
857
|
+
if (a !== Fail) return Option.some(a);
|
|
858
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
859
|
+
recover(state, start);
|
|
860
|
+
return Option.none();
|
|
861
|
+
}, self.name, {
|
|
862
|
+
_tag: "Opt",
|
|
863
|
+
self
|
|
864
|
+
});
|
|
781
865
|
};
|
|
782
866
|
/**
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
/**
|
|
790
|
-
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
/**
|
|
798
|
-
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
805
|
-
|
|
806
|
-
|
|
807
|
-
|
|
808
|
-
|
|
809
|
-
|
|
810
|
-
|
|
811
|
-
|
|
812
|
-
|
|
813
|
-
|
|
814
|
-
|
|
815
|
-
|
|
816
|
-
|
|
867
|
+
* Like {@link opt}, but returns `undefined` instead of `None`.
|
|
868
|
+
*
|
|
869
|
+
* @since 0.1.0
|
|
870
|
+
* @category control
|
|
871
|
+
*/
|
|
872
|
+
const optional = (self) => orElseSucceed(self, () => void 0);
|
|
873
|
+
/**
|
|
874
|
+
* Runs the parser only if `condition` holds, otherwise succeeds with
|
|
875
|
+
* `Option.none()` (winnow's and nom's `cond`).
|
|
876
|
+
*
|
|
877
|
+
* @since 0.1.0
|
|
878
|
+
* @category control
|
|
879
|
+
*/
|
|
880
|
+
const cond = (condition, self) => condition ? asSome(self) : succeed(Option.none());
|
|
881
|
+
/**
|
|
882
|
+
* Runs a parser without consuming input (winnow's and nom's `peek`).
|
|
883
|
+
*
|
|
884
|
+
* @since 0.1.0
|
|
885
|
+
* @category control
|
|
886
|
+
*/
|
|
887
|
+
const peek = (self) => {
|
|
888
|
+
const step = self.step;
|
|
889
|
+
return described((state) => {
|
|
890
|
+
const start = state.pos;
|
|
891
|
+
const hintOffset = state.hintOffset;
|
|
892
|
+
const hintExpected = state.hintExpected;
|
|
893
|
+
const deepest = state.deepest;
|
|
894
|
+
const a = step(state);
|
|
895
|
+
if (a !== Fail) {
|
|
896
|
+
state.pos = start;
|
|
897
|
+
state.hintOffset = hintOffset;
|
|
898
|
+
state.hintExpected = hintExpected;
|
|
899
|
+
state.deepest = deepest;
|
|
900
|
+
}
|
|
901
|
+
return a;
|
|
902
|
+
}, self.name, {
|
|
903
|
+
_tag: "Peek",
|
|
904
|
+
self
|
|
905
|
+
});
|
|
817
906
|
};
|
|
818
907
|
/**
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
908
|
+
* Succeeds, without consuming input, only if the parser fails recoverably
|
|
909
|
+
* (winnow's and nom's `not`).
|
|
910
|
+
*
|
|
911
|
+
* @since 0.1.0
|
|
912
|
+
* @category control
|
|
913
|
+
*/
|
|
914
|
+
const not = (self) => {
|
|
915
|
+
const step = self.step;
|
|
916
|
+
return described((state) => {
|
|
917
|
+
const start = state.pos;
|
|
918
|
+
const hintOffset = state.hintOffset;
|
|
919
|
+
const hintExpected = state.hintExpected;
|
|
920
|
+
const deepest = state.deepest;
|
|
921
|
+
if (step(state) !== Fail) {
|
|
922
|
+
state.pos = start;
|
|
923
|
+
state.deepest = deepest;
|
|
924
|
+
return state.backtrack("Not");
|
|
925
|
+
}
|
|
926
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
927
|
+
state.pos = start;
|
|
928
|
+
state.hintOffset = hintOffset;
|
|
929
|
+
state.hintExpected = hintExpected;
|
|
930
|
+
state.deepest = deepest;
|
|
931
|
+
}, self.name, {
|
|
932
|
+
_tag: "Not",
|
|
933
|
+
self
|
|
934
|
+
});
|
|
843
935
|
};
|
|
844
936
|
/**
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
|
|
851
|
-
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
858
|
-
|
|
859
|
-
|
|
860
|
-
|
|
861
|
-
|
|
862
|
-
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
870
|
-
|
|
871
|
-
|
|
872
|
-
|
|
873
|
-
|
|
937
|
+
* Requires the parser to consume the whole input (nom's `all_consuming`).
|
|
938
|
+
*
|
|
939
|
+
* @since 0.1.0
|
|
940
|
+
* @category control
|
|
941
|
+
*/
|
|
942
|
+
const allConsuming = (self) => terminated(self, eof);
|
|
943
|
+
/**
|
|
944
|
+
* Runs parsers in sequence, collecting their outputs in a tuple (winnow's
|
|
945
|
+
* and nom's tuple parsers, winnow's `seq!`).
|
|
946
|
+
*
|
|
947
|
+
* @since 0.1.0
|
|
948
|
+
* @category sequencing
|
|
949
|
+
*/
|
|
950
|
+
const seq = (...parsers) => {
|
|
951
|
+
const steps = parsers.map((p) => p.step);
|
|
952
|
+
const n = steps.length;
|
|
953
|
+
return described((state) => {
|
|
954
|
+
const out = new Array(n);
|
|
955
|
+
for (let i = 0; i < n; i++) {
|
|
956
|
+
const a = steps[i](state);
|
|
957
|
+
if (a === Fail) return Fail;
|
|
958
|
+
out[i] = a;
|
|
959
|
+
}
|
|
960
|
+
return out;
|
|
961
|
+
}, "seq", {
|
|
962
|
+
_tag: "Seq",
|
|
963
|
+
parsers,
|
|
964
|
+
output: "tuple"
|
|
965
|
+
});
|
|
874
966
|
};
|
|
875
967
|
/**
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
968
|
+
* Runs a record of parsers in sequence (in key order), collecting their
|
|
969
|
+
* outputs in a record. This is the Effect counterpart to winnow's
|
|
970
|
+
* `seq!(Struct { .. })`.
|
|
971
|
+
*
|
|
972
|
+
* @since 0.1.0
|
|
973
|
+
* @category sequencing
|
|
974
|
+
*/
|
|
975
|
+
const struct = (fields) => {
|
|
976
|
+
const keys = Object.keys(fields);
|
|
977
|
+
const steps = keys.map((k) => fields[k].step);
|
|
978
|
+
const n = keys.length;
|
|
979
|
+
return described((state) => {
|
|
980
|
+
const out = {};
|
|
981
|
+
for (let i = 0; i < n; i++) {
|
|
982
|
+
const a = steps[i](state);
|
|
983
|
+
if (a === Fail) return Fail;
|
|
984
|
+
out[keys[i]] = a;
|
|
985
|
+
}
|
|
986
|
+
return out;
|
|
987
|
+
}, "struct", {
|
|
988
|
+
_tag: "Struct",
|
|
989
|
+
keys,
|
|
990
|
+
parsers: keys.map((k) => fields[k])
|
|
991
|
+
});
|
|
897
992
|
};
|
|
898
993
|
/**
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
/**
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
994
|
+
* Runs two parsers in sequence and returns both outputs.
|
|
995
|
+
*
|
|
996
|
+
* @since 0.1.0
|
|
997
|
+
* @category sequencing
|
|
998
|
+
*/
|
|
999
|
+
const zip = /*#__PURE__*/ dual(2, (self, that) => zipWith(self, that, (a, b) => [a, b]));
|
|
1000
|
+
/**
|
|
1001
|
+
* Runs two parsers in sequence and combines their outputs.
|
|
1002
|
+
*
|
|
1003
|
+
* @since 0.1.0
|
|
1004
|
+
* @category sequencing
|
|
1005
|
+
*/
|
|
1006
|
+
const zipWith = /*#__PURE__*/ dual(3, (self, that, f) => {
|
|
1007
|
+
const s1 = self.step;
|
|
1008
|
+
const s2 = that.step;
|
|
1009
|
+
return described((state) => {
|
|
1010
|
+
const a = s1(state);
|
|
1011
|
+
if (a === Fail) return Fail;
|
|
1012
|
+
const b = s2(state);
|
|
1013
|
+
if (b === Fail) return Fail;
|
|
1014
|
+
return f(a, b);
|
|
1015
|
+
}, void 0, {
|
|
1016
|
+
_tag: "ZipWith",
|
|
1017
|
+
self,
|
|
1018
|
+
that,
|
|
1019
|
+
f
|
|
1020
|
+
});
|
|
923
1021
|
});
|
|
924
1022
|
/**
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
|
|
929
|
-
|
|
930
|
-
|
|
931
|
-
/**
|
|
932
|
-
|
|
933
|
-
|
|
934
|
-
|
|
935
|
-
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
/**
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
|
|
950
|
-
|
|
951
|
-
|
|
952
|
-
|
|
1023
|
+
* Runs two parsers in sequence, keeping the output of the first.
|
|
1024
|
+
*
|
|
1025
|
+
* @since 0.1.0
|
|
1026
|
+
* @category sequencing
|
|
1027
|
+
*/
|
|
1028
|
+
const zipLeft = /*#__PURE__*/ dual(2, (self, that) => terminated(self, that));
|
|
1029
|
+
/**
|
|
1030
|
+
* Runs two parsers in sequence, keeping the output of the second.
|
|
1031
|
+
*
|
|
1032
|
+
* @since 0.1.0
|
|
1033
|
+
* @category sequencing
|
|
1034
|
+
*/
|
|
1035
|
+
const zipRight = /*#__PURE__*/ dual(2, (self, that) => preceded(self, that));
|
|
1036
|
+
/**
|
|
1037
|
+
* Runs `prefix` then `parser`, keeping the output of `parser` (winnow's and
|
|
1038
|
+
* nom's `preceded`).
|
|
1039
|
+
*
|
|
1040
|
+
* @since 0.1.0
|
|
1041
|
+
* @category sequencing
|
|
1042
|
+
*/
|
|
1043
|
+
const preceded = (prefix, parser) => {
|
|
1044
|
+
const s1 = prefix.step;
|
|
1045
|
+
const s2 = parser.step;
|
|
1046
|
+
return described((state) => s1(state) === Fail ? Fail : s2(state), parser.name, {
|
|
1047
|
+
_tag: "Seq",
|
|
1048
|
+
parsers: [prefix, parser],
|
|
1049
|
+
output: 1
|
|
1050
|
+
});
|
|
953
1051
|
};
|
|
954
1052
|
/**
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
1053
|
+
* Runs `parser` then `suffix`, keeping the output of `parser` (winnow's and
|
|
1054
|
+
* nom's `terminated`).
|
|
1055
|
+
*
|
|
1056
|
+
* @since 0.1.0
|
|
1057
|
+
* @category sequencing
|
|
1058
|
+
*/
|
|
1059
|
+
const terminated = (parser, suffix) => {
|
|
1060
|
+
const s1 = parser.step;
|
|
1061
|
+
const s2 = suffix.step;
|
|
1062
|
+
return described((state) => {
|
|
1063
|
+
const a = s1(state);
|
|
1064
|
+
if (a === Fail) return Fail;
|
|
1065
|
+
return s2(state) === Fail ? Fail : a;
|
|
1066
|
+
}, parser.name, {
|
|
1067
|
+
_tag: "Seq",
|
|
1068
|
+
parsers: [parser, suffix],
|
|
1069
|
+
output: 0
|
|
1070
|
+
});
|
|
970
1071
|
};
|
|
971
1072
|
/**
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
1073
|
+
* Runs `open`, `parser` and `close`, keeping the output of `parser`
|
|
1074
|
+
* (winnow's and nom's `delimited`).
|
|
1075
|
+
*
|
|
1076
|
+
* @since 0.1.0
|
|
1077
|
+
* @category sequencing
|
|
1078
|
+
*/
|
|
1079
|
+
const delimited = (open, parser, close) => {
|
|
1080
|
+
const s1 = open.step;
|
|
1081
|
+
const s2 = parser.step;
|
|
1082
|
+
const s3 = close.step;
|
|
1083
|
+
return described((state) => {
|
|
1084
|
+
if (s1(state) === Fail) return Fail;
|
|
1085
|
+
const a = s2(state);
|
|
1086
|
+
if (a === Fail) return Fail;
|
|
1087
|
+
return s3(state) === Fail ? Fail : a;
|
|
1088
|
+
}, parser.name, {
|
|
1089
|
+
_tag: "Seq",
|
|
1090
|
+
parsers: [
|
|
1091
|
+
open,
|
|
1092
|
+
parser,
|
|
1093
|
+
close
|
|
1094
|
+
],
|
|
1095
|
+
output: 1
|
|
1096
|
+
});
|
|
990
1097
|
};
|
|
991
1098
|
/**
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1099
|
+
* Runs `first`, `separator` and `second`, keeping the outputs of `first` and
|
|
1100
|
+
* `second` (winnow's and nom's `separated_pair`).
|
|
1101
|
+
*
|
|
1102
|
+
* @since 0.1.0
|
|
1103
|
+
* @category sequencing
|
|
1104
|
+
*/
|
|
1105
|
+
const separatedPair = (first, separator, second) => {
|
|
1106
|
+
const s1 = first.step;
|
|
1107
|
+
const s2 = separator.step;
|
|
1108
|
+
const s3 = second.step;
|
|
1109
|
+
return described((state) => {
|
|
1110
|
+
const a = s1(state);
|
|
1111
|
+
if (a === Fail) return Fail;
|
|
1112
|
+
if (s2(state) === Fail) return Fail;
|
|
1113
|
+
const b = s3(state);
|
|
1114
|
+
return b === Fail ? Fail : [a, b];
|
|
1115
|
+
}, void 0, {
|
|
1116
|
+
_tag: "Seq",
|
|
1117
|
+
parsers: [
|
|
1118
|
+
first,
|
|
1119
|
+
separator,
|
|
1120
|
+
second
|
|
1121
|
+
],
|
|
1122
|
+
output: [0, 2]
|
|
1123
|
+
});
|
|
1011
1124
|
};
|
|
1012
1125
|
/**
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
|
|
1037
|
-
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1126
|
+
* Writes a parser in imperative style with a generator, Effect style:
|
|
1127
|
+
* `yield*` a parser to run it and get its output.
|
|
1128
|
+
*
|
|
1129
|
+
* @example
|
|
1130
|
+
* ```ts
|
|
1131
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1132
|
+
*
|
|
1133
|
+
* const keyValue = Parser.gen(function*() {
|
|
1134
|
+
* const key = yield* Ascii.alpha1
|
|
1135
|
+
* yield* Token.literal("=")
|
|
1136
|
+
* const value = yield* Ascii.decInt
|
|
1137
|
+
* return [key, value] as const
|
|
1138
|
+
* })
|
|
1139
|
+
* ```
|
|
1140
|
+
*
|
|
1141
|
+
* The body runs on every parse, so parsers created inside it, like
|
|
1142
|
+
* `Token.literal("=")` above, are rebuilt on every run. On hot paths, build
|
|
1143
|
+
* them once outside the body:
|
|
1144
|
+
*
|
|
1145
|
+
* ```ts
|
|
1146
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1147
|
+
*
|
|
1148
|
+
* const equals = Token.literal("=")
|
|
1149
|
+
*
|
|
1150
|
+
* const keyValue = Parser.gen(function*() {
|
|
1151
|
+
* const key = yield* Ascii.alpha1
|
|
1152
|
+
* yield* equals
|
|
1153
|
+
* const value = yield* Ascii.decInt
|
|
1154
|
+
* return [key, value] as const
|
|
1155
|
+
* })
|
|
1156
|
+
* ```
|
|
1157
|
+
*
|
|
1158
|
+
* @since 0.1.0
|
|
1159
|
+
* @category sequencing
|
|
1160
|
+
*/
|
|
1161
|
+
const gen = (f) => make((state) => {
|
|
1162
|
+
const iterator = f();
|
|
1163
|
+
let next = iterator.next();
|
|
1164
|
+
while (!next.done) {
|
|
1165
|
+
const a = next.value.step(state);
|
|
1166
|
+
if (a === Fail) {
|
|
1167
|
+
iterator.return?.(void 0);
|
|
1168
|
+
return Fail;
|
|
1169
|
+
}
|
|
1170
|
+
next = iterator.next(a);
|
|
1171
|
+
}
|
|
1172
|
+
return next.value;
|
|
1043
1173
|
}, "gen");
|
|
1044
1174
|
/**
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
/**
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
return Fail;
|
|
1119
|
-
failure = failure === undefined ? state.failure : mergeFailures(failure, state.failure);
|
|
1120
|
-
state.pos = start;
|
|
1121
|
-
}
|
|
1122
|
-
return state.failWith("Backtrack", failure);
|
|
1123
|
-
}, "alt", { _tag: "Alt", parsers });
|
|
1175
|
+
* The starting point for Do notation.
|
|
1176
|
+
*
|
|
1177
|
+
* @example
|
|
1178
|
+
* ```ts
|
|
1179
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1180
|
+
*
|
|
1181
|
+
* const range = Parser.Do.pipe(
|
|
1182
|
+
* Parser.bind("start", () => Ascii.decUint),
|
|
1183
|
+
* Parser.zipLeft(Token.literal("..")),
|
|
1184
|
+
* Parser.bind("end", () => Ascii.decUint)
|
|
1185
|
+
* )
|
|
1186
|
+
* ```
|
|
1187
|
+
*
|
|
1188
|
+
* @since 0.1.0
|
|
1189
|
+
* @category do notation
|
|
1190
|
+
*/
|
|
1191
|
+
const Do = /*#__PURE__*/ succeed({});
|
|
1192
|
+
/**
|
|
1193
|
+
* Binds the output of a parser to a field of the Do record.
|
|
1194
|
+
*
|
|
1195
|
+
* @since 0.1.0
|
|
1196
|
+
* @category do notation
|
|
1197
|
+
*/
|
|
1198
|
+
const bind = /*#__PURE__*/ dual(3, (self, name, f) => flatMap(self, (a) => map(f(a), (b) => ({
|
|
1199
|
+
...a,
|
|
1200
|
+
[name]: b
|
|
1201
|
+
}))));
|
|
1202
|
+
/**
|
|
1203
|
+
* Wraps the output of a parser in a record with a single field.
|
|
1204
|
+
*
|
|
1205
|
+
* @since 0.1.0
|
|
1206
|
+
* @category do notation
|
|
1207
|
+
*/
|
|
1208
|
+
const bindTo = /*#__PURE__*/ dual(2, (self, name) => map(self, (a) => ({ [name]: a })));
|
|
1209
|
+
const let_ = /*#__PURE__*/ dual(3, (self, name, f) => map(self, (a) => ({
|
|
1210
|
+
...a,
|
|
1211
|
+
[name]: f(a)
|
|
1212
|
+
})));
|
|
1213
|
+
/**
|
|
1214
|
+
* Tries each parser in turn and returns the output of the first that
|
|
1215
|
+
* succeeds (winnow's and nom's `alt`).
|
|
1216
|
+
*
|
|
1217
|
+
* Unrecoverable (`Cut`) and `Incomplete` failures stop the search
|
|
1218
|
+
* immediately. If every alternative fails recoverably, the failures are
|
|
1219
|
+
* merged: the one that got furthest wins, and failures at the same offset
|
|
1220
|
+
* combine their expectations, e.g. `expected "true", "false" or "null"`.
|
|
1221
|
+
*
|
|
1222
|
+
* @since 0.1.0
|
|
1223
|
+
* @category choice
|
|
1224
|
+
*/
|
|
1225
|
+
const alt = (...parsers) => {
|
|
1226
|
+
const steps = parsers.map((p) => p.step);
|
|
1227
|
+
const n = steps.length;
|
|
1228
|
+
if (n === 0) return reject;
|
|
1229
|
+
if (n === 1) return parsers[0];
|
|
1230
|
+
return described((state) => {
|
|
1231
|
+
const start = state.pos;
|
|
1232
|
+
let failure;
|
|
1233
|
+
for (let i = 0; i < n; i++) {
|
|
1234
|
+
const a = steps[i](state);
|
|
1235
|
+
if (a !== Fail) {
|
|
1236
|
+
if (failure !== void 0 && failure.offset === state.pos) state.setHint(failure.offset, failure.expected);
|
|
1237
|
+
return a;
|
|
1238
|
+
}
|
|
1239
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1240
|
+
failure = failure === void 0 ? state.failure : mergeFailures(failure, state.failure);
|
|
1241
|
+
state.pos = start;
|
|
1242
|
+
}
|
|
1243
|
+
return state.failWith("Backtrack", failure ?? state.failure);
|
|
1244
|
+
}, "alt", {
|
|
1245
|
+
_tag: "Alt",
|
|
1246
|
+
parsers
|
|
1247
|
+
});
|
|
1124
1248
|
};
|
|
1125
1249
|
/**
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1250
|
+
* Selects the parser to run next based on the output of `selector`, like a
|
|
1251
|
+
* `match` over the input (winnow's `dispatch!`). If no case matches, the
|
|
1252
|
+
* parser backtracks to where it started.
|
|
1253
|
+
*
|
|
1254
|
+
* Cases are given either as a record keyed by the selector's output (with
|
|
1255
|
+
* an optional `otherwise` parser for unlisted keys, like winnow's `_ =>`
|
|
1256
|
+
* arm), or as a function returning a parser (or `undefined` for no match).
|
|
1257
|
+
*
|
|
1258
|
+
* Prefer a record where the keys are known. Compiled code (see the
|
|
1259
|
+
* `Compiler` module) switches on the key and runs every case compiled; the
|
|
1260
|
+
* parser a function returns always runs interpreted, and one the function
|
|
1261
|
+
* builds (`(c) => Parser.cut(p)`) is rebuilt on every run.
|
|
1262
|
+
*
|
|
1263
|
+
* @example
|
|
1264
|
+
* ```ts
|
|
1265
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1266
|
+
*
|
|
1267
|
+
* const escape = Parser.preceded(
|
|
1268
|
+
* Token.literal("\\"),
|
|
1269
|
+
* Parser.dispatch(Token.any, {
|
|
1270
|
+
* n: Parser.succeed("\n"),
|
|
1271
|
+
* t: Parser.succeed("\t"),
|
|
1272
|
+
* u: Parser.preceded(Token.literal("{"), Ascii.hexUint).pipe(
|
|
1273
|
+
* Parser.zipLeft(Token.literal("}")),
|
|
1274
|
+
* Parser.map((cp) => String.fromCodePoint(cp))
|
|
1275
|
+
* )
|
|
1276
|
+
* })
|
|
1277
|
+
* )
|
|
1278
|
+
* ```
|
|
1279
|
+
*
|
|
1280
|
+
* @since 0.1.0
|
|
1281
|
+
* @category choice
|
|
1282
|
+
*/
|
|
1283
|
+
const dispatch = (selector, cases, otherwise) => {
|
|
1284
|
+
const choose = Predicate.isFunction(cases) ? cases : (key) => Object.prototype.hasOwnProperty.call(cases, key) ? cases[key] : otherwise;
|
|
1285
|
+
const step = selector.step;
|
|
1286
|
+
return described((state) => {
|
|
1287
|
+
const start = state.pos;
|
|
1288
|
+
const key = step(state);
|
|
1289
|
+
if (key === Fail) return Fail;
|
|
1290
|
+
const parser = choose(key);
|
|
1291
|
+
if (parser === void 0) {
|
|
1292
|
+
state.pos = start;
|
|
1293
|
+
return state.backtrack("Dispatch");
|
|
1294
|
+
}
|
|
1295
|
+
return parser.step(state);
|
|
1296
|
+
}, "dispatch", {
|
|
1297
|
+
_tag: "Dispatch",
|
|
1298
|
+
selector,
|
|
1299
|
+
cases: Predicate.isFunction(cases) ? void 0 : cases,
|
|
1300
|
+
otherwise: Predicate.isFunction(cases) ? void 0 : otherwise,
|
|
1301
|
+
choose
|
|
1302
|
+
});
|
|
1175
1303
|
};
|
|
1176
1304
|
/**
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
}
|
|
1214
|
-
return out;
|
|
1215
|
-
}, "permutation");
|
|
1305
|
+
* Applies every parser exactly once, in whatever order they match, and
|
|
1306
|
+
* returns their outputs in the order the parsers were given (winnow's and
|
|
1307
|
+
* nom's `permutation`).
|
|
1308
|
+
*
|
|
1309
|
+
* @since 0.1.0
|
|
1310
|
+
* @category choice
|
|
1311
|
+
*/
|
|
1312
|
+
const permutation = (...parsers) => {
|
|
1313
|
+
const steps = parsers.map((p) => p.step);
|
|
1314
|
+
const n = steps.length;
|
|
1315
|
+
return make((state) => {
|
|
1316
|
+
const out = new Array(n);
|
|
1317
|
+
const done = new Array(n).fill(false);
|
|
1318
|
+
let remaining = n;
|
|
1319
|
+
while (remaining > 0) {
|
|
1320
|
+
let failure;
|
|
1321
|
+
let matched = false;
|
|
1322
|
+
for (let i = 0; i < n; i++) {
|
|
1323
|
+
if (done[i]) continue;
|
|
1324
|
+
const start = state.pos;
|
|
1325
|
+
const a = steps[i](state);
|
|
1326
|
+
if (a !== Fail) {
|
|
1327
|
+
out[i] = a;
|
|
1328
|
+
done[i] = true;
|
|
1329
|
+
remaining--;
|
|
1330
|
+
matched = true;
|
|
1331
|
+
break;
|
|
1332
|
+
}
|
|
1333
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1334
|
+
failure = failure === void 0 ? state.failure : mergeFailures(failure, state.failure);
|
|
1335
|
+
state.pos = start;
|
|
1336
|
+
}
|
|
1337
|
+
if (!matched) return state.failWith("Backtrack", failure ?? state.failure);
|
|
1338
|
+
}
|
|
1339
|
+
return out;
|
|
1340
|
+
}, "permutation");
|
|
1216
1341
|
};
|
|
1217
|
-
// -----------------------------------------------------------------------------
|
|
1218
|
-
// Repetition
|
|
1219
|
-
// -----------------------------------------------------------------------------
|
|
1220
1342
|
const normalizeRange = (range) => {
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
if (min > max)
|
|
1228
|
-
throw new RangeError(`invalid range: min (${min}) > max (${max})`);
|
|
1229
|
-
return [min, max];
|
|
1343
|
+
if (range === void 0) return [0, Infinity];
|
|
1344
|
+
if (Predicate.isNumber(range)) return [range, range];
|
|
1345
|
+
const min = range.min ?? 0;
|
|
1346
|
+
const max = range.max ?? Infinity;
|
|
1347
|
+
if (min > max) throw new RangeError(`invalid range: min (${min}) > max (${max})`);
|
|
1348
|
+
return [min, max];
|
|
1230
1349
|
};
|
|
1231
1350
|
const assertConsumes = (state, name) => state.failMessage("Cut", "Assert", `\`${name}\` parsers must always consume`);
|
|
1232
|
-
const saveFold = (state, key, entry, acc, pos) => Resume.save(state, key, entry, {
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
|
|
1237
|
-
|
|
1238
|
-
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1244
|
-
|
|
1245
|
-
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1250
|
-
|
|
1251
|
-
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1351
|
+
const saveFold = (state, key, entry, acc, pos) => Resume.save(state, key, entry, {
|
|
1352
|
+
acc,
|
|
1353
|
+
count: 0,
|
|
1354
|
+
pos
|
|
1355
|
+
});
|
|
1356
|
+
const foldImpl = (self, range, init, f, name, node, cond) => {
|
|
1357
|
+
const [min, max] = normalizeRange(range);
|
|
1358
|
+
const step = self.step;
|
|
1359
|
+
const checkLoop = max === Infinity;
|
|
1360
|
+
const key = {};
|
|
1361
|
+
return described((state) => {
|
|
1362
|
+
const entry = state.pos;
|
|
1363
|
+
let acc;
|
|
1364
|
+
let count = 0;
|
|
1365
|
+
const resumed = state.resume === void 0 ? void 0 : Resume.take(state, key, entry);
|
|
1366
|
+
if (resumed instanceof Resume.Done) {
|
|
1367
|
+
state.pos = resumed.pos;
|
|
1368
|
+
return resumed.value;
|
|
1369
|
+
}
|
|
1370
|
+
if (resumed !== void 0) {
|
|
1371
|
+
acc = resumed.acc;
|
|
1372
|
+
count = resumed.count;
|
|
1373
|
+
state.pos = resumed.pos;
|
|
1374
|
+
} else acc = init();
|
|
1375
|
+
for (; count < max; count++) {
|
|
1376
|
+
if (cond !== void 0 && count >= min && !cond(acc)) break;
|
|
1377
|
+
const start = state.pos;
|
|
1378
|
+
const a = step(state);
|
|
1379
|
+
if (a === Fail) {
|
|
1380
|
+
if (state.mode === "Incomplete" && state.resume !== void 0) Resume.save(state, key, entry, {
|
|
1381
|
+
acc,
|
|
1382
|
+
count,
|
|
1383
|
+
pos: start
|
|
1384
|
+
});
|
|
1385
|
+
if (state.mode !== "Backtrack" || count < min) return Fail;
|
|
1386
|
+
recover(state, start);
|
|
1387
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1388
|
+
}
|
|
1389
|
+
if (checkLoop && state.pos === start) return assertConsumes(state, name);
|
|
1390
|
+
acc = f(acc, a);
|
|
1391
|
+
}
|
|
1392
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1393
|
+
}, name, node ?? {
|
|
1394
|
+
_tag: "Fold",
|
|
1395
|
+
self,
|
|
1396
|
+
init,
|
|
1397
|
+
f,
|
|
1398
|
+
while: cond,
|
|
1399
|
+
name,
|
|
1400
|
+
...bounds(range)
|
|
1401
|
+
});
|
|
1273
1402
|
};
|
|
1274
1403
|
/**
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
|
|
1278
|
-
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
}, "repeat", {
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
|
|
1328
|
-
|
|
1329
|
-
|
|
1330
|
-
|
|
1331
|
-
|
|
1332
|
-
|
|
1333
|
-
|
|
1334
|
-
|
|
1335
|
-
|
|
1336
|
-
|
|
1337
|
-
|
|
1338
|
-
|
|
1339
|
-
|
|
1340
|
-
|
|
1341
|
-
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1348
|
-
|
|
1349
|
-
|
|
1404
|
+
* Applies a parser repeatedly, collecting the outputs in an array (winnow's
|
|
1405
|
+
* `repeat`, nom's `many0`, `many1`, `many_m_n` and `count`).
|
|
1406
|
+
*
|
|
1407
|
+
* The range defaults to `{ min: 0 }` (zero or more). A number repeats
|
|
1408
|
+
* exactly that many times. With a literal positive `min`, the output is
|
|
1409
|
+
* typed as a non-empty array.
|
|
1410
|
+
*
|
|
1411
|
+
* As in winnow, an unbounded repetition of a parser that succeeds without
|
|
1412
|
+
* consuming input fails with an unrecoverable assertion instead of looping
|
|
1413
|
+
* forever.
|
|
1414
|
+
*
|
|
1415
|
+
* @example
|
|
1416
|
+
* ```ts
|
|
1417
|
+
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1418
|
+
*
|
|
1419
|
+
* const abs = Parser.repeat(Token.literal("ab"), { min: 1 })
|
|
1420
|
+
* Parser.parse(abs, "ababab") // Result.succeed(["ab", "ab", "ab"])
|
|
1421
|
+
* ```
|
|
1422
|
+
*
|
|
1423
|
+
* @since 0.1.0
|
|
1424
|
+
* @category repetition
|
|
1425
|
+
*/
|
|
1426
|
+
const repeat = /*#__PURE__*/ dual(isParserArg, (self, range) => foldImpl(self, range, () => [], (acc, a) => {
|
|
1427
|
+
acc.push(a);
|
|
1428
|
+
return acc;
|
|
1429
|
+
}, "repeat", {
|
|
1430
|
+
_tag: "Repeat",
|
|
1431
|
+
self,
|
|
1432
|
+
...bounds(range)
|
|
1433
|
+
}));
|
|
1434
|
+
/**
|
|
1435
|
+
* Applies a parser repeatedly, folding the outputs into an accumulator
|
|
1436
|
+
* (winnow's `repeat(..).fold(..)`, nom's `fold_many0`/`fold_many1`/
|
|
1437
|
+
* `fold_many_m_n`).
|
|
1438
|
+
*
|
|
1439
|
+
* With `while`, the repetition also stops, successfully and without
|
|
1440
|
+
* consuming more input, as soon as the predicate is false of the
|
|
1441
|
+
* accumulator. It is checked before each element once `range.min` elements
|
|
1442
|
+
* have been folded, so `min` still holds.
|
|
1443
|
+
*
|
|
1444
|
+
* @example
|
|
1445
|
+
* ```ts
|
|
1446
|
+
* import { Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1447
|
+
*
|
|
1448
|
+
* // Keeps the first two digits, leaving the rest unparsed
|
|
1449
|
+
* const two = Parser.fold(Token.oneOf("0123456789"), {
|
|
1450
|
+
* init: () => "",
|
|
1451
|
+
* f: (acc, d) => acc + d,
|
|
1452
|
+
* while: (acc) => acc.length < 2
|
|
1453
|
+
* })
|
|
1454
|
+
* Parser.parsePeek(two, "1234") // Result.succeed(["34", "12"])
|
|
1455
|
+
* ```
|
|
1456
|
+
*
|
|
1457
|
+
* @since 0.1.0
|
|
1458
|
+
* @category repetition
|
|
1459
|
+
*/
|
|
1460
|
+
const fold = /*#__PURE__*/ dual(2, (self, options) => foldImpl(self, options.range, options.init, options.f, "fold", void 0, options.while));
|
|
1461
|
+
/**
|
|
1462
|
+
* Applies `self` repeatedly until `end` succeeds, returning the collected
|
|
1463
|
+
* outputs and the output of `end` (winnow's `repeat_till`, nom's
|
|
1464
|
+
* `many_till`).
|
|
1465
|
+
*
|
|
1466
|
+
* @since 0.1.0
|
|
1467
|
+
* @category repetition
|
|
1468
|
+
*/
|
|
1469
|
+
const repeatTill = /*#__PURE__*/ dual(isTwoParserArgs, (self, end, range) => {
|
|
1470
|
+
const [min, max] = normalizeRange(range);
|
|
1471
|
+
const step = self.step;
|
|
1472
|
+
const endStep = end.step;
|
|
1473
|
+
return described((state) => {
|
|
1474
|
+
const acc = [];
|
|
1475
|
+
while (true) {
|
|
1476
|
+
const start = state.pos;
|
|
1477
|
+
let endFailure;
|
|
1478
|
+
if (acc.length >= min) {
|
|
1479
|
+
const b = endStep(state);
|
|
1480
|
+
if (b !== Fail) return [acc, b];
|
|
1481
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1482
|
+
endFailure = state.failure;
|
|
1483
|
+
state.pos = start;
|
|
1484
|
+
if (acc.length >= max) return Fail;
|
|
1485
|
+
}
|
|
1486
|
+
const a = step(state);
|
|
1487
|
+
if (a === Fail) {
|
|
1488
|
+
if (endFailure !== void 0 && state.mode === "Backtrack") state.failWith("Backtrack", mergeFailures(endFailure, state.failure));
|
|
1489
|
+
return Fail;
|
|
1490
|
+
}
|
|
1491
|
+
if (state.pos === start) return assertConsumes(state, "repeatTill");
|
|
1492
|
+
acc.push(a);
|
|
1493
|
+
}
|
|
1494
|
+
}, "repeatTill", {
|
|
1495
|
+
_tag: "RepeatTill",
|
|
1496
|
+
self,
|
|
1497
|
+
end,
|
|
1498
|
+
...bounds(range)
|
|
1499
|
+
});
|
|
1350
1500
|
});
|
|
1351
|
-
|
|
1352
|
-
|
|
1353
|
-
|
|
1354
|
-
|
|
1355
|
-
|
|
1356
|
-
|
|
1357
|
-
|
|
1358
|
-
|
|
1359
|
-
|
|
1360
|
-
|
|
1361
|
-
|
|
1362
|
-
|
|
1363
|
-
|
|
1364
|
-
|
|
1365
|
-
|
|
1366
|
-
|
|
1367
|
-
|
|
1368
|
-
|
|
1369
|
-
|
|
1370
|
-
|
|
1371
|
-
|
|
1372
|
-
|
|
1373
|
-
|
|
1374
|
-
|
|
1375
|
-
|
|
1376
|
-
|
|
1377
|
-
|
|
1378
|
-
|
|
1379
|
-
|
|
1380
|
-
|
|
1381
|
-
|
|
1382
|
-
|
|
1383
|
-
|
|
1384
|
-
|
|
1385
|
-
|
|
1386
|
-
|
|
1387
|
-
|
|
1388
|
-
|
|
1389
|
-
|
|
1390
|
-
|
|
1391
|
-
|
|
1392
|
-
|
|
1393
|
-
|
|
1394
|
-
|
|
1395
|
-
|
|
1396
|
-
|
|
1397
|
-
|
|
1398
|
-
|
|
1399
|
-
|
|
1400
|
-
|
|
1401
|
-
|
|
1402
|
-
|
|
1403
|
-
|
|
1404
|
-
|
|
1405
|
-
|
|
1406
|
-
|
|
1407
|
-
|
|
1408
|
-
|
|
1409
|
-
|
|
1501
|
+
/**
|
|
1502
|
+
* Applies `self` repeatedly, separated by `separator`, collecting the
|
|
1503
|
+
* outputs of `self` (winnow's `separated`, nom's `separated_list0` and
|
|
1504
|
+
* `separated_list1`). A trailing separator is not consumed.
|
|
1505
|
+
*
|
|
1506
|
+
* @example
|
|
1507
|
+
* ```ts
|
|
1508
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1509
|
+
*
|
|
1510
|
+
* const csv = Parser.separated(Ascii.decInt, Token.literal(","))
|
|
1511
|
+
* Parser.parse(csv, "1,2,3") // Result.succeed([1, 2, 3])
|
|
1512
|
+
* ```
|
|
1513
|
+
*
|
|
1514
|
+
* @since 0.1.0
|
|
1515
|
+
* @category repetition
|
|
1516
|
+
*/
|
|
1517
|
+
const separated = /*#__PURE__*/ dual(isTwoParserArgs, (self, separator, range) => {
|
|
1518
|
+
const [min, max] = normalizeRange(range);
|
|
1519
|
+
const step = self.step;
|
|
1520
|
+
const sepStep = separator.step;
|
|
1521
|
+
const checkLoop = max === Infinity;
|
|
1522
|
+
const key = {};
|
|
1523
|
+
return described((state) => {
|
|
1524
|
+
const entry = state.pos;
|
|
1525
|
+
let acc;
|
|
1526
|
+
const resumed = state.resume === void 0 ? void 0 : Resume.take(state, key, entry);
|
|
1527
|
+
if (resumed instanceof Resume.Done) {
|
|
1528
|
+
state.pos = resumed.pos;
|
|
1529
|
+
return resumed.value;
|
|
1530
|
+
}
|
|
1531
|
+
if (resumed !== void 0) {
|
|
1532
|
+
acc = resumed.acc;
|
|
1533
|
+
state.pos = resumed.pos;
|
|
1534
|
+
} else {
|
|
1535
|
+
acc = [];
|
|
1536
|
+
if (max === 0) return acc;
|
|
1537
|
+
const start = state.pos;
|
|
1538
|
+
const first = step(state);
|
|
1539
|
+
if (first === Fail) {
|
|
1540
|
+
if (state.mode !== "Backtrack" || min > 0) return Fail;
|
|
1541
|
+
recover(state, start);
|
|
1542
|
+
return acc;
|
|
1543
|
+
}
|
|
1544
|
+
acc.push(first);
|
|
1545
|
+
}
|
|
1546
|
+
while (acc.length < max) {
|
|
1547
|
+
const start = state.pos;
|
|
1548
|
+
if (sepStep(state) === Fail) {
|
|
1549
|
+
if (state.mode === "Incomplete" && state.resume !== void 0) Resume.save(state, key, entry, {
|
|
1550
|
+
acc,
|
|
1551
|
+
pos: start
|
|
1552
|
+
});
|
|
1553
|
+
if (state.mode !== "Backtrack" || acc.length < min) return Fail;
|
|
1554
|
+
recover(state, start);
|
|
1555
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1556
|
+
}
|
|
1557
|
+
if (checkLoop && state.pos === start) return assertConsumes(state, "separated");
|
|
1558
|
+
const a = step(state);
|
|
1559
|
+
if (a === Fail) {
|
|
1560
|
+
if (state.mode === "Incomplete" && state.resume !== void 0) Resume.save(state, key, entry, {
|
|
1561
|
+
acc,
|
|
1562
|
+
pos: start
|
|
1563
|
+
});
|
|
1564
|
+
if (state.mode !== "Backtrack" || acc.length < min) return Fail;
|
|
1565
|
+
state.pos = start;
|
|
1566
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1567
|
+
}
|
|
1568
|
+
acc.push(a);
|
|
1569
|
+
}
|
|
1570
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1571
|
+
}, "separated", {
|
|
1572
|
+
_tag: "Separated",
|
|
1573
|
+
self,
|
|
1574
|
+
separator,
|
|
1575
|
+
...bounds(range)
|
|
1576
|
+
});
|
|
1410
1577
|
});
|
|
1411
1578
|
/**
|
|
1412
|
-
|
|
1413
|
-
|
|
1414
|
-
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1430
|
-
|
|
1431
|
-
|
|
1432
|
-
|
|
1433
|
-
|
|
1434
|
-
|
|
1435
|
-
|
|
1436
|
-
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
|
|
1451
|
-
|
|
1452
|
-
|
|
1453
|
-
|
|
1454
|
-
|
|
1455
|
-
|
|
1456
|
-
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
}, "separatedFoldl1", { _tag: "SeparatedFold", self, separator, op, right: false });
|
|
1579
|
+
* Parses one or more `self` separated by `separator`, folding them from the
|
|
1580
|
+
* left with the separator's output: `((a op b) op c)` (winnow's
|
|
1581
|
+
* `separated_foldl1`). This is the classic way to parse left-associative
|
|
1582
|
+
* binary operators.
|
|
1583
|
+
*
|
|
1584
|
+
* @example
|
|
1585
|
+
* ```ts
|
|
1586
|
+
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1587
|
+
*
|
|
1588
|
+
* const sum = Parser.separatedFoldl1(
|
|
1589
|
+
* Ascii.decInt,
|
|
1590
|
+
* Parser.alt(Token.literal("+"), Token.literal("-")),
|
|
1591
|
+
* (l, op, r) => op === "+" ? l + r : l - r
|
|
1592
|
+
* )
|
|
1593
|
+
* Parser.parse(sum, "10-3+2") // Result.succeed(9)
|
|
1594
|
+
* ```
|
|
1595
|
+
*
|
|
1596
|
+
* @since 0.1.0
|
|
1597
|
+
* @category repetition
|
|
1598
|
+
*/
|
|
1599
|
+
const separatedFoldl1 = (self, separator, op) => {
|
|
1600
|
+
const step = self.step;
|
|
1601
|
+
const sepStep = separator.step;
|
|
1602
|
+
const key = {};
|
|
1603
|
+
return described((state) => {
|
|
1604
|
+
const entry = state.pos;
|
|
1605
|
+
const resumed = state.resume === void 0 ? void 0 : Resume.take(state, key, entry);
|
|
1606
|
+
let acc;
|
|
1607
|
+
if (resumed instanceof Resume.Done) {
|
|
1608
|
+
state.pos = resumed.pos;
|
|
1609
|
+
return resumed.value;
|
|
1610
|
+
}
|
|
1611
|
+
if (resumed !== void 0) {
|
|
1612
|
+
acc = resumed.acc;
|
|
1613
|
+
state.pos = resumed.pos;
|
|
1614
|
+
} else {
|
|
1615
|
+
acc = step(state);
|
|
1616
|
+
if (acc === Fail) return Fail;
|
|
1617
|
+
}
|
|
1618
|
+
while (true) {
|
|
1619
|
+
const start = state.pos;
|
|
1620
|
+
const s = sepStep(state);
|
|
1621
|
+
if (s === Fail) {
|
|
1622
|
+
if (state.mode === "Incomplete" && state.resume !== void 0) saveFold(state, key, entry, acc, start);
|
|
1623
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1624
|
+
recover(state, start);
|
|
1625
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1626
|
+
}
|
|
1627
|
+
if (state.pos === start) return assertConsumes(state, "separatedFoldl1");
|
|
1628
|
+
const a = step(state);
|
|
1629
|
+
if (a === Fail) {
|
|
1630
|
+
if (state.mode === "Incomplete" && state.resume !== void 0) saveFold(state, key, entry, acc, start);
|
|
1631
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1632
|
+
state.pos = start;
|
|
1633
|
+
return Resume.done(state, key, entry, resumed !== void 0, acc);
|
|
1634
|
+
}
|
|
1635
|
+
acc = op(acc, s, a);
|
|
1636
|
+
}
|
|
1637
|
+
}, "separatedFoldl1", {
|
|
1638
|
+
_tag: "SeparatedFold",
|
|
1639
|
+
self,
|
|
1640
|
+
separator,
|
|
1641
|
+
op,
|
|
1642
|
+
right: false
|
|
1643
|
+
});
|
|
1478
1644
|
};
|
|
1479
1645
|
/**
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1501
|
-
|
|
1502
|
-
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
|
|
1507
|
-
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1520
|
-
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1646
|
+
* Parses one or more `self` separated by `separator`, folding them from the
|
|
1647
|
+
* right with the separator's output: `(a op (b op c))` (winnow's
|
|
1648
|
+
* `separated_foldr1`). Useful for right-associative operators such as
|
|
1649
|
+
* exponentiation.
|
|
1650
|
+
*
|
|
1651
|
+
* @since 0.1.0
|
|
1652
|
+
* @category repetition
|
|
1653
|
+
*/
|
|
1654
|
+
const separatedFoldr1 = (self, separator, op) => {
|
|
1655
|
+
const step = self.step;
|
|
1656
|
+
const sepStep = separator.step;
|
|
1657
|
+
return described((state) => {
|
|
1658
|
+
const first = step(state);
|
|
1659
|
+
if (first === Fail) return Fail;
|
|
1660
|
+
const values = [first];
|
|
1661
|
+
const seps = [];
|
|
1662
|
+
while (true) {
|
|
1663
|
+
const start = state.pos;
|
|
1664
|
+
const s = sepStep(state);
|
|
1665
|
+
if (s === Fail) {
|
|
1666
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1667
|
+
recover(state, start);
|
|
1668
|
+
break;
|
|
1669
|
+
}
|
|
1670
|
+
if (state.pos === start) return assertConsumes(state, "separatedFoldr1");
|
|
1671
|
+
const a = step(state);
|
|
1672
|
+
if (a === Fail) {
|
|
1673
|
+
if (state.mode !== "Backtrack") return Fail;
|
|
1674
|
+
state.pos = start;
|
|
1675
|
+
break;
|
|
1676
|
+
}
|
|
1677
|
+
seps.push(s);
|
|
1678
|
+
values.push(a);
|
|
1679
|
+
}
|
|
1680
|
+
let acc = values[values.length - 1];
|
|
1681
|
+
for (let i = seps.length - 1; i >= 0; i--) acc = op(values[i], seps[i], acc);
|
|
1682
|
+
return acc;
|
|
1683
|
+
}, "separatedFoldr1", {
|
|
1684
|
+
_tag: "SeparatedFold",
|
|
1685
|
+
self,
|
|
1686
|
+
separator,
|
|
1687
|
+
op,
|
|
1688
|
+
right: true
|
|
1689
|
+
});
|
|
1524
1690
|
};
|
|
1525
|
-
|
|
1526
|
-
|
|
1527
|
-
|
|
1528
|
-
|
|
1529
|
-
|
|
1530
|
-
|
|
1531
|
-
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1545
|
-
|
|
1546
|
-
|
|
1547
|
-
|
|
1548
|
-
|
|
1549
|
-
|
|
1550
|
-
|
|
1551
|
-
|
|
1552
|
-
|
|
1553
|
-
|
|
1554
|
-
|
|
1555
|
-
|
|
1556
|
-
|
|
1557
|
-
* @category services
|
|
1558
|
-
*/
|
|
1559
|
-
export const service = (key) => described((state) => Context.getUnsafe(state.context, key), "service", serviceNode);
|
|
1691
|
+
/**
|
|
1692
|
+
* Accesses a service from the parser's context. Services take the place of
|
|
1693
|
+
* winnow's `Stateful` input: they can hold configuration, or mutable state
|
|
1694
|
+
* such as an indentation stack or a symbol table.
|
|
1695
|
+
*
|
|
1696
|
+
* Services are provided with {@link provideService} / {@link provide}, with
|
|
1697
|
+
* the `context` option of the runners, or taken from the surrounding Effect
|
|
1698
|
+
* by {@link parseEffect}.
|
|
1699
|
+
*
|
|
1700
|
+
* Mutations made to a service's state are not undone on backtracking (as
|
|
1701
|
+
* with winnow's `Stateful`).
|
|
1702
|
+
*
|
|
1703
|
+
* @example
|
|
1704
|
+
* ```ts
|
|
1705
|
+
* import { Context } from "effect"
|
|
1706
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1707
|
+
*
|
|
1708
|
+
* class Radix extends Context.Service<Radix, number>()("Radix") {}
|
|
1709
|
+
*
|
|
1710
|
+
* const number = Parser.service(Radix).pipe(
|
|
1711
|
+
* Parser.flatMap((radix) =>
|
|
1712
|
+
* Ascii.alphanumeric1.pipe(Parser.map((digits) => parseInt(digits, radix)))
|
|
1713
|
+
* )
|
|
1714
|
+
* )
|
|
1715
|
+
*
|
|
1716
|
+
* Parser.parse(Parser.provideService(number, Radix, 16), "ff") // Result.succeed(255)
|
|
1717
|
+
* ```
|
|
1718
|
+
*
|
|
1719
|
+
* @since 0.1.0
|
|
1720
|
+
* @category services
|
|
1721
|
+
*/
|
|
1722
|
+
const service = (key) => described((state) => Context.getUnsafe(state.context, key), "service", serviceNode);
|
|
1560
1723
|
const serviceNode = { _tag: "Service" };
|
|
1561
1724
|
/**
|
|
1562
|
-
|
|
1563
|
-
|
|
1564
|
-
|
|
1565
|
-
|
|
1566
|
-
|
|
1567
|
-
|
|
1568
|
-
|
|
1569
|
-
|
|
1570
|
-
|
|
1571
|
-
|
|
1572
|
-
|
|
1573
|
-
|
|
1574
|
-
|
|
1575
|
-
|
|
1576
|
-
|
|
1577
|
-
|
|
1578
|
-
|
|
1725
|
+
* Provides a service to a parser.
|
|
1726
|
+
*
|
|
1727
|
+
* @since 0.1.0
|
|
1728
|
+
* @category services
|
|
1729
|
+
*/
|
|
1730
|
+
const provideService = /*#__PURE__*/ dual(3, (self, key, service) => {
|
|
1731
|
+
const step = self.step;
|
|
1732
|
+
return described((state) => {
|
|
1733
|
+
const prev = state.context;
|
|
1734
|
+
state.context = Context.add(prev, key, service);
|
|
1735
|
+
try {
|
|
1736
|
+
return step(state);
|
|
1737
|
+
} finally {
|
|
1738
|
+
state.context = prev;
|
|
1739
|
+
}
|
|
1740
|
+
}, self.name, {
|
|
1741
|
+
_tag: "Provide",
|
|
1742
|
+
self
|
|
1743
|
+
});
|
|
1579
1744
|
});
|
|
1580
1745
|
/**
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
|
|
1584
|
-
|
|
1585
|
-
|
|
1586
|
-
|
|
1587
|
-
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1592
|
-
|
|
1593
|
-
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
|
|
1597
|
-
|
|
1746
|
+
* Provides a context of services to a parser.
|
|
1747
|
+
*
|
|
1748
|
+
* @since 0.1.0
|
|
1749
|
+
* @category services
|
|
1750
|
+
*/
|
|
1751
|
+
const provide = /*#__PURE__*/ dual(2, (self, context) => {
|
|
1752
|
+
const step = self.step;
|
|
1753
|
+
return described((state) => {
|
|
1754
|
+
const prev = state.context;
|
|
1755
|
+
state.context = Context.merge(prev, context);
|
|
1756
|
+
try {
|
|
1757
|
+
return step(state);
|
|
1758
|
+
} finally {
|
|
1759
|
+
state.context = prev;
|
|
1760
|
+
}
|
|
1761
|
+
}, self.name, {
|
|
1762
|
+
_tag: "Provide",
|
|
1763
|
+
self
|
|
1764
|
+
});
|
|
1598
1765
|
});
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1603
|
-
|
|
1604
|
-
|
|
1605
|
-
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
}, self.name, { _tag: "ResumeAfter", self, recover });
|
|
1766
|
+
/**
|
|
1767
|
+
* Error recovery: if the parser fails, record the failure, skip input with
|
|
1768
|
+
* `recover`, and continue with `Option.none()` (winnow's `resume_after`).
|
|
1769
|
+
*
|
|
1770
|
+
* Recovery is only active when running with {@link parseRecoverable}. Under
|
|
1771
|
+
* the other runners the parser behaves like `Parser.asSome(self)`.
|
|
1772
|
+
*
|
|
1773
|
+
* @since 0.1.0
|
|
1774
|
+
* @category error recovery
|
|
1775
|
+
*/
|
|
1776
|
+
const resumeAfter = /*#__PURE__*/ dual(2, (self, recover) => {
|
|
1777
|
+
const step = self.step;
|
|
1778
|
+
const recoverStep = recover.step;
|
|
1779
|
+
return described((state) => {
|
|
1780
|
+
const start = state.pos;
|
|
1781
|
+
const a = step(state);
|
|
1782
|
+
if (a !== Fail) return Option.some(a);
|
|
1783
|
+
if (state.recovered === void 0 || state.mode === "Incomplete") return Fail;
|
|
1784
|
+
const failure = state.failure;
|
|
1785
|
+
const mode = state.mode;
|
|
1786
|
+
state.pos = start;
|
|
1787
|
+
if (recoverStep(state) === Fail) return state.failWith(mode, failure);
|
|
1788
|
+
state.recovered.push(failure);
|
|
1789
|
+
state.syntax?.skipped(start, failure, mode);
|
|
1790
|
+
return Option.none();
|
|
1791
|
+
}, self.name, {
|
|
1792
|
+
_tag: "ResumeAfter",
|
|
1793
|
+
self,
|
|
1794
|
+
recover
|
|
1795
|
+
});
|
|
1630
1796
|
});
|
|
1631
1797
|
/**
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
|
|
1635
|
-
|
|
1636
|
-
|
|
1637
|
-
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
|
|
1659
|
-
|
|
1798
|
+
* Error recovery: if the parser fails, record the failure, skip input with
|
|
1799
|
+
* `recover`, and try the parser again (winnow's `retry_after`).
|
|
1800
|
+
*
|
|
1801
|
+
* Recovery is only active when running with {@link parseRecoverable}. Under
|
|
1802
|
+
* the other runners the parser behaves like `self`.
|
|
1803
|
+
*
|
|
1804
|
+
* @since 0.1.0
|
|
1805
|
+
* @category error recovery
|
|
1806
|
+
*/
|
|
1807
|
+
const retryAfter = /*#__PURE__*/ dual(2, (self, recover) => {
|
|
1808
|
+
const step = self.step;
|
|
1809
|
+
const recoverStep = recover.step;
|
|
1810
|
+
return described((state) => {
|
|
1811
|
+
while (true) {
|
|
1812
|
+
const start = state.pos;
|
|
1813
|
+
const a = step(state);
|
|
1814
|
+
if (a !== Fail) return a;
|
|
1815
|
+
if (state.recovered === void 0 || state.mode === "Incomplete") return Fail;
|
|
1816
|
+
const failure = state.failure;
|
|
1817
|
+
const mode = state.mode;
|
|
1818
|
+
state.pos = start;
|
|
1819
|
+
if (recoverStep(state) === Fail || state.pos === start) return state.failWith(mode, failure);
|
|
1820
|
+
state.recovered.push(failure);
|
|
1821
|
+
state.syntax?.skipped(start, failure, mode);
|
|
1822
|
+
}
|
|
1823
|
+
}, self.name, {
|
|
1824
|
+
_tag: "RetryAfter",
|
|
1825
|
+
self,
|
|
1826
|
+
recover
|
|
1827
|
+
});
|
|
1660
1828
|
});
|
|
1661
|
-
|
|
1662
|
-
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1670
|
-
|
|
1671
|
-
|
|
1672
|
-
|
|
1673
|
-
|
|
1674
|
-
|
|
1675
|
-
|
|
1676
|
-
|
|
1677
|
-
|
|
1678
|
-
|
|
1679
|
-
|
|
1680
|
-
|
|
1681
|
-
|
|
1682
|
-
|
|
1683
|
-
|
|
1684
|
-
|
|
1685
|
-
|
|
1686
|
-
|
|
1687
|
-
|
|
1688
|
-
|
|
1689
|
-
|
|
1690
|
-
|
|
1691
|
-
|
|
1692
|
-
|
|
1693
|
-
|
|
1694
|
-
|
|
1695
|
-
|
|
1829
|
+
/**
|
|
1830
|
+
* Traces the execution of a parser (winnow's `trace`). When a tracer is
|
|
1831
|
+
* installed (see the `trace` option of {@link parse}), `Enter` and `Exit`
|
|
1832
|
+
* events are reported for each traced parser; otherwise tracing has no
|
|
1833
|
+
* effect.
|
|
1834
|
+
*
|
|
1835
|
+
* @since 0.1.0
|
|
1836
|
+
* @category debugging
|
|
1837
|
+
*/
|
|
1838
|
+
const trace = /*#__PURE__*/ dual(2, (self, name) => {
|
|
1839
|
+
const step = self.step;
|
|
1840
|
+
return described((state) => {
|
|
1841
|
+
const tracer = state.tracer;
|
|
1842
|
+
if (tracer === void 0) return step(state);
|
|
1843
|
+
const start = state.pos;
|
|
1844
|
+
const depth = state.depth;
|
|
1845
|
+
tracer({
|
|
1846
|
+
_tag: "Enter",
|
|
1847
|
+
name,
|
|
1848
|
+
depth,
|
|
1849
|
+
offset: start,
|
|
1850
|
+
state
|
|
1851
|
+
});
|
|
1852
|
+
state.depth = depth + 1;
|
|
1853
|
+
const a = step(state);
|
|
1854
|
+
state.depth = depth;
|
|
1855
|
+
tracer({
|
|
1856
|
+
_tag: "Exit",
|
|
1857
|
+
name,
|
|
1858
|
+
depth,
|
|
1859
|
+
offset: start,
|
|
1860
|
+
endOffset: state.pos,
|
|
1861
|
+
result: a === Fail ? state.mode : "Ok",
|
|
1862
|
+
state
|
|
1863
|
+
});
|
|
1864
|
+
return a;
|
|
1865
|
+
}, name, {
|
|
1866
|
+
_tag: "Trace",
|
|
1867
|
+
self,
|
|
1868
|
+
name
|
|
1869
|
+
});
|
|
1696
1870
|
});
|
|
1697
1871
|
const preview = (state, offset) => {
|
|
1698
|
-
|
|
1699
|
-
|
|
1700
|
-
|
|
1701
|
-
|
|
1702
|
-
|
|
1703
|
-
|
|
1704
|
-
|
|
1705
|
-
}
|
|
1706
|
-
return `[${offset}/${input.length}]`;
|
|
1872
|
+
const input = state.input;
|
|
1873
|
+
if (Predicate.isString(input)) {
|
|
1874
|
+
const s = input.slice(offset, offset + 24);
|
|
1875
|
+
return JSON.stringify(s) + (offset + 24 < input.length ? "…" : "∅");
|
|
1876
|
+
}
|
|
1877
|
+
if (input instanceof Uint8Array) return Array.from(input.subarray(offset, offset + 12), (b) => b.toString(16).padStart(2, "0")).join(" ");
|
|
1878
|
+
return `[${offset}/${input.length}]`;
|
|
1707
1879
|
};
|
|
1708
1880
|
/**
|
|
1709
|
-
|
|
1710
|
-
|
|
1711
|
-
|
|
1712
|
-
|
|
1713
|
-
|
|
1714
|
-
|
|
1715
|
-
|
|
1716
|
-
|
|
1717
|
-
|
|
1718
|
-
|
|
1719
|
-
|
|
1720
|
-
|
|
1721
|
-
|
|
1722
|
-
console.log(`${indent}< ${event.name} | ${event.result === "Ok" ? `+${consumed}` : event.result}`);
|
|
1723
|
-
}
|
|
1881
|
+
* A tracer that writes an indented trace to `console.log`, similar to
|
|
1882
|
+
* winnow's `debug` feature.
|
|
1883
|
+
*
|
|
1884
|
+
* @since 0.1.0
|
|
1885
|
+
* @category debugging
|
|
1886
|
+
*/
|
|
1887
|
+
const consoleTracer = (event) => {
|
|
1888
|
+
const indent = " ".repeat(event.depth);
|
|
1889
|
+
if (event._tag === "Enter") console.log(`${indent}> ${event.name} @${event.offset} | ${preview(event.state, event.offset)}`);
|
|
1890
|
+
else {
|
|
1891
|
+
const consumed = (event.endOffset ?? event.offset) - event.offset;
|
|
1892
|
+
console.log(`${indent}< ${event.name} | ${event.result === "Ok" ? `+${consumed}` : event.result}`);
|
|
1893
|
+
}
|
|
1724
1894
|
};
|
|
1725
|
-
/**
|
|
1726
|
-
* Compiles a parser into fused, specialized code, and returns the same
|
|
1727
|
-
* parser. Grammars are written as usual; compilation only makes
|
|
1728
|
-
* {@link parse}, {@link parseSync}, {@link parsePeek} and
|
|
1729
|
-
* {@link parseEffect} faster. Compile the root of a grammar once, where it
|
|
1730
|
-
* is defined:
|
|
1731
|
-
*
|
|
1732
|
-
* ```ts
|
|
1733
|
-
* import { Ascii, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
1734
|
-
*
|
|
1735
|
-
* export const list = Parser.compile(
|
|
1736
|
-
* Parser.separated(Ascii.decInt, Token.literal(","))
|
|
1737
|
-
* )
|
|
1738
|
-
* Parser.parse(list, "1,2,3") // Result.succeed([1, 2, 3]), compiled
|
|
1739
|
-
* ```
|
|
1740
|
-
*
|
|
1741
|
-
* The code is generated lazily on the first run, for each kind of input
|
|
1742
|
-
* (string, bytes or array), and cached with the parser.
|
|
1743
|
-
*
|
|
1744
|
-
* **Results and errors are unchanged.** Compiled code records no
|
|
1745
|
-
* diagnostics. When it fails, the parser runs again, interpreted, from the
|
|
1746
|
-
* start, so every `ParseError` (offset, expected, context, cause and
|
|
1747
|
-
* message) is exactly the one the interpreter reports.
|
|
1748
|
-
*
|
|
1749
|
-
* **Callbacks should be pure.** On an input that fails, the callbacks given
|
|
1750
|
-
* to `map`, `filter`, `tryMap`, `tap`, `fold`, ... may run twice, once
|
|
1751
|
-
* compiled and once interpreted. Callbacks whose results are discarded
|
|
1752
|
-
* (a `map` inside `taken` or `preceded`, for example) may not run at all,
|
|
1753
|
-
* nor those of a branch that is certain to fail, and token-set predicates
|
|
1754
|
-
* may be called once per character code and cached.
|
|
1755
|
-
*
|
|
1756
|
-
* Runs on partial input ({@link parsePartial}, `ParserStream`), with
|
|
1757
|
-
* {@link parseRecoverable} or with the `trace` option are always
|
|
1758
|
-
* interpreted. Parsers that can't be described, such as {@link make} and
|
|
1759
|
-
* {@link gen}, run interpreted inside compiled code.
|
|
1760
|
-
*
|
|
1761
|
-
* @since 0.1.0
|
|
1762
|
-
* @category compilation
|
|
1763
|
-
*/
|
|
1764
|
-
export const compile = dual(isParserArg, (self, options) => {
|
|
1765
|
-
Compiler.register(self, options?.backend ?? "auto");
|
|
1766
|
-
return self;
|
|
1767
|
-
});
|
|
1768
1895
|
/** Returned by {@link runCompiled} when there is no compiled form to run. */
|
|
1769
|
-
const NotCompiled = Symbol("NotCompiled");
|
|
1770
|
-
/**
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
|
|
1781
|
-
|
|
1782
|
-
|
|
1783
|
-
return Fail;
|
|
1784
|
-
return a;
|
|
1896
|
+
const NotCompiled = /*#__PURE__*/ Symbol("NotCompiled");
|
|
1897
|
+
/**
|
|
1898
|
+
* Runs the compiled form of `self`, if it has one: returns its value, or
|
|
1899
|
+
* `Fail` when it failed (or, with `whole`, did not consume the whole
|
|
1900
|
+
* input), in which case the state must be thrown away. `resumable` selects
|
|
1901
|
+
* the build that handles partial input.
|
|
1902
|
+
*/
|
|
1903
|
+
const runCompiled = (self, state, whole, resumable = false) => {
|
|
1904
|
+
if (state.tracer !== void 0 || state.recovered !== void 0) return NotCompiled;
|
|
1905
|
+
const step = Registry.stepFor(self, state, resumable);
|
|
1906
|
+
if (step === void 0) return NotCompiled;
|
|
1907
|
+
const a = Stack.run(step, state);
|
|
1908
|
+
if (a === Fail || whole && state.pos < state.end) return Fail;
|
|
1909
|
+
return a;
|
|
1785
1910
|
};
|
|
1786
1911
|
const makeState = (input, options, partial) => {
|
|
1787
|
-
|
|
1788
|
-
|
|
1789
|
-
|
|
1790
|
-
|
|
1791
|
-
|
|
1912
|
+
const offset = options?.offset;
|
|
1913
|
+
const state = new State(input, {
|
|
1914
|
+
partial,
|
|
1915
|
+
context: options?.context,
|
|
1916
|
+
offset
|
|
1917
|
+
});
|
|
1918
|
+
if (offset !== void 0 && !(Number.isInteger(offset) && offset >= 0 && offset <= state.end)) throw new RangeError(`Invalid offset ${offset} for an input of length ${state.end}`);
|
|
1919
|
+
const t = options?.trace;
|
|
1920
|
+
if (t !== void 0 && t !== false) state.tracer = t === true ? consoleTracer : t;
|
|
1921
|
+
return state;
|
|
1792
1922
|
};
|
|
1793
1923
|
const toError = (state) => ParseError.fromFailure(state.input, state.reportedFailure(), state.mode, state.encoding);
|
|
1794
1924
|
/** Runs a parser against a complete input, compiled when possible. */
|
|
1795
1925
|
const parseComplete = (self, input, options) => {
|
|
1796
|
-
|
|
1797
|
-
|
|
1798
|
-
|
|
1799
|
-
|
|
1800
|
-
|
|
1801
|
-
return Result.succeed(a);
|
|
1802
|
-
// Re-run interpreted for the diagnostics
|
|
1803
|
-
return runComplete(self, makeState(input, options, false));
|
|
1926
|
+
const state = makeState(input, options, false);
|
|
1927
|
+
const a = runCompiled(self, state, true);
|
|
1928
|
+
if (a === NotCompiled) return runComplete(self, state);
|
|
1929
|
+
if (a !== Fail) return Result.succeed(a);
|
|
1930
|
+
return runComplete(self, makeState(input, options, false));
|
|
1804
1931
|
};
|
|
1805
1932
|
const runComplete = (self, state) => {
|
|
1806
|
-
|
|
1807
|
-
|
|
1808
|
-
|
|
1809
|
-
|
|
1810
|
-
|
|
1811
|
-
|
|
1812
|
-
|
|
1813
|
-
return Result.succeed(a);
|
|
1933
|
+
const a = Stack.run(self.step, state);
|
|
1934
|
+
if (a === Fail) return Result.fail(toError(state));
|
|
1935
|
+
if (state.pos < state.end) {
|
|
1936
|
+
state.backtrack("Eof", expectedEof);
|
|
1937
|
+
return Result.fail(toError(state));
|
|
1938
|
+
}
|
|
1939
|
+
return Result.succeed(a);
|
|
1814
1940
|
};
|
|
1815
1941
|
/**
|
|
1816
|
-
|
|
1817
|
-
|
|
1818
|
-
|
|
1819
|
-
|
|
1820
|
-
|
|
1821
|
-
|
|
1822
|
-
|
|
1823
|
-
|
|
1824
|
-
|
|
1825
|
-
|
|
1826
|
-
|
|
1827
|
-
|
|
1828
|
-
|
|
1829
|
-
|
|
1830
|
-
|
|
1831
|
-
|
|
1832
|
-
/**
|
|
1833
|
-
|
|
1834
|
-
|
|
1835
|
-
|
|
1836
|
-
|
|
1837
|
-
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
throw result.failure;
|
|
1942
|
+
* Runs a parser against a complete input, requiring the whole input to be
|
|
1943
|
+
* consumed (winnow's `Parser::parse`).
|
|
1944
|
+
*
|
|
1945
|
+
* @example
|
|
1946
|
+
* ```ts
|
|
1947
|
+
* import { Result } from "effect"
|
|
1948
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
1949
|
+
*
|
|
1950
|
+
* Parser.parse(Ascii.decInt, "-42") // Result.succeed(-42)
|
|
1951
|
+
* Result.isFailure(Parser.parse(Ascii.decInt, "42x")) // true
|
|
1952
|
+
* ```
|
|
1953
|
+
*
|
|
1954
|
+
* @since 0.1.0
|
|
1955
|
+
* @category running
|
|
1956
|
+
*/
|
|
1957
|
+
const parse = (self, input, options) => parseComplete(self, input, options);
|
|
1958
|
+
/**
|
|
1959
|
+
* Like {@link parse}, but throws the `ParseError` on failure.
|
|
1960
|
+
*
|
|
1961
|
+
* @since 0.1.0
|
|
1962
|
+
* @category running
|
|
1963
|
+
*/
|
|
1964
|
+
const parseSync = (self, input, options) => {
|
|
1965
|
+
const result = parseComplete(self, input, options);
|
|
1966
|
+
if (Result.isSuccess(result)) return result.success;
|
|
1967
|
+
throw result.failure;
|
|
1843
1968
|
};
|
|
1844
1969
|
/**
|
|
1845
|
-
|
|
1846
|
-
|
|
1847
|
-
|
|
1848
|
-
|
|
1849
|
-
|
|
1850
|
-
|
|
1851
|
-
|
|
1852
|
-
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
|
|
1856
|
-
|
|
1857
|
-
|
|
1858
|
-
|
|
1859
|
-
|
|
1860
|
-
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1970
|
+
* Runs a parser against the start of an input, returning the remaining
|
|
1971
|
+
* input along with the output, nom-style (winnow's `parse_peek`, nom's
|
|
1972
|
+
* `IResult`).
|
|
1973
|
+
*
|
|
1974
|
+
* The parser ended at offset `input.length - remaining.length`, which is
|
|
1975
|
+
* how a caller running from an `offset` (see {@link ParseOptions}) finds
|
|
1976
|
+
* where to continue.
|
|
1977
|
+
*
|
|
1978
|
+
* @since 0.1.0
|
|
1979
|
+
* @category running
|
|
1980
|
+
*/
|
|
1981
|
+
const parsePeek = (self, input, options) => {
|
|
1982
|
+
let state = makeState(input, options, false);
|
|
1983
|
+
const compiled = runCompiled(self, state, false);
|
|
1984
|
+
if (compiled !== NotCompiled && compiled !== Fail) return Result.succeed([state.slice(state.pos, state.end), compiled]);
|
|
1985
|
+
if (compiled === Fail) state = makeState(input, options, false);
|
|
1986
|
+
const a = Stack.run(self.step, state);
|
|
1987
|
+
if (a === Fail) return Result.fail(toError(state));
|
|
1988
|
+
return Result.succeed([state.slice(state.pos, state.end), a]);
|
|
1864
1989
|
};
|
|
1865
1990
|
/**
|
|
1866
|
-
|
|
1867
|
-
|
|
1868
|
-
|
|
1869
|
-
|
|
1870
|
-
|
|
1871
|
-
|
|
1872
|
-
|
|
1873
|
-
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
|
|
1882
|
-
|
|
1883
|
-
|
|
1884
|
-
|
|
1885
|
-
|
|
1886
|
-
|
|
1887
|
-
|
|
1888
|
-
|
|
1889
|
-
|
|
1890
|
-
|
|
1891
|
-
|
|
1892
|
-
|
|
1893
|
-
|
|
1894
|
-
|
|
1895
|
-
|
|
1896
|
-
|
|
1991
|
+
* Runs a parser against a *partial* input, i.e. a chunk of a stream that may
|
|
1992
|
+
* be followed by more data (winnow's `Partial`, nom's `streaming` parsers).
|
|
1993
|
+
* Token parsers that reach the end of the chunk report `Incomplete` instead
|
|
1994
|
+
* of failing or succeeding early.
|
|
1995
|
+
*
|
|
1996
|
+
* The parser does not have to consume the whole input; the returned
|
|
1997
|
+
* `offset` says where it stopped. It is a position in the whole input, so
|
|
1998
|
+
* with the `offset` option (see {@link ParseOptions}) it is where the next
|
|
1999
|
+
* parse starts.
|
|
2000
|
+
*
|
|
2001
|
+
* A parser compiled with the `Compiler` module runs compiled, with a build
|
|
2002
|
+
* that handles partial input. When it fails or needs more input, it runs again
|
|
2003
|
+
* interpreted, so `Incomplete`'s `needed` and every `ParseError` are the
|
|
2004
|
+
* interpreter's.
|
|
2005
|
+
*
|
|
2006
|
+
* @example
|
|
2007
|
+
* ```ts
|
|
2008
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
2009
|
+
*
|
|
2010
|
+
* Parser.parsePartial(Ascii.digit1, "123")
|
|
2011
|
+
* // Result.succeed({ _tag: "Incomplete", needed: Option.some(1) })
|
|
2012
|
+
* Parser.parsePartial(Ascii.digit1, "123;")
|
|
2013
|
+
* // Result.succeed({ _tag: "Done", value: "123", offset: 3 })
|
|
2014
|
+
* ```
|
|
2015
|
+
*
|
|
2016
|
+
* @since 0.1.0
|
|
2017
|
+
* @category running
|
|
2018
|
+
*/
|
|
2019
|
+
const parsePartial = (self, input, options) => {
|
|
2020
|
+
let state = makeState(input, options, true);
|
|
2021
|
+
const compiled = runCompiled(self, state, false, true);
|
|
2022
|
+
if (compiled !== NotCompiled && compiled !== Fail) return Result.succeed({
|
|
2023
|
+
_tag: "Done",
|
|
2024
|
+
value: compiled,
|
|
2025
|
+
offset: state.pos
|
|
2026
|
+
});
|
|
2027
|
+
if (compiled === Fail) state = makeState(input, options, true);
|
|
2028
|
+
const a = Stack.run(self.step, state);
|
|
2029
|
+
if (a !== Fail) return Result.succeed({
|
|
2030
|
+
_tag: "Done",
|
|
2031
|
+
value: a,
|
|
2032
|
+
offset: state.pos
|
|
2033
|
+
});
|
|
2034
|
+
if (state.mode === "Incomplete") {
|
|
2035
|
+
const needed = state.failure.needed;
|
|
2036
|
+
return Result.succeed({
|
|
2037
|
+
_tag: "Incomplete",
|
|
2038
|
+
needed: needed > 0 ? Option.some(needed) : Option.none()
|
|
2039
|
+
});
|
|
2040
|
+
}
|
|
2041
|
+
return Result.fail(toError(state));
|
|
1897
2042
|
};
|
|
1898
2043
|
/**
|
|
1899
|
-
|
|
1900
|
-
|
|
1901
|
-
|
|
1902
|
-
|
|
1903
|
-
|
|
1904
|
-
|
|
1905
|
-
|
|
1906
|
-
|
|
1907
|
-
|
|
1908
|
-
|
|
1909
|
-
|
|
1910
|
-
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
|
|
1915
|
-
|
|
1916
|
-
|
|
1917
|
-
|
|
1918
|
-
|
|
1919
|
-
|
|
1920
|
-
|
|
1921
|
-
|
|
1922
|
-
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1927
|
-
|
|
1928
|
-
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
2044
|
+
* Runs a parser against a complete input as an `Effect`. Services required
|
|
2045
|
+
* by the parser are taken from the Effect's context.
|
|
2046
|
+
*
|
|
2047
|
+
* @example
|
|
2048
|
+
* ```ts
|
|
2049
|
+
* import { Effect } from "effect"
|
|
2050
|
+
* import { Ascii, Parser } from "@thomasfosterau/effect-parse"
|
|
2051
|
+
*
|
|
2052
|
+
* const program = Effect.gen(function*() {
|
|
2053
|
+
* const n = yield* Parser.parseEffect(Ascii.decInt, "42")
|
|
2054
|
+
* return n + 1
|
|
2055
|
+
* })
|
|
2056
|
+
* ```
|
|
2057
|
+
*
|
|
2058
|
+
* @since 0.1.0
|
|
2059
|
+
* @category running
|
|
2060
|
+
*/
|
|
2061
|
+
const parseEffect = (self, input, options) => Effect.flatMap(Effect.context(), (context) => Effect.fromResult(parseComplete(self, input, {
|
|
2062
|
+
...options,
|
|
2063
|
+
context
|
|
2064
|
+
})));
|
|
2065
|
+
/**
|
|
2066
|
+
* Runs a parser on a state you own, from `state.pos`, and returns its output
|
|
2067
|
+
* or {@link Fail}: like calling `self.step(state)`, but using the parser's
|
|
2068
|
+
* compiled form when it has one (see the `Compiler` module). This is the
|
|
2069
|
+
* runner for drivers that make many small runs over one buffer, such as a
|
|
2070
|
+
* lexer reading one token at a time: keep one `Input.State` and
|
|
2071
|
+
* `reset` it (`Input.State.reset`) before each run, and no run allocates a
|
|
2072
|
+
* state, a `Result` or a slice.
|
|
2073
|
+
*
|
|
2074
|
+
* On success the cursor is where the parser stopped. On failure the state
|
|
2075
|
+
* holds the interpreter's failure (`mode`, `failure`, and what
|
|
2076
|
+
* `reportedFailure()` returns): a compiled run that fails is thrown away and
|
|
2077
|
+
* the parser's step runs again from the same position. A partial state
|
|
2078
|
+
* (`partial: true`) runs the compiled build that handles partial input, so
|
|
2079
|
+
* `Incomplete` is reported as the step would report it. Under a tracer,
|
|
2080
|
+
* with recovery on or inside a `Syntax` parse, the step runs interpreted.
|
|
2081
|
+
*
|
|
2082
|
+
* A driver that only needs to know *that* a parser failed (it tries
|
|
2083
|
+
* another, or skips a character) can pass `{ diagnostics: false }` to skip
|
|
2084
|
+
* the interpreted re-run; see {@link RunOptions}.
|
|
2085
|
+
*
|
|
2086
|
+
* Compiled code records no hints or furthest failure when it succeeds, so
|
|
2087
|
+
* a later failure on the same state can list fewer expectations than one
|
|
2088
|
+
* after interpreted runs would: reset the state between runs (as above) and
|
|
2089
|
+
* its errors are the ones {@link parse} reports. Deep nesting fails with an
|
|
2090
|
+
* unrecoverable `TooDeep` failure instead of throwing, as in the other
|
|
2091
|
+
* runners.
|
|
2092
|
+
*
|
|
2093
|
+
* @example
|
|
2094
|
+
* ```ts
|
|
2095
|
+
* import { Ascii, Compiler, Input, Parser, Token } from "@thomasfosterau/effect-parse"
|
|
2096
|
+
*
|
|
2097
|
+
* const token = Compiler.jit(Parser.preceded(Ascii.space0, Parser.alt(Ascii.alpha1, Token.oneOf("+-"))))
|
|
2098
|
+
*
|
|
2099
|
+
* const tokens = (text: string): Array<string> => {
|
|
2100
|
+
* const state = new Input.State(text)
|
|
2101
|
+
* const out: Array<string> = []
|
|
2102
|
+
* while (state.pos < state.end) {
|
|
2103
|
+
* const t = Parser.run(token, state.reset(state.pos))
|
|
2104
|
+
* if (t === Input.Fail) break
|
|
2105
|
+
* out.push(t)
|
|
2106
|
+
* }
|
|
2107
|
+
* return out
|
|
2108
|
+
* }
|
|
2109
|
+
*
|
|
2110
|
+
* tokens("a + b") // ["a", "+", "b"]
|
|
2111
|
+
* ```
|
|
2112
|
+
*
|
|
2113
|
+
* @since 0.2.0
|
|
2114
|
+
* @category running
|
|
2115
|
+
*/
|
|
2116
|
+
const run = (self, state, options) => Run.runOn(self, self.step, state, options?.diagnostics !== false);
|
|
2117
|
+
/**
|
|
2118
|
+
* Runs a parser with error recovery enabled (winnow's `Recoverable`
|
|
2119
|
+
* stream). Failures handled by {@link resumeAfter} and {@link retryAfter}
|
|
2120
|
+
* are collected instead of aborting the parse.
|
|
2121
|
+
*
|
|
2122
|
+
* @since 0.1.0
|
|
2123
|
+
* @category running
|
|
2124
|
+
*/
|
|
2125
|
+
const parseRecoverable = (self, input, options) => {
|
|
2126
|
+
const state = makeState(input, options, false);
|
|
2127
|
+
const recovered = [];
|
|
2128
|
+
state.recovered = recovered;
|
|
2129
|
+
const result = runComplete(self, state);
|
|
2130
|
+
const errors = recovered.map((f) => ParseError.fromFailure(state.input, f, "Backtrack", state.encoding));
|
|
2131
|
+
if (Result.isSuccess(result)) return {
|
|
2132
|
+
value: Option.some(result.success),
|
|
2133
|
+
errors
|
|
2134
|
+
};
|
|
2135
|
+
errors.push(result.failure);
|
|
2136
|
+
return {
|
|
2137
|
+
value: Option.none(),
|
|
2138
|
+
errors
|
|
2139
|
+
};
|
|
1935
2140
|
};
|
|
2141
|
+
//#endregion
|
|
2142
|
+
export { Do, TypeId, allConsuming, alt, as, asSome, asVoid, backtrack, bind, bindTo, complete, cond, consoleTracer, context, cut, delimited, described, dispatch, empty, eof, expected, fail, failMessage, filter, filterMap, filterOrFail, flatMap, fold, fromEntries, gen, isParser, let_ as let, location, make, map, mapError, mapParser, named, not, opt, optional, orElse, orElseSucceed, orFail, parse, parseEffect, parsePartial, parsePeek, parseRecoverable, parseSync, peek, permutation, preceded, provide, provideService, reject, repeat, repeatTill, resumeAfter, retryAfter, run, separated, separatedFoldl1, separatedFoldr1, separatedPair, seq, service, span, struct, succeed, suspend, sync, taken, tap, terminated, todo, trace, tryMap, tryMapUnknown, void_ as void, void_, withSpan, withTaken, zip, zipLeft, zipRight, zipWith };
|
|
2143
|
+
|
|
1936
2144
|
//# sourceMappingURL=Parser.js.map
|