Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 55 additions & 2 deletions src/stdlib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 (`(?=`, `(?!`,
* `(?<=`, `(?<!`). Escaped parens and parens inside char classes are
* literals. Keep this in step with compile-item.ts if its group
* conventions ever change.
*/
export function countCaptureGroups(re: string): number {
let count = 0
let i = 0
while (i < re.length) {
const c = re[i]
if (c === "\\") {
i += 2
continue
}
if (c === "[") {
// Char class: scan to the closing bracket, honouring escapes.
i++
while (i < re.length && re[i] !== "]") {
if (re[i] === "\\") i++
i++
}
i++
continue
}
if (c === "(" && re[i + 1] !== "?") count++
i++
}
return count
}

/**
* Lowercase a string. Maps to `Interscript.functions.downcase`.
*/
Expand Down Expand Up @@ -179,6 +214,11 @@ export function decompose(input: string): string {
*
* Onigmo and V8 share alternation semantics: at each scan position,
* alternatives are tried in declaration order and the first match wins.
*
* Capture templates resolve against the RULE'S OWN capture list (rs
* semantics, WO18 G1): the wrapping groups occupy the leading slots of
* the global group list, so each rule's replacement receives only the
* slice of numbered groups belonging to its own pattern.
*/
export interface MegaregexpRule {
readonly pattern: string
Expand All @@ -189,13 +229,26 @@ export function parallelMegaregexp(input: string, rules: readonly MegaregexpRule
if (rules.length === 0) return input
const parts = rules.map((r, i) => `(?<__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<string, string | undefined>
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
Expand Down
72 changes: 72 additions & 0 deletions test/isc/capture-parallel.test.ts
Original file line number Diff line number Diff line change
@@ -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")
})
})
69 changes: 69 additions & 0 deletions test/stdlib.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { describe, it, expect } from "vitest"
import {
countCaptureGroups,
parallelMegaregexp,
parallelReplace,
regexpEscape,
titleCase,
Expand All @@ -8,6 +10,7 @@ import {
upcase,
compose,
decompose,
type MegaregexpRule,
} from "../src/stdlib.js"

describe("stdlib", () => {
Expand Down Expand Up @@ -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("(?<!x)(?!y)")).toBe(0)
})

it("ignores escaped parens and parens inside char classes", () => {
expect(countCaptureGroups("\\(x\\)")).toBe(0)
expect(countCaptureGroups("[a(b]c")).toBe(0)
expect(countCaptureGroups("[(\\(]")).toBe(0)
})
})
})
Loading