@thomasfosterau/effect-parse 0.2.0 → 0.4.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 +44 -1
- package/dist/Ascii.d.ts +0 -2
- package/dist/Ascii.d.ts.map +1 -1
- package/dist/Ascii.js +6 -6
- package/dist/Ascii.js.map +1 -1
- package/dist/Binary.d.ts +0 -2
- package/dist/Binary.d.ts.map +1 -1
- package/dist/Binary.js +1 -2
- package/dist/Bits.d.ts +0 -2
- package/dist/Bits.d.ts.map +1 -1
- package/dist/Bits.js +2 -3
- package/dist/Bits.js.map +1 -1
- package/dist/Compiler.d.ts +0 -2
- package/dist/Compiler.d.ts.map +1 -1
- package/dist/Compiler.js +1 -2
- package/dist/Encoding.d.ts +0 -2
- package/dist/Encoding.d.ts.map +1 -1
- package/dist/Encoding.js +1 -2
- package/dist/Expression.d.ts +0 -2
- package/dist/Expression.d.ts.map +1 -1
- package/dist/Expression.js +61 -18
- package/dist/Expression.js.map +1 -1
- package/dist/Input.d.ts +80 -7
- package/dist/Input.d.ts.map +1 -1
- package/dist/Input.js +149 -18
- package/dist/Input.js.map +1 -1
- package/dist/Nom.d.ts +1 -3
- package/dist/Nom.d.ts.map +1 -1
- package/dist/Nom.js +1 -2
- package/dist/ParseError.d.ts +0 -2
- package/dist/ParseError.d.ts.map +1 -1
- package/dist/ParseError.js +6 -10
- package/dist/ParseError.js.map +1 -1
- package/dist/Parser.d.ts +2 -2
- package/dist/Parser.d.ts.map +1 -1
- package/dist/Parser.js +58 -32
- package/dist/Parser.js.map +1 -1
- package/dist/ParserSchema.d.ts +198 -2
- package/dist/ParserSchema.d.ts.map +1 -1
- package/dist/ParserSchema.js +646 -18
- package/dist/ParserSchema.js.map +1 -1
- package/dist/ParserStream.d.ts +0 -2
- package/dist/ParserStream.d.ts.map +1 -1
- package/dist/ParserStream.js +15 -9
- package/dist/ParserStream.js.map +1 -1
- package/dist/Syntax.d.ts +0 -2
- package/dist/Syntax.d.ts.map +1 -1
- package/dist/Syntax.js +13 -11
- package/dist/Syntax.js.map +1 -1
- package/dist/Text.d.ts +0 -2
- package/dist/Text.d.ts.map +1 -1
- package/dist/Text.js +1 -2
- package/dist/Token.d.ts +0 -2
- package/dist/Token.d.ts.map +1 -1
- package/dist/Token.js +1 -2
- package/dist/internal/compiler/closure.d.ts +5 -2
- package/dist/internal/compiler/closure.d.ts.map +1 -1
- package/dist/internal/compiler/closure.js +160 -92
- package/dist/internal/compiler/closure.js.map +1 -1
- package/dist/internal/compiler/entries.d.ts +0 -2
- package/dist/internal/compiler/entries.d.ts.map +1 -1
- package/dist/internal/compiler/entries.js +6 -7
- package/dist/internal/compiler/entries.js.map +1 -1
- package/dist/internal/compiler/first.d.ts +0 -2
- package/dist/internal/compiler/first.d.ts.map +1 -1
- package/dist/internal/compiler/first.js +10 -10
- package/dist/internal/compiler/first.js.map +1 -1
- package/dist/internal/compiler/jit.d.ts +0 -2
- package/dist/internal/compiler/jit.d.ts.map +1 -1
- package/dist/internal/compiler/jit.js +52 -51
- package/dist/internal/compiler/jit.js.map +1 -1
- package/dist/internal/compiler/registry.d.ts +0 -2
- package/dist/internal/compiler/registry.d.ts.map +1 -1
- package/dist/internal/compiler/registry.js +1 -2
- package/dist/internal/compiler/sets.d.ts +2 -2
- package/dist/internal/compiler/sets.d.ts.map +1 -1
- package/dist/internal/compiler/sets.js +8 -3
- package/dist/internal/compiler/sets.js.map +1 -1
- package/dist/internal/entries.d.ts +0 -2
- package/dist/internal/entries.d.ts.map +1 -1
- package/dist/internal/entries.js +1 -2
- package/dist/internal/key.d.ts +10 -0
- package/dist/internal/key.d.ts.map +1 -0
- package/dist/internal/key.js +11 -0
- package/dist/internal/key.js.map +1 -0
- package/dist/internal/node.d.ts +11 -4
- package/dist/internal/node.d.ts.map +1 -1
- package/dist/internal/node.js +15 -3
- package/dist/internal/node.js.map +1 -1
- package/dist/internal/numbers.d.ts +0 -2
- package/dist/internal/numbers.d.ts.map +1 -1
- package/dist/internal/numbers.js +1 -2
- package/dist/internal/resume.d.ts +5 -6
- package/dist/internal/resume.d.ts.map +1 -1
- package/dist/internal/resume.js +1 -2
- package/dist/internal/resume.js.map +1 -1
- package/dist/internal/run.d.ts +0 -2
- package/dist/internal/run.d.ts.map +1 -1
- package/dist/internal/run.js +2 -3
- package/dist/internal/run.js.map +1 -1
- package/dist/internal/stack.d.ts +0 -2
- package/dist/internal/stack.d.ts.map +1 -1
- package/dist/internal/stack.js +12 -3
- package/dist/internal/stack.js.map +1 -1
- package/dist/internal/syntax.d.ts +30 -19
- package/dist/internal/syntax.d.ts.map +1 -1
- package/dist/internal/syntax.js +74 -63
- package/dist/internal/syntax.js.map +1 -1
- package/dist/internal/text.d.ts +5 -2
- package/dist/internal/text.d.ts.map +1 -1
- package/dist/internal/text.js +14 -5
- package/dist/internal/text.js.map +1 -1
- package/dist/internal/ties.d.ts +28 -0
- package/dist/internal/ties.d.ts.map +1 -0
- package/dist/internal/ties.js +66 -0
- package/dist/internal/ties.js.map +1 -0
- package/dist/internal/tokens.d.ts +10 -9
- package/dist/internal/tokens.d.ts.map +1 -1
- package/dist/internal/tokens.js +4 -8
- package/dist/internal/tokens.js.map +1 -1
- package/package.json +6 -6
- package/src/Ascii.ts +6 -5
- package/src/Bits.ts +1 -1
- package/src/Expression.ts +27 -23
- package/src/Input.ts +157 -21
- package/src/ParseError.ts +10 -8
- package/src/Parser.ts +67 -32
- package/src/ParserSchema.ts +982 -23
- package/src/ParserStream.ts +20 -8
- package/src/Syntax.ts +12 -9
- package/src/internal/compiler/closure.ts +202 -90
- package/src/internal/compiler/entries.ts +14 -11
- package/src/internal/compiler/first.ts +9 -8
- package/src/internal/compiler/jit.ts +95 -74
- package/src/internal/compiler/sets.ts +7 -0
- package/src/internal/key.ts +11 -0
- package/src/internal/node.ts +19 -2
- package/src/internal/resume.ts +5 -4
- package/src/internal/run.ts +1 -1
- package/src/internal/stack.ts +6 -1
- package/src/internal/syntax.ts +80 -84
- package/src/internal/text.ts +24 -4
- package/src/internal/ties.ts +83 -0
- package/src/internal/tokens.ts +20 -13
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { Input, State } from "../Input.js";
|
|
2
|
-
//#region src/internal/tokens.d.ts
|
|
3
2
|
/** A compiled token set. */
|
|
4
3
|
export interface Matcher {
|
|
5
4
|
/** Tests a code point (string input) or a byte (Uint8Array input). */
|
|
@@ -59,21 +58,24 @@ export declare const splitSurrogate: (state: State<Input>, i: number) => boolean
|
|
|
59
58
|
export declare const NoMatch: unique symbol;
|
|
60
59
|
export declare const End: unique symbol;
|
|
61
60
|
export declare const nextIf: (state: State<Input>, m: AnyMatcher, negate: boolean) => unknown;
|
|
61
|
+
/** How far a {@link scan} got. */
|
|
62
|
+
export interface Scanned extends Advanced {
|
|
63
|
+
/** Whether the end of input was hit before `max` tokens. */
|
|
64
|
+
readonly hitEnd: boolean;
|
|
65
|
+
}
|
|
62
66
|
/**
|
|
63
67
|
* Scans forward while tokens satisfy the matcher (xor `negate`), up to `max`
|
|
64
68
|
* tokens. Returns the offset reached, the number of tokens matched, and
|
|
65
69
|
* whether the end of input was hit before `max` tokens.
|
|
66
70
|
*/
|
|
67
|
-
export declare const scan: (state: State<Input>, m: AnyMatcher, negate: boolean, max: number) =>
|
|
71
|
+
export declare const scan: (state: State<Input>, m: AnyMatcher, negate: boolean, max: number) => Scanned;
|
|
72
|
+
/** The offset reached and the number of tokens passed. */
|
|
73
|
+
export interface Advanced {
|
|
68
74
|
readonly offset: number;
|
|
69
75
|
readonly count: number;
|
|
70
|
-
|
|
71
|
-
};
|
|
76
|
+
}
|
|
72
77
|
/** Advances `n` tokens from `start`, returning the offset reached and the count. */
|
|
73
|
-
export declare const advance: (state: State<Input>, start: number, n: number) =>
|
|
74
|
-
offset: number;
|
|
75
|
-
count: number;
|
|
76
|
-
};
|
|
78
|
+
export declare const advance: (state: State<Input>, start: number, n: number) => Advanced;
|
|
77
79
|
/** Counts tokens between two offsets. */
|
|
78
80
|
export declare const countTokens: (state: State<Input>, start: number, end: number) => number;
|
|
79
81
|
/** Finds `needle` within `[from, state.end)`; returns -1 if absent. */
|
|
@@ -84,5 +86,4 @@ export declare const indexOf: (state: State<Input>, needle: Input, from: number)
|
|
|
84
86
|
* -1 on mismatch, or -2 when the input ends while matching a prefix.
|
|
85
87
|
*/
|
|
86
88
|
export declare const matchLiteral: (state: State<Input>, lit: Input, caseless: boolean) => number;
|
|
87
|
-
//#endregion
|
|
88
89
|
//# sourceMappingURL=tokens.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.d.ts","names":[],"sources":["../../src/internal/tokens.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"tokens.d.ts","names":[],"sources":["../../src/internal/tokens.ts"],"mappings":";;iBAQiB;;WAEN,OAAO;;WAEP,QAAQ;;WAER;;;iBAIM;WACN;WACA;WACA;;;qBAQE,eAAgB;;;;;;qBA2FhB;;;;;qBAMA,qBAAsB,WAAW,sBAC5C,cACA,GAAG,GACH,SAAS,6CACR;;;;;;qBAYU,UAAW,cAAc,6BAAmC;;;;;iBA+BxD;WACN,WAAW;WACX,SAAS;WACT;;;qBAuBE,kBAAmB,KAAK;qBASxB,aAAc,iBAAe;;;;;;qBAmB7B,iBAAkB,OAAO,MAAM,QAAQ;;;;;qBAoBvC;qBACA;qBAEA,SAAU,OAAO,MAAM,QAAQ,GAAG,YAAY;;iBA6B1C,gBAAgB;;WAEtB;;;;;;;qBAQE,OAAQ,OAAO,MAAM,QAAQ,GAAG,YAAY,iBAAiB,gBAAc;;iBAkDvE;WACN;WACA;;;qBAIE,UAAW,OAAO,MAAM,QAAQ,eAAe,cAAY;;qBAkB3D,cAAe,OAAO,MAAM,QAAQ,eAAe;;qBAanD,UAAW,OAAO,MAAM,QAAQ,QAAQ,OAAO;;;;;;qBAwC/C,eAAgB,OAAO,MAAM,QAAQ,KAAK,OAAO"}
|
package/dist/internal/tokens.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Predicate from "effect/Predicate";
|
|
2
|
+
import * as Result from "effect/Result";
|
|
2
3
|
import { charOf, firstCodePoint } from "./text.js";
|
|
3
|
-
|
|
4
|
+
// src/internal/tokens.ts
|
|
4
5
|
const isRange = (u) => Predicate.isObjectOrArray(u) && u._tag === "TokenRange";
|
|
5
6
|
const toCode = (u) => Predicate.isNumber(u) ? u : firstCodePoint(u);
|
|
6
7
|
/** @internal */
|
|
@@ -118,11 +119,7 @@ const compileCodeSafe = (set, kind) => {
|
|
|
118
119
|
return false;
|
|
119
120
|
};
|
|
120
121
|
}
|
|
121
|
-
try
|
|
122
|
-
return compileCode(set);
|
|
123
|
-
} catch {
|
|
124
|
-
return () => false;
|
|
125
|
-
}
|
|
122
|
+
return Result.getOrElse(Result.try(() => compileCode(set)), () => () => false);
|
|
126
123
|
};
|
|
127
124
|
/**
|
|
128
125
|
* A small cache keyed by value, for the parts of a parser that are costly to
|
|
@@ -333,7 +330,7 @@ const indexOf = (state, needle, from) => {
|
|
|
333
330
|
case 1: {
|
|
334
331
|
const hay = state.input;
|
|
335
332
|
const n = needle;
|
|
336
|
-
if (n.
|
|
333
|
+
if (n.byteLength === 0) return from;
|
|
337
334
|
const first = n[0];
|
|
338
335
|
let i = hay.indexOf(first, from);
|
|
339
336
|
while (i !== -1 && i <= last) {
|
|
@@ -400,7 +397,6 @@ const match = (state, lit, caseless) => {
|
|
|
400
397
|
}
|
|
401
398
|
};
|
|
402
399
|
const lowerAscii = (c) => c >= 65 && c <= 90 ? c + 32 : c;
|
|
403
|
-
//#endregion
|
|
404
400
|
export { End, NoMatch, PredicateRanges, advance, codePointPredicate, compile, compileAny, countTokens, describeChar, describeLiteral, indexOf, matchLiteral, nextIf, scan, splitSurrogate };
|
|
405
401
|
|
|
406
402
|
//# sourceMappingURL=tokens.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tokens.js","names":[],"sources":["../../src/internal/tokens.ts"],"sourcesContent":["// fallow-ignore-file duplicate-export -- `End` and `scan` here are the token-set versions of the text\n// helpers in ./text.ts; both are internal modules, imported by path, never re-exported from one barrel.\nimport * as Predicate from \"effect/Predicate\"\nimport type { Input, State } from \"../Input.js\"\nimport { charOf, firstCodePoint } from \"./text.js\"\n\n/** A compiled token set. */\nexport interface Matcher {\n /** Tests a code point (string input) or a byte (Uint8Array input). */\n readonly code: (code: number) => boolean\n /** Tests an element of an array input. */\n readonly token: (token: unknown) => boolean\n /** Human readable description, used in error messages. */\n readonly description: string\n}\n\n/** @internal */\nexport interface TokenRangeLike {\n readonly _tag: \"TokenRange\"\n readonly from: string | number\n readonly to: string | number\n}\n\nconst isRange = (u: unknown): u is TokenRangeLike => Predicate.isObjectOrArray(u) && (u as any)._tag === \"TokenRange\"\n\nconst toCode = (u: string | number): number => (Predicate.isNumber(u) ? u : firstCodePoint(u))\n\n/** @internal */\nexport const describeChar = (s: string): string => JSON.stringify(s)\n\nconst describeSet = (u: unknown): string => {\n if (Predicate.isString(u)) {\n const chars = Array.from(u)\n return chars.length === 1 ? describeChar(u) : `one of ${JSON.stringify(u)}`\n }\n if (Predicate.isNumber(u)) return `0x${u.toString(16).padStart(2, \"0\")}`\n if (Predicate.isFunction(u)) return u.name ? u.name : \"a matching token\"\n if (u instanceof Uint8Array) return `one of ${describeBytes(u)}`\n if (isRange(u)) return `${describeSet(u.from)}..=${describeSet(u.to)}`\n if (Array.isArray(u)) return u.map(describeSet).join(\" or \")\n return JSON.stringify(u)\n}\n\n/** Renders bytes as a Rust-style byte string literal: b\"GET\\r\\n\". */\nconst describeBytes = (bytes: Uint8Array): string => {\n let out = 'b\"'\n for (const b of bytes) {\n if (b === 0x22) out += '\\\\\"'\n else if (b === 0x5c) out += \"\\\\\\\\\"\n else if (b === 0x0a) out += \"\\\\n\"\n else if (b === 0x0d) out += \"\\\\r\"\n else if (b === 0x09) out += \"\\\\t\"\n else if (b >= 0x20 && b < 0x7f) out += String.fromCharCode(b)\n else out += `\\\\x${b.toString(16).padStart(2, \"0\")}`\n }\n return out + '\"'\n}\n\nconst compileCode = (u: unknown): ((code: number) => boolean) => {\n if (Predicate.isString(u)) {\n const codes = Array.from(u, firstCodePoint)\n if (codes.length === 1) {\n const c = codes[0]\n return (x) => x === c\n }\n return bitmap(codes)\n }\n if (Predicate.isNumber(u)) return (x) => x === u\n if (Predicate.isFunction(u)) return u as (code: number) => boolean\n if (u instanceof Uint8Array) return bitmap(Array.from(u))\n if (isRange(u)) {\n const from = toCode(u.from)\n const to = toCode(u.to)\n return (x) => x >= from && x <= to\n }\n if (Array.isArray(u)) {\n const tests = u.map(compileCode)\n if (tests.length === 1) return tests[0]\n return (x) => {\n for (let i = 0; i < tests.length; i++) if (tests[i](x)) return true\n return false\n }\n }\n throw new TypeError(`invalid token set: ${String(u)}`)\n}\n\nconst bitmap = (codes: ReadonlyArray<number>): ((code: number) => boolean) => {\n const ascii = new Uint8Array(256)\n let other: Set<number> | undefined\n for (const c of codes) {\n if (c < 256) ascii[c] = 1\n else (other ??= new Set()).add(c)\n }\n if (other === undefined) return (x) => x < 256 && ascii[x] === 1\n const set = other\n return (x) => (x < 256 ? ascii[x] === 1 : set.has(x))\n}\n\nconst compileToken = (u: unknown): ((token: unknown) => boolean) => {\n if (Predicate.isFunction(u)) return u as (token: unknown) => boolean\n if (Array.isArray(u)) {\n const set = new Set(u)\n return (t) => set.has(t)\n }\n return (t) => t === u\n}\n\n/**\n * Marks a predicate over characters that also accepts a code point (a\n * number), so string inputs can call it without allocating a string per\n * character.\n */\nconst CodePointPredicate: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/CodePointPredicate\")\n\n/**\n * The code points accepted by a predicate, as sorted, disjoint, inclusive\n * ranges, when they are known. The compiler turns such predicates into\n * comparisons or tables.\n */\nexport const PredicateRanges: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/PredicateRanges\")\n\n/**\n * Marks `f` as a {@link CodePointPredicate} named `name` (used in error\n * messages), optionally recording the code point ranges it accepts.\n */\nexport const codePointPredicate = <F extends (c: never) => boolean>(\n name: string,\n f: F,\n ranges?: ReadonlyArray<readonly [number, number]>\n): F => {\n Object.defineProperty(f, \"name\", { value: name })\n ;(f as any)[CodePointPredicate] = true\n if (ranges !== undefined) (f as any)[PredicateRanges] = ranges\n return f\n}\n\n/**\n * Compiles a token set. Predicates for string inputs receive the token as a\n * string, so they are wrapped here; `charOf` avoids `String.fromCodePoint`,\n * which is several times slower, for the Basic Multilingual Plane.\n */\nexport const compile = (set: unknown, kind: \"string\" | \"code\" = \"code\"): Matcher => {\n const description = describeSet(set)\n let code: (code: number) => boolean\n if (Predicate.isFunction(set) && kind === \"string\" && !(CodePointPredicate in set)) {\n const pred = set as (c: string) => boolean\n code = (x) => pred(charOf(x))\n } else {\n code = compileCodeSafe(set, kind)\n }\n return { code, token: compileToken(set), description }\n}\n\nconst compileCodeSafe = (set: unknown, kind: \"string\" | \"code\"): ((code: number) => boolean) => {\n if (Array.isArray(set) && kind === \"string\") {\n // Predicates nested in arrays also take strings\n const tests = set.map((s) => compile(s, kind).code)\n return (x) => {\n for (let i = 0; i < tests.length; i++) if (tests[i](x)) return true\n return false\n }\n }\n try {\n return compileCode(set)\n } catch {\n return () => false\n }\n}\n\n/**\n * A matcher that works for every input kind. String predicates receive the\n * token as a string, byte predicates receive numbers.\n */\nexport interface AnyMatcher {\n readonly forString: Matcher\n readonly forCode: Matcher\n readonly description: string\n}\n\n/**\n * A small cache keyed by value, for the parts of a parser that are costly to\n * build and depend only on a string (compiled sets, descriptions). Parsers\n * built inside `Parser.gen` bodies are rebuilt on every run, usually from the\n * same few strings. Dynamic strings could grow it without bound, so it is\n * cleared when full.\n */\nconst cached = <A>(cache: Map<string, A>, key: string, build: (key: string) => A): A => {\n let a = cache.get(key)\n if (a === undefined) {\n if (cache.size >= 1024) cache.clear()\n a = build(key)\n cache.set(key, a)\n }\n return a\n}\n\nconst literalDescriptions = /*#__PURE__*/ new Map<string, string>()\n\n/** Describes a literal for error messages: `\"abc\"`, `b\"abc\"` or `[\"a\", 1]`. */\nexport const describeLiteral = (lit: Input): string =>\n Predicate.isString(lit)\n ? cached(literalDescriptions, lit, describeChar)\n : lit instanceof Uint8Array\n ? describeBytes(lit)\n : `[${lit.map((t) => JSON.stringify(t)).join(\", \")}]`\n\nconst stringMatchers = /*#__PURE__*/ new Map<string, AnyMatcher>()\n\nexport const compileAny = (set: unknown): AnyMatcher =>\n // Matchers for strings are pure functions of the string, so they're shared\n Predicate.isString(set) ? cached(stringMatchers, set, compileAnyImpl) : compileAnyImpl(set)\n\nconst compileAnyImpl = (set: unknown): AnyMatcher => {\n const forCode = compile(set, \"code\")\n const needsString = Predicate.isFunction(set) || Array.isArray(set)\n return {\n forString: needsString ? compile(set, \"string\") : forCode,\n forCode,\n description: forCode.description\n }\n}\n\n/**\n * Whether the string token at `i` is a high surrogate at the very end of a\n * partial input, i.e. the first half of a code point whose second half has\n * not arrived yet.\n */\nexport const splitSurrogate = (state: State<Input>, i: number): boolean => {\n if (!state.partial || i !== state.end - 1) return false\n const c = (state.input as string).charCodeAt(i)\n return c >= 0xd800 && c <= 0xdbff\n}\n\n/** Reads the code point at `i` of a string. */\nconst codePointAt = (s: string, i: number): number => {\n const c = s.charCodeAt(i)\n if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {\n const d = s.charCodeAt(i + 1)\n if (d >= 0xdc00 && d <= 0xdfff) return ((c - 0xd800) << 10) + (d - 0xdc00) + 0x10000\n }\n return c\n}\n\n/**\n * Tests the token at the current position. Returns the token (as the input's\n * token type) and advances, or returns `NoMatch`/`End`.\n */\nexport const NoMatch: unique symbol = /*#__PURE__*/ Symbol(\"NoMatch\")\nexport const End: unique symbol = /*#__PURE__*/ Symbol(\"End\")\n\nexport const nextIf = (state: State<Input>, m: AnyMatcher, negate: boolean): unknown => {\n const pos = state.pos\n if (pos >= state.end) return End\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n if (splitSurrogate(state, pos)) return End\n const cp = codePointAt(s, pos)\n if (m.forString.code(cp) === negate) return NoMatch\n const len = cp > 0xffff ? 2 : 1\n state.pos = pos + len\n return len === 1 ? s[pos] : s.slice(pos, pos + 2)\n }\n case 1: {\n const b = (state.input as Uint8Array)[pos]\n if (m.forCode.code(b) === negate) return NoMatch\n state.pos = pos + 1\n return b\n }\n default: {\n const t = (state.input as ReadonlyArray<unknown>)[pos]\n if (m.forCode.token(t) === negate) return NoMatch\n state.pos = pos + 1\n return t\n }\n }\n}\n\n/**\n * Scans forward while tokens satisfy the matcher (xor `negate`), up to `max`\n * tokens. Returns the offset reached, the number of tokens matched, and\n * whether the end of input was hit before `max` tokens.\n */\nexport const scan = (\n state: State<Input>,\n m: AnyMatcher,\n negate: boolean,\n max: number\n): { readonly offset: number; readonly count: number; readonly hitEnd: boolean } => {\n let i = state.pos\n const end = state.end\n let count = 0\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n const test = m.forString.code\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n const c = s.charCodeAt(i)\n if (c < 0xd800 || c > 0xdbff) {\n if (test(c) === negate) break\n i++\n } else {\n if (splitSurrogate(state, i)) return { offset: i, count, hitEnd: true }\n const cp = codePointAt(s, i)\n if (test(cp) === negate) break\n i += cp > 0xffff ? 2 : 1\n }\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n case 1: {\n const bytes = state.input as Uint8Array\n const test = m.forCode.code\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n if (test(bytes[i]) === negate) break\n i++\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n default: {\n const arr = state.input as ReadonlyArray<unknown>\n const test = m.forCode.token\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n if (test(arr[i]) === negate) break\n i++\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n }\n}\n\n/** Advances `n` tokens from `start`, returning the offset reached and the count. */\nexport const advance = (state: State<Input>, start: number, n: number): { offset: number; count: number } => {\n if (state.inputKind !== 0) {\n const offset = Math.min(state.end, start + n)\n return { offset, count: offset - start }\n }\n const s = state.input as string\n let i = start\n let count = 0\n while (count < n && i < state.end) {\n if (splitSurrogate(state, i)) break\n const c = s.charCodeAt(i)\n i += c >= 0xd800 && c <= 0xdbff && i + 1 < s.length && (s.charCodeAt(i + 1) & 0xfc00) === 0xdc00 ? 2 : 1\n count++\n }\n return { offset: i, count }\n}\n\n/** Counts tokens between two offsets. */\nexport const countTokens = (state: State<Input>, start: number, end: number): number => {\n if (state.inputKind !== 0) return end - start\n const s = state.input as string\n let count = 0\n for (let i = start; i < end; i++) {\n const c = s.charCodeAt(i)\n if (c >= 0xd800 && c <= 0xdbff && i + 1 < end && (s.charCodeAt(i + 1) & 0xfc00) === 0xdc00) i++\n count++\n }\n return count\n}\n\n/** Finds `needle` within `[from, state.end)`; returns -1 if absent. */\nexport const indexOf = (state: State<Input>, needle: Input, from: number): number => {\n // The whole needle must lie before `state.end`, which may narrow the input\n const last = state.end - needle.length\n if (from > last) return -1\n switch (state.inputKind) {\n case 0: {\n const i = (state.input as string).indexOf(needle as string, from)\n return i <= last ? i : -1\n }\n case 1: {\n const hay = state.input as Uint8Array\n const n = needle as Uint8Array\n if (n.length === 0) return from\n const first = n[0]\n let i = hay.indexOf(first, from)\n while (i !== -1 && i <= last) {\n let j = 1\n while (j < n.length && hay[i + j] === n[j]) j++\n if (j === n.length) return i\n i = hay.indexOf(first, i + 1)\n }\n return -1\n }\n default: {\n const hay = state.input as ReadonlyArray<unknown>\n const n = needle as ReadonlyArray<unknown>\n outer: for (let i = from; i <= last; i++) {\n for (let j = 0; j < n.length; j++) if (hay[i + j] !== n[j]) continue outer\n return i\n }\n return -1\n }\n }\n}\n\n/**\n * Compares the input at the current position with a literal. Returns the\n * number of matching units if the literal matches completely (its length),\n * -1 on mismatch, or -2 when the input ends while matching a prefix.\n */\nexport const matchLiteral = (state: State<Input>, lit: Input, caseless: boolean): number => {\n const r = match(state, lit, caseless)\n // A failed match may have read up to its length past where it fails (see `Syntax`)\n if (r < 0 && state.syntax !== undefined) state.syntax.read(state.pos + Math.min(lit.length, state.end - state.pos))\n return r\n}\n\nconst match = (state: State<Input>, lit: Input, caseless: boolean): number => {\n const pos = state.pos\n const len = lit.length\n const avail = state.end - pos\n const n = Math.min(len, avail)\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n const l = lit as string\n if (!caseless) {\n if (avail >= len) return s.startsWith(l, pos) ? len : -1\n return l.startsWith(s.slice(pos)) ? -2 : -1\n }\n for (let i = 0; i < n; i++) {\n if (lowerAscii(s.charCodeAt(pos + i)) !== lowerAscii(l.charCodeAt(i))) return -1\n }\n return n === len ? len : -2\n }\n case 1: {\n const b = state.input as Uint8Array\n const l = lit as Uint8Array\n for (let i = 0; i < n; i++) {\n const x = b[pos + i]\n const y = l[i]\n if (caseless ? lowerAscii(x) !== lowerAscii(y) : x !== y) return -1\n }\n return n === len ? len : -2\n }\n default: {\n const a = state.input as ReadonlyArray<unknown>\n const l = lit as ReadonlyArray<unknown>\n for (let i = 0; i < n; i++) if (a[pos + i] !== l[i]) return -1\n return n === len ? len : -2\n }\n }\n}\n\nconst lowerAscii = (c: number): number => (c >= 65 && c <= 90 ? c + 32 : c)\n"],"mappings":";;;AAuBA,MAAM,WAAW,MAAoC,UAAU,gBAAgB,CAAC,KAAM,EAAU,SAAS;AAEzG,MAAM,UAAU,MAAgC,UAAU,SAAS,CAAC,IAAI,IAAI,eAAe,CAAC;;AAG5F,MAAa,gBAAgB,MAAsB,KAAK,UAAU,CAAC;AAEnE,MAAM,eAAe,MAAuB;CAC1C,IAAI,UAAU,SAAS,CAAC,GAEtB,OADc,MAAM,KAAK,CACd,CAAC,CAAC,WAAW,IAAI,aAAa,CAAC,IAAI,UAAU,KAAK,UAAU,CAAC;CAE1E,IAAI,UAAU,SAAS,CAAC,GAAG,OAAO,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG;CACrE,IAAI,UAAU,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,MAAM;IACtD,OAAO,EAAE,WAAW,EAAE,MAAM,GAAG,CAAC,IAAI,KAAK;GAC3C;GACA,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KACrB,IAAI,WAAW,EAAE,WAAW,MAAM,CAAC,CAAC,MAAM,WAAW,EAAE,WAAW,CAAC,CAAC,GAAG,OAAO;GAEhF,OAAO,MAAM,MAAM,MAAM;EAC3B;EACA,KAAK,GAAG;GACN,MAAM,IAAI,MAAM;GAChB,MAAM,IAAI;GACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;IAC1B,MAAM,IAAI,EAAE,MAAM;IAClB,MAAM,IAAI,EAAE;IACZ,IAAI,WAAW,WAAW,CAAC,MAAM,WAAW,CAAC,IAAI,MAAM,GAAG,OAAO;GACnE;GACA,OAAO,MAAM,MAAM,MAAM;EAC3B;EACA,SAAS;GACP,MAAM,IAAI,MAAM;GAChB,MAAM,IAAI;GACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,IAAI,EAAE,MAAM,OAAO,EAAE,IAAI,OAAO;GAC5D,OAAO,MAAM,MAAM,MAAM;EAC3B;CACF;AACF;AAEA,MAAM,cAAc,MAAuB,KAAK,MAAM,KAAK,KAAK,IAAI,KAAK"}
|
|
1
|
+
{"version":3,"file":"tokens.js","names":[],"sources":["../../src/internal/tokens.ts"],"sourcesContent":["// fallow-ignore-file duplicate-export -- `End` and `scan` here are the token-set versions of the text\n// helpers in ./text.ts; both are internal modules, imported by path, never re-exported from one barrel.\nimport * as Predicate from \"effect/Predicate\"\nimport * as Result from \"effect/Result\"\nimport type { Input, State } from \"../Input.js\"\nimport { charOf, firstCodePoint } from \"./text.js\"\n\n/** A compiled token set. */\nexport interface Matcher {\n /** Tests a code point (string input) or a byte (Uint8Array input). */\n readonly code: (code: number) => boolean\n /** Tests an element of an array input. */\n readonly token: (token: unknown) => boolean\n /** Human readable description, used in error messages. */\n readonly description: string\n}\n\n/** @internal */\nexport interface TokenRangeLike {\n readonly _tag: \"TokenRange\"\n readonly from: string | number\n readonly to: string | number\n}\n\nconst isRange = (u: unknown): u is TokenRangeLike => Predicate.isObjectOrArray(u) && (u as any)._tag === \"TokenRange\"\n\nconst toCode = (u: string | number): number => (Predicate.isNumber(u) ? u : firstCodePoint(u))\n\n/** @internal */\nexport const describeChar = (s: string): string => JSON.stringify(s)\n\nconst describeSet = (u: unknown): string => {\n if (Predicate.isString(u)) {\n const chars = Array.from(u)\n return chars.length === 1 ? describeChar(u) : `one of ${JSON.stringify(u)}`\n }\n if (Predicate.isNumber(u)) return `0x${u.toString(16).padStart(2, \"0\")}`\n if (Predicate.isFunction(u)) return u.name ? u.name : \"a matching token\"\n if (u instanceof Uint8Array) return `one of ${describeBytes(u)}`\n if (isRange(u)) return `${describeSet(u.from)}..=${describeSet(u.to)}`\n if (Array.isArray(u)) return u.map(describeSet).join(\" or \")\n return JSON.stringify(u)\n}\n\n/** Renders bytes as a Rust-style byte string literal: b\"GET\\r\\n\". */\nconst describeBytes = (bytes: Uint8Array): string => {\n let out = 'b\"'\n for (const b of bytes) {\n if (b === 0x22) out += '\\\\\"'\n else if (b === 0x5c) out += \"\\\\\\\\\"\n else if (b === 0x0a) out += \"\\\\n\"\n else if (b === 0x0d) out += \"\\\\r\"\n else if (b === 0x09) out += \"\\\\t\"\n else if (b >= 0x20 && b < 0x7f) out += String.fromCharCode(b)\n else out += `\\\\x${b.toString(16).padStart(2, \"0\")}`\n }\n return out + '\"'\n}\n\nconst compileCode = (u: unknown): ((code: number) => boolean) => {\n if (Predicate.isString(u)) {\n const codes = Array.from(u, firstCodePoint)\n if (codes.length === 1) {\n const c = codes[0]\n return (x) => x === c\n }\n return bitmap(codes)\n }\n if (Predicate.isNumber(u)) return (x) => x === u\n if (Predicate.isFunction(u)) return u as (code: number) => boolean\n if (u instanceof Uint8Array) return bitmap(Array.from(u))\n if (isRange(u)) {\n const from = toCode(u.from)\n const to = toCode(u.to)\n return (x) => x >= from && x <= to\n }\n if (Array.isArray(u)) {\n const tests = u.map(compileCode)\n if (tests.length === 1) return tests[0]\n return (x) => {\n for (let i = 0; i < tests.length; i++) if (tests[i](x)) return true\n return false\n }\n }\n throw new TypeError(`invalid token set: ${String(u)}`)\n}\n\nconst bitmap = (codes: ReadonlyArray<number>): ((code: number) => boolean) => {\n const ascii = new Uint8Array(256)\n let other: Set<number> | undefined\n for (const c of codes) {\n if (c < 256) ascii[c] = 1\n else (other ??= new Set()).add(c)\n }\n if (other === undefined) return (x) => x < 256 && ascii[x] === 1\n const set = other\n return (x) => (x < 256 ? ascii[x] === 1 : set.has(x))\n}\n\nconst compileToken = (u: unknown): ((token: unknown) => boolean) => {\n if (Predicate.isFunction(u)) return u as (token: unknown) => boolean\n if (Array.isArray(u)) {\n const set = new Set(u)\n return (t) => set.has(t)\n }\n return (t) => t === u\n}\n\n/**\n * Marks a predicate over characters that also accepts a code point (a\n * number), so string inputs can call it without allocating a string per\n * character.\n */\nconst CodePointPredicate: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/CodePointPredicate\")\n\n/**\n * The code points accepted by a predicate, as sorted, disjoint, inclusive\n * ranges, when they are known. The compiler turns such predicates into\n * comparisons or tables.\n */\nexport const PredicateRanges: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/PredicateRanges\")\n\n/**\n * Marks `f` as a {@link CodePointPredicate} named `name` (used in error\n * messages), optionally recording the code point ranges it accepts.\n */\nexport const codePointPredicate = <F extends (c: never) => boolean>(\n name: string,\n f: F,\n ranges?: ReadonlyArray<readonly [number, number]>\n): F => {\n Object.defineProperty(f, \"name\", { value: name })\n ;(f as any)[CodePointPredicate] = true\n if (ranges !== undefined) (f as any)[PredicateRanges] = ranges\n return f\n}\n\n/**\n * Compiles a token set. Predicates for string inputs receive the token as a\n * string, so they are wrapped here; `charOf` avoids `String.fromCodePoint`,\n * which is several times slower, for the Basic Multilingual Plane.\n */\nexport const compile = (set: unknown, kind: \"string\" | \"code\" = \"code\"): Matcher => {\n const description = describeSet(set)\n let code: (code: number) => boolean\n if (Predicate.isFunction(set) && kind === \"string\" && !(CodePointPredicate in set)) {\n const pred = set as (c: string) => boolean\n code = (x) => pred(charOf(x))\n } else {\n code = compileCodeSafe(set, kind)\n }\n return { code, token: compileToken(set), description }\n}\n\nconst compileCodeSafe = (set: unknown, kind: \"string\" | \"code\"): ((code: number) => boolean) => {\n if (Array.isArray(set) && kind === \"string\") {\n // Predicates nested in arrays also take strings\n const tests = set.map((s) => compile(s, kind).code)\n return (x) => {\n for (let i = 0; i < tests.length; i++) if (tests[i](x)) return true\n return false\n }\n }\n return Result.getOrElse(\n Result.try(() => compileCode(set)),\n (): ((code: number) => boolean) => () => false\n )\n}\n\n/**\n * A matcher that works for every input kind. String predicates receive the\n * token as a string, byte predicates receive numbers.\n */\nexport interface AnyMatcher {\n readonly forString: Matcher\n readonly forCode: Matcher\n readonly description: string\n}\n\n/**\n * A small cache keyed by value, for the parts of a parser that are costly to\n * build and depend only on a string (compiled sets, descriptions). Parsers\n * built inside `Parser.gen` bodies are rebuilt on every run, usually from the\n * same few strings. Dynamic strings could grow it without bound, so it is\n * cleared when full.\n */\nconst cached = <A>(cache: Map<string, A>, key: string, build: (key: string) => A): A => {\n let a = cache.get(key)\n if (a === undefined) {\n if (cache.size >= 1024) cache.clear()\n a = build(key)\n cache.set(key, a)\n }\n return a\n}\n\nconst literalDescriptions = /*#__PURE__*/ new Map<string, string>()\n\n/** Describes a literal for error messages: `\"abc\"`, `b\"abc\"` or `[\"a\", 1]`. */\nexport const describeLiteral = (lit: Input): string =>\n Predicate.isString(lit)\n ? cached(literalDescriptions, lit, describeChar)\n : lit instanceof Uint8Array\n ? describeBytes(lit)\n : `[${lit.map((t) => JSON.stringify(t)).join(\", \")}]`\n\nconst stringMatchers = /*#__PURE__*/ new Map<string, AnyMatcher>()\n\nexport const compileAny = (set: unknown): AnyMatcher =>\n // Matchers for strings are pure functions of the string, so they're shared\n Predicate.isString(set) ? cached(stringMatchers, set, compileAnyImpl) : compileAnyImpl(set)\n\nconst compileAnyImpl = (set: unknown): AnyMatcher => {\n const forCode = compile(set, \"code\")\n const needsString = Predicate.isFunction(set) || Array.isArray(set)\n return {\n forString: needsString ? compile(set, \"string\") : forCode,\n forCode,\n description: forCode.description\n }\n}\n\n/**\n * Whether the string token at `i` is a high surrogate at the very end of a\n * partial input, i.e. the first half of a code point whose second half has\n * not arrived yet.\n */\nexport const splitSurrogate = (state: State<Input>, i: number): boolean => {\n if (!state.partial || i !== state.end - 1) return false\n const c = (state.input as string).charCodeAt(i)\n return c >= 0xd800 && c <= 0xdbff\n}\n\n/** Reads the code point at `i` of a string. */\nconst codePointAt = (s: string, i: number): number => {\n const c = s.charCodeAt(i)\n if (c >= 0xd800 && c <= 0xdbff && i + 1 < s.length) {\n const d = s.charCodeAt(i + 1)\n if (d >= 0xdc00 && d <= 0xdfff) return ((c - 0xd800) << 10) + (d - 0xdc00) + 0x10000\n }\n return c\n}\n\n/**\n * Tests the token at the current position. Returns the token (as the input's\n * token type) and advances, or returns `NoMatch`/`End`.\n */\nexport const NoMatch: unique symbol = /*#__PURE__*/ Symbol(\"NoMatch\")\nexport const End: unique symbol = /*#__PURE__*/ Symbol(\"End\")\n\nexport const nextIf = (state: State<Input>, m: AnyMatcher, negate: boolean): unknown => {\n const pos = state.pos\n if (pos >= state.end) return End\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n if (splitSurrogate(state, pos)) return End\n const cp = codePointAt(s, pos)\n if (m.forString.code(cp) === negate) return NoMatch\n const len = cp > 0xffff ? 2 : 1\n state.pos = pos + len\n return len === 1 ? s[pos] : s.slice(pos, pos + 2)\n }\n case 1: {\n const b = (state.input as Uint8Array)[pos]\n if (m.forCode.code(b) === negate) return NoMatch\n state.pos = pos + 1\n return b\n }\n default: {\n const t = (state.input as ReadonlyArray<unknown>)[pos]\n if (m.forCode.token(t) === negate) return NoMatch\n state.pos = pos + 1\n return t\n }\n }\n}\n\n/** How far a {@link scan} got. */\nexport interface Scanned extends Advanced {\n /** Whether the end of input was hit before `max` tokens. */\n readonly hitEnd: boolean\n}\n\n/**\n * Scans forward while tokens satisfy the matcher (xor `negate`), up to `max`\n * tokens. Returns the offset reached, the number of tokens matched, and\n * whether the end of input was hit before `max` tokens.\n */\nexport const scan = (state: State<Input>, m: AnyMatcher, negate: boolean, max: number): Scanned => {\n let i = state.pos\n const end = state.end\n let count = 0\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n const test = m.forString.code\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n const c = s.charCodeAt(i)\n if (c < 0xd800 || c > 0xdbff) {\n if (test(c) === negate) break\n i++\n } else {\n if (splitSurrogate(state, i)) return { offset: i, count, hitEnd: true }\n const cp = codePointAt(s, i)\n if (test(cp) === negate) break\n i += cp > 0xffff ? 2 : 1\n }\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n case 1: {\n const bytes = state.input as Uint8Array\n const test = m.forCode.code\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n if (test(bytes[i]) === negate) break\n i++\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n default: {\n const arr = state.input as ReadonlyArray<unknown>\n const test = m.forCode.token\n while (count < max) {\n if (i >= end) return { offset: i, count, hitEnd: true }\n if (test(arr[i]) === negate) break\n i++\n count++\n }\n return { offset: i, count, hitEnd: false }\n }\n }\n}\n\n/** The offset reached and the number of tokens passed. */\nexport interface Advanced {\n readonly offset: number\n readonly count: number\n}\n\n/** Advances `n` tokens from `start`, returning the offset reached and the count. */\nexport const advance = (state: State<Input>, start: number, n: number): Advanced => {\n if (state.inputKind !== 0) {\n const offset = Math.min(state.end, start + n)\n return { offset, count: offset - start }\n }\n const s = state.input as string\n let i = start\n let count = 0\n while (count < n && i < state.end) {\n if (splitSurrogate(state, i)) break\n const c = s.charCodeAt(i)\n i += c >= 0xd800 && c <= 0xdbff && i + 1 < s.length && (s.charCodeAt(i + 1) & 0xfc00) === 0xdc00 ? 2 : 1\n count++\n }\n return { offset: i, count }\n}\n\n/** Counts tokens between two offsets. */\nexport const countTokens = (state: State<Input>, start: number, end: number): number => {\n if (state.inputKind !== 0) return end - start\n const s = state.input as string\n let count = 0\n for (let i = start; i < end; i++) {\n const c = s.charCodeAt(i)\n if (c >= 0xd800 && c <= 0xdbff && i + 1 < end && (s.charCodeAt(i + 1) & 0xfc00) === 0xdc00) i++\n count++\n }\n return count\n}\n\n/** Finds `needle` within `[from, state.end)`; returns -1 if absent. */\nexport const indexOf = (state: State<Input>, needle: Input, from: number): number => {\n // The whole needle must lie before `state.end`, which may narrow the input\n const last = state.end - needle.length\n if (from > last) return -1\n switch (state.inputKind) {\n case 0: {\n const i = (state.input as string).indexOf(needle as string, from)\n return i <= last ? i : -1\n }\n case 1: {\n const hay = state.input as Uint8Array\n const n = needle as Uint8Array\n if (n.byteLength === 0) return from\n const first = n[0]\n let i = hay.indexOf(first, from)\n while (i !== -1 && i <= last) {\n let j = 1\n while (j < n.length && hay[i + j] === n[j]) j++\n if (j === n.length) return i\n i = hay.indexOf(first, i + 1)\n }\n return -1\n }\n default: {\n const hay = state.input as ReadonlyArray<unknown>\n const n = needle as ReadonlyArray<unknown>\n outer: for (let i = from; i <= last; i++) {\n for (let j = 0; j < n.length; j++) if (hay[i + j] !== n[j]) continue outer\n return i\n }\n return -1\n }\n }\n}\n\n/**\n * Compares the input at the current position with a literal. Returns the\n * number of matching units if the literal matches completely (its length),\n * -1 on mismatch, or -2 when the input ends while matching a prefix.\n */\nexport const matchLiteral = (state: State<Input>, lit: Input, caseless: boolean): number => {\n const r = match(state, lit, caseless)\n // A failed match may have read up to its length past where it fails (see `Syntax`)\n if (r < 0 && state.syntax !== undefined) state.syntax.read(state.pos + Math.min(lit.length, state.end - state.pos))\n return r\n}\n\nconst match = (state: State<Input>, lit: Input, caseless: boolean): number => {\n const pos = state.pos\n const len = lit.length\n const avail = state.end - pos\n const n = Math.min(len, avail)\n switch (state.inputKind) {\n case 0: {\n const s = state.input as string\n const l = lit as string\n if (!caseless) {\n if (avail >= len) return s.startsWith(l, pos) ? len : -1\n return l.startsWith(s.slice(pos)) ? -2 : -1\n }\n for (let i = 0; i < n; i++) {\n if (lowerAscii(s.charCodeAt(pos + i)) !== lowerAscii(l.charCodeAt(i))) return -1\n }\n return n === len ? len : -2\n }\n case 1: {\n const b = state.input as Uint8Array\n const l = lit as Uint8Array\n for (let i = 0; i < n; i++) {\n const x = b[pos + i]\n const y = l[i]\n if (caseless ? lowerAscii(x) !== lowerAscii(y) : x !== y) return -1\n }\n return n === len ? len : -2\n }\n default: {\n const a = state.input as ReadonlyArray<unknown>\n const l = lit as ReadonlyArray<unknown>\n for (let i = 0; i < n; i++) if (a[pos + i] !== l[i]) return -1\n return n === len ? len : -2\n }\n }\n}\n\nconst lowerAscii = (c: number): number => (c >= 65 && c <= 90 ? c + 32 : c)\n"],"mappings":";;;;AAwBA,MAAM,WAAW,MAAoC,UAAU,gBAAgB,CAAC,KAAM,EAAU,SAAS;AAEzG,MAAM,UAAU,MAAgC,UAAU,SAAS,CAAC,IAAI,IAAI,eAAe,CAAC;;AAG5F,MAAa,gBAAgB,MAAsB,KAAK,UAAU,CAAC;AAEnE,MAAM,eAAe,MAAuB;CAC1C,IAAI,UAAU,SAAS,CAAC,GAEtB,OADc,MAAM,KAAK,CACd,CAAC,CAAC,WAAW,IAAI,aAAa,CAAC,IAAI,UAAU,KAAK,UAAU,CAAC;CAE1E,IAAI,UAAU,SAAS,CAAC,GAAG,OAAO,KAAK,EAAE,SAAS,EAAE,CAAC,CAAC,SAAS,GAAG,GAAG;CACrE,IAAI,UAAU,WAAW,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,OAAO;CACtD,IAAI,aAAa,YAAY,OAAO,UAAU,cAAc,CAAC;CAC7D,IAAI,QAAQ,CAAC,GAAG,OAAO,GAAG,YAAY,EAAE,IAAI,EAAE,KAAK,YAAY,EAAE,EAAE;CACnE,IAAI,MAAM,QAAQ,CAAC,GAAG,OAAO,EAA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MAAM,MAAM,MAAM;EAC3B;EACA,KAAK,GAAG;GACN,MAAM,IAAI,MAAM;GAChB,MAAM,IAAI;GACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK;IAC1B,MAAM,IAAI,EAAE,MAAM;IAClB,MAAM,IAAI,EAAE;IACZ,IAAI,WAAW,WAAW,CAAC,MAAM,WAAW,CAAC,IAAI,MAAM,GAAG,OAAO;GACnE;GACA,OAAO,MAAM,MAAM,MAAM;EAC3B;EACA,SAAS;GACP,MAAM,IAAI,MAAM;GAChB,MAAM,IAAI;GACV,KAAK,IAAI,IAAI,GAAG,IAAI,GAAG,KAAK,IAAI,EAAE,MAAM,OAAO,EAAE,IAAI,OAAO;GAC5D,OAAO,MAAM,MAAM,MAAM;EAC3B;CACF;AACF;AAEA,MAAM,cAAc,MAAuB,KAAK,MAAM,KAAK,KAAK,IAAI,KAAK"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"$schema": "https://www.schemastore.org/package",
|
|
3
3
|
"name": "@thomasfosterau/effect-parse",
|
|
4
|
-
"version": "0.
|
|
4
|
+
"version": "0.4.0",
|
|
5
5
|
"description": "Parser combinators for Effect, ported from Rust's nom and winnow: text, bytes, bits and token streams, streaming input, compiled parsers and Schema integration",
|
|
6
6
|
"keywords": [
|
|
7
7
|
"effect",
|
|
@@ -102,17 +102,17 @@
|
|
|
102
102
|
"@changesets/changelog-github": "^1.0.1",
|
|
103
103
|
"@changesets/cli": "^3.0.3",
|
|
104
104
|
"@effect/language-service": "^0.87.5",
|
|
105
|
-
"@effect/tsgo": "0.
|
|
105
|
+
"@effect/tsgo": "0.63.0",
|
|
106
106
|
"@mpsuesser/oxlint-plugin-effect": "^0.6.0",
|
|
107
107
|
"@types/node": "^26.6.5",
|
|
108
108
|
"@typescript/native": "npm:typescript@7.0.2",
|
|
109
109
|
"@vitest/coverage-istanbul": "5.0.5",
|
|
110
|
-
"effect": "4.0
|
|
111
|
-
"fallow": "3.
|
|
110
|
+
"effect": "4.1.0",
|
|
111
|
+
"fallow": "3.37.0",
|
|
112
112
|
"rolldown": "^1.2.14",
|
|
113
113
|
"typescript": "npm:@typescript/typescript6@6.0.2",
|
|
114
|
-
"vite": "npm:@voidzero-dev/vite-plus-core@1.
|
|
115
|
-
"vite-plus": "1.
|
|
114
|
+
"vite": "npm:@voidzero-dev/vite-plus-core@1.4.0",
|
|
115
|
+
"vite-plus": "1.4.0",
|
|
116
116
|
"vitest": "5.0.5"
|
|
117
117
|
},
|
|
118
118
|
"peerDependencies": {
|
package/src/Ascii.ts
CHANGED
|
@@ -30,6 +30,7 @@ import type { ErrorKind } from "./ParseError.js"
|
|
|
30
30
|
import type { Parser } from "./Parser.js"
|
|
31
31
|
import * as P from "./Parser.js"
|
|
32
32
|
import * as numbers from "./internal/numbers.js"
|
|
33
|
+
import { Key } from "./internal/key.js"
|
|
33
34
|
import * as Resume from "./internal/resume.js"
|
|
34
35
|
import { codePointPredicate } from "./internal/tokens.js"
|
|
35
36
|
import { literalImpl, range, takeWhileImpl } from "./Token.js"
|
|
@@ -570,7 +571,7 @@ const isIncomplete = (state: State<any>): boolean => state.mode === "Incomplete"
|
|
|
570
571
|
* Fails with the current (`Incomplete`) failure, recording a checkpoint
|
|
571
572
|
* first in a streaming parse.
|
|
572
573
|
*/
|
|
573
|
-
const suspendAt = (state: State<any>, key:
|
|
574
|
+
const suspendAt = (state: State<any>, key: Key, start: number, checkpoint: unknown): Fail => {
|
|
574
575
|
if (state.resume !== undefined) Resume.save(state, key, start, checkpoint)
|
|
575
576
|
return Fail
|
|
576
577
|
}
|
|
@@ -586,7 +587,7 @@ export const takeEscapedImpl = <A, E, R, I extends Input, B, E2, R2, I2 extends
|
|
|
586
587
|
): Parser<S, E | E2, R | R2, I & I2> => {
|
|
587
588
|
const normalStep = normal.step
|
|
588
589
|
const escStep = escapable.step
|
|
589
|
-
const key =
|
|
590
|
+
const key = new Key()
|
|
590
591
|
return P.described(
|
|
591
592
|
(state: State<any>) => {
|
|
592
593
|
const start = state.pos
|
|
@@ -612,7 +613,7 @@ export const takeEscapedImpl = <A, E, R, I extends Input, B, E2, R2, I2 extends
|
|
|
612
613
|
}
|
|
613
614
|
if (state.mode === "Incomplete") return suspendAt(state, key, start, before)
|
|
614
615
|
if (state.mode !== "Backtrack") return Fail
|
|
615
|
-
state.
|
|
616
|
+
state.rewind(before)
|
|
616
617
|
if (matchControl(state)) {
|
|
617
618
|
if (escStep(state) === Fail) return isIncomplete(state) ? suspendAt(state, key, start, before) : Fail
|
|
618
619
|
} else {
|
|
@@ -693,7 +694,7 @@ export const escapedImpl = <E, R, I extends Input, E2, R2, I2 extends Input>(
|
|
|
693
694
|
): Parser<string, E | E2, R | R2, I & I2> => {
|
|
694
695
|
const normalStep = normal.step
|
|
695
696
|
const escStep = escape.step
|
|
696
|
-
const key =
|
|
697
|
+
const key = new Key()
|
|
697
698
|
return P.described(
|
|
698
699
|
(state: State<any>) => {
|
|
699
700
|
const start = state.pos
|
|
@@ -723,7 +724,7 @@ export const escapedImpl = <E, R, I extends Input, E2, R2, I2 extends Input>(
|
|
|
723
724
|
} else {
|
|
724
725
|
if (state.mode === "Incomplete") return suspendAt(state, key, start, { out, pos: before })
|
|
725
726
|
if (state.mode !== "Backtrack") return Fail
|
|
726
|
-
state.
|
|
727
|
+
state.rewind(before)
|
|
727
728
|
}
|
|
728
729
|
if (matchControl(state)) {
|
|
729
730
|
const b = escStep(state)
|
package/src/Bits.ts
CHANGED
|
@@ -176,7 +176,7 @@ export const pattern = (pattern: number, count: number): Parser<number, never, n
|
|
|
176
176
|
if (avail < count) return state.eoi("Bits", expected, count - avail)
|
|
177
177
|
const value = readBits(state, count)
|
|
178
178
|
if (value !== pattern) {
|
|
179
|
-
state.
|
|
179
|
+
state.rewind(start)
|
|
180
180
|
return state.backtrack("Bits", expected)
|
|
181
181
|
}
|
|
182
182
|
return value
|
package/src/Expression.ts
CHANGED
|
@@ -34,11 +34,12 @@
|
|
|
34
34
|
*
|
|
35
35
|
* @since 0.1.0
|
|
36
36
|
*/
|
|
37
|
+
import * as Predicate from "effect/Predicate"
|
|
37
38
|
import type { Input, State } from "./Input.js"
|
|
38
|
-
import { Fail
|
|
39
|
-
import type { Failure } from "./Input.js"
|
|
39
|
+
import { Fail } from "./Input.js"
|
|
40
40
|
import type { Parser } from "./Parser.js"
|
|
41
41
|
import { make as makeParser } from "./Parser.js"
|
|
42
|
+
import { TiedFailures } from "./internal/ties.js"
|
|
42
43
|
|
|
43
44
|
/**
|
|
44
45
|
* An operator in an expression grammar.
|
|
@@ -154,15 +155,17 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
154
155
|
readonly operators: Ops
|
|
155
156
|
}): Parser<A, E | OpError<Ops[number]>, R | OpServices<Ops[number]>, I & OpInput<Ops[number]>> => {
|
|
156
157
|
const operand = options.operand.step
|
|
157
|
-
const prefixes = options.operators.filter((o): o is Prefix<A, any, any, any> => o
|
|
158
|
-
const postfixes = options.operators.filter((o): o is Postfix<A, any, any, any> => o
|
|
159
|
-
const infixes = options.operators.filter((o): o is Infix<A, any, any, any> => o
|
|
158
|
+
const prefixes = options.operators.filter((o): o is Prefix<A, any, any, any> => Predicate.isTagged(o, "Prefix"))
|
|
159
|
+
const postfixes = options.operators.filter((o): o is Postfix<A, any, any, any> => Predicate.isTagged(o, "Postfix"))
|
|
160
|
+
const infixes = options.operators.filter((o): o is Infix<A, any, any, any> => Predicate.isTagged(o, "Infix"))
|
|
160
161
|
|
|
161
162
|
// Attempts each operator in turn. Returns the index of the first match
|
|
162
163
|
// (with its output in `matched`), -1 if none matched, or -2 on a fatal
|
|
163
|
-
// failure.
|
|
164
|
+
// failure. Failed attempts are folded into `failures`, merged only when
|
|
165
|
+
// read (#44): a round in which an operator matches discards them unmerged.
|
|
166
|
+
// Like `matched`, it is shared by nested expressions, which clear it.
|
|
164
167
|
let matched: unknown
|
|
165
|
-
|
|
168
|
+
const failures = new TiedFailures()
|
|
166
169
|
const tryOps = (state: State<any>, ops: ReadonlyArray<OperatorBase<any, any, any>>, mustConsume: boolean): number => {
|
|
167
170
|
const start = state.pos
|
|
168
171
|
for (let i = 0; i < ops.length; i++) {
|
|
@@ -177,15 +180,15 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
177
180
|
return i
|
|
178
181
|
}
|
|
179
182
|
if (state.mode !== "Backtrack") return -2
|
|
180
|
-
|
|
181
|
-
state
|
|
183
|
+
state.rewind(start)
|
|
184
|
+
failures.add(state)
|
|
182
185
|
}
|
|
183
186
|
return -1
|
|
184
187
|
}
|
|
185
188
|
|
|
186
189
|
const expr = (state: State<any>, minPower: number): A | Fail => {
|
|
187
190
|
let lhs: A | Fail
|
|
188
|
-
|
|
191
|
+
failures.clear()
|
|
189
192
|
const p = tryOps(state, prefixes, true)
|
|
190
193
|
if (p === -2) return Fail
|
|
191
194
|
if (p >= 0) {
|
|
@@ -195,11 +198,18 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
195
198
|
if (operandValue === Fail) return Fail
|
|
196
199
|
lhs = op.fold(opOut, operandValue)
|
|
197
200
|
} else {
|
|
198
|
-
const prefixFailure = failure
|
|
201
|
+
const prefixFailure = failures.failure
|
|
202
|
+
const prefixTie1 = failures.tie1
|
|
203
|
+
const prefixTie2 = failures.tie2
|
|
199
204
|
lhs = operand(state)
|
|
200
205
|
if (lhs === Fail) {
|
|
201
206
|
if (prefixFailure !== undefined && state.mode === "Backtrack") {
|
|
202
|
-
|
|
207
|
+
// The operand may have parsed nested expressions, which reuse `failures`
|
|
208
|
+
failures.failure = prefixFailure
|
|
209
|
+
failures.tie1 = prefixTie1
|
|
210
|
+
failures.tie2 = prefixTie2
|
|
211
|
+
failures.add(state)
|
|
212
|
+
failures.fail(state)
|
|
203
213
|
}
|
|
204
214
|
return Fail
|
|
205
215
|
}
|
|
@@ -207,35 +217,29 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
207
217
|
|
|
208
218
|
while (true) {
|
|
209
219
|
const start = state.pos
|
|
210
|
-
|
|
220
|
+
failures.clear()
|
|
211
221
|
const q = tryOps(state, postfixes, true)
|
|
212
222
|
if (q === -2) return Fail
|
|
213
223
|
if (q >= 0) {
|
|
214
224
|
const op = postfixes[q]
|
|
215
225
|
if (op.precedence * 2 < minPower) {
|
|
216
|
-
state.
|
|
226
|
+
state.rewind(start)
|
|
217
227
|
break
|
|
218
228
|
}
|
|
219
229
|
lhs = op.fold(lhs, matched)
|
|
220
230
|
continue
|
|
221
231
|
}
|
|
222
|
-
|
|
232
|
+
// The infix attempts are folded on top of the postfix ones
|
|
223
233
|
const i = tryOps(state, infixes, false)
|
|
224
234
|
if (i === -2) return Fail
|
|
225
235
|
if (i < 0) {
|
|
226
|
-
|
|
227
|
-
postfixFailure === undefined
|
|
228
|
-
? failure
|
|
229
|
-
: failure === undefined
|
|
230
|
-
? postfixFailure
|
|
231
|
-
: mergeFailures(postfixFailure, failure)
|
|
232
|
-
if (f !== undefined && f.offset === state.pos) state.setHint(f.offset, f.expected)
|
|
236
|
+
failures.hint(state)
|
|
233
237
|
break
|
|
234
238
|
}
|
|
235
239
|
const op = infixes[i]
|
|
236
240
|
const leftPower = op.precedence * 2
|
|
237
241
|
if (leftPower < minPower) {
|
|
238
|
-
state.
|
|
242
|
+
state.rewind(start)
|
|
239
243
|
break
|
|
240
244
|
}
|
|
241
245
|
const opOut = matched
|