From 93403ddd8634aab1ff795ca63489c8af18beafd9 Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Sat, 14 Nov 2026 17:49:05 +0800 Subject: [PATCH] =?UTF-8?q?feat(py):=20reverse=20mode=20=E2=80=94=20port?= =?UTF-8?q?=20of=20interscript-rs=20src/reverse.rs=20(WO19=20G10)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Mirror the rs reverse engine choice-for-choice at the compiled-IR level: stages reverse (children in reverse order), sub rules swap from/to with boundary-like alias hoisting across the transfer and the capture/ref exchange, to upcase/downcase swap via item case walks, funcalls invert through the Ruby Stdlib.reverse_function table, the document name goes through Document.reverse_name, metadata swaps source/destination scripts, aliases/languages/dependencies pass through. Runtime surface: Runtime.reverse reverses the dependency closure and serves the reversed docs under their forward names (rs reverse_document recursion); Runtime.load_map gains the ruby DSL.parse reverse-name load fallback; MapLoader.register mirrors interscript-ts. Stdlib: unseparate added (reverse_function counterpart of separate); title_case corrected to ruby parity (upcase first char and the char after each separator — no lowering). Documented ruby-crash deviations surface as named MapLogicErrors at reverse time (rs Error::Unsupported): repeat under a to-upcase/downcase sub, a boundary hoist consuming the whole from group, a funcall outside the reverse_function map, an unhandled item kind in the transfer walk. Gates: pytest 450 collected, 0 failed (GOLDEN_ROOT set); 18 new reverse tests pass; py-vs-py parse parity driver canonical 435/435, raw 435/435 (hand rev 7a84260) — reverse adds no parse behavior. --- interscript-py/src/interscript/__init__.py | 99 +++- interscript-py/src/interscript/loader.py | 9 +- interscript-py/src/interscript/reverse.py | 474 ++++++++++++++++++ interscript-py/src/interscript/stdlib.py | 37 +- .../reverse_maps/alalc-bul-Cyrl-Latn-1997.isc | 121 +++++ .../tests/data/reverse_maps/rev-boundary.isc | 6 + .../tests/data/reverse_maps/rev-capture.isc | 8 + .../tests/data/reverse_maps/rev-case.isc | 9 + .../tests/data/reverse_maps/rev-delete.isc | 5 + .../tests/data/reverse_maps/rev-dep-lib.isc | 6 + .../tests/data/reverse_maps/rev-dep.isc | 7 + .../tests/data/reverse_maps/rev-funcall.isc | 6 + .../tests/data/reverse_maps/rev-parallel.isc | 9 + .../tests/data/reverse_maps/rev-sequence.isc | 8 + .../data/reverse_maps/rev-upcase-funcall.isc | 5 + .../data/reverse_maps/rev-upcase-repeat.isc | 8 + interscript-py/tests/test_reverse.py | 284 +++++++++++ interscript-py/tests/test_stdlib.py | 7 + 18 files changed, 1099 insertions(+), 9 deletions(-) create mode 100644 interscript-py/src/interscript/reverse.py create mode 100644 interscript-py/tests/data/reverse_maps/alalc-bul-Cyrl-Latn-1997.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-boundary.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-capture.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-case.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-delete.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-dep-lib.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-dep.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-funcall.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-parallel.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-sequence.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-upcase-funcall.isc create mode 100644 interscript-py/tests/data/reverse_maps/rev-upcase-repeat.isc create mode 100644 interscript-py/tests/test_reverse.py diff --git a/interscript-py/src/interscript/__init__.py b/interscript-py/src/interscript/__init__.py index 49218f03..edc7f570 100644 --- a/interscript-py/src/interscript/__init__.py +++ b/interscript-py/src/interscript/__init__.py @@ -39,16 +39,32 @@ load_manifest, verify_artifact, ) +from .reverse import reverse_map, reverse_name T = TypeVar("T") DEFAULT_STAGE = "main" +class _ReversedDependencyResolver: + """Serves reversed dependency documents under their FORWARD names + (run rules reference the forward names) — the runtime-side half of + rs's `reverse_document` dependency recursion.""" + + def __init__(self, maps: dict[str, object]) -> None: + self._maps = maps + + def load(self, system_code: str): + try: + return self._maps[system_code] + except KeyError: + raise MapNotFoundError(system_code) from None + + class Runtime: """A map runtime bound to a set of artifact load paths. - Mirrors the TS `Intercript` class: default stage "main", dependency + Mirrors the TS `Interscript` class: default stage "main", dependency maps resolved through the same loader. """ @@ -58,8 +74,13 @@ def __init__( *, verify: bool = False, stage: str = DEFAULT_STAGE, + loader: MapLoader | None = None, ) -> None: - self.loader = MapLoader(load_paths, verify=verify) + # `loader` injects a custom resolver (tests, in-memory corpora); + # default: the artifact loader over `load_paths`. + self.loader: MapLoader = loader if loader is not None else MapLoader( + load_paths, verify=verify + ) self.default_stage = stage def add_load_path(self, path: Path | str) -> None: @@ -72,7 +93,25 @@ def map_exist(self, system_code: str) -> bool: return self.loader.map_exist(system_code) def load_map(self, system_code: str): - return self.loader.load(system_code) + """Parse and cache a compiled map (returns a CompiledMap). + + Carries the ruby `Interscript::DSL.parse` reverse-name fallback: + a missing map whose reverse-named sibling exists loads that + sibling and returns its reverse (cached under the requested + name).""" + try: + return self.loader.load(system_code) + except MapNotFoundError as original: + fallback = reverse_name(system_code) + if fallback == system_code: + raise + try: + forward = self.loader.load(fallback) + except MapNotFoundError: + raise original from None + reversed_doc = reverse_map(forward) + self.loader.register(system_code, reversed_doc) + return reversed_doc def transliterate( self, @@ -90,11 +129,48 @@ def transliterate( """ from .executor import execute_stage - map_ = self.loader.load(system_code) + map_ = self.load_map(system_code) return execute_stage( map_, stage or self.default_stage, text, self.loader, language ) + def reverse( + self, + system_code: str, + text: str, + stage: str | None = None, + language: str | None = None, + ) -> str: + """Reverse-mode transliteration — the `iscrs reverse` analog: + load the map, reverse it (mirroring rs `reverse_document`, which + recursively reverses embedded dependency documents — here the + whole dependency closure is reversed and served to execution), + then run one stage of the reversed document.""" + from .executor import execute_stage + + map_ = self.load_map(system_code) + + # rs: dependency documents reverse with the parent. Memoized + # closure walk — cycles recurse in rs/ruby too, only repeat + # work is skipped here. + reversed_deps: dict[str, object] = {} + + def collect(m) -> None: + for dep in m.dependencies: + if dep in reversed_deps: + continue + dep_map = self.loader.load(dep) + reversed_deps[dep] = reverse_map(dep_map) + collect(dep_map) + + collect(map_) + + reversed_doc = reverse_map(map_) + loader = _ReversedDependencyResolver(reversed_deps) if reversed_deps else None + return execute_stage( + reversed_doc, stage or self.default_stage, text, loader, language + ) + def _default_runtime() -> Runtime: """Module-level runtime; resolves paths from `$INTERSCRIPT_MAPS_IR` @@ -140,6 +216,18 @@ def transliterate( return _default_runtime().transliterate(system_code, text, stage, language) +def reverse( + system_code: str, + text: str, + stage: str | None = None, + language: str | None = None, +) -> str: + """Reverse-mode transliteration (`iscrs reverse` analog): load + `system_code`, reverse the document (dependencies reverse with the + parent), and transliterate `text` through it.""" + return _default_runtime().reverse(system_code, text, stage, language) + + __all__ = [ "DEFAULT_STAGE", "ArtifactIntegrityError", @@ -160,6 +248,9 @@ def transliterate( "map_exist", "map_list", "parse_isc", + "reverse", + "reverse_map", + "reverse_name", "transliterate", "verify_artifact", ] diff --git a/interscript-py/src/interscript/loader.py b/interscript-py/src/interscript/loader.py index 28a63fc0..30a80b6d 100644 --- a/interscript-py/src/interscript/loader.py +++ b/interscript-py/src/interscript/loader.py @@ -153,7 +153,14 @@ def map_list(self) -> list[str]: return sorted(self._known) def map_exist(self, system_code: str) -> bool: - return system_code in self._known + return system_code in self._known or system_code in self._cache + + def register(self, system_code: str, map_: CompiledMap) -> None: + """Register a compiled map directly (bypasses load paths) — the + mirror of interscript-ts's `MapLoader.register`. Used by the + runtime's reverse-name load fallback to cache a reversed + document under the requested name.""" + self._cache[system_code] = map_ def _default_cache_dir() -> Path: diff --git a/interscript-py/src/interscript/reverse.py b/interscript-py/src/interscript/reverse.py new file mode 100644 index 00000000..a33e00d9 --- /dev/null +++ b/interscript-py/src/interscript/reverse.py @@ -0,0 +1,474 @@ +# Copyright 2026 Interscript Project. BSD-3-Clause (see LICENSE). +"""Reverse mode — port of interscript-rs ``src/reverse.rs``, which +mirrors ``Interscript::Node::*#reverse`` (interscript-ruby origin/main, +spec'd by spec/reversibility_spec.rb). + +The reversed document runs through the NORMAL executor: the inversion +lives entirely in the produced document. What rs does at the node +level, this module does at the compiled-map level: + +- stages reverse (children in REVERSE order, each rule reversed); +- sub rules swap from/to, hoist boundary-like aliases across the + transfer, and exchange capture groups/refs; +- ``to upcase``/``to downcase`` swap via Item#downcase/#upcase parity; +- funcalls map through the Ruby ``Stdlib.reverse_function`` table; +- the document name goes through ``Document.reverse_name``; +- metadata swaps source_script/destination_script; +- aliases, languages, tests (absent from the IR) pass through. + +Documented ruby-crash deviations (rs ``Error::Unsupported``, mirrored +here as MapLogicError with the same information): + +- a repeat item under a ``to upcase``/``to downcase`` sub + (ruby: NoMethodError), +- a boundary hoist consuming the whole from group + (ruby: NoMethodError on ``children=``), +- a funcall outside the reverse_function map (ruby: nil.to_sym + TypeError when the reversed funcall executes), +- an unhandled item kind in the transfer walk (ruby: raise + ``"Type ... unhandled!"``). + +Compiled-IR deltas vs the rs node model (behavior identical): + +- the compiled IR carries no ``reverse_run``; a rule whose reversed + ``from`` is empty and has no assertions — which ruby marks + reverse_run: true so the executor skips it — is DROPPED here + (ruby's own JSON-IR compilers omit reverse_run rules too); +- dependency documents are names in the IR, so rs's recursion into + embedded dependency documents lives on the runtime entry point + (``Runtime.reverse``), not on this pure function; +- capture groups carry no ids: the regex engine numbers them in regex + order at compile time, which is exactly the numbering rs's + ``assign_group_ids`` reproduces; refs keep their numbers; +- ``priority`` (absent from the rs model) negates on reverse per ruby + ``Sub#reverse``. +""" + +from __future__ import annotations + +from dataclasses import replace + +from ._ir_types import ( + AnyCharClassItem, + AnyItem, + CaptureGroupItem, + CaptureRefItem, + CompiledMap, + FuncallInline, + FuncallRule, + GroupItem, + Item, + ParallelRule, + RepeatItem, + Rule, + SequentialRule, + Stage, + StringItem, + SubRule, +) +from .errors import MapLogicError + +# `Interscript::Stdlib.reverse_function`: the inverse funcall used by +# `Funcall#reverse`. `upcase` has no entry (the Ruby map omits it, so +# reversing an upcase funcall crashes there) — and any other funcall +# name is equally unmapped, mirroring rs's `FuncallName::reverse_name`. +REVERSE_FUNCTION: dict[str, str] = { + # Those two are best-effort, but probably wrong (per Ruby). + "title_case": "downcase", + "downcase": "title_case", + "compose": "decompose", + "decompose": "compose", + "separate": "unseparate", + "unseparate": "separate", +} + +# `Interscript::Stdlib.boundary_like_alias?` restricted to the prims rs +# treats as boundary-like (`Boundary`, `NonWordBoundary`, `LineStart`, +# `LineEnd` — the ISC grammar has no string_start/string_end +# spellings). Names as the ISC parser emits them. +BOUNDARY_LIKE_ALIASES: frozenset[str] = frozenset( + {"boundary", "non_boundary", "non_word_boundary", "line_start", "line_end"} +) + +# The compiled-IR form of ruby's `none` (converter lowering). +EMPTY_STRING = StringItem(value="") + + +def reverse_name(name: str) -> str: + """`Interscript::Node::Document.reverse_name`: split on ``-``, swap + positions 2/3 (source/target code) when there are at least 4 + segments; otherwise strip a ``-reverse`` suffix, otherwise append + one.""" + parts = name.split("-") + if len(parts) >= 4: + swapped = list(parts) + swapped[2], swapped[3] = swapped[3], swapped[2] + joined = "-".join(swapped) + if joined != name: + return joined + stripped = name.replace("-reverse", "") + return stripped if stripped != name else f"{name}-reverse" + + +def reverse_map(map_: CompiledMap) -> CompiledMap: + """`Interscript::Node::Document#reverse` over the compiled form. + Aliases are NOT reversed (``rdoc.aliases = self.aliases``); + dependency names pass through (see the module docstring for rs's + dependency recursion and where it lives instead).""" + return CompiledMap( + schema_version=map_.schema_version, + system_code=reverse_name(map_.system_code), + dependencies=map_.dependencies, + languages=map_.languages, + metadata=_reverse_metadata(map_.metadata) if map_.metadata is not None else None, + stages=tuple(_reverse_stage(s) for s in map_.stages), + aliases=map_.aliases, + functions=map_.functions, + ) + + +def _reverse_metadata(metadata: dict) -> dict: + """`MetaData#reverse` swaps source_script/destination_script (each + side only when the other is present — rs ``reverse_metadata``).""" + src = metadata.get("source_script") + dst = metadata.get("destination_script") + out = dict(metadata) + if "source_script" in out and dst is not None: + out["source_script"] = dst + if "destination_script" in out and src is not None: + out["destination_script"] = src + return out + + +def _reverse_stage(stage: Stage) -> Stage: + """`Interscript::Node::Stage#reverse` / `Group#reverse`: children + in REVERSE order, each reversed, reverse_run toggled. (The ISC + grammar cannot express `dont_reverse`, so stages always reverse.)""" + rules: list[Rule] = [] + for rule in reversed(stage.rules): + # A sub whose reversed `from` is empty is dropped, not emitted + # (see the module docstring — ruby marks it reverse_run: true). + r = _reverse_rule(rule) + if r is not None: + rules.append(r) + return Stage(name=stage.name, rules=tuple(rules)) + + +def _reverse_rule(rule: Rule) -> Rule | None: + """`None` = the reversed rule must not execute (ruby + reverse_run: true).""" + kind = rule.kind + if kind == "sub": + return _reverse_sub(rule) + + if kind == "run": + # rs toggles reverse_run; the compiled IR carries none -> identity. + return rule + + if kind == "funcall": + # `Interscript::Node::Rule::Funcall#reverse`: the name maps + # through `Stdlib.reverse_function`; kwargs carry over. + target = REVERSE_FUNCTION.get(rule.name) + if target is None: + raise MapLogicError( + f"funcall {rule.name} has no reverse (the Ruby reverse_function map omits it)" + ) + return FuncallRule(name=target, kwargs=rule.kwargs) + + if kind in ("parallel", "sequential"): + rules: list[Rule] = [] + for inner in reversed(rule.rules): # type: ignore[union-attr] + r = _reverse_rule(inner) + if r is not None: + rules.append(r) + if kind == "parallel": + return ParallelRule(rules=tuple(rules)) + return SequentialRule(rules=tuple(rules)) + + raise MapLogicError(f"unknown rule kind: {kind!r}") # pragma: no cover + + +def _reverse_sub(rule: SubRule) -> SubRule | None: + """`Interscript::Node::Rule::Sub#reverse`. Returns ``None`` when the + reversed rule must not execute: a rule whose reversed ``from`` is + empty (and carries no assertions) would match EVERYWHERE — ruby's + Sub#reverse marks those reverse_run: true so the executor skips + them, and the compiled-IR equivalent is absence.""" + if rule.from_ is None: + raise MapLogicError("sub rule missing 'from'") + + to = rule.to + if isinstance(to, FuncallInline): + # `sub from, to upcase` reverses to `sub from.downcase, + # to downcase` (Item#downcase / Item#upcase parity, and vice versa). + if to.name == "upcase": + xfrom = _item_case(rule.from_, False) + xto: Item | FuncallInline = FuncallInline(name="downcase") + elif to.name == "downcase": + xfrom = _item_case(rule.from_, True) + xto = FuncallInline(name="upcase") + else: + # rs builder: only upcase/downcase functions become SubTo + # targets; any other inline funcall walks the transfer and + # hits its unhandled arm. + xfrom, xto = _reverse_transfer(rule.from_, to) + else: + xfrom, xto = _reverse_transfer(rule.from_, to if to is not None else EMPTY_STRING) + + has_assertions = any( + v is not None for v in (rule.before, rule.not_before, rule.after, rule.not_after) + ) + if has_assertions or not _is_empty_from(xfrom): + return SubRule( + from_=xfrom, + to=xto, + # Constraints pass through UNCHANGED (ruby keeps + # before/after/not_before/not_after as-is); `priority` + # negates (ruby Sub#reverse); `when` guards carry over + # unchanged (ISC-WF-1 W3 — the language selection filters + # the same rows on either surface). + before=rule.before, + not_before=rule.not_before, + after=rule.after, + not_after=rule.not_after, + priority=-rule.priority if rule.priority is not None else None, + when=rule.when, + kind="sub", + ) + return None + + +def _is_empty_from(item: Item) -> bool: + """The ruby empty-from test: ``xfrom == "" || String("")`` (plus + ruby's ``Alias(:none)`` arm, unreachable in the compiled IR — the + converter lowers ``none`` to an empty string item).""" + return isinstance(item, StringItem) and item.value == "" + + +# --------------------------------------------------------------------------- +# Item#downcase / Item#upcase (Sub#reverse to upcase/downcase case) +# --------------------------------------------------------------------------- + + +def item_downcase(item: Item) -> Item: + """`Item#downcase`.""" + return _item_case(item, False) + + +def item_upcase(item: Item) -> Item: + """`Item#upcase`.""" + return _item_case(item, True) + + +def _item_case(item: Item, up: bool) -> Item: + """`Item#downcase`/`Item#upcase`: string data case-maps; groups and + captures recurse; aliases (incl. boundary prims) and refs are + identity; `any` rebuilds from per-entry case-mapped children; a + char-class range rebuilds as the set of its per-char entries + (``Any.new(data.map(&:downcase))`` parity).""" + + def apply(s: str) -> str: + return s.upper() if up else s.lower() + + kind = item.kind + if kind == "string": + assert isinstance(item, StringItem) + return StringItem(value=apply(item.value)) + if kind in ("alias", "capture_ref"): + # Item::Alias#downcase/#upcase = self; refs too. + return item + if kind == "capture_group": + assert isinstance(item, CaptureGroupItem) + return CaptureGroupItem(data=_item_case(item.data, up)) + if kind == "group": + assert isinstance(item, GroupItem) + return GroupItem(items=tuple(_item_case(c, up) for c in item.items)) + if kind == "any": + assert isinstance(item, AnyItem) + return AnyItem(of=tuple(_item_case(c, up) for c in item.of)) + if kind == "any_char_class": + assert isinstance(item, AnyCharClassItem) + if item.range is not None: + lo, hi = item.range + entries: list[Item] = [] + cp = ord(lo) + while True: + entries.append(StringItem(value=apply(chr(cp)))) + if cp == ord(hi): + break + cp += 1 + if 0xD800 <= cp <= 0xDFFF: + # surrogate gap — rs char::from_u32 breaks here too + break + return AnyItem(of=tuple(entries)) + if item.chars is not None: + return AnyItem(of=tuple(StringItem(value=apply(c)) for c in item.chars)) + return item + if kind == "repeat": + # Ruby Item::Repeat has no downcase/upcase: NoMethodError. + raise MapLogicError( + "repeat item in a to-upcase/downcase sub (the Ruby reverse raises NoMethodError)" + ) + raise MapLogicError( + f"unhandled item kind {kind!r} in a to-upcase/downcase sub " + "(the Ruby reverse raises NoMethodError)" + ) + + +# --------------------------------------------------------------------------- +# Sub#reverse_transfer: boundary hoisting + capture/ref exchange +# --------------------------------------------------------------------------- + + +def _is_boundary_like(item: Item) -> bool: + return item.kind == "alias" and item.name in BOUNDARY_LIKE_ALIASES # type: ignore[union-attr] + + +def _compact_items(children: list[Item]) -> Item: + """Ruby ``Group#compact`` over an already-collected children list: + drop none/"" children; 0 -> none (the IR empty string), 1 -> the + child.""" + kept = [c for c in children if not (c.kind == "string" and c.value == "")] # type: ignore[union-attr] + if not kept: + return StringItem(value="") + if len(kept) == 1: + return kept[0] + return GroupItem(items=tuple(kept)) + + +def _group_plus_compact(prefix: list[Item], last: Item) -> Item: + """Ruby ``Group.new + a + b ...`` then ``compact``: a fresh group + whose children are the leading items plus ``last`` — flattened when + ``last`` is itself a group (``Group#+`` merges children).""" + children = list(prefix) + if last.kind == "group": + children.extend(last.items) # type: ignore[union-attr] + else: + children.append(last) + return _compact_items(children) + + +def _reverse_transfer(from_: Item, to: Item | FuncallInline) -> tuple[Item, Item]: + """`Interscript::Node::Rule::Sub#reverse_transfer`: boundary-like + aliases at the from edges move onto the to side; then the + capture/ref exchange runs. Returns (reversed_from, reversed_to) — + the ruby multi-assign ``xto, xfrom = reverse_transfer(from, to)`` + swaps the results.""" + f: Item | FuncallInline = from_ + t: Item | FuncallInline = to + + if isinstance(f, GroupItem): + # Ruby reads first/last BEFORE either mutation. + parts = f.items + first = parts[0] if parts else None + last = parts[-1] if parts else None + + # first child boundary-like -> prepended to `to` + if first is not None and _is_boundary_like(first): + t = _group_plus_compact([first], t) # type: ignore[arg-type] + f = _compact_items(list(parts[1:])) + # last child boundary-like -> appended to `to`. Ruby mutates + # `from.children` here, which requires `from` to still be a + # Group; a first-hoist that consumed the whole group crashes + # there (NoMethodError). + if last is not None and _is_boundary_like(last): + if not isinstance(f, GroupItem): + raise MapLogicError( + "boundary hoist consumed the whole from group " + "(the Ruby reverse raises NoMethodError)" + ) + shorter = list(f.items[:-1]) + t = _group_plus_compact([t], last) # type: ignore[arg-type] + f = _compact_items(shorter) + elif _is_boundary_like(f): + # Whole-from boundary alias: `_end`-suffixed names append after + # `to`, everything else prepends before it; from becomes `none`. + # Ruby tests `from.name.end_with?("_end")` — among the ISC prims + # only line_end qualifies (string_end is not in the grammar). + boundary = f + if boundary.kind == "alias" and boundary.name == "line_end": # type: ignore[union-attr] + t = _group_plus_compact([t], boundary) # type: ignore[arg-type] + else: + t = _group_plus_compact([boundary], t) # type: ignore[arg-type] + f = StringItem(value="") + + # --- capture/ref exchange --- + state = _TransferState() + visited_from = _transfer_visit(f, "from", state) + visited_to = _transfer_visit(t, "to", state) + # xto, xfrom = reverse_transfer(from, to) + return visited_to, visited_from + + +class _TransferState: + """Ruby ``state[:left]`` (every capture group seen, in visit order; + minted ref ids index into it, 1-based) and ``state[:right]`` (ref + ids already substituted on the `to` side — a second occurrence + stays a literal ref).""" + + def __init__(self) -> None: + self.left: list[Item | FuncallInline] = [] + self.right: set[int] = set() + + +def _transfer_visit(node: Item | FuncallInline, side: str, state: _TransferState) -> Item: + """``reverse_transfer_visit``. Refs stay positional numbers — the + regex engine numbers the reversed rule's groups in regex order at + compile time, the same numbers ruby's ``build_regexp`` assigns at + build time.""" + kind = node.kind + + if kind == "alias": + # Alias :kor_maybedash -> CaptureRef(left.length). (rs's + # builder expands the alias to its capture(maybe("-")) tree + # before reverse sees it, where the CaptureGroup branch handles + # it with identical numbering — this branch guards direct alias + # items and documents the Ruby special case.) + if node.name == "kor_maybedash": # type: ignore[union-attr] + state.left.append(node) + return CaptureRefItem(id=len(state.left)) + return node # type: ignore[return-value] + + if kind == "string": + return node # type: ignore[return-value] + + if kind == "capture_ref": + if side == "from": + return node # type: ignore[return-value] + # refs on the to side: the first occurrence substitutes the + # matching from-side group; further occurrences stay refs. + assert isinstance(node, CaptureRefItem) + if node.id in state.right: + return node + state.right.add(node.id) + if node.id - 1 >= len(state.left): + raise MapLogicError("Capture count doesn't match") + return state.left[node.id - 1] # type: ignore[return-value] + + if kind == "capture_group": + assert isinstance(node, CaptureGroupItem) + state.left.append(node) + out: Item = CaptureRefItem(id=len(state.left)) + _transfer_visit(node.data, side, state) # visit but don't care + return out + + if kind == "any": + assert isinstance(node, AnyItem) + return AnyItem(of=tuple(_transfer_visit(c, side, state) for c in node.of)) + + if kind == "group": + assert isinstance(node, GroupItem) + return GroupItem(items=tuple(_transfer_visit(c, side, state) for c in node.items)) + + if kind == "repeat": + # Repeat: only the data is visited; the repeat kind survives. + assert isinstance(node, RepeatItem) + return replace(node, item=_transfer_visit(node.item, side, state)) + + if kind == "any_char_class": + return node # type: ignore[return-value] + + raise MapLogicError( + f"Type {kind} unhandled in reverse_transfer_visit " + '(the Ruby raises "Type ... unhandled!")' + ) diff --git a/interscript-py/src/interscript/stdlib.py b/interscript-py/src/interscript/stdlib.py index 6b44ffbc..85174286 100644 --- a/interscript-py/src/interscript/stdlib.py +++ b/interscript-py/src/interscript/stdlib.py @@ -169,12 +169,29 @@ def upcase(text: str, opts: dict | None = None) -> str: def title_case(text: str, opts: dict | None = None) -> str: + """`Interscript::Stdlib::Functions.title_case` / rs `title_case` + parity: upcase the FIRST character of the string, then the + character following each word separator. Other characters are + untouched (ruby `gsub(/^(.)/)` + `gsub(/#{sep}(.)/)` — notably NOT + full per-word title-casing).""" sep = (opts or {}).get("wordSeparator", " ") + chars = list(text) + if chars: + chars[0] = chars[0].upper() if sep == "": - return text[:1].upper() + text[1:].lower() - return sep.join( - w[:1].upper() + w[1:].lower() if w else w for w in text.split(sep) - ) + return "".join(chars) + sep_chars = list(sep) + out: list[str] = [] + i = 0 + while i < len(chars): + if i + len(sep_chars) < len(chars) and chars[i : i + len(sep_chars)] == sep_chars: + out.append(sep) + out.append(chars[i + len(sep_chars)].upper()) + i += len(sep_chars) + 1 + else: + out.append(chars[i]) + i += 1 + return "".join(out) def separate(text: str, opts: dict | None = None) -> str: @@ -182,6 +199,16 @@ def separate(text: str, opts: dict | None = None) -> str: return sep.join(text) +def unseparate(text: str, opts: dict | None = None) -> str: + """Remove every separator (the inverse of `separate`). + `Interscript.functions.unseparate` — used by reverse mode + (`Stdlib.reverse_function` maps separate <-> unseparate). Split + semantics mirror rs/ruby: trailing empty segments survive the split + and rejoin (no trimming).""" + sep = (opts or {}).get("separator", " ") + return "".join(text.split(sep)) + + def compose(text: str, opts: dict | None = None) -> str: import unicodedata @@ -203,6 +230,8 @@ def __call__(self, text: str, opts: dict | None = None) -> str: ... "upcase": upcase, "title_case": title_case, "separate": separate, + # Reverse-mode counterpart of `separate` (Stdlib.reverse_function). + "unseparate": unseparate, "compose": compose, "decompose": decompose, } diff --git a/interscript-py/tests/data/reverse_maps/alalc-bul-Cyrl-Latn-1997.isc b/interscript-py/tests/data/reverse_maps/alalc-bul-Cyrl-Latn-1997.isc new file mode 100644 index 00000000..dce3169b --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/alalc-bul-Cyrl-Latn-1997.isc @@ -0,0 +1,121 @@ +system "ALA-LC:bul-Cyrl:Latn:1997" { + +metadata { + authority_id alalc + id 1997 + language iso-639-2:bul + source_script Cyrl + destination_script Latn + name Romanization Table -- Bulgarian (1997) + url http://www.rechtertie.nl/databases/judd/downloads/Bulgarian.pdf + creation_date 1997 + + notes { + note " The letter Ъ in the initial and medial position is a vowel in Bulgarian and is romanized as ŭ.\n It appears in the final position chiefly in texts published before 1945, in which it serves as\n a hard sign and is romanized as ʺ." + note "The letters Ѣ and Ѫ are found chiefly in texts published before the orthographic reform of 1945." + } +} + +tests { + # missing +} + +# This map has been partially converted by the bin/maps_v1_to_v2 script +# The section below requires human attention. Remember to remove this +# comment and move the converted map to 'maps/' directory. Please also +# take note that the maps-staging directory will be cleaned up whenever +# you run the bin/maps_v1_to_v2 script. You should particularly be +# concerned about any regular expressions found in this file and about +# advanced expressions in parallel {} parts, and also about the order +# of particular parts of the stage. + +stage main { + + # RULES + # note[1] + sub { + from "\u042a" + to "ʺ" + before "" + after boundary + } + sub { + from "u044a" + to "ʺ" + before "" + after boundary + } + + + # CHARACTERS + parallel { + sub "\u0410" "A" + sub "\u0411" "B" + sub "\u0412" "V" + sub "\u0413" "G" + sub "\u0414" "D" + sub "\u0415" "E" + sub "\u0416" "Zh" + sub "\u0417" "Z" + sub "\u0418" "I" + sub "\u0419" "Ĭ" + sub "\u041a" "K" + sub "\u041b" "L" + sub "\u041c" "M" + sub "\u041d" "N" + sub "\u041e" "O" + sub "\u041f" "P" + sub "\u0420" "R" + sub "\u0421" "S" + sub "\u0422" "T" + sub "\u0423" "U" + sub "\u0424" "F" + sub "\u0425" "Kh" + sub "\u0426" "T͡S" + sub "\u0427" "Ch" + sub "\u0428" "Sh" + sub "\u0429" "Sht" + sub "\u042a" "Ŭ" + sub "\u042c" "\u02B9" + sub "\u0462" "I͡E" + sub "\u042e" "I͡U" + sub "\u042f" "I͡A" + sub "\u046A" "U̐" + + sub "\u0430" "a" + sub "\u0431" "b" + sub "\u0432" "v" + sub "\u0433" "g" + sub "\u0434" "d" + sub "\u0435" "e" + sub "\u0436" "zh" + sub "\u0437" "z" + sub "\u0438" "i" + sub "\u0439" "ĭ" + sub "\u043a" "k" + sub "\u043b" "l" + sub "\u043c" "m" + sub "\u043d" "n" + sub "\u043e" "o" + sub "\u043f" "p" + sub "\u0440" "r" + sub "\u0441" "s" + sub "\u0442" "t" + sub "\u0443" "u" + sub "\u0444" "f" + sub "\u0445" "kh" + sub "\u0446" "t͡s" + sub "\u0447" "ch" + sub "\u0448" "sh" + sub "\u0449" "sht" + sub "\u044a" "ŭ" + sub "\u044c" "\u02B9" + sub "\u0463" "i͡e" + sub "\u044e" "i͡u" + sub "\u044f" "i͡a" + sub "\u046B" "u̐" + } + +} + +} diff --git a/interscript-py/tests/data/reverse_maps/rev-boundary.isc b/interscript-py/tests/data/reverse_maps/rev-boundary.isc new file mode 100644 index 00000000..09e8b1c4 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-boundary.isc @@ -0,0 +1,6 @@ +system "rev:boundary" { +stage main { + sub "x" "y" after boundary + sub "q" "r" before line_end +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-capture.isc b/interscript-py/tests/data/reverse_maps/rev-capture.isc new file mode 100644 index 00000000..81d84ad6 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-capture.isc @@ -0,0 +1,8 @@ +system "rev:capture" { +stage main { + sub { + from "a" + capture("b" + maybe("c")) + "d" + to "X" + ref(1) + } +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-case.isc b/interscript-py/tests/data/reverse_maps/rev-case.isc new file mode 100644 index 00000000..da8fa913 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-case.isc @@ -0,0 +1,9 @@ +system "rev:case" { +stage main { + sub { + from "abc" + to upcase + } + downcase +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-delete.isc b/interscript-py/tests/data/reverse_maps/rev-delete.isc new file mode 100644 index 00000000..47ec578a --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-delete.isc @@ -0,0 +1,5 @@ +system "rev:delete" { +stage main { + sub "h" none +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-dep-lib.isc b/interscript-py/tests/data/reverse_maps/rev-dep-lib.isc new file mode 100644 index 00000000..37454635 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-dep-lib.isc @@ -0,0 +1,6 @@ +system "rev:dep-lib" { +stage main { + sub "n" "p" + sub "e" "f" +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-dep.isc b/interscript-py/tests/data/reverse_maps/rev-dep.isc new file mode 100644 index 00000000..9c425801 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-dep.isc @@ -0,0 +1,7 @@ +system "rev:dep-main" { +dependency "rev-dep-lib" as base +stage main { + sub "m" "n" + run map.base.stage.main +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-funcall.isc b/interscript-py/tests/data/reverse_maps/rev-funcall.isc new file mode 100644 index 00000000..38e024bd --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-funcall.isc @@ -0,0 +1,6 @@ +system "rev:funcall" { +stage main { + separate separator "-" + compose +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-parallel.isc b/interscript-py/tests/data/reverse_maps/rev-parallel.isc new file mode 100644 index 00000000..2e050fc4 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-parallel.isc @@ -0,0 +1,9 @@ +system "rev:parallel" { +stage main { + parallel { + sub "ph" "f" + sub "th" "t" + sub "x" "ks" + } +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-sequence.isc b/interscript-py/tests/data/reverse_maps/rev-sequence.isc new file mode 100644 index 00000000..68f3f3ea --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-sequence.isc @@ -0,0 +1,8 @@ +system "rev:sequence" { +stage main { + sequence { + sub "ab" "X" + sub "X" "Q" + } +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-upcase-funcall.isc b/interscript-py/tests/data/reverse_maps/rev-upcase-funcall.isc new file mode 100644 index 00000000..272f7920 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-upcase-funcall.isc @@ -0,0 +1,5 @@ +system "rev:upcase" { +stage main { + upcase +} +} diff --git a/interscript-py/tests/data/reverse_maps/rev-upcase-repeat.isc b/interscript-py/tests/data/reverse_maps/rev-upcase-repeat.isc new file mode 100644 index 00000000..7f90cbe6 --- /dev/null +++ b/interscript-py/tests/data/reverse_maps/rev-upcase-repeat.isc @@ -0,0 +1,8 @@ +system "rev:upcase-repeat" { +stage main { + sub { + from maybe("a") + to upcase + } +} +} diff --git a/interscript-py/tests/test_reverse.py b/interscript-py/tests/test_reverse.py new file mode 100644 index 00000000..b39e7f60 --- /dev/null +++ b/interscript-py/tests/test_reverse.py @@ -0,0 +1,284 @@ +# Copyright 2026 Interscript Project. BSD-3-Clause (see LICENSE). +"""Reverse-mode parity — ported from interscript-rs ``tests/reverse.rs``, +whose expectations are ruby ground truth (``Document#reverse`` over +interscript-ruby origin/main, spec'd by spec/reversibility_spec.rb). + +Fixtures: ``tests/data/reverse_maps/*.isc`` (vendored from +interscript-rs tests/reverse_maps/, plus rev-upcase-repeat for the +repeat-item deviation). + +Documented ruby-crash deviations surface as named MapLogicErrors at +reverse time (mirroring rs ``Error::Unsupported`` messages): + +- a repeat (maybe/some) item under a ``to upcase``/``to downcase`` sub +- a boundary hoist that consumes the whole from group +- a funcall outside the reverse_function map (upcase has no entry) +- an unhandled item kind in the transfer walk +""" + +from __future__ import annotations + +from pathlib import Path + +import pytest + +from interscript import Runtime +from interscript._ir_types import ( + AliasItem, + CompiledMap, + GroupItem, + Stage, + StringItem, + SubRule, +) +from interscript.errors import MapLogicError, MapNotFoundError +from interscript.isc import isc_to_compiled_map, parse_isc +from interscript.reverse import reverse_map, reverse_name + +FIXTURES_DIR = Path(__file__).parent / "data" / "reverse_maps" + + +def _load_sources() -> dict[str, str]: + """Load every fixture under both its file stem and its system code + (dependency targets are file stems; top-level loads use the code).""" + sources: dict[str, str] = {} + for path in sorted(FIXTURES_DIR.glob("*.isc")): + src = path.read_text(encoding="utf-8") + doc = parse_isc(src, filename=str(path)) + sources[path.stem] = src + sources[doc.system_code] = src + return sources + + +SOURCES = _load_sources() + + +class _DictLoader: + """MapResolver over the in-memory fixture corpus (plus try_load / + register for the runtime fallback contract).""" + + def __init__(self, compiled: dict[str, CompiledMap]) -> None: + self._compiled = compiled + + def load(self, system_code: str) -> CompiledMap: + try: + return self._compiled[system_code] + except KeyError: + raise MapNotFoundError(system_code) from None + + def try_load(self, system_code: str) -> CompiledMap | None: + return self._compiled.get(system_code) + + def register(self, system_code: str, map_: CompiledMap) -> None: + self._compiled[system_code] = map_ + + +@pytest.fixture(scope="module") +def runtime() -> Runtime: + compiled = { + code: isc_to_compiled_map(parse_isc(src, filename=code)) + for code, src in SOURCES.items() + } + return Runtime(loader=_DictLoader(compiled)) # type: ignore[arg-type] + + +def compile_map(code: str) -> CompiledMap: + return isc_to_compiled_map(parse_isc(SOURCES[code], filename=code)) + + +def reverse_run(runtime: Runtime, map_id: str, text: str) -> str: + """The mirror of the rs test's ``reverse_run`` helper (rt.load + + reverse_document + execute): the runtime path, so dependency + documents reverse with the parent exactly like rs.""" + return runtime.reverse(map_id, text) + + +# --------------------------------------------------------------------------- +# reverse_name (Document.reverse_name) +# --------------------------------------------------------------------------- + + +def test_reverse_name_swaps_source_and_target() -> None: + # Document.reverse_name: >= 4 dash-parts swaps [2]/[3]. + assert reverse_name("alalc-bul-Cyrl-Latn-1997") == "alalc-bul-Latn-Cyrl-1997" + # the swap is an involution: swapping back reproduces the input. + assert reverse_name("alalc-bul-Latn-Cyrl-1997") == "alalc-bul-Cyrl-Latn-1997" + + +def test_reverse_name_strips_or_appends_the_suffix() -> None: + # shorter names strip -reverse, else append it. + assert reverse_name("some-map-reverse") == "some-map" + assert reverse_name("short-x") == "short-x-reverse" + assert reverse_name("a-b-c") == "a-b-c-reverse" + + +# --------------------------------------------------------------------------- +# reverse_map — rs reverse.rs parity slice +# --------------------------------------------------------------------------- + + +def test_parallel_rules_swap_and_reverse_order(runtime: Runtime) -> None: + # Ruby: "phth" -> "phthh", "x" -> "x", "fats" -> "phaths". + # (reversed rules ks->x, t->th, f->ph; "fats" hits t then f) + assert reverse_run(runtime, "rev:parallel", "phth") == "phthh" + assert reverse_run(runtime, "rev:parallel", "x") == "x" + assert reverse_run(runtime, "rev:parallel", "fats") == "phaths" + + +def test_sequential_children_reverse_order(runtime: Runtime) -> None: + # forward: ab->X then X->Q; reversed: Q->X then X->ab. + assert reverse_run(runtime, "rev:sequence", "Q") == "ab" + assert reverse_run(runtime, "rev:sequence", "X") == "ab" + assert reverse_run(runtime, "rev:sequence", "ab") == "ab" + assert reverse_run(runtime, "rev:sequence", "abq") == "abq" + + +def test_case_swap_downcases_the_from_tree(runtime: Runtime) -> None: + # forward: sub "abc" to upcase; downcase. + # reversed: title_case, then sub "abc" to downcase. + # Ruby: "abc" -> "Abc", "abc ABC" -> "Abc ABC". + assert reverse_run(runtime, "rev:case", "abc") == "Abc" + assert reverse_run(runtime, "rev:case", "abc ABC") == "Abc ABC" + + +def test_boundary_constraints_pass_through_unchanged(runtime: Runtime) -> None: + # forward: sub "x" "y" after boundary; sub "q" "r" before line_end. + # Ruby reverse keeps the constraints on the same side: "r y" -> "r x" + # (y->x fires after the boundary before end-of-string). + assert reverse_run(runtime, "rev:boundary", "r y") == "r x" + assert reverse_run(runtime, "rev:boundary", "yq") == "yq" + assert reverse_run(runtime, "rev:boundary", "qr") == "qr" + + +def test_capture_and_ref_exchange_sides(runtime: Runtime) -> None: + # forward: from "a" + capture("b" + maybe("c")) + "d" to "X" + ref(1) + # reversed: from "X" + capture("b" + maybe("c")) to "a" + ref(1). + assert reverse_run(runtime, "rev:capture", "Xbc") == "abcd" + assert reverse_run(runtime, "rev:capture", "Xd") == "Xd" + assert reverse_run(runtime, "rev:capture", "abcd") == "abcd" + + +def test_funcalls_invert_through_the_reverse_function_map(runtime: Runtime) -> None: + # forward: separate separator "-"; compose. + # reversed: decompose, then unseparate separator "-". + assert reverse_run(runtime, "rev:funcall", "a-b-c") == "abc" + assert reverse_run(runtime, "rev:funcall", "abc") == "abc" + + +def test_dependency_documents_reverse_with_the_parent(runtime: Runtime) -> None: + # dep main: n->p, e->f. forward parent: m->n then run dep. + # reversed parent: run reversed-dep (p->n, f->e) then n->m. + # Ruby: "men" -> "mem", "pem" -> "mem". + assert reverse_run(runtime, "rev:dep-main", "men") == "mem" + assert reverse_run(runtime, "rev:dep-main", "pem") == "mem" + + +def test_real_corpus_map_reverses_like_ruby(runtime: Runtime) -> None: + # Vendored alalc-bul-Cyrl-Latn-1997.isc; Ruby ground truth: + assert reverse_run(runtime, "ALA-LC:bul-Cyrl:Latn:1997", "Kirillicata") == "Кириллиcата" + assert reverse_run(runtime, "ALA-LC:bul-Cyrl:Latn:1997", "zapad") == "запад" + assert reverse_run(runtime, "ALA-LC:bul-Cyrl:Latn:1997", "Sofiya") == "Софиyа" + + +def test_empty_reversed_from_disables_the_rule(runtime: Runtime) -> None: + # sub "h" none (deletion): the reversed from is empty -> Ruby + # marks it reverse_run: true so the executor skips it; the + # compiled-IR form drops the rule. A forward-deletion map must not + # infinite-expand on reverse. + # Ruby ground truth: "hh" -> "hh", "hi" -> "hi". + assert reverse_run(runtime, "rev:delete", "hh") == "hh" + assert reverse_run(runtime, "rev:delete", "hi") == "hi" + + +def test_document_name_and_metadata_reverse(runtime: Runtime) -> None: + doc = reverse_map(compile_map("ALA-LC:bul-Cyrl:Latn:1997")) + # The system code has 3 dash-parts (colons separate the fields), + # so Document.reverse_name appends the -reverse suffix. + assert doc.system_code == "ALA-LC:bul-Cyrl:Latn:1997-reverse" + assert doc.metadata is not None + assert doc.metadata["source_script"] == "Latn" + assert doc.metadata["destination_script"] == "Cyrl" + # aliases and languages pass through untouched (rs: rdoc.aliases = self.aliases) + forward = compile_map("ALA-LC:bul-Cyrl:Latn:1997") + assert doc.aliases == forward.aliases + assert doc.languages == forward.languages + assert doc.dependencies == forward.dependencies + + +# --------------------------------------------------------------------------- +# Runtime reverse-name load fallback (ruby DSL.parse behavior) +# --------------------------------------------------------------------------- + + +def test_transliterate_resolves_a_missing_map_through_its_reverse_name( + runtime: Runtime, +) -> None: + # no "rev:parallel-reverse" map exists — the fallback loads + # rev:parallel and returns its reverse (ruby DSL.parse behavior). + assert runtime.transliterate("rev:parallel-reverse", "fats") == "phaths" + # the fallback result is cached under the requested name. + assert runtime.loader.try_load("rev:parallel-reverse") is not None + + +def test_missing_map_without_a_reverse_sibling_raises(runtime: Runtime) -> None: + with pytest.raises(MapNotFoundError): + runtime.transliterate("no-such-map-xyz", "x") + + +# --------------------------------------------------------------------------- +# Documented ruby-crash deviations surface as named errors +# --------------------------------------------------------------------------- + + +def test_upcase_funcall_reverse_is_unsupported_like_ruby() -> None: + # The Ruby reverse_function map has no :upcase entry — reversing a + # funcall upcase crashes there (nil.to_sym TypeError when the + # reversed funcall executes). We raise cleanly at reverse time. + with pytest.raises(MapLogicError, match=r"upcase has no reverse"): + reverse_map(compile_map("rev:upcase")) + + +def test_repeat_item_under_to_upcase_raises_like_ruby() -> None: + # Ruby Item::Repeat has no downcase/upcase: NoMethodError. + with pytest.raises(MapLogicError, match=r"repeat item in a to-upcase/downcase sub"): + reverse_map(compile_map("rev:upcase-repeat")) + + +def test_boundary_hoist_consuming_the_whole_from_group_raises_like_ruby() -> None: + # Hand-built: a one-child group whose only child is boundary-like — + # unreachable through the ISC converter (single-part concats + # unwrap) but the same path rs guards with Error::Unsupported. + boundary = AliasItem(name="boundary") + rule = SubRule( + from_=GroupItem(items=(boundary,)), + to=StringItem(value="x"), + ) + doc = CompiledMap( + schema_version=1, + system_code="unit:hoist", + dependencies=(), + stages=(Stage(name="main", rules=(rule,)),), + aliases={}, + functions={}, + ) + with pytest.raises( + MapLogicError, match=r"boundary hoist consumed the whole from group" + ): + reverse_map(doc) + + +def test_unhandled_item_kind_raises_like_ruby() -> None: + # Ruby reverse_transfer_visit: raise "Type ... unhandled!" + from interscript._ir_types import StageItem + + rule = SubRule(from_=StageItem(name="other"), to=StringItem(value="x")) + doc = CompiledMap( + schema_version=1, + system_code="unit:unhandled", + dependencies=(), + stages=(Stage(name="main", rules=(rule,)),), + aliases={}, + functions={}, + ) + with pytest.raises(MapLogicError, match=r"unhandled in reverse_transfer_visit"): + reverse_map(doc) diff --git a/interscript-py/tests/test_stdlib.py b/interscript-py/tests/test_stdlib.py index b4e6a1dc..734b1e9b 100644 --- a/interscript-py/tests/test_stdlib.py +++ b/interscript-py/tests/test_stdlib.py @@ -18,6 +18,7 @@ regexp_escape, separate, title_case, + unseparate, upcase, ) @@ -143,6 +144,11 @@ def test_separate(self): assert separate("abc") == "a b c" assert separate("abc", {"separator": "-"}) == "a-b-c" + def test_unseparate(self): + assert unseparate("a b c") == "abc" + assert unseparate("a-b-c", {"separator": "-"}) == "abc" + assert unseparate("a b") == "ab" + def test_compose_decompose(self): composed = "ñ" decomposed = decompose(composed) @@ -155,6 +161,7 @@ def test_registry_has_all_builtins(self): "upcase", "title_case", "separate", + "unseparate", "compose", "decompose", }