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.
- checksums.yaml +4 -4
- data/lib/js_regex/conversion.rb +0 -1
- data/lib/js_regex/converter/anchor_converter.rb +2 -2
- data/lib/js_regex/converter/backreference_converter.rb +16 -51
- data/lib/js_regex/converter/base.rb +27 -25
- data/lib/js_regex/converter/conditional_converter.rb +10 -4
- data/lib/js_regex/converter/context.rb +27 -7
- data/lib/js_regex/converter/escape_converter.rb +3 -29
- data/lib/js_regex/converter/group_converter.rb +12 -6
- data/lib/js_regex/converter/literal_converter.rb +13 -45
- data/lib/js_regex/converter/meta_converter.rb +13 -13
- data/lib/js_regex/converter/property_converter.rb +2 -20
- data/lib/js_regex/converter/set_converter.rb +7 -36
- data/lib/js_regex/converter/type_converter.rb +43 -66
- 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 +46 -44
- 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 +363 -141
- 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 +1 -1
- data/lib/js_regex.rb +3 -3
- metadata +26 -4
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a25e95d1a39714ba3f0d0f87dc6f02029e75bc1df2b218a4e5d0111cbf6524a4
|
|
4
|
+
data.tar.gz: 11f74f2b8a0f95c6933ce10cb546ddb31dc9cd7626963d548de5dc8f13f51f6d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 920289779a37bc810ec3030b4b9dafb8ea95803813e5fcc8d037bc6d74285c13d4170ead8993869c47a9af75fcfca723270a29f62bdfa5aefa48d1ab14dcfc0b
|
|
7
|
+
data.tar.gz: e73d7ab9a56c768b7d3827e2f22e17a185a47322365b74da6985eeacfaf59ce18f4f233f48f0dea17d7851e8fb7070721fab016b0f6e51f883abf0be95ab490d
|
data/lib/js_regex/conversion.rb
CHANGED
|
@@ -51,9 +51,9 @@ class JsRegex
|
|
|
51
51
|
|
|
52
52
|
# This is an approximation to the word boundary behavior in Ruby, c.f.
|
|
53
53
|
# https://github.com/ruby/ruby/blob/08476c45/tool/enc-unicode.rb#L130
|
|
54
|
-
W = '\
|
|
54
|
+
W = '\p{N}\p{L}\p{M}\p{Pc}'
|
|
55
55
|
BOUNDARY_EXPANSION = "(?:(?<=[#{W}])(?=[^#{W}]|$)|(?<=[^#{W}]|^)(?=[#{W}]))"
|
|
56
|
-
NONBOUNDARY_EXPANSION = "(?<=[#{W}])(?=[#{W}])"
|
|
56
|
+
NONBOUNDARY_EXPANSION = "(?:(?<=[#{W}])(?=[#{W}])|(?<=[^#{W}]|^)(?=[^#{W}]|$))"
|
|
57
57
|
|
|
58
58
|
def pass_boundary_with_warning
|
|
59
59
|
warn_of("The anchor '#{data}' at index #{expression.ts} only works "\
|
|
@@ -32,28 +32,26 @@ class JsRegex
|
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
def convert_to_plain_num_ref
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
35
|
+
positions = context.get_recursive_group_positions(target_position)
|
|
36
|
+
positions = [new_position] if positions.empty?
|
|
37
|
+
references = positions.map do |position|
|
|
38
|
+
Node.new("\\#{position}", reference: position, type: :backref)
|
|
39
|
+
end
|
|
40
|
+
if references.length > 1 && Utils::Captures.nonempty?(expression.referenced_expression)
|
|
41
|
+
Node.new(*references, type: :capture_backref)
|
|
42
|
+
else
|
|
43
|
+
references.first
|
|
42
44
|
end
|
|
43
|
-
|
|
44
|
-
text = "\\#{position}#{'(?:)' if expression.x?}"
|
|
45
|
-
Node.new(text, reference: position, type: :backref)
|
|
46
45
|
end
|
|
47
46
|
|
|
48
47
|
def convert_multiplexed_name_ref
|
|
49
48
|
# Create alternation of all groups with the same name
|
|
50
|
-
|
|
51
|
-
|
|
49
|
+
# Ruby tries groups with the same name from last to first.
|
|
50
|
+
references = expression.referenced_expressions.reverse.map do |ref_exp|
|
|
51
|
+
position = context.new_capturing_group_position(ref_exp.number)
|
|
52
|
+
Node.new("\\#{position}", reference: position, type: :backref)
|
|
52
53
|
end
|
|
53
|
-
|
|
54
|
-
# Build alternation like (?:\1|\2)
|
|
55
|
-
alternation = positions.map { |pos| "\\#{pos}" }.join('|')
|
|
56
|
-
Node.new("(?:#{alternation})")
|
|
54
|
+
Node.new(*references, type: :multiplex_backref)
|
|
57
55
|
end
|
|
58
56
|
|
|
59
57
|
def new_position
|
|
@@ -72,48 +70,15 @@ class JsRegex
|
|
|
72
70
|
|
|
73
71
|
context.count_recursion(expression)
|
|
74
72
|
|
|
75
|
-
# Track groups before the wrapper group is added
|
|
76
|
-
groups_before_wrapper = context.capturing_group_count
|
|
77
|
-
|
|
78
|
-
context.increment_local_capturing_group_count
|
|
79
|
-
target_copy = expression.referenced_expression.unquantified_clone
|
|
80
|
-
# avoid "Duplicate capture group name" error in JS
|
|
81
|
-
target_copy.token = :capture if target_copy.is?(:named, :group)
|
|
82
73
|
context.start_subexp_recursion
|
|
83
|
-
|
|
74
|
+
ref = expression.referenced_expression
|
|
75
|
+
result = Converter.for(ref).convert(ref, context, quantified: false)
|
|
84
76
|
context.end_subexp_recursion
|
|
85
77
|
|
|
86
|
-
# Track all groups created during this recursive call
|
|
87
|
-
# This handles both the directly called group and any nested groups within it
|
|
88
|
-
# Get all group numbers from the referenced expression
|
|
89
|
-
original_groups = collect_group_numbers(expression.referenced_expression)
|
|
90
|
-
|
|
91
|
-
# The first new group number is groups_before_wrapper + 1
|
|
92
|
-
# (the wrapper group from increment_local_capturing_group_count doesn't appear in output)
|
|
93
|
-
first_new_group = groups_before_wrapper + 1
|
|
94
|
-
|
|
95
|
-
# Map each original group to its corresponding new group
|
|
96
|
-
# For example, if we recursively called group 1 which contains group 2,
|
|
97
|
-
# and this created groups 3 and 4, then:
|
|
98
|
-
# - group 1 -> group 3
|
|
99
|
-
# - group 2 -> group 4
|
|
100
|
-
original_groups.each_with_index do |old_group_num, index|
|
|
101
|
-
new_group_num = first_new_group + index
|
|
102
|
-
context.track_recursive_group_call(old_group_num, new_group_num)
|
|
103
|
-
end
|
|
104
|
-
|
|
105
78
|
# wrap in passive group if it is a full-pattern recursion
|
|
106
79
|
expression.reference == 0 ? Node.new('(?:', result, ')') : result
|
|
107
80
|
end
|
|
108
81
|
|
|
109
|
-
def collect_group_numbers(exp)
|
|
110
|
-
return [] if exp.terminal?
|
|
111
|
-
|
|
112
|
-
numbers = []
|
|
113
|
-
numbers << exp.number if exp.capturing?
|
|
114
|
-
exp.each_expression { |sub| numbers += collect_group_numbers(sub) }
|
|
115
|
-
numbers
|
|
116
|
-
end
|
|
117
82
|
end
|
|
118
83
|
end
|
|
119
84
|
end
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require_relative '../node'
|
|
4
|
+
require_relative '../utils'
|
|
5
|
+
|
|
3
6
|
class JsRegex
|
|
4
7
|
module Converter
|
|
5
8
|
#
|
|
@@ -8,13 +11,15 @@ class JsRegex
|
|
|
8
11
|
#
|
|
9
12
|
class Base
|
|
10
13
|
# returns instance of Node with #quantifier attached.
|
|
11
|
-
def convert(expression, context)
|
|
14
|
+
def convert(expression, context, quantified: true)
|
|
12
15
|
self.context = context
|
|
13
16
|
self.expression = expression
|
|
14
17
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
+
if quantified && expression.quantifier&.possessive?
|
|
19
|
+
wrap_in_backrefed_lookahead { convert_node(quantified) }
|
|
20
|
+
else
|
|
21
|
+
convert_node(quantified)
|
|
22
|
+
end
|
|
18
23
|
end
|
|
19
24
|
|
|
20
25
|
private
|
|
@@ -30,19 +35,17 @@ class JsRegex
|
|
|
30
35
|
end
|
|
31
36
|
alias pass_through data
|
|
32
37
|
|
|
33
|
-
def
|
|
34
|
-
|
|
38
|
+
def convert_node(quantified)
|
|
39
|
+
node = convert_data
|
|
40
|
+
node = Node.new(node) if node.instance_of?(String)
|
|
41
|
+
quantified ? apply_quantifier(node) : node
|
|
42
|
+
end
|
|
35
43
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
elsif qtf.token == :interval && qtf.text[0..1] == "{,"
|
|
40
|
-
node.update(quantifier: qtf.dup.tap { |q| q.text = "{0,#{q.max}}" })
|
|
41
|
-
else
|
|
42
|
-
node.update(quantifier: qtf)
|
|
43
|
-
end
|
|
44
|
+
def apply_quantifier(node)
|
|
45
|
+
return node if node.dropped? ||
|
|
46
|
+
(qtf = Node::Quantifier.from_expression(expression)).nil?
|
|
44
47
|
|
|
45
|
-
node
|
|
48
|
+
node.update(quantifier: qtf)
|
|
46
49
|
end
|
|
47
50
|
|
|
48
51
|
def convert_subexpressions
|
|
@@ -67,11 +70,7 @@ class JsRegex
|
|
|
67
70
|
end
|
|
68
71
|
|
|
69
72
|
def warn_of(text)
|
|
70
|
-
|
|
71
|
-
raise ConversionError, text.sub(/^Dropped /, '')
|
|
72
|
-
else
|
|
73
|
-
context.warnings << text
|
|
74
|
-
end
|
|
73
|
+
context.warn_of(text)
|
|
75
74
|
end
|
|
76
75
|
|
|
77
76
|
def drop
|
|
@@ -79,13 +78,16 @@ class JsRegex
|
|
|
79
78
|
end
|
|
80
79
|
alias drop_without_warning drop
|
|
81
80
|
|
|
82
|
-
def wrap_in_backrefed_lookahead
|
|
83
|
-
number = context.
|
|
81
|
+
def wrap_in_backrefed_lookahead
|
|
82
|
+
number = context.capture_group(added: true)
|
|
83
|
+
content = yield
|
|
84
|
+
if content.is_a?(Node) && content.dropped?
|
|
85
|
+
context.cancel_added_capturing_group
|
|
86
|
+
return content
|
|
87
|
+
end
|
|
84
88
|
backref_node = Node.new("\\#{number}", reference: number, type: :backref)
|
|
85
89
|
backrefed_group = Node.new('(', *content, ')', reference: number, type: :captured_group)
|
|
86
|
-
|
|
87
|
-
# The surrounding group is added so that quantifiers apply to the whole.
|
|
88
|
-
# Without it, `(?:)` would need to be appended as literal digits may follow.
|
|
90
|
+
# The surrounding group makes quantifiers apply to the whole expansion.
|
|
89
91
|
Node.new('(?:(?=', backrefed_group, ')', backref_node, ')')
|
|
90
92
|
end
|
|
91
93
|
|
|
@@ -18,11 +18,17 @@ class JsRegex
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def mark_conditional_for_second_pass
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
expression.branches.
|
|
24
|
-
|
|
21
|
+
target = expression.referenced_expression
|
|
22
|
+
positions = context.get_recursive_group_positions(target.number)
|
|
23
|
+
branches = expression.branches.map { |branch| Node.new('(?:', convert_expression(branch), ')') }
|
|
24
|
+
if positions.length > 1 && Utils::Captures.nonempty?(target)
|
|
25
|
+
references = positions.map { |position| Node.new(reference: position) }
|
|
26
|
+
return Node.new(Node.new(*references), *branches, type: :capture_conditional)
|
|
25
27
|
end
|
|
28
|
+
|
|
29
|
+
reference = positions.first || context.new_capturing_group_position(target.number)
|
|
30
|
+
node = Node.new('(?:', reference: reference, type: :conditional)
|
|
31
|
+
branches.each { |branch| node << branch }
|
|
26
32
|
node << ')'
|
|
27
33
|
end
|
|
28
34
|
end
|
|
@@ -24,11 +24,20 @@ class JsRegex
|
|
|
24
24
|
self.required_options_hash = {}
|
|
25
25
|
self.warnings = []
|
|
26
26
|
self.recursive_group_map = {}
|
|
27
|
+
self.recursive_group_history = Hash.new { |hash, key| hash[key] = [] }
|
|
27
28
|
|
|
28
29
|
self.case_insensitive_root = case_insensitive_root
|
|
29
30
|
self.target = target
|
|
30
31
|
end
|
|
31
32
|
|
|
33
|
+
def warn_of(text)
|
|
34
|
+
if fail_fast
|
|
35
|
+
raise ConversionError, text.sub(/^Dropped /, '')
|
|
36
|
+
else
|
|
37
|
+
warnings << text
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
32
41
|
# target context
|
|
33
42
|
|
|
34
43
|
def es_2015_or_higher?
|
|
@@ -57,7 +66,10 @@ class JsRegex
|
|
|
57
66
|
|
|
58
67
|
# group context
|
|
59
68
|
|
|
60
|
-
def capture_group
|
|
69
|
+
def capture_group(added: false)
|
|
70
|
+
if added
|
|
71
|
+
added_capturing_groups_after_group[original_capturing_group_count] += 1
|
|
72
|
+
end
|
|
61
73
|
self.capturing_group_count = capturing_group_count + 1
|
|
62
74
|
end
|
|
63
75
|
|
|
@@ -69,9 +81,9 @@ class JsRegex
|
|
|
69
81
|
self.in_atomic_group = false
|
|
70
82
|
end
|
|
71
83
|
|
|
72
|
-
def
|
|
73
|
-
added_capturing_groups_after_group[original_capturing_group_count]
|
|
74
|
-
|
|
84
|
+
def cancel_added_capturing_group
|
|
85
|
+
added_capturing_groups_after_group[original_capturing_group_count] -= 1
|
|
86
|
+
self.capturing_group_count = capturing_group_count - 1
|
|
75
87
|
end
|
|
76
88
|
|
|
77
89
|
def recursions(exp)
|
|
@@ -93,14 +105,14 @@ class JsRegex
|
|
|
93
105
|
end
|
|
94
106
|
|
|
95
107
|
def end_subexp_recursion
|
|
96
|
-
self.in_subexp_recursion = false
|
|
97
108
|
# Pop the last recursion from stack when exiting
|
|
98
109
|
recursion_stack.pop if recursion_stack.any?
|
|
110
|
+
self.in_subexp_recursion = recursion_stack.any?
|
|
99
111
|
end
|
|
100
112
|
|
|
101
113
|
# Get the number of groups at the start of the current recursion
|
|
102
114
|
def recursion_start_group_count
|
|
103
|
-
|
|
115
|
+
@recursion_start_group_count || 0
|
|
104
116
|
end
|
|
105
117
|
|
|
106
118
|
# takes and returns 1-indexed group positions.
|
|
@@ -120,6 +132,9 @@ class JsRegex
|
|
|
120
132
|
# Track that a group was created by a recursive call
|
|
121
133
|
def track_recursive_group_call(original_group_num, new_group_num)
|
|
122
134
|
recursive_group_map[original_group_num] = new_group_num
|
|
135
|
+
history = recursive_group_history[original_group_num]
|
|
136
|
+
history.delete(new_group_num)
|
|
137
|
+
history.unshift(new_group_num)
|
|
123
138
|
end
|
|
124
139
|
|
|
125
140
|
# Get the group number created by a recursive call
|
|
@@ -127,6 +142,10 @@ class JsRegex
|
|
|
127
142
|
recursive_group_map[original_group_num]
|
|
128
143
|
end
|
|
129
144
|
|
|
145
|
+
def get_recursive_group_positions(original_group_num)
|
|
146
|
+
recursive_group_history[original_group_num].dup
|
|
147
|
+
end
|
|
148
|
+
|
|
130
149
|
private
|
|
131
150
|
|
|
132
151
|
attr_accessor :added_capturing_groups_after_group,
|
|
@@ -134,6 +153,7 @@ class JsRegex
|
|
|
134
153
|
:recursion_stack,
|
|
135
154
|
:required_options_hash,
|
|
136
155
|
:recursive_group_map,
|
|
156
|
+
:recursive_group_history,
|
|
137
157
|
:target
|
|
138
158
|
|
|
139
159
|
attr_writer :capturing_group_count,
|
|
@@ -145,7 +165,7 @@ class JsRegex
|
|
|
145
165
|
:warnings
|
|
146
166
|
|
|
147
167
|
def total_added_capturing_groups
|
|
148
|
-
added_capturing_groups_after_group.values.
|
|
168
|
+
added_capturing_groups_after_group.values.sum
|
|
149
169
|
end
|
|
150
170
|
end
|
|
151
171
|
end
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base'
|
|
4
|
-
require_relative 'literal_converter'
|
|
5
4
|
|
|
6
5
|
class JsRegex
|
|
7
6
|
module Converter
|
|
@@ -9,31 +8,6 @@ class JsRegex
|
|
|
9
8
|
# Template class implementation.
|
|
10
9
|
#
|
|
11
10
|
class EscapeConverter < JsRegex::Converter::Base
|
|
12
|
-
ESCAPES_SHARED_BY_RUBY_AND_JS = %i[
|
|
13
|
-
alternation
|
|
14
|
-
backslash
|
|
15
|
-
backspace
|
|
16
|
-
bol
|
|
17
|
-
carriage
|
|
18
|
-
codepoint
|
|
19
|
-
dot
|
|
20
|
-
eol
|
|
21
|
-
form_feed
|
|
22
|
-
group_close
|
|
23
|
-
group_open
|
|
24
|
-
hex
|
|
25
|
-
interval_close
|
|
26
|
-
interval_open
|
|
27
|
-
newline
|
|
28
|
-
one_or_more
|
|
29
|
-
set_close
|
|
30
|
-
set_open
|
|
31
|
-
tab
|
|
32
|
-
vertical_tab
|
|
33
|
-
zero_or_more
|
|
34
|
-
zero_or_one
|
|
35
|
-
].freeze
|
|
36
|
-
|
|
37
11
|
private
|
|
38
12
|
|
|
39
13
|
def convert_data
|
|
@@ -43,10 +17,10 @@ class JsRegex
|
|
|
43
17
|
when :control, :meta_sequence, :utf8_hex
|
|
44
18
|
unicode_escape_codepoint
|
|
45
19
|
when :literal
|
|
46
|
-
|
|
20
|
+
Utils::Literals.convert_data(expression.char, context)
|
|
47
21
|
when :bell, :escape, :hex, :octal
|
|
48
22
|
hex_escape_codepoint
|
|
49
|
-
when *ESCAPES_SHARED_BY_RUBY_AND_JS
|
|
23
|
+
when *Utils::Escapes::ESCAPES_SHARED_BY_RUBY_AND_JS
|
|
50
24
|
pass_through
|
|
51
25
|
else
|
|
52
26
|
warn_of_unsupported_feature
|
|
@@ -58,7 +32,7 @@ class JsRegex
|
|
|
58
32
|
split_codepoint_list
|
|
59
33
|
else
|
|
60
34
|
expression.chars.each_with_object(Node.new) do |char, node|
|
|
61
|
-
node <<
|
|
35
|
+
node << Utils::Literals.convert_data(Regexp.escape(char), context)
|
|
62
36
|
end
|
|
63
37
|
end
|
|
64
38
|
end
|
|
@@ -36,7 +36,7 @@ class JsRegex
|
|
|
36
36
|
build_passive_group
|
|
37
37
|
else
|
|
38
38
|
context.start_atomic_group
|
|
39
|
-
result = wrap_in_backrefed_lookahead
|
|
39
|
+
result = wrap_in_backrefed_lookahead { convert_subexpressions }
|
|
40
40
|
context.end_atomic_group
|
|
41
41
|
result
|
|
42
42
|
end
|
|
@@ -70,8 +70,10 @@ class JsRegex
|
|
|
70
70
|
end
|
|
71
71
|
|
|
72
72
|
def build_absence_group
|
|
73
|
-
head =
|
|
74
|
-
|
|
73
|
+
head = '(?:(?:(?!'
|
|
74
|
+
maximum = expression.inner_match_length.min - 1
|
|
75
|
+
suffix = maximum > 0 ? "(?:.|\\n){0,#{maximum}}" : ''
|
|
76
|
+
tail = ")(?:.|\\n))*#{suffix})"
|
|
75
77
|
build_group(head: head, tail: tail, capturing: false)
|
|
76
78
|
end
|
|
77
79
|
|
|
@@ -80,9 +82,13 @@ class JsRegex
|
|
|
80
82
|
tail = opts[:tail] || ')'
|
|
81
83
|
return Node.new(*wrap(head, tail)) if opts[:capturing].equal?(false)
|
|
82
84
|
|
|
83
|
-
context.capture_group
|
|
84
|
-
|
|
85
|
-
Node.new(*wrap(head, tail), reference: ref, type: :captured_group
|
|
85
|
+
ref = context.capture_group(added: context.in_subexp_recursion)
|
|
86
|
+
context.track_recursive_group_call(expression.number, ref)
|
|
87
|
+
node = Node.new(*wrap(head, tail), reference: ref, type: :captured_group,
|
|
88
|
+
marked_nonempty: Utils::Captures.nonempty?(expression),
|
|
89
|
+
nullable: Utils::Captures.nullable?(expression))
|
|
90
|
+
context.track_recursive_group_call(expression.number, ref)
|
|
91
|
+
node
|
|
86
92
|
end
|
|
87
93
|
|
|
88
94
|
def wrap(head, tail)
|
|
@@ -8,55 +8,14 @@ class JsRegex
|
|
|
8
8
|
# Template class implementation.
|
|
9
9
|
#
|
|
10
10
|
class LiteralConverter < JsRegex::Converter::Base
|
|
11
|
-
ASTRAL_PLANE_CODEPOINT_PATTERN = /[\u{10000}-\u{10FFFF}]/.freeze
|
|
12
|
-
LITERAL_REQUIRING_ESCAPE_PATTERN = /[\/\f\n\r\t\v]/.freeze
|
|
13
|
-
|
|
14
|
-
class << self
|
|
15
|
-
def convert_data(data, context)
|
|
16
|
-
if !context.u? && data =~ ASTRAL_PLANE_CODEPOINT_PATTERN
|
|
17
|
-
if context.enable_u_option
|
|
18
|
-
escape_incompatible_bmp_literals(data)
|
|
19
|
-
else
|
|
20
|
-
convert_astral_data(data)
|
|
21
|
-
end
|
|
22
|
-
else
|
|
23
|
-
escape_incompatible_bmp_literals(data)
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
|
-
def convert_astral_data(data)
|
|
28
|
-
data.each_char.each_with_object(Node.new) do |char, node|
|
|
29
|
-
if char.ord > 0xFFFF
|
|
30
|
-
node << surrogate_substitution_for(char)
|
|
31
|
-
else
|
|
32
|
-
node << escape_incompatible_bmp_literals(char)
|
|
33
|
-
end
|
|
34
|
-
end
|
|
35
|
-
end
|
|
36
|
-
|
|
37
|
-
ESCAPES = Hash.new { |h, k| raise KeyError, "#{h}[#{k.inspect}]" }
|
|
38
|
-
.merge("\f\n\r\t\v".chars.to_h { |c| [c, Regexp.escape(c)] })
|
|
39
|
-
.merge('/' => '\\/')
|
|
40
|
-
|
|
41
|
-
def escape_incompatible_bmp_literals(data)
|
|
42
|
-
data.gsub(LITERAL_REQUIRING_ESCAPE_PATTERN, ESCAPES)
|
|
43
|
-
end
|
|
44
|
-
|
|
45
|
-
private
|
|
46
|
-
|
|
47
|
-
def surrogate_substitution_for(char)
|
|
48
|
-
CharacterSet::Writer.write_surrogate_ranges([], [char.codepoints])
|
|
49
|
-
end
|
|
50
|
-
end
|
|
51
|
-
|
|
52
11
|
private
|
|
53
12
|
|
|
54
13
|
def convert_data
|
|
55
|
-
result =
|
|
14
|
+
result = Utils::Literals.convert_data(data, context)
|
|
56
15
|
if context.case_insensitive_root && !expression.case_insensitive?
|
|
57
16
|
warn_of_unsupported_feature('nested case-sensitive literal')
|
|
58
17
|
elsif !context.case_insensitive_root && expression.case_insensitive?
|
|
59
|
-
return handle_locally_case_insensitive_literal(
|
|
18
|
+
return handle_locally_case_insensitive_literal(data)
|
|
60
19
|
end
|
|
61
20
|
result
|
|
62
21
|
end
|
|
@@ -64,12 +23,21 @@ class JsRegex
|
|
|
64
23
|
HAS_CASE_PATTERN = /[\p{lower}\p{upper}]/.freeze
|
|
65
24
|
|
|
66
25
|
def handle_locally_case_insensitive_literal(literal)
|
|
67
|
-
literal
|
|
26
|
+
literal.match?(HAS_CASE_PATTERN) ? case_insensitivize(literal) : Utils::Literals.convert_data(literal, context)
|
|
68
27
|
end
|
|
69
28
|
|
|
70
29
|
def case_insensitivize(literal)
|
|
71
30
|
literal.each_char.each_with_object(Node.new) do |chr, node|
|
|
72
|
-
|
|
31
|
+
if chr.match?(HAS_CASE_PATTERN)
|
|
32
|
+
if chr.ord > 0xFFFF && !context.enable_u_option
|
|
33
|
+
node << Node.new('(?:', Utils::Literals.convert_data(chr, context), '|',
|
|
34
|
+
Utils::Literals.convert_data(chr.swapcase, context), ')')
|
|
35
|
+
else
|
|
36
|
+
node << "[#{chr}#{chr.swapcase}]"
|
|
37
|
+
end
|
|
38
|
+
else
|
|
39
|
+
node << Utils::Literals.convert_data(chr, context)
|
|
40
|
+
end
|
|
73
41
|
end
|
|
74
42
|
end
|
|
75
43
|
end
|
|
@@ -8,19 +8,6 @@ class JsRegex
|
|
|
8
8
|
# Template class implementation.
|
|
9
9
|
#
|
|
10
10
|
class MetaConverter < JsRegex::Converter::Base
|
|
11
|
-
DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\n\uD800-\uDFFF])'
|
|
12
|
-
ML_DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF])'
|
|
13
|
-
# Possible improvements for dot conversion:
|
|
14
|
-
#
|
|
15
|
-
# In ES2015, the 'u' flag allows dots to match astral chars. Unfortunately
|
|
16
|
-
# the dot keeps matching lone surrogates even with this flag, so the use
|
|
17
|
-
# of an expansion is still necessary to get the same behavior as in Ruby.
|
|
18
|
-
#
|
|
19
|
-
# ES2018 has the dotall flag 's', but it is tricky to use in conversions.
|
|
20
|
-
# 's' activates matching of BOTH astral chars and "\n", whereas the dot in
|
|
21
|
-
# Ruby doesn't match "\n" by default, and even with the 'm' flag set on
|
|
22
|
-
# the root, subexps might still exclude "\n" like so: /.(?-m:.)./m
|
|
23
|
-
|
|
24
11
|
private
|
|
25
12
|
|
|
26
13
|
def convert_data
|
|
@@ -34,6 +21,19 @@ class JsRegex
|
|
|
34
21
|
end
|
|
35
22
|
end
|
|
36
23
|
|
|
24
|
+
DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\n\uD800-\uDFFF])'
|
|
25
|
+
ML_DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF])'
|
|
26
|
+
# Possible improvements for dot conversion:
|
|
27
|
+
#
|
|
28
|
+
# In ES2015, the 'u' flag allows dots to match astral chars. Unfortunately
|
|
29
|
+
# the dot keeps matching lone surrogates even with this flag, so the use
|
|
30
|
+
# of an expansion is still necessary to get the same behavior as in Ruby.
|
|
31
|
+
#
|
|
32
|
+
# ES2018 has the dotall flag 's', but it is tricky to use in conversions.
|
|
33
|
+
# 's' activates matching of BOTH astral chars and "\n", whereas the dot in
|
|
34
|
+
# Ruby doesn't match "\n" by default, and even with the 'm' flag set on
|
|
35
|
+
# the root, subexps might still exclude "\n" like so: /.(?-m:.)./m
|
|
36
|
+
|
|
37
37
|
def convert_alternatives
|
|
38
38
|
kept_any_previous_branch = nil
|
|
39
39
|
|
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base'
|
|
4
|
-
require 'character_set'
|
|
5
4
|
|
|
6
5
|
class JsRegex
|
|
7
6
|
module Converter
|
|
@@ -12,34 +11,17 @@ class JsRegex
|
|
|
12
11
|
# codepoints matched by the property and build a set string from them.
|
|
13
12
|
#
|
|
14
13
|
class PropertyConverter < JsRegex::Converter::Base
|
|
15
|
-
# A map of normalized Ruby property names to names supported by ES2018+.
|
|
16
|
-
def self.map
|
|
17
|
-
@map ||= File.read("#{__dir__}/property_map.csv").scan(/(.+),(.+)/).to_h
|
|
18
|
-
end
|
|
19
|
-
|
|
20
14
|
private
|
|
21
15
|
|
|
22
16
|
def convert_data
|
|
23
17
|
if context.es_2018_or_higher? &&
|
|
24
|
-
(prop_name_in_js =
|
|
18
|
+
(prop_name_in_js = Utils::Properties.name_in_js(subtype))
|
|
25
19
|
context.enable_u_option
|
|
26
20
|
"\\#{expression.negative? ? 'P' : 'p'}{#{prop_name_in_js}}"
|
|
27
21
|
else
|
|
28
|
-
|
|
22
|
+
Utils::CharSets.convert(expression, context)
|
|
29
23
|
end
|
|
30
24
|
end
|
|
31
|
-
|
|
32
|
-
def build_character_set
|
|
33
|
-
content = CharacterSet.of_expression(expression)
|
|
34
|
-
|
|
35
|
-
if expression.case_insensitive? && !context.case_insensitive_root
|
|
36
|
-
content = content.case_insensitive
|
|
37
|
-
elsif !expression.case_insensitive? && context.case_insensitive_root
|
|
38
|
-
warn_of_unsupported_feature('nested case-sensitive property')
|
|
39
|
-
end
|
|
40
|
-
|
|
41
|
-
content.to_s_with_surrogate_ranges
|
|
42
|
-
end
|
|
43
25
|
end
|
|
44
26
|
end
|
|
45
27
|
end
|