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
66 changes: 65 additions & 1 deletion lib/interscript/interpreter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -173,7 +202,42 @@ 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).
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`
# 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)
Expand Down
11 changes: 8 additions & 3 deletions lib/interscript/stdlib.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
Loading