js_regex 3.13.0 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/js_regex/conversion.rb +2 -1
  3. data/lib/js_regex/converter/anchor_converter.rb +4 -2
  4. data/lib/js_regex/converter/assertion_converter.rb +2 -0
  5. data/lib/js_regex/converter/backreference_converter.rb +33 -12
  6. data/lib/js_regex/converter/base.rb +38 -27
  7. data/lib/js_regex/converter/conditional_converter.rb +12 -4
  8. data/lib/js_regex/converter/context.rb +54 -8
  9. data/lib/js_regex/converter/escape_converter.rb +6 -30
  10. data/lib/js_regex/converter/freespace_converter.rb +2 -0
  11. data/lib/js_regex/converter/group_converter.rb +18 -16
  12. data/lib/js_regex/converter/keep_converter.rb +2 -0
  13. data/lib/js_regex/converter/literal_converter.rb +16 -46
  14. data/lib/js_regex/converter/meta_converter.rb +15 -13
  15. data/lib/js_regex/converter/property_converter.rb +4 -20
  16. data/lib/js_regex/converter/set_converter.rb +10 -37
  17. data/lib/js_regex/converter/subexpression_converter.rb +2 -0
  18. data/lib/js_regex/converter/type_converter.rb +45 -39
  19. data/lib/js_regex/converter/unsupported_token_converter.rb +2 -0
  20. data/lib/js_regex/converter.rb +2 -0
  21. data/lib/js_regex/error.rb +2 -0
  22. data/lib/js_regex/node/checks.rb +70 -0
  23. data/lib/js_regex/node/conditional_parts.rb +17 -0
  24. data/lib/js_regex/node/quantifier.rb +134 -0
  25. data/lib/js_regex/node/stringification.rb +14 -0
  26. data/lib/js_regex/node/transformations.rb +56 -0
  27. data/lib/js_regex/node/traversal.rb +24 -0
  28. data/lib/js_regex/node.rb +49 -40
  29. data/lib/js_regex/second_pass/alternate_conditional_permutations.rb +89 -0
  30. data/lib/js_regex/second_pass/delimit_backrefs.rb +31 -0
  31. data/lib/js_regex/second_pass/expand_bounded_capture_loops.rb +79 -0
  32. data/lib/js_regex/second_pass/expand_constant_capture_loops.rb +287 -0
  33. data/lib/js_regex/second_pass/expand_nullable_captures.rb +327 -0
  34. data/lib/js_regex/second_pass/handle_non_participating_backrefs.rb +69 -0
  35. data/lib/js_regex/second_pass/preserve_repeated_captures.rb +370 -0
  36. data/lib/js_regex/second_pass/resolve_capture_references.rb +116 -0
  37. data/lib/js_regex/second_pass/substitute_root_level_keep_mark.rb +26 -0
  38. data/lib/js_regex/second_pass.rb +364 -107
  39. data/lib/js_regex/target.rb +2 -0
  40. data/lib/js_regex/utils/captures.rb +46 -0
  41. data/lib/js_regex/utils/char_sets.rb +34 -0
  42. data/lib/js_regex/utils/char_types.rb +18 -0
  43. data/lib/js_regex/utils/escapes.rb +32 -0
  44. data/lib/js_regex/utils/literals.rb +53 -0
  45. data/lib/js_regex/utils/properties.rb +19 -0
  46. data/lib/js_regex/{converter → utils}/property_map.csv +31 -5
  47. data/lib/js_regex/utils.rb +8 -0
  48. data/lib/js_regex/version.rb +3 -1
  49. data/lib/js_regex.rb +5 -3
  50. metadata +29 -10
data/lib/js_regex/node.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class JsRegex
2
4
  #
3
5
  # Converter#convert result. Represents a branch or leaf node with an optional
@@ -5,31 +7,47 @@ class JsRegex
5
7
  #
6
8
  class Node
7
9
  require_relative 'error'
10
+ Dir[File.join(__dir__, 'node', '*.rb')].sort.each { |f| require f }
11
+
12
+ ATTRIBUTES = %i[
13
+ children
14
+ marked_nonempty
15
+ nullable
16
+ quantifier
17
+ reference
18
+ type
19
+ ].freeze
8
20
 
9
- attr_reader :children, :quantifier, :reference, :type
21
+ attr_reader(*ATTRIBUTES)
10
22
 
11
- TYPES = %i[
23
+ REFERENCE_TYPES = %i[
12
24
  backref
13
- captured_group
25
+ capture_backref
26
+ capture_check
27
+ capture_conditional
14
28
  conditional
29
+ multiplex_backref
30
+ ].freeze
31
+
32
+ REFERENCE_TYPES_MAP = REFERENCE_TYPES.to_h { |k| [k, true] }.freeze
33
+
34
+ TYPES = (REFERENCE_TYPES + %i[
35
+ branch
36
+ captured_group
15
37
  dropped
16
38
  keep_mark
17
39
  plain
18
- ].freeze
40
+ ]).freeze
41
+
42
+ TYPES_MAP = TYPES.to_h { |k| [k, true] }.freeze
19
43
 
20
- def initialize(*children, reference: nil, type: :plain)
44
+ def initialize(*children, reference: nil, type: :plain, marked_nonempty: false, nullable: false, quantifier: nil)
21
45
  self.children = children
22
46
  self.reference = reference
23
47
  self.type = type
24
- end
25
-
26
- def initialize_copy(*)
27
- self.children = children.map(&:clone)
28
- end
29
-
30
- def transform(&block)
31
- children.map!(&block)
32
- self
48
+ self.marked_nonempty = marked_nonempty
49
+ self.nullable = nullable
50
+ self.quantifier = quantifier
33
51
  end
34
52
 
35
53
  def <<(node)
@@ -37,30 +55,9 @@ class JsRegex
37
55
  self
38
56
  end
39
57
 
40
- def dropped?
41
- # keep everything else, including empty or depleted capturing groups
42
- # so as not to not mess with reference numbers (e.g. backrefs)
43
- type.equal?(:dropped)
44
- end
45
-
46
- def to_s
47
- case type
48
- when :dropped
49
- ''
50
- when :backref, :captured_group, :plain
51
- children.join << quantifier.to_s
52
- else
53
- raise TypeError.new(
54
- "#{type} must be substituted before stringification"
55
- ).extend(JsRegex::Error)
56
- end
57
- end
58
-
59
- def update(attrs)
60
- self.children = attrs.fetch(:children) if attrs.key?(:children)
61
- self.quantifier = attrs.fetch(:quantifier) if attrs.key?(:quantifier)
62
- self.type = attrs.fetch(:type) if attrs.key?(:type)
63
- self
58
+ def initialize_copy(*)
59
+ super
60
+ self.children = children.map(&:clone)
64
61
  end
65
62
 
66
63
  private
@@ -68,7 +65,7 @@ class JsRegex
68
65
  TypeError = Class.new(::TypeError).extend(JsRegex::Error)
69
66
 
70
67
  def type=(arg)
71
- arg.nil? || TYPES.include?(arg) ||
68
+ arg.nil? || TYPES_MAP[arg] ||
72
69
  raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
73
70
  @type = arg
74
71
  end
@@ -80,7 +77,7 @@ class JsRegex
80
77
  end
81
78
 
82
79
  def quantifier=(arg)
83
- arg.nil? || arg.class == Regexp::Expression::Quantifier ||
80
+ arg.nil? || arg.class == Quantifier ||
84
81
  raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
85
82
  @quantifier = arg
86
83
  end
@@ -90,5 +87,17 @@ class JsRegex
90
87
  raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
91
88
  @reference = arg
92
89
  end
90
+
91
+ def marked_nonempty=(arg)
92
+ arg == true || arg == false ||
93
+ raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
94
+ @marked_nonempty = arg
95
+ end
96
+
97
+ def nullable=(arg)
98
+ arg == true || arg == false ||
99
+ raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
100
+ @nullable = arg
101
+ end
93
102
  end
94
103
  end
@@ -0,0 +1,89 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Replace capture conditionals with alternatives that JavaScript can match.
4
+ # Each alternative copies the pattern for one combination of participating
5
+ # groups, requires or excludes those groups, and selects the matching branches.
6
+ # Example: /(a)?(?(1)b|c)/ => /(){0}c|(a)b/
7
+ # with redundant noncapturing groups omitted.
8
+ class JsRegex::SecondPass::AlternateConditionalPermutations < JsRegex::SecondPass
9
+ def call
10
+ permutations = conditional_tree_permutations
11
+ return if permutations.empty?
12
+
13
+ alternatives = permutations.map.with_index do |variant, i|
14
+ Node.new((i.zero? ? '(?:' : '|(?:'), variant, ')', type: :branch)
15
+ end
16
+ tree.update(children: alternatives)
17
+ end
18
+
19
+ private
20
+
21
+ # For /(a)?(?(1)b|c)/, produce an absent variant and a present variant:
22
+ # /(){0}c/ and /(a)b/ (redundant noncapturing groups omitted).
23
+ def conditional_tree_permutations
24
+ conds = conditions
25
+ return [] if conds.empty?
26
+
27
+ caps_per_branch = captured_group_count
28
+
29
+ condition_permutations(conds).map.with_index do |truthy_conds, i|
30
+ tree_permutation = tree.clone
31
+ # find referenced groups and conditionals and make one-sided
32
+ tree_permutation.walk do |node|
33
+ build_permutation(node, conds, truthy_conds, caps_per_branch, i)
34
+ end
35
+ truthy_conds.each do |reference|
36
+ require_group_participation(tree_permutation, reference + caps_per_branch * i)
37
+ end
38
+ tree_permutation
39
+ end
40
+ end
41
+
42
+ # /(a)?(?(1)b|c)(?(1)d|e)/ => [1], counting the repeated condition once.
43
+ def conditions
44
+ tree.walk.select { |node| node.type == :conditional }.map(&:reference).uniq
45
+ end
46
+
47
+ # /(a)(?:b(c))/ => 2.
48
+ def captured_group_count
49
+ tree.walk.count { |node| node.type == :captured_group }
50
+ end
51
+
52
+ # [1, 2] => [[], [1], [2], [1, 2]], the captures present in each variant.
53
+ def condition_permutations(conditions)
54
+ (0..(conditions.length)).inject([]) do |arr, n|
55
+ arr + conditions.combination(n).to_a
56
+ end
57
+ end
58
+
59
+ # With one capture per variant, \1 in the second variant => \2.
60
+ def build_permutation(node, conds, truthy_conds, caps_per_branch, i)
61
+ truthy = truthy_conds.include?(node.reference)
62
+
63
+ case node.type
64
+ when :backref, :capture_check
65
+ node.renumber(node.reference + caps_per_branch * i)
66
+ when :captured_group
67
+ # if the group is referenced by any condition, modulate its quantity
68
+ if conds.include?(node.reference)
69
+ adapt_referenced_group_to_permutation(node, truthy)
70
+ end
71
+ node.renumber(node.reference + caps_per_branch * i)
72
+ when :conditional
73
+ select_conditional_branch(node, truthy)
74
+ end
75
+ end
76
+
77
+ # /(a)?/ => /(a)/ when present, or /(){0}/ when absent.
78
+ def adapt_referenced_group_to_permutation(group_node, truthy)
79
+ if truthy
80
+ min_quantify(group_node)
81
+ elsif group_node.optional?
82
+ discard_contents(group_node)
83
+ else
84
+ # Keep mandatory captures mandatory. An enclosing optional group or
85
+ # another alternative may still allow this capture not to participate.
86
+ group_node.children.insert(1, Node.new('(?!)'))
87
+ end
88
+ end
89
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Separate numeric backreferences from following literal digits with (?:).
4
+ # Otherwise JavaScript could read a reference followed by a digit, such as
5
+ # \1 followed by 0, as a different reference or an octal escape.
6
+ # Example: /(a)\1 0/x => /(a)\1(?:)0/.
7
+ class JsRegex::SecondPass::DelimitBackrefs < JsRegex::SecondPass
8
+ def call
9
+ delimit_backrefs(tree, '')
10
+ end
11
+
12
+ # A /\1/ node with following = '0' becomes /\1(?:)/ and returns '\'
13
+ # as its first character; following = 'a' leaves it unchanged.
14
+ def delimit_backrefs(node, following)
15
+ quantifier_start = node.quantifier&.to_s&.[](0)
16
+ following = quantifier_start || following
17
+ if node.type == :backref && /\A[0-9]/.match(following) &&
18
+ /\\[0-9]+\z/.match(node.children.last.to_s)
19
+ node << '(?:)'
20
+ end
21
+
22
+ first = quantifier_start
23
+ node.children.reverse_each do |child|
24
+ child_start = child.is_a?(Node) ? delimit_backrefs(child, following) : child[0]
25
+ first = following = child_start if child_start
26
+ end
27
+ # Return the first character while visiting the children, rather than
28
+ # rendering each subtree again just to inspect its first character.
29
+ first unless node.dropped?
30
+ end
31
+ end
@@ -0,0 +1,79 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Expand finite repetitions so each iteration has its own capture groups.
4
+ # This preserves earlier captures when later iterations skip those groups,
5
+ # and preserves Ruby's stopping rule for repeated empty iterations.
6
+ # Example: /(?:(a)|b){2}\1/ => /(?:(a)|b)(?:(a)|b)history[2,1]/.
7
+ # The history is resolved later to the latest participating copy of (a).
8
+ class JsRegex::SecondPass::ExpandBoundedCaptureLoops < JsRegex::SecondPass
9
+ def call
10
+ @references = reference_counts(tree).keys
11
+ return if @references.empty?
12
+
13
+ expand(tree)
14
+ renumber_captures if @expanded
15
+ end
16
+
17
+ private
18
+
19
+ # /(?:(a)|b){2}\1/ unrolls its loop; /(a){2}\1/ stays as-is because
20
+ # every iteration necessarily updates the referenced capture.
21
+ def expand(node)
22
+ return unless node.is_a?(Node)
23
+
24
+ node.children.each { |child| expand(child) }
25
+ quantity = node.quantifier
26
+ return unless quantity&.bounded_repetition?
27
+
28
+ return unless node.walk.any? { |part| part.type == :captured_group && @references.include?(part.reference) }
29
+
30
+ iteration = node.with(quantifier: nil)
31
+ nullable = may_match_empty?(iteration)
32
+ references = potentially_absent_groups(iteration)
33
+ if nullable
34
+ iteration.walk { |part| references << part.reference if part.type == :captured_group }
35
+ end
36
+ return if (references & @references).empty?
37
+
38
+ tail = unroll(iteration, quantity, nullable)
39
+ node.update(type: :plain, reference: nil, quantifier: nil, children: [tail])
40
+ @expanded = true
41
+ end
42
+
43
+ # Body /(?:(a)|b)/ with {2} => /(?:(a)|b)(?:(a)|b)/, before renumbering.
44
+ def unroll(iteration, quantity, nullable)
45
+ positions = {}
46
+ quantity.iteration_indices.each do |index|
47
+ positions[index - 1] = temporary_capture('[\\s\\S]*') if nullable && quantity.before_last_iteration?(index + 1)
48
+ end
49
+ tail = Node.new
50
+ quantity.iteration_indices.reverse_each do |index|
51
+ current = Node.new(iteration.clone, tail)
52
+ probe = positions[index - 1]
53
+ current.children.unshift(Node.new('(?=', probe, ')')) if probe
54
+ previous_position = index > 2 ? positions[index - 3] : nil
55
+ tail = iteration_choice(current, index, quantity, previous_position)
56
+ end
57
+ tail
58
+ end
59
+
60
+ # For the optional second iteration of {1,2}, /a/ => /(?:a|)/;
61
+ # for {1,2}?, /a/ => /(?:|a)/.
62
+ def iteration_choice(current, index, quantity, previous_position)
63
+ # Ruby permits one empty iteration before its loop check sees the same
64
+ # position again. Keep that last empty capture when the loop stops.
65
+ if previous_position
66
+ current = Node.new(require_progress(previous_position.reference), current)
67
+ end
68
+ if quantity.optional_iteration?(index)
69
+ branches = [current, Node.new]
70
+ branches.reverse! if quantity.reluctant?
71
+ alternate(branches)
72
+ elsif previous_position
73
+ stopped = Node.new('(?=', capture_value(previous_position.reference), ')')
74
+ alternate([current, stopped])
75
+ else
76
+ current
77
+ end
78
+ end
79
+ end
@@ -0,0 +1,287 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Rewrite unbounded loops whose backreferences can be replaced by fixed strings.
4
+ # Track which captures have participated, then substitute their known values
5
+ # in backreferences and select conditional branches in the original choice order.
6
+ # Captures used only by conditions may vary: only their participation matters.
7
+ # Example: /(?:(a)|\1)+/ => /(a)(?:(a)|a)*/
8
+ # with redundant noncapturing groups omitted: once set, \1 is always a.
9
+ class JsRegex::SecondPass::ExpandConstantCaptureLoops < JsRegex::SecondPass
10
+ def call
11
+ loop do
12
+ @counts = reference_counts(tree)
13
+ break if @counts.empty? || !expand(tree)
14
+
15
+ renumber_captures
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ # In /x(?:(a)|\1)+y/, compile the loop together with its tail y, then
22
+ # replace that suffix; return true after one such replacement.
23
+ def expand(node)
24
+ rewrite_loop_and_tail(node) do |child, following|
25
+ catch(:unsupported_capture_loop) { compile(child, following) }
26
+ end
27
+ end
28
+
29
+ # For /(?:(a)|\1)+/, state 0 has no capture and must choose (a) first;
30
+ # state 1 can then repeat either (a) or the known value a.
31
+ def compile(node, following)
32
+ groups = node.walk.select { |part| part.type == :captured_group && @counts[part.reference] > 0 }
33
+ return if groups.empty?
34
+
35
+ references = groups.map(&:reference)
36
+ internal = reference_counts(node)
37
+ return unless groups.length > 1 || references.any? { |reference| internal[reference] > 0 } ||
38
+ (node.quantifier.reluctant? && conditional_reference?(following, references))
39
+
40
+ following_counts = reference_counts(following)
41
+ return unless references.all? do |reference|
42
+ internal[reference] + following_counts[reference] == @counts[reference]
43
+ end
44
+
45
+ @values = constant_capture_values(groups)
46
+ # One bit per capture records whether it has participated in the loop.
47
+ @capture_bits = references.each.with_index.to_h { |ref, idx| [ref, 1 << idx] }
48
+ @all_captures = (1 << references.length) - 1
49
+ @iteration = node.with(quantifier: nil)
50
+ @following = following
51
+ @reluctant = node.quantifier.reluctant?
52
+ @expanded_states = {}
53
+ if node.optional?
54
+ remaining_iterations(0)
55
+ else
56
+ alternate(iteration_paths(0).map { |part, state| Node.new(part, remaining_iterations(state)) })
57
+ end
58
+ end
59
+
60
+ # In /(?:(a)|ab)+?(?(1)c|a)/, the trailing condition needs capture 1's state
61
+ # even after a later ab iteration skips it.
62
+ def conditional_reference?(node, references)
63
+ node.walk.any? do |part|
64
+ part.conditional? &&
65
+ part.condition_references.any? { |ref| references.include?(ref) }
66
+ end
67
+ end
68
+
69
+ # Capture 1 containing /(ab)/ => {1 => /(?:ab)/}; /(a+)/ is allowed only
70
+ # when conditions need its participation but no reference reads its value.
71
+ def constant_capture_values(groups)
72
+ groups.each_with_object({}) do |group, values|
73
+ expression = Regexp::Parser.parse(group.children.join)
74
+ unless constant_word(expression)
75
+ throw :unsupported_capture_loop if capture_value_referenced?(group.reference)
76
+ next
77
+ end
78
+
79
+ values[group.reference] = without_captures(group).update(quantifier: nil)
80
+ end
81
+ end
82
+
83
+ # /(a+)(?(1)b|c)/ => false for capture 1; /(a+)\1/ => true.
84
+ def capture_value_referenced?(reference)
85
+ tree.walk.any? do |node|
86
+ [:backref, :capture_check].include?(node.type) && node.reference == reference
87
+ end
88
+ end
89
+
90
+ # Once capture 1 is set to a, the remaining /(?:(a)|\1)*/ iterations can
91
+ # use /(?:(a)|a)*/ (redundant noncapturing groups omitted).
92
+ def remaining_iterations(state)
93
+ return @expanded_states[state].clone if @expanded_states.key?(state)
94
+
95
+ if state == @all_captures
96
+ body = substitute_known_captures(@iteration, state)
97
+ tail = substitute_known_captures(@following, state)
98
+ result = Node.new(repeat(body, @reluctant), tail)
99
+ else
100
+ result = repeat_without_new_captures(continuation_options(state))
101
+ end
102
+ @expanded_states[state] = result
103
+ end
104
+
105
+ # Each option either repeats the current state or continues to a new one.
106
+ # Their order determines which choices backtracking tries first.
107
+ # For /(?:(a)|b)*/ in state 0, (a) advances to state 1, b repeats state 0,
108
+ # and stopping comes last; for *?, stopping comes first.
109
+ def continuation_options(state)
110
+ options = iteration_paths(state).map do |part, next_state|
111
+ next_state == state ? [part, nil] : [nil, Node.new(part, remaining_iterations(next_state))]
112
+ end
113
+ ending = [nil, substitute_known_captures(@following, state)]
114
+ @reluctant ? options.unshift(ending) : options.push(ending)
115
+ options.reject! { |_, continuation| continuation && unmatchable?(continuation) }
116
+ options
117
+ end
118
+
119
+ # Options [[/b/, nil], [nil, /c/]] => /(?:b)*c/: repeat b, then continue at c.
120
+ def repeat_without_new_captures(options)
121
+ loops = options.map(&:first).compact
122
+ return alternate(options.map(&:last)) if loops.empty?
123
+
124
+ first_loop = options.index { |part, _| part }
125
+ last_loop = options.rindex { |part, _| part }
126
+ before = options[0...first_loop].map(&:last)
127
+ after = options[(last_loop + 1)..-1].map(&:last)
128
+ throw :unsupported_capture_loop if options[first_loop..last_loop].any? { |_, continuation| continuation }
129
+
130
+ # Regrouping these iterations must leave only one way to split the text.
131
+ # Otherwise the regrouping could change backtracking order.
132
+ unless before.empty? || after.empty? || unique_iteration_boundaries?(loops)
133
+ throw :unsupported_capture_loop
134
+ end
135
+ loop_body = alternate(loops)
136
+ branches = []
137
+ branches << Node.new(repeat(loop_body.clone, true), alternate(before)) unless before.empty?
138
+ branches << Node.new(repeat(loop_body, false), alternate(after)) unless after.empty?
139
+ alternate(branches)
140
+ end
141
+
142
+ # [/a/, /b/] => true; [/a/, /ab/] => false because one word prefixes another.
143
+ def unique_iteration_boundaries?(loops)
144
+ words = loops.map { |part| constant_word(Regexp::Parser.parse(part.to_s)) }
145
+ return false if words.any? { |word| !word || word.empty? }
146
+
147
+ words.map!(&:downcase)
148
+ words.combination(2).none? do |left, right|
149
+ left.start_with?(right) || right.start_with?(left)
150
+ end
151
+ end
152
+
153
+ # For /(?:(a)|\1)/ in state 0, keep only [(a), state 1]; \1 cannot yet match.
154
+ def iteration_paths(state)
155
+ expand_iteration(@iteration, state).reject { |part, _| unmatchable?(part) }.each do |part, _|
156
+ throw :unsupported_capture_loop if may_match_empty?(part)
157
+ end
158
+ end
159
+
160
+ # With bit 1 assigned to capture 1, /(?:(a)|b)/ in state 0 yields
161
+ # [(a), state 1] and [b, state 0].
162
+ def expand_iteration(node, state)
163
+ return [[node.clone, state]] if node.is_a?(String)
164
+
165
+ updates = node.walk.any? do |part|
166
+ part.type == :captured_group && @capture_bits.key?(part.reference)
167
+ end
168
+ return [[substitute_known_captures(node, state), state]] unless updates
169
+
170
+ return optional_iteration_paths(node, state) if node.quantifier
171
+ if node.conditional?
172
+ selected = conditional_branch(node, state)
173
+ throw :unsupported_capture_loop unless selected
174
+
175
+ return expand_iteration(selected, state)
176
+ end
177
+ head = node.children.first
178
+ throw :unsupported_capture_loop if head.is_a?(String) && head.match?(/\A\((?!\?:\z|\z)/)
179
+
180
+ if node.alternation?
181
+ return node.children.flat_map do |branch|
182
+ child = branch.with(type: :plain)
183
+ child.children.shift if child.children.first == '|'
184
+ expand_iteration(child, state)
185
+ end
186
+ end
187
+ sequence_paths(node, state)
188
+ end
189
+
190
+ # /(a)?/ in state 0 => [(a), state 1], ['', state 0]; ?? reverses the order.
191
+ def optional_iteration_paths(node, state)
192
+ throw :unsupported_capture_loop unless node.zero_or_one?
193
+
194
+ present = expand_iteration(node.with(quantifier: nil), state)
195
+ absent = [[Node.new, state]]
196
+ node.quantifier.reluctant? ? absent + present : present + absent
197
+ end
198
+
199
+ # /(a)\1/ starting in state 0 => /(a)(?:(?:a))/ in state 1:
200
+ # the first child sets the capture before the second child reads it.
201
+ def sequence_paths(node, state)
202
+ results = [[[], state]]
203
+ node.children.each do |child|
204
+ results = results.flat_map do |parts, previous_state|
205
+ following = expand_iteration(child, previous_state)
206
+ if following.length > 1 && parts.any? { |part| can_backtrack?(part) }
207
+ throw :unsupported_capture_loop
208
+ end
209
+ following.map { |part, next_state| [[*parts.map(&:clone), part], next_state] }
210
+ end
211
+ end
212
+ results.map do |parts, next_state|
213
+ next_state |= @capture_bits[node.reference] if node.type == :captured_group && @capture_bits.key?(node.reference)
214
+ [node.with(children: parts), next_state]
215
+ end
216
+ end
217
+
218
+ # If capture 1 holds a, \1 => /(?:(?:a))/; if it has not participated,
219
+ # \1 => /(?:(?!))/ instead.
220
+ def substitute_known_captures(node, state)
221
+ return node.clone if node.is_a?(String)
222
+
223
+ case node.type
224
+ when :backref, :capture_backref, :multiplex_backref
225
+ references = node.type == :backref ? [node.reference] : node.children.map(&:reference)
226
+ if references.any? { |reference| @capture_bits.key?(reference) }
227
+ throw :unsupported_capture_loop unless references.all? { |reference| @capture_bits.key?(reference) }
228
+
229
+ reference = references.find { |number| state & @capture_bits[number] != 0 }
230
+ value = reference ? @values[reference].clone : Node.new('(?!)')
231
+ return node.with(children: ['(?:', value, ')'], type: :plain, reference: nil)
232
+ end
233
+ when :conditional, :capture_conditional
234
+ selected = conditional_branch(node, state)
235
+ if selected
236
+ return node.with(children: ['(?:', substitute_known_captures(selected, state), ')'], type: :plain, reference: nil)
237
+ end
238
+ end
239
+ node.with(children: node.children.map { |child| substitute_known_captures(child, state) })
240
+ end
241
+
242
+ # (?(1)b|c) selects b when bit 1 is set, and c when it is clear.
243
+ def conditional_branch(node, state)
244
+ references = node.condition_references
245
+ return unless references.any? { |reference| @capture_bits.key?(reference) }
246
+
247
+ throw :unsupported_capture_loop unless references.all? { |ref| @capture_bits.key?(ref) }
248
+ present = references.any? { |reference| state & @capture_bits[reference] != 0 }
249
+ branches = node.conditional_branches
250
+ (present ? branches[0] : branches[1]) || Node.new
251
+ end
252
+
253
+ # /(?:ab){2}/ => 'abab'; /a?/ => nil because its value can vary.
254
+ def constant_word(expression)
255
+ quantifier = Node::Quantifier.from_expression(expression)
256
+ return unless !quantifier || quantifier.fixed?
257
+ return '' if quantifier&.zero?
258
+
259
+ word =
260
+ case expression.type
261
+ when :literal then expression.text
262
+ when :group, :expression
263
+ children = expression.expressions.map { |child| constant_word(child) }
264
+ children.join if children.all?
265
+ when :escape
266
+ characters = CharacterSet.of_expression(expression)
267
+ characters.first.chr(Encoding::UTF_8) if characters.size == 1
268
+ end
269
+
270
+ (quantifier ? quantifier.repeat_word(word) : word) if word
271
+ end
272
+
273
+ # /a+/ => true; /a{2}/ => false.
274
+ def can_backtrack?(node)
275
+ return false if node.is_a?(String)
276
+ return true if node.quantifier && !node.quantifier.fixed?
277
+ return true if node.internal_reference?
278
+ return true if node.children.any? { |child| (child.is_a?(Node) && child.branch?) }
279
+
280
+ node.children.any? { |child| can_backtrack?(child) }
281
+ end
282
+
283
+ # (/a/, false) => /(?:a)*/; (/a/, true) => /(?:a)*?/.
284
+ def repeat(node, reluctant)
285
+ Node.new('(?:', node, ')').quantify(0, -1, reluctant: reluctant)
286
+ end
287
+ end