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
|
@@ -1,5 +1,6 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'base'
|
|
2
|
-
require 'character_set'
|
|
3
4
|
|
|
4
5
|
class JsRegex
|
|
5
6
|
module Converter
|
|
@@ -10,33 +11,16 @@ class JsRegex
|
|
|
10
11
|
# codepoints matched by the property and build a set string from them.
|
|
11
12
|
#
|
|
12
13
|
class PropertyConverter < JsRegex::Converter::Base
|
|
13
|
-
# A map of normalized Ruby property names to names supported by ES2018+.
|
|
14
|
-
def self.map
|
|
15
|
-
@map ||= File.read("#{__dir__}/property_map.csv").scan(/(.+),(.+)/).to_h
|
|
16
|
-
end
|
|
17
|
-
|
|
18
14
|
private
|
|
19
15
|
|
|
20
16
|
def convert_data
|
|
21
17
|
if context.es_2018_or_higher? &&
|
|
22
|
-
(prop_name_in_js =
|
|
18
|
+
(prop_name_in_js = Utils::Properties.name_in_js(subtype))
|
|
23
19
|
context.enable_u_option
|
|
24
20
|
"\\#{expression.negative? ? 'P' : 'p'}{#{prop_name_in_js}}"
|
|
25
21
|
else
|
|
26
|
-
|
|
27
|
-
end
|
|
28
|
-
end
|
|
29
|
-
|
|
30
|
-
def build_character_set
|
|
31
|
-
content = CharacterSet.of_expression(expression)
|
|
32
|
-
|
|
33
|
-
if expression.case_insensitive? && !context.case_insensitive_root
|
|
34
|
-
content = content.case_insensitive
|
|
35
|
-
elsif !expression.case_insensitive? && context.case_insensitive_root
|
|
36
|
-
warn_of_unsupported_feature('nested case-sensitive property')
|
|
22
|
+
Utils::CharSets.convert(expression, context)
|
|
37
23
|
end
|
|
38
|
-
|
|
39
|
-
content.to_s_with_surrogate_ranges
|
|
40
24
|
end
|
|
41
25
|
end
|
|
42
26
|
end
|
|
@@ -1,7 +1,6 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'base'
|
|
2
|
-
require_relative 'escape_converter'
|
|
3
|
-
require_relative 'type_converter'
|
|
4
|
-
require 'character_set'
|
|
5
4
|
|
|
6
5
|
class JsRegex
|
|
7
6
|
module Converter
|
|
@@ -17,7 +16,7 @@ class JsRegex
|
|
|
17
16
|
private
|
|
18
17
|
|
|
19
18
|
def convert_data
|
|
20
|
-
simple_conversion ||
|
|
19
|
+
simple_conversion || Utils::CharSets.convert(expression, context)
|
|
21
20
|
end
|
|
22
21
|
|
|
23
22
|
def simple_conversion
|
|
@@ -48,62 +47,36 @@ class JsRegex
|
|
|
48
47
|
simple_convert_child(op) or return false
|
|
49
48
|
end.join('-')
|
|
50
49
|
when :type
|
|
51
|
-
|
|
52
|
-
exp.text
|
|
50
|
+
Utils::CharTypes.directly_compatible?(exp) && exp.text
|
|
53
51
|
when :escape
|
|
54
52
|
return exp.text if SET_SPECIFIC_ESCAPES_PATTERN.match?(exp.text)
|
|
55
53
|
|
|
56
54
|
case exp.token
|
|
57
55
|
when *CONVERTIBLE_ESCAPE_TOKENS
|
|
58
|
-
|
|
56
|
+
JsRegex::Converter.convert(exp, context)
|
|
59
57
|
when :literal
|
|
60
58
|
exp.char.ord <= 0xFFFF &&
|
|
61
|
-
|
|
59
|
+
Utils::Literals.escape_incompatible_bmp_literals(exp.char)
|
|
62
60
|
end
|
|
63
61
|
end
|
|
64
62
|
end
|
|
65
63
|
|
|
66
64
|
def simple_convert_literal_child(exp)
|
|
67
65
|
if !context.u? &&
|
|
68
|
-
exp.text
|
|
66
|
+
exp.text.match?(Utils::Literals::ASTRAL_PLANE_CODEPOINT_PATTERN) &&
|
|
69
67
|
!context.enable_u_option
|
|
70
68
|
false
|
|
71
69
|
elsif SET_LITERALS_REQUIRING_ESCAPE_PATTERN.match?(exp.text)
|
|
72
70
|
"\\#{exp.text}"
|
|
73
71
|
else
|
|
74
|
-
|
|
72
|
+
Utils::Literals.escape_incompatible_bmp_literals(exp.text)
|
|
75
73
|
end
|
|
76
74
|
end
|
|
77
75
|
|
|
78
76
|
SET_LITERALS_REQUIRING_ESCAPE_PATTERN = Regexp.union(%w<( ) [ ] { } / - |>)
|
|
79
|
-
SET_SPECIFIC_ESCAPES_PATTERN = /[\^\-]
|
|
77
|
+
SET_SPECIFIC_ESCAPES_PATTERN = /[\^\-]/.freeze
|
|
80
78
|
CONVERTIBLE_ESCAPE_TOKENS = %i[control meta_sequence bell escape octal] +
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
def full_recalculation
|
|
84
|
-
# Fetch codepoints as if the set was case-sensitive, then re-add
|
|
85
|
-
# case-insensitivity if needed.
|
|
86
|
-
# This way we preserve the casing of the original set in cases where the
|
|
87
|
-
# whole regexp is case-insensitive, e.g. /[ABc]/i => /[ABc]/i.
|
|
88
|
-
content = original_case_character_set
|
|
89
|
-
if expression.case_insensitive? && !context.case_insensitive_root
|
|
90
|
-
content = content.case_insensitive
|
|
91
|
-
elsif !expression.case_insensitive? && context.case_insensitive_root
|
|
92
|
-
warn_of_unsupported_feature('nested case-sensitive set')
|
|
93
|
-
end
|
|
94
|
-
if context.es_2015_or_higher?
|
|
95
|
-
context.enable_u_option if content.astral_part?
|
|
96
|
-
content.to_s(format: 'es6', in_brackets: true)
|
|
97
|
-
else
|
|
98
|
-
content.to_s_with_surrogate_ranges
|
|
99
|
-
end
|
|
100
|
-
end
|
|
101
|
-
|
|
102
|
-
def original_case_character_set
|
|
103
|
-
neutral_set = expression.dup
|
|
104
|
-
neutral_set.each_expression(true) { |exp| exp.options[:i] = false }
|
|
105
|
-
CharacterSet.of_expression(neutral_set)
|
|
106
|
-
end
|
|
79
|
+
Utils::Escapes::ESCAPES_SHARED_BY_RUBY_AND_JS
|
|
107
80
|
end
|
|
108
81
|
end
|
|
109
82
|
end
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
1
3
|
require_relative 'base'
|
|
2
4
|
|
|
3
5
|
class JsRegex
|
|
@@ -6,24 +8,6 @@ class JsRegex
|
|
|
6
8
|
# Template class implementation.
|
|
7
9
|
#
|
|
8
10
|
class TypeConverter < JsRegex::Converter::Base
|
|
9
|
-
HEX_EXPANSION = '[0-9A-Fa-f]'
|
|
10
|
-
NONHEX_EXPANSION = '[^0-9A-Fa-f]'
|
|
11
|
-
I_MODE_HEX_EXPANSION = '[0-9A-F]'
|
|
12
|
-
I_MODE_NONHEX_EXPANSION = '[^0-9A-F]'
|
|
13
|
-
ES2018_HEX_EXPANSION = '\p{AHex}'
|
|
14
|
-
ES2018_NONHEX_EXPANSION = '\P{AHex}'
|
|
15
|
-
ES2018_XGRAPHEME_EXPANSION = '[\P{M}\P{Lm}](?:(?:[\u035C\u0361]\P{M}\p{M}*)|\u200d|\p{M}|\p{Lm}|\p{Emoji_Modifier})*'
|
|
16
|
-
LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
|
|
17
|
-
|
|
18
|
-
def self.directly_compatible?(expression, _context = nil)
|
|
19
|
-
case expression.token
|
|
20
|
-
when :space, :nonspace
|
|
21
|
-
!expression.ascii_classes?
|
|
22
|
-
when :digit, :nondigit, :word, :nonword
|
|
23
|
-
!expression.unicode_classes?
|
|
24
|
-
end
|
|
25
|
-
end
|
|
26
|
-
|
|
27
11
|
private
|
|
28
12
|
|
|
29
13
|
def convert_data
|
|
@@ -32,12 +16,10 @@ class JsRegex
|
|
|
32
16
|
when :nonhex then nonhex_expansion
|
|
33
17
|
when :linebreak then linebreak_expansion
|
|
34
18
|
when :xgrapheme then xgrapheme
|
|
35
|
-
when :digit, :space, :word
|
|
36
|
-
return pass_through if
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
return pass_through if self.class.directly_compatible?(expression)
|
|
40
|
-
negative_set_substitution
|
|
19
|
+
when :digit, :space, :word, :nondigit, :nonspace, :nonword
|
|
20
|
+
return pass_through if Utils::CharTypes.directly_compatible?(expression)
|
|
21
|
+
|
|
22
|
+
Utils::CharSets.convert(expression, context)
|
|
41
23
|
else
|
|
42
24
|
warn_of_unsupported_feature
|
|
43
25
|
end
|
|
@@ -53,6 +35,10 @@ class JsRegex
|
|
|
53
35
|
end
|
|
54
36
|
end
|
|
55
37
|
|
|
38
|
+
ES2018_HEX_EXPANSION = '\p{AHex}'
|
|
39
|
+
I_MODE_HEX_EXPANSION = '[0-9A-F]'
|
|
40
|
+
HEX_EXPANSION = '[0-9A-Fa-f]'
|
|
41
|
+
|
|
56
42
|
def nonhex_expansion
|
|
57
43
|
if context.es_2018_or_higher? && context.enable_u_option
|
|
58
44
|
ES2018_NONHEX_EXPANSION
|
|
@@ -63,31 +49,51 @@ class JsRegex
|
|
|
63
49
|
end
|
|
64
50
|
end
|
|
65
51
|
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
def negative_set_substitution
|
|
71
|
-
# ::of_expression returns an inverted set for negative expressions,
|
|
72
|
-
# so we need to un-invert before wrapping in [^ and ]. Kinda lame.
|
|
73
|
-
"[^#{character_set.inversion.bmp_part}]"
|
|
74
|
-
end
|
|
52
|
+
NONHEX_EXPANSION = '[^0-9A-Fa-f]'
|
|
53
|
+
I_MODE_NONHEX_EXPANSION = '[^0-9A-F]'
|
|
54
|
+
ES2018_NONHEX_EXPANSION = '\P{AHex}'
|
|
75
55
|
|
|
76
|
-
def
|
|
77
|
-
|
|
56
|
+
def linebreak_expansion
|
|
57
|
+
wrap_in_backrefed_lookahead { LINEBREAK_EXPANSION }
|
|
78
58
|
end
|
|
79
59
|
|
|
80
|
-
|
|
81
|
-
CharacterSet.of_expression(expression)
|
|
82
|
-
end
|
|
60
|
+
LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
|
|
83
61
|
|
|
84
62
|
def xgrapheme
|
|
85
63
|
if context.es_2018_or_higher? && context.enable_u_option
|
|
86
|
-
wrap_in_backrefed_lookahead
|
|
64
|
+
wrap_in_backrefed_lookahead { ES2018_XGRAPHEME_EXPANSION }
|
|
87
65
|
else
|
|
88
66
|
warn_of_unsupported_feature
|
|
89
67
|
end
|
|
90
68
|
end
|
|
69
|
+
|
|
70
|
+
# partially taken from https://unicode.org/reports/tr51/#EBNF_and_Regex
|
|
71
|
+
ES2018_XGRAPHEME_EXPANSION = <<-'REGEXP'.gsub(/\s+/, '')
|
|
72
|
+
(?:
|
|
73
|
+
\r\n
|
|
74
|
+
|
|
|
75
|
+
\p{RI}\p{RI}
|
|
76
|
+
|
|
|
77
|
+
\p{Emoji}
|
|
78
|
+
(?:
|
|
79
|
+
\p{EMod}
|
|
80
|
+
|
|
|
81
|
+
\uFE0F\u20E3?
|
|
82
|
+
|
|
|
83
|
+
[\u{E0020}-\u{E007E}]+\u{E007F}
|
|
84
|
+
)?
|
|
85
|
+
(?:
|
|
86
|
+
\u200D
|
|
87
|
+
(?:
|
|
88
|
+
\p{RI}\p{RI}
|
|
89
|
+
|
|
|
90
|
+
\p{Emoji}(?:\p{EMod}|\uFE0F\u20E3?|[\u{E0020}-\u{E007E}]+\u{E007F})?
|
|
91
|
+
)
|
|
92
|
+
)*
|
|
93
|
+
|
|
|
94
|
+
[\P{M}\P{Lm}](?:\u200d|\p{M}|\p{Lm}|\p{Emoji_Modifier})*
|
|
95
|
+
)
|
|
96
|
+
REGEXP
|
|
91
97
|
end
|
|
92
98
|
end
|
|
93
99
|
end
|
data/lib/js_regex/converter.rb
CHANGED
data/lib/js_regex/error.rb
CHANGED
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
class JsRegex::Node
|
|
2
|
+
def dropped?
|
|
3
|
+
# keep everything else, including empty or depleted capturing groups
|
|
4
|
+
# so as not to not mess with reference numbers (e.g. backrefs)
|
|
5
|
+
type.equal?(:dropped)
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
def branch?
|
|
9
|
+
type == :branch
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
def alternation?
|
|
13
|
+
children.any? && children.all? do |child|
|
|
14
|
+
child.is_a?(JsRegex::Node) && child.branch?
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def repeated?
|
|
19
|
+
!!quantifier&.repeated?
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def optional?
|
|
23
|
+
!!quantifier&.optional?
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def zero_or_one?
|
|
27
|
+
!!quantifier&.zero_or_one?
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def zero_quantified?
|
|
31
|
+
!!quantifier&.zero?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def max_one?
|
|
35
|
+
!!quantifier&.max_one?
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def negative_assertion?
|
|
39
|
+
head = children.first
|
|
40
|
+
head == '(?!' || head == '(?<!'
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def positive_assertion?
|
|
44
|
+
head = children.first
|
|
45
|
+
head.is_a?(String) && (head.start_with?('(?=') || head == '(?<=')
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def atomic?
|
|
49
|
+
# Atomic groups are emitted as a lookahead followed by a backreference.
|
|
50
|
+
children.first == '(?:(?='
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
def marked_nonempty?
|
|
54
|
+
@marked_nonempty
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The body can match empty at any position, without relying on assertions
|
|
58
|
+
# or backreferences. Neither flag being set means the body is uncertain.
|
|
59
|
+
def nullable?
|
|
60
|
+
@nullable
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
def conditional?
|
|
64
|
+
type == :conditional || type == :capture_conditional
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
def internal_reference?
|
|
68
|
+
REFERENCE_TYPES_MAP.key?(type)
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
class JsRegex::Node
|
|
2
|
+
def conditional_branches
|
|
3
|
+
case type
|
|
4
|
+
when :capture_conditional then children[1..-1]
|
|
5
|
+
when :conditional then children[1...-1]
|
|
6
|
+
else raise TypeError, "cannot get conditional branches for #{type}"
|
|
7
|
+
end
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def condition_references
|
|
11
|
+
case type
|
|
12
|
+
when :capture_conditional then children.first.children.map(&:reference)
|
|
13
|
+
when :conditional then [reference]
|
|
14
|
+
else raise TypeError, "cannot get condition references for #{type}"
|
|
15
|
+
end
|
|
16
|
+
end
|
|
17
|
+
end
|
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
class JsRegex::Node::Quantifier
|
|
2
|
+
# Boundary between regexp_parser expressions and their quantifiers
|
|
3
|
+
# and our internal representation.
|
|
4
|
+
def self.from_expression(expression)
|
|
5
|
+
return if (qtf = expression.quantifier).nil?
|
|
6
|
+
|
|
7
|
+
new(qtf.min, qtf.max, reluctant: qtf.reluctant?)
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def initialize(min, max, reluctant: false)
|
|
11
|
+
self.min = min
|
|
12
|
+
self.max = max
|
|
13
|
+
self.reluctant = reluctant
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def optional?
|
|
17
|
+
min.zero?
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def zero?
|
|
21
|
+
max.zero?
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def max_one?
|
|
25
|
+
max == 1
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def zero_or_one?
|
|
29
|
+
optional? && max_one?
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def unbounded?
|
|
33
|
+
max < 0
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
def repeated?
|
|
37
|
+
unbounded? || max > 1
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
def bounded_repetition?
|
|
41
|
+
!unbounded? && max > 1
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
def minimum_at_most_one?
|
|
45
|
+
min <= 1
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
def zero_or_more?
|
|
49
|
+
optional? && unbounded?
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def one_or_more?
|
|
53
|
+
min == 1 && unbounded?
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
def fixed?
|
|
57
|
+
min == max
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
def optional_iteration?(index)
|
|
61
|
+
index > min
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def remaining_minimum(consumed)
|
|
65
|
+
[min - consumed, 0].max
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def iteration_indices
|
|
69
|
+
raise ArgumentError, 'cannot enumerate unbounded repetitions' if unbounded?
|
|
70
|
+
|
|
71
|
+
(1..max)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def before_last_iteration?(index)
|
|
75
|
+
index < max
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def repeat_word(word)
|
|
79
|
+
raise ArgumentError, 'variable repetition has no constant word' unless fixed?
|
|
80
|
+
|
|
81
|
+
word * min
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Prefixes before one required iteration, in quantifier preference order.
|
|
85
|
+
def prefix_counts
|
|
86
|
+
minimum = remaining_minimum(1)
|
|
87
|
+
counts = (0...minimum).map { |count| [count, count] } + [[minimum, -1]]
|
|
88
|
+
reluctant? ? counts : counts.reverse
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
def with_minimum_one
|
|
92
|
+
self.class.new(1, max, reluctant: reluctant?)
|
|
93
|
+
end
|
|
94
|
+
|
|
95
|
+
def reluctant?
|
|
96
|
+
!!@reluctant
|
|
97
|
+
end
|
|
98
|
+
|
|
99
|
+
def to_s
|
|
100
|
+
case [min, max]
|
|
101
|
+
when [0, -1] then '*'
|
|
102
|
+
when [1, -1] then '+'
|
|
103
|
+
when [0, 1] then '?'
|
|
104
|
+
else
|
|
105
|
+
if min == max
|
|
106
|
+
"{#{min}}"
|
|
107
|
+
else
|
|
108
|
+
"{#{min},#{max < 0 ? '' : max}}"
|
|
109
|
+
end
|
|
110
|
+
end + (reluctant? ? '?' : '')
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
private
|
|
114
|
+
|
|
115
|
+
attr_reader :min, :max
|
|
116
|
+
|
|
117
|
+
def min=(arg)
|
|
118
|
+
arg.instance_of?(Integer) ||
|
|
119
|
+
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
120
|
+
@min = arg
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def max=(arg)
|
|
124
|
+
arg.instance_of?(Integer) ||
|
|
125
|
+
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
126
|
+
@max = arg
|
|
127
|
+
end
|
|
128
|
+
|
|
129
|
+
def reluctant=(arg)
|
|
130
|
+
arg == true || arg == false ||
|
|
131
|
+
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
132
|
+
@reluctant = arg
|
|
133
|
+
end
|
|
134
|
+
end
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
class JsRegex::Node
|
|
2
|
+
def to_s
|
|
3
|
+
case type
|
|
4
|
+
when :dropped
|
|
5
|
+
''
|
|
6
|
+
when :backref, :branch, :capture_check, :captured_group, :plain
|
|
7
|
+
children.join << quantifier.to_s
|
|
8
|
+
else
|
|
9
|
+
raise TypeError.new(
|
|
10
|
+
"#{type} must be substituted before stringification"
|
|
11
|
+
).extend(JsRegex::Error)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
class JsRegex::Node
|
|
2
|
+
# Creates a copy with the given attributes replaced, without mutating self.
|
|
3
|
+
def with(**attrs)
|
|
4
|
+
validate_attrs(**attrs)
|
|
5
|
+
children = attrs.key?(:children) ? attrs.delete(:children) : self.children.map(&:clone)
|
|
6
|
+
new_attrs = (ATTRIBUTES - [:children]).to_h do |attr|
|
|
7
|
+
[attr, attrs.fetch(attr) { send(attr) }]
|
|
8
|
+
end
|
|
9
|
+
self.class.new(*children, **new_attrs)
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
# Mutates self with validations.
|
|
13
|
+
def update(**attrs)
|
|
14
|
+
validate_attrs(**attrs)
|
|
15
|
+
ATTRIBUTES.each { |attr| send("#{attr}=", attrs.fetch(attr)) if attrs.key?(attr) }
|
|
16
|
+
self
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def transform(&block)
|
|
20
|
+
children.map!(&block)
|
|
21
|
+
self
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Preserve optionality: a failing node with ? can still be skipped.
|
|
25
|
+
def make_unmatchable
|
|
26
|
+
update(type: :plain, reference: nil, children: ['(?!)'])
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
# Keep numeric reference text in sync with its annotation. Captures and
|
|
30
|
+
# unresolved conditions only store the number in their annotation.
|
|
31
|
+
def renumber(number)
|
|
32
|
+
self.reference = number
|
|
33
|
+
case type
|
|
34
|
+
when :backref, :capture_check then update(children: ["\\#{number}"])
|
|
35
|
+
when :captured_group, :conditional then nil
|
|
36
|
+
else raise TypeError, "cannot renumber #{type} node"
|
|
37
|
+
end
|
|
38
|
+
self
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
def make_passive
|
|
42
|
+
raise TypeError, "cannot make #{type} passive" unless type == :captured_group
|
|
43
|
+
|
|
44
|
+
update(type: :plain, reference: nil, children: ['(?:', *children[1..-1]])
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def quantify(min, max, reluctant: false)
|
|
48
|
+
update(quantifier: Quantifier.new(min, max, reluctant: reluctant))
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
def validate_attrs(**attrs)
|
|
54
|
+
attrs.each_key { |k| ATTRIBUTES.include?(k) || raise(ArgumentError, "no attribute #{k}") }
|
|
55
|
+
end
|
|
56
|
+
end
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class JsRegex::Node
|
|
2
|
+
# Traverse the node tree, depth first.
|
|
3
|
+
# If `skip_self: true` is given, yield only the children and their descendants.
|
|
4
|
+
# If `trace: true` is given, yield the node and its path on both entry and exit.
|
|
5
|
+
def walk(skip_self: false, trace: false, path: [], &block)
|
|
6
|
+
return enum_for(__method__, skip_self: skip_self, trace: trace, path: path) unless block
|
|
7
|
+
|
|
8
|
+
if trace
|
|
9
|
+
path << self
|
|
10
|
+
yield(self, :enter, path) if !skip_self
|
|
11
|
+
else
|
|
12
|
+
yield(self) if !skip_self
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
children.each do |child|
|
|
16
|
+
child.walk(trace: trace, path: path, &block) unless child.instance_of?(String)
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
if trace
|
|
20
|
+
yield(self, :exit, path) if !skip_self
|
|
21
|
+
path.pop
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|