regexp_parser 1.3.0 → 1.7.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (170) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +65 -1
  3. data/Gemfile +3 -3
  4. data/README.md +10 -14
  5. data/Rakefile +3 -4
  6. data/lib/regexp_parser/expression.rb +28 -53
  7. data/lib/regexp_parser/expression/classes/backref.rb +18 -10
  8. data/lib/regexp_parser/expression/classes/conditional.rb +7 -2
  9. data/lib/regexp_parser/expression/classes/escape.rb +0 -4
  10. data/lib/regexp_parser/expression/classes/group.rb +4 -2
  11. data/lib/regexp_parser/expression/classes/keep.rb +1 -3
  12. data/lib/regexp_parser/expression/methods/match.rb +13 -0
  13. data/lib/regexp_parser/expression/methods/match_length.rb +172 -0
  14. data/lib/regexp_parser/expression/methods/options.rb +35 -0
  15. data/lib/regexp_parser/expression/methods/strfregexp.rb +0 -1
  16. data/lib/regexp_parser/expression/methods/tests.rb +6 -15
  17. data/lib/regexp_parser/expression/methods/traverse.rb +3 -1
  18. data/lib/regexp_parser/expression/quantifier.rb +2 -2
  19. data/lib/regexp_parser/expression/sequence.rb +3 -6
  20. data/lib/regexp_parser/expression/sequence_operation.rb +2 -6
  21. data/lib/regexp_parser/expression/subexpression.rb +3 -5
  22. data/lib/regexp_parser/lexer.rb +30 -44
  23. data/lib/regexp_parser/parser.rb +47 -24
  24. data/lib/regexp_parser/scanner.rb +1159 -1329
  25. data/lib/regexp_parser/scanner/char_type.rl +0 -3
  26. data/lib/regexp_parser/scanner/properties/long.yml +34 -1
  27. data/lib/regexp_parser/scanner/properties/short.yml +12 -0
  28. data/lib/regexp_parser/scanner/scanner.rl +82 -190
  29. data/lib/regexp_parser/syntax/tokens.rb +2 -10
  30. data/lib/regexp_parser/syntax/tokens/unicode_property.rb +72 -21
  31. data/lib/regexp_parser/syntax/versions/2.6.0.rb +10 -0
  32. data/lib/regexp_parser/syntax/versions/2.6.2.rb +10 -0
  33. data/lib/regexp_parser/syntax/versions/2.6.3.rb +10 -0
  34. data/lib/regexp_parser/version.rb +1 -1
  35. data/regexp_parser.gemspec +3 -3
  36. data/spec/expression/base_spec.rb +94 -0
  37. data/spec/expression/clone_spec.rb +120 -0
  38. data/spec/expression/conditional_spec.rb +89 -0
  39. data/spec/expression/free_space_spec.rb +27 -0
  40. data/spec/expression/methods/match_length_spec.rb +161 -0
  41. data/spec/expression/methods/match_spec.rb +25 -0
  42. data/spec/expression/methods/strfregexp_spec.rb +224 -0
  43. data/spec/expression/methods/tests_spec.rb +99 -0
  44. data/spec/expression/methods/traverse_spec.rb +161 -0
  45. data/spec/expression/options_spec.rb +128 -0
  46. data/spec/expression/root_spec.rb +9 -0
  47. data/spec/expression/sequence_spec.rb +9 -0
  48. data/spec/expression/subexpression_spec.rb +50 -0
  49. data/spec/expression/to_h_spec.rb +26 -0
  50. data/spec/expression/to_s_spec.rb +100 -0
  51. data/spec/lexer/all_spec.rb +22 -0
  52. data/spec/lexer/conditionals_spec.rb +53 -0
  53. data/spec/lexer/escapes_spec.rb +14 -0
  54. data/spec/lexer/keep_spec.rb +10 -0
  55. data/spec/lexer/literals_spec.rb +89 -0
  56. data/spec/lexer/nesting_spec.rb +99 -0
  57. data/spec/lexer/refcalls_spec.rb +55 -0
  58. data/spec/parser/all_spec.rb +43 -0
  59. data/spec/parser/alternation_spec.rb +88 -0
  60. data/spec/parser/anchors_spec.rb +17 -0
  61. data/spec/parser/conditionals_spec.rb +179 -0
  62. data/spec/parser/errors_spec.rb +30 -0
  63. data/spec/parser/escapes_spec.rb +121 -0
  64. data/spec/parser/free_space_spec.rb +130 -0
  65. data/spec/parser/groups_spec.rb +108 -0
  66. data/spec/parser/keep_spec.rb +6 -0
  67. data/spec/parser/posix_classes_spec.rb +8 -0
  68. data/spec/parser/properties_spec.rb +115 -0
  69. data/spec/parser/quantifiers_spec.rb +51 -0
  70. data/spec/parser/refcalls_spec.rb +112 -0
  71. data/spec/parser/set/intersections_spec.rb +127 -0
  72. data/spec/parser/set/ranges_spec.rb +111 -0
  73. data/spec/parser/sets_spec.rb +178 -0
  74. data/spec/parser/types_spec.rb +18 -0
  75. data/spec/scanner/all_spec.rb +18 -0
  76. data/spec/scanner/anchors_spec.rb +21 -0
  77. data/spec/scanner/conditionals_spec.rb +128 -0
  78. data/spec/scanner/errors_spec.rb +68 -0
  79. data/spec/scanner/escapes_spec.rb +53 -0
  80. data/spec/scanner/free_space_spec.rb +133 -0
  81. data/spec/scanner/groups_spec.rb +52 -0
  82. data/spec/scanner/keep_spec.rb +10 -0
  83. data/spec/scanner/literals_spec.rb +49 -0
  84. data/spec/scanner/meta_spec.rb +18 -0
  85. data/spec/scanner/properties_spec.rb +64 -0
  86. data/spec/scanner/quantifiers_spec.rb +20 -0
  87. data/spec/scanner/refcalls_spec.rb +36 -0
  88. data/spec/scanner/sets_spec.rb +102 -0
  89. data/spec/scanner/types_spec.rb +14 -0
  90. data/spec/spec_helper.rb +15 -0
  91. data/{test → spec}/support/runner.rb +9 -8
  92. data/spec/support/shared_examples.rb +77 -0
  93. data/{test → spec}/support/warning_extractor.rb +5 -7
  94. data/spec/syntax/syntax_spec.rb +48 -0
  95. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  96. data/spec/syntax/versions/1.8.6_spec.rb +17 -0
  97. data/spec/syntax/versions/1.9.1_spec.rb +10 -0
  98. data/spec/syntax/versions/1.9.3_spec.rb +9 -0
  99. data/spec/syntax/versions/2.0.0_spec.rb +13 -0
  100. data/spec/syntax/versions/2.2.0_spec.rb +9 -0
  101. data/spec/syntax/versions/aliases_spec.rb +37 -0
  102. data/spec/token/token_spec.rb +85 -0
  103. metadata +144 -143
  104. data/test/expression/test_all.rb +0 -12
  105. data/test/expression/test_base.rb +0 -90
  106. data/test/expression/test_clone.rb +0 -89
  107. data/test/expression/test_conditionals.rb +0 -113
  108. data/test/expression/test_free_space.rb +0 -35
  109. data/test/expression/test_set.rb +0 -84
  110. data/test/expression/test_strfregexp.rb +0 -230
  111. data/test/expression/test_subexpression.rb +0 -58
  112. data/test/expression/test_tests.rb +0 -99
  113. data/test/expression/test_to_h.rb +0 -59
  114. data/test/expression/test_to_s.rb +0 -104
  115. data/test/expression/test_traverse.rb +0 -161
  116. data/test/helpers.rb +0 -10
  117. data/test/lexer/test_all.rb +0 -41
  118. data/test/lexer/test_conditionals.rb +0 -127
  119. data/test/lexer/test_keep.rb +0 -24
  120. data/test/lexer/test_literals.rb +0 -130
  121. data/test/lexer/test_nesting.rb +0 -132
  122. data/test/lexer/test_refcalls.rb +0 -56
  123. data/test/parser/set/test_intersections.rb +0 -127
  124. data/test/parser/set/test_ranges.rb +0 -111
  125. data/test/parser/test_all.rb +0 -64
  126. data/test/parser/test_alternation.rb +0 -92
  127. data/test/parser/test_anchors.rb +0 -34
  128. data/test/parser/test_conditionals.rb +0 -187
  129. data/test/parser/test_errors.rb +0 -63
  130. data/test/parser/test_escapes.rb +0 -134
  131. data/test/parser/test_free_space.rb +0 -139
  132. data/test/parser/test_groups.rb +0 -289
  133. data/test/parser/test_keep.rb +0 -21
  134. data/test/parser/test_posix_classes.rb +0 -27
  135. data/test/parser/test_properties.rb +0 -133
  136. data/test/parser/test_quantifiers.rb +0 -301
  137. data/test/parser/test_refcalls.rb +0 -186
  138. data/test/parser/test_sets.rb +0 -179
  139. data/test/parser/test_types.rb +0 -50
  140. data/test/scanner/test_all.rb +0 -38
  141. data/test/scanner/test_anchors.rb +0 -38
  142. data/test/scanner/test_conditionals.rb +0 -184
  143. data/test/scanner/test_errors.rb +0 -91
  144. data/test/scanner/test_escapes.rb +0 -56
  145. data/test/scanner/test_free_space.rb +0 -200
  146. data/test/scanner/test_groups.rb +0 -79
  147. data/test/scanner/test_keep.rb +0 -35
  148. data/test/scanner/test_literals.rb +0 -89
  149. data/test/scanner/test_meta.rb +0 -40
  150. data/test/scanner/test_properties.rb +0 -312
  151. data/test/scanner/test_quantifiers.rb +0 -37
  152. data/test/scanner/test_refcalls.rb +0 -52
  153. data/test/scanner/test_scripts.rb +0 -53
  154. data/test/scanner/test_sets.rb +0 -119
  155. data/test/scanner/test_types.rb +0 -35
  156. data/test/scanner/test_unicode_blocks.rb +0 -30
  157. data/test/support/disable_autotest.rb +0 -8
  158. data/test/syntax/test_all.rb +0 -6
  159. data/test/syntax/test_syntax.rb +0 -61
  160. data/test/syntax/test_syntax_token_map.rb +0 -25
  161. data/test/syntax/versions/test_1.8.rb +0 -55
  162. data/test/syntax/versions/test_1.9.1.rb +0 -36
  163. data/test/syntax/versions/test_1.9.3.rb +0 -32
  164. data/test/syntax/versions/test_2.0.0.rb +0 -37
  165. data/test/syntax/versions/test_2.2.0.rb +0 -32
  166. data/test/syntax/versions/test_aliases.rb +0 -129
  167. data/test/syntax/versions/test_all.rb +0 -5
  168. data/test/test_all.rb +0 -5
  169. data/test/token/test_all.rb +0 -2
  170. data/test/token/test_token.rb +0 -107
@@ -0,0 +1,111 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('CharacterSet::Range parsing') do
4
+ specify('parse set range') do
5
+ root = RP.parse('[a-z]')
6
+ set = root[0]
7
+ range = set[0]
8
+
9
+ expect(set.count).to eq 1
10
+ expect(range).to be_instance_of(CharacterSet::Range)
11
+ expect(range.count).to eq 2
12
+ expect(range.first.to_s).to eq 'a'
13
+ expect(range.first).to be_instance_of(Literal)
14
+ expect(range.last.to_s).to eq 'z'
15
+ expect(range.last).to be_instance_of(Literal)
16
+ expect(set).to match 'm'
17
+ end
18
+
19
+ specify('parse set range hex') do
20
+ root = RP.parse('[\\x00-\\x99]')
21
+ set = root[0]
22
+ range = set[0]
23
+
24
+ expect(set.count).to eq 1
25
+ expect(range).to be_instance_of(CharacterSet::Range)
26
+ expect(range.count).to eq 2
27
+ expect(range.first.to_s).to eq '\\x00'
28
+ expect(range.first).to be_instance_of(EscapeSequence::Hex)
29
+ expect(range.last.to_s).to eq '\\x99'
30
+ expect(range.last).to be_instance_of(EscapeSequence::Hex)
31
+ expect(set).to match '\\x50'
32
+ end
33
+
34
+ specify('parse set range unicode') do
35
+ root = RP.parse('[\\u{40 42}-\\u1234]')
36
+ set = root[0]
37
+ range = set[0]
38
+
39
+ expect(set.count).to eq 1
40
+ expect(range).to be_instance_of(CharacterSet::Range)
41
+ expect(range.count).to eq 2
42
+ expect(range.first.to_s).to eq '\\u{40 42}'
43
+ expect(range.first).to be_instance_of(EscapeSequence::CodepointList)
44
+ expect(range.last.to_s).to eq '\\u1234'
45
+ expect(range.last).to be_instance_of(EscapeSequence::Codepoint)
46
+ expect(set).to match '\\u600'
47
+ end
48
+
49
+ specify('parse set range edge case leading dash') do
50
+ root = RP.parse('[--z]')
51
+ set = root[0]
52
+ range = set[0]
53
+
54
+ expect(set.count).to eq 1
55
+ expect(range.count).to eq 2
56
+ expect(set).to match 'a'
57
+ end
58
+
59
+ specify('parse set range edge case trailing dash') do
60
+ root = RP.parse('[!--]')
61
+ set = root[0]
62
+ range = set[0]
63
+
64
+ expect(set.count).to eq 1
65
+ expect(range.count).to eq 2
66
+ expect(set).to match '$'
67
+ end
68
+
69
+ specify('parse set range edge case leading negate') do
70
+ root = RP.parse('[^-z]')
71
+ set = root[0]
72
+
73
+ expect(set.count).to eq 2
74
+ expect(set).to match 'a'
75
+ expect(set).not_to match 'z'
76
+ end
77
+
78
+ specify('parse set range edge case trailing negate') do
79
+ root = RP.parse('[!-^]')
80
+ set = root[0]
81
+ range = set[0]
82
+
83
+ expect(set.count).to eq 1
84
+ expect(range.count).to eq 2
85
+ expect(set).to match '$'
86
+ end
87
+
88
+ specify('parse set range edge case leading intersection') do
89
+ root = RP.parse('[[\\-ab]&&-bc]')
90
+ set = root[0]
91
+
92
+ expect(set.count).to eq 1
93
+ expect(set.first.last.to_s).to eq '-bc'
94
+ expect(set).to match '-'
95
+ expect(set).to match 'b'
96
+ expect(set).not_to match 'a'
97
+ expect(set).not_to match 'c'
98
+ end
99
+
100
+ specify('parse set range edge case trailing intersection') do
101
+ root = RP.parse('[bc-&&[\\-ab]]')
102
+ set = root[0]
103
+
104
+ expect(set.count).to eq 1
105
+ expect(set.first.first.to_s).to eq 'bc-'
106
+ expect(set).to match '-'
107
+ expect(set).to match 'b'
108
+ expect(set).not_to match 'a'
109
+ expect(set).not_to match 'c'
110
+ end
111
+ end
@@ -0,0 +1,178 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('CharacterSet parsing') do
4
+ specify('parse set basic') do
5
+ root = RP.parse('[ab]+')
6
+ exp = root[0]
7
+
8
+ expect(exp).to be_instance_of(CharacterSet)
9
+ expect(exp.count).to eq 2
10
+
11
+ expect(exp[0]).to be_instance_of(Literal)
12
+ expect(exp[0].text).to eq 'a'
13
+ expect(exp[1]).to be_instance_of(Literal)
14
+ expect(exp[1].text).to eq 'b'
15
+
16
+ expect(exp).to be_quantified
17
+ expect(exp.quantifier.min).to eq 1
18
+ expect(exp.quantifier.max).to eq(-1)
19
+ end
20
+
21
+ specify('parse set char type') do
22
+ root = RP.parse('[a\\dc]')
23
+ exp = root[0]
24
+
25
+ expect(exp).to be_instance_of(CharacterSet)
26
+ expect(exp.count).to eq 3
27
+
28
+ expect(exp[1]).to be_instance_of(CharacterType::Digit)
29
+ expect(exp[1].text).to eq '\\d'
30
+ end
31
+
32
+ specify('parse set escape sequence backspace') do
33
+ root = RP.parse('[a\\bc]')
34
+ exp = root[0]
35
+
36
+ expect(exp).to be_instance_of(CharacterSet)
37
+ expect(exp.count).to eq 3
38
+
39
+ expect(exp[1]).to be_instance_of(EscapeSequence::Backspace)
40
+ expect(exp[1].text).to eq '\\b'
41
+
42
+ expect(exp).to match 'a'
43
+ expect(exp).to match "\b"
44
+ expect(exp).not_to match 'b'
45
+ expect(exp).to match 'c'
46
+ end
47
+
48
+ specify('parse set escape sequence hex') do
49
+ root = RP.parse('[a\\x20c]', :any)
50
+ exp = root[0]
51
+
52
+ expect(exp).to be_instance_of(CharacterSet)
53
+ expect(exp.count).to eq 3
54
+
55
+ expect(exp[1]).to be_instance_of(EscapeSequence::Hex)
56
+ expect(exp[1].text).to eq '\\x20'
57
+ end
58
+
59
+ specify('parse set escape sequence codepoint') do
60
+ root = RP.parse('[a\\u0640]')
61
+ exp = root[0]
62
+
63
+ expect(exp).to be_instance_of(CharacterSet)
64
+ expect(exp.count).to eq 2
65
+
66
+ expect(exp[1]).to be_instance_of(EscapeSequence::Codepoint)
67
+ expect(exp[1].text).to eq '\\u0640'
68
+ end
69
+
70
+ specify('parse set escape sequence codepoint list') do
71
+ root = RP.parse('[a\\u{41 1F60D}]')
72
+ exp = root[0]
73
+
74
+ expect(exp).to be_instance_of(CharacterSet)
75
+ expect(exp.count).to eq 2
76
+
77
+ expect(exp[1]).to be_instance_of(EscapeSequence::CodepointList)
78
+ expect(exp[1].text).to eq '\\u{41 1F60D}'
79
+ end
80
+
81
+ specify('parse set posix class') do
82
+ root = RP.parse('[[:digit:][:^lower:]]+')
83
+ exp = root[0]
84
+
85
+ expect(exp).to be_instance_of(CharacterSet)
86
+ expect(exp.count).to eq 2
87
+
88
+ expect(exp[0]).to be_instance_of(PosixClass)
89
+ expect(exp[0].text).to eq '[:digit:]'
90
+ expect(exp[1]).to be_instance_of(PosixClass)
91
+ expect(exp[1].text).to eq '[:^lower:]'
92
+ end
93
+
94
+ specify('parse set nesting') do
95
+ root = RP.parse('[a[b[c]d]e]')
96
+
97
+ exp = root[0]
98
+ expect(exp).to be_instance_of(CharacterSet)
99
+ expect(exp.count).to eq 3
100
+ expect(exp[0]).to be_instance_of(Literal)
101
+ expect(exp[2]).to be_instance_of(Literal)
102
+
103
+ subset1 = exp[1]
104
+ expect(subset1).to be_instance_of(CharacterSet)
105
+ expect(subset1.count).to eq 3
106
+ expect(subset1[0]).to be_instance_of(Literal)
107
+ expect(subset1[2]).to be_instance_of(Literal)
108
+
109
+ subset2 = subset1[1]
110
+ expect(subset2).to be_instance_of(CharacterSet)
111
+ expect(subset2.count).to eq 1
112
+ expect(subset2[0]).to be_instance_of(Literal)
113
+ end
114
+
115
+ specify('parse set nesting negative') do
116
+ root = RP.parse('[a[^b[c]]]')
117
+ exp = root[0]
118
+
119
+ expect(exp).to be_instance_of(CharacterSet)
120
+ expect(exp.count).to eq 2
121
+ expect(exp[0]).to be_instance_of(Literal)
122
+ expect(exp).not_to be_negative
123
+
124
+ subset1 = exp[1]
125
+ expect(subset1).to be_instance_of(CharacterSet)
126
+ expect(subset1.count).to eq 2
127
+ expect(subset1[0]).to be_instance_of(Literal)
128
+ expect(subset1).to be_negative
129
+
130
+ subset2 = subset1[1]
131
+ expect(subset2).to be_instance_of(CharacterSet)
132
+ expect(subset2.count).to eq 1
133
+ expect(subset2[0]).to be_instance_of(Literal)
134
+ expect(subset2).not_to be_negative
135
+ end
136
+
137
+ specify('parse set nesting #to_s') do
138
+ pattern = '[a[b[^c]]]'
139
+ root = RP.parse(pattern)
140
+
141
+ expect(root.to_s).to eq pattern
142
+ end
143
+
144
+ specify('parse set literals are not merged') do
145
+ root = RP.parse("[#{('a' * 10)}]")
146
+ exp = root[0]
147
+
148
+ expect(exp.count).to eq 10
149
+ end
150
+
151
+ specify('parse set whitespace is not merged') do
152
+ root = RP.parse("[#{(' ' * 10)}]")
153
+ exp = root[0]
154
+
155
+ expect(exp.count).to eq 10
156
+ end
157
+
158
+ specify('parse set whitespace is not merged in x mode') do
159
+ root = RP.parse("(?x)[#{(' ' * 10)}]")
160
+ exp = root[1]
161
+
162
+ expect(exp.count).to eq 10
163
+ end
164
+
165
+ specify('parse set collating sequence') do
166
+ root = RP.parse('[a[.span-ll.]h]', :any)
167
+ exp = root[0]
168
+
169
+ expect(exp[1].to_s).to eq '[.span-ll.]'
170
+ end
171
+
172
+ specify('parse set character equivalents') do
173
+ root = RP.parse('[a[=e=]h]', :any)
174
+ exp = root[0]
175
+
176
+ expect(exp[1].to_s).to eq '[=e=]'
177
+ end
178
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('CharacterType parsing') do
4
+ include_examples 'parse', /a\dc/, 1 => [:type, :digit, CharacterType::Digit]
5
+ include_examples 'parse', /a\Dc/, 1 => [:type, :nondigit, CharacterType::NonDigit]
6
+
7
+ include_examples 'parse', /a\sc/, 1 => [:type, :space, CharacterType::Space]
8
+ include_examples 'parse', /a\Sc/, 1 => [:type, :nonspace, CharacterType::NonSpace]
9
+
10
+ include_examples 'parse', /a\hc/, 1 => [:type, :hex, CharacterType::Hex]
11
+ include_examples 'parse', /a\Hc/, 1 => [:type, :nonhex, CharacterType::NonHex]
12
+
13
+ include_examples 'parse', /a\wc/, 1 => [:type, :word, CharacterType::Word]
14
+ include_examples 'parse', /a\Wc/, 1 => [:type, :nonword, CharacterType::NonWord]
15
+
16
+ include_examples 'parse', 'a\\Rc', 1 => [:type, :linebreak, CharacterType::Linebreak]
17
+ include_examples 'parse', 'a\\Xc', 1 => [:type, :xgrapheme, CharacterType::ExtendedGrapheme]
18
+ end
@@ -0,0 +1,18 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Scanner) do
4
+ specify('scanner returns an array') do
5
+ expect(RS.scan('abc')).to be_instance_of(Array)
6
+ end
7
+
8
+ specify('scanner returns tokens as arrays') do
9
+ tokens = RS.scan('^abc+[^one]{2,3}\\b\\d\\\\C-C$')
10
+ expect(tokens).to all(be_a Array)
11
+ expect(tokens.map(&:length)).to all(eq 5)
12
+ end
13
+
14
+ specify('scanner token count') do
15
+ re = /^(one|two){2,3}([^d\]efm-qz\,\-]*)(ghi)+$/i
16
+ expect(RS.scan(re).length).to eq 28
17
+ end
18
+ end
@@ -0,0 +1,21 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Anchor scanning') do
4
+ include_examples 'scan', '^abc', 0 => [:anchor, :bol, '^', 0, 1]
5
+ include_examples 'scan', 'abc$', 1 => [:anchor, :eol, '$', 3, 4]
6
+
7
+ include_examples 'scan', '\Aabc', 0 => [:anchor, :bos, '\A', 0, 2]
8
+ include_examples 'scan', 'abc\z', 1 => [:anchor, :eos, '\z', 3, 5]
9
+ include_examples 'scan', 'abc\Z', 1 => [:anchor, :eos_ob_eol, '\Z', 3, 5]
10
+
11
+ include_examples 'scan', 'a\bc', 1 => [:anchor, :word_boundary, '\b', 1, 3]
12
+ include_examples 'scan', 'a\Bc', 1 => [:anchor, :nonword_boundary, '\B', 1, 3]
13
+
14
+ include_examples 'scan', 'a\Gc', 1 => [:anchor, :match_start, '\G', 1, 3]
15
+
16
+ include_examples 'scan', "\\\\Ac", 0 => [:escape, :backslash, '\\\\', 0, 2]
17
+ include_examples 'scan', "a\\\\z", 1 => [:escape, :backslash, '\\\\', 1, 3]
18
+ include_examples 'scan', "a\\\\Z", 1 => [:escape, :backslash, '\\\\', 1, 3]
19
+ include_examples 'scan', "a\\\\bc", 1 => [:escape, :backslash, '\\\\', 1, 3]
20
+ include_examples 'scan', "a\\\\Bc", 1 => [:escape, :backslash, '\\\\', 1, 3]
21
+ end
@@ -0,0 +1,128 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Conditional scanning') do
4
+ include_examples 'scan', /(a)(?(1)T|F)1/, 3 => [:conditional, :open, '(?', 3, 5]
5
+ include_examples 'scan', /(a)(?(1)T|F)2/, 4 => [:conditional, :condition_open, '(', 5, 6]
6
+ include_examples 'scan', /(a)(?(1)T|F)3/, 5 => [:conditional, :condition, '1', 6, 7]
7
+ include_examples 'scan', /(a)(?(1)T|F)4/, 6 => [:conditional, :condition_close, ')', 7, 8]
8
+ include_examples 'scan', /(a)(?(1)T|F)5/, 7 => [:literal, :literal, 'T', 8, 9]
9
+ include_examples 'scan', /(a)(?(1)T|F)6/, 8 => [:conditional, :separator, '|', 9, 10]
10
+ include_examples 'scan', /(a)(?(1)T|F)7/, 9 => [:literal, :literal, 'F', 10, 11]
11
+ include_examples 'scan', /(a)(?(1)T|F)8/, 10 => [:conditional, :close, ')', 11, 12]
12
+ include_examples 'scan', /(a)(?(1)TRUE)9/, 8 => [:conditional, :close, ')', 12, 13]
13
+ include_examples 'scan', /(a)(?(1)TRUE|)10/, 8 => [:conditional, :separator, '|', 12, 13]
14
+ include_examples 'scan', /(a)(?(1)TRUE|)11/, 9 => [:conditional, :close, ')', 13, 14]
15
+ include_examples 'scan', /(?<N>A)(?(<N>)T|F)1/, 5 => [:conditional, :condition, '<N>', 10, 13]
16
+ include_examples 'scan', /(?'N'A)(?('N')T|F)2/, 5 => [:conditional, :condition, "'N'", 10, 13]
17
+
18
+ include_examples 'scan', /(a(b(c)))(?(1)(?(2)d|(?(3)e|f))|(?(2)(?(1)g|h)))/,
19
+ 0 => [:group, :capture, '(', 0, 1],
20
+ 1 => [:literal, :literal, 'a', 1, 2],
21
+ 2 => [:group, :capture, '(', 2, 3],
22
+ 3 => [:literal, :literal, 'b', 3, 4],
23
+ 4 => [:group, :capture, '(', 4, 5],
24
+ 5 => [:literal, :literal, 'c', 5, 6],
25
+ 6 => [:group, :close, ')', 6, 7],
26
+ 7 => [:group, :close, ')', 7, 8],
27
+ 8 => [:group, :close, ')', 8, 9],
28
+ 9 => [:conditional, :open, '(?', 9, 11],
29
+ 10 => [:conditional, :condition_open, '(', 11, 12],
30
+ 11 => [:conditional, :condition, '1', 12, 13],
31
+ 12 => [:conditional, :condition_close, ')', 13, 14],
32
+ 13 => [:conditional, :open, '(?', 14, 16],
33
+ 14 => [:conditional, :condition_open, '(', 16, 17],
34
+ 15 => [:conditional, :condition, '2', 17, 18],
35
+ 16 => [:conditional, :condition_close, ')', 18, 19],
36
+ 17 => [:literal, :literal, 'd', 19, 20],
37
+ 18 => [:conditional, :separator, '|', 20, 21],
38
+ 19 => [:conditional, :open, '(?', 21, 23],
39
+ 20 => [:conditional, :condition_open, '(', 23, 24],
40
+ 21 => [:conditional, :condition, '3', 24, 25],
41
+ 22 => [:conditional, :condition_close, ')', 25, 26],
42
+ 23 => [:literal, :literal, 'e', 26, 27],
43
+ 24 => [:conditional, :separator, '|', 27, 28],
44
+ 25 => [:literal, :literal, 'f', 28, 29],
45
+ 26 => [:conditional, :close, ')', 29, 30],
46
+ 27 => [:conditional, :close, ')', 30, 31],
47
+ 28 => [:conditional, :separator, '|', 31, 32],
48
+ 29 => [:conditional, :open, '(?', 32, 34],
49
+ 30 => [:conditional, :condition_open, '(', 34, 35],
50
+ 31 => [:conditional, :condition, '2', 35, 36],
51
+ 32 => [:conditional, :condition_close, ')', 36, 37],
52
+ 33 => [:conditional, :open, '(?', 37, 39],
53
+ 34 => [:conditional, :condition_open, '(', 39, 40],
54
+ 35 => [:conditional, :condition, '1', 40, 41],
55
+ 36 => [:conditional, :condition_close, ')', 41, 42],
56
+ 37 => [:literal, :literal, 'g', 42, 43],
57
+ 38 => [:conditional, :separator, '|', 43, 44],
58
+ 39 => [:literal, :literal, 'h', 44, 45],
59
+ 40 => [:conditional, :close, ')', 45, 46],
60
+ 41 => [:conditional, :close, ')', 46, 47],
61
+ 42 => [:conditional, :close, ')', 47, 48]
62
+
63
+ include_examples 'scan', /((a)|(b)|((?(2)(c(d|e)+)?|(?(3)f|(?(4)(g|(h)(i)))))))/,
64
+ 0 => [:group, :capture, '(', 0, 1],
65
+ 1 => [:group, :capture, '(', 1, 2],
66
+ 2 => [:literal, :literal, 'a', 2, 3],
67
+ 3 => [:group, :close, ')', 3, 4],
68
+ 4 => [:meta, :alternation, '|', 4, 5],
69
+ 5 => [:group, :capture, '(', 5, 6],
70
+ 6 => [:literal, :literal, 'b', 6, 7],
71
+ 7 => [:group, :close, ')', 7, 8],
72
+ 8 => [:meta, :alternation, '|', 8, 9],
73
+ 9 => [:group, :capture, '(', 9, 10],
74
+ 10 => [:conditional, :open, '(?', 10, 12],
75
+ 11 => [:conditional, :condition_open, '(', 12, 13],
76
+ 12 => [:conditional, :condition, '2', 13, 14],
77
+ 13 => [:conditional, :condition_close, ')', 14, 15],
78
+ 14 => [:group, :capture, '(', 15, 16],
79
+ 15 => [:literal, :literal, 'c', 16, 17],
80
+ 16 => [:group, :capture, '(', 17, 18],
81
+ 17 => [:literal, :literal, 'd', 18, 19],
82
+ 18 => [:meta, :alternation, '|', 19, 20],
83
+ 19 => [:literal, :literal, 'e', 20, 21],
84
+ 20 => [:group, :close, ')', 21, 22],
85
+ 21 => [:quantifier, :one_or_more, '+', 22, 23],
86
+ 22 => [:group, :close, ')', 23, 24],
87
+ 23 => [:quantifier, :zero_or_one, '?', 24, 25],
88
+ 24 => [:conditional, :separator, '|', 25, 26],
89
+ 25 => [:conditional, :open, '(?', 26, 28],
90
+ 26 => [:conditional, :condition_open, '(', 28, 29],
91
+ 27 => [:conditional, :condition, '3', 29, 30],
92
+ 28 => [:conditional, :condition_close, ')', 30, 31],
93
+ 29 => [:literal, :literal, 'f', 31, 32],
94
+ 30 => [:conditional, :separator, '|', 32, 33],
95
+ 31 => [:conditional, :open, '(?', 33, 35],
96
+ 32 => [:conditional, :condition_open, '(', 35, 36],
97
+ 33 => [:conditional, :condition, '4', 36, 37],
98
+ 34 => [:conditional, :condition_close, ')', 37, 38],
99
+ 35 => [:group, :capture, '(', 38, 39],
100
+ 36 => [:literal, :literal, 'g', 39, 40],
101
+ 37 => [:meta, :alternation, '|', 40, 41],
102
+ 38 => [:group, :capture, '(', 41, 42],
103
+ 39 => [:literal, :literal, 'h', 42, 43],
104
+ 40 => [:group, :close, ')', 43, 44],
105
+ 41 => [:group, :capture, '(', 44, 45],
106
+ 42 => [:literal, :literal, 'i', 45, 46],
107
+ 43 => [:group, :close, ')', 46, 47],
108
+ 44 => [:group, :close, ')', 47, 48],
109
+ 45 => [:conditional, :close, ')', 48, 49],
110
+ 46 => [:conditional, :close, ')', 49, 50],
111
+ 47 => [:conditional, :close, ')', 50, 51],
112
+ 48 => [:group, :close, ')', 51, 52],
113
+ 49 => [:group, :close, ')', 52, 53]
114
+
115
+ include_examples 'scan', /(a)(?(1)(b|c|d)|(e|f|g))(h)(?(2)(i|j|k)|(l|m|n))|o|p/,
116
+ 9 => [:meta, :alternation, '|', 10, 11],
117
+ 11 => [:meta, :alternation, '|', 12, 13],
118
+ 14 => [:conditional, :separator, '|', 15, 16],
119
+ 17 => [:meta, :alternation, '|', 18, 19],
120
+ 19 => [:meta, :alternation, '|', 20, 21],
121
+ 32 => [:meta, :alternation, '|', 34, 35],
122
+ 34 => [:meta, :alternation, '|', 36, 37],
123
+ 37 => [:conditional, :separator, '|', 39, 40],
124
+ 40 => [:meta, :alternation, '|', 42, 43],
125
+ 42 => [:meta, :alternation, '|', 44, 45],
126
+ 46 => [:meta, :alternation, '|', 48, 49],
127
+ 48 => [:meta, :alternation, '|', 50, 51]
128
+ end