diff --git a/interscript-py/src/interscript/executor.py b/interscript-py/src/interscript/executor.py index 1d657734..a699bd9b 100644 --- a/interscript-py/src/interscript/executor.py +++ b/interscript-py/src/interscript/executor.py @@ -359,8 +359,10 @@ def replace_fn(m: str, groups: list[str | None], _f=replacement): class _MegaMatch: - """Adapter so _resolve_template can read megaregexp captures, whose - global numbering already includes the wrapping named group.""" + """Adapter so _resolve_template can read megaregexp captures. The + group list is already RULE-LOCAL (parallel_megaregexp strips the + wrapping named groups, WO18 G1), so $N indexes the matched rule's + own captures, matching rs.""" def __init__(self, whole: str, groups: list[str | None]) -> None: self._whole = whole diff --git a/interscript-py/src/interscript/stdlib.py b/interscript-py/src/interscript/stdlib.py index e29927f1..6b44ffbc 100644 --- a/interscript-py/src/interscript/stdlib.py +++ b/interscript-py/src/interscript/stdlib.py @@ -119,9 +119,11 @@ def parallel_megaregexp(text: str, rules: list[MegaregexpRule]) -> str: `parallelMegaregexp`. Each alternative is wrapped in a uniquely-named group so the - callback can tell which rule matched; the group also shifts inner - capture numbering exactly as in the TS/Ruby engines, so `$1` - templates resolve against the same global group list. + callback can tell which rule matched. The wrappers occupy the + leading slots of the global numbered-group list, so the replacement + receives only the SLICE of groups belonging to the matched rule's + own pattern (rs semantics, WO18 G1): `$1` templates resolve against + the rule's first capture, never the wrapper's whole match. """ if not rules: return text @@ -129,11 +131,25 @@ def parallel_megaregexp(text: str, rules: list[MegaregexpRule]) -> str: mega = regex.compile("|".join(parts), REGEX_FLAGS) n_groups = mega.groups + # Numbered groups BEFORE each rule's wrapper: one wrapper per + # earlier rule plus each earlier rule's inner captures. The wrapper + # itself is global group (base + 1) — list index base — so the + # rule's own captures occupy list slice [base + 1, base + 1 + n). + # Compiling each pattern on its own yields its exact group count — + # the same grammar the alternation embeds. + offsets: list[tuple[int, int]] = [] + base = 0 + for r in rules: + n = regex.compile(r.pattern, REGEX_FLAGS).groups + offsets.append((base + 1, n)) + base += 1 + n + def _sub(m: regex.Match) -> str: for i, rule in enumerate(rules): if m.group(f"__r{i}") is not None: + start, n = offsets[i] captures = [m.group(k) for k in range(1, n_groups + 1)] - return rule.replace(m.group(0), captures) + return rule.replace(m.group(0), captures[start : start + n]) return m.group(0) return mega.sub(_sub, text) diff --git a/interscript-py/tests/test_executor.py b/interscript-py/tests/test_executor.py index 76e645c5..07166bc1 100644 --- a/interscript-py/tests/test_executor.py +++ b/interscript-py/tests/test_executor.py @@ -111,6 +111,50 @@ def test_megaregexp_mode_triggered_by_constraints(self): m = make_map([stage("main", [ParallelRule(rules=tuple(rules))])]) assert execute_stage(m, "main", "ac a") == "Xc Y" + # WO18 G1 — capture/ref inside parallel blocks. A capture in `from` + # forces the megaregexp path; ref(N) templates must resolve against + # the matched rule's own capture list (rs semantics; the audit's + # probe maps tst-capture-parallel / tst-capture-par2 pin these). + + def _probe_parallel_map(self, rules): + return make_map([stage("main", [ParallelRule(rules=tuple(rules))])]) + + def test_capture_ref_in_parallel_resolves_rule_locally(self): + # tst-capture-parallel: capture(a-z)+"x" -> ref(1)+"X", then "b" -> "B" + rules = [ + SubRule( + from_=GroupItem( + items=( + CaptureGroupItem(data=AnyCharClassItem(range=("a", "z"))), + StringItem(value="x"), + ) + ), + to=GroupItem(items=(CaptureRefItem(id=1), StringItem(value="X"))), + ), + sub("b", "B"), + ] + m = self._probe_parallel_map(rules) + assert execute_stage(m, "main", "ax") == "aX" + assert execute_stage(m, "main", "axb") == "aXB" + + def test_capture_numbering_not_shifted_by_earlier_parallel_rule(self): + # tst-capture-par2: "k" -> "K", capture(a-z)+capture(0-9) -> ref(2)+ref(1) + rules = [ + sub("k", "K"), + SubRule( + from_=GroupItem( + items=( + CaptureGroupItem(data=AnyCharClassItem(range=("a", "z"))), + CaptureGroupItem(data=AnyCharClassItem(range=("0", "9"))), + ) + ), + to=GroupItem(items=(CaptureRefItem(id=2), CaptureRefItem(id=1))), + ), + ] + m = self._probe_parallel_map(rules) + assert execute_stage(m, "main", "k3a") == "3ka" + + def test_trailing_rules_run_after_parallel_pass(self): rules = [sub("a", "b")] m = make_map( diff --git a/interscript-py/tests/test_stdlib.py b/interscript-py/tests/test_stdlib.py index fb4a0530..b4e6a1dc 100644 --- a/interscript-py/tests/test_stdlib.py +++ b/interscript-py/tests/test_stdlib.py @@ -87,6 +87,48 @@ def repl(m, groups): rules = [MegaregexpRule(pattern="([0-9]+)", replace=repl)] assert parallel_megaregexp("a12b3", rules) == "a<12>b<3>" + # 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 list that includes the wrappers. + + def test_rule_gets_its_own_captures_not_the_wrapper_match(self): + seen = [] + + def repl(m, groups): + seen.append(list(groups)) + return f"{groups[0]}X" + + rules = [ + MegaregexpRule(pattern="([a-z])x", replace=repl), + MegaregexpRule(pattern="b", replace=lambda m, g: m.upper()), + ] + assert parallel_megaregexp("axb", rules) == "aXB" + assert seen[0] == ["a"] + + def test_later_rule_captures_offset_past_earlier_inner_groups(self): + rules = [ + MegaregexpRule( + pattern="([a-z])x", + replace=lambda m, g: f"{g[0]}X", + ), + MegaregexpRule( + pattern="b([0-9])", + replace=lambda m, g: g[0] if g[0] is not None else "?", + ), + ] + # "ax" hits rule 0; "b5" hits rule 1, whose only capture is "5" — + # the wrappers of rule 0 and rule 1 must not shift it. + assert parallel_megaregexp("axb5", rules) == "aX5" + + def test_rule_local_numbering_with_two_captures(self): + rules = [ + MegaregexpRule( + pattern="([a-z])([0-9])", + replace=lambda m, g: f"{g[1]}{g[0]}", + ) + ] + assert parallel_megaregexp("k3", rules) == "3k" + class TestFunctions: def test_case_functions(self): diff --git a/lib/interscript/node/item/capture.rb b/lib/interscript/node/item/capture.rb index 0bf2634d..be66c9de 100644 --- a/lib/interscript/node/item/capture.rb +++ b/lib/interscript/node/item/capture.rb @@ -7,6 +7,13 @@ def initialize(data) @data = data end + # rs parity (WO18 G1): the deterministic parallel sort needs a + # max_length on every Item. A capture's length is its innards' + # length (ts maxLengthOfItem: capture_group -> inner). + def max_length + data.max_length + end + def first_string data.first_string end @@ -45,6 +52,11 @@ def initialize(id) @id = id end + # rs parity (WO18 G1): a backreference matches one captured run. + def max_length + 1 + end + def first_string self end diff --git a/spec/isc/semantics_spec.rb b/spec/isc/semantics_spec.rb index 6bb8a1e7..25d7db48 100644 --- a/spec/isc/semantics_spec.rb +++ b/spec/isc/semantics_spec.rb @@ -132,13 +132,32 @@ def context_map(id, rule) end end - it "rules: capture inside parallel is pending capture support (WO80)" do + # WO18 G1 — capture inside parallel. The tree path rejects captures + # by design; with Item::CaptureGroup/CaptureRef#max_length defined + # (rs parity), the deterministic parallel sort no longer crashes with + # NoMethodError and the megaregexp fallback surfaces the named + # SystemConversionError for ref templates. + it "rules: capture with a ref template in parallel fails loud and named (WO18 G1)" do with_map("tst-cappar-1.isc", context_map(15, 'from capture(any("0".."9")) to ref(1)')) do expect { Interscript.transliterate("tst-cappar-1", "5") } - .to raise_error(NoMethodError, /max_length/) + .to raise_error(Interscript::SystemConversionError, /Can't use CaptureRef in parallel mode/) end end + it "rules: capture without a ref template runs the megaregexp fallback (WO18 G1)" do + with_map("tst-cappar-2.isc", context_map(16, 'from capture(any("0".."9")) to "N"')) do + expect(Interscript.transliterate("tst-cappar-2", "a5b")).to eq("aNb") + end + end + + it "rules: Item::CaptureGroup/#CaptureRef max_length mirrors the rs runtime (WO18 G1)" do + expect(Interscript::Node::Item::CaptureGroup.new("abc").max_length).to eq(3) + expect(Interscript::Node::Item::CaptureGroup.new( + Interscript::Node::Item::Group.new("ab", "cde") + ).max_length).to eq(5) + expect(Interscript::Node::Item::CaptureRef.new(1).max_length).to eq(1) + end + # WO17 — rs parity: the deterministic parallel sort counts every # alias as max_length 1 (rs Item::max_length: Prim/Alias => 1). The # reference runtime raised NotImplementedError for map-local aliases,