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
43 changes: 20 additions & 23 deletions interscript-py/src/interscript/isc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1050,39 +1050,36 @@ 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):
if self._from_key(it) in okeys:
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) ----------------------
Expand Down
44 changes: 44 additions & 0 deletions interscript-py/tests/test_isc_wf1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Loading