@thomasfosterau/effect-parse 0.4.0 → 0.5.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 +40 -14
- package/dist/Ascii.d.ts +11 -4
- package/dist/Ascii.d.ts.map +1 -1
- package/dist/Ascii.js +7 -1
- package/dist/Ascii.js.map +1 -1
- package/dist/Binary.d.ts.map +1 -1
- package/dist/Binary.js +64 -35
- package/dist/Binary.js.map +1 -1
- package/dist/Bits.d.ts.map +1 -1
- package/dist/Bits.js +7 -4
- package/dist/Bits.js.map +1 -1
- package/dist/Compiler.d.ts +74 -2
- package/dist/Compiler.d.ts.map +1 -1
- package/dist/Compiler.js +78 -1
- package/dist/Compiler.js.map +1 -1
- package/dist/Expression.d.ts.map +1 -1
- package/dist/Expression.js +86 -74
- package/dist/Expression.js.map +1 -1
- package/dist/Input.d.ts +37 -4
- package/dist/Input.d.ts.map +1 -1
- package/dist/Input.js +42 -0
- package/dist/Input.js.map +1 -1
- package/dist/Nom.d.ts +5 -15
- package/dist/Nom.d.ts.map +1 -1
- package/dist/Nom.js.map +1 -1
- package/dist/ParseError.d.ts +2 -2
- package/dist/ParseError.d.ts.map +1 -1
- package/dist/ParseError.js +1 -1
- package/dist/ParseError.js.map +1 -1
- package/dist/Parser.d.ts +370 -28
- package/dist/Parser.d.ts.map +1 -1
- package/dist/Parser.js +390 -19
- package/dist/Parser.js.map +1 -1
- package/dist/ParserSchema.js +2 -2
- package/dist/ParserSchema.js.map +1 -1
- package/dist/ParserStream.d.ts +3 -2
- package/dist/ParserStream.d.ts.map +1 -1
- package/dist/ParserStream.js +15 -10
- package/dist/ParserStream.js.map +1 -1
- package/dist/Syntax.d.ts +23 -5
- package/dist/Syntax.d.ts.map +1 -1
- package/dist/Syntax.js.map +1 -1
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +9 -9
- package/dist/Text.js.map +1 -1
- package/dist/Token.d.ts +55 -10
- package/dist/Token.d.ts.map +1 -1
- package/dist/Token.js +59 -21
- package/dist/Token.js.map +1 -1
- package/dist/internal/compiler/closure.d.ts.map +1 -1
- package/dist/internal/compiler/closure.js +257 -97
- package/dist/internal/compiler/closure.js.map +1 -1
- package/dist/internal/compiler/entries.d.ts +3 -1
- package/dist/internal/compiler/entries.d.ts.map +1 -1
- package/dist/internal/compiler/entries.js +3 -2
- package/dist/internal/compiler/entries.js.map +1 -1
- package/dist/internal/compiler/first.d.ts.map +1 -1
- package/dist/internal/compiler/first.js +7 -1
- package/dist/internal/compiler/first.js.map +1 -1
- package/dist/internal/compiler/graph.d.ts +17 -0
- package/dist/internal/compiler/graph.d.ts.map +1 -0
- package/dist/internal/compiler/graph.js +56 -0
- package/dist/internal/compiler/graph.js.map +1 -0
- package/dist/internal/compiler/jit.d.ts +5 -0
- package/dist/internal/compiler/jit.d.ts.map +1 -1
- package/dist/internal/compiler/jit.js +185 -96
- package/dist/internal/compiler/jit.js.map +1 -1
- package/dist/internal/compiler/registry.d.ts +10 -2
- package/dist/internal/compiler/registry.d.ts.map +1 -1
- package/dist/internal/compiler/registry.js.map +1 -1
- package/dist/internal/compiler/search.d.ts +32 -0
- package/dist/internal/compiler/search.d.ts.map +1 -0
- package/dist/internal/compiler/search.js +68 -0
- package/dist/internal/compiler/search.js.map +1 -0
- package/dist/internal/compiler/sets.d.ts.map +1 -1
- package/dist/internal/compiler/sets.js +1 -1
- package/dist/internal/compiler/sets.js.map +1 -1
- package/dist/internal/compiler/wasm.d.ts +8 -0
- package/dist/internal/compiler/wasm.d.ts.map +1 -0
- package/dist/internal/compiler/wasm.js +2769 -0
- package/dist/internal/compiler/wasm.js.map +1 -0
- package/dist/internal/compiler/wasmAsm.d.ts +199 -0
- package/dist/internal/compiler/wasmAsm.d.ts.map +1 -0
- package/dist/internal/compiler/wasmAsm.js +427 -0
- package/dist/internal/compiler/wasmAsm.js.map +1 -0
- package/dist/internal/entries.d.ts +3 -1
- package/dist/internal/entries.d.ts.map +1 -1
- package/dist/internal/entries.js +1 -2
- package/dist/internal/entries.js.map +1 -1
- package/dist/internal/node.d.ts +58 -2
- package/dist/internal/node.d.ts.map +1 -1
- package/dist/internal/node.js +13 -3
- package/dist/internal/node.js.map +1 -1
- package/dist/internal/numbers.d.ts.map +1 -1
- package/dist/internal/numbers.js.map +1 -1
- package/dist/internal/run.d.ts +12 -0
- package/dist/internal/run.d.ts.map +1 -1
- package/dist/internal/run.js +51 -11
- package/dist/internal/run.js.map +1 -1
- package/dist/internal/stack.d.ts +6 -0
- package/dist/internal/stack.d.ts.map +1 -1
- package/dist/internal/stack.js +8 -2
- package/dist/internal/stack.js.map +1 -1
- package/dist/internal/syntax.d.ts +4 -4
- package/dist/internal/syntax.d.ts.map +1 -1
- package/dist/internal/syntax.js +18 -10
- package/dist/internal/syntax.js.map +1 -1
- package/dist/internal/text.d.ts +32 -0
- package/dist/internal/text.d.ts.map +1 -1
- package/dist/internal/text.js +68 -14
- package/dist/internal/text.js.map +1 -1
- package/dist/internal/tokens.d.ts +13 -0
- package/dist/internal/tokens.d.ts.map +1 -1
- package/dist/internal/tokens.js +59 -39
- package/dist/internal/tokens.js.map +1 -1
- package/package.json +5 -4
- package/src/Ascii.ts +42 -9
- package/src/Binary.ts +151 -71
- package/src/Bits.ts +12 -6
- package/src/Compiler.ts +101 -10
- package/src/Expression.ts +94 -81
- package/src/Input.ts +65 -6
- package/src/Nom.ts +15 -5
- package/src/ParseError.ts +9 -4
- package/src/Parser.ts +682 -129
- package/src/ParserSchema.ts +1 -1
- package/src/ParserStream.ts +40 -19
- package/src/Syntax.ts +60 -36
- package/src/Text.ts +16 -15
- package/src/Token.ts +138 -29
- package/src/internal/compiler/closure.ts +362 -135
- package/src/internal/compiler/entries.ts +4 -2
- package/src/internal/compiler/first.ts +10 -0
- package/src/internal/compiler/graph.ts +68 -0
- package/src/internal/compiler/jit.ts +284 -154
- package/src/internal/compiler/registry.ts +10 -2
- package/src/internal/compiler/search.ts +68 -0
- package/src/internal/compiler/sets.ts +7 -3
- package/src/internal/compiler/wasm.ts +3203 -0
- package/src/internal/compiler/wasmAsm.ts +484 -0
- package/src/internal/entries.ts +10 -4
- package/src/internal/node.ts +54 -4
- package/src/internal/numbers.ts +3 -1
- package/src/internal/run.ts +71 -12
- package/src/internal/stack.ts +8 -1
- package/src/internal/syntax.ts +39 -18
- package/src/internal/text.ts +97 -9
- package/src/internal/tokens.ts +96 -14
package/README.md
CHANGED
|
@@ -169,6 +169,20 @@ const upper = Parser.make<string>((state) => {
|
|
|
169
169
|
})
|
|
170
170
|
```
|
|
171
171
|
|
|
172
|
+
A combinator that runs other parsers should be written with `Parser.combinator` instead, which hands it their steps. A `make` parser is opaque to the [compilers](#compiled-parsers), so in a compiled grammar it runs interpreted, and so does every parser it calls; a `combinator` receives their compiled steps there, so the parsers inside compile with the grammar:
|
|
173
|
+
|
|
174
|
+
```ts
|
|
175
|
+
const twice = <A>(p: Parser.Parser<A>) =>
|
|
176
|
+
Parser.combinator([p], ([step]) => (state) => {
|
|
177
|
+
const a = step(state)
|
|
178
|
+
if (a === Input.Fail) return Input.Fail
|
|
179
|
+
const b = step(state)
|
|
180
|
+
return b === Input.Fail ? Input.Fail : ([a, b] as const)
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
Parser.parseSync(twice(Token.oneOf("ab")), "ba") // ["b", "a"]
|
|
184
|
+
```
|
|
185
|
+
|
|
172
186
|
A driver that makes many small runs over one buffer, such as a tokenizer reading one token at a time, can run a parser's `step` on an `Input.State` itself and reuse that state between runs. `state.reset(offset, { partial, context })` resets every per-run field, so afterwards the state behaves exactly like `new Input.State(state.input, { offset, partial, context })`, even after a run that failed or returned `Incomplete`:
|
|
173
187
|
|
|
174
188
|
```ts
|
|
@@ -187,14 +201,14 @@ const token = (offset: number) => {
|
|
|
187
201
|
|
|
188
202
|
| Category | Combinators |
|
|
189
203
|
| --- | --- |
|
|
190
|
-
| Constructors | `succeed`, `sync`, `empty`, `fail`, `failMessage`, `reject`, `todo`, `suspend`, `make`, `location`, `eof` |
|
|
191
|
-
| Mapping | `map`, `as`, `asVoid`, `asSome`, `tap`, `tryMap`, `tryMapUnknown`, `filterMap`, `filter`, `filterOrFail`, `mapParser`, `fromEntries` |
|
|
192
|
-
| Sequencing | `seq`, `struct`, `zip`, `zipWith`, `zipLeft`, `zipRight`, `preceded`, `terminated`, `delimited`, `separatedPair`, `flatMap`, `gen`, `Do`/`bind`/`bindTo`/`let` |
|
|
204
|
+
| Constructors | `succeed`, `sync`, `empty`, `fail`, `failAt`, `failMessage`, `reject`, `todo`, `suspend`, `make`, `location`, `eof` |
|
|
205
|
+
| Mapping | `map`, `as`, `asVoid`, `asSome`, `tap`, `tryMap`, `tryMapUnknown`, `mapOrFail`, `filterMap`, `filter`, `filterOrFail`, `mapParser`, `fromEntries` |
|
|
206
|
+
| Sequencing | `seq`, `seqWith`, `struct`, `zip`, `zipWith`, `zipLeft`, `zipRight`, `preceded`, `terminated`, `delimited`, `separatedPair`, `flatMap`, `gen`, `Do`/`bind`/`bindTo`/`let` |
|
|
193
207
|
| Choice | `alt`, `orElse`, `orElseSucceed`, `dispatch`, `permutation` |
|
|
194
208
|
| Repetition | `repeat`, `repeatTill`, `separated`, `separatedFoldl1`, `separatedFoldr1`, `fold` |
|
|
195
|
-
| Control | `opt`, `optional`, `cond`, `peek`, `not`, `allConsuming` |
|
|
196
|
-
| Consumed input | `taken`, `withTaken`, `span`, `withSpan` |
|
|
197
|
-
| Errors | `cut`, `backtrack`, `complete`, `context`, `expected`, `mapError`, `orFail` |
|
|
209
|
+
| Control | `opt`, `optional`, `cond`, `peek`, `not`, `allConsuming`, `withFastPath` |
|
|
210
|
+
| Consumed input | `taken`, `withTaken`, `span`, `withSpan`, `mapSpan` |
|
|
211
|
+
| Errors | `cut`, `backtrack`, `complete`, `context`, `expected`, `quiet`, `mapError`, `orFail` |
|
|
198
212
|
| Services | `service`, `provideService`, `provide` |
|
|
199
213
|
| Recovery | `resumeAfter`, `retryAfter` |
|
|
200
214
|
| Debugging | `trace`, `named` |
|
|
@@ -250,18 +264,19 @@ export const json = Compiler.jit(Parser.preceded(ws, value))
|
|
|
250
264
|
Parser.parse(json, text) // same Result as before, about 5 times faster
|
|
251
265
|
```
|
|
252
266
|
|
|
253
|
-
On the JSON example, compiled parsing runs at 5–6 times the speed of the interpreter, for strings, UTF-8 and UTF-16 alike, and at 1–4 times the time of `JSON.parse` depending on the document (see [the design notes](docs/DESIGN.md#results)).
|
|
267
|
+
On the JSON example, compiled parsing runs at 5–6 times the speed of the interpreter, for strings, UTF-8 and UTF-16 alike, and at 1–4 times the time of `JSON.parse` depending on the document (see [the design notes](docs/DESIGN.md#results)). That is 3–5 times as fast as the same JSON grammar in Chevrotain, the fastest of the other JavaScript parser libraries measured, and about 8 and 20 times as fast as in Peggy and Parsimmon ([the comparison](docs/DESIGN.md#against-other-javascript-parser-libraries)).
|
|
254
268
|
|
|
255
|
-
There are
|
|
269
|
+
There are three compilers, one function each:
|
|
256
270
|
|
|
271
|
+
- `Compiler.wasm` scans the input in WebAssembly and builds the value with generated JavaScript. It is the fastest on strings and bytes: on the JSON benchmark it takes 17–36% less time than the JIT. It uses the JIT for array input, for streaming, and where `WebAssembly` or `new Function` is unavailable.
|
|
257
272
|
- `Compiler.jit` generates JavaScript where `new Function` is allowed, and falls back to the closure compiler where it isn't.
|
|
258
273
|
- `Compiler.closure` never generates code. It is meant for pages with a Content Security Policy.
|
|
259
274
|
|
|
260
|
-
A bundle contains only the compilers it calls. A program that compiles nothing bundles
|
|
275
|
+
A bundle contains only the compilers it calls. A program that compiles nothing bundles none, and one that uses only `Compiler.closure` contains no `new Function`. esbuild keeps every member of a namespace imported from the root entry, so with esbuild import from the subpaths instead (`import * as Compiler from "@thomasfosterau/effect-parse/Compiler"`). Rolldown and Rollup drop unused members either way.
|
|
261
276
|
|
|
262
|
-
|
|
277
|
+
Each returns the same parser. The code is built on the first run, for each kind of input, and cached. `parse`, `parseSync`, `parsePeek`, `parsePartial`, `parseEffect`, `ParserStream.parseMany` and `ParserStream.parseAll` use it, from any `offset`; `parseRecoverable` and tracing stay interpreted. A driver that keeps its own `Input.State` (a lexer reading one token at a time, say) runs the compiled code with `Parser.run(p, state)`; calling `p.step(state)` directly always runs interpreted.
|
|
263
278
|
|
|
264
|
-
Compiled code records no diagnostics. When it fails, the parser runs again interpreted, so errors are exactly the same, byte for byte. Callbacks should therefore be pure: on failing input they may run twice, and those whose results are unused may be skipped. Parsers built with `Parser.make` or `Parser.gen` run interpreted inside compiled code. See the [design notes](docs/DESIGN.md#6-compiled-parsers) for details.
|
|
279
|
+
Compiled code records no diagnostics. When it fails, the parser runs again interpreted, so errors are exactly the same, byte for byte. Callbacks should therefore be pure: on failing input they may run twice, and those whose results are unused may be skipped. Parsers built with `Parser.make` or `Parser.gen` run interpreted inside compiled code; the parsers given to a `Parser.combinator` compile. See the [design notes](docs/DESIGN.md#6-compiled-parsers) for details.
|
|
265
280
|
|
|
266
281
|
Some ways of writing a grammar compile to faster code than others:
|
|
267
282
|
|
|
@@ -280,8 +295,11 @@ Some ways of writing a grammar compile to faster code than others:
|
|
|
280
295
|
- Give `Parser.dispatch` a record of cases, not a function. With a record, compiled code switches on the key and runs the chosen case compiled, and on `Token.any` or `Parser.peek(Token.any)` it switches on the next unit without building the key. The parser a function returns runs interpreted, and if the function builds it (`(c) => Parser.cut(p)`), it is built again on every run.
|
|
281
296
|
- Build parsers once, outside callbacks and `Parser.gen` bodies. A parser built while parsing costs an allocation per combinator, every time, and is never compiled.
|
|
282
297
|
- On token arrays (the output of a lexer), match tokens with `Token.oneOf`, `Token.noneOf`, `Token.takeWhile` and `Token.anyToken`, with a predicate or a list of tokens: the JIT calls the predicate inline. A `Parser.make` that reads `state.input[state.pos]` runs interpreted, and records a failure, with its expected list, every time it doesn't match.
|
|
283
|
-
- Drive a lexer with `Parser.run` on one `Input.State` that you `reset` before each token: no run allocates a state, a `Result` or a slice, and the token parser runs compiled. When a failure only means "try something else", pass `{ diagnostics: false }`, so a failed compiled run isn't repeated interpreted to build an error you won't read.
|
|
298
|
+
- Drive a lexer with `Parser.run` on one `Input.State` that you `reset` before each token: no run allocates a state, a `Result` or a slice, and the token parser runs compiled. When a failure only means "try something else", pass `{ diagnostics: false }`, so a failed compiled run isn't repeated interpreted to build an error you won't read. `Compiler.runner(p)` prepares the parser once and returns a function of the state that does what `Parser.run` does, without looking the compiled code up on every run: about 20% faster per token.
|
|
284
299
|
- `Ascii.float` and `Text.float` convert numbers as they match them.
|
|
300
|
+
- Where a scanner written by hand beats compiled code on the tokens that dominate an input, put it in front of the grammar with `Parser.withFastPath(grammar, fast)`: compiled code runs `fast` first, and it returns `Parser.Defer` for anything it doesn't handle. The grammar stays the specification: interpreted runs, and so every error, use it alone, and `Parser.verifyFastPath(grammar, fast, inputs)` lists where the two disagree, for a test to assert that there are none.
|
|
301
|
+
- Build a string from parts with `Parser.fold(part, { init: () => "", f: (acc, x) => acc + x })` rather than `Parser.map(Parser.repeat(part), (parts) => parts.join(""))`: no array per string. A CSV grammar's all-quoted fields parse 40% faster that way.
|
|
302
|
+
- Write the end of a run as `Token.takeTill` of one to three characters (`Token.takeTill('"')`, `Token.takeTill(",\r\n")`), not as a predicate: on string input, compiled code finds the end with `indexOf`, up to 9 times as fast on long runs.
|
|
285
303
|
|
|
286
304
|
[`json-fast.ts`](examples/json-fast.ts) is the JSON example written this way; it runs records-heavy documents about 1.5 times as fast as [`json-text.ts`](examples/json-text.ts).
|
|
287
305
|
|
|
@@ -309,11 +327,14 @@ effect-parse tries hard to produce good messages without annotations:
|
|
|
309
327
|
- Optional and repeated parsers leave _hints_, so after `repeat(digit)` a failure reads `expected digit or ")"`.
|
|
310
328
|
- The furthest failure wins (the PEG heuristic), so a value that fails after a separator is reported where it failed.
|
|
311
329
|
- `Parser.expected("value")` names a rule; `Parser.context("array")` adds "while parsing array".
|
|
330
|
+
- `Parser.quiet(skip)` keeps a whitespace or comment skipper out of later errors: without it, every error after the skipper also expects whitespace.
|
|
312
331
|
|
|
313
332
|
User errors are typed. `Parser.fail(e)`, `Parser.tryMap(f)` and `Parser.filterOrFail(pred, f)` add their error type to `E`, and the error arrives as `ParseError.cause`:
|
|
314
333
|
|
|
315
334
|
```ts
|
|
316
|
-
class OutOfRange extends Schema.TaggedError<OutOfRange
|
|
335
|
+
class OutOfRange extends Schema.TaggedError<OutOfRange, { readonly brand: unique symbol }>()("OutOfRange", {
|
|
336
|
+
value: Schema.Finite
|
|
337
|
+
}) {}
|
|
317
338
|
|
|
318
339
|
const port = Ascii.decUint.pipe(
|
|
319
340
|
Parser.filterOrFail(
|
|
@@ -324,6 +345,8 @@ const port = Ascii.decUint.pipe(
|
|
|
324
345
|
// Parser<number, string | OutOfRange>
|
|
325
346
|
```
|
|
326
347
|
|
|
348
|
+
These checks fail where the checked parser started. A check that can only run once a whole construct has parsed, but should point at the part at fault, returns `Parser.failAt(offset, error)` from a `flatMap`, with an offset from `Parser.location` or `Parser.withSpan`: a mismatched end tag is reported at the end tag, not at the start of the element.
|
|
349
|
+
|
|
327
350
|
## Streaming and partial input
|
|
328
351
|
|
|
329
352
|
nom has separate `streaming` and `complete` modules. winnow marks the input as `Partial`. In effect-parse, partiality is decided by the runner, so every parser works in both modes:
|
|
@@ -513,7 +536,7 @@ On a 0.58 M-character JSON document (`vp run bench:syntax`), building the tree t
|
|
|
513
536
|
| Module | Contents | winnow / nom equivalent |
|
|
514
537
|
| --- | --- | --- |
|
|
515
538
|
| `Parser` | the type, combinators, runners | `Parser` trait, `combinator` / `combinator`, `branch`, `multi`, `sequence` |
|
|
516
|
-
| `Token` | `any`, `literal`, `oneOf`, `noneOf`, `take`, `takeWhile`, `takeTill`, `takeUntil`, `rest`, `range`, `codePoints` | `token` / `bytes` |
|
|
539
|
+
| `Token` | `any`, `literal`, `oneOf`, `noneOf`, `take`, `takeWhile`, `takeTill`, `takeUntil`, `takeUntilAny`, `rest`, `range`, `codePoints` | `token` / `bytes` |
|
|
517
540
|
| `Text` | Unicode parsers for any encoding: `string`, `utf8`, `utf16le`, `utf16be`, `latin1`, `make`; Unicode predicates | — |
|
|
518
541
|
| `Encoding` | text encodings for `Text`, `sniff` | — |
|
|
519
542
|
| `Ascii` | character classes, `lineEnding`, `decInt`, `decUint`, `hexUint`, `float`, `caseless`, `takeEscaped`, `escaped`, `Ascii.bytes.*` | `ascii` / `character`, `number` |
|
|
@@ -584,10 +607,13 @@ The `Nom` module provides the nom names directly, with nom's argument orders, fo
|
|
|
584
607
|
- [`ini.ts`](examples/ini.ts): INI files with a generator-style parser
|
|
585
608
|
- [`http.ts`](examples/http.ts): HTTP requests over bytes, complete and streamed
|
|
586
609
|
- [`sexpr.ts`](examples/sexpr.ts): s-expressions with typed evaluation errors
|
|
610
|
+
- [`jq.ts`](examples/jq.ts): a subset of [jq](https://jqlang.org): its filter language with the Pratt parser and its JSON input with the compiled `json-fast.ts` grammar, behind an `effect/cli` command in [`jq-cli.ts`](examples/jq-cli.ts); `vp run bench:jq` races it against the real `jq`
|
|
587
611
|
- [`json-syntax.ts`](examples/json-syntax.ts): JSON as a syntax tree for editors, with fields, `ERROR` nodes for malformed elements, and incremental reparsing (tested in [`test/Syntax.test.ts`](test/Syntax.test.ts))
|
|
588
612
|
|
|
589
613
|
`vp run bench` (from a clone) compares the JSON examples, on each encoding, with `JSON.parse`.
|
|
590
614
|
|
|
615
|
+
[`examples/ripgrep`](examples/ripgrep) is a larger example, a package of its own: `erg`, a search tool with ripgrep's command line whose regex engine is built from these combinators (Rust's regex syntax parsed with effect-parse, and compiled to a backtracking matcher over UTF-8 bytes, behind ripgrep-style literal prefilters and a lazy DFA), with a harness that benchmarks it against ripgrep, grep and other search tools, and tests that compare its output with ripgrep's.
|
|
616
|
+
|
|
591
617
|
## Design
|
|
592
618
|
|
|
593
619
|
[`docs/DESIGN.md`](docs/DESIGN.md) records the research into nom, winnow and Effect, and the reasoning behind each decision in this port.
|
package/dist/Ascii.d.ts
CHANGED
|
@@ -254,6 +254,16 @@ export declare const toFloat: (s: Input) => number;
|
|
|
254
254
|
* @category numbers
|
|
255
255
|
*/
|
|
256
256
|
export declare const float: Parser<number>;
|
|
257
|
+
/**
|
|
258
|
+
* The overloads of {@link caseless}.
|
|
259
|
+
*
|
|
260
|
+
* @since 0.5.0
|
|
261
|
+
* @category literals
|
|
262
|
+
*/
|
|
263
|
+
export interface CaselessFn {
|
|
264
|
+
(literal: string): Parser<string>;
|
|
265
|
+
(literal: Uint8Array): Parser<Uint8Array, never, never, Uint8Array>;
|
|
266
|
+
}
|
|
257
267
|
/**
|
|
258
268
|
* Matches a literal ignoring ASCII case (winnow's `Caseless`, nom's
|
|
259
269
|
* `tag_no_case`). Returns the input as written.
|
|
@@ -261,10 +271,7 @@ export declare const float: Parser<number>;
|
|
|
261
271
|
* @since 0.1.0
|
|
262
272
|
* @category literals
|
|
263
273
|
*/
|
|
264
|
-
export declare const caseless:
|
|
265
|
-
(literal: string): Parser<string>;
|
|
266
|
-
(literal: Uint8Array): Parser<Uint8Array, never, never, Uint8Array>;
|
|
267
|
-
};
|
|
274
|
+
export declare const caseless: CaselessFn;
|
|
268
275
|
/**
|
|
269
276
|
* Recognizes a string containing escape sequences, returning the raw slice
|
|
270
277
|
* (winnow's `take_escaped`, nom's `escaped`).
|
package/dist/Ascii.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ascii.d.ts","names":[],"sources":["../src/Ascii.ts"],"mappings":";;;;;;;;;;qBAkDa,UAAU;;;;;;;qBAkBV,UAAU;;;;;;;qBAeV,aAAa;;;;;;;qBAmBb,aAAa;;;;;;;qBAeb,iBAAiB;;;;;;;qBAgBjB,UAAU;;;;;;;qBAkBV,eAAe;;;;;;;qBAmBf,YAAY;;;;;;;
|
|
1
|
+
{"version":3,"file":"Ascii.d.ts","names":[],"sources":["../src/Ascii.ts"],"mappings":";;;;;;;;;;qBAkDa,UAAU;;;;;;;qBAkBV,UAAU;;;;;;;qBAeV,aAAa;;;;;;;qBAmBb,aAAa;;;;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
563
|
+
* Gives a value the type of another view of the same parsers (see
|
|
564
|
+
* {@link bytes}).
|
|
565
|
+
*/
|
|
566
|
+
const retype = (value) => value;
|
|
567
|
+
/**
|
|
563
568
|
* The parsers of this module, typed for `Uint8Array` input. Slices are
|
|
564
569
|
* returned as `Uint8Array`s.
|
|
565
570
|
*
|
|
@@ -582,7 +587,7 @@ const bytes = /*#__PURE__*/ (() => {
|
|
|
582
587
|
const lit = (s) => Token$1.literal(new TextEncoder().encode(s));
|
|
583
588
|
const crlfB = lit("\r\n");
|
|
584
589
|
const newlineB = lit("\n");
|
|
585
|
-
|
|
590
|
+
const views = {
|
|
586
591
|
alpha0,
|
|
587
592
|
alpha1,
|
|
588
593
|
digit0,
|
|
@@ -609,6 +614,7 @@ const bytes = /*#__PURE__*/ (() => {
|
|
|
609
614
|
hexUint,
|
|
610
615
|
float
|
|
611
616
|
};
|
|
617
|
+
return retype(views);
|
|
612
618
|
})();
|
|
613
619
|
export { alpha0, alpha1, alphanumeric0, alphanumeric1, bytes, caseless, crlf, decBigInt, decBigUint, decInt, decUint, digit0, digit1, escaped, escapedImpl, float, hexDigit0, hexDigit1, hexUint, isAlpha, isAlphanumeric, isDigit, isHexDigit, isMultispace, isNewline, isOctDigit, isSpace, lineEnding, multispace0, multispace1, newline, octDigit0, octDigit1, overflow, space0, space1, tab, takeEscaped, takeEscapedImpl, tillLineEnding, toFloat };
|
|
614
620
|
|
package/dist/Ascii.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Ascii.js","names":["floatText","Token"],"sources":["../src/Ascii.ts"],"sourcesContent":["/**\n * Parsers for ASCII text: character classes, line endings, numbers and\n * escaped strings (winnow's `ascii` module, nom's `character` module).\n *\n * The parsers are typed for `string` input. Each also works on\n * `Uint8Array` input (treating bytes as ASCII), and `Ascii.bytes` gives the\n * same parsers typed for bytes:\n *\n * ```ts\n * import { Ascii, Parser } from \"@thomasfosterau/effect-parse\"\n *\n * Parser.parse(Ascii.decUint, \"42\") // 42\n * Parser.parse(Ascii.bytes.decUint, new TextEncoder().encode(\"42\")) // 42\n * ```\n *\n * Partial (streaming) input is supported: for example `digit1` on the\n * partial input `\"123\"` reports `Incomplete`, since more digits may follow.\n *\n * For Unicode character classes, or for text in other encodings (UTF-8,\n * UTF-16), use the `Text` module.\n *\n * @since 0.1.0\n */\nimport * as Predicate from \"effect/Predicate\"\nimport * as Result from \"effect/Result\"\nimport type { Encoding } from \"./Encoding.js\"\nimport type { Input, Slice, State } from \"./Input.js\"\nimport { Fail } from \"./Input.js\"\nimport type { ErrorKind } from \"./ParseError.js\"\nimport type { Parser } from \"./Parser.js\"\nimport * as P from \"./Parser.js\"\nimport * as numbers from \"./internal/numbers.js\"\nimport { Key } from \"./internal/key.js\"\nimport * as Resume from \"./internal/resume.js\"\nimport { codePointPredicate } from \"./internal/tokens.js\"\nimport { literalImpl, range, takeWhileImpl } from \"./Token.js\"\nimport * as Token from \"./Token.js\"\n\n// -----------------------------------------------------------------------------\n// Character predicates\n// -----------------------------------------------------------------------------\n\nconst code = (c: string | number): number => (Predicate.isNumber(c) ? c : c.charCodeAt(0))\n\n/**\n * `[a-zA-Z]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isAlpha: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isAlpha\",\n (c: string | number): boolean => {\n const x = code(c)\n return (x >= 65 && x <= 90) || (x >= 97 && x <= 122)\n },\n [\n [65, 90],\n [97, 122]\n ]\n)\n\n/**\n * `[0-9]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return x >= 48 && x <= 57\n },\n [[48, 57]]\n)\n\n/**\n * `[0-9a-fA-F]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isHexDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isHexDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return (x >= 48 && x <= 57) || (x >= 65 && x <= 70) || (x >= 97 && x <= 102)\n },\n [\n [48, 57],\n [65, 70],\n [97, 102]\n ]\n)\n\n/**\n * `[0-7]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isOctDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isOctDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return x >= 48 && x <= 55\n },\n [[48, 55]]\n)\n\n/**\n * `[0-9a-zA-Z]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isAlphanumeric: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isAlphanumeric\",\n (c: string | number): boolean => isAlpha(c) || isDigit(c),\n [\n [48, 57],\n [65, 90],\n [97, 122]\n ]\n)\n\n/**\n * `[ \\t]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isSpace: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isSpace\",\n (c: string | number): boolean => {\n const x = code(c)\n return x === 32 || x === 9\n },\n [\n [9, 9],\n [32, 32]\n ]\n)\n\n/**\n * `[ \\t\\r\\n]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isMultispace: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isMultispace\",\n (c: string | number): boolean => {\n const x = code(c)\n return x === 32 || x === 9 || x === 10 || x === 13\n },\n [\n [9, 10],\n [13, 13],\n [32, 32]\n ]\n)\n\n/**\n * `\\n`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isNewline: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isNewline\",\n (c: string | number): boolean => code(c) === 10,\n [[10, 10]]\n)\n\n// Token sets usable for both string and byte input\nconst alphaSet = [/*#__PURE__*/ range(\"a\", \"z\"), /*#__PURE__*/ range(\"A\", \"Z\")]\nconst digitSet = /*#__PURE__*/ range(\"0\", \"9\")\nconst hexSet = [/*#__PURE__*/ range(\"0\", \"9\"), /*#__PURE__*/ range(\"a\", \"f\"), /*#__PURE__*/ range(\"A\", \"F\")]\nconst octSet = /*#__PURE__*/ range(\"0\", \"7\")\nconst alnumSet = [/*#__PURE__*/ range(\"a\", \"z\"), /*#__PURE__*/ range(\"A\", \"Z\"), /*#__PURE__*/ range(\"0\", \"9\")]\nconst spaceSet = \" \\t\"\nconst multispaceSet = \" \\t\\r\\n\"\n\nconst cls = (set: unknown, min: 0 | 1, description: string, kind: ErrorKind, hint = true): Parser<string> =>\n takeWhileImpl<string>(set, { min }, false, `${kind.toLowerCase()}${min}`, description, kind, hint)\n\n// -----------------------------------------------------------------------------\n// Character classes\n// -----------------------------------------------------------------------------\n\n/**\n * Zero or more ASCII letters, `[a-zA-Z]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alpha0: Parser<string> = /*#__PURE__*/ cls(alphaSet, 0, \"letter\", \"Alpha\")\n\n/**\n * One or more ASCII letters, `[a-zA-Z]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alpha1: Parser<string> = /*#__PURE__*/ cls(alphaSet, 1, \"letter\", \"Alpha\")\n\n/**\n * Zero or more ASCII digits, `[0-9]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const digit0: Parser<string> = /*#__PURE__*/ cls(digitSet, 0, \"digit\", \"Digit\")\n\n/**\n * One or more ASCII digits, `[0-9]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const digit1: Parser<string> = /*#__PURE__*/ cls(digitSet, 1, \"digit\", \"Digit\")\n\n/**\n * Zero or more hexadecimal digits, `[0-9a-fA-F]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const hexDigit0: Parser<string> = /*#__PURE__*/ cls(hexSet, 0, \"hexadecimal digit\", \"HexDigit\")\n\n/**\n * One or more hexadecimal digits, `[0-9a-fA-F]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const hexDigit1: Parser<string> = /*#__PURE__*/ cls(hexSet, 1, \"hexadecimal digit\", \"HexDigit\")\n\n/**\n * Zero or more octal digits, `[0-7]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const octDigit0: Parser<string> = /*#__PURE__*/ cls(octSet, 0, \"octal digit\", \"OctDigit\")\n\n/**\n * One or more octal digits, `[0-7]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const octDigit1: Parser<string> = /*#__PURE__*/ cls(octSet, 1, \"octal digit\", \"OctDigit\")\n\n/**\n * Zero or more ASCII letters or digits, `[0-9a-zA-Z]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alphanumeric0: Parser<string> = /*#__PURE__*/ cls(alnumSet, 0, \"letter or digit\", \"AlphaNumeric\")\n\n/**\n * One or more ASCII letters or digits, `[0-9a-zA-Z]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alphanumeric1: Parser<string> = /*#__PURE__*/ cls(alnumSet, 1, \"letter or digit\", \"AlphaNumeric\")\n\n/**\n * Zero or more spaces and tabs, `[ \\t]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const space0: Parser<string> = /*#__PURE__*/ cls(spaceSet, 0, \"space\", \"Space\", false)\n\n/**\n * One or more spaces and tabs, `[ \\t]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const space1: Parser<string> = /*#__PURE__*/ cls(spaceSet, 1, \"space\", \"Space\", false)\n\n/**\n * Zero or more spaces, tabs, carriage returns and line feeds,\n * `[ \\t\\r\\n]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const multispace0: Parser<string> = /*#__PURE__*/ cls(multispaceSet, 0, \"whitespace\", \"MultiSpace\", false)\n\n/**\n * One or more spaces, tabs, carriage returns and line feeds,\n * `[ \\t\\r\\n]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const multispace1: Parser<string> = /*#__PURE__*/ cls(multispaceSet, 1, \"whitespace\", \"MultiSpace\", false)\n\n// -----------------------------------------------------------------------------\n// Line endings\n// -----------------------------------------------------------------------------\n\n/**\n * A carriage return followed by a line feed, `\\r\\n`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const crlf: Parser<\"\\r\\n\"> = /*#__PURE__*/ Token.literal(\"\\r\\n\")\n\n/**\n * A line feed, `\\n` (nom's `newline`).\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const newline: Parser<\"\\n\"> = /*#__PURE__*/ Token.literal(\"\\n\")\n\n/**\n * A tab, `\\t`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const tab: Parser<\"\\t\"> = /*#__PURE__*/ Token.literal(\"\\t\")\n\n/**\n * A line ending: `\\n` or `\\r\\n`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const lineEnding: Parser<\"\\n\" | \"\\r\\n\"> = /*#__PURE__*/ P.alt(newline, crlf).pipe(\n /*#__PURE__*/ P.expected(\"line ending\")\n)\n\nconst expectedLineEnding = [\"line ending\"]\n\n/**\n * Everything up to (not including) a line ending, `[^\\r\\n]*` (winnow's\n * `till_line_ending`, nom's `not_line_ending`). A carriage return that is\n * not followed by a line feed is an error.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const tillLineEnding: Parser<string> = /*#__PURE__*/ P.described<string>(\n (state: State<any>) => {\n const start = state.pos\n const input = state.input\n const isString = state.inputKind === 0\n let i = start\n while (i < state.end) {\n const c = isString ? (input as string).charCodeAt(i) : (input as Uint8Array)[i]\n if (c === 10) break\n if (c === 13) {\n // The line feed after it is read, not consumed\n state.syntax?.read(i + 2)\n if (i + 1 >= state.end) {\n if (state.partial) return state.incomplete(1, expectedLineEnding)\n return state.backtrack(\"LineEnding\", expectedLineEnding, i)\n }\n const d = isString ? (input as string).charCodeAt(i + 1) : (input as Uint8Array)[i + 1]\n if (d === 10) break\n return state.backtrack(\"LineEnding\", expectedLineEnding, i)\n }\n i++\n }\n if (i >= state.end && state.partial) return state.incomplete(1, expectedLineEnding)\n state.pos = i\n return state.slice(start, i) as string\n },\n \"tillLineEnding\",\n { _tag: \"TillLineEnding\", encoding: undefined }\n)\n\n// -----------------------------------------------------------------------------\n// Numbers\n// -----------------------------------------------------------------------------\n\n/** @internal */\nexport const overflow = (value: number): Result.Result<number, string> =>\n Number.isSafeInteger(value) ? Result.succeed(value) : Result.fail(\"integer overflow\")\n\n/**\n * An unsigned decimal integer: one or more digits (winnow's `dec_uint`,\n * nom's `u32`/`u64`). Fails if the value is not a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decUint: Parser<number, string> = /*#__PURE__*/ digit1.pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(Number(numbers.toText(s)))),\n /*#__PURE__*/ P.named(\"decUint\")\n)\n\n/**\n * A signed decimal integer: an optional `+` or `-` followed by one or more\n * digits (winnow's `dec_int`, nom's `i32`/`i64`). Fails if the value is not\n * a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decInt: Parser<number, string> = /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.seq(/*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\")), digit1)\n).pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(Number(numbers.toText(s)))),\n /*#__PURE__*/ P.named(\"decInt\")\n)\n\n/**\n * An unsigned decimal integer as a `bigint`, of any size.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decBigUint: Parser<bigint> = /*#__PURE__*/ digit1.pipe(\n /*#__PURE__*/ P.map((s) => BigInt(numbers.toText(s)))\n)\n\n/**\n * A signed decimal integer as a `bigint`, of any size.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decBigInt: Parser<bigint> = /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.seq(/*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\")), digit1)\n).pipe(\n /*#__PURE__*/ P.map((s) => {\n const t = numbers.toText(s)\n return BigInt(t.startsWith(\"+\") ? t.slice(1) : t)\n })\n)\n\n/**\n * A hexadecimal unsigned integer, without prefix (winnow's `hex_uint`, nom's\n * `hex_u32`). Fails if the value is not a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const hexUint: Parser<number, string> = /*#__PURE__*/ hexDigit1.pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(parseInt(numbers.toText(s), 16))),\n /*#__PURE__*/ P.named(\"hexUint\")\n)\n\nconst sign = /*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\"))\nconst exponent = /*#__PURE__*/ P.opt(/*#__PURE__*/ P.seq(/*#__PURE__*/ Token.oneOf(\"eE\"), sign, digit1))\nconst unsignedFloat = /*#__PURE__*/ P.alt(\n /*#__PURE__*/ P.seq(\n /*#__PURE__*/ literalImpl(\"inf\", true),\n /*#__PURE__*/ P.opt(/*#__PURE__*/ literalImpl(\"inity\", true))\n ),\n /*#__PURE__*/ P.seq(/*#__PURE__*/ Token.literal(\".\"), digit1, exponent),\n /*#__PURE__*/ P.seq(\n digit1,\n /*#__PURE__*/ P.opt(/*#__PURE__*/ P.seq(/*#__PURE__*/ Token.literal(\".\"), /*#__PURE__*/ P.opt(digit1))),\n exponent\n )\n)\nconst floatText = /*#__PURE__*/ numbers.floatText(\n /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.alt(/*#__PURE__*/ literalImpl(\"nan\", true), /*#__PURE__*/ P.seq(sign, unsignedFloat))\n ).pipe(/*#__PURE__*/ P.expected(\"number\")) as Parser<string>\n)\n\n/** @internal */\nexport const toFloat: (s: Input) => number = numbers.toFloat\n\n/**\n * A floating point number in decimal or scientific notation, including\n * `inf`, `infinity` and `nan` (case insensitive), as accepted by winnow's\n * `float` and nom's `double`.\n *\n * @example\n * ```ts\n * import { Ascii, Parser } from \"@thomasfosterau/effect-parse\"\n *\n * Parser.parse(Ascii.float, \"-1.5e3\") // Result.succeed(-1500)\n * ```\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const float: Parser<number> = /*#__PURE__*/ floatText.pipe(\n /*#__PURE__*/ P.map(toFloat),\n /*#__PURE__*/ P.named(\"float\")\n)\n\n// -----------------------------------------------------------------------------\n// Literals\n// -----------------------------------------------------------------------------\n\n/**\n * Matches a literal ignoring ASCII case (winnow's `Caseless`, nom's\n * `tag_no_case`). Returns the input as written.\n *\n * @since 0.1.0\n * @category literals\n */\nexport const caseless: {\n (literal: string): Parser<string>\n (literal: Uint8Array): Parser<Uint8Array, never, never, Uint8Array>\n} = (lit: string | Uint8Array): any => literalImpl(lit, true)\n\n// -----------------------------------------------------------------------------\n// Escapes\n// -----------------------------------------------------------------------------\n\nconst controlCode = (c: string | number): number => (Predicate.isNumber(c) ? c : c.charCodeAt(0))\n\nconst atControl = (state: State<Input>, control: number): boolean => {\n if (state.pos >= state.end) return false\n const input = state.input\n const c = state.inputKind === 0 ? (input as string).charCodeAt(state.pos) : (input as Uint8Array)[state.pos]\n return c === control\n}\n\n/**\n * Recognizes a string containing escape sequences, returning the raw slice\n * (winnow's `take_escaped`, nom's `escaped`).\n *\n * - `normal` matches runs of ordinary characters (it must consume input)\n * - `control` is the escape character, e.g. `\"\\\\\"`\n * - `escapable` matches what may follow the control character\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const raw = Ascii.takeEscaped(Ascii.alpha1, \"\\\\\", Token.oneOf(\"\\\"n\\\\\"))\n * Parser.parse(raw, \"ab\\\\\\\"cd\") // Result.succeed(\"ab\\\\\\\"cd\")\n * ```\n *\n * @since 0.1.0\n * @category escapes\n */\nexport const takeEscaped = <A, E, R, I extends Input, B, E2, R2, I2 extends Input>(\n normal: Parser<A, E, R, I>,\n control: string | number,\n escapable: Parser<B, E2, R2, I2>\n): Parser<Slice<I & I2>, E | E2, R | R2, I & I2> => {\n const ctrl = controlCode(control)\n return takeEscapedImpl(\n normal,\n (state) => {\n if (!atControl(state, ctrl)) return false\n state.pos++\n return true\n },\n escapable,\n (state, start) => state.slice(start, state.pos) as Slice<I & I2>,\n ctrl,\n undefined\n )\n}\n\nconst isIncomplete = (state: State<any>): boolean => state.mode === \"Incomplete\"\n\n/**\n * Fails with the current (`Incomplete`) failure, recording a checkpoint\n * first in a streaming parse.\n */\nconst suspendAt = (state: State<any>, key: Key, start: number, checkpoint: unknown): Fail => {\n if (state.resume !== undefined) Resume.save(state, key, start, checkpoint)\n return Fail\n}\n\n/** @internal */\nexport const takeEscapedImpl = <A, E, R, I extends Input, B, E2, R2, I2 extends Input, S>(\n normal: Parser<A, E, R, I>,\n matchControl: (state: State<any>) => boolean,\n escapable: Parser<B, E2, R2, I2>,\n slice: (state: State<any>, start: number) => S,\n control: number,\n encoding: Encoding<any> | undefined\n): Parser<S, E | E2, R | R2, I & I2> => {\n const normalStep = normal.step\n const escStep = escapable.step\n const key = new Key()\n return P.described(\n (state: State<any>) => {\n const start = state.pos\n let resumed = false\n if (state.resume !== undefined) {\n const checkpoint = Resume.take(state, key, start) as number | Resume.Done | undefined\n if (checkpoint instanceof Resume.Done) {\n state.pos = checkpoint.pos\n return checkpoint.value as S\n }\n if (checkpoint !== undefined) {\n state.pos = checkpoint\n resumed = true\n }\n }\n while (state.pos < state.end) {\n const before = state.pos\n const a = normalStep(state)\n if (a !== Fail) {\n if (state.pos === before)\n return state.failMessage(\"Cut\", \"Assert\", \"`takeEscaped` parsers must always consume\")\n continue\n }\n if (state.mode === \"Incomplete\") return suspendAt(state, key, start, before)\n if (state.mode !== \"Backtrack\") return Fail\n state.rewind(before)\n if (matchControl(state)) {\n if (escStep(state) === Fail) return isIncomplete(state) ? suspendAt(state, key, start, before) : Fail\n } else {\n return Resume.done(state, key, start, resumed, slice(state, start))\n }\n }\n if (state.partial) {\n state.incomplete(0)\n return suspendAt(state, key, start, state.pos)\n }\n return Resume.done(state, key, start, resumed, slice(state, start))\n },\n \"takeEscaped\",\n { _tag: \"Escaped\", normal, control, escape: escapable, raw: true, encoding }\n )\n}\n\n/**\n * Parses a string containing escape sequences, returning the *unescaped*\n * string (winnow's `escaped`, nom's `escaped_transform`).\n *\n * - `normal` matches runs of ordinary characters, returning them\n * - `control` is the escape character, e.g. `\"\\\\\"`\n * - `escape` parses what follows the control character, returning its\n * replacement\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const unescaped = Ascii.escaped(\n * Token.takeTill(\"\\\\\\\"\"),\n * \"\\\\\",\n * Parser.alt(\n * Token.literal(\"n\").pipe(Parser.as(\"\\n\")),\n * Token.literal(\"\\\\\"),\n * Token.literal(\"\\\"\")\n * )\n * )\n * Parser.parse(unescaped, \"a\\\\nb\") // Result.succeed(\"a\\nb\")\n * ```\n *\n * @since 0.1.0\n * @category escapes\n */\nexport const escaped = <E, R, I extends Input, E2, R2, I2 extends Input>(\n normal: Parser<string, E, R, I>,\n control: string | number,\n escape: Parser<string, E2, R2, I2>\n): Parser<string, E | E2, R | R2, I & I2> => {\n const ctrl = controlCode(control)\n return escapedImpl(\n normal,\n (state) => {\n if (!atControl(state, ctrl)) return false\n state.pos++\n return true\n },\n escape,\n ctrl,\n undefined\n )\n}\n\n/** How far `escaped` got: the text so far, and where to carry on. */\ninterface EscapedCheckpoint {\n readonly out: string\n readonly pos: number\n}\n\n/** @internal */\nexport const escapedImpl = <E, R, I extends Input, E2, R2, I2 extends Input>(\n normal: Parser<string, E, R, I>,\n matchControl: (state: State<any>) => boolean,\n escape: Parser<string, E2, R2, I2>,\n control: number,\n encoding: Encoding<any> | undefined\n): Parser<string, E | E2, R | R2, I & I2> => {\n const normalStep = normal.step\n const escStep = escape.step\n const key = new Key()\n return P.described(\n (state: State<any>) => {\n const start = state.pos\n let out = \"\"\n let resumed = false\n if (state.resume !== undefined) {\n const checkpoint = Resume.take(state, key, start) as EscapedCheckpoint | Resume.Done | undefined\n if (checkpoint instanceof Resume.Done) {\n state.pos = checkpoint.pos\n return checkpoint.value as string\n }\n if (checkpoint !== undefined) {\n out = checkpoint.out\n state.pos = checkpoint.pos\n resumed = true\n }\n }\n while (state.pos < state.end) {\n const before = state.pos\n const a = normalStep(state)\n if (a !== Fail) {\n if (state.pos !== before) {\n out += a\n continue\n }\n // `normal` matched nothing: fall through to the control character\n } else {\n if (state.mode === \"Incomplete\") return suspendAt(state, key, start, { out, pos: before })\n if (state.mode !== \"Backtrack\") return Fail\n state.rewind(before)\n }\n if (matchControl(state)) {\n const b = escStep(state)\n if (b === Fail) return isIncomplete(state) ? suspendAt(state, key, start, { out, pos: before }) : Fail\n out += b\n } else {\n return Resume.done(state, key, start, resumed, out)\n }\n }\n if (state.partial) {\n state.incomplete(0)\n return suspendAt(state, key, start, { out, pos: state.pos })\n }\n return Resume.done(state, key, start, resumed, out)\n },\n \"escaped\",\n { _tag: \"Escaped\", normal, control, escape, raw: false, encoding }\n )\n}\n\n// -----------------------------------------------------------------------------\n// Byte views\n// -----------------------------------------------------------------------------\n\ntype Bytes<P> =\n P extends Parser<infer A, infer E, infer R, infer _I>\n ? Parser<A extends string ? Uint8Array : A, E, R, Uint8Array>\n : never\n\n/**\n * The parsers of this module, typed for `Uint8Array` input. Slices are\n * returned as `Uint8Array`s.\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const encode = (s: string) => new TextEncoder().encode(s)\n * const header = Parser.separatedPair(\n * Ascii.bytes.alpha1,\n * Token.literal(encode(\": \")),\n * Ascii.bytes.tillLineEnding\n * )\n * ```\n *\n * @since 0.1.0\n * @category byte views\n */\nexport const bytes: {\n readonly alpha0: Bytes<typeof alpha0>\n readonly alpha1: Bytes<typeof alpha1>\n readonly digit0: Bytes<typeof digit0>\n readonly digit1: Bytes<typeof digit1>\n readonly hexDigit0: Bytes<typeof hexDigit0>\n readonly hexDigit1: Bytes<typeof hexDigit1>\n readonly octDigit0: Bytes<typeof octDigit0>\n readonly octDigit1: Bytes<typeof octDigit1>\n readonly alphanumeric0: Bytes<typeof alphanumeric0>\n readonly alphanumeric1: Bytes<typeof alphanumeric1>\n readonly space0: Bytes<typeof space0>\n readonly space1: Bytes<typeof space1>\n readonly multispace0: Bytes<typeof multispace0>\n readonly multispace1: Bytes<typeof multispace1>\n readonly crlf: Parser<Uint8Array, never, never, Uint8Array>\n readonly newline: Parser<Uint8Array, never, never, Uint8Array>\n readonly tab: Parser<Uint8Array, never, never, Uint8Array>\n readonly lineEnding: Parser<Uint8Array, never, never, Uint8Array>\n readonly tillLineEnding: Bytes<typeof tillLineEnding>\n readonly decUint: Parser<number, string, never, Uint8Array>\n readonly decInt: Parser<number, string, never, Uint8Array>\n readonly decBigUint: Parser<bigint, never, never, Uint8Array>\n readonly decBigInt: Parser<bigint, never, never, Uint8Array>\n readonly hexUint: Parser<number, string, never, Uint8Array>\n readonly float: Parser<number, never, never, Uint8Array>\n} = /*#__PURE__*/ (() => {\n const lit = (s: string) => Token.literal(new TextEncoder().encode(s))\n const crlfB = lit(\"\\r\\n\")\n const newlineB = lit(\"\\n\")\n return {\n alpha0,\n alpha1,\n digit0,\n digit1,\n hexDigit0,\n hexDigit1,\n octDigit0,\n octDigit1,\n alphanumeric0,\n alphanumeric1,\n space0,\n space1,\n multispace0,\n multispace1,\n crlf: crlfB,\n newline: newlineB,\n tab: lit(\"\\t\"),\n lineEnding: P.alt(newlineB, crlfB).pipe(P.expected(\"line ending\")),\n tillLineEnding,\n decUint,\n decInt,\n decBigUint,\n decBigInt,\n hexUint,\n float\n } as any\n})()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,MAAM,QAAQ,MAAgC,UAAU,SAAS,CAAC,IAAI,IAAI,EAAE,WAAW,CAAC;;;;;;;AAQxF,MAAa,UAAyD,iCACpE,YACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAQ,KAAK,MAAM,KAAK,MAAQ,KAAK,MAAM,KAAK;AAClD,GACA,CACE,CAAC,IAAI,EAAE,GACP,CAAC,IAAI,GAAG,CACV,CACF;;;;;;;AAQA,MAAa,UAAyD,iCACpE,YACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAO,KAAK,MAAM,KA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I,MAAM,SAAS;GACjB,MAAM,WAAW,CAAC;GAClB,OAAO,UAAU,OAAO,KAAK,OAAO;IAAE;IAAK,KAAK,MAAM;GAAI,CAAC;EAC7D;EACA,OAAO,OAAO,KAAK,OAAO,KAAK,OAAO,SAAS,GAAG;CACpD,GACA,WACA;EAAE,MAAM;EAAW;EAAQ;EAAS;EAAQ,KAAK;EAAO;CAAS,CACnE;AACF;;;;;;;;;;;;;;;;;;;;AA8BA,MAAa,QA0BK,qBAAO;CACvB,MAAM,OAAO,MAAcC,QAAM,QAAQ,IAAI,YAAY,CAAC,CAAC,OAAO,CAAC,CAAC;CACpE,MAAM,QAAQ,IAAI,MAAM;CACxB,MAAM,WAAW,IAAI,IAAI;CACzB,OAAO;EACL;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAM;EACN,SAAS;EACT,KAAK,IAAI,GAAI;EACb,YAAY,EAAE,IAAI,UAAU,KAAK,CAAC,CAAC,KAAK,EAAE,SAAS,aAAa,CAAC;EACjE;EACA;EACA;EACA;EACA;EACA;EACA;CACF;AACF,EAAA,CAAG"}
|
|
1
|
+
{"version":3,"file":"Ascii.js","names":["floatText","Token"],"sources":["../src/Ascii.ts"],"sourcesContent":["/**\n * Parsers for ASCII text: character classes, line endings, numbers and\n * escaped strings (winnow's `ascii` module, nom's `character` module).\n *\n * The parsers are typed for `string` input. Each also works on\n * `Uint8Array` input (treating bytes as ASCII), and `Ascii.bytes` gives the\n * same parsers typed for bytes:\n *\n * ```ts\n * import { Ascii, Parser } from \"@thomasfosterau/effect-parse\"\n *\n * Parser.parse(Ascii.decUint, \"42\") // 42\n * Parser.parse(Ascii.bytes.decUint, new TextEncoder().encode(\"42\")) // 42\n * ```\n *\n * Partial (streaming) input is supported: for example `digit1` on the\n * partial input `\"123\"` reports `Incomplete`, since more digits may follow.\n *\n * For Unicode character classes, or for text in other encodings (UTF-8,\n * UTF-16), use the `Text` module.\n *\n * @since 0.1.0\n */\nimport * as Predicate from \"effect/Predicate\"\nimport * as Result from \"effect/Result\"\nimport type { Encoding } from \"./Encoding.js\"\nimport type { Input, Slice, State } from \"./Input.js\"\nimport { Fail } from \"./Input.js\"\nimport type { ErrorKind } from \"./ParseError.js\"\nimport type { Parser } from \"./Parser.js\"\nimport * as P from \"./Parser.js\"\nimport * as numbers from \"./internal/numbers.js\"\nimport { Key } from \"./internal/key.js\"\nimport * as Resume from \"./internal/resume.js\"\nimport { codePointPredicate } from \"./internal/tokens.js\"\nimport { literalImpl, range, takeWhileImpl } from \"./Token.js\"\nimport * as Token from \"./Token.js\"\n\n// -----------------------------------------------------------------------------\n// Character predicates\n// -----------------------------------------------------------------------------\n\nconst code = (c: string | number): number => (Predicate.isNumber(c) ? c : c.charCodeAt(0))\n\n/**\n * `[a-zA-Z]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isAlpha: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isAlpha\",\n (c: string | number): boolean => {\n const x = code(c)\n return (x >= 65 && x <= 90) || (x >= 97 && x <= 122)\n },\n [\n [65, 90],\n [97, 122]\n ]\n)\n\n/**\n * `[0-9]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return x >= 48 && x <= 57\n },\n [[48, 57]]\n)\n\n/**\n * `[0-9a-fA-F]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isHexDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isHexDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return (x >= 48 && x <= 57) || (x >= 65 && x <= 70) || (x >= 97 && x <= 102)\n },\n [\n [48, 57],\n [65, 70],\n [97, 102]\n ]\n)\n\n/**\n * `[0-7]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isOctDigit: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isOctDigit\",\n (c: string | number): boolean => {\n const x = code(c)\n return x >= 48 && x <= 55\n },\n [[48, 55]]\n)\n\n/**\n * `[0-9a-zA-Z]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isAlphanumeric: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isAlphanumeric\",\n (c: string | number): boolean => isAlpha(c) || isDigit(c),\n [\n [48, 57],\n [65, 90],\n [97, 122]\n ]\n)\n\n/**\n * `[ \\t]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isSpace: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isSpace\",\n (c: string | number): boolean => {\n const x = code(c)\n return x === 32 || x === 9\n },\n [\n [9, 9],\n [32, 32]\n ]\n)\n\n/**\n * `[ \\t\\r\\n]`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isMultispace: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isMultispace\",\n (c: string | number): boolean => {\n const x = code(c)\n return x === 32 || x === 9 || x === 10 || x === 13\n },\n [\n [9, 10],\n [13, 13],\n [32, 32]\n ]\n)\n\n/**\n * `\\n`\n *\n * @since 0.1.0\n * @category predicates\n */\nexport const isNewline: (c: string | number) => boolean = /*#__PURE__*/ codePointPredicate(\n \"isNewline\",\n (c: string | number): boolean => code(c) === 10,\n [[10, 10]]\n)\n\n// Token sets usable for both string and byte input\nconst alphaSet = [/*#__PURE__*/ range(\"a\", \"z\"), /*#__PURE__*/ range(\"A\", \"Z\")]\nconst digitSet = /*#__PURE__*/ range(\"0\", \"9\")\nconst hexSet = [/*#__PURE__*/ range(\"0\", \"9\"), /*#__PURE__*/ range(\"a\", \"f\"), /*#__PURE__*/ range(\"A\", \"F\")]\nconst octSet = /*#__PURE__*/ range(\"0\", \"7\")\nconst alnumSet = [/*#__PURE__*/ range(\"a\", \"z\"), /*#__PURE__*/ range(\"A\", \"Z\"), /*#__PURE__*/ range(\"0\", \"9\")]\nconst spaceSet = \" \\t\"\nconst multispaceSet = \" \\t\\r\\n\"\n\nconst cls = (\n set: Token.StringTokenSet,\n min: 0 | 1,\n description: string,\n kind: ErrorKind,\n hint = true\n): Parser<string> => takeWhileImpl<string>(set, { min }, false, `${kind.toLowerCase()}${min}`, description, kind, hint)\n\n// -----------------------------------------------------------------------------\n// Character classes\n// -----------------------------------------------------------------------------\n\n/**\n * Zero or more ASCII letters, `[a-zA-Z]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alpha0: Parser<string> = /*#__PURE__*/ cls(alphaSet, 0, \"letter\", \"Alpha\")\n\n/**\n * One or more ASCII letters, `[a-zA-Z]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alpha1: Parser<string> = /*#__PURE__*/ cls(alphaSet, 1, \"letter\", \"Alpha\")\n\n/**\n * Zero or more ASCII digits, `[0-9]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const digit0: Parser<string> = /*#__PURE__*/ cls(digitSet, 0, \"digit\", \"Digit\")\n\n/**\n * One or more ASCII digits, `[0-9]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const digit1: Parser<string> = /*#__PURE__*/ cls(digitSet, 1, \"digit\", \"Digit\")\n\n/**\n * Zero or more hexadecimal digits, `[0-9a-fA-F]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const hexDigit0: Parser<string> = /*#__PURE__*/ cls(hexSet, 0, \"hexadecimal digit\", \"HexDigit\")\n\n/**\n * One or more hexadecimal digits, `[0-9a-fA-F]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const hexDigit1: Parser<string> = /*#__PURE__*/ cls(hexSet, 1, \"hexadecimal digit\", \"HexDigit\")\n\n/**\n * Zero or more octal digits, `[0-7]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const octDigit0: Parser<string> = /*#__PURE__*/ cls(octSet, 0, \"octal digit\", \"OctDigit\")\n\n/**\n * One or more octal digits, `[0-7]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const octDigit1: Parser<string> = /*#__PURE__*/ cls(octSet, 1, \"octal digit\", \"OctDigit\")\n\n/**\n * Zero or more ASCII letters or digits, `[0-9a-zA-Z]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alphanumeric0: Parser<string> = /*#__PURE__*/ cls(alnumSet, 0, \"letter or digit\", \"AlphaNumeric\")\n\n/**\n * One or more ASCII letters or digits, `[0-9a-zA-Z]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const alphanumeric1: Parser<string> = /*#__PURE__*/ cls(alnumSet, 1, \"letter or digit\", \"AlphaNumeric\")\n\n/**\n * Zero or more spaces and tabs, `[ \\t]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const space0: Parser<string> = /*#__PURE__*/ cls(spaceSet, 0, \"space\", \"Space\", false)\n\n/**\n * One or more spaces and tabs, `[ \\t]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const space1: Parser<string> = /*#__PURE__*/ cls(spaceSet, 1, \"space\", \"Space\", false)\n\n/**\n * Zero or more spaces, tabs, carriage returns and line feeds,\n * `[ \\t\\r\\n]*`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const multispace0: Parser<string> = /*#__PURE__*/ cls(multispaceSet, 0, \"whitespace\", \"MultiSpace\", false)\n\n/**\n * One or more spaces, tabs, carriage returns and line feeds,\n * `[ \\t\\r\\n]+`.\n *\n * @since 0.1.0\n * @category character classes\n */\nexport const multispace1: Parser<string> = /*#__PURE__*/ cls(multispaceSet, 1, \"whitespace\", \"MultiSpace\", false)\n\n// -----------------------------------------------------------------------------\n// Line endings\n// -----------------------------------------------------------------------------\n\n/**\n * A carriage return followed by a line feed, `\\r\\n`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const crlf: Parser<\"\\r\\n\"> = /*#__PURE__*/ Token.literal(\"\\r\\n\")\n\n/**\n * A line feed, `\\n` (nom's `newline`).\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const newline: Parser<\"\\n\"> = /*#__PURE__*/ Token.literal(\"\\n\")\n\n/**\n * A tab, `\\t`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const tab: Parser<\"\\t\"> = /*#__PURE__*/ Token.literal(\"\\t\")\n\n/**\n * A line ending: `\\n` or `\\r\\n`.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const lineEnding: Parser<\"\\n\" | \"\\r\\n\"> = /*#__PURE__*/ P.alt(newline, crlf).pipe(\n /*#__PURE__*/ P.expected(\"line ending\")\n)\n\nconst expectedLineEnding = [\"line ending\"]\n\n/**\n * Everything up to (not including) a line ending, `[^\\r\\n]*` (winnow's\n * `till_line_ending`, nom's `not_line_ending`). A carriage return that is\n * not followed by a line feed is an error.\n *\n * @since 0.1.0\n * @category line endings\n */\nexport const tillLineEnding: Parser<string> = /*#__PURE__*/ P.described<string>(\n (state: State<any>) => {\n const start = state.pos\n const input = state.input\n const isString = state.inputKind === 0\n let i = start\n while (i < state.end) {\n // SAFETY: `Ascii` parsers run on strings or `Uint8Array`s, and `isString` is `inputKind === 0`, set only for a string\n const c = isString ? (input as string).charCodeAt(i) : (input as Uint8Array)[i]\n if (c === 10) break\n if (c === 13) {\n // The line feed after it is read, not consumed\n state.syntax?.read(i + 2)\n if (i + 1 >= state.end) {\n if (state.partial) return state.incomplete(1, expectedLineEnding)\n return state.backtrack(\"LineEnding\", expectedLineEnding, i)\n }\n // SAFETY: `Ascii` parsers run on strings or `Uint8Array`s, and `isString` is `inputKind === 0`, set only for a string\n const d = isString ? (input as string).charCodeAt(i + 1) : (input as Uint8Array)[i + 1]\n if (d === 10) break\n return state.backtrack(\"LineEnding\", expectedLineEnding, i)\n }\n i++\n }\n if (i >= state.end && state.partial) return state.incomplete(1, expectedLineEnding)\n state.pos = i\n // SAFETY: on string input, the input type of a `Parser<string>`, the slice is a string (`bytes` retypes it for bytes)\n return state.slice(start, i) as string\n },\n \"tillLineEnding\",\n { _tag: \"TillLineEnding\", encoding: undefined }\n)\n\n// -----------------------------------------------------------------------------\n// Numbers\n// -----------------------------------------------------------------------------\n\n/** @internal */\nexport const overflow = (value: number): Result.Result<number, string> =>\n Number.isSafeInteger(value) ? Result.succeed(value) : Result.fail(\"integer overflow\")\n\n/**\n * An unsigned decimal integer: one or more digits (winnow's `dec_uint`,\n * nom's `u32`/`u64`). Fails if the value is not a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decUint: Parser<number, string> = /*#__PURE__*/ digit1.pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(Number(numbers.toText(s)))),\n /*#__PURE__*/ P.named(\"decUint\")\n)\n\n/**\n * A signed decimal integer: an optional `+` or `-` followed by one or more\n * digits (winnow's `dec_int`, nom's `i32`/`i64`). Fails if the value is not\n * a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decInt: Parser<number, string> = /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.seq(/*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\")), digit1)\n).pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(Number(numbers.toText(s)))),\n /*#__PURE__*/ P.named(\"decInt\")\n)\n\n/**\n * An unsigned decimal integer as a `bigint`, of any size.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decBigUint: Parser<bigint> = /*#__PURE__*/ digit1.pipe(\n /*#__PURE__*/ P.map((s) => BigInt(numbers.toText(s)))\n)\n\n/**\n * A signed decimal integer as a `bigint`, of any size.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const decBigInt: Parser<bigint> = /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.seq(/*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\")), digit1)\n).pipe(\n /*#__PURE__*/ P.map((s) => {\n const t = numbers.toText(s)\n return BigInt(t.startsWith(\"+\") ? t.slice(1) : t)\n })\n)\n\n/**\n * A hexadecimal unsigned integer, without prefix (winnow's `hex_uint`, nom's\n * `hex_u32`). Fails if the value is not a safe integer.\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const hexUint: Parser<number, string> = /*#__PURE__*/ hexDigit1.pipe(\n /*#__PURE__*/ P.tryMap((s) => overflow(parseInt(numbers.toText(s), 16))),\n /*#__PURE__*/ P.named(\"hexUint\")\n)\n\nconst sign = /*#__PURE__*/ P.opt(/*#__PURE__*/ Token.oneOf(\"+-\"))\nconst exponent = /*#__PURE__*/ P.opt(/*#__PURE__*/ P.seq(/*#__PURE__*/ Token.oneOf(\"eE\"), sign, digit1))\nconst unsignedFloat = /*#__PURE__*/ P.alt(\n /*#__PURE__*/ P.seq(\n /*#__PURE__*/ literalImpl(\"inf\", true),\n /*#__PURE__*/ P.opt(/*#__PURE__*/ literalImpl(\"inity\", true))\n ),\n /*#__PURE__*/ P.seq(/*#__PURE__*/ Token.literal(\".\"), digit1, exponent),\n /*#__PURE__*/ P.seq(\n digit1,\n /*#__PURE__*/ P.opt(/*#__PURE__*/ P.seq(/*#__PURE__*/ Token.literal(\".\"), /*#__PURE__*/ P.opt(digit1))),\n exponent\n )\n)\nconst floatText = /*#__PURE__*/ numbers.floatText(\n /*#__PURE__*/ P.taken(\n /*#__PURE__*/ P.alt(/*#__PURE__*/ literalImpl(\"nan\", true), /*#__PURE__*/ P.seq(sign, unsignedFloat))\n ).pipe(/*#__PURE__*/ P.expected(\"number\"))\n)\n\n/** @internal */\nexport const toFloat: (s: Input) => number = numbers.toFloat\n\n/**\n * A floating point number in decimal or scientific notation, including\n * `inf`, `infinity` and `nan` (case insensitive), as accepted by winnow's\n * `float` and nom's `double`.\n *\n * @example\n * ```ts\n * import { Ascii, Parser } from \"@thomasfosterau/effect-parse\"\n *\n * Parser.parse(Ascii.float, \"-1.5e3\") // Result.succeed(-1500)\n * ```\n *\n * @since 0.1.0\n * @category numbers\n */\nexport const float: Parser<number> = /*#__PURE__*/ floatText.pipe(\n /*#__PURE__*/ P.map(toFloat),\n /*#__PURE__*/ P.named(\"float\")\n)\n\n// -----------------------------------------------------------------------------\n// Literals\n// -----------------------------------------------------------------------------\n\n/**\n * The overloads of {@link caseless}.\n *\n * @since 0.5.0\n * @category literals\n */\nexport interface CaselessFn {\n (literal: string): Parser<string>\n (literal: Uint8Array): Parser<Uint8Array, never, never, Uint8Array>\n}\n\n/**\n * Matches a literal ignoring ASCII case (winnow's `Caseless`, nom's\n * `tag_no_case`). Returns the input as written.\n *\n * @since 0.1.0\n * @category literals\n */\nexport const caseless: CaselessFn = (lit: string | Uint8Array): any => literalImpl(lit, true)\n\n// -----------------------------------------------------------------------------\n// Escapes\n// -----------------------------------------------------------------------------\n\nconst controlCode = (c: string | number): number => (Predicate.isNumber(c) ? c : c.charCodeAt(0))\n\nconst atControl = (state: State<Input>, control: number): boolean => {\n if (state.pos >= state.end) return false\n const input = state.input\n // SAFETY: escapes are read from strings or `Uint8Array`s, and `inputKind` 0 is set only for a string\n const c = state.inputKind === 0 ? (input as string).charCodeAt(state.pos) : (input as Uint8Array)[state.pos]\n return c === control\n}\n\n/**\n * Recognizes a string containing escape sequences, returning the raw slice\n * (winnow's `take_escaped`, nom's `escaped`).\n *\n * - `normal` matches runs of ordinary characters (it must consume input)\n * - `control` is the escape character, e.g. `\"\\\\\"`\n * - `escapable` matches what may follow the control character\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const raw = Ascii.takeEscaped(Ascii.alpha1, \"\\\\\", Token.oneOf(\"\\\"n\\\\\"))\n * Parser.parse(raw, \"ab\\\\\\\"cd\") // Result.succeed(\"ab\\\\\\\"cd\")\n * ```\n *\n * @since 0.1.0\n * @category escapes\n */\nexport const takeEscaped = <A, E, R, I extends Input, B, E2, R2, I2 extends Input>(\n normal: Parser<A, E, R, I>,\n control: string | number,\n escapable: Parser<B, E2, R2, I2>\n): Parser<Slice<I & I2>, E | E2, R | R2, I & I2> => {\n const ctrl = controlCode(control)\n return takeEscapedImpl(\n normal,\n (state) => {\n if (!atControl(state, ctrl)) return false\n state.pos++\n return true\n },\n escapable,\n // SAFETY: the parser runs on input of type `I & I2`, and `State.slice` returns that input's own slice type\n (state, start) => state.slice(start, state.pos) as Slice<I & I2>,\n ctrl,\n undefined\n )\n}\n\nconst isIncomplete = (state: State<any>): boolean => state.mode === \"Incomplete\"\n\n/**\n * Fails with the current (`Incomplete`) failure, recording a checkpoint\n * first in a streaming parse.\n */\nconst suspendAt = (state: State<any>, key: Key, start: number, checkpoint: unknown): Fail => {\n if (state.resume !== undefined) Resume.save(state, key, start, checkpoint)\n return Fail\n}\n\n/** @internal */\nexport const takeEscapedImpl = <A, E, R, I extends Input, B, E2, R2, I2 extends Input, S>(\n normal: Parser<A, E, R, I>,\n matchControl: (state: State<any>) => boolean,\n escapable: Parser<B, E2, R2, I2>,\n slice: (state: State<any>, start: number) => S,\n control: number,\n encoding: Encoding<any> | undefined\n): Parser<S, E | E2, R | R2, I & I2> => {\n const normalStep = normal.step\n const escStep = escapable.step\n const key = new Key()\n return P.described(\n (state: State<any>) => {\n const start = state.pos\n let resumed = false\n if (state.resume !== undefined) {\n // SAFETY: under its own `key`, this parser saves only a resume offset (`suspendAt`) or a `Done`\n const checkpoint = Resume.take(state, key, start) as number | Resume.Done | undefined\n if (checkpoint instanceof Resume.Done) {\n state.pos = checkpoint.pos\n // SAFETY: a `Done` saved under this parser's `key` holds the `S` it returned\n return checkpoint.value as S\n }\n if (checkpoint !== undefined) {\n state.pos = checkpoint\n resumed = true\n }\n }\n while (state.pos < state.end) {\n const before = state.pos\n const a = normalStep(state)\n if (a !== Fail) {\n if (state.pos === before)\n return state.failMessage(\"Cut\", \"Assert\", \"`takeEscaped` parsers must always consume\")\n continue\n }\n if (state.mode === \"Incomplete\") return suspendAt(state, key, start, before)\n if (state.mode !== \"Backtrack\") return Fail\n state.rewind(before)\n if (matchControl(state)) {\n if (escStep(state) === Fail) return isIncomplete(state) ? suspendAt(state, key, start, before) : Fail\n } else {\n return Resume.done(state, key, start, resumed, slice(state, start))\n }\n }\n if (state.partial) {\n state.incomplete(0)\n return suspendAt(state, key, start, state.pos)\n }\n return Resume.done(state, key, start, resumed, slice(state, start))\n },\n \"takeEscaped\",\n { _tag: \"Escaped\", normal, control, escape: escapable, raw: true, encoding }\n )\n}\n\n/**\n * Parses a string containing escape sequences, returning the *unescaped*\n * string (winnow's `escaped`, nom's `escaped_transform`).\n *\n * - `normal` matches runs of ordinary characters, returning them\n * - `control` is the escape character, e.g. `\"\\\\\"`\n * - `escape` parses what follows the control character, returning its\n * replacement\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const unescaped = Ascii.escaped(\n * Token.takeTill(\"\\\\\\\"\"),\n * \"\\\\\",\n * Parser.alt(\n * Token.literal(\"n\").pipe(Parser.as(\"\\n\")),\n * Token.literal(\"\\\\\"),\n * Token.literal(\"\\\"\")\n * )\n * )\n * Parser.parse(unescaped, \"a\\\\nb\") // Result.succeed(\"a\\nb\")\n * ```\n *\n * @since 0.1.0\n * @category escapes\n */\nexport const escaped = <E, R, I extends Input, E2, R2, I2 extends Input>(\n normal: Parser<string, E, R, I>,\n control: string | number,\n escape: Parser<string, E2, R2, I2>\n): Parser<string, E | E2, R | R2, I & I2> => {\n const ctrl = controlCode(control)\n return escapedImpl(\n normal,\n (state) => {\n if (!atControl(state, ctrl)) return false\n state.pos++\n return true\n },\n escape,\n ctrl,\n undefined\n )\n}\n\n/** How far `escaped` got: the text so far, and where to carry on. */\ninterface EscapedCheckpoint {\n readonly out: string\n readonly pos: number\n}\n\n/** @internal */\nexport const escapedImpl = <E, R, I extends Input, E2, R2, I2 extends Input>(\n normal: Parser<string, E, R, I>,\n matchControl: (state: State<any>) => boolean,\n escape: Parser<string, E2, R2, I2>,\n control: number,\n encoding: Encoding<any> | undefined\n): Parser<string, E | E2, R | R2, I & I2> => {\n const normalStep = normal.step\n const escStep = escape.step\n const key = new Key()\n return P.described(\n (state: State<any>) => {\n const start = state.pos\n let out = \"\"\n let resumed = false\n if (state.resume !== undefined) {\n // SAFETY: under its own `key`, this parser saves only an `EscapedCheckpoint` (`suspendAt`) or a `Done`\n const checkpoint = Resume.take(state, key, start) as EscapedCheckpoint | Resume.Done | undefined\n if (checkpoint instanceof Resume.Done) {\n state.pos = checkpoint.pos\n // SAFETY: a `Done` saved under this parser's `key` holds the string it returned\n return checkpoint.value as string\n }\n if (checkpoint !== undefined) {\n out = checkpoint.out\n state.pos = checkpoint.pos\n resumed = true\n }\n }\n while (state.pos < state.end) {\n const before = state.pos\n const a = normalStep(state)\n if (a !== Fail) {\n if (state.pos !== before) {\n out += a\n continue\n }\n // `normal` matched nothing: fall through to the control character\n } else {\n if (state.mode === \"Incomplete\") return suspendAt(state, key, start, { out, pos: before })\n if (state.mode !== \"Backtrack\") return Fail\n state.rewind(before)\n }\n if (matchControl(state)) {\n const b = escStep(state)\n if (b === Fail) return isIncomplete(state) ? suspendAt(state, key, start, { out, pos: before }) : Fail\n out += b\n } else {\n return Resume.done(state, key, start, resumed, out)\n }\n }\n if (state.partial) {\n state.incomplete(0)\n return suspendAt(state, key, start, { out, pos: state.pos })\n }\n return Resume.done(state, key, start, resumed, out)\n },\n \"escaped\",\n { _tag: \"Escaped\", normal, control, escape, raw: false, encoding }\n )\n}\n\n// -----------------------------------------------------------------------------\n// Byte views\n// -----------------------------------------------------------------------------\n\ntype Bytes<P> =\n P extends Parser<infer A, infer E, infer R, infer _I>\n ? Parser<A extends string ? Uint8Array : A, E, R, Uint8Array>\n : never\n\n/**\n * Gives a value the type of another view of the same parsers (see\n * {@link bytes}).\n */\nconst retype = <T>(value: unknown): T =>\n // SAFETY: callers pass the byte views' parsers, which are the string parsers themselves (below)\n value as T\n\n/**\n * The parsers of this module, typed for `Uint8Array` input. Slices are\n * returned as `Uint8Array`s.\n *\n * @example\n * ```ts\n * import { Ascii, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const encode = (s: string) => new TextEncoder().encode(s)\n * const header = Parser.separatedPair(\n * Ascii.bytes.alpha1,\n * Token.literal(encode(\": \")),\n * Ascii.bytes.tillLineEnding\n * )\n * ```\n *\n * @since 0.1.0\n * @category byte views\n */\nexport const bytes: {\n readonly alpha0: Bytes<typeof alpha0>\n readonly alpha1: Bytes<typeof alpha1>\n readonly digit0: Bytes<typeof digit0>\n readonly digit1: Bytes<typeof digit1>\n readonly hexDigit0: Bytes<typeof hexDigit0>\n readonly hexDigit1: Bytes<typeof hexDigit1>\n readonly octDigit0: Bytes<typeof octDigit0>\n readonly octDigit1: Bytes<typeof octDigit1>\n readonly alphanumeric0: Bytes<typeof alphanumeric0>\n readonly alphanumeric1: Bytes<typeof alphanumeric1>\n readonly space0: Bytes<typeof space0>\n readonly space1: Bytes<typeof space1>\n readonly multispace0: Bytes<typeof multispace0>\n readonly multispace1: Bytes<typeof multispace1>\n readonly crlf: Parser<Uint8Array, never, never, Uint8Array>\n readonly newline: Parser<Uint8Array, never, never, Uint8Array>\n readonly tab: Parser<Uint8Array, never, never, Uint8Array>\n readonly lineEnding: Parser<Uint8Array, never, never, Uint8Array>\n readonly tillLineEnding: Bytes<typeof tillLineEnding>\n readonly decUint: Parser<number, string, never, Uint8Array>\n readonly decInt: Parser<number, string, never, Uint8Array>\n readonly decBigUint: Parser<bigint, never, never, Uint8Array>\n readonly decBigInt: Parser<bigint, never, never, Uint8Array>\n readonly hexUint: Parser<number, string, never, Uint8Array>\n readonly float: Parser<number, never, never, Uint8Array>\n} = /*#__PURE__*/ (() => {\n const lit = (s: string) => Token.literal(new TextEncoder().encode(s))\n const crlfB = lit(\"\\r\\n\")\n const newlineB = lit(\"\\n\")\n const views = {\n alpha0,\n alpha1,\n digit0,\n digit1,\n hexDigit0,\n hexDigit1,\n octDigit0,\n octDigit1,\n alphanumeric0,\n alphanumeric1,\n space0,\n space1,\n multispace0,\n multispace1,\n crlf: crlfB,\n newline: newlineB,\n tab: lit(\"\\t\"),\n lineEnding: P.alt(newlineB, crlfB).pipe(P.expected(\"line ending\")),\n tillLineEnding,\n decUint,\n decInt,\n decBigUint,\n decBigInt,\n hexUint,\n float\n }\n // SAFETY: the string parsers run on byte input unchanged (their slices of a `Uint8Array` are `Uint8Array`s),\n // so only their declared input and slice types differ from the byte views'\n return retype<typeof bytes>(views)\n})()\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA0CA,MAAM,QAAQ,MAAgC,UAAU,SAAS,CAAC,IAAI,IAAI,EAAE,WAAW,CAAC;;;;;;;AAQxF,MAAa,UAAyD,iCACpE,YACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAQ,KAAK,MAAM,KAAK,MAAQ,KAAK,MAAM,KAAK;AAClD,GACA,CACE,CAAC,IAAI,EAAE,GACP,CAAC,IAAI,GAAG,CACV,CACF;;;;;;;AAQA,MAAa,UAAyD,iCACpE,YACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAO,KAAK,MAAM,KAAK;AACzB,GACA,CAAC,CAAC,IAAI,EAAE,CAAC,CACX;;;;;;;AAQA,MAAa,aAA4D,iCACvE,eACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAQ,KAAK,MAAM,KAAK,MAAQ,KAAK,MAAM,KAAK,MAAQ,KAAK,MAAM,KAAK;AAC1E,GACA;CACE,CAAC,IAAI,EAAE;CACP,CAAC,IAAI,EAAE;CACP,CAAC,IAAI,GAAG;AACV,CACF;;;;;;;AAQA,MAAa,aAA4D,iCACvE,eACC,MAAgC;CAC/B,MAAM,IAAI,KAAK,CAAC;CAChB,OAAO,KAAK,MAAM,KAAK;AACzB,GACA,CAAC,CAAC,IAAI,EAAE,CAAC,CACX;;;;;;;AAQA,MAAa,iBAAgE,iCAC3E,mBACC,MAAgC,QAAQ,CAAC,KAAK,QAAQ,CAAC,GACxD;CACE,CAAC,IAAI,EAAE;CACP,CAAC,IAAI,EAAE;CACP,CAAC,IAAI,GAAG;AACV,CACF;;;;;;;AAQA,MAAa,UAAyD,iCACpE,YACC,MAA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AAe,6BAA6B;;qBAK1D,MAAM,QAAQ,eAAe,6BAA6B;;qBAK1D,OAAO,QAAQ,eAAe,6BAA6B;;qBAK3D,OAAO,QAAQ,eAAe,6BAA6B;;qBAK3D,MAAM,QAAQ,eAAe,6BAA6B;;qBAK1D,MAAM,QAAQ,eAAe,6BAA6B;;;;;;;;qBAmC1D,aAAc,GAAG,GAAG,UAAU,QAAQ,YACjD,OAAO,wBAAwB,GAAG,GAAG,OACpC,OAAO,MAAM,IAAI,GAAG,GAAG;;;;;;;;qBAuBb,eAAgB,GAAG,GAAG,UAAU,OAAO,GAAG,IAAI,IAAI,WAAW,OACxE,OAAO,wBAAwB,GAAG,GAAG,IACrC,QAAQ,OAAO,GAAG,IAAI,IAAI,QACzB,OAAO,MAAM,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI;;;;;;;;;qBAc3B,gBAAiB,GAAG,GAAG,UAAU,OAAO,GAAG,IAAI,IAC1D,OAAO,wBAAwB,GAAG,GAAG,IACrC,QAAQ,OAAO,GAAG,IAAI,IAAI,MAAM,KAAK,WACpC,OAAO,GAAG,IAAI,IAAI,IAAI,IAAI"}
|
package/dist/Binary.js
CHANGED
|
@@ -24,34 +24,44 @@ import { bits } from "./Bits.js";
|
|
|
24
24
|
* @since 0.1.0
|
|
25
25
|
*/
|
|
26
26
|
const nativeLittle = (/*#__PURE__*/ new Uint8Array((/*#__PURE__*/ new Uint16Array([1])).buffer))[0] === 1;
|
|
27
|
+
/**
|
|
28
|
+
* A `DataView` of a state's bytes, for the numbers integer arithmetic can't
|
|
29
|
+
* read: created once per state (whose input never changes), as one per
|
|
30
|
+
* read cost most of a number's time.
|
|
31
|
+
*/
|
|
32
|
+
const viewOf = (state) => {
|
|
33
|
+
const b = state.input;
|
|
34
|
+
return state.view ??= new DataView(b.buffer, b.byteOffset, b.byteLength);
|
|
35
|
+
};
|
|
36
|
+
/** A {@link Read} through the state's view. */
|
|
37
|
+
const viewed = (read) => (_, o, s) => read(viewOf(s), o);
|
|
27
38
|
const fixed = (size, name, read) => {
|
|
28
39
|
const expected = [`${size} byte${size === 1 ? "" : "s"}`];
|
|
29
|
-
return P.
|
|
40
|
+
return P.described((state) => {
|
|
30
41
|
const pos = state.pos;
|
|
31
42
|
const avail = state.end - pos;
|
|
32
43
|
if (avail < size) return state.eoi("Token", expected, size - avail);
|
|
33
|
-
const bytes = state.input;
|
|
34
|
-
const view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
35
44
|
state.pos = pos + size;
|
|
36
|
-
return read(
|
|
37
|
-
}, name
|
|
45
|
+
return read(state.input, pos, state);
|
|
46
|
+
}, name, {
|
|
47
|
+
_tag: "Fixed",
|
|
48
|
+
size,
|
|
49
|
+
read
|
|
50
|
+
});
|
|
38
51
|
};
|
|
39
|
-
const readU24 = (little) => (
|
|
52
|
+
const readU24 = (little) => (b, o) => little ? b[o] | b[o + 1] << 8 | b[o + 2] << 16 : b[o] << 16 | b[o + 1] << 8 | b[o + 2];
|
|
40
53
|
const readI24 = (little) => {
|
|
41
54
|
const u = readU24(little);
|
|
42
|
-
return (
|
|
43
|
-
const x = u(v, o);
|
|
44
|
-
return x & 8388608 ? x - 16777216 : x;
|
|
45
|
-
};
|
|
55
|
+
return (b, o, s) => u(b, o, s) << 8 >> 8;
|
|
46
56
|
};
|
|
47
|
-
const readU128 = (little) => (v, o) => {
|
|
57
|
+
const readU128 = (little) => viewed((v, o) => {
|
|
48
58
|
const a = v.getBigUint64(o, little);
|
|
49
59
|
const b = v.getBigUint64(o + 8, little);
|
|
50
60
|
return little ? b << 64n | a : a << 64n | b;
|
|
51
|
-
};
|
|
61
|
+
});
|
|
52
62
|
const readI128 = (little) => {
|
|
53
63
|
const u = readU128(little);
|
|
54
|
-
return (
|
|
64
|
+
return (b, o, s) => BigInt.asIntN(128, u(b, o, s));
|
|
55
65
|
};
|
|
56
66
|
/**
|
|
57
67
|
* An unsigned byte.
|
|
@@ -59,22 +69,22 @@ const readI128 = (little) => {
|
|
|
59
69
|
* @since 0.1.0
|
|
60
70
|
* @category integers
|
|
61
71
|
*/
|
|
62
|
-
const u8 = /*#__PURE__*/ fixed(1, "u8", (
|
|
72
|
+
const u8 = /*#__PURE__*/ fixed(1, "u8", (b, o) => b[o]);
|
|
63
73
|
/**
|
|
64
74
|
* A signed byte.
|
|
65
75
|
*
|
|
66
76
|
* @since 0.1.0
|
|
67
77
|
* @category integers
|
|
68
78
|
*/
|
|
69
|
-
const i8 = /*#__PURE__*/ fixed(1, "i8", (
|
|
79
|
+
const i8 = /*#__PURE__*/ fixed(1, "i8", (b, o) => b[o] << 24 >> 24);
|
|
70
80
|
/** @since 0.1.0 @category integers */
|
|
71
|
-
const beU16 = /*#__PURE__*/ fixed(2, "beU16", (
|
|
81
|
+
const beU16 = /*#__PURE__*/ fixed(2, "beU16", (b, o) => b[o] << 8 | b[o + 1]);
|
|
72
82
|
/** @since 0.1.0 @category integers */
|
|
73
|
-
const leU16 = /*#__PURE__*/ fixed(2, "leU16", (
|
|
83
|
+
const leU16 = /*#__PURE__*/ fixed(2, "leU16", (b, o) => b[o] | b[o + 1] << 8);
|
|
74
84
|
/** @since 0.1.0 @category integers */
|
|
75
|
-
const beI16 = /*#__PURE__*/ fixed(2, "beI16", (
|
|
85
|
+
const beI16 = /*#__PURE__*/ fixed(2, "beI16", (b, o) => (b[o] << 8 | b[o + 1]) << 16 >> 16);
|
|
76
86
|
/** @since 0.1.0 @category integers */
|
|
77
|
-
const leI16 = /*#__PURE__*/ fixed(2, "leI16", (
|
|
87
|
+
const leI16 = /*#__PURE__*/ fixed(2, "leI16", (b, o) => (b[o] | b[o + 1] << 8) << 16 >> 16);
|
|
78
88
|
/** @since 0.1.0 @category integers */
|
|
79
89
|
const beU24 = /*#__PURE__*/ fixed(3, "beU24", /*#__PURE__*/ readU24(false));
|
|
80
90
|
/** @since 0.1.0 @category integers */
|
|
@@ -84,21 +94,21 @@ const beI24 = /*#__PURE__*/ fixed(3, "beI24", /*#__PURE__*/ readI24(false));
|
|
|
84
94
|
/** @since 0.1.0 @category integers */
|
|
85
95
|
const leI24 = /*#__PURE__*/ fixed(3, "leI24", /*#__PURE__*/ readI24(true));
|
|
86
96
|
/** @since 0.1.0 @category integers */
|
|
87
|
-
const beU32 = /*#__PURE__*/ fixed(4, "beU32", (
|
|
97
|
+
const beU32 = /*#__PURE__*/ fixed(4, "beU32", (b, o) => (b[o] << 24 | b[o + 1] << 16 | b[o + 2] << 8 | b[o + 3]) >>> 0);
|
|
88
98
|
/** @since 0.1.0 @category integers */
|
|
89
|
-
const leU32 = /*#__PURE__*/ fixed(4, "leU32", (
|
|
99
|
+
const leU32 = /*#__PURE__*/ fixed(4, "leU32", (b, o) => (b[o] | b[o + 1] << 8 | b[o + 2] << 16 | b[o + 3] << 24) >>> 0);
|
|
90
100
|
/** @since 0.1.0 @category integers */
|
|
91
|
-
const beI32 = /*#__PURE__*/ fixed(4, "beI32", (
|
|
101
|
+
const beI32 = /*#__PURE__*/ fixed(4, "beI32", (b, o) => b[o] << 24 | b[o + 1] << 16 | b[o + 2] << 8 | b[o + 3]);
|
|
92
102
|
/** @since 0.1.0 @category integers */
|
|
93
|
-
const leI32 = /*#__PURE__*/ fixed(4, "leI32", (
|
|
103
|
+
const leI32 = /*#__PURE__*/ fixed(4, "leI32", (b, o) => b[o] | b[o + 1] << 8 | b[o + 2] << 16 | b[o + 3] << 24);
|
|
94
104
|
/** @since 0.1.0 @category integers */
|
|
95
|
-
const beU64 = /*#__PURE__*/ fixed(8, "beU64", (v, o) => v.getBigUint64(o, false));
|
|
105
|
+
const beU64 = /*#__PURE__*/ fixed(8, "beU64", /*#__PURE__*/ viewed((v, o) => v.getBigUint64(o, false)));
|
|
96
106
|
/** @since 0.1.0 @category integers */
|
|
97
|
-
const leU64 = /*#__PURE__*/ fixed(8, "leU64", (v, o) => v.getBigUint64(o, true));
|
|
107
|
+
const leU64 = /*#__PURE__*/ fixed(8, "leU64", /*#__PURE__*/ viewed((v, o) => v.getBigUint64(o, true)));
|
|
98
108
|
/** @since 0.1.0 @category integers */
|
|
99
|
-
const beI64 = /*#__PURE__*/ fixed(8, "beI64", (v, o) => v.getBigInt64(o, false));
|
|
109
|
+
const beI64 = /*#__PURE__*/ fixed(8, "beI64", /*#__PURE__*/ viewed((v, o) => v.getBigInt64(o, false)));
|
|
100
110
|
/** @since 0.1.0 @category integers */
|
|
101
|
-
const leI64 = /*#__PURE__*/ fixed(8, "leI64", (v, o) => v.getBigInt64(o, true));
|
|
111
|
+
const leI64 = /*#__PURE__*/ fixed(8, "leI64", /*#__PURE__*/ viewed((v, o) => v.getBigInt64(o, true)));
|
|
102
112
|
/** @since 0.1.0 @category integers */
|
|
103
113
|
const beU128 = /*#__PURE__*/ fixed(16, "beU128", /*#__PURE__*/ readU128(false));
|
|
104
114
|
/** @since 0.1.0 @category integers */
|
|
@@ -108,13 +118,13 @@ const beI128 = /*#__PURE__*/ fixed(16, "beI128", /*#__PURE__*/ readI128(false));
|
|
|
108
118
|
/** @since 0.1.0 @category integers */
|
|
109
119
|
const leI128 = /*#__PURE__*/ fixed(16, "leI128", /*#__PURE__*/ readI128(true));
|
|
110
120
|
/** @since 0.1.0 @category floats */
|
|
111
|
-
const beF32 = /*#__PURE__*/ fixed(4, "beF32", (v, o) => v.getFloat32(o, false));
|
|
121
|
+
const beF32 = /*#__PURE__*/ fixed(4, "beF32", /*#__PURE__*/ viewed((v, o) => v.getFloat32(o, false)));
|
|
112
122
|
/** @since 0.1.0 @category floats */
|
|
113
|
-
const leF32 = /*#__PURE__*/ fixed(4, "leF32", (v, o) => v.getFloat32(o, true));
|
|
123
|
+
const leF32 = /*#__PURE__*/ fixed(4, "leF32", /*#__PURE__*/ viewed((v, o) => v.getFloat32(o, true)));
|
|
114
124
|
/** @since 0.1.0 @category floats */
|
|
115
|
-
const beF64 = /*#__PURE__*/ fixed(8, "beF64", (v, o) => v.getFloat64(o, false));
|
|
125
|
+
const beF64 = /*#__PURE__*/ fixed(8, "beF64", /*#__PURE__*/ viewed((v, o) => v.getFloat64(o, false)));
|
|
116
126
|
/** @since 0.1.0 @category floats */
|
|
117
|
-
const leF64 = /*#__PURE__*/ fixed(8, "leF64", (v, o) => v.getFloat64(o, true));
|
|
127
|
+
const leF64 = /*#__PURE__*/ fixed(8, "leF64", /*#__PURE__*/ viewed((v, o) => v.getFloat64(o, true)));
|
|
118
128
|
const byEndian = (big, little) => (endian) => endian === "big" ? big : endian === "little" ? little : nativeLittle ? little : big;
|
|
119
129
|
/**
|
|
120
130
|
* An unsigned 16-bit integer in the given byte order (winnow's `u16`).
|
|
@@ -146,6 +156,25 @@ const f32 = /*#__PURE__*/ byEndian(beF32, leF32);
|
|
|
146
156
|
/** @since 0.1.0 @category floats */
|
|
147
157
|
const f64 = /*#__PURE__*/ byEndian(beF64, leF64);
|
|
148
158
|
const toCount = (n) => Predicate.isBigInt(n) ? Number(n) : n;
|
|
159
|
+
/** How many counts {@link byCount} keeps the parsers of. */
|
|
160
|
+
const CountCache = 256;
|
|
161
|
+
/**
|
|
162
|
+
* `build`, keeping the parsers it built for the first {@link CountCache}
|
|
163
|
+
* counts seen: length-prefixed data repeats a few lengths, and building a
|
|
164
|
+
* parser per value cost several times the parse.
|
|
165
|
+
*/
|
|
166
|
+
const byCount = (build) => {
|
|
167
|
+
const built = /* @__PURE__ */ new Map();
|
|
168
|
+
return (n) => {
|
|
169
|
+
const count = toCount(n);
|
|
170
|
+
let a = built.get(count);
|
|
171
|
+
if (a === void 0) {
|
|
172
|
+
a = build(count);
|
|
173
|
+
if (built.size < CountCache) built.set(count, a);
|
|
174
|
+
}
|
|
175
|
+
return a;
|
|
176
|
+
};
|
|
177
|
+
};
|
|
149
178
|
/**
|
|
150
179
|
* Parses a length with `count`, then takes that many tokens (winnow's
|
|
151
180
|
* `length_take`, nom's `length_data`).
|
|
@@ -154,11 +183,11 @@ const toCount = (n) => Predicate.isBigInt(n) ? Number(n) : n;
|
|
|
154
183
|
* @category length-prefixed
|
|
155
184
|
*/
|
|
156
185
|
const lengthTake = (count) => {
|
|
157
|
-
const
|
|
158
|
-
return P.
|
|
186
|
+
const take = byCount((n) => Token$1.take(n).step);
|
|
187
|
+
return P.combinator([count], ([countStep]) => (state) => {
|
|
159
188
|
const n = countStep(state);
|
|
160
189
|
if (n === Fail) return Fail;
|
|
161
|
-
return
|
|
190
|
+
return take(n)(state);
|
|
162
191
|
}, "lengthTake");
|
|
163
192
|
};
|
|
164
193
|
/**
|
|
@@ -168,7 +197,7 @@ const lengthTake = (count) => {
|
|
|
168
197
|
* @since 0.1.0
|
|
169
198
|
* @category length-prefixed
|
|
170
199
|
*/
|
|
171
|
-
const lengthRepeat = (count, parser) => P.flatMap(count, (n) => P.repeat(parser,
|
|
200
|
+
const lengthRepeat = (count, parser) => P.flatMap(count, byCount((n) => P.repeat(parser, n)));
|
|
172
201
|
/**
|
|
173
202
|
* Parses a length with `count`, takes that many tokens, and applies
|
|
174
203
|
* `parser` to them (winnow's `length_and_then`, nom's `length_value`).
|