Skip to content

fix(runtime): resolve megaregexp capture refs against the rule's own groups - #135

Merged
ronaldtse merged 1 commit into
mainfrom
wo18-g1-capture-parallel
Nov 13, 2026
Merged

ronaldtse merged 1 commit into
mainfrom
wo18-g1-capture-parallel

Conversation

@ronaldtse

Copy link
Copy Markdown
Contributor

Summary

WO18 G1 from the maps parity audit: capture(...)/ref(N) inside a parallel block produced silently wrong output.

The megaregexp join wraps every rule in a named group ((?<__r0>...)), so the wrapper groups occupy the leading slots of the global numbered-group list. The replace callback then handed that global list to the template resolver, so $1 in ref(1) resolved to the wrapper's whole match instead of the rule's first capture.

Semantics chosen: correct output (rs parity) — the offset math is exact because the pattern grammar is fully controlled by compile-item.ts (the only capturing construct is capture_group compiled as a bare (...); all other groups are (?:...) or lookarounds). Rejection was unnecessary.

Change

  • src/stdlib.ts: parallelMegaregexp now computes, per rule, the number of numbered groups its own pattern emits (countCaptureGroups) and hands the replacement only the rule-owned slice of the global group list.
  • New probes copied verbatim from the audit battery (docs/parity/probes/maps): ax -> aX, axb -> aXB, k3a -> 3ka.

Evidence

Red (before the fix, audit values reproduced exactly):

× capture-parallel-match: ax -> aX (not axX)      Expected "aX"  Received "axX"
× capture-parallel: axb -> aXB                    Expected "aXB" Received "axXB"
× capture-par2-shift: k3a -> 3ka                  Expected "3ka" Received "kk3a"
Tests  9 failed | 15 passed (24)

Green after the fix: 24 passed (24) for the two touched files; full npm test shows only pre-existing network-flaky failures (cli/client-work/imf) — the same families fail on a pristine origin/main worktree baseline (16 failures there vs 9 here, all network-timeout). Generated-parser, golden and parity suites untouched and green. Grammar files untouched.

Test plan

  • npx vitest run test/stdlib.test.ts test/isc/capture-parallel.test.ts — red before, green after
  • full npm test — no new failures vs origin/main baseline
  • tsc --noEmit and eslint clean

…groups

WO18 G1 (maps parity audit): a parallel block joined into one
alternation wraps each rule in a named group, and the replace callback
handed the template resolver the GLOBAL group list - so a ref(1)
template in capture-in-parallel rules resolved to the wrapper's whole
match instead of the rule's first capture (ax -> axX, k3a -> kk3a).

parallelMegaregexp now counts each rule's numbered capture groups
(countCaptureGroups mirrors compile-item's group-emission grammar) and
hands the replacement only the slice of global groups the matched rule
owns, matching rs interpreter semantics. Probe maps from the audit's
battery pin the canonical outputs (aX / aXB / 3ka).
@ronaldtse
ronaldtse merged commit 9f9bdcd into main Nov 13, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant