@thomasfosterau/effect-parse 0.0.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
|
@@ -1,1668 +1,1489 @@
|
|
|
1
|
+
import * as Predicate from "effect/Predicate";
|
|
1
2
|
import { Fail } from "../../Input.js";
|
|
3
|
+
import { floatScan, floatTexts, scanFloatBytes, scanFloatString, toFloat } from "../numbers.js";
|
|
4
|
+
import * as Resume from "../resume.js";
|
|
5
|
+
import { countTokens, indexOf } from "../tokens.js";
|
|
2
6
|
import * as Option from "effect/Option";
|
|
7
|
+
import { charOf, fastLimit, firstCodePoint, fold, validate } from "../text.js";
|
|
3
8
|
import { addEntry, flushEntries, fromEntries, isPlainKey } from "../entries.js";
|
|
4
9
|
import { nodeOf } from "../node.js";
|
|
5
|
-
import {
|
|
6
|
-
import { charOf, fastLimit, fold, validate } from "../text.js";
|
|
7
|
-
import { countTokens, indexOf } from "../tokens.js";
|
|
8
|
-
import * as Resume from "../resume.js";
|
|
9
|
-
import { entriesShape, pairOf } from "./entries.js";
|
|
10
|
+
import { buildsEntries, pairOf } from "./entries.js";
|
|
10
11
|
import { FirstSets, planAlt } from "./first.js";
|
|
11
|
-
import {
|
|
12
|
+
import { TableSize, kindOfEncoding, textSet, tokenSet } from "./sets.js";
|
|
13
|
+
//#region src/internal/compiler/closure.ts
|
|
14
|
+
/**
|
|
15
|
+
* The closure compiler: turns a parser's description into specialized
|
|
16
|
+
* closures with the step signature. Compiled steps return a value, or
|
|
17
|
+
* `Fail` with only `state.mode` set: no diagnostics are recorded.
|
|
18
|
+
*
|
|
19
|
+
* Nothing here generates code, so it works under a Content Security
|
|
20
|
+
* Policy. Parsers that can't be compiled run their own (interpreted) steps
|
|
21
|
+
* on the same state.
|
|
22
|
+
*/
|
|
12
23
|
/** The maximum number of distinct parsers compiled for one root. */
|
|
13
|
-
|
|
24
|
+
const maxNodes = 2048;
|
|
14
25
|
const BT = "Backtrack";
|
|
15
26
|
const CUT = "Cut";
|
|
16
27
|
const INC = "Incomplete";
|
|
17
28
|
/**
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
29
|
+
* How many units past the cursor a single token can depend on (a code
|
|
30
|
+
* point is at most 4 units). In a partial parse, a token parser that read
|
|
31
|
+
* no closer than this to the end of the input behaves exactly as on
|
|
32
|
+
* complete input.
|
|
33
|
+
*/
|
|
23
34
|
const Margin = 4;
|
|
24
35
|
const lowerAscii = (c) => c >= 65 && c <= 90 ? c + 32 : c;
|
|
25
36
|
const isSuccess = (r) => r._tag === "Success";
|
|
26
37
|
const isSome = (o) => o._tag === "Some";
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
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
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
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
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
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
|
-
|
|
337
|
-
|
|
338
|
-
|
|
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
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
545
|
-
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
|
|
558
|
-
|
|
559
|
-
|
|
560
|
-
|
|
561
|
-
|
|
562
|
-
|
|
563
|
-
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
593
|
-
|
|
594
|
-
|
|
595
|
-
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
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
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
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
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
|
|
782
|
-
|
|
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
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
|
|
820
|
-
|
|
821
|
-
|
|
822
|
-
|
|
823
|
-
|
|
824
|
-
|
|
825
|
-
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
|
|
829
|
-
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
835
|
-
|
|
836
|
-
|
|
837
|
-
|
|
838
|
-
|
|
839
|
-
|
|
840
|
-
|
|
841
|
-
|
|
842
|
-
|
|
843
|
-
|
|
844
|
-
|
|
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
|
-
|
|
874
|
-
|
|
875
|
-
|
|
876
|
-
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
885
|
-
|
|
886
|
-
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
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
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
956
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
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
|
-
|
|
1043
|
-
|
|
1044
|
-
|
|
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
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
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
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
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
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
1231
|
-
|
|
1232
|
-
|
|
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
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
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
|
-
|
|
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
|
-
|
|
1350
|
-
|
|
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
|
-
taken(node, used) {
|
|
1387
|
-
const c = this.compile(node.self, false);
|
|
1388
|
-
const enc = node.encoding;
|
|
1389
|
-
if (enc === undefined) {
|
|
1390
|
-
if (!used)
|
|
1391
|
-
return c;
|
|
1392
|
-
if (this.kind === 0) {
|
|
1393
|
-
return (s) => {
|
|
1394
|
-
const start = s.pos;
|
|
1395
|
-
return c(s) === Fail ? Fail : s.input.slice(start, s.pos);
|
|
1396
|
-
};
|
|
1397
|
-
}
|
|
1398
|
-
return (s) => {
|
|
1399
|
-
const start = s.pos;
|
|
1400
|
-
return c(s) === Fail ? Fail : s.slice(start, s.pos);
|
|
1401
|
-
};
|
|
1402
|
-
}
|
|
1403
|
-
// `Text.taken` checks that the text is well formed, then decodes it
|
|
1404
|
-
const fast = fastLimit(enc);
|
|
1405
|
-
const toText = enc.text;
|
|
1406
|
-
const unitAt = this.kind === 0
|
|
1407
|
-
? (input, i) => input.charCodeAt(i)
|
|
1408
|
-
: (input, i) => input[i];
|
|
1409
|
-
return (s) => {
|
|
1410
|
-
const start = s.pos;
|
|
1411
|
-
if (c(s) === Fail)
|
|
1412
|
-
return Fail;
|
|
1413
|
-
const end = s.pos;
|
|
1414
|
-
const input = s.input;
|
|
1415
|
-
let i = start;
|
|
1416
|
-
while (i < end && unitAt(input, i) < fast)
|
|
1417
|
-
i++;
|
|
1418
|
-
if (i < end && validate(enc, s, i, end) < 0)
|
|
1419
|
-
return fail(s);
|
|
1420
|
-
return used ? toText(input, start, end) : undefined;
|
|
1421
|
-
};
|
|
1422
|
-
}
|
|
1423
|
-
/**
|
|
1424
|
-
* `Ascii.float` and `Text.float` (`map(floatText, toFloat)`): numbers in
|
|
1425
|
-
* decimal notation are matched and converted in one pass, without
|
|
1426
|
-
* slicing the input; the rest runs the compiled parser (see the JIT).
|
|
1427
|
-
*/
|
|
1428
|
-
float(node) {
|
|
1429
|
-
if (!floatTexts.has(node.self) || this.kind === 2)
|
|
1430
|
-
return undefined;
|
|
1431
|
-
let taken = nodeOf(node.self);
|
|
1432
|
-
while (taken._tag === "Expected")
|
|
1433
|
-
taken = nodeOf(taken.self);
|
|
1434
|
-
if (taken._tag !== "Taken")
|
|
1435
|
-
return undefined;
|
|
1436
|
-
const enc = taken.encoding;
|
|
1437
|
-
if (enc !== undefined && !(this.native(enc) && enc.unit === 1 && enc.asciiCompatible))
|
|
1438
|
-
return undefined;
|
|
1439
|
-
const c = this.compile(node.self, true);
|
|
1440
|
-
const scan = (this.kind === 0 ? scanFloatString : scanFloatBytes);
|
|
1441
|
-
const resumable = this.resumable;
|
|
1442
|
-
return (s) => {
|
|
1443
|
-
const v = scan(s.input, s.pos, s.end);
|
|
1444
|
-
// Near the end of a partial input, the parser may have to report `Incomplete`
|
|
1445
|
-
if (v === undefined || (resumable && s.partial && floatScan.read + Margin > s.end)) {
|
|
1446
|
-
const t = c(s);
|
|
1447
|
-
return t === Fail ? Fail : toFloat(t);
|
|
1448
|
-
}
|
|
1449
|
-
if (v === Fail)
|
|
1450
|
-
return fail(s);
|
|
1451
|
-
s.pos = floatScan.end;
|
|
1452
|
-
return v;
|
|
1453
|
-
};
|
|
1454
|
-
}
|
|
1455
|
-
escaped(node) {
|
|
1456
|
-
const raw = node.raw;
|
|
1457
|
-
const normal = this.compile(node.normal, !raw);
|
|
1458
|
-
const escape = this.compile(node.escape, !raw);
|
|
1459
|
-
const enc = node.encoding;
|
|
1460
|
-
const ctrl = node.control;
|
|
1461
|
-
const kind = this.kind;
|
|
1462
|
-
// Matches the control character, advancing past it
|
|
1463
|
-
let control;
|
|
1464
|
-
if (enc === undefined || (ctrl < fastLimit(enc) && ctrl < TableSize)) {
|
|
1465
|
-
control = kind === 0
|
|
1466
|
-
? (s) => {
|
|
1467
|
-
if (s.pos < s.end && s.input.charCodeAt(s.pos) === ctrl) {
|
|
1468
|
-
s.pos++;
|
|
1469
|
-
return true;
|
|
1470
|
-
}
|
|
1471
|
-
return false;
|
|
1472
|
-
}
|
|
1473
|
-
: (s) => {
|
|
1474
|
-
if (s.pos < s.end && s.input[s.pos] === ctrl) {
|
|
1475
|
-
s.pos++;
|
|
1476
|
-
return true;
|
|
1477
|
-
}
|
|
1478
|
-
return false;
|
|
1479
|
-
};
|
|
1480
|
-
}
|
|
1481
|
-
else {
|
|
1482
|
-
control = (s) => {
|
|
1483
|
-
if (s.pos >= s.end)
|
|
1484
|
-
return false;
|
|
1485
|
-
const cp = enc.decode(s.input, s.pos, s.end);
|
|
1486
|
-
if (cp !== ctrl)
|
|
1487
|
-
return false;
|
|
1488
|
-
s.pos += enc.width(cp);
|
|
1489
|
-
return true;
|
|
1490
|
-
};
|
|
1491
|
-
}
|
|
1492
|
-
const resumable = this.resumable;
|
|
1493
|
-
const key = {};
|
|
1494
|
-
/** Fails as incomplete, keeping a checkpoint in a streaming parse. */
|
|
1495
|
-
const incomplete = (s, start, checkpoint) => {
|
|
1496
|
-
if (s.resume !== undefined)
|
|
1497
|
-
Resume.save(s, key, start, checkpoint);
|
|
1498
|
-
s.mode = INC;
|
|
1499
|
-
return Fail;
|
|
1500
|
-
};
|
|
1501
|
-
if (raw) {
|
|
1502
|
-
const slice = enc === undefined
|
|
1503
|
-
? (s, start) => s.slice(start, s.pos)
|
|
1504
|
-
: (s, start) => enc.text(s.input, start, s.pos);
|
|
1505
|
-
return (s) => {
|
|
1506
|
-
const start = s.pos;
|
|
1507
|
-
let resumed = false;
|
|
1508
|
-
if (resumable && s.resume !== undefined) {
|
|
1509
|
-
const cp = Resume.take(s, key, start);
|
|
1510
|
-
if (cp instanceof Resume.Done) {
|
|
1511
|
-
s.pos = cp.pos;
|
|
1512
|
-
return cp.value;
|
|
1513
|
-
}
|
|
1514
|
-
if (cp !== undefined) {
|
|
1515
|
-
s.pos = cp;
|
|
1516
|
-
resumed = true;
|
|
1517
|
-
}
|
|
1518
|
-
}
|
|
1519
|
-
while (s.pos < s.end) {
|
|
1520
|
-
const before = s.pos;
|
|
1521
|
-
if (normal(s) !== Fail) {
|
|
1522
|
-
if (s.pos === before) {
|
|
1523
|
-
s.mode = CUT;
|
|
1524
|
-
return Fail;
|
|
1525
|
-
}
|
|
1526
|
-
continue;
|
|
1527
|
-
}
|
|
1528
|
-
if (s.mode === INC)
|
|
1529
|
-
return incomplete(s, start, before);
|
|
1530
|
-
if (s.mode !== BT)
|
|
1531
|
-
return Fail;
|
|
1532
|
-
s.pos = before;
|
|
1533
|
-
if (control(s)) {
|
|
1534
|
-
if (escape(s) === Fail)
|
|
1535
|
-
return s.mode === INC ? incomplete(s, start, before) : Fail;
|
|
1536
|
-
}
|
|
1537
|
-
else {
|
|
1538
|
-
return resumed ? Resume.done(s, key, start, true, slice(s, start)) : slice(s, start);
|
|
1539
|
-
}
|
|
1540
|
-
}
|
|
1541
|
-
if (s.partial)
|
|
1542
|
-
return incomplete(s, start, s.pos);
|
|
1543
|
-
return resumed ? Resume.done(s, key, start, true, slice(s, start)) : slice(s, start);
|
|
1544
|
-
};
|
|
1545
|
-
}
|
|
1546
|
-
return (s) => {
|
|
1547
|
-
const start = s.pos;
|
|
1548
|
-
let out = "";
|
|
1549
|
-
let resumed = false;
|
|
1550
|
-
if (resumable && s.resume !== undefined) {
|
|
1551
|
-
const cp = Resume.take(s, key, start);
|
|
1552
|
-
if (cp instanceof Resume.Done) {
|
|
1553
|
-
s.pos = cp.pos;
|
|
1554
|
-
return cp.value;
|
|
1555
|
-
}
|
|
1556
|
-
if (cp !== undefined) {
|
|
1557
|
-
out = cp.out;
|
|
1558
|
-
s.pos = cp.pos;
|
|
1559
|
-
resumed = true;
|
|
1560
|
-
}
|
|
1561
|
-
}
|
|
1562
|
-
while (s.pos < s.end) {
|
|
1563
|
-
const before = s.pos;
|
|
1564
|
-
const a = normal(s);
|
|
1565
|
-
if (a !== Fail) {
|
|
1566
|
-
if (s.pos !== before) {
|
|
1567
|
-
out += a;
|
|
1568
|
-
continue;
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
else {
|
|
1572
|
-
if (s.mode === INC)
|
|
1573
|
-
return incomplete(s, start, { out, pos: before });
|
|
1574
|
-
if (s.mode !== BT)
|
|
1575
|
-
return Fail;
|
|
1576
|
-
s.pos = before;
|
|
1577
|
-
}
|
|
1578
|
-
if (control(s)) {
|
|
1579
|
-
const b = escape(s);
|
|
1580
|
-
if (b === Fail)
|
|
1581
|
-
return s.mode === INC ? incomplete(s, start, { out, pos: before }) : Fail;
|
|
1582
|
-
out += b;
|
|
1583
|
-
}
|
|
1584
|
-
else {
|
|
1585
|
-
return resumed ? Resume.done(s, key, start, true, out) : out;
|
|
1586
|
-
}
|
|
1587
|
-
}
|
|
1588
|
-
if (s.partial)
|
|
1589
|
-
return incomplete(s, start, { out, pos: s.pos });
|
|
1590
|
-
return resumed ? Resume.done(s, key, start, true, out) : out;
|
|
1591
|
-
};
|
|
1592
|
-
}
|
|
1593
|
-
}
|
|
38
|
+
var Compiler = class {
|
|
39
|
+
kind;
|
|
40
|
+
resumable;
|
|
41
|
+
used = /* @__PURE__ */ new Map();
|
|
42
|
+
unused = /* @__PURE__ */ new Map();
|
|
43
|
+
firsts;
|
|
44
|
+
fallbacks = [];
|
|
45
|
+
fellBack = /* @__PURE__ */ new Set();
|
|
46
|
+
budget = maxNodes;
|
|
47
|
+
/**
|
|
48
|
+
* `resumable`: compile for streaming parses, which may be partial (see
|
|
49
|
+
* `state.partial`) and keep checkpoints (see `internal/resume.ts`).
|
|
50
|
+
*/
|
|
51
|
+
constructor(kind, resumable = false) {
|
|
52
|
+
this.kind = kind;
|
|
53
|
+
this.resumable = resumable;
|
|
54
|
+
this.firsts = new FirstSets(kind);
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* In a resumable build, runs the compiled token parser `step`, and falls
|
|
58
|
+
* back to the interpreted one when it read within `lookahead` units of
|
|
59
|
+
* the end of a partial input, where it might have to report `Incomplete`.
|
|
60
|
+
* Compiled token parsers leave `state.pos` where they stopped reading,
|
|
61
|
+
* even when they fail.
|
|
62
|
+
*/
|
|
63
|
+
partialSafe(p, step, lookahead) {
|
|
64
|
+
if (!this.resumable) return step;
|
|
65
|
+
const interpreted = p.step;
|
|
66
|
+
const ahead = Math.max(lookahead, Margin);
|
|
67
|
+
return (s) => {
|
|
68
|
+
if (!s.partial) return step(s);
|
|
69
|
+
const start = s.pos;
|
|
70
|
+
const r = step(s);
|
|
71
|
+
if (s.pos + ahead <= s.end) return r;
|
|
72
|
+
s.pos = start;
|
|
73
|
+
return interpreted(s);
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
/** Compiles `p`; when `used` is false its output is not needed. */
|
|
77
|
+
compile(p, used) {
|
|
78
|
+
const memo = used ? this.used : this.unused;
|
|
79
|
+
const slot = memo.get(p);
|
|
80
|
+
if (slot !== void 0) {
|
|
81
|
+
if (slot.fn !== void 0) return slot.fn;
|
|
82
|
+
return (s) => (slot.fn ?? p.step)(s);
|
|
83
|
+
}
|
|
84
|
+
const fresh = { fn: void 0 };
|
|
85
|
+
memo.set(p, fresh);
|
|
86
|
+
const node = nodeOf(p);
|
|
87
|
+
const fn = --this.budget < 0 ? this.fallback(p, node, "node budget exceeded") : this.build(p, node, used);
|
|
88
|
+
fresh.fn = fn;
|
|
89
|
+
return fn;
|
|
90
|
+
}
|
|
91
|
+
fallback(p, node, reason) {
|
|
92
|
+
this.note(p, node, reason);
|
|
93
|
+
return p.step;
|
|
94
|
+
}
|
|
95
|
+
/** Records that (part of) `p` runs interpreted. */
|
|
96
|
+
note(p, node, reason) {
|
|
97
|
+
if (!this.fellBack.has(p)) {
|
|
98
|
+
this.fellBack.add(p);
|
|
99
|
+
this.fallbacks.push({
|
|
100
|
+
name: p.name,
|
|
101
|
+
tag: node._tag,
|
|
102
|
+
reason
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
/** Whether a token node reads this input kind natively. */
|
|
107
|
+
native(enc) {
|
|
108
|
+
return enc === void 0 ? this.kind !== 2 : kindOfEncoding(enc) === this.kind;
|
|
109
|
+
}
|
|
110
|
+
build(p, node, used) {
|
|
111
|
+
switch (node._tag) {
|
|
112
|
+
case "Opaque": return this.fallback(p, node, "not describable (Parser.make, gen, ...)");
|
|
113
|
+
case "Succeed": {
|
|
114
|
+
const value = node.value;
|
|
115
|
+
return () => value;
|
|
116
|
+
}
|
|
117
|
+
case "Sync": {
|
|
118
|
+
const evaluate = node.evaluate;
|
|
119
|
+
return used ? () => evaluate() : () => void 0;
|
|
120
|
+
}
|
|
121
|
+
case "Fail": {
|
|
122
|
+
const mode = node.mode;
|
|
123
|
+
return (s) => {
|
|
124
|
+
s.mode = mode;
|
|
125
|
+
return Fail;
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
case "Eof": return (s) => {
|
|
129
|
+
if (s.pos < s.end) {
|
|
130
|
+
s.mode = BT;
|
|
131
|
+
return Fail;
|
|
132
|
+
}
|
|
133
|
+
if (s.partial) {
|
|
134
|
+
s.mode = INC;
|
|
135
|
+
return Fail;
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
case "Location": return (s) => s.pos;
|
|
139
|
+
case "RestLen": return (s) => s.end - s.pos;
|
|
140
|
+
case "Suspend": return this.compile(node.resolve(), used);
|
|
141
|
+
case "Any":
|
|
142
|
+
case "Literal":
|
|
143
|
+
case "OneOf":
|
|
144
|
+
case "Take":
|
|
145
|
+
case "TakeWhile":
|
|
146
|
+
case "TakeUntil":
|
|
147
|
+
case "Rest":
|
|
148
|
+
if (!this.native(node.encoding)) return this.fallback(p, node, "token parser on array input");
|
|
149
|
+
return this.partialSafe(p, node.encoding === void 0 ? this.token(p, node, used) : this.text(p, node, node.encoding, used), node._tag === "Literal" || node._tag === "TakeUntil" ? node.literal.length * 4 : Margin);
|
|
150
|
+
case "TillLineEnding":
|
|
151
|
+
case "Identifier":
|
|
152
|
+
case "Bom": return this.fallback(p, node, "leaf parser without a compiled form");
|
|
153
|
+
case "Escaped":
|
|
154
|
+
if (!this.native(node.encoding)) return this.fallback(p, node, "token parser on array input");
|
|
155
|
+
return this.escaped(node);
|
|
156
|
+
case "Map": {
|
|
157
|
+
if (!used) return this.compile(node.self, false);
|
|
158
|
+
if (node.f === toFloat) {
|
|
159
|
+
const step = this.float(node);
|
|
160
|
+
if (step !== void 0) return step;
|
|
161
|
+
}
|
|
162
|
+
if (node.f === fromEntries && !this.resumable && buildsEntries(node.self)) return this.entries(node.self);
|
|
163
|
+
const c = this.compile(node.self, true);
|
|
164
|
+
const f = node.f;
|
|
165
|
+
return (s) => {
|
|
166
|
+
const a = c(s);
|
|
167
|
+
return a === Fail ? Fail : f(a);
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
case "As": {
|
|
171
|
+
const c = this.compile(node.self, false);
|
|
172
|
+
if (!used) return c;
|
|
173
|
+
const value = node.value;
|
|
174
|
+
return (s) => c(s) === Fail ? Fail : value;
|
|
175
|
+
}
|
|
176
|
+
case "Tap": {
|
|
177
|
+
const c = this.compile(node.self, true);
|
|
178
|
+
const f = node.f;
|
|
179
|
+
return (s) => {
|
|
180
|
+
const a = c(s);
|
|
181
|
+
if (a !== Fail) f(a);
|
|
182
|
+
return a;
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
case "TryMap": {
|
|
186
|
+
const c = this.compile(node.self, true);
|
|
187
|
+
const f = node.f;
|
|
188
|
+
return (s) => {
|
|
189
|
+
const a = c(s);
|
|
190
|
+
if (a === Fail) return Fail;
|
|
191
|
+
const r = f(a);
|
|
192
|
+
if (isSuccess(r)) return r.success;
|
|
193
|
+
s.mode = BT;
|
|
194
|
+
return Fail;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
case "FilterMap": {
|
|
198
|
+
const c = this.compile(node.self, true);
|
|
199
|
+
const f = node.f;
|
|
200
|
+
return (s) => {
|
|
201
|
+
const a = c(s);
|
|
202
|
+
if (a === Fail) return Fail;
|
|
203
|
+
const o = f(a);
|
|
204
|
+
if (isSome(o)) return o.value;
|
|
205
|
+
s.mode = BT;
|
|
206
|
+
return Fail;
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
case "Filter": {
|
|
210
|
+
const c = this.compile(node.self, true);
|
|
211
|
+
const predicate = node.predicate;
|
|
212
|
+
return (s) => {
|
|
213
|
+
const a = c(s);
|
|
214
|
+
if (a === Fail) return Fail;
|
|
215
|
+
if (predicate(a)) return a;
|
|
216
|
+
s.mode = BT;
|
|
217
|
+
return Fail;
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
case "FlatMap": {
|
|
221
|
+
const c = this.compile(node.self, true);
|
|
222
|
+
const f = node.f;
|
|
223
|
+
this.note(p, node, "the parser returned by the function runs interpreted");
|
|
224
|
+
return (s) => {
|
|
225
|
+
const a = c(s);
|
|
226
|
+
return a === Fail ? Fail : f(a).step(s);
|
|
227
|
+
};
|
|
228
|
+
}
|
|
229
|
+
case "Taken":
|
|
230
|
+
if (node.encoding !== void 0 && !this.native(node.encoding)) return this.fallback(p, node, "Text parser on another input kind");
|
|
231
|
+
return this.taken(node, used);
|
|
232
|
+
case "WithTaken": {
|
|
233
|
+
const c = this.compile(node.self, true);
|
|
234
|
+
return (s) => {
|
|
235
|
+
const start = s.pos;
|
|
236
|
+
const a = c(s);
|
|
237
|
+
return a === Fail ? Fail : [a, s.slice(start, s.pos)];
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
case "Span": {
|
|
241
|
+
const c = this.compile(node.self, false);
|
|
242
|
+
if (!used) return c;
|
|
243
|
+
return (s) => {
|
|
244
|
+
const start = s.pos;
|
|
245
|
+
return c(s) === Fail ? Fail : {
|
|
246
|
+
start,
|
|
247
|
+
end: s.pos
|
|
248
|
+
};
|
|
249
|
+
};
|
|
250
|
+
}
|
|
251
|
+
case "WithSpan": {
|
|
252
|
+
const c = this.compile(node.self, true);
|
|
253
|
+
return (s) => {
|
|
254
|
+
const start = s.pos;
|
|
255
|
+
const a = c(s);
|
|
256
|
+
return a === Fail ? Fail : [a, {
|
|
257
|
+
start,
|
|
258
|
+
end: s.pos
|
|
259
|
+
}];
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
case "Mode": {
|
|
263
|
+
const c = this.compile(node.self, used);
|
|
264
|
+
const from = node.from;
|
|
265
|
+
const to = node.to;
|
|
266
|
+
return (s) => {
|
|
267
|
+
const a = c(s);
|
|
268
|
+
if (a === Fail && s.mode === from) s.mode = to;
|
|
269
|
+
return a;
|
|
270
|
+
};
|
|
271
|
+
}
|
|
272
|
+
case "Context":
|
|
273
|
+
case "Expected":
|
|
274
|
+
case "MapError":
|
|
275
|
+
case "OrFail":
|
|
276
|
+
case "Trace":
|
|
277
|
+
case "Syntax":
|
|
278
|
+
case "RetryAfter": return this.compile(node.self, used);
|
|
279
|
+
case "ResumeAfter": {
|
|
280
|
+
const c = this.compile(node.self, used);
|
|
281
|
+
if (!used) return c;
|
|
282
|
+
return (s) => {
|
|
283
|
+
const a = c(s);
|
|
284
|
+
return a === Fail ? Fail : Option.some(a);
|
|
285
|
+
};
|
|
286
|
+
}
|
|
287
|
+
case "Opt":
|
|
288
|
+
case "OrElseSucceed": {
|
|
289
|
+
const c = this.compile(node.self, used);
|
|
290
|
+
const orElse = !used ? void 0 : node._tag === "Opt" ? Option.none : node.value;
|
|
291
|
+
const some = used && node._tag === "Opt";
|
|
292
|
+
return (s) => {
|
|
293
|
+
const start = s.pos;
|
|
294
|
+
const a = c(s);
|
|
295
|
+
if (a !== Fail) return some ? Option.some(a) : a;
|
|
296
|
+
if (s.mode !== BT) return Fail;
|
|
297
|
+
s.pos = start;
|
|
298
|
+
return orElse === void 0 ? void 0 : orElse();
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
case "Peek": {
|
|
302
|
+
const c = this.compile(node.self, used);
|
|
303
|
+
return (s) => {
|
|
304
|
+
const start = s.pos;
|
|
305
|
+
const a = c(s);
|
|
306
|
+
if (a !== Fail) s.pos = start;
|
|
307
|
+
return a;
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
case "Not": {
|
|
311
|
+
const c = this.compile(node.self, false);
|
|
312
|
+
return (s) => {
|
|
313
|
+
const start = s.pos;
|
|
314
|
+
if (c(s) !== Fail) {
|
|
315
|
+
s.pos = start;
|
|
316
|
+
s.mode = BT;
|
|
317
|
+
return Fail;
|
|
318
|
+
}
|
|
319
|
+
if (s.mode !== BT) return Fail;
|
|
320
|
+
s.pos = start;
|
|
321
|
+
};
|
|
322
|
+
}
|
|
323
|
+
case "Seq": return this.seq(node, used);
|
|
324
|
+
case "Struct": {
|
|
325
|
+
const keys = node.keys;
|
|
326
|
+
const steps = node.parsers.map((q) => this.compile(q, used));
|
|
327
|
+
const n = steps.length;
|
|
328
|
+
if (!used) return this.chain(steps);
|
|
329
|
+
return (s) => {
|
|
330
|
+
const out = {};
|
|
331
|
+
for (let i = 0; i < n; i++) {
|
|
332
|
+
const a = steps[i](s);
|
|
333
|
+
if (a === Fail) return Fail;
|
|
334
|
+
out[keys[i]] = a;
|
|
335
|
+
}
|
|
336
|
+
return out;
|
|
337
|
+
};
|
|
338
|
+
}
|
|
339
|
+
case "ZipWith": {
|
|
340
|
+
const c1 = this.compile(node.self, used);
|
|
341
|
+
const c2 = this.compile(node.that, used);
|
|
342
|
+
if (!used) return this.chain([c1, c2]);
|
|
343
|
+
const f = node.f;
|
|
344
|
+
return (s) => {
|
|
345
|
+
const a = c1(s);
|
|
346
|
+
if (a === Fail) return Fail;
|
|
347
|
+
const b = c2(s);
|
|
348
|
+
return b === Fail ? Fail : f(a, b);
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
case "Alt": return this.alt(node, used);
|
|
352
|
+
case "Dispatch": return this.dispatch(p, node, used);
|
|
353
|
+
case "Repeat":
|
|
354
|
+
case "Fold": return this.repeat(node, used);
|
|
355
|
+
case "RepeatTill": return this.repeatTill(node);
|
|
356
|
+
case "Separated": return this.separated(node, used);
|
|
357
|
+
case "SeparatedFold": return this.separatedFold(node);
|
|
358
|
+
case "Service":
|
|
359
|
+
case "Provide": return this.fallback(p, node, "services stay interpreted");
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/** Runs steps in order, for their effect on the cursor only. */
|
|
363
|
+
chain(steps) {
|
|
364
|
+
const n = steps.length;
|
|
365
|
+
if (n === 0) return () => void 0;
|
|
366
|
+
if (n === 1) return steps[0];
|
|
367
|
+
if (n === 2) {
|
|
368
|
+
const [a, b] = steps;
|
|
369
|
+
return (s) => a(s) === Fail ? Fail : b(s);
|
|
370
|
+
}
|
|
371
|
+
return (s) => {
|
|
372
|
+
for (let i = 0; i < n; i++) if (steps[i](s) === Fail) return Fail;
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
seq(node, used) {
|
|
376
|
+
const output = node.output;
|
|
377
|
+
const keep = (i) => used && (output === "tuple" || output === i || Array.isArray(output) && output.includes(i));
|
|
378
|
+
const steps = node.parsers.map((q, i) => this.compile(q, keep(i)));
|
|
379
|
+
const n = steps.length;
|
|
380
|
+
if (!used) return this.chain(steps);
|
|
381
|
+
if (output === "tuple") return (s) => {
|
|
382
|
+
const out = new Array(n);
|
|
383
|
+
for (let i = 0; i < n; i++) {
|
|
384
|
+
const a = steps[i](s);
|
|
385
|
+
if (a === Fail) return Fail;
|
|
386
|
+
out[i] = a;
|
|
387
|
+
}
|
|
388
|
+
return out;
|
|
389
|
+
};
|
|
390
|
+
if (Predicate.isNumber(output)) {
|
|
391
|
+
if (n === 2) {
|
|
392
|
+
const [a, b] = steps;
|
|
393
|
+
return output === 0 ? (s) => {
|
|
394
|
+
const x = a(s);
|
|
395
|
+
if (x === Fail) return Fail;
|
|
396
|
+
return b(s) === Fail ? Fail : x;
|
|
397
|
+
} : (s) => a(s) === Fail ? Fail : b(s);
|
|
398
|
+
}
|
|
399
|
+
return (s) => {
|
|
400
|
+
let out;
|
|
401
|
+
for (let i = 0; i < n; i++) {
|
|
402
|
+
const a = steps[i](s);
|
|
403
|
+
if (a === Fail) return Fail;
|
|
404
|
+
if (i === output) out = a;
|
|
405
|
+
}
|
|
406
|
+
return out;
|
|
407
|
+
};
|
|
408
|
+
}
|
|
409
|
+
const [i0, i1] = output;
|
|
410
|
+
return (s) => {
|
|
411
|
+
let x;
|
|
412
|
+
let y;
|
|
413
|
+
for (let i = 0; i < n; i++) {
|
|
414
|
+
const a = steps[i](s);
|
|
415
|
+
if (a === Fail) return Fail;
|
|
416
|
+
if (i === i0) x = a;
|
|
417
|
+
else if (i === i1) y = a;
|
|
418
|
+
}
|
|
419
|
+
return [x, y];
|
|
420
|
+
};
|
|
421
|
+
}
|
|
422
|
+
/** The unit at the cursor, or -1 at the end of the input. */
|
|
423
|
+
unitReader() {
|
|
424
|
+
return this.kind === 0 ? (s) => s.pos < s.end ? s.input.charCodeAt(s.pos) : -1 : (s) => s.pos < s.end ? s.input[s.pos] : -1;
|
|
425
|
+
}
|
|
426
|
+
alt(node, used) {
|
|
427
|
+
const steps = node.parsers.map((q) => this.compile(q, used));
|
|
428
|
+
const n = steps.length;
|
|
429
|
+
if (n === 0) return (s) => {
|
|
430
|
+
s.mode = BT;
|
|
431
|
+
return Fail;
|
|
432
|
+
};
|
|
433
|
+
if (n === 1) return steps[0];
|
|
434
|
+
const plan = this.kind === 2 ? void 0 : planAlt(node.parsers.map((q) => this.firsts.of(q)));
|
|
435
|
+
if (plan === void 0) return (s) => {
|
|
436
|
+
const start = s.pos;
|
|
437
|
+
for (let i = 0; i < n; i++) {
|
|
438
|
+
const a = steps[i](s);
|
|
439
|
+
if (a !== Fail) return a;
|
|
440
|
+
if (s.mode !== BT) return Fail;
|
|
441
|
+
s.pos = start;
|
|
442
|
+
}
|
|
443
|
+
s.mode = BT;
|
|
444
|
+
return Fail;
|
|
445
|
+
};
|
|
446
|
+
const lists = plan.lists.map((l) => l.map((i) => steps[i]));
|
|
447
|
+
const { byUnit, end, high } = plan;
|
|
448
|
+
const unit = this.unitReader();
|
|
449
|
+
const resumable = this.resumable;
|
|
450
|
+
return (s) => {
|
|
451
|
+
const u = resumable && s.partial && s.pos + Margin > s.end ? -2 : unit(s);
|
|
452
|
+
const candidates = u === -2 ? steps : lists[u < 0 ? end : u < TableSize ? byUnit[u] : high];
|
|
453
|
+
const start = s.pos;
|
|
454
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
455
|
+
const a = candidates[i](s);
|
|
456
|
+
if (a !== Fail) return a;
|
|
457
|
+
if (s.mode !== BT) return Fail;
|
|
458
|
+
s.pos = start;
|
|
459
|
+
}
|
|
460
|
+
s.mode = BT;
|
|
461
|
+
return Fail;
|
|
462
|
+
};
|
|
463
|
+
}
|
|
464
|
+
dispatch(p, node, used) {
|
|
465
|
+
const selector = this.compile(node.selector, true);
|
|
466
|
+
const choose = node.choose;
|
|
467
|
+
const compiled = /* @__PURE__ */ new Map();
|
|
468
|
+
if (node.cases !== void 0) for (const key of Reflect.ownKeys(node.cases)) {
|
|
469
|
+
const q = node.cases[key];
|
|
470
|
+
if (!compiled.has(q)) compiled.set(q, this.compile(q, used));
|
|
471
|
+
}
|
|
472
|
+
if (node.otherwise !== void 0 && !compiled.has(node.otherwise)) compiled.set(node.otherwise, this.compile(node.otherwise, used));
|
|
473
|
+
const general = (s) => {
|
|
474
|
+
const start = s.pos;
|
|
475
|
+
const key = selector(s);
|
|
476
|
+
if (key === Fail) return Fail;
|
|
477
|
+
const q = choose(key);
|
|
478
|
+
if (q === void 0) {
|
|
479
|
+
s.pos = start;
|
|
480
|
+
s.mode = BT;
|
|
481
|
+
return Fail;
|
|
482
|
+
}
|
|
483
|
+
return (compiled.get(q) ?? q.step)(s);
|
|
484
|
+
};
|
|
485
|
+
if (node.cases === void 0) {
|
|
486
|
+
this.note(p, node, "the parser returned by the function of cases runs interpreted");
|
|
487
|
+
return general;
|
|
488
|
+
}
|
|
489
|
+
let sel = nodeOf(node.selector);
|
|
490
|
+
const peek = sel._tag === "Peek";
|
|
491
|
+
if (peek) sel = nodeOf(sel.self);
|
|
492
|
+
if (sel._tag !== "Any" || !this.native(sel.encoding)) return general;
|
|
493
|
+
const enc = sel.encoding;
|
|
494
|
+
const limit = enc === void 0 ? this.kind === 0 ? 55296 : TableSize : Math.min(fastLimit(enc), 55296);
|
|
495
|
+
const size = Math.min(limit, TableSize);
|
|
496
|
+
if (size === 0) return general;
|
|
497
|
+
const table = [];
|
|
498
|
+
for (let u = 0; u < size; u++) {
|
|
499
|
+
const q = choose(enc === void 0 && this.kind === 1 ? u : charOf(u));
|
|
500
|
+
table.push(q === void 0 ? null : compiled.get(q) ?? q.step);
|
|
501
|
+
}
|
|
502
|
+
const unit = this.unitReader();
|
|
503
|
+
const advance = peek ? 0 : 1;
|
|
504
|
+
const resumable = this.resumable;
|
|
505
|
+
return (s) => {
|
|
506
|
+
if (resumable && s.partial && s.pos + Margin > s.end) return general(s);
|
|
507
|
+
const u = unit(s);
|
|
508
|
+
if (u < 0) {
|
|
509
|
+
s.mode = BT;
|
|
510
|
+
return Fail;
|
|
511
|
+
}
|
|
512
|
+
if (u >= size) return general(s);
|
|
513
|
+
const q = table[u];
|
|
514
|
+
if (q === null) {
|
|
515
|
+
s.mode = BT;
|
|
516
|
+
return Fail;
|
|
517
|
+
}
|
|
518
|
+
s.pos += advance;
|
|
519
|
+
return q(s);
|
|
520
|
+
};
|
|
521
|
+
}
|
|
522
|
+
repeat(node, used) {
|
|
523
|
+
const { max, min } = node;
|
|
524
|
+
const checkLoop = max === Infinity;
|
|
525
|
+
const isFold = node._tag === "Fold";
|
|
526
|
+
const cond = isFold ? node.while : void 0;
|
|
527
|
+
const c = this.compile(node.self, used);
|
|
528
|
+
const resumable = this.resumable;
|
|
529
|
+
const key = {};
|
|
530
|
+
const tracked = used || cond !== void 0;
|
|
531
|
+
const init = !tracked ? () => void 0 : isFold ? node.init : () => [];
|
|
532
|
+
const add = !tracked ? (acc) => acc : isFold ? node.f : (acc, a) => {
|
|
533
|
+
acc.push(a);
|
|
534
|
+
return acc;
|
|
535
|
+
};
|
|
536
|
+
return (s) => {
|
|
537
|
+
const entry = s.pos;
|
|
538
|
+
let acc;
|
|
539
|
+
let count = 0;
|
|
540
|
+
let resumed = false;
|
|
541
|
+
if (resumable && s.resume !== void 0) {
|
|
542
|
+
const cp = Resume.take(s, key, entry);
|
|
543
|
+
if (cp instanceof Resume.Done) {
|
|
544
|
+
s.pos = cp.pos;
|
|
545
|
+
return cp.value;
|
|
546
|
+
}
|
|
547
|
+
if (cp !== void 0) {
|
|
548
|
+
acc = cp.acc;
|
|
549
|
+
count = cp.count;
|
|
550
|
+
s.pos = cp.pos;
|
|
551
|
+
resumed = true;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (!resumed) acc = init();
|
|
555
|
+
for (; count < max; count++) {
|
|
556
|
+
if (cond !== void 0 && count >= min && !cond(acc)) break;
|
|
557
|
+
const start = s.pos;
|
|
558
|
+
const a = c(s);
|
|
559
|
+
if (a === Fail) {
|
|
560
|
+
if (s.mode === INC && s.resume !== void 0) Resume.save(s, key, entry, {
|
|
561
|
+
acc,
|
|
562
|
+
count,
|
|
563
|
+
pos: start
|
|
564
|
+
});
|
|
565
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
566
|
+
s.pos = start;
|
|
567
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
568
|
+
}
|
|
569
|
+
if (checkLoop && s.pos === start) {
|
|
570
|
+
s.mode = CUT;
|
|
571
|
+
return Fail;
|
|
572
|
+
}
|
|
573
|
+
acc = add(acc, a);
|
|
574
|
+
}
|
|
575
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
576
|
+
};
|
|
577
|
+
}
|
|
578
|
+
/**
|
|
579
|
+
* `map(self, Object.fromEntries)`, where `buildsEntries(self)`: builds the
|
|
580
|
+
* object directly (see the JIT).
|
|
581
|
+
*/
|
|
582
|
+
entries(p) {
|
|
583
|
+
const node = nodeOf(p);
|
|
584
|
+
switch (node._tag) {
|
|
585
|
+
case "Mode": {
|
|
586
|
+
const c = this.entries(node.self);
|
|
587
|
+
const from = node.from;
|
|
588
|
+
const to = node.to;
|
|
589
|
+
return (s) => {
|
|
590
|
+
const a = c(s);
|
|
591
|
+
if (a === Fail && s.mode === from) s.mode = to;
|
|
592
|
+
return a;
|
|
593
|
+
};
|
|
594
|
+
}
|
|
595
|
+
case "Seq": {
|
|
596
|
+
const i = node.output;
|
|
597
|
+
const steps = node.parsers.map((q, j) => j === i ? this.entries(q) : this.compile(q, false));
|
|
598
|
+
return (s) => {
|
|
599
|
+
let out;
|
|
600
|
+
for (let j = 0; j < steps.length; j++) {
|
|
601
|
+
const a = steps[j](s);
|
|
602
|
+
if (a === Fail) return Fail;
|
|
603
|
+
if (j === i) out = a;
|
|
604
|
+
}
|
|
605
|
+
return out;
|
|
606
|
+
};
|
|
607
|
+
}
|
|
608
|
+
case "Repeat":
|
|
609
|
+
case "Separated": {
|
|
610
|
+
const pair = pairOf(node.self);
|
|
611
|
+
if (pair !== void 0) return this.entriesLoop(node, pair);
|
|
612
|
+
const c = this.compile(p, true);
|
|
613
|
+
return (s) => {
|
|
614
|
+
const a = c(s);
|
|
615
|
+
return a === Fail ? Fail : fromEntries(a);
|
|
616
|
+
};
|
|
617
|
+
}
|
|
618
|
+
default: return this.entries(node.self);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
entriesLoop(node, pair) {
|
|
622
|
+
const parts = pair.parsers.map((q, j) => this.compile(q, j === pair.key || j === pair.value));
|
|
623
|
+
const { key: ki, value: vi } = pair;
|
|
624
|
+
const entry = (s, acc) => {
|
|
625
|
+
let key;
|
|
626
|
+
let value;
|
|
627
|
+
for (let j = 0; j < parts.length; j++) {
|
|
628
|
+
const a = parts[j](s);
|
|
629
|
+
if (a === Fail) return false;
|
|
630
|
+
if (j === ki) key = a;
|
|
631
|
+
else if (j === vi) value = a;
|
|
632
|
+
}
|
|
633
|
+
acc.add(key, value);
|
|
634
|
+
return true;
|
|
635
|
+
};
|
|
636
|
+
const { max, min } = node;
|
|
637
|
+
const checkLoop = max === Infinity;
|
|
638
|
+
if (node._tag === "Repeat") return (s) => {
|
|
639
|
+
const acc = new EntriesAcc();
|
|
640
|
+
for (let count = 0; count < max; count++) {
|
|
641
|
+
const start = s.pos;
|
|
642
|
+
let key;
|
|
643
|
+
let value;
|
|
644
|
+
let ok = true;
|
|
645
|
+
for (let j = 0; j < parts.length; j++) {
|
|
646
|
+
const a = parts[j](s);
|
|
647
|
+
if (a === Fail) {
|
|
648
|
+
ok = false;
|
|
649
|
+
break;
|
|
650
|
+
}
|
|
651
|
+
if (j === ki) key = a;
|
|
652
|
+
else if (j === vi) value = a;
|
|
653
|
+
}
|
|
654
|
+
if (!ok) {
|
|
655
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
656
|
+
s.pos = start;
|
|
657
|
+
return acc.result();
|
|
658
|
+
}
|
|
659
|
+
if (checkLoop && s.pos === start) {
|
|
660
|
+
s.mode = CUT;
|
|
661
|
+
return Fail;
|
|
662
|
+
}
|
|
663
|
+
acc.add(key, value);
|
|
664
|
+
}
|
|
665
|
+
return acc.result();
|
|
666
|
+
};
|
|
667
|
+
const sep = this.compile(node.separator, false);
|
|
668
|
+
return (s) => {
|
|
669
|
+
const acc = new EntriesAcc();
|
|
670
|
+
let count = 0;
|
|
671
|
+
if (max === 0) return acc.result();
|
|
672
|
+
let start = s.pos;
|
|
673
|
+
if (!entry(s, acc)) {
|
|
674
|
+
if (s.mode !== BT || min > 0) return Fail;
|
|
675
|
+
s.pos = start;
|
|
676
|
+
return acc.result();
|
|
677
|
+
}
|
|
678
|
+
count++;
|
|
679
|
+
while (count < max) {
|
|
680
|
+
start = s.pos;
|
|
681
|
+
if (sep(s) === Fail) {
|
|
682
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
683
|
+
s.pos = start;
|
|
684
|
+
return acc.result();
|
|
685
|
+
}
|
|
686
|
+
if (checkLoop && s.pos === start) {
|
|
687
|
+
s.mode = CUT;
|
|
688
|
+
return Fail;
|
|
689
|
+
}
|
|
690
|
+
if (!entry(s, acc)) {
|
|
691
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
692
|
+
s.pos = start;
|
|
693
|
+
return acc.result();
|
|
694
|
+
}
|
|
695
|
+
count++;
|
|
696
|
+
}
|
|
697
|
+
return acc.result();
|
|
698
|
+
};
|
|
699
|
+
}
|
|
700
|
+
repeatTill(node) {
|
|
701
|
+
const { max, min } = node;
|
|
702
|
+
const c = this.compile(node.self, true);
|
|
703
|
+
const end = this.compile(node.end, true);
|
|
704
|
+
return (s) => {
|
|
705
|
+
const acc = [];
|
|
706
|
+
while (true) {
|
|
707
|
+
const start = s.pos;
|
|
708
|
+
if (acc.length >= min) {
|
|
709
|
+
const b = end(s);
|
|
710
|
+
if (b !== Fail) return [acc, b];
|
|
711
|
+
if (s.mode !== BT) return Fail;
|
|
712
|
+
s.pos = start;
|
|
713
|
+
if (acc.length >= max) return Fail;
|
|
714
|
+
}
|
|
715
|
+
const a = c(s);
|
|
716
|
+
if (a === Fail) return Fail;
|
|
717
|
+
if (s.pos === start) {
|
|
718
|
+
s.mode = CUT;
|
|
719
|
+
return Fail;
|
|
720
|
+
}
|
|
721
|
+
acc.push(a);
|
|
722
|
+
}
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
separated(node, used) {
|
|
726
|
+
const { max, min } = node;
|
|
727
|
+
const checkLoop = max === Infinity;
|
|
728
|
+
const c = this.compile(node.self, used);
|
|
729
|
+
const sep = this.compile(node.separator, false);
|
|
730
|
+
const resumable = this.resumable;
|
|
731
|
+
const key = {};
|
|
732
|
+
return (s) => {
|
|
733
|
+
const entry = s.pos;
|
|
734
|
+
let acc;
|
|
735
|
+
let count = 0;
|
|
736
|
+
let resumed = false;
|
|
737
|
+
if (resumable && s.resume !== void 0) {
|
|
738
|
+
const cp = Resume.take(s, key, entry);
|
|
739
|
+
if (cp instanceof Resume.Done) {
|
|
740
|
+
s.pos = cp.pos;
|
|
741
|
+
return cp.value;
|
|
742
|
+
}
|
|
743
|
+
if (cp !== void 0) {
|
|
744
|
+
acc = cp.acc;
|
|
745
|
+
count = cp.count;
|
|
746
|
+
s.pos = cp.pos;
|
|
747
|
+
resumed = true;
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
if (!resumed) {
|
|
751
|
+
acc = used ? [] : void 0;
|
|
752
|
+
if (max === 0) return acc;
|
|
753
|
+
const start = s.pos;
|
|
754
|
+
const first = c(s);
|
|
755
|
+
if (first === Fail) {
|
|
756
|
+
if (s.mode !== BT || min > 0) return Fail;
|
|
757
|
+
s.pos = start;
|
|
758
|
+
return acc;
|
|
759
|
+
}
|
|
760
|
+
acc?.push(first);
|
|
761
|
+
count = 1;
|
|
762
|
+
}
|
|
763
|
+
while (count < max) {
|
|
764
|
+
const start = s.pos;
|
|
765
|
+
if (sep(s) === Fail) {
|
|
766
|
+
if (s.mode === INC && s.resume !== void 0) Resume.save(s, key, entry, {
|
|
767
|
+
acc,
|
|
768
|
+
count,
|
|
769
|
+
pos: start
|
|
770
|
+
});
|
|
771
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
772
|
+
s.pos = start;
|
|
773
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
774
|
+
}
|
|
775
|
+
if (checkLoop && s.pos === start) {
|
|
776
|
+
s.mode = CUT;
|
|
777
|
+
return Fail;
|
|
778
|
+
}
|
|
779
|
+
const a = c(s);
|
|
780
|
+
if (a === Fail) {
|
|
781
|
+
if (s.mode === INC && s.resume !== void 0) Resume.save(s, key, entry, {
|
|
782
|
+
acc,
|
|
783
|
+
count,
|
|
784
|
+
pos: start
|
|
785
|
+
});
|
|
786
|
+
if (s.mode !== BT || count < min) return Fail;
|
|
787
|
+
s.pos = start;
|
|
788
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
789
|
+
}
|
|
790
|
+
acc?.push(a);
|
|
791
|
+
count++;
|
|
792
|
+
}
|
|
793
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
794
|
+
};
|
|
795
|
+
}
|
|
796
|
+
separatedFold(node) {
|
|
797
|
+
const c = this.compile(node.self, true);
|
|
798
|
+
const sep = this.compile(node.separator, true);
|
|
799
|
+
const op = node.op;
|
|
800
|
+
const resumable = this.resumable;
|
|
801
|
+
const key = {};
|
|
802
|
+
if (!node.right) return (s) => {
|
|
803
|
+
const entry = s.pos;
|
|
804
|
+
let acc;
|
|
805
|
+
let resumed = false;
|
|
806
|
+
if (resumable && s.resume !== void 0) {
|
|
807
|
+
const cp = Resume.take(s, key, entry);
|
|
808
|
+
if (cp instanceof Resume.Done) {
|
|
809
|
+
s.pos = cp.pos;
|
|
810
|
+
return cp.value;
|
|
811
|
+
}
|
|
812
|
+
if (cp !== void 0) {
|
|
813
|
+
acc = cp.acc;
|
|
814
|
+
s.pos = cp.pos;
|
|
815
|
+
resumed = true;
|
|
816
|
+
}
|
|
817
|
+
}
|
|
818
|
+
if (!resumed) {
|
|
819
|
+
acc = c(s);
|
|
820
|
+
if (acc === Fail) return Fail;
|
|
821
|
+
}
|
|
822
|
+
while (true) {
|
|
823
|
+
const start = s.pos;
|
|
824
|
+
const x = sep(s);
|
|
825
|
+
if (x === Fail) {
|
|
826
|
+
if (s.mode === INC && s.resume !== void 0) Resume.save(s, key, entry, {
|
|
827
|
+
acc,
|
|
828
|
+
pos: start
|
|
829
|
+
});
|
|
830
|
+
if (s.mode !== BT) return Fail;
|
|
831
|
+
s.pos = start;
|
|
832
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
833
|
+
}
|
|
834
|
+
if (s.pos === start) {
|
|
835
|
+
s.mode = CUT;
|
|
836
|
+
return Fail;
|
|
837
|
+
}
|
|
838
|
+
const a = c(s);
|
|
839
|
+
if (a === Fail) {
|
|
840
|
+
if (s.mode === INC && s.resume !== void 0) Resume.save(s, key, entry, {
|
|
841
|
+
acc,
|
|
842
|
+
pos: start
|
|
843
|
+
});
|
|
844
|
+
if (s.mode !== BT) return Fail;
|
|
845
|
+
s.pos = start;
|
|
846
|
+
return resumed ? Resume.done(s, key, entry, true, acc) : acc;
|
|
847
|
+
}
|
|
848
|
+
acc = op(acc, x, a);
|
|
849
|
+
}
|
|
850
|
+
};
|
|
851
|
+
return (s) => {
|
|
852
|
+
const first = c(s);
|
|
853
|
+
if (first === Fail) return Fail;
|
|
854
|
+
const values = [first];
|
|
855
|
+
const seps = [];
|
|
856
|
+
while (true) {
|
|
857
|
+
const start = s.pos;
|
|
858
|
+
const x = sep(s);
|
|
859
|
+
if (x === Fail) {
|
|
860
|
+
if (s.mode !== BT) return Fail;
|
|
861
|
+
s.pos = start;
|
|
862
|
+
break;
|
|
863
|
+
}
|
|
864
|
+
if (s.pos === start) {
|
|
865
|
+
s.mode = CUT;
|
|
866
|
+
return Fail;
|
|
867
|
+
}
|
|
868
|
+
const a = c(s);
|
|
869
|
+
if (a === Fail) {
|
|
870
|
+
if (s.mode !== BT) return Fail;
|
|
871
|
+
s.pos = start;
|
|
872
|
+
break;
|
|
873
|
+
}
|
|
874
|
+
seps.push(x);
|
|
875
|
+
values.push(a);
|
|
876
|
+
}
|
|
877
|
+
let acc = values[values.length - 1];
|
|
878
|
+
for (let i = seps.length - 1; i >= 0; i--) acc = op(values[i], seps[i], acc);
|
|
879
|
+
return acc;
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
token(p, node, used) {
|
|
883
|
+
const kind = this.kind;
|
|
884
|
+
switch (node._tag) {
|
|
885
|
+
case "Any":
|
|
886
|
+
if (kind === 1) return (s) => {
|
|
887
|
+
const pos = s.pos;
|
|
888
|
+
if (pos >= s.end) return fail(s);
|
|
889
|
+
s.pos = pos + 1;
|
|
890
|
+
return s.input[pos];
|
|
891
|
+
};
|
|
892
|
+
return (s) => {
|
|
893
|
+
const pos = s.pos;
|
|
894
|
+
if (pos >= s.end) return fail(s);
|
|
895
|
+
const str = s.input;
|
|
896
|
+
const c = str.charCodeAt(pos);
|
|
897
|
+
if (c >= 55296 && c <= 56319 && pos + 1 < str.length && (str.charCodeAt(pos + 1) & 64512) === 56320) {
|
|
898
|
+
s.pos = pos + 2;
|
|
899
|
+
return used ? str.slice(pos, pos + 2) : void 0;
|
|
900
|
+
}
|
|
901
|
+
s.pos = pos + 1;
|
|
902
|
+
return used ? str[pos] : void 0;
|
|
903
|
+
};
|
|
904
|
+
case "Literal": return this.tokenLiteral(p, node);
|
|
905
|
+
case "OneOf": {
|
|
906
|
+
const set = tokenSet(node.set, kind);
|
|
907
|
+
const negate = node.negate;
|
|
908
|
+
const accept = acceptTable(set, negate);
|
|
909
|
+
const test = set.test;
|
|
910
|
+
if (kind === 1) return (s) => {
|
|
911
|
+
const pos = s.pos;
|
|
912
|
+
if (pos >= s.end) return fail(s);
|
|
913
|
+
const b = s.input[pos];
|
|
914
|
+
if (accept[b] === 0) return fail(s);
|
|
915
|
+
s.pos = pos + 1;
|
|
916
|
+
return b;
|
|
917
|
+
};
|
|
918
|
+
return (s) => {
|
|
919
|
+
const pos = s.pos;
|
|
920
|
+
if (pos >= s.end) return fail(s);
|
|
921
|
+
const str = s.input;
|
|
922
|
+
const c = str.charCodeAt(pos);
|
|
923
|
+
if (c < TableSize) {
|
|
924
|
+
if (accept[c] === 0) return fail(s);
|
|
925
|
+
s.pos = pos + 1;
|
|
926
|
+
return used ? str[pos] : void 0;
|
|
927
|
+
}
|
|
928
|
+
let cp = c;
|
|
929
|
+
if (c >= 55296 && c <= 56319 && pos + 1 < str.length) {
|
|
930
|
+
const d = str.charCodeAt(pos + 1);
|
|
931
|
+
if ((d & 64512) === 56320) cp = (c - 55296 << 10) + (d - 56320) + 65536;
|
|
932
|
+
}
|
|
933
|
+
if (test(cp) === negate) return fail(s);
|
|
934
|
+
const len = cp > 65535 ? 2 : 1;
|
|
935
|
+
s.pos = pos + len;
|
|
936
|
+
return !used ? void 0 : len === 1 ? str[pos] : str.slice(pos, pos + 2);
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
case "Take": {
|
|
940
|
+
const count = node.count;
|
|
941
|
+
if (kind === 1) return (s) => {
|
|
942
|
+
const start = s.pos;
|
|
943
|
+
if (s.end - start < count) return failAt(s, s.end);
|
|
944
|
+
s.pos = start + count;
|
|
945
|
+
return used ? s.input.subarray(start, start + count) : void 0;
|
|
946
|
+
};
|
|
947
|
+
return (s) => {
|
|
948
|
+
const str = s.input;
|
|
949
|
+
const start = s.pos;
|
|
950
|
+
const end = s.end;
|
|
951
|
+
let i = start;
|
|
952
|
+
let got = 0;
|
|
953
|
+
while (got < count && i < end) {
|
|
954
|
+
const c = str.charCodeAt(i);
|
|
955
|
+
i += c >= 55296 && c <= 56319 && i + 1 < str.length && (str.charCodeAt(i + 1) & 64512) === 56320 ? 2 : 1;
|
|
956
|
+
got++;
|
|
957
|
+
}
|
|
958
|
+
if (got < count) return failAt(s, i);
|
|
959
|
+
s.pos = i;
|
|
960
|
+
return used ? str.slice(start, i) : void 0;
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
case "TakeWhile": return this.tokenTakeWhile(node, used);
|
|
964
|
+
case "TakeUntil": {
|
|
965
|
+
const { literal, max, min } = node;
|
|
966
|
+
const bounded = min > 0 || max !== Infinity;
|
|
967
|
+
return (s) => {
|
|
968
|
+
const start = s.pos;
|
|
969
|
+
const idx = indexOf(s, literal, start);
|
|
970
|
+
if (idx < 0) return failAt(s, s.end);
|
|
971
|
+
if (bounded) {
|
|
972
|
+
const count = countTokens(s, start, idx);
|
|
973
|
+
if (count < min || count > max) return failAt(s, idx);
|
|
974
|
+
}
|
|
975
|
+
s.pos = idx;
|
|
976
|
+
return used ? s.slice(start, idx) : void 0;
|
|
977
|
+
};
|
|
978
|
+
}
|
|
979
|
+
case "Rest": return (s) => {
|
|
980
|
+
const start = s.pos;
|
|
981
|
+
s.pos = s.end;
|
|
982
|
+
return used ? s.slice(start, s.end) : void 0;
|
|
983
|
+
};
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
tokenLiteral(p, node) {
|
|
987
|
+
const lit = node.literal;
|
|
988
|
+
const literalKind = Predicate.isString(lit) ? 0 : lit instanceof Uint8Array ? 1 : 2;
|
|
989
|
+
if (this.kind === 0) {
|
|
990
|
+
if (literalKind !== 0) return this.fallback(p, node, "byte or array literal on string input");
|
|
991
|
+
const str = lit;
|
|
992
|
+
const len = str.length;
|
|
993
|
+
if (node.caseless) {
|
|
994
|
+
const lowered = Array.from({ length: len }, (_, i) => lowerAscii(str.charCodeAt(i)));
|
|
995
|
+
return (s) => {
|
|
996
|
+
const pos = s.pos;
|
|
997
|
+
if (s.end - pos < len) return fail(s);
|
|
998
|
+
const input = s.input;
|
|
999
|
+
for (let i = 0; i < len; i++) if (lowerAscii(input.charCodeAt(pos + i)) !== lowered[i]) return fail(s);
|
|
1000
|
+
s.pos = pos + len;
|
|
1001
|
+
return input.slice(pos, pos + len);
|
|
1002
|
+
};
|
|
1003
|
+
}
|
|
1004
|
+
return stringLiteral(str, str);
|
|
1005
|
+
}
|
|
1006
|
+
if (literalKind === 2) return this.fallback(p, node, "array literal on byte input");
|
|
1007
|
+
const bytes = literalKind === 0 ? new TextEncoder().encode(lit) : lit;
|
|
1008
|
+
const len = bytes.length;
|
|
1009
|
+
const sameKind = literalKind === 1 && !node.caseless;
|
|
1010
|
+
const expect = node.caseless ? Array.from(bytes, lowerAscii) : Array.from(bytes);
|
|
1011
|
+
const caseless = node.caseless;
|
|
1012
|
+
return (s) => {
|
|
1013
|
+
const pos = s.pos;
|
|
1014
|
+
if (s.end - pos < len) return fail(s);
|
|
1015
|
+
const input = s.input;
|
|
1016
|
+
for (let i = 0; i < len; i++) {
|
|
1017
|
+
const b = input[pos + i];
|
|
1018
|
+
if ((caseless ? lowerAscii(b) : b) !== expect[i]) return fail(s);
|
|
1019
|
+
}
|
|
1020
|
+
s.pos = pos + len;
|
|
1021
|
+
return sameKind ? lit : input.subarray(pos, pos + len);
|
|
1022
|
+
};
|
|
1023
|
+
}
|
|
1024
|
+
tokenTakeWhile(node, used) {
|
|
1025
|
+
const kind = this.kind;
|
|
1026
|
+
const { max, min, negate } = node;
|
|
1027
|
+
const set = tokenSet(node.set, kind);
|
|
1028
|
+
const accept = acceptTable(set, negate);
|
|
1029
|
+
if (kind === 1) return (s) => {
|
|
1030
|
+
const bytes = s.input;
|
|
1031
|
+
const start = s.pos;
|
|
1032
|
+
const limit = max === Infinity ? s.end : Math.min(s.end, start + max);
|
|
1033
|
+
let i = start;
|
|
1034
|
+
while (i < limit && accept[bytes[i]] === 1) i++;
|
|
1035
|
+
if (i - start < min) return failAt(s, i);
|
|
1036
|
+
s.pos = i;
|
|
1037
|
+
return used ? bytes.subarray(start, i) : void 0;
|
|
1038
|
+
};
|
|
1039
|
+
const test = set.test;
|
|
1040
|
+
return (s) => {
|
|
1041
|
+
const str = s.input;
|
|
1042
|
+
const start = s.pos;
|
|
1043
|
+
const end = s.end;
|
|
1044
|
+
let i = start;
|
|
1045
|
+
let count = 0;
|
|
1046
|
+
while (count < max && i < end) {
|
|
1047
|
+
const c = str.charCodeAt(i);
|
|
1048
|
+
if (c < TableSize) {
|
|
1049
|
+
if (accept[c] === 0) break;
|
|
1050
|
+
i++;
|
|
1051
|
+
} else if (c < 55296 || c > 56319) {
|
|
1052
|
+
if (test(c) === negate) break;
|
|
1053
|
+
i++;
|
|
1054
|
+
} else {
|
|
1055
|
+
let cp = c;
|
|
1056
|
+
if (i + 1 < str.length) {
|
|
1057
|
+
const d = str.charCodeAt(i + 1);
|
|
1058
|
+
if ((d & 64512) === 56320) cp = (c - 55296 << 10) + (d - 56320) + 65536;
|
|
1059
|
+
}
|
|
1060
|
+
if (test(cp) === negate) break;
|
|
1061
|
+
i += cp > 65535 ? 2 : 1;
|
|
1062
|
+
}
|
|
1063
|
+
count++;
|
|
1064
|
+
}
|
|
1065
|
+
if (count < min) return failAt(s, i);
|
|
1066
|
+
s.pos = i;
|
|
1067
|
+
return used ? str.slice(start, i) : void 0;
|
|
1068
|
+
};
|
|
1069
|
+
}
|
|
1070
|
+
text(p, node, enc, used) {
|
|
1071
|
+
const kind = this.kind;
|
|
1072
|
+
const fast = fastLimit(enc);
|
|
1073
|
+
const decode = enc.decode;
|
|
1074
|
+
const width = enc.width;
|
|
1075
|
+
const toText = enc.text;
|
|
1076
|
+
const unitAt = kind === 0 ? (input, i) => input.charCodeAt(i) : (input, i) => input[i];
|
|
1077
|
+
switch (node._tag) {
|
|
1078
|
+
case "Any": return (s) => {
|
|
1079
|
+
const pos = s.pos;
|
|
1080
|
+
const end = s.end;
|
|
1081
|
+
if (pos >= end) return fail(s);
|
|
1082
|
+
const c = unitAt(s.input, pos);
|
|
1083
|
+
if (c < fast) {
|
|
1084
|
+
s.pos = pos + 1;
|
|
1085
|
+
return used ? charOf(c) : void 0;
|
|
1086
|
+
}
|
|
1087
|
+
const cp = decode(s.input, pos, end);
|
|
1088
|
+
if (cp < 0) return fail(s);
|
|
1089
|
+
s.pos = pos + width(cp);
|
|
1090
|
+
return used ? charOf(cp) : void 0;
|
|
1091
|
+
};
|
|
1092
|
+
case "Literal": {
|
|
1093
|
+
const lit = node.literal;
|
|
1094
|
+
if (node.caseless) {
|
|
1095
|
+
const folded = Array.from(lit, (c) => fold(firstCodePoint(c)));
|
|
1096
|
+
const n = folded.length;
|
|
1097
|
+
return (s) => {
|
|
1098
|
+
const start = s.pos;
|
|
1099
|
+
const end = s.end;
|
|
1100
|
+
const input = s.input;
|
|
1101
|
+
let pos = start;
|
|
1102
|
+
for (let i = 0; i < n; i++) {
|
|
1103
|
+
if (pos >= end) return failAt(s, pos);
|
|
1104
|
+
const c = unitAt(input, pos);
|
|
1105
|
+
let cp;
|
|
1106
|
+
if (c < fast) {
|
|
1107
|
+
cp = c;
|
|
1108
|
+
pos++;
|
|
1109
|
+
} else {
|
|
1110
|
+
cp = decode(input, pos, end);
|
|
1111
|
+
if (cp < 0) return failAt(s, pos);
|
|
1112
|
+
pos += width(cp);
|
|
1113
|
+
}
|
|
1114
|
+
if (fold(cp) !== folded[i]) return failAt(s, pos);
|
|
1115
|
+
}
|
|
1116
|
+
s.pos = pos;
|
|
1117
|
+
return used ? toText(input, start, pos) : void 0;
|
|
1118
|
+
};
|
|
1119
|
+
}
|
|
1120
|
+
const encoded = enc.encode(lit);
|
|
1121
|
+
if (Predicate.isString(encoded)) return stringLiteral(encoded, lit);
|
|
1122
|
+
const bytes = encoded;
|
|
1123
|
+
const len = bytes.length;
|
|
1124
|
+
if (len === 1) {
|
|
1125
|
+
const b0 = bytes[0];
|
|
1126
|
+
return (s) => {
|
|
1127
|
+
const pos = s.pos;
|
|
1128
|
+
if (pos < s.end && s.input[pos] === b0) {
|
|
1129
|
+
s.pos = pos + 1;
|
|
1130
|
+
return lit;
|
|
1131
|
+
}
|
|
1132
|
+
return fail(s);
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
return (s) => {
|
|
1136
|
+
const pos = s.pos;
|
|
1137
|
+
if (s.end - pos < len) return fail(s);
|
|
1138
|
+
const input = s.input;
|
|
1139
|
+
for (let i = 0; i < len; i++) if (input[pos + i] !== bytes[i]) return fail(s);
|
|
1140
|
+
s.pos = pos + len;
|
|
1141
|
+
return lit;
|
|
1142
|
+
};
|
|
1143
|
+
}
|
|
1144
|
+
case "OneOf": {
|
|
1145
|
+
const set = textSet(node.set);
|
|
1146
|
+
const negate = node.negate;
|
|
1147
|
+
const accept = acceptTable(set, negate);
|
|
1148
|
+
const test = set.test;
|
|
1149
|
+
return (s) => {
|
|
1150
|
+
const pos = s.pos;
|
|
1151
|
+
const end = s.end;
|
|
1152
|
+
if (pos >= end) return fail(s);
|
|
1153
|
+
const c = unitAt(s.input, pos);
|
|
1154
|
+
if (c < fast && c < TableSize) {
|
|
1155
|
+
if (accept[c] === 0) return fail(s);
|
|
1156
|
+
s.pos = pos + 1;
|
|
1157
|
+
return used ? charOf(c) : void 0;
|
|
1158
|
+
}
|
|
1159
|
+
const cp = c < fast ? c : decode(s.input, pos, end);
|
|
1160
|
+
if (cp < 0 || test(cp) === negate) return fail(s);
|
|
1161
|
+
s.pos = pos + (c < fast ? 1 : width(cp));
|
|
1162
|
+
return used ? charOf(cp) : void 0;
|
|
1163
|
+
};
|
|
1164
|
+
}
|
|
1165
|
+
case "Take": {
|
|
1166
|
+
const count = node.count;
|
|
1167
|
+
return (s) => {
|
|
1168
|
+
const start = s.pos;
|
|
1169
|
+
const end = s.end;
|
|
1170
|
+
const input = s.input;
|
|
1171
|
+
let pos = start;
|
|
1172
|
+
for (let got = 0; got < count; got++) {
|
|
1173
|
+
if (pos >= end) return failAt(s, pos);
|
|
1174
|
+
if (unitAt(input, pos) < fast) pos++;
|
|
1175
|
+
else {
|
|
1176
|
+
const cp = decode(input, pos, end);
|
|
1177
|
+
if (cp < 0) return failAt(s, pos);
|
|
1178
|
+
pos += width(cp);
|
|
1179
|
+
}
|
|
1180
|
+
}
|
|
1181
|
+
s.pos = pos;
|
|
1182
|
+
return used ? toText(input, start, pos) : void 0;
|
|
1183
|
+
};
|
|
1184
|
+
}
|
|
1185
|
+
case "TakeWhile": {
|
|
1186
|
+
const { max, min, negate } = node;
|
|
1187
|
+
const set = textSet(node.set);
|
|
1188
|
+
const accept = acceptTable(set, negate);
|
|
1189
|
+
const test = set.test;
|
|
1190
|
+
const small = Math.min(fast, TableSize);
|
|
1191
|
+
return (s) => {
|
|
1192
|
+
const input = s.input;
|
|
1193
|
+
const start = s.pos;
|
|
1194
|
+
const end = s.end;
|
|
1195
|
+
let i = start;
|
|
1196
|
+
let count = 0;
|
|
1197
|
+
while (count < max && i < end) {
|
|
1198
|
+
const c = unitAt(input, i);
|
|
1199
|
+
if (c < small) {
|
|
1200
|
+
if (accept[c] === 0) break;
|
|
1201
|
+
i++;
|
|
1202
|
+
} else if (c < fast) {
|
|
1203
|
+
if (test(c) === negate) break;
|
|
1204
|
+
i++;
|
|
1205
|
+
} else {
|
|
1206
|
+
const cp = decode(input, i, end);
|
|
1207
|
+
if (cp < 0 || test(cp) === negate) break;
|
|
1208
|
+
i += width(cp);
|
|
1209
|
+
}
|
|
1210
|
+
count++;
|
|
1211
|
+
}
|
|
1212
|
+
if (count < min) return failAt(s, i);
|
|
1213
|
+
s.pos = i;
|
|
1214
|
+
return used ? toText(input, start, i) : void 0;
|
|
1215
|
+
};
|
|
1216
|
+
}
|
|
1217
|
+
case "TakeUntil":
|
|
1218
|
+
case "Rest": return this.fallback(p, node, "Text parser without a compiled form");
|
|
1219
|
+
}
|
|
1220
|
+
}
|
|
1221
|
+
taken(node, used) {
|
|
1222
|
+
const c = this.compile(node.self, false);
|
|
1223
|
+
const enc = node.encoding;
|
|
1224
|
+
if (enc === void 0) {
|
|
1225
|
+
if (!used) return c;
|
|
1226
|
+
if (this.kind === 0) return (s) => {
|
|
1227
|
+
const start = s.pos;
|
|
1228
|
+
return c(s) === Fail ? Fail : s.input.slice(start, s.pos);
|
|
1229
|
+
};
|
|
1230
|
+
return (s) => {
|
|
1231
|
+
const start = s.pos;
|
|
1232
|
+
return c(s) === Fail ? Fail : s.slice(start, s.pos);
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
const fast = fastLimit(enc);
|
|
1236
|
+
const toText = enc.text;
|
|
1237
|
+
const unitAt = this.kind === 0 ? (input, i) => input.charCodeAt(i) : (input, i) => input[i];
|
|
1238
|
+
return (s) => {
|
|
1239
|
+
const start = s.pos;
|
|
1240
|
+
if (c(s) === Fail) return Fail;
|
|
1241
|
+
const end = s.pos;
|
|
1242
|
+
const input = s.input;
|
|
1243
|
+
let i = start;
|
|
1244
|
+
while (i < end && unitAt(input, i) < fast) i++;
|
|
1245
|
+
if (i < end && validate(enc, s, i, end) < 0) return fail(s);
|
|
1246
|
+
return used ? toText(input, start, end) : void 0;
|
|
1247
|
+
};
|
|
1248
|
+
}
|
|
1249
|
+
/**
|
|
1250
|
+
* `Ascii.float` and `Text.float` (`map(floatText, toFloat)`): numbers in
|
|
1251
|
+
* decimal notation are matched and converted in one pass, without
|
|
1252
|
+
* slicing the input; the rest runs the compiled parser (see the JIT).
|
|
1253
|
+
*/
|
|
1254
|
+
float(node) {
|
|
1255
|
+
if (!floatTexts.has(node.self) || this.kind === 2) return void 0;
|
|
1256
|
+
let taken = nodeOf(node.self);
|
|
1257
|
+
while (taken._tag === "Expected") taken = nodeOf(taken.self);
|
|
1258
|
+
if (taken._tag !== "Taken") return void 0;
|
|
1259
|
+
const enc = taken.encoding;
|
|
1260
|
+
if (enc !== void 0 && !(this.native(enc) && enc.unit === 1 && enc.asciiCompatible)) return void 0;
|
|
1261
|
+
const c = this.compile(node.self, true);
|
|
1262
|
+
const scan = this.kind === 0 ? scanFloatString : scanFloatBytes;
|
|
1263
|
+
const resumable = this.resumable;
|
|
1264
|
+
return (s) => {
|
|
1265
|
+
const v = scan(s.input, s.pos, s.end);
|
|
1266
|
+
if (v === void 0 || resumable && s.partial && floatScan.read + Margin > s.end) {
|
|
1267
|
+
const t = c(s);
|
|
1268
|
+
return t === Fail ? Fail : toFloat(t);
|
|
1269
|
+
}
|
|
1270
|
+
if (v === Fail) return fail(s);
|
|
1271
|
+
s.pos = floatScan.end;
|
|
1272
|
+
return v;
|
|
1273
|
+
};
|
|
1274
|
+
}
|
|
1275
|
+
escaped(node) {
|
|
1276
|
+
const raw = node.raw;
|
|
1277
|
+
const normal = this.compile(node.normal, !raw);
|
|
1278
|
+
const escape = this.compile(node.escape, !raw);
|
|
1279
|
+
const enc = node.encoding;
|
|
1280
|
+
const ctrl = node.control;
|
|
1281
|
+
const kind = this.kind;
|
|
1282
|
+
let control;
|
|
1283
|
+
if (enc === void 0 || ctrl < fastLimit(enc) && ctrl < TableSize) control = kind === 0 ? (s) => {
|
|
1284
|
+
if (s.pos < s.end && s.input.charCodeAt(s.pos) === ctrl) {
|
|
1285
|
+
s.pos++;
|
|
1286
|
+
return true;
|
|
1287
|
+
}
|
|
1288
|
+
return false;
|
|
1289
|
+
} : (s) => {
|
|
1290
|
+
if (s.pos < s.end && s.input[s.pos] === ctrl) {
|
|
1291
|
+
s.pos++;
|
|
1292
|
+
return true;
|
|
1293
|
+
}
|
|
1294
|
+
return false;
|
|
1295
|
+
};
|
|
1296
|
+
else control = (s) => {
|
|
1297
|
+
if (s.pos >= s.end) return false;
|
|
1298
|
+
const cp = enc.decode(s.input, s.pos, s.end);
|
|
1299
|
+
if (cp !== ctrl) return false;
|
|
1300
|
+
s.pos += enc.width(cp);
|
|
1301
|
+
return true;
|
|
1302
|
+
};
|
|
1303
|
+
const resumable = this.resumable;
|
|
1304
|
+
const key = {};
|
|
1305
|
+
/** Fails as incomplete, keeping a checkpoint in a streaming parse. */
|
|
1306
|
+
const incomplete = (s, start, checkpoint) => {
|
|
1307
|
+
if (s.resume !== void 0) Resume.save(s, key, start, checkpoint);
|
|
1308
|
+
s.mode = INC;
|
|
1309
|
+
return Fail;
|
|
1310
|
+
};
|
|
1311
|
+
if (raw) {
|
|
1312
|
+
const slice = enc === void 0 ? (s, start) => s.slice(start, s.pos) : (s, start) => enc.text(s.input, start, s.pos);
|
|
1313
|
+
return (s) => {
|
|
1314
|
+
const start = s.pos;
|
|
1315
|
+
let resumed = false;
|
|
1316
|
+
if (resumable && s.resume !== void 0) {
|
|
1317
|
+
const cp = Resume.take(s, key, start);
|
|
1318
|
+
if (cp instanceof Resume.Done) {
|
|
1319
|
+
s.pos = cp.pos;
|
|
1320
|
+
return cp.value;
|
|
1321
|
+
}
|
|
1322
|
+
if (cp !== void 0) {
|
|
1323
|
+
s.pos = cp;
|
|
1324
|
+
resumed = true;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
while (s.pos < s.end) {
|
|
1328
|
+
const before = s.pos;
|
|
1329
|
+
if (normal(s) !== Fail) {
|
|
1330
|
+
if (s.pos === before) {
|
|
1331
|
+
s.mode = CUT;
|
|
1332
|
+
return Fail;
|
|
1333
|
+
}
|
|
1334
|
+
continue;
|
|
1335
|
+
}
|
|
1336
|
+
if (s.mode === INC) return incomplete(s, start, before);
|
|
1337
|
+
if (s.mode !== BT) return Fail;
|
|
1338
|
+
s.pos = before;
|
|
1339
|
+
if (control(s)) {
|
|
1340
|
+
if (escape(s) === Fail) return s.mode === INC ? incomplete(s, start, before) : Fail;
|
|
1341
|
+
} else return resumed ? Resume.done(s, key, start, true, slice(s, start)) : slice(s, start);
|
|
1342
|
+
}
|
|
1343
|
+
if (s.partial) return incomplete(s, start, s.pos);
|
|
1344
|
+
return resumed ? Resume.done(s, key, start, true, slice(s, start)) : slice(s, start);
|
|
1345
|
+
};
|
|
1346
|
+
}
|
|
1347
|
+
return (s) => {
|
|
1348
|
+
const start = s.pos;
|
|
1349
|
+
let out = "";
|
|
1350
|
+
let resumed = false;
|
|
1351
|
+
if (resumable && s.resume !== void 0) {
|
|
1352
|
+
const cp = Resume.take(s, key, start);
|
|
1353
|
+
if (cp instanceof Resume.Done) {
|
|
1354
|
+
s.pos = cp.pos;
|
|
1355
|
+
return cp.value;
|
|
1356
|
+
}
|
|
1357
|
+
if (cp !== void 0) {
|
|
1358
|
+
out = cp.out;
|
|
1359
|
+
s.pos = cp.pos;
|
|
1360
|
+
resumed = true;
|
|
1361
|
+
}
|
|
1362
|
+
}
|
|
1363
|
+
while (s.pos < s.end) {
|
|
1364
|
+
const before = s.pos;
|
|
1365
|
+
const a = normal(s);
|
|
1366
|
+
if (a !== Fail) {
|
|
1367
|
+
if (s.pos !== before) {
|
|
1368
|
+
out += a;
|
|
1369
|
+
continue;
|
|
1370
|
+
}
|
|
1371
|
+
} else {
|
|
1372
|
+
if (s.mode === INC) return incomplete(s, start, {
|
|
1373
|
+
out,
|
|
1374
|
+
pos: before
|
|
1375
|
+
});
|
|
1376
|
+
if (s.mode !== BT) return Fail;
|
|
1377
|
+
s.pos = before;
|
|
1378
|
+
}
|
|
1379
|
+
if (control(s)) {
|
|
1380
|
+
const b = escape(s);
|
|
1381
|
+
if (b === Fail) return s.mode === INC ? incomplete(s, start, {
|
|
1382
|
+
out,
|
|
1383
|
+
pos: before
|
|
1384
|
+
}) : Fail;
|
|
1385
|
+
out += b;
|
|
1386
|
+
} else return resumed ? Resume.done(s, key, start, true, out) : out;
|
|
1387
|
+
}
|
|
1388
|
+
if (s.partial) return incomplete(s, start, {
|
|
1389
|
+
out,
|
|
1390
|
+
pos: s.pos
|
|
1391
|
+
});
|
|
1392
|
+
return resumed ? Resume.done(s, key, start, true, out) : out;
|
|
1393
|
+
};
|
|
1394
|
+
}
|
|
1395
|
+
};
|
|
1594
1396
|
const fail = (s) => {
|
|
1595
|
-
|
|
1596
|
-
|
|
1397
|
+
s.mode = BT;
|
|
1398
|
+
return Fail;
|
|
1597
1399
|
};
|
|
1598
1400
|
/** Fails, leaving the cursor at `pos`: how far a token parser read (see `partialSafe`). */
|
|
1599
1401
|
const failAt = (s, pos) => {
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1402
|
+
s.pos = pos;
|
|
1403
|
+
s.mode = BT;
|
|
1404
|
+
return Fail;
|
|
1603
1405
|
};
|
|
1604
1406
|
/** 1 where a small code is accepted (taking `negate` into account). */
|
|
1605
1407
|
const acceptTable = (set, negate) => {
|
|
1606
|
-
|
|
1607
|
-
|
|
1608
|
-
|
|
1609
|
-
return t;
|
|
1408
|
+
const t = new Uint8Array(TableSize);
|
|
1409
|
+
for (let c = 0; c < TableSize; c++) t[c] = set.table[c] === 1 !== negate ? 1 : 0;
|
|
1410
|
+
return t;
|
|
1610
1411
|
};
|
|
1611
1412
|
/** A literal matched against a string input, returning `value`. */
|
|
1612
1413
|
const stringLiteral = (str, value) => {
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1616
|
-
|
|
1617
|
-
|
|
1618
|
-
|
|
1619
|
-
|
|
1620
|
-
|
|
1621
|
-
|
|
1622
|
-
|
|
1623
|
-
|
|
1624
|
-
|
|
1625
|
-
|
|
1626
|
-
|
|
1627
|
-
|
|
1628
|
-
|
|
1629
|
-
|
|
1630
|
-
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1634
|
-
};
|
|
1414
|
+
const len = str.length;
|
|
1415
|
+
if (len === 0) return () => value;
|
|
1416
|
+
if (len === 1) {
|
|
1417
|
+
const c0 = str.charCodeAt(0);
|
|
1418
|
+
return (s) => {
|
|
1419
|
+
const pos = s.pos;
|
|
1420
|
+
if (pos < s.end && s.input.charCodeAt(pos) === c0) {
|
|
1421
|
+
s.pos = pos + 1;
|
|
1422
|
+
return value;
|
|
1423
|
+
}
|
|
1424
|
+
return fail(s);
|
|
1425
|
+
};
|
|
1426
|
+
}
|
|
1427
|
+
return (s) => {
|
|
1428
|
+
const pos = s.pos;
|
|
1429
|
+
if (s.end - pos >= len && s.input.startsWith(str, pos)) {
|
|
1430
|
+
s.pos = pos + len;
|
|
1431
|
+
return value;
|
|
1432
|
+
}
|
|
1433
|
+
return fail(s);
|
|
1434
|
+
};
|
|
1635
1435
|
};
|
|
1636
1436
|
/** A closure compiler for one input kind; it memoizes what it compiles. */
|
|
1637
1437
|
/** The object of `Object.fromEntries`, built an entry at a time (see `internal/entries.ts`). */
|
|
1638
|
-
|
|
1639
|
-
|
|
1640
|
-
|
|
1641
|
-
|
|
1642
|
-
|
|
1643
|
-
|
|
1644
|
-
|
|
1645
|
-
|
|
1646
|
-
|
|
1647
|
-
|
|
1648
|
-
|
|
1649
|
-
|
|
1650
|
-
|
|
1651
|
-
|
|
1652
|
-
|
|
1653
|
-
|
|
1654
|
-
|
|
1655
|
-
|
|
1656
|
-
|
|
1657
|
-
|
|
1658
|
-
firsts: compiler.firsts,
|
|
1659
|
-
fallbacks: compiler.fallbacks
|
|
1660
|
-
};
|
|
1438
|
+
var EntriesAcc = class {
|
|
1439
|
+
o = {};
|
|
1440
|
+
deferred = void 0;
|
|
1441
|
+
add(key, value) {
|
|
1442
|
+
if (this.deferred === void 0 && isPlainKey(key)) this.o[key] = value;
|
|
1443
|
+
else this.deferred = addEntry(this.o, this.deferred, key, value);
|
|
1444
|
+
}
|
|
1445
|
+
result() {
|
|
1446
|
+
if (this.deferred !== void 0) flushEntries(this.o, this.deferred);
|
|
1447
|
+
return this.o;
|
|
1448
|
+
}
|
|
1449
|
+
};
|
|
1450
|
+
const createClosureCompiler = (kind, resumable = false) => {
|
|
1451
|
+
const compiler = new Compiler(kind, resumable);
|
|
1452
|
+
return {
|
|
1453
|
+
compile: (p, used) => compiler.compile(p, used),
|
|
1454
|
+
note: (p, node, reason) => compiler.note(p, node, reason),
|
|
1455
|
+
firsts: compiler.firsts,
|
|
1456
|
+
fallbacks: compiler.fallbacks
|
|
1457
|
+
};
|
|
1661
1458
|
};
|
|
1662
1459
|
/** Compiles a root parser for one input kind. */
|
|
1663
|
-
|
|
1664
|
-
|
|
1665
|
-
|
|
1666
|
-
|
|
1460
|
+
const compileClosure = (root, kind, resumable = false) => {
|
|
1461
|
+
const compiler = new Compiler(kind, resumable);
|
|
1462
|
+
return {
|
|
1463
|
+
step: compiler.compile(root, true),
|
|
1464
|
+
fallbacks: compiler.fallbacks
|
|
1465
|
+
};
|
|
1667
1466
|
};
|
|
1467
|
+
/** The closure compiler as a backend. */
|
|
1468
|
+
const backend = { build: (p, kind, resumable) => {
|
|
1469
|
+
try {
|
|
1470
|
+
const { fallbacks, step } = compileClosure(p, kind, resumable);
|
|
1471
|
+
return {
|
|
1472
|
+
backend: "closure",
|
|
1473
|
+
step,
|
|
1474
|
+
fallbacks,
|
|
1475
|
+
error: void 0
|
|
1476
|
+
};
|
|
1477
|
+
} catch (error) {
|
|
1478
|
+
return {
|
|
1479
|
+
backend: void 0,
|
|
1480
|
+
step: void 0,
|
|
1481
|
+
fallbacks: [],
|
|
1482
|
+
error
|
|
1483
|
+
};
|
|
1484
|
+
}
|
|
1485
|
+
} };
|
|
1486
|
+
//#endregion
|
|
1487
|
+
export { backend, createClosureCompiler };
|
|
1488
|
+
|
|
1668
1489
|
//# sourceMappingURL=closure.js.map
|