From db05b450fa25a4571ae015292ad4f4ba2124d33e Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Wed, 18 Nov 2026 22:15:42 +0800 Subject: [PATCH 1/2] fix(isc): megaregexp replacement build, deferred re-only tos, undefined-ref drops - WO20 reverse shrink-to-zero MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Three engine fixes closing the ruby side of the WO20 reverse-golden divergence families (rs PR wo20-reverse-shrink-to-zero is the reference side; with these the full 15,321-row reverse replay is 0-delta): - Megaregexp replacements now carry the STRING build (`build_item(:str)` - groups join, captures parenthesize, refs become backrefs), which is what rs `build_replacement` renders over the nth_string collapse. `build_item(:parstr)` demotes to the :par product build and returns ARRAYS for group `to`s, which gsub stringified as Ruby inspect into the output (odni-pus `Aبد[" اللَّه"]`). The WO18 G1 canon - a ref template in a parallel pass fails loud and named - is pinned by an explicit guard (spec/isc/semantics_spec.rb stays green). - Re-only aliases in a reversed `to` (the boundary a word-boundary rule's reversal hoists onto the to side) defer the parstr build to match time: rs renders replacements lazily, so a rule that never fires must not poison the whole pass (bgnpcgn-per / un-prs "Can't use boundary in a par context" nulls). - A from-pattern backreference to a group the pattern does not itself define drops the rule from the alternation: rs match_item fails a ref on an unparticipated group (never matches), while the combined named-group compile refused numbered refs outright (bgnpcgn-zho RegexpError nulls). ruby gsub semantics for unmatched/out-of-range refs (empty insertion) were already correct here. --- lib/interscript/interpreter.rb | 64 +++++++++++++++++++++++++++++++++- lib/interscript/stdlib.rb | 11 ++++-- 2 files changed, 71 insertions(+), 4 deletions(-) diff --git a/lib/interscript/interpreter.rb b/lib/interscript/interpreter.rb index 487719d1..ec32cca1 100644 --- a/lib/interscript/interpreter.rb +++ b/lib/interscript/interpreter.rb @@ -90,6 +90,35 @@ def self.select_language(map, language) language.to_sym end + # Does this replacement item carry a capture-ref template anywhere? + # (WO18 G1: ref templates fail loud and named in a parallel pass.) + def self.ref_template?(item) + case item + when Interscript::Node::Item::CaptureRef + true + when Interscript::Node::Item::Group + item.children.any? { |c| ref_template?(c) } + when Interscript::Node::Item::Repeat, Interscript::Node::Item::Maybe, + Interscript::Node::Item::Some, Interscript::Node::Item::MaybeSome, + Interscript::Node::Item::CaptureGroup + ref_template?(item.data) + else + false + end + end + + # Does this rule pattern carry a `\N` backreference to a capture + # group the pattern itself does not define? Machine-generated rule + # patterns only: capture groups open with `(` not followed by `?` + # (lookarounds and `(?:` are not capture groups); backrefs are `\N`. + # Such a rule can never match (rs match_item fails a ref on an + # unparticipated group), so a megaregexp pass drops it instead of + # failing the combined compile (WO20 reverse-golden bgnpcgn-zho). + def self.backref_without_group?(pattern) + group_count = pattern.scan(/\((?!\?)/).size + pattern.scan(/\\(\d+)/).any? { |(n)| n.to_i > group_count } + end + class Stage attr_reader :language @@ -173,7 +202,40 @@ def execute_rule r Interscript::Stdlib.deterministic_sort_by_max_length(r.children).each do |i| # rule.from.max_length gives somewhat better test results, why is that raise Interscript::SystemConversionError, "Can't parallelize #{i.class}" unless Interscript::Node::Rule::Sub === i next if i.reverse_run == true - subs_array << [build_regexp(i), build_item(i.to, :parstr)] + pattern = build_regexp(i) + # A from-pattern backreference to a group the pattern does + # not itself define (reversed rules re-reference capture + # groups their reversal hoisted away — the WO20 + # reverse-golden bgnpcgn-zho family) can never match: rs + # `match_item` fails the ref on an unparticipated group, + # and ruby's combined alternation refuses to compile the + # numbered ref next to the named-group wrappers at all. + # Drop the rule, mirroring the rs never-matches semantics. + next if Interscript::Interpreter.backref_without_group?(pattern) + # WO18 G1 canon: a ref TEMPLATE in a parallel pass fails + # loud and named (spec/isc/semantics_spec.rb pins it). + raise Interscript::SystemConversionError, + "Can't use CaptureRef in parallel mode" if Interscript::Interpreter.ref_template?(i.to) + # The megaregexp replacement carries the STRING build + # (:str semantics — groups join, captures parenthesize, + # refs become \N), which is what rs `build_replacement` + # renders over the nth_string collapse. `build_item(:parstr)` + # demotes to the :par product build and returns ARRAYS for + # group `to`s — stringified as Ruby inspect into the + # output (the WO20 reverse-golden odni-pus family). + # rs also renders a sub's replacement LAZILY (only when + # the rule fires): a re-only alias in a reversed `to` — + # the boundary a word-boundary rule's reversal hoists onto + # the to side — must not poison the whole pass when the + # rule never matches (WO20 reverse-golden bgnpcgn-per / + # un-prs family). Defer the build to match time in that + # case. + begin + replacement = build_item(i.to, :str) + rescue Interscript::SystemConversionError + replacement = -> { build_item(i.to, :str) } + end + subs_array << [pattern, replacement] end r.subs_regexp = Interscript::Stdlib.parallel_regexp_compile(subs_array) r.subs_replacements = subs_array.map(&:last) diff --git a/lib/interscript/stdlib.rb b/lib/interscript/stdlib.rb index 92d89f58..6011287f 100644 --- a/lib/interscript/stdlib.rb +++ b/lib/interscript/stdlib.rb @@ -42,7 +42,11 @@ def self.parallel_regexp_gsub(string, subs_regexp, subs_hash) lm = Regexp.last_match # Extract the match name idx = lm.named_captures.compact.keys.first[1..].to_i - subs_hash[idx] + # A deferred replacement (a `to` whose parstr build raises + # eagerly — rs renders replacements only when a rule fires) + # builds now. + replacement = subs_hash[idx] + replacement.is_a?(Proc) ? replacement.call : replacement end end @@ -63,11 +67,12 @@ def self.parallel_regexp_gsub_debug(string, subs_regexp, subs_array) # puts matched.inspect # puts [lm.begin(matched), lm.end(matched)].inspect idx = matched[1..].to_i - debug_info = {begin: lm.begin(matched), end: lm.end(matched), idx: idx, result: subs_array[idx]} + replacement = subs_array[idx] + debug_info = {begin: lm.begin(matched), end: lm.end(matched), idx: idx, result: replacement} # standard:disable Style/GlobalVars (deliberate $DEBUG / -d-flag debug idiom) $subs_matches << debug_info # standard:enable Style/GlobalVars - subs_array[idx] + replacement.is_a?(Proc) ? replacement.call : replacement end end From fd7e47447ecf40edff7546c8d1014a76b68c509b Mon Sep 17 00:00:00 2001 From: Ronald Tse Date: Wed, 18 Nov 2026 22:23:58 +0800 Subject: [PATCH 2/2] style: standardrb - un-nest the G1 guard multiline if-modifier --- lib/interscript/interpreter.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/interscript/interpreter.rb b/lib/interscript/interpreter.rb index ec32cca1..3faa9027 100644 --- a/lib/interscript/interpreter.rb +++ b/lib/interscript/interpreter.rb @@ -214,8 +214,10 @@ def execute_rule r next if Interscript::Interpreter.backref_without_group?(pattern) # WO18 G1 canon: a ref TEMPLATE in a parallel pass fails # loud and named (spec/isc/semantics_spec.rb pins it). - raise Interscript::SystemConversionError, - "Can't use CaptureRef in parallel mode" if Interscript::Interpreter.ref_template?(i.to) + if Interscript::Interpreter.ref_template?(i.to) + raise Interscript::SystemConversionError, + "Can't use CaptureRef in parallel mode" + end # The megaregexp replacement carries the STRING build # (:str semantics — groups join, captures parenthesize, # refs become \N), which is what rs `build_replacement`