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,120 @@
|
|
|
1
|
+
import type { Combinator, ParseContext, ParseResult, ParserMeta } from '../types.ts'
|
|
2
|
+
import { pushCstLeaf, cstCaptureActive } from '../cst/capture-buffer.ts'
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Treat a parser as one source token.
|
|
6
|
+
*
|
|
7
|
+
* The wrapped parser runs with trivia cleared, so its parts must be contiguous.
|
|
8
|
+
* On success the token returns the matched source text and captures one CST leaf
|
|
9
|
+
* for the full span, suppressing any internal terminal leaves.
|
|
10
|
+
*/
|
|
11
|
+
export function token(root: Combinator<unknown>): Combinator<string> {
|
|
12
|
+
const meta: ParserMeta = {
|
|
13
|
+
firstSet: root._meta.firstSet,
|
|
14
|
+
canMatchNewline: root._meta.canMatchNewline,
|
|
15
|
+
isTrivia: false,
|
|
16
|
+
}
|
|
17
|
+
return {
|
|
18
|
+
_tag: 'token',
|
|
19
|
+
_meta: meta,
|
|
20
|
+
_def: { tag: 'token', parser: root },
|
|
21
|
+
parse(input: string, pos: number, ctx: ParseContext): ParseResult<string> {
|
|
22
|
+
const savedTrivia = ctx.trivia
|
|
23
|
+
const savedKinds = ctx.triviaKindLabels
|
|
24
|
+
const savedBuf = ctx._cstBuf
|
|
25
|
+
const savedChildren = ctx._cstChildren
|
|
26
|
+
const savedLeaves = ctx._cstLeaves
|
|
27
|
+
const savedRaw = ctx._cstRawChildren
|
|
28
|
+
const savedTriviaLog = ctx._cstTriviaLog
|
|
29
|
+
const savedOuterTriviaLog = ctx._triviaLog
|
|
30
|
+
const savedRootTriviaLog = ctx._rootTriviaLog
|
|
31
|
+
const wasCapturing = cstCaptureActive(ctx)
|
|
32
|
+
|
|
33
|
+
ctx.trivia = undefined
|
|
34
|
+
ctx.triviaKindLabels = undefined
|
|
35
|
+
ctx._cstBuf = undefined
|
|
36
|
+
ctx._cstChildren = undefined
|
|
37
|
+
ctx._cstLeaves = undefined
|
|
38
|
+
ctx._cstRawChildren = undefined
|
|
39
|
+
ctx._cstTriviaLog = undefined
|
|
40
|
+
ctx._triviaLog = undefined
|
|
41
|
+
ctx._rootTriviaLog = undefined
|
|
42
|
+
|
|
43
|
+
let result: ParseResult<unknown>
|
|
44
|
+
try {
|
|
45
|
+
result = root.parse(input, pos, ctx)
|
|
46
|
+
} finally {
|
|
47
|
+
ctx.trivia = savedTrivia
|
|
48
|
+
ctx.triviaKindLabels = savedKinds
|
|
49
|
+
ctx._cstBuf = savedBuf
|
|
50
|
+
ctx._cstChildren = savedChildren
|
|
51
|
+
ctx._cstLeaves = savedLeaves
|
|
52
|
+
ctx._cstRawChildren = savedRaw
|
|
53
|
+
ctx._cstTriviaLog = savedTriviaLog
|
|
54
|
+
ctx._triviaLog = savedOuterTriviaLog
|
|
55
|
+
ctx._rootTriviaLog = savedRootTriviaLog
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
if (!result.ok) return result
|
|
59
|
+
|
|
60
|
+
const span = result.span
|
|
61
|
+
const value = input.slice(pos, span.end)
|
|
62
|
+
if (wasCapturing) pushCstLeaf(ctx, { _tag: 'leaf', value, span })
|
|
63
|
+
return { ok: true, value, span }
|
|
64
|
+
},
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Reduce a structural parser to one semantic leaf.
|
|
70
|
+
*
|
|
71
|
+
* Unlike `token()`, `leaf()` does not change trivia policy: put `noTrivia()` or
|
|
72
|
+
* a scoped `parser({ trivia })` inside the supplied grammar when that region has
|
|
73
|
+
* a local spacing rule. Internal CST captures are suppressed, while selected
|
|
74
|
+
* root-source trivia remains visible, and one leaf with the reducer's value and
|
|
75
|
+
* the complete consumed span is exposed to the parent.
|
|
76
|
+
*/
|
|
77
|
+
export function leaf<T, U>(
|
|
78
|
+
root: Combinator<T>,
|
|
79
|
+
fn: (value: T, span: { start: number; end: number }) => U,
|
|
80
|
+
): Combinator<U> {
|
|
81
|
+
const meta: ParserMeta = {
|
|
82
|
+
firstSet: root._meta.firstSet,
|
|
83
|
+
canMatchNewline: root._meta.canMatchNewline,
|
|
84
|
+
isTrivia: false,
|
|
85
|
+
}
|
|
86
|
+
return {
|
|
87
|
+
_tag: 'leaf',
|
|
88
|
+
_meta: meta,
|
|
89
|
+
_def: { tag: 'leaf', parser: root as Combinator<unknown>, fn: fn as (v: unknown, span: { start: number; end: number }) => unknown },
|
|
90
|
+
parse(input: string, pos: number, ctx: ParseContext): ParseResult<U> {
|
|
91
|
+
const savedBuf = ctx._cstBuf
|
|
92
|
+
const savedChildren = ctx._cstChildren
|
|
93
|
+
const savedLeaves = ctx._cstLeaves
|
|
94
|
+
const savedRaw = ctx._cstRawChildren
|
|
95
|
+
const savedTriviaLog = ctx._cstTriviaLog
|
|
96
|
+
const savedOuterTriviaLog = ctx._triviaLog
|
|
97
|
+
const wasCapturing = cstCaptureActive(ctx)
|
|
98
|
+
ctx._cstBuf = undefined
|
|
99
|
+
ctx._cstChildren = undefined
|
|
100
|
+
ctx._cstLeaves = undefined
|
|
101
|
+
ctx._cstRawChildren = undefined
|
|
102
|
+
ctx._cstTriviaLog = undefined
|
|
103
|
+
ctx._triviaLog = undefined
|
|
104
|
+
let result: ParseResult<T>
|
|
105
|
+
try { result = root.parse(input, pos, ctx) }
|
|
106
|
+
finally {
|
|
107
|
+
ctx._cstBuf = savedBuf
|
|
108
|
+
ctx._cstChildren = savedChildren
|
|
109
|
+
ctx._cstLeaves = savedLeaves
|
|
110
|
+
ctx._cstRawChildren = savedRaw
|
|
111
|
+
ctx._cstTriviaLog = savedTriviaLog
|
|
112
|
+
ctx._triviaLog = savedOuterTriviaLog
|
|
113
|
+
}
|
|
114
|
+
if (!result.ok) return result
|
|
115
|
+
const value = fn(result.value, result.span)
|
|
116
|
+
if (wasCapturing) pushCstLeaf(ctx, { _tag: 'leaf', value, span: result.span })
|
|
117
|
+
return { ok: true, value, span: result.span }
|
|
118
|
+
},
|
|
119
|
+
}
|
|
120
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import type { Combinator, ParserDef } from '../types.ts'
|
|
2
|
+
|
|
3
|
+
/** Wrapper nesting this walk will look through before giving up and saying yes. */
|
|
4
|
+
const MAX_DEPTH = 12
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Does this rule body have a position OF ITS OWN at which ambient trivia is
|
|
8
|
+
* consulted?
|
|
9
|
+
*
|
|
10
|
+
* This is the gate on re-establishing a rule's ambient trivia when it is
|
|
11
|
+
* referenced from a region that CLEARED it (`ref.ts` in the interpreter,
|
|
12
|
+
* `Encoder.scopedRef` in the table — both must ask the same question, or the two
|
|
13
|
+
* engines parse different languages again).
|
|
14
|
+
*
|
|
15
|
+
* A body with no such position — a bare alternation, a dispatch, a single
|
|
16
|
+
* terminal — cannot be repaired by installing a scanner, because it never
|
|
17
|
+
* consults one. Installing it there changes nothing about the rule and
|
|
18
|
+
* everything about what its arms delegate to, which is how a `noTrivia(...)`
|
|
19
|
+
* region stopped meaning anything two references down.
|
|
20
|
+
*
|
|
21
|
+
* `lazy` is deliberately false and is never followed: a reference is the
|
|
22
|
+
* delegation this question is about, so following it would both loop and answer
|
|
23
|
+
* for a different rule. `grammar` is false because it declares its own scope, so
|
|
24
|
+
* an outer install never reaches its body. `dispatch` is false because it is a
|
|
25
|
+
* ROUTE: its arms re-enter from the routed token and spell their own padding.
|
|
26
|
+
*
|
|
27
|
+
* Structural and pure — callers resolve it ONCE, not per parse.
|
|
28
|
+
*/
|
|
29
|
+
export function hasOwnTriviaBoundary(p: Combinator<unknown>, depth: number = MAX_DEPTH): boolean {
|
|
30
|
+
// Out of budget: assume yes. The gate exists to REMOVE a scope install that
|
|
31
|
+
// cannot help, and a wrapper stack this deep is not a case it was measured on.
|
|
32
|
+
if (depth === 0) return true
|
|
33
|
+
const d = p._def as ParserDef
|
|
34
|
+
switch (d.tag) {
|
|
35
|
+
// Multi-term forms: the gaps BETWEEN terms are the ambient-trivia positions.
|
|
36
|
+
case 'sequence': return d.parsers.length > 1
|
|
37
|
+
case 'many': case 'oneOrMore': case 'sepBy': return true
|
|
38
|
+
// Transparent wrappers — they add no position, so ask what they wrap.
|
|
39
|
+
case 'node': case 'transform': case 'leaf': case 'label': case 'field':
|
|
40
|
+
case 'token': case 'attempt': case 'optional': case 'expect': case 'withCtx':
|
|
41
|
+
case 'recover': case 'trivia':
|
|
42
|
+
return hasOwnTriviaBoundary(d.parser, depth - 1)
|
|
43
|
+
// An alternation adds no position either, but ONE arm that has one is enough
|
|
44
|
+
// to make the scope meaningful for this rule.
|
|
45
|
+
case 'choice': return d.parsers.some(a => hasOwnTriviaBoundary(a, depth - 1))
|
|
46
|
+
default: return false
|
|
47
|
+
}
|
|
48
|
+
}
|