js_regex 3.14.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 (42) hide show
  1. checksums.yaml +4 -4
  2. data/lib/js_regex/conversion.rb +0 -1
  3. data/lib/js_regex/converter/anchor_converter.rb +2 -2
  4. data/lib/js_regex/converter/backreference_converter.rb +16 -51
  5. data/lib/js_regex/converter/base.rb +27 -25
  6. data/lib/js_regex/converter/conditional_converter.rb +10 -4
  7. data/lib/js_regex/converter/context.rb +27 -7
  8. data/lib/js_regex/converter/escape_converter.rb +3 -29
  9. data/lib/js_regex/converter/group_converter.rb +12 -6
  10. data/lib/js_regex/converter/literal_converter.rb +13 -45
  11. data/lib/js_regex/converter/meta_converter.rb +13 -13
  12. data/lib/js_regex/converter/property_converter.rb +2 -20
  13. data/lib/js_regex/converter/set_converter.rb +7 -36
  14. data/lib/js_regex/converter/type_converter.rb +43 -66
  15. data/lib/js_regex/node/checks.rb +70 -0
  16. data/lib/js_regex/node/conditional_parts.rb +17 -0
  17. data/lib/js_regex/node/quantifier.rb +134 -0
  18. data/lib/js_regex/node/stringification.rb +14 -0
  19. data/lib/js_regex/node/transformations.rb +56 -0
  20. data/lib/js_regex/node/traversal.rb +24 -0
  21. data/lib/js_regex/node.rb +46 -44
  22. data/lib/js_regex/second_pass/alternate_conditional_permutations.rb +89 -0
  23. data/lib/js_regex/second_pass/delimit_backrefs.rb +31 -0
  24. data/lib/js_regex/second_pass/expand_bounded_capture_loops.rb +79 -0
  25. data/lib/js_regex/second_pass/expand_constant_capture_loops.rb +287 -0
  26. data/lib/js_regex/second_pass/expand_nullable_captures.rb +327 -0
  27. data/lib/js_regex/second_pass/handle_non_participating_backrefs.rb +69 -0
  28. data/lib/js_regex/second_pass/preserve_repeated_captures.rb +370 -0
  29. data/lib/js_regex/second_pass/resolve_capture_references.rb +116 -0
  30. data/lib/js_regex/second_pass/substitute_root_level_keep_mark.rb +26 -0
  31. data/lib/js_regex/second_pass.rb +363 -141
  32. data/lib/js_regex/utils/captures.rb +46 -0
  33. data/lib/js_regex/utils/char_sets.rb +34 -0
  34. data/lib/js_regex/utils/char_types.rb +18 -0
  35. data/lib/js_regex/utils/escapes.rb +32 -0
  36. data/lib/js_regex/utils/literals.rb +53 -0
  37. data/lib/js_regex/utils/properties.rb +19 -0
  38. data/lib/js_regex/{converter → utils}/property_map.csv +31 -5
  39. data/lib/js_regex/utils.rb +8 -0
  40. data/lib/js_regex/version.rb +1 -1
  41. data/lib/js_regex.rb +3 -3
  42. metadata +26 -4
@@ -1,183 +1,405 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class JsRegex
4
- #
5
- # After conversion of a full Regexp::Expression tree, this
6
- # checks for Node instances that need further processing.
7
- #
8
- module SecondPass
9
- class << self
10
- def call(tree)
11
- substitute_root_level_keep_mark(tree)
12
- alternate_conditional_permutations(tree)
13
- handle_non_participating_backrefs(tree)
14
- tree
15
- end
16
-
17
- private
18
-
19
- def substitute_root_level_keep_mark(tree)
20
- keep_mark_index = nil
21
- tree.children.each.with_index do |child, i|
22
- break keep_mark_index = i if child.type == :keep_mark
23
- end
24
- return unless keep_mark_index
25
-
26
- pre = tree.children[0...keep_mark_index]
27
- post = tree.children[(keep_mark_index + 1)..-1]
28
- lookbehind = Node.new('(?<=', *pre, ')')
29
- tree.update(children: [lookbehind, *post])
30
- end
31
-
32
- def handle_non_participating_backrefs(tree)
33
- level = 0
34
- completed_group_numbers = {}
35
- group_branches = {}
36
- branch_stack = []
37
-
38
- crawl(tree, true) do |node, event|
39
- case [node.type, event]
40
- when [:branch, :enter]
41
- branch_stack.push(node)
42
- when [:branch, :exit]
43
- branch_stack.pop
44
- when [:captured_group, :enter]
45
- level += 1
46
- when [:captured_group, :exit]
47
- unless node.optional? # ignore optional groups
48
- group_branches[node.reference] = branch_stack.last
49
- end
50
- number = level
51
- number += 1 while completed_group_numbers[number]
52
- completed_group_numbers[number] = true
53
- level -= 1
54
- when [:backref, :exit]
55
- ref_branch = group_branches[node.reference]
56
- current_branch = branch_stack.last
57
-
58
- # make bad backrefs non-matchable
59
- references_other_branch =
60
- ref_branch && current_branch && ref_branch != current_branch
61
- forward_reference = !completed_group_numbers[node.reference]
62
- if references_other_branch || forward_reference
63
- node.update(type: :plain, children: ['(?!)'])
64
- end
65
- end
66
- end
4
+ # Run ordered rewrites on the converted node tree to preserve Ruby matching
5
+ # semantics in JavaScript. Share traversal, capture analysis, and node-building
6
+ # helpers with the individual steps.
7
+ # Example: /\1(a)/ => /(?!)(a)/, since Ruby rejects the forward reference.
8
+ class SecondPass
9
+ Node = JsRegex::Node
10
+
11
+ Dir[File.join(__dir__, 'second_pass', '*.rb')].sort.each do |file|
12
+ require file
13
+ end
14
+
15
+ # `tree` is a JsRegex::Node. SecondPass and its subclasses mutate it.
16
+ def self.call(tree)
17
+ new(tree).call
18
+ tree
19
+ end
20
+
21
+ def initialize(tree)
22
+ self.tree = tree
23
+ end
24
+
25
+ def call
26
+ # the whole second pass is only needed if the regexp uses specific features.
27
+ return unless tree.walk.any? { |node| node.internal_reference? || node.type == :keep_mark }
28
+
29
+ STEPS.each { |step| step.call(tree) }
30
+ end
31
+
32
+ STEPS = [
33
+ SubstituteRootLevelKeepMark,
34
+ # Expand bounded loops first so later steps can inspect each iteration.
35
+ ExpandBoundedCaptureLoops,
36
+ ExpandNullableCaptures,
37
+ ExpandConstantCaptureLoops,
38
+ PreserveRepeatedCaptures,
39
+ # Resolve capture histories after all rewrites that can duplicate groups.
40
+ ResolveCaptureReferences,
41
+ AlternateConditionalPermutations,
42
+ HandleNonParticipatingBackrefs,
43
+ # Conditional expansion can renumber references, so delimit them last.
44
+ DelimitBackrefs,
45
+ ].freeze
46
+
47
+ private
48
+
49
+ attr_accessor :tree
50
+
51
+ #
52
+ # Shared helper methods used in multiple subclasses / steps
53
+ #
54
+
55
+ # Children ['(?:', a, ')'] => 2, the insertion point before the closing ')'.
56
+ def sequence_end(node)
57
+ return if node.alternation? || node.conditional?
58
+
59
+ head = node.children.first
60
+ if ['(', '(?:', '(?=', '(?!', '(?<=', '(?<!'].include?(head) && node.children.last == ')'
61
+ node.children.length - 1
62
+ elsif !(head.instance_of?(String) && head.start_with?('('))
63
+ node.children.length
67
64
  end
65
+ end
68
66
 
69
- def alternate_conditional_permutations(tree)
70
- permutations = conditional_tree_permutations(tree)
71
- return if permutations.empty?
67
+ # /(a)\1\1/ => {1 => 2}; the capture itself is not counted.
68
+ def reference_counts(node)
69
+ counts = Hash.new(0)
70
+ node.walk { |n| counts[n.reference] += 1 if n.reference && n.type != :captured_group }
71
+ counts
72
+ end
72
73
 
73
- alternatives = permutations.map.with_index do |variant, i|
74
- Node.new((i.zero? ? '(?:' : '|(?:'), variant, ')', type: :branch)
75
- end
76
- tree.update(children: alternatives)
74
+ # Capture 1 at \1 in /(a)\1/ => true; in /(a)?\1/ => false.
75
+ def required_on_path?(group_path, path)
76
+ group_path&.all? do |ancestor|
77
+ !skippable_scope?(ancestor) || path.include?(ancestor)
77
78
  end
79
+ end
78
80
 
79
- def conditional_tree_permutations(tree)
80
- conds = conditions(tree)
81
- return [] if conds.empty?
81
+ # /(a)?/ => true; /(a)/ => false; a branch of /a|b/ => true.
82
+ def skippable_scope?(node)
83
+ node.optional? || node.branch? || node.negative_assertion?
84
+ end
82
85
 
83
- caps_per_branch = captured_group_count(tree)
86
+ # Force a node to occur at least once (unless it is quantified with {0,0}).
87
+ # /(a)?/ => /(a)/; /(a)*?/ => /(a){1,}?/.
88
+ def min_quantify(node)
89
+ return unless node.optional?
84
90
 
85
- condition_permutations(conds).map.with_index do |truthy_conds, i|
86
- tree_permutation = tree.clone
87
- # find referenced groups and conditionals and make one-sided
88
- crawl(tree_permutation) do |node|
89
- build_permutation(node, conds, truthy_conds, caps_per_branch, i)
90
- end
91
- tree_permutation
92
- end
91
+ if node.zero_quantified?
92
+ node.update(children: ['(?!)', *node.children], quantifier: nil)
93
+ elsif node.max_one?
94
+ node.update(quantifier: nil)
95
+ else
96
+ node.update(quantifier: node.quantifier.with_minimum_one)
93
97
  end
98
+ end
94
99
 
95
- def crawl(node, trace = false, &block)
96
- return if node.instance_of?(String)
100
+ # Discard a node's contents, retaining only disabled capture slots.
101
+ # /(a(b))+/ => /(()){0}/; /(?:abc)/ => an empty sequence.
102
+ # Plain () would participate; quantifying with {0} keeps it unmatched.
103
+ def discard_contents(node)
104
+ captures = []
105
+ node.walk(skip_self: true) { |el| captures << empty_capture(el) if el.type == :captured_group }
106
+ if node.type == :captured_group
107
+ node.update(children: ['(', *captures, ')'])
108
+ elsif captures.empty?
109
+ return node.update(type: :plain, reference: nil, children: [], quantifier: nil)
110
+ else
111
+ node.update(type: :plain, reference: nil, children: ['(?:', *captures, ')'])
112
+ end
113
+ node.quantify(0, 0)
114
+ end
97
115
 
98
- trace ? yield(node, :enter) : yield(node)
99
- node.children.each { |child| crawl(child, trace, &block) }
100
- trace && yield(node, :exit)
116
+ # A copy of /(a*)+/ => /()/, with the same capture ID and an empty value.
117
+ def empty_capture(capture)
118
+ Node.new('(', ')', reference: capture.reference, type: :captured_group, nullable: true)
119
+ end
120
+
121
+ # With capture 1 present: /(?(1)b|(c))/ => /(?:(?:b)(?:()){0})/.
122
+ # Keep discarded captures in source order so later references keep their numbers.
123
+ def select_conditional_branch(node, truthy)
124
+ branches = node.conditional_branches
125
+ branches.each.with_index { |branch, i| discard_contents(branch) unless i == (truthy ? 0 : 1) }
126
+ node.update(type: :plain, reference: nil, children: ['(?:', *branches, ')'])
127
+ end
128
+
129
+ # At the end of the input, a backreference succeeds only when its value
130
+ # is empty or unmatched. Negating that test checks for a nonempty value.
131
+ # (1, true) => /(?=[\s\S]*(?![\s\S])(?!\1))/;
132
+ # (1, false) => /(?=[\s\S]*(?![\s\S])\1)/.
133
+ def nonempty_capture_check(reference, nonempty)
134
+ backref = capture_value(reference)
135
+ test = nonempty ? Node.new('(?!', backref, ')') : backref
136
+ Node.new('(?=[\\s\\S]*(?![\\s\\S])', test, ')')
137
+ end
138
+
139
+ # /(a)(b)?/ => [2]; /(?:(a)|b)/ => [1].
140
+ def potentially_absent_groups(node = tree, optional = false, reachable_only: false)
141
+ return [] if node.instance_of?(String) || (reachable_only && unmatchable?(node))
142
+
143
+ optional ||= node.optional? || node.conditional? ||
144
+ node.negative_assertion?
145
+ groups = optional && node.type == :captured_group ? [node.reference] : []
146
+ children = node.children
147
+ if node.alternation?
148
+ children = children.reject { |child| unmatchable?(child) } if reachable_only
149
+ optional ||= children.length > 1
150
+ end
151
+ children.each do |child|
152
+ groups.concat(potentially_absent_groups(child, optional, reachable_only: reachable_only))
101
153
  end
154
+ groups
155
+ end
102
156
 
103
- def conditions(tree)
104
- conditions = []
105
- crawl(tree) do |node|
106
- conditions << node.reference if node.type.equal?(:conditional)
107
- end
108
- conditions
157
+ # With a generated failure node: /a(?!)/ => true; /(?!)?/ => false,
158
+ # because the failing part can be skipped.
159
+ def unmatchable?(node)
160
+ return node == '(?!)' if node.instance_of?(String)
161
+ return false if node.optional? || node.negative_assertion?
162
+
163
+ if node.conditional?
164
+ branches = node.conditional_branches
165
+ branches.length == 2 && branches.all? { |child| unmatchable?(child) }
166
+ elsif node.alternation?
167
+ node.children.all? { |child| unmatchable?(child) }
168
+ else
169
+ node.children.any? { |child| unmatchable?(child) }
109
170
  end
171
+ end
172
+
173
+ # Make sure the referenced group actually matches by preventing optional
174
+ # groups and alternative branches from skipping it. Returns true if the
175
+ # referenced group is anywhere inside this node, including the node itself.
176
+ # Requiring capture 1: /(?:(a)|b)?/ => /(?:(a)|b(?!))/; returns true.
177
+ def require_group_participation(node, reference)
178
+ return false if node.instance_of?(String)
110
179
 
111
- def captured_group_count(tree)
112
- count = 0
113
- crawl(tree) { |node| count += 1 if node.type.equal?(:captured_group) }
114
- count
180
+ children_with_group = node.children.map do |child|
181
+ require_group_participation(child, reference)
115
182
  end
183
+ found = node.type == :captured_group && node.reference == reference
184
+ return false unless found || children_with_group.any?
116
185
 
117
- def condition_permutations(conditions)
118
- (0..(conditions.length)).inject([]) do |arr, n|
119
- arr + conditions.combination(n).to_a
186
+ min_quantify(node)
187
+ if node.children.length > 1 && node.alternation?
188
+ node.children.each.with_index do |branch, i|
189
+ branch << Node.new('(?!)') unless children_with_group[i]
120
190
  end
121
191
  end
192
+ true
193
+ end
194
+
195
+ # After copying capture 1: /(a)(a)\1/ => /(a)(a)history[2,1]/,
196
+ # where "history" is a placeholder for renumbering references later.
197
+ # The two groups start with the same annotation and receive distinct numbers.
198
+ def renumber_captures
199
+ nodes, paths = reference_nodes_with_paths
200
+ candidates = renumber_groups(nodes, paths)
201
+ nodes.each do |node|
202
+ parent = paths[node][-2]
203
+ # A capture history replaces its children when it is renumbered.
204
+ next if parent&.type == :capture_backref
122
205
 
123
- def build_permutation(node, conds, truthy_conds, caps_per_branch, i)
124
- truthy = truthy_conds.include?(node.reference)
125
-
126
- case node.type
127
- when :backref
128
- # We cannot use named groups or backrefs in the conditional expansion,
129
- # their repetition would cause a "Duplicate capture group name" error in JS.
130
- node.update(children: [
131
- node.children.first.sub(/k<.*>/, node.reference.to_s)
132
- ])
133
- # backref numbers need to be incremented for subsequent "branches"
134
- adapt_backref_to_permutation(node, caps_per_branch, i)
135
- when :captured_group
136
- # if the group is referenced by any condition, modulate its quantity
137
- if conds.include?(node.reference)
138
- adapt_referenced_group_to_permutation(node, truthy)
206
+ renumber_reference(node, candidates)
207
+ end
208
+ end
209
+
210
+ # Original capture IDs [1, 1, 2] => [1, 2, 3]; candidates for old ID 1
211
+ # are [2, 1], before excluding copies on incompatible paths.
212
+ def renumber_groups(nodes, paths)
213
+ positions = Hash.new { |hash, key| hash[key] = [] }
214
+ capture_paths = {}
215
+ nodes.select { |node| node.type == :captured_group }.each.with_index do |node, index|
216
+ number = index + 1
217
+ positions[node.reference].unshift(number)
218
+ capture_paths[number] = paths[node]
219
+ node.renumber(number)
220
+ end
221
+ lambda do |reference, node|
222
+ positions[reference].reject do |number|
223
+ capture_path = capture_paths[number]
224
+ reference_path = paths[node]
225
+ # Captures from a negative assertion are always unset outside it.
226
+ mutually_exclusive?(capture_path, reference_path) || capture_path.any? do |ancestor|
227
+ ancestor.negative_assertion? && !reference_path.include?(ancestor)
139
228
  end
140
- when :conditional
141
- adapt_conditional_to_permutation(node, truthy)
142
229
  end
143
230
  end
231
+ end
232
+
233
+ # For /(a)\1/, collect the capture and reference nodes with their paths
234
+ # from the root; ordinary literal nodes are omitted.
235
+ def reference_nodes_with_paths
236
+ nodes = []
237
+ paths = {}
238
+ tree.walk(trace: true) do |node, event, path|
239
+ next unless event == :enter
240
+ next unless node.reference || [:capture_backref, :capture_conditional].include?(node.type)
144
241
 
145
- def adapt_referenced_group_to_permutation(group_node, truthy)
146
- truthy ? min_quantify(group_node) : null_quantify(group_node)
242
+ nodes << node
243
+ paths[node] = path.dup
147
244
  end
245
+ [nodes, paths]
246
+ end
148
247
 
149
- def adapt_conditional_to_permutation(conditional_node, truthy)
150
- branches = conditional_node.children[1...-1]
151
- if branches.count == 1
152
- truthy || null_quantify(branches.first)
248
+ # A condition on capture 1, copied into groups 2 and 3, becomes a condition
249
+ # on either copy; a backreference becomes history[3,2].
250
+ def renumber_reference(node, candidates)
251
+ case node.type
252
+ when :backref, :capture_check
253
+ renumber_backreference(node, candidates.call(node.reference, node))
254
+ when :conditional
255
+ references = candidates.call(node.reference, node)
256
+ if references.length == 1
257
+ node.renumber(references.first)
153
258
  else
154
- null_quantify(truthy ? branches.last : branches.first)
259
+ checks = Node.new(*references.map { |number| Node.new(reference: number) })
260
+ node.update(type: :capture_conditional, reference: nil,
261
+ children: [checks, *node.conditional_branches])
155
262
  end
156
- conditional_node.update(type: :plain)
263
+ when :capture_backref
264
+ references = node.children.flat_map { |child| candidates.call(child.reference, node) }
265
+ node.update(children: references.map { |number| backref(number) })
266
+ when :capture_conditional
267
+ checks = node.children.first
268
+ references = checks.children.flat_map { |child| candidates.call(child.reference, node) }
269
+ checks.update(children: references.map { |number| Node.new(reference: number) })
157
270
  end
271
+ end
158
272
 
159
- def adapt_backref_to_permutation(backref_node, caps_per_branch, i)
160
- new_num = backref_node.reference + caps_per_branch * i
161
- backref_node.update(children: ["\\#{new_num}"])
273
+ # \1 with candidates [3] => \3; with [3, 2] => history[3,2].
274
+ def renumber_backreference(node, references)
275
+ if references.length == 1
276
+ node.renumber(references.first)
277
+ elsif references.empty? && node.type == :capture_check
278
+ node.update(type: :plain, reference: nil, children: [''])
279
+ else
280
+ node.update(type: :capture_backref, reference: nil,
281
+ children: references.map { |number| backref(number) })
162
282
  end
283
+ end
163
284
 
164
- def min_quantify(node)
165
- return unless node.optional?
285
+ # 1 => a /\1/ node that must fail if capture 1 did not participate.
286
+ def backref(number)
287
+ Node.new("\\#{number}", reference: number, type: :backref)
288
+ end
166
289
 
167
- qtf = node.quantifier
168
- if qtf.max.equal?(1) # any zero_or_one quantifier (?, ??, ?+)
169
- node.update(quantifier: nil)
170
- else
171
- min_quantifier = qtf.dup
172
- min_quantifier.text = "{1,#{qtf.max}}#{'?' if qtf.reluctant?}"
173
- node.update(quantifier: min_quantifier)
290
+ # These references inspect a value and permit an unmatched capture.
291
+ # 1 => a /\1/ node that may match empty when capture 1 did not participate.
292
+ def capture_value(number)
293
+ Node.new("\\#{number}", reference: number, type: :capture_check)
294
+ end
295
+
296
+ # (?(1)a|b) => a condition over capture 1's copies, keeping branches a and b
297
+ # for ResolveCaptureReferences to choose using the current capture values.
298
+ def make_condition_dynamic(node)
299
+ checks = Node.new(Node.new(reference: node.reference))
300
+ node.update(type: :capture_conditional, reference: nil, children: [checks, *node.conditional_branches])
301
+ end
302
+
303
+ # [/a/, /b/] => /(?:a|b)/; [] => /(?!)/.
304
+ def alternate(branches)
305
+ return Node.new('(?!)') if branches.empty?
306
+ return branches.first if branches.length == 1
307
+
308
+ parts = branches.map { |branch| (branch.is_a?(Node) && branch.branch?) ? branch : Node.new(branch, type: :branch) }
309
+ parts.each.with_index { |part, index| part.children.unshift('|') unless index.zero? }
310
+ Node.new('(?:', Node.new(*parts), ')')
311
+ end
312
+
313
+ # /(?:a|b)/ => [/(?:a)/, /(?:b)/], preserving wrappers on each choice.
314
+ def split_alternatives(node)
315
+ if node.alternation?
316
+ node.children.map do |branch|
317
+ chosen = branch.with(type: :plain)
318
+ chosen.children.shift if chosen.children.first == '|'
319
+ chosen
320
+ end
321
+ else
322
+ grouped = ['(', '(?:'].include?(node.children.first) && node.children.last == ')'
323
+ return unless grouped || node.children.length == 1
324
+
325
+ children = node.children.grep(Node)
326
+ return unless children.length == 1 && !children.first.quantifier
327
+
328
+ alternatives = split_alternatives(children.first)
329
+ return unless alternatives
330
+
331
+ alternatives.map do |alternative|
332
+ node.with(children: node.children.map { |el| el.equal?(children.first) ? alternative : el.clone })
174
333
  end
175
334
  end
335
+ end
336
+
337
+ # Body /a/ => /(a)/ with a fresh negative capture ID, numbered later.
338
+ def temporary_capture(*children)
339
+ # Negative IDs avoid existing group numbers until renumber_captures runs.
340
+ @temporary_reference ||= -1
341
+ Node.new('(', *children, ')', reference: (@temporary_reference -= 1), type: :captured_group)
342
+ end
343
+
344
+ # If capture 1 saved the remaining input before an iteration, emit /(?!\1)/
345
+ # to reject an iteration that leaves the input position unchanged.
346
+ def require_progress(reference)
347
+ Node.new('(?!', capture_value(reference), ')')
348
+ end
349
+
350
+ def common_path_length(left, right)
351
+ length = 0
352
+ length += 1 while left[length] && left[length].equal?(right[length])
353
+ length
354
+ end
355
+
356
+ # Paths to (a) and \1 in /(a)|\1/ => true; in /(?:(a)|\1)+/ => false,
357
+ # since different iterations can take different branches.
358
+ def mutually_exclusive?(left, right)
359
+ common_length = common_path_length(left, right)
360
+ owner = left[common_length - 1] if common_length > 0
361
+ return false unless owner&.alternation?
362
+
363
+ # Different branches can still participate on different loop iterations.
364
+ left[0...common_length].none? { |node| node.repeated? }
365
+ end
176
366
 
177
- def null_quantify(node)
178
- null_quantifier = Regexp::Expression::Quantifier.construct(text: '{0}')
179
- node.update(quantifier: null_quantifier)
367
+ # Visit unbounded loops with their continuation, replacing the first suffix
368
+ # for which the caller supplies a rewrite. Do not descend into repetitions.
369
+ def rewrite_loop_and_tail(node, &block)
370
+ return false unless node.is_a?(Node)
371
+ return false if node.repeated?
372
+
373
+ finish = sequence_end(node)
374
+ if finish
375
+ node.children.each.with_index do |child, index|
376
+ next unless child.is_a?(Node) && child.quantifier&.unbounded? && child.quantifier.minimum_at_most_one?
377
+
378
+ following = Node.new(*node.children[(index + 1)...finish])
379
+ replacement = yield(child, following)
380
+ next unless replacement
381
+
382
+ node.update(children: [*node.children[0...index], replacement, *node.children[finish..-1]])
383
+ return true
384
+ end
180
385
  end
386
+ node.children.any? { |child| rewrite_loop_and_tail(child, &block) }
387
+ end
388
+
389
+ # /(a(b))/ => /(?:a(?:b))/.
390
+ def without_captures(node)
391
+ copy = node.clone
392
+ copy.walk { |part| part.make_passive if part.type == :captured_group }
393
+ copy
394
+ end
395
+
396
+ # /a?/ => true; /a/ => false.
397
+ def may_match_empty?(node)
398
+ match_length(node).min.zero?
399
+ end
400
+
401
+ def match_length(node)
402
+ Regexp::Parser.parse(node.to_s).match_length
181
403
  end
182
404
  end
183
405
  end
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ class JsRegex
4
+ module Utils
5
+ module Captures
6
+ # Unlike assertions and backreferences, these expressions can match an
7
+ # empty string at every input position, regardless of surrounding text.
8
+ def self.nullable?(expression, quantified = false)
9
+ return true if quantified && JsRegex::Node::Quantifier.from_expression(expression)&.optional?
10
+
11
+ case expression.type
12
+ when :literal
13
+ expression.text.empty?
14
+ when :group, :expression
15
+ !expression.terminal? && expression.token != :absence &&
16
+ expression.expressions.all? { |child| nullable?(child, true) }
17
+ when :meta
18
+ expression.token == :alternation && expression.expressions.any? { |child| nullable?(child, true) }
19
+ else
20
+ false
21
+ end
22
+ end
23
+
24
+ # A participating capture ignores its own quantifier. Treat backreferences
25
+ # conservatively rather than following potentially recursive references.
26
+ def self.nonempty?(expression, quantified = false)
27
+ return false if quantified && JsRegex::Node::Quantifier.from_expression(expression)&.optional?
28
+
29
+ case expression.type
30
+ when :literal
31
+ !expression.text.empty?
32
+ when :escape, :property, :nonproperty, :set, :type
33
+ true
34
+ when :group, :expression
35
+ !expression.terminal? && expression.token != :absence &&
36
+ expression.expressions.any? { |child| nonempty?(child, true) }
37
+ when :meta
38
+ expression.token == :dot ||
39
+ (expression.token == :alternation && expression.expressions.all? { |child| nonempty?(child, true) })
40
+ else
41
+ false
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'character_set'
4
+
5
+ class JsRegex
6
+ module Utils
7
+ module CharSets
8
+ def self.convert(expression, context)
9
+ # Calculate the set without case folding so the original casing is
10
+ # preserved when the JavaScript regexp already has the i flag.
11
+ neutral_expression = expression.dup
12
+ neutral_expression.options[:i] = false
13
+ unless neutral_expression.terminal?
14
+ neutral_expression.each_expression(true) { |exp| exp.options[:i] = false }
15
+ end
16
+ content = CharacterSet.of_expression(neutral_expression)
17
+
18
+ if expression.case_insensitive? && !context.case_insensitive_root
19
+ content = content.case_insensitive
20
+ elsif !expression.case_insensitive? && context.case_insensitive_root
21
+ context.warn_of("Dropped unsupported nested case-sensitive character set '#{expression}' "\
22
+ "at index #{expression.ts}")
23
+ end
24
+
25
+ if context.es_2015_or_higher?
26
+ context.enable_u_option if content.astral_part?
27
+ content.to_s(format: 'es6', in_brackets: true)
28
+ else
29
+ content.to_s_with_surrogate_ranges
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ class JsRegex
4
+ module Utils
5
+ module CharTypes
6
+ def self.directly_compatible?(expression)
7
+ case expression.token
8
+ when :space, :nonspace
9
+ !expression.ascii_classes?
10
+ when :digit, :nondigit, :word, :nonword
11
+ !expression.unicode_classes?
12
+ else # :hex, :nonhex, :linebreak, :xgrapheme
13
+ false
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end