parseman 0.34.0 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "version": 3,
3
3
  "sources": ["../../src/language-service/index.ts", "../../src/cst/capture-buffer.ts", "../../src/recovery/scan.ts", "../../src/functional/run.ts", "../../src/combinators/completions.ts", "../../src/compiler/scannable-run.ts", "../../src/compiler/trivia-fast-path.ts", "../../src/compiler/linker.ts", "../../src/cst/walk.ts", "../../src/cst/relative-spans.ts", "../../src/functional/doc.ts"],
4
- "sourcesContent": ["/**\n * `parseman/language-service` \u2014 the external, grammar-agnostic IDE layer.\n *\n * A grammar is pure structure (it carries no recovery/completion/lint config).\n * `languageService(grammar, config)` layers editor behaviour ON TOP, keyed by rule\n * name = node type, without ever modifying the grammar:\n *\n * - `parse(src, { tolerant })` \u2192 CST + `ParseError[]` (recovers on the compiled\n * fast path when `grammar` is a `compile(g, { recovery: true })` grammar).\n * - `diagnostics(src)` \u2192 structural parse errors + your per-node-type lint rules.\n * - `completionsAt(src, offset)` \u2192 the grammar's expected-token set at the cursor,\n * mapped through your per-rule semantic completion handlers.\n *\n * The grammar can be the interpreter combinator OR a compiled grammar \u2014 the domain\n * knowledge (what to suggest after `color:`, which nodes to lint) lives here, in the\n * consumer's config, never in the grammar.\n */\nimport type { Combinator, ParseContext, ParseResult, Span, ParseError } from '../types.ts'\nimport { run } from '../functional/run.ts'\nimport { completionsAt as coreCompletionsAt } from '../combinators/completions.ts'\nimport { cstBuildHost } from '../compiler/linker.ts'\nimport { walk, type Walkable } from '../cst/walk.ts'\nimport { parseDoc, type Registry } from '../functional/doc.ts'\nimport { absolutizeCST } from '../cst/relative-spans.ts'\nimport type { NodeLike } from '../cst/types.ts'\n\nexport type Severity = 'error' | 'warning' | 'info'\nexport type Diagnostic = { severity: Severity; message: string; span: Span }\nexport type CompletionItem = { label: string; detail?: string }\n\n/** A CST node/leaf as a diagnostics handler sees it: the walk shape plus span/value. */\nexport type LsNode = Walkable & { readonly span: Span; readonly value?: string }\n\n/** Context handed to a completion handler: which rule the cursor sits in and the\n * grammar's raw expected-token labels there. */\nexport type CompletionContext = { rule: string | null; expected: string[]; offset: number }\n\nexport type LanguageServiceConfig = {\n /** Lint/diagnostic rules keyed by node type (rule name). Return zero or more\n * Diagnostics for each matching node. */\n diagnostics?: Record<string, (node: LsNode) => Diagnostic | Diagnostic[] | null | undefined | void>\n /** Semantic completion handlers keyed by the rule the cursor is in. Turn the\n * grammar's structural expectation into domain suggestions. */\n complete?: Record<string, (ctx: CompletionContext) => CompletionItem[]>\n}\n\n/** Grammar the service drives: an interpreter combinator or a compiled entry fn. */\ntype Grammar = Combinator<unknown> | ((input: string, pos: number, ctx: ParseContext) => ParseResult<unknown>)\n\n/**\n * What you hand `languageService`:\n * - a single entry (combinator or `compile()`d fn) \u2014 stateless methods only, or\n * - `{ rules, root }` (a `rules()` registry + its root rule name) \u2014 also unlocks\n * `openDocument`, the live incremental editor document.\n */\nexport type GrammarInput = Grammar | { rules: Registry<NodeLike>; root: string }\n\n/**\n * A live incremental document \u2014 the fused editor backend. Holds a tolerant\n * `parseDoc` whose tree survives broken input (recovery) and re-parses only the\n * edited span on `edit()`. Diagnostics walk the maintained tree (recovered errors\n * ride inside it as `parseError` nodes), so they stay complete across edits without\n * re-parsing the whole document.\n */\nexport type LsDocument = {\n /** The current CST with ABSOLUTE spans (recovered errors embedded as parseError nodes). */\n readonly tree: LsNode | null\n /** Apply an editor change (byte offsets in the CURRENT text); returns the next document. */\n edit(from: number, to: number, replacement: string): LsDocument\n /** Structural parse errors (from the tree) + your per-node-type lint rules. */\n diagnostics(): Diagnostic[]\n /** Completions at a cursor offset in the current text. */\n completionsAt(offset: number): CompletionItem[]\n}\n\nexport type LanguageService = {\n parse(src: string, opts?: { tolerant?: boolean }): ParseResult<unknown> & { errors: ParseError[] }\n diagnostics(src: string): Diagnostic[]\n completionsAt(src: string, offset: number): CompletionItem[]\n /** Open a live incremental document. Requires `languageService({ rules, root }, \u2026)`. */\n openDocument(src: string): LsDocument\n}\n\nconst errorMessage = (expected: readonly string[]): string =>\n expected.length ? `Unexpected input; expected ${expected.join(' or ')}` : 'Unexpected input'\n\n/**\n * Merge every syntax-error source into one deduped diagnostic list:\n * (a) recovered `parseError` nodes embedded in the tree + per-node lint rules\n * (via {@link diagnoseTree}),\n * (b) residual FLAT recovery/failure errors from the run/doc (present when the\n * parse produced no tree, or as the flat mirror of embedded errors), and\n * (c) a sentinel over any trailing `unconsumedFrom..end` junk the grammar left.\n * Deduped by span+message so an embedded error and its flat twin collapse to one,\n * while distinct diagnostics at the same offset survive. With the reporting fixes\n * (expect() embeds, speculative rollback is clean) (a) is already complete; the\n * merge is what guarantees missing-closers and trailing junk are never invisible.\n */\nfunction mergeDiagnostics(\n tree: LsNode | null,\n flat: ReadonlyArray<{ expected: readonly string[]; span: Span }>,\n unconsumedFrom: number | null,\n inputLength: number,\n config: LanguageServiceConfig,\n): Diagnostic[] {\n const out: Diagnostic[] = []\n const seen = new Set<string>()\n const add = (d: Diagnostic): void => {\n const key = `${d.severity}|${d.span.start}|${d.span.end}|${d.message}`\n if (seen.has(key)) return\n seen.add(key)\n out.push(d)\n }\n if (tree) for (const d of diagnoseTree(tree, config)) add(d)\n for (const e of flat) add({ severity: 'error', message: errorMessage(e.expected), span: e.span })\n if (unconsumedFrom !== null) {\n add({ severity: 'error', message: 'Unexpected input', span: { start: unconsumedFrom, end: inputLength } })\n }\n return out\n}\n\n/** One walk over a CST: recovered `parseError` nodes \u2192 error diagnostics, and each\n * node dispatched to its `config.diagnostics[type]` lint handler. Errors live in the\n * tree, so this is the single source for both \u2014 no separate flat channel to merge. */\nfunction diagnoseTree(root: LsNode, config: LanguageServiceConfig): Diagnostic[] {\n const out: Diagnostic[] = []\n const rules = config.diagnostics\n walk(root, {\n enter(node) {\n if (node._tag === 'parseError') {\n out.push({ severity: 'error', message: errorMessage((node as { expected?: string[] }).expected ?? []), span: node.span })\n return\n }\n const h = rules && node.type !== undefined ? rules[node.type] : undefined\n if (h) {\n const d = h(node)\n if (d) out.push(...(Array.isArray(d) ? d : [d]))\n }\n },\n })\n return out\n}\n\nexport function languageService(grammar: GrammarInput, config: LanguageServiceConfig = {}): LanguageService {\n const build = cstBuildHost()\n const asReg = typeof grammar === 'object' && grammar !== null && 'rules' in grammar\n const registry = asReg ? grammar.rules : undefined\n const root = asReg ? grammar.root : undefined\n const entry: Grammar = asReg ? (registry![root!] as Grammar) : grammar\n\n // The grammar's ambient trivia (interpreter combinators carry it in _meta) \u2014\n // handed to run() so it skips a trailing whitespace/comment tail before\n // reporting unconsumedFrom, matching parseDoc/run semantics.\n const trivia = typeof entry !== 'function' ? (entry as Combinator<unknown>)._meta.grammarTrivia : undefined\n const parseTolerant = (src: string) =>\n run(entry as never, src, { tolerant: true, build, ...(trivia !== undefined ? { trivia } : {}) })\n\n const complete = (input: string, offset: number, treeForCursor: unknown): CompletionItem[] => {\n const expected = coreCompletionsAt(entry as never, input, offset, { tolerant: true })\n const rule = ruleAtCursor(treeForCursor, offset)\n const handler = rule !== null ? config.complete?.[rule] : undefined\n if (handler) return handler({ rule, expected, offset })\n return expected.map(label => ({ label }))\n }\n\n return {\n parse(src, opts) {\n return run(entry as never, src, { tolerant: opts?.tolerant ?? true, build }) as ParseResult<unknown> & { errors: ParseError[] }\n },\n\n diagnostics(src) {\n const r = parseTolerant(src)\n const tree = r.value && typeof r.value === 'object' ? (r.value as LsNode) : null\n return mergeDiagnostics(tree, r.errors, r.unconsumedFrom, src.length, config)\n },\n\n completionsAt(src, offset) {\n return complete(src, offset, parseTolerant(src.slice(0, offset)).value)\n },\n\n openDocument(src) {\n if (!registry || root === undefined) {\n throw new Error('openDocument requires languageService({ rules, root }, config) \u2014 a rules() registry, not a single entry')\n }\n const host = cstBuildHost()\n const make = (doc: ReturnType<typeof parseDoc<NodeLike>>): LsDocument => {\n // Absolutize once per document version (memoized in this closure).\n const abs = doc.tree ? (absolutizeCST(doc.tree as never) as unknown as LsNode) : null\n return {\n get tree() { return abs },\n edit: (from, to, replacement) => make(doc.edit(from, to, replacement)),\n // Same unified merge as the stateless path: embedded parseError nodes +\n // lint, the flat recovery errors the doc collected, and a trailing-junk\n // sentinel \u2014 so a live editor document surfaces missing-closers and junk.\n diagnostics: () => mergeDiagnostics(abs, doc.errors, doc.unconsumedFrom, doc.input.length, config),\n completionsAt: (offset) => complete(doc.input, offset, abs),\n }\n }\n return make(parseDoc<NodeLike>(registry, root, src, { tolerant: true, structuralReuse: true, build: host }))\n },\n }\n}\n\n/** Deepest node whose span brackets the cursor \u2014 \"which rule is the cursor in\".\n * The grammar carries no cursor knowledge, so we reconstruct it from the CST. */\nfunction ruleAtCursor(root: unknown, offset: number): string | null {\n if (!root || typeof root !== 'object') return null\n let best: string | null = null\n walk(root as LsNode, {\n enter(node) {\n const s = node.span\n if (s && s.start <= offset && offset <= s.end && node.type !== undefined) best = node.type\n },\n })\n return best\n}\n", "import type { ParseContext } from '../types.ts'\n\n/** Lazy per-node capture state \u2014 arrays materialized on first push. */\nexport type CstCaptureBuf = {\n single?: unknown | undefined\n ch?: unknown[] | undefined\n rawSingle?: unknown | undefined\n raw?: unknown[] | undefined\n tl?: number[] | undefined\n}\n\nconst EMPTY: unknown[] = []\nconst EMPTY_TL: number[] = []\n\nexport function cstCaptureActive(ctx: ParseContext): boolean {\n return ctx._cstBuf !== undefined || ctx._cstLeaves !== undefined\n}\n\nexport type CstNodeCaptureSaved = {\n ch: ParseContext['_cstChildren']\n lv: ParseContext['_cstLeaves']\n raw: ParseContext['_cstRawChildren']\n tl: ParseContext['_cstTriviaLog']\n cap: boolean | undefined\n buf: CstCaptureBuf | undefined\n}\n\nexport function beginCstNodeCapture(ctx: ParseContext): CstNodeCaptureSaved {\n const saved: CstNodeCaptureSaved = {\n ch: ctx._cstChildren,\n lv: ctx._cstLeaves,\n raw: ctx._cstRawChildren,\n tl: ctx._cstTriviaLog,\n cap: ctx.captureTrivia,\n buf: ctx._cstBuf,\n }\n ctx._cstBuf = {}\n ctx._cstChildren = undefined\n ctx._cstLeaves = undefined\n ctx._cstRawChildren = undefined\n ctx._cstTriviaLog = undefined\n ctx.captureTrivia = true\n return saved\n}\n\nexport function finishCstBuf(buf: CstCaptureBuf | undefined): {\n children: unknown[]\n rawChildren: unknown[]\n triviaLog: number[]\n} {\n if (!buf) return { children: EMPTY, rawChildren: EMPTY, triviaLog: EMPTY_TL }\n const children = buf.ch ?? (buf.single !== undefined ? [buf.single] : EMPTY)\n const rawChildren = buf.raw ?? (buf.rawSingle !== undefined ? [buf.rawSingle] : EMPTY)\n const triviaLog = buf.tl ?? EMPTY_TL\n return { children, rawChildren, triviaLog }\n}\n\nexport function endCstNodeCapture(\n ctx: ParseContext,\n saved: CstNodeCaptureSaved,\n): { children: unknown[]; rawChildren: unknown[]; triviaLog: number[] } {\n const materialized = finishCstBuf(ctx._cstBuf)\n ctx._cstBuf = saved.buf\n ctx._cstChildren = saved.ch\n ctx._cstLeaves = saved.lv\n ctx._cstRawChildren = saved.raw\n ctx._cstTriviaLog = saved.tl\n ctx.captureTrivia = saved.cap\n return materialized\n}\n\nexport function pushCstLeaf(ctx: ParseContext, leaf: unknown): void {\n pushCstChild(ctx, leaf, leaf)\n}\n\n/** Record a built sub-node into the active collector (children vs rawChildren may differ). */\nexport function pushCstChild(ctx: ParseContext, built: unknown, rawEntry: unknown): void {\n const b = ctx._cstBuf\n if (b) {\n if (b.ch) b.ch.push(built)\n else if (b.single !== undefined) { b.ch = [b.single, built]; b.single = undefined }\n else b.single = built\n\n if (b.raw) b.raw.push(rawEntry)\n else if (b.rawSingle !== undefined) { b.raw = [b.rawSingle, rawEntry]; b.rawSingle = undefined }\n else b.rawSingle = rawEntry\n return\n }\n if (ctx._cstChildren) ctx._cstChildren.push(built)\n else if (ctx._cstLeaves) ctx._cstLeaves.push(built)\n if (ctx._cstRawChildren) ctx._cstRawChildren.push(rawEntry)\n}\n\nexport function cstRawLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) {\n if (b.raw) return b.raw.length\n return b.rawSingle !== undefined ? 1 : 0\n }\n return ctx._cstRawChildren?.length ?? 0\n}\n\nexport function cstLeavesLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) {\n if (b.ch) return b.ch.length\n return b.single !== undefined ? 1 : 0\n }\n return ctx._cstLeaves?.length ?? 0\n}\n\nexport function cstTlLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) return b.tl?.length ?? 0\n return ctx._cstTriviaLog?.length ?? 0\n}\n\nexport type CstRollbackMark = { raw: number; tlog: number; leaves: number; fields: number; errors: number }\n\nexport function saveCstMark(ctx: ParseContext): CstRollbackMark {\n return {\n raw: cstRawLen(ctx),\n tlog: cstTlLen(ctx),\n leaves: cstLeavesLen(ctx),\n fields: ctx._fields?.length ?? 0,\n // Speculative rollback must truncate the flat recovery-error sink in lockstep\n // with the CST leaves: a branch that recovered (pushing a ParseError to\n // ctx._errors AND embedding it as a CST child via captureError) then failed\n // must remove BOTH, or the flat error survives as a GHOST that a tree walk\n // can't see (flat run().errors would disagree with the embedded set).\n errors: ctx._errors?.length ?? 0,\n }\n}\n\nfunction rollbackBufList(\n b: CstCaptureBuf,\n keyMulti: 'ch' | 'raw',\n keySingle: 'single' | 'rawSingle',\n len: number,\n): void {\n const arr = b[keyMulti]\n if (arr) {\n if (len === 0) b[keyMulti] = undefined\n else if (len === 1) { b[keySingle] = arr[0]; b[keyMulti] = undefined }\n else arr.length = len\n return\n }\n if (len === 0) b[keySingle] = undefined\n}\n\nexport function rollbackCstCapture(ctx: ParseContext, mark: CstRollbackMark): void {\n // Truncate the flat recovery-error sink alongside the CST, so a rolled-back\n // speculative branch leaves no ghost error (see saveCstMark). Guarded on a\n // present sink + a defined mark (rollbackTrivia forwards the same field).\n if (ctx._errors && mark.errors !== undefined) ctx._errors.length = mark.errors\n const b = ctx._cstBuf\n if (b) {\n rollbackBufList(b, 'raw', 'rawSingle', mark.raw)\n rollbackBufList(b, 'ch', 'single', mark.leaves)\n if (b.tl) {\n if (mark.tlog === 0) b.tl = undefined\n else b.tl.length = mark.tlog\n }\n if (ctx._fields) ctx._fields.length = mark.fields\n return\n }\n if (ctx._cstRawChildren) ctx._cstRawChildren.length = mark.raw\n if (ctx._cstTriviaLog) ctx._cstTriviaLog.length = mark.tlog\n if (ctx._cstLeaves) ctx._cstLeaves.length = mark.leaves\n if (ctx._fields) ctx._fields.length = mark.fields\n}\n\nexport function pushCstTriviaEntry(\n ctx: ParseContext,\n start: number,\n end: number,\n kindIndex?: number,\n): void {\n const insertIdx = cstRawLen(ctx)\n const b = ctx._cstBuf\n const withKind = ctx.triviaKindLabels !== undefined && kindIndex !== undefined\n if (b) {\n if (!b.tl) {\n b.tl = withKind ? [start, end, insertIdx, kindIndex] : [start, end, insertIdx]\n } else if (withKind) {\n b.tl.push(start, end, insertIdx, kindIndex)\n } else {\n b.tl.push(start, end, insertIdx)\n }\n return\n }\n if (ctx._cstTriviaLog) {\n if (withKind) ctx._cstTriviaLog.push(start, end, insertIdx, kindIndex)\n else ctx._cstTriviaLog.push(start, end, insertIdx)\n }\n}\n\nexport function pushTriviaLogEntry(\n ctx: ParseContext,\n start: number,\n end: number,\n kindIndex?: number,\n): void {\n if (!ctx._triviaLog) return\n if (ctx.triviaKindLabels !== undefined && kindIndex !== undefined) {\n ctx._triviaLog.push(start, end, kindIndex)\n } else {\n ctx._triviaLog.push(start, end)\n }\n}\n\nexport function cstDeferredTriviaActive(ctx: ParseContext): boolean {\n return ctx._triviaLog !== undefined || ctx._cstBuf !== undefined || ctx._cstTriviaLog !== undefined\n}\n", "import type { Combinator, FirstSet, ParseContext, ParseError, ParseResult, RecoveryHelpers } from '../types.ts'\nimport { cstCaptureActive, pushCstChild } from '../cst/capture-buffer.ts'\n\n/**\n * Recovery MECHANISM \u2014 sync-source-agnostic. These primitives implement \"scan\n * forward to a sync token and emit a ParseError\" and \"is the sync token here?\".\n * They are deliberately independent of WHERE the sync sentinel comes from: the\n * grammar never carries recovery config. Sync points are inferred automatically\n * from grammar structure (see `infer.ts`) and may be overridden externally by the\n * language service via `ctx._listSync`. This is the salvaged core of the old\n * `combinators/recover-scan.ts`.\n */\n\n/**\n * Run `sentinel` at `pos` as PURE lookahead: succeed/fail only, with every\n * side-effecting sink suppressed (CST leaves/children/trivia, the trivia log,\n * the recovery error sink, and the completions probe). A recovery scan probes the\n * sentinel at many positions and must not capture leaves, log trivia, or pollute\n * `completionsAt`'s furthest-failure set as a byproduct of looking ahead.\n */\nfunction probeAt(sentinel: Combinator<unknown>, input: string, pos: number, ctx: ParseContext): boolean {\n const s = {\n _probe: ctx._probe, _errors: ctx._errors,\n _cstLeaves: ctx._cstLeaves, _cstChildren: ctx._cstChildren, _cstRawChildren: ctx._cstRawChildren,\n _cstTriviaLog: ctx._cstTriviaLog, _triviaLog: ctx._triviaLog, _fields: ctx._fields, _cstBuf: ctx._cstBuf,\n _tolerant: ctx._tolerant, _sync: ctx._sync,\n }\n ctx._probe = undefined; ctx._errors = undefined\n ctx._cstLeaves = undefined; ctx._cstChildren = undefined; ctx._cstRawChildren = undefined\n ctx._cstTriviaLog = undefined; ctx._triviaLog = undefined; ctx._fields = undefined; ctx._cstBuf = undefined\n // A sentinel is pure lookahead: it must not itself enter tolerant recovery (nor\n // read a stale outer `_sync`). Otherwise a sentinel that composes many/sepBy would\n // run recovery on every probed position \u2014 wasted work, and it could resync to the\n // enclosing list's sync token. Cleared here and restored in `finally`.\n ctx._tolerant = undefined; ctx._sync = undefined\n try {\n return sentinel.parse(input, pos, ctx).ok\n } finally {\n ctx._probe = s._probe; ctx._errors = s._errors\n ctx._cstLeaves = s._cstLeaves; ctx._cstChildren = s._cstChildren; ctx._cstRawChildren = s._cstRawChildren\n ctx._cstTriviaLog = s._cstTriviaLog; ctx._triviaLog = s._triviaLog; ctx._fields = s._fields; ctx._cstBuf = s._cstBuf\n ctx._tolerant = s._tolerant; ctx._sync = s._sync\n }\n}\n\n/**\n * Combine two recovery sentinels into one that matches when either does \u2014 used by\n * tolerant `sepBy` to resync to its own separator OR the enclosing delimiter. Cold\n * path only; a bare wrapper so we don't pull `choice()` (and its analysis) in.\n */\nexport function orSentinel(\n a: Combinator<unknown>,\n b: Combinator<unknown> | undefined,\n): Combinator<unknown> {\n if (b === undefined) return a\n return {\n _tag: 'orSentinel',\n _meta: { firstSet: { kind: 'any' }, canMatchNewline: false, isTrivia: false },\n _def: { tag: 'unknown' },\n parse(input: string, pos: number, ctx: ParseContext): ParseResult<unknown> {\n const ra = a.parse(input, pos, ctx)\n if (ra.ok) return ra\n return b.parse(input, pos, ctx)\n },\n }\n}\n\n/**\n * A lightweight zero-width sentinel that \"matches\" (succeeds, consuming nothing)\n * when the input code point at `pos` starts the given first set. Sync inference\n * builds one from a list's follow set so a nested list can resync to the enclosing\n * delimiter with no grammar annotation. Returns `null` when the first set is `any`\n * or `empty` (no usable inference).\n */\nexport function firstSetSentinel(fs: FirstSet): Combinator<null> | null {\n if (fs.kind !== 'ranges' || fs.ranges.length === 0) return null\n const ranges = fs.ranges\n return {\n _tag: 'firstSetSentinel',\n _meta: { firstSet: fs, canMatchNewline: false, isTrivia: false },\n _def: { tag: 'unknown' },\n parse(input: string, pos: number): ParseResult<null> {\n const code = pos < input.length ? input.codePointAt(pos) : undefined\n if (code !== undefined) {\n for (const r of ranges) {\n if (code >= r.lo && code <= r.hi) return { ok: true, value: null, span: { start: pos, end: pos } }\n }\n }\n return { ok: false, expected: [], span: { start: pos, end: pos } }\n },\n }\n}\n\n/**\n * Cold-path recovery scan shared by tolerant `many()` / `sepBy()`. Scans forward\n * from `from` until `sync` matches (or EOF) \u2014 probing the sentinel as pure\n * lookahead \u2014 emits a {@link ParseError} spanning the skipped range, and pushes it\n * to `ctx._errors`. The sync token is NOT consumed; the caller's loop resumes from\n * `end`. Only ever reached after an element has failed, so entirely off the hot path.\n *\n * The caller owns the loop guard: `sepBy` progress is driven by its separator (a\n * zero-width error for a missing element is fine); `many` checks `matchesAt(sync)`\n * itself and breaks when sitting on the terminator, so a zero-width failure can\n * never spin.\n */\nexport function recoverScan(\n input: string,\n from: number,\n ctx: ParseContext,\n sync: Combinator<unknown>,\n expected: string[],\n): { error: ParseError; end: number } {\n let scanPos = from\n while (scanPos < input.length && !probeAt(sync, input, scanPos, ctx)) scanPos++\n const error: ParseError = { _tag: 'parseError', span: { start: from, end: scanPos }, expected }\n ctx._errors?.push(error)\n return { error, end: scanPos }\n}\n\n/**\n * Does `sentinel` match at `pos` (pure lookahead)? Used by the tolerant list loops\n * to detect \"we are sitting on the sync/terminator token\" without consuming it or\n * capturing anything.\n */\nexport function matchesAt(\n sentinel: Combinator<unknown>,\n input: string,\n pos: number,\n ctx: ParseContext,\n): boolean {\n return probeAt(sentinel, input, pos, ctx)\n}\n\n/**\n * Embed a recovered error as a `parseError` CST child at the recovery point, so the\n * error lives IN the tree (rides reused subtrees across incremental edits; a tree\n * walk finds every diagnostic) \u2014 not only in the flat `ctx._errors` side-channel.\n * No-op when CST capture is off (value/AST mode). Both the interpreter list loops\n * and the compiled recovery branches call this (the latter via `_ctx._rec.capture`),\n * so the embedded node is identical on both paths.\n */\nexport function captureError(ctx: ParseContext, error: ParseError): void {\n if (cstCaptureActive(ctx)) pushCstChild(ctx, error, error)\n}\n\n/**\n * The recovery-helper bundle handed to a tolerant parse via `ctx._rec`. A COMPILED\n * grammar can't import these, so the driver (`run`, `parseDoc`) injects them; the\n * interpreter list loops read the same bundle, guaranteeing byte-identical recovery\n * across paths. Shared here so every driver installs the exact same instance.\n */\nexport const REC: RecoveryHelpers = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSetSentinel, capture: captureError }\n", "import type { Combinator, ParseContext, ParseError, ParseResult } from '../types.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/**\n * Run a compiled/interpreted grammar entry against an input and collect the raw\n * outcome a tool needs to shape a parse result \u2014 WITHOUT the consumer having to\n * hand-build a `ctx`, branch on function-vs-combinator, or scan for leftover\n * input itself.\n *\n * A `compile()`/macro grammar hands you a map of bare parse FUNCTIONS; the\n * interpreter hands you COMBINATORS. `run()` invokes either, threads the standard\n * framework ctx (trivia log, recover/expect error sink, the `ctx.build` host,\n * grammar state), and \u2014 given the grammar's trivia parser \u2014 reports where\n * non-trivia input was left unconsumed. The consumer keeps only its own policy:\n * how to shape the tree and how to turn diagnostics into its error type.\n */\n\n/** A compiled rule (macro/`compile()` output) OR an interpreter combinator. */\nexport type Runnable =\n | ((input: string, pos: number, ctx: ParseContext) => ParseResult<unknown>)\n | Combinator<unknown>\n\nexport type RunOptions = {\n /** Optional hooks for a coverage-enabled compiled or macro parser. Ordinary\n * parses omit this completely, so instrumentation has no normal-path cost. */\n instrumentation?: {\n _grammarCoverage?: (id: string) => void\n _grammarTrace?: { write(event: { id: string; phase: 'enter' | 'attempt' | 'selected' | 'success' | 'failure' | 'backtrack' | 'rollback'; offset: number; end?: number }): void }\n }\n /** `ctx.build` host \u2014 makes structural `node()` rules build a CST / AST via the\n * host instead of their own eval builders. Omit for a grammar's own builders. */\n build?: ParseContext['build']\n /** Initial grammar state threaded into `ctx.state`. */\n state?: unknown\n /**\n * The grammar's trivia rule. A root rule consumes trivia BETWEEN terms but not\n * after the last one, so trailing whitespace/comments would otherwise look like\n * unparsed input. Given the trivia rule, `run` skips that tail before computing\n * `unconsumedFrom` \u2014 so only real leftover is reported. Also encodes dialect\n * differences for free: pass the CSS trivia and a trailing `//` counts as\n * leftover; pass the Less trivia (which treats `//` as a line comment) and it\n * doesn't. An UNTERMINATED comment (which the trivia rule won't match) surfaces\n * at its start. Omit to require the parse to reach the exact end itself.\n */\n trivia?: Runnable\n /**\n * Restrict PER-NODE CST trivia capture (the `triviaLog` a node's builder sees)\n * to these trivia kinds \u2014 a bitmask over the grammar's `triviaKindLabels`\n * indices (build it with `triviaKindMask(labels, ['comment', \u2026])`). Unlisted\n * kinds (e.g. whitespace) are skipped over but not recorded per node, so a host\n * that only reads comments doesn't pay to log every whitespace run. The global\n * `triviaLog` returned by `run()` is unaffected. Omit to capture every kind.\n */\n triviaCaptureMask?: number\n /**\n * Activate automatic list recovery. When true, `many`/`sepBy`/`oneOrMore` recover\n * from a failed element \u2014 skip to a sync point (a resume token inferred from the\n * grammar's structure; the grammar carries no recovery config), emit a\n * `ParseError` over the skipped span (collected in `errors`), and keep parsing the\n * rest of the list \u2014 instead of stopping at the first bad element. Omit (the\n * default) for the strict \"one clean error and stop\" behavior, byte-identical to a\n * run with no recovery. Recovery is a cold path: on well-formed input nothing\n * fails, so none of the machinery runs.\n */\n tolerant?: boolean\n /**\n * Run three compiled-parser-only profiling passes: recognizer (no global\n * sinks), structural capture without the host, then the normal host path. This\n * is a measurement boundary, not a parser mode; ordinary `run()` output is\n * unchanged when omitted. The input is parsed THREE times \u2014 each pass gets its\n * own shallow copy of `options.state`, so keep per-parse state shallow (only\n * the top level is isolated between passes).\n */\n profile?: boolean\n}\n\nexport type RunProfilePass = {\n ms: number\n nodes: number\n childSlots: number\n rawSlots: number\n triviaSlots: number\n fieldSlots: number\n hostCalls: number\n}\n\nexport type RunProfile = {\n /** Existing `voidOf(transform(..., () => undefined))` semantics, generalized\n * to compiled structural nodes: no `ch`/`raw`/`tl` capture or raw entries. */\n recognizer: RunProfilePass\n /** Captures children/raw/trivia/fields but suppresses node construction. */\n structuralCapture: RunProfilePass\n /** The ordinary parser path, including its injected build host. */\n hostConstruction: RunProfilePass\n}\n\nexport type RunResult = {\n ok: boolean\n /** The entry's value on success; undefined on failure. */\n value: unknown\n span: { start: number; end: number }\n /** Expected-token set when the TOP-LEVEL parse failed (empty on success). */\n expected: string[]\n /** Recovery diagnostics (tolerant lists / `expect()`) collected during the parse (in order). */\n errors: ParseError[]\n /**\n * Flat trivia log for building a trivia map. Entry width depends on whether the\n * grammar uses labeled trivia: 2 numbers per entry (`start, end`) for plain\n * trivia, 3 numbers per entry (`start, end, kindIndex`) for labeled trivia\n * (grammars with `label()` arms in their trivia choice). Use `buildTriviaIndex`\n * / `triviaEntries` to consume it format-agnostically rather than iterating raw.\n */\n triviaLog: number[]\n /** Offset where unparsed input begins \u2014 the first non-trivia character the parse\n * left unconsumed (trailing trivia skipped when `trivia` is given), or null if\n * the whole input was consumed. This is how you detect \"the grammar stopped short,\n * there's junk here\". Only meaningful on success \u2014 a failed parse reports its own\n * `span`/`expected`. */\n unconsumedFrom: number | null\n /** Present only when `RunOptions.profile` is true. */\n profile?: RunProfile\n}\n\nconst invoke = (r: Runnable, input: string, pos: number, ctx: ParseContext): ParseResult<unknown> =>\n typeof r === 'function' ? r(input, pos, ctx) : r.parse(input, pos, ctx)\n\ntype ProfilePhase = NonNullable<ParseContext['_pmProfile']>['phase']\ntype ProfileState = NonNullable<ParseContext['_pmProfile']>\n\nfunction runOnce(entry: Runnable, input: string, options: RunOptions, phase?: ProfilePhase, profileState?: ProfileState): RunResult {\n if (typeof entry !== 'function' && typeof (entry as Combinator<unknown> | undefined)?.parse !== 'function') {\n throw new TypeError(\n `run(): start production is ${entry === null ? 'null' : typeof entry}, not a rule \u2014 the requested grammar rule does not exist (check the rule name).`,\n )\n }\n const triviaLog: number[] = []\n const errors: ParseError[] = []\n const profile = profileState ?? (phase === undefined\n ? undefined\n : { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 })\n // NOT \"no output\" \u2014 the `capture` phase still allocates per-node children/raw/\n // trivia buffers and records slot counts. This flag only omits the two GLOBAL\n // sinks (`_triviaLog`, `_errors`) from the context; hence `skipGlobalSinks`.\n const skipGlobalSinks = phase === 'recognizer' || phase === 'capture'\n // Grammar-level ambient trivia declared via rules({ trivia }, factory): install\n // it as ctx.trivia so it's ambient for the whole parse (the interpreter path;\n // a compiled entry has it baked in and carries no _meta). parser/noTrivia still\n // override locally.\n const grammarTrivia = typeof entry !== 'function' ? entry._meta.grammarTrivia : undefined\n const grammarScanSkip = typeof entry !== 'function' ? entry._meta.grammarScanSkip : undefined\n const ctx: ParseContext = {\n trackLines: false,\n ...(skipGlobalSinks\n ? { _pmProfile: profile! }\n : { _triviaLog: triviaLog, _errors: errors, ...(profile === undefined ? {} : { _pmProfile: profile }) }),\n build: options.build,\n state: options.state,\n ...(grammarTrivia !== undefined\n ? { trivia: grammarTrivia, ...(grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {}) }\n : {}),\n ...(grammarScanSkip !== undefined ? { scanSkip: grammarScanSkip } : {}),\n ...(options.triviaCaptureMask !== undefined ? { _triviaCaptureMask: options.triviaCaptureMask } : {}),\n ...(options.tolerant ? { _tolerant: true, _rec: REC } : {}),\n ...(options.instrumentation === undefined ? {} : options.instrumentation),\n }\n const r = invoke(entry, input, 0, ctx)\n\n let unconsumedFrom: number | null = null\n if (r.ok) {\n let pos = r.span?.end ?? 0\n if (options.trivia && pos < input.length) {\n // Throwaway ctx: trailing trivia must NOT pollute the parse's trivia log.\n const t = invoke(options.trivia, input, pos, { trackLines: false })\n if (t.ok && t.span.end > pos) pos = t.span.end\n }\n unconsumedFrom = pos < input.length ? pos : null\n }\n\n return {\n ok: r.ok,\n value: r.ok ? (r as { value: unknown }).value : undefined,\n span: r.span ?? { start: 0, end: 0 },\n expected: r.ok ? [] : ((r as { expected?: string[] }).expected ?? []),\n errors,\n triviaLog,\n unconsumedFrom,\n }\n}\n\n/** Shallow copy of `options.state` per pass so an in-place mutation by one\n * profiling pass doesn't leak into the next (the parity check only compares\n * `ok`/`unconsumedFrom`, so a diverged structural output would go unnoticed).\n * Only the top level is isolated \u2014 deeply-nested mutable state is shared; a\n * profiled grammar should keep per-parse state shallow (see the `profile` docs). */\nfunction clonePassState(state: unknown): unknown {\n if (state === null || typeof state !== 'object') return state\n return Array.isArray(state) ? [...state] : { ...(state as Record<string, unknown>) }\n}\n\nfunction profilePass(entry: Runnable, input: string, options: RunOptions, phase: ProfilePhase): { result: RunResult; profile: RunProfilePass } {\n const state: ProfileState = { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 }\n const passOptions: RunOptions = options.state === undefined ? options : { ...options, state: clonePassState(options.state) }\n const start = performance.now()\n const result = runOnce(entry, input, passOptions, phase, state)\n const { phase: _phase, ...counts } = state\n return { result, profile: { ms: performance.now() - start, ...counts } }\n}\n\nexport function run(entry: Runnable, input: string, options: RunOptions = {}): RunResult {\n if (!options.profile) return runOnce(entry, input, options)\n if (typeof entry !== 'function') {\n throw new TypeError('run({ profile: true }) requires a compiled parser entry')\n }\n\n const recognizer = profilePass(entry, input, options, 'recognizer')\n const capture = profilePass(entry, input, options, 'capture')\n const host = profilePass(entry, input, options, 'host')\n if (recognizer.result.ok !== host.result.ok || capture.result.ok !== host.result.ok\n || recognizer.result.unconsumedFrom !== host.result.unconsumedFrom\n || capture.result.unconsumedFrom !== host.result.unconsumedFrom) {\n throw new Error('run({ profile: true }) changed recognition; the grammar is not profile-safe')\n }\n return {\n ...host.result,\n profile: {\n recognizer: recognizer.profile,\n structuralCapture: capture.profile,\n hostConstruction: host.profile,\n },\n }\n}\n", "import type { Combinator, ParseFail, ParseContext, ParseResult } from '../types.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/** Anything `completionsAt` can probe: an interpreter combinator OR a `compile()`d\n * grammar. A grammar compiled with `{ recovery: true }` records the probe on its fast\n * path, so completions work on the published compiled artifact \u2014 no interpreter needed. */\ntype CompletionsTarget =\n | Combinator<unknown>\n | { parseWithContext(input: string, ctx: ParseContext, pos?: number): ParseResult<unknown> }\n\n/**\n * Returns the set of expected tokens at the given cursor offset.\n * Useful for implementing IDE completions.\n *\n * Runs the parser on input truncated at `offset` with a probe that tracks the\n * highest-position failure seen, even when sepBy/many backtrack past the cursor.\n * Returns the expected tokens from that deepest failure.\n *\n * Works on the interpreter combinator OR a `compile(g, { recovery: true })` grammar\n * (the probe is recorded on the compiled fast path). Returns an empty array when the\n * input up to `offset` parses completely with no failures at or before the cursor.\n *\n * The returned strings use the same labels as ParseFail.expected:\n * quoted literals like `\"\\\"{\\\"\"` and regex patterns like `\"/[0-9]+/\"`.\n */\nexport function completionsAt(\n target: CompletionsTarget,\n input: string,\n offset: number,\n options: { tolerant?: boolean } = {},\n): string[] {\n const probe: { offset: number; best: ParseFail | null } = { offset, best: null }\n // In tolerant mode, list recovery keeps the enclosing node parsing past a bad\n // element so the failure at the cursor is actually recorded \u2014 the completion set\n // is otherwise empty when a permissive top rule \"succeeds\" with an unconsumed tail.\n const ctx: ParseContext = { trackLines: false, _probe: probe, ...(options.tolerant ? { _tolerant: true, _rec: REC } : {}) }\n const sliced = input.slice(0, offset)\n const result = 'parseWithContext' in target\n ? target.parseWithContext(sliced, ctx, 0)\n : target.parse(sliced, 0, ctx)\n\n // If the parser consumed everything up to offset successfully, there is nothing\n // to complete \u2014 the input is already valid at this position. In tolerant mode the\n // parse \"succeeds\" (list recovery swallowed the incomplete element at the cursor),\n // so the meaningful expectation lives in the deepest failure the probe recorded\n // while recovering: return that instead of an empty set.\n if (result.ok) return options.tolerant ? (probe.best?.expected ?? []) : []\n\n // Use whichever failure (probe or top-level) sits at the deeper position.\n const best = deeperFail(probe.best, result)\n return best?.expected ?? []\n}\n\nfunction deeperFail(a: ParseFail | null, b: ParseFail | null): ParseFail | null {\n if (!a) return b\n if (!b) return a\n return a.span.start >= b.span.start ? a : b\n}\n", "/**\n * Structural recognition of \"scannable\" parser arms \u2014 regex shapes that lower to\n * a tight character-scan loop instead of `regex.exec` / combinator dispatch. Each\n * shape is derived from the regex STRUCTURE, not from any hardcoded knowledge\n * that a given regex \"is whitespace\" or \"is a comment\":\n *\n * [X]+ / [X]* \u2192 run while the char \u2208 X (chars)\n * <lit>[^X]* \u2192 consume <lit>, run until char \u2208 X (until)\n * <open>(?:\u2026)*<close> \u2192 consume <open>, run to <close> literal (delimited)\n * x?<lit><[X]*>\u2026 \u2192 a general linear chain of lit/run (seq)\n *\n * `seq` is the CATEGORY generalization: any chain of literal segments (required\n * or optional `x?`) and char-class runs (positive/negated, `?`/`*`/`+`). It\n * subsumes CSS/Less `-?ident`, `--custom`, `@-?keyword`, `[^\u2026]+`, `::?`, etc.\n * without hardcoding a single byte \u2014 lowered only when a greedy one-pass scan\n * provably equals the engine's backtracking (see `seqIsUnambiguous`).\n *\n * A `oneOrMore(choice(a, b, \u2026))` where every arm is one of these compiles to a\n * single char-dispatch loop with one branch per arm \u2014 any count/order, because\n * the shapes dispatch on their first 1\u20132 chars and are checked in turn. Trivia\n * (whitespace + comments) is just the value-discarded instance of this; nothing\n * here is trivia-specific.\n */\n\n/**\n * One segment of a `seq` shape. A `seq` is the general category \"a fixed linear\n * chain of literals and char-class runs\" \u2014 no alternation, no groups, no\n * backtracking. It subsumes every CSS/Less token that is \"(optional prefix)?\n * literal* char-run* \u2026\" without hardcoding any particular byte:\n *\n * lit \u2014 a run of fixed code points, optionally present (`x?`) or required.\n * run \u2014 a char-class run: positive/negated ranges, min 0/1, bounded to one\n * char (`[x]`, `[x]?`) or unbounded (`[x]*`, `[x]+`).\n * group \u2014 a non-capturing sub-pattern `(?:\u2026)` (\u00A78f), quantified the same way\n * as `run` (`min`/`unbounded`). `inner` is any already-recognized\n * `ScanShape` for the group's body (so a group can itself contain\n * `seq`/`chars`/`alt`/\u2026 \u2014 including a nested alternation via \u00A78e).\n * Only used when `groupInnerSafe(inner)` holds (see that function).\n *\n * We only lower a `seq` when greedy left-to-right scanning provably equals the\n * regex engine's backtracking match (see `seqIsUnambiguous`).\n */\nexport type SeqPart =\n | { part: 'lit'; cps: number[]; optional: boolean }\n // A char-class run with a general counted quantifier: `min`..`max` occurrences.\n // `max === Infinity` is unbounded (`+`/`*`/`{n,}`). The four legacy shapes map\n // as `+`\u2192{1,\u221E} `*`\u2192{0,\u221E} `?`\u2192{0,1} bare\u2192{1,1}; `{n,m}` (\u00A78c) fills the rest.\n // Greedy scanning has \"wiggle\" a backtracker could exploit iff `max > min`.\n | { part: 'run'; ranges: Array<[number, number]>; negated: boolean; min: number; max: number }\n | { part: 'group'; inner: ScanShape; min: 0 | 1; unbounded: boolean }\n\nexport type ScanShape =\n | { kind: 'chars'; ranges: Array<[number, number]>; minOne: boolean }\n | { kind: 'ident'; head: Array<[number, number]>; tail: Array<[number, number]> }\n | { kind: 'until'; open: number[]; stop: Array<[number, number]> }\n | { kind: 'delimited'; open: number[]; close: number[] }\n // <q>(?:[^<q>\\\\]|\\\\.)*<q> \u2014 a quote-delimited string with backslash escapes.\n // `excluded` = the body's negated class (always contains the quote + backslash,\n // maybe a newline); `escLineTerm` = whether `\\\\` may be followed by a line\n // terminator (`\\\\[\\s\\S]` \u2192 true, `\\\\.` \u2192 false, since `.` excludes them).\n | { kind: 'string'; quote: number; excluded: Array<[number, number]>; escLineTerm: boolean }\n // A linear chain of literal/char-run segments (optional prefixes, literal\n // openers, negated runs, \u2026) \u2014 the general CSS/Less token category.\n | { kind: 'seq'; parts: SeqPart[] }\n // A pure literal matched case-insensitively (`/i`): the general \"ASCII\n // case-fold literal\" category (CSS `url(`, `@media` keywords under `i`, \u2026).\n | { kind: 'litFold'; open: number[] }\n // `<inner>(?!class)` / `<inner>(?=class)` \u2014 a trailing lookahead boundary\n // check on an already-lowered shape. Zero-width: `end` is `inner`'s end, never\n // extended. `classNegated` is the bracket's OWN `[^\u2026]` negation (e.g.\n // `(?![0-9a-fA-F])` vs `(?=[^0-9])`), independent of `negative` (`!` vs `=`).\n | { kind: 'lookahead'; inner: ScanShape; ranges: Array<[number, number]>; classNegated: boolean; negative: boolean }\n // `A|B|C` \u2014 top-level alternation (\u00A78e), split outside any `[]`/`()`, each arm\n // independently lowered. `disjoint` is true iff every pair of arms has\n // provably non-overlapping first-char sets (`firsts[i]` holds that arm's set,\n // or `null` if it could start with any char \u2014 an unbounded-optional `chars`\n // arm, or a non-disjoint mix). When `disjoint`, codegen dispatches straight to\n // the one matching arm; otherwise it tries arms in order and takes the first\n // that succeeds \u2014 the same ordered-choice semantics as regex `|` itself (see\n // the ordered-vs-longest verification in PERF_IDEAS \u00A78e).\n | { kind: 'alt'; arms: ScanShape[]; disjoint: boolean; firsts: Array<CharSet | null> }\n\n/** Backslash (`\\`) code point \u2014 the escape lead char in string shapes. */\nexport const BACKSLASH = 92\n\nconst META = new Set('()[]{}*+?|^$.'.split(''))\n\n// Regex char-class primitives are shared with the interpreter's first-set\n// analyzer and `regex()`'s scanner \u2014 see `../regex/classes.ts`. `SPACE_RANGES`\n// and `parseClassRanges` are re-exported here because codegen's boundary-class\n// lowering imports them from this module.\nimport { CLASS_ESCAPES, SPACE_RANGES, shorthandRanges, readUnicodeEscape, parseClassRanges } from '../regex/classes.ts'\nexport { SPACE_RANGES, parseClassRanges }\n\n/** A single class token (`[...]` or `\\d`/`\\w`) to ranges, or null. Rejects negation. */\nfunction classToRanges(cls: string): Array<[number, number]> | null {\n if (cls === '\\\\d') return shorthandRanges('d')\n if (cls === '\\\\w') return shorthandRanges('w')\n if (cls === '\\\\s') return shorthandRanges('s')\n const body = cls.slice(1, -1)\n if (body.startsWith('^')) return null\n return parseClassRanges(body)\n}\n\n/** ASCII case-insensitive letter check for `i`-flag literal lowering. */\n// ASCII case-insensitive char compare. For a letter, upper/lower differ only in\n// bit 0x20, and `(c | 32) === <lowercase>` is satisfied by EXACTLY that letter's\n// two cases (nothing else) \u2014 one OR + one compare, ~1.75\u00D7 faster than the\n// two-compare `(c===U || c===L)` form (measured). `(\u2026 | 32)` is parenthesized\n// because `===` binds tighter than `|`. Non-letters keep an exact compare.\n// Exported so the case-insensitive `literal()` emitter (codegen) shares the exact\n// same ASCII fold as `/i`-flag regex lowering \u2014 no `Intl.Collator` on any path.\nexport const foldEq = (cVar: string, cp: number): string => {\n if (cp >= 65 && cp <= 90) return `(${cVar} | 32) === ${cp + 32}`\n if (cp >= 97 && cp <= 122) return `(${cVar} | 32) === ${cp}`\n return `${cVar} === ${cp}`\n}\n\n/** All-literal regex fragment (`\\/\\*`, `\\/\\/`, \u2026) \u2192 its code points, or null on an unescaped metachar. */\nfunction literalCodePoints(frag: string): number[] | null {\n const out: number[] = []\n let i = 0\n while (i < frag.length) {\n const ch = frag[i]!\n if (ch === '\\\\') {\n const e = frag[i + 1]\n if (e === undefined) return null\n out.push(e in CLASS_ESCAPES ? CLASS_ESCAPES[e]! : e.codePointAt(0)!)\n i += 2\n continue\n }\n if (META.has(ch)) return null\n out.push(ch.codePointAt(0)!)\n i += 1\n }\n return out.length ? out : null\n}\n\n/**\n * Recognize a quote-delimited string with backslash escapes:\n * <q>(?:[^\u2026]|\\\\.)*<q> or <q>(?:\\\\.|[^\u2026])*<q> (`?:` optional)\n * where the negated body class contains the quote and the backslash, and the\n * escape body is `.` (no line terminators) or `[\\s\\S]` (any char). Returns null\n * for anything else so the caller falls back to `regex.exec`.\n */\nfunction parseStringShape(source: string): ScanShape | null {\n const q = source[0]\n if (q === undefined || q === '\\\\' || META.has(q)) return null\n if (source.length < 3 || source[source.length - 1] !== q) return null\n const inner = source.slice(1, -1)\n const CLS = String.raw`\\[\\^((?:\\\\.|[^\\]])+)\\]`\n const ESC = String.raw`\\\\\\\\(\\.|\\[\\\\s\\\\S\\]|\\[\\\\S\\\\s\\])`\n // Two arm orders; `?:` optional. Capture groups differ, so read both out.\n let body: string | undefined\n let esc: string | undefined\n let m = new RegExp(`^\\\\((?:\\\\?:)?${CLS}\\\\|${ESC}\\\\)\\\\*$`).exec(inner)\n if (m) { body = m[1]; esc = m[2] }\n else {\n m = new RegExp(`^\\\\((?:\\\\?:)?${ESC}\\\\|${CLS}\\\\)\\\\*$`).exec(inner)\n if (m) { esc = m[1]; body = m[2] }\n }\n if (body === undefined || esc === undefined) return null\n const excluded = parseClassRanges(body)\n if (!excluded) return null\n const qcp = q.codePointAt(0)!\n const inSet = (cp: number) => excluded.some(([lo, hi]) => cp >= lo && cp <= hi)\n if (!inSet(qcp) || !inSet(BACKSLASH)) return null\n return { kind: 'string', quote: qcp, excluded, escLineTerm: esc !== '.' }\n}\n\n/**\n * Parse a regex source into a linear chain of literal / char-run segments, or\n * null if it uses any construct outside that category (alternation, groups,\n * `.`, `{n,m}`, lookaround, unknown escapes). This is the STRUCTURAL recognizer\n * for the `seq` shape \u2014 it encodes categories (optional prefix, literal opener,\n * negated run, \u2026), never any specific byte value.\n */\nfunction parseSeqParts(source: string): SeqPart[] | null {\n const parts: SeqPart[] = []\n let lit: number[] = []\n const flush = () => {\n if (lit.length) { parts.push({ part: 'lit', cps: lit, optional: false }); lit = [] }\n }\n // Parse a `{n}` / `{n,}` / `{n,m}` counted quantifier at `source[at]` (`'{'`).\n // Returns the bounds and the token length, or null when it isn't a well-formed\n // brace quantifier (then the `{` is left for the caller's metachar decline).\n const braceQuant = (at: number): { min: number; max: number; len: number } | null => {\n const close = source.indexOf('}', at)\n if (close === -1) return null\n const body = source.slice(at + 1, close)\n const m = /^(\\d+)(,(\\d*)?)?$/.exec(body)\n if (!m) return null\n const min = Number(m[1])\n const max = m[2] === undefined ? min : m[3] ? Number(m[3]!) : Infinity\n // Decline degenerate/empty-capable or invalid bounds \u2014 RegExp handles them.\n if (max < 1 || min > max) return null\n return { min, max, len: close - at + 1 }\n }\n const runFrom = (ranges: Array<[number, number]>, negated: boolean, at: number) => {\n const q = source[at]\n if (q === '+') { parts.push({ part: 'run', ranges, negated, min: 1, max: Infinity }); return 1 }\n if (q === '*') { parts.push({ part: 'run', ranges, negated, min: 0, max: Infinity }); return 1 }\n if (q === '?') { parts.push({ part: 'run', ranges, negated, min: 0, max: 1 }); return 1 }\n if (q === '{') {\n const b = braceQuant(at)\n if (b) { parts.push({ part: 'run', ranges, negated, min: b.min, max: b.max }); return b.len }\n }\n parts.push({ part: 'run', ranges, negated, min: 1, max: 1 })\n return 0\n }\n let i = 0\n while (i < source.length) {\n const ch = source[i]!\n if (ch === '[') {\n // Read a char class, honoring `\\]`.\n let k = i + 1\n const neg = source[k] === '^'\n if (neg) k++\n let body = ''\n while (k < source.length && source[k] !== ']') {\n if (source[k] === '\\\\') { body += source[k]! + (source[k + 1] ?? ''); k += 2 }\n else { body += source[k]!; k++ }\n }\n if (source[k] !== ']') return null\n const ranges = parseClassRanges(body)\n if (!ranges) return null\n flush()\n i = k + 1\n i += runFrom(ranges, neg, i)\n continue\n }\n if (ch === '\\\\') {\n const e = source[i + 1]\n if (e === 'd' || e === 'w' || e === 's') {\n flush()\n i += 2\n i += runFrom(shorthandRanges(e), false, i)\n continue\n }\n // Escaped literal code point (incl. `\\uXXXX`, `\\t`, `\\(`, \u2026).\n const uni = readUnicodeEscape(source, i)\n let cp: number\n if (uni) { cp = uni.cp; i = uni.next }\n else if (e === undefined) return null\n else if (e in CLASS_ESCAPES) { cp = CLASS_ESCAPES[e]!; i += 2 }\n else if ((e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z')) return null\n else { cp = e.codePointAt(0)!; i += 2 }\n if (source[i] === '?') { flush(); parts.push({ part: 'lit', cps: [cp], optional: true }); i++ }\n else lit.push(cp)\n continue\n }\n if (ch === '(') {\n // Only a non-capturing group `(?:\u2026)` is modeled (\u00A78f) \u2014 a bare capturing\n // group, or a lookaround appearing mid-pattern (a TRAILING one is peeled\n // off separately by `stripTrailingLookahead` before we ever get here), is\n // declined outright rather than risk misreading it.\n if (source[i + 1] !== '?' || source[i + 2] !== ':') return null\n let k = i + 3\n let depth = 1\n while (k < source.length && depth > 0) {\n const c2 = source[k]\n if (c2 === '\\\\') { k += 2; continue }\n if (c2 === '[') {\n let j = k + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') return null\n k = j + 1\n continue\n }\n if (c2 === '(') depth++\n else if (c2 === ')') depth--\n k++\n }\n if (depth !== 0) return null\n const body = source.slice(i + 3, k - 1)\n const inner = parseScanShape(body)\n if (!inner || !groupInnerSafe(inner)) return null\n const q = source[k]\n // `{n,m}` on a group is a \u00A78c/\u00A78f combination not modeled \u2014 decline.\n if (q === '{') return null\n flush()\n if (q === '+') {\n if (shapeCanBeEmpty(inner)) return null // would loop forever \u2014 decline\n parts.push({ part: 'group', inner, min: 1, unbounded: true })\n i = k + 1\n } else if (q === '*') {\n if (shapeCanBeEmpty(inner)) return null\n parts.push({ part: 'group', inner, min: 0, unbounded: true })\n i = k + 1\n } else if (q === '?') {\n parts.push({ part: 'group', inner, min: 0, unbounded: false })\n i = k + 1\n } else {\n parts.push({ part: 'group', inner, min: 1, unbounded: false })\n i = k\n }\n continue\n }\n // A bare metachar (`|`, `.`, `{`, stray `*`/`+`/`?`, \u2026) is outside the\n // linear-chain category \u2014 bail to the RegExp fallback.\n if (META.has(ch)) return null\n const cp = ch.codePointAt(0)!\n i += ch.length\n if (source[i] === '?') { flush(); parts.push({ part: 'lit', cps: [cp], optional: true }); i++ }\n else lit.push(cp)\n }\n flush()\n return parts.length ? parts : null\n}\n\n/**\n * Only lower a `seq` when greedy left-to-right scanning matches EXACTLY what the\n * backtracking regex engine would. The dangerous cases are (a) an optional part\n * that could also be consumed by what follows, and (b) an unbounded/repeated\n * part whose accepted class overlaps what follows \u2014 both let the engine\n * backtrack to a different boundary than a one-pass scan. We also require at\n * least one mandatory segment (so the token can't match zero-width).\n *\n * A skippable/repeatable part `p` at index `k` is checked against\n * `seqFirstAccept(parts.slice(k + 1))` \u2014 the accept-set of EVERYTHING that\n * could start at the very next position, unioned through any further chain of\n * optionals up to the next mandatory part (the exact same computation\n * `seqFirstAccept` already does for a seq's OWN leading accept-set). This\n * subsumes the simpler \"check only the immediate neighbor\" rule as the\n * one-part-following case, while also correctly handling a CHAIN of several\n * consecutive optional parts (e.g. two independent optional groups in a row,\n * `(?:\\.\\d+)?(?:[eE][+-]?\\d+)?` \u2014 JSON/GraphQL's number tail, \u00A78f): each is\n * safe precisely when disjoint from everything that could follow it, however\n * many further optionals that spans. An empty tail (`p` is the last part)\n * naturally resolves to a trivially-disjoint empty set \u2014 nothing to check.\n *\n * Uniform across `lit`/`run`/`group`: each part's own \"first char it could\n * start with\" (`partFirstAccept`) is compared via `firstSetsDisjoint` \u2014 the\n * same subset/disjoint math used everywhere else in this file (\u00A78b's\n * lookahead guard, \u00A78e's alt-dispatch guard).\n */\nfunction seqIsUnambiguous(parts: SeqPart[]): boolean {\n let hasMandatory = false\n for (let k = 0; k < parts.length; k++) {\n const p = parts[k]!\n const mandatory = p.part === 'lit' ? !p.optional : p.min >= 1\n if (mandatory) hasMandatory = true\n // A `group` part has a right-edge exposure (`groupPartExposure`): the chars a\n // backtracking engine could give back by shrinking the body's own trailing\n // quantifier, dropping the group (if optional/repeated), or \u2014 for a\n // non-disjoint alt body \u2014 switching arms. Ordered-choice greedy scanning\n // matches the engine only when NONE of those relinquished chars could also\n // start what follows the group (else the scan and the engine disagree on\n // where the group ends). Two outcomes:\n // - a concrete class \u2192 must be disjoint from `seqFirstAccept(rest)`. This\n // is the general fix for a group whose body ends in an unbounded run\n // (`(?:\\d+)\\d` must NOT lower), and it lets a mutually-exclusive fixed\n // alt (\u00A78i, exposure `null`) lower at ANY position, not just trailing.\n // - `'unsupported'` \u2192 the exposure can't be bounded (e.g. a non-disjoint\n // alt like `(?:a|ab)` that genuinely needs arm-switching). Sound only\n // when nothing follows to force reconsideration \u2014 the group must be the\n // trailing, matched-exactly-once part (\u00A78h); otherwise decline.\n if (p.part === 'group') {\n const exposure = groupPartExposure(p)\n if (exposure === 'unsupported') {\n const trailingOnce = k === parts.length - 1 && p.min === 1 && !p.unbounded\n if (!trailingOnce) return false\n } else if (exposure !== null) {\n if (!firstSetsDisjoint(exposure, seqFirstAccept(parts.slice(k + 1)))) return false\n }\n }\n // A part needs the disjoint-from-continuation check when a greedy one-pass\n // scan has freedom a backtracking engine could resolve differently: an\n // optional `lit`, a `run` with wiggle (`max > min` \u2014 covers `?`/`*`/`+` and\n // bounded `{n,m}` with `m > n`; a fixed `{n}` run has none), or an\n // optional/repeated `group`. A fixed-length run (`max === min`) consumes an\n // exact count with no boundary to shift, so it never needs the check.\n const needsDisjoint =\n p.part === 'lit' ? p.optional\n : p.part === 'run' ? p.max > p.min\n : (p.unbounded || p.min === 0)\n if (needsDisjoint) {\n const rest = seqFirstAccept(parts.slice(k + 1))\n if (!firstSetsDisjoint(partFirstAccept(p), rest)) return false\n }\n }\n return hasMandatory\n}\n\nfunction rangesOverlap(a: Array<[number, number]>, b: Array<[number, number]>): boolean {\n return a.some(([lo, hi]) => b.some(([lo2, hi2]) => lo <= hi2 && lo2 <= hi))\n}\n\n/** Is every range in `sub` fully contained in some range of `sup`? */\nfunction rangesSubset(sub: Array<[number, number]>, sup: Array<[number, number]>): boolean {\n return sub.every(([lo, hi]) => sup.some(([lo2, hi2]) => lo >= lo2 && hi <= hi2))\n}\n\n/**\n * A `(?!X)`/`(?=X)` operand: a char class (`[...]`, incl. `[^...]`), a\n * shorthand class (`\\d`/`\\w`/`\\s`), or a single literal/escaped char \u2014 never a\n * sub-pattern (no alternation, no nested groups). Returns the ranges to test\n * membership against, and whether the bracket itself was negated (`[^...]`) \u2014\n * independent of the lookahead's own `!`/`=` polarity.\n */\nfunction parseClassOperand(body: string): { ranges: Array<[number, number]>; negated: boolean } | null {\n if (body === '\\\\d' || body === '\\\\w' || body === '\\\\s') {\n return { ranges: shorthandRanges(body[1] as 'd' | 'w' | 's'), negated: false }\n }\n if (body.length >= 2 && body[0] === '[' && body[body.length - 1] === ']') {\n let inner = body.slice(1, -1)\n const negated = inner.startsWith('^')\n if (negated) inner = inner.slice(1)\n const ranges = parseClassRanges(inner)\n return ranges ? { ranges, negated } : null\n }\n const cps = literalCodePoints(body)\n if (cps && cps.length === 1) return { ranges: [[cps[0]!, cps[0]!]], negated: false }\n return null\n}\n\ntype TrailingLookahead = {\n base: string\n ranges: Array<[number, number]>\n classNegated: boolean\n negative: boolean\n}\n\ntype CharSet = { ranges: Array<[number, number]>; negated: boolean }\n\n/** Is char-set `t` fully contained in char-set `u`? Handles all 4 sign combos. */\nfunction classSubset(t: CharSet, u: CharSet): boolean {\n if (!t.negated && !u.negated) return rangesSubset(t.ranges, u.ranges)\n if (!t.negated && u.negated) return !rangesOverlap(t.ranges, u.ranges) // t \u2286 \u00ACu \u27FA t \u2229 u = \u2205\n if (t.negated && !u.negated) return false // \u00ACt \u2286 u is never true for finite ranges u\n return rangesSubset(u.ranges, t.ranges) // \u00ACt \u2286 \u00ACu \u27FA u \u2286 t\n}\n\n/** Are char-sets `t` and `u` disjoint (no overlap)? Handles all 4 sign combos. */\nfunction classDisjoint(t: CharSet, u: CharSet): boolean {\n if (!t.negated && !u.negated) return !rangesOverlap(t.ranges, u.ranges)\n if (!t.negated && u.negated) return rangesSubset(t.ranges, u.ranges) // t \u2229 \u00ACu = \u2205 \u27FA t \u2286 u\n if (t.negated && !u.negated) return rangesSubset(u.ranges, t.ranges) // symmetric\n return false // \u00ACt \u2229 \u00ACu = \u2205 would require t \u222A u = every code point \u2014 can't prove with finite ranges\n}\n\n/** `classDisjoint`, `'any'`-aware: an empty-capable part's first-set degrades\n * to `'any'` (see `shapeFirstAccept`), and `'any'` is never disjoint from\n * anything (it could start with literally any char, including whatever the\n * other side accepts). */\nfunction firstSetsDisjoint(a: CharSet | 'any', b: CharSet | 'any'): boolean {\n if (a === 'any' || b === 'any') return false\n return classDisjoint(a, b)\n}\n\n/**\n * A shape's match as a fixed-length list of per-position char-sets, or `null`\n * if its match length is NOT fixed. Only a `seq` of mandatory `lit`s (each code\n * point is one fixed position) and mandatory single-char runs (`[x]`, min 1,\n * bounded) qualifies \u2014 anything with an optional part, an unbounded/optional\n * run, or a nested group has a variable length and returns `null`. Used to\n * decide when a non-disjoint alternation's arms are mutually exclusive (\u00A78i).\n */\nfunction fixedClassSeq(shape: ScanShape): CharSet[] | null {\n // A case-fold literal is fixed-length: each code point becomes a single-char\n // class holding BOTH ASCII cases (so mutual-exclusivity is judged on what the\n // `/i` match actually accepts \u2014 `and` vs `or` are disjoint at position 0).\n if (shape.kind === 'litFold') {\n return shape.open.map(cp => {\n const other = cp >= 65 && cp <= 90 ? cp + 32 : cp >= 97 && cp <= 122 ? cp - 32 : cp\n return other === cp\n ? { ranges: [[cp, cp]] as Array<[number, number]>, negated: false }\n : { ranges: [[Math.min(cp, other), Math.min(cp, other)], [Math.max(cp, other), Math.max(cp, other)]] as Array<[number, number]>, negated: false }\n })\n }\n if (shape.kind !== 'seq') return null\n const out: CharSet[] = []\n for (const p of shape.parts) {\n if (p.part === 'lit') {\n if (p.optional) return null\n for (const cp of p.cps) out.push({ ranges: [[cp, cp]], negated: false })\n } else if (p.part === 'run' && p.max === p.min && Number.isFinite(p.max)) {\n // A fixed-count run `[x]{n}` (incl. the bare `[x]`, n=1) is n fixed\n // positions of the same class. Variable-length runs return null below.\n for (let c = 0; c < p.max; c++) out.push({ ranges: p.ranges, negated: p.negated })\n } else {\n return null // optional/variable/unbounded run, or a nested group \u2014 length not fixed\n }\n }\n return out.length ? out : null\n}\n\n/**\n * \u00A78i \u2014 is a NON-disjoint `alt` shape one whose arms are pairwise MUTUALLY\n * EXCLUSIVE fixed-length token sets? Every arm must be a `fixedClassSeq`, and no\n * arm's match may ever be a prefix (proper, or an equal-length overlap) of\n * another arm's \u2014 i.e. for every ordered pair (A, B) with `A.length <= B.length`\n * there is a position within A where their classes are DISJOINT. That property\n * guarantees at most one arm matches at any input position, so ordered-choice\n * commit lands on the SAME arm (and the SAME fixed end) the engine would, with\n * no arm to switch to under backtracking \u2014 sound in ANY position, not just\n * trailing (unlike the general non-disjoint alt, which needs the trailing-once\n * gate in `seqIsUnambiguous`). `(?:ab|ac)` and `(?:foo|barn)` qualify;\n * `(?:a|ab)` (arm `a` is a prefix of `ab`) does NOT.\n */\nfunction altFixedMutuallyExclusive(shape: ScanShape): boolean {\n if (shape.kind !== 'alt' || shape.disjoint) return false\n const lists: CharSet[][] = []\n for (const arm of shape.arms) {\n const f = fixedClassSeq(arm)\n if (!f) return false\n lists.push(f)\n }\n for (let i = 0; i < lists.length; i++) {\n for (let j = 0; j < lists.length; j++) {\n if (i === j) continue\n const a = lists[i]!\n const b = lists[j]!\n if (a.length > b.length) continue // only check the shorter (a) as a prefix of the longer (b)\n let disjointAt = false\n for (let p = 0; p < a.length; p++) {\n if (classDisjoint(a[p]!, b[p]!)) { disjointAt = true; break }\n }\n if (!disjointAt) return false // `a` could be a prefix of `b` \u2014 not mutually exclusive\n }\n }\n return true\n}\n\n/**\n * The char-set a backtracking engine could relinquish at a `group` PART's right\n * edge \u2014 its \"trailing exposure\". Combines two independent backtrack moves: the\n * group body's OWN trailing wiggle (`trailingBacktrackClass(inner)`, e.g. the\n * body ends in an unbounded run), and \u2014 if the group is optional (`min 0`) or\n * repeated (`unbounded`) \u2014 the \"drop the last occurrence\" move, which exposes\n * the body's own first-char set. A required-once group (`min 1`, bounded) has\n * only the first move. Returns `'unsupported'` if either component can't be\n * pinned to a concrete set. This is the single computation used everywhere a\n * group's right boundary matters: `trailingBacktrackClass` (a `seq` ending in a\n * group) and `seqIsUnambiguous` (a group at ANY position, followed by more).\n */\nfunction groupPartExposure(part: Extract<SeqPart, { part: 'group' }>): CharSet | null | 'unsupported' {\n const innerWiggle = trailingBacktrackClass(part.inner)\n if (innerWiggle === 'unsupported') return 'unsupported'\n const requiredOnce = !part.unbounded && part.min === 1\n if (requiredOnce) return innerWiggle // no \"drop it\" move possible \u2014 only inner's own wiggle matters\n const dropExposed = shapeFirstAccept(part.inner)\n if (dropExposed === 'any') return 'unsupported'\n if (innerWiggle === null) return dropExposed\n const unioned = unionCharSets([innerWiggle, dropExposed])\n return unioned === 'any' ? 'unsupported' : unioned\n}\n\n/**\n * The char-class a backtracking engine could expose by shrinking `shape`'s\n * OWN trailing quantifier by one position \u2014 i.e. the \"wiggle room\" a trailing\n * lookahead could interact with. Returns `null` when `shape`'s matched length\n * is fully fixed once it succeeds (no quantifier to shrink), which makes\n * wrapping it in a lookahead unconditionally safe. Returns `'unsupported'` for\n * shape kinds whose backtracking semantics aren't modeled here (declines the\n * lookahead lowering rather than risking an unproven guard).\n *\n * Only the LAST quantifier matters: `seqIsUnambiguous` already proves every\n * EARLIER part of a `seq` has exactly one valid parse (no alternate length),\n * so the sole remaining freedom a backtracking engine has \u2014 once we attach an\n * external trailing lookahead \u2014 is in whatever quantifier sits at the very end.\n */\nfunction trailingBacktrackClass(shape: ScanShape): CharSet | null | 'unsupported' {\n if (shape.kind === 'chars') return { ranges: shape.ranges, negated: false }\n if (shape.kind === 'ident') return { ranges: shape.tail, negated: false }\n if (shape.kind === 'litFold') return null // fixed-length literal \u2014 no quantifier at all\n if (shape.kind === 'seq') {\n const last = shape.parts[shape.parts.length - 1]!\n if (last.part === 'lit') return null // fixed literal tail \u2014 no wiggle room\n if (last.part === 'run') {\n // Wiggle iff `max > min`: unbounded (`+`/`*`/`{n,}`), optional (`[x]?`),\n // or bounded `{n,m}` with `m > n` all let a backtracker shrink the trailing\n // run by \u22651 position. A fixed run (`[x]`, `{n}`, `max === min`) has no\n // choice \u2014 no wiggle, hence no risk.\n if (last.max > last.min) return { ranges: last.ranges, negated: last.negated }\n return null\n }\n // A trailing `group` (\u00A78f): its trailing exposure combines its body's own\n // wiggle and (if optional/repeated) the \"drop it\" move \u2014 see\n // `groupPartExposure`, shared with `seqIsUnambiguous`.\n return groupPartExposure(last)\n }\n if (shape.kind === 'alt') {\n // Arm-switching hazard (see `groupInnerSafe`) applies here too: a\n // non-disjoint alt can't be trusted to expose the right wiggle class,\n // since which arm ultimately \"wins\" could itself change under backtrack.\n // A disjoint alt has no such hazard \u2014 exactly one arm is ever reachable\n // from a given starting char, so the union of every arm's OWN wiggle\n // covers whichever one is actually taken. A non-disjoint alt whose arms are\n // fixed-length and mutually exclusive (\u00A78i) also has no wiggle \u2014 exactly one\n // arm matches at any position and its length is fixed, so `null`.\n if (!shape.disjoint) return altFixedMutuallyExclusive(shape) ? null : 'unsupported'\n const classes = shape.arms.map(trailingBacktrackClass)\n if (classes.some(c => c === 'unsupported')) return 'unsupported'\n const concrete = classes.filter((c): c is CharSet => c !== null)\n if (concrete.length === 0) return null\n const unioned = unionCharSets(concrete)\n return unioned === 'any' ? 'unsupported' : unioned\n }\n // `until`/`delimited`/`string`/nested `lookahead` \u2014 not modeled; decline.\n return 'unsupported'\n}\n\n/**\n * Is it safe to lower `inner(?!ranges)` / `inner(?=ranges)` to a one-shot\n * post-match check (vs. requiring real backtracking)? See PERF_IDEAS \u00A78b.\n */\nfunction lookaheadUnambiguous(inner: ScanShape, operand: CharSet, negative: boolean): boolean {\n const tail = trailingBacktrackClass(inner)\n if (tail === 'unsupported') return false\n if (tail === null) return true // inner's length is fixed \u2014 no backtracking possible\n // Negative `(?!op)`: safe iff every char the tail could expose is ALSO\n // excluded by `op` (so shrinking never turns a failure into a pass).\n // Positive `(?=op)`: safe iff the tail is disjoint from `op` (so shrinking\n // could never expose a char that suddenly satisfies `op`).\n return negative ? classSubset(tail, operand) : classDisjoint(tail, operand)\n}\n\n/**\n * Strip a trailing `(?!class)` / `(?=class)` boundary assertion, if present.\n * Only a TRAILING lookahead with a char-class/single-char operand is\n * recognized \u2014 no sub-patterns, no nested groups. Anything else containing an\n * unescaped `(` is left to the other recognizers, which reject it as a\n * metachar (`META`) and fall back to `RegExp.exec`, so a false read here can\n * only fail to lower, never mis-lower.\n */\nfunction stripTrailingLookahead(source: string): TrailingLookahead | null {\n if (!source.endsWith(')')) return null\n for (const negative of [true, false] as const) {\n const lead = negative ? '(?!' : '(?='\n const idx = source.lastIndexOf(lead)\n if (idx === -1) continue\n const body = source.slice(idx + lead.length, -1)\n const operand = body.length ? parseClassOperand(body) : null\n if (!operand) continue\n return { base: source.slice(0, idx), ranges: operand.ranges, classNegated: operand.negated, negative }\n }\n return null\n}\n\n/**\n * Strip ONE redundant outer `(?:\u2026)` wrapper, if it spans the ENTIRE source\n * (nothing before it, nothing \u2014 not even a quantifier \u2014 after its matching\n * close). A non-capturing group with no trailing quantifier is semantically\n * identical to its bare contents, so this is always safe to unwrap; it's what\n * lets `(?:a|b|c)` (a whole-token alternation written as idiomatic regex\n * style, e.g. CSS `basicSel`) reach the top-level `|` split below. Bails\n * (leaves `source` untouched) on anything that isn't a clean whole-string\n * wrap: unbalanced parens, or a group that closes before the string ends\n * (which would mean a quantifier or trailing content follows it).\n */\nfunction unwrapOuterGroup(source: string): string {\n while (source.startsWith('(?:')) {\n let depth = 1\n let i = 3\n let ok = true\n while (i < source.length && depth > 0) {\n const ch = source[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') { ok = false; break }\n i = j + 1\n continue\n }\n if (ch === '(') depth++\n else if (ch === ')') depth--\n i++\n }\n if (!ok || depth !== 0 || i !== source.length) break\n source = source.slice(3, -1)\n }\n return source\n}\n\n/**\n * Split a regex source on top-level `|` \u2014 i.e. `|` outside any `[]` bracket\n * class and outside any `(...)` group (parens still count toward depth even\n * though we don't otherwise understand groups; a `|` nested in one is never a\n * split point). Returns null if there's no top-level `|` at all, if brackets/\n * parens are unbalanced, or if any resulting alternative is empty (a\n * zero-width arm \u2014 an unmodeled edge case, decline rather than mis-lower).\n * One redundant whole-string `(?:\u2026)` wrapper is stripped first (see\n * `unwrapOuterGroup`) so `(?:a|b)` splits the same as bare `a|b`.\n */\nfunction splitTopLevelAlternation(source: string): string[] | null {\n const body = unwrapOuterGroup(source)\n const parts: string[] = []\n let depth = 0\n let last = 0\n let i = 0\n while (i < body.length) {\n const ch = body[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (body[j] === '^') j++\n while (j < body.length && body[j] !== ']') {\n if (body[j] === '\\\\') j += 2\n else j++\n }\n if (body[j] !== ']') return null\n i = j + 1\n continue\n }\n if (ch === '(') { depth++; i++; continue }\n if (ch === ')') { depth--; if (depth < 0) return null; i++; continue }\n if (ch === '|' && depth === 0) {\n parts.push(body.slice(last, i))\n last = i + 1\n i++\n continue\n }\n i++\n }\n if (depth !== 0) return null\n parts.push(body.slice(last))\n if (parts.length < 2 || parts.some(p => p.length === 0)) return null\n return parts\n}\n\n/** Can `shape` match the empty string? Only an unbounded `chars` run with no\n * `+` (`[x]*`) can \u2014 every other shape requires at least one fixed/mandatory\n * char (`seq` always has \u22651 mandatory part per `seqIsUnambiguous`; `ident`/\n * `until`/`delimited`/`string`/`litFold` all require a fixed opening char). A\n * `lookahead`/`alt` defer to what they wrap. */\nfunction shapeCanBeEmpty(shape: ScanShape): boolean {\n if (shape.kind === 'chars') return !shape.minOne\n if (shape.kind === 'lookahead') return shapeCanBeEmpty(shape.inner)\n if (shape.kind === 'alt') return shape.arms.some(shapeCanBeEmpty)\n return false\n}\n\n/** Exact union of char-sets when it's computable, else `'any'` (conservative:\n * forces ordered dispatch rather than guessing at a mixed-polarity union, or\n * when any input is already `'any'`). A single concrete-set list is returned\n * as-is (including if negated) \u2014 the ambiguity is only in combining \u22652 sets\n * of differing polarity. */\nfunction unionCharSets(sets: Array<CharSet | 'any'>): CharSet | 'any' {\n if (sets.some(s => s === 'any')) return 'any'\n const concrete = sets as CharSet[]\n if (concrete.length === 1) return concrete[0]!\n if (concrete.some(s => s.negated)) return 'any'\n return { ranges: concrete.flatMap(s => s.ranges), negated: false }\n}\n\n/** A single seq part's own first-char set, ignoring whether it's skippable.\n * A `group` part delegates to its inner shape's own first-set (\u00A78f) \u2014\n * possibly `'any'` if the inner shape can match empty or is a non-disjoint\n * `alt` (see `shapeFirstAccept`). */\nfunction partFirstAccept(part: SeqPart): CharSet | 'any' {\n if (part.part === 'lit') return { ranges: [[part.cps[0]!, part.cps[0]!]], negated: false }\n if (part.part === 'run') return { ranges: part.ranges, negated: part.negated }\n return shapeFirstAccept(part.inner)\n}\n\n/** The char-set a `seq` can start with: union of each leading skippable part's\n * own set up to and including the first mandatory part (after which later\n * parts can no longer be what the very first input char matches). */\nfunction seqFirstAccept(parts: SeqPart[]): CharSet | 'any' {\n const sets: Array<CharSet | 'any'> = []\n for (const p of parts) {\n const mandatory = p.part === 'lit' ? !p.optional : p.min >= 1\n sets.push(partFirstAccept(p))\n if (mandatory) break\n }\n return unionCharSets(sets)\n}\n\n/**\n * Is `inner` safe to use as a group's body (\u00A78f)? `chars`/`ident`/`seq`/\n * `litFold` are always safe: each is only ever constructed after proving (via\n * `seqIsUnambiguous` or equivalent) that greedy resolution is the UNIQUE valid\n * match at a given position \u2014 there is no alternate length for a backtracker\n * to reconsider, so treating the group as an atomic, resolve-once unit is\n * sound regardless of what follows it. A `lookahead` defers to what it wraps\n * (zero-width, adds no new choice). An `alt` is safe ONLY when `disjoint`:\n * a NON-disjoint alt (`(?:a|ab)`) genuinely can need real backtracking into a\n * DIFFERENT arm if something after the group fails (verified: `/^(?:a|ab)c/`\n * matches \"abc\" via the SECOND arm, only because the first arm's match left\n * \"c\" unsatisfied) \u2014 our ordered-dispatch codegen resolves once and never\n * reconsiders, so that case must decline. `until`/`delimited`/`string` are\n * declined outright, same conservative call as `trailingBacktrackClass`.\n */\nfunction groupInnerSafe(shape: ScanShape): boolean {\n if (shape.kind === 'chars' || shape.kind === 'ident' || shape.kind === 'seq' || shape.kind === 'litFold') return true\n if (shape.kind === 'lookahead') return groupInnerSafe(shape.inner)\n // A disjoint alt is safe anywhere. A NON-disjoint alt is admitted here too,\n // but its ordered-choice-commit is only sound in the trailing-once position \u2014\n // `seqIsUnambiguous` (\u00A78h) enforces that positional constraint; a non-trailing\n // or optional/repeated non-disjoint-alt group is rejected there.\n if (shape.kind === 'alt') return true\n return false\n}\n\n/**\n * The set of first chars a NON-EMPTY match of `shape` could start with, or\n * `'any'` if that can't be pinned down to a single char-set (used to decide\n * whether `alt` arms have disjoint first-sets). A shape that can match empty\n * degrades to `'any'`: since it could succeed with zero chars regardless of\n * what follows, it would \"accept\" any first char by never actually needing one\n * \u2014 treating that as disjoint from anything else would be unsound.\n */\nfunction shapeFirstAccept(shape: ScanShape): CharSet | 'any' {\n if (shapeCanBeEmpty(shape)) return 'any'\n switch (shape.kind) {\n case 'chars': return { ranges: shape.ranges, negated: false }\n case 'ident': return { ranges: shape.head, negated: false }\n case 'until': return { ranges: [[shape.open[0]!, shape.open[0]!]], negated: false }\n case 'delimited': return { ranges: [[shape.open[0]!, shape.open[0]!]], negated: false }\n case 'string': return { ranges: [[shape.quote, shape.quote]], negated: false }\n case 'litFold': {\n const cp = shape.open[0]!\n if (cp >= 65 && cp <= 90) return { ranges: [[cp, cp], [cp + 32, cp + 32]], negated: false }\n if (cp >= 97 && cp <= 122) return { ranges: [[cp, cp], [cp - 32, cp - 32]], negated: false }\n return { ranges: [[cp, cp]], negated: false }\n }\n case 'seq': return seqFirstAccept(shape.parts)\n case 'lookahead': return shapeFirstAccept(shape.inner) // zero-width \u2014 doesn't change first-char acceptance\n case 'alt': {\n // The set of first chars is the union of every arm's own first-set,\n // independent of whether the arms are mutually disjoint (that flag only\n // governs dispatch, not what the shape can start with). Any arm whose\n // first-set is itself `'any'` (stored as `null`) makes the union `'any'`.\n const sets = shape.firsts.map((f): CharSet | 'any' => (f === null ? 'any' : f))\n return unionCharSets(sets)\n }\n }\n}\n\n/** True iff every pair of `sets` is provably disjoint; `'any'` is disjoint from nothing. */\nfunction allPairsDisjoint(sets: Array<CharSet | 'any'>): boolean {\n for (let i = 0; i < sets.length; i++) {\n const a = sets[i]!\n if (a === 'any') return false\n for (let j = i + 1; j < sets.length; j++) {\n const b = sets[j]!\n if (b === 'any' || !classDisjoint(a, b)) return false\n }\n }\n return true\n}\n\n/**\n * Recognize top-level alternation `A|B|C` (\u00A78e): split on `|` outside any\n * `[]`/`()`, lower each arm independently (an arm may itself be a `seq`,\n * `chars`, `lookahead`, \u2026, or even nested alternation via one redundant\n * `(?:\u2026)` wrapper \u2014 see `unwrapOuterGroup`). Declines (null) unless EVERY arm\n * independently lowers \u2014 a single unsupported arm (e.g. one with a nested\n * capturing/non-capturing group, \u00A78f) falls the whole alternation back to\n * `RegExp.exec` rather than partially lowering.\n */\nfunction parseAlternation(source: string): ScanShape | null {\n const parts = splitTopLevelAlternation(source)\n if (!parts) return null\n const arms: ScanShape[] = []\n for (const p of parts) {\n const arm = parseScanShape(p)\n if (!arm) return null\n arms.push(arm)\n }\n const firstSets = arms.map(shapeFirstAccept)\n const disjoint = allPairsDisjoint(firstSets)\n return { kind: 'alt', arms, disjoint, firsts: firstSets.map(f => (f === 'any' ? null : f)) }\n}\n\n/**\n * Recognize one scannable arm from its regex source, or null if it isn't one of\n * the structural shapes. Top-level alternation (\u00A78e) is tried FIRST \u2014 `|` has\n * the lowest precedence in regex, so splitting on it happens before anything\n * else, with each arm then recursively re-entering this same function (so an\n * arm's own trailing lookahead, or its own nested `(?:\u2026)`-wrapped alternation,\n * is still recognized). Failing that: char-class run, ident, string, then\n * open-until-terminator, delimited, and finally the general linear `seq` chain.\n * A trailing `(?!class)`/`(?=class)` is peeled off next (\u00A78b) and re-wraps\n * whatever shape the remaining base recognizes as.\n */\nexport function parseScanShape(source: string): ScanShape | null {\n const alt = parseAlternation(source)\n if (alt) return alt\n const la = stripTrailingLookahead(source)\n if (la) {\n const inner = parseScanShapeCore(la.base)\n if (!inner) return null\n const operand: CharSet = { ranges: la.ranges, negated: la.classNegated }\n if (!lookaheadUnambiguous(inner, operand, la.negative)) return null\n return { kind: 'lookahead', inner, ranges: la.ranges, classNegated: la.classNegated, negative: la.negative }\n }\n return parseScanShapeCore(source)\n}\n\n/**\n * Split a regex fragment on top-level `|` (outside `[]`/`()`), escape-aware.\n * Unlike `splitTopLevelAlternation` this allows a SINGLE arm (no `|` at all) and\n * does not unwrap an outer group \u2014 it's used to enumerate the arms of a\n * `delimited` body, which may legitimately be one bare arm (`[^x]`). Returns\n * null on unbalanced brackets/parens.\n */\nfunction splitDelimArms(body: string): string[] | null {\n const arms: string[] = []\n let depth = 0\n let last = 0\n let i = 0\n while (i < body.length) {\n const ch = body[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (body[j] === '^') j++\n while (j < body.length && body[j] !== ']') {\n if (body[j] === '\\\\') j += 2\n else j++\n }\n if (body[j] !== ']') return null\n i = j + 1\n continue\n }\n if (ch === '(') { depth++; i++; continue }\n if (ch === ')') { depth--; if (depth < 0) return null; i++; continue }\n if (ch === '|' && depth === 0) { arms.push(body.slice(last, i)); last = i + 1 }\n i++\n }\n if (depth !== 0) return null\n arms.push(body.slice(last))\n return arms\n}\n\n/**\n * Prove that the `delimited` lowering \u2014 \"scan the raw input to the first full\n * `close` literal, ignoring the body entirely\" \u2014 is EQUIVALENT to the real regex\n * `<open>(?:body)*<close>`. That equivalence holds iff `(?:body)*` can never\n * match a string that contains the `close` literal: then greedy repetition stops\n * exactly before the first `close`, which is where the scan lands too.\n *\n * We only PROVE this for the block-comment idiom (the sole shape that expresses\n * \"any char that doesn't form the close\"):\n *\n * n === 1 (`close` is one char l0): body must be exactly `[^l0]`.\n * n === 2 (`close` is `l0 l1`): body must be exactly `[^l0] | l0(?!l1)`.\n *\n * In both the bulk arm `[^l0]` consumes every char except l0, and the guard arm\n * (n === 2) consumes l0 only when it is NOT followed by l1 \u2014 so the body admits\n * l0 but never the full `l0 l1`, and rejects NOTHING else (a body that excluded\n * some non-close char would stop early while the raw scan skipped ahead to the\n * next close, diverging). Anything outside this template \u2014 an alternation whose\n * arms can consume the close (`z(?:a|[0-2]+)*a`), a bulk class narrower/wider\n * than `[^l0]`, a longer close, extra arms \u2014 is declined, falling through to\n * `RegExp.exec`. Sound-by-construction: we only ever return true for a body we\n * can prove never contains `close`.\n */\nfunction delimitedBodySound(body: string, close: number[]): boolean {\n const l0 = close[0]!\n if (close.length > 2) return false\n const arms = splitDelimArms(body)\n if (!arms) return false\n let bulkSeen = false\n let guardSeen = false\n for (const arm of arms) {\n // Bulk arm: a bare negated class `[^l0]` \u2014 excluded set is exactly {l0}.\n const bulk = /^\\[\\^((?:\\\\.|[^\\]])+)\\]$/.exec(arm)\n if (bulk) {\n if (bulkSeen) return false\n const ranges = parseClassRanges(bulk[1]!)\n if (!ranges || ranges.length !== 1 || ranges[0]![0] !== l0 || ranges[0]![1] !== l0) return false\n bulkSeen = true\n continue\n }\n // Guard arm (n === 2 only): `l0(?!l1)` \u2014 consume l0 unless it starts the close.\n if (close.length === 2 && arm.endsWith(')')) {\n const idx = arm.indexOf('(?!')\n if (idx === -1) return false\n const prefix = literalCodePoints(arm.slice(0, idx))\n const op = parseClassOperand(arm.slice(idx + 3, -1))\n const prefixIsL0 = prefix?.length === 1 && prefix[0] === l0\n const forbidsL1 = op && !op.negated && op.ranges.length === 1 &&\n op.ranges[0]![0] === close[1] && op.ranges[0]![1] === close[1]\n if (!prefixIsL0 || !forbidsL1 || guardSeen) return false\n guardSeen = true\n continue\n }\n return false\n }\n // Require the exact arm set: bulk always; guard iff the close is two chars.\n return bulkSeen && guardSeen === (close.length === 2)\n}\n\n/**\n * Structurally recognize `<openLit>(?:<body>)*<closeLit>` \u2014 literal opener, a\n * single non-capturing `*`-repeated group, literal closer \u2014 and lower it to a\n * `delimited` scan ONLY when `delimitedBodySound` proves the body can't contain\n * the close. Returns null (\u2192 fall through to `seq`/`RegExp.exec`) otherwise.\n */\nfunction parseDelimited(source: string): ScanShape | null {\n // An escape-aware body (a literal `\\\\` in the source \u21D2 string-like) is handled\n // by `parseStringShape`; here \"scan to first close\" would wrongly stop at an\n // escaped delimiter, so decline.\n if (source.includes('\\\\\\\\')) return null\n // Opener: literal chars up to the first unescaped `(`, which must open `(?:`.\n let i = 0\n while (i < source.length && source[i] !== '(') {\n i += source[i] === '\\\\' ? 2 : 1\n }\n if (source[i] !== '(' || source[i + 1] !== '?' || source[i + 2] !== ':') return null\n const openFrag = source.slice(0, i)\n // Balanced scan of the group body to its matching `)`.\n let k = i + 3\n let depth = 1\n while (k < source.length && depth > 0) {\n const ch = source[k]\n if (ch === '\\\\') { k += 2; continue }\n if (ch === '[') {\n let j = k + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') return null\n k = j + 1\n continue\n }\n if (ch === '(') depth++\n else if (ch === ')') depth--\n k++\n }\n if (depth !== 0) return null\n const body = source.slice(i + 3, k - 1)\n if (source[k] !== '*') return null\n const closeFrag = source.slice(k + 1)\n const open = literalCodePoints(openFrag)\n const close = literalCodePoints(closeFrag)\n if (!open || !close || !delimitedBodySound(body, close)) return null\n return { kind: 'delimited', open, close }\n}\n\nfunction parseScanShapeCore(source: string): ScanShape | null {\n // [X]+ / [X]* \u2014 a positive char-class run. A negated `[^X]+`/`[^X]*` is left to\n // the general `seq` path (which lowers `[^X]+` and declines zero-width `[^X]*`).\n let m = /^\\[((?:\\\\.|[^\\]])+)\\]([+*])$/.exec(source)\n if (m && !m[1]!.startsWith('^')) {\n const ranges = parseClassRanges(m[1]!)\n return ranges ? { kind: 'chars', ranges, minOne: m[2] === '+' } : null\n }\n // \\d+ / \\w+ / \\s+ / \\d* / \\w* / \\s* \u2014 a bare shorthand-class run.\n m = /^\\\\([dws])([+*])$/.exec(source)\n if (m) {\n return { kind: 'chars', ranges: shorthandRanges(m[1] as 'd' | 'w' | 's'), minOne: m[2] === '+' }\n }\n // <head><tail>* \u2014 identifier run: one head char, then a run of tail chars.\n m = /^(\\[(?:\\\\.|[^\\]])+\\]|\\\\[dws])(\\[(?:\\\\.|[^\\]])+\\]|\\\\[dws])\\*$/.exec(source)\n if (m) {\n const head = classToRanges(m[1]!)\n const tail = classToRanges(m[2]!)\n if (head && tail) return { kind: 'ident', head, tail }\n }\n // <q>(?:[^q\\\\]|\\\\.)*<q> \u2014 a quote-delimited string with escapes.\n const str = parseStringShape(source)\n if (str) return str\n // <lit>[^X]* \u2014 consume a literal opener, then run until a terminator char.\n m = /^(.*?)\\[\\^((?:\\\\.|[^\\]])+)\\]\\*$/.exec(source)\n if (m) {\n const open = literalCodePoints(m[1]!)\n const stop = parseClassRanges(m[2]!)\n if (open && stop) return { kind: 'until', open, stop }\n }\n // <open>(?:\u2026)*<close> \u2014 delimited token scanned to its first close literal.\n // Only lowered when the body provably can't contain the close (see\n // `parseDelimited` / `delimitedBodySound`); any other `X(?:\u2026)*Y` declines here\n // and falls through to the `seq` path / `RegExp.exec`.\n const delim = parseDelimited(source)\n if (delim) return delim\n // General category: a linear chain of literals + char runs (optional prefixes,\n // literal openers, negated runs), lowered only when unambiguously greedy.\n const parts = parseSeqParts(source)\n if (parts && seqIsUnambiguous(parts)) return { kind: 'seq', parts }\n return null\n}\n\n/**\n * Flag-aware wrapper around `parseScanShape`. Lowering to a raw code-point scan\n * assumes default regex semantics, so any flag that changes matching (`i` case\n * folding, `u` surrogate handling, `m`/`s` anchor/dot behavior) disables it.\n * `g`/`y` are stickiness-only and safe.\n */\nexport function scanShapeFromRegex(source: string, flags: string): ScanShape | null {\n if (/[msu]/.test(flags)) return null\n if (/i/.test(flags)) return caseFoldShape(source)\n return parseScanShape(source)\n}\n\n/**\n * Case-insensitive (`/i`) scannable shapes. Beyond a pure case-fold literal\n * (`litFold`), this recognizes a top-level alternation of literals (an ordered\n * `alt` of litFolds \u2014 CSS `(?:and|or)`, `(?:media|container|supports)`), each\n * optionally wrapped in a trailing lookahead boundary (`(?![-\\w])`, `(?=\\()`).\n * That last form is the regex spelling of `makeWord` \u2014 a keyword plus a word\n * boundary \u2014 the common CSS at-rule/keyword dispatcher. Anything else declines\n * (\u2192 `RegExp.exec`); a false read here can only fail to lower, never mis-lower.\n */\nfunction caseFoldShape(source: string): ScanShape | null {\n const la = stripTrailingLookahead(source)\n if (la) {\n const inner = caseFoldLiteralOrAlt(la.base)\n if (!inner) return null\n const operand: CharSet = { ranges: la.ranges, negated: la.classNegated }\n if (!lookaheadUnambiguous(inner, operand, la.negative)) return null\n return { kind: 'lookahead', inner, ranges: la.ranges, classNegated: la.classNegated, negative: la.negative }\n }\n return caseFoldLiteralOrAlt(source)\n}\n\n/** A case-fold literal, or a top-level `a|b|c` alternation of pure literals. */\nfunction caseFoldLiteralOrAlt(source: string): ScanShape | null {\n // `foldEq` only folds ASCII letters (65\u201390 / 97\u2013122). A non-ASCII code point\n // with a Unicode case pair (\u00E9/\u00C9, \u00E4/\u00C4, \u2026) would be emitted as an exact compare\n // and silently miss its alternate case \u2014 a mis-lower, not just a failed lower.\n // Decline those so they fall back to `RegExp.exec` (correct Unicode folding),\n // preserving this file's \"a false read can only fail to lower, never mis-lower\".\n const asciiOnly = (cps: number[]): boolean => cps.every(cp => cp <= 127)\n\n const open = literalCodePoints(source)\n if (open) return asciiOnly(open) ? { kind: 'litFold', open } : null\n // `splitTopLevelAlternation` unwraps the outer group itself, so no pre-strip.\n const arms = splitTopLevelAlternation(source)\n if (!arms || arms.length < 2) return null\n const litArms: ScanShape[] = []\n for (const arm of arms) {\n const cp = literalCodePoints(arm)\n if (!cp || !asciiOnly(cp)) return null\n litArms.push({ kind: 'litFold', open: cp })\n }\n // Ordered choice: each litFold arm case-folds on its own, so skipping the\n // first-char dispatch guard (disjoint:false, firsts:null) is always correct \u2014\n // it just tries arms in order, exactly like regex `|`.\n return { kind: 'alt', arms: litArms, disjoint: false, firsts: litArms.map(() => null) }\n}\n\nexport const classCond = (cVar: string, ranges: Array<[number, number]>): string =>\n ranges\n .map(([lo, hi]) => (lo === hi ? `${cVar} === ${lo}` : `(${cVar} >= ${lo} && ${cVar} <= ${hi})`))\n .join(' || ')\n\n/** Literal-match condition at `base + k` for each code point; uses `firstVar` at offset 0. */\nexport const litCond = (base: string, cps: number[], firstVar?: string): string =>\n cps\n .map((cp, k) =>\n k === 0 && firstVar\n ? `${firstVar} === ${cp}`\n : `input.charCodeAt(${base}${k ? ` + ${k}` : ''}) === ${cp}`,\n )\n .join(' && ')\n\n/** Line-terminator code points `.` does not match (`\\n \\r \\u2028 \\u2029`). */\nexport const LINE_TERMINATORS = [10, 13, 8232, 8233] as const\n\n/** The body-stop chars that abort a string match (excluded set minus quote/backslash). */\nexport function stringHardStop(\n shape: Extract<ScanShape, { kind: 'string' }>,\n): Array<[number, number]> {\n return shape.excluded.filter(\n ([lo, hi]) => !(lo === hi && (lo === shape.quote || lo === BACKSLASH)),\n )\n}\n\n/** Mints a fresh, unique local variable name (`prefix` + counter). */\nexport type Mint = (prefix?: string) => string\n\n/**\n * The SINGLE source of truth for how a scannable shape matches at `start`. Both\n * the terminal emitter and the trivia scan loop consume this, so no context can\n * silently reinterpret an incomplete match (e.g. an unterminated string):\n *\n * - `setup` statements (indented by `ind`) that compute the match.\n * - `ok` a boolean expr: did a token match at `start`? (zero-width for\n * `chars*` counts as a match \u2014 terminals allow it.)\n * - `end` the position AFTER the token. **Invariant:** `end === start`\n * whenever there is no progress (match failed or matched empty),\n * so the trivia loop can gate purely on `end > start`.\n *\n * `firstChar`, when supplied, is an expression already equal to\n * `charCodeAt(start)` (the trivia loop reads it once and shares it).\n */\nexport type ShapeMatch = { setup: string[]; ok: string; end: string }\n\nconst codeAt = (start: string, firstChar?: string): string =>\n firstChar ?? `input.charCodeAt(${start})`\n\n// Dispatch-planning thresholds for a disjoint alt's first-char `switch` \u2014 same\n// tuning as codegen's `planDisjointDispatch` (kept local to avoid a scannable-run\n// \u2192 codegen import cycle): only worth a jump table when every arm keys off a few\n// DISCRETE points; a wide range (`[a-z]+`) would explode into dozens of cases.\nconst ALT_SWITCH_RANGE_LIMIT = 4\nconst ALT_SWITCH_MAX_CASES = 48\nconst ALT_SWITCH_MIN_CASES = 3\n\n/**\n * A `switch` jump table for a *disjoint* alt when every arm's first-set is a few\n * discrete code points (operator/keyword first chars); else `{ kind: 'if' }` to\n * keep the range-comparison `if/else if` chain. Arms are pairwise-disjoint, so\n * each code point maps to exactly one arm.\n */\nfunction planAltSwitch(firsts: Array<CharSet | null>): { kind: 'switch'; cases: number[][] } | { kind: 'if' } {\n const cases: number[][] = []\n let total = 0\n for (const fs of firsts) {\n if (!fs || fs.negated) return { kind: 'if' } // any / negated \u2192 no discrete keys\n const pts: number[] = []\n for (const [lo, hi] of fs.ranges) {\n if (hi - lo + 1 > ALT_SWITCH_RANGE_LIMIT) return { kind: 'if' }\n for (let cp = lo; cp <= hi; cp++) pts.push(cp)\n }\n total += pts.length\n if (total > ALT_SWITCH_MAX_CASES) return { kind: 'if' }\n cases.push(pts)\n }\n return total >= ALT_SWITCH_MIN_CASES ? { kind: 'switch', cases } : { kind: 'if' }\n}\n\nexport function emitShapeMatch(\n shape: ScanShape,\n start: string,\n mint: Mint,\n ind: string,\n firstChar?: string,\n): ShapeMatch {\n if (shape.kind === 'lookahead') {\n const inner = emitShapeMatch(shape.inner, start, mint, ind, firstChar)\n const okV = mint('_ok')\n const endV = mint('_end')\n // `charInClass` folds the operand's OWN `[^\u2026]` negation in; `guardFails`\n // then applies the lookahead's `!`/`=` polarity on top. Zero-width: `end`\n // is always `inner.end` (on success) or `start` (on failure) \u2014 never past\n // where `inner` itself stopped.\n const rawIn = classCond(`input.charCodeAt(${inner.end})`, shape.ranges)\n const charInClass = shape.classNegated ? `!(${rawIn})` : `(${rawIn})`\n const hasMatchingChar = `${inner.end} < input.length && ${charInClass}`\n const guardFails = shape.negative ? hasMatchingChar : `!(${hasMatchingChar})`\n return {\n setup: [\n ...inner.setup,\n `${ind}let ${okV} = ${inner.ok}`,\n `${ind}let ${endV} = ${inner.end}`,\n `${ind}if (${okV} && (${guardFails})) { ${okV} = false; ${endV} = ${start} }`,\n ],\n ok: okV,\n end: endV,\n }\n }\n\n if (shape.kind === 'alt') {\n const okV = mint('_ok')\n const endV = mint('_end')\n const lines: string[] = [`${ind}let ${okV} = false`, `${ind}let ${endV} = ${start}`]\n // Fast path: an alternation of case-fold literals (CSS keyword lists like\n // `@media|@container|@supports`, `even|odd`) \u2192 a `switch` on the first char\n // that jumps straight to the matching group, tails compared with the bit-OR\n // fold. Measured ~2.4x over the ordered if-chain / ~1.4x over per-arm bit-OR.\n // Ordered-choice is preserved: arms are grouped by their FOLDED first code\n // point and, within a group, emitted in source order (first match wins);\n // arms in different groups can't both match a given first char.\n if (shape.arms.length >= 2 && shape.arms.every(a => a.kind === 'litFold' && a.open.length >= 1)) {\n const groups = new Map<number, number[][]>()\n for (const a of shape.arms) {\n const open = (a as Extract<ScanShape, { kind: 'litFold' }>).open\n const f = open[0]!\n const lower = f >= 65 && f <= 90 ? f + 32 : f\n ;(groups.get(lower) ?? groups.set(lower, []).get(lower)!).push(open)\n }\n lines.push(`${ind}if (${start} < input.length) switch (${codeAt(start, firstChar)}) {`)\n for (const [lower, arms] of groups) {\n const labels = lower >= 97 && lower <= 122 ? `case ${lower}: case ${lower - 32}:` : `case ${lower}:`\n lines.push(`${ind} ${labels} {`)\n for (const open of arms) {\n const tail = open.slice(1).map((cp, i) => foldEq(`input.charCodeAt(${start} + ${i + 1})`, cp))\n const cond = tail.length ? ` && ${tail.join(' && ')}` : ''\n lines.push(`${ind} if (${start} + ${open.length} <= input.length${cond}) { ${okV} = true; ${endV} = ${start} + ${open.length}; break }`)\n }\n lines.push(`${ind} break`)\n lines.push(`${ind} }`)\n }\n lines.push(`${ind}}`)\n return { setup: lines, ok: okV, end: endV }\n }\n if (shape.disjoint) {\n // Mutually exclusive first-sets: dispatch straight to the one matching\n // arm, no ordering/backtracking concerns (\u00A77b-style disjoint dispatch). A\n // `switch` jump table when arms key off discrete first chars, else the\n // range-comparison `if/else if` chain (same plan as codegen's choice()).\n const plan = planAltSwitch(shape.firsts)\n if (plan.kind === 'switch') {\n lines.push(`${ind}if (${start} < input.length) switch (${codeAt(start, firstChar)}) {`)\n shape.arms.forEach((arm, k) => {\n const labels = plan.cases[k]!.map(cp => `case ${cp}:`).join(' ')\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(`${ind} ${labels} {`)\n lines.push(...m.setup)\n lines.push(`${ind} ${okV} = ${m.ok}`)\n lines.push(`${ind} ${endV} = ${m.ok} ? ${m.end} : ${start}`)\n lines.push(`${ind} break`)\n lines.push(`${ind} }`)\n })\n lines.push(`${ind}}`)\n } else {\n const c0 = codeAt(start, firstChar)\n shape.arms.forEach((arm, k) => {\n const fs = shape.firsts[k]! // non-null whenever `disjoint` is true\n const cond = fs.negated ? `!(${classCond(c0, fs.ranges)})` : `(${classCond(c0, fs.ranges)})`\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(`${ind}${k === 0 ? 'if' : 'else if'} (${start} < input.length && ${cond}) {`)\n lines.push(...m.setup)\n lines.push(`${ind} ${okV} = ${m.ok}`)\n lines.push(`${ind} ${endV} = ${m.ok} ? ${m.end} : ${start}`)\n lines.push(`${ind}}`)\n })\n }\n } else {\n // Overlapping first-sets: ordered choice \u2014 try each arm in turn and take\n // the first that succeeds. This is exactly regex `|`'s own semantics:\n // first alternative to match AT ALL wins on ITS OWN greedy length, never\n // compared against a later alternative's (possibly longer) match.\n const lbl = mint('_alt')\n lines.push(`${ind}${lbl}: {`)\n for (const arm of shape.arms) {\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(...m.setup)\n lines.push(`${ind} if (${m.ok}) { ${okV} = true; ${endV} = ${m.end}; break ${lbl} }`)\n }\n lines.push(`${ind}}`)\n }\n return { setup: lines, ok: okV, end: endV }\n }\n\n if (shape.kind === 'chars') {\n const cur = mint('_e')\n return {\n setup: [\n `${ind}let ${cur} = ${start}`,\n `${ind}while (${cur} < input.length && (${classCond(`input.charCodeAt(${cur})`, shape.ranges)})) ${cur}++`,\n ],\n // `*` always matches (possibly empty); `+` needs at least one char.\n ok: shape.minOne ? `${cur} > ${start}` : 'true',\n end: cur,\n }\n }\n\n if (shape.kind === 'ident') {\n const cur = mint('_e')\n return {\n setup: [\n `${ind}let ${cur} = ${start}`,\n `${ind}if (${start} < input.length && (${classCond(codeAt(start, firstChar), shape.head)})) {`,\n `${ind} ${cur} = ${start} + 1`,\n `${ind} while (${cur} < input.length && (${classCond(`input.charCodeAt(${cur})`, shape.tail)})) ${cur}++`,\n `${ind}}`,\n ],\n ok: `${cur} > ${start}`,\n end: cur,\n }\n }\n\n if (shape.kind === 'seq') {\n const endV = mint('_e')\n const okV = mint('_ok')\n const runCond = (p: Extract<SeqPart, { part: 'run' }>, at: string) => {\n const c = classCond(`input.charCodeAt(${at})`, p.ranges)\n return p.negated ? `!(${c})` : `(${c})`\n }\n const lines = [`${ind}let ${endV} = ${start}`, `${ind}let ${okV} = false`, `${ind}do {`]\n let first = true\n for (const p of shape.parts) {\n if (p.part === 'lit') {\n const L = p.cps.length\n const cond = litCond(endV, p.cps, first ? firstChar : undefined)\n if (p.optional) {\n lines.push(`${ind} if (${endV} + ${L} <= input.length && (${cond})) ${endV} += ${L}`)\n } else {\n lines.push(`${ind} if (!(${endV} + ${L} <= input.length && (${cond}))) break`)\n lines.push(`${ind} ${endV} += ${L}`)\n }\n } else if (p.part === 'run' && p.max === Infinity) {\n // Unbounded run (`+`/`*`/`{n,}`): greedy scan, then enforce the minimum.\n const s = mint('_s')\n lines.push(`${ind} const ${s} = ${endV}`)\n lines.push(`${ind} while (${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n if (p.min === 1) lines.push(`${ind} if (${endV} === ${s}) break`)\n else if (p.min > 1) lines.push(`${ind} if (${endV} - ${s} < ${p.min}) break`)\n } else if (p.part === 'run' && p.max === 1) {\n // Exactly one char: required (min 1) or optional (min 0).\n if (p.min === 1) {\n lines.push(`${ind} if (!(${endV} < input.length && ${runCond(p, endV)})) break`)\n lines.push(`${ind} ${endV}++`)\n } else {\n lines.push(`${ind} if (${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n }\n } else if (p.part === 'run') {\n // Bounded counted run `[x]{min,max}` (\u00A78c), max \u2265 2: consume up to `max`\n // occurrences, then require at least `min` (greedy \u2014 sound because\n // `seqIsUnambiguous` proved any wiggle is disjoint from what follows).\n const s = mint('_s')\n lines.push(`${ind} const ${s} = ${endV}`)\n lines.push(`${ind} while (${endV} - ${s} < ${p.max} && ${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n if (p.min > 0) lines.push(`${ind} if (${endV} - ${s} < ${p.min}) break`)\n } else {\n // `group` (\u00A78f): the group's own body is just another `ScanShape` \u2014\n // recurse via `emitShapeMatch`, then splice its setup in as the body\n // of a required-once check, an optional take, or a repeat loop.\n const inner = emitShapeMatch(p.inner, endV, mint, `${ind} `, first ? firstChar : undefined)\n if (!p.unbounded) {\n lines.push(...inner.setup)\n if (p.min === 1) {\n lines.push(`${ind} if (!(${inner.ok})) break`)\n lines.push(`${ind} ${endV} = ${inner.end}`)\n } else {\n lines.push(`${ind} if (${inner.ok}) ${endV} = ${inner.end}`)\n }\n } else {\n const progV = mint('_prog')\n lines.push(`${ind} let ${progV} = false`)\n lines.push(`${ind} while (true) {`)\n lines.push(...inner.setup)\n lines.push(`${ind} if (!(${inner.ok})) break`)\n lines.push(`${ind} ${endV} = ${inner.end}`)\n lines.push(`${ind} ${progV} = true`)\n lines.push(`${ind} }`)\n if (p.min === 1) lines.push(`${ind} if (!${progV}) break`)\n }\n }\n first = false\n }\n lines.push(`${ind} ${okV} = true`)\n lines.push(`${ind}} while (false)`)\n lines.push(`${ind}if (!${okV}) ${endV} = ${start}`)\n return { setup: lines, ok: okV, end: endV }\n }\n\n if (shape.kind === 'litFold') {\n const openLen = shape.open.length\n const endV = mint('_end')\n const checks = shape.open.map((cp, k) => {\n const cVar = k === 0 && firstChar ? firstChar : `input.charCodeAt(${start} + ${k})`\n return foldEq(cVar, cp)\n })\n return {\n setup: [\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${checks.join(' && ')})) ${endV} = ${start} + ${openLen}`,\n ],\n ok: `${endV} > ${start}`,\n end: endV,\n }\n }\n\n if (shape.kind === 'until') {\n const j = mint('_j')\n const openLen = shape.open.length\n const openChk = openLen === 1\n ? `${codeAt(start, firstChar)} === ${shape.open[0]}`\n : litCond(start, shape.open, firstChar)\n return {\n setup: [\n `${ind}let ${j} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${openChk})) {`,\n `${ind} ${j} = ${start} + ${openLen}`,\n `${ind} while (${j} < input.length && !(${classCond(`input.charCodeAt(${j})`, shape.stop)})) ${j}++`,\n `${ind}}`,\n ],\n // An open-until-terminator always completes (stop char or EOF), so a matched\n // open literal is a full match; `end === start` iff the open didn't match.\n ok: `${j} > ${start}`,\n end: j,\n }\n }\n\n if (shape.kind === 'string') {\n const okV = mint('_ok')\n const endV = mint('_end')\n const i = mint('_i')\n const c2 = mint('_c')\n const hard = stringHardStop(shape)\n const ltLines = shape.escLineTerm\n ? []\n : (() => {\n const c3 = mint('_c')\n return [\n `${ind} const ${c3} = input.charCodeAt(${i} + 1)`,\n `${ind} if (${LINE_TERMINATORS.map(t => `${c3} === ${t}`).join(' || ')}) break`,\n ]\n })()\n return {\n setup: [\n `${ind}let ${okV} = false`,\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} < input.length && (${codeAt(start, firstChar)}) === ${shape.quote}) {`,\n `${ind} let ${i} = ${start} + 1`,\n `${ind} while (${i} < input.length) {`,\n `${ind} const ${c2} = input.charCodeAt(${i})`,\n `${ind} if (${c2} === ${shape.quote}) { ${okV} = true; ${endV} = ${i} + 1; break }`,\n `${ind} if (${c2} === ${BACKSLASH}) {`,\n `${ind} if (${i} + 1 >= input.length) break`,\n ...ltLines,\n `${ind} ${i} += 2`,\n `${ind} continue`,\n `${ind} }`,\n ...(hard.length ? [`${ind} if (${classCond(c2, hard)}) break`] : []),\n `${ind} ${i}++`,\n `${ind} }`,\n `${ind}}`,\n ],\n ok: okV,\n end: endV,\n }\n }\n\n // delimited: <open>\u2026<close>, requires the close literal (unclosed \u21D2 no match).\n const j = mint('_j')\n const endV = mint('_end')\n const openLen = shape.open.length\n const closeLen = shape.close.length\n const openChk = openLen === 1\n ? `${codeAt(start, firstChar)} === ${shape.open[0]}`\n : litCond(start, shape.open, firstChar)\n const closeChk = litCond(j, shape.close)\n return {\n setup: [\n `${ind}let ${j} = ${start}`,\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${openChk})) {`,\n `${ind} ${j} = ${start} + ${openLen}`,\n `${ind} while (${j} + ${closeLen - 1} < input.length && !(${closeChk})) ${j}++`,\n `${ind} if (${j} + ${closeLen} <= input.length && (${closeChk})) ${endV} = ${j} + ${closeLen}`,\n `${ind}}`,\n ],\n ok: `${endV} > ${start}`,\n end: endV,\n }\n}\n\n/**\n * One trivia-loop branch for a shape, dispatched on the current char `c`\n * (= charCodeAt(_e)). Advances `_e` and `continue`s ONLY on real progress\n * (`end > _e`) \u2014 an unterminated delimited/string token leaves `end === _e`, so\n * the loop stops there exactly as the interpreter's `oneOrMore(choice(\u2026))` would.\n */\nexport function scanBranch(shape: ScanShape, mint: Mint): string {\n const m = emitShapeMatch(shape, '_e', mint, ' ', 'c')\n return [...m.setup, ` if (${m.end} > _e) { _e = ${m.end}; continue }`].join('\\n')\n}\n\n/**\n * A labeled branch: match one token and, on progress, log its [start, end,\n * kindIndex] trivia chunk. Same completion semantics as scanBranch.\n */\nexport function scanBranchLabeled(shape: ScanShape, kindIndex: number, mint: Mint): string {\n const m = emitShapeMatch(shape, '_e', mint, ' ', 'c')\n return [\n ...m.setup,\n ` if (${m.end} > _e) {`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, ${m.end}, ${kindIndex})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << kindIndex}))) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,\n ` }`,\n ` _e = ${m.end}`,\n ` continue`,\n ` }`,\n ].join('\\n')\n}\n", "import type { Combinator } from '../types.ts'\nimport { getCoreRegexDef } from '../combinators/choice.ts'\nimport type { LabeledTriviaSpec } from '../cst/trivia-kinds.ts'\nimport { analyzeLabeledTrivia } from '../cst/trivia-kinds.ts'\nimport { type ScanShape, type Mint, scanShapeFromRegex, scanBranch, scanBranchLabeled } from './scannable-run.ts'\n\n/** Fresh, collision-free local names for one generated scan function. */\nfunction makeMint(): Mint {\n let n = 0\n return (prefix = '_v') => `${prefix}${n++}`\n}\n\nfunction unwrapTrivia(p: Combinator<unknown>): Combinator<unknown> {\n let cur = p\n while (cur._def.tag === 'trivia') cur = cur._def.parser\n return cur\n}\n\nfunction oneOrMoreInner(p: Combinator<unknown>): Combinator<unknown> | null {\n const d = p._def\n if (d.tag === 'oneOrMore') return d.parser\n if (d.tag === 'many' && d.min >= 1) return d.parser\n return null\n}\n\nfunction choiceArms(p: Combinator<unknown>): Combinator<unknown>[] | null {\n if (p._def.tag === 'choice') return p._def.parsers\n return null\n}\n\n/** The scannable shape of one arm (a regex), or null if it isn't scannable. */\nfunction armShape(arm: Combinator<unknown>): ScanShape | null {\n const d = getCoreRegexDef(arm)\n return d ? scanShapeFromRegex(d.source, d.flags) : null\n}\n\n/**\n * A trivia parser that lowers to a hand-rolled char-scan loop: `oneOrMore(choice(\n * \u2026))` (or `many`, min\u22651) where EVERY arm is a scannable shape (a `[X]+` run, a\n * `<lit>[^X]*` open-until-terminator, or a `<open>(?:\u2026)*<close>` delimited token\n * \u2014 see scannable-run.ts). Returns the per-arm shapes, or a single-element list\n * for a bare scannable regex, or null (\u2192 caller falls back to the regex/generic\n * trivia path). A single alternation regex is NOT scannable (one exec matches one\n * arm per call, defeating the loop).\n */\nexport function analyzeTriviaFastPath(trivia: Combinator<unknown>): ScanShape[] | null {\n const core = unwrapTrivia(trivia)\n\n const directDef = getCoreRegexDef(core)\n if (directDef) {\n const shape = scanShapeFromRegex(directDef.source, directDef.flags)\n return shape && shape.kind === 'chars' ? [shape] : null\n }\n\n const inner = oneOrMoreInner(core)\n if (!inner) return null\n\n const innerShape = armShape(inner)\n if (innerShape) return innerShape.kind === 'chars' ? [innerShape] : null\n\n const arms = choiceArms(inner)\n if (!arms || arms.length < 2) return null\n\n const shapes: ScanShape[] = []\n for (const arm of arms) {\n const shape = armShape(arm)\n if (!shape) return null\n shapes.push(shape)\n }\n return shapes\n}\n\nconst CAP_RECORD = [\n ` if (_cap && _e > _pos) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_pos, _e)`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,\n ` }`,\n].join('\\n')\n\n/** One char-scan loop carrying a branch per scannable shape, dispatched on `c`. */\nfunction composeFastLoop(shapes: ScanShape[]): string {\n const mint = makeMint()\n return [\n ` while (_e < input.length) {`,\n ` const c = input.charCodeAt(_e)`,\n ...shapes.map(s => scanBranch(s, mint)),\n ` break`,\n ` }`,\n ].join('\\n')\n}\n\n/** Emit a specialized `_tfN` that skips (unlabeled) trivia via a char-scan loop. */\nexport function buildFastTriviaFnDecl(fnName: string, shapes: ScanShape[]): string {\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n composeFastLoop(shapes),\n CAP_RECORD,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\n/**\n * A LABELED trivia parser whose every arm is scannable \u2192 per-arm shape + kind\n * index, or null. Same recognition as analyzeTriviaFastPath, but keyed by the\n * labeled arm kinds so the fast loop can log each chunk's kind. Generalizes what\n * used to be a hardcoded ws-run + block-comment special case to any scannable\n * shape set / arm count.\n */\nexport function analyzeLabeledScannableRun(\n trivia: Combinator<unknown>,\n): Array<{ shape: ScanShape; kindIndex: number }> | null {\n const spec = analyzeLabeledTrivia(trivia)\n if (!spec) return null\n const out: Array<{ shape: ScanShape; kindIndex: number }> = []\n for (const arm of spec.arms) {\n const d = getCoreRegexDef(arm.parser)\n const shape = d ? scanShapeFromRegex(d.source, d.flags) : null\n if (!shape) return null\n out.push({ shape, kindIndex: arm.kindIndex })\n }\n return out\n}\n\n/** Emit a labeled scannable trivia `_tfN`: char-scan loop with per-chunk kind capture. */\nexport function buildLabeledScannableTriviaFnDecl(\n fnName: string,\n arms: ReadonlyArray<{ shape: ScanShape; kindIndex: number }>,\n): string {\n const mint = makeMint()\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` const c = input.charCodeAt(_e)`,\n ...arms.map(a => scanBranchLabeled(a.shape, a.kindIndex, mint)),\n ` break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\n/** Labeled trivia with regex arms \u2014 per-chunk kind capture in compiled output. */\nexport function buildLabeledRegexTriviaFnDecl(\n fnName: string,\n spec: LabeledTriviaSpec,\n reNames: string[],\n): string {\n const tryArms = spec.arms.map((arm, i) => {\n const re = reNames[i]!\n const k = arm.kindIndex\n return [\n ` ${re}.lastIndex = _e`,\n ` const _m${i} = ${re}.exec(input)`,\n ` if (_m${i} && _m${i}.index === _e) {`,\n ` const _ce = _e + _m${i}[0].length`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,\n ` }`,\n ` _e = _ce`,\n ` _matched = true`,\n ` continue`,\n ` }`,\n ].join('\\n')\n }).join('\\n')\n\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` let _matched = false`,\n tryArms,\n ` if (!_matched) break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\nexport function labeledTriviaRegexArms(trivia: Combinator<unknown>): LabeledTriviaSpec | null {\n const spec = analyzeLabeledTrivia(trivia)\n if (!spec) return null\n for (const arm of spec.arms) {\n if (!getCoreRegexDef(arm.parser)) return null\n }\n return spec\n}\n\n/** Labeled trivia via per-arm runtime parsers (non-regex or mixed arms). */\nexport function buildLabeledRuntimeTriviaFnDecl(\n fnName: string,\n spec: LabeledTriviaSpec,\n rpStartIndex: number,\n): string {\n const tryArms = spec.arms.map((arm, i) => {\n const k = arm.kindIndex\n const ri = rpStartIndex + i\n return [\n ` const _r${i} = _rp[${ri}].parse(input, _e, _ctx)`,\n ` if (_r${i}.ok && _r${i}.span.end > _e) {`,\n ` const _ce = _r${i}.span.end`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,\n ` }`,\n ` _e = _ce`,\n ` _matched = true`,\n ` continue`,\n ` }`,\n ].join('\\n')\n }).join('\\n')\n\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` let _matched = false`,\n tryArms,\n ` if (!_matched) break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n", "/**\n * The linker (RULE_ABI_PLAN \u00A74): fuse independently-compiled linkable artifacts\n * (`compileLinkable`) into ONE closure of parse functions.\n *\n * Every rule is a canonical `_r_<Name>` function and every sibling reference is a\n * call to that name; because fusion drops them all into one scope, those calls\n * are **direct local calls** (0% dispatch). Composition is by name:\n * - **override** \u2014 later artifact wins per rule name; because references resolve\n * by name in the shared scope, overriding a rule reroutes EVERY call to it,\n * including calls inside a base artifact's own rules (open recursion).\n * - **\u00E0 la carte** \u2014 `pick(artifact, names)` keeps only those rules + their\n * transitive dependency closure.\n * Private per-artifact state (`_ns_re`, `_ns_pf`, \u2026) is namespaced so it can't\n * collide; the sentinel protocol / `_EMPTY_TL` are shared and\n * emitted once.\n *\n * Uses `new Function` (like `compile()`), so it needs `'unsafe-eval'` under a\n * strict CSP; a build-time variant that emits fused source instead is a later\n * addition. Fusion runs ONCE at parser construction \u2014 parsing is then full speed.\n */\nimport { compileLinkable, firstSetCond, runFusedGatingDiagnostic, HOST_READS_DECL } from './codegen.ts'\nimport { evalRuleMapIR, serializeRuleMap } from './ir-serialize.ts'\nimport type { LinkablePieces, FirstSetRecipe } from './codegen.ts'\nimport { union } from '../combinators/first-set.ts'\nimport { PARSEMAN_VERSION } from '../version.ts'\nimport type { BuildHost, Combinator, CstCollapsePredicate, FirstSet, ParseContext, ParseResult } from '../types.ts'\n\n/**\n * Compile a `rules()` map to a **linkable artifact** \u2014 the composable, shippable\n * form (RULE_ABI_PLAN \u00A74). A package exports `linkable(rules(g => \u2026))`; consumers\n * import that artifact and `fuse([...])` it \u2014 **no source of the base grammar is\n * ever read**. Under the macro this is precompiled to static pieces; in the\n * interpreter it compiles here at load (like `compile()`).\n *\n * `ns` is a per-artifact namespace; omit it to auto-assign a process-unique one\n * (fine at runtime \u2014 the macro supplies a stable module-derived ns instead).\n */\nlet _nsCounter = 0\nexport function linkable(\n rulesMap: Record<string, Combinator<unknown>>,\n ns?: string,\n trivia?: Combinator<unknown>,\n): LinkablePieces {\n const pieces = compileLinkable(Object.entries(rulesMap), ns ?? `_lk${_nsCounter++}_`, trivia ? { trivia } : undefined)\n if (!pieces) throw new Error('linkable(): this grammar cannot be compiled to a linkable artifact (contains a runtime-only parser fallback)')\n return pieces\n}\n\nexport type CstBuildHostOptions = {\n /**\n * Collapse transparent one-child CST wrapper nodes at build time.\n * - `true`: collapse any one-child node whose rawChildren also has exactly one\n * entry, so trivia/error boundaries are not silently dropped.\n * - `string[]`: collapse only these grammar node types.\n * - predicate: final policy hook for language-specific public CSTs.\n */\n collapse?: boolean | readonly string[] | CstCollapsePredicate\n}\n\nfunction normalizeCstCollapse(collapse: CstBuildHostOptions['collapse']): CstCollapsePredicate | undefined {\n if (collapse === true) return () => true\n if (Array.isArray(collapse)) {\n const types = new Set(collapse)\n return type => types.has(type)\n }\n return typeof collapse === 'function' ? collapse : undefined\n}\n\nfunction buildCstNode(\n type: string,\n children: ReadonlyArray<unknown>,\n _fields: unknown,\n span: { start: number; end: number },\n _rawChildren?: ReadonlyArray<unknown>,\n _triviaLog?: readonly number[],\n state?: unknown,\n): unknown {\n // Carry the grammar's `ctx.state` snapshot onto the node (null when unset) \u2014 the\n // CST contract includes `state` and incremental re-parse replays it on edit.\n return { _tag: 'node', type, span: { start: span.start, end: span.end }, state: state ?? null, children: [...children] }\n}\n\n/**\n * A generic positioned-CST build host (RULE_ABI_PLAN \u00A77). Pass as `ctx.build`\n * (or `parseDoc(..., { build: cstBuildHost })`) to make ANY linkable/fused\n * grammar produce a uniform CST \u2014 `{ _tag:'node', type, span, state, children }`\n * \u2014 instead of its own eval-AST builders. This is the host the linter and IDE\n * drivers use; the eval driver leaves `ctx.build` unset (grammar's own builders).\n *\n * For public syntax trees, call `cstBuildHost({ collapse })`: Parseman will skip\n * allocating wrapper CST nodes whose single child should stand in for the rule.\n */\nexport function cstBuildHost(options?: CstBuildHostOptions): BuildHost\nexport function cstBuildHost(\n type: string,\n children: ReadonlyArray<unknown>,\n fields: unknown,\n span: { start: number; end: number },\n rawChildren?: ReadonlyArray<unknown>,\n triviaLog?: readonly number[],\n state?: unknown,\n): unknown\nexport function cstBuildHost(\n typeOrOptions?: string | CstBuildHostOptions,\n children?: ReadonlyArray<unknown>,\n _fields?: unknown,\n span?: { start: number; end: number },\n rawChildren?: ReadonlyArray<unknown>,\n triviaLog?: readonly number[],\n state?: unknown,\n): unknown {\n if (typeof typeOrOptions === 'string') {\n return buildCstNode(typeOrOptions, children ?? [], _fields, span ?? { start: 0, end: 0 }, rawChildren, triviaLog, state)\n }\n const collapse = normalizeCstCollapse(typeOrOptions?.collapse)\n const host: BuildHost = (\n type: string,\n children: ReadonlyArray<unknown> | undefined,\n fields: unknown,\n span: { start: number; end: number },\n rawChildren: ReadonlyArray<unknown>,\n triviaLog: readonly number[],\n state: unknown,\n // A CST/collapse host always keeps `children` (chV) \u2014 the opt-out never\n // applies \u2014 so `?? []` is unreachable defensive modeling for the widened type.\n ) => buildCstNode(type, children ?? [], fields, span, rawChildren, triviaLog, state)\n ;(host as typeof host & { _parsemanCstOutput?: true })._parsemanCstOutput = true\n if (collapse) host._parsemanCstCollapse = collapse\n return host\n}\n\n// `cstBuildHost` itself is also accepted as a BuildHost (without options).\n;(cstBuildHost as unknown as { _parsemanCstOutput?: true })._parsemanCstOutput = true\n\n/**\n * A fused function receives the full ParseContext through `run()`. Direct\n * callers historically supplied a plain context object, so keep that usage\n * valid while making the function assignable to the public `Runnable` type.\n * Generated code treats optional framework fields as absent when they are not\n * provided, matching the interpreter's normal defaults.\n */\nexport type FusedRule = (\n input: string,\n pos: number,\n ctx: ParseContext | Record<string, unknown>,\n) => ParseResult<unknown> & { readonly value?: unknown }\n\n/**\n * Restrict a grammar/artifact to `names` plus their transitive rule-dependency\n * closure (\u00E0 la carte selection) \u2014 e.g. Jess taking parts of Less and parts of\n * Sass: `compose([pick(less, ['MixinCall']), pick(sass, ['EachFor']), css])`. A\n * picked rule pulls in every rule name it references, so the result is always\n * self-consistent within the artifact. Accepts a grammar (`rules()` result) or a\n * compiled artifact; returns an artifact for `compose()`.\n */\nexport function pick(\n grammar: LinkablePieces | Record<string, Combinator<unknown>>,\n names: string[],\n): LinkablePieces {\n // A COMPOSED grammar (`compose([...])` result): it has no single rule map \u2014 its rules\n // live across several carried pieces. Materialize them under the composing trivia it\n // was built with (so the selection keeps composing-wins trivia), then filter across\n // pieces to `names` + their transitive dep closure, keeping each rule in the piece\n // that WINS it (compose override order = last wins). Return a composed-like value so a\n // downstream `compose([pick(composed, \u2026)])` flattens it the same way.\n const carried = (grammar as Record<symbol, unknown>)[COMPOSED_PIECES]\n if (Array.isArray(carried)) {\n const trivia = (grammar as Record<symbol, unknown>)[COMPOSED_TRIVIA] as Combinator<unknown> | undefined\n const pieces = (carried as Array<LinkablePieces | IRPiece>).map(pc => materializePiece(pc, trivia))\n const filtered = pickPieces(pieces, names)\n // A composed-like value (carries COMPOSED_PIECES, no single rule map). It is only\n // ever consumed through compose()'s COMPOSED_PIECES branch \u2014 which is checked before\n // any LinkablePieces field \u2014 so this masquerades as LinkablePieces for the signature.\n const out: Record<string, unknown> = {}\n Object.defineProperty(out, COMPOSED_PIECES, { value: filtered, enumerable: false })\n if (trivia) Object.defineProperty(out, COMPOSED_TRIVIA, { value: trivia, enumerable: false })\n return out as unknown as LinkablePieces\n }\n\n const p = (grammar as LinkablePieces).ruleFns instanceof Map\n ? (grammar as LinkablePieces)\n : linkable(grammar as Record<string, Combinator<unknown>>)\n return pickPieces([p], names)[0] ?? { ...p, keys: [], ruleFns: new Map(), wrappers: new Map(), deps: new Map() }\n}\n\n/** Restrict a set of linkable pieces to `names` + their transitive dep closure,\n * keeping each surviving rule in the piece that WINS it (later piece wins, matching\n * compose override order). Shared by `pick()` (runtime) and the macro's build-time\n * `pick(...)` handling, so \u00E0-la-carte selection is identical on both paths. */\nexport function pickPieces(pieces: LinkablePieces[], names: string[]): LinkablePieces[] {\n const winner = new Map<string, LinkablePieces>()\n for (const pc of pieces) for (const k of pc.keys) winner.set(k, pc)\n // A requested name that isn't in the grammar is a typo \u2014 fail here, not later with a\n // confusing name-closure error at compose() time.\n for (const n of names) {\n if (!winner.has(n)) throw new Error(`pick: rule \"${n}\" is not in this grammar (available: ${[...winner.keys()].join(', ')})`)\n }\n const keep = new Set<string>()\n const visit = (n: string): void => {\n // A missing winner is an EXTERNAL dep (a base-grammar rule) \u2014 it resolves at\n // compose() time, not here. Top-level `names` were already validated above.\n const w = winner.get(n)\n if (keep.has(n) || !w) return\n keep.add(n)\n for (const d of w.deps.get(n) ?? []) visit(d)\n }\n for (const n of names) visit(n)\n const filt = <V>(m: Map<string, V>, pc: LinkablePieces): Map<string, V> =>\n new Map([...m].filter(([k]) => keep.has(k) && winner.get(k) === pc))\n return pieces\n .map(pc => ({ ...pc, keys: pc.keys.filter(k => keep.has(k) && winner.get(k) === pc), ruleFns: filt(pc.ruleFns, pc), wrappers: filt(pc.wrappers, pc), deps: filt(pc.deps, pc) }))\n .filter(pc => pc.keys.length > 0)\n}\n\n/**\n * Build the fused-closure body (shared by the runtime `fuseRules` and the\n * build-time `emitFusedSource`). Returns the closure body (which reads `_env`\n * for any non-inlined callbacks) and the `_env` to bind. In macro mode callbacks\n * are inlined from source, so `_env` is empty and the body is fully static.\n */\nexport function fusedBody(pieces: LinkablePieces[]): { body: string; env: Record<string, unknown> } {\n // ARTIFACT VERSION LOCK (see src/version.ts): a compiled artifact is fused by the\n // SAME parseman version that produced it \u2014 the artifact format carries no\n // cross-version back-compat. Reject BOTH a version MISMATCH and an UNSTAMPED piece\n // (a pre-0.32 artifact predates the invariant \u2192 its recipe/pieces shape is\n // unsupported): fail LOUDLY rather than silently mis-reading a stale shape. Every\n // artifact `compileLinkable` produces is stamped, so an absent stamp means a stale\n // serialized artifact, not a current one.\n for (const p of pieces) {\n if (p.v === undefined) {\n throw new Error(\n `parseman: artifact \"${p.ns}\" is UNSTAMPED (compiled before the version-lock invariant). ` +\n `Recompile the grammar with parseman ${PARSEMAN_VERSION}; parseman does not fuse unversioned or cross-version artifacts.`,\n )\n }\n if (p.v !== PARSEMAN_VERSION) {\n throw new Error(\n `parseman: artifact \"${p.ns}\" was compiled with parseman ${p.v}, but is being fused with parseman ${PARSEMAN_VERSION}. ` +\n `Compiled grammar artifacts are version-locked \u2014 recompile the grammar with parseman ${PARSEMAN_VERSION}; parseman does not fuse across versions.`,\n )\n }\n }\n // Winner per rule name \u2014 later pieces override earlier ones.\n const winner = new Map<string, LinkablePieces>()\n for (const p of pieces) for (const k of p.keys) winner.set(k, p)\n\n // Name-closure check: every referenced rule must resolve in the fused set.\n for (const [k, p] of winner) {\n for (const d of p.deps.get(k) ?? []) {\n if (!winner.has(d)) throw new Error(`compose: rule \"${k}\" references missing rule \"${d}\"`)\n }\n }\n\n const contributing = new Set(winner.values())\n const contributingPieces = [...contributing]\n const needsEmptyTl = contributingPieces.some(p => p.needsEmptyTl)\n const needsHostReads = contributingPieces.some(p => p.needsHostReads)\n\n const lines: string[] = [\n // Shared sentinel protocol (must match NAMED_FN_FAIL / NAMED_FN_END in codegen).\n 'const _pfFail = {}',\n 'let _pfEnd',\n ...(needsEmptyTl ? ['const _EMPTY_TL = Object.freeze([])'] : []),\n ...(needsHostReads ? [HOST_READS_DECL] : []),\n // Each contributing artifact's namespaced private prelude (regexes, _pf, \u2026).\n ...new Set(contributingPieces.flatMap(p => p.prelude)),\n // The winning `_r_<Name>` function for each rule (one per name \u2192 no redeclare).\n ...[...winner].map(([k, p]) => p.ruleFns.get(k)!),\n ]\n const wrapperEntries = [...winner].map(([k, p]) => `${JSON.stringify(k)}: ${p.wrappers.get(k)!}`)\n const rawBody = [...lines, 'return {', wrapperEntries.join(',\\n'), '}'].join('\\n')\n\n // Fuse-time first-set dispatch: a rule-ref choice arm was emitted (in linkable\n // mode) as `/*@FS:rule:codevar@*\u200B/true`. Resolve it now against the WINNING rule's\n // LEADING first-set recipe \u2014 sound under override, since we use the final rule, not\n // the one visible when the referencing rule was compiled. Each rule's recipe is a\n // union of ordered leading-term chains; resolving them over the WINNING rules to a\n // fixpoint makes a `sequence(ref, \u2026)`-led arm/node dispatch on the ref's real first\n // char (parity with a monolithic compile) instead of degrading to always-try\n // because the ref baked `any`. A genuinely unknown ref \u2192 `any` (always try).\n const ANY: FirstSet = { kind: 'any' }\n const EMPTY: FirstSet = { kind: 'empty' }\n const recipes = new Map<string, FirstSetRecipe>()\n const shallow = new Map<string, FirstSet>()\n const nullable = new Map<string, boolean>()\n for (const [k, p] of winner) {\n const r = p.firstSetRecipes?.get(k); if (r) recipes.set(k, r)\n const fs = p.firstSets?.get(k); if (fs) shallow.set(k, fs)\n const nu = p.nullable?.get(k); if (nu !== undefined) nullable.set(k, nu)\n }\n const knownRef = (ref: string): boolean => recipes.has(ref) || shallow.has(ref)\n // A referenced rule's resolved first-set (or `any` for a genuinely unknown ref \u2014\n // always try). A ref whose nullability is unknown defaults to nullable (keep\n // unioning the chain tail \u2014 never drops a valid first char).\n const refFS = (ref: string): FirstSet => (knownRef(ref) ? (finalFS.get(ref) ?? ANY) : ANY)\n const refNullable = (ref: string): boolean => (knownRef(ref) ? (nullable.get(ref) ?? true) : true)\n // Resolve a recipe: union each ordered chain, and within a chain union each\n // segment's first-set left-to-right STOPPING after the first non-nullable segment\n // (its tail can't start the rule). A ref segment is skippable if it was forced so\n // at build (a wrapping optional/many \u2192 `seg.nullable`) OR the resolved rule is\n // itself nullable; only a definitely-consuming ref stops the chain.\n const resolveRecipe = (r: FirstSetRecipe): FirstSet => {\n let out: FirstSet = EMPTY\n for (const chain of r.alts) {\n for (const seg of chain) {\n out = union(out, seg.ref !== undefined ? refFS(seg.ref) : seg.set)\n const skippable = seg.ref !== undefined ? (seg.nullable || refNullable(seg.ref)) : seg.nullable\n if (!skippable) break\n }\n }\n return out\n }\n // Least-fixpoint: recipe-bearing rules start EMPTY and grow monotonically via\n // `resolveRecipe` (union of refs is monotone; the chain stop is static), so a\n // recursive rule converges to its tightest sound first-set. Rules with no recipe\n // keep their static shallow set.\n const finalFS = new Map<string, FirstSet>()\n for (const [k] of winner) finalFS.set(k, recipes.has(k) ? EMPTY : (shallow.get(k) ?? ANY))\n for (let iter = 0; iter <= recipes.size; iter++) {\n let changed = false\n for (const [k, r] of recipes) {\n const fs = resolveRecipe(r)\n if (JSON.stringify(finalFS.get(k)) !== JSON.stringify(fs)) { finalFS.set(k, fs); changed = true }\n }\n if (!changed) break\n }\n const body = rawBody.replace(\n // Rule name + code-point var are both JS identifiers (rule names are validated\n // at compile time \u2014 see assertRuleName in codegen), so an identifier class\n // matches every well-formed placeholder.\n /\\/\\*@FS:([A-Za-z0-9_$]+):([A-Za-z0-9_$]+)@\\*\\/true/g,\n (_m, name: string, codevar: string) => {\n const fs = finalFS.get(name)\n if (!fs || fs.kind === 'any' || fs.kind === 'empty') return 'true'\n return `(${firstSetCond(codevar, fs)})`\n },\n )\n\n // Non-inlined callbacks (runtime compile() mode), keyed `<ns>mf` / `<ns>build`.\n const env: Record<string, unknown> = {}\n for (const p of contributingPieces) {\n if (p.mfFns.length) env[`${p.ns}mf`] = p.mfFns\n if (p.buildFns.length) env[`${p.ns}build`] = p.buildFns\n }\n return { body, env }\n}\n\n/** Fuse at RUNTIME (via `new Function`) \u2014 used by `compose()` when not compiled\n * by the macro (like `compile()`). The macro path uses `emitFusedSource` instead. */\nexport function fuseRules(pieces: LinkablePieces[]): Record<string, FusedRule> {\n const { body, env } = fusedBody(pieces)\n // eslint-disable-next-line no-new-func\n return new Function('_env', body)(env) as Record<string, FusedRule>\n}\n\n/**\n * Fuse at BUILD time \u2014 emit the fused closure as a self-contained SOURCE\n * expression (`(() => { \u2026 })()`), with **no `new Function`**. This is what the\n * macro splices in for a `compose([...])` call, so macro output stays eval-free.\n * Requires every callback to be inlined from source (macro mode); throws if any\n * artifact carries runtime-only callback functions.\n */\nexport function emitFusedSource(pieces: LinkablePieces[]): string {\n const { body, env } = fusedBody(pieces)\n if (Object.keys(env).length > 0) {\n throw new Error('emitFusedSource: artifact carries non-static callbacks (runtime-only); cannot emit static source')\n }\n return `/* @__PURE__ */ (() => {\\n${body}\\n})()`\n}\n\n/**\n * Compose grammars/artifacts into a runnable parser map \u2014 the ONLY public\n * composition entry point. `compose([base, ext, \u2026])`: later entries override\n * earlier ones by rule name, and because fusion re-binds every reference in one\n * shared scope, an override reroutes the base's OWN calls too (open recursion).\n *\n * Each entry may be a **grammar** (a `rules()` result \u2014 a map of combinators,\n * linkable-ified here) OR an already-compiled **linkable artifact** (what the\n * macro emits and a package ships). So a package needs no opt-in wrapper to be\n * composable \u2014 `compose([importedGrammar, myRules])` just works.\n *\n * The macro compiles `compose([...])` to STATIC fused source (no `new Function`).\n * Called at runtime (no macro, like `compile()`) it fuses via `new Function`.\n */\n/** A composed parser carries its flattened source pieces (non-enumerable) so it\n * can be composed AGAIN \u2014 `compose([lessGrammar, delta])` where `lessGrammar` is\n * itself a `compose([...])` result. */\nconst COMPOSED_PIECES = Symbol.for('parseman.composedPieces')\n\n/**\n * A terminal fused grammar may be used to run a parser, but not as an input to\n * another composition. Macro `composeLeaf()` uses this for a local semantic\n * reduction over imported recognition-only IR: the local reductions stay in\n * their lexical module and therefore never become carried IR.\n */\nconst LEAF_COMPOSED = Symbol.for('parseman.leafComposed')\n\n/** The composing (outermost) trivia a runtime `compose()` applied \u2014 stored so a\n * later `pick(composedGrammar, \u2026)` can re-lower the selected rules under the SAME\n * trivia (composing-wins survives \u00E0-la-carte selection). The carried IR pieces hold\n * no trivia of their own, so it must be remembered separately. Not serialized by the\n * macro (which delegates pick to the runtime linker). */\nconst COMPOSED_TRIVIA = Symbol.for('parseman.composedTrivia')\n\n/** Final winner map for semantic-coverage tooling. It exists only when every\n * carried compose piece is re-lowerable IR; opaque precompiled artifacts have no\n * combinator graph to inspect and therefore deliberately expose no fake map. */\nconst COMPOSED_COVERAGE_RULES = Symbol.for('parseman.composedCoverageRules')\n\n/** The compact IR form a grammar carries instead of its lowered rule source: the\n * combinator-construction expression, re-lowered here at fuse time. */\nexport type IRPiece = { ns: string; ir: string }\n\nfunction isIRPiece(p: unknown): p is IRPiece {\n return !!p && typeof p === 'object'\n && typeof (p as IRPiece).ir === 'string' && typeof (p as IRPiece).ns === 'string'\n && !('ruleFns' in (p as object))\n}\n\n/** The re-lowerable carried pieces' rule maps, in compose order \u2014 the input to the\n * fuse-time gating diagnostic. An opaque precompiled artifact contributes no\n * combinator graph, so it is simply skipped: a hole it would have bound stays\n * unresolved and its choice stays deferred (silent), never falsely warned. */\nexport function carriedRuleMaps(carried: ReadonlyArray<LinkablePieces | IRPiece>): Array<Array<[string, Combinator<unknown>]>> {\n return carried.filter(isIRPiece).map(p => evalRuleMapIR(p.ir))\n}\n\nfunction coverageRulesOf(carried: Array<LinkablePieces | IRPiece>): Record<string, Combinator<unknown>> | undefined {\n const winners: Record<string, Combinator<unknown>> = {}\n for (const piece of carried) {\n if (!isIRPiece(piece)) return undefined\n const map = evalRuleMapIR(piece.ir)\n for (const [name, rule] of map) {\n // An accessed-but-undefined `g.Name` is an external reference, never a\n // rule definition. Match compose's IR filtering rule exactly.\n if (rule._def.tag === 'lazy') {\n try { rule._def.thunk() } catch { continue }\n }\n winners[name] = rule\n }\n }\n return winners\n}\n\n/** Return the final override-winner combinator map carried by runtime\n * `compose()`, or `undefined` when a precompiled opaque artifact participated.\n * This is intentionally internal: callers must not treat it as a parser API. */\nexport function composedCoverageRules(grammar: Record<string, unknown>): Record<string, Combinator<unknown>> | undefined {\n return (grammar as Record<symbol, unknown>)[COMPOSED_COVERAGE_RULES] as Record<string, Combinator<unknown>> | undefined\n}\n\n/** Materialize a carried item to full `LinkablePieces`: an IR piece is evaluated\n * back to a rule map and re-lowered; a full piece passes through. */\nexport function materializePiece(\n p: LinkablePieces | IRPiece,\n trivia?: Combinator<unknown>,\n captureTerminals = false,\n): LinkablePieces {\n if (!isIRPiece(p)) return p\n const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns, {\n ...(trivia ? { trivia } : {}),\n ...(captureTerminals ? { captureTerminals: true } : {}),\n })\n if (!pieces) throw new Error(`compose: carried IR for ns \"${p.ns}\" could not be re-lowered`)\n return pieces\n}\n\n/** Flatten one `compose()` item to its pieces: a prior composed result \u2192 its\n * carried list; an artifact \u2192 itself; a grammar (`rules()` map) \u2192 linkable-ified. */\nfunction nextComposeNs(used: Set<string>): string {\n let ns: string\n do { ns = `_lk${_nsCounter++}_` } while (used.has(ns))\n used.add(ns)\n return ns\n}\n\n/** Flatten one `compose()` item to its RE-LOWERABLE carried items \u2014 the form stored\n * on the composed result so it can be composed AGAIN under a NEW composing trivia.\n * A grammar (`rules()` map) is carried as compact IR (`{ns, ir}`), NOT baked source,\n * so a later `compose([thisResult, delta])` re-lowers it with the delta's trivia\n * (multi-level composing-wins). A prior composed result contributes its OWN carried\n * items (already IR); a pre-compiled artifact has no source, so it stays baked. */\nfunction itemCarried(\n item: LinkablePieces | Record<string, unknown>,\n used: Set<string>,\n trivia?: Combinator<unknown>,\n): Array<LinkablePieces | IRPiece> {\n const carried = (item as Record<symbol, unknown>)[COMPOSED_PIECES]\n // A prior composed result (runtime or macro-compiled): its carried list is already\n // re-lowerable (IR pieces, plus any pre-compiled artifacts). Pass it through so THIS\n // compose re-lowers it under its own composing trivia. Reserve its namespaces so a\n // sibling grammar map can't collide with them.\n if (Array.isArray(carried)) {\n const items = carried as Array<LinkablePieces | IRPiece>\n for (const p of items) used.add(p.ns)\n return items\n }\n // A pre-compiled artifact (`linkable()`/`pick()`): no source to re-lower \u2014 its trivia\n // was baked when it was compiled. Keep it as-is.\n if ((item as LinkablePieces).ruleFns instanceof Map) {\n const p = item as LinkablePieces\n used.add(p.ns)\n return [p]\n }\n // A grammar (`rules()` map): carry it as compact IR so a later compose re-lowers it\n // under ITS trivia. Unserializable \u2192 bake now with this compose's trivia (can't\n // re-lower later; acceptable fallback, mirrors the macro's full-pieces fallback).\n const map = item as Record<string, Combinator<unknown>>\n const ns = nextComposeNs(used)\n // Drop EXTERNAL entries first (same filter as compileLinkable): a `rules()` cache\n // also holds every ACCESSED-but-undefined `g.X` as an unresolved-lazy entry. Left in,\n // serializeRuleMap would emit `X: g[\"X\"]` \u2014 a self-referential rule that shadows the\n // sibling artifact defining X and recurses forever. They resolve by name at fuse time.\n const entries = Object.entries(map).filter(([, val]) => {\n const d = val._def\n if (d.tag !== 'lazy') return true\n try { d.thunk(); return true } catch { return false }\n })\n // Carry this grammar's ambient `scanSkip` (per-piece \u2014 opaque units are\n // dialect-specific, NOT composing-wins) into the IR so a re-lower stamps\n // `grammarScanSkip` back on. `linkable()`'s fallback reads it off `_meta` directly.\n const scanSkip = entries\n .map(([, val]) => (val._meta as { grammarScanSkip?: Combinator<unknown>[] }).grammarScanSkip)\n .find(Boolean)\n const ir = serializeRuleMap(entries, scanSkip)\n return ir ? [{ ns, ir }] : [linkable(map, ns, trivia)]\n}\n\n/** The composed grammar's ambient trivia = the LAST composed item that declares a\n * grammar-level trivia (via `rules({ trivia }, \u2026)`, which tags `grammarTrivia` on its\n * rules). Outermost wins: the composing grammar's trivia applies to every fused rule,\n * including those inherited from a base \u2014 so e.g. an SCSS `rw` (which extends Less's)\n * governs the inherited Less/CSS rules too. `parser`/`noTrivia` still override locally. */\nfunction composingTriviaOf(items: Array<LinkablePieces | Record<string, unknown>>): Combinator<unknown> | undefined {\n for (let i = items.length - 1; i >= 0; i--) {\n const item = items[i] as Record<string, unknown> | undefined\n if (!item || (item as LinkablePieces).ruleFns instanceof Map || (item as Record<symbol, unknown>)[COMPOSED_PIECES]) continue\n for (const v of Object.values(item)) {\n const t = (v as Combinator<unknown> | undefined)?._meta?.grammarTrivia\n if (t) return t\n }\n }\n return undefined\n}\n\nexport function compose(\n items: Array<LinkablePieces | Record<string, unknown>>,\n): Record<string, FusedRule> {\n if (items.some(item => (item as Record<symbol, unknown>)[LEAF_COMPOSED] === true)) {\n throw new Error('compose: a composeLeaf() result is terminal and cannot be composed again')\n }\n const used = new Set<string>()\n // The composed grammar's ambient trivia comes from the composing grammar itself \u2014\n // whatever the last piece declared via rules({ trivia }, \u2026). No separate option:\n // the trivia rides with the grammar that declared it.\n const trivia = composingTriviaOf(items)\n // Carried items are RE-LOWERABLE (IR); materialize them ONCE with this compose's\n // trivia for the now-fuse, but STORE the un-materialized carried list so a later\n // compose can re-lower it under a different trivia (multi-level composing-wins).\n const carried = items.flatMap(item => itemCarried(item, used, trivia))\n // Fuse time is where a shared shape's `g.Foo` hole is finally bound, so it is the\n // only site that can answer whether the choices it leads actually gate.\n runFusedGatingDiagnostic(() => carriedRuleMaps(carried))\n const pieces = carried.map(p => materializePiece(p, trivia))\n const map = fuseRules(pieces)\n Object.defineProperty(map, COMPOSED_PIECES, { value: carried, enumerable: false })\n if (trivia) Object.defineProperty(map, COMPOSED_TRIVIA, { value: trivia, enumerable: false })\n const coverageRules = coverageRulesOf(carried)\n if (coverageRules) Object.defineProperty(map, COMPOSED_COVERAGE_RULES, { value: coverageRules, enumerable: false })\n return map\n}\n\n/**\n * Compose a terminal grammar. This is for a leaf parser that overlays local\n * semantic reductions on reusable recognition rules. It is macro-only: without\n * macro lowering there is no safe way to keep lexical builders out of carried\n * IR, so it fails rather than falling back to runtime composition.\n */\nexport function composeLeaf(\n _items: Array<LinkablePieces | Record<string, unknown>>,\n): Record<string, FusedRule> {\n throw new Error('composeLeaf(): requires Parseman macro lowering; runtime composition is forbidden')\n}\n", "/**\n * Tree traversal for the CST/AST a grammar produces. Pars\u00E9man hands back a plain\n * object tree, so you *can* recurse it yourself \u2014 these helpers save you from\n * re-writing the same recursion for diagnostics, folding, or lowering a tree.\n *\n * Two entry points:\n * - `walk` \u2014 imperative depth-first traversal with enter/leave hooks.\n * - `createVisitor` \u2014 Chevrotain-style dispatch keyed on each node's `type`.\n *\n * Both default to the built-in CST shape (`CSTChild` \u2014 the `node()` / leaf / error\n * union), so with no annotation you get precise typing out of the box. Parsing to\n * a custom AST? Pass your node type as a generic: `walk<MyNode>(root, \u2026)`.\n */\nimport type { CSTChild } from './types.ts'\n\n/**\n * The minimal shape these helpers traverse: a `_tag`, an optional rule `type`,\n * and optional structural `children`. The built-in `CSTChild` satisfies it, and\n * so does any custom AST node (that's what the generic override targets).\n */\nexport type Walkable = {\n readonly _tag: string\n readonly type?: string\n readonly children?: ReadonlyArray<Walkable>\n}\n\nexport interface WalkVisitor<N extends Walkable = CSTChild, C = undefined> {\n /**\n * Called on entering a node, before its children. Return `false` to skip\n * descending into this node's children (`leave` still runs).\n */\n enter?(node: N, parent: N | null, ctx: C): boolean | void\n /** Called on leaving a node, after its children. */\n leave?(node: N, parent: N | null, ctx: C): void\n}\n\n/**\n * Depth-first traversal. Visits `root`, then each structural child in order.\n * `ctx` is threaded to both hooks unchanged (use it as an accumulator).\n *\n * Defaults to the CST shape; override with `walk<MyNode>(root, \u2026)`:\n *\n * const idents: string[] = []\n * walk(tree, {\n * enter(node) {\n * if (node._tag === 'leaf') idents.push(node.value)\n * },\n * })\n */\nexport function walk<N extends Walkable = CSTChild, C = undefined>(\n root: N,\n visitor: WalkVisitor<N, C>,\n ctx: C = undefined as C,\n): void {\n const go = (node: N, parent: N | null): void => {\n const descend = visitor.enter ? visitor.enter(node, parent, ctx) : undefined\n const children = node.children as ReadonlyArray<N> | undefined\n if (descend !== false && Array.isArray(children)) {\n for (const child of children) go(child, node)\n }\n visitor.leave?.(node, parent, ctx)\n }\n go(root, null)\n}\n\nexport interface VisitApi<R, N extends Walkable = CSTChild> {\n /** Dispatch a node to its handler (by `type`); returns the handler's result. */\n visit(node: N): R | undefined\n /** Visit every structural child, collecting the defined results in order. */\n visitChildren(node: N): R[]\n}\n\nexport type VisitorHandlers<R, N extends Walkable = CSTChild> = Record<\n string,\n (node: N, api: VisitApi<R, N>) => R\n>\n\n/**\n * Build a visitor that dispatches on each node's `type` \u2014 the direct analog of a\n * generated CST-visitor base class. Handlers are keyed by rule name and receive\n * an `api` with `visit` / `visitChildren` to recurse:\n *\n * const evalExpr = createVisitor<number>({\n * Num: (n) => Number((n as NumNode).value),\n * Add: (n, api) => api.visitChildren(n).reduce((a, b) => a + b, 0),\n * })\n * const total = evalExpr(tree)\n *\n * Defaults to the CST shape; override the return type and node type together with\n * `createVisitor<number, MyNode>({ \u2026 })`. A node whose `type` has no handler\n * falls through to visiting its children (results discarded), so partial visitors\n * work without listing every rule.\n */\nexport function createVisitor<R = void, N extends Walkable = CSTChild>(\n handlers: VisitorHandlers<R, N>,\n): (node: N) => R | undefined {\n const api: VisitApi<R, N> = {\n visit(node) {\n const handler = node.type !== undefined ? handlers[node.type] : undefined\n if (handler) return handler(node, api)\n api.visitChildren(node)\n return undefined\n },\n visitChildren(node) {\n const out: R[] = []\n const children = node.children as ReadonlyArray<N> | undefined\n if (Array.isArray(children)) {\n for (const child of children) {\n const r = api.visit(child)\n if (r !== undefined) out.push(r)\n }\n }\n return out\n },\n }\n return (node) => api.visit(node)\n}\n", "/**\n * Relative span model for incremental edits.\n *\n * Absolute source offsets are simple but fragile: an insertion at offset `at`\n * shifts EVERY downstream offset, so a whole-tree renumber is O(nodes-after-edit).\n * Storing each node's span RELATIVE to its parent's start makes edits local: an\n * insertion inside child k of a node only changes\n * (a) child k's relative end,\n * (b) the relative start of siblings after k,\n * and leaves the *children* of those shifted siblings untouched \u2014 they move with\n * their parent for free. So the mutation count is O(depth + trailing siblings),\n * not O(all following nodes), and every unaffected subtree is shared by identity.\n *\n * An absolute view is recovered on query by accumulating the parent base as you\n * descend (the `absolutize` / `absoluteSpanAt` cursor) \u2014 one source of truth,\n * two manifestations.\n */\n\n/** A node with an ABSOLUTE source span and ordered children. */\nexport type AbsNode = {\n readonly start: number\n readonly end: number\n readonly children: readonly AbsNode[]\n}\n\n/** A node whose `start`/`end` are RELATIVE to its parent's start (root base = 0). */\nexport type RelNode = {\n readonly start: number\n readonly end: number\n readonly children: readonly RelNode[]\n}\n\n/** Convert an absolute tree to parent-relative spans. */\nexport function relativize(node: AbsNode, parentBase = 0): RelNode {\n return {\n start: node.start - parentBase,\n end: node.end - parentBase,\n children: node.children.map((c) => relativize(c, node.start)),\n }\n}\n\n/** Reconstruct absolute spans from a relative tree. Inverse of `relativize`. */\nexport function absolutize(node: RelNode, parentBase = 0): AbsNode {\n const start = parentBase + node.start\n return {\n start,\n end: parentBase + node.end,\n children: node.children.map((c) => absolutize(c, start)),\n }\n}\n\n/**\n * Absolute span of the node at `path` (child indices from the root), accumulating\n * the base as it descends \u2014 O(path length), no full absolutize. The relative-form\n * cursor: query any node's absolute position without materializing the whole tree.\n */\nexport function absoluteSpanAt(root: RelNode, path: readonly number[]): { start: number; end: number } {\n let base = 0\n let node = root\n for (const idx of path) {\n base += node.start\n const child = node.children[idx]\n if (!child) throw new RangeError(`absoluteSpanAt: no child ${idx} on path ${path.join('.')}`)\n node = child\n }\n return { start: base + node.start, end: base + node.end }\n}\n\n// ---------------------------------------------------------------------------\n// CST-shaped projection (parseDoc incremental trees)\n//\n// The bare Abs/RelNode helpers above are the reference model. The functions\n// below apply the same relative-span algebra to real CST children \u2014 nodes,\n// leaves, and error nodes \u2014 where the span is a nested `{ start, end }` and\n// children are heterogeneous (leaves carry a span but no `children`). Trivia\n// never appears in structural `children`, so every child has a span.\n// ---------------------------------------------------------------------------\n\n/**\n * A CST child as far as span projection cares: a nested span + optional children.\n * `children` is typed loosely (`unknown[]`) so the real heterogeneous CST unions\n * (nodes, leaves, error nodes \u2014 a leaf has no `children`) satisfy it directly.\n */\ntype Spanned = {\n readonly span: { readonly start: number; readonly end: number }\n readonly children?: readonly unknown[]\n}\n\n/**\n * Rewrite a CST subtree's spans to be RELATIVE to `parentBase` (the absolute\n * start of the node's parent; root base = 0). Each child's own children are\n * relativized against *this* node's absolute start. Preserves every other field\n * (`_tag`, `type`, `state`, leaf `value`, \u2026) by shallow spread.\n */\nexport function relativizeCST<T extends Spanned>(node: T, parentBase = 0): T {\n const span = { start: node.span.start - parentBase, end: node.span.end - parentBase }\n if (node.children) {\n const base = node.span.start\n return { ...node, span, children: node.children.map((c) => relativizeCST(c as Spanned, base)) } as T\n }\n return { ...node, span } as T\n}\n\n/** Reconstruct absolute CST spans from a relative subtree. Inverse of `relativizeCST`. */\nexport function absolutizeCST<T extends Spanned>(node: T, parentBase = 0): T {\n const start = parentBase + node.span.start\n const span = { start, end: parentBase + node.span.end }\n if (node.children) {\n return { ...node, span, children: node.children.map((c) => absolutizeCST(c as Spanned, start)) } as T\n }\n return { ...node, span } as T\n}\n\n/**\n * Absolute span of the CST node at `path` (child indices from the root),\n * accumulating the base as it descends \u2014 O(path length), no full absolutize.\n * The relative-form cursor for a positioned CST: query one node's absolute\n * position without materializing the whole tree.\n */\nexport function absoluteSpanCST(root: Spanned, path: readonly number[]): { start: number; end: number } {\n let base = 0\n let node: Spanned = root\n for (const idx of path) {\n base += node.span.start\n const child = node.children?.[idx] as Spanned | undefined\n if (!child) throw new RangeError(`absoluteSpanCST: no child ${idx} on path ${path.join('.')}`)\n node = child\n }\n return { start: base + node.span.start, end: base + node.span.end }\n}\n\n/** Naive absolute reshift (baseline): shift every offset >= `at` by `delta`. */\nexport function shiftAbsolute(node: AbsNode, at: number, delta: number): AbsNode {\n const s = node.start >= at ? node.start + delta : node.start\n const e = node.end >= at ? node.end + delta : node.end\n return {\n start: s,\n end: e,\n children: node.children.map((c) => shiftAbsolute(c, at, delta)),\n }\n}\n\n/**\n * Apply an edit of `delta` characters at absolute offset `at` to a RELATIVE tree.\n * Returns a new relative tree; every subtree that ends at/before `at` (in absolute\n * terms) is returned by IDENTITY (unchanged), so the result structurally shares\n * all unaffected subtrees with the input.\n *\n * `stats.allocated` counts nodes that had to be re-created \u2014 the incremental cost.\n */\nexport function applyEdit(\n root: RelNode,\n at: number,\n delta: number,\n stats?: { allocated: number },\n): RelNode {\n // The root has no parent; its base never shifts (base 0).\n return editNode(root, at, delta, 0, 0, stats)\n}\n\nfunction editNode(\n node: RelNode,\n at: number,\n delta: number,\n parentBase: number,\n parentShift: number, // how much this node's parent base moved (0 or delta)\n stats?: { allocated: number },\n): RelNode {\n const absStart = parentBase + node.start\n const absEnd = parentBase + node.end\n\n // Strictly before the edit \u2192 unchanged (its parent base is also unshifted).\n // Uses `< at` (not `<=`) to match the shift convention: offsets `>= at` move,\n // so a node ending exactly at `at` grows and must be rewritten.\n if (absEnd < at) return node\n\n // Fully after the edit AND the parent moved by `delta` too \u2192 this node moves as\n // one unit with its parent, so its parent-relative offsets are identical. Share\n // the ENTIRE subtree by identity \u2014 this is the O(depth) locality win.\n const startShifts = absStart >= at\n if (startShifts && parentShift === delta) return node\n\n if (stats) stats.allocated++\n\n const newAbsStart = startShifts ? absStart + delta : absStart\n const newAbsEnd = absEnd + delta // absEnd > at here (fully-before returned above)\n const newParentBase = parentBase + parentShift\n const newStart = newAbsStart - newParentBase\n const newEnd = newAbsEnd - newParentBase\n\n // Children are relative to this node's absolute start. Our own shift (0 or delta)\n // becomes their parentShift.\n const ownShift = newAbsStart - absStart\n let changed = false\n const newChildren: RelNode[] = []\n for (const c of node.children) {\n const nc = editNode(c, at, delta, absStart, ownShift, stats)\n if (nc !== c) changed = true\n newChildren.push(nc)\n }\n\n return {\n start: newStart,\n end: newEnd,\n children: changed ? newChildren : node.children,\n }\n}\n", "/**\n * Incremental parse document \u2014 incremental re-parse over a rules() registry.\n */\nimport type { Combinator, ParseContext, ParseError, ParseResult, ParserDef, Span } from '../types.ts'\nimport type { NodeLike, CSTLeaf, CSTError } from '../cst/types.ts'\nimport { relativizeCST, absoluteSpanCST } from '../cst/relative-spans.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/**\n * Build the parse ctx for a (re)parse. In `tolerant` mode the same recovery bundle\n * the interpreter/`run` use is installed (`_tolerant` + `_rec`), so a broken edit\n * keeps producing a tree (with recovered `ParseError`s embedded) instead of\n * collapsing to `null`. Strict mode is byte-identical to before. Every reparse site\n * \u2014 root, localized rule reparse, list-splice middle, and the soundness probes \u2014\n * threads the SAME flag, so a probe's tolerance matches the parse it is checking\n * (a mismatch would spuriously diverge and forgo reuse, never miscompare).\n */\nfunction mkCtx(state: unknown, build: ParseContext['build'], tolerant: boolean): ParseContext {\n return tolerant\n ? { trackLines: false, state, build, _tolerant: true, _rec: REC, _errors: [] }\n : { trackLines: false, state, build }\n}\n\n/** A single compiled (or interpreted) rule: parse from `pos`, producing node `N`. */\nexport type RuleFn<N> = (input: string, pos: number, ctx: ParseContext) => ParseResult<N>\n\n/**\n * Rule name \u2192 parser. Each entry is either a bare parse function or a\n * `Combinator` (what `rules()` returns). Passing the `rules()` combinators\n * directly lets `.edit()` inspect the grammar \u2014 required for **sound** structural\n * list-reuse (see `structuralReuse`): only a rule the grammar proves is a genuine\n * repetition is ever spliced. A bare-function registry still parses correctly; it\n * just can't be structurally reused (the splice is skipped, never guessed).\n */\nexport type Registry<N> = Record<string, RuleFn<N> | Combinator<N>>\n\n/** Normalize a registry entry to a callable parse function. */\nfunction asRuleFn<N>(entry: RuleFn<N> | Combinator<N>): RuleFn<N> {\n return typeof entry === 'function' ? entry : (input, pos, ctx) => entry.parse(input, pos, ctx)\n}\n\n/** The combinator def behind a registry entry, if it carries one (bare functions don't). */\nfunction defOf(entry: unknown): ParserDef | undefined {\n return typeof entry === 'object' && entry !== null && '_def' in entry\n ? (entry as { _def: ParserDef })._def\n : undefined\n}\n\n/**\n * Does this rule's grammar produce its element children via a genuine, unbounded\n * **repetition** (`sepBy` / `many` / `oneOrMore`) \u2014 as opposed to a fixed-arity\n * sequence of same-typed tokens (e.g. `Triple = Num ',' Num ',' Num`)? Only the\n * former is sound to structurally reuse: a full reparse accepts any element count,\n * so splicing one in or out matches it; a fixed-arity rule would not. We walk the\n * def, transparently unwrapping semantic wrappers (`node`, `transform`, \u2026) and\n * looking through a top-level `sequence` / `optional` (the `[ (sepBy)? ]` shape),\n * and return true iff a repetition combinator is reachable that way. Structurally\n * indistinguishable-from-the-CST cases (fixed sequences) return false and fall\n * back to a full, correct reparse. This is what makes `structuralReuse` sound\n * rather than a promise the caller has to keep.\n */\nfunction producesRepetition(def: ParserDef | undefined, depth = 0): boolean {\n if (!def || depth > 24) return false\n const d = def as ParserDef & Record<string, unknown>\n switch (d.tag) {\n case 'sepBy':\n case 'many':\n case 'oneOrMore':\n return true\n // Rule entries and `ref`s wrap their body in a `lazy` thunk \u2014 resolve it. The\n // `depth` cap bounds any self-referential cycle (a repetition, if present, is\n // found shallowly before recursion goes deep).\n case 'lazy': {\n const thunk = d.thunk as (() => { _def?: ParserDef }) | undefined\n let inner: { _def?: ParserDef } | undefined\n try { inner = typeof thunk === 'function' ? thunk() : undefined } catch { return false }\n return producesRepetition(inner?._def, depth + 1)\n }\n // Transparent wrappers \u2014 look through to the inner parser.\n case 'node':\n case 'transform':\n case 'attempt':\n case 'token':\n case 'leaf':\n case 'label':\n case 'field':\n case 'expect':\n case 'withCtx':\n case 'grammar':\n case 'optional':\n return producesRepetition(defOf(d.parser), depth + 1)\n case 'skip':\n return producesRepetition(defOf(d.main), depth + 1)\n // A bracketed/anchored list is a sequence whose element run is a repetition.\n case 'sequence':\n return (d.parsers as unknown[]).some(p => producesRepetition(defOf(p), depth + 1))\n default:\n return false\n }\n}\n\nexport type ParseDocOptions<N extends NodeLike> = {\n /** Initial grammar state threaded into ctx.state for the root parse. */\n state?: unknown\n /**\n * Reconstruct a parent node with one child replaced (used when grafting a\n * re-parsed subtree into its ancestors). Defaults to a shallow spread, which\n * works for plain-object nodes; class-instance ASTs should supply their own.\n */\n rebuild?: (node: N, children: ReadonlyArray<N | CSTLeaf | CSTError>) => N\n /**\n * Mode host for a linkable/fused grammar (RULE_ABI_PLAN \u00A77): threaded into\n * `ctx.build` on every (re)parse so `node()` rules build a positioned CST /\n * language-service tree instead of their own eval-AST. Unset \u2192 the grammar's\n * own builders (eval mode).\n */\n build?: ParseContext['build']\n /**\n * Enable structural list-reuse: on a length-changing *structural* edit (adding\n * or removing a whole element in a collection) that would otherwise force a full\n * reparse, reparse only the disturbed span and reuse the collection's untouched\n * tail elements by identity \u2014 turning an insert near the top of a large list from\n * O(list) into O(edit + trailing siblings).\n *\n * OFF by default because it is sound only when a rule whose CST children form a\n * homogeneous, separator-delimited element list is a genuine REPETITION\n * (`many` / `sepBy` / `oneOrMore`), not a fixed-arity sequence of same-typed\n * tokens (e.g. `Triple = Num ',' Num ',' Num`) \u2014 the two are structurally\n * indistinguishable without the grammar, and splicing the latter would accept an\n * element count it shouldn't. Turn it on when your list rules are true\n * repetitions (the common case: JSON arrays/objects, CSS value lists, argument\n * lists). Every splice is still guarded (exact tiling + lookahead probe +\n * stateless-tail check) and falls back to a full, correct reparse when unproven;\n * the flag only authorises *attempting* the reuse.\n */\n structuralReuse?: boolean\n /**\n * Parse tolerantly: `many`/`sepBy`/`oneOrMore` recover from a failed element\n * (skip to an inferred sync point, embed a `ParseError` over the skipped span),\n * so a broken edit keeps producing a tree instead of collapsing to `null` \u2014 the\n * editor-backend path. Off by default (strict, byte-identical). Recovery is a cold\n * path: well-formed input never triggers it. Every reparse the doc does \u2014 root,\n * localized rule, and list-splice \u2014 inherits this flag, and the reuse-soundness\n * probes run at the same tolerance so incremental reuse stays valid under recovery\n * (a scan that would cross a splice boundary just falls back to a full reparse).\n */\n tolerant?: boolean\n /**\n * The grammar's trivia rule, used ONLY to compute `unconsumedFrom`: a root rule\n * consumes trivia BETWEEN terms but not after the last, so trailing\n * whitespace/comments would otherwise read as leftover input. Given the trivia\n * rule, the tail is skipped before reporting the first unconsumed offset \u2014\n * matching `run()`'s semantics. Defaults to the root rule's own\n * `_meta.grammarTrivia` (from `rules({ trivia })`); set it only to override.\n */\n trivia?: Combinator<unknown>\n}\n\nexport interface ParseDoc<N extends NodeLike> {\n /**\n * The parse tree with PARENT-RELATIVE spans \u2014 each node's `span` is relative to\n * its parent's start (root base 0). This is the shareable representation: a\n * length-changing `.edit()` keeps every untouched subtree shared by identity,\n * and reading the tree is O(1) (no offset rewrite). For absolute positions use\n * the O(depth) cursor `spanAt(path)`, or `absolutizeCST(doc.tree)` to\n * materialize the whole absolute tree. A fresh non-incremental `node().parse()`\n * result is unchanged \u2014 still absolute.\n */\n readonly tree: N | null\n /**\n * Recovery diagnostics collected during the (re)parse \u2014 the missing-token\n * `expect()` errors and tolerant-list recovery errors that ride the tree as\n * `parseError` nodes, surfaced here as a flat list too (spans ABSOLUTE). Empty\n * in strict mode. On a hard (non-recovered) parse failure this holds the single\n * top-level failure. This is what makes an editor document able to see syntax\n * errors \u2014 a blank `errors: []` (the prior behaviour) hid every recovery.\n */\n readonly errors: ParseError[]\n /**\n * Offset where unparsed input begins \u2014 the first non-trivia character the parse\n * left unconsumed (trailing trivia skipped when a trivia rule is available), or\n * `null` if the whole input was consumed. This is how a document detects \"the\n * grammar stopped short, there's junk here\"; computed exactly as `run()` does.\n */\n readonly unconsumedFrom: number | null\n readonly input: string\n /**\n * Absolute span of the node at `path` (child indices from the root) \u2014 O(depth),\n * without materializing the absolute tree. The projection cursor for the\n * relative representation; use it for spot queries on a large incremental doc.\n */\n spanAt(path: readonly number[]): { start: number; end: number }\n /**\n * Incrementally re-parse after a text change. `from`/`to` are byte offsets in\n * the OLD input; `replacement` fills that range (editor change-event shape).\n * Sound: the result tree is always structurally identical to a fresh\n * `parseDoc` of the edited text (the Stage-2 guard falls back to a full\n * reparse whenever reuse can't be proven safe). Reuse/strategy is intentionally\n * NOT reported here \u2014 an observer derives it by diffing this tree against the\n * previous one (see the incremental tests); the runtime's job is to be fast,\n * not to measure itself.\n */\n edit(from: number, to: number, replacement: string): ParseDoc<N>\n}\n\n// ---------------------------------------------------------------------------\n// Tree navigation (generic over NodeLike \u2014 no class, no CST assumptions)\n// ---------------------------------------------------------------------------\n\n/**\n * A reentry candidate is only worth reparsing if it's meaningfully smaller than\n * the whole document \u2014 reparsing a rule that spans (say) >half the input costs\n * about as much as a full reparse with none of the reuse. Above this fraction of\n * the input length, `.edit()` skips straight to a full reparse. Keeps the worst\n * case (a structural edit near the front) at ~1\u00D7 full reparse, never several.\n */\nconst REENTRY_MAX_SPAN_FRACTION = 0.5\n\ntype FoundNode<N extends NodeLike> = { node: N; path: number[] }\n\nfunction isNode(x: unknown): x is NodeLike {\n return typeof x === 'object' && x !== null && (x as { _tag?: string })._tag === 'node'\n}\n\n/**\n * Locate the deepest node containing absolute offset `pos`. The tree stores\n * PARENT-RELATIVE spans (see relative-spans), so we thread each node's absolute\n * start down the descent: a child's absolute start is `nodeAbsStart +\n * child.span.start`.\n */\nfunction findContaining<N extends NodeLike>(\n node: N,\n nodeAbsStart: number,\n pos: number,\n path: number[] = [],\n): FoundNode<N> | null {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i]!\n if (!isNode(child)) continue\n const childAbsStart = nodeAbsStart + child.span.start\n const childAbsEnd = nodeAbsStart + child.span.end\n if (childAbsStart <= pos && pos < childAbsEnd) {\n const inner = findContaining(child as N, childAbsStart, pos, [...path, i])\n return inner ?? { node: child as N, path: [...path, i] }\n }\n }\n return null\n}\n\nfunction ancestorsAt<N extends NodeLike>(root: N, path: number[]): N[] {\n const ancestors: N[] = [root]\n let cur: N = root\n for (const idx of path.slice(0, -1)) {\n const child = cur.children[idx]\n if (!child || !isNode(child)) break\n ancestors.push(child as N)\n cur = child as N\n }\n return ancestors\n}\n\nfunction replaceAtPath<N extends NodeLike>(\n rebuild: NonNullable<ParseDocOptions<N>['rebuild']>,\n root: N,\n path: number[],\n newNode: N,\n): N {\n if (path.length === 0) return newNode\n const [idx, ...rest] = path as [number, ...number[]]\n const newChildren = [...root.children] as Array<N | CSTLeaf | CSTError>\n newChildren[idx] = rest.length === 0\n ? newNode\n : replaceAtPath(rebuild, root.children[idx] as N, rest, newNode)\n return rebuild(root, newChildren)\n}\n\nfunction defaultRebuild<N extends NodeLike>(node: N, children: ReadonlyArray<N | CSTLeaf | CSTError>): N {\n return { ...node, children } as N\n}\n\ntype SpanChild = { span: { start: number; end: number }; children?: readonly unknown[] }\n\n/**\n * Shift a node's PARENT-RELATIVE span by `delta`. Used for siblings that sit\n * *after* a length-changing edit inside the same parent: the node moved as a\n * unit, so both endpoints slide by `delta`, but its own children are relative to\n * *its* start and are unchanged \u2014 so `children` is shared by identity. That's the\n * relative-span win: O(1) per trailing sibling, not O(subtree). (Contrast the old\n * absolute model, which had to deep-rewrite every descendant.)\n */\nfunction shiftRelStart<T>(child: T, delta: number): T {\n const c = child as unknown as SpanChild\n return { ...(c as object), span: { start: c.span.start + delta, end: c.span.end + delta } } as unknown as T\n}\n\n/**\n * Graft `newNode` at `path` into a parent-relative tree for a length-changing\n * edit (`delta !== 0`): children before the edit are shared by reference, the\n * edited child is replaced, children *after* it have their relative start slid by\n * `delta` (one shallow alloc each \u2014 their subtrees are shared by identity), and\n * each ancestor's relative `span.end` grows by `delta` while its start is\n * untouched. Cost is O(depth + trailing siblings along the spine), independent of\n * how many nodes sit inside those trailing siblings.\n */\nfunction graftRelative<N extends NodeLike>(root: N, path: number[], newNode: N, delta: number): N {\n if (path.length === 0) return newNode\n const [idx, ...rest] = path as [number, ...number[]]\n const oldChildren = root.children as ReadonlyArray<N | CSTLeaf | CSTError>\n const newChildren = oldChildren.map((child, i) => {\n if (i < idx) return child\n if (i === idx) {\n return rest.length === 0 ? newNode : graftRelative(child as N, rest, newNode, delta)\n }\n return shiftRelStart(child, delta)\n })\n return {\n ...root,\n span: { start: root.span.start, end: root.span.end + delta },\n children: newChildren,\n } as N\n}\n\n// ---------------------------------------------------------------------------\n// List-splice reuse (structural edits in a collection)\n//\n// A structural edit \u2014 inserting or deleting a whole element in a list \u2014 makes the\n// innermost-rule reparse fail to converge, and the containing collection is often\n// too big to reparse (that's the \"insert at the top of a large array\" case where\n// a whole-rule reparse costs ~a full reparse). Instead of reparsing the whole\n// collection, reparse ONLY the disturbed span between the last untouched element\n// before the edit and the first untouched element after it, then splice: the head\n// children are shared as-is, the freshly-parsed middle replaces the changed run,\n// and the tail children are reused with their relative start slid by `delta`\n// (O(1) each \u2014 their subtrees are shared by identity, the relative-span win).\n//\n// Soundness rests on: (1) the reparsed middle exactly fills [reStart, reEnd) and\n// lands on element/separator boundaries; (2) a lookahead probe proving the middle\n// read nothing at/after the splice point (else it peeked into the reused tail);\n// (3) a stateless-reentry precondition on the reused tail (its saved `state` is\n// null), so a forward, context-free grammar reproduces those subtrees unchanged.\n// Any failure \u21D2 `null` \u21D2 the caller falls back to a full, correct reparse. The\n// incremental oracle fuzz (edit() \u2261 full reparse, 400 seeds \u00D7 3 list grammars +\n// edge cases) is the end-to-end correctness net.\n// ---------------------------------------------------------------------------\n\ntype AnyChild = { readonly _tag: string; readonly span: { start: number; end: number }; readonly value?: string; readonly type?: string; readonly state?: unknown; readonly children?: readonly unknown[] }\n\n/**\n * Parse the collection's disturbed middle `[reStart, reEnd)` as an\n * `element (separator element)*` run (optionally with a trailing separator that\n * connects to the reused tail), C-relative. Alternation is STRICT: after an\n * element, if we're not yet at `reEnd`, the separator MUST match \u2014 element/element\n * juxtaposition is rejected (that's a `sepBy` violation). When the grammar has no\n * separator (`separator === null`, i.e. a bare repetition), consecutive elements\n * are allowed. Returns the C-relative children, or `null` if the run doesn't tile\n * `[reStart, reEnd)` exactly on token boundaries.\n */\nfunction parseMiddle(\n input: string,\n reStart: number,\n reEnd: number,\n cStart: number,\n ruleFn: RuleFn<NodeLike>,\n separator: string | null,\n build: ParseContext['build'],\n tolerant: boolean,\n): AnyChild[] | null {\n const ctx: ParseContext = mkCtx(null, build, tolerant)\n const out: AnyChild[] = []\n let pos = reStart\n let guard = 0\n while (pos < reEnd) {\n if (++guard > reEnd - reStart + 2) return null // no-progress backstop\n // After an element, a separator is mandatory before the next element (unless\n // the grammar is separator-free). This is what keeps `elem elem` \u2014 invalid in\n // a `sepBy` \u2014 from being accepted as two elements.\n if (separator && out.length > 0 && out[out.length - 1]!._tag === 'node') {\n if (pos + separator.length > reEnd || !input.startsWith(separator, pos)) return null\n out.push({ _tag: 'leaf', value: separator, span: { start: pos - cStart, end: pos + separator.length - cStart } })\n pos += separator.length\n continue\n }\n let r: ParseResult<NodeLike>\n try {\n r = ruleFn(input, pos, ctx)\n } catch {\n return null\n }\n if (!r.ok || r.span.end <= pos || r.span.end > reEnd) return null\n out.push(relativizeCST(r.value as unknown as AnyChild, cStart))\n pos = r.span.end\n }\n return pos === reEnd ? out : null\n}\n\n/**\n * Try to reuse the untouched tail of collection `C` (at `path`, absolute start\n * `cStart`) around a length-changing edit, reparsing only the disturbed middle.\n * Returns the new relative root, or `null` to fall back to a full reparse.\n */\nfunction tryListSplice<N extends NodeLike>(\n root: N,\n path: number[],\n C: N,\n cStart: number,\n newInput: string,\n from: number,\n to: number,\n delta: number,\n registry: Record<string, RuleFn<N>>,\n build: ParseContext['build'],\n tolerant: boolean,\n): N | null {\n const kids = C.children as ReadonlyArray<AnyChild>\n if (kids.length === 0) return null\n\n // head = maximal prefix ending before the edit, backed off so it ends at a\n // leaf (open delimiter or separator) \u2014 i.e. where an ELEMENT is next expected.\n let h = 0\n while (h < kids.length && cStart + kids[h]!.span.end <= from) h++\n while (h > 0 && kids[h - 1]!._tag === 'node') h--\n // tail = maximal suffix starting after the edit, advanced so it BEGINS at an\n // element (node) boundary \u2014 a self-contained element run to reuse.\n let t = kids.length\n while (t > h && cStart + kids[t - 1]!.span.start >= to) t--\n while (t < kids.length && kids[t]!._tag !== 'node') t++\n if (t >= kids.length) return null // nothing reusable after the edit\n\n // Stateless-reentry precondition on the reused tail (see soundness note).\n for (let i = t; i < kids.length; i++) {\n if (kids[i]!._tag === 'node' && kids[i]!.state != null) return null\n }\n\n // C must look like a genuine homogeneous COLLECTION \u2014 a repetition of one\n // element rule joined by one separator \u2014 not a fixed heterogeneous sequence\n // (e.g. `Pair = Key ':' Val`, whose `:` is not a list separator). Require: all\n // element (node) children share a type, and there are \u22652 elements joined by a\n // consistent separator leaf. This rejects fixed sequences (their nodes differ\n // in type, or there's only one) so we never treat them as splice-able lists.\n let elemType: string | undefined\n let elemCount = 0\n for (const k of kids) {\n if (k._tag !== 'node') continue\n elemCount++\n if (elemType === undefined) elemType = k.type\n else if (k.type !== elemType) return null // heterogeneous \u2192 not a collection\n }\n if (elemType === undefined || elemCount < 2) return null\n const ruleFn = registry[elemType]\n if (!ruleFn) return null\n // The separator is the leaf that appears between two elements; require it to be\n // consistent everywhere two elements meet (a real list delimiter).\n let separator: string | null = null\n for (let i = 1; i < kids.length - 1; i++) {\n if (kids[i - 1]!._tag === 'node' && kids[i + 1]!._tag === 'node') {\n if (kids[i]!._tag !== 'leaf') return null\n const sep = kids[i]!.value ?? null\n if (separator === null) separator = sep\n else if (sep !== separator) return null // inconsistent delimiter \u2192 not a plain list\n }\n }\n if (separator === null) return null // \u22652 elements but no delimiter between any pair\n\n // The disturbed middle must be pure elements + separators. If the collection is\n // bracketed, its OPENING / CLOSING delimiter (a leaf whose value isn't the\n // separator, sitting before the first / after the last element) must lie OUTSIDE\n // the edit \u2014 otherwise the edit changed the collection's own framing and a\n // whole-rule reparse (not a splice) is required.\n const first = kids[0]!\n const last = kids[kids.length - 1]!\n if (first._tag === 'leaf' && first.value !== separator && from < cStart + first.span.end) return null\n if (last._tag === 'leaf' && last.value !== separator && to > cStart + last.span.start) return null\n\n const reStart = h > 0 ? cStart + kids[h - 1]!.span.end : cStart\n const reEnd = cStart + kids[t]!.span.start + delta\n if (reEnd < reStart) return null\n\n const middle = parseMiddle(newInput, reStart, reEnd, cStart, ruleFn as RuleFn<NodeLike>, separator, build, tolerant)\n if (!middle) return null\n\n // Junction check: `tail` always begins with an ELEMENT (we advanced `t` to a\n // node), so a non-empty middle must END with a SEPARATOR to connect validly \u2014\n // otherwise the edit deleted the delimiter between the middle's last element and\n // the tail's first, and splicing would fuse two elements with no separator (a\n // `sepBy` violation the whole-collection reparse would never produce). When the\n // middle is empty the tail is preceded by `head`'s last token, itself already a\n // separator or the open delimiter (head was backed off to a leaf), so it's fine.\n if (middle.length > 0) {\n const last = middle[middle.length - 1]!\n if (!(last._tag === 'leaf' && last.value === separator)) return null\n }\n\n // Lookahead guard: the middle must have read nothing at/after `reEnd`, else it\n // peeked into the reused tail. Re-run over an input whose tail is overwritten\n // with a sentinel and require an identical middle. Two sentinels so the real\n // byte at `reEnd` can't accidentally match the probe.\n if (reEnd < newInput.length) {\n for (const sentinel of [' ', '\uFFFF']) {\n if (newInput[reEnd] === sentinel) continue\n const probed = newInput.slice(0, reEnd) + sentinel.repeat(newInput.length - reEnd)\n const probe = parseMiddle(probed, reStart, reEnd, cStart, ruleFn as RuleFn<NodeLike>, separator, build, tolerant)\n if (!probe || probe.length !== middle.length) return null\n for (let i = 0; i < middle.length; i++) if (!structurallyEqual(probe[i], middle[i])) return null\n }\n }\n\n const head = kids.slice(0, h)\n const tail = kids.slice(t).map((k) => shiftRelStart(k, delta))\n const newC = {\n ...C,\n span: { start: C.span.start, end: C.span.end + delta },\n children: [...head, ...middle, ...tail],\n } as N\n\n // Replace C at `path` and slide C's own trailing siblings / ancestor ends.\n return graftRelative(root, path, newC, delta)\n}\n\n// ---------------------------------------------------------------------------\n// Stage-2 soundness guard\n// ---------------------------------------------------------------------------\n\n/**\n * Deep structural equality on parse trees: `_tag`, `span`, node `type`, leaf\n * `value`, and children pairwise. This is the oracle relation `.edit()` must\n * preserve against a full reparse; it's also what the Stage-2 guard compares\n * probe results with.\n */\nexport function structurallyEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false\n const at = (a as { _tag?: string })._tag\n const bt = (b as { _tag?: string })._tag\n if (at !== bt) return false\n const as = (a as { span?: { start: number; end: number } }).span\n const bs = (b as { span?: { start: number; end: number } }).span\n if (as || bs) {\n if (!as || !bs || as.start !== bs.start || as.end !== bs.end) return false\n }\n if (at === 'leaf' || at === 'trivia') {\n return (a as { value: string }).value === (b as { value: string }).value\n }\n if (at === 'parseError') {\n // Embedded recovered error: span already compared above; also compare the\n // expected-token set so two errors at the same span but different expectations\n // are not conflated (the oracle must distinguish them).\n const ae = (a as { expected?: readonly string[] }).expected ?? []\n const be = (b as { expected?: readonly string[] }).expected ?? []\n return ae.length === be.length && ae.every((x, i) => x === be[i])\n }\n if ((a as { type?: string }).type !== (b as { type?: string }).type) return false\n const ac = (a as { children?: readonly unknown[] }).children\n const bc = (b as { children?: readonly unknown[] }).children\n if (ac || bc) {\n if (!ac || !bc || ac.length !== bc.length) return false\n for (let i = 0; i < ac.length; i++) if (!structurallyEqual(ac[i], bc[i])) return false\n }\n return true\n}\n\n/**\n * A reused suffix spliced at `boundary` (new-input coords) is sound only if the\n * re-parse of the containing rule did NOT read any input at or after `boundary`\n * \u2014 otherwise a lookahead or backtrack peeked across the splice and the reused\n * tail could be wrong. We prove independence by re-running the same rule at the\n * same start on an input whose entire tail from `boundary` is overwritten with a\n * sentinel: if the produced node is byte-for-byte structurally identical,\n * nothing past `boundary` was inspected. Two distinct sentinels are tried so the\n * real char at `boundary` can't accidentally equal the probe. Conservative by\n * construction \u2014 any probe difference, failure, or throw \u21D2 not safe \u21D2 the caller\n * widens toward a full reparse (correctness over reuse fraction).\n */\nfunction boundaryIsSafe<N extends NodeLike>(\n ruleFn: RuleFn<N>,\n newInput: string,\n start: number,\n boundary: number,\n state: unknown,\n build: ParseContext['build'],\n produced: ParseResult<N>,\n tolerant: boolean,\n): boolean {\n if (!produced.ok) return false\n if (boundary >= newInput.length) return true // nothing after the node to peek at\n for (const sentinel of [' ', '\uFFFF']) {\n if (newInput[boundary] === sentinel) continue\n const probed = newInput.slice(0, boundary) + sentinel.repeat(newInput.length - boundary)\n const ctx: ParseContext = mkCtx(state, build, tolerant)\n let r: ParseResult<N>\n try {\n r = ruleFn(probed, start, ctx)\n } catch {\n return false\n }\n if (!r.ok || r.span.end !== produced.span.end) return false\n if (!structurallyEqual(r.value, produced.value)) return false\n }\n return true\n}\n\n// ---------------------------------------------------------------------------\n// Document\n// ---------------------------------------------------------------------------\n\nclass ParseDocImpl<N extends NodeLike> implements ParseDoc<N> {\n private readonly _registry: Registry<N>\n /** Registry entries normalized to callable parse functions (memoized once). */\n private readonly _fns: Record<string, RuleFn<N>>\n /** Rule names the grammar proves are genuine repetitions \u2014 the ONLY splice-safe types. */\n private readonly _reps: Set<string>\n private readonly _rootRule: string\n private readonly _opts: ParseDocOptions<N>\n /**\n * The tree, held in whichever coordinate system it arrived in. A fresh parse\n * arrives ABSOLUTE (`_abs` set); an incremental graft arrives PARENT-RELATIVE\n * (`_rel` set). The public `tree` is always the RELATIVE form (so untouched\n * subtrees stay shared by identity across edits and reads are O(1)); it's\n * materialized from `_abs` once, on demand, and memoized. `undefined` = not yet\n * computed; `null` = failed parse. A fresh parse that's never edited nor\n * tree-read pays no conversion \u2014 important for the full-reparse fallback path.\n */\n private _abs: N | null | undefined\n private _rel: N | null | undefined\n readonly errors: ParseError[]\n readonly unconsumedFrom: number | null\n readonly input: string\n\n constructor(\n registry: Registry<N>,\n rootRule: string,\n opts: ParseDocOptions<N>,\n trees: { abs?: N | null; rel?: N | null },\n errors: ParseError[],\n unconsumedFrom: number | null,\n input: string,\n ) {\n this._registry = registry\n this._fns = {}\n this._reps = new Set()\n for (const [name, entry] of Object.entries(registry)) {\n this._fns[name] = asRuleFn(entry)\n if (producesRepetition(defOf(entry))) this._reps.add(name)\n }\n this._rootRule = rootRule\n this._opts = opts\n this._abs = trees.abs\n this._rel = trees.rel\n this.errors = errors\n this.unconsumedFrom = unconsumedFrom\n this.input = input\n }\n\n /**\n * The parse tree with PARENT-RELATIVE spans \u2014 a node's `span` is relative to\n * its parent's start (root base 0). This is the shareable representation:\n * untouched subtrees keep the same identity across `.edit()`s, and reading the\n * tree after a length-changing edit is O(1) (no offset rewrite). For absolute\n * positions use the O(depth) cursor `spanAt(path)`, or `absolutizeCST(tree)` to\n * materialize the whole absolute tree. (A fresh non-incremental `node().parse()`\n * result is unchanged \u2014 still absolute.)\n */\n get tree(): N | null {\n if (this._rel === undefined) {\n this._rel = this._abs ? (relativizeCST(this._abs as unknown as N & { span: Span }, 0) as unknown as N) : null\n }\n return this._rel\n }\n\n spanAt(path: readonly number[]): { start: number; end: number } {\n const rel = this.tree\n if (!rel) throw new Error('spanAt on a failed parse (tree is null)')\n return absoluteSpanCST(rel as unknown as { span: Span; children?: readonly unknown[] }, path)\n }\n\n /**\n * Wrap a reused (grafted/spliced) RELATIVE tree, recomputing `errors` and\n * `unconsumedFrom` from it so both stay consistent with the tree across the\n * edit (the flat errors are recovered errors embedded in the reused subtrees;\n * unconsumedFrom re-derives trailing junk over the new input).\n */\n private wrapReuse(newTree: N, newInput: string): ParseDoc<N> {\n const trivia = triviaOf(this._registry[this._rootRule], this._opts)\n const end = (newTree as unknown as { span: { end: number } }).span.end\n const errors = collectEmbeddedErrors(newTree, 0, [])\n const unconsumedFrom = unconsumedAfter(end, newInput, trivia)\n return new ParseDocImpl(this._registry, this._rootRule, this._opts, { rel: newTree }, errors, unconsumedFrom, newInput)\n }\n\n edit(from: number, to: number, replacement: string): ParseDoc<N> {\n const newInput = this.input.slice(0, from) + replacement + this.input.slice(to)\n const reparse = () => parseDoc(this._registry, this._rootRule, newInput, this._opts)\n\n const root = this.tree\n if (!root) return reparse()\n\n const delta = replacement.length - (to - from)\n // When `from` sits on the root's own boundary (e.g. right after a top-level\n // element, before its separator), no child node contains it and\n // `findContaining` returns null \u2014 but the root still does. Fall back to the\n // root as the container so the structural splice below still gets a shot\n // (this is exactly the \"insert/delete a top-level list element\" position).\n const found = findContaining(root, 0, from) ?? { node: root, path: [] as number[] }\n\n // Try the innermost containing rule first, then widen outward.\n const ancestors = ancestorsAt(root, found.path)\n const candidates: FoundNode<N>[] = [found]\n const pathCopy = [...found.path]\n for (let i = ancestors.length - 2; i >= 0; i--) {\n pathCopy.pop()\n candidates.push({ node: ancestors[i + 1]!, path: [...pathCopy] })\n }\n\n const rebuild = this._opts.rebuild ?? defaultRebuild\n for (const { node, path } of candidates) {\n // Absolute span of this candidate, projected from the relative root (O(depth)).\n const { start: absStart, end: absEnd } = absoluteSpanCST(root as unknown as { span: Span; children?: readonly unknown[] }, path)\n // Reentry only pays off when the re-parsed rule is substantially smaller\n // than the whole document. Candidates widen deepest\u2192root (monotonically\n // growing span), so once one covers most of the input, reparsing it \u2014 and\n // every larger ancestor after it \u2014 can't beat a full reparse and would just\n // be wasted work before the fallback. Bail to the full reparse NOW. This\n // caps `.edit()` at ~one full reparse in the worst case (e.g. a structural\n // insert near the front) instead of stacking several near-full reparses.\n if (absEnd - absStart > this.input.length * REENTRY_MAX_SPAN_FRACTION) break\n const ruleFn = this._fns[node.type]\n if (!ruleFn) continue\n // The reused subtree must FULLY CONTAIN the edited range (old coords).\n // `findContaining` only locates `from`; if the edit's end `to` spills past\n // this node's end, the edit also changed a sibling/separator after it, and\n // reusing the untouched suffix would be unsound \u2014 widen to an ancestor\n // that does span the whole edit (ultimately a full reparse).\n if (!(absStart <= from && to <= absEnd)) continue\n const ctx: ParseContext = mkCtx(node.state, this._opts.build, !!this._opts.tolerant)\n const r = ruleFn(newInput, absStart, ctx)\n if (!r.ok) continue\n if (r.span.end !== absEnd + delta) continue\n\n // Stage-2 soundness guard: only reuse the untouched suffix if the re-parse\n // provably read no input past its own end (else a lookahead/backtrack\n // crossed the splice). Widen to the next candidate \u2014 ultimately a full\n // reparse \u2014 when it can't be proven.\n if (!boundaryIsSafe(ruleFn, newInput, absStart, absEnd + delta, node.state, this._opts.build, r, !!this._opts.tolerant)) {\n continue\n }\n\n // The re-parse produced an ABSOLUTE subtree; rebase it to parent-relative\n // for splicing into the relative tree. The parent's absolute start is\n // `absStart - node.span.start` (node.span is already relative to it).\n const parentBase = absStart - node.span.start\n const newRel = relativizeCST(r.value as unknown as N & { span: Span }, parentBase) as unknown as N\n\n // delta === 0: relative spans are unchanged, so the spine graft (sharing\n // every untouched sibling by reference) is already correct.\n if (delta === 0) {\n const newTree = replaceAtPath(rebuild, root, path, newRel)\n return this.wrapReuse(newTree, newInput)\n }\n // Length-changing edit: trailing siblings' relative starts slide by `delta`.\n // A custom `rebuild` (possibly a class instance) can't have its span slid\n // safely, so fall back to a full, correct reparse.\n if (this._opts.rebuild) return reparse()\n const newTree = graftRelative(root, path, newRel, delta)\n return this.wrapReuse(newTree, newInput)\n }\n\n // No localized rule reparse converged \u2014 the edit is structural. Before paying\n // a full reparse, try reusing the untouched tail of a containing collection\n // (add/remove an element in a list). We only ever splice a rule the GRAMMAR\n // proves is a genuine repetition (`this._reps`, from its combinator def) \u2014 a\n // fixed-arity same-typed sequence is structurally indistinguishable from a list\n // by its CST alone, so splicing it could accept a wrong element count; it's\n // excluded here and falls back to a full, correct reparse. Innermost containing\n // collection first; a candidate whose disturbed middle doesn't tile cleanly\n // returns null and we widen. Correctness net is the incremental oracle fuzz.\n if (this._opts.structuralReuse && delta !== 0 && !this._opts.rebuild) {\n // Candidates run innermost\u2192ancestor but never include the root itself; the\n // splice-able collection can BE the root (e.g. a top-level `sepBy` list), so\n // consider it last.\n const spliceCandidates: FoundNode<N>[] = [...candidates, { node: root, path: [] }]\n for (const { node, path } of spliceCandidates) {\n if (!this._reps.has(node.type)) continue // grammar didn't prove this rule a repetition\n const { start: cStart, end: cEnd } = absoluteSpanCST(root as unknown as { span: Span; children?: readonly unknown[] }, path)\n if (!(cStart <= from && to <= cEnd)) continue\n const spliced = tryListSplice(root, path, node, cStart, newInput, from, to, delta, this._fns, this._opts.build, !!this._opts.tolerant)\n if (spliced) return this.wrapReuse(spliced, newInput)\n }\n }\n\n return reparse()\n }\n}\n\n/**\n * The relative (parent-offset) tree backing a doc. Currently identical to the\n * public `doc.tree` (which is relative); kept as a named internal handle for the\n * reuse-metric tests, which assert on the shareable representation explicitly.\n */\nexport function relTreeOf<N extends NodeLike>(doc: ParseDoc<N>): N | null {\n return doc.tree\n}\n\n/**\n * The grammar trivia rule to skip when computing `unconsumedFrom`: an explicit\n * `trivia` option wins; otherwise the root entry's ambient `grammarTrivia` (the\n * same source `run()` derives it from). A bare-function registry carries no meta,\n * so trailing trivia can't be inferred and the parse must reach the exact end.\n */\nfunction triviaOf<N extends NodeLike>(\n entry: RuleFn<N> | Combinator<N> | undefined,\n opts: ParseDocOptions<N>,\n): Combinator<unknown> | undefined {\n if (opts.trivia) return opts.trivia\n // Only an interpreter Combinator carries `_meta`; a bare parse function or a\n // compiled-grammar object (which bakes its ambient trivia into codegen) does\n // not, so there's nothing to derive and trailing trivia isn't skipped.\n if (entry === undefined || typeof entry === 'function') return undefined\n const meta = (entry as { _meta?: { grammarTrivia?: Combinator<unknown> } })._meta\n return meta ? meta.grammarTrivia : undefined\n}\n\n/**\n * First unconsumed non-trivia offset after a parse that ended at `end` (trailing\n * trivia skipped when a trivia rule is available), or `null` when the whole input\n * was consumed \u2014 byte-for-byte the computation in `run()` (run.ts:117-129).\n */\nfunction unconsumedAfter(end: number, input: string, trivia: Combinator<unknown> | undefined): number | null {\n let pos = end\n if (trivia && pos < input.length) {\n const t = trivia.parse(input, pos, { trackLines: false })\n if (t.ok && t.span.end > pos) pos = t.span.end\n }\n return pos < input.length ? pos : null\n}\n\n/**\n * Collect embedded `parseError` recovery nodes from a PARENT-RELATIVE tree,\n * projecting each to an ABSOLUTE span (root base 0) \u2014 the flat mirror of the\n * errors that ride the tree, recomputed for a reuse-path (grafted/spliced) result\n * so `doc.errors` stays consistent with the tree across incremental edits.\n */\nfunction collectEmbeddedErrors(node: unknown, base: number, out: ParseError[]): ParseError[] {\n const c = node as { _tag?: string; span?: { start: number; end: number }; expected?: string[]; children?: readonly unknown[] }\n if (!c || typeof c !== 'object' || !c.span) return out\n const start = base + c.span.start\n if (c._tag === 'parseError') {\n out.push({ _tag: 'parseError', span: { start, end: base + c.span.end }, expected: c.expected ?? [] })\n return out\n }\n if (Array.isArray(c.children)) for (const k of c.children) collectEmbeddedErrors(k, start, out)\n return out\n}\n\n/**\n * Parse `input` from `rootRule` and wrap the result in a ParseDoc that can\n * be incrementally re-parsed via `.edit()`.\n */\nexport function parseDoc<N extends NodeLike>(\n registry: Registry<N>,\n rootRule: string,\n input: string,\n opts: ParseDocOptions<N> = {},\n): ParseDoc<N> {\n const entry = registry[rootRule]\n if (!entry) throw new Error(`No rule '${rootRule}' in registry`)\n const ctx: ParseContext = mkCtx(opts.state, opts.build, !!opts.tolerant)\n const r: ParseResult<N> = asRuleFn(entry)(input, 0, ctx)\n if (r.ok) {\n // A fresh parse is ABSOLUTE; the relative form is materialized lazily on the\n // first edit/spanAt, so a parse that's never edited pays no conversion. The\n // recovery errors the tolerant parse collected into ctx._errors are surfaced\n // flat too (like run()), and unconsumedFrom reports any trailing junk.\n const errors = ctx._errors ? [...ctx._errors] : []\n const unconsumedFrom = unconsumedAfter(r.span.end, input, triviaOf(entry, opts))\n return new ParseDocImpl(registry, rootRule, opts, { abs: r.value }, errors, unconsumedFrom, input)\n }\n // Hard (non-recovered) failure: the single top-level failure, as a parseError.\n const fail: ParseError = { _tag: 'parseError', span: r.span, expected: r.expected }\n return new ParseDocImpl(registry, rootRule, opts, { abs: null }, [fail], null, input)\n}\n"],
4
+ "sourcesContent": ["/**\n * `parseman/language-service` \u2014 the external, grammar-agnostic IDE layer.\n *\n * A grammar is pure structure (it carries no recovery/completion/lint config).\n * `languageService(grammar, config)` layers editor behaviour ON TOP, keyed by rule\n * name = node type, without ever modifying the grammar:\n *\n * - `parse(src, { tolerant })` \u2192 CST + `ParseError[]` (recovers on the compiled\n * fast path when `grammar` is a `compile(g, { recovery: true })` grammar).\n * - `diagnostics(src)` \u2192 structural parse errors + your per-node-type lint rules.\n * - `completionsAt(src, offset)` \u2192 the grammar's expected-token set at the cursor,\n * mapped through your per-rule semantic completion handlers.\n *\n * The grammar can be the interpreter combinator OR a compiled grammar \u2014 the domain\n * knowledge (what to suggest after `color:`, which nodes to lint) lives here, in the\n * consumer's config, never in the grammar.\n */\nimport type { Combinator, ParseContext, ParseResult, Span, ParseError } from '../types.ts'\nimport { run } from '../functional/run.ts'\nimport { completionsAt as coreCompletionsAt } from '../combinators/completions.ts'\nimport { cstBuildHost } from '../compiler/linker.ts'\nimport { walk, type Walkable } from '../cst/walk.ts'\nimport { parseDoc, type Registry } from '../functional/doc.ts'\nimport { absolutizeCST } from '../cst/relative-spans.ts'\nimport type { NodeLike } from '../cst/types.ts'\n\nexport type Severity = 'error' | 'warning' | 'info'\nexport type Diagnostic = { severity: Severity; message: string; span: Span }\nexport type CompletionItem = { label: string; detail?: string }\n\n/** A CST node/leaf as a diagnostics handler sees it: the walk shape plus span/value. */\nexport type LsNode = Walkable & { readonly span: Span; readonly value?: string }\n\n/** Context handed to a completion handler: which rule the cursor sits in and the\n * grammar's raw expected-token labels there. */\nexport type CompletionContext = { rule: string | null; expected: string[]; offset: number }\n\nexport type LanguageServiceConfig = {\n /** Lint/diagnostic rules keyed by node type (rule name). Return zero or more\n * Diagnostics for each matching node. */\n diagnostics?: Record<string, (node: LsNode) => Diagnostic | Diagnostic[] | null | undefined | void>\n /** Semantic completion handlers keyed by the rule the cursor is in. Turn the\n * grammar's structural expectation into domain suggestions. */\n complete?: Record<string, (ctx: CompletionContext) => CompletionItem[]>\n}\n\n/** Grammar the service drives: an interpreter combinator or a compiled entry fn. */\ntype Grammar = Combinator<unknown> | ((input: string, pos: number, ctx: ParseContext) => ParseResult<unknown>)\n\n/**\n * What you hand `languageService`:\n * - a single entry (combinator or `compile()`d fn) \u2014 stateless methods only, or\n * - `{ rules, root }` (a `rules()` registry + its root rule name) \u2014 also unlocks\n * `openDocument`, the live incremental editor document.\n */\nexport type GrammarInput = Grammar | { rules: Registry<NodeLike>; root: string }\n\n/**\n * A live incremental document \u2014 the fused editor backend. Holds a tolerant\n * `parseDoc` whose tree survives broken input (recovery) and re-parses only the\n * edited span on `edit()`. Diagnostics walk the maintained tree (recovered errors\n * ride inside it as `parseError` nodes), so they stay complete across edits without\n * re-parsing the whole document.\n */\nexport type LsDocument = {\n /** The current CST with ABSOLUTE spans (recovered errors embedded as parseError nodes). */\n readonly tree: LsNode | null\n /** Apply an editor change (byte offsets in the CURRENT text); returns the next document. */\n edit(from: number, to: number, replacement: string): LsDocument\n /** Structural parse errors (from the tree) + your per-node-type lint rules. */\n diagnostics(): Diagnostic[]\n /** Completions at a cursor offset in the current text. */\n completionsAt(offset: number): CompletionItem[]\n}\n\nexport type LanguageService = {\n parse(src: string, opts?: { tolerant?: boolean }): ParseResult<unknown> & { errors: ParseError[] }\n diagnostics(src: string): Diagnostic[]\n completionsAt(src: string, offset: number): CompletionItem[]\n /** Open a live incremental document. Requires `languageService({ rules, root }, \u2026)`. */\n openDocument(src: string): LsDocument\n}\n\nconst errorMessage = (expected: readonly string[]): string =>\n expected.length ? `Unexpected input; expected ${expected.join(' or ')}` : 'Unexpected input'\n\n/**\n * Merge every syntax-error source into one deduped diagnostic list:\n * (a) recovered `parseError` nodes embedded in the tree + per-node lint rules\n * (via {@link diagnoseTree}),\n * (b) residual FLAT recovery/failure errors from the run/doc (present when the\n * parse produced no tree, or as the flat mirror of embedded errors), and\n * (c) a sentinel over any trailing `unconsumedFrom..end` junk the grammar left.\n * Deduped by span+message so an embedded error and its flat twin collapse to one,\n * while distinct diagnostics at the same offset survive. With the reporting fixes\n * (expect() embeds, speculative rollback is clean) (a) is already complete; the\n * merge is what guarantees missing-closers and trailing junk are never invisible.\n */\nfunction mergeDiagnostics(\n tree: LsNode | null,\n flat: ReadonlyArray<{ expected: readonly string[]; span: Span }>,\n unconsumedFrom: number | null,\n inputLength: number,\n config: LanguageServiceConfig,\n): Diagnostic[] {\n const out: Diagnostic[] = []\n const seen = new Set<string>()\n const add = (d: Diagnostic): void => {\n const key = `${d.severity}|${d.span.start}|${d.span.end}|${d.message}`\n if (seen.has(key)) return\n seen.add(key)\n out.push(d)\n }\n if (tree) for (const d of diagnoseTree(tree, config)) add(d)\n for (const e of flat) add({ severity: 'error', message: errorMessage(e.expected), span: e.span })\n if (unconsumedFrom !== null) {\n add({ severity: 'error', message: 'Unexpected input', span: { start: unconsumedFrom, end: inputLength } })\n }\n return out\n}\n\n/** One walk over a CST: recovered `parseError` nodes \u2192 error diagnostics, and each\n * node dispatched to its `config.diagnostics[type]` lint handler. Errors live in the\n * tree, so this is the single source for both \u2014 no separate flat channel to merge. */\nfunction diagnoseTree(root: LsNode, config: LanguageServiceConfig): Diagnostic[] {\n const out: Diagnostic[] = []\n const rules = config.diagnostics\n walk(root, {\n enter(node) {\n if (node._tag === 'parseError') {\n out.push({ severity: 'error', message: errorMessage((node as { expected?: string[] }).expected ?? []), span: node.span })\n return\n }\n const h = rules && node.type !== undefined ? rules[node.type] : undefined\n if (h) {\n const d = h(node)\n if (d) out.push(...(Array.isArray(d) ? d : [d]))\n }\n },\n })\n return out\n}\n\nexport function languageService(grammar: GrammarInput, config: LanguageServiceConfig = {}): LanguageService {\n const build = cstBuildHost()\n const asReg = typeof grammar === 'object' && grammar !== null && 'rules' in grammar\n const registry = asReg ? grammar.rules : undefined\n const root = asReg ? grammar.root : undefined\n const entry: Grammar = asReg ? (registry![root!] as Grammar) : grammar\n\n // The grammar's ambient trivia (interpreter combinators carry it in _meta) \u2014\n // handed to run() so it skips a trailing whitespace/comment tail before\n // reporting unconsumedFrom, matching parseDoc/run semantics.\n const trivia = typeof entry !== 'function' ? (entry as Combinator<unknown>)._meta.grammarTrivia : undefined\n const parseTolerant = (src: string) =>\n run(entry as never, src, { tolerant: true, build, ...(trivia !== undefined ? { trivia } : {}) })\n\n const complete = (input: string, offset: number, treeForCursor: unknown): CompletionItem[] => {\n const expected = coreCompletionsAt(entry as never, input, offset, { tolerant: true })\n const rule = ruleAtCursor(treeForCursor, offset)\n const handler = rule !== null ? config.complete?.[rule] : undefined\n if (handler) return handler({ rule, expected, offset })\n return expected.map(label => ({ label }))\n }\n\n return {\n parse(src, opts) {\n return run(entry as never, src, { tolerant: opts?.tolerant ?? true, build }) as ParseResult<unknown> & { errors: ParseError[] }\n },\n\n diagnostics(src) {\n const r = parseTolerant(src)\n const tree = r.value && typeof r.value === 'object' ? (r.value as LsNode) : null\n return mergeDiagnostics(tree, r.errors, r.unconsumedFrom, src.length, config)\n },\n\n completionsAt(src, offset) {\n return complete(src, offset, parseTolerant(src.slice(0, offset)).value)\n },\n\n openDocument(src) {\n if (!registry || root === undefined) {\n throw new Error('openDocument requires languageService({ rules, root }, config) \u2014 a rules() registry, not a single entry')\n }\n const host = cstBuildHost()\n const make = (doc: ReturnType<typeof parseDoc<NodeLike>>): LsDocument => {\n // Absolutize once per document version (memoized in this closure).\n const abs = doc.tree ? (absolutizeCST(doc.tree as never) as unknown as LsNode) : null\n return {\n get tree() { return abs },\n edit: (from, to, replacement) => make(doc.edit(from, to, replacement)),\n // Same unified merge as the stateless path: embedded parseError nodes +\n // lint, the flat recovery errors the doc collected, and a trailing-junk\n // sentinel \u2014 so a live editor document surfaces missing-closers and junk.\n diagnostics: () => mergeDiagnostics(abs, doc.errors, doc.unconsumedFrom, doc.input.length, config),\n completionsAt: (offset) => complete(doc.input, offset, abs),\n }\n }\n return make(parseDoc<NodeLike>(registry, root, src, { tolerant: true, structuralReuse: true, build: host }))\n },\n }\n}\n\n/** Deepest node whose span brackets the cursor \u2014 \"which rule is the cursor in\".\n * The grammar carries no cursor knowledge, so we reconstruct it from the CST. */\nfunction ruleAtCursor(root: unknown, offset: number): string | null {\n if (!root || typeof root !== 'object') return null\n let best: string | null = null\n walk(root as LsNode, {\n enter(node) {\n const s = node.span\n if (s && s.start <= offset && offset <= s.end && node.type !== undefined) best = node.type\n },\n })\n return best\n}\n", "import type { ParseContext } from '../types.ts'\n\n/** Lazy per-node capture state \u2014 arrays materialized on first push. */\nexport type CstCaptureBuf = {\n single?: unknown | undefined\n ch?: unknown[] | undefined\n rawSingle?: unknown | undefined\n raw?: unknown[] | undefined\n tl?: number[] | undefined\n}\n\nconst EMPTY: unknown[] = []\nconst EMPTY_TL: number[] = []\n\nexport function cstCaptureActive(ctx: ParseContext): boolean {\n return ctx._cstBuf !== undefined || ctx._cstLeaves !== undefined\n}\n\nexport type CstNodeCaptureSaved = {\n ch: ParseContext['_cstChildren']\n lv: ParseContext['_cstLeaves']\n raw: ParseContext['_cstRawChildren']\n tl: ParseContext['_cstTriviaLog']\n cap: boolean | undefined\n buf: CstCaptureBuf | undefined\n}\n\nexport function beginCstNodeCapture(ctx: ParseContext): CstNodeCaptureSaved {\n const saved: CstNodeCaptureSaved = {\n ch: ctx._cstChildren,\n lv: ctx._cstLeaves,\n raw: ctx._cstRawChildren,\n tl: ctx._cstTriviaLog,\n cap: ctx.captureTrivia,\n buf: ctx._cstBuf,\n }\n ctx._cstBuf = {}\n ctx._cstChildren = undefined\n ctx._cstLeaves = undefined\n ctx._cstRawChildren = undefined\n ctx._cstTriviaLog = undefined\n ctx.captureTrivia = true\n return saved\n}\n\nexport function finishCstBuf(buf: CstCaptureBuf | undefined): {\n children: unknown[]\n rawChildren: unknown[]\n triviaLog: number[]\n} {\n if (!buf) return { children: EMPTY, rawChildren: EMPTY, triviaLog: EMPTY_TL }\n const children = buf.ch ?? (buf.single !== undefined ? [buf.single] : EMPTY)\n const rawChildren = buf.raw ?? (buf.rawSingle !== undefined ? [buf.rawSingle] : EMPTY)\n const triviaLog = buf.tl ?? EMPTY_TL\n return { children, rawChildren, triviaLog }\n}\n\nexport function endCstNodeCapture(\n ctx: ParseContext,\n saved: CstNodeCaptureSaved,\n): { children: unknown[]; rawChildren: unknown[]; triviaLog: number[] } {\n const materialized = finishCstBuf(ctx._cstBuf)\n ctx._cstBuf = saved.buf\n ctx._cstChildren = saved.ch\n ctx._cstLeaves = saved.lv\n ctx._cstRawChildren = saved.raw\n ctx._cstTriviaLog = saved.tl\n ctx.captureTrivia = saved.cap\n return materialized\n}\n\nexport function pushCstLeaf(ctx: ParseContext, leaf: unknown): void {\n pushCstChild(ctx, leaf, leaf)\n}\n\n/** Record a built sub-node into the active collector (children vs rawChildren may differ). */\nexport function pushCstChild(ctx: ParseContext, built: unknown, rawEntry: unknown): void {\n const b = ctx._cstBuf\n if (b) {\n if (b.ch) b.ch.push(built)\n else if (b.single !== undefined) { b.ch = [b.single, built]; b.single = undefined }\n else b.single = built\n\n if (b.raw) b.raw.push(rawEntry)\n else if (b.rawSingle !== undefined) { b.raw = [b.rawSingle, rawEntry]; b.rawSingle = undefined }\n else b.rawSingle = rawEntry\n return\n }\n if (ctx._cstChildren) ctx._cstChildren.push(built)\n else if (ctx._cstLeaves) ctx._cstLeaves.push(built)\n if (ctx._cstRawChildren) ctx._cstRawChildren.push(rawEntry)\n}\n\nexport function cstRawLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) {\n if (b.raw) return b.raw.length\n return b.rawSingle !== undefined ? 1 : 0\n }\n return ctx._cstRawChildren?.length ?? 0\n}\n\nexport function cstLeavesLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) {\n if (b.ch) return b.ch.length\n return b.single !== undefined ? 1 : 0\n }\n return ctx._cstLeaves?.length ?? 0\n}\n\nexport function cstTlLen(ctx: ParseContext): number {\n const b = ctx._cstBuf\n if (b) return b.tl?.length ?? 0\n return ctx._cstTriviaLog?.length ?? 0\n}\n\nexport type CstRollbackMark = { raw: number; tlog: number; leaves: number; fields: number; errors: number }\n\nexport function saveCstMark(ctx: ParseContext): CstRollbackMark {\n return {\n raw: cstRawLen(ctx),\n tlog: cstTlLen(ctx),\n leaves: cstLeavesLen(ctx),\n fields: ctx._fields?.length ?? 0,\n // Speculative rollback must truncate the flat recovery-error sink in lockstep\n // with the CST leaves: a branch that recovered (pushing a ParseError to\n // ctx._errors AND embedding it as a CST child via captureError) then failed\n // must remove BOTH, or the flat error survives as a GHOST that a tree walk\n // can't see (flat run().errors would disagree with the embedded set).\n errors: ctx._errors?.length ?? 0,\n }\n}\n\nfunction rollbackBufList(\n b: CstCaptureBuf,\n keyMulti: 'ch' | 'raw',\n keySingle: 'single' | 'rawSingle',\n len: number,\n): void {\n const arr = b[keyMulti]\n if (arr) {\n if (len === 0) b[keyMulti] = undefined\n else if (len === 1) { b[keySingle] = arr[0]; b[keyMulti] = undefined }\n // Guarded like every other truncation \u2014 see rollbackCstCapture. This is the\n // buffered node-capture path, so it is reached only by the INTERPRETER (the\n // compiled engine inlines its own capture and never calls here), and it is\n // where the redundant-store rate is highest: over a backtracking fixture\n // this branch ran 32,800 times and 31,198 of those \u2014 95% \u2014 restored a\n // length that had not moved.\n else if (arr.length !== len) arr.length = len\n return\n }\n if (len === 0) b[keySingle] = undefined\n}\n\nexport function rollbackCstCapture(ctx: ParseContext, mark: CstRollbackMark): void {\n // Truncate the flat recovery-error sink alongside the CST, so a rolled-back\n // speculative branch leaves no ghost error (see saveCstMark). Guarded on a\n // present sink + a defined mark (rollbackTrivia forwards the same field).\n //\n // Each truncation is guarded on `length !== mark`, mirroring the compiled\n // engine (codegen.ts `captureRestoreBody`). Assigning `array.length` runs V8's\n // length setter \u2014 a backing-store trim decision \u2014 and costs the same when the\n // value is unchanged, which is the overwhelmingly common case here: a\n // speculative branch that captured nothing still pays for every store. The\n // compare is an in-object load. Both engines must carry the guard or the\n // interpreted path silently keeps the cost the compiled path shed.\n if (ctx._errors && mark.errors !== undefined && ctx._errors.length !== mark.errors) ctx._errors.length = mark.errors\n const b = ctx._cstBuf\n if (b) {\n rollbackBufList(b, 'raw', 'rawSingle', mark.raw)\n rollbackBufList(b, 'ch', 'single', mark.leaves)\n if (b.tl) {\n if (mark.tlog === 0) b.tl = undefined\n else if (b.tl.length !== mark.tlog) b.tl.length = mark.tlog\n }\n if (ctx._fields && ctx._fields.length !== mark.fields) ctx._fields.length = mark.fields\n return\n }\n if (ctx._cstRawChildren && ctx._cstRawChildren.length !== mark.raw) ctx._cstRawChildren.length = mark.raw\n if (ctx._cstTriviaLog && ctx._cstTriviaLog.length !== mark.tlog) ctx._cstTriviaLog.length = mark.tlog\n if (ctx._cstLeaves && ctx._cstLeaves.length !== mark.leaves) ctx._cstLeaves.length = mark.leaves\n if (ctx._fields && ctx._fields.length !== mark.fields) ctx._fields.length = mark.fields\n}\n\nexport function pushCstTriviaEntry(\n ctx: ParseContext,\n start: number,\n end: number,\n kindIndex?: number,\n): void {\n const insertIdx = cstRawLen(ctx)\n const b = ctx._cstBuf\n const withKind = ctx.triviaKindLabels !== undefined && kindIndex !== undefined\n if (b) {\n if (!b.tl) {\n b.tl = withKind ? [start, end, insertIdx, kindIndex] : [start, end, insertIdx]\n } else if (withKind) {\n b.tl.push(start, end, insertIdx, kindIndex)\n } else {\n b.tl.push(start, end, insertIdx)\n }\n return\n }\n if (ctx._cstTriviaLog) {\n if (withKind) ctx._cstTriviaLog.push(start, end, insertIdx, kindIndex)\n else ctx._cstTriviaLog.push(start, end, insertIdx)\n }\n}\n\nexport function pushTriviaLogEntry(\n ctx: ParseContext,\n start: number,\n end: number,\n kindIndex?: number,\n): void {\n if (!ctx._triviaLog) return\n if (ctx.triviaKindLabels !== undefined && kindIndex !== undefined) {\n ctx._triviaLog.push(start, end, kindIndex)\n } else {\n ctx._triviaLog.push(start, end)\n }\n}\n\nexport function cstDeferredTriviaActive(ctx: ParseContext): boolean {\n return ctx._triviaLog !== undefined || ctx._cstBuf !== undefined || ctx._cstTriviaLog !== undefined\n}\n", "import type { Combinator, FirstSet, ParseContext, ParseError, ParseResult, RecoveryHelpers } from '../types.ts'\nimport { cstCaptureActive, pushCstChild } from '../cst/capture-buffer.ts'\n\n/**\n * Recovery MECHANISM \u2014 sync-source-agnostic. These primitives implement \"scan\n * forward to a sync token and emit a ParseError\" and \"is the sync token here?\".\n * They are deliberately independent of WHERE the sync sentinel comes from: the\n * grammar never carries recovery config. Sync points are inferred automatically\n * from grammar structure (see `infer.ts`) and may be overridden externally by the\n * language service via `ctx._listSync`. This is the salvaged core of the old\n * `combinators/recover-scan.ts`.\n */\n\n/**\n * Run `sentinel` at `pos` as PURE lookahead: succeed/fail only, with every\n * side-effecting sink suppressed (CST leaves/children/trivia, the trivia log,\n * the recovery error sink, and the completions probe). A recovery scan probes the\n * sentinel at many positions and must not capture leaves, log trivia, or pollute\n * `completionsAt`'s furthest-failure set as a byproduct of looking ahead.\n */\nfunction probeAt(sentinel: Combinator<unknown>, input: string, pos: number, ctx: ParseContext): boolean {\n const s = {\n _probe: ctx._probe, _errors: ctx._errors,\n _cstLeaves: ctx._cstLeaves, _cstChildren: ctx._cstChildren, _cstRawChildren: ctx._cstRawChildren,\n _cstTriviaLog: ctx._cstTriviaLog, _triviaLog: ctx._triviaLog, _fields: ctx._fields, _cstBuf: ctx._cstBuf,\n _tolerant: ctx._tolerant, _sync: ctx._sync,\n }\n ctx._probe = undefined; ctx._errors = undefined\n ctx._cstLeaves = undefined; ctx._cstChildren = undefined; ctx._cstRawChildren = undefined\n ctx._cstTriviaLog = undefined; ctx._triviaLog = undefined; ctx._fields = undefined; ctx._cstBuf = undefined\n // A sentinel is pure lookahead: it must not itself enter tolerant recovery (nor\n // read a stale outer `_sync`). Otherwise a sentinel that composes many/sepBy would\n // run recovery on every probed position \u2014 wasted work, and it could resync to the\n // enclosing list's sync token. Cleared here and restored in `finally`.\n ctx._tolerant = undefined; ctx._sync = undefined\n try {\n return sentinel.parse(input, pos, ctx).ok\n } finally {\n ctx._probe = s._probe; ctx._errors = s._errors\n ctx._cstLeaves = s._cstLeaves; ctx._cstChildren = s._cstChildren; ctx._cstRawChildren = s._cstRawChildren\n ctx._cstTriviaLog = s._cstTriviaLog; ctx._triviaLog = s._triviaLog; ctx._fields = s._fields; ctx._cstBuf = s._cstBuf\n ctx._tolerant = s._tolerant; ctx._sync = s._sync\n }\n}\n\n/**\n * Combine two recovery sentinels into one that matches when either does \u2014 used by\n * tolerant `sepBy` to resync to its own separator OR the enclosing delimiter. Cold\n * path only; a bare wrapper so we don't pull `choice()` (and its analysis) in.\n */\nexport function orSentinel(\n a: Combinator<unknown>,\n b: Combinator<unknown> | undefined,\n): Combinator<unknown> {\n if (b === undefined) return a\n return {\n _tag: 'orSentinel',\n _meta: { firstSet: { kind: 'any' }, canMatchNewline: false, isTrivia: false },\n _def: { tag: 'unknown' },\n parse(input: string, pos: number, ctx: ParseContext): ParseResult<unknown> {\n const ra = a.parse(input, pos, ctx)\n if (ra.ok) return ra\n return b.parse(input, pos, ctx)\n },\n }\n}\n\n/**\n * A lightweight zero-width sentinel that \"matches\" (succeeds, consuming nothing)\n * when the input code point at `pos` starts the given first set. Sync inference\n * builds one from a list's follow set so a nested list can resync to the enclosing\n * delimiter with no grammar annotation. Returns `null` when the first set is `any`\n * or `empty` (no usable inference).\n */\nexport function firstSetSentinel(fs: FirstSet): Combinator<null> | null {\n if (fs.kind !== 'ranges' || fs.ranges.length === 0) return null\n const ranges = fs.ranges\n return {\n _tag: 'firstSetSentinel',\n _meta: { firstSet: fs, canMatchNewline: false, isTrivia: false },\n _def: { tag: 'unknown' },\n parse(input: string, pos: number): ParseResult<null> {\n const code = pos < input.length ? input.codePointAt(pos) : undefined\n if (code !== undefined) {\n for (const r of ranges) {\n if (code >= r.lo && code <= r.hi) return { ok: true, value: null, span: { start: pos, end: pos } }\n }\n }\n return { ok: false, expected: [], span: { start: pos, end: pos } }\n },\n }\n}\n\n/**\n * Cold-path recovery scan shared by tolerant `many()` / `sepBy()`. Scans forward\n * from `from` until `sync` matches (or EOF) \u2014 probing the sentinel as pure\n * lookahead \u2014 emits a {@link ParseError} spanning the skipped range, and pushes it\n * to `ctx._errors`. The sync token is NOT consumed; the caller's loop resumes from\n * `end`. Only ever reached after an element has failed, so entirely off the hot path.\n *\n * The caller owns the loop guard: `sepBy` progress is driven by its separator (a\n * zero-width error for a missing element is fine); `many` checks `matchesAt(sync)`\n * itself and breaks when sitting on the terminator, so a zero-width failure can\n * never spin.\n */\nexport function recoverScan(\n input: string,\n from: number,\n ctx: ParseContext,\n sync: Combinator<unknown>,\n expected: string[],\n): { error: ParseError; end: number } {\n let scanPos = from\n while (scanPos < input.length && !probeAt(sync, input, scanPos, ctx)) scanPos++\n const error: ParseError = { _tag: 'parseError', span: { start: from, end: scanPos }, expected }\n ctx._errors?.push(error)\n return { error, end: scanPos }\n}\n\n/**\n * Does `sentinel` match at `pos` (pure lookahead)? Used by the tolerant list loops\n * to detect \"we are sitting on the sync/terminator token\" without consuming it or\n * capturing anything.\n */\nexport function matchesAt(\n sentinel: Combinator<unknown>,\n input: string,\n pos: number,\n ctx: ParseContext,\n): boolean {\n return probeAt(sentinel, input, pos, ctx)\n}\n\n/**\n * Embed a recovered error as a `parseError` CST child at the recovery point, so the\n * error lives IN the tree (rides reused subtrees across incremental edits; a tree\n * walk finds every diagnostic) \u2014 not only in the flat `ctx._errors` side-channel.\n * No-op when CST capture is off (value/AST mode). Both the interpreter list loops\n * and the compiled recovery branches call this (the latter via `_ctx._rec.capture`),\n * so the embedded node is identical on both paths.\n */\nexport function captureError(ctx: ParseContext, error: ParseError): void {\n if (cstCaptureActive(ctx)) pushCstChild(ctx, error, error)\n}\n\n/**\n * The recovery-helper bundle handed to a tolerant parse via `ctx._rec`. A COMPILED\n * grammar can't import these, so the driver (`run`, `parseDoc`) injects them; the\n * interpreter list loops read the same bundle, guaranteeing byte-identical recovery\n * across paths. Shared here so every driver installs the exact same instance.\n */\nexport const REC: RecoveryHelpers = { scan: recoverScan, at: matchesAt, or: orSentinel, sentinel: firstSetSentinel, capture: captureError }\n", "import type { Combinator, ParseContext, ParseError, ParseResult } from '../types.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/**\n * Run a compiled/interpreted grammar entry against an input and collect the raw\n * outcome a tool needs to shape a parse result \u2014 WITHOUT the consumer having to\n * hand-build a `ctx`, branch on function-vs-combinator, or scan for leftover\n * input itself.\n *\n * A `compile()`/macro grammar hands you a map of bare parse FUNCTIONS; the\n * interpreter hands you COMBINATORS. `run()` invokes either, threads the standard\n * framework ctx (trivia log, recover/expect error sink, the `ctx.build` host,\n * grammar state), and \u2014 given the grammar's trivia parser \u2014 reports where\n * non-trivia input was left unconsumed. The consumer keeps only its own policy:\n * how to shape the tree and how to turn diagnostics into its error type.\n */\n\n/** A compiled rule (macro/`compile()` output) OR an interpreter combinator. */\nexport type Runnable =\n | ((input: string, pos: number, ctx: ParseContext) => ParseResult<unknown>)\n | Combinator<unknown>\n\nexport type RunOptions = {\n /** Optional hooks for a coverage-enabled compiled or macro parser. Ordinary\n * parses omit this completely, so instrumentation has no normal-path cost. */\n instrumentation?: {\n _grammarCoverage?: (id: string) => void\n _grammarTrace?: { write(event: { id: string; phase: 'enter' | 'attempt' | 'selected' | 'success' | 'failure' | 'backtrack' | 'rollback'; offset: number; end?: number }): void }\n }\n /** `ctx.build` host \u2014 makes structural `node()` rules build a CST / AST via the\n * host instead of their own eval builders. Omit for a grammar's own builders. */\n build?: ParseContext['build']\n /** Initial grammar state threaded into `ctx.state`. */\n state?: unknown\n /**\n * The grammar's trivia rule. A root rule consumes trivia BETWEEN terms but not\n * after the last one, so trailing whitespace/comments would otherwise look like\n * unparsed input. Given the trivia rule, `run` skips that tail before computing\n * `unconsumedFrom` \u2014 so only real leftover is reported. Also encodes dialect\n * differences for free: pass the CSS trivia and a trailing `//` counts as\n * leftover; pass the Less trivia (which treats `//` as a line comment) and it\n * doesn't. An UNTERMINATED comment (which the trivia rule won't match) surfaces\n * at its start. Omit to require the parse to reach the exact end itself.\n */\n trivia?: Runnable\n /**\n * Restrict PER-NODE CST trivia capture (the `triviaLog` a node's builder sees)\n * to these trivia kinds \u2014 a bitmask over the grammar's `triviaKindLabels`\n * indices (build it with `triviaKindMask(labels, ['comment', \u2026])`). Unlisted\n * kinds (e.g. whitespace) are skipped over but not recorded per node, so a host\n * that only reads comments doesn't pay to log every whitespace run. The global\n * `triviaLog` returned by `run()` is unaffected. Omit to capture every kind.\n */\n triviaCaptureMask?: number\n /**\n * Activate automatic list recovery. When true, `many`/`sepBy`/`oneOrMore` recover\n * from a failed element \u2014 skip to a sync point (a resume token inferred from the\n * grammar's structure; the grammar carries no recovery config), emit a\n * `ParseError` over the skipped span (collected in `errors`), and keep parsing the\n * rest of the list \u2014 instead of stopping at the first bad element. Omit (the\n * default) for the strict \"one clean error and stop\" behavior, byte-identical to a\n * run with no recovery. Recovery is a cold path: on well-formed input nothing\n * fails, so none of the machinery runs.\n */\n tolerant?: boolean\n /**\n * Run three compiled-parser-only profiling passes: recognizer (no global\n * sinks), structural capture without the host, then the normal host path. This\n * is a measurement boundary, not a parser mode; ordinary `run()` output is\n * unchanged when omitted. The input is parsed THREE times \u2014 each pass gets its\n * own shallow copy of `options.state`, so keep per-parse state shallow (only\n * the top level is isolated between passes).\n */\n profile?: boolean\n}\n\nexport type RunProfilePass = {\n ms: number\n nodes: number\n childSlots: number\n rawSlots: number\n triviaSlots: number\n fieldSlots: number\n hostCalls: number\n}\n\nexport type RunProfile = {\n /** Existing `voidOf(transform(..., () => undefined))` semantics, generalized\n * to compiled structural nodes: no `ch`/`raw`/`tl` capture or raw entries. */\n recognizer: RunProfilePass\n /** Captures children/raw/trivia/fields but suppresses node construction. */\n structuralCapture: RunProfilePass\n /** The ordinary parser path, including its injected build host. */\n hostConstruction: RunProfilePass\n}\n\nexport type RunResult = {\n ok: boolean\n /** The entry's value on success; undefined on failure. */\n value: unknown\n span: { start: number; end: number }\n /** Expected-token set when the TOP-LEVEL parse failed (empty on success). */\n expected: string[]\n /** Recovery diagnostics (tolerant lists / `expect()`) collected during the parse (in order). */\n errors: ParseError[]\n /**\n * Flat trivia log for building a trivia map. Entry width depends on whether the\n * grammar uses labeled trivia: 2 numbers per entry (`start, end`) for plain\n * trivia, 3 numbers per entry (`start, end, kindIndex`) for labeled trivia\n * (grammars with `label()` arms in their trivia choice). Use `buildTriviaIndex`\n * / `triviaEntries` to consume it format-agnostically rather than iterating raw.\n */\n triviaLog: number[]\n /** Offset where unparsed input begins \u2014 the first non-trivia character the parse\n * left unconsumed (trailing trivia skipped when `trivia` is given), or null if\n * the whole input was consumed. This is how you detect \"the grammar stopped short,\n * there's junk here\". Only meaningful on success \u2014 a failed parse reports its own\n * `span`/`expected`. */\n unconsumedFrom: number | null\n /** Present only when `RunOptions.profile` is true. */\n profile?: RunProfile\n}\n\nconst invoke = (r: Runnable, input: string, pos: number, ctx: ParseContext): ParseResult<unknown> =>\n typeof r === 'function' ? r(input, pos, ctx) : r.parse(input, pos, ctx)\n\ntype ProfilePhase = NonNullable<ParseContext['_pmProfile']>['phase']\ntype ProfileState = NonNullable<ParseContext['_pmProfile']>\n\nfunction runOnce(entry: Runnable, input: string, options: RunOptions, phase?: ProfilePhase, profileState?: ProfileState): RunResult {\n if (typeof entry !== 'function' && typeof (entry as Combinator<unknown> | undefined)?.parse !== 'function') {\n throw new TypeError(\n `run(): start production is ${entry === null ? 'null' : typeof entry}, not a rule \u2014 the requested grammar rule does not exist (check the rule name).`,\n )\n }\n const triviaLog: number[] = []\n const errors: ParseError[] = []\n const profile = profileState ?? (phase === undefined\n ? undefined\n : { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 })\n // NOT \"no output\" \u2014 the `capture` phase still allocates per-node children/raw/\n // trivia buffers and records slot counts. This flag only omits the two GLOBAL\n // sinks (`_triviaLog`, `_errors`) from the context; hence `skipGlobalSinks`.\n const skipGlobalSinks = phase === 'recognizer' || phase === 'capture'\n // Grammar-level ambient trivia declared via rules({ trivia }, factory): install\n // it as ctx.trivia so it's ambient for the whole parse (the interpreter path;\n // a compiled entry has it baked in and carries no _meta). parser/noTrivia still\n // override locally.\n const grammarTrivia = typeof entry !== 'function' ? entry._meta.grammarTrivia : undefined\n const grammarScanSkip = typeof entry !== 'function' ? entry._meta.grammarScanSkip : undefined\n const ctx: ParseContext = {\n trackLines: false,\n ...(skipGlobalSinks\n ? { _pmProfile: profile! }\n : { _triviaLog: triviaLog, _errors: errors, ...(profile === undefined ? {} : { _pmProfile: profile }) }),\n build: options.build,\n state: options.state,\n ...(grammarTrivia !== undefined\n ? { trivia: grammarTrivia, ...(grammarTrivia._meta.triviaKindLabels ? { triviaKindLabels: grammarTrivia._meta.triviaKindLabels } : {}) }\n : {}),\n ...(grammarScanSkip !== undefined ? { scanSkip: grammarScanSkip } : {}),\n ...(options.triviaCaptureMask !== undefined ? { _triviaCaptureMask: options.triviaCaptureMask } : {}),\n ...(options.tolerant ? { _tolerant: true, _rec: REC } : {}),\n ...(options.instrumentation === undefined ? {} : options.instrumentation),\n }\n const r = invoke(entry, input, 0, ctx)\n\n let unconsumedFrom: number | null = null\n if (r.ok) {\n let pos = r.span?.end ?? 0\n if (options.trivia && pos < input.length) {\n // Throwaway ctx: trailing trivia must NOT pollute the parse's trivia log.\n const t = invoke(options.trivia, input, pos, { trackLines: false })\n if (t.ok && t.span.end > pos) pos = t.span.end\n }\n unconsumedFrom = pos < input.length ? pos : null\n }\n\n return {\n ok: r.ok,\n value: r.ok ? (r as { value: unknown }).value : undefined,\n span: r.span ?? { start: 0, end: 0 },\n expected: r.ok ? [] : ((r as { expected?: string[] }).expected ?? []),\n errors,\n triviaLog,\n unconsumedFrom,\n }\n}\n\n/** Shallow copy of `options.state` per pass so an in-place mutation by one\n * profiling pass doesn't leak into the next (the parity check only compares\n * `ok`/`unconsumedFrom`, so a diverged structural output would go unnoticed).\n * Only the top level is isolated \u2014 deeply-nested mutable state is shared; a\n * profiled grammar should keep per-parse state shallow (see the `profile` docs). */\nfunction clonePassState(state: unknown): unknown {\n if (state === null || typeof state !== 'object') return state\n return Array.isArray(state) ? [...state] : { ...(state as Record<string, unknown>) }\n}\n\nfunction profilePass(entry: Runnable, input: string, options: RunOptions, phase: ProfilePhase): { result: RunResult; profile: RunProfilePass } {\n const state: ProfileState = { phase, nodes: 0, childSlots: 0, rawSlots: 0, triviaSlots: 0, fieldSlots: 0, hostCalls: 0 }\n const passOptions: RunOptions = options.state === undefined ? options : { ...options, state: clonePassState(options.state) }\n const start = performance.now()\n const result = runOnce(entry, input, passOptions, phase, state)\n const { phase: _phase, ...counts } = state\n return { result, profile: { ms: performance.now() - start, ...counts } }\n}\n\nexport function run(entry: Runnable, input: string, options: RunOptions = {}): RunResult {\n if (!options.profile) return runOnce(entry, input, options)\n if (typeof entry !== 'function') {\n throw new TypeError('run({ profile: true }) requires a compiled parser entry')\n }\n\n const recognizer = profilePass(entry, input, options, 'recognizer')\n const capture = profilePass(entry, input, options, 'capture')\n const host = profilePass(entry, input, options, 'host')\n if (recognizer.result.ok !== host.result.ok || capture.result.ok !== host.result.ok\n || recognizer.result.unconsumedFrom !== host.result.unconsumedFrom\n || capture.result.unconsumedFrom !== host.result.unconsumedFrom) {\n throw new Error('run({ profile: true }) changed recognition; the grammar is not profile-safe')\n }\n return {\n ...host.result,\n profile: {\n recognizer: recognizer.profile,\n structuralCapture: capture.profile,\n hostConstruction: host.profile,\n },\n }\n}\n", "import type { Combinator, ParseFail, ParseContext, ParseResult } from '../types.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/** Anything `completionsAt` can probe: an interpreter combinator OR a `compile()`d\n * grammar. A grammar compiled with `{ recovery: true }` records the probe on its fast\n * path, so completions work on the published compiled artifact \u2014 no interpreter needed. */\ntype CompletionsTarget =\n | Combinator<unknown>\n | { parseWithContext(input: string, ctx: ParseContext, pos?: number): ParseResult<unknown> }\n\n/**\n * Returns the set of expected tokens at the given cursor offset.\n * Useful for implementing IDE completions.\n *\n * Runs the parser on input truncated at `offset` with a probe that tracks the\n * highest-position failure seen, even when sepBy/many backtrack past the cursor.\n * Returns the expected tokens from that deepest failure.\n *\n * Works on the interpreter combinator OR a `compile(g, { recovery: true })` grammar\n * (the probe is recorded on the compiled fast path). Returns an empty array when the\n * input up to `offset` parses completely with no failures at or before the cursor.\n *\n * The returned strings use the same labels as ParseFail.expected:\n * quoted literals like `\"\\\"{\\\"\"` and regex patterns like `\"/[0-9]+/\"`.\n */\nexport function completionsAt(\n target: CompletionsTarget,\n input: string,\n offset: number,\n options: { tolerant?: boolean } = {},\n): string[] {\n const probe: { offset: number; best: ParseFail | null } = { offset, best: null }\n // In tolerant mode, list recovery keeps the enclosing node parsing past a bad\n // element so the failure at the cursor is actually recorded \u2014 the completion set\n // is otherwise empty when a permissive top rule \"succeeds\" with an unconsumed tail.\n const ctx: ParseContext = { trackLines: false, _probe: probe, ...(options.tolerant ? { _tolerant: true, _rec: REC } : {}) }\n const sliced = input.slice(0, offset)\n const result = 'parseWithContext' in target\n ? target.parseWithContext(sliced, ctx, 0)\n : target.parse(sliced, 0, ctx)\n\n // If the parser consumed everything up to offset successfully, there is nothing\n // to complete \u2014 the input is already valid at this position. In tolerant mode the\n // parse \"succeeds\" (list recovery swallowed the incomplete element at the cursor),\n // so the meaningful expectation lives in the deepest failure the probe recorded\n // while recovering: return that instead of an empty set.\n if (result.ok) return options.tolerant ? (probe.best?.expected ?? []) : []\n\n // Use whichever failure (probe or top-level) sits at the deeper position.\n const best = deeperFail(probe.best, result)\n return best?.expected ?? []\n}\n\nfunction deeperFail(a: ParseFail | null, b: ParseFail | null): ParseFail | null {\n if (!a) return b\n if (!b) return a\n return a.span.start >= b.span.start ? a : b\n}\n", "/**\n * Structural recognition of \"scannable\" parser arms \u2014 regex shapes that lower to\n * a tight character-scan loop instead of `regex.exec` / combinator dispatch. Each\n * shape is derived from the regex STRUCTURE, not from any hardcoded knowledge\n * that a given regex \"is whitespace\" or \"is a comment\":\n *\n * [X]+ / [X]* \u2192 run while the char \u2208 X (chars)\n * <lit>[^X]* \u2192 consume <lit>, run until char \u2208 X (until)\n * <open>(?:\u2026)*<close> \u2192 consume <open>, run to <close> literal (delimited)\n * x?<lit><[X]*>\u2026 \u2192 a general linear chain of lit/run (seq)\n *\n * `seq` is the CATEGORY generalization: any chain of literal segments (required\n * or optional `x?`) and char-class runs (positive/negated, `?`/`*`/`+`). It\n * subsumes CSS/Less `-?ident`, `--custom`, `@-?keyword`, `[^\u2026]+`, `::?`, etc.\n * without hardcoding a single byte \u2014 lowered only when a greedy one-pass scan\n * provably equals the engine's backtracking (see `seqIsUnambiguous`).\n *\n * A `oneOrMore(choice(a, b, \u2026))` where every arm is one of these compiles to a\n * single char-dispatch loop with one branch per arm \u2014 any count/order, because\n * the shapes dispatch on their first 1\u20132 chars and are checked in turn. Trivia\n * (whitespace + comments) is just the value-discarded instance of this; nothing\n * here is trivia-specific.\n */\n\n/**\n * One segment of a `seq` shape. A `seq` is the general category \"a fixed linear\n * chain of literals and char-class runs\" \u2014 no alternation, no groups, no\n * backtracking. It subsumes every CSS/Less token that is \"(optional prefix)?\n * literal* char-run* \u2026\" without hardcoding any particular byte:\n *\n * lit \u2014 a run of fixed code points, optionally present (`x?`) or required.\n * run \u2014 a char-class run: positive/negated ranges, min 0/1, bounded to one\n * char (`[x]`, `[x]?`) or unbounded (`[x]*`, `[x]+`).\n * group \u2014 a non-capturing sub-pattern `(?:\u2026)` (\u00A78f), quantified the same way\n * as `run` (`min`/`unbounded`). `inner` is any already-recognized\n * `ScanShape` for the group's body (so a group can itself contain\n * `seq`/`chars`/`alt`/\u2026 \u2014 including a nested alternation via \u00A78e).\n * Only used when `groupInnerSafe(inner)` holds (see that function).\n *\n * We only lower a `seq` when greedy left-to-right scanning provably equals the\n * regex engine's backtracking match (see `seqIsUnambiguous`).\n */\nexport type SeqPart =\n | { part: 'lit'; cps: number[]; optional: boolean }\n // A char-class run with a general counted quantifier: `min`..`max` occurrences.\n // `max === Infinity` is unbounded (`+`/`*`/`{n,}`). The four legacy shapes map\n // as `+`\u2192{1,\u221E} `*`\u2192{0,\u221E} `?`\u2192{0,1} bare\u2192{1,1}; `{n,m}` (\u00A78c) fills the rest.\n // Greedy scanning has \"wiggle\" a backtracker could exploit iff `max > min`.\n | { part: 'run'; ranges: Array<[number, number]>; negated: boolean; min: number; max: number }\n | { part: 'group'; inner: ScanShape; min: 0 | 1; unbounded: boolean }\n\nexport type ScanShape =\n | { kind: 'chars'; ranges: Array<[number, number]>; minOne: boolean }\n | { kind: 'ident'; head: Array<[number, number]>; tail: Array<[number, number]> }\n | { kind: 'until'; open: number[]; stop: Array<[number, number]> }\n | { kind: 'delimited'; open: number[]; close: number[] }\n // <q>(?:[^<q>\\\\]|\\\\.)*<q> \u2014 a quote-delimited string with backslash escapes.\n // `excluded` = the body's negated class (always contains the quote + backslash,\n // maybe a newline); `escLineTerm` = whether `\\\\` may be followed by a line\n // terminator (`\\\\[\\s\\S]` \u2192 true, `\\\\.` \u2192 false, since `.` excludes them).\n | { kind: 'string'; quote: number; excluded: Array<[number, number]>; escLineTerm: boolean }\n // A linear chain of literal/char-run segments (optional prefixes, literal\n // openers, negated runs, \u2026) \u2014 the general CSS/Less token category.\n | { kind: 'seq'; parts: SeqPart[] }\n // A pure literal matched case-insensitively (`/i`): the general \"ASCII\n // case-fold literal\" category (CSS `url(`, `@media` keywords under `i`, \u2026).\n | { kind: 'litFold'; open: number[] }\n // `<inner>(?!class)` / `<inner>(?=class)` \u2014 a trailing lookahead boundary\n // check on an already-lowered shape. Zero-width: `end` is `inner`'s end, never\n // extended. `classNegated` is the bracket's OWN `[^\u2026]` negation (e.g.\n // `(?![0-9a-fA-F])` vs `(?=[^0-9])`), independent of `negative` (`!` vs `=`).\n | { kind: 'lookahead'; inner: ScanShape; ranges: Array<[number, number]>; classNegated: boolean; negative: boolean }\n // `A|B|C` \u2014 top-level alternation (\u00A78e), split outside any `[]`/`()`, each arm\n // independently lowered. `disjoint` is true iff every pair of arms has\n // provably non-overlapping first-char sets (`firsts[i]` holds that arm's set,\n // or `null` if it could start with any char \u2014 an unbounded-optional `chars`\n // arm, or a non-disjoint mix). When `disjoint`, codegen dispatches straight to\n // the one matching arm; otherwise it tries arms in order and takes the first\n // that succeeds \u2014 the same ordered-choice semantics as regex `|` itself (see\n // the ordered-vs-longest verification in PERF_IDEAS \u00A78e).\n | { kind: 'alt'; arms: ScanShape[]; disjoint: boolean; firsts: Array<CharSet | null> }\n\n/** Backslash (`\\`) code point \u2014 the escape lead char in string shapes. */\nexport const BACKSLASH = 92\n\nconst META = new Set('()[]{}*+?|^$.'.split(''))\n\n// Regex char-class primitives are shared with the interpreter's first-set\n// analyzer and `regex()`'s scanner \u2014 see `../regex/classes.ts`. `SPACE_RANGES`\n// and `parseClassRanges` are re-exported here because codegen's boundary-class\n// lowering imports them from this module.\nimport { CLASS_ESCAPES, SPACE_RANGES, shorthandRanges, readUnicodeEscape, parseClassRanges } from '../regex/classes.ts'\nexport { SPACE_RANGES, parseClassRanges }\n\n/** A single class token (`[...]` or `\\d`/`\\w`) to ranges, or null. Rejects negation. */\nfunction classToRanges(cls: string): Array<[number, number]> | null {\n if (cls === '\\\\d') return shorthandRanges('d')\n if (cls === '\\\\w') return shorthandRanges('w')\n if (cls === '\\\\s') return shorthandRanges('s')\n const body = cls.slice(1, -1)\n if (body.startsWith('^')) return null\n return parseClassRanges(body)\n}\n\n/** ASCII case-insensitive letter check for `i`-flag literal lowering. */\n// ASCII case-insensitive char compare. For a letter, upper/lower differ only in\n// bit 0x20, and `(c | 32) === <lowercase>` is satisfied by EXACTLY that letter's\n// two cases (nothing else) \u2014 one OR + one compare, ~1.75\u00D7 faster than the\n// two-compare `(c===U || c===L)` form (measured). `(\u2026 | 32)` is parenthesized\n// because `===` binds tighter than `|`. Non-letters keep an exact compare.\n// Exported so the case-insensitive `literal()` emitter (codegen) shares the exact\n// same ASCII fold as `/i`-flag regex lowering \u2014 no `Intl.Collator` on any path.\nexport const foldEq = (cVar: string, cp: number): string => {\n if (cp >= 65 && cp <= 90) return `(${cVar} | 32) === ${cp + 32}`\n if (cp >= 97 && cp <= 122) return `(${cVar} | 32) === ${cp}`\n return `${cVar} === ${cp}`\n}\n\n/** All-literal regex fragment (`\\/\\*`, `\\/\\/`, \u2026) \u2192 its code points, or null on an unescaped metachar. */\nfunction literalCodePoints(frag: string): number[] | null {\n const out: number[] = []\n let i = 0\n while (i < frag.length) {\n const ch = frag[i]!\n if (ch === '\\\\') {\n const e = frag[i + 1]\n if (e === undefined) return null\n out.push(e in CLASS_ESCAPES ? CLASS_ESCAPES[e]! : e.codePointAt(0)!)\n i += 2\n continue\n }\n if (META.has(ch)) return null\n out.push(ch.codePointAt(0)!)\n i += 1\n }\n return out.length ? out : null\n}\n\n/**\n * Recognize a quote-delimited string with backslash escapes:\n * <q>(?:[^\u2026]|\\\\.)*<q> or <q>(?:\\\\.|[^\u2026])*<q> (`?:` optional)\n * where the negated body class contains the quote and the backslash, and the\n * escape body is `.` (no line terminators) or `[\\s\\S]` (any char). Returns null\n * for anything else so the caller falls back to `regex.exec`.\n */\nfunction parseStringShape(source: string): ScanShape | null {\n const q = source[0]\n if (q === undefined || q === '\\\\' || META.has(q)) return null\n if (source.length < 3 || source[source.length - 1] !== q) return null\n const inner = source.slice(1, -1)\n const CLS = String.raw`\\[\\^((?:\\\\.|[^\\]])+)\\]`\n const ESC = String.raw`\\\\\\\\(\\.|\\[\\\\s\\\\S\\]|\\[\\\\S\\\\s\\])`\n // Two arm orders; `?:` optional. Capture groups differ, so read both out.\n let body: string | undefined\n let esc: string | undefined\n let m = new RegExp(`^\\\\((?:\\\\?:)?${CLS}\\\\|${ESC}\\\\)\\\\*$`).exec(inner)\n if (m) { body = m[1]; esc = m[2] }\n else {\n m = new RegExp(`^\\\\((?:\\\\?:)?${ESC}\\\\|${CLS}\\\\)\\\\*$`).exec(inner)\n if (m) { esc = m[1]; body = m[2] }\n }\n if (body === undefined || esc === undefined) return null\n const excluded = parseClassRanges(body)\n if (!excluded) return null\n const qcp = q.codePointAt(0)!\n const inSet = (cp: number) => excluded.some(([lo, hi]) => cp >= lo && cp <= hi)\n if (!inSet(qcp) || !inSet(BACKSLASH)) return null\n return { kind: 'string', quote: qcp, excluded, escLineTerm: esc !== '.' }\n}\n\n/**\n * Parse a regex source into a linear chain of literal / char-run segments, or\n * null if it uses any construct outside that category (alternation, groups,\n * `.`, `{n,m}`, lookaround, unknown escapes). This is the STRUCTURAL recognizer\n * for the `seq` shape \u2014 it encodes categories (optional prefix, literal opener,\n * negated run, \u2026), never any specific byte value.\n */\nfunction parseSeqParts(source: string): SeqPart[] | null {\n const parts: SeqPart[] = []\n let lit: number[] = []\n const flush = () => {\n if (lit.length) { parts.push({ part: 'lit', cps: lit, optional: false }); lit = [] }\n }\n // Parse a `{n}` / `{n,}` / `{n,m}` counted quantifier at `source[at]` (`'{'`).\n // Returns the bounds and the token length, or null when it isn't a well-formed\n // brace quantifier (then the `{` is left for the caller's metachar decline).\n const braceQuant = (at: number): { min: number; max: number; len: number } | null => {\n const close = source.indexOf('}', at)\n if (close === -1) return null\n const body = source.slice(at + 1, close)\n const m = /^(\\d+)(,(\\d*)?)?$/.exec(body)\n if (!m) return null\n const min = Number(m[1])\n const max = m[2] === undefined ? min : m[3] ? Number(m[3]!) : Infinity\n // Decline degenerate/empty-capable or invalid bounds \u2014 RegExp handles them.\n if (max < 1 || min > max) return null\n return { min, max, len: close - at + 1 }\n }\n const runFrom = (ranges: Array<[number, number]>, negated: boolean, at: number) => {\n const q = source[at]\n if (q === '+') { parts.push({ part: 'run', ranges, negated, min: 1, max: Infinity }); return 1 }\n if (q === '*') { parts.push({ part: 'run', ranges, negated, min: 0, max: Infinity }); return 1 }\n if (q === '?') { parts.push({ part: 'run', ranges, negated, min: 0, max: 1 }); return 1 }\n if (q === '{') {\n const b = braceQuant(at)\n if (b) { parts.push({ part: 'run', ranges, negated, min: b.min, max: b.max }); return b.len }\n }\n parts.push({ part: 'run', ranges, negated, min: 1, max: 1 })\n return 0\n }\n let i = 0\n while (i < source.length) {\n const ch = source[i]!\n if (ch === '[') {\n // Read a char class, honoring `\\]`.\n let k = i + 1\n const neg = source[k] === '^'\n if (neg) k++\n let body = ''\n while (k < source.length && source[k] !== ']') {\n if (source[k] === '\\\\') { body += source[k]! + (source[k + 1] ?? ''); k += 2 }\n else { body += source[k]!; k++ }\n }\n if (source[k] !== ']') return null\n const ranges = parseClassRanges(body)\n if (!ranges) return null\n flush()\n i = k + 1\n i += runFrom(ranges, neg, i)\n continue\n }\n if (ch === '\\\\') {\n const e = source[i + 1]\n if (e === 'd' || e === 'w' || e === 's') {\n flush()\n i += 2\n i += runFrom(shorthandRanges(e), false, i)\n continue\n }\n // Escaped literal code point (incl. `\\uXXXX`, `\\t`, `\\(`, \u2026).\n const uni = readUnicodeEscape(source, i)\n let cp: number\n if (uni) { cp = uni.cp; i = uni.next }\n else if (e === undefined) return null\n else if (e in CLASS_ESCAPES) { cp = CLASS_ESCAPES[e]!; i += 2 }\n else if ((e >= 'a' && e <= 'z') || (e >= 'A' && e <= 'Z')) return null\n else { cp = e.codePointAt(0)!; i += 2 }\n if (source[i] === '?') { flush(); parts.push({ part: 'lit', cps: [cp], optional: true }); i++ }\n else lit.push(cp)\n continue\n }\n if (ch === '(') {\n // Only a non-capturing group `(?:\u2026)` is modeled (\u00A78f) \u2014 a bare capturing\n // group, or a lookaround appearing mid-pattern (a TRAILING one is peeled\n // off separately by `stripTrailingLookahead` before we ever get here), is\n // declined outright rather than risk misreading it.\n if (source[i + 1] !== '?' || source[i + 2] !== ':') return null\n let k = i + 3\n let depth = 1\n while (k < source.length && depth > 0) {\n const c2 = source[k]\n if (c2 === '\\\\') { k += 2; continue }\n if (c2 === '[') {\n let j = k + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') return null\n k = j + 1\n continue\n }\n if (c2 === '(') depth++\n else if (c2 === ')') depth--\n k++\n }\n if (depth !== 0) return null\n const body = source.slice(i + 3, k - 1)\n const inner = parseScanShape(body)\n if (!inner || !groupInnerSafe(inner)) return null\n const q = source[k]\n // `{n,m}` on a group is a \u00A78c/\u00A78f combination not modeled \u2014 decline.\n if (q === '{') return null\n flush()\n if (q === '+') {\n if (shapeCanBeEmpty(inner)) return null // would loop forever \u2014 decline\n parts.push({ part: 'group', inner, min: 1, unbounded: true })\n i = k + 1\n } else if (q === '*') {\n if (shapeCanBeEmpty(inner)) return null\n parts.push({ part: 'group', inner, min: 0, unbounded: true })\n i = k + 1\n } else if (q === '?') {\n parts.push({ part: 'group', inner, min: 0, unbounded: false })\n i = k + 1\n } else {\n parts.push({ part: 'group', inner, min: 1, unbounded: false })\n i = k\n }\n continue\n }\n // A bare metachar (`|`, `.`, `{`, stray `*`/`+`/`?`, \u2026) is outside the\n // linear-chain category \u2014 bail to the RegExp fallback.\n if (META.has(ch)) return null\n const cp = ch.codePointAt(0)!\n i += ch.length\n if (source[i] === '?') { flush(); parts.push({ part: 'lit', cps: [cp], optional: true }); i++ }\n else lit.push(cp)\n }\n flush()\n return parts.length ? parts : null\n}\n\n/**\n * Only lower a `seq` when greedy left-to-right scanning matches EXACTLY what the\n * backtracking regex engine would. The dangerous cases are (a) an optional part\n * that could also be consumed by what follows, and (b) an unbounded/repeated\n * part whose accepted class overlaps what follows \u2014 both let the engine\n * backtrack to a different boundary than a one-pass scan. We also require at\n * least one mandatory segment (so the token can't match zero-width).\n *\n * A skippable/repeatable part `p` at index `k` is checked against\n * `seqFirstAccept(parts.slice(k + 1))` \u2014 the accept-set of EVERYTHING that\n * could start at the very next position, unioned through any further chain of\n * optionals up to the next mandatory part (the exact same computation\n * `seqFirstAccept` already does for a seq's OWN leading accept-set). This\n * subsumes the simpler \"check only the immediate neighbor\" rule as the\n * one-part-following case, while also correctly handling a CHAIN of several\n * consecutive optional parts (e.g. two independent optional groups in a row,\n * `(?:\\.\\d+)?(?:[eE][+-]?\\d+)?` \u2014 JSON/GraphQL's number tail, \u00A78f): each is\n * safe precisely when disjoint from everything that could follow it, however\n * many further optionals that spans. An empty tail (`p` is the last part)\n * naturally resolves to a trivially-disjoint empty set \u2014 nothing to check.\n *\n * Uniform across `lit`/`run`/`group`: each part's own \"first char it could\n * start with\" (`partFirstAccept`) is compared via `firstSetsDisjoint` \u2014 the\n * same subset/disjoint math used everywhere else in this file (\u00A78b's\n * lookahead guard, \u00A78e's alt-dispatch guard).\n */\nfunction seqIsUnambiguous(parts: SeqPart[]): boolean {\n let hasMandatory = false\n for (let k = 0; k < parts.length; k++) {\n const p = parts[k]!\n const mandatory = p.part === 'lit' ? !p.optional : p.min >= 1\n if (mandatory) hasMandatory = true\n // A `group` part has a right-edge exposure (`groupPartExposure`): the chars a\n // backtracking engine could give back by shrinking the body's own trailing\n // quantifier, dropping the group (if optional/repeated), or \u2014 for a\n // non-disjoint alt body \u2014 switching arms. Ordered-choice greedy scanning\n // matches the engine only when NONE of those relinquished chars could also\n // start what follows the group (else the scan and the engine disagree on\n // where the group ends). Two outcomes:\n // - a concrete class \u2192 must be disjoint from `seqFirstAccept(rest)`. This\n // is the general fix for a group whose body ends in an unbounded run\n // (`(?:\\d+)\\d` must NOT lower), and it lets a mutually-exclusive fixed\n // alt (\u00A78i, exposure `null`) lower at ANY position, not just trailing.\n // - `'unsupported'` \u2192 the exposure can't be bounded (e.g. a non-disjoint\n // alt like `(?:a|ab)` that genuinely needs arm-switching). Sound only\n // when nothing follows to force reconsideration \u2014 the group must be the\n // trailing, matched-exactly-once part (\u00A78h); otherwise decline.\n if (p.part === 'group') {\n const exposure = groupPartExposure(p)\n if (exposure === 'unsupported') {\n const trailingOnce = k === parts.length - 1 && p.min === 1 && !p.unbounded\n if (!trailingOnce) return false\n } else if (exposure !== null) {\n if (!firstSetsDisjoint(exposure, seqFirstAccept(parts.slice(k + 1)))) return false\n }\n }\n // A part needs the disjoint-from-continuation check when a greedy one-pass\n // scan has freedom a backtracking engine could resolve differently: an\n // optional `lit`, a `run` with wiggle (`max > min` \u2014 covers `?`/`*`/`+` and\n // bounded `{n,m}` with `m > n`; a fixed `{n}` run has none), or an\n // optional/repeated `group`. A fixed-length run (`max === min`) consumes an\n // exact count with no boundary to shift, so it never needs the check.\n const needsDisjoint =\n p.part === 'lit' ? p.optional\n : p.part === 'run' ? p.max > p.min\n : (p.unbounded || p.min === 0)\n if (needsDisjoint) {\n const rest = seqFirstAccept(parts.slice(k + 1))\n if (!firstSetsDisjoint(partFirstAccept(p), rest)) return false\n }\n }\n return hasMandatory\n}\n\nfunction rangesOverlap(a: Array<[number, number]>, b: Array<[number, number]>): boolean {\n return a.some(([lo, hi]) => b.some(([lo2, hi2]) => lo <= hi2 && lo2 <= hi))\n}\n\n/** Is every range in `sub` fully contained in some range of `sup`? */\nfunction rangesSubset(sub: Array<[number, number]>, sup: Array<[number, number]>): boolean {\n return sub.every(([lo, hi]) => sup.some(([lo2, hi2]) => lo >= lo2 && hi <= hi2))\n}\n\n/**\n * A `(?!X)`/`(?=X)` operand: a char class (`[...]`, incl. `[^...]`), a\n * shorthand class (`\\d`/`\\w`/`\\s`), or a single literal/escaped char \u2014 never a\n * sub-pattern (no alternation, no nested groups). Returns the ranges to test\n * membership against, and whether the bracket itself was negated (`[^...]`) \u2014\n * independent of the lookahead's own `!`/`=` polarity.\n */\nfunction parseClassOperand(body: string): { ranges: Array<[number, number]>; negated: boolean } | null {\n if (body === '\\\\d' || body === '\\\\w' || body === '\\\\s') {\n return { ranges: shorthandRanges(body[1] as 'd' | 'w' | 's'), negated: false }\n }\n if (body.length >= 2 && body[0] === '[' && body[body.length - 1] === ']') {\n let inner = body.slice(1, -1)\n const negated = inner.startsWith('^')\n if (negated) inner = inner.slice(1)\n const ranges = parseClassRanges(inner)\n return ranges ? { ranges, negated } : null\n }\n const cps = literalCodePoints(body)\n if (cps && cps.length === 1) return { ranges: [[cps[0]!, cps[0]!]], negated: false }\n return null\n}\n\ntype TrailingLookahead = {\n base: string\n ranges: Array<[number, number]>\n classNegated: boolean\n negative: boolean\n}\n\ntype CharSet = { ranges: Array<[number, number]>; negated: boolean }\n\n/** Is char-set `t` fully contained in char-set `u`? Handles all 4 sign combos. */\nfunction classSubset(t: CharSet, u: CharSet): boolean {\n if (!t.negated && !u.negated) return rangesSubset(t.ranges, u.ranges)\n if (!t.negated && u.negated) return !rangesOverlap(t.ranges, u.ranges) // t \u2286 \u00ACu \u27FA t \u2229 u = \u2205\n if (t.negated && !u.negated) return false // \u00ACt \u2286 u is never true for finite ranges u\n return rangesSubset(u.ranges, t.ranges) // \u00ACt \u2286 \u00ACu \u27FA u \u2286 t\n}\n\n/** Are char-sets `t` and `u` disjoint (no overlap)? Handles all 4 sign combos. */\nfunction classDisjoint(t: CharSet, u: CharSet): boolean {\n if (!t.negated && !u.negated) return !rangesOverlap(t.ranges, u.ranges)\n if (!t.negated && u.negated) return rangesSubset(t.ranges, u.ranges) // t \u2229 \u00ACu = \u2205 \u27FA t \u2286 u\n if (t.negated && !u.negated) return rangesSubset(u.ranges, t.ranges) // symmetric\n return false // \u00ACt \u2229 \u00ACu = \u2205 would require t \u222A u = every code point \u2014 can't prove with finite ranges\n}\n\n/** `classDisjoint`, `'any'`-aware: an empty-capable part's first-set degrades\n * to `'any'` (see `shapeFirstAccept`), and `'any'` is never disjoint from\n * anything (it could start with literally any char, including whatever the\n * other side accepts). */\nfunction firstSetsDisjoint(a: CharSet | 'any', b: CharSet | 'any'): boolean {\n if (a === 'any' || b === 'any') return false\n return classDisjoint(a, b)\n}\n\n/**\n * A shape's match as a fixed-length list of per-position char-sets, or `null`\n * if its match length is NOT fixed. Only a `seq` of mandatory `lit`s (each code\n * point is one fixed position) and mandatory single-char runs (`[x]`, min 1,\n * bounded) qualifies \u2014 anything with an optional part, an unbounded/optional\n * run, or a nested group has a variable length and returns `null`. Used to\n * decide when a non-disjoint alternation's arms are mutually exclusive (\u00A78i).\n */\nfunction fixedClassSeq(shape: ScanShape): CharSet[] | null {\n // A case-fold literal is fixed-length: each code point becomes a single-char\n // class holding BOTH ASCII cases (so mutual-exclusivity is judged on what the\n // `/i` match actually accepts \u2014 `and` vs `or` are disjoint at position 0).\n if (shape.kind === 'litFold') {\n return shape.open.map(cp => {\n const other = cp >= 65 && cp <= 90 ? cp + 32 : cp >= 97 && cp <= 122 ? cp - 32 : cp\n return other === cp\n ? { ranges: [[cp, cp]] as Array<[number, number]>, negated: false }\n : { ranges: [[Math.min(cp, other), Math.min(cp, other)], [Math.max(cp, other), Math.max(cp, other)]] as Array<[number, number]>, negated: false }\n })\n }\n if (shape.kind !== 'seq') return null\n const out: CharSet[] = []\n for (const p of shape.parts) {\n if (p.part === 'lit') {\n if (p.optional) return null\n for (const cp of p.cps) out.push({ ranges: [[cp, cp]], negated: false })\n } else if (p.part === 'run' && p.max === p.min && Number.isFinite(p.max)) {\n // A fixed-count run `[x]{n}` (incl. the bare `[x]`, n=1) is n fixed\n // positions of the same class. Variable-length runs return null below.\n for (let c = 0; c < p.max; c++) out.push({ ranges: p.ranges, negated: p.negated })\n } else {\n return null // optional/variable/unbounded run, or a nested group \u2014 length not fixed\n }\n }\n return out.length ? out : null\n}\n\n/**\n * \u00A78i \u2014 is a NON-disjoint `alt` shape one whose arms are pairwise MUTUALLY\n * EXCLUSIVE fixed-length token sets? Every arm must be a `fixedClassSeq`, and no\n * arm's match may ever be a prefix (proper, or an equal-length overlap) of\n * another arm's \u2014 i.e. for every ordered pair (A, B) with `A.length <= B.length`\n * there is a position within A where their classes are DISJOINT. That property\n * guarantees at most one arm matches at any input position, so ordered-choice\n * commit lands on the SAME arm (and the SAME fixed end) the engine would, with\n * no arm to switch to under backtracking \u2014 sound in ANY position, not just\n * trailing (unlike the general non-disjoint alt, which needs the trailing-once\n * gate in `seqIsUnambiguous`). `(?:ab|ac)` and `(?:foo|barn)` qualify;\n * `(?:a|ab)` (arm `a` is a prefix of `ab`) does NOT.\n */\nfunction altFixedMutuallyExclusive(shape: ScanShape): boolean {\n if (shape.kind !== 'alt' || shape.disjoint) return false\n const lists: CharSet[][] = []\n for (const arm of shape.arms) {\n const f = fixedClassSeq(arm)\n if (!f) return false\n lists.push(f)\n }\n for (let i = 0; i < lists.length; i++) {\n for (let j = 0; j < lists.length; j++) {\n if (i === j) continue\n const a = lists[i]!\n const b = lists[j]!\n if (a.length > b.length) continue // only check the shorter (a) as a prefix of the longer (b)\n let disjointAt = false\n for (let p = 0; p < a.length; p++) {\n if (classDisjoint(a[p]!, b[p]!)) { disjointAt = true; break }\n }\n if (!disjointAt) return false // `a` could be a prefix of `b` \u2014 not mutually exclusive\n }\n }\n return true\n}\n\n/**\n * The char-set a backtracking engine could relinquish at a `group` PART's right\n * edge \u2014 its \"trailing exposure\". Combines two independent backtrack moves: the\n * group body's OWN trailing wiggle (`trailingBacktrackClass(inner)`, e.g. the\n * body ends in an unbounded run), and \u2014 if the group is optional (`min 0`) or\n * repeated (`unbounded`) \u2014 the \"drop the last occurrence\" move, which exposes\n * the body's own first-char set. A required-once group (`min 1`, bounded) has\n * only the first move. Returns `'unsupported'` if either component can't be\n * pinned to a concrete set. This is the single computation used everywhere a\n * group's right boundary matters: `trailingBacktrackClass` (a `seq` ending in a\n * group) and `seqIsUnambiguous` (a group at ANY position, followed by more).\n */\nfunction groupPartExposure(part: Extract<SeqPart, { part: 'group' }>): CharSet | null | 'unsupported' {\n const innerWiggle = trailingBacktrackClass(part.inner)\n if (innerWiggle === 'unsupported') return 'unsupported'\n const requiredOnce = !part.unbounded && part.min === 1\n if (requiredOnce) return innerWiggle // no \"drop it\" move possible \u2014 only inner's own wiggle matters\n const dropExposed = shapeFirstAccept(part.inner)\n if (dropExposed === 'any') return 'unsupported'\n if (innerWiggle === null) return dropExposed\n const unioned = unionCharSets([innerWiggle, dropExposed])\n return unioned === 'any' ? 'unsupported' : unioned\n}\n\n/**\n * The char-class a backtracking engine could expose by shrinking `shape`'s\n * OWN trailing quantifier by one position \u2014 i.e. the \"wiggle room\" a trailing\n * lookahead could interact with. Returns `null` when `shape`'s matched length\n * is fully fixed once it succeeds (no quantifier to shrink), which makes\n * wrapping it in a lookahead unconditionally safe. Returns `'unsupported'` for\n * shape kinds whose backtracking semantics aren't modeled here (declines the\n * lookahead lowering rather than risking an unproven guard).\n *\n * Only the LAST quantifier matters: `seqIsUnambiguous` already proves every\n * EARLIER part of a `seq` has exactly one valid parse (no alternate length),\n * so the sole remaining freedom a backtracking engine has \u2014 once we attach an\n * external trailing lookahead \u2014 is in whatever quantifier sits at the very end.\n */\nfunction trailingBacktrackClass(shape: ScanShape): CharSet | null | 'unsupported' {\n if (shape.kind === 'chars') return { ranges: shape.ranges, negated: false }\n if (shape.kind === 'ident') return { ranges: shape.tail, negated: false }\n if (shape.kind === 'litFold') return null // fixed-length literal \u2014 no quantifier at all\n if (shape.kind === 'seq') {\n const last = shape.parts[shape.parts.length - 1]!\n if (last.part === 'lit') return null // fixed literal tail \u2014 no wiggle room\n if (last.part === 'run') {\n // Wiggle iff `max > min`: unbounded (`+`/`*`/`{n,}`), optional (`[x]?`),\n // or bounded `{n,m}` with `m > n` all let a backtracker shrink the trailing\n // run by \u22651 position. A fixed run (`[x]`, `{n}`, `max === min`) has no\n // choice \u2014 no wiggle, hence no risk.\n if (last.max > last.min) return { ranges: last.ranges, negated: last.negated }\n return null\n }\n // A trailing `group` (\u00A78f): its trailing exposure combines its body's own\n // wiggle and (if optional/repeated) the \"drop it\" move \u2014 see\n // `groupPartExposure`, shared with `seqIsUnambiguous`.\n return groupPartExposure(last)\n }\n if (shape.kind === 'alt') {\n // Arm-switching hazard (see `groupInnerSafe`) applies here too: a\n // non-disjoint alt can't be trusted to expose the right wiggle class,\n // since which arm ultimately \"wins\" could itself change under backtrack.\n // A disjoint alt has no such hazard \u2014 exactly one arm is ever reachable\n // from a given starting char, so the union of every arm's OWN wiggle\n // covers whichever one is actually taken. A non-disjoint alt whose arms are\n // fixed-length and mutually exclusive (\u00A78i) also has no wiggle \u2014 exactly one\n // arm matches at any position and its length is fixed, so `null`.\n if (!shape.disjoint) return altFixedMutuallyExclusive(shape) ? null : 'unsupported'\n const classes = shape.arms.map(trailingBacktrackClass)\n if (classes.some(c => c === 'unsupported')) return 'unsupported'\n const concrete = classes.filter((c): c is CharSet => c !== null)\n if (concrete.length === 0) return null\n const unioned = unionCharSets(concrete)\n return unioned === 'any' ? 'unsupported' : unioned\n }\n // `until`/`delimited`/`string`/nested `lookahead` \u2014 not modeled; decline.\n return 'unsupported'\n}\n\n/**\n * Is it safe to lower `inner(?!ranges)` / `inner(?=ranges)` to a one-shot\n * post-match check (vs. requiring real backtracking)? See PERF_IDEAS \u00A78b.\n */\nfunction lookaheadUnambiguous(inner: ScanShape, operand: CharSet, negative: boolean): boolean {\n const tail = trailingBacktrackClass(inner)\n if (tail === 'unsupported') return false\n if (tail === null) return true // inner's length is fixed \u2014 no backtracking possible\n // Negative `(?!op)`: safe iff every char the tail could expose is ALSO\n // excluded by `op` (so shrinking never turns a failure into a pass).\n // Positive `(?=op)`: safe iff the tail is disjoint from `op` (so shrinking\n // could never expose a char that suddenly satisfies `op`).\n return negative ? classSubset(tail, operand) : classDisjoint(tail, operand)\n}\n\n/**\n * Strip a trailing `(?!class)` / `(?=class)` boundary assertion, if present.\n * Only a TRAILING lookahead with a char-class/single-char operand is\n * recognized \u2014 no sub-patterns, no nested groups. Anything else containing an\n * unescaped `(` is left to the other recognizers, which reject it as a\n * metachar (`META`) and fall back to `RegExp.exec`, so a false read here can\n * only fail to lower, never mis-lower.\n */\nfunction stripTrailingLookahead(source: string): TrailingLookahead | null {\n if (!source.endsWith(')')) return null\n for (const negative of [true, false] as const) {\n const lead = negative ? '(?!' : '(?='\n const idx = source.lastIndexOf(lead)\n if (idx === -1) continue\n const body = source.slice(idx + lead.length, -1)\n const operand = body.length ? parseClassOperand(body) : null\n if (!operand) continue\n return { base: source.slice(0, idx), ranges: operand.ranges, classNegated: operand.negated, negative }\n }\n return null\n}\n\n/**\n * Strip ONE redundant outer `(?:\u2026)` wrapper, if it spans the ENTIRE source\n * (nothing before it, nothing \u2014 not even a quantifier \u2014 after its matching\n * close). A non-capturing group with no trailing quantifier is semantically\n * identical to its bare contents, so this is always safe to unwrap; it's what\n * lets `(?:a|b|c)` (a whole-token alternation written as idiomatic regex\n * style, e.g. CSS `basicSel`) reach the top-level `|` split below. Bails\n * (leaves `source` untouched) on anything that isn't a clean whole-string\n * wrap: unbalanced parens, or a group that closes before the string ends\n * (which would mean a quantifier or trailing content follows it).\n */\nfunction unwrapOuterGroup(source: string): string {\n while (source.startsWith('(?:')) {\n let depth = 1\n let i = 3\n let ok = true\n while (i < source.length && depth > 0) {\n const ch = source[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') { ok = false; break }\n i = j + 1\n continue\n }\n if (ch === '(') depth++\n else if (ch === ')') depth--\n i++\n }\n if (!ok || depth !== 0 || i !== source.length) break\n source = source.slice(3, -1)\n }\n return source\n}\n\n/**\n * Split a regex source on top-level `|` \u2014 i.e. `|` outside any `[]` bracket\n * class and outside any `(...)` group (parens still count toward depth even\n * though we don't otherwise understand groups; a `|` nested in one is never a\n * split point). Returns null if there's no top-level `|` at all, if brackets/\n * parens are unbalanced, or if any resulting alternative is empty (a\n * zero-width arm \u2014 an unmodeled edge case, decline rather than mis-lower).\n * One redundant whole-string `(?:\u2026)` wrapper is stripped first (see\n * `unwrapOuterGroup`) so `(?:a|b)` splits the same as bare `a|b`.\n */\nfunction splitTopLevelAlternation(source: string): string[] | null {\n const body = unwrapOuterGroup(source)\n const parts: string[] = []\n let depth = 0\n let last = 0\n let i = 0\n while (i < body.length) {\n const ch = body[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (body[j] === '^') j++\n while (j < body.length && body[j] !== ']') {\n if (body[j] === '\\\\') j += 2\n else j++\n }\n if (body[j] !== ']') return null\n i = j + 1\n continue\n }\n if (ch === '(') { depth++; i++; continue }\n if (ch === ')') { depth--; if (depth < 0) return null; i++; continue }\n if (ch === '|' && depth === 0) {\n parts.push(body.slice(last, i))\n last = i + 1\n i++\n continue\n }\n i++\n }\n if (depth !== 0) return null\n parts.push(body.slice(last))\n if (parts.length < 2 || parts.some(p => p.length === 0)) return null\n return parts\n}\n\n/** Can `shape` match the empty string? Only an unbounded `chars` run with no\n * `+` (`[x]*`) can \u2014 every other shape requires at least one fixed/mandatory\n * char (`seq` always has \u22651 mandatory part per `seqIsUnambiguous`; `ident`/\n * `until`/`delimited`/`string`/`litFold` all require a fixed opening char). A\n * `lookahead`/`alt` defer to what they wrap. */\nfunction shapeCanBeEmpty(shape: ScanShape): boolean {\n if (shape.kind === 'chars') return !shape.minOne\n if (shape.kind === 'lookahead') return shapeCanBeEmpty(shape.inner)\n if (shape.kind === 'alt') return shape.arms.some(shapeCanBeEmpty)\n return false\n}\n\n/** Exact union of char-sets when it's computable, else `'any'` (conservative:\n * forces ordered dispatch rather than guessing at a mixed-polarity union, or\n * when any input is already `'any'`). A single concrete-set list is returned\n * as-is (including if negated) \u2014 the ambiguity is only in combining \u22652 sets\n * of differing polarity. */\nfunction unionCharSets(sets: Array<CharSet | 'any'>): CharSet | 'any' {\n if (sets.some(s => s === 'any')) return 'any'\n const concrete = sets as CharSet[]\n if (concrete.length === 1) return concrete[0]!\n if (concrete.some(s => s.negated)) return 'any'\n return { ranges: concrete.flatMap(s => s.ranges), negated: false }\n}\n\n/** A single seq part's own first-char set, ignoring whether it's skippable.\n * A `group` part delegates to its inner shape's own first-set (\u00A78f) \u2014\n * possibly `'any'` if the inner shape can match empty or is a non-disjoint\n * `alt` (see `shapeFirstAccept`). */\nfunction partFirstAccept(part: SeqPart): CharSet | 'any' {\n if (part.part === 'lit') return { ranges: [[part.cps[0]!, part.cps[0]!]], negated: false }\n if (part.part === 'run') return { ranges: part.ranges, negated: part.negated }\n return shapeFirstAccept(part.inner)\n}\n\n/** The char-set a `seq` can start with: union of each leading skippable part's\n * own set up to and including the first mandatory part (after which later\n * parts can no longer be what the very first input char matches). */\nfunction seqFirstAccept(parts: SeqPart[]): CharSet | 'any' {\n const sets: Array<CharSet | 'any'> = []\n for (const p of parts) {\n const mandatory = p.part === 'lit' ? !p.optional : p.min >= 1\n sets.push(partFirstAccept(p))\n if (mandatory) break\n }\n return unionCharSets(sets)\n}\n\n/**\n * Is `inner` safe to use as a group's body (\u00A78f)? `chars`/`ident`/`seq`/\n * `litFold` are always safe: each is only ever constructed after proving (via\n * `seqIsUnambiguous` or equivalent) that greedy resolution is the UNIQUE valid\n * match at a given position \u2014 there is no alternate length for a backtracker\n * to reconsider, so treating the group as an atomic, resolve-once unit is\n * sound regardless of what follows it. A `lookahead` defers to what it wraps\n * (zero-width, adds no new choice). An `alt` is safe ONLY when `disjoint`:\n * a NON-disjoint alt (`(?:a|ab)`) genuinely can need real backtracking into a\n * DIFFERENT arm if something after the group fails (verified: `/^(?:a|ab)c/`\n * matches \"abc\" via the SECOND arm, only because the first arm's match left\n * \"c\" unsatisfied) \u2014 our ordered-dispatch codegen resolves once and never\n * reconsiders, so that case must decline. `until`/`delimited`/`string` are\n * declined outright, same conservative call as `trailingBacktrackClass`.\n */\nfunction groupInnerSafe(shape: ScanShape): boolean {\n if (shape.kind === 'chars' || shape.kind === 'ident' || shape.kind === 'seq' || shape.kind === 'litFold') return true\n if (shape.kind === 'lookahead') return groupInnerSafe(shape.inner)\n // A disjoint alt is safe anywhere. A NON-disjoint alt is admitted here too,\n // but its ordered-choice-commit is only sound in the trailing-once position \u2014\n // `seqIsUnambiguous` (\u00A78h) enforces that positional constraint; a non-trailing\n // or optional/repeated non-disjoint-alt group is rejected there.\n if (shape.kind === 'alt') return true\n return false\n}\n\n/**\n * The set of first chars a NON-EMPTY match of `shape` could start with, or\n * `'any'` if that can't be pinned down to a single char-set (used to decide\n * whether `alt` arms have disjoint first-sets). A shape that can match empty\n * degrades to `'any'`: since it could succeed with zero chars regardless of\n * what follows, it would \"accept\" any first char by never actually needing one\n * \u2014 treating that as disjoint from anything else would be unsound.\n */\nfunction shapeFirstAccept(shape: ScanShape): CharSet | 'any' {\n if (shapeCanBeEmpty(shape)) return 'any'\n switch (shape.kind) {\n case 'chars': return { ranges: shape.ranges, negated: false }\n case 'ident': return { ranges: shape.head, negated: false }\n case 'until': return { ranges: [[shape.open[0]!, shape.open[0]!]], negated: false }\n case 'delimited': return { ranges: [[shape.open[0]!, shape.open[0]!]], negated: false }\n case 'string': return { ranges: [[shape.quote, shape.quote]], negated: false }\n case 'litFold': {\n const cp = shape.open[0]!\n if (cp >= 65 && cp <= 90) return { ranges: [[cp, cp], [cp + 32, cp + 32]], negated: false }\n if (cp >= 97 && cp <= 122) return { ranges: [[cp, cp], [cp - 32, cp - 32]], negated: false }\n return { ranges: [[cp, cp]], negated: false }\n }\n case 'seq': return seqFirstAccept(shape.parts)\n case 'lookahead': return shapeFirstAccept(shape.inner) // zero-width \u2014 doesn't change first-char acceptance\n case 'alt': {\n // The set of first chars is the union of every arm's own first-set,\n // independent of whether the arms are mutually disjoint (that flag only\n // governs dispatch, not what the shape can start with). Any arm whose\n // first-set is itself `'any'` (stored as `null`) makes the union `'any'`.\n const sets = shape.firsts.map((f): CharSet | 'any' => (f === null ? 'any' : f))\n return unionCharSets(sets)\n }\n }\n}\n\n/** True iff every pair of `sets` is provably disjoint; `'any'` is disjoint from nothing. */\nfunction allPairsDisjoint(sets: Array<CharSet | 'any'>): boolean {\n for (let i = 0; i < sets.length; i++) {\n const a = sets[i]!\n if (a === 'any') return false\n for (let j = i + 1; j < sets.length; j++) {\n const b = sets[j]!\n if (b === 'any' || !classDisjoint(a, b)) return false\n }\n }\n return true\n}\n\n/**\n * Recognize top-level alternation `A|B|C` (\u00A78e): split on `|` outside any\n * `[]`/`()`, lower each arm independently (an arm may itself be a `seq`,\n * `chars`, `lookahead`, \u2026, or even nested alternation via one redundant\n * `(?:\u2026)` wrapper \u2014 see `unwrapOuterGroup`). Declines (null) unless EVERY arm\n * independently lowers \u2014 a single unsupported arm (e.g. one with a nested\n * capturing/non-capturing group, \u00A78f) falls the whole alternation back to\n * `RegExp.exec` rather than partially lowering.\n */\nfunction parseAlternation(source: string): ScanShape | null {\n const parts = splitTopLevelAlternation(source)\n if (!parts) return null\n const arms: ScanShape[] = []\n for (const p of parts) {\n const arm = parseScanShape(p)\n if (!arm) return null\n arms.push(arm)\n }\n const firstSets = arms.map(shapeFirstAccept)\n const disjoint = allPairsDisjoint(firstSets)\n return { kind: 'alt', arms, disjoint, firsts: firstSets.map(f => (f === 'any' ? null : f)) }\n}\n\n/**\n * Recognize one scannable arm from its regex source, or null if it isn't one of\n * the structural shapes. Top-level alternation (\u00A78e) is tried FIRST \u2014 `|` has\n * the lowest precedence in regex, so splitting on it happens before anything\n * else, with each arm then recursively re-entering this same function (so an\n * arm's own trailing lookahead, or its own nested `(?:\u2026)`-wrapped alternation,\n * is still recognized). Failing that: char-class run, ident, string, then\n * open-until-terminator, delimited, and finally the general linear `seq` chain.\n * A trailing `(?!class)`/`(?=class)` is peeled off next (\u00A78b) and re-wraps\n * whatever shape the remaining base recognizes as.\n */\nexport function parseScanShape(source: string): ScanShape | null {\n const alt = parseAlternation(source)\n if (alt) return alt\n const la = stripTrailingLookahead(source)\n if (la) {\n const inner = parseScanShapeCore(la.base)\n if (!inner) return null\n const operand: CharSet = { ranges: la.ranges, negated: la.classNegated }\n if (!lookaheadUnambiguous(inner, operand, la.negative)) return null\n return { kind: 'lookahead', inner, ranges: la.ranges, classNegated: la.classNegated, negative: la.negative }\n }\n return parseScanShapeCore(source)\n}\n\n/**\n * Split a regex fragment on top-level `|` (outside `[]`/`()`), escape-aware.\n * Unlike `splitTopLevelAlternation` this allows a SINGLE arm (no `|` at all) and\n * does not unwrap an outer group \u2014 it's used to enumerate the arms of a\n * `delimited` body, which may legitimately be one bare arm (`[^x]`). Returns\n * null on unbalanced brackets/parens.\n */\nfunction splitDelimArms(body: string): string[] | null {\n const arms: string[] = []\n let depth = 0\n let last = 0\n let i = 0\n while (i < body.length) {\n const ch = body[i]\n if (ch === '\\\\') { i += 2; continue }\n if (ch === '[') {\n let j = i + 1\n if (body[j] === '^') j++\n while (j < body.length && body[j] !== ']') {\n if (body[j] === '\\\\') j += 2\n else j++\n }\n if (body[j] !== ']') return null\n i = j + 1\n continue\n }\n if (ch === '(') { depth++; i++; continue }\n if (ch === ')') { depth--; if (depth < 0) return null; i++; continue }\n if (ch === '|' && depth === 0) { arms.push(body.slice(last, i)); last = i + 1 }\n i++\n }\n if (depth !== 0) return null\n arms.push(body.slice(last))\n return arms\n}\n\n/**\n * Prove that the `delimited` lowering \u2014 \"scan the raw input to the first full\n * `close` literal, ignoring the body entirely\" \u2014 is EQUIVALENT to the real regex\n * `<open>(?:body)*<close>`. That equivalence holds iff `(?:body)*` can never\n * match a string that contains the `close` literal: then greedy repetition stops\n * exactly before the first `close`, which is where the scan lands too.\n *\n * We only PROVE this for the block-comment idiom (the sole shape that expresses\n * \"any char that doesn't form the close\"):\n *\n * n === 1 (`close` is one char l0): body must be exactly `[^l0]`.\n * n === 2 (`close` is `l0 l1`): body must be exactly `[^l0] | l0(?!l1)`.\n *\n * In both the bulk arm `[^l0]` consumes every char except l0, and the guard arm\n * (n === 2) consumes l0 only when it is NOT followed by l1 \u2014 so the body admits\n * l0 but never the full `l0 l1`, and rejects NOTHING else (a body that excluded\n * some non-close char would stop early while the raw scan skipped ahead to the\n * next close, diverging). Anything outside this template \u2014 an alternation whose\n * arms can consume the close (`z(?:a|[0-2]+)*a`), a bulk class narrower/wider\n * than `[^l0]`, a longer close, extra arms \u2014 is declined, falling through to\n * `RegExp.exec`. Sound-by-construction: we only ever return true for a body we\n * can prove never contains `close`.\n */\nfunction delimitedBodySound(body: string, close: number[]): boolean {\n const l0 = close[0]!\n if (close.length > 2) return false\n const arms = splitDelimArms(body)\n if (!arms) return false\n let bulkSeen = false\n let guardSeen = false\n for (const arm of arms) {\n // Bulk arm: a bare negated class `[^l0]` \u2014 excluded set is exactly {l0}.\n const bulk = /^\\[\\^((?:\\\\.|[^\\]])+)\\]$/.exec(arm)\n if (bulk) {\n if (bulkSeen) return false\n const ranges = parseClassRanges(bulk[1]!)\n if (!ranges || ranges.length !== 1 || ranges[0]![0] !== l0 || ranges[0]![1] !== l0) return false\n bulkSeen = true\n continue\n }\n // Guard arm (n === 2 only): `l0(?!l1)` \u2014 consume l0 unless it starts the close.\n if (close.length === 2 && arm.endsWith(')')) {\n const idx = arm.indexOf('(?!')\n if (idx === -1) return false\n const prefix = literalCodePoints(arm.slice(0, idx))\n const op = parseClassOperand(arm.slice(idx + 3, -1))\n const prefixIsL0 = prefix?.length === 1 && prefix[0] === l0\n const forbidsL1 = op && !op.negated && op.ranges.length === 1 &&\n op.ranges[0]![0] === close[1] && op.ranges[0]![1] === close[1]\n if (!prefixIsL0 || !forbidsL1 || guardSeen) return false\n guardSeen = true\n continue\n }\n return false\n }\n // Require the exact arm set: bulk always; guard iff the close is two chars.\n return bulkSeen && guardSeen === (close.length === 2)\n}\n\n/**\n * Structurally recognize `<openLit>(?:<body>)*<closeLit>` \u2014 literal opener, a\n * single non-capturing `*`-repeated group, literal closer \u2014 and lower it to a\n * `delimited` scan ONLY when `delimitedBodySound` proves the body can't contain\n * the close. Returns null (\u2192 fall through to `seq`/`RegExp.exec`) otherwise.\n */\nfunction parseDelimited(source: string): ScanShape | null {\n // An escape-aware body (a literal `\\\\` in the source \u21D2 string-like) is handled\n // by `parseStringShape`; here \"scan to first close\" would wrongly stop at an\n // escaped delimiter, so decline.\n if (source.includes('\\\\\\\\')) return null\n // Opener: literal chars up to the first unescaped `(`, which must open `(?:`.\n let i = 0\n while (i < source.length && source[i] !== '(') {\n i += source[i] === '\\\\' ? 2 : 1\n }\n if (source[i] !== '(' || source[i + 1] !== '?' || source[i + 2] !== ':') return null\n const openFrag = source.slice(0, i)\n // Balanced scan of the group body to its matching `)`.\n let k = i + 3\n let depth = 1\n while (k < source.length && depth > 0) {\n const ch = source[k]\n if (ch === '\\\\') { k += 2; continue }\n if (ch === '[') {\n let j = k + 1\n if (source[j] === '^') j++\n while (j < source.length && source[j] !== ']') {\n if (source[j] === '\\\\') j += 2\n else j++\n }\n if (source[j] !== ']') return null\n k = j + 1\n continue\n }\n if (ch === '(') depth++\n else if (ch === ')') depth--\n k++\n }\n if (depth !== 0) return null\n const body = source.slice(i + 3, k - 1)\n if (source[k] !== '*') return null\n const closeFrag = source.slice(k + 1)\n const open = literalCodePoints(openFrag)\n const close = literalCodePoints(closeFrag)\n if (!open || !close || !delimitedBodySound(body, close)) return null\n return { kind: 'delimited', open, close }\n}\n\nfunction parseScanShapeCore(source: string): ScanShape | null {\n // [X]+ / [X]* \u2014 a positive char-class run. A negated `[^X]+`/`[^X]*` is left to\n // the general `seq` path (which lowers `[^X]+` and declines zero-width `[^X]*`).\n let m = /^\\[((?:\\\\.|[^\\]])+)\\]([+*])$/.exec(source)\n if (m && !m[1]!.startsWith('^')) {\n const ranges = parseClassRanges(m[1]!)\n return ranges ? { kind: 'chars', ranges, minOne: m[2] === '+' } : null\n }\n // \\d+ / \\w+ / \\s+ / \\d* / \\w* / \\s* \u2014 a bare shorthand-class run.\n m = /^\\\\([dws])([+*])$/.exec(source)\n if (m) {\n return { kind: 'chars', ranges: shorthandRanges(m[1] as 'd' | 'w' | 's'), minOne: m[2] === '+' }\n }\n // <head><tail>* \u2014 identifier run: one head char, then a run of tail chars.\n m = /^(\\[(?:\\\\.|[^\\]])+\\]|\\\\[dws])(\\[(?:\\\\.|[^\\]])+\\]|\\\\[dws])\\*$/.exec(source)\n if (m) {\n const head = classToRanges(m[1]!)\n const tail = classToRanges(m[2]!)\n if (head && tail) return { kind: 'ident', head, tail }\n }\n // <q>(?:[^q\\\\]|\\\\.)*<q> \u2014 a quote-delimited string with escapes.\n const str = parseStringShape(source)\n if (str) return str\n // <lit>[^X]* \u2014 consume a literal opener, then run until a terminator char.\n m = /^(.*?)\\[\\^((?:\\\\.|[^\\]])+)\\]\\*$/.exec(source)\n if (m) {\n const open = literalCodePoints(m[1]!)\n const stop = parseClassRanges(m[2]!)\n if (open && stop) return { kind: 'until', open, stop }\n }\n // <open>(?:\u2026)*<close> \u2014 delimited token scanned to its first close literal.\n // Only lowered when the body provably can't contain the close (see\n // `parseDelimited` / `delimitedBodySound`); any other `X(?:\u2026)*Y` declines here\n // and falls through to the `seq` path / `RegExp.exec`.\n const delim = parseDelimited(source)\n if (delim) return delim\n // General category: a linear chain of literals + char runs (optional prefixes,\n // literal openers, negated runs), lowered only when unambiguously greedy.\n const parts = parseSeqParts(source)\n if (parts && seqIsUnambiguous(parts)) return { kind: 'seq', parts }\n return null\n}\n\n/**\n * Flag-aware wrapper around `parseScanShape`. Lowering to a raw code-point scan\n * assumes default regex semantics, so any flag that changes matching (`i` case\n * folding, `u` surrogate handling, `m`/`s` anchor/dot behavior) disables it.\n * `g`/`y` are stickiness-only and safe.\n */\nexport function scanShapeFromRegex(source: string, flags: string): ScanShape | null {\n if (/[msu]/.test(flags)) return null\n if (/i/.test(flags)) return caseFoldShape(source)\n return parseScanShape(source)\n}\n\n/**\n * Case-insensitive (`/i`) scannable shapes. Beyond a pure case-fold literal\n * (`litFold`), this recognizes a top-level alternation of literals (an ordered\n * `alt` of litFolds \u2014 CSS `(?:and|or)`, `(?:media|container|supports)`), each\n * optionally wrapped in a trailing lookahead boundary (`(?![-\\w])`, `(?=\\()`).\n * That last form is the regex spelling of `makeWord` \u2014 a keyword plus a word\n * boundary \u2014 the common CSS at-rule/keyword dispatcher. Anything else declines\n * (\u2192 `RegExp.exec`); a false read here can only fail to lower, never mis-lower.\n */\nfunction caseFoldShape(source: string): ScanShape | null {\n const la = stripTrailingLookahead(source)\n if (la) {\n const inner = caseFoldLiteralOrAlt(la.base)\n if (!inner) return null\n const operand: CharSet = { ranges: la.ranges, negated: la.classNegated }\n if (!lookaheadUnambiguous(inner, operand, la.negative)) return null\n return { kind: 'lookahead', inner, ranges: la.ranges, classNegated: la.classNegated, negative: la.negative }\n }\n return caseFoldLiteralOrAlt(source)\n}\n\n/** A case-fold literal, or a top-level `a|b|c` alternation of pure literals. */\nfunction caseFoldLiteralOrAlt(source: string): ScanShape | null {\n // `foldEq` only folds ASCII letters (65\u201390 / 97\u2013122). A non-ASCII code point\n // with a Unicode case pair (\u00E9/\u00C9, \u00E4/\u00C4, \u2026) would be emitted as an exact compare\n // and silently miss its alternate case \u2014 a mis-lower, not just a failed lower.\n // Decline those so they fall back to `RegExp.exec` (correct Unicode folding),\n // preserving this file's \"a false read can only fail to lower, never mis-lower\".\n const asciiOnly = (cps: number[]): boolean => cps.every(cp => cp <= 127)\n\n const open = literalCodePoints(source)\n if (open) return asciiOnly(open) ? { kind: 'litFold', open } : null\n // `splitTopLevelAlternation` unwraps the outer group itself, so no pre-strip.\n const arms = splitTopLevelAlternation(source)\n if (!arms || arms.length < 2) return null\n const litArms: ScanShape[] = []\n for (const arm of arms) {\n const cp = literalCodePoints(arm)\n if (!cp || !asciiOnly(cp)) return null\n litArms.push({ kind: 'litFold', open: cp })\n }\n // Ordered choice: each litFold arm case-folds on its own, so skipping the\n // first-char dispatch guard (disjoint:false, firsts:null) is always correct \u2014\n // it just tries arms in order, exactly like regex `|`.\n return { kind: 'alt', arms: litArms, disjoint: false, firsts: litArms.map(() => null) }\n}\n\nexport const classCond = (cVar: string, ranges: Array<[number, number]>): string =>\n ranges\n .map(([lo, hi]) => (lo === hi ? `${cVar} === ${lo}` : `(${cVar} >= ${lo} && ${cVar} <= ${hi})`))\n .join(' || ')\n\n/** Literal-match condition at `base + k` for each code point; uses `firstVar` at offset 0. */\nexport const litCond = (base: string, cps: number[], firstVar?: string): string =>\n cps\n .map((cp, k) =>\n k === 0 && firstVar\n ? `${firstVar} === ${cp}`\n : `input.charCodeAt(${base}${k ? ` + ${k}` : ''}) === ${cp}`,\n )\n .join(' && ')\n\n/** Line-terminator code points `.` does not match (`\\n \\r \\u2028 \\u2029`). */\nexport const LINE_TERMINATORS = [10, 13, 8232, 8233] as const\n\n/** The body-stop chars that abort a string match (excluded set minus quote/backslash). */\nexport function stringHardStop(\n shape: Extract<ScanShape, { kind: 'string' }>,\n): Array<[number, number]> {\n return shape.excluded.filter(\n ([lo, hi]) => !(lo === hi && (lo === shape.quote || lo === BACKSLASH)),\n )\n}\n\n/** Mints a fresh, unique local variable name (`prefix` + counter). */\nexport type Mint = (prefix?: string) => string\n\n/**\n * The SINGLE source of truth for how a scannable shape matches at `start`. Both\n * the terminal emitter and the trivia scan loop consume this, so no context can\n * silently reinterpret an incomplete match (e.g. an unterminated string):\n *\n * - `setup` statements (indented by `ind`) that compute the match.\n * - `ok` a boolean expr: did a token match at `start`? (zero-width for\n * `chars*` counts as a match \u2014 terminals allow it.)\n * - `end` the position AFTER the token. **Invariant:** `end === start`\n * whenever there is no progress (match failed or matched empty),\n * so the trivia loop can gate purely on `end > start`.\n *\n * `firstChar`, when supplied, is an expression already equal to\n * `charCodeAt(start)` (the trivia loop reads it once and shares it).\n */\nexport type ShapeMatch = { setup: string[]; ok: string; end: string }\n\nconst codeAt = (start: string, firstChar?: string): string =>\n firstChar ?? `input.charCodeAt(${start})`\n\n// Dispatch-planning thresholds for a disjoint alt's first-char `switch` \u2014 same\n// tuning as codegen's `planDisjointDispatch` (kept local to avoid a scannable-run\n// \u2192 codegen import cycle): only worth a jump table when every arm keys off a few\n// DISCRETE points; a wide range (`[a-z]+`) would explode into dozens of cases.\nconst ALT_SWITCH_RANGE_LIMIT = 4\nconst ALT_SWITCH_MAX_CASES = 48\nconst ALT_SWITCH_MIN_CASES = 3\n\n/**\n * A `switch` jump table for a *disjoint* alt when every arm's first-set is a few\n * discrete code points (operator/keyword first chars); else `{ kind: 'if' }` to\n * keep the range-comparison `if/else if` chain. Arms are pairwise-disjoint, so\n * each code point maps to exactly one arm.\n */\nfunction planAltSwitch(firsts: Array<CharSet | null>): { kind: 'switch'; cases: number[][] } | { kind: 'if' } {\n const cases: number[][] = []\n let total = 0\n for (const fs of firsts) {\n if (!fs || fs.negated) return { kind: 'if' } // any / negated \u2192 no discrete keys\n const pts: number[] = []\n for (const [lo, hi] of fs.ranges) {\n if (hi - lo + 1 > ALT_SWITCH_RANGE_LIMIT) return { kind: 'if' }\n for (let cp = lo; cp <= hi; cp++) pts.push(cp)\n }\n total += pts.length\n if (total > ALT_SWITCH_MAX_CASES) return { kind: 'if' }\n cases.push(pts)\n }\n return total >= ALT_SWITCH_MIN_CASES ? { kind: 'switch', cases } : { kind: 'if' }\n}\n\nexport function emitShapeMatch(\n shape: ScanShape,\n start: string,\n mint: Mint,\n ind: string,\n firstChar?: string,\n): ShapeMatch {\n if (shape.kind === 'lookahead') {\n const inner = emitShapeMatch(shape.inner, start, mint, ind, firstChar)\n const okV = mint('_ok')\n const endV = mint('_end')\n // `charInClass` folds the operand's OWN `[^\u2026]` negation in; `guardFails`\n // then applies the lookahead's `!`/`=` polarity on top. Zero-width: `end`\n // is always `inner.end` (on success) or `start` (on failure) \u2014 never past\n // where `inner` itself stopped.\n const rawIn = classCond(`input.charCodeAt(${inner.end})`, shape.ranges)\n const charInClass = shape.classNegated ? `!(${rawIn})` : `(${rawIn})`\n const hasMatchingChar = `${inner.end} < input.length && ${charInClass}`\n const guardFails = shape.negative ? hasMatchingChar : `!(${hasMatchingChar})`\n return {\n setup: [\n ...inner.setup,\n `${ind}let ${okV} = ${inner.ok}`,\n `${ind}let ${endV} = ${inner.end}`,\n `${ind}if (${okV} && (${guardFails})) { ${okV} = false; ${endV} = ${start} }`,\n ],\n ok: okV,\n end: endV,\n }\n }\n\n if (shape.kind === 'alt') {\n const okV = mint('_ok')\n const endV = mint('_end')\n const lines: string[] = [`${ind}let ${okV} = false`, `${ind}let ${endV} = ${start}`]\n // Fast path: an alternation of case-fold literals (CSS keyword lists like\n // `@media|@container|@supports`, `even|odd`) \u2192 a `switch` on the first char\n // that jumps straight to the matching group, tails compared with the bit-OR\n // fold. Measured ~2.4x over the ordered if-chain / ~1.4x over per-arm bit-OR.\n // Ordered-choice is preserved: arms are grouped by their FOLDED first code\n // point and, within a group, emitted in source order (first match wins);\n // arms in different groups can't both match a given first char.\n if (shape.arms.length >= 2 && shape.arms.every(a => a.kind === 'litFold' && a.open.length >= 1)) {\n const groups = new Map<number, number[][]>()\n for (const a of shape.arms) {\n const open = (a as Extract<ScanShape, { kind: 'litFold' }>).open\n const f = open[0]!\n const lower = f >= 65 && f <= 90 ? f + 32 : f\n ;(groups.get(lower) ?? groups.set(lower, []).get(lower)!).push(open)\n }\n lines.push(`${ind}if (${start} < input.length) switch (${codeAt(start, firstChar)}) {`)\n for (const [lower, arms] of groups) {\n const labels = lower >= 97 && lower <= 122 ? `case ${lower}: case ${lower - 32}:` : `case ${lower}:`\n lines.push(`${ind} ${labels} {`)\n for (const open of arms) {\n const tail = open.slice(1).map((cp, i) => foldEq(`input.charCodeAt(${start} + ${i + 1})`, cp))\n const cond = tail.length ? ` && ${tail.join(' && ')}` : ''\n lines.push(`${ind} if (${start} + ${open.length} <= input.length${cond}) { ${okV} = true; ${endV} = ${start} + ${open.length}; break }`)\n }\n lines.push(`${ind} break`)\n lines.push(`${ind} }`)\n }\n lines.push(`${ind}}`)\n return { setup: lines, ok: okV, end: endV }\n }\n if (shape.disjoint) {\n // Mutually exclusive first-sets: dispatch straight to the one matching\n // arm, no ordering/backtracking concerns (\u00A77b-style disjoint dispatch). A\n // `switch` jump table when arms key off discrete first chars, else the\n // range-comparison `if/else if` chain (same plan as codegen's choice()).\n const plan = planAltSwitch(shape.firsts)\n if (plan.kind === 'switch') {\n lines.push(`${ind}if (${start} < input.length) switch (${codeAt(start, firstChar)}) {`)\n shape.arms.forEach((arm, k) => {\n const labels = plan.cases[k]!.map(cp => `case ${cp}:`).join(' ')\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(`${ind} ${labels} {`)\n lines.push(...m.setup)\n lines.push(`${ind} ${okV} = ${m.ok}`)\n lines.push(`${ind} ${endV} = ${m.ok} ? ${m.end} : ${start}`)\n lines.push(`${ind} break`)\n lines.push(`${ind} }`)\n })\n lines.push(`${ind}}`)\n } else {\n const c0 = codeAt(start, firstChar)\n shape.arms.forEach((arm, k) => {\n const fs = shape.firsts[k]! // non-null whenever `disjoint` is true\n const cond = fs.negated ? `!(${classCond(c0, fs.ranges)})` : `(${classCond(c0, fs.ranges)})`\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(`${ind}${k === 0 ? 'if' : 'else if'} (${start} < input.length && ${cond}) {`)\n lines.push(...m.setup)\n lines.push(`${ind} ${okV} = ${m.ok}`)\n lines.push(`${ind} ${endV} = ${m.ok} ? ${m.end} : ${start}`)\n lines.push(`${ind}}`)\n })\n }\n } else {\n // Overlapping first-sets: ordered choice \u2014 try each arm in turn and take\n // the first that succeeds. This is exactly regex `|`'s own semantics:\n // first alternative to match AT ALL wins on ITS OWN greedy length, never\n // compared against a later alternative's (possibly longer) match.\n const lbl = mint('_alt')\n lines.push(`${ind}${lbl}: {`)\n for (const arm of shape.arms) {\n const m = emitShapeMatch(arm, start, mint, `${ind} `, firstChar)\n lines.push(...m.setup)\n lines.push(`${ind} if (${m.ok}) { ${okV} = true; ${endV} = ${m.end}; break ${lbl} }`)\n }\n lines.push(`${ind}}`)\n }\n return { setup: lines, ok: okV, end: endV }\n }\n\n if (shape.kind === 'chars') {\n const cur = mint('_e')\n return {\n setup: [\n `${ind}let ${cur} = ${start}`,\n `${ind}while (${cur} < input.length && (${classCond(`input.charCodeAt(${cur})`, shape.ranges)})) ${cur}++`,\n ],\n // `*` always matches (possibly empty); `+` needs at least one char.\n ok: shape.minOne ? `${cur} > ${start}` : 'true',\n end: cur,\n }\n }\n\n if (shape.kind === 'ident') {\n const cur = mint('_e')\n return {\n setup: [\n `${ind}let ${cur} = ${start}`,\n `${ind}if (${start} < input.length && (${classCond(codeAt(start, firstChar), shape.head)})) {`,\n `${ind} ${cur} = ${start} + 1`,\n `${ind} while (${cur} < input.length && (${classCond(`input.charCodeAt(${cur})`, shape.tail)})) ${cur}++`,\n `${ind}}`,\n ],\n ok: `${cur} > ${start}`,\n end: cur,\n }\n }\n\n if (shape.kind === 'seq') {\n const endV = mint('_e')\n const okV = mint('_ok')\n const runCond = (p: Extract<SeqPart, { part: 'run' }>, at: string) => {\n const c = classCond(`input.charCodeAt(${at})`, p.ranges)\n return p.negated ? `!(${c})` : `(${c})`\n }\n const lines = [`${ind}let ${endV} = ${start}`, `${ind}let ${okV} = false`, `${ind}do {`]\n let first = true\n for (const p of shape.parts) {\n if (p.part === 'lit') {\n const L = p.cps.length\n const cond = litCond(endV, p.cps, first ? firstChar : undefined)\n if (p.optional) {\n lines.push(`${ind} if (${endV} + ${L} <= input.length && (${cond})) ${endV} += ${L}`)\n } else {\n lines.push(`${ind} if (!(${endV} + ${L} <= input.length && (${cond}))) break`)\n lines.push(`${ind} ${endV} += ${L}`)\n }\n } else if (p.part === 'run' && p.max === Infinity) {\n // Unbounded run (`+`/`*`/`{n,}`): greedy scan, then enforce the minimum.\n const s = mint('_s')\n lines.push(`${ind} const ${s} = ${endV}`)\n lines.push(`${ind} while (${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n if (p.min === 1) lines.push(`${ind} if (${endV} === ${s}) break`)\n else if (p.min > 1) lines.push(`${ind} if (${endV} - ${s} < ${p.min}) break`)\n } else if (p.part === 'run' && p.max === 1) {\n // Exactly one char: required (min 1) or optional (min 0).\n if (p.min === 1) {\n lines.push(`${ind} if (!(${endV} < input.length && ${runCond(p, endV)})) break`)\n lines.push(`${ind} ${endV}++`)\n } else {\n lines.push(`${ind} if (${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n }\n } else if (p.part === 'run') {\n // Bounded counted run `[x]{min,max}` (\u00A78c), max \u2265 2: consume up to `max`\n // occurrences, then require at least `min` (greedy \u2014 sound because\n // `seqIsUnambiguous` proved any wiggle is disjoint from what follows).\n const s = mint('_s')\n lines.push(`${ind} const ${s} = ${endV}`)\n lines.push(`${ind} while (${endV} - ${s} < ${p.max} && ${endV} < input.length && ${runCond(p, endV)}) ${endV}++`)\n if (p.min > 0) lines.push(`${ind} if (${endV} - ${s} < ${p.min}) break`)\n } else {\n // `group` (\u00A78f): the group's own body is just another `ScanShape` \u2014\n // recurse via `emitShapeMatch`, then splice its setup in as the body\n // of a required-once check, an optional take, or a repeat loop.\n const inner = emitShapeMatch(p.inner, endV, mint, `${ind} `, first ? firstChar : undefined)\n if (!p.unbounded) {\n lines.push(...inner.setup)\n if (p.min === 1) {\n lines.push(`${ind} if (!(${inner.ok})) break`)\n lines.push(`${ind} ${endV} = ${inner.end}`)\n } else {\n lines.push(`${ind} if (${inner.ok}) ${endV} = ${inner.end}`)\n }\n } else {\n const progV = mint('_prog')\n lines.push(`${ind} let ${progV} = false`)\n lines.push(`${ind} while (true) {`)\n lines.push(...inner.setup)\n lines.push(`${ind} if (!(${inner.ok})) break`)\n lines.push(`${ind} ${endV} = ${inner.end}`)\n lines.push(`${ind} ${progV} = true`)\n lines.push(`${ind} }`)\n if (p.min === 1) lines.push(`${ind} if (!${progV}) break`)\n }\n }\n first = false\n }\n lines.push(`${ind} ${okV} = true`)\n lines.push(`${ind}} while (false)`)\n lines.push(`${ind}if (!${okV}) ${endV} = ${start}`)\n return { setup: lines, ok: okV, end: endV }\n }\n\n if (shape.kind === 'litFold') {\n const openLen = shape.open.length\n const endV = mint('_end')\n const checks = shape.open.map((cp, k) => {\n const cVar = k === 0 && firstChar ? firstChar : `input.charCodeAt(${start} + ${k})`\n return foldEq(cVar, cp)\n })\n return {\n setup: [\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${checks.join(' && ')})) ${endV} = ${start} + ${openLen}`,\n ],\n ok: `${endV} > ${start}`,\n end: endV,\n }\n }\n\n if (shape.kind === 'until') {\n const j = mint('_j')\n const openLen = shape.open.length\n const openChk = openLen === 1\n ? `${codeAt(start, firstChar)} === ${shape.open[0]}`\n : litCond(start, shape.open, firstChar)\n return {\n setup: [\n `${ind}let ${j} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${openChk})) {`,\n `${ind} ${j} = ${start} + ${openLen}`,\n `${ind} while (${j} < input.length && !(${classCond(`input.charCodeAt(${j})`, shape.stop)})) ${j}++`,\n `${ind}}`,\n ],\n // An open-until-terminator always completes (stop char or EOF), so a matched\n // open literal is a full match; `end === start` iff the open didn't match.\n ok: `${j} > ${start}`,\n end: j,\n }\n }\n\n if (shape.kind === 'string') {\n const okV = mint('_ok')\n const endV = mint('_end')\n const i = mint('_i')\n const c2 = mint('_c')\n const hard = stringHardStop(shape)\n const ltLines = shape.escLineTerm\n ? []\n : (() => {\n const c3 = mint('_c')\n return [\n `${ind} const ${c3} = input.charCodeAt(${i} + 1)`,\n `${ind} if (${LINE_TERMINATORS.map(t => `${c3} === ${t}`).join(' || ')}) break`,\n ]\n })()\n return {\n setup: [\n `${ind}let ${okV} = false`,\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} < input.length && (${codeAt(start, firstChar)}) === ${shape.quote}) {`,\n `${ind} let ${i} = ${start} + 1`,\n `${ind} while (${i} < input.length) {`,\n `${ind} const ${c2} = input.charCodeAt(${i})`,\n `${ind} if (${c2} === ${shape.quote}) { ${okV} = true; ${endV} = ${i} + 1; break }`,\n `${ind} if (${c2} === ${BACKSLASH}) {`,\n `${ind} if (${i} + 1 >= input.length) break`,\n ...ltLines,\n `${ind} ${i} += 2`,\n `${ind} continue`,\n `${ind} }`,\n ...(hard.length ? [`${ind} if (${classCond(c2, hard)}) break`] : []),\n `${ind} ${i}++`,\n `${ind} }`,\n `${ind}}`,\n ],\n ok: okV,\n end: endV,\n }\n }\n\n // delimited: <open>\u2026<close>, requires the close literal (unclosed \u21D2 no match).\n const j = mint('_j')\n const endV = mint('_end')\n const openLen = shape.open.length\n const closeLen = shape.close.length\n const openChk = openLen === 1\n ? `${codeAt(start, firstChar)} === ${shape.open[0]}`\n : litCond(start, shape.open, firstChar)\n const closeChk = litCond(j, shape.close)\n return {\n setup: [\n `${ind}let ${j} = ${start}`,\n `${ind}let ${endV} = ${start}`,\n `${ind}if (${start} + ${openLen} <= input.length && (${openChk})) {`,\n `${ind} ${j} = ${start} + ${openLen}`,\n `${ind} while (${j} + ${closeLen - 1} < input.length && !(${closeChk})) ${j}++`,\n `${ind} if (${j} + ${closeLen} <= input.length && (${closeChk})) ${endV} = ${j} + ${closeLen}`,\n `${ind}}`,\n ],\n ok: `${endV} > ${start}`,\n end: endV,\n }\n}\n\n/**\n * One trivia-loop branch for a shape, dispatched on the current char `c`\n * (= charCodeAt(_e)). Advances `_e` and `continue`s ONLY on real progress\n * (`end > _e`) \u2014 an unterminated delimited/string token leaves `end === _e`, so\n * the loop stops there exactly as the interpreter's `oneOrMore(choice(\u2026))` would.\n */\nexport function scanBranch(shape: ScanShape, mint: Mint): string {\n const m = emitShapeMatch(shape, '_e', mint, ' ', 'c')\n return [...m.setup, ` if (${m.end} > _e) { _e = ${m.end}; continue }`].join('\\n')\n}\n\n/**\n * A labeled branch: match one token and, on progress, log its [start, end,\n * kindIndex] trivia chunk. Same completion semantics as scanBranch.\n */\nexport function scanBranchLabeled(shape: ScanShape, kindIndex: number, mint: Mint): string {\n const m = emitShapeMatch(shape, '_e', mint, ' ', 'c')\n return [\n ...m.setup,\n ` if (${m.end} > _e) {`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, ${m.end}, ${kindIndex})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << kindIndex}))) _ctx._cstTriviaLog.push(_e, ${m.end}, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${kindIndex})`,\n ` }`,\n ` _e = ${m.end}`,\n ` continue`,\n ` }`,\n ].join('\\n')\n}\n", "import type { Combinator } from '../types.ts'\nimport { getCoreRegexDef } from '../combinators/choice.ts'\nimport type { LabeledTriviaSpec } from '../cst/trivia-kinds.ts'\nimport { analyzeLabeledTrivia } from '../cst/trivia-kinds.ts'\nimport { type ScanShape, type Mint, scanShapeFromRegex, scanBranch, scanBranchLabeled } from './scannable-run.ts'\n\n/** Fresh, collision-free local names for one generated scan function. */\nfunction makeMint(): Mint {\n let n = 0\n return (prefix = '_v') => `${prefix}${n++}`\n}\n\nfunction unwrapTrivia(p: Combinator<unknown>): Combinator<unknown> {\n let cur = p\n while (cur._def.tag === 'trivia') cur = cur._def.parser\n return cur\n}\n\nfunction oneOrMoreInner(p: Combinator<unknown>): Combinator<unknown> | null {\n const d = p._def\n if (d.tag === 'oneOrMore') return d.parser\n if (d.tag === 'many' && d.min >= 1) return d.parser\n return null\n}\n\nfunction choiceArms(p: Combinator<unknown>): Combinator<unknown>[] | null {\n if (p._def.tag === 'choice') return p._def.parsers\n return null\n}\n\n/** The scannable shape of one arm (a regex), or null if it isn't scannable. */\nfunction armShape(arm: Combinator<unknown>): ScanShape | null {\n const d = getCoreRegexDef(arm)\n return d ? scanShapeFromRegex(d.source, d.flags) : null\n}\n\n/**\n * A trivia parser that lowers to a hand-rolled char-scan loop: `oneOrMore(choice(\n * \u2026))` (or `many`, min\u22651) where EVERY arm is a scannable shape (a `[X]+` run, a\n * `<lit>[^X]*` open-until-terminator, or a `<open>(?:\u2026)*<close>` delimited token\n * \u2014 see scannable-run.ts). Returns the per-arm shapes, or a single-element list\n * for a bare scannable regex, or null (\u2192 caller falls back to the regex/generic\n * trivia path). A single alternation regex is NOT scannable (one exec matches one\n * arm per call, defeating the loop).\n */\nexport function analyzeTriviaFastPath(trivia: Combinator<unknown>): ScanShape[] | null {\n const core = unwrapTrivia(trivia)\n\n const directDef = getCoreRegexDef(core)\n if (directDef) {\n const shape = scanShapeFromRegex(directDef.source, directDef.flags)\n return shape && shape.kind === 'chars' ? [shape] : null\n }\n\n const inner = oneOrMoreInner(core)\n if (!inner) return null\n\n const innerShape = armShape(inner)\n if (innerShape) return innerShape.kind === 'chars' ? [innerShape] : null\n\n const arms = choiceArms(inner)\n if (!arms || arms.length < 2) return null\n\n const shapes: ScanShape[] = []\n for (const arm of arms) {\n const shape = armShape(arm)\n if (!shape) return null\n shapes.push(shape)\n }\n return shapes\n}\n\nconst CAP_RECORD = [\n ` if (_cap && _e > _pos) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_pos, _e)`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia) _ctx._cstTriviaLog.push(_pos, _e, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0)`,\n ` }`,\n].join('\\n')\n\n/** One char-scan loop carrying a branch per scannable shape, dispatched on `c`. */\nfunction composeFastLoop(shapes: ScanShape[]): string {\n const mint = makeMint()\n return [\n ` while (_e < input.length) {`,\n ` const c = input.charCodeAt(_e)`,\n ...shapes.map(s => scanBranch(s, mint)),\n ` break`,\n ` }`,\n ].join('\\n')\n}\n\n/** Emit a specialized `_tfN` that skips (unlabeled) trivia via a char-scan loop. */\nexport function buildFastTriviaFnDecl(fnName: string, shapes: ScanShape[]): string {\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n composeFastLoop(shapes),\n CAP_RECORD,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\n/**\n * A LABELED trivia parser whose every arm is scannable \u2192 per-arm shape + kind\n * index, or null. Same recognition as analyzeTriviaFastPath, but keyed by the\n * labeled arm kinds so the fast loop can log each chunk's kind. Generalizes what\n * used to be a hardcoded ws-run + block-comment special case to any scannable\n * shape set / arm count.\n */\nexport function analyzeLabeledScannableRun(\n trivia: Combinator<unknown>,\n): Array<{ shape: ScanShape; kindIndex: number }> | null {\n const spec = analyzeLabeledTrivia(trivia)\n if (!spec) return null\n const out: Array<{ shape: ScanShape; kindIndex: number }> = []\n for (const arm of spec.arms) {\n const d = getCoreRegexDef(arm.parser)\n const shape = d ? scanShapeFromRegex(d.source, d.flags) : null\n if (!shape) return null\n out.push({ shape, kindIndex: arm.kindIndex })\n }\n return out\n}\n\n/** Emit a labeled scannable trivia `_tfN`: char-scan loop with per-chunk kind capture. */\nexport function buildLabeledScannableTriviaFnDecl(\n fnName: string,\n arms: ReadonlyArray<{ shape: ScanShape; kindIndex: number }>,\n): string {\n const mint = makeMint()\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` const c = input.charCodeAt(_e)`,\n ...arms.map(a => scanBranchLabeled(a.shape, a.kindIndex, mint)),\n ` break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\n/** Labeled trivia with regex arms \u2014 per-chunk kind capture in compiled output. */\nexport function buildLabeledRegexTriviaFnDecl(\n fnName: string,\n spec: LabeledTriviaSpec,\n reNames: string[],\n): string {\n const tryArms = spec.arms.map((arm, i) => {\n const re = reNames[i]!\n const k = arm.kindIndex\n return [\n ` ${re}.lastIndex = _e`,\n ` const _m${i} = ${re}.exec(input)`,\n ` if (_m${i} && _m${i}.index === _e) {`,\n ` const _ce = _e + _m${i}[0].length`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,\n ` }`,\n ` _e = _ce`,\n ` _matched = true`,\n ` continue`,\n ` }`,\n ].join('\\n')\n }).join('\\n')\n\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` let _matched = false`,\n tryArms,\n ` if (!_matched) break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n\nexport function labeledTriviaRegexArms(trivia: Combinator<unknown>): LabeledTriviaSpec | null {\n const spec = analyzeLabeledTrivia(trivia)\n if (!spec) return null\n for (const arm of spec.arms) {\n if (!getCoreRegexDef(arm.parser)) return null\n }\n return spec\n}\n\n/** Labeled trivia via per-arm runtime parsers (non-regex or mixed arms). */\nexport function buildLabeledRuntimeTriviaFnDecl(\n fnName: string,\n spec: LabeledTriviaSpec,\n rpStartIndex: number,\n): string {\n const tryArms = spec.arms.map((arm, i) => {\n const k = arm.kindIndex\n const ri = rpStartIndex + i\n return [\n ` const _r${i} = _rp[${ri}].parse(input, _e, _ctx)`,\n ` if (_r${i}.ok && _r${i}.span.end > _e) {`,\n ` const _ce = _r${i}.span.end`,\n ` if (_cap) {`,\n ` if (_ctx._triviaLog !== undefined) _ctx._triviaLog.push(_e, _ce, ${k})`,\n ` if (_ctx._cstTriviaLog !== undefined && _ctx.captureTrivia && (_ctx._triviaCaptureMask === undefined || (_ctx._triviaCaptureMask & ${1 << k}))) _ctx._cstTriviaLog.push(_e, _ce, _ctx._cstRawChildren ? _ctx._cstRawChildren.length : 0, ${k})`,\n ` }`,\n ` _e = _ce`,\n ` _matched = true`,\n ` continue`,\n ` }`,\n ].join('\\n')\n }).join('\\n')\n\n return [\n `function ${fnName}(input, _pos, _ctx, _cap) {`,\n ` let _e = _pos`,\n ` while (_e < input.length) {`,\n ` let _matched = false`,\n tryArms,\n ` if (!_matched) break`,\n ` }`,\n ` return _e`,\n `}`,\n ].join('\\n')\n}\n", "/**\n * The linker (RULE_ABI_PLAN \u00A74): fuse independently-compiled linkable artifacts\n * (`compileLinkable`) into ONE closure of parse functions.\n *\n * Every rule is a canonical `_r_<Name>` function and every sibling reference is a\n * call to that name; because fusion drops them all into one scope, those calls\n * are **direct local calls** (0% dispatch). Composition is by name:\n * - **override** \u2014 later artifact wins per rule name; because references resolve\n * by name in the shared scope, overriding a rule reroutes EVERY call to it,\n * including calls inside a base artifact's own rules (open recursion).\n * - **\u00E0 la carte** \u2014 `pick(artifact, names)` keeps only those rules + their\n * transitive dependency closure.\n * Private per-artifact state (`_ns_re`, `_ns_pf`, \u2026) is namespaced so it can't\n * collide; the sentinel protocol / `_EMPTY_TL` are shared and\n * emitted once.\n *\n * Uses `new Function` (like `compile()`), so it needs `'unsafe-eval'` under a\n * strict CSP; a build-time variant that emits fused source instead is a later\n * addition. Fusion runs ONCE at parser construction \u2014 parsing is then full speed.\n */\nimport { compileLinkable, firstSetCond, runFusedGatingDiagnostic, HOST_READS_DECL } from './codegen.ts'\nimport { evalRuleMapIR, serializeRuleMap } from './ir-serialize.ts'\nimport type { LinkablePieces, FirstSetRecipe } from './codegen.ts'\nimport { union } from '../combinators/first-set.ts'\nimport { PARSEMAN_VERSION } from '../version.ts'\nimport type { BuildHost, Combinator, CstCollapsePredicate, FirstSet, ParseContext, ParseResult } from '../types.ts'\n\n/**\n * Compile a `rules()` map to a **linkable artifact** \u2014 the composable, shippable\n * form (RULE_ABI_PLAN \u00A74). A package exports `linkable(rules(g => \u2026))`; consumers\n * import that artifact and `fuse([...])` it \u2014 **no source of the base grammar is\n * ever read**. Under the macro this is precompiled to static pieces; in the\n * interpreter it compiles here at load (like `compile()`).\n *\n * `ns` is a per-artifact namespace; omit it to auto-assign a process-unique one\n * (fine at runtime \u2014 the macro supplies a stable module-derived ns instead).\n */\nlet _nsCounter = 0\nexport function linkable(\n rulesMap: Record<string, Combinator<unknown>>,\n ns?: string,\n trivia?: Combinator<unknown>,\n): LinkablePieces {\n const pieces = compileLinkable(Object.entries(rulesMap), ns ?? `_lk${_nsCounter++}_`, trivia ? { trivia } : undefined)\n if (!pieces) throw new Error('linkable(): this grammar cannot be compiled to a linkable artifact (contains a runtime-only parser fallback)')\n return pieces\n}\n\nexport type CstBuildHostOptions = {\n /**\n * Collapse transparent one-child CST wrapper nodes at build time.\n * - `true`: collapse any one-child node whose rawChildren also has exactly one\n * entry, so trivia/error boundaries are not silently dropped.\n * - `string[]`: collapse only these grammar node types.\n * - predicate: final policy hook for language-specific public CSTs.\n */\n collapse?: boolean | readonly string[] | CstCollapsePredicate\n}\n\nfunction normalizeCstCollapse(collapse: CstBuildHostOptions['collapse']): CstCollapsePredicate | undefined {\n if (collapse === true) return () => true\n if (Array.isArray(collapse)) {\n const types = new Set(collapse)\n return type => types.has(type)\n }\n return typeof collapse === 'function' ? collapse : undefined\n}\n\nfunction buildCstNode(\n type: string,\n children: ReadonlyArray<unknown>,\n _fields: unknown,\n span: { start: number; end: number },\n _rawChildren?: ReadonlyArray<unknown>,\n _triviaLog?: readonly number[],\n state?: unknown,\n): unknown {\n // Carry the grammar's `ctx.state` snapshot onto the node (null when unset) \u2014 the\n // CST contract includes `state` and incremental re-parse replays it on edit.\n return { _tag: 'node', type, span: { start: span.start, end: span.end }, state: state ?? null, children: [...children] }\n}\n\n/**\n * A generic positioned-CST build host (RULE_ABI_PLAN \u00A77). Pass as `ctx.build`\n * (or `parseDoc(..., { build: cstBuildHost })`) to make ANY linkable/fused\n * grammar produce a uniform CST \u2014 `{ _tag:'node', type, span, state, children }`\n * \u2014 instead of its own eval-AST builders. This is the host the linter and IDE\n * drivers use; the eval driver leaves `ctx.build` unset (grammar's own builders).\n *\n * For public syntax trees, call `cstBuildHost({ collapse })`: Parseman will skip\n * allocating wrapper CST nodes whose single child should stand in for the rule.\n */\nexport function cstBuildHost(options?: CstBuildHostOptions): BuildHost\nexport function cstBuildHost(\n type: string,\n children: ReadonlyArray<unknown>,\n fields: unknown,\n span: { start: number; end: number },\n rawChildren?: ReadonlyArray<unknown>,\n triviaLog?: readonly number[],\n state?: unknown,\n): unknown\nexport function cstBuildHost(\n typeOrOptions?: string | CstBuildHostOptions,\n children?: ReadonlyArray<unknown>,\n _fields?: unknown,\n span?: { start: number; end: number },\n rawChildren?: ReadonlyArray<unknown>,\n triviaLog?: readonly number[],\n state?: unknown,\n): unknown {\n if (typeof typeOrOptions === 'string') {\n return buildCstNode(typeOrOptions, children ?? [], _fields, span ?? { start: 0, end: 0 }, rawChildren, triviaLog, state)\n }\n const collapse = normalizeCstCollapse(typeOrOptions?.collapse)\n const host: BuildHost = (\n type: string,\n children: ReadonlyArray<unknown> | undefined,\n fields: unknown,\n span: { start: number; end: number },\n rawChildren: ReadonlyArray<unknown>,\n triviaLog: readonly number[],\n state: unknown,\n // A CST/collapse host always keeps `children` (chV) \u2014 the opt-out never\n // applies \u2014 so `?? []` is unreachable defensive modeling for the widened type.\n ) => buildCstNode(type, children ?? [], fields, span, rawChildren, triviaLog, state)\n ;(host as typeof host & { _parsemanCstOutput?: true })._parsemanCstOutput = true\n if (collapse) host._parsemanCstCollapse = collapse\n return host\n}\n\n// `cstBuildHost` itself is also accepted as a BuildHost (without options).\n;(cstBuildHost as unknown as { _parsemanCstOutput?: true })._parsemanCstOutput = true\n\n/**\n * A fused function receives the full ParseContext through `run()`. Direct\n * callers historically supplied a plain context object, so keep that usage\n * valid while making the function assignable to the public `Runnable` type.\n * Generated code treats optional framework fields as absent when they are not\n * provided, matching the interpreter's normal defaults.\n */\nexport type FusedRule = (\n input: string,\n pos: number,\n ctx: ParseContext | Record<string, unknown>,\n) => ParseResult<unknown> & { readonly value?: unknown }\n\n/**\n * Restrict a grammar/artifact to `names` plus their transitive rule-dependency\n * closure (\u00E0 la carte selection) \u2014 e.g. Jess taking parts of Less and parts of\n * Sass: `compose([pick(less, ['MixinCall']), pick(sass, ['EachFor']), css])`. A\n * picked rule pulls in every rule name it references, so the result is always\n * self-consistent within the artifact. Accepts a grammar (`rules()` result) or a\n * compiled artifact; returns an artifact for `compose()`.\n */\nexport function pick(\n grammar: LinkablePieces | Record<string, Combinator<unknown>>,\n names: string[],\n): LinkablePieces {\n // A COMPOSED grammar (`compose([...])` result): it has no single rule map \u2014 its rules\n // live across several carried pieces. Materialize them under the composing trivia it\n // was built with (so the selection keeps composing-wins trivia), then filter across\n // pieces to `names` + their transitive dep closure, keeping each rule in the piece\n // that WINS it (compose override order = last wins). Return a composed-like value so a\n // downstream `compose([pick(composed, \u2026)])` flattens it the same way.\n const carried = (grammar as Record<symbol, unknown>)[COMPOSED_PIECES]\n if (Array.isArray(carried)) {\n const trivia = (grammar as Record<symbol, unknown>)[COMPOSED_TRIVIA] as Combinator<unknown> | undefined\n const pieces = (carried as Array<LinkablePieces | IRPiece>).map(pc => materializePiece(pc, trivia))\n const filtered = pickPieces(pieces, names)\n // A composed-like value (carries COMPOSED_PIECES, no single rule map). It is only\n // ever consumed through compose()'s COMPOSED_PIECES branch \u2014 which is checked before\n // any LinkablePieces field \u2014 so this masquerades as LinkablePieces for the signature.\n const out: Record<string, unknown> = {}\n Object.defineProperty(out, COMPOSED_PIECES, { value: filtered, enumerable: false })\n if (trivia) Object.defineProperty(out, COMPOSED_TRIVIA, { value: trivia, enumerable: false })\n return out as unknown as LinkablePieces\n }\n\n const p = (grammar as LinkablePieces).ruleFns instanceof Map\n ? (grammar as LinkablePieces)\n : linkable(grammar as Record<string, Combinator<unknown>>)\n return pickPieces([p], names)[0] ?? { ...p, keys: [], ruleFns: new Map(), wrappers: new Map(), deps: new Map() }\n}\n\n/** Restrict a set of linkable pieces to `names` + their transitive dep closure,\n * keeping each surviving rule in the piece that WINS it (later piece wins, matching\n * compose override order). Shared by `pick()` (runtime) and the macro's build-time\n * `pick(...)` handling, so \u00E0-la-carte selection is identical on both paths. */\nexport function pickPieces(pieces: LinkablePieces[], names: string[]): LinkablePieces[] {\n const winner = new Map<string, LinkablePieces>()\n for (const pc of pieces) for (const k of pc.keys) winner.set(k, pc)\n // A requested name that isn't in the grammar is a typo \u2014 fail here, not later with a\n // confusing name-closure error at compose() time.\n for (const n of names) {\n if (!winner.has(n)) throw new Error(`pick: rule \"${n}\" is not in this grammar (available: ${[...winner.keys()].join(', ')})`)\n }\n const keep = new Set<string>()\n const visit = (n: string): void => {\n // A missing winner is an EXTERNAL dep (a base-grammar rule) \u2014 it resolves at\n // compose() time, not here. Top-level `names` were already validated above.\n const w = winner.get(n)\n if (keep.has(n) || !w) return\n keep.add(n)\n for (const d of w.deps.get(n) ?? []) visit(d)\n }\n for (const n of names) visit(n)\n const filt = <V>(m: Map<string, V>, pc: LinkablePieces): Map<string, V> =>\n new Map([...m].filter(([k]) => keep.has(k) && winner.get(k) === pc))\n return pieces\n .map(pc => ({ ...pc, keys: pc.keys.filter(k => keep.has(k) && winner.get(k) === pc), ruleFns: filt(pc.ruleFns, pc), wrappers: filt(pc.wrappers, pc), deps: filt(pc.deps, pc) }))\n .filter(pc => pc.keys.length > 0)\n}\n\n/**\n * Build the fused-closure body (shared by the runtime `fuseRules` and the\n * build-time `emitFusedSource`). Returns the closure body (which reads `_env`\n * for any non-inlined callbacks) and the `_env` to bind. In macro mode callbacks\n * are inlined from source, so `_env` is empty and the body is fully static.\n */\nexport function fusedBody(pieces: LinkablePieces[]): { body: string; env: Record<string, unknown> } {\n // ARTIFACT VERSION LOCK (see src/version.ts): a compiled artifact is fused by the\n // SAME parseman version that produced it \u2014 the artifact format carries no\n // cross-version back-compat. Reject BOTH a version MISMATCH and an UNSTAMPED piece\n // (a pre-0.32 artifact predates the invariant \u2192 its recipe/pieces shape is\n // unsupported): fail LOUDLY rather than silently mis-reading a stale shape. Every\n // artifact `compileLinkable` produces is stamped, so an absent stamp means a stale\n // serialized artifact, not a current one.\n for (const p of pieces) {\n if (p.v === undefined) {\n throw new Error(\n `parseman: artifact \"${p.ns}\" is UNSTAMPED (compiled before the version-lock invariant). ` +\n `Recompile the grammar with parseman ${PARSEMAN_VERSION}; parseman does not fuse unversioned or cross-version artifacts.`,\n )\n }\n if (p.v !== PARSEMAN_VERSION) {\n throw new Error(\n `parseman: artifact \"${p.ns}\" was compiled with parseman ${p.v}, but is being fused with parseman ${PARSEMAN_VERSION}. ` +\n `Compiled grammar artifacts are version-locked \u2014 recompile the grammar with parseman ${PARSEMAN_VERSION}; parseman does not fuse across versions.`,\n )\n }\n }\n // Winner per rule name \u2014 later pieces override earlier ones.\n const winner = new Map<string, LinkablePieces>()\n for (const p of pieces) for (const k of p.keys) winner.set(k, p)\n\n // Name-closure check: every referenced rule must resolve in the fused set.\n for (const [k, p] of winner) {\n for (const d of p.deps.get(k) ?? []) {\n if (!winner.has(d)) throw new Error(`compose: rule \"${k}\" references missing rule \"${d}\"`)\n }\n }\n\n const contributing = new Set(winner.values())\n const contributingPieces = [...contributing]\n const needsEmptyTl = contributingPieces.some(p => p.needsEmptyTl)\n const needsHostReads = contributingPieces.some(p => p.needsHostReads)\n\n const lines: string[] = [\n // Shared sentinel protocol (must match NAMED_FN_FAIL / NAMED_FN_END in codegen).\n 'const _pfFail = {}',\n 'let _pfEnd',\n ...(needsEmptyTl ? ['const _EMPTY_TL = Object.freeze([])'] : []),\n ...(needsHostReads ? [HOST_READS_DECL] : []),\n // Each contributing artifact's namespaced private prelude (regexes, _pf, \u2026).\n ...new Set(contributingPieces.flatMap(p => p.prelude)),\n // The winning `_r_<Name>` function for each rule (one per name \u2192 no redeclare).\n ...[...winner].map(([k, p]) => p.ruleFns.get(k)!),\n ]\n const wrapperEntries = [...winner].map(([k, p]) => `${JSON.stringify(k)}: ${p.wrappers.get(k)!}`)\n const rawBody = [...lines, 'return {', wrapperEntries.join(',\\n'), '}'].join('\\n')\n\n // Fuse-time first-set dispatch: a rule-ref choice arm was emitted (in linkable\n // mode) as `/*@FS:rule:codevar@*\u200B/true`. Resolve it now against the WINNING rule's\n // LEADING first-set recipe \u2014 sound under override, since we use the final rule, not\n // the one visible when the referencing rule was compiled. Each rule's recipe is a\n // union of ordered leading-term chains; resolving them over the WINNING rules to a\n // fixpoint makes a `sequence(ref, \u2026)`-led arm/node dispatch on the ref's real first\n // char (parity with a monolithic compile) instead of degrading to always-try\n // because the ref baked `any`. A genuinely unknown ref \u2192 `any` (always try).\n const ANY: FirstSet = { kind: 'any' }\n const EMPTY: FirstSet = { kind: 'empty' }\n const recipes = new Map<string, FirstSetRecipe>()\n const shallow = new Map<string, FirstSet>()\n const nullable = new Map<string, boolean>()\n for (const [k, p] of winner) {\n const r = p.firstSetRecipes?.get(k); if (r) recipes.set(k, r)\n const fs = p.firstSets?.get(k); if (fs) shallow.set(k, fs)\n const nu = p.nullable?.get(k); if (nu !== undefined) nullable.set(k, nu)\n }\n const knownRef = (ref: string): boolean => recipes.has(ref) || shallow.has(ref)\n // A referenced rule's resolved first-set (or `any` for a genuinely unknown ref \u2014\n // always try). A ref whose nullability is unknown defaults to nullable (keep\n // unioning the chain tail \u2014 never drops a valid first char).\n const refFS = (ref: string): FirstSet => (knownRef(ref) ? (finalFS.get(ref) ?? ANY) : ANY)\n const refNullable = (ref: string): boolean => (knownRef(ref) ? (nullable.get(ref) ?? true) : true)\n // Resolve a recipe: union each ordered chain, and within a chain union each\n // segment's first-set left-to-right STOPPING after the first non-nullable segment\n // (its tail can't start the rule). A ref segment is skippable if it was forced so\n // at build (a wrapping optional/many \u2192 `seg.nullable`) OR the resolved rule is\n // itself nullable; only a definitely-consuming ref stops the chain.\n const resolveRecipe = (r: FirstSetRecipe): FirstSet => {\n let out: FirstSet = EMPTY\n for (const chain of r.alts) {\n for (const seg of chain) {\n out = union(out, seg.ref !== undefined ? refFS(seg.ref) : seg.set)\n const skippable = seg.ref !== undefined ? (seg.nullable || refNullable(seg.ref)) : seg.nullable\n if (!skippable) break\n }\n }\n return out\n }\n // Least-fixpoint: recipe-bearing rules start EMPTY and grow monotonically via\n // `resolveRecipe` (union of refs is monotone; the chain stop is static), so a\n // recursive rule converges to its tightest sound first-set. Rules with no recipe\n // keep their static shallow set.\n const finalFS = new Map<string, FirstSet>()\n for (const [k] of winner) finalFS.set(k, recipes.has(k) ? EMPTY : (shallow.get(k) ?? ANY))\n for (let iter = 0; iter <= recipes.size; iter++) {\n let changed = false\n for (const [k, r] of recipes) {\n const fs = resolveRecipe(r)\n if (JSON.stringify(finalFS.get(k)) !== JSON.stringify(fs)) { finalFS.set(k, fs); changed = true }\n }\n if (!changed) break\n }\n const body = rawBody.replace(\n // Rule name + code-point var are both JS identifiers (rule names are validated\n // at compile time \u2014 see assertRuleName in codegen), so an identifier class\n // matches every well-formed placeholder.\n /\\/\\*@FS:([A-Za-z0-9_$]+):([A-Za-z0-9_$]+)@\\*\\/true/g,\n (_m, name: string, codevar: string) => {\n const fs = finalFS.get(name)\n if (!fs || fs.kind === 'any' || fs.kind === 'empty') return 'true'\n return `(${firstSetCond(codevar, fs)})`\n },\n )\n\n // Non-inlined callbacks (runtime compile() mode), keyed `<ns>mf` / `<ns>build`.\n const env: Record<string, unknown> = {}\n for (const p of contributingPieces) {\n if (p.mfFns.length) env[`${p.ns}mf`] = p.mfFns\n if (p.buildFns.length) env[`${p.ns}build`] = p.buildFns\n }\n return { body, env }\n}\n\n/** Fuse at RUNTIME (via `new Function`) \u2014 used by `compose()` when not compiled\n * by the macro (like `compile()`). The macro path uses `emitFusedSource` instead. */\nexport function fuseRules(pieces: LinkablePieces[]): Record<string, FusedRule> {\n const { body, env } = fusedBody(pieces)\n // eslint-disable-next-line no-new-func\n return new Function('_env', body)(env) as Record<string, FusedRule>\n}\n\n/**\n * Fuse at BUILD time \u2014 emit the fused closure as a self-contained SOURCE\n * expression (`(() => { \u2026 })()`), with **no `new Function`**. This is what the\n * macro splices in for a `compose([...])` call, so macro output stays eval-free.\n * Requires every callback to be inlined from source (macro mode); throws if any\n * artifact carries runtime-only callback functions.\n */\nexport function emitFusedSource(pieces: LinkablePieces[]): string {\n const { body, env } = fusedBody(pieces)\n if (Object.keys(env).length > 0) {\n throw new Error('emitFusedSource: artifact carries non-static callbacks (runtime-only); cannot emit static source')\n }\n return `/* @__PURE__ */ (() => {\\n${body}\\n})()`\n}\n\n/**\n * Compose grammars/artifacts into a runnable parser map \u2014 the ONLY public\n * composition entry point. `compose([base, ext, \u2026])`: later entries override\n * earlier ones by rule name, and because fusion re-binds every reference in one\n * shared scope, an override reroutes the base's OWN calls too (open recursion).\n *\n * Each entry may be a **grammar** (a `rules()` result \u2014 a map of combinators,\n * linkable-ified here) OR an already-compiled **linkable artifact** (what the\n * macro emits and a package ships). So a package needs no opt-in wrapper to be\n * composable \u2014 `compose([importedGrammar, myRules])` just works.\n *\n * The macro compiles `compose([...])` to STATIC fused source (no `new Function`).\n * Called at runtime (no macro, like `compile()`) it fuses via `new Function`.\n */\n/** A composed parser carries its flattened source pieces (non-enumerable) so it\n * can be composed AGAIN \u2014 `compose([lessGrammar, delta])` where `lessGrammar` is\n * itself a `compose([...])` result. */\nconst COMPOSED_PIECES = Symbol.for('parseman.composedPieces')\n\n/**\n * A terminal fused grammar may be used to run a parser, but not as an input to\n * another composition. Macro `composeLeaf()` uses this for a local semantic\n * reduction over imported recognition-only IR: the local reductions stay in\n * their lexical module and therefore never become carried IR.\n */\nconst LEAF_COMPOSED = Symbol.for('parseman.leafComposed')\n\n/** The composing (outermost) trivia a runtime `compose()` applied \u2014 stored so a\n * later `pick(composedGrammar, \u2026)` can re-lower the selected rules under the SAME\n * trivia (composing-wins survives \u00E0-la-carte selection). The carried IR pieces hold\n * no trivia of their own, so it must be remembered separately. Not serialized by the\n * macro (which delegates pick to the runtime linker). */\nconst COMPOSED_TRIVIA = Symbol.for('parseman.composedTrivia')\n\n/** Final winner map for semantic-coverage tooling. It exists only when every\n * carried compose piece is re-lowerable IR; opaque precompiled artifacts have no\n * combinator graph to inspect and therefore deliberately expose no fake map. */\nconst COMPOSED_COVERAGE_RULES = Symbol.for('parseman.composedCoverageRules')\n\n/** The compact IR form a grammar carries instead of its lowered rule source: the\n * combinator-construction expression, re-lowered here at fuse time. */\nexport type IRPiece = { ns: string; ir: string }\n\nfunction isIRPiece(p: unknown): p is IRPiece {\n return !!p && typeof p === 'object'\n && typeof (p as IRPiece).ir === 'string' && typeof (p as IRPiece).ns === 'string'\n && !('ruleFns' in (p as object))\n}\n\n/** The re-lowerable carried pieces' rule maps, in compose order \u2014 the input to the\n * fuse-time gating diagnostic. An opaque precompiled artifact contributes no\n * combinator graph, so it is simply skipped: a hole it would have bound stays\n * unresolved and its choice stays deferred (silent), never falsely warned. */\nexport function carriedRuleMaps(carried: ReadonlyArray<LinkablePieces | IRPiece>): Array<Array<[string, Combinator<unknown>]>> {\n return carried.filter(isIRPiece).map(p => evalRuleMapIR(p.ir))\n}\n\nfunction coverageRulesOf(carried: Array<LinkablePieces | IRPiece>): Record<string, Combinator<unknown>> | undefined {\n const winners: Record<string, Combinator<unknown>> = {}\n for (const piece of carried) {\n if (!isIRPiece(piece)) return undefined\n const map = evalRuleMapIR(piece.ir)\n for (const [name, rule] of map) {\n // An accessed-but-undefined `g.Name` is an external reference, never a\n // rule definition. Match compose's IR filtering rule exactly.\n if (rule._def.tag === 'lazy') {\n try { rule._def.thunk() } catch { continue }\n }\n winners[name] = rule\n }\n }\n return winners\n}\n\n/** Return the final override-winner combinator map carried by runtime\n * `compose()`, or `undefined` when a precompiled opaque artifact participated.\n * This is intentionally internal: callers must not treat it as a parser API. */\nexport function composedCoverageRules(grammar: Record<string, unknown>): Record<string, Combinator<unknown>> | undefined {\n return (grammar as Record<symbol, unknown>)[COMPOSED_COVERAGE_RULES] as Record<string, Combinator<unknown>> | undefined\n}\n\n/** Materialize a carried item to full `LinkablePieces`: an IR piece is evaluated\n * back to a rule map and re-lowered; a full piece passes through. */\nexport function materializePiece(\n p: LinkablePieces | IRPiece,\n trivia?: Combinator<unknown>,\n captureTerminals = false,\n): LinkablePieces {\n if (!isIRPiece(p)) return p\n const pieces = compileLinkable(evalRuleMapIR(p.ir), p.ns, {\n ...(trivia ? { trivia } : {}),\n ...(captureTerminals ? { captureTerminals: true } : {}),\n })\n if (!pieces) throw new Error(`compose: carried IR for ns \"${p.ns}\" could not be re-lowered`)\n return pieces\n}\n\n/** Flatten one `compose()` item to its pieces: a prior composed result \u2192 its\n * carried list; an artifact \u2192 itself; a grammar (`rules()` map) \u2192 linkable-ified. */\nfunction nextComposeNs(used: Set<string>): string {\n let ns: string\n do { ns = `_lk${_nsCounter++}_` } while (used.has(ns))\n used.add(ns)\n return ns\n}\n\n/** Flatten one `compose()` item to its RE-LOWERABLE carried items \u2014 the form stored\n * on the composed result so it can be composed AGAIN under a NEW composing trivia.\n * A grammar (`rules()` map) is carried as compact IR (`{ns, ir}`), NOT baked source,\n * so a later `compose([thisResult, delta])` re-lowers it with the delta's trivia\n * (multi-level composing-wins). A prior composed result contributes its OWN carried\n * items (already IR); a pre-compiled artifact has no source, so it stays baked. */\nfunction itemCarried(\n item: LinkablePieces | Record<string, unknown>,\n used: Set<string>,\n trivia?: Combinator<unknown>,\n): Array<LinkablePieces | IRPiece> {\n const carried = (item as Record<symbol, unknown>)[COMPOSED_PIECES]\n // A prior composed result (runtime or macro-compiled): its carried list is already\n // re-lowerable (IR pieces, plus any pre-compiled artifacts). Pass it through so THIS\n // compose re-lowers it under its own composing trivia. Reserve its namespaces so a\n // sibling grammar map can't collide with them.\n if (Array.isArray(carried)) {\n const items = carried as Array<LinkablePieces | IRPiece>\n for (const p of items) used.add(p.ns)\n return items\n }\n // A pre-compiled artifact (`linkable()`/`pick()`): no source to re-lower \u2014 its trivia\n // was baked when it was compiled. Keep it as-is.\n if ((item as LinkablePieces).ruleFns instanceof Map) {\n const p = item as LinkablePieces\n used.add(p.ns)\n return [p]\n }\n // A grammar (`rules()` map): carry it as compact IR so a later compose re-lowers it\n // under ITS trivia. Unserializable \u2192 bake now with this compose's trivia (can't\n // re-lower later; acceptable fallback, mirrors the macro's full-pieces fallback).\n const map = item as Record<string, Combinator<unknown>>\n const ns = nextComposeNs(used)\n // Drop EXTERNAL entries first (same filter as compileLinkable): a `rules()` cache\n // also holds every ACCESSED-but-undefined `g.X` as an unresolved-lazy entry. Left in,\n // serializeRuleMap would emit `X: g[\"X\"]` \u2014 a self-referential rule that shadows the\n // sibling artifact defining X and recurses forever. They resolve by name at fuse time.\n const entries = Object.entries(map).filter(([, val]) => {\n const d = val._def\n if (d.tag !== 'lazy') return true\n try { d.thunk(); return true } catch { return false }\n })\n // Carry this grammar's ambient `scanSkip` (per-piece \u2014 opaque units are\n // dialect-specific, NOT composing-wins) into the IR so a re-lower stamps\n // `grammarScanSkip` back on. `linkable()`'s fallback reads it off `_meta` directly.\n const scanSkip = entries\n .map(([, val]) => (val._meta as { grammarScanSkip?: Combinator<unknown>[] }).grammarScanSkip)\n .find(Boolean)\n const ir = serializeRuleMap(entries, scanSkip)\n return ir ? [{ ns, ir }] : [linkable(map, ns, trivia)]\n}\n\n/** The composed grammar's ambient trivia = the LAST composed item that declares a\n * grammar-level trivia (via `rules({ trivia }, \u2026)`, which tags `grammarTrivia` on its\n * rules). Outermost wins: the composing grammar's trivia applies to every fused rule,\n * including those inherited from a base \u2014 so e.g. an SCSS `rw` (which extends Less's)\n * governs the inherited Less/CSS rules too. `parser`/`noTrivia` still override locally. */\nfunction composingTriviaOf(items: Array<LinkablePieces | Record<string, unknown>>): Combinator<unknown> | undefined {\n for (let i = items.length - 1; i >= 0; i--) {\n const item = items[i] as Record<string, unknown> | undefined\n if (!item || (item as LinkablePieces).ruleFns instanceof Map || (item as Record<symbol, unknown>)[COMPOSED_PIECES]) continue\n for (const v of Object.values(item)) {\n const t = (v as Combinator<unknown> | undefined)?._meta?.grammarTrivia\n if (t) return t\n }\n }\n return undefined\n}\n\nexport function compose(\n items: Array<LinkablePieces | Record<string, unknown>>,\n): Record<string, FusedRule> {\n if (items.some(item => (item as Record<symbol, unknown>)[LEAF_COMPOSED] === true)) {\n throw new Error('compose: a composeLeaf() result is terminal and cannot be composed again')\n }\n const used = new Set<string>()\n // The composed grammar's ambient trivia comes from the composing grammar itself \u2014\n // whatever the last piece declared via rules({ trivia }, \u2026). No separate option:\n // the trivia rides with the grammar that declared it.\n const trivia = composingTriviaOf(items)\n // Carried items are RE-LOWERABLE (IR); materialize them ONCE with this compose's\n // trivia for the now-fuse, but STORE the un-materialized carried list so a later\n // compose can re-lower it under a different trivia (multi-level composing-wins).\n const carried = items.flatMap(item => itemCarried(item, used, trivia))\n // Fuse time is where a shared shape's `g.Foo` hole is finally bound, so it is the\n // only site that can answer whether the choices it leads actually gate.\n runFusedGatingDiagnostic(() => carriedRuleMaps(carried))\n const pieces = carried.map(p => materializePiece(p, trivia))\n const map = fuseRules(pieces)\n Object.defineProperty(map, COMPOSED_PIECES, { value: carried, enumerable: false })\n if (trivia) Object.defineProperty(map, COMPOSED_TRIVIA, { value: trivia, enumerable: false })\n const coverageRules = coverageRulesOf(carried)\n if (coverageRules) Object.defineProperty(map, COMPOSED_COVERAGE_RULES, { value: coverageRules, enumerable: false })\n return map\n}\n\n/**\n * Compose a terminal grammar. This is for a leaf parser that overlays local\n * semantic reductions on reusable recognition rules. It is macro-only: without\n * macro lowering there is no safe way to keep lexical builders out of carried\n * IR, so it fails rather than falling back to runtime composition.\n */\nexport function composeLeaf(\n _items: Array<LinkablePieces | Record<string, unknown>>,\n): Record<string, FusedRule> {\n throw new Error('composeLeaf(): requires Parseman macro lowering; runtime composition is forbidden')\n}\n", "/**\n * Tree traversal for the CST/AST a grammar produces. Pars\u00E9man hands back a plain\n * object tree, so you *can* recurse it yourself \u2014 these helpers save you from\n * re-writing the same recursion for diagnostics, folding, or lowering a tree.\n *\n * Two entry points:\n * - `walk` \u2014 imperative depth-first traversal with enter/leave hooks.\n * - `createVisitor` \u2014 Chevrotain-style dispatch keyed on each node's `type`.\n *\n * Both default to the built-in CST shape (`CSTChild` \u2014 the `node()` / leaf / error\n * union), so with no annotation you get precise typing out of the box. Parsing to\n * a custom AST? Pass your node type as a generic: `walk<MyNode>(root, \u2026)`.\n */\nimport type { CSTChild } from './types.ts'\n\n/**\n * The minimal shape these helpers traverse: a `_tag`, an optional rule `type`,\n * and optional structural `children`. The built-in `CSTChild` satisfies it, and\n * so does any custom AST node (that's what the generic override targets).\n */\nexport type Walkable = {\n readonly _tag: string\n readonly type?: string\n readonly children?: ReadonlyArray<Walkable>\n}\n\nexport interface WalkVisitor<N extends Walkable = CSTChild, C = undefined> {\n /**\n * Called on entering a node, before its children. Return `false` to skip\n * descending into this node's children (`leave` still runs).\n */\n enter?(node: N, parent: N | null, ctx: C): boolean | void\n /** Called on leaving a node, after its children. */\n leave?(node: N, parent: N | null, ctx: C): void\n}\n\n/**\n * Depth-first traversal. Visits `root`, then each structural child in order.\n * `ctx` is threaded to both hooks unchanged (use it as an accumulator).\n *\n * Defaults to the CST shape; override with `walk<MyNode>(root, \u2026)`:\n *\n * const idents: string[] = []\n * walk(tree, {\n * enter(node) {\n * if (node._tag === 'leaf') idents.push(node.value)\n * },\n * })\n */\nexport function walk<N extends Walkable = CSTChild, C = undefined>(\n root: N,\n visitor: WalkVisitor<N, C>,\n ctx: C = undefined as C,\n): void {\n const go = (node: N, parent: N | null): void => {\n const descend = visitor.enter ? visitor.enter(node, parent, ctx) : undefined\n const children = node.children as ReadonlyArray<N> | undefined\n if (descend !== false && Array.isArray(children)) {\n for (const child of children) go(child, node)\n }\n visitor.leave?.(node, parent, ctx)\n }\n go(root, null)\n}\n\nexport interface VisitApi<R, N extends Walkable = CSTChild> {\n /** Dispatch a node to its handler (by `type`); returns the handler's result. */\n visit(node: N): R | undefined\n /** Visit every structural child, collecting the defined results in order. */\n visitChildren(node: N): R[]\n}\n\nexport type VisitorHandlers<R, N extends Walkable = CSTChild> = Record<\n string,\n (node: N, api: VisitApi<R, N>) => R\n>\n\n/**\n * Build a visitor that dispatches on each node's `type` \u2014 the direct analog of a\n * generated CST-visitor base class. Handlers are keyed by rule name and receive\n * an `api` with `visit` / `visitChildren` to recurse:\n *\n * const evalExpr = createVisitor<number>({\n * Num: (n) => Number((n as NumNode).value),\n * Add: (n, api) => api.visitChildren(n).reduce((a, b) => a + b, 0),\n * })\n * const total = evalExpr(tree)\n *\n * Defaults to the CST shape; override the return type and node type together with\n * `createVisitor<number, MyNode>({ \u2026 })`. A node whose `type` has no handler\n * falls through to visiting its children (results discarded), so partial visitors\n * work without listing every rule.\n */\nexport function createVisitor<R = void, N extends Walkable = CSTChild>(\n handlers: VisitorHandlers<R, N>,\n): (node: N) => R | undefined {\n const api: VisitApi<R, N> = {\n visit(node) {\n const handler = node.type !== undefined ? handlers[node.type] : undefined\n if (handler) return handler(node, api)\n api.visitChildren(node)\n return undefined\n },\n visitChildren(node) {\n const out: R[] = []\n const children = node.children as ReadonlyArray<N> | undefined\n if (Array.isArray(children)) {\n for (const child of children) {\n const r = api.visit(child)\n if (r !== undefined) out.push(r)\n }\n }\n return out\n },\n }\n return (node) => api.visit(node)\n}\n", "/**\n * Relative span model for incremental edits.\n *\n * Absolute source offsets are simple but fragile: an insertion at offset `at`\n * shifts EVERY downstream offset, so a whole-tree renumber is O(nodes-after-edit).\n * Storing each node's span RELATIVE to its parent's start makes edits local: an\n * insertion inside child k of a node only changes\n * (a) child k's relative end,\n * (b) the relative start of siblings after k,\n * and leaves the *children* of those shifted siblings untouched \u2014 they move with\n * their parent for free. So the mutation count is O(depth + trailing siblings),\n * not O(all following nodes), and every unaffected subtree is shared by identity.\n *\n * An absolute view is recovered on query by accumulating the parent base as you\n * descend (the `absolutize` / `absoluteSpanAt` cursor) \u2014 one source of truth,\n * two manifestations.\n */\n\n/** A node with an ABSOLUTE source span and ordered children. */\nexport type AbsNode = {\n readonly start: number\n readonly end: number\n readonly children: readonly AbsNode[]\n}\n\n/** A node whose `start`/`end` are RELATIVE to its parent's start (root base = 0). */\nexport type RelNode = {\n readonly start: number\n readonly end: number\n readonly children: readonly RelNode[]\n}\n\n/** Convert an absolute tree to parent-relative spans. */\nexport function relativize(node: AbsNode, parentBase = 0): RelNode {\n return {\n start: node.start - parentBase,\n end: node.end - parentBase,\n children: node.children.map((c) => relativize(c, node.start)),\n }\n}\n\n/** Reconstruct absolute spans from a relative tree. Inverse of `relativize`. */\nexport function absolutize(node: RelNode, parentBase = 0): AbsNode {\n const start = parentBase + node.start\n return {\n start,\n end: parentBase + node.end,\n children: node.children.map((c) => absolutize(c, start)),\n }\n}\n\n/**\n * Absolute span of the node at `path` (child indices from the root), accumulating\n * the base as it descends \u2014 O(path length), no full absolutize. The relative-form\n * cursor: query any node's absolute position without materializing the whole tree.\n */\nexport function absoluteSpanAt(root: RelNode, path: readonly number[]): { start: number; end: number } {\n let base = 0\n let node = root\n for (const idx of path) {\n base += node.start\n const child = node.children[idx]\n if (!child) throw new RangeError(`absoluteSpanAt: no child ${idx} on path ${path.join('.')}`)\n node = child\n }\n return { start: base + node.start, end: base + node.end }\n}\n\n// ---------------------------------------------------------------------------\n// CST-shaped projection (parseDoc incremental trees)\n//\n// The bare Abs/RelNode helpers above are the reference model. The functions\n// below apply the same relative-span algebra to real CST children \u2014 nodes,\n// leaves, and error nodes \u2014 where the span is a nested `{ start, end }` and\n// children are heterogeneous (leaves carry a span but no `children`). Trivia\n// never appears in structural `children`, so every child has a span.\n// ---------------------------------------------------------------------------\n\n/**\n * A CST child as far as span projection cares: a nested span + optional children.\n * `children` is typed loosely (`unknown[]`) so the real heterogeneous CST unions\n * (nodes, leaves, error nodes \u2014 a leaf has no `children`) satisfy it directly.\n */\ntype Spanned = {\n readonly span: { readonly start: number; readonly end: number }\n readonly children?: readonly unknown[]\n}\n\n/**\n * Rewrite a CST subtree's spans to be RELATIVE to `parentBase` (the absolute\n * start of the node's parent; root base = 0). Each child's own children are\n * relativized against *this* node's absolute start. Preserves every other field\n * (`_tag`, `type`, `state`, leaf `value`, \u2026) by shallow spread.\n */\nexport function relativizeCST<T extends Spanned>(node: T, parentBase = 0): T {\n const span = { start: node.span.start - parentBase, end: node.span.end - parentBase }\n if (node.children) {\n const base = node.span.start\n return { ...node, span, children: node.children.map((c) => relativizeCST(c as Spanned, base)) } as T\n }\n return { ...node, span } as T\n}\n\n/** Reconstruct absolute CST spans from a relative subtree. Inverse of `relativizeCST`. */\nexport function absolutizeCST<T extends Spanned>(node: T, parentBase = 0): T {\n const start = parentBase + node.span.start\n const span = { start, end: parentBase + node.span.end }\n if (node.children) {\n return { ...node, span, children: node.children.map((c) => absolutizeCST(c as Spanned, start)) } as T\n }\n return { ...node, span } as T\n}\n\n/**\n * Absolute span of the CST node at `path` (child indices from the root),\n * accumulating the base as it descends \u2014 O(path length), no full absolutize.\n * The relative-form cursor for a positioned CST: query one node's absolute\n * position without materializing the whole tree.\n */\nexport function absoluteSpanCST(root: Spanned, path: readonly number[]): { start: number; end: number } {\n let base = 0\n let node: Spanned = root\n for (const idx of path) {\n base += node.span.start\n const child = node.children?.[idx] as Spanned | undefined\n if (!child) throw new RangeError(`absoluteSpanCST: no child ${idx} on path ${path.join('.')}`)\n node = child\n }\n return { start: base + node.span.start, end: base + node.span.end }\n}\n\n/** Naive absolute reshift (baseline): shift every offset >= `at` by `delta`. */\nexport function shiftAbsolute(node: AbsNode, at: number, delta: number): AbsNode {\n const s = node.start >= at ? node.start + delta : node.start\n const e = node.end >= at ? node.end + delta : node.end\n return {\n start: s,\n end: e,\n children: node.children.map((c) => shiftAbsolute(c, at, delta)),\n }\n}\n\n/**\n * Apply an edit of `delta` characters at absolute offset `at` to a RELATIVE tree.\n * Returns a new relative tree; every subtree that ends at/before `at` (in absolute\n * terms) is returned by IDENTITY (unchanged), so the result structurally shares\n * all unaffected subtrees with the input.\n *\n * `stats.allocated` counts nodes that had to be re-created \u2014 the incremental cost.\n */\nexport function applyEdit(\n root: RelNode,\n at: number,\n delta: number,\n stats?: { allocated: number },\n): RelNode {\n // The root has no parent; its base never shifts (base 0).\n return editNode(root, at, delta, 0, 0, stats)\n}\n\nfunction editNode(\n node: RelNode,\n at: number,\n delta: number,\n parentBase: number,\n parentShift: number, // how much this node's parent base moved (0 or delta)\n stats?: { allocated: number },\n): RelNode {\n const absStart = parentBase + node.start\n const absEnd = parentBase + node.end\n\n // Strictly before the edit \u2192 unchanged (its parent base is also unshifted).\n // Uses `< at` (not `<=`) to match the shift convention: offsets `>= at` move,\n // so a node ending exactly at `at` grows and must be rewritten.\n if (absEnd < at) return node\n\n // Fully after the edit AND the parent moved by `delta` too \u2192 this node moves as\n // one unit with its parent, so its parent-relative offsets are identical. Share\n // the ENTIRE subtree by identity \u2014 this is the O(depth) locality win.\n const startShifts = absStart >= at\n if (startShifts && parentShift === delta) return node\n\n if (stats) stats.allocated++\n\n const newAbsStart = startShifts ? absStart + delta : absStart\n const newAbsEnd = absEnd + delta // absEnd > at here (fully-before returned above)\n const newParentBase = parentBase + parentShift\n const newStart = newAbsStart - newParentBase\n const newEnd = newAbsEnd - newParentBase\n\n // Children are relative to this node's absolute start. Our own shift (0 or delta)\n // becomes their parentShift.\n const ownShift = newAbsStart - absStart\n let changed = false\n const newChildren: RelNode[] = []\n for (const c of node.children) {\n const nc = editNode(c, at, delta, absStart, ownShift, stats)\n if (nc !== c) changed = true\n newChildren.push(nc)\n }\n\n return {\n start: newStart,\n end: newEnd,\n children: changed ? newChildren : node.children,\n }\n}\n", "/**\n * Incremental parse document \u2014 incremental re-parse over a rules() registry.\n */\nimport type { Combinator, ParseContext, ParseError, ParseResult, ParserDef, Span } from '../types.ts'\nimport type { NodeLike, CSTLeaf, CSTError } from '../cst/types.ts'\nimport { relativizeCST, absoluteSpanCST } from '../cst/relative-spans.ts'\nimport { REC } from '../recovery/scan.ts'\n\n/**\n * Build the parse ctx for a (re)parse. In `tolerant` mode the same recovery bundle\n * the interpreter/`run` use is installed (`_tolerant` + `_rec`), so a broken edit\n * keeps producing a tree (with recovered `ParseError`s embedded) instead of\n * collapsing to `null`. Strict mode is byte-identical to before. Every reparse site\n * \u2014 root, localized rule reparse, list-splice middle, and the soundness probes \u2014\n * threads the SAME flag, so a probe's tolerance matches the parse it is checking\n * (a mismatch would spuriously diverge and forgo reuse, never miscompare).\n */\nfunction mkCtx(state: unknown, build: ParseContext['build'], tolerant: boolean): ParseContext {\n return tolerant\n ? { trackLines: false, state, build, _tolerant: true, _rec: REC, _errors: [] }\n : { trackLines: false, state, build }\n}\n\n/** A single compiled (or interpreted) rule: parse from `pos`, producing node `N`. */\nexport type RuleFn<N> = (input: string, pos: number, ctx: ParseContext) => ParseResult<N>\n\n/**\n * Rule name \u2192 parser. Each entry is either a bare parse function or a\n * `Combinator` (what `rules()` returns). Passing the `rules()` combinators\n * directly lets `.edit()` inspect the grammar \u2014 required for **sound** structural\n * list-reuse (see `structuralReuse`): only a rule the grammar proves is a genuine\n * repetition is ever spliced. A bare-function registry still parses correctly; it\n * just can't be structurally reused (the splice is skipped, never guessed).\n */\nexport type Registry<N> = Record<string, RuleFn<N> | Combinator<N>>\n\n/** Normalize a registry entry to a callable parse function. */\nfunction asRuleFn<N>(entry: RuleFn<N> | Combinator<N>): RuleFn<N> {\n return typeof entry === 'function' ? entry : (input, pos, ctx) => entry.parse(input, pos, ctx)\n}\n\n/** The combinator def behind a registry entry, if it carries one (bare functions don't). */\nfunction defOf(entry: unknown): ParserDef | undefined {\n return typeof entry === 'object' && entry !== null && '_def' in entry\n ? (entry as { _def: ParserDef })._def\n : undefined\n}\n\n/**\n * Does this rule's grammar produce its element children via a genuine, unbounded\n * **repetition** (`sepBy` / `many` / `oneOrMore`) \u2014 as opposed to a fixed-arity\n * sequence of same-typed tokens (e.g. `Triple = Num ',' Num ',' Num`)? Only the\n * former is sound to structurally reuse: a full reparse accepts any element count,\n * so splicing one in or out matches it; a fixed-arity rule would not. We walk the\n * def, transparently unwrapping semantic wrappers (`node`, `transform`, \u2026) and\n * looking through a top-level `sequence` / `optional` (the `[ (sepBy)? ]` shape),\n * and return true iff a repetition combinator is reachable that way. Structurally\n * indistinguishable-from-the-CST cases (fixed sequences) return false and fall\n * back to a full, correct reparse. This is what makes `structuralReuse` sound\n * rather than a promise the caller has to keep.\n */\nfunction producesRepetition(def: ParserDef | undefined, depth = 0): boolean {\n if (!def || depth > 24) return false\n const d = def as ParserDef & Record<string, unknown>\n switch (d.tag) {\n case 'sepBy':\n case 'many':\n case 'oneOrMore':\n return true\n // Rule entries and `ref`s wrap their body in a `lazy` thunk \u2014 resolve it. The\n // `depth` cap bounds any self-referential cycle (a repetition, if present, is\n // found shallowly before recursion goes deep).\n case 'lazy': {\n const thunk = d.thunk as (() => { _def?: ParserDef }) | undefined\n let inner: { _def?: ParserDef } | undefined\n try { inner = typeof thunk === 'function' ? thunk() : undefined } catch { return false }\n return producesRepetition(inner?._def, depth + 1)\n }\n // Transparent wrappers \u2014 look through to the inner parser.\n case 'node':\n case 'transform':\n case 'attempt':\n case 'token':\n case 'leaf':\n case 'label':\n case 'field':\n case 'expect':\n case 'withCtx':\n case 'grammar':\n case 'optional':\n return producesRepetition(defOf(d.parser), depth + 1)\n case 'skip':\n return producesRepetition(defOf(d.main), depth + 1)\n // A bracketed/anchored list is a sequence whose element run is a repetition.\n case 'sequence':\n return (d.parsers as unknown[]).some(p => producesRepetition(defOf(p), depth + 1))\n default:\n return false\n }\n}\n\nexport type ParseDocOptions<N extends NodeLike> = {\n /** Initial grammar state threaded into ctx.state for the root parse. */\n state?: unknown\n /**\n * Reconstruct a parent node with one child replaced (used when grafting a\n * re-parsed subtree into its ancestors). Defaults to a shallow spread, which\n * works for plain-object nodes; class-instance ASTs should supply their own.\n */\n rebuild?: (node: N, children: ReadonlyArray<N | CSTLeaf | CSTError>) => N\n /**\n * Mode host for a linkable/fused grammar (RULE_ABI_PLAN \u00A77): threaded into\n * `ctx.build` on every (re)parse so `node()` rules build a positioned CST /\n * language-service tree instead of their own eval-AST. Unset \u2192 the grammar's\n * own builders (eval mode).\n */\n build?: ParseContext['build']\n /**\n * Enable structural list-reuse: on a length-changing *structural* edit (adding\n * or removing a whole element in a collection) that would otherwise force a full\n * reparse, reparse only the disturbed span and reuse the collection's untouched\n * tail elements by identity \u2014 turning an insert near the top of a large list from\n * O(list) into O(edit + trailing siblings).\n *\n * OFF by default because it is sound only when a rule whose CST children form a\n * homogeneous, separator-delimited element list is a genuine REPETITION\n * (`many` / `sepBy` / `oneOrMore`), not a fixed-arity sequence of same-typed\n * tokens (e.g. `Triple = Num ',' Num ',' Num`) \u2014 the two are structurally\n * indistinguishable without the grammar, and splicing the latter would accept an\n * element count it shouldn't. Turn it on when your list rules are true\n * repetitions (the common case: JSON arrays/objects, CSS value lists, argument\n * lists). Every splice is still guarded (exact tiling + lookahead probe +\n * stateless-tail check) and falls back to a full, correct reparse when unproven;\n * the flag only authorises *attempting* the reuse.\n */\n structuralReuse?: boolean\n /**\n * Parse tolerantly: `many`/`sepBy`/`oneOrMore` recover from a failed element\n * (skip to an inferred sync point, embed a `ParseError` over the skipped span),\n * so a broken edit keeps producing a tree instead of collapsing to `null` \u2014 the\n * editor-backend path. Off by default (strict, byte-identical). Recovery is a cold\n * path: well-formed input never triggers it. Every reparse the doc does \u2014 root,\n * localized rule, and list-splice \u2014 inherits this flag, and the reuse-soundness\n * probes run at the same tolerance so incremental reuse stays valid under recovery\n * (a scan that would cross a splice boundary just falls back to a full reparse).\n */\n tolerant?: boolean\n /**\n * The grammar's trivia rule, used ONLY to compute `unconsumedFrom`: a root rule\n * consumes trivia BETWEEN terms but not after the last, so trailing\n * whitespace/comments would otherwise read as leftover input. Given the trivia\n * rule, the tail is skipped before reporting the first unconsumed offset \u2014\n * matching `run()`'s semantics. Defaults to the root rule's own\n * `_meta.grammarTrivia` (from `rules({ trivia })`); set it only to override.\n */\n trivia?: Combinator<unknown>\n}\n\nexport interface ParseDoc<N extends NodeLike> {\n /**\n * The parse tree with PARENT-RELATIVE spans \u2014 each node's `span` is relative to\n * its parent's start (root base 0). This is the shareable representation: a\n * length-changing `.edit()` keeps every untouched subtree shared by identity,\n * and reading the tree is O(1) (no offset rewrite). For absolute positions use\n * the O(depth) cursor `spanAt(path)`, or `absolutizeCST(doc.tree)` to\n * materialize the whole absolute tree. A fresh non-incremental `node().parse()`\n * result is unchanged \u2014 still absolute.\n */\n readonly tree: N | null\n /**\n * Recovery diagnostics collected during the (re)parse \u2014 the missing-token\n * `expect()` errors and tolerant-list recovery errors that ride the tree as\n * `parseError` nodes, surfaced here as a flat list too (spans ABSOLUTE). Empty\n * in strict mode. On a hard (non-recovered) parse failure this holds the single\n * top-level failure. This is what makes an editor document able to see syntax\n * errors \u2014 a blank `errors: []` (the prior behaviour) hid every recovery.\n */\n readonly errors: ParseError[]\n /**\n * Offset where unparsed input begins \u2014 the first non-trivia character the parse\n * left unconsumed (trailing trivia skipped when a trivia rule is available), or\n * `null` if the whole input was consumed. This is how a document detects \"the\n * grammar stopped short, there's junk here\"; computed exactly as `run()` does.\n */\n readonly unconsumedFrom: number | null\n readonly input: string\n /**\n * Absolute span of the node at `path` (child indices from the root) \u2014 O(depth),\n * without materializing the absolute tree. The projection cursor for the\n * relative representation; use it for spot queries on a large incremental doc.\n */\n spanAt(path: readonly number[]): { start: number; end: number }\n /**\n * Incrementally re-parse after a text change. `from`/`to` are byte offsets in\n * the OLD input; `replacement` fills that range (editor change-event shape).\n * Sound: the result tree is always structurally identical to a fresh\n * `parseDoc` of the edited text (the Stage-2 guard falls back to a full\n * reparse whenever reuse can't be proven safe). Reuse/strategy is intentionally\n * NOT reported here \u2014 an observer derives it by diffing this tree against the\n * previous one (see the incremental tests); the runtime's job is to be fast,\n * not to measure itself.\n */\n edit(from: number, to: number, replacement: string): ParseDoc<N>\n}\n\n// ---------------------------------------------------------------------------\n// Tree navigation (generic over NodeLike \u2014 no class, no CST assumptions)\n// ---------------------------------------------------------------------------\n\n/**\n * A reentry candidate is only worth reparsing if it's meaningfully smaller than\n * the whole document \u2014 reparsing a rule that spans (say) >half the input costs\n * about as much as a full reparse with none of the reuse. Above this fraction of\n * the input length, `.edit()` skips straight to a full reparse. Keeps the worst\n * case (a structural edit near the front) at ~1\u00D7 full reparse, never several.\n */\nconst REENTRY_MAX_SPAN_FRACTION = 0.5\n\ntype FoundNode<N extends NodeLike> = { node: N; path: number[] }\n\nfunction isNode(x: unknown): x is NodeLike {\n return typeof x === 'object' && x !== null && (x as { _tag?: string })._tag === 'node'\n}\n\n/**\n * Locate the deepest node containing absolute offset `pos`. The tree stores\n * PARENT-RELATIVE spans (see relative-spans), so we thread each node's absolute\n * start down the descent: a child's absolute start is `nodeAbsStart +\n * child.span.start`.\n */\nfunction findContaining<N extends NodeLike>(\n node: N,\n nodeAbsStart: number,\n pos: number,\n path: number[] = [],\n): FoundNode<N> | null {\n for (let i = 0; i < node.children.length; i++) {\n const child = node.children[i]!\n if (!isNode(child)) continue\n const childAbsStart = nodeAbsStart + child.span.start\n const childAbsEnd = nodeAbsStart + child.span.end\n if (childAbsStart <= pos && pos < childAbsEnd) {\n const inner = findContaining(child as N, childAbsStart, pos, [...path, i])\n return inner ?? { node: child as N, path: [...path, i] }\n }\n }\n return null\n}\n\nfunction ancestorsAt<N extends NodeLike>(root: N, path: number[]): N[] {\n const ancestors: N[] = [root]\n let cur: N = root\n for (const idx of path.slice(0, -1)) {\n const child = cur.children[idx]\n if (!child || !isNode(child)) break\n ancestors.push(child as N)\n cur = child as N\n }\n return ancestors\n}\n\nfunction replaceAtPath<N extends NodeLike>(\n rebuild: NonNullable<ParseDocOptions<N>['rebuild']>,\n root: N,\n path: number[],\n newNode: N,\n): N {\n if (path.length === 0) return newNode\n const [idx, ...rest] = path as [number, ...number[]]\n const newChildren = [...root.children] as Array<N | CSTLeaf | CSTError>\n newChildren[idx] = rest.length === 0\n ? newNode\n : replaceAtPath(rebuild, root.children[idx] as N, rest, newNode)\n return rebuild(root, newChildren)\n}\n\nfunction defaultRebuild<N extends NodeLike>(node: N, children: ReadonlyArray<N | CSTLeaf | CSTError>): N {\n return { ...node, children } as N\n}\n\ntype SpanChild = { span: { start: number; end: number }; children?: readonly unknown[] }\n\n/**\n * Shift a node's PARENT-RELATIVE span by `delta`. Used for siblings that sit\n * *after* a length-changing edit inside the same parent: the node moved as a\n * unit, so both endpoints slide by `delta`, but its own children are relative to\n * *its* start and are unchanged \u2014 so `children` is shared by identity. That's the\n * relative-span win: O(1) per trailing sibling, not O(subtree). (Contrast the old\n * absolute model, which had to deep-rewrite every descendant.)\n */\nfunction shiftRelStart<T>(child: T, delta: number): T {\n const c = child as unknown as SpanChild\n return { ...(c as object), span: { start: c.span.start + delta, end: c.span.end + delta } } as unknown as T\n}\n\n/**\n * Graft `newNode` at `path` into a parent-relative tree for a length-changing\n * edit (`delta !== 0`): children before the edit are shared by reference, the\n * edited child is replaced, children *after* it have their relative start slid by\n * `delta` (one shallow alloc each \u2014 their subtrees are shared by identity), and\n * each ancestor's relative `span.end` grows by `delta` while its start is\n * untouched. Cost is O(depth + trailing siblings along the spine), independent of\n * how many nodes sit inside those trailing siblings.\n */\nfunction graftRelative<N extends NodeLike>(root: N, path: number[], newNode: N, delta: number): N {\n if (path.length === 0) return newNode\n const [idx, ...rest] = path as [number, ...number[]]\n const oldChildren = root.children as ReadonlyArray<N | CSTLeaf | CSTError>\n const newChildren = oldChildren.map((child, i) => {\n if (i < idx) return child\n if (i === idx) {\n return rest.length === 0 ? newNode : graftRelative(child as N, rest, newNode, delta)\n }\n return shiftRelStart(child, delta)\n })\n return {\n ...root,\n span: { start: root.span.start, end: root.span.end + delta },\n children: newChildren,\n } as N\n}\n\n// ---------------------------------------------------------------------------\n// List-splice reuse (structural edits in a collection)\n//\n// A structural edit \u2014 inserting or deleting a whole element in a list \u2014 makes the\n// innermost-rule reparse fail to converge, and the containing collection is often\n// too big to reparse (that's the \"insert at the top of a large array\" case where\n// a whole-rule reparse costs ~a full reparse). Instead of reparsing the whole\n// collection, reparse ONLY the disturbed span between the last untouched element\n// before the edit and the first untouched element after it, then splice: the head\n// children are shared as-is, the freshly-parsed middle replaces the changed run,\n// and the tail children are reused with their relative start slid by `delta`\n// (O(1) each \u2014 their subtrees are shared by identity, the relative-span win).\n//\n// Soundness rests on: (1) the reparsed middle exactly fills [reStart, reEnd) and\n// lands on element/separator boundaries; (2) a lookahead probe proving the middle\n// read nothing at/after the splice point (else it peeked into the reused tail);\n// (3) a stateless-reentry precondition on the reused tail (its saved `state` is\n// null), so a forward, context-free grammar reproduces those subtrees unchanged.\n// Any failure \u21D2 `null` \u21D2 the caller falls back to a full, correct reparse. The\n// incremental oracle fuzz (edit() \u2261 full reparse, 400 seeds \u00D7 3 list grammars +\n// edge cases) is the end-to-end correctness net.\n// ---------------------------------------------------------------------------\n\ntype AnyChild = { readonly _tag: string; readonly span: { start: number; end: number }; readonly value?: string; readonly type?: string; readonly state?: unknown; readonly children?: readonly unknown[] }\n\n/**\n * Parse the collection's disturbed middle `[reStart, reEnd)` as an\n * `element (separator element)*` run (optionally with a trailing separator that\n * connects to the reused tail), C-relative. Alternation is STRICT: after an\n * element, if we're not yet at `reEnd`, the separator MUST match \u2014 element/element\n * juxtaposition is rejected (that's a `sepBy` violation). When the grammar has no\n * separator (`separator === null`, i.e. a bare repetition), consecutive elements\n * are allowed. Returns the C-relative children, or `null` if the run doesn't tile\n * `[reStart, reEnd)` exactly on token boundaries.\n */\nfunction parseMiddle(\n input: string,\n reStart: number,\n reEnd: number,\n cStart: number,\n ruleFn: RuleFn<NodeLike>,\n separator: string | null,\n build: ParseContext['build'],\n tolerant: boolean,\n): AnyChild[] | null {\n const ctx: ParseContext = mkCtx(null, build, tolerant)\n const out: AnyChild[] = []\n let pos = reStart\n let guard = 0\n while (pos < reEnd) {\n if (++guard > reEnd - reStart + 2) return null // no-progress backstop\n // After an element, a separator is mandatory before the next element (unless\n // the grammar is separator-free). This is what keeps `elem elem` \u2014 invalid in\n // a `sepBy` \u2014 from being accepted as two elements.\n if (separator && out.length > 0 && out[out.length - 1]!._tag === 'node') {\n if (pos + separator.length > reEnd || !input.startsWith(separator, pos)) return null\n out.push({ _tag: 'leaf', value: separator, span: { start: pos - cStart, end: pos + separator.length - cStart } })\n pos += separator.length\n continue\n }\n let r: ParseResult<NodeLike>\n try {\n r = ruleFn(input, pos, ctx)\n } catch {\n return null\n }\n if (!r.ok || r.span.end <= pos || r.span.end > reEnd) return null\n out.push(relativizeCST(r.value as unknown as AnyChild, cStart))\n pos = r.span.end\n }\n return pos === reEnd ? out : null\n}\n\n/**\n * Try to reuse the untouched tail of collection `C` (at `path`, absolute start\n * `cStart`) around a length-changing edit, reparsing only the disturbed middle.\n * Returns the new relative root, or `null` to fall back to a full reparse.\n */\nfunction tryListSplice<N extends NodeLike>(\n root: N,\n path: number[],\n C: N,\n cStart: number,\n newInput: string,\n from: number,\n to: number,\n delta: number,\n registry: Record<string, RuleFn<N>>,\n build: ParseContext['build'],\n tolerant: boolean,\n): N | null {\n const kids = C.children as ReadonlyArray<AnyChild>\n if (kids.length === 0) return null\n\n // head = maximal prefix ending before the edit, backed off so it ends at a\n // leaf (open delimiter or separator) \u2014 i.e. where an ELEMENT is next expected.\n let h = 0\n while (h < kids.length && cStart + kids[h]!.span.end <= from) h++\n while (h > 0 && kids[h - 1]!._tag === 'node') h--\n // tail = maximal suffix starting after the edit, advanced so it BEGINS at an\n // element (node) boundary \u2014 a self-contained element run to reuse.\n let t = kids.length\n while (t > h && cStart + kids[t - 1]!.span.start >= to) t--\n while (t < kids.length && kids[t]!._tag !== 'node') t++\n if (t >= kids.length) return null // nothing reusable after the edit\n\n // Stateless-reentry precondition on the reused tail (see soundness note).\n for (let i = t; i < kids.length; i++) {\n if (kids[i]!._tag === 'node' && kids[i]!.state != null) return null\n }\n\n // C must look like a genuine homogeneous COLLECTION \u2014 a repetition of one\n // element rule joined by one separator \u2014 not a fixed heterogeneous sequence\n // (e.g. `Pair = Key ':' Val`, whose `:` is not a list separator). Require: all\n // element (node) children share a type, and there are \u22652 elements joined by a\n // consistent separator leaf. This rejects fixed sequences (their nodes differ\n // in type, or there's only one) so we never treat them as splice-able lists.\n let elemType: string | undefined\n let elemCount = 0\n for (const k of kids) {\n if (k._tag !== 'node') continue\n elemCount++\n if (elemType === undefined) elemType = k.type\n else if (k.type !== elemType) return null // heterogeneous \u2192 not a collection\n }\n if (elemType === undefined || elemCount < 2) return null\n const ruleFn = registry[elemType]\n if (!ruleFn) return null\n // The separator is the leaf that appears between two elements; require it to be\n // consistent everywhere two elements meet (a real list delimiter).\n let separator: string | null = null\n for (let i = 1; i < kids.length - 1; i++) {\n if (kids[i - 1]!._tag === 'node' && kids[i + 1]!._tag === 'node') {\n if (kids[i]!._tag !== 'leaf') return null\n const sep = kids[i]!.value ?? null\n if (separator === null) separator = sep\n else if (sep !== separator) return null // inconsistent delimiter \u2192 not a plain list\n }\n }\n if (separator === null) return null // \u22652 elements but no delimiter between any pair\n\n // The disturbed middle must be pure elements + separators. If the collection is\n // bracketed, its OPENING / CLOSING delimiter (a leaf whose value isn't the\n // separator, sitting before the first / after the last element) must lie OUTSIDE\n // the edit \u2014 otherwise the edit changed the collection's own framing and a\n // whole-rule reparse (not a splice) is required.\n const first = kids[0]!\n const last = kids[kids.length - 1]!\n if (first._tag === 'leaf' && first.value !== separator && from < cStart + first.span.end) return null\n if (last._tag === 'leaf' && last.value !== separator && to > cStart + last.span.start) return null\n\n const reStart = h > 0 ? cStart + kids[h - 1]!.span.end : cStart\n const reEnd = cStart + kids[t]!.span.start + delta\n if (reEnd < reStart) return null\n\n const middle = parseMiddle(newInput, reStart, reEnd, cStart, ruleFn as RuleFn<NodeLike>, separator, build, tolerant)\n if (!middle) return null\n\n // Junction check: `tail` always begins with an ELEMENT (we advanced `t` to a\n // node), so a non-empty middle must END with a SEPARATOR to connect validly \u2014\n // otherwise the edit deleted the delimiter between the middle's last element and\n // the tail's first, and splicing would fuse two elements with no separator (a\n // `sepBy` violation the whole-collection reparse would never produce). When the\n // middle is empty the tail is preceded by `head`'s last token, itself already a\n // separator or the open delimiter (head was backed off to a leaf), so it's fine.\n if (middle.length > 0) {\n const last = middle[middle.length - 1]!\n if (!(last._tag === 'leaf' && last.value === separator)) return null\n }\n\n // Lookahead guard: the middle must have read nothing at/after `reEnd`, else it\n // peeked into the reused tail. Re-run over an input whose tail is overwritten\n // with a sentinel and require an identical middle. Two sentinels so the real\n // byte at `reEnd` can't accidentally match the probe.\n if (reEnd < newInput.length) {\n for (const sentinel of [' ', '\uFFFF']) {\n if (newInput[reEnd] === sentinel) continue\n const probed = newInput.slice(0, reEnd) + sentinel.repeat(newInput.length - reEnd)\n const probe = parseMiddle(probed, reStart, reEnd, cStart, ruleFn as RuleFn<NodeLike>, separator, build, tolerant)\n if (!probe || probe.length !== middle.length) return null\n for (let i = 0; i < middle.length; i++) if (!structurallyEqual(probe[i], middle[i])) return null\n }\n }\n\n const head = kids.slice(0, h)\n const tail = kids.slice(t).map((k) => shiftRelStart(k, delta))\n const newC = {\n ...C,\n span: { start: C.span.start, end: C.span.end + delta },\n children: [...head, ...middle, ...tail],\n } as N\n\n // Replace C at `path` and slide C's own trailing siblings / ancestor ends.\n return graftRelative(root, path, newC, delta)\n}\n\n// ---------------------------------------------------------------------------\n// Stage-2 soundness guard\n// ---------------------------------------------------------------------------\n\n/**\n * Deep structural equality on parse trees: `_tag`, `span`, node `type`, leaf\n * `value`, and children pairwise. This is the oracle relation `.edit()` must\n * preserve against a full reparse; it's also what the Stage-2 guard compares\n * probe results with.\n */\nexport function structurallyEqual(a: unknown, b: unknown): boolean {\n if (a === b) return true\n if (typeof a !== 'object' || typeof b !== 'object' || a === null || b === null) return false\n const at = (a as { _tag?: string })._tag\n const bt = (b as { _tag?: string })._tag\n if (at !== bt) return false\n const as = (a as { span?: { start: number; end: number } }).span\n const bs = (b as { span?: { start: number; end: number } }).span\n if (as || bs) {\n if (!as || !bs || as.start !== bs.start || as.end !== bs.end) return false\n }\n if (at === 'leaf' || at === 'trivia') {\n return (a as { value: string }).value === (b as { value: string }).value\n }\n if (at === 'parseError') {\n // Embedded recovered error: span already compared above; also compare the\n // expected-token set so two errors at the same span but different expectations\n // are not conflated (the oracle must distinguish them).\n const ae = (a as { expected?: readonly string[] }).expected ?? []\n const be = (b as { expected?: readonly string[] }).expected ?? []\n return ae.length === be.length && ae.every((x, i) => x === be[i])\n }\n if ((a as { type?: string }).type !== (b as { type?: string }).type) return false\n const ac = (a as { children?: readonly unknown[] }).children\n const bc = (b as { children?: readonly unknown[] }).children\n if (ac || bc) {\n if (!ac || !bc || ac.length !== bc.length) return false\n for (let i = 0; i < ac.length; i++) if (!structurallyEqual(ac[i], bc[i])) return false\n }\n return true\n}\n\n/**\n * A reused suffix spliced at `boundary` (new-input coords) is sound only if the\n * re-parse of the containing rule did NOT read any input at or after `boundary`\n * \u2014 otherwise a lookahead or backtrack peeked across the splice and the reused\n * tail could be wrong. We prove independence by re-running the same rule at the\n * same start on an input whose entire tail from `boundary` is overwritten with a\n * sentinel: if the produced node is byte-for-byte structurally identical,\n * nothing past `boundary` was inspected. Two distinct sentinels are tried so the\n * real char at `boundary` can't accidentally equal the probe. Conservative by\n * construction \u2014 any probe difference, failure, or throw \u21D2 not safe \u21D2 the caller\n * widens toward a full reparse (correctness over reuse fraction).\n */\nfunction boundaryIsSafe<N extends NodeLike>(\n ruleFn: RuleFn<N>,\n newInput: string,\n start: number,\n boundary: number,\n state: unknown,\n build: ParseContext['build'],\n produced: ParseResult<N>,\n tolerant: boolean,\n): boolean {\n if (!produced.ok) return false\n if (boundary >= newInput.length) return true // nothing after the node to peek at\n for (const sentinel of [' ', '\uFFFF']) {\n if (newInput[boundary] === sentinel) continue\n const probed = newInput.slice(0, boundary) + sentinel.repeat(newInput.length - boundary)\n const ctx: ParseContext = mkCtx(state, build, tolerant)\n let r: ParseResult<N>\n try {\n r = ruleFn(probed, start, ctx)\n } catch {\n return false\n }\n if (!r.ok || r.span.end !== produced.span.end) return false\n if (!structurallyEqual(r.value, produced.value)) return false\n }\n return true\n}\n\n// ---------------------------------------------------------------------------\n// Document\n// ---------------------------------------------------------------------------\n\nclass ParseDocImpl<N extends NodeLike> implements ParseDoc<N> {\n private readonly _registry: Registry<N>\n /** Registry entries normalized to callable parse functions (memoized once). */\n private readonly _fns: Record<string, RuleFn<N>>\n /** Rule names the grammar proves are genuine repetitions \u2014 the ONLY splice-safe types. */\n private readonly _reps: Set<string>\n private readonly _rootRule: string\n private readonly _opts: ParseDocOptions<N>\n /**\n * The tree, held in whichever coordinate system it arrived in. A fresh parse\n * arrives ABSOLUTE (`_abs` set); an incremental graft arrives PARENT-RELATIVE\n * (`_rel` set). The public `tree` is always the RELATIVE form (so untouched\n * subtrees stay shared by identity across edits and reads are O(1)); it's\n * materialized from `_abs` once, on demand, and memoized. `undefined` = not yet\n * computed; `null` = failed parse. A fresh parse that's never edited nor\n * tree-read pays no conversion \u2014 important for the full-reparse fallback path.\n */\n private _abs: N | null | undefined\n private _rel: N | null | undefined\n readonly errors: ParseError[]\n readonly unconsumedFrom: number | null\n readonly input: string\n\n constructor(\n registry: Registry<N>,\n rootRule: string,\n opts: ParseDocOptions<N>,\n trees: { abs?: N | null; rel?: N | null },\n errors: ParseError[],\n unconsumedFrom: number | null,\n input: string,\n ) {\n this._registry = registry\n this._fns = {}\n this._reps = new Set()\n for (const [name, entry] of Object.entries(registry)) {\n this._fns[name] = asRuleFn(entry)\n if (producesRepetition(defOf(entry))) this._reps.add(name)\n }\n this._rootRule = rootRule\n this._opts = opts\n this._abs = trees.abs\n this._rel = trees.rel\n this.errors = errors\n this.unconsumedFrom = unconsumedFrom\n this.input = input\n }\n\n /**\n * The parse tree with PARENT-RELATIVE spans \u2014 a node's `span` is relative to\n * its parent's start (root base 0). This is the shareable representation:\n * untouched subtrees keep the same identity across `.edit()`s, and reading the\n * tree after a length-changing edit is O(1) (no offset rewrite). For absolute\n * positions use the O(depth) cursor `spanAt(path)`, or `absolutizeCST(tree)` to\n * materialize the whole absolute tree. (A fresh non-incremental `node().parse()`\n * result is unchanged \u2014 still absolute.)\n */\n get tree(): N | null {\n if (this._rel === undefined) {\n this._rel = this._abs ? (relativizeCST(this._abs as unknown as N & { span: Span }, 0) as unknown as N) : null\n }\n return this._rel\n }\n\n spanAt(path: readonly number[]): { start: number; end: number } {\n const rel = this.tree\n if (!rel) throw new Error('spanAt on a failed parse (tree is null)')\n return absoluteSpanCST(rel as unknown as { span: Span; children?: readonly unknown[] }, path)\n }\n\n /**\n * Wrap a reused (grafted/spliced) RELATIVE tree, recomputing `errors` and\n * `unconsumedFrom` from it so both stay consistent with the tree across the\n * edit (the flat errors are recovered errors embedded in the reused subtrees;\n * unconsumedFrom re-derives trailing junk over the new input).\n */\n private wrapReuse(newTree: N, newInput: string): ParseDoc<N> {\n const trivia = triviaOf(this._registry[this._rootRule], this._opts)\n const end = (newTree as unknown as { span: { end: number } }).span.end\n const errors = collectEmbeddedErrors(newTree, 0, [])\n const unconsumedFrom = unconsumedAfter(end, newInput, trivia)\n return new ParseDocImpl(this._registry, this._rootRule, this._opts, { rel: newTree }, errors, unconsumedFrom, newInput)\n }\n\n edit(from: number, to: number, replacement: string): ParseDoc<N> {\n const newInput = this.input.slice(0, from) + replacement + this.input.slice(to)\n const reparse = () => parseDoc(this._registry, this._rootRule, newInput, this._opts)\n\n const root = this.tree\n if (!root) return reparse()\n\n const delta = replacement.length - (to - from)\n // When `from` sits on the root's own boundary (e.g. right after a top-level\n // element, before its separator), no child node contains it and\n // `findContaining` returns null \u2014 but the root still does. Fall back to the\n // root as the container so the structural splice below still gets a shot\n // (this is exactly the \"insert/delete a top-level list element\" position).\n const found = findContaining(root, 0, from) ?? { node: root, path: [] as number[] }\n\n // Try the innermost containing rule first, then widen outward.\n const ancestors = ancestorsAt(root, found.path)\n const candidates: FoundNode<N>[] = [found]\n const pathCopy = [...found.path]\n for (let i = ancestors.length - 2; i >= 0; i--) {\n pathCopy.pop()\n candidates.push({ node: ancestors[i + 1]!, path: [...pathCopy] })\n }\n\n const rebuild = this._opts.rebuild ?? defaultRebuild\n for (const { node, path } of candidates) {\n // Absolute span of this candidate, projected from the relative root (O(depth)).\n const { start: absStart, end: absEnd } = absoluteSpanCST(root as unknown as { span: Span; children?: readonly unknown[] }, path)\n // Reentry only pays off when the re-parsed rule is substantially smaller\n // than the whole document. Candidates widen deepest\u2192root (monotonically\n // growing span), so once one covers most of the input, reparsing it \u2014 and\n // every larger ancestor after it \u2014 can't beat a full reparse and would just\n // be wasted work before the fallback. Bail to the full reparse NOW. This\n // caps `.edit()` at ~one full reparse in the worst case (e.g. a structural\n // insert near the front) instead of stacking several near-full reparses.\n if (absEnd - absStart > this.input.length * REENTRY_MAX_SPAN_FRACTION) break\n const ruleFn = this._fns[node.type]\n if (!ruleFn) continue\n // The reused subtree must FULLY CONTAIN the edited range (old coords).\n // `findContaining` only locates `from`; if the edit's end `to` spills past\n // this node's end, the edit also changed a sibling/separator after it, and\n // reusing the untouched suffix would be unsound \u2014 widen to an ancestor\n // that does span the whole edit (ultimately a full reparse).\n if (!(absStart <= from && to <= absEnd)) continue\n const ctx: ParseContext = mkCtx(node.state, this._opts.build, !!this._opts.tolerant)\n const r = ruleFn(newInput, absStart, ctx)\n if (!r.ok) continue\n if (r.span.end !== absEnd + delta) continue\n\n // Stage-2 soundness guard: only reuse the untouched suffix if the re-parse\n // provably read no input past its own end (else a lookahead/backtrack\n // crossed the splice). Widen to the next candidate \u2014 ultimately a full\n // reparse \u2014 when it can't be proven.\n if (!boundaryIsSafe(ruleFn, newInput, absStart, absEnd + delta, node.state, this._opts.build, r, !!this._opts.tolerant)) {\n continue\n }\n\n // The re-parse produced an ABSOLUTE subtree; rebase it to parent-relative\n // for splicing into the relative tree. The parent's absolute start is\n // `absStart - node.span.start` (node.span is already relative to it).\n const parentBase = absStart - node.span.start\n const newRel = relativizeCST(r.value as unknown as N & { span: Span }, parentBase) as unknown as N\n\n // delta === 0: relative spans are unchanged, so the spine graft (sharing\n // every untouched sibling by reference) is already correct.\n if (delta === 0) {\n const newTree = replaceAtPath(rebuild, root, path, newRel)\n return this.wrapReuse(newTree, newInput)\n }\n // Length-changing edit: trailing siblings' relative starts slide by `delta`.\n // A custom `rebuild` (possibly a class instance) can't have its span slid\n // safely, so fall back to a full, correct reparse.\n if (this._opts.rebuild) return reparse()\n const newTree = graftRelative(root, path, newRel, delta)\n return this.wrapReuse(newTree, newInput)\n }\n\n // No localized rule reparse converged \u2014 the edit is structural. Before paying\n // a full reparse, try reusing the untouched tail of a containing collection\n // (add/remove an element in a list). We only ever splice a rule the GRAMMAR\n // proves is a genuine repetition (`this._reps`, from its combinator def) \u2014 a\n // fixed-arity same-typed sequence is structurally indistinguishable from a list\n // by its CST alone, so splicing it could accept a wrong element count; it's\n // excluded here and falls back to a full, correct reparse. Innermost containing\n // collection first; a candidate whose disturbed middle doesn't tile cleanly\n // returns null and we widen. Correctness net is the incremental oracle fuzz.\n if (this._opts.structuralReuse && delta !== 0 && !this._opts.rebuild) {\n // Candidates run innermost\u2192ancestor but never include the root itself; the\n // splice-able collection can BE the root (e.g. a top-level `sepBy` list), so\n // consider it last.\n const spliceCandidates: FoundNode<N>[] = [...candidates, { node: root, path: [] }]\n for (const { node, path } of spliceCandidates) {\n if (!this._reps.has(node.type)) continue // grammar didn't prove this rule a repetition\n const { start: cStart, end: cEnd } = absoluteSpanCST(root as unknown as { span: Span; children?: readonly unknown[] }, path)\n if (!(cStart <= from && to <= cEnd)) continue\n const spliced = tryListSplice(root, path, node, cStart, newInput, from, to, delta, this._fns, this._opts.build, !!this._opts.tolerant)\n if (spliced) return this.wrapReuse(spliced, newInput)\n }\n }\n\n return reparse()\n }\n}\n\n/**\n * The relative (parent-offset) tree backing a doc. Currently identical to the\n * public `doc.tree` (which is relative); kept as a named internal handle for the\n * reuse-metric tests, which assert on the shareable representation explicitly.\n */\nexport function relTreeOf<N extends NodeLike>(doc: ParseDoc<N>): N | null {\n return doc.tree\n}\n\n/**\n * The grammar trivia rule to skip when computing `unconsumedFrom`: an explicit\n * `trivia` option wins; otherwise the root entry's ambient `grammarTrivia` (the\n * same source `run()` derives it from). A bare-function registry carries no meta,\n * so trailing trivia can't be inferred and the parse must reach the exact end.\n */\nfunction triviaOf<N extends NodeLike>(\n entry: RuleFn<N> | Combinator<N> | undefined,\n opts: ParseDocOptions<N>,\n): Combinator<unknown> | undefined {\n if (opts.trivia) return opts.trivia\n // Only an interpreter Combinator carries `_meta`; a bare parse function or a\n // compiled-grammar object (which bakes its ambient trivia into codegen) does\n // not, so there's nothing to derive and trailing trivia isn't skipped.\n if (entry === undefined || typeof entry === 'function') return undefined\n const meta = (entry as { _meta?: { grammarTrivia?: Combinator<unknown> } })._meta\n return meta ? meta.grammarTrivia : undefined\n}\n\n/**\n * First unconsumed non-trivia offset after a parse that ended at `end` (trailing\n * trivia skipped when a trivia rule is available), or `null` when the whole input\n * was consumed \u2014 byte-for-byte the computation in `run()` (run.ts:117-129).\n */\nfunction unconsumedAfter(end: number, input: string, trivia: Combinator<unknown> | undefined): number | null {\n let pos = end\n if (trivia && pos < input.length) {\n const t = trivia.parse(input, pos, { trackLines: false })\n if (t.ok && t.span.end > pos) pos = t.span.end\n }\n return pos < input.length ? pos : null\n}\n\n/**\n * Collect embedded `parseError` recovery nodes from a PARENT-RELATIVE tree,\n * projecting each to an ABSOLUTE span (root base 0) \u2014 the flat mirror of the\n * errors that ride the tree, recomputed for a reuse-path (grafted/spliced) result\n * so `doc.errors` stays consistent with the tree across incremental edits.\n */\nfunction collectEmbeddedErrors(node: unknown, base: number, out: ParseError[]): ParseError[] {\n const c = node as { _tag?: string; span?: { start: number; end: number }; expected?: string[]; children?: readonly unknown[] }\n if (!c || typeof c !== 'object' || !c.span) return out\n const start = base + c.span.start\n if (c._tag === 'parseError') {\n out.push({ _tag: 'parseError', span: { start, end: base + c.span.end }, expected: c.expected ?? [] })\n return out\n }\n if (Array.isArray(c.children)) for (const k of c.children) collectEmbeddedErrors(k, start, out)\n return out\n}\n\n/**\n * Parse `input` from `rootRule` and wrap the result in a ParseDoc that can\n * be incrementally re-parsed via `.edit()`.\n */\nexport function parseDoc<N extends NodeLike>(\n registry: Registry<N>,\n rootRule: string,\n input: string,\n opts: ParseDocOptions<N> = {},\n): ParseDoc<N> {\n const entry = registry[rootRule]\n if (!entry) throw new Error(`No rule '${rootRule}' in registry`)\n const ctx: ParseContext = mkCtx(opts.state, opts.build, !!opts.tolerant)\n const r: ParseResult<N> = asRuleFn(entry)(input, 0, ctx)\n if (r.ok) {\n // A fresh parse is ABSOLUTE; the relative form is materialized lazily on the\n // first edit/spanAt, so a parse that's never edited pays no conversion. The\n // recovery errors the tolerant parse collected into ctx._errors are surfaced\n // flat too (like run()), and unconsumedFrom reports any trailing junk.\n const errors = ctx._errors ? [...ctx._errors] : []\n const unconsumedFrom = unconsumedAfter(r.span.end, input, triviaOf(entry, opts))\n return new ParseDocImpl(registry, rootRule, opts, { abs: r.value }, errors, unconsumedFrom, input)\n }\n // Hard (non-recovered) failure: the single top-level failure, as a parseError.\n const fail: ParseError = { _tag: 'parseError', span: r.span, expected: r.expected }\n return new ParseDocImpl(registry, rootRule, opts, { abs: null }, [fail], null, input)\n}\n"],
5
5
  "mappings": ";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACcO,SAAS,iBAAiB,KAA4B;AAC3D,SAAO,IAAI,YAA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,KAAK,QAAQ;AACzB,aAAO,SAAS,KAAK,QAAQ,cAAc,IAAI,MAAM,GAAG,MAAM,CAAC,EAAE,KAAK;AAAA,IACxE;AAAA,IAEA,aAAa,KAAK;AAChB,UAAI,CAAC,YAAY,SAAS,QAAW;AACnC,cAAM,IAAI,MAAM,8GAAyG;AAAA,MAC3H;AACA,YAAM,OAAO,aAAa;AAC1B,YAAM,OAAO,CAAC,QAA2D;AAEvE,cAAM,MAAM,IAAI,OAAQ,cAAc,IAAI,IAAa,IAA0B;AACjF,eAAO;AAAA,UACL,IAAI,OAAO;AAAE,mBAAO;AAAA,UAAI;AAAA,UACxB,MAAM,CAAC,MAAM,IAAI,gBAAgB,KAAK,IAAI,KAAK,MAAM,IAAI,WAAW,CAAC;AAAA;AAAA;AAAA;AAAA,UAIrE,aAAa,MAAM,iBAAiB,KAAK,IAAI,QAAQ,IAAI,gBAAgB,IAAI,MAAM,QAAQ,MAAM;AAAA,UACjG,eAAe,CAAC,WAAW,SAAS,IAAI,OAAO,QAAQ,GAAG;AAAA,QAC5D;AAAA,MACF;AACA,aAAO,KAAK,SAAmB,UAAU,MAAM,KAAK,EAAE,UAAU,MAAM,iBAAiB,MAAM,OAAO,KAAK,CAAC,CAAC;AAAA,IAC7G;AAAA,EACF;AACF;AAIA,SAAS,aAAa,MAAe,QAA+B;AAClE,MAAI,CAAC,QAAQ,OAAO,SAAS,SAAU,QAAO;AAC9C,MAAI,OAAsB;AAC1B,OAAK,MAAgB;AAAA,IACnB,MAAMF,OAAM;AACV,YAAM,IAAIA,MAAK;AACf,UAAI,KAAK,EAAE,SAAS,UAAU,UAAU,EAAE,OAAOA,MAAK,SAAS,OAAW,QAAOA,MAAK;AAAA,IACxF;AAAA,EACF,CAAC;AACD,SAAO;AACT;",
6
6
  "names": ["children", "span", "rawChildren", "triviaLog", "state", "node", "node", "node", "last", "trivia", "newTree", "trivia", "node", "rules", "node", "trivia", "label"]
7
7
  }