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
|
@@ -1,9 +1,6 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative 'base'
|
|
4
|
-
require_relative 'escape_converter'
|
|
5
|
-
require_relative 'type_converter'
|
|
6
|
-
require 'character_set'
|
|
7
4
|
|
|
8
5
|
class JsRegex
|
|
9
6
|
module Converter
|
|
@@ -19,7 +16,7 @@ class JsRegex
|
|
|
19
16
|
private
|
|
20
17
|
|
|
21
18
|
def convert_data
|
|
22
|
-
simple_conversion ||
|
|
19
|
+
simple_conversion || Utils::CharSets.convert(expression, context)
|
|
23
20
|
end
|
|
24
21
|
|
|
25
22
|
def simple_conversion
|
|
@@ -50,62 +47,36 @@ class JsRegex
|
|
|
50
47
|
simple_convert_child(op) or return false
|
|
51
48
|
end.join('-')
|
|
52
49
|
when :type
|
|
53
|
-
|
|
54
|
-
exp.text
|
|
50
|
+
Utils::CharTypes.directly_compatible?(exp) && exp.text
|
|
55
51
|
when :escape
|
|
56
52
|
return exp.text if SET_SPECIFIC_ESCAPES_PATTERN.match?(exp.text)
|
|
57
53
|
|
|
58
54
|
case exp.token
|
|
59
55
|
when *CONVERTIBLE_ESCAPE_TOKENS
|
|
60
|
-
|
|
56
|
+
JsRegex::Converter.convert(exp, context)
|
|
61
57
|
when :literal
|
|
62
58
|
exp.char.ord <= 0xFFFF &&
|
|
63
|
-
|
|
59
|
+
Utils::Literals.escape_incompatible_bmp_literals(exp.char)
|
|
64
60
|
end
|
|
65
61
|
end
|
|
66
62
|
end
|
|
67
63
|
|
|
68
64
|
def simple_convert_literal_child(exp)
|
|
69
65
|
if !context.u? &&
|
|
70
|
-
exp.text
|
|
66
|
+
exp.text.match?(Utils::Literals::ASTRAL_PLANE_CODEPOINT_PATTERN) &&
|
|
71
67
|
!context.enable_u_option
|
|
72
68
|
false
|
|
73
69
|
elsif SET_LITERALS_REQUIRING_ESCAPE_PATTERN.match?(exp.text)
|
|
74
70
|
"\\#{exp.text}"
|
|
75
71
|
else
|
|
76
|
-
|
|
72
|
+
Utils::Literals.escape_incompatible_bmp_literals(exp.text)
|
|
77
73
|
end
|
|
78
74
|
end
|
|
79
75
|
|
|
80
76
|
SET_LITERALS_REQUIRING_ESCAPE_PATTERN = Regexp.union(%w<( ) [ ] { } / - |>)
|
|
81
77
|
SET_SPECIFIC_ESCAPES_PATTERN = /[\^\-]/.freeze
|
|
82
78
|
CONVERTIBLE_ESCAPE_TOKENS = %i[control meta_sequence bell escape octal] +
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
def full_recalculation
|
|
86
|
-
# Fetch codepoints as if the set was case-sensitive, then re-add
|
|
87
|
-
# case-insensitivity if needed.
|
|
88
|
-
# This way we preserve the casing of the original set in cases where the
|
|
89
|
-
# whole regexp is case-insensitive, e.g. /[ABc]/i => /[ABc]/i.
|
|
90
|
-
content = original_case_character_set
|
|
91
|
-
if expression.case_insensitive? && !context.case_insensitive_root
|
|
92
|
-
content = content.case_insensitive
|
|
93
|
-
elsif !expression.case_insensitive? && context.case_insensitive_root
|
|
94
|
-
warn_of_unsupported_feature('nested case-sensitive set')
|
|
95
|
-
end
|
|
96
|
-
if context.es_2015_or_higher?
|
|
97
|
-
context.enable_u_option if content.astral_part?
|
|
98
|
-
content.to_s(format: 'es6', in_brackets: true)
|
|
99
|
-
else
|
|
100
|
-
content.to_s_with_surrogate_ranges
|
|
101
|
-
end
|
|
102
|
-
end
|
|
103
|
-
|
|
104
|
-
def original_case_character_set
|
|
105
|
-
neutral_set = expression.dup
|
|
106
|
-
neutral_set.each_expression(true) { |exp| exp.options[:i] = false }
|
|
107
|
-
CharacterSet.of_expression(neutral_set)
|
|
108
|
-
end
|
|
79
|
+
Utils::Escapes::ESCAPES_SHARED_BY_RUBY_AND_JS
|
|
109
80
|
end
|
|
110
81
|
end
|
|
111
82
|
end
|
|
@@ -8,51 +8,6 @@ class JsRegex
|
|
|
8
8
|
# Template class implementation.
|
|
9
9
|
#
|
|
10
10
|
class TypeConverter < JsRegex::Converter::Base
|
|
11
|
-
HEX_EXPANSION = '[0-9A-Fa-f]'
|
|
12
|
-
NONHEX_EXPANSION = '[^0-9A-Fa-f]'
|
|
13
|
-
I_MODE_HEX_EXPANSION = '[0-9A-F]'
|
|
14
|
-
I_MODE_NONHEX_EXPANSION = '[^0-9A-F]'
|
|
15
|
-
LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
|
|
16
|
-
ES2018_HEX_EXPANSION = '\p{AHex}'
|
|
17
|
-
ES2018_NONHEX_EXPANSION = '\P{AHex}'
|
|
18
|
-
# partially taken from https://unicode.org/reports/tr51/#EBNF_and_Regex
|
|
19
|
-
ES2018_XGRAPHEME_EXPANSION = <<-'REGEXP'.gsub(/\s+/, '')
|
|
20
|
-
(?:
|
|
21
|
-
\r\n
|
|
22
|
-
|
|
|
23
|
-
\p{RI}\p{RI}
|
|
24
|
-
|
|
|
25
|
-
\p{Emoji}
|
|
26
|
-
(?:
|
|
27
|
-
\p{EMod}
|
|
28
|
-
|
|
|
29
|
-
\uFE0F\u20E3?
|
|
30
|
-
|
|
|
31
|
-
[\u{E0020}-\u{E007E}]+\u{E007F}
|
|
32
|
-
)?
|
|
33
|
-
(?:
|
|
34
|
-
\u200D
|
|
35
|
-
(?:
|
|
36
|
-
\p{RI}\p{RI}
|
|
37
|
-
|
|
|
38
|
-
\p{Emoji}(?:\p{EMod}|\uFE0F\u20E3?|[\u{E0020}-\u{E007E}]+\u{E007F})?
|
|
39
|
-
)
|
|
40
|
-
)*
|
|
41
|
-
|
|
|
42
|
-
[\P{M}\P{Lm}](?:\u200d|\p{M}|\p{Lm}|\p{Emoji_Modifier})*
|
|
43
|
-
)
|
|
44
|
-
REGEXP
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
def self.directly_compatible?(expression, _context = nil)
|
|
48
|
-
case expression.token
|
|
49
|
-
when :space, :nonspace
|
|
50
|
-
!expression.ascii_classes?
|
|
51
|
-
when :digit, :nondigit, :word, :nonword
|
|
52
|
-
!expression.unicode_classes?
|
|
53
|
-
end
|
|
54
|
-
end
|
|
55
|
-
|
|
56
11
|
private
|
|
57
12
|
|
|
58
13
|
def convert_data
|
|
@@ -61,12 +16,10 @@ class JsRegex
|
|
|
61
16
|
when :nonhex then nonhex_expansion
|
|
62
17
|
when :linebreak then linebreak_expansion
|
|
63
18
|
when :xgrapheme then xgrapheme
|
|
64
|
-
when :digit, :space, :word
|
|
65
|
-
return pass_through if
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
return pass_through if self.class.directly_compatible?(expression)
|
|
69
|
-
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)
|
|
70
23
|
else
|
|
71
24
|
warn_of_unsupported_feature
|
|
72
25
|
end
|
|
@@ -82,6 +35,10 @@ class JsRegex
|
|
|
82
35
|
end
|
|
83
36
|
end
|
|
84
37
|
|
|
38
|
+
ES2018_HEX_EXPANSION = '\p{AHex}'
|
|
39
|
+
I_MODE_HEX_EXPANSION = '[0-9A-F]'
|
|
40
|
+
HEX_EXPANSION = '[0-9A-Fa-f]'
|
|
41
|
+
|
|
85
42
|
def nonhex_expansion
|
|
86
43
|
if context.es_2018_or_higher? && context.enable_u_option
|
|
87
44
|
ES2018_NONHEX_EXPANSION
|
|
@@ -92,31 +49,51 @@ class JsRegex
|
|
|
92
49
|
end
|
|
93
50
|
end
|
|
94
51
|
|
|
52
|
+
NONHEX_EXPANSION = '[^0-9A-Fa-f]'
|
|
53
|
+
I_MODE_NONHEX_EXPANSION = '[^0-9A-F]'
|
|
54
|
+
ES2018_NONHEX_EXPANSION = '\P{AHex}'
|
|
55
|
+
|
|
95
56
|
def linebreak_expansion
|
|
96
|
-
wrap_in_backrefed_lookahead
|
|
57
|
+
wrap_in_backrefed_lookahead { LINEBREAK_EXPANSION }
|
|
97
58
|
end
|
|
98
59
|
|
|
99
|
-
|
|
100
|
-
# ::of_expression returns an inverted set for negative expressions,
|
|
101
|
-
# so we need to un-invert before wrapping in [^ and ]. Kinda lame.
|
|
102
|
-
"[^#{character_set.inversion.bmp_part}]"
|
|
103
|
-
end
|
|
104
|
-
|
|
105
|
-
def set_substitution
|
|
106
|
-
character_set.bmp_part.to_s(in_brackets: true)
|
|
107
|
-
end
|
|
108
|
-
|
|
109
|
-
def character_set
|
|
110
|
-
CharacterSet.of_expression(expression)
|
|
111
|
-
end
|
|
60
|
+
LINEBREAK_EXPANSION = '(?:\r\n|[\n\v\f\r\u0085\u2028\u2029])'
|
|
112
61
|
|
|
113
62
|
def xgrapheme
|
|
114
63
|
if context.es_2018_or_higher? && context.enable_u_option
|
|
115
|
-
wrap_in_backrefed_lookahead
|
|
64
|
+
wrap_in_backrefed_lookahead { ES2018_XGRAPHEME_EXPANSION }
|
|
116
65
|
else
|
|
117
66
|
warn_of_unsupported_feature
|
|
118
67
|
end
|
|
119
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
|
|
120
97
|
end
|
|
121
98
|
end
|
|
122
99
|
end
|
|
@@ -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
|
data/lib/js_regex/node.rb
CHANGED
|
@@ -7,32 +7,47 @@ class JsRegex
|
|
|
7
7
|
#
|
|
8
8
|
class Node
|
|
9
9
|
require_relative 'error'
|
|
10
|
+
Dir[File.join(__dir__, 'node', '*.rb')].sort.each { |f| require f }
|
|
11
|
+
|
|
12
|
+
ATTRIBUTES = %i[
|
|
13
|
+
children
|
|
14
|
+
marked_nonempty
|
|
15
|
+
nullable
|
|
16
|
+
quantifier
|
|
17
|
+
reference
|
|
18
|
+
type
|
|
19
|
+
].freeze
|
|
10
20
|
|
|
11
|
-
attr_reader
|
|
21
|
+
attr_reader(*ATTRIBUTES)
|
|
12
22
|
|
|
13
|
-
|
|
23
|
+
REFERENCE_TYPES = %i[
|
|
14
24
|
backref
|
|
25
|
+
capture_backref
|
|
26
|
+
capture_check
|
|
27
|
+
capture_conditional
|
|
28
|
+
conditional
|
|
29
|
+
multiplex_backref
|
|
30
|
+
].freeze
|
|
31
|
+
|
|
32
|
+
REFERENCE_TYPES_MAP = REFERENCE_TYPES.to_h { |k| [k, true] }.freeze
|
|
33
|
+
|
|
34
|
+
TYPES = (REFERENCE_TYPES + %i[
|
|
15
35
|
branch
|
|
16
36
|
captured_group
|
|
17
|
-
conditional
|
|
18
37
|
dropped
|
|
19
38
|
keep_mark
|
|
20
39
|
plain
|
|
21
|
-
].freeze
|
|
40
|
+
]).freeze
|
|
41
|
+
|
|
42
|
+
TYPES_MAP = TYPES.to_h { |k| [k, true] }.freeze
|
|
22
43
|
|
|
23
|
-
def initialize(*children, reference: nil, type: :plain)
|
|
44
|
+
def initialize(*children, reference: nil, type: :plain, marked_nonempty: false, nullable: false, quantifier: nil)
|
|
24
45
|
self.children = children
|
|
25
46
|
self.reference = reference
|
|
26
47
|
self.type = type
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
self.children = children.map(&:clone)
|
|
31
|
-
end
|
|
32
|
-
|
|
33
|
-
def transform(&block)
|
|
34
|
-
children.map!(&block)
|
|
35
|
-
self
|
|
48
|
+
self.marked_nonempty = marked_nonempty
|
|
49
|
+
self.nullable = nullable
|
|
50
|
+
self.quantifier = quantifier
|
|
36
51
|
end
|
|
37
52
|
|
|
38
53
|
def <<(node)
|
|
@@ -40,34 +55,9 @@ class JsRegex
|
|
|
40
55
|
self
|
|
41
56
|
end
|
|
42
57
|
|
|
43
|
-
def
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
type.equal?(:dropped)
|
|
47
|
-
end
|
|
48
|
-
|
|
49
|
-
def to_s
|
|
50
|
-
case type
|
|
51
|
-
when :dropped
|
|
52
|
-
''
|
|
53
|
-
when :backref, :branch, :captured_group, :plain
|
|
54
|
-
children.join << quantifier.to_s
|
|
55
|
-
else
|
|
56
|
-
raise TypeError.new(
|
|
57
|
-
"#{type} must be substituted before stringification"
|
|
58
|
-
).extend(JsRegex::Error)
|
|
59
|
-
end
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
def update(attrs)
|
|
63
|
-
self.children = attrs.fetch(:children) if attrs.key?(:children)
|
|
64
|
-
self.quantifier = attrs.fetch(:quantifier) if attrs.key?(:quantifier)
|
|
65
|
-
self.type = attrs.fetch(:type) if attrs.key?(:type)
|
|
66
|
-
self
|
|
67
|
-
end
|
|
68
|
-
|
|
69
|
-
def optional?
|
|
70
|
-
quantifier && quantifier.min == 0
|
|
58
|
+
def initialize_copy(*)
|
|
59
|
+
super
|
|
60
|
+
self.children = children.map(&:clone)
|
|
71
61
|
end
|
|
72
62
|
|
|
73
63
|
private
|
|
@@ -75,7 +65,7 @@ class JsRegex
|
|
|
75
65
|
TypeError = Class.new(::TypeError).extend(JsRegex::Error)
|
|
76
66
|
|
|
77
67
|
def type=(arg)
|
|
78
|
-
arg.nil? ||
|
|
68
|
+
arg.nil? || TYPES_MAP[arg] ||
|
|
79
69
|
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
80
70
|
@type = arg
|
|
81
71
|
end
|
|
@@ -87,7 +77,7 @@ class JsRegex
|
|
|
87
77
|
end
|
|
88
78
|
|
|
89
79
|
def quantifier=(arg)
|
|
90
|
-
arg.nil? || arg.class ==
|
|
80
|
+
arg.nil? || arg.class == Quantifier ||
|
|
91
81
|
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
92
82
|
@quantifier = arg
|
|
93
83
|
end
|
|
@@ -97,5 +87,17 @@ class JsRegex
|
|
|
97
87
|
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
98
88
|
@reference = arg
|
|
99
89
|
end
|
|
90
|
+
|
|
91
|
+
def marked_nonempty=(arg)
|
|
92
|
+
arg == true || arg == false ||
|
|
93
|
+
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
94
|
+
@marked_nonempty = arg
|
|
95
|
+
end
|
|
96
|
+
|
|
97
|
+
def nullable=(arg)
|
|
98
|
+
arg == true || arg == false ||
|
|
99
|
+
raise(TypeError, "unsupported type #{arg.class} for #{__method__}")
|
|
100
|
+
@nullable = arg
|
|
101
|
+
end
|
|
100
102
|
end
|
|
101
103
|
end
|