@thomasfosterau/effect-parse 0.3.0 → 0.4.1
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 +3 -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 +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 +2 -4
- 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 +6 -4
- 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 +3 -4
- 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 +17 -10
- 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 +2 -4
- package/dist/internal/entries.js.map +1 -1
- 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 +7 -7
- 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 +13 -9
- package/src/Parser.ts +30 -25
- package/src/ParserSchema.ts +1 -1
- package/src/ParserStream.ts +23 -9
- 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/entries.ts +2 -2
- 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/Expression.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Expression.js","names":["makeParser"],"sources":["../src/Expression.ts"],"sourcesContent":["/**\n * Operator-precedence expression parsing with a Pratt parser (from\n * `nom-language`'s `precedence`, and winnow's expression parser).\n *\n * Describe the operand parser and a table of prefix, postfix and infix\n * operators with their precedence (higher binds tighter) and associativity.\n * Each operator carries a function that folds its operands into a result.\n *\n * @example\n * ```ts\n * import { Ascii, Expression, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const op = (s: string) => Parser.delimited(Ascii.space0, Token.literal(s), Ascii.space0)\n *\n * const arithmetic: Parser.Parser<number, string> = Expression.make({\n * operand: Parser.alt(\n * Ascii.decInt,\n * Parser.delimited(op(\"(\"), Parser.suspend(() => arithmetic), op(\")\"))\n * ),\n * operators: [\n * Expression.prefix(op(\"-\"), 4, (_, x) => -x),\n * Expression.postfix(op(\"!\"), 5, (x) => factorial(x)),\n * Expression.infixRight(op(\"^\"), 3, (l, _, r) => l ** r),\n * Expression.infixLeft(op(\"*\"), 2, (l, _, r) => l * r),\n * Expression.infixLeft(op(\"/\"), 2, (l, _, r) => l / r),\n * Expression.infixLeft(op(\"+\"), 1, (l, _, r) => l + r),\n * Expression.infixLeft(op(\"-\"), 1, (l, _, r) => l - r)\n * ]\n * })\n *\n * declare const factorial: (n: number) => number\n * Parser.parse(arithmetic, \"1 + 2 * 3 ^ 2\") // Result.succeed(19)\n * ```\n *\n * @since 0.1.0\n */\nimport type { Input, State } from \"./Input.js\"\nimport { Fail } from \"./Input.js\"\nimport type { Parser } from \"./Parser.js\"\nimport { make as makeParser } from \"./Parser.js\"\nimport { TiedFailures } from \"./internal/ties.js\"\n\n/**\n * An operator in an expression grammar.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Operator<A, E = never, R = never, I extends Input = string> =\n | Prefix<A, E, R, I>\n | Postfix<A, E, R, I>\n | Infix<A, E, R, I>\n\ninterface OperatorBase<E, R, I extends Input> {\n readonly parser: Parser<unknown, E, R, I>\n readonly precedence: number\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Prefix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Prefix\"\n readonly fold: (op: any, operand: A) => A\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Postfix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Postfix\"\n readonly fold: (operand: A, op: any) => A\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Infix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Infix\"\n readonly associativity: \"left\" | \"right\"\n readonly fold: (left: A, op: any, right: A) => A\n}\n\n/**\n * A prefix operator, e.g. unary minus.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const prefix = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (op: O, operand: A) => A\n): Prefix<A, E, R, I> => ({ _tag: \"Prefix\", parser, precedence, fold })\n\n/**\n * A postfix operator, e.g. factorial or a function call suffix.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const postfix = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (operand: A, op: O) => A\n): Postfix<A, E, R, I> => ({ _tag: \"Postfix\", parser, precedence, fold })\n\n/**\n * A left-associative infix operator: `a - b - c` is `(a - b) - c`.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const infixLeft = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (left: A, op: O, right: A) => A\n): Infix<A, E, R, I> => ({ _tag: \"Infix\", associativity: \"left\", parser, precedence, fold })\n\n/**\n * A right-associative infix operator: `a ^ b ^ c` is `a ^ (b ^ c)`.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const infixRight = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (left: A, op: O, right: A) => A\n): Infix<A, E, R, I> => ({ _tag: \"Infix\", associativity: \"right\", parser, precedence, fold })\n\ntype OpError<Op> = Op extends OperatorBase<infer E, infer _R, infer _I> ? E : never\ntype OpServices<Op> = Op extends OperatorBase<infer _E, infer R, infer _I> ? R : never\ntype OpInput<Op> = [Op] extends [never] ? Input : [Op] extends [OperatorBase<infer _E, infer _R, infer I>] ? I : never\n\n/**\n * Builds an expression parser from an operand parser and an operator table.\n *\n * Precedences are compared numerically: higher binds tighter. A prefix\n * operator applies to everything that binds tighter than it, so with `-`\n * at precedence 4 and `^` at 3, `-2 ^ 2` parses as `(-2) ^ 2`.\n *\n * When several operators of the same kind match, the first one in the\n * table wins, so list longer operators (`<=`) before their prefixes (`<`).\n *\n * @since 0.1.0\n * @category constructors\n */\nexport const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<Operator<A, any, any, any>>>(options: {\n readonly operand: Parser<A, E, R, I>\n readonly operators: Ops\n}): Parser<A, E | OpError<Ops[number]>, R | OpServices<Ops[number]>, I & OpInput<Ops[number]>> => {\n const operand = options.operand.step\n const prefixes = options.operators.filter((o): o is Prefix<A, any, any, any> => o._tag === \"Prefix\")\n const postfixes = options.operators.filter((o): o is Postfix<A, any, any, any> => o._tag === \"Postfix\")\n const infixes = options.operators.filter((o): o is Infix<A, any, any, any> => o._tag === \"Infix\")\n\n // Attempts each operator in turn. Returns the index of the first match\n // (with its output in `matched`), -1 if none matched, or -2 on a fatal\n // failure. Failed attempts are folded into `failures`, merged only when\n // read (#44): a round in which an operator matches discards them unmerged.\n // Like `matched`, it is shared by nested expressions, which clear it.\n let matched: unknown\n const failures = new TiedFailures()\n const tryOps = (state: State<any>, ops: ReadonlyArray<OperatorBase<any, any, any>>, mustConsume: boolean): number => {\n const start = state.pos\n for (let i = 0; i < ops.length; i++) {\n const out = ops[i].parser.step(state)\n if (out !== Fail) {\n // A prefix or postfix operator matching nothing would apply forever\n if (mustConsume && state.pos === start) {\n state.failMessage(\"Cut\", \"Assert\", \"prefix and postfix operators must always consume\")\n return -2\n }\n matched = out\n return i\n }\n if (state.mode !== \"Backtrack\") return -2\n state.pos = start\n failures.add(state)\n }\n return -1\n }\n\n const expr = (state: State<any>, minPower: number): A | Fail => {\n let lhs: A | Fail\n failures.clear()\n const p = tryOps(state, prefixes, true)\n if (p === -2) return Fail\n if (p >= 0) {\n const op = prefixes[p]\n const opOut = matched\n const operandValue = expr(state, op.precedence * 2)\n if (operandValue === Fail) return Fail\n lhs = op.fold(opOut, operandValue)\n } else {\n const prefixFailure = failures.failure\n const prefixTie1 = failures.tie1\n const prefixTie2 = failures.tie2\n lhs = operand(state)\n if (lhs === Fail) {\n if (prefixFailure !== undefined && state.mode === \"Backtrack\") {\n // The operand may have parsed nested expressions, which reuse `failures`\n failures.failure = prefixFailure\n failures.tie1 = prefixTie1\n failures.tie2 = prefixTie2\n failures.add(state)\n failures.fail(state)\n }\n return Fail\n }\n }\n\n while (true) {\n const start = state.pos\n failures.clear()\n const q = tryOps(state, postfixes, true)\n if (q === -2) return Fail\n if (q >= 0) {\n const op = postfixes[q]\n if (op.precedence * 2 < minPower) {\n state.pos = start\n break\n }\n lhs = op.fold(lhs, matched)\n continue\n }\n // The infix attempts are folded on top of the postfix ones\n const i = tryOps(state, infixes, false)\n if (i === -2) return Fail\n if (i < 0) {\n failures.hint(state)\n break\n }\n const op = infixes[i]\n const leftPower = op.precedence * 2\n if (leftPower < minPower) {\n state.pos = start\n break\n }\n const opOut = matched\n const rhs = expr(state, op.associativity === \"left\" ? leftPower + 1 : leftPower)\n if (rhs === Fail) return Fail\n lhs = op.fold(lhs, opOut, rhs)\n }\n return lhs\n }\n\n return makeParser((state: State<any>) => expr(state, 0), \"expression\")\n}\n"],"mappings":";;;;;;;;;;AA4FA,MAAa,UACX,QACA,YACA,UACwB;CAAE,MAAM;CAAU;CAAQ;CAAY;AAAK;;;;;;;AAQrE,MAAa,WACX,QACA,YACA,UACyB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O;GAClC,MAAM,GAAG,KAAK,OAAO,YAAY;EACnC,OAAO;GACL,MAAM,gBAAgB,SAAS;GAC/B,MAAM,aAAa,SAAS;GAC5B,MAAM,aAAa,SAAS;GAC5B,MAAM,QAAQ,KAAK;GACnB,IAAI,QAAQ,MAAM;IAChB,IAAI,kBAAkB,KAAA,KAAa,MAAM,SAAS,aAAa;KAE7D,SAAS,UAAU;KACnB,SAAS,OAAO;KAChB,SAAS,OAAO;KAChB,SAAS,IAAI,KAAK;KAClB,SAAS,KAAK,KAAK;IACrB;IACA,OAAO;GACT;EACF;EAEA,OAAO,MAAM;GACX,MAAM,QAAQ,MAAM;GACpB,SAAS,MAAM;GACf,MAAM,IAAI,OAAO,OAAO,WAAW,IAAI;GACvC,IAAI,MAAM,IAAI,OAAO;GACrB,IAAI,KAAK,GAAG;IACV,MAAM,KAAK,UAAU;IACrB,IAAI,GAAG,aAAa,IAAI,UAAU;KAChC,MAAM,MAAM;KACZ;IACF;IACA,MAAM,GAAG,KAAK,KAAK,OAAO;IAC1B;GACF;GAEA,MAAM,IAAI,OAAO,OAAO,SAAS,KAAK;GACtC,IAAI,MAAM,IAAI,OAAO;GACrB,IAAI,IAAI,GAAG;IACT,SAAS,KAAK,KAAK;IACnB;GACF;GACA,MAAM,KAAK,QAAQ;GACnB,MAAM,YAAY,GAAG,aAAa;GAClC,IAAI,YAAY,UAAU;IACxB,MAAM,MAAM;IACZ;GACF;GACA,MAAM,QAAQ;GACd,MAAM,MAAM,KAAK,OAAO,GAAG,kBAAkB,SAAS,YAAY,IAAI,SAAS;GAC/E,IAAI,QAAQ,MAAM,OAAO;GACzB,MAAM,GAAG,KAAK,KAAK,OAAO,GAAG;EAC/B;EACA,OAAO;CACT;CAEA,OAAOA,QAAY,UAAsB,KAAK,OAAO,CAAC,GAAG,YAAY;AACvE"}
|
|
1
|
+
{"version":3,"file":"Expression.js","names":["makeParser"],"sources":["../src/Expression.ts"],"sourcesContent":["/**\n * Operator-precedence expression parsing with a Pratt parser (from\n * `nom-language`'s `precedence`, and winnow's expression parser).\n *\n * Describe the operand parser and a table of prefix, postfix and infix\n * operators with their precedence (higher binds tighter) and associativity.\n * Each operator carries a function that folds its operands into a result.\n *\n * @example\n * ```ts\n * import { Ascii, Expression, Parser, Token } from \"@thomasfosterau/effect-parse\"\n *\n * const op = (s: string) => Parser.delimited(Ascii.space0, Token.literal(s), Ascii.space0)\n *\n * const arithmetic: Parser.Parser<number, string> = Expression.make({\n * operand: Parser.alt(\n * Ascii.decInt,\n * Parser.delimited(op(\"(\"), Parser.suspend(() => arithmetic), op(\")\"))\n * ),\n * operators: [\n * Expression.prefix(op(\"-\"), 4, (_, x) => -x),\n * Expression.postfix(op(\"!\"), 5, (x) => factorial(x)),\n * Expression.infixRight(op(\"^\"), 3, (l, _, r) => l ** r),\n * Expression.infixLeft(op(\"*\"), 2, (l, _, r) => l * r),\n * Expression.infixLeft(op(\"/\"), 2, (l, _, r) => l / r),\n * Expression.infixLeft(op(\"+\"), 1, (l, _, r) => l + r),\n * Expression.infixLeft(op(\"-\"), 1, (l, _, r) => l - r)\n * ]\n * })\n *\n * declare const factorial: (n: number) => number\n * Parser.parse(arithmetic, \"1 + 2 * 3 ^ 2\") // Result.succeed(19)\n * ```\n *\n * @since 0.1.0\n */\nimport * as Predicate from \"effect/Predicate\"\nimport type { Input, State } from \"./Input.js\"\nimport { Fail } from \"./Input.js\"\nimport type { Parser } from \"./Parser.js\"\nimport { make as makeParser } from \"./Parser.js\"\nimport { TiedFailures } from \"./internal/ties.js\"\n\n/**\n * An operator in an expression grammar.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Operator<A, E = never, R = never, I extends Input = string> =\n | Prefix<A, E, R, I>\n | Postfix<A, E, R, I>\n | Infix<A, E, R, I>\n\ninterface OperatorBase<E, R, I extends Input> {\n readonly parser: Parser<unknown, E, R, I>\n readonly precedence: number\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Prefix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Prefix\"\n readonly fold: (op: any, operand: A) => A\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Postfix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Postfix\"\n readonly fold: (operand: A, op: any) => A\n}\n\n/**\n * @since 0.1.0\n * @category models\n */\nexport interface Infix<A, E = never, R = never, I extends Input = string> extends OperatorBase<E, R, I> {\n readonly _tag: \"Infix\"\n readonly associativity: \"left\" | \"right\"\n readonly fold: (left: A, op: any, right: A) => A\n}\n\n/**\n * A prefix operator, e.g. unary minus.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const prefix = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (op: O, operand: A) => A\n): Prefix<A, E, R, I> => ({ _tag: \"Prefix\", parser, precedence, fold })\n\n/**\n * A postfix operator, e.g. factorial or a function call suffix.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const postfix = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (operand: A, op: O) => A\n): Postfix<A, E, R, I> => ({ _tag: \"Postfix\", parser, precedence, fold })\n\n/**\n * A left-associative infix operator: `a - b - c` is `(a - b) - c`.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const infixLeft = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (left: A, op: O, right: A) => A\n): Infix<A, E, R, I> => ({ _tag: \"Infix\", associativity: \"left\", parser, precedence, fold })\n\n/**\n * A right-associative infix operator: `a ^ b ^ c` is `a ^ (b ^ c)`.\n *\n * @since 0.1.0\n * @category operators\n */\nexport const infixRight = <O, E, R, I extends Input, A>(\n parser: Parser<O, E, R, I>,\n precedence: number,\n fold: (left: A, op: O, right: A) => A\n): Infix<A, E, R, I> => ({ _tag: \"Infix\", associativity: \"right\", parser, precedence, fold })\n\ntype OpError<Op> = Op extends OperatorBase<infer E, infer _R, infer _I> ? E : never\ntype OpServices<Op> = Op extends OperatorBase<infer _E, infer R, infer _I> ? R : never\ntype OpInput<Op> = [Op] extends [never] ? Input : [Op] extends [OperatorBase<infer _E, infer _R, infer I>] ? I : never\n\n/**\n * Builds an expression parser from an operand parser and an operator table.\n *\n * Precedences are compared numerically: higher binds tighter. A prefix\n * operator applies to everything that binds tighter than it, so with `-`\n * at precedence 4 and `^` at 3, `-2 ^ 2` parses as `(-2) ^ 2`.\n *\n * When several operators of the same kind match, the first one in the\n * table wins, so list longer operators (`<=`) before their prefixes (`<`).\n *\n * @since 0.1.0\n * @category constructors\n */\nexport const make = <A, E, R, I extends Input, const Ops extends ReadonlyArray<Operator<A, any, any, any>>>(options: {\n readonly operand: Parser<A, E, R, I>\n readonly operators: Ops\n}): Parser<A, E | OpError<Ops[number]>, R | OpServices<Ops[number]>, I & OpInput<Ops[number]>> => {\n const operand = options.operand.step\n const prefixes = options.operators.filter((o): o is Prefix<A, any, any, any> => Predicate.isTagged(o, \"Prefix\"))\n const postfixes = options.operators.filter((o): o is Postfix<A, any, any, any> => Predicate.isTagged(o, \"Postfix\"))\n const infixes = options.operators.filter((o): o is Infix<A, any, any, any> => Predicate.isTagged(o, \"Infix\"))\n\n // Attempts each operator in turn. Returns the index of the first match\n // (with its output in `matched`), -1 if none matched, or -2 on a fatal\n // failure. Failed attempts are folded into `failures`, merged only when\n // read (#44): a round in which an operator matches discards them unmerged.\n // Like `matched`, it is shared by nested expressions, which clear it.\n let matched: unknown\n const failures = new TiedFailures()\n const tryOps = (state: State<any>, ops: ReadonlyArray<OperatorBase<any, any, any>>, mustConsume: boolean): number => {\n const start = state.pos\n for (let i = 0; i < ops.length; i++) {\n const out = ops[i].parser.step(state)\n if (out !== Fail) {\n // A prefix or postfix operator matching nothing would apply forever\n if (mustConsume && state.pos === start) {\n state.failMessage(\"Cut\", \"Assert\", \"prefix and postfix operators must always consume\")\n return -2\n }\n matched = out\n return i\n }\n if (state.mode !== \"Backtrack\") return -2\n state.rewind(start)\n failures.add(state)\n }\n return -1\n }\n\n const expr = (state: State<any>, minPower: number): A | Fail => {\n let lhs: A | Fail\n failures.clear()\n const p = tryOps(state, prefixes, true)\n if (p === -2) return Fail\n if (p >= 0) {\n const op = prefixes[p]\n const opOut = matched\n const operandValue = expr(state, op.precedence * 2)\n if (operandValue === Fail) return Fail\n lhs = op.fold(opOut, operandValue)\n } else {\n const prefixFailure = failures.failure\n const prefixTie1 = failures.tie1\n const prefixTie2 = failures.tie2\n lhs = operand(state)\n if (lhs === Fail) {\n if (prefixFailure !== undefined && state.mode === \"Backtrack\") {\n // The operand may have parsed nested expressions, which reuse `failures`\n failures.failure = prefixFailure\n failures.tie1 = prefixTie1\n failures.tie2 = prefixTie2\n failures.add(state)\n failures.fail(state)\n }\n return Fail\n }\n }\n\n while (true) {\n const start = state.pos\n failures.clear()\n const q = tryOps(state, postfixes, true)\n if (q === -2) return Fail\n if (q >= 0) {\n const op = postfixes[q]\n if (op.precedence * 2 < minPower) {\n state.rewind(start)\n break\n }\n lhs = op.fold(lhs, matched)\n continue\n }\n // The infix attempts are folded on top of the postfix ones\n const i = tryOps(state, infixes, false)\n if (i === -2) return Fail\n if (i < 0) {\n failures.hint(state)\n break\n }\n const op = infixes[i]\n const leftPower = op.precedence * 2\n if (leftPower < minPower) {\n state.rewind(start)\n break\n }\n const opOut = matched\n const rhs = expr(state, op.associativity === \"left\" ? leftPower + 1 : leftPower)\n if (rhs === Fail) return Fail\n lhs = op.fold(lhs, opOut, rhs)\n }\n return lhs\n }\n\n return makeParser((state: State<any>) => expr(state, 0), \"expression\")\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA6FA,MAAa,UACX,QACA,YACA,UACwB;CAAE,MAAM;CAAU;CAAQ;CAAY;AAAK;;;;;;;AAQrE,MAAa,WACX,QACA,YACA,UACyB;CAAE,MAAM;CAAW;CAAQ;CAAY;AAAK;;;;;;;AAQvE,MAA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";
|
|
3
3
|
import { Builder } from "./internal/syntax.js";
|
|
4
4
|
import { Encoding } from "./Encoding.js";
|
|
5
|
-
//#region src/Input.d.ts
|
|
6
5
|
/**
|
|
7
6
|
* The kinds of input a parser can consume.
|
|
8
7
|
*
|
|
@@ -161,7 +160,10 @@ export interface TraceEvent {
|
|
|
161
160
|
export declare class State<I extends Input = Input> {
|
|
162
161
|
/** The full input being parsed. */
|
|
163
162
|
readonly input: I;
|
|
164
|
-
/**
|
|
163
|
+
/**
|
|
164
|
+
* The current offset. Parsers advance it by assigning it; moving it back
|
|
165
|
+
* (backtracking, or after a lookahead) goes through {@link rewind}.
|
|
166
|
+
*/
|
|
165
167
|
pos: number;
|
|
166
168
|
/** The end offset (the input's length; in bits inside `Bits.bits`). */
|
|
167
169
|
end: number;
|
|
@@ -237,7 +239,8 @@ export declare class State<I extends Input = Input> {
|
|
|
237
239
|
*/
|
|
238
240
|
resume: Map<object, Map<number, unknown>> | undefined;
|
|
239
241
|
/**
|
|
240
|
-
* The tree builder of a `Syntax` parse; `undefined` otherwise.
|
|
242
|
+
* The tree builder of a `Syntax` parse; `undefined` otherwise. A builder
|
|
243
|
+
* rather than a subclass of `State`, so that every state has one shape.
|
|
241
244
|
*
|
|
242
245
|
* @internal
|
|
243
246
|
*/
|
|
@@ -284,6 +287,32 @@ export declare class State<I extends Input = Input> {
|
|
|
284
287
|
readonly partial?: boolean | undefined;
|
|
285
288
|
readonly context?: Context.Context<never> | undefined;
|
|
286
289
|
}): this;
|
|
290
|
+
/**
|
|
291
|
+
* Moves the cursor back to `offset`, abandoning what was parsed after it:
|
|
292
|
+
* how a parser backtracks, or returns to where a lookahead started. A step
|
|
293
|
+
* that moves the cursor backwards must use this rather than assigning
|
|
294
|
+
* {@link pos}, so that a `Syntax` parse drops the nodes the abandoned
|
|
295
|
+
* attempt built. Moving forward is an assignment.
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```ts
|
|
299
|
+
* import { Input, Parser } from "@thomasfosterau/effect-parse"
|
|
300
|
+
*
|
|
301
|
+
* // Like `Parser.opt`, for one parser
|
|
302
|
+
* const optional = <A>(p: Parser.Parser<A>) =>
|
|
303
|
+
* Parser.make<A | undefined>((state) => {
|
|
304
|
+
* const start = state.pos
|
|
305
|
+
* const a = p.step(state)
|
|
306
|
+
* if (a !== Input.Fail) return a
|
|
307
|
+
* if (state.mode !== "Backtrack") return Input.Fail
|
|
308
|
+
* state.rewind(start)
|
|
309
|
+
* return undefined
|
|
310
|
+
* })
|
|
311
|
+
* ```
|
|
312
|
+
*
|
|
313
|
+
* @since 0.4.0
|
|
314
|
+
*/
|
|
315
|
+
rewind(offset: number): void;
|
|
287
316
|
/**
|
|
288
317
|
* The number of input units (code units, bytes or elements) remaining.
|
|
289
318
|
*/
|
|
@@ -371,5 +400,4 @@ export declare const mergeFailures: (a: Failure, b: Failure) => Failure;
|
|
|
371
400
|
* @internal
|
|
372
401
|
*/
|
|
373
402
|
export declare const mergeTiedFailures: (a: Failure, b: Failure, expected: ReadonlyArray<string>) => Failure;
|
|
374
|
-
//#endregion
|
|
375
403
|
//# sourceMappingURL=Input.d.ts.map
|
package/dist/Input.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Input.d.ts","names":["SyntaxBuilder"],"sources":["../src/Input.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Input.d.ts","names":["SyntaxBuilder"],"sources":["../src/Input.ts"],"mappings":";;;;;;;;;;;;;;;YAiCY,iBAAiB,aAAa;;;;;;;;;;;;;YAc9B,cAAc,kBAAkB;;;;;;;;YAShC,OAAO,KAAK,MAAM,YAAY,cAAc;;;;;;;;;;;;;;;qBAgB3C;GACV,QAAQ,cAAc;GACtB,UAAU,OAAO,QAAQ,OAAO,KAAK;;;;;;;;YAkB5B,MAAM,KAAK,4BAEnB,UAAU,sBAER,UAAU,oBAAoB,KAC5B;;;;;;;YASI,MAAM,KAAK,4BAEnB,UAAU,aACR,aACA,U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;;;;EAOtF,YAAY,MAAM,SAAS,MAAM,WAAW,iBAAiB,kBAA4B;;;;EAOzF,SAAS,MAAM,SAAS,SAAS,UAAU;;;;;;;;EAwB3C,SAAS,MAAM,SAAS,MAAM,SAAS,MAAM,sBAAsB;;;;;EAenE,eAAe,SAAS;;;;;;EAUxB,mBAAmB;;;;;;EAwBnB,QAAQ,gBAAgB,UAAU;;EAWlC,YAAY,gBAAgB,MAAM,WAAW,UAAU,wBAAwB;;;qBASpE,gBAAiB,GAAG,uBAAuB,GAAG,0BAAwB;;;;;;;qBAgBtE,gBAAiB,GAAG,SAAS,GAAG,YAAU;;;;;;;;qBAa1C,oBAAqB,GAAG,SAAS,GAAG,SAAS,UAAU,0BAAwB"}
|
package/dist/Input.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import * as Predicate from "effect/Predicate";
|
|
2
|
+
import * as Arr from "effect/Array";
|
|
2
3
|
import * as Context from "effect/Context";
|
|
3
|
-
|
|
4
|
+
// src/Input.ts
|
|
4
5
|
/**
|
|
5
6
|
* Input types and the mutable parse {@link State} cursor.
|
|
6
7
|
*
|
|
@@ -85,6 +86,11 @@ const emptyStrings = empty;
|
|
|
85
86
|
var State = class {
|
|
86
87
|
/** The full input being parsed. */
|
|
87
88
|
input;
|
|
89
|
+
/**
|
|
90
|
+
* The current offset. Parsers advance it by assigning it; moving it back
|
|
91
|
+
* (backtracking, or after a lookahead) goes through {@link rewind}.
|
|
92
|
+
*/
|
|
93
|
+
pos;
|
|
88
94
|
/** The end offset (the input's length; in bits inside `Bits.bits`). */
|
|
89
95
|
end;
|
|
90
96
|
/** 0 = string, 1 = Uint8Array, 2 = array. */
|
|
@@ -165,7 +171,8 @@ var State = class {
|
|
|
165
171
|
*/
|
|
166
172
|
resume = void 0;
|
|
167
173
|
/**
|
|
168
|
-
* The tree builder of a `Syntax` parse; `undefined` otherwise.
|
|
174
|
+
* The tree builder of a `Syntax` parse; `undefined` otherwise. A builder
|
|
175
|
+
* rather than a subclass of `State`, so that every state has one shape.
|
|
169
176
|
*
|
|
170
177
|
* @internal
|
|
171
178
|
*/
|
|
@@ -257,6 +264,36 @@ var State = class {
|
|
|
257
264
|
return this;
|
|
258
265
|
}
|
|
259
266
|
/**
|
|
267
|
+
* Moves the cursor back to `offset`, abandoning what was parsed after it:
|
|
268
|
+
* how a parser backtracks, or returns to where a lookahead started. A step
|
|
269
|
+
* that moves the cursor backwards must use this rather than assigning
|
|
270
|
+
* {@link pos}, so that a `Syntax` parse drops the nodes the abandoned
|
|
271
|
+
* attempt built. Moving forward is an assignment.
|
|
272
|
+
*
|
|
273
|
+
* @example
|
|
274
|
+
* ```ts
|
|
275
|
+
* import { Input, Parser } from "@thomasfosterau/effect-parse"
|
|
276
|
+
*
|
|
277
|
+
* // Like `Parser.opt`, for one parser
|
|
278
|
+
* const optional = <A>(p: Parser.Parser<A>) =>
|
|
279
|
+
* Parser.make<A | undefined>((state) => {
|
|
280
|
+
* const start = state.pos
|
|
281
|
+
* const a = p.step(state)
|
|
282
|
+
* if (a !== Input.Fail) return a
|
|
283
|
+
* if (state.mode !== "Backtrack") return Input.Fail
|
|
284
|
+
* state.rewind(start)
|
|
285
|
+
* return undefined
|
|
286
|
+
* })
|
|
287
|
+
* ```
|
|
288
|
+
*
|
|
289
|
+
* @since 0.4.0
|
|
290
|
+
*/
|
|
291
|
+
rewind(offset) {
|
|
292
|
+
const syntax = this.syntax;
|
|
293
|
+
if (syntax !== void 0 && offset < this.pos) syntax.rewound(this.pos, offset);
|
|
294
|
+
this.pos = offset;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
260
297
|
* The number of input units (code units, bytes or elements) remaining.
|
|
261
298
|
*/
|
|
262
299
|
remaining() {
|
|
@@ -330,6 +367,7 @@ var State = class {
|
|
|
330
367
|
this.failureTie1 = void 0;
|
|
331
368
|
this.failureTie2 = void 0;
|
|
332
369
|
if (mode === "Backtrack" && (this.deepestFailure === void 0 || failure.offset >= this.deepestFailure.offset)) this.deepestFailure = failure;
|
|
370
|
+
if (this.syntax !== void 0) this.syntax.reach(failure.offset);
|
|
333
371
|
return Fail;
|
|
334
372
|
}
|
|
335
373
|
/**
|
|
@@ -346,6 +384,7 @@ var State = class {
|
|
|
346
384
|
this.failureTie1 = tie1;
|
|
347
385
|
this.failureTie2 = tie2;
|
|
348
386
|
this.deepestIsLatest = this.deepestFailure === void 0 || head.offset >= this.deepestFailure.offset;
|
|
387
|
+
if (this.syntax !== void 0) this.syntax.reach(head.offset);
|
|
349
388
|
return Fail;
|
|
350
389
|
}
|
|
351
390
|
/**
|
|
@@ -365,7 +404,7 @@ var State = class {
|
|
|
365
404
|
const f = this.failure;
|
|
366
405
|
const d = this.deepest;
|
|
367
406
|
if (d === void 0 || d === f || d.offset <= f.offset || this.mode === "Incomplete" || f.hasCause || f.message !== void 0) return f;
|
|
368
|
-
const context = d.context
|
|
407
|
+
const context = Arr.isReadonlyArrayEmpty(d.context) ? f.context : [...d.context, ...f.context.filter((l) => !d.context.includes(l))];
|
|
369
408
|
return new Failure(d.offset, d.kind, d.expected, context, d.hasCause, d.cause, d.message, d.needed);
|
|
370
409
|
}
|
|
371
410
|
/**
|
|
@@ -374,6 +413,7 @@ var State = class {
|
|
|
374
413
|
* `repeat` and `Ascii.digit1`.
|
|
375
414
|
*/
|
|
376
415
|
setHint(offset, expected) {
|
|
416
|
+
if (this.syntax !== void 0) this.syntax.reach(offset);
|
|
377
417
|
if (this.hintOffset === offset) this.hintExpected = mergeExpected(this.hintExpected, expected);
|
|
378
418
|
else {
|
|
379
419
|
this.hintOffset = offset;
|
|
@@ -382,14 +422,14 @@ var State = class {
|
|
|
382
422
|
}
|
|
383
423
|
/** @internal */
|
|
384
424
|
makeFailure(offset, kind, expected) {
|
|
385
|
-
if (this.hintOffset === offset && this.hintExpected
|
|
425
|
+
if (this.hintOffset === offset && Arr.isReadonlyArrayNonEmpty(this.hintExpected)) expected = mergeExpected(this.hintExpected, expected);
|
|
386
426
|
return new Failure(offset, kind, expected, empty, false, void 0, void 0, 0);
|
|
387
427
|
}
|
|
388
428
|
};
|
|
389
429
|
/** @internal */
|
|
390
430
|
const mergeExpected = (a, b) => {
|
|
391
|
-
if (a
|
|
392
|
-
if (b
|
|
431
|
+
if (Arr.isReadonlyArrayEmpty(a)) return b;
|
|
432
|
+
if (Arr.isReadonlyArrayEmpty(b)) return a;
|
|
393
433
|
const out = a.slice();
|
|
394
434
|
for (const e of b) if (!out.includes(e)) out.push(e);
|
|
395
435
|
return out;
|
|
@@ -414,9 +454,8 @@ const mergeFailures = (a, b) => {
|
|
|
414
454
|
*/
|
|
415
455
|
const mergeTiedFailures = (a, b, expected) => {
|
|
416
456
|
if (expected === b.expected) return b;
|
|
417
|
-
return new Failure(b.offset, b.kind, expected, b.context
|
|
457
|
+
return new Failure(b.offset, b.kind, expected, Arr.isReadonlyArrayNonEmpty(b.context) ? b.context : a.context, b.hasCause || a.hasCause, b.hasCause ? b.cause : a.cause, b.message ?? a.message, b.needed);
|
|
418
458
|
};
|
|
419
|
-
//#endregion
|
|
420
459
|
export { Fail, Failure, State, emptyStrings, fromSource, mergeExpected, mergeFailures, mergeTiedFailures };
|
|
421
460
|
|
|
422
461
|
//# sourceMappingURL=Input.js.map
|
package/dist/Input.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Input.js","names":[],"sources":["../src/Input.ts"],"sourcesContent":["/**\n * Input types and the mutable parse {@link State} cursor.\n *\n * Like winnow, effect-parse parsers advance a mutable cursor rather than\n * returning the remaining input. A {@link State} wraps the original input\n * together with the current offset, the streaming (partial) flag, the\n * services available to the parser, and the details of the most recent\n * failure.\n *\n * Most users never touch `State` directly. It is exposed for writing custom\n * low-level parsers with `Parser.make`, and for drivers that run a parser's\n * `step` many times over one input, reusing a state with {@link State.reset}.\n *\n * @since 0.1.0\n */\nimport * as Predicate from \"effect/Predicate\"\nimport * as Context from \"effect/Context\"\nimport type { Encoding } from \"./Encoding.js\"\nimport type { ErrorKind } from \"./ParseError.js\"\nimport type { Builder as SyntaxBuilder } from \"./internal/syntax.js\"\n\n/**\n * The kinds of input a parser can consume.\n *\n * - `string`: tokens are Unicode code points (returned as strings), offsets\n * are UTF-16 code-unit indices.\n * - `Uint8Array`: tokens are bytes (numbers), slices are `subarray` views.\n * - `ReadonlyArray<T>`: tokens are elements, e.g. the output of a lexer.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Input = string | Uint8Array | ReadonlyArray<unknown>\n\n/**\n * Binary data that can be passed wherever a `Uint8Array` input is expected:\n * an `ArrayBuffer`, a `SharedArrayBuffer`, a `DataView` or any typed array.\n *\n * It is viewed, not copied, as a `Uint8Array` of its bytes. A typed array\n * with wider elements is viewed in the platform's byte order, which is\n * little endian on all common platforms, so a `Uint16Array` of UTF-16 code\n * units can be parsed with `Text.utf16le`.\n *\n * @since 0.1.0\n * @category models\n */\nexport type BinaryInput = ArrayBufferLike | ArrayBufferView\n\n/**\n * What the runners accept for a parser over `I`: the input itself, or, for\n * byte parsers, any {@link BinaryInput}.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Source<I> = I | ([I] extends [Uint8Array] ? BinaryInput : never)\n\n/**\n * Converts a {@link Source} to the input parsers read: binary data becomes a\n * `Uint8Array` view of the same memory, everything else is returned as is.\n *\n * @example\n * ```ts\n * import { Input } from \"@thomasfosterau/effect-parse\"\n *\n * Input.fromSource(new Uint16Array([0x41]).buffer) // Uint8Array [0x41, 0x00]\n * ```\n *\n * @since 0.1.0\n * @category constructors\n */\nexport const fromSource: {\n (source: BinaryInput): Uint8Array\n <I extends Input>(source: Source<I>): I\n} = (source: Input | BinaryInput): any => {\n if (Predicate.isString(source) || source instanceof Uint8Array || Array.isArray(source)) return source\n if (ArrayBuffer.isView(source)) return new Uint8Array(source.buffer, source.byteOffset, source.byteLength)\n // An explicit length fixes the view's size even for a resizable buffer\n if (isArrayBufferLike(source)) return new Uint8Array(source, 0, source.byteLength)\n return source\n}\n\nconst isArrayBufferLike = (u: unknown): u is ArrayBufferLike =>\n u instanceof ArrayBuffer || (globalThis.SharedArrayBuffer !== undefined && u instanceof SharedArrayBuffer)\n\n/**\n * The type of a single token of an input.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Token<I> = I extends string\n ? string\n : I extends Uint8Array\n ? number\n : I extends ReadonlyArray<infer T>\n ? T\n : never\n\n/**\n * The type of a contiguous slice of an input.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Slice<I> = I extends string\n ? string\n : I extends Uint8Array\n ? Uint8Array\n : I extends ReadonlyArray<infer T>\n ? ReadonlyArray<T>\n : never\n\n/**\n * Sentinel returned by a parser step to signal failure. The failure details\n * are stored on the {@link State}.\n *\n * @since 0.1.0\n * @category failure\n */\nexport const Fail: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/Fail\")\n\n/**\n * @since 0.1.0\n * @category failure\n */\nexport type Fail = typeof Fail\n\n/**\n * How a failure propagates (winnow's `ErrMode`, nom's `Err`).\n *\n * - `Backtrack`: recoverable; `alt`, `opt`, `repeat`, ... try something else.\n * - `Cut`: unrecoverable; alternatives are not attempted.\n * - `Incomplete`: a partial (streaming) input ran out; more data is needed.\n *\n * @since 0.1.0\n * @category failure\n */\nexport type ErrMode = \"Backtrack\" | \"Cut\" | \"Incomplete\"\n\n/**\n * The details of a parse failure.\n *\n * @since 0.1.0\n * @category failure\n */\nexport class Failure {\n constructor(\n /** Offset into the input where the failure occurred. */\n readonly offset: number,\n /** The kind of parser that failed. */\n readonly kind: ErrorKind,\n /** Rendered descriptions of what was expected at `offset`. */\n readonly expected: ReadonlyArray<string>,\n /** Context labels added by `Parser.context`, innermost first. */\n readonly context: ReadonlyArray<string>,\n /** Whether a user supplied cause is present. */\n readonly hasCause: boolean,\n /** A user supplied (typed) cause. */\n readonly cause: unknown,\n /** An optional free-form message. */\n readonly message: string | undefined,\n /** For `Incomplete` failures, how many more tokens are needed (0 if unknown). */\n readonly needed: number\n ) {}\n}\n\nconst empty: ReadonlyArray<string> = []\n\n/** The failure a {@link State} starts with, before any step has failed. */\nconst initialFailure = /*#__PURE__*/ new Failure(0, \"Fail\", empty, empty, false, undefined, undefined, 0)\n\n/** @internal */\nexport const emptyStrings = empty\n\n/**\n * A tracing event emitted by `Parser.trace`.\n *\n * @since 0.1.0\n * @category tracing\n */\nexport interface TraceEvent {\n readonly _tag: \"Enter\" | \"Exit\"\n readonly name: string\n readonly depth: number\n readonly offset: number\n /** For `Exit` events: the outcome. */\n readonly result?: \"Ok\" | ErrMode\n /** For `Exit` events: the offset reached. */\n readonly endOffset?: number\n readonly state: State\n}\n\n/**\n * The mutable parsing cursor.\n *\n * @since 0.1.0\n * @category models\n */\nexport class State<I extends Input = Input> {\n /** The full input being parsed. */\n readonly input: I\n /** The current offset. Advanced by parsers, rewound on backtracking. */\n // Declared, not defined, so that a subclass can make it an accessor\n declare pos: number\n /** The end offset (the input's length; in bits inside `Bits.bits`). */\n end: number\n /** 0 = string, 1 = Uint8Array, 2 = array. */\n readonly inputKind: 0 | 1 | 2\n /** Whether the input is partial (streaming): running out yields `Incomplete`. */\n partial: boolean\n /** Services available to `Parser.service`. */\n context: Context.Context<never>\n /** Mode of the latest failure. Only meaningful after a step returned {@link Fail}. */\n mode: ErrMode = \"Backtrack\"\n /**\n * The latest failure, kept unmerged: the failure is `failureHead` merged\n * (as `mergeFailures` does) with `failureTie1` and then `failureTie2`, all\n * at the same offset. `Parser.alt` records its tied branch failures here\n * with {@link failTies} instead of merging them, so that an enclosing `alt`\n * whose next branch succeeds never pays for the merge. Reading\n * {@link failure} performs the merge, once.\n *\n * @internal\n */\n failureHead: Failure\n /** @internal */\n failureTie1: Failure | undefined = undefined\n /** @internal */\n failureTie2: Failure | undefined = undefined\n /** Backs {@link deepest}. @internal */\n deepestFailure: Failure | undefined = undefined\n /**\n * Whether the furthest failure is the latest failure, still unmerged, so\n * that {@link deepest} is that failure once merged. Only set while\n * `failureTie1` is.\n *\n * @internal\n */\n deepestIsLatest = false\n /**\n * The offset of a recoverable failure that occurred without consuming\n * input (for example the last attempt of a `repeat`). Its\n * expectations are merged into new failures at the same offset, producing\n * messages like `expected digit or \")\"`.\n */\n hintOffset = -1\n /** The expectations of the hint at {@link hintOffset}. */\n hintExpected: ReadonlyArray<string> = empty\n /**\n * The furthest recoverable failure seen so far, including failures that\n * were later swallowed by backtracking (for example a trailing separator\n * followed by an invalid element). It is reported instead of the final\n * failure when it lies further into the input. This is the classic PEG\n * \"farthest failure\" heuristic. `Parser.cut` keeps only the failures\n * recorded inside it when it commits, and `Parser.peek` and `Parser.not`\n * forget the failures of a lookahead that succeeded.\n */\n get deepest(): Failure | undefined {\n if (this.deepestIsLatest) this.settleFailure()\n return this.deepestFailure\n }\n set deepest(failure: Failure | undefined) {\n this.deepestIsLatest = false\n this.deepestFailure = failure\n }\n /** When defined, `Parser.resumeAfter`/`retryAfter` record failures here. */\n recovered: Array<Failure> | undefined = undefined\n /** When defined, `Parser.trace` reports events here. */\n tracer: ((event: TraceEvent) => void) | undefined = undefined\n /** Current tracing depth. */\n depth = 0\n /**\n * The text encoding of a byte input, recorded by `Text` parsers when they\n * fail so that errors can show the input as text.\n */\n encoding: Encoding<any> | undefined = undefined\n /**\n * The checkpoints of a resumable streaming parse (see\n * `ParserStream.parseAll`); `undefined` otherwise.\n *\n * @internal\n */\n resume: Map<object, Map<number, unknown>> | undefined = undefined\n /**\n * The tree builder of a `Syntax` parse; `undefined` otherwise.\n *\n * @internal\n */\n syntax: SyntaxBuilder | undefined = undefined\n\n constructor(\n source: I | Source<I>,\n options?: {\n readonly partial?: boolean | undefined\n readonly context?: Context.Context<never> | undefined\n readonly offset?: number | undefined\n }\n ) {\n const input = fromSource(source as Input) as I\n this.input = input\n this.end = input.length\n this.pos = options?.offset ?? 0\n this.partial = options?.partial ?? false\n this.context = options?.context ?? Context.empty()\n this.inputKind = Predicate.isString(input) ? 0 : input instanceof Uint8Array ? 1 : 2\n this.failureHead = initialFailure\n }\n\n /** Details of the latest failure. Only meaningful after a step returned {@link Fail}. */\n get failure(): Failure {\n if (this.failureTie1 !== undefined) this.settleFailure()\n return this.failureHead\n }\n set failure(failure: Failure) {\n if (this.deepestIsLatest) this.settleFailure()\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n }\n\n /** Merges the latest failure's ties into it (see {@link failureHead}). */\n private settleFailure(): void {\n let failure = this.failureHead\n const tie1 = this.failureTie1\n if (tie1 !== undefined) {\n failure = mergeTiedFailures(failure, tie1, mergeExpected(failure.expected, tie1.expected))\n const tie2 = this.failureTie2\n if (tie2 !== undefined) failure = mergeTiedFailures(failure, tie2, mergeExpected(failure.expected, tie2.expected))\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n }\n if (this.deepestIsLatest) {\n this.deepestFailure = failure\n this.deepestIsLatest = false\n }\n }\n\n /**\n * Prepares the state for another run over the same input, starting at\n * `offset`: afterwards it behaves exactly like\n * `new State(state.input, { ...options, offset })`, without allocating a\n * new state.\n *\n * Every per-run field is reset: the cursor and end, the latest failure,\n * hints and furthest failure, recorded recoveries, the tracer and tracing\n * depth, the recorded encoding and streaming checkpoints. Like the\n * constructor, `partial` defaults to `false` and `context` to the empty\n * context, so pass them again on every reset.\n *\n * Use it to run many small parses over one buffer, for example one per\n * token, with `parser.step`. A state can be reused after any run,\n * including one that failed, returned `Incomplete` or threw.\n *\n * @example\n * ```ts\n * import { Ascii, Input } from \"@thomasfosterau/effect-parse\"\n *\n * const state = new Input.State(\"12 34\")\n * Ascii.decUint.step(state) // 12\n * state.reset(3)\n * Ascii.decUint.step(state) // 34\n * ```\n *\n * @since 0.2.0\n */\n reset(\n offset: number = 0,\n options?: {\n readonly partial?: boolean | undefined\n readonly context?: Context.Context<never> | undefined\n }\n ): this {\n this.pos = offset\n this.end = this.input.length\n this.partial = options?.partial ?? false\n this.context = options?.context ?? Context.empty()\n this.mode = \"Backtrack\"\n this.failureHead = initialFailure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n this.hintOffset = -1\n this.hintExpected = empty\n this.deepestFailure = undefined\n this.deepestIsLatest = false\n this.recovered = undefined\n this.tracer = undefined\n this.depth = 0\n this.encoding = undefined\n this.resume = undefined\n return this\n }\n\n /**\n * The number of input units (code units, bytes or elements) remaining.\n */\n remaining(): number {\n return this.end - this.pos\n }\n\n /**\n * Whether the cursor is at the end of the input.\n */\n atEnd(): boolean {\n return this.pos >= this.end\n }\n\n /**\n * Extracts the slice `[start, end)` of the input.\n */\n slice(start: number, end: number): Slice<I> {\n const input = this.input as any\n switch (this.inputKind) {\n case 0:\n return input.slice(start, end)\n case 1:\n return input.subarray(start, end)\n default:\n return input.slice(start, end)\n }\n }\n\n /**\n * Records a recoverable failure at the current offset and returns {@link Fail}.\n */\n backtrack(kind: ErrorKind, expected: ReadonlyArray<string> = empty, offset: number = this.pos): Fail {\n return this.failWith(\"Backtrack\", this.makeFailure(offset, kind, expected))\n }\n\n /**\n * Records an unrecoverable failure at the current offset and returns {@link Fail}.\n */\n cut(kind: ErrorKind, expected: ReadonlyArray<string> = empty, offset: number = this.pos): Fail {\n return this.failWith(\"Cut\", this.makeFailure(offset, kind, expected))\n }\n\n /**\n * Records an `Incomplete` failure (only meaningful for partial input) and\n * returns {@link Fail}.\n */\n incomplete(needed: number, expected: ReadonlyArray<string> = empty): Fail {\n return this.failWith(\n \"Incomplete\",\n new Failure(this.pos, \"Incomplete\", expected, empty, false, undefined, undefined, needed)\n )\n }\n\n /**\n * Fails because the input ended: `Incomplete` for partial input, otherwise\n * a `Backtrack` failure of the given kind.\n */\n eoi(kind: ErrorKind, expected: ReadonlyArray<string>, needed: number): Fail {\n return this.partial ? this.incomplete(needed, expected) : this.backtrack(kind, expected)\n }\n\n /**\n * Records a failure carrying a user supplied cause.\n */\n failCause(mode: ErrMode, kind: ErrorKind, cause: unknown, offset: number = this.pos): Fail {\n return this.failWith(mode, new Failure(offset, kind, empty, empty, true, cause, undefined, 0))\n }\n\n /**\n * Records a failure carrying a message.\n */\n failMessage(mode: ErrMode, kind: ErrorKind, message: string, offset: number = this.pos): Fail {\n return this.failWith(mode, new Failure(offset, kind, empty, empty, false, undefined, message, 0))\n }\n\n /**\n * Records the given failure and returns {@link Fail}.\n */\n failWith(mode: ErrMode, failure: Failure): Fail {\n this.mode = mode\n if (this.deepestIsLatest) {\n // The furthest failure is the latest one: it is replaced below, unmerged, if `failure` is as far\n if (mode === \"Backtrack\" && failure.offset >= this.failureHead.offset) this.deepestIsLatest = false\n else this.settleFailure()\n }\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n if (mode === \"Backtrack\" && (this.deepestFailure === undefined || failure.offset >= this.deepestFailure.offset)) {\n this.deepestFailure = failure\n }\n return Fail\n }\n\n /**\n * Records a recoverable failure made of tied failures, merged only when\n * read: `failWith(\"Backtrack\", f)` where `f` merges `head` with `tie1`\n * and then `tie2` (all at the same offset), as `mergeFailures` would.\n *\n * @internal\n */\n failTies(head: Failure, tie1: Failure, tie2: Failure | undefined): Fail {\n this.mode = \"Backtrack\"\n if (this.deepestIsLatest && head.offset < this.failureHead.offset) this.settleFailure()\n this.failureHead = head\n this.failureTie1 = tie1\n this.failureTie2 = tie2\n this.deepestIsLatest = this.deepestFailure === undefined || head.offset >= this.deepestFailure.offset\n return Fail\n }\n\n /**\n * Replaces the details of the current failure (for example to add context),\n * keeping the record of the furthest failure consistent.\n */\n replaceFailure(failure: Failure): void {\n if (this.deepest === this.failure) this.deepest = failure\n this.failure = failure\n }\n\n /**\n * The failure to report to the user: the current failure, or the furthest\n * failure if it lies beyond the current one and the current failure is a\n * plain syntax error (no user cause or message).\n */\n reportedFailure(): Failure {\n const f = this.failure\n const d = this.deepest\n if (\n d === undefined ||\n d === f ||\n d.offset <= f.offset ||\n this.mode === \"Incomplete\" ||\n f.hasCause ||\n f.message !== undefined\n ) {\n return f\n }\n const context =\n d.context.length === 0 ? f.context : [...d.context, ...f.context.filter((l) => !d.context.includes(l))]\n return new Failure(d.offset, d.kind, d.expected, context, d.hasCause, d.cause, d.message, d.needed)\n }\n\n /**\n * Records that, at `offset`, any of `expected` could also have been\n * accepted. Used by parsers that stop without failing, such as `opt`,\n * `repeat` and `Ascii.digit1`.\n */\n setHint(offset: number, expected: ReadonlyArray<string>): void {\n if (this.hintOffset === offset) {\n this.hintExpected = mergeExpected(this.hintExpected, expected)\n } else {\n this.hintOffset = offset\n this.hintExpected = expected\n }\n }\n\n /** @internal */\n makeFailure(offset: number, kind: ErrorKind, expected: ReadonlyArray<string>): Failure {\n if (this.hintOffset === offset && this.hintExpected.length > 0) {\n expected = mergeExpected(this.hintExpected, expected)\n }\n return new Failure(offset, kind, expected, empty, false, undefined, undefined, 0)\n }\n}\n\n/** @internal */\nexport const mergeExpected = (a: ReadonlyArray<string>, b: ReadonlyArray<string>): ReadonlyArray<string> => {\n if (a.length === 0) return b\n if (b.length === 0) return a\n const out = a.slice()\n for (const e of b) {\n if (!out.includes(e)) out.push(e)\n }\n return out\n}\n\n/**\n * Combines the failures of two alternatives: the furthest failure wins, and\n * failures at the same offset have their expectations merged.\n *\n * @internal\n */\nexport const mergeFailures = (a: Failure, b: Failure): Failure => {\n if (a.offset > b.offset) return a\n if (b.offset > a.offset) return b\n return mergeTiedFailures(a, b, mergeExpected(a.expected, b.expected))\n}\n\n/**\n * {@link mergeFailures} for two failures at the same offset, given their\n * merged expectations (`mergeExpected(a.expected, b.expected)`, possibly\n * memoised by the caller).\n *\n * @internal\n */\nexport const mergeTiedFailures = (a: Failure, b: Failure, expected: ReadonlyArray<string>): Failure => {\n if (expected === b.expected) return b\n return new Failure(\n b.offset,\n b.kind,\n expected,\n b.context.length > 0 ? b.context : a.context,\n b.hasCause || a.hasCause,\n b.hasCause ? b.cause : a.cause,\n b.message ?? a.message,\n b.needed\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAuEA,MAAa,cAGR,WAAqC;CACxC,IAAI,UAAU,SAAS,MAAM,KAAK,kBAAkB,cAAc,MAAM,QAAQ,MAAM,GA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AAE,QAAQ,EAAE,OACzB,EAAE,WAAW,EAAE,SACf,EAAE,MACJ;AACF"}
|
|
1
|
+
{"version":3,"file":"Input.js","names":[],"sources":["../src/Input.ts"],"sourcesContent":["/**\n * Input types and the mutable parse {@link State} cursor.\n *\n * Like winnow, effect-parse parsers advance a mutable cursor rather than\n * returning the remaining input. A {@link State} wraps the original input\n * together with the current offset, the streaming (partial) flag, the\n * services available to the parser, and the details of the most recent\n * failure.\n *\n * Most users never touch `State` directly. It is exposed for writing custom\n * low-level parsers with `Parser.make`, and for drivers that run a parser's\n * `step` many times over one input, reusing a state with {@link State.reset}.\n *\n * @since 0.1.0\n */\nimport * as Arr from \"effect/Array\"\nimport * as Predicate from \"effect/Predicate\"\nimport * as Context from \"effect/Context\"\nimport type { Encoding } from \"./Encoding.js\"\nimport type { ErrorKind } from \"./ParseError.js\"\nimport type { Builder as SyntaxBuilder } from \"./internal/syntax.js\"\n\n/**\n * The kinds of input a parser can consume.\n *\n * - `string`: tokens are Unicode code points (returned as strings), offsets\n * are UTF-16 code-unit indices.\n * - `Uint8Array`: tokens are bytes (numbers), slices are `subarray` views.\n * - `ReadonlyArray<T>`: tokens are elements, e.g. the output of a lexer.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Input = string | Uint8Array | ReadonlyArray<unknown>\n\n/**\n * Binary data that can be passed wherever a `Uint8Array` input is expected:\n * an `ArrayBuffer`, a `SharedArrayBuffer`, a `DataView` or any typed array.\n *\n * It is viewed, not copied, as a `Uint8Array` of its bytes. A typed array\n * with wider elements is viewed in the platform's byte order, which is\n * little endian on all common platforms, so a `Uint16Array` of UTF-16 code\n * units can be parsed with `Text.utf16le`.\n *\n * @since 0.1.0\n * @category models\n */\nexport type BinaryInput = ArrayBufferLike | ArrayBufferView\n\n/**\n * What the runners accept for a parser over `I`: the input itself, or, for\n * byte parsers, any {@link BinaryInput}.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Source<I> = I | ([I] extends [Uint8Array] ? BinaryInput : never)\n\n/**\n * Converts a {@link Source} to the input parsers read: binary data becomes a\n * `Uint8Array` view of the same memory, everything else is returned as is.\n *\n * @example\n * ```ts\n * import { Input } from \"@thomasfosterau/effect-parse\"\n *\n * Input.fromSource(new Uint16Array([0x41]).buffer) // Uint8Array [0x41, 0x00]\n * ```\n *\n * @since 0.1.0\n * @category constructors\n */\nexport const fromSource: {\n (source: BinaryInput): Uint8Array\n <I extends Input>(source: Source<I>): I\n} = (source: Input | BinaryInput): any => {\n if (Predicate.isString(source) || source instanceof Uint8Array || Array.isArray(source)) return source\n if (ArrayBuffer.isView(source)) return new Uint8Array(source.buffer, source.byteOffset, source.byteLength)\n // An explicit length fixes the view's size even for a resizable buffer\n if (isArrayBufferLike(source)) return new Uint8Array(source, 0, source.byteLength)\n return source\n}\n\nconst isArrayBufferLike = (u: Input | BinaryInput): u is ArrayBufferLike =>\n u instanceof ArrayBuffer || (globalThis.SharedArrayBuffer !== undefined && u instanceof SharedArrayBuffer)\n\n/**\n * The type of a single token of an input.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Token<I> = I extends string\n ? string\n : I extends Uint8Array\n ? number\n : I extends ReadonlyArray<infer T>\n ? T\n : never\n\n/**\n * The type of a contiguous slice of an input.\n *\n * @since 0.1.0\n * @category models\n */\nexport type Slice<I> = I extends string\n ? string\n : I extends Uint8Array\n ? Uint8Array\n : I extends ReadonlyArray<infer T>\n ? ReadonlyArray<T>\n : never\n\n/**\n * Sentinel returned by a parser step to signal failure. The failure details\n * are stored on the {@link State}.\n *\n * @since 0.1.0\n * @category failure\n */\nexport const Fail: unique symbol = /*#__PURE__*/ Symbol.for(\"effect-parse/Fail\")\n\n/**\n * @since 0.1.0\n * @category failure\n */\nexport type Fail = typeof Fail\n\n/**\n * How a failure propagates (winnow's `ErrMode`, nom's `Err`).\n *\n * - `Backtrack`: recoverable; `alt`, `opt`, `repeat`, ... try something else.\n * - `Cut`: unrecoverable; alternatives are not attempted.\n * - `Incomplete`: a partial (streaming) input ran out; more data is needed.\n *\n * @since 0.1.0\n * @category failure\n */\nexport type ErrMode = \"Backtrack\" | \"Cut\" | \"Incomplete\"\n\n/**\n * The details of a parse failure.\n *\n * @since 0.1.0\n * @category failure\n */\nexport class Failure {\n constructor(\n /** Offset into the input where the failure occurred. */\n readonly offset: number,\n /** The kind of parser that failed. */\n readonly kind: ErrorKind,\n /** Rendered descriptions of what was expected at `offset`. */\n readonly expected: ReadonlyArray<string>,\n /** Context labels added by `Parser.context`, innermost first. */\n readonly context: ReadonlyArray<string>,\n /** Whether a user supplied cause is present. */\n readonly hasCause: boolean,\n /** A user supplied (typed) cause. */\n readonly cause: unknown,\n /** An optional free-form message. */\n readonly message: string | undefined,\n /** For `Incomplete` failures, how many more tokens are needed (0 if unknown). */\n readonly needed: number\n ) {}\n}\n\nconst empty: ReadonlyArray<string> = []\n\n/** The failure a {@link State} starts with, before any step has failed. */\nconst initialFailure = /*#__PURE__*/ new Failure(0, \"Fail\", empty, empty, false, undefined, undefined, 0)\n\n/** @internal */\nexport const emptyStrings = empty\n\n/**\n * A tracing event emitted by `Parser.trace`.\n *\n * @since 0.1.0\n * @category tracing\n */\nexport interface TraceEvent {\n readonly _tag: \"Enter\" | \"Exit\"\n readonly name: string\n readonly depth: number\n readonly offset: number\n /** For `Exit` events: the outcome. */\n readonly result?: \"Ok\" | ErrMode\n /** For `Exit` events: the offset reached. */\n readonly endOffset?: number\n readonly state: State\n}\n\n/**\n * The mutable parsing cursor.\n *\n * @since 0.1.0\n * @category models\n */\nexport class State<I extends Input = Input> {\n /** The full input being parsed. */\n readonly input: I\n /**\n * The current offset. Parsers advance it by assigning it; moving it back\n * (backtracking, or after a lookahead) goes through {@link rewind}.\n */\n pos: number\n /** The end offset (the input's length; in bits inside `Bits.bits`). */\n end: number\n /** 0 = string, 1 = Uint8Array, 2 = array. */\n readonly inputKind: 0 | 1 | 2\n /** Whether the input is partial (streaming): running out yields `Incomplete`. */\n partial: boolean\n /** Services available to `Parser.service`. */\n context: Context.Context<never>\n /** Mode of the latest failure. Only meaningful after a step returned {@link Fail}. */\n mode: ErrMode = \"Backtrack\"\n /**\n * The latest failure, kept unmerged: the failure is `failureHead` merged\n * (as `mergeFailures` does) with `failureTie1` and then `failureTie2`, all\n * at the same offset. `Parser.alt` records its tied branch failures here\n * with {@link failTies} instead of merging them, so that an enclosing `alt`\n * whose next branch succeeds never pays for the merge. Reading\n * {@link failure} performs the merge, once.\n *\n * @internal\n */\n failureHead: Failure\n /** @internal */\n failureTie1: Failure | undefined = undefined\n /** @internal */\n failureTie2: Failure | undefined = undefined\n /** Backs {@link deepest}. @internal */\n deepestFailure: Failure | undefined = undefined\n /**\n * Whether the furthest failure is the latest failure, still unmerged, so\n * that {@link deepest} is that failure once merged. Only set while\n * `failureTie1` is.\n *\n * @internal\n */\n deepestIsLatest = false\n /**\n * The offset of a recoverable failure that occurred without consuming\n * input (for example the last attempt of a `repeat`). Its\n * expectations are merged into new failures at the same offset, producing\n * messages like `expected digit or \")\"`.\n */\n hintOffset = -1\n /** The expectations of the hint at {@link hintOffset}. */\n hintExpected: ReadonlyArray<string> = empty\n /**\n * The furthest recoverable failure seen so far, including failures that\n * were later swallowed by backtracking (for example a trailing separator\n * followed by an invalid element). It is reported instead of the final\n * failure when it lies further into the input. This is the classic PEG\n * \"farthest failure\" heuristic. `Parser.cut` keeps only the failures\n * recorded inside it when it commits, and `Parser.peek` and `Parser.not`\n * forget the failures of a lookahead that succeeded.\n */\n get deepest(): Failure | undefined {\n if (this.deepestIsLatest) this.settleFailure()\n return this.deepestFailure\n }\n set deepest(failure: Failure | undefined) {\n this.deepestIsLatest = false\n this.deepestFailure = failure\n }\n /** When defined, `Parser.resumeAfter`/`retryAfter` record failures here. */\n recovered: Array<Failure> | undefined = undefined\n /** When defined, `Parser.trace` reports events here. */\n tracer: ((event: TraceEvent) => void) | undefined = undefined\n /** Current tracing depth. */\n depth = 0\n /**\n * The text encoding of a byte input, recorded by `Text` parsers when they\n * fail so that errors can show the input as text.\n */\n encoding: Encoding<any> | undefined = undefined\n /**\n * The checkpoints of a resumable streaming parse (see\n * `ParserStream.parseAll`); `undefined` otherwise.\n *\n * @internal\n */\n resume: Map<object, Map<number, unknown>> | undefined = undefined\n /**\n * The tree builder of a `Syntax` parse; `undefined` otherwise. A builder\n * rather than a subclass of `State`, so that every state has one shape.\n *\n * @internal\n */\n syntax: SyntaxBuilder | undefined = undefined\n\n constructor(\n source: I | Source<I>,\n options?: {\n readonly partial?: boolean | undefined\n readonly context?: Context.Context<never> | undefined\n readonly offset?: number | undefined\n }\n ) {\n const input = fromSource(source as Input) as I\n this.input = input\n this.end = input.length\n this.pos = options?.offset ?? 0\n this.partial = options?.partial ?? false\n this.context = options?.context ?? Context.empty()\n this.inputKind = Predicate.isString(input) ? 0 : input instanceof Uint8Array ? 1 : 2\n this.failureHead = initialFailure\n }\n\n /** Details of the latest failure. Only meaningful after a step returned {@link Fail}. */\n get failure(): Failure {\n if (this.failureTie1 !== undefined) this.settleFailure()\n return this.failureHead\n }\n set failure(failure: Failure) {\n if (this.deepestIsLatest) this.settleFailure()\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n }\n\n /** Merges the latest failure's ties into it (see {@link failureHead}). */\n private settleFailure(): void {\n let failure = this.failureHead\n const tie1 = this.failureTie1\n if (tie1 !== undefined) {\n failure = mergeTiedFailures(failure, tie1, mergeExpected(failure.expected, tie1.expected))\n const tie2 = this.failureTie2\n if (tie2 !== undefined) failure = mergeTiedFailures(failure, tie2, mergeExpected(failure.expected, tie2.expected))\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n }\n if (this.deepestIsLatest) {\n this.deepestFailure = failure\n this.deepestIsLatest = false\n }\n }\n\n /**\n * Prepares the state for another run over the same input, starting at\n * `offset`: afterwards it behaves exactly like\n * `new State(state.input, { ...options, offset })`, without allocating a\n * new state.\n *\n * Every per-run field is reset: the cursor and end, the latest failure,\n * hints and furthest failure, recorded recoveries, the tracer and tracing\n * depth, the recorded encoding and streaming checkpoints. Like the\n * constructor, `partial` defaults to `false` and `context` to the empty\n * context, so pass them again on every reset.\n *\n * Use it to run many small parses over one buffer, for example one per\n * token, with `parser.step`. A state can be reused after any run,\n * including one that failed, returned `Incomplete` or threw.\n *\n * @example\n * ```ts\n * import { Ascii, Input } from \"@thomasfosterau/effect-parse\"\n *\n * const state = new Input.State(\"12 34\")\n * Ascii.decUint.step(state) // 12\n * state.reset(3)\n * Ascii.decUint.step(state) // 34\n * ```\n *\n * @since 0.2.0\n */\n reset(\n offset: number = 0,\n options?: {\n readonly partial?: boolean | undefined\n readonly context?: Context.Context<never> | undefined\n }\n ): this {\n this.pos = offset\n this.end = this.input.length\n this.partial = options?.partial ?? false\n this.context = options?.context ?? Context.empty()\n this.mode = \"Backtrack\"\n this.failureHead = initialFailure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n this.hintOffset = -1\n this.hintExpected = empty\n this.deepestFailure = undefined\n this.deepestIsLatest = false\n this.recovered = undefined\n this.tracer = undefined\n this.depth = 0\n this.encoding = undefined\n this.resume = undefined\n return this\n }\n\n /**\n * Moves the cursor back to `offset`, abandoning what was parsed after it:\n * how a parser backtracks, or returns to where a lookahead started. A step\n * that moves the cursor backwards must use this rather than assigning\n * {@link pos}, so that a `Syntax` parse drops the nodes the abandoned\n * attempt built. Moving forward is an assignment.\n *\n * @example\n * ```ts\n * import { Input, Parser } from \"@thomasfosterau/effect-parse\"\n *\n * // Like `Parser.opt`, for one parser\n * const optional = <A>(p: Parser.Parser<A>) =>\n * Parser.make<A | undefined>((state) => {\n * const start = state.pos\n * const a = p.step(state)\n * if (a !== Input.Fail) return a\n * if (state.mode !== \"Backtrack\") return Input.Fail\n * state.rewind(start)\n * return undefined\n * })\n * ```\n *\n * @since 0.4.0\n */\n rewind(offset: number): void {\n const syntax = this.syntax\n if (syntax !== undefined && offset < this.pos) syntax.rewound(this.pos, offset)\n this.pos = offset\n }\n\n /**\n * The number of input units (code units, bytes or elements) remaining.\n */\n remaining(): number {\n return this.end - this.pos\n }\n\n /**\n * Whether the cursor is at the end of the input.\n */\n atEnd(): boolean {\n return this.pos >= this.end\n }\n\n /**\n * Extracts the slice `[start, end)` of the input.\n */\n slice(start: number, end: number): Slice<I> {\n const input = this.input as any\n switch (this.inputKind) {\n case 0:\n return input.slice(start, end)\n case 1:\n return input.subarray(start, end)\n default:\n return input.slice(start, end)\n }\n }\n\n /**\n * Records a recoverable failure at the current offset and returns {@link Fail}.\n */\n backtrack(kind: ErrorKind, expected: ReadonlyArray<string> = empty, offset: number = this.pos): Fail {\n return this.failWith(\"Backtrack\", this.makeFailure(offset, kind, expected))\n }\n\n /**\n * Records an unrecoverable failure at the current offset and returns {@link Fail}.\n */\n cut(kind: ErrorKind, expected: ReadonlyArray<string> = empty, offset: number = this.pos): Fail {\n return this.failWith(\"Cut\", this.makeFailure(offset, kind, expected))\n }\n\n /**\n * Records an `Incomplete` failure (only meaningful for partial input) and\n * returns {@link Fail}.\n */\n incomplete(needed: number, expected: ReadonlyArray<string> = empty): Fail {\n return this.failWith(\n \"Incomplete\",\n new Failure(this.pos, \"Incomplete\", expected, empty, false, undefined, undefined, needed)\n )\n }\n\n /**\n * Fails because the input ended: `Incomplete` for partial input, otherwise\n * a `Backtrack` failure of the given kind.\n */\n eoi(kind: ErrorKind, expected: ReadonlyArray<string>, needed: number): Fail {\n return this.partial ? this.incomplete(needed, expected) : this.backtrack(kind, expected)\n }\n\n /**\n * Records a failure carrying a user supplied cause.\n */\n failCause(mode: ErrMode, kind: ErrorKind, cause: unknown, offset: number = this.pos): Fail {\n return this.failWith(mode, new Failure(offset, kind, empty, empty, true, cause, undefined, 0))\n }\n\n /**\n * Records a failure carrying a message.\n */\n failMessage(mode: ErrMode, kind: ErrorKind, message: string, offset: number = this.pos): Fail {\n return this.failWith(mode, new Failure(offset, kind, empty, empty, false, undefined, message, 0))\n }\n\n /**\n * Records the given failure and returns {@link Fail}.\n */\n failWith(mode: ErrMode, failure: Failure): Fail {\n this.mode = mode\n if (this.deepestIsLatest) {\n // The furthest failure is the latest one: it is replaced below, unmerged, if `failure` is as far\n if (mode === \"Backtrack\" && failure.offset >= this.failureHead.offset) this.deepestIsLatest = false\n else this.settleFailure()\n }\n this.failureHead = failure\n this.failureTie1 = undefined\n this.failureTie2 = undefined\n if (mode === \"Backtrack\" && (this.deepestFailure === undefined || failure.offset >= this.deepestFailure.offset)) {\n this.deepestFailure = failure\n }\n if (this.syntax !== undefined) this.syntax.reach(failure.offset)\n return Fail\n }\n\n /**\n * Records a recoverable failure made of tied failures, merged only when\n * read: `failWith(\"Backtrack\", f)` where `f` merges `head` with `tie1`\n * and then `tie2` (all at the same offset), as `mergeFailures` would.\n *\n * @internal\n */\n failTies(head: Failure, tie1: Failure, tie2: Failure | undefined): Fail {\n this.mode = \"Backtrack\"\n if (this.deepestIsLatest && head.offset < this.failureHead.offset) this.settleFailure()\n this.failureHead = head\n this.failureTie1 = tie1\n this.failureTie2 = tie2\n this.deepestIsLatest = this.deepestFailure === undefined || head.offset >= this.deepestFailure.offset\n if (this.syntax !== undefined) this.syntax.reach(head.offset)\n return Fail\n }\n\n /**\n * Replaces the details of the current failure (for example to add context),\n * keeping the record of the furthest failure consistent.\n */\n replaceFailure(failure: Failure): void {\n if (this.deepest === this.failure) this.deepest = failure\n this.failure = failure\n }\n\n /**\n * The failure to report to the user: the current failure, or the furthest\n * failure if it lies beyond the current one and the current failure is a\n * plain syntax error (no user cause or message).\n */\n reportedFailure(): Failure {\n const f = this.failure\n const d = this.deepest\n if (\n d === undefined ||\n d === f ||\n d.offset <= f.offset ||\n this.mode === \"Incomplete\" ||\n f.hasCause ||\n f.message !== undefined\n ) {\n return f\n }\n const context = Arr.isReadonlyArrayEmpty(d.context)\n ? f.context\n : [...d.context, ...f.context.filter((l) => !d.context.includes(l))]\n return new Failure(d.offset, d.kind, d.expected, context, d.hasCause, d.cause, d.message, d.needed)\n }\n\n /**\n * Records that, at `offset`, any of `expected` could also have been\n * accepted. Used by parsers that stop without failing, such as `opt`,\n * `repeat` and `Ascii.digit1`.\n */\n setHint(offset: number, expected: ReadonlyArray<string>): void {\n if (this.syntax !== undefined) this.syntax.reach(offset)\n if (this.hintOffset === offset) {\n this.hintExpected = mergeExpected(this.hintExpected, expected)\n } else {\n this.hintOffset = offset\n this.hintExpected = expected\n }\n }\n\n /** @internal */\n makeFailure(offset: number, kind: ErrorKind, expected: ReadonlyArray<string>): Failure {\n if (this.hintOffset === offset && Arr.isReadonlyArrayNonEmpty(this.hintExpected)) {\n expected = mergeExpected(this.hintExpected, expected)\n }\n return new Failure(offset, kind, expected, empty, false, undefined, undefined, 0)\n }\n}\n\n/** @internal */\nexport const mergeExpected = (a: ReadonlyArray<string>, b: ReadonlyArray<string>): ReadonlyArray<string> => {\n if (Arr.isReadonlyArrayEmpty(a)) return b\n if (Arr.isReadonlyArrayEmpty(b)) return a\n const out = a.slice()\n for (const e of b) {\n if (!out.includes(e)) out.push(e)\n }\n return out\n}\n\n/**\n * Combines the failures of two alternatives: the furthest failure wins, and\n * failures at the same offset have their expectations merged.\n *\n * @internal\n */\nexport const mergeFailures = (a: Failure, b: Failure): Failure => {\n if (a.offset > b.offset) return a\n if (b.offset > a.offset) return b\n return mergeTiedFailures(a, b, mergeExpected(a.expected, b.expected))\n}\n\n/**\n * {@link mergeFailures} for two failures at the same offset, given their\n * merged expectations (`mergeExpected(a.expected, b.expected)`, possibly\n * memoised by the caller).\n *\n * @internal\n */\nexport const mergeTiedFailures = (a: Failure, b: Failure, expected: ReadonlyArray<string>): Failure => {\n if (expected === b.expected) return b\n return new Failure(\n b.offset,\n b.kind,\n expected,\n Arr.isReadonlyArrayNonEmpty(b.context) ? b.context : a.context,\n b.hasCause || a.hasCause,\n b.hasCause ? b.cause : a.cause,\n b.message ?? a.message,\n b.needed\n )\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAwEA,MAAa,cAGR,WAAqC;CACxC,IAAI,UAAU,SAAS,MAA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AAG,IAAI,KAAK,CAAC;CAElC,OAAO;AACT;;;;;;;AAQA,MAAa,iBAAiB,GAAY,MAAwB;CAChE,IAAI,EAAE,SAAS,EAAE,QAAQ,OAAO;CAChC,IAAI,EAAE,SAAS,EAAE,QAAQ,OAAO;CAChC,OAAO,kBAAkB,GAAG,GAAG,cAAc,EAAE,UAAU,EAAE,QAAQ,CAAC;AACtE;;;;;;;;AASA,MAAa,qBAAqB,GAAY,GAAY,aAA6C;CACrG,IAAI,aAAa,EAAE,UAAU,OAAO;CACpC,OAAO,IAAI,QACT,EAAE,QACF,EAAE,MACF,UACA,IAAI,wBAAwB,EAAE,OAAO,IAAI,EAAE,UAAU,EAAE,SACvD,EAAE,YAAY,EAAE,UAChB,EAAE,WAAW,EAAE,QAAQ,EAAE,OACzB,EAAE,WAAW,EAAE,SACf,EAAE,MACJ;AACF"}
|
package/dist/Nom.d.ts
CHANGED
|
@@ -4,7 +4,6 @@ import * as P from "./Parser.js";
|
|
|
4
4
|
import { Parser } from "./Parser.js";
|
|
5
5
|
import { TokenSet } from "./Token.js";
|
|
6
6
|
import * as Option from "effect/Option";
|
|
7
|
-
//#region src/Nom.d.ts
|
|
8
7
|
/**
|
|
9
8
|
* Matches a literal (nom's `tag`).
|
|
10
9
|
*
|
|
@@ -474,5 +473,4 @@ export declare const restLen: Parser<number, never, never, Input>;
|
|
|
474
473
|
* @category error
|
|
475
474
|
*/
|
|
476
475
|
export declare const context: <A, E, R, I extends Input>(label: string, parser: Parser<A, E, R, I>) => Parser<A, E, R, I>;
|
|
477
|
-
//#endregion
|
|
478
476
|
//# sourceMappingURL=Nom.d.ts.map
|
package/dist/Nom.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Nom.d.ts","names":["TokenOf"],"sources":["../src/Nom.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"Nom.d.ts","names":["TokenOf"],"sources":["../src/Nom.ts"],"mappings":";;;;;;;;;;;;qBA2Da;;;;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QP,UAAW,GAAG,GAAG,GAAG,UAAU,OAAO,eAAe,QAAQ,OAAO,GAAG,GAAG,GAAG,OAAK,OAAO,GAAG,GAAG,GAAG"}
|
package/dist/Nom.js
CHANGED
|
@@ -4,7 +4,7 @@ import * as Token from "./Token.js";
|
|
|
4
4
|
import "effect/Option";
|
|
5
5
|
import * as Ascii from "./Ascii.js";
|
|
6
6
|
import * as Binary from "./Binary.js";
|
|
7
|
-
|
|
7
|
+
// src/Nom.ts
|
|
8
8
|
/**
|
|
9
9
|
* Matches a literal (nom's `tag`).
|
|
10
10
|
*
|
|
@@ -483,7 +483,6 @@ const restLen = Token.restLen;
|
|
|
483
483
|
* @category error
|
|
484
484
|
*/
|
|
485
485
|
const context = (label, parser) => P.context(parser, label);
|
|
486
|
-
//#endregion
|
|
487
486
|
export { allConsuming, alpha0, alpha1, alphanumeric0, alphanumeric1, alt, anychar, beF32, beF64, beI128, beI16, beI24, beI32, beI64, beI8, beU128, beU16, beU24, beU32, beU64, beU8, char, complete, cond, consumed, context, count, crlf, cut, delimited, digit0, digit1, double, eof, escaped, escapedTransform, fail, flatMap, foldMany0, foldMany1, foldManyMN, hexDigit0, hexDigit1, hexU32, i64, isA, isNot, leF32, leF64, leI128, leI16, leI24, leI32, leI64, leI8, leU128, leU16, leU24, leU32, leU64, leU8, lengthCount, lengthData, lengthValue, lineEnding, many0, many1, manyMN, manyTill, map, mapOpt, mapParser, mapRes, multispace0, multispace1, newline, noneOf, not, notLineEnding, octDigit0, octDigit1, oneOf, opt, pair, peek, permutation, preceded, recognize, recognizeFloat, rest, restLen, satisfy, separatedList0, separatedList1, separatedPair, space0, space1, success, tab, tag, tagNoCase, take, takeTill, takeTill1, takeUntil, takeWhile, takeWhile1, takeWhileMN, terminated, tuple, u64, value, verify };
|
|
488
487
|
|
|
489
488
|
//# sourceMappingURL=Nom.js.map
|
package/dist/ParseError.d.ts
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { ErrMode, Failure, Input } from "./Input.js";
|
|
2
2
|
import * as Option from "effect/Option";
|
|
3
3
|
import { Encoding } from "./Encoding.js";
|
|
4
|
-
//#region src/ParseError.d.ts
|
|
5
4
|
/**
|
|
6
5
|
* The kind of parser that produced an error, in the spirit of nom's
|
|
7
6
|
* `ErrorKind`.
|
|
@@ -83,7 +82,7 @@ export declare const isParseError: (u: unknown) => u is ParseError<unknown>;
|
|
|
83
82
|
* @since 0.1.0
|
|
84
83
|
* @category constructors
|
|
85
84
|
*/
|
|
86
|
-
export declare const fromFailure: <E = never>(input: Input, failure: Failure, mode: ErrMode, encoding?: Encoding<any>
|
|
85
|
+
export declare const fromFailure: <E = never>(input: Input, failure: Failure, mode: ErrMode, encoding?: Encoding<any>) => ParseError<E>;
|
|
87
86
|
/**
|
|
88
87
|
* The user supplied cause of an error, if any.
|
|
89
88
|
*
|
|
@@ -110,7 +109,7 @@ export interface LineColumn {
|
|
|
110
109
|
* @since 0.1.0
|
|
111
110
|
* @category utils
|
|
112
111
|
*/
|
|
113
|
-
export declare const lineColumn: (input: Input, offset: number, encoding?: Encoding<any>
|
|
112
|
+
export declare const lineColumn: (input: Input, offset: number, encoding?: Encoding<any>) => LineColumn;
|
|
114
113
|
/**
|
|
115
114
|
* Formats a list of expectations: `a`, `a or b`, `a, b or c`.
|
|
116
115
|
*
|
|
@@ -120,6 +119,5 @@ export declare const lineColumn: (input: Input, offset: number, encoding?: Encod
|
|
|
120
119
|
export declare const formatExpected: (expected: ReadonlyArray<string>) => string;
|
|
121
120
|
/** @internal */
|
|
122
121
|
export declare const inspectToken: (u: unknown) => string;
|
|
123
|
-
//#endregion
|
|
124
122
|
export {};
|
|
125
123
|
//# sourceMappingURL=ParseError.d.ts.map
|
package/dist/ParseError.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"ParseError.d.ts","names":[],"sources":["../src/ParseError.ts"],"mappings":"
|
|
1
|
+
{"version":3,"file":"ParseError.d.ts","names":[],"sources":["../src/ParseError.ts"],"mappings":";;;;;;;;;;;;;YAwCY;;;;;qBAuCC;;;;;;;;;;qBAQA,eAAe,iCACJ;;WAEX,OAAO;;WAEP;;WAEA,MAAM;;WAEN,MAAM;;WAEN,UAAU;;WAEV,SAAS;;WAET,OAAO;;WAEP;;;;;WAKA,WAAW,SAAS;;;;;YAMrB,gBAAgB;;;;;;;MAQtB,YAAY;;;;;;MASH;;;;EAOJ;;;;;;qBASE,eAAgB,eAAa,KAAK;;;;;;;qBAQlC,cAAe,WAC1B,OAAO,OACP,SAAS,SACT,MAAM,SACN,WAAW,kBACV,WAAW;;;;;;;qBAmBD,WAAY,GAAG,MAAM,WAAW,OAAK,OAAO,OAAO;;;;;;;iBAS/C;WACN;WACA;;;;;;;;;;;qBAYE,aAAc,OAAO,OAAO,gBAAgB,WAAW,kBAAgB;;;;;;;qBA6CvE,iBAAkB,UAAU;;qBA8B5B,eAAgB"}
|
package/dist/ParseError.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import * as Predicate from "effect/Predicate";
|
|
2
|
+
import * as Result from "effect/Result";
|
|
2
3
|
import * as Option from "effect/Option";
|
|
3
4
|
import * as text from "./internal/text.js";
|
|
5
|
+
import * as Arr from "effect/Array";
|
|
4
6
|
import * as Data from "effect/Data";
|
|
5
|
-
|
|
7
|
+
// src/ParseError.ts
|
|
6
8
|
/**
|
|
7
9
|
* The error type produced when running a parser.
|
|
8
10
|
*
|
|
@@ -151,7 +153,7 @@ const codePointCount = (s, start, end) => {
|
|
|
151
153
|
* @category utils
|
|
152
154
|
*/
|
|
153
155
|
const formatExpected = (expected) => {
|
|
154
|
-
if (expected
|
|
156
|
+
if (Arr.isReadonlyArrayEmpty(expected)) return "";
|
|
155
157
|
if (expected.length === 1) return expected[0];
|
|
156
158
|
return `${expected.slice(0, -1).join(", ")} or ${expected[expected.length - 1]}`;
|
|
157
159
|
};
|
|
@@ -174,11 +176,7 @@ const describeFound = (input, offset, encoding) => {
|
|
|
174
176
|
/** @internal */
|
|
175
177
|
const inspectToken = (u) => {
|
|
176
178
|
if (Predicate.isString(u)) return JSON.stringify(u);
|
|
177
|
-
if (Predicate.isObjectOrArray(u)) try
|
|
178
|
-
return JSON.stringify(u);
|
|
179
|
-
} catch {
|
|
180
|
-
return String(u);
|
|
181
|
-
}
|
|
179
|
+
if (Predicate.isObjectOrArray(u)) return Result.getOrElse(Result.try(() => JSON.stringify(u)), () => Array.isArray(u) ? u.join(",") : Object.prototype.toString.call(u));
|
|
182
180
|
return String(u);
|
|
183
181
|
};
|
|
184
182
|
const causeMessage = (cause) => {
|
|
@@ -248,11 +246,10 @@ const render = (error) => {
|
|
|
248
246
|
if (cause !== void 0) lines.push(cause);
|
|
249
247
|
if (error.description !== void 0) lines.push(error.description);
|
|
250
248
|
if (error.mode === "Incomplete") lines.push("unexpected end of input (incomplete)");
|
|
251
|
-
else if (error.expected
|
|
249
|
+
else if (Arr.isReadonlyArrayNonEmpty(error.expected)) lines.push(`expected ${formatExpected(error.expected)}, found ${describeFound(input, offset, encoding)}`);
|
|
252
250
|
else if (cause === void 0 && error.description === void 0) lines.push(`unexpected ${describeFound(input, offset, encoding)} (${error.kind})`);
|
|
253
251
|
return lines.join("\n");
|
|
254
252
|
};
|
|
255
|
-
//#endregion
|
|
256
253
|
export { ParseError, TypeId, formatExpected, fromFailure, getCause, inspectToken, isParseError, lineColumn };
|
|
257
254
|
|
|
258
255
|
//# sourceMappingURL=ParseError.js.map
|