From 5781a996fbb20450661ef44d5fbdf251da0bc0dc Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Wed, 11 Nov 2026 23:51:21 +0800 Subject: [PATCH] fix(WO17): override rows join the expansion's trailing parallel pass - cross-grammar parity MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The first run of the cross-grammar parity harness (maps scripts/parse-parity.mjs) caught interscript-py appending unmatched override additions as bare stage rules while ts/ruby join ALL override rows into the trailing parallel block (§4.2.6) - bgnpcgn-tha compiled 344 vs 327+17, alalc-tha 1523 vs 327+1196: the additions ran as separate sequential gsubs instead of participating in the single longest-match pass with the rows they extend. _apply_override now mirrors the canonical merge: partition R by byte-exact from-equality, then when the expansion ends with a parallel block ALL of O joins it; otherwise O appends as stage-level rows. Two placement specs watched failing first (one item, kept-then- overrides order, one-pass execution). Suites: wf1/parser/constraints green; the golden (12,337 rows, in-process) green in-suite. --- interscript-py/src/interscript/isc.py | 43 ++++++++++++-------------- interscript-py/tests/test_isc_wf1.py | 44 +++++++++++++++++++++++++++ 2 files changed, 64 insertions(+), 23 deletions(-) diff --git a/interscript-py/src/interscript/isc.py b/interscript-py/src/interscript/isc.py index 1a093713..8bb91150 100644 --- a/interscript-py/src/interscript/isc.py +++ b/interscript-py/src/interscript/isc.py @@ -1050,14 +1050,16 @@ def _from_key(rule: ParsedRule) -> Any: def _apply_override( self, items: list[Any], overrides: list[ParsedRule] ) -> list[Any]: - """ISC-WF-1 §4.2 merge: R_kept (in R's order) followed by O (in - O's order); ALL rules sharing an override's from-string are - replaced. For rows inside a parallel/sequence block the matched - overrides are appended at that block's end so they stay in the - pass where the replaced row lived (§4.3); overrides matching - nothing are simply added at the end (§4.2 rule 4).""" + """ISC-WF-1 §4.2 + §4.2.6 merge: partition R by byte-exact + from-equality (ALL same-from rules replaced), then place O — + when the expansion ends with a parallel block, ALL of O joins + that block (replacements and additions share one longest-match + pass, §4.2.5/§4.2.6); otherwise O appends after R_kept as + stage-level rows. Cross-grammar canonical: the ruby reference + and the ts runtime both join the trailing parallel (found by + maps scripts/parse-parity.mjs — py appended unmatched additions + as bare stage rules, splitting the pass).""" okeys = [self._from_key(o) for o in overrides] - placed: set[int] = set() out: list[Any] = [] for it in items: if isinstance(it, ParsedRule): @@ -1065,24 +1067,19 @@ def _apply_override( continue out.append(it) elif isinstance(it, (ParallelBlock, SequenceBlock)): - kept: list[Any] = [] - dropped_keys: set[Any] = set() - for r in it.rules: - if isinstance(r, ParsedRule) and self._from_key(r) in okeys: - dropped_keys.add(self._from_key(r)) - else: - kept.append(r) - block_overrides: list[ParsedRule] = [] - for i, o in enumerate(overrides): - if i not in placed and self._from_key(o) in dropped_keys: - block_overrides.append(o) - placed.add(i) - out.append(type(it)(rules=tuple(kept + block_overrides))) + kept = tuple( + r + for r in it.rules + if not (isinstance(r, ParsedRule) and self._from_key(r) in okeys) + ) + out.append(type(it)(rules=kept)) else: out.append(it) - for i, o in enumerate(overrides): - if i not in placed: - out.append(o) + tail = out[-1] if out else None + if isinstance(tail, ParallelBlock): + out[-1] = ParallelBlock(rules=tuple(tail.rules) + tuple(overrides)) + return out + out.extend(overrides) return out # -- Scanner helpers (mirror the TypeScript parser) ---------------------- diff --git a/interscript-py/tests/test_isc_wf1.py b/interscript-py/tests/test_isc_wf1.py index a41344a1..5b922671 100644 --- a/interscript-py/tests/test_isc_wf1.py +++ b/interscript-py/tests/test_isc_wf1.py @@ -470,3 +470,47 @@ def test_public_api_language_parameter(self, tmp_path, compiled): rt = interscript.Runtime([tmp_path]) assert rt.transliterate(cm.system_code, "қ") == "ķ" assert rt.transliterate(cm.system_code, "қ", language="kur") == "k" + + +class TestOverridePlacement: + """§4.2.6 (WO17, cross-grammar parity finding): ALL override rows — + replacements AND additions — join the expansion's trailing parallel + block, so the whole expansion shares one longest-match pass. The + pre-parity behavior appended unmatched additions as bare stage + rules, splitting the pass (found by maps scripts/parse-parity.mjs + on bgnpcgn-tha / alalc-tha: 344 vs 327+17, 1523 vs 327+1196).""" + + SRC = """ +system "test-ovr-place" { + ruleset rs { + parallel { + sub "a" "x" + sub "b" "y" + } + } + stage main { + use rs override { + sub "a" "A" + sub "c" "Z" + } + } +} +""" + + def test_additions_join_the_trailing_parallel(self): + doc = parse_isc(self.SRC) + body = doc.stages[0].items + assert len(body) == 1, f"expected one pass, got {len(body)} items" + blk = body[0] + froms = [r.from_source for r in blk.rules if getattr(r, "from_source", None)] + # kept row keeps its order; overrides join after it + assert froms == ['"b"', '"a"', '"c"'] + + def test_expansion_executes_as_one_pass(self): + doc = parse_isc(self.SRC) + cm = isc_to_compiled_map(doc) + from interscript.executor import execute_stage + + # one longest-match pass over the joined block: "ac" -> "AZ" + assert execute_stage(cm, "main", "ac") == "AZ" + assert execute_stage(cm, "main", "ab") == "Ay"