diff --git a/src/stdlib.ts b/src/stdlib.ts index 3378eac..9912a67 100644 --- a/src/stdlib.ts +++ b/src/stdlib.ts @@ -120,6 +120,41 @@ export function regexpEscape(input: string): string { return input.replace(/[.*+?^${}()|[\]\\]/g, "\\$&") } +/** + * Count the numbered capture groups a RegExp source emits. + * + * Mirrors compile-item.ts's emission grammar: the ONLY capturing + * construct is `capture_group`, compiled as a bare `(...)`; every other + * group is non-capturing (`(?:...)`) or a lookaround (`(?=`, `(?!`, + * `(?<=`, `(? `(?<__r${i}>${r.pattern})`) const re = new RegExp(parts.join("|"), "gmu") + // Numbered groups BEFORE each rule's wrapper: one wrapper per earlier + // rule plus each earlier rule's inner captures. Rule i's own captures + // therefore occupy global slice [before + 1, before + 1 + count). + const before: number[] = [] + const counts: number[] = [] + let base = 0 + for (const r of rules) { + before.push(base) + const n = countCaptureGroups(r.pattern) + counts.push(n) + base += 1 + n + } return input.replace(re, (...args: unknown[]) => { const groups = args[args.length - 1] as Record for (let i = 0; i < rules.length; i++) { if (groups[`__r${i}`] !== undefined) { const match = args[0] as string - const captures = args.slice(1, -2) as (string | undefined)[] - return rules[i]!.replace(match, captures) + const all = args.slice(1, -2) as (string | undefined)[] + const start = before[i]! + 1 + return rules[i]!.replace(match, all.slice(start, start + counts[i]!)) } } return args[0] as string diff --git a/test/isc/capture-parallel.test.ts b/test/isc/capture-parallel.test.ts new file mode 100644 index 0000000..6953b34 --- /dev/null +++ b/test/isc/capture-parallel.test.ts @@ -0,0 +1,72 @@ +/** + * WO18 G1 — capture/ref inside parallel blocks. + * + * The parity audit (maps docs/parity/README.md G1) found that ts/py + * join a parallel block into one alternation wrapping each rule in a + * named group, then handed the template resolver the GLOBAL group list + * — so `$1` resolved to the wrapper's whole match instead of the + * rule's first capture (`ax` → `axX` instead of `aX`). + * + * Canonical behavior is rs (`tst-capture-par2` pins it): + * `$N` resolves against the matched rule's own capture list. The maps + * here are the audit's probe maps (docs/parity/probes/maps), embedded + * verbatim. + * + * A capture in `from` forces the block off the trie path (captures are + * not literal-expandable) onto the megaregexp path, so these reach the + * exact code the audit flagged. + */ + +import { describe, it, expect, beforeEach } from "vitest" +import { parseIsc } from "../../src/isc/parser.ts" +import { iscBundledStrategy } from "../../src/isc/loader.ts" +import { configure, reset, transliterate } from "../../src/index.js" + +const PARALLEL_MAP = `# P10: capture/ref inside a parallel block (block-rule form). +system "T:tst-capture-parallel:1" { + stage main { + parallel { + sub { + from capture(any("a".."z")) + "x" + to ref(1) + "X" + } + sub "b" "B" + } + } +}` + +const PAR2_MAP = `# P10c: capture AFTER a plain rule in the same parallel block — +# group-numbering shift in the megaregexp join. +system "T:tst-capture-par2:1" { + stage main { + parallel { + sub "k" "K" + sub { + from capture(any("a".."z")) + capture(any("0".."9")) + to ref(2) + ref(1) + } + } + } +}` + +function loadMap(src: string): string { + const code = parseIsc(src, "probe").systemCode + configure({ strategies: [iscBundledStrategy({ [code]: src })] }) + return code +} + +describe("WO18 G1 — capture/ref inside parallel blocks", () => { + beforeEach(() => reset()) + + it("capture-parallel-match: ax -> aX (not axX)", () => { + expect(transliterate(loadMap(PARALLEL_MAP), "ax")).toBe("aX") + }) + + it("capture-parallel: axb -> aXB (the plain rule still applies)", () => { + expect(transliterate(loadMap(PARALLEL_MAP), "axb")).toBe("aXB") + }) + + it("capture-par2-shift: k3a -> 3ka (capture offsets past the earlier rule)", () => { + expect(transliterate(loadMap(PAR2_MAP), "k3a")).toBe("3ka") + }) +}) diff --git a/test/stdlib.test.ts b/test/stdlib.test.ts index e492599..7e8be8c 100644 --- a/test/stdlib.test.ts +++ b/test/stdlib.test.ts @@ -1,5 +1,7 @@ import { describe, it, expect } from "vitest" import { + countCaptureGroups, + parallelMegaregexp, parallelReplace, regexpEscape, titleCase, @@ -8,6 +10,7 @@ import { upcase, compose, decompose, + type MegaregexpRule, } from "../src/stdlib.js" describe("stdlib", () => { @@ -95,4 +98,70 @@ describe("stdlib", () => { expect(c).toBe(s) }) }) + + // WO18 G1 — the megaregexp join wraps each rule in a named group. + // $N templates must resolve against the RULE's own capture list (rs + // semantics), not the global group list that includes the wrappers. + describe("parallelMegaregexp", () => { + it("first alternative wins at each position", () => { + const rules: MegaregexpRule[] = [ + { pattern: "ab", replace: () => "1" }, + { pattern: "a", replace: () => "2" }, + ] + expect(parallelMegaregexp("ab", rules)).toBe("1") + }) + + it("gives the rule its own captures, not the wrapper's whole match", () => { + const rules: MegaregexpRule[] = [ + { + pattern: "([a-z])x", + replace: (m, g) => (g[0] !== undefined ? g[0] + "X" : m), + }, + { pattern: "b", replace: (m) => m.toUpperCase() }, + ] + expect(parallelMegaregexp("axb", rules)).toBe("aXB") + }) + + it("offsets a later rule's captures past earlier rules' inner groups", () => { + const rules: MegaregexpRule[] = [ + { + pattern: "([a-z])x", + replace: (_m, g) => (g[0] !== undefined ? g[0] + "X" : _m), + }, + { + pattern: "b([0-9])", + replace: (_m, g) => g[0] ?? "?", + }, + ] + // "ax" hits rule 0; "b5" hits rule 1, whose only capture is "5" — + // the wrapper of rule 0 (and of rule 1) must not shift it. + expect(parallelMegaregexp("axb5", rules)).toBe("aX5") + }) + + it("keeps rule-local numbering when a single rule has two captures", () => { + const rules: MegaregexpRule[] = [ + { pattern: "([a-z])([0-9])", replace: (_m, g) => g[1]! + g[0]! }, + ] + expect(parallelMegaregexp("k3", rules)).toBe("3k") + }) + }) + + describe("countCaptureGroups", () => { + it("counts bare capture groups", () => { + expect(countCaptureGroups("([a-z])x")).toBe(1) + expect(countCaptureGroups("([a-z])([0-9])")).toBe(2) + }) + + it("ignores non-capturing groups and lookarounds", () => { + expect(countCaptureGroups("(?:ab)")).toBe(0) + expect(countCaptureGroups("(?<=x)y(?=z)")).toBe(0) + expect(countCaptureGroups("(? { + expect(countCaptureGroups("\\(x\\)")).toBe(0) + expect(countCaptureGroups("[a(b]c")).toBe(0) + expect(countCaptureGroups("[(\\(]")).toBe(0) + }) + }) })