parseman 0.46.0 → 0.47.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 +8 -3
- package/dist/analysis/choice-cost-render.d.ts +12 -4
- package/dist/analysis/choice-cost-render.d.ts.map +1 -1
- package/dist/analysis/choice-cost.d.ts +2 -2
- package/dist/analysis/choice-cost.d.ts.map +1 -1
- package/dist/analysis/commitment.d.ts +131 -0
- package/dist/analysis/commitment.d.ts.map +1 -0
- package/dist/analysis/diagnostics.cjs +10339 -7787
- package/dist/analysis/diagnostics.cjs.map +3 -4
- package/dist/analysis/diagnostics.d.ts +2 -2
- package/dist/analysis/diagnostics.js +10339 -7787
- package/dist/analysis/diagnostics.js.map +3 -4
- package/dist/analysis/duplication.d.ts.map +1 -1
- package/dist/analysis/fix.d.ts +3 -3
- package/dist/analysis/fix.d.ts.map +1 -1
- package/dist/analysis/format-number.d.ts +23 -0
- package/dist/analysis/format-number.d.ts.map +1 -0
- package/dist/analysis/gating.d.ts +39 -2
- package/dist/analysis/gating.d.ts.map +1 -1
- package/dist/analysis/rebuild.d.ts.map +1 -1
- package/dist/analysis/terminal.d.ts +5 -2
- package/dist/analysis/terminal.d.ts.map +1 -1
- package/dist/cli/index.cjs +10924 -8506
- package/dist/cli/index.cjs.map +3 -4
- package/dist/cli/index.js +10924 -8506
- package/dist/cli/index.js.map +3 -4
- package/dist/combinators/adjacency.d.ts +100 -0
- package/dist/combinators/adjacency.d.ts.map +1 -0
- package/dist/combinators/choice.d.ts.map +1 -1
- package/dist/combinators/completions.d.ts.map +1 -1
- package/dist/combinators/dispatch.d.ts +8 -1
- package/dist/combinators/dispatch.d.ts.map +1 -1
- package/dist/combinators/expect.d.ts.map +1 -1
- package/dist/combinators/first-set.d.ts.map +1 -1
- package/dist/combinators/gate.d.ts.map +1 -1
- package/dist/combinators/grammar.d.ts +22 -0
- package/dist/combinators/grammar.d.ts.map +1 -1
- package/dist/combinators/literal.d.ts +8 -0
- package/dist/combinators/literal.d.ts.map +1 -1
- package/dist/combinators/map.d.ts +0 -1
- package/dist/combinators/map.d.ts.map +1 -1
- package/dist/combinators/node.d.ts +13 -3
- package/dist/combinators/node.d.ts.map +1 -1
- package/dist/combinators/parser.d.ts +1 -1
- package/dist/combinators/parser.d.ts.map +1 -1
- package/dist/combinators/ref.d.ts.map +1 -1
- package/dist/combinators/regex.d.ts +1 -16
- package/dist/combinators/regex.d.ts.map +1 -1
- package/dist/combinators/repeat.d.ts +30 -2
- package/dist/combinators/repeat.d.ts.map +1 -1
- package/dist/combinators/scanTo.d.ts +71 -1
- package/dist/combinators/scanTo.d.ts.map +1 -1
- package/dist/combinators/sequence.d.ts.map +1 -1
- package/dist/combinators/token.d.ts.map +1 -1
- package/dist/combinators/trivia-boundary.d.ts +26 -0
- package/dist/combinators/trivia-boundary.d.ts.map +1 -0
- package/dist/combinators/trivia-skip.d.ts +45 -1
- package/dist/combinators/trivia-skip.d.ts.map +1 -1
- package/dist/combinators/withCtx.d.ts.map +1 -1
- package/dist/compiler/compile-linkable-table.d.ts +104 -0
- package/dist/compiler/compile-linkable-table.d.ts.map +1 -0
- package/dist/compiler/degradation.d.ts +0 -2
- package/dist/compiler/degradation.d.ts.map +1 -1
- package/dist/compiler/fields.d.ts +13 -0
- package/dist/compiler/fields.d.ts.map +1 -1
- package/dist/compiler/grammar-coverage-ids.d.ts +5 -5
- package/dist/compiler/grammar-coverage-ids.d.ts.map +1 -1
- package/dist/compiler/ir-serialize.d.ts.map +1 -1
- package/dist/compiler/linker.d.ts +47 -53
- package/dist/compiler/linker.d.ts.map +1 -1
- package/dist/compiler/token-alphabet.d.ts +81 -0
- package/dist/compiler/token-alphabet.d.ts.map +1 -0
- package/dist/compiler/token-dispatch.d.ts +95 -0
- package/dist/compiler/token-dispatch.d.ts.map +1 -0
- package/dist/compiler/token-scanner.d.ts +53 -0
- package/dist/compiler/token-scanner.d.ts.map +1 -0
- package/dist/compiler/value-usage.d.ts.map +1 -1
- package/dist/coverage.d.ts +1 -1
- package/dist/coverage.d.ts.map +1 -1
- package/dist/cst/capture-buffer.d.ts +35 -0
- package/dist/cst/capture-buffer.d.ts.map +1 -1
- package/dist/cst/reflection-attach.d.ts +8 -0
- package/dist/cst/reflection-attach.d.ts.map +1 -0
- package/dist/cst/reflection-symbols.d.ts +10 -0
- package/dist/cst/reflection-symbols.d.ts.map +1 -0
- package/dist/cst/reflection.d.ts +2 -4
- package/dist/cst/reflection.d.ts.map +1 -1
- package/dist/cst/root-trivia-scope.d.ts +26 -0
- package/dist/cst/root-trivia-scope.d.ts.map +1 -0
- package/dist/cst/trivia-charscan.d.ts +18 -0
- package/dist/cst/trivia-charscan.d.ts.map +1 -0
- package/dist/cst/trivia-kinds.d.ts +19 -0
- package/dist/cst/trivia-kinds.d.ts.map +1 -1
- package/dist/functional/doc.d.ts.map +1 -1
- package/dist/functional/run-tabled.d.ts +12 -0
- package/dist/functional/run-tabled.d.ts.map +1 -0
- package/dist/functional/run.d.ts +25 -42
- package/dist/functional/run.d.ts.map +1 -1
- package/dist/grammar-metadata.d.ts +3 -0
- package/dist/grammar-metadata.d.ts.map +1 -0
- package/dist/index.cjs +9290 -7181
- package/dist/index.cjs.map +3 -4
- package/dist/index.d.ts +18 -9
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +9300 -7191
- package/dist/index.js.map +3 -4
- package/dist/language-service/index.cjs +313 -121
- package/dist/language-service/index.cjs.map +3 -4
- package/dist/language-service/index.js +313 -121
- package/dist/language-service/index.js.map +3 -4
- package/dist/oracle/index.cjs.map +0 -1
- package/dist/oracle/index.js.map +0 -1
- package/dist/parse-context.d.ts +49 -0
- package/dist/parse-context.d.ts.map +1 -0
- package/dist/plugin/evaluator.d.ts +40 -1
- package/dist/plugin/evaluator.d.ts.map +1 -1
- package/dist/plugin/index.cjs +11175 -9534
- package/dist/plugin/index.cjs.map +3 -4
- package/dist/plugin/index.d.ts.map +1 -1
- package/dist/plugin/index.js +11175 -9534
- package/dist/plugin/index.js.map +3 -4
- package/dist/regex/classes.d.ts +28 -0
- package/dist/regex/classes.d.ts.map +1 -1
- package/dist/regex/first-set.d.ts +14 -0
- package/dist/regex/first-set.d.ts.map +1 -1
- package/dist/run/index.cjs +136 -51
- package/dist/run/index.cjs.map +2 -3
- package/dist/run/index.js +136 -51
- package/dist/run/index.js.map +2 -3
- package/dist/spec/index.cjs +2934 -1611
- package/dist/spec/index.cjs.map +3 -4
- package/dist/spec/index.js +2934 -1611
- package/dist/spec/index.js.map +3 -4
- package/dist/spec/model.d.ts +8 -0
- package/dist/spec/model.d.ts.map +1 -1
- package/dist/table/assemble.d.ts +307 -0
- package/dist/table/assemble.d.ts.map +1 -0
- package/dist/table/cell.d.ts +42 -0
- package/dist/table/cell.d.ts.map +1 -0
- package/dist/table/child-slots.d.ts +30 -0
- package/dist/table/child-slots.d.ts.map +1 -0
- package/dist/table/compile-rule-map.d.ts +136 -0
- package/dist/table/compile-rule-map.d.ts.map +1 -0
- package/dist/table/compile.d.ts +56 -0
- package/dist/table/compile.d.ts.map +1 -0
- package/dist/table/duplication-hook.d.ts +32 -0
- package/dist/table/duplication-hook.d.ts.map +1 -0
- package/dist/table/emit-assembly.d.ts +139 -0
- package/dist/table/emit-assembly.d.ts.map +1 -0
- package/dist/table/emit.d.ts +113 -0
- package/dist/table/emit.d.ts.map +1 -0
- package/dist/table/encode-baseline.d.ts +16 -0
- package/dist/table/encode-baseline.d.ts.map +1 -0
- package/dist/table/encode.d.ts +71 -0
- package/dist/table/encode.d.ts.map +1 -0
- package/dist/table/exec-baseline.d.ts +9 -0
- package/dist/table/exec-baseline.d.ts.map +1 -0
- package/dist/table/exec.d.ts +72 -0
- package/dist/table/exec.d.ts.map +1 -0
- package/dist/table/fold.d.ts +25 -0
- package/dist/table/fold.d.ts.map +1 -0
- package/dist/table/index.cjs +11463 -0
- package/dist/table/index.cjs.map +6 -0
- package/dist/table/index.d.ts +100 -0
- package/dist/table/index.d.ts.map +1 -0
- package/dist/table/index.js +11440 -0
- package/dist/table/index.js.map +6 -0
- package/dist/table/inspect.d.ts +17 -0
- package/dist/table/inspect.d.ts.map +1 -0
- package/dist/table/ops.d.ts +453 -0
- package/dist/table/ops.d.ts.map +1 -0
- package/dist/table/program.d.ts +496 -0
- package/dist/table/program.d.ts.map +1 -0
- package/dist/table/run-support.d.ts +38 -0
- package/dist/table/run-support.d.ts.map +1 -0
- package/dist/{compiler/scannable-run.d.ts → table/scan-shapes.d.ts} +14 -13
- package/dist/table/scan-shapes.d.ts.map +1 -0
- package/dist/table/site-labels.d.ts +58 -0
- package/dist/table/site-labels.d.ts.map +1 -0
- package/dist/table/stamp.d.ts +70 -0
- package/dist/table/stamp.d.ts.map +1 -0
- package/dist/types.d.ts +80 -24
- package/dist/types.d.ts.map +1 -1
- package/dist/version.d.ts +1 -1
- package/package.json +26 -2
- package/src/analysis/choice-cost-gate.ts +359 -0
- package/src/analysis/choice-cost-render.ts +293 -0
- package/src/analysis/choice-cost.ts +1008 -0
- package/src/analysis/commitment.ts +557 -0
- package/src/analysis/corpus.ts +130 -0
- package/src/analysis/diagnose-render.ts +625 -0
- package/src/analysis/diagnose.ts +317 -0
- package/src/analysis/diagnostics.ts +33 -0
- package/src/analysis/duplication.ts +1955 -0
- package/src/analysis/fix-render.ts +217 -0
- package/src/analysis/fix.ts +669 -0
- package/src/analysis/format-number.ts +31 -0
- package/src/analysis/gating.ts +792 -0
- package/src/analysis/grammar.ts +72 -0
- package/src/analysis/rebuild.ts +287 -0
- package/src/analysis/terminal.ts +336 -0
- package/src/cli/index.ts +413 -0
- package/src/combinators/adjacency.ts +171 -0
- package/src/combinators/attempt.ts +41 -0
- package/src/combinators/case-fold.ts +90 -0
- package/src/combinators/choice.ts +466 -0
- package/src/combinators/completions.ts +64 -0
- package/src/combinators/dispatch.ts +358 -0
- package/src/combinators/expect.ts +159 -0
- package/src/combinators/first-set.ts +348 -0
- package/src/combinators/gate.ts +50 -0
- package/src/combinators/grammar.ts +272 -0
- package/src/combinators/keywords.ts +145 -0
- package/src/combinators/literal.ts +102 -0
- package/src/combinators/map.ts +106 -0
- package/src/combinators/node.ts +345 -0
- package/src/combinators/not.ts +55 -0
- package/src/combinators/parser.ts +266 -0
- package/src/combinators/peek.ts +63 -0
- package/src/combinators/probe.ts +25 -0
- package/src/combinators/ref.ts +109 -0
- package/src/combinators/regex.ts +170 -0
- package/src/combinators/repeat.ts +561 -0
- package/src/combinators/scanTo.ts +393 -0
- package/src/combinators/sequence.ts +237 -0
- package/src/combinators/token.ts +120 -0
- package/src/combinators/trivia-boundary.ts +48 -0
- package/src/combinators/trivia-skip.ts +852 -0
- package/src/combinators/withCtx.ts +45 -0
- package/src/compiler/build-arity.ts +330 -0
- package/src/compiler/compile-linkable-table.ts +209 -0
- package/src/compiler/degradation.ts +266 -0
- package/src/compiler/fields.ts +191 -0
- package/src/compiler/grammar-coverage-ids.ts +129 -0
- package/src/compiler/ir-serialize.ts +615 -0
- package/src/compiler/linker.ts +954 -0
- package/src/compiler/token-alphabet.ts +209 -0
- package/src/compiler/token-dispatch.ts +353 -0
- package/src/compiler/token-scanner.ts +318 -0
- package/src/compiler/value-usage.ts +147 -0
- package/src/coverage.ts +383 -0
- package/src/cst/capture-buffer.ts +297 -0
- package/src/cst/host-mode.ts +69 -0
- package/src/cst/reflection-attach.ts +11 -0
- package/src/cst/reflection-symbols.ts +9 -0
- package/src/cst/reflection.ts +149 -0
- package/src/cst/relative-spans.ts +207 -0
- package/src/cst/root-trivia-scope.ts +30 -0
- package/src/cst/trivia-charscan.ts +312 -0
- package/src/cst/trivia-entries.ts +285 -0
- package/src/cst/trivia-index.ts +131 -0
- package/src/cst/trivia-kinds.ts +358 -0
- package/src/cst/types.ts +76 -0
- package/src/cst/walk.ts +101 -0
- package/src/functional/doc.ts +959 -0
- package/src/functional/run-tabled.ts +153 -0
- package/src/functional/run.ts +558 -0
- package/src/grammar-metadata.ts +2 -0
- package/src/index.ts +189 -0
- package/src/language-service/index.ts +221 -0
- package/src/line-index.ts +131 -0
- package/src/oracle/digest.ts +620 -0
- package/src/oracle/index.ts +55 -0
- package/src/parse-context.ts +91 -0
- package/src/plugin/direct-builder-static.ts +102 -0
- package/src/plugin/evaluator.ts +1505 -0
- package/src/plugin/free-identifiers.ts +338 -0
- package/src/plugin/index.ts +2281 -0
- package/src/plugin/reducer-resolver.ts +606 -0
- package/src/recovery/scan.ts +155 -0
- package/src/regex/classes.ts +183 -0
- package/src/regex/first-set.ts +316 -0
- package/src/run/index.ts +31 -0
- package/src/spec/ebnf.ts +128 -0
- package/src/spec/index.ts +54 -0
- package/src/spec/model.ts +636 -0
- package/src/spec/railroad-lib.ts +559 -0
- package/src/spec/railroad.ts +256 -0
- package/src/table/assemble.ts +3006 -0
- package/src/table/cell.ts +42 -0
- package/src/table/child-slots.ts +113 -0
- package/src/table/compile-rule-map.ts +336 -0
- package/src/table/compile.ts +244 -0
- package/src/table/duplication-hook.ts +89 -0
- package/src/table/emit-assembly.ts +1777 -0
- package/src/table/emit.ts +429 -0
- package/src/table/encode-baseline.ts +340 -0
- package/src/table/encode.ts +1569 -0
- package/src/table/exec-baseline.ts +537 -0
- package/src/table/exec.ts +1680 -0
- package/src/table/fold.ts +38 -0
- package/src/table/index.ts +108 -0
- package/src/table/inspect.ts +52 -0
- package/src/table/ops.ts +467 -0
- package/src/table/program.ts +876 -0
- package/src/table/run-support.ts +66 -0
- package/src/table/scan-shapes.ts +1578 -0
- package/src/table/site-labels.ts +228 -0
- package/src/table/stamp.ts +229 -0
- package/src/types.ts +563 -0
- package/src/version.ts +16 -0
- package/dist/compiler/codegen.d.ts +0 -334
- package/dist/compiler/codegen.d.ts.map +0 -1
- package/dist/compiler/inline-build.d.ts +0 -11
- package/dist/compiler/inline-build.d.ts.map +0 -1
- package/dist/compiler/inline-callback.d.ts +0 -21
- package/dist/compiler/inline-callback.d.ts.map +0 -1
- package/dist/compiler/line-index.d.ts +0 -2
- package/dist/compiler/line-index.d.ts.map +0 -1
- package/dist/compiler/module-hoist.d.ts +0 -106
- package/dist/compiler/module-hoist.d.ts.map +0 -1
- package/dist/compiler/scannable-run.d.ts.map +0 -1
- package/dist/compiler/scannable-terminal.d.ts +0 -23
- package/dist/compiler/scannable-terminal.d.ts.map +0 -1
- package/dist/compiler/trivia-fast-path.d.ts +0 -38
- package/dist/compiler/trivia-fast-path.d.ts.map +0 -1
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* THE PUBLIC PARSE PATH — `run()` as the main entry exports it, routed through
|
|
3
|
+
* the TABLE.
|
|
4
|
+
*
|
|
5
|
+
* `functional/run.ts` is the DRIVER: it threads the ctx, skips trailing trivia
|
|
6
|
+
* and shapes a `RunResult`. Handed a function it runs that function; handed a
|
|
7
|
+
* COMBINATOR it walks `combinator.parse()`, which is the interpreter. That
|
|
8
|
+
* second branch is the reason parseman has had three engines reachable at once,
|
|
9
|
+
* and three engines is how a divergence gets to be silent: the interpreter,
|
|
10
|
+
* source lowering and the table each decide dispatch for themselves, and on this
|
|
11
|
+
* branch alone six disagreements were found that no test saw.
|
|
12
|
+
*
|
|
13
|
+
* So the combinator branch stops being a public parse path. This module encodes
|
|
14
|
+
* the combinator to a table, assembles it, and hands the driver the resulting
|
|
15
|
+
* rule FUNCTION — the same artifact a shipped table parser runs. One engine on
|
|
16
|
+
* the product path, by construction rather than by discipline.
|
|
17
|
+
*
|
|
18
|
+
* ## Why this is a separate module and not an edit to `run.ts`
|
|
19
|
+
*
|
|
20
|
+
* `parseman/run` exists to be small, and `test/unit/run-entry-closure.test.ts`
|
|
21
|
+
* pins its module closure to nine leaf modules with a hard "must not reach
|
|
22
|
+
* `src/compiler/` or `src/combinators/`". `encodeTable` reaches both — it reads
|
|
23
|
+
* first sets, build arity and field maps off the combinator graph. Putting the
|
|
24
|
+
* encode inside `run.ts` would drag the compiler and the whole combinator set
|
|
25
|
+
* into every consumer that ships a compiled parser, which is precisely the
|
|
26
|
+
* bundle regression that entry exists to prevent.
|
|
27
|
+
*
|
|
28
|
+
* The split is also the honest one. Encoding is a GRAMMAR-BUILD step, and a
|
|
29
|
+
* consumer of `parseman/run` has no grammar to build: it was handed functions by
|
|
30
|
+
* the macro. So the driver keeps the function path, the main entry adds the
|
|
31
|
+
* encode, and the combinator branch inside the driver is reachable only from
|
|
32
|
+
* tests and the identity sweep — which import `functional/run.ts` directly and
|
|
33
|
+
* must keep reaching all three engines to bisect against.
|
|
34
|
+
*
|
|
35
|
+
* ## The cache is not an optimisation
|
|
36
|
+
*
|
|
37
|
+
* `run()` is called in tight loops. A per-call `encodeTable` + `assemble` would
|
|
38
|
+
* turn a microsecond parse into a millisecond one. Keyed on the combinator's
|
|
39
|
+
* IDENTITY — a `WeakMap`, so a grammar built inside a test and dropped is
|
|
40
|
+
* collected with it — the encode happens once per grammar per process and every
|
|
41
|
+
* later call is one map lookup.
|
|
42
|
+
*/
|
|
43
|
+
import type { Combinator, ParseContext, ParseResult } from '../types.ts'
|
|
44
|
+
import { run as runDriver, type Runnable, type RunOptions, type RunResult } from './run.ts'
|
|
45
|
+
import { encodeTable } from '../table/encode.ts'
|
|
46
|
+
import { tableRules } from '../table/assemble.ts'
|
|
47
|
+
import { closureArtifact } from '../table/program.ts'
|
|
48
|
+
|
|
49
|
+
const ENTRY = 'Entry'
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* The assembled table entry for a combinator, built at most once per combinator.
|
|
53
|
+
*
|
|
54
|
+
* A root combinator is a one-rule map — the same construction `compile()`
|
|
55
|
+
* uses, verified round-tripping value-identically on the shipped json/csv/graphql
|
|
56
|
+
* roots.
|
|
57
|
+
*
|
|
58
|
+
* The settings come off the combinator itself, because they select the table's
|
|
59
|
+
* CONTENTS and so must be known before encoding. `hostMode` decides whether
|
|
60
|
+
* nodes capture for a positioned-CST host, and `trackLines` decides whether
|
|
61
|
+
* literals and regexes carry their line-tracking variant; both are declared on
|
|
62
|
+
* the grammar (`rules({ hostMode, trackLines }, …)`), which is exactly where the
|
|
63
|
+
* interpreter reads them from too.
|
|
64
|
+
*/
|
|
65
|
+
type TableEntry = (input: string, pos: number, ctx: ParseContext) => ParseResult<unknown>
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* TWO slots per combinator, `[ast, cst]`, and that is not a micro-optimisation —
|
|
69
|
+
* it is what makes the routing faithful.
|
|
70
|
+
*
|
|
71
|
+
* `hostMode` is an ENCODE setting: it decides whether every node captures for a
|
|
72
|
+
* positioned-CST host, so it has to be chosen before the table exists. A
|
|
73
|
+
* compiled artifact chooses once, at build time, and `assertHostModeCompatible`
|
|
74
|
+
* then refuses the other host — correctly, because an `'ast'` artifact really
|
|
75
|
+
* did drop its CST branch.
|
|
76
|
+
*
|
|
77
|
+
* The INTERPRETER has no build step and re-decides per parse, so
|
|
78
|
+
* `run(grammar, input, { build: cstBuildHost })` on a grammar that declares no
|
|
79
|
+
* mode has always worked. Encoding once, unconditionally as `'ast'`, would turn
|
|
80
|
+
* that into a thrown host mismatch — a table detail leaking out as a behaviour
|
|
81
|
+
* change in the public API. Keying on the host instead reproduces the
|
|
82
|
+
* interpreter's decision exactly, and pays for it with at most one extra
|
|
83
|
+
* encode per grammar that is genuinely driven both ways.
|
|
84
|
+
*
|
|
85
|
+
* A grammar that DECLARES `hostMode` keeps its declaration: that is a statement
|
|
86
|
+
* about the grammar, and the mismatch error is the right answer to driving it
|
|
87
|
+
* with the wrong host.
|
|
88
|
+
*/
|
|
89
|
+
const TABLED = new WeakMap<Combinator<unknown>, Array<TableEntry | undefined>>()
|
|
90
|
+
|
|
91
|
+
/** Does this `ctx.build` host produce a positioned CST? Mirrors `assemble.ts`. */
|
|
92
|
+
function cstOutput(build: unknown): boolean {
|
|
93
|
+
return (build as { _parsemanCstOutput?: true } | undefined)?._parsemanCstOutput === true
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function tableEntryFor(c: Combinator<unknown>, hostCst: boolean, tolerant: boolean): TableEntry {
|
|
97
|
+
let slots = TABLED.get(c)
|
|
98
|
+
if (slots === undefined) { slots = [undefined, undefined, undefined, undefined]; TABLED.set(c, slots) }
|
|
99
|
+
const i = (hostCst ? 1 : 0) | (tolerant ? 2 : 0)
|
|
100
|
+
const hit = slots[i]
|
|
101
|
+
if (hit !== undefined) return hit
|
|
102
|
+
const declared = c._meta.grammarHostMode
|
|
103
|
+
const hostMode = declared ?? (hostCst ? 'cst' : 'ast')
|
|
104
|
+
const trackLines = c._def.tag === 'grammar' ? c._def.trackLines : false
|
|
105
|
+
const encoded = encodeTable({ [ENTRY]: c }, {
|
|
106
|
+
hostMode,
|
|
107
|
+
...(trackLines ? { trackLines: true } : {}),
|
|
108
|
+
...(tolerant ? { recovery: true } : {}),
|
|
109
|
+
})
|
|
110
|
+
// Match compiler-produced and macro-produced table artifacts: an explicit
|
|
111
|
+
// empty inventory selects the shared closure assembler rather than making
|
|
112
|
+
// this public runtime convenience a hidden `new Function` path.
|
|
113
|
+
const made = tableRules(closureArtifact(encoded))[ENTRY]!
|
|
114
|
+
slots[i] = made
|
|
115
|
+
return made
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* A combinator becomes its table entry; a function is already an artifact.
|
|
120
|
+
*
|
|
121
|
+
* A non-rule (`undefined`, `null`, an object with no `parse`) is passed through
|
|
122
|
+
* UNTOUCHED so the driver's own `run(): start production is …, not a rule`
|
|
123
|
+
* TypeError is what the caller sees. Encoding it first would replace a message
|
|
124
|
+
* that names the mistake with whatever the encoder happens to throw on garbage.
|
|
125
|
+
*/
|
|
126
|
+
function tabled(r: Runnable, hostCst: boolean, tolerant: boolean): Runnable {
|
|
127
|
+
if (typeof r === 'function') return r
|
|
128
|
+
if (typeof (r as Combinator<unknown> | undefined)?.parse !== 'function') return r
|
|
129
|
+
return tableEntryFor(r, hostCst, tolerant)
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Run a grammar against an input.
|
|
134
|
+
*
|
|
135
|
+
* Identical contract to `parseman/run`'s `run()`, with one difference that is
|
|
136
|
+
* the point of this module: a COMBINATOR entry is lowered to a table and the
|
|
137
|
+
* table is what parses. `options.trivia` is lowered the same way — it is a parse
|
|
138
|
+
* of the same grammar and would otherwise be the one place the interpreter still
|
|
139
|
+
* ran on the product path.
|
|
140
|
+
*/
|
|
141
|
+
export function run(entry: Runnable, input: string, options: RunOptions = {}): RunResult {
|
|
142
|
+
const hostCst = cstOutput(options.build)
|
|
143
|
+
// `tolerant` selects the TABLE, not just a flag on the parse: the sync sentinel
|
|
144
|
+
// a list resyncs to is inferred from grammar structure, so it has to be encoded
|
|
145
|
+
// before the table exists — the same reason `hostMode` is a build setting. A
|
|
146
|
+
// grammar driven both ways is encoded both ways, once each.
|
|
147
|
+
const tolerant = options.tolerant === true
|
|
148
|
+
const e = tabled(entry, hostCst, tolerant)
|
|
149
|
+
if (options.trivia === undefined) return runDriver(e, input, options)
|
|
150
|
+
// The trailing-trivia probe runs on a throwaway ctx with no host and nothing to
|
|
151
|
+
// recover, so it is always the plain 'ast' lowering regardless of the entry's.
|
|
152
|
+
return runDriver(e, input, { ...options, trivia: tabled(options.trivia, false, false) })
|
|
153
|
+
}
|