js_regex 3.13.0 → 3.15.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (50) hide show
  1. checksums.yaml +4 -4
  2. data/lib/js_regex/conversion.rb +2 -1
  3. data/lib/js_regex/converter/anchor_converter.rb +4 -2
  4. data/lib/js_regex/converter/assertion_converter.rb +2 -0
  5. data/lib/js_regex/converter/backreference_converter.rb +33 -12
  6. data/lib/js_regex/converter/base.rb +38 -27
  7. data/lib/js_regex/converter/conditional_converter.rb +12 -4
  8. data/lib/js_regex/converter/context.rb +54 -8
  9. data/lib/js_regex/converter/escape_converter.rb +6 -30
  10. data/lib/js_regex/converter/freespace_converter.rb +2 -0
  11. data/lib/js_regex/converter/group_converter.rb +18 -16
  12. data/lib/js_regex/converter/keep_converter.rb +2 -0
  13. data/lib/js_regex/converter/literal_converter.rb +16 -46
  14. data/lib/js_regex/converter/meta_converter.rb +15 -13
  15. data/lib/js_regex/converter/property_converter.rb +4 -20
  16. data/lib/js_regex/converter/set_converter.rb +10 -37
  17. data/lib/js_regex/converter/subexpression_converter.rb +2 -0
  18. data/lib/js_regex/converter/type_converter.rb +45 -39
  19. data/lib/js_regex/converter/unsupported_token_converter.rb +2 -0
  20. data/lib/js_regex/converter.rb +2 -0
  21. data/lib/js_regex/error.rb +2 -0
  22. data/lib/js_regex/node/checks.rb +70 -0
  23. data/lib/js_regex/node/conditional_parts.rb +17 -0
  24. data/lib/js_regex/node/quantifier.rb +134 -0
  25. data/lib/js_regex/node/stringification.rb +14 -0
  26. data/lib/js_regex/node/transformations.rb +56 -0
  27. data/lib/js_regex/node/traversal.rb +24 -0
  28. data/lib/js_regex/node.rb +49 -40
  29. data/lib/js_regex/second_pass/alternate_conditional_permutations.rb +89 -0
  30. data/lib/js_regex/second_pass/delimit_backrefs.rb +31 -0
  31. data/lib/js_regex/second_pass/expand_bounded_capture_loops.rb +79 -0
  32. data/lib/js_regex/second_pass/expand_constant_capture_loops.rb +287 -0
  33. data/lib/js_regex/second_pass/expand_nullable_captures.rb +327 -0
  34. data/lib/js_regex/second_pass/handle_non_participating_backrefs.rb +69 -0
  35. data/lib/js_regex/second_pass/preserve_repeated_captures.rb +370 -0
  36. data/lib/js_regex/second_pass/resolve_capture_references.rb +116 -0
  37. data/lib/js_regex/second_pass/substitute_root_level_keep_mark.rb +26 -0
  38. data/lib/js_regex/second_pass.rb +364 -107
  39. data/lib/js_regex/target.rb +2 -0
  40. data/lib/js_regex/utils/captures.rb +46 -0
  41. data/lib/js_regex/utils/char_sets.rb +34 -0
  42. data/lib/js_regex/utils/char_types.rb +18 -0
  43. data/lib/js_regex/utils/escapes.rb +32 -0
  44. data/lib/js_regex/utils/literals.rb +53 -0
  45. data/lib/js_regex/utils/properties.rb +19 -0
  46. data/lib/js_regex/{converter → utils}/property_map.csv +31 -5
  47. data/lib/js_regex/utils.rb +8 -0
  48. data/lib/js_regex/version.rb +3 -1
  49. data/lib/js_regex.rb +5 -3
  50. metadata +29 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 054de046ea7e8e7244c80e8c8385270ffd821f0fdd690b8a15a79ea1c1d2a6f4
4
- data.tar.gz: d6d3dbd4d191e16008bfd586ff22a10a8240275ac328c0a080af910d99b650a1
3
+ metadata.gz: a25e95d1a39714ba3f0d0f87dc6f02029e75bc1df2b218a4e5d0111cbf6524a4
4
+ data.tar.gz: 11f74f2b8a0f95c6933ce10cb546ddb31dc9cd7626963d548de5dc8f13f51f6d
5
5
  SHA512:
6
- metadata.gz: 93e7c0a574cadfb867c598a9adbcbd9b18585204912865e589691246ce879bec7b7d48a02872c599447db3f2feeda80236d08b1030aecd1d6bf0568792ac859a
7
- data.tar.gz: e766f2ce0305ed0e6b10372d58f07fb0137e51a4a62a607b1f0272ac4b431c820fdb9ca2b8d2ce18de30d4bd33ca6cd8c49846d5c8efa79c081324342b990e87
6
+ metadata.gz: 920289779a37bc810ec3030b4b9dafb8ea95803813e5fcc8d037bc6d74285c13d4170ead8993869c47a9af75fcfca723270a29f62bdfa5aefa48d1ab14dcfc0b
7
+ data.tar.gz: e73d7ab9a56c768b7d3827e2f22e17a185a47322365b74da6985eeacfaf59ce18f4f233f48f0dea17d7851e8fb7070721fab016b0f6e51f883abf0be95ab490d
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class JsRegex
2
4
  #
3
5
  # This class acts as a facade, passing a Regexp to the Converters.
@@ -8,7 +10,6 @@ class JsRegex
8
10
  require 'regexp_parser'
9
11
  require_relative 'converter'
10
12
  require_relative 'error'
11
- require_relative 'node'
12
13
  require_relative 'second_pass'
13
14
  require_relative 'target'
14
15
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -49,9 +51,9 @@ class JsRegex
49
51
 
50
52
  # This is an approximation to the word boundary behavior in Ruby, c.f.
51
53
  # https://github.com/ruby/ruby/blob/08476c45/tool/enc-unicode.rb#L130
52
- W = '\d\p{L}\p{M}\p{Pc}'
54
+ W = '\p{N}\p{L}\p{M}\p{Pc}'
53
55
  BOUNDARY_EXPANSION = "(?:(?<=[#{W}])(?=[^#{W}]|$)|(?<=[^#{W}]|^)(?=[#{W}]))"
54
- NONBOUNDARY_EXPANSION = "(?<=[#{W}])(?=[#{W}])"
56
+ NONBOUNDARY_EXPANSION = "(?:(?<=[#{W}])(?=[#{W}])|(?<=[^#{W}]|^)(?=[^#{W}]|$))"
55
57
 
56
58
  def pass_boundary_with_warning
57
59
  warn_of("The anchor '#{data}' at index #{expression.ts} only works "\
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
  require_relative 'group_converter'
3
5
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -19,18 +21,37 @@ class JsRegex
19
21
  end
20
22
 
21
23
  def convert_name_ref
22
- if context.es_2018_or_higher?
23
- # ES 2018+ supports named backrefs, but only the angled-bracket syntax
24
- Node.new("\\k<#{expression.name}>", reference: new_position, type: :backref)
24
+ # Check if this is a multiplexed named group reference
25
+ if expression.referenced_expressions.count > 1
26
+ convert_multiplexed_name_ref
25
27
  else
28
+ # Always use numeric backrefs since we convert all named groups to numbered
29
+ # (see comment in GroupConverter)
26
30
  convert_to_plain_num_ref
27
31
  end
28
32
  end
29
33
 
30
34
  def convert_to_plain_num_ref
31
- position = new_position
32
- text = "\\#{position}#{'(?:)' if expression.x?}"
33
- Node.new(text, reference: position, type: :backref)
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
44
+ end
45
+ end
46
+
47
+ def convert_multiplexed_name_ref
48
+ # Create alternation of all groups with the same name
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)
53
+ end
54
+ Node.new(*references, type: :multiplex_backref)
34
55
  end
35
56
 
36
57
  def new_position
@@ -48,16 +69,16 @@ class JsRegex
48
69
  end
49
70
 
50
71
  context.count_recursion(expression)
51
- context.increment_local_capturing_group_count
52
- target_copy = expression.referenced_expression.unquantified_clone
53
- # avoid "Duplicate capture group name" error in JS
54
- target_copy.token = :capture if target_copy.is?(:named, :group)
72
+
55
73
  context.start_subexp_recursion
56
- result = convert_expression(target_copy)
74
+ ref = expression.referenced_expression
75
+ result = Converter.for(ref).convert(ref, context, quantified: false)
57
76
  context.end_subexp_recursion
58
- # wrap in group if it is a full-pattern recursion
77
+
78
+ # wrap in passive group if it is a full-pattern recursion
59
79
  expression.reference == 0 ? Node.new('(?:', result, ')') : result
60
80
  end
81
+
61
82
  end
62
83
  end
63
84
  end
@@ -1,3 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative '../node'
4
+ require_relative '../utils'
5
+
1
6
  class JsRegex
2
7
  module Converter
3
8
  #
@@ -6,13 +11,15 @@ class JsRegex
6
11
  #
7
12
  class Base
8
13
  # returns instance of Node with #quantifier attached.
9
- def convert(expression, context)
14
+ def convert(expression, context, quantified: true)
10
15
  self.context = context
11
16
  self.expression = expression
12
17
 
13
- node = convert_data
14
- node = Node.new(node) if node.instance_of?(String)
15
- apply_quantifier(node)
18
+ if quantified && expression.quantifier&.possessive?
19
+ wrap_in_backrefed_lookahead { convert_node(quantified) }
20
+ else
21
+ convert_node(quantified)
22
+ end
16
23
  end
17
24
 
18
25
  private
@@ -28,23 +35,23 @@ class JsRegex
28
35
  end
29
36
  alias pass_through data
30
37
 
31
- def apply_quantifier(node)
32
- return node if node.dropped? || (qtf = expression.quantifier).nil?
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
33
43
 
34
- if qtf.possessive?
35
- node.update(quantifier: qtf.dup.tap { |q| q.text = q.text[0..-2] })
36
- return wrap_in_backrefed_lookahead(node)
37
- elsif qtf.token == :interval && qtf.text[0..1] == "{,"
38
- node.update(quantifier: qtf.dup.tap { |q| q.text = "{0,#{q.max}}" })
39
- else
40
- node.update(quantifier: qtf)
41
- end
44
+ def apply_quantifier(node)
45
+ return node if node.dropped? ||
46
+ (qtf = Node::Quantifier.from_expression(expression)).nil?
42
47
 
43
- node
48
+ node.update(quantifier: qtf)
44
49
  end
45
50
 
46
51
  def convert_subexpressions
47
- Node.new(*expression.map { |subexp| convert_expression(subexp) })
52
+ # mark alternation and conditional branches for processing in second pass
53
+ type = expression.is?(:sequence) ? :branch : :plain
54
+ Node.new(*expression.map { |subexp| convert_expression(subexp) }, type: type)
48
55
  end
49
56
 
50
57
  def convert_expression(expression)
@@ -63,11 +70,7 @@ class JsRegex
63
70
  end
64
71
 
65
72
  def warn_of(text)
66
- if context.fail_fast
67
- raise ConversionError, text.sub(/^Dropped /, '')
68
- else
69
- context.warnings << text
70
- end
73
+ context.warn_of(text)
71
74
  end
72
75
 
73
76
  def drop
@@ -75,13 +78,21 @@ class JsRegex
75
78
  end
76
79
  alias drop_without_warning drop
77
80
 
78
- def wrap_in_backrefed_lookahead(content)
79
- number = context.capturing_group_count + 1
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
80
88
  backref_node = Node.new("\\#{number}", reference: number, type: :backref)
81
- context.increment_local_capturing_group_count
82
- # The surrounding group is added so that quantifiers apply to the whole.
83
- # Without it, `(?:)` would need to be appended as literal digits may follow.
84
- Node.new('(?:(?=(', *content, '))', backref_node, ')')
89
+ backrefed_group = Node.new('(', *content, ')', reference: number, type: :captured_group)
90
+ # The surrounding group makes quantifiers apply to the whole expansion.
91
+ Node.new('(?:(?=', backrefed_group, ')', backref_node, ')')
92
+ end
93
+
94
+ def unmatchable_substitution
95
+ '(?!)'
85
96
  end
86
97
  end
87
98
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -16,11 +18,17 @@ class JsRegex
16
18
  end
17
19
 
18
20
  def mark_conditional_for_second_pass
19
- reference = expression.referenced_expression.number
20
- node = Node.new('(?:', reference: reference, type: :conditional)
21
- expression.branches.each do |branch|
22
- node << Node.new('(?:', convert_expression(branch), ')')
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)
23
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 }
24
32
  node << ')'
25
33
  end
26
34
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  class JsRegex
2
4
  module Converter
3
5
  #
@@ -18,13 +20,24 @@ class JsRegex
18
20
  self.capturing_group_count = 0
19
21
  self.fail_fast = fail_fast
20
22
  self.recursions_per_expression = {}
23
+ self.recursion_stack = []
21
24
  self.required_options_hash = {}
22
25
  self.warnings = []
26
+ self.recursive_group_map = {}
27
+ self.recursive_group_history = Hash.new { |hash, key| hash[key] = [] }
23
28
 
24
29
  self.case_insensitive_root = case_insensitive_root
25
30
  self.target = target
26
31
  end
27
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
+
28
41
  # target context
29
42
 
30
43
  def es_2015_or_higher?
@@ -53,7 +66,10 @@ class JsRegex
53
66
 
54
67
  # group context
55
68
 
56
- 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
57
73
  self.capturing_group_count = capturing_group_count + 1
58
74
  end
59
75
 
@@ -65,17 +81,18 @@ class JsRegex
65
81
  self.in_atomic_group = false
66
82
  end
67
83
 
68
- def increment_local_capturing_group_count
69
- added_capturing_groups_after_group[original_capturing_group_count] += 1
70
- capture_group
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
71
87
  end
72
88
 
73
89
  def recursions(exp)
74
- recursions_per_expression[recursion_id(exp)] || 0
90
+ # Count recursions in the current stack path only
91
+ recursion_stack.count { |e| recursion_id(e) == recursion_id(exp) }
75
92
  end
76
93
 
77
94
  def count_recursion(exp)
78
- recursions_per_expression[recursion_id(exp)] = recursions(exp) + 1
95
+ recursion_stack.push(exp)
79
96
  end
80
97
 
81
98
  def recursion_id(exp)
@@ -84,10 +101,18 @@ class JsRegex
84
101
 
85
102
  def start_subexp_recursion
86
103
  self.in_subexp_recursion = true
104
+ self.recursion_start_group_count = capturing_group_count
87
105
  end
88
106
 
89
107
  def end_subexp_recursion
90
- self.in_subexp_recursion = false
108
+ # Pop the last recursion from stack when exiting
109
+ recursion_stack.pop if recursion_stack.any?
110
+ self.in_subexp_recursion = recursion_stack.any?
111
+ end
112
+
113
+ # Get the number of groups at the start of the current recursion
114
+ def recursion_start_group_count
115
+ @recursion_start_group_count || 0
91
116
  end
92
117
 
93
118
  # takes and returns 1-indexed group positions.
@@ -104,11 +129,31 @@ class JsRegex
104
129
  capturing_group_count - total_added_capturing_groups
105
130
  end
106
131
 
132
+ # Track that a group was created by a recursive call
133
+ def track_recursive_group_call(original_group_num, new_group_num)
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)
138
+ end
139
+
140
+ # Get the group number created by a recursive call
141
+ def get_recursive_group_position(original_group_num)
142
+ recursive_group_map[original_group_num]
143
+ end
144
+
145
+ def get_recursive_group_positions(original_group_num)
146
+ recursive_group_history[original_group_num].dup
147
+ end
148
+
107
149
  private
108
150
 
109
151
  attr_accessor :added_capturing_groups_after_group,
110
152
  :recursions_per_expression,
153
+ :recursion_stack,
111
154
  :required_options_hash,
155
+ :recursive_group_map,
156
+ :recursive_group_history,
112
157
  :target
113
158
 
114
159
  attr_writer :capturing_group_count,
@@ -116,10 +161,11 @@ class JsRegex
116
161
  :fail_fast,
117
162
  :in_atomic_group,
118
163
  :in_subexp_recursion,
164
+ :recursion_start_group_count,
119
165
  :warnings
120
166
 
121
167
  def total_added_capturing_groups
122
- added_capturing_groups_after_group.values.inject(0, &:+)
168
+ added_capturing_groups_after_group.values.sum
123
169
  end
124
170
  end
125
171
  end
@@ -1,5 +1,6 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
- require_relative 'literal_converter'
3
4
 
4
5
  class JsRegex
5
6
  module Converter
@@ -7,44 +8,19 @@ class JsRegex
7
8
  # Template class implementation.
8
9
  #
9
10
  class EscapeConverter < JsRegex::Converter::Base
10
- ESCAPES_SHARED_BY_RUBY_AND_JS = %i[
11
- alternation
12
- backslash
13
- backspace
14
- bol
15
- carriage
16
- codepoint
17
- dot
18
- eol
19
- form_feed
20
- group_close
21
- group_open
22
- hex
23
- interval_close
24
- interval_open
25
- newline
26
- one_or_more
27
- set_close
28
- set_open
29
- tab
30
- vertical_tab
31
- zero_or_more
32
- zero_or_one
33
- ].freeze
34
-
35
11
  private
36
12
 
37
13
  def convert_data
38
14
  case subtype
39
15
  when :codepoint_list
40
16
  convert_codepoint_list
41
- when :control, :meta_sequence
17
+ when :control, :meta_sequence, :utf8_hex
42
18
  unicode_escape_codepoint
43
19
  when :literal
44
- LiteralConverter.convert_data(expression.char, context)
20
+ Utils::Literals.convert_data(expression.char, context)
45
21
  when :bell, :escape, :hex, :octal
46
22
  hex_escape_codepoint
47
- when *ESCAPES_SHARED_BY_RUBY_AND_JS
23
+ when *Utils::Escapes::ESCAPES_SHARED_BY_RUBY_AND_JS
48
24
  pass_through
49
25
  else
50
26
  warn_of_unsupported_feature
@@ -56,7 +32,7 @@ class JsRegex
56
32
  split_codepoint_list
57
33
  else
58
34
  expression.chars.each_with_object(Node.new) do |char, node|
59
- node << LiteralConverter.convert_data(Regexp.escape(char), context)
35
+ node << Utils::Literals.convert_data(Regexp.escape(char), context)
60
36
  end
61
37
  end
62
38
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -22,12 +24,10 @@ class JsRegex
22
24
  end
23
25
 
24
26
  def build_named_group
25
- if context.es_2018_or_higher?
26
- # ES 2018+ supports named groups, but only the angled-bracket syntax
27
- build_group(head: "(?<#{expression.name}>")
28
- else
29
- build_group
30
- end
27
+ # Always convert named groups to numbered groups. ES2018+ supports named
28
+ # groups, but can not handle repeated names in multiplexing or conditional
29
+ # expansion scenarios.
30
+ build_group
31
31
  end
32
32
 
33
33
  def emulate_atomic_group
@@ -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(convert_subexpressions)
39
+ result = wrap_in_backrefed_lookahead { convert_subexpressions }
40
40
  context.end_atomic_group
41
41
  result
42
42
  end
@@ -69,13 +69,11 @@ class JsRegex
69
69
  expression.empty?
70
70
  end
71
71
 
72
- def unmatchable_substitution
73
- '(?!)'
74
- end
75
-
76
72
  def build_absence_group
77
- head = "(?:(?:.|\\n){,#{expression.inner_match_length.min - 1}}|(?:(?!"
78
- tail = ')(?:.|\n))*)'
73
+ head = '(?:(?:(?!'
74
+ maximum = expression.inner_match_length.min - 1
75
+ suffix = maximum > 0 ? "(?:.|\\n){0,#{maximum}}" : ''
76
+ tail = ")(?:.|\\n))*#{suffix})"
79
77
  build_group(head: head, tail: tail, capturing: false)
80
78
  end
81
79
 
@@ -84,9 +82,13 @@ class JsRegex
84
82
  tail = opts[:tail] || ')'
85
83
  return Node.new(*wrap(head, tail)) if opts[:capturing].equal?(false)
86
84
 
87
- context.capture_group unless context.in_subexp_recursion
88
- ref = expression.number
89
- 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
90
92
  end
91
93
 
92
94
  def wrap(head, tail)
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -6,68 +8,36 @@ class JsRegex
6
8
  # Template class implementation.
7
9
  #
8
10
  class LiteralConverter < JsRegex::Converter::Base
9
- ASTRAL_PLANE_CODEPOINT_PATTERN = /[\u{10000}-\u{10FFFF}]/
10
- LITERAL_REQUIRING_ESCAPE_PATTERN = /[\/\f\n\r\t\v]/
11
-
12
- class << self
13
- def convert_data(data, context)
14
- if !context.u? && data =~ ASTRAL_PLANE_CODEPOINT_PATTERN
15
- if context.enable_u_option
16
- escape_incompatible_bmp_literals(data)
17
- else
18
- convert_astral_data(data)
19
- end
20
- else
21
- escape_incompatible_bmp_literals(data)
22
- end
23
- end
24
-
25
- def convert_astral_data(data)
26
- data.each_char.each_with_object(Node.new) do |char, node|
27
- if char.ord > 0xFFFF
28
- node << surrogate_substitution_for(char)
29
- else
30
- node << escape_incompatible_bmp_literals(char)
31
- end
32
- end
33
- end
34
-
35
- ESCAPES = Hash.new { |h, k| raise KeyError, "#{h}[#{k.inspect}]" }
36
- .merge("\f\n\r\t\v".chars.to_h { |c| [c, Regexp.escape(c)] })
37
- .merge('/' => '\\/')
38
-
39
- def escape_incompatible_bmp_literals(data)
40
- data.gsub(LITERAL_REQUIRING_ESCAPE_PATTERN, ESCAPES)
41
- end
42
-
43
- private
44
-
45
- def surrogate_substitution_for(char)
46
- CharacterSet::Writer.write_surrogate_ranges([], [char.codepoints])
47
- end
48
- end
49
-
50
11
  private
51
12
 
52
13
  def convert_data
53
- result = self.class.convert_data(data, context)
14
+ result = Utils::Literals.convert_data(data, context)
54
15
  if context.case_insensitive_root && !expression.case_insensitive?
55
16
  warn_of_unsupported_feature('nested case-sensitive literal')
56
17
  elsif !context.case_insensitive_root && expression.case_insensitive?
57
- return handle_locally_case_insensitive_literal(result)
18
+ return handle_locally_case_insensitive_literal(data)
58
19
  end
59
20
  result
60
21
  end
61
22
 
62
- HAS_CASE_PATTERN = /[\p{lower}\p{upper}]/
23
+ HAS_CASE_PATTERN = /[\p{lower}\p{upper}]/.freeze
63
24
 
64
25
  def handle_locally_case_insensitive_literal(literal)
65
- literal =~ HAS_CASE_PATTERN ? case_insensitivize(literal) : literal
26
+ literal.match?(HAS_CASE_PATTERN) ? case_insensitivize(literal) : Utils::Literals.convert_data(literal, context)
66
27
  end
67
28
 
68
29
  def case_insensitivize(literal)
69
30
  literal.each_char.each_with_object(Node.new) do |chr, node|
70
- node << (chr =~ HAS_CASE_PATTERN ? "[#{chr}#{chr.swapcase}]" : chr)
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
71
41
  end
72
42
  end
73
43
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require_relative 'base'
2
4
 
3
5
  class JsRegex
@@ -6,19 +8,6 @@ class JsRegex
6
8
  # Template class implementation.
7
9
  #
8
10
  class MetaConverter < JsRegex::Converter::Base
9
- DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\n\uD800-\uDFFF])'
10
- ML_DOT_EXPANSION = '(?:[\uD800-\uDBFF][\uDC00-\uDFFF]|[^\uD800-\uDFFF])'
11
- # Possible improvements for dot conversion:
12
- #
13
- # In ES2015, the 'u' flag allows dots to match astral chars. Unfortunately
14
- # the dot keeps matching lone surrogates even with this flag, so the use
15
- # of an expansion is still necessary to get the same behavior as in Ruby.
16
- #
17
- # ES2018 has the dotall flag 's', but it is tricky to use in conversions.
18
- # 's' activates matching of BOTH astral chars and "\n", whereas the dot in
19
- # Ruby doesn't match "\n" by default, and even with the 'm' flag set on
20
- # the root, subexps might still exclude "\n" like so: /.(?-m:.)./m
21
-
22
11
  private
23
12
 
24
13
  def convert_data
@@ -32,6 +21,19 @@ class JsRegex
32
21
  end
33
22
  end
34
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
+
35
37
  def convert_alternatives
36
38
  kept_any_previous_branch = nil
37
39