@thomasfosterau/effect-parse 0.3.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/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 +44 -8
- package/dist/Expression.js.map +1 -1
- package/dist/Input.d.ts +32 -4
- package/dist/Input.d.ts.map +1 -1
- package/dist/Input.js +47 -8
- package/dist/Input.js.map +1 -1
- package/dist/Nom.d.ts +0 -2
- 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 -9
- 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 +26 -24
- package/dist/Parser.js.map +1 -1
- package/dist/ParserSchema.d.ts +0 -2
- package/dist/ParserSchema.d.ts.map +1 -1
- package/dist/ParserSchema.js +2 -3
- 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 +1 -2
- package/dist/internal/syntax.d.ts +30 -20
- package/dist/internal/syntax.d.ts.map +1 -1
- package/dist/internal/syntax.js +73 -66
- 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 +0 -2
- package/dist/internal/ties.d.ts.map +1 -1
- package/dist/internal/ties.js +1 -2
- 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 +7 -6
- package/src/Input.ts +50 -11
- package/src/ParseError.ts +9 -7
- package/src/Parser.ts +26 -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/syntax.ts +78 -87
- package/src/internal/text.ts +24 -4
- package/src/internal/tokens.ts +20 -13
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,WAAW,CAAC,GAAG,OAAO,EAAE,OAAO,EAAE,OAAO;CACtD,IAA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AI,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,EAAE,IAAI,WAAW,CAAC,CAAC,KAAK,MAAM;CAC3D,OAAO,KAAK,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,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,6 +34,7 @@
|
|
|
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
39
|
import { Fail } from "./Input.js"
|
|
39
40
|
import type { Parser } from "./Parser.js"
|
|
@@ -154,9 +155,9 @@ 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
|
|
@@ -179,7 +180,7 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
179
180
|
return i
|
|
180
181
|
}
|
|
181
182
|
if (state.mode !== "Backtrack") return -2
|
|
182
|
-
state.
|
|
183
|
+
state.rewind(start)
|
|
183
184
|
failures.add(state)
|
|
184
185
|
}
|
|
185
186
|
return -1
|
|
@@ -222,7 +223,7 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
222
223
|
if (q >= 0) {
|
|
223
224
|
const op = postfixes[q]
|
|
224
225
|
if (op.precedence * 2 < minPower) {
|
|
225
|
-
state.
|
|
226
|
+
state.rewind(start)
|
|
226
227
|
break
|
|
227
228
|
}
|
|
228
229
|
lhs = op.fold(lhs, matched)
|
|
@@ -238,7 +239,7 @@ export const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<O
|
|
|
238
239
|
const op = infixes[i]
|
|
239
240
|
const leftPower = op.precedence * 2
|
|
240
241
|
if (leftPower < minPower) {
|
|
241
|
-
state.
|
|
242
|
+
state.rewind(start)
|
|
242
243
|
break
|
|
243
244
|
}
|
|
244
245
|
const opOut = matched
|
package/src/Input.ts
CHANGED
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
*
|
|
14
14
|
* @since 0.1.0
|
|
15
15
|
*/
|
|
16
|
+
import * as Arr from "effect/Array"
|
|
16
17
|
import * as Predicate from "effect/Predicate"
|
|
17
18
|
import * as Context from "effect/Context"
|
|
18
19
|
import type { Encoding } from "./Encoding.js"
|
|
@@ -80,7 +81,7 @@ export const fromSource: {
|
|
|
80
81
|
return source
|
|
81
82
|
}
|
|
82
83
|
|
|
83
|
-
const isArrayBufferLike = (u:
|
|
84
|
+
const isArrayBufferLike = (u: Input | BinaryInput): u is ArrayBufferLike =>
|
|
84
85
|
u instanceof ArrayBuffer || (globalThis.SharedArrayBuffer !== undefined && u instanceof SharedArrayBuffer)
|
|
85
86
|
|
|
86
87
|
/**
|
|
@@ -200,9 +201,11 @@ export interface TraceEvent {
|
|
|
200
201
|
export class State<I extends Input = Input> {
|
|
201
202
|
/** The full input being parsed. */
|
|
202
203
|
readonly input: I
|
|
203
|
-
/**
|
|
204
|
-
|
|
205
|
-
|
|
204
|
+
/**
|
|
205
|
+
* The current offset. Parsers advance it by assigning it; moving it back
|
|
206
|
+
* (backtracking, or after a lookahead) goes through {@link rewind}.
|
|
207
|
+
*/
|
|
208
|
+
pos: number
|
|
206
209
|
/** The end offset (the input's length; in bits inside `Bits.bits`). */
|
|
207
210
|
end: number
|
|
208
211
|
/** 0 = string, 1 = Uint8Array, 2 = array. */
|
|
@@ -283,7 +286,8 @@ export class State<I extends Input = Input> {
|
|
|
283
286
|
*/
|
|
284
287
|
resume: Map<object, Map<number, unknown>> | undefined = undefined
|
|
285
288
|
/**
|
|
286
|
-
* The tree builder of a `Syntax` parse; `undefined` otherwise.
|
|
289
|
+
* The tree builder of a `Syntax` parse; `undefined` otherwise. A builder
|
|
290
|
+
* rather than a subclass of `State`, so that every state has one shape.
|
|
287
291
|
*
|
|
288
292
|
* @internal
|
|
289
293
|
*/
|
|
@@ -392,6 +396,37 @@ export class State<I extends Input = Input> {
|
|
|
392
396
|
return this
|
|
393
397
|
}
|
|
394
398
|
|
|
399
|
+
/**
|
|
400
|
+
* Moves the cursor back to `offset`, abandoning what was parsed after it:
|
|
401
|
+
* how a parser backtracks, or returns to where a lookahead started. A step
|
|
402
|
+
* that moves the cursor backwards must use this rather than assigning
|
|
403
|
+
* {@link pos}, so that a `Syntax` parse drops the nodes the abandoned
|
|
404
|
+
* attempt built. Moving forward is an assignment.
|
|
405
|
+
*
|
|
406
|
+
* @example
|
|
407
|
+
* ```ts
|
|
408
|
+
* import { Input, Parser } from "@thomasfosterau/effect-parse"
|
|
409
|
+
*
|
|
410
|
+
* // Like `Parser.opt`, for one parser
|
|
411
|
+
* const optional = <A>(p: Parser.Parser<A>) =>
|
|
412
|
+
* Parser.make<A | undefined>((state) => {
|
|
413
|
+
* const start = state.pos
|
|
414
|
+
* const a = p.step(state)
|
|
415
|
+
* if (a !== Input.Fail) return a
|
|
416
|
+
* if (state.mode !== "Backtrack") return Input.Fail
|
|
417
|
+
* state.rewind(start)
|
|
418
|
+
* return undefined
|
|
419
|
+
* })
|
|
420
|
+
* ```
|
|
421
|
+
*
|
|
422
|
+
* @since 0.4.0
|
|
423
|
+
*/
|
|
424
|
+
rewind(offset: number): void {
|
|
425
|
+
const syntax = this.syntax
|
|
426
|
+
if (syntax !== undefined && offset < this.pos) syntax.rewound(this.pos, offset)
|
|
427
|
+
this.pos = offset
|
|
428
|
+
}
|
|
429
|
+
|
|
395
430
|
/**
|
|
396
431
|
* The number of input units (code units, bytes or elements) remaining.
|
|
397
432
|
*/
|
|
@@ -484,6 +519,7 @@ export class State<I extends Input = Input> {
|
|
|
484
519
|
if (mode === "Backtrack" && (this.deepestFailure === undefined || failure.offset >= this.deepestFailure.offset)) {
|
|
485
520
|
this.deepestFailure = failure
|
|
486
521
|
}
|
|
522
|
+
if (this.syntax !== undefined) this.syntax.reach(failure.offset)
|
|
487
523
|
return Fail
|
|
488
524
|
}
|
|
489
525
|
|
|
@@ -501,6 +537,7 @@ export class State<I extends Input = Input> {
|
|
|
501
537
|
this.failureTie1 = tie1
|
|
502
538
|
this.failureTie2 = tie2
|
|
503
539
|
this.deepestIsLatest = this.deepestFailure === undefined || head.offset >= this.deepestFailure.offset
|
|
540
|
+
if (this.syntax !== undefined) this.syntax.reach(head.offset)
|
|
504
541
|
return Fail
|
|
505
542
|
}
|
|
506
543
|
|
|
@@ -531,8 +568,9 @@ export class State<I extends Input = Input> {
|
|
|
531
568
|
) {
|
|
532
569
|
return f
|
|
533
570
|
}
|
|
534
|
-
const context =
|
|
535
|
-
|
|
571
|
+
const context = Arr.isReadonlyArrayEmpty(d.context)
|
|
572
|
+
? f.context
|
|
573
|
+
: [...d.context, ...f.context.filter((l) => !d.context.includes(l))]
|
|
536
574
|
return new Failure(d.offset, d.kind, d.expected, context, d.hasCause, d.cause, d.message, d.needed)
|
|
537
575
|
}
|
|
538
576
|
|
|
@@ -542,6 +580,7 @@ export class State<I extends Input = Input> {
|
|
|
542
580
|
* `repeat` and `Ascii.digit1`.
|
|
543
581
|
*/
|
|
544
582
|
setHint(offset: number, expected: ReadonlyArray<string>): void {
|
|
583
|
+
if (this.syntax !== undefined) this.syntax.reach(offset)
|
|
545
584
|
if (this.hintOffset === offset) {
|
|
546
585
|
this.hintExpected = mergeExpected(this.hintExpected, expected)
|
|
547
586
|
} else {
|
|
@@ -552,7 +591,7 @@ export class State<I extends Input = Input> {
|
|
|
552
591
|
|
|
553
592
|
/** @internal */
|
|
554
593
|
makeFailure(offset: number, kind: ErrorKind, expected: ReadonlyArray<string>): Failure {
|
|
555
|
-
if (this.hintOffset === offset && this.hintExpected
|
|
594
|
+
if (this.hintOffset === offset && Arr.isReadonlyArrayNonEmpty(this.hintExpected)) {
|
|
556
595
|
expected = mergeExpected(this.hintExpected, expected)
|
|
557
596
|
}
|
|
558
597
|
return new Failure(offset, kind, expected, empty, false, undefined, undefined, 0)
|
|
@@ -561,8 +600,8 @@ export class State<I extends Input = Input> {
|
|
|
561
600
|
|
|
562
601
|
/** @internal */
|
|
563
602
|
export const mergeExpected = (a: ReadonlyArray<string>, b: ReadonlyArray<string>): ReadonlyArray<string> => {
|
|
564
|
-
if (a
|
|
565
|
-
if (b
|
|
603
|
+
if (Arr.isReadonlyArrayEmpty(a)) return b
|
|
604
|
+
if (Arr.isReadonlyArrayEmpty(b)) return a
|
|
566
605
|
const out = a.slice()
|
|
567
606
|
for (const e of b) {
|
|
568
607
|
if (!out.includes(e)) out.push(e)
|
|
@@ -595,7 +634,7 @@ export const mergeTiedFailures = (a: Failure, b: Failure, expected: ReadonlyArra
|
|
|
595
634
|
b.offset,
|
|
596
635
|
b.kind,
|
|
597
636
|
expected,
|
|
598
|
-
b.context
|
|
637
|
+
Arr.isReadonlyArrayNonEmpty(b.context) ? b.context : a.context,
|
|
599
638
|
b.hasCause || a.hasCause,
|
|
600
639
|
b.hasCause ? b.cause : a.cause,
|
|
601
640
|
b.message ?? a.message,
|
package/src/ParseError.ts
CHANGED
|
@@ -19,9 +19,11 @@
|
|
|
19
19
|
*
|
|
20
20
|
* @since 0.1.0
|
|
21
21
|
*/
|
|
22
|
+
import * as Arr from "effect/Array"
|
|
22
23
|
import * as Predicate from "effect/Predicate"
|
|
23
24
|
import * as Data from "effect/Data"
|
|
24
25
|
import * as Option from "effect/Option"
|
|
26
|
+
import * as Result from "effect/Result"
|
|
25
27
|
import type { Encoding } from "./Encoding.js"
|
|
26
28
|
import type { ErrMode, Failure, Input } from "./Input.js"
|
|
27
29
|
import * as text from "./internal/text.js"
|
|
@@ -245,7 +247,7 @@ const codePointCount = (s: string, start: number, end: number): number => {
|
|
|
245
247
|
* @category utils
|
|
246
248
|
*/
|
|
247
249
|
export const formatExpected = (expected: ReadonlyArray<string>): string => {
|
|
248
|
-
if (expected
|
|
250
|
+
if (Arr.isReadonlyArrayEmpty(expected)) return ""
|
|
249
251
|
if (expected.length === 1) return expected[0]
|
|
250
252
|
return `${expected.slice(0, -1).join(", ")} or ${expected[expected.length - 1]}`
|
|
251
253
|
}
|
|
@@ -277,11 +279,11 @@ const describeFound = (input: Input, offset: number, encoding: Encoding<any> | u
|
|
|
277
279
|
export const inspectToken = (u: unknown): string => {
|
|
278
280
|
if (Predicate.isString(u)) return JSON.stringify(u)
|
|
279
281
|
if (Predicate.isObjectOrArray(u)) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
282
|
+
// `JSON.stringify` throws on cycles and `BigInt`s
|
|
283
|
+
return Result.getOrElse(
|
|
284
|
+
Result.try(() => JSON.stringify(u)),
|
|
285
|
+
() => String(u)
|
|
286
|
+
)
|
|
285
287
|
}
|
|
286
288
|
return String(u)
|
|
287
289
|
}
|
|
@@ -366,7 +368,7 @@ const render = (error: ParseError<unknown>): string => {
|
|
|
366
368
|
if (error.description !== undefined) lines.push(error.description)
|
|
367
369
|
if (error.mode === "Incomplete") {
|
|
368
370
|
lines.push("unexpected end of input (incomplete)")
|
|
369
|
-
} else if (error.expected
|
|
371
|
+
} else if (Arr.isReadonlyArrayNonEmpty(error.expected)) {
|
|
370
372
|
lines.push(`expected ${formatExpected(error.expected)}, found ${describeFound(input, offset, encoding)}`)
|
|
371
373
|
} else if (cause === undefined && error.description === undefined) {
|
|
372
374
|
lines.push(`unexpected ${describeFound(input, offset, encoding)} (${error.kind})`)
|