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.
- checksums.yaml +4 -4
- data/lib/js_regex/conversion.rb +2 -1
- data/lib/js_regex/converter/anchor_converter.rb +4 -2
- data/lib/js_regex/converter/assertion_converter.rb +2 -0
- data/lib/js_regex/converter/backreference_converter.rb +33 -12
- data/lib/js_regex/converter/base.rb +38 -27
- data/lib/js_regex/converter/conditional_converter.rb +12 -4
- data/lib/js_regex/converter/context.rb +54 -8
- data/lib/js_regex/converter/escape_converter.rb +6 -30
- data/lib/js_regex/converter/freespace_converter.rb +2 -0
- data/lib/js_regex/converter/group_converter.rb +18 -16
- data/lib/js_regex/converter/keep_converter.rb +2 -0
- data/lib/js_regex/converter/literal_converter.rb +16 -46
- data/lib/js_regex/converter/meta_converter.rb +15 -13
- data/lib/js_regex/converter/property_converter.rb +4 -20
- data/lib/js_regex/converter/set_converter.rb +10 -37
- data/lib/js_regex/converter/subexpression_converter.rb +2 -0
- data/lib/js_regex/converter/type_converter.rb +45 -39
- data/lib/js_regex/converter/unsupported_token_converter.rb +2 -0
- data/lib/js_regex/converter.rb +2 -0
- data/lib/js_regex/error.rb +2 -0
- data/lib/js_regex/node/checks.rb +70 -0
- data/lib/js_regex/node/conditional_parts.rb +17 -0
- data/lib/js_regex/node/quantifier.rb +134 -0
- data/lib/js_regex/node/stringification.rb +14 -0
- data/lib/js_regex/node/transformations.rb +56 -0
- data/lib/js_regex/node/traversal.rb +24 -0
- data/lib/js_regex/node.rb +49 -40
- data/lib/js_regex/second_pass/alternate_conditional_permutations.rb +89 -0
- data/lib/js_regex/second_pass/delimit_backrefs.rb +31 -0
- data/lib/js_regex/second_pass/expand_bounded_capture_loops.rb +79 -0
- data/lib/js_regex/second_pass/expand_constant_capture_loops.rb +287 -0
- data/lib/js_regex/second_pass/expand_nullable_captures.rb +327 -0
- data/lib/js_regex/second_pass/handle_non_participating_backrefs.rb +69 -0
- data/lib/js_regex/second_pass/preserve_repeated_captures.rb +370 -0
- data/lib/js_regex/second_pass/resolve_capture_references.rb +116 -0
- data/lib/js_regex/second_pass/substitute_root_level_keep_mark.rb +26 -0
- data/lib/js_regex/second_pass.rb +364 -107
- data/lib/js_regex/target.rb +2 -0
- data/lib/js_regex/utils/captures.rb +46 -0
- data/lib/js_regex/utils/char_sets.rb +34 -0
- data/lib/js_regex/utils/char_types.rb +18 -0
- data/lib/js_regex/utils/escapes.rb +32 -0
- data/lib/js_regex/utils/literals.rb +53 -0
- data/lib/js_regex/utils/properties.rb +19 -0
- data/lib/js_regex/{converter → utils}/property_map.csv +31 -5
- data/lib/js_regex/utils.rb +8 -0
- data/lib/js_regex/version.rb +3 -1
- data/lib/js_regex.rb +5 -3
- metadata +29 -10
|
@@ -0,0 +1,370 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Rewrite unbounded repetitions so captures survive later iterations that skip
|
|
4
|
+
# their groups, as in Ruby. Isolate the last capturing iteration, or group each
|
|
5
|
+
# capturing iteration with the intervening iterations that skip the group.
|
|
6
|
+
# Example shape: /(?:(a)|b)+\1/ => /(?:b*?(a))+b*\1/.
|
|
7
|
+
# This shows the successful capturing path, omitting disabled branches and the
|
|
8
|
+
# separate path with no capture, whose required backreference will fail.
|
|
9
|
+
class JsRegex::SecondPass::PreserveRepeatedCaptures < JsRegex::SecondPass
|
|
10
|
+
def call
|
|
11
|
+
collect_references
|
|
12
|
+
return if @references.empty?
|
|
13
|
+
|
|
14
|
+
while @references.uniq.length > 1 && expand_lazy_capture_loops(tree)
|
|
15
|
+
renumber_captures
|
|
16
|
+
collect_references
|
|
17
|
+
end
|
|
18
|
+
rewrite_repetitions(tree)
|
|
19
|
+
renumber_captures if @rewritten
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
# /(a)\1/ => references [1], mandatory references [1]; /(a)\1?/ keeps
|
|
25
|
+
# reference 1 but does not mark it mandatory.
|
|
26
|
+
def collect_references
|
|
27
|
+
@references = []
|
|
28
|
+
@groups = {}
|
|
29
|
+
group_paths = {}
|
|
30
|
+
reference_paths = []
|
|
31
|
+
tree.walk(trace: true) do |node, event, path|
|
|
32
|
+
next if event == :exit
|
|
33
|
+
|
|
34
|
+
if node.type == :captured_group
|
|
35
|
+
@groups[node.reference] = node
|
|
36
|
+
group_paths[node.reference] = path.dup
|
|
37
|
+
elsif node.reference
|
|
38
|
+
@references << node.reference
|
|
39
|
+
end
|
|
40
|
+
if node.type == :backref && path.none? { |ancestor| [:multiplex_backref, :capture_backref].include?(ancestor.type) }
|
|
41
|
+
reference_paths << [node.reference, path.dup]
|
|
42
|
+
end
|
|
43
|
+
end
|
|
44
|
+
# A reference can be required within its own branch or optional parent,
|
|
45
|
+
# even when that entire scope can be skipped by the rest of the pattern.
|
|
46
|
+
@mandatory_references = reference_paths.select do |reference, reference_path|
|
|
47
|
+
required_on_path?(reference_path, group_paths[reference] || [])
|
|
48
|
+
end.map(&:first)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# For /(?:(a)|b)+\1/, choose capture-update cycles; for {2}, leave the loop
|
|
52
|
+
# to ExpandBoundedCaptureLoops.
|
|
53
|
+
def rewrite_repetitions(node)
|
|
54
|
+
return unless node.is_a?(Node)
|
|
55
|
+
|
|
56
|
+
node.children.each { |child| rewrite_repetitions(child) }
|
|
57
|
+
quantifier = node.quantifier
|
|
58
|
+
return unless quantifier&.unbounded?
|
|
59
|
+
|
|
60
|
+
iteration = node.with(quantifier: nil)
|
|
61
|
+
plan = nil
|
|
62
|
+
reference = potentially_absent_groups(iteration, reachable_only: true).find do |number|
|
|
63
|
+
@references.include?(number) && (@groups[number].marked_nonempty? ||
|
|
64
|
+
@groups[number].nullable?) &&
|
|
65
|
+
(plan = rewrite_plan(iteration, number, quantifier))
|
|
66
|
+
end
|
|
67
|
+
return unless reference
|
|
68
|
+
|
|
69
|
+
kind, capture_first = plan
|
|
70
|
+
if kind == :last
|
|
71
|
+
isolate_last_capture(node, iteration, reference, quantifier)
|
|
72
|
+
else
|
|
73
|
+
repeat_capture_updates(node, iteration, reference, quantifier,
|
|
74
|
+
capture_first: capture_first, internal: kind == :retained)
|
|
75
|
+
end
|
|
76
|
+
@rewritten = true
|
|
77
|
+
rewrite_repetitions(node)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
# In /x(?:(a+)|(b+))+?\1\2y/, replace the loop together with /\1\2y/,
|
|
81
|
+
# so its ending can test the continuation before choosing iteration boundaries.
|
|
82
|
+
def expand_lazy_capture_loops(node)
|
|
83
|
+
rewrite_loop_and_tail(node) do |child, following|
|
|
84
|
+
lazy_capture_loop(child, following) if child.quantifier.reluctant?
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
|
|
90
|
+
# Shape for /(?:(a+)|(b+))+?\1\2/: optional b-run, repeated a/b run pairs,
|
|
91
|
+
# then guarded endings. Each run retains its last capture for the continuation.
|
|
92
|
+
def lazy_capture_loop(node, following)
|
|
93
|
+
alternatives = alternating_capture_runs(node, following)
|
|
94
|
+
return unless alternatives
|
|
95
|
+
|
|
96
|
+
left, right = alternatives.map { |part| Node.new(lazy_run(without_captures(part), 0), part) }
|
|
97
|
+
pair = Node.new(left.clone, right.clone)
|
|
98
|
+
ending = lazy_loop_endings(alternatives.first, left, right, pair, following)
|
|
99
|
+
prefix = Node.new('(?:', right, ')').quantify(0, -1, reluctant: true)
|
|
100
|
+
Node.new(prefix, lazy_run(pair, 0), ending)
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
# /(?:(a+)|(b+))+?\2\1/ => [/(b+)/, /(a+)/], ordered by the following refs.
|
|
104
|
+
def alternating_capture_runs(node, following)
|
|
105
|
+
return unless reference_counts(node).empty?
|
|
106
|
+
|
|
107
|
+
iteration = node.with(quantifier: nil)
|
|
108
|
+
alternatives = split_alternatives(iteration)
|
|
109
|
+
return unless alternatives&.length == 2
|
|
110
|
+
|
|
111
|
+
references = alternatives.map { |alternative| referenced_run(alternative) }
|
|
112
|
+
return unless references.all? && references.uniq.length == 2
|
|
113
|
+
|
|
114
|
+
first, second = following.children
|
|
115
|
+
return unless [first, second].all? { |part| part.is_a?(Node) && part.type == :backref && !part.quantifier }
|
|
116
|
+
return unless [first.reference, second.reference].sort == references.sort
|
|
117
|
+
|
|
118
|
+
counts = reference_counts(following)
|
|
119
|
+
return unless references.all? { |reference| counts[reference] == 1 && @references.count(reference) == 1 }
|
|
120
|
+
|
|
121
|
+
alternatives.reverse! if first.reference != references.first
|
|
122
|
+
expressions = alternatives.map { |part| Regexp::Parser.parse(part.to_s) }
|
|
123
|
+
alternatives if disjoint_starts?(*expressions)
|
|
124
|
+
end
|
|
125
|
+
|
|
126
|
+
# An alternative /(a+)/ with capture 1 referenced => 1; /(ab+)/ => nil,
|
|
127
|
+
# since it is not a run of a single character.
|
|
128
|
+
def referenced_run(alternative)
|
|
129
|
+
groups = alternative.walk.select do |part|
|
|
130
|
+
part.type == :captured_group && @references.include?(part.reference)
|
|
131
|
+
end
|
|
132
|
+
return unless groups.length == 1 && groups.first.marked_nonempty?
|
|
133
|
+
return if potentially_absent_groups(alternative).include?(groups.first.reference)
|
|
134
|
+
|
|
135
|
+
groups.first.reference if capture_run?(alternative, groups.first)
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# For /(?:(a+)|(b+))+?\1\2/, endings try the tail, one final a capture,
|
|
139
|
+
# then a split a-run; probes prevent these from outranking an earlier iteration.
|
|
140
|
+
def lazy_loop_endings(first, left, right, pair, following)
|
|
141
|
+
continuation = Node.new(right.clone, lazy_run(pair.clone, 0), alternate([
|
|
142
|
+
following.clone, Node.new(left.clone, following.clone),
|
|
143
|
+
]))
|
|
144
|
+
raw_future = Node.new(first.clone, continuation.clone)
|
|
145
|
+
discarded = without_captures(first)
|
|
146
|
+
|
|
147
|
+
# A final first-character run has two stopping choices: shorten its first
|
|
148
|
+
# iteration, or make another iteration. Prefer any successful continuation
|
|
149
|
+
# from an earlier (longer) first iteration before accepting either choice.
|
|
150
|
+
single_probe = Node.new('(?=', first.clone, following.clone, ')')
|
|
151
|
+
shorter = Node.new(discarded.clone, Node.new('(?!', capture_value(following.children.first.reference), ')'),
|
|
152
|
+
first.clone, continuation.clone)
|
|
153
|
+
single_tail = Node.new(single_probe, Node.new('(?!', alternate([raw_future.clone, shorter]), ')'),
|
|
154
|
+
first.clone, following.clone)
|
|
155
|
+
|
|
156
|
+
discarded_prefix = temporary_capture(discarded.clone)
|
|
157
|
+
split_probe = Node.new('(?=', discarded_prefix, first.clone, following.clone, ')')
|
|
158
|
+
earlier = Node.new(capture_value(discarded_prefix.reference), lazy_run(discarded, 0),
|
|
159
|
+
first.clone, continuation)
|
|
160
|
+
split_tail = Node.new(split_probe, Node.new('(?!', alternate([raw_future, earlier]), ')'),
|
|
161
|
+
backref(discarded_prefix.reference), first.clone, following.clone)
|
|
162
|
+
alternate([following.clone, single_tail, split_tail])
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
# Restrict this rewrite to greedy runs of one character, with the capture
|
|
166
|
+
# covering the entire alternative. Splitting more general words can change
|
|
167
|
+
# the order in which a loop explores its iteration boundaries.
|
|
168
|
+
# /(a+)/ => true; /(a+?)/ => false; /(ab+)/ => nil (also ineligible).
|
|
169
|
+
def capture_run?(node, capture)
|
|
170
|
+
return ['(', '(?:', ')', ''].include?(node) if node.is_a?(String)
|
|
171
|
+
if node.equal?(capture)
|
|
172
|
+
return false if node.quantifier || node.walk.any? { |part| part.quantifier&.reluctant? }
|
|
173
|
+
|
|
174
|
+
expression = Regexp::Parser.parse(node.children.join)
|
|
175
|
+
while !expression.terminal? && !expression.quantifier && expression.expressions.length == 1
|
|
176
|
+
expression = expression.expressions.first
|
|
177
|
+
end
|
|
178
|
+
quantifier = Node::Quantifier.from_expression(expression)
|
|
179
|
+
return !!quantifier&.one_or_more? &&
|
|
180
|
+
!quantifier.reluctant? && expression.match_length.min == 1 &&
|
|
181
|
+
CharacterSet.of_expression(expression).size == 1
|
|
182
|
+
end
|
|
183
|
+
return false if node.quantifier
|
|
184
|
+
|
|
185
|
+
node.children.all? { |child| capture_run?(child, capture) }
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
# (/a/, 0) => /(?:a){0,}?/; (/a/, 1) => /(?:a){1,}?/.
|
|
189
|
+
def lazy_run(node, minimum)
|
|
190
|
+
Node.new('(?:', node, ')').quantify(minimum, -1, reluctant: true)
|
|
191
|
+
end
|
|
192
|
+
|
|
193
|
+
# Capturing-path shape: /(?:(a)|b)+/ => /(?:a|b)*(a)b*/.
|
|
194
|
+
# Earlier (a) copies become passive; the final copy survives all later b's.
|
|
195
|
+
def isolate_last_capture(node, iteration, reference, quantifier)
|
|
196
|
+
absent = iteration.clone
|
|
197
|
+
remove_capture(absent, reference, exclude: true)
|
|
198
|
+
absent.update(quantifier: quantifier)
|
|
199
|
+
branches = quantifier.prefix_counts.map do |prefix_min, prefix_max|
|
|
200
|
+
prefix = iteration.clone
|
|
201
|
+
remove_capture(prefix, reference, exclude: false)
|
|
202
|
+
prefix.quantify(prefix_min, prefix_max, reluctant: quantifier.reluctant?)
|
|
203
|
+
last_update = iteration.clone
|
|
204
|
+
require_group_participation(last_update, reference)
|
|
205
|
+
suffix = iteration.clone
|
|
206
|
+
remove_capture(suffix, reference, exclude: true)
|
|
207
|
+
suffix_min = quantifier.remaining_minimum(prefix_min + 1)
|
|
208
|
+
suffix.quantify(suffix_min, -1, reluctant: quantifier.reluctant?)
|
|
209
|
+
Node.new(prefix, last_update, suffix)
|
|
210
|
+
end
|
|
211
|
+
replace_loop(node, branches, absent, quantifier.reluctant?)
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# For the loop in /(?:(a)|b)+\1/, return [:cycles, true]: each cycle updates
|
|
215
|
+
# capture 1, and its alternative is tried before the skipping alternative.
|
|
216
|
+
def rewrite_plan(iteration, reference, quantifier)
|
|
217
|
+
target = nil
|
|
218
|
+
internal_reference = false
|
|
219
|
+
other_capture = false
|
|
220
|
+
iteration.walk do |node|
|
|
221
|
+
target = node if node.type == :captured_group && node.reference == reference
|
|
222
|
+
other_capture ||= node.type == :captured_group && node.reference != reference && @references.include?(node.reference)
|
|
223
|
+
internal_reference ||= node.internal_reference?
|
|
224
|
+
end
|
|
225
|
+
capturing = iteration.clone
|
|
226
|
+
require_group_participation(capturing, reference)
|
|
227
|
+
skipping = iteration.clone
|
|
228
|
+
remove_capture(skipping, reference, exclude: true)
|
|
229
|
+
captured_expression = Regexp::Parser.parse(analysis_source(capturing))
|
|
230
|
+
skipped_expression = Regexp::Parser.parse(analysis_source(skipping))
|
|
231
|
+
mandatory = @mandatory_references.include?(reference)
|
|
232
|
+
single_capture = (potentially_absent_groups(iteration, reachable_only: true) & @references).uniq.length == 1
|
|
233
|
+
if !internal_reference && !other_capture && single_capture && target.marked_nonempty? && quantifier.minimum_at_most_one? &&
|
|
234
|
+
captured_expression.match_length.min > 0 && skipped_expression.match_length.min > 0
|
|
235
|
+
first = capture_at_edge?(iteration, reference, first: true)
|
|
236
|
+
last = capture_at_edge?(iteration, reference, first: false)
|
|
237
|
+
if (first && !quantifier.reluctant?) || (mandatory && (first || last))
|
|
238
|
+
return [:cycles, first]
|
|
239
|
+
end
|
|
240
|
+
end
|
|
241
|
+
|
|
242
|
+
disjoint = disjoint_starts?(captured_expression, skipped_expression)
|
|
243
|
+
capture = Regexp::Parser.parse(target.children.join)
|
|
244
|
+
empty_capture_safe = target.nullable? && disjoint && !quantifier.reluctant?
|
|
245
|
+
single_character_run = target.marked_nonempty? && CharacterSet.of_expression(capture).size == 1 && !quantifier.reluctant?
|
|
246
|
+
if !internal_reference && (capture.match_length.fixed? || empty_capture_safe || single_character_run) &&
|
|
247
|
+
(disjoint || (single_capture && mandatory && !quantifier.reluctant?))
|
|
248
|
+
[:last, nil]
|
|
249
|
+
elsif (disjoint || (single_capture && mandatory && quantifier.reluctant?)) &&
|
|
250
|
+
quantifier.minimum_at_most_one? && skipped_expression.match_length.min > 0
|
|
251
|
+
[:retained, nil]
|
|
252
|
+
end
|
|
253
|
+
end
|
|
254
|
+
|
|
255
|
+
# /a+/ and /b+/ => true; /a/ and /A/ => false after case folding.
|
|
256
|
+
def disjoint_starts?(left, right)
|
|
257
|
+
left_characters = first_characters(left).case_insensitive
|
|
258
|
+
right_characters = first_characters(right).case_insensitive
|
|
259
|
+
left_characters.disjoint?(right_characters)
|
|
260
|
+
end
|
|
261
|
+
|
|
262
|
+
# Whether branch and optional choices try the capture first or last.
|
|
263
|
+
# In /(?:(a)|b)/, capture 1 is first: true, last: false;
|
|
264
|
+
# /(?:(a)??)/ is first: false, last: true because skipping is tried first.
|
|
265
|
+
def capture_at_edge?(node, reference, first:)
|
|
266
|
+
return false if node.is_a?(String)
|
|
267
|
+
return false if node.optional? && node.quantifier.reluctant? == first
|
|
268
|
+
return true if node.type == :captured_group && node.reference == reference
|
|
269
|
+
|
|
270
|
+
if node.alternation?
|
|
271
|
+
capture_at_edge?(first ? node.children.first : node.children.last, reference, first: first)
|
|
272
|
+
else
|
|
273
|
+
node.children.any? { |child| capture_at_edge?(child, reference, first: first) }
|
|
274
|
+
end
|
|
275
|
+
end
|
|
276
|
+
|
|
277
|
+
# Capturing-path shape: /(?:(a)|b)+/ => /(?:b*?(a))+b*/.
|
|
278
|
+
# Every repeated cycle contains (a), so trailing b iterations cannot clear it.
|
|
279
|
+
def repeat_capture_updates(node, iteration, reference, quantifier, capture_first:, internal:)
|
|
280
|
+
skipping = iteration.clone
|
|
281
|
+
remove_capture(skipping, reference, exclude: true)
|
|
282
|
+
prefix = skipping.with(quantifier: Node::Quantifier.new(0, -1, reluctant: internal ? quantifier.reluctant? : capture_first))
|
|
283
|
+
update = iteration.clone
|
|
284
|
+
require_group_participation(update, reference)
|
|
285
|
+
suffix = skipping.with(quantifier: Node::Quantifier.new(0, -1, reluctant: quantifier.reluctant?))
|
|
286
|
+
# Put skips before or after each update according to the original branch
|
|
287
|
+
# order. Every successful cycle includes the capture, so JS retains it.
|
|
288
|
+
ends_with_capture = !internal && capture_first != quantifier.reluctant?
|
|
289
|
+
cycle = ends_with_capture ? Node.new('(?:', prefix, update, ')') : Node.new('(?:', update, suffix, ')')
|
|
290
|
+
cycle.quantify(1, -1, reluctant: quantifier.reluctant?)
|
|
291
|
+
present = ends_with_capture ? Node.new(cycle, suffix) : Node.new(prefix, cycle)
|
|
292
|
+
absent = skipping.update(quantifier: quantifier)
|
|
293
|
+
replace_loop(node, [present], absent, quantifier.reluctant?)
|
|
294
|
+
node.walk do |child|
|
|
295
|
+
make_condition_dynamic(child) if child.conditional? && child.reference == reference
|
|
296
|
+
end
|
|
297
|
+
end
|
|
298
|
+
|
|
299
|
+
# Present [/a/], absent /b/: greedy => /(?:a|b)/; lazy => /(?:b|a)/.
|
|
300
|
+
def replace_loop(node, present_branches, absent, reluctant)
|
|
301
|
+
branches = reluctant ? [absent, *present_branches] : [*present_branches, absent]
|
|
302
|
+
node.update(type: :plain, reference: nil, quantifier: nil, children: [alternate(branches)])
|
|
303
|
+
end
|
|
304
|
+
|
|
305
|
+
# \1 => /(?:[\s\S]*)/ for conservative length/character analysis only;
|
|
306
|
+
# this approximation is not emitted as the converted regexp.
|
|
307
|
+
def analysis_source(node)
|
|
308
|
+
return node if node.is_a?(String)
|
|
309
|
+
|
|
310
|
+
case node.type
|
|
311
|
+
when :backref, :capture_check, :capture_backref, :multiplex_backref
|
|
312
|
+
'(?:[\s\S]*)'
|
|
313
|
+
when :conditional, :capture_conditional
|
|
314
|
+
'(?:' + node.conditional_branches.map { |child| analysis_source(child) }.join('|') + ')'
|
|
315
|
+
else
|
|
316
|
+
if node.alternation?
|
|
317
|
+
branches = node.children.reject { |child| unmatchable?(child) }
|
|
318
|
+
'(?:' + branches.map { |child| analysis_source(child).sub(/\A\|/, '') }.join('|') + ')'
|
|
319
|
+
else
|
|
320
|
+
node.children.map { |child| analysis_source(child) }.join
|
|
321
|
+
end
|
|
322
|
+
end + node.quantifier.to_s
|
|
323
|
+
end
|
|
324
|
+
|
|
325
|
+
# /a?b/ => {'a', 'b'}; /^b/ => {'b'} since the anchor consumes nothing.
|
|
326
|
+
def first_characters(expression)
|
|
327
|
+
return CharacterSet.new if Node::Quantifier.from_expression(expression)&.zero?
|
|
328
|
+
|
|
329
|
+
case expression.type
|
|
330
|
+
when :literal
|
|
331
|
+
CharacterSet.of_string(expression.text[0].to_s)
|
|
332
|
+
when :assertion, :anchor
|
|
333
|
+
CharacterSet.new
|
|
334
|
+
when :group, :expression
|
|
335
|
+
result = nil
|
|
336
|
+
expression.expressions.each do |child|
|
|
337
|
+
characters = first_characters(child)
|
|
338
|
+
unless characters.empty?
|
|
339
|
+
result = result ? result | characters : characters
|
|
340
|
+
end
|
|
341
|
+
break unless child.match_length.min.zero?
|
|
342
|
+
end
|
|
343
|
+
result || CharacterSet.new
|
|
344
|
+
when :meta
|
|
345
|
+
if expression.token == :dot
|
|
346
|
+
CharacterSet.unicode
|
|
347
|
+
else
|
|
348
|
+
expression.expressions.map { |child| first_characters(child) }.reject(&:empty?).reduce(:|) || CharacterSet.new
|
|
349
|
+
end
|
|
350
|
+
else
|
|
351
|
+
CharacterSet.of_expression(expression)
|
|
352
|
+
end
|
|
353
|
+
end
|
|
354
|
+
|
|
355
|
+
# For capture 1, (a) => (?:a) with exclude: false; with exclude: true,
|
|
356
|
+
# (a) => (?!) and (a)? => an empty sequence.
|
|
357
|
+
def remove_capture(node, reference, exclude:)
|
|
358
|
+
return unless node.is_a?(Node)
|
|
359
|
+
|
|
360
|
+
if node.type == :captured_group && node.reference == reference
|
|
361
|
+
if exclude
|
|
362
|
+
node.update(type: :plain, reference: nil, children: [node.optional? ? '' : '(?!)'], quantifier: nil)
|
|
363
|
+
else
|
|
364
|
+
node.make_passive
|
|
365
|
+
end
|
|
366
|
+
else
|
|
367
|
+
node.children.each { |child| remove_capture(child, reference, exclude: exclude) }
|
|
368
|
+
end
|
|
369
|
+
end
|
|
370
|
+
end
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Resolve references to a Ruby capture represented by several JavaScript groups
|
|
4
|
+
# after loop expansion or subexpression calls. Backreferences use the latest
|
|
5
|
+
# participating copy; conditionals check whether any copy participated.
|
|
6
|
+
# Duplicate names try participating values newest first and commit to the first
|
|
7
|
+
# match, even if the rest of the pattern later fails.
|
|
8
|
+
# Optional nonempty captures can also be tested here, keeping their conditions
|
|
9
|
+
# in place so that greedy and lazy choices retain their original priority.
|
|
10
|
+
# Example: /(?<n>a)(?<n>b)\k<n>/ => /(a)(b)(?:\2|(?!\2)\1)/.
|
|
11
|
+
class JsRegex::SecondPass::ResolveCaptureReferences < JsRegex::SecondPass
|
|
12
|
+
def call
|
|
13
|
+
@group_paths = {}
|
|
14
|
+
@groups = {}
|
|
15
|
+
@optional_groups = potentially_absent_groups
|
|
16
|
+
tree.walk(trace: true) do |node, event, path|
|
|
17
|
+
if event == :exit
|
|
18
|
+
if node.type == :captured_group
|
|
19
|
+
@group_paths[node.reference] = path.dup
|
|
20
|
+
@groups[node.reference] = node
|
|
21
|
+
end
|
|
22
|
+
next
|
|
23
|
+
end
|
|
24
|
+
case node.type
|
|
25
|
+
when :conditional
|
|
26
|
+
if optional_consuming_capture?(node.reference, path)
|
|
27
|
+
make_condition_dynamic(node)
|
|
28
|
+
resolve_condition(node, path)
|
|
29
|
+
end
|
|
30
|
+
when :multiplex_backref then resolve_multiplexed_backreference(node, path)
|
|
31
|
+
when :capture_backref then resolve_backreference(node, path)
|
|
32
|
+
when :capture_conditional then resolve_condition(node, path)
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
private
|
|
38
|
+
|
|
39
|
+
# Three mandatory candidates [3, 2, 1] => /(?:\3|(?!\3)(?:\2|(?!\2)\1))/;
|
|
40
|
+
# a failed continuation cannot make a matching candidate give way to an older one.
|
|
41
|
+
def resolve_multiplexed_backreference(node, path)
|
|
42
|
+
references = node.children.select { |reference| @group_paths.key?(reference.reference) }
|
|
43
|
+
alternatives = references.map { |reference| guard_named_reference(reference, path) }
|
|
44
|
+
choice = alternatives.reverse_each.reduce do |fallback, preferred|
|
|
45
|
+
# Nest fallbacks so each candidate needs only one exclusion check.
|
|
46
|
+
alternate([preferred, Node.new('(?!', preferred.clone, ')', fallback)])
|
|
47
|
+
end
|
|
48
|
+
node.update(type: :plain, children: [choice || Node.new('(?!)')])
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
# For an optional nullable capture, \1 becomes a condition equivalent to
|
|
52
|
+
# (?(1)\1|(?!)); a later pass expands that condition.
|
|
53
|
+
def guard_named_reference(reference, path)
|
|
54
|
+
return reference if required_capture?(reference.reference, path)
|
|
55
|
+
|
|
56
|
+
if @groups.fetch(reference.reference).marked_nonempty?
|
|
57
|
+
Node.new(nonempty_capture_check(reference.reference, true), reference)
|
|
58
|
+
else
|
|
59
|
+
Node.new('(?:', Node.new('(?:', reference, ')'), Node.new('(?:(?!))'), ')',
|
|
60
|
+
reference: reference.reference, type: :conditional)
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
# Capture 1 in /(a)?(?(1)b|c)/ => true; in /(a*)?(?(1)b|c)/ => false,
|
|
65
|
+
# since an empty capture cannot be distinguished from absence by its value.
|
|
66
|
+
def optional_consuming_capture?(reference, path)
|
|
67
|
+
group = @groups[reference]
|
|
68
|
+
return false unless group&.marked_nonempty? && !required_capture?(reference, path)
|
|
69
|
+
|
|
70
|
+
# Captures inside lookarounds can belong to atomic-group emulation. Leave
|
|
71
|
+
# those under the existing conditional permutation rules.
|
|
72
|
+
@group_paths[reference].none? do |ancestor|
|
|
73
|
+
ancestor.positive_assertion? || ancestor.atomic?
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
# history[2,1] => \2 when capture 2 is required; an empty history => /(?!)/.
|
|
78
|
+
def resolve_backreference(node, path)
|
|
79
|
+
references = node.children.select { |reference| @group_paths.key?(reference.reference) }
|
|
80
|
+
if references.empty?
|
|
81
|
+
node.update(type: :plain, children: ['(?!)'])
|
|
82
|
+
elsif required_capture?(references.first.reference, path)
|
|
83
|
+
node.update(type: :plain, children: [references.first])
|
|
84
|
+
else
|
|
85
|
+
branches = []
|
|
86
|
+
absent_checks = []
|
|
87
|
+
references.each do |reference|
|
|
88
|
+
branches << Node.new(*absent_checks, nonempty_capture_check(reference.reference, true), reference)
|
|
89
|
+
absent_checks += [nonempty_capture_check(reference.reference, false)]
|
|
90
|
+
break unless @optional_groups.include?(reference.reference)
|
|
91
|
+
end
|
|
92
|
+
node.update(type: :plain, children: [alternate(branches)])
|
|
93
|
+
end
|
|
94
|
+
end
|
|
95
|
+
|
|
96
|
+
# Capture 1 at \1 in /(?:(a)\1)?/ => true; in /(a)?\1/ => false.
|
|
97
|
+
def required_capture?(reference, path)
|
|
98
|
+
!@optional_groups.include?(reference) || required_on_path?(@group_paths[reference], path)
|
|
99
|
+
end
|
|
100
|
+
|
|
101
|
+
# After a required capture 1, /(?(1)b|c)/ => /(?:(?:b))/.
|
|
102
|
+
def resolve_condition(node, path)
|
|
103
|
+
references = node.condition_references
|
|
104
|
+
if references.any? { |reference| required_on_path?(@group_paths[reference], path) }
|
|
105
|
+
select_conditional_branch(node, true)
|
|
106
|
+
else
|
|
107
|
+
true_branch, false_branch = node.conditional_branches
|
|
108
|
+
present = alternate(references.map { |reference| nonempty_capture_check(reference, true) })
|
|
109
|
+
absent = references.map { |reference| nonempty_capture_check(reference, false) }
|
|
110
|
+
node.update(type: :plain, children: [alternate([
|
|
111
|
+
Node.new(present, true_branch),
|
|
112
|
+
Node.new(*absent, false_branch || Node.new),
|
|
113
|
+
])])
|
|
114
|
+
end
|
|
115
|
+
end
|
|
116
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# Replace supported \K marks with lookbehind for the preceding part of the branch.
|
|
4
|
+
# That text must still match, but is excluded from the returned match.
|
|
5
|
+
# Example: /a\Kb/ => /(?<=a)b/.
|
|
6
|
+
class JsRegex::SecondPass::SubstituteRootLevelKeepMark < JsRegex::SecondPass
|
|
7
|
+
def call
|
|
8
|
+
substitute_in(tree)
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
# /a\Kb\Kc/ => /(?<=ab)c/: only the last mark sets the match start.
|
|
12
|
+
def substitute_in(node)
|
|
13
|
+
node.children.each do |child|
|
|
14
|
+
substitute_in(child) if child.is_a?(Node)
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
keep_mark_index = node.children.rindex { |child| child.is_a?(Node) && child.type == :keep_mark }
|
|
18
|
+
return unless keep_mark_index
|
|
19
|
+
|
|
20
|
+
pre = node.children[0...keep_mark_index].reject { |child| child.is_a?(Node) && child.type == :keep_mark }
|
|
21
|
+
separator = pre.first == '|' ? [pre.shift] : []
|
|
22
|
+
post = node.children[(keep_mark_index + 1)..-1]
|
|
23
|
+
lookbehind = Node.new('(?<=', *pre, ')')
|
|
24
|
+
node.update(children: [*separator, lookbehind, *post])
|
|
25
|
+
end
|
|
26
|
+
end
|