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
data/lib/js_regex/second_pass.rb
CHANGED
|
@@ -1,148 +1,405 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
class JsRegex
|
|
2
|
-
#
|
|
3
|
-
#
|
|
4
|
-
#
|
|
5
|
-
#
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
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
|
|
22
31
|
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
|
27
64
|
end
|
|
65
|
+
end
|
|
28
66
|
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
|
32
73
|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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)
|
|
37
78
|
end
|
|
79
|
+
end
|
|
80
|
+
|
|
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
|
|
38
85
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
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?
|
|
42
90
|
|
|
43
|
-
|
|
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)
|
|
97
|
+
end
|
|
98
|
+
end
|
|
44
99
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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, ')'])
|
|
52
112
|
end
|
|
113
|
+
node.quantify(0, 0)
|
|
114
|
+
end
|
|
53
115
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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))
|
|
58
153
|
end
|
|
154
|
+
groups
|
|
155
|
+
end
|
|
59
156
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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) }
|
|
66
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)
|
|
67
179
|
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
crawl(tree) { |node| count += 1 if node.type.equal?(:captured_group) }
|
|
71
|
-
count
|
|
180
|
+
children_with_group = node.children.map do |child|
|
|
181
|
+
require_group_participation(child, reference)
|
|
72
182
|
end
|
|
183
|
+
found = node.type == :captured_group && node.reference == reference
|
|
184
|
+
return false unless found || children_with_group.any?
|
|
73
185
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
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]
|
|
77
190
|
end
|
|
78
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
|
|
205
|
+
|
|
206
|
+
renumber_reference(node, candidates)
|
|
207
|
+
end
|
|
208
|
+
end
|
|
79
209
|
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
# if the group is referenced by any condition, modulate its quantity
|
|
99
|
-
if conds.include?(node.reference)
|
|
100
|
-
adapt_referenced_group_to_permutation(node, truthy)
|
|
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)
|
|
101
228
|
end
|
|
102
|
-
when :conditional
|
|
103
|
-
adapt_conditional_to_permutation(node, truthy)
|
|
104
229
|
end
|
|
105
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)
|
|
106
241
|
|
|
107
|
-
|
|
108
|
-
|
|
242
|
+
nodes << node
|
|
243
|
+
paths[node] = path.dup
|
|
109
244
|
end
|
|
245
|
+
[nodes, paths]
|
|
246
|
+
end
|
|
110
247
|
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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)
|
|
115
258
|
else
|
|
116
|
-
|
|
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])
|
|
117
262
|
end
|
|
118
|
-
|
|
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) })
|
|
119
270
|
end
|
|
271
|
+
end
|
|
120
272
|
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
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) })
|
|
124
282
|
end
|
|
283
|
+
end
|
|
125
284
|
|
|
126
|
-
|
|
127
|
-
|
|
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
|
|
128
289
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
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
|
|
135
320
|
end
|
|
136
|
-
|
|
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
|
|
137
330
|
|
|
138
|
-
|
|
139
|
-
|
|
331
|
+
alternatives.map do |alternative|
|
|
332
|
+
node.with(children: node.children.map { |el| el.equal?(children.first) ? alternative : el.clone })
|
|
333
|
+
end
|
|
140
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
|
|
141
366
|
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
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
|
|
145
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
|
|
146
403
|
end
|
|
147
404
|
end
|
|
148
405
|
end
|
data/lib/js_regex/target.rb
CHANGED
|
@@ -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
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
class JsRegex
|
|
4
|
+
module Utils
|
|
5
|
+
module Escapes
|
|
6
|
+
ESCAPES_SHARED_BY_RUBY_AND_JS = %i[
|
|
7
|
+
alternation
|
|
8
|
+
backslash
|
|
9
|
+
backspace
|
|
10
|
+
bol
|
|
11
|
+
carriage
|
|
12
|
+
codepoint
|
|
13
|
+
dot
|
|
14
|
+
eol
|
|
15
|
+
form_feed
|
|
16
|
+
group_close
|
|
17
|
+
group_open
|
|
18
|
+
hex
|
|
19
|
+
interval_close
|
|
20
|
+
interval_open
|
|
21
|
+
newline
|
|
22
|
+
one_or_more
|
|
23
|
+
set_close
|
|
24
|
+
set_open
|
|
25
|
+
tab
|
|
26
|
+
vertical_tab
|
|
27
|
+
zero_or_more
|
|
28
|
+
zero_or_one
|
|
29
|
+
].freeze
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
end
|