regexp_parser 1.3.0 → 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (169) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +53 -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/quantifier.rb +2 -2
  18. data/lib/regexp_parser/expression/sequence.rb +3 -6
  19. data/lib/regexp_parser/expression/sequence_operation.rb +2 -6
  20. data/lib/regexp_parser/expression/subexpression.rb +3 -5
  21. data/lib/regexp_parser/lexer.rb +30 -44
  22. data/lib/regexp_parser/parser.rb +47 -24
  23. data/lib/regexp_parser/scanner.rb +1159 -1329
  24. data/lib/regexp_parser/scanner/char_type.rl +0 -3
  25. data/lib/regexp_parser/scanner/properties/long.yml +34 -1
  26. data/lib/regexp_parser/scanner/properties/short.yml +12 -0
  27. data/lib/regexp_parser/scanner/scanner.rl +82 -190
  28. data/lib/regexp_parser/syntax/tokens.rb +2 -10
  29. data/lib/regexp_parser/syntax/tokens/unicode_property.rb +72 -21
  30. data/lib/regexp_parser/syntax/versions/2.6.0.rb +10 -0
  31. data/lib/regexp_parser/syntax/versions/2.6.2.rb +10 -0
  32. data/lib/regexp_parser/syntax/versions/2.6.3.rb +10 -0
  33. data/lib/regexp_parser/version.rb +1 -1
  34. data/regexp_parser.gemspec +3 -3
  35. data/spec/expression/base_spec.rb +94 -0
  36. data/spec/expression/clone_spec.rb +120 -0
  37. data/spec/expression/conditional_spec.rb +89 -0
  38. data/spec/expression/free_space_spec.rb +27 -0
  39. data/spec/expression/methods/match_length_spec.rb +154 -0
  40. data/spec/expression/methods/match_spec.rb +25 -0
  41. data/spec/expression/methods/strfregexp_spec.rb +224 -0
  42. data/spec/expression/methods/tests_spec.rb +99 -0
  43. data/spec/expression/methods/traverse_spec.rb +140 -0
  44. data/spec/expression/options_spec.rb +128 -0
  45. data/spec/expression/root_spec.rb +9 -0
  46. data/spec/expression/sequence_spec.rb +9 -0
  47. data/spec/expression/subexpression_spec.rb +50 -0
  48. data/spec/expression/to_h_spec.rb +26 -0
  49. data/spec/expression/to_s_spec.rb +100 -0
  50. data/spec/lexer/all_spec.rb +22 -0
  51. data/spec/lexer/conditionals_spec.rb +53 -0
  52. data/spec/lexer/escapes_spec.rb +14 -0
  53. data/spec/lexer/keep_spec.rb +10 -0
  54. data/spec/lexer/literals_spec.rb +89 -0
  55. data/spec/lexer/nesting_spec.rb +99 -0
  56. data/spec/lexer/refcalls_spec.rb +55 -0
  57. data/spec/parser/all_spec.rb +43 -0
  58. data/spec/parser/alternation_spec.rb +88 -0
  59. data/spec/parser/anchors_spec.rb +17 -0
  60. data/spec/parser/conditionals_spec.rb +179 -0
  61. data/spec/parser/errors_spec.rb +30 -0
  62. data/spec/parser/escapes_spec.rb +121 -0
  63. data/spec/parser/free_space_spec.rb +130 -0
  64. data/spec/parser/groups_spec.rb +108 -0
  65. data/spec/parser/keep_spec.rb +6 -0
  66. data/spec/parser/posix_classes_spec.rb +8 -0
  67. data/spec/parser/properties_spec.rb +115 -0
  68. data/spec/parser/quantifiers_spec.rb +51 -0
  69. data/spec/parser/refcalls_spec.rb +112 -0
  70. data/spec/parser/set/intersections_spec.rb +127 -0
  71. data/spec/parser/set/ranges_spec.rb +111 -0
  72. data/spec/parser/sets_spec.rb +178 -0
  73. data/spec/parser/types_spec.rb +18 -0
  74. data/spec/scanner/all_spec.rb +18 -0
  75. data/spec/scanner/anchors_spec.rb +21 -0
  76. data/spec/scanner/conditionals_spec.rb +128 -0
  77. data/spec/scanner/errors_spec.rb +68 -0
  78. data/spec/scanner/escapes_spec.rb +53 -0
  79. data/spec/scanner/free_space_spec.rb +133 -0
  80. data/spec/scanner/groups_spec.rb +52 -0
  81. data/spec/scanner/keep_spec.rb +10 -0
  82. data/spec/scanner/literals_spec.rb +49 -0
  83. data/spec/scanner/meta_spec.rb +18 -0
  84. data/spec/scanner/properties_spec.rb +64 -0
  85. data/spec/scanner/quantifiers_spec.rb +20 -0
  86. data/spec/scanner/refcalls_spec.rb +36 -0
  87. data/spec/scanner/sets_spec.rb +102 -0
  88. data/spec/scanner/types_spec.rb +14 -0
  89. data/spec/spec_helper.rb +15 -0
  90. data/{test → spec}/support/runner.rb +9 -8
  91. data/spec/support/shared_examples.rb +77 -0
  92. data/{test → spec}/support/warning_extractor.rb +5 -7
  93. data/spec/syntax/syntax_spec.rb +48 -0
  94. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  95. data/spec/syntax/versions/1.8.6_spec.rb +17 -0
  96. data/spec/syntax/versions/1.9.1_spec.rb +10 -0
  97. data/spec/syntax/versions/1.9.3_spec.rb +9 -0
  98. data/spec/syntax/versions/2.0.0_spec.rb +13 -0
  99. data/spec/syntax/versions/2.2.0_spec.rb +9 -0
  100. data/spec/syntax/versions/aliases_spec.rb +37 -0
  101. data/spec/token/token_spec.rb +85 -0
  102. metadata +144 -143
  103. data/test/expression/test_all.rb +0 -12
  104. data/test/expression/test_base.rb +0 -90
  105. data/test/expression/test_clone.rb +0 -89
  106. data/test/expression/test_conditionals.rb +0 -113
  107. data/test/expression/test_free_space.rb +0 -35
  108. data/test/expression/test_set.rb +0 -84
  109. data/test/expression/test_strfregexp.rb +0 -230
  110. data/test/expression/test_subexpression.rb +0 -58
  111. data/test/expression/test_tests.rb +0 -99
  112. data/test/expression/test_to_h.rb +0 -59
  113. data/test/expression/test_to_s.rb +0 -104
  114. data/test/expression/test_traverse.rb +0 -161
  115. data/test/helpers.rb +0 -10
  116. data/test/lexer/test_all.rb +0 -41
  117. data/test/lexer/test_conditionals.rb +0 -127
  118. data/test/lexer/test_keep.rb +0 -24
  119. data/test/lexer/test_literals.rb +0 -130
  120. data/test/lexer/test_nesting.rb +0 -132
  121. data/test/lexer/test_refcalls.rb +0 -56
  122. data/test/parser/set/test_intersections.rb +0 -127
  123. data/test/parser/set/test_ranges.rb +0 -111
  124. data/test/parser/test_all.rb +0 -64
  125. data/test/parser/test_alternation.rb +0 -92
  126. data/test/parser/test_anchors.rb +0 -34
  127. data/test/parser/test_conditionals.rb +0 -187
  128. data/test/parser/test_errors.rb +0 -63
  129. data/test/parser/test_escapes.rb +0 -134
  130. data/test/parser/test_free_space.rb +0 -139
  131. data/test/parser/test_groups.rb +0 -289
  132. data/test/parser/test_keep.rb +0 -21
  133. data/test/parser/test_posix_classes.rb +0 -27
  134. data/test/parser/test_properties.rb +0 -133
  135. data/test/parser/test_quantifiers.rb +0 -301
  136. data/test/parser/test_refcalls.rb +0 -186
  137. data/test/parser/test_sets.rb +0 -179
  138. data/test/parser/test_types.rb +0 -50
  139. data/test/scanner/test_all.rb +0 -38
  140. data/test/scanner/test_anchors.rb +0 -38
  141. data/test/scanner/test_conditionals.rb +0 -184
  142. data/test/scanner/test_errors.rb +0 -91
  143. data/test/scanner/test_escapes.rb +0 -56
  144. data/test/scanner/test_free_space.rb +0 -200
  145. data/test/scanner/test_groups.rb +0 -79
  146. data/test/scanner/test_keep.rb +0 -35
  147. data/test/scanner/test_literals.rb +0 -89
  148. data/test/scanner/test_meta.rb +0 -40
  149. data/test/scanner/test_properties.rb +0 -312
  150. data/test/scanner/test_quantifiers.rb +0 -37
  151. data/test/scanner/test_refcalls.rb +0 -52
  152. data/test/scanner/test_scripts.rb +0 -53
  153. data/test/scanner/test_sets.rb +0 -119
  154. data/test/scanner/test_types.rb +0 -35
  155. data/test/scanner/test_unicode_blocks.rb +0 -30
  156. data/test/support/disable_autotest.rb +0 -8
  157. data/test/syntax/test_all.rb +0 -6
  158. data/test/syntax/test_syntax.rb +0 -61
  159. data/test/syntax/test_syntax_token_map.rb +0 -25
  160. data/test/syntax/versions/test_1.8.rb +0 -55
  161. data/test/syntax/versions/test_1.9.1.rb +0 -36
  162. data/test/syntax/versions/test_1.9.3.rb +0 -32
  163. data/test/syntax/versions/test_2.0.0.rb +0 -37
  164. data/test/syntax/versions/test_2.2.0.rb +0 -32
  165. data/test/syntax/versions/test_aliases.rb +0 -129
  166. data/test/syntax/versions/test_all.rb +0 -5
  167. data/test/test_all.rb +0 -5
  168. data/test/token/test_all.rb +0 -2
  169. data/test/token/test_token.rb +0 -107
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Group parsing') do
4
+ include_examples 'parse', /(?=abc)(?!def)/,
5
+ 0 => [:assertion, :lookahead, Assertion::Lookahead],
6
+ 1 => [:assertion, :nlookahead, Assertion::NegativeLookahead]
7
+
8
+ include_examples 'parse', /(?<=abc)(?<!def)/,
9
+ 0 => [:assertion, :lookbehind, Assertion::Lookbehind],
10
+ 1 => [:assertion, :nlookbehind, Assertion::NegativeLookbehind]
11
+
12
+ include_examples 'parse', /a(?# is for apple)b(?# for boy)c(?# cat)/,
13
+ 1 => [:group, :comment, Group::Comment],
14
+ 3 => [:group, :comment, Group::Comment],
15
+ 5 => [:group, :comment, Group::Comment]
16
+
17
+ if ruby_version_at_least('2.4.1')
18
+ include_examples 'parse', 'a(?~b)c(?~d)e',
19
+ 1 => [:group, :absence, Group::Absence],
20
+ 3 => [:group, :absence, Group::Absence]
21
+ end
22
+
23
+ include_examples 'parse', /(?m:a)/,
24
+ 0 => [:group, :options, Group::Options, options: { m: true }, option_changes: { m: true }]
25
+
26
+ # self-defeating group option
27
+ include_examples 'parse', /(?m-m:a)/,
28
+ 0 => [:group, :options, Group::Options, options: {}, option_changes: { m: false }]
29
+
30
+ # activate one option in nested group
31
+ include_examples 'parse', /(?x-mi:a(?m:b))/,
32
+ 0 => [:group, :options, Group::Options, options: { x: true }, option_changes: { i: false, m: false, x: true }],
33
+ [0, 1] => [:group, :options, Group::Options, options: { m: true, x: true }, option_changes: { m: true }]
34
+
35
+ # deactivate one option in nested group
36
+ include_examples 'parse', /(?ix-m:a(?-i:b))/,
37
+ 0 => [:group, :options, Group::Options, options: { i: true, x: true }, option_changes: { i: true, m: false, x: true }],
38
+ [0, 1] => [:group, :options, Group::Options, options: { x: true }, option_changes: { i: false }]
39
+
40
+ # invert all options in nested group
41
+ include_examples 'parse', /(?xi-m:a(?m-ix:b))/,
42
+ 0 => [:group, :options, Group::Options, options: { i: true, x: true }, option_changes: { i: true, m: false, x: true }],
43
+ [0, 1] => [:group, :options, Group::Options, options: { m: true }, option_changes: { i: false, m: true, x: false }]
44
+
45
+ # nested options affect literal subexpressions
46
+ include_examples 'parse', /(?x-mi:a(?m:b))/,
47
+ [0, 0] => [:literal, :literal, Literal, text: 'a', options: { x: true }],
48
+ [0, 1, 0] => [:literal, :literal, Literal, text: 'b', options: { m: true, x: true }]
49
+
50
+ # option switching group
51
+ include_examples 'parse', /a(?i-m)b/m,
52
+ 0 => [:literal, :literal, Literal, text: 'a', options: { m: true }],
53
+ 1 => [:group, :options_switch, Group::Options, options: { i: true }, option_changes: { i: true, m: false }],
54
+ 2 => [:literal, :literal, Literal, text: 'b', options: { i: true }]
55
+
56
+ # option switch in group
57
+ include_examples 'parse', /(a(?i-m)b)c/m,
58
+ 0 => [:group, :capture, Group::Capture, options: { m: true }],
59
+ [0, 0] => [:literal, :literal, Literal, text: 'a', options: { m: true }],
60
+ [0, 1] => [:group, :options_switch, Group::Options, options: { i: true }, option_changes: { i: true, m: false }],
61
+ [0, 2] => [:literal, :literal, Literal, text: 'b', options: { i: true }],
62
+ 1 => [:literal, :literal, Literal, text: 'c', options: { m: true }]
63
+
64
+ # nested option switch in group
65
+ include_examples 'parse', /((?i-m)(a(?-i)b))/m,
66
+ [0, 1] => [:group, :capture, Group::Capture, options: { i: true }],
67
+ [0, 1, 0] => [:literal, :literal, Literal, text: 'a', options: { i: true }],
68
+ [0, 1, 1] => [:group, :options_switch, Group::Options, options: {}, option_changes: { i: false }],
69
+ [0, 1, 2] => [:literal, :literal, Literal, text: 'b', options: {}]
70
+
71
+ # options dau
72
+ include_examples 'parse', /(?dua:abc)/,
73
+ 0 => [:group, :options, Group::Options, options: { a: true }, option_changes: { a: true }]
74
+
75
+ # nested options dau
76
+ include_examples 'parse', /(?u:a(?d:b))/,
77
+ 0 => [:group, :options, Group::Options, options: { u: true }, option_changes: { u: true }],
78
+ [0, 1] => [:group, :options, Group::Options, options: { d: true }, option_changes: { d: true, u: false }],
79
+ [0, 1, 0] => [:literal, :literal, Literal, text: 'b', options: { d: true }]
80
+
81
+ # nested options da
82
+ include_examples 'parse', /(?di-xm:a(?da-x:b))/,
83
+ 0 => [:group, :options, Group::Options, options: { d: true, i:true }],
84
+ [0, 1] => [:group, :options, Group::Options, options: { a: true, i: true }, option_changes: { a: true, d: false, x: false}],
85
+ [0, 1, 0] => [:literal, :literal, Literal, text: 'b', options: { a: true, i: true }]
86
+
87
+ specify('parse group number') do
88
+ root = RP.parse(/(a)(?=b)((?:c)(d|(e)))/)
89
+
90
+ expect(root[0].number).to eq 1
91
+ expect(root[1]).not_to respond_to(:number)
92
+ expect(root[2].number).to eq 2
93
+ expect(root[2][0]).not_to respond_to(:number)
94
+ expect(root[2][1].number).to eq 3
95
+ expect(root[2][1][0][1][0].number).to eq 4
96
+ end
97
+
98
+ specify('parse group number at level') do
99
+ root = RP.parse(/(a)(?=b)((?:c)(d|(e)))/)
100
+
101
+ expect(root[0].number_at_level).to eq 1
102
+ expect(root[1]).not_to respond_to(:number_at_level)
103
+ expect(root[2].number_at_level).to eq 2
104
+ expect(root[2][0]).not_to respond_to(:number_at_level)
105
+ expect(root[2][1].number_at_level).to eq 1
106
+ expect(root[2][1][0][1][0].number_at_level).to eq 1
107
+ end
108
+ end
@@ -0,0 +1,6 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Keep parsing') do
4
+ include_examples 'parse', /ab\Kcd/, 1 => [:keep, :mark, Keep::Mark, text: '\K']
5
+ include_examples 'parse', /(a\K)/, [0, 1] => [:keep, :mark, Keep::Mark, text: '\K']
6
+ end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('PosixClass parsing') do
4
+ include_examples 'parse', /[[:word:]]/, [0, 0] => [:posixclass, :word, PosixClass,
5
+ name: 'word', text: '[:word:]', negative?: false]
6
+ include_examples 'parse', /[[:^word:]]/, [0, 0] => [:nonposixclass, :word, PosixClass,
7
+ name: 'word', text: '[:^word:]', negative?: true]
8
+ end
@@ -0,0 +1,115 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Property parsing') do
4
+ example_props = [
5
+ 'Alnum',
6
+ 'Any',
7
+ 'Age=1.1',
8
+ 'Dash',
9
+ 'di',
10
+ 'Default_Ignorable_Code_Point',
11
+ 'Math',
12
+ 'Noncharacter-Code_Point', # test dash
13
+ 'sd',
14
+ 'Soft Dotted', # test whitespace
15
+ 'sterm',
16
+ 'xidc',
17
+ 'XID_Continue',
18
+ 'Emoji',
19
+ 'InChessSymbols'
20
+ ]
21
+
22
+ example_props.each do |name|
23
+ it("parses property #{name}") do
24
+ exp = RP.parse("ab\\p{#{name}}", '*').last
25
+
26
+ expect(exp).to be_a(UnicodeProperty::Base)
27
+ expect(exp.type).to eq :property
28
+ expect(exp.name).to eq name
29
+ end
30
+
31
+ it("parses nonproperty #{name}") do
32
+ exp = RP.parse("ab\\P{#{name}}", '*').last
33
+
34
+ expect(exp).to be_a(UnicodeProperty::Base)
35
+ expect(exp.type).to eq :nonproperty
36
+ expect(exp.name).to eq name
37
+ end
38
+ end
39
+
40
+ specify('parse all properties of current ruby') do
41
+ unsupported = RegexpPropertyValues.all_for_current_ruby.reject do |prop|
42
+ RP.parse("\\p{#{prop}}") rescue false
43
+ end
44
+ expect(unsupported).to be_empty
45
+ end
46
+
47
+ specify('parse property negative') do
48
+ root = RP.parse('ab\p{L}cd', 'ruby/1.9')
49
+ expect(root[1]).not_to be_negative
50
+ end
51
+
52
+ specify('parse nonproperty negative') do
53
+ root = RP.parse('ab\P{L}cd', 'ruby/1.9')
54
+ expect(root[1]).to be_negative
55
+ end
56
+
57
+ specify('parse caret nonproperty negative') do
58
+ root = RP.parse('ab\p{^L}cd', 'ruby/1.9')
59
+ expect(root[1]).to be_negative
60
+ end
61
+
62
+ specify('parse double negated property negative') do
63
+ root = RP.parse('ab\P{^L}cd', 'ruby/1.9')
64
+ expect(root[1]).not_to be_negative
65
+ end
66
+
67
+ specify('parse property shortcut') do
68
+ expect(RP.parse('\p{lowercase_letter}')[0].shortcut).to eq 'll'
69
+ expect(RP.parse('\p{sc}')[0].shortcut).to eq 'sc'
70
+ expect(RP.parse('\p{in_bengali}')[0].shortcut).to be_nil
71
+ end
72
+
73
+ specify('parse property age') do
74
+ root = RP.parse('ab\p{age=5.2}cd', 'ruby/1.9')
75
+ expect(root[1]).to be_a(UnicodeProperty::Age)
76
+ end
77
+
78
+ specify('parse property derived') do
79
+ root = RP.parse('ab\p{Math}cd', 'ruby/1.9')
80
+ expect(root[1]).to be_a(UnicodeProperty::Derived)
81
+ end
82
+
83
+ specify('parse property script') do
84
+ root = RP.parse('ab\p{Hiragana}cd', 'ruby/1.9')
85
+ expect(root[1]).to be_a(UnicodeProperty::Script)
86
+ end
87
+
88
+ specify('parse property script V1 9 3') do
89
+ root = RP.parse('ab\p{Brahmi}cd', 'ruby/1.9.3')
90
+ expect(root[1]).to be_a(UnicodeProperty::Script)
91
+ end
92
+
93
+ specify('parse property script V2 2 0') do
94
+ root = RP.parse('ab\p{Caucasian_Albanian}cd', 'ruby/2.2')
95
+ expect(root[1]).to be_a(UnicodeProperty::Script)
96
+ end
97
+
98
+ specify('parse property block') do
99
+ root = RP.parse('ab\p{InArmenian}cd', 'ruby/1.9')
100
+ expect(root[1]).to be_a(UnicodeProperty::Block)
101
+ end
102
+
103
+ specify('parse property following literal') do
104
+ root = RP.parse('ab\p{Lu}cd', 'ruby/1.9')
105
+ expect(root[2]).to be_a(Literal)
106
+ end
107
+
108
+ specify('parse abandoned newline property') do
109
+ root = RP.parse('\p{newline}', 'ruby/1.9')
110
+ expect(root.expressions.last).to be_a(UnicodeProperty::Base)
111
+
112
+ expect { RP.parse('\p{newline}', 'ruby/2.0') }
113
+ .to raise_error(Regexp::Syntax::NotImplementedError)
114
+ end
115
+ end
@@ -0,0 +1,51 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Quantifier parsing') do
4
+ RSpec.shared_examples 'quantifier' do |pattern, text, mode, token, min, max|
5
+ it "parses the quantifier in #{pattern} as #{mode} #{token}" do
6
+ root = RP.parse(pattern, '*')
7
+ exp = root[0]
8
+
9
+ expect(exp).to be_quantified
10
+ expect(exp.quantifier.token).to eq token
11
+ expect(exp.quantifier.min).to eq min
12
+ expect(exp.quantifier.max).to eq max
13
+ expect(exp.quantifier.mode).to eq mode
14
+ end
15
+ end
16
+
17
+ include_examples 'quantifier', /a?b/, '?', :greedy, :zero_or_one, 0, 1
18
+ include_examples 'quantifier', /a??b/, '??', :reluctant, :zero_or_one, 0, 1
19
+ include_examples 'quantifier', /a?+b/, '?+', :possessive, :zero_or_one, 0, 1
20
+ include_examples 'quantifier', /a*b/, '*', :greedy, :zero_or_more, 0, -1
21
+ include_examples 'quantifier', /a*?b/, '*?', :reluctant, :zero_or_more, 0, -1
22
+ include_examples 'quantifier', /a*+b/, '*+', :possessive, :zero_or_more, 0, -1
23
+ include_examples 'quantifier', /a+b/, '+', :greedy, :one_or_more, 1, -1
24
+ include_examples 'quantifier', /a+?b/, '+?', :reluctant, :one_or_more, 1, -1
25
+ include_examples 'quantifier', /a++b/, '++', :possessive, :one_or_more, 1, -1
26
+ include_examples 'quantifier', /a{2,4}b/, '{2,4}', :greedy, :interval, 2, 4
27
+ include_examples 'quantifier', /a{2,4}?b/, '{2,4}?', :reluctant, :interval, 2, 4
28
+ include_examples 'quantifier', /a{2,4}+b/, '{2,4}+', :possessive, :interval, 2, 4
29
+ include_examples 'quantifier', /a{2,}b/, '{2,}', :greedy, :interval, 2, -1
30
+ include_examples 'quantifier', /a{2,}?b/, '{2,}?', :reluctant, :interval, 2, -1
31
+ include_examples 'quantifier', /a{2,}+b/, '{2,}+', :possessive, :interval, 2, -1
32
+ include_examples 'quantifier', /a{,3}b/, '{,3}', :greedy, :interval, 0, 3
33
+ include_examples 'quantifier', /a{,3}?b/, '{,3}?', :reluctant, :interval, 0, 3
34
+ include_examples 'quantifier', /a{,3}+b/, '{,3}+', :possessive, :interval, 0, 3
35
+ include_examples 'quantifier', /a{4}b/, '{4}', :greedy, :interval, 4, 4
36
+ include_examples 'quantifier', /a{4}?b/, '{4}?', :reluctant, :interval, 4, 4
37
+ include_examples 'quantifier', /a{4}+b/, '{4}+', :possessive, :interval, 4, 4
38
+
39
+ specify('mode-checking methods') do
40
+ exp = RP.parse(/a??/).first
41
+
42
+ expect(exp).to be_reluctant
43
+ expect(exp).to be_lazy
44
+ expect(exp).not_to be_greedy
45
+ expect(exp).not_to be_possessive
46
+ expect(exp.quantifier).to be_reluctant
47
+ expect(exp.quantifier).to be_lazy
48
+ expect(exp.quantifier).not_to be_greedy
49
+ expect(exp.quantifier).not_to be_possessive
50
+ end
51
+ end
@@ -0,0 +1,112 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Refcall parsing') do
4
+ include_examples 'parse', /(abc)\1/,
5
+ 1 => [:backref, :number, Backreference::Number, number: 1]
6
+
7
+ include_examples 'parse', /(?<X>abc)\k<X>/,
8
+ 1 => [:backref, :name_ref, Backreference::Name, name: 'X']
9
+ include_examples 'parse', /(?<X>abc)\k'X'/,
10
+ 1 => [:backref, :name_ref, Backreference::Name, name: 'X']
11
+
12
+ include_examples 'parse', /(abc)\k<1>/,
13
+ 1 => [:backref, :number_ref, Backreference::Number, number: 1]
14
+ include_examples 'parse', /(abc)\k'1'/,
15
+ 1 => [:backref, :number_ref, Backreference::Number, number: 1]
16
+
17
+ include_examples 'parse', /(abc)\k<-1>/,
18
+ 1 => [:backref, :number_rel_ref, Backreference::NumberRelative, number: -1]
19
+ include_examples 'parse', /(abc)\k'-1'/,
20
+ 1 => [:backref, :number_rel_ref, Backreference::NumberRelative, number: -1]
21
+
22
+ include_examples 'parse', /(?<X>abc)\g<X>/,
23
+ 1 => [:backref, :name_call, Backreference::NameCall, name: 'X']
24
+ include_examples 'parse', /(?<X>abc)\g'X'/,
25
+ 1 => [:backref, :name_call, Backreference::NameCall, name: 'X']
26
+
27
+ include_examples 'parse', /(abc)\g<1>/,
28
+ 1 => [:backref, :number_call, Backreference::NumberCall, number: 1]
29
+ include_examples 'parse', /(abc)\g'1'/,
30
+ 1 => [:backref, :number_call, Backreference::NumberCall, number: 1]
31
+
32
+ include_examples 'parse', /(abc)\g<-1>/,
33
+ 1 => [:backref, :number_rel_call, Backreference::NumberCallRelative, number: -1]
34
+ include_examples 'parse', /(abc)\g'-1'/,
35
+ 1 => [:backref, :number_rel_call, Backreference::NumberCallRelative, number: -1]
36
+
37
+ include_examples 'parse', /\g<+1>(abc)/,
38
+ 0 => [:backref, :number_rel_call, Backreference::NumberCallRelative, number: 1]
39
+ include_examples 'parse', /\g'+1'(abc)/,
40
+ 0 => [:backref, :number_rel_call, Backreference::NumberCallRelative, number: 1]
41
+
42
+ include_examples 'parse', /(?<X>abc)\k<X-0>/,
43
+ 1 => [:backref, :name_recursion_ref, Backreference::NameRecursionLevel,
44
+ name: 'X', recursion_level: 0]
45
+ include_examples 'parse', /(?<X>abc)\k'X-0'/,
46
+ 1 => [:backref, :name_recursion_ref, Backreference::NameRecursionLevel,
47
+ name: 'X', recursion_level: 0]
48
+
49
+ include_examples 'parse', /(abc)\k<1-0>/,
50
+ 1 => [:backref, :number_recursion_ref, Backreference::NumberRecursionLevel,
51
+ number: 1, recursion_level: 0]
52
+ include_examples 'parse', /(abc)\k'1-0'/,
53
+ 1 => [:backref, :number_recursion_ref, Backreference::NumberRecursionLevel,
54
+ number: 1, recursion_level: 0]
55
+ include_examples 'parse', /(abc)\k'-1+0'/,
56
+ 1 => [:backref, :number_recursion_ref, Backreference::NumberRecursionLevel,
57
+ number: -1, recursion_level: 0]
58
+ include_examples 'parse', /(abc)\k'1+1'/,
59
+ 1 => [:backref, :number_recursion_ref, Backreference::NumberRecursionLevel,
60
+ number: 1, recursion_level: 1]
61
+ include_examples 'parse', /(abc)\k'1-1'/,
62
+ 1 => [:backref, :number_recursion_ref, Backreference::NumberRecursionLevel,
63
+ number: 1, recursion_level: -1]
64
+
65
+ specify('parse backref effective_number') do
66
+ root = RP.parse('(abc)(def)\\k<-1>(ghi)\\k<-3>\\k<-1>', 'ruby/1.9')
67
+ exp1 = root[2]
68
+ exp2 = root[4]
69
+ exp3 = root[5]
70
+
71
+ expect([exp1, exp2, exp3]).to all be_instance_of(Backreference::NumberRelative)
72
+ expect(exp1.effective_number).to eq 2
73
+ expect(exp2.effective_number).to eq 1
74
+ expect(exp3.effective_number).to eq 3
75
+ end
76
+
77
+ specify('parse backref referenced_expression') do
78
+ root = RP.parse('(abc)(def)\\k<-1>(ghi)\\k<-3>\\k<-1>', 'ruby/1.9')
79
+ exp1 = root[2]
80
+ exp2 = root[4]
81
+ exp3 = root[5]
82
+
83
+ expect([exp1, exp2, exp3]).to all be_instance_of(Backreference::NumberRelative)
84
+ expect(exp1.referenced_expression.to_s).to eq '(def)'
85
+ expect(exp2.referenced_expression.to_s).to eq '(abc)'
86
+ expect(exp3.referenced_expression.to_s).to eq '(ghi)'
87
+ end
88
+
89
+ specify('parse backref call effective_number') do
90
+ root = RP.parse('\\g<+1>(abc)\\g<+2>(def)(ghi)\\g<-2>', 'ruby/1.9')
91
+ exp1 = root[0]
92
+ exp2 = root[2]
93
+ exp3 = root[5]
94
+
95
+ expect([exp1, exp2, exp3]).to all be_instance_of(Backreference::NumberCallRelative)
96
+ expect(exp1.effective_number).to eq 1
97
+ expect(exp2.effective_number).to eq 3
98
+ expect(exp3.effective_number).to eq 2
99
+ end
100
+
101
+ specify('parse backref call referenced_expression') do
102
+ root = RP.parse('\\g<+1>(abc)\\g<+2>(def)(ghi)\\g<-2>', 'ruby/1.9')
103
+ exp1 = root[0]
104
+ exp2 = root[2]
105
+ exp3 = root[5]
106
+
107
+ expect([exp1, exp2, exp3]).to all be_instance_of(Backreference::NumberCallRelative)
108
+ expect(exp1.referenced_expression.to_s).to eq '(abc)'
109
+ expect(exp2.referenced_expression.to_s).to eq '(ghi)'
110
+ expect(exp3.referenced_expression.to_s).to eq '(def)'
111
+ end
112
+ end
@@ -0,0 +1,127 @@
1
+ require 'spec_helper'
2
+
3
+ # edge cases with `...-&&...` and `...&&-...` are checked in test_ranges.rb
4
+
5
+ RSpec.describe('CharacterSet::Intersection parsing') do
6
+ specify('parse set intersection') do
7
+ root = RP.parse('[a&&z]')
8
+ set = root[0]
9
+ ints = set[0]
10
+
11
+ expect(set.count).to eq 1
12
+ expect(ints).to be_instance_of(CharacterSet::Intersection)
13
+ expect(ints.count).to eq 2
14
+
15
+ seq1, seq2 = ints.expressions
16
+ expect(seq1).to be_instance_of(CharacterSet::IntersectedSequence)
17
+ expect(seq1.count).to eq 1
18
+ expect(seq1.first.to_s).to eq 'a'
19
+ expect(seq1.first).to be_instance_of(Literal)
20
+ expect(seq2).to be_instance_of(CharacterSet::IntersectedSequence)
21
+ expect(seq2.count).to eq 1
22
+ expect(seq2.first.to_s).to eq 'z'
23
+ expect(seq2.first).to be_instance_of(Literal)
24
+
25
+ expect(set).not_to match 'a'
26
+ expect(set).not_to match '&'
27
+ expect(set).not_to match 'z'
28
+ end
29
+
30
+ specify('parse set intersection range and subset') do
31
+ root = RP.parse('[a-z&&[^a]]')
32
+ set = root[0]
33
+ ints = set[0]
34
+
35
+ expect(set.count).to eq 1
36
+ expect(ints).to be_instance_of(CharacterSet::Intersection)
37
+ expect(ints.count).to eq 2
38
+
39
+ seq1, seq2 = ints.expressions
40
+ expect(seq1).to be_instance_of(CharacterSet::IntersectedSequence)
41
+ expect(seq1.count).to eq 1
42
+ expect(seq1.first.to_s).to eq 'a-z'
43
+ expect(seq1.first).to be_instance_of(CharacterSet::Range)
44
+ expect(seq2).to be_instance_of(CharacterSet::IntersectedSequence)
45
+ expect(seq2.count).to eq 1
46
+ expect(seq2.first.to_s).to eq '[^a]'
47
+ expect(seq2.first).to be_instance_of(CharacterSet)
48
+
49
+ expect(set).not_to match 'a'
50
+ expect(set).not_to match '&'
51
+ expect(set).to match 'b'
52
+ end
53
+
54
+ specify('parse set intersection trailing range') do
55
+ root = RP.parse('[a&&a-z]')
56
+ set = root[0]
57
+ ints = set[0]
58
+
59
+ expect(set.count).to eq 1
60
+ expect(ints).to be_instance_of(CharacterSet::Intersection)
61
+ expect(ints.count).to eq 2
62
+
63
+ seq1, seq2 = ints.expressions
64
+ expect(seq1).to be_instance_of(CharacterSet::IntersectedSequence)
65
+ expect(seq1.count).to eq 1
66
+ expect(seq1.first.to_s).to eq 'a'
67
+ expect(seq1.first).to be_instance_of(Literal)
68
+ expect(seq2).to be_instance_of(CharacterSet::IntersectedSequence)
69
+ expect(seq2.count).to eq 1
70
+ expect(seq2.first.to_s).to eq 'a-z'
71
+ expect(seq2.first).to be_instance_of(CharacterSet::Range)
72
+
73
+ expect(set).to match 'a'
74
+ expect(set).not_to match '&'
75
+ expect(set).not_to match 'b'
76
+ end
77
+
78
+ specify('parse set intersection type') do
79
+ root = RP.parse('[a&&\\w]')
80
+ set = root[0]
81
+ ints = set[0]
82
+
83
+ expect(set.count).to eq 1
84
+ expect(ints).to be_instance_of(CharacterSet::Intersection)
85
+ expect(ints.count).to eq 2
86
+
87
+ seq1, seq2 = ints.expressions
88
+ expect(seq1).to be_instance_of(CharacterSet::IntersectedSequence)
89
+ expect(seq1.count).to eq 1
90
+ expect(seq1.first.to_s).to eq 'a'
91
+ expect(seq1.first).to be_instance_of(Literal)
92
+ expect(seq2).to be_instance_of(CharacterSet::IntersectedSequence)
93
+ expect(seq2.count).to eq 1
94
+ expect(seq2.first.to_s).to eq '\\w'
95
+ expect(seq2.first).to be_instance_of(CharacterType::Word)
96
+
97
+ expect(set).to match 'a'
98
+ expect(set).not_to match '&'
99
+ expect(set).not_to match 'b'
100
+ end
101
+
102
+ specify('parse set intersection multipart') do
103
+ root = RP.parse('[\\h&&\\w&&efg]')
104
+ set = root[0]
105
+ ints = set[0]
106
+
107
+ expect(set.count).to eq 1
108
+ expect(ints).to be_instance_of(CharacterSet::Intersection)
109
+ expect(ints.count).to eq 3
110
+
111
+ seq1, seq2, seq3 = ints.expressions
112
+ expect(seq1).to be_instance_of(CharacterSet::IntersectedSequence)
113
+ expect(seq1.count).to eq 1
114
+ expect(seq1.first.to_s).to eq '\\h'
115
+ expect(seq2).to be_instance_of(CharacterSet::IntersectedSequence)
116
+ expect(seq2.count).to eq 1
117
+ expect(seq2.first.to_s).to eq '\\w'
118
+ expect(seq3).to be_instance_of(CharacterSet::IntersectedSequence)
119
+ expect(seq3.count).to eq 3
120
+ expect(seq3.to_s).to eq 'efg'
121
+
122
+ expect(set).to match 'e'
123
+ expect(set).to match 'f'
124
+ expect(set).not_to match 'a'
125
+ expect(set).not_to match 'g'
126
+ end
127
+ end