regexp_parser 1.4.0 → 1.5.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (133) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +27 -1
  3. data/Gemfile +1 -1
  4. data/README.md +9 -13
  5. data/lib/regexp_parser/expression.rb +33 -21
  6. data/lib/regexp_parser/expression/classes/backref.rb +18 -10
  7. data/lib/regexp_parser/expression/classes/conditional.rb +4 -0
  8. data/lib/regexp_parser/expression/classes/group.rb +4 -2
  9. data/lib/regexp_parser/expression/classes/keep.rb +1 -3
  10. data/lib/regexp_parser/expression/methods/match_length.rb +172 -0
  11. data/lib/regexp_parser/expression/quantifier.rb +2 -2
  12. data/lib/regexp_parser/expression/sequence.rb +0 -4
  13. data/lib/regexp_parser/expression/subexpression.rb +3 -5
  14. data/lib/regexp_parser/lexer.rb +31 -24
  15. data/lib/regexp_parser/parser.rb +25 -3
  16. data/lib/regexp_parser/syntax/tokens.rb +2 -10
  17. data/lib/regexp_parser/version.rb +1 -1
  18. data/regexp_parser.gemspec +2 -2
  19. data/spec/expression/base_spec.rb +80 -0
  20. data/spec/expression/clone_spec.rb +120 -0
  21. data/spec/expression/conditional_spec.rb +89 -0
  22. data/spec/expression/free_space_spec.rb +27 -0
  23. data/spec/expression/methods/match_length_spec.rb +141 -0
  24. data/spec/expression/methods/strfregexp_spec.rb +224 -0
  25. data/spec/expression/methods/tests_spec.rb +97 -0
  26. data/spec/expression/methods/traverse_spec.rb +140 -0
  27. data/spec/expression/subexpression_spec.rb +50 -0
  28. data/spec/expression/to_h_spec.rb +26 -0
  29. data/spec/expression/to_s_spec.rb +100 -0
  30. data/spec/lexer/all_spec.rb +22 -0
  31. data/{test/lexer/test_conditionals.rb → spec/lexer/conditionals_spec.rb} +31 -35
  32. data/spec/lexer/escapes_spec.rb +38 -0
  33. data/spec/lexer/keep_spec.rb +22 -0
  34. data/{test/lexer/test_literals.rb → spec/lexer/literals_spec.rb} +20 -24
  35. data/{test/lexer/test_nesting.rb → spec/lexer/nesting_spec.rb} +11 -13
  36. data/spec/lexer/refcalls_spec.rb +54 -0
  37. data/spec/parser/all_spec.rb +31 -0
  38. data/spec/parser/alternation_spec.rb +88 -0
  39. data/{test/parser/test_anchors.rb → spec/parser/anchors_spec.rb} +7 -10
  40. data/spec/parser/conditionals_spec.rb +179 -0
  41. data/spec/parser/errors_spec.rb +51 -0
  42. data/spec/parser/escapes_spec.rb +132 -0
  43. data/spec/parser/free_space_spec.rb +130 -0
  44. data/spec/parser/groups_spec.rb +267 -0
  45. data/spec/parser/keep_spec.rb +19 -0
  46. data/spec/parser/posix_classes_spec.rb +27 -0
  47. data/spec/parser/properties_spec.rb +127 -0
  48. data/spec/parser/quantifiers_spec.rb +293 -0
  49. data/spec/parser/refcalls_spec.rb +237 -0
  50. data/spec/parser/set/intersections_spec.rb +127 -0
  51. data/spec/parser/set/ranges_spec.rb +111 -0
  52. data/spec/parser/sets_spec.rb +178 -0
  53. data/{test/parser/test_types.rb → spec/parser/types_spec.rb} +13 -20
  54. data/spec/scanner/all_spec.rb +18 -0
  55. data/{test/scanner/test_anchors.rb → spec/scanner/anchors_spec.rb} +8 -10
  56. data/{test/scanner/test_conditionals.rb → spec/scanner/conditionals_spec.rb} +49 -53
  57. data/spec/scanner/errors_spec.rb +90 -0
  58. data/{test/scanner/test_escapes.rb → spec/scanner/escapes_spec.rb} +8 -10
  59. data/{test/scanner/test_free_space.rb → spec/scanner/free_space_spec.rb} +48 -52
  60. data/{test/scanner/test_groups.rb → spec/scanner/groups_spec.rb} +33 -41
  61. data/spec/scanner/keep_spec.rb +33 -0
  62. data/{test/scanner/test_literals.rb → spec/scanner/literals_spec.rb} +8 -12
  63. data/{test/scanner/test_meta.rb → spec/scanner/meta_spec.rb} +8 -10
  64. data/{test/scanner/test_properties.rb → spec/scanner/properties_spec.rb} +14 -19
  65. data/{test/scanner/test_quantifiers.rb → spec/scanner/quantifiers_spec.rb} +7 -9
  66. data/{test/scanner/test_refcalls.rb → spec/scanner/refcalls_spec.rb} +9 -9
  67. data/{test/scanner/test_scripts.rb → spec/scanner/scripts_spec.rb} +8 -12
  68. data/{test/scanner/test_sets.rb → spec/scanner/sets_spec.rb} +14 -17
  69. data/spec/scanner/types_spec.rb +29 -0
  70. data/spec/scanner/unicode_blocks_spec.rb +28 -0
  71. data/spec/spec_helper.rb +14 -0
  72. data/{test → spec}/support/runner.rb +9 -8
  73. data/{test → spec}/support/warning_extractor.rb +5 -7
  74. data/spec/syntax/syntax_spec.rb +44 -0
  75. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  76. data/spec/syntax/versions/1.8.6_spec.rb +38 -0
  77. data/spec/syntax/versions/1.9.1_spec.rb +23 -0
  78. data/spec/syntax/versions/1.9.3_spec.rb +22 -0
  79. data/spec/syntax/versions/2.0.0_spec.rb +28 -0
  80. data/spec/syntax/versions/2.2.0_spec.rb +22 -0
  81. data/spec/syntax/versions/aliases_spec.rb +119 -0
  82. data/spec/token/token_spec.rb +85 -0
  83. metadata +131 -140
  84. data/test/expression/test_all.rb +0 -12
  85. data/test/expression/test_base.rb +0 -90
  86. data/test/expression/test_clone.rb +0 -89
  87. data/test/expression/test_conditionals.rb +0 -113
  88. data/test/expression/test_free_space.rb +0 -35
  89. data/test/expression/test_set.rb +0 -84
  90. data/test/expression/test_strfregexp.rb +0 -230
  91. data/test/expression/test_subexpression.rb +0 -58
  92. data/test/expression/test_tests.rb +0 -99
  93. data/test/expression/test_to_h.rb +0 -59
  94. data/test/expression/test_to_s.rb +0 -104
  95. data/test/expression/test_traverse.rb +0 -161
  96. data/test/helpers.rb +0 -10
  97. data/test/lexer/test_all.rb +0 -41
  98. data/test/lexer/test_keep.rb +0 -24
  99. data/test/lexer/test_refcalls.rb +0 -56
  100. data/test/parser/set/test_intersections.rb +0 -127
  101. data/test/parser/set/test_ranges.rb +0 -111
  102. data/test/parser/test_all.rb +0 -64
  103. data/test/parser/test_alternation.rb +0 -92
  104. data/test/parser/test_conditionals.rb +0 -187
  105. data/test/parser/test_errors.rb +0 -63
  106. data/test/parser/test_escapes.rb +0 -134
  107. data/test/parser/test_free_space.rb +0 -139
  108. data/test/parser/test_groups.rb +0 -289
  109. data/test/parser/test_keep.rb +0 -21
  110. data/test/parser/test_posix_classes.rb +0 -27
  111. data/test/parser/test_properties.rb +0 -134
  112. data/test/parser/test_quantifiers.rb +0 -301
  113. data/test/parser/test_refcalls.rb +0 -186
  114. data/test/parser/test_sets.rb +0 -179
  115. data/test/scanner/test_all.rb +0 -38
  116. data/test/scanner/test_errors.rb +0 -91
  117. data/test/scanner/test_keep.rb +0 -35
  118. data/test/scanner/test_types.rb +0 -35
  119. data/test/scanner/test_unicode_blocks.rb +0 -30
  120. data/test/support/disable_autotest.rb +0 -8
  121. data/test/syntax/test_all.rb +0 -6
  122. data/test/syntax/test_syntax.rb +0 -61
  123. data/test/syntax/test_syntax_token_map.rb +0 -25
  124. data/test/syntax/versions/test_1.8.rb +0 -55
  125. data/test/syntax/versions/test_1.9.1.rb +0 -36
  126. data/test/syntax/versions/test_1.9.3.rb +0 -32
  127. data/test/syntax/versions/test_2.0.0.rb +0 -37
  128. data/test/syntax/versions/test_2.2.0.rb +0 -32
  129. data/test/syntax/versions/test_aliases.rb +0 -129
  130. data/test/syntax/versions/test_all.rb +0 -5
  131. data/test/test_all.rb +0 -5
  132. data/test/token/test_all.rb +0 -2
  133. data/test/token/test_token.rb +0 -107
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Expression::FreeSpace) do
4
+ specify('white space quantify raises error') do
5
+ regexp = /
6
+ a # Comment
7
+ /x
8
+
9
+ root = RP.parse(regexp)
10
+ space = root[0]
11
+
12
+ expect(space).to be_instance_of(FreeSpace::WhiteSpace)
13
+ expect { space.quantify(:dummy, '#') }.to raise_error(RuntimeError)
14
+ end
15
+
16
+ specify('comment quantify raises error') do
17
+ regexp = /
18
+ a # Comment
19
+ /x
20
+
21
+ root = RP.parse(regexp)
22
+ comment = root[3]
23
+
24
+ expect(comment).to be_instance_of(FreeSpace::Comment)
25
+ expect { comment.quantify(:dummy, '#') }.to raise_error(RuntimeError)
26
+ end
27
+ end
@@ -0,0 +1,141 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::MatchLength) do
4
+ ML = described_class
5
+
6
+ specify('literal') { expect(ML.of(/a/).minmax).to eq [1, 1] }
7
+ specify('literal sequence') { expect(ML.of(/abc/).minmax).to eq [3, 3] }
8
+ specify('dot') { expect(ML.of(/./).minmax).to eq [1, 1] }
9
+ specify('set') { expect(ML.of(/[abc]/).minmax).to eq [1, 1] }
10
+ specify('type') { expect(ML.of(/\d/).minmax).to eq [1, 1] }
11
+ specify('escape') { expect(ML.of(/\n/).minmax).to eq [1, 1] }
12
+ specify('property') { expect(ML.of(/\p{ascii}/).minmax).to eq [1, 1] }
13
+ specify('codepoint list') { expect(ML.of(/\u{61 62 63}/).minmax).to eq [3, 3] }
14
+ specify('multi-char literal') { expect(ML.of(/abc/).minmax).to eq [3, 3] }
15
+ specify('fixed quantified') { expect(ML.of(/a{5}/).minmax).to eq [5, 5] }
16
+ specify('range quantified') { expect(ML.of(/a{5,9}/).minmax).to eq [5, 9] }
17
+ specify('nested quantified') { expect(ML.of(/(a{2}){3,4}/).minmax).to eq [6, 8] }
18
+ specify('open-end quantified') { expect(ML.of(/a*/).minmax).to eq [0, Float::INFINITY] }
19
+ specify('empty subexpression') { expect(ML.of(//).minmax).to eq [0, 0] }
20
+ specify('anchor') { expect(ML.of(/^$/).minmax).to eq [0, 0] }
21
+ specify('lookaround') { expect(ML.of(/(?=abc)/).minmax).to eq [0, 0] }
22
+ specify('free space') { expect(ML.of(/ /x).minmax).to eq [0, 0] }
23
+ specify('comment') { expect(ML.of(/(?#comment)/x).minmax).to eq [0, 0] }
24
+ specify('backreference') { expect(ML.of(/(abc){2}\1/).minmax).to eq [9, 9] }
25
+ specify('subexp call') { expect(ML.of(/(abc){2}\g<-1>/).minmax).to eq [9, 9] }
26
+ specify('alternation') { expect(ML.of(/a|bcde/).minmax).to eq [1, 4] }
27
+ specify('nested alternation') { expect(ML.of(/a|bc(d|efg)/).minmax).to eq [1, 5] }
28
+ specify('quantified alternation') { expect(ML.of(/a|bcde?/).minmax).to eq [1, 4] }
29
+ if ruby_version_at_least('2.4.1')
30
+ specify('absence group') { expect(ML.of('(?~abc)').minmax).to eq [0, Float::INFINITY] }
31
+ end
32
+
33
+ describe('::of') do
34
+ it('works with Regexps') { expect(ML.of(/foo/).minmax).to eq [3, 3] }
35
+ it('works with Strings') { expect(ML.of('foo').minmax).to eq [3, 3] }
36
+ it('works with Expressions') { expect(ML.of(RP.parse(/foo/)).minmax).to eq [3, 3] }
37
+ end
38
+
39
+ describe('Expression#match_length') do
40
+ it('returns the MatchLength') { expect(RP.parse(/abc/).match_length.minmax).to eq [3, 3] }
41
+ end
42
+
43
+ describe('Expression#inner_match_length') do
44
+ it 'returns the MatchLength of an expression that does not count towards parent match_length' do
45
+ exp = RP.parse(/(?=ab|cdef)/)[0]
46
+ expect(exp).to be_a Regexp::Expression::Assertion::Base
47
+ expect(exp.match_length.minmax).to eq [0, 0]
48
+ expect(exp.inner_match_length.minmax).to eq [2, 4]
49
+ end
50
+ end
51
+
52
+ describe('#include?') do
53
+ specify('unquantified') do
54
+ expect(ML.of(/a/)).to include 1
55
+ expect(ML.of(/a/)).not_to include 0
56
+ expect(ML.of(/a/)).not_to include 2
57
+ end
58
+
59
+ specify('fixed quantified') do
60
+ expect(ML.of(/a{5}/)).to include 5
61
+ expect(ML.of(/a{5}/)).not_to include 0
62
+ expect(ML.of(/a{5}/)).not_to include 4
63
+ expect(ML.of(/a{5}/)).not_to include 6
64
+ end
65
+
66
+ specify('variably quantified') do
67
+ expect(ML.of(/a?/)).to include 0
68
+ expect(ML.of(/a?/)).to include 1
69
+ expect(ML.of(/a?/)).not_to include 2
70
+ end
71
+
72
+ specify('nested quantified') do
73
+ expect(ML.of(/(a{2}){3,4}/)).to include 6
74
+ expect(ML.of(/(a{2}){3,4}/)).to include 8
75
+ expect(ML.of(/(a{2}){3,4}/)).not_to include 0
76
+ expect(ML.of(/(a{2}){3,4}/)).not_to include 5
77
+ expect(ML.of(/(a{2}){3,4}/)).not_to include 7
78
+ expect(ML.of(/(a{2}){3,4}/)).not_to include 9
79
+ end
80
+
81
+ specify('branches') do
82
+ expect(ML.of(/ab|cdef/)).to include 2
83
+ expect(ML.of(/ab|cdef/)).to include 4
84
+ expect(ML.of(/ab|cdef/)).not_to include 0
85
+ expect(ML.of(/ab|cdef/)).not_to include 3
86
+ expect(ML.of(/ab|cdef/)).not_to include 5
87
+ end
88
+
89
+ specify('called on leaf node') do
90
+ expect(ML.of(RP.parse(/a{2}/)[0])).to include 2
91
+ expect(ML.of(RP.parse(/a{2}/)[0])).not_to include 0
92
+ expect(ML.of(RP.parse(/a{2}/)[0])).not_to include 1
93
+ expect(ML.of(RP.parse(/a{2}/)[0])).not_to include 3
94
+ end
95
+ end
96
+
97
+ describe('#fixed?') do
98
+ specify('unquantified') { expect(ML.of(/a/)).to be_fixed }
99
+ specify('fixed quantified') { expect(ML.of(/a{5}/)).to be_fixed }
100
+ specify('variably quantified') { expect(ML.of(/a?/)).not_to be_fixed }
101
+ specify('equal branches') { expect(ML.of(/ab|cd/)).to be_fixed }
102
+ specify('unequal branches') { expect(ML.of(/ab|cdef/)).not_to be_fixed }
103
+ specify('equal quantified branches') { expect(ML.of(/a{2}|cd/)).to be_fixed }
104
+ specify('unequal quantified branches') { expect(ML.of(/a{3}|cd/)).not_to be_fixed }
105
+ specify('empty') { expect(ML.of(//)).to be_fixed }
106
+ end
107
+
108
+ describe('#each') do
109
+ it 'returns an Enumerator if called without a block' do
110
+ result = ML.of(/a?/).each
111
+ expect(result).to be_a(Enumerator)
112
+ expect(result.next).to eq 0
113
+ expect(result.next).to eq 1
114
+ expect { result.next }.to raise_error(StopIteration)
115
+ end
116
+
117
+ it 'is limited to 1000 iterations in case there are infinite match lengths' do
118
+ expect(ML.of(/a*/).first(3000).size).to eq 1000
119
+ end
120
+
121
+ it 'scaffolds the Enumerable interface' do
122
+ expect(ML.of(/abc|defg/).count).to eq 2
123
+ expect(ML.of(/(ab)*/).first(5)).to eq [0, 2, 4, 6, 8]
124
+ expect(ML.of(/a{,10}/).any? { |len| len > 20 }).to be false
125
+ end
126
+ end
127
+
128
+ describe('#endless_each') do
129
+ it 'returns an Enumerator if called without a block' do
130
+ result = ML.of(/a?/).endless_each
131
+ expect(result).to be_a(Enumerator)
132
+ expect(result.next).to eq 0
133
+ expect(result.next).to eq 1
134
+ expect { result.next }.to raise_error(StopIteration)
135
+ end
136
+
137
+ it 'never stops iterating for infinite match lengths' do
138
+ expect(ML.of(/a*/).endless_each.first(3000).size).to eq 3000
139
+ end
140
+ end
141
+ end
@@ -0,0 +1,224 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Expression#strfregexp') do
4
+ specify('#strfre alias') do
5
+ expect(RP.parse(/a/)).to respond_to(:strfre)
6
+ end
7
+
8
+ specify('#strfregexp level') do
9
+ root = RP.parse(/a(b(c))/)
10
+
11
+ expect(root.strfregexp('%l')).to eq 'root'
12
+
13
+ a = root.first
14
+ expect(a.strfregexp('%%l')).to eq '%0'
15
+
16
+ b = root[1].first
17
+ expect(b.strfregexp('<%l>')).to eq '<1>'
18
+
19
+ c = root[1][1].first
20
+ expect(c.strfregexp('[at: %l]')).to eq '[at: 2]'
21
+ end
22
+
23
+ specify('#strfregexp start end') do
24
+ root = RP.parse(/a(b(c))/)
25
+
26
+ expect(root.strfregexp('%s')).to eq '0'
27
+ expect(root.strfregexp('%e')).to eq '7'
28
+
29
+ a = root.first
30
+ expect(a.strfregexp('%%s')).to eq '%0'
31
+ expect(a.strfregexp('%e')).to eq '1'
32
+
33
+ group_1 = root[1]
34
+ expect(group_1.strfregexp('GRP:%s')).to eq 'GRP:1'
35
+ expect(group_1.strfregexp('%e')).to eq '7'
36
+
37
+ b = group_1.first
38
+ expect(b.strfregexp('<@%s>')).to eq '<@2>'
39
+ expect(b.strfregexp('%e')).to eq '3'
40
+
41
+ c = group_1.last.first
42
+ expect(c.strfregexp('[at: %s]')).to eq '[at: 4]'
43
+ expect(c.strfregexp('%e')).to eq '5'
44
+ end
45
+
46
+ specify('#strfregexp length') do
47
+ root = RP.parse(/a[b]c/)
48
+
49
+ expect(root.strfregexp('%S')).to eq '5'
50
+
51
+ a = root.first
52
+ expect(a.strfregexp('%S')).to eq '1'
53
+
54
+ set = root[1]
55
+ expect(set.strfregexp('%S')).to eq '3'
56
+ end
57
+
58
+ specify('#strfregexp coded offset') do
59
+ root = RP.parse(/a[b]c/)
60
+
61
+ expect(root.strfregexp('%o')).to eq '@0+5'
62
+
63
+ a = root.first
64
+ expect(a.strfregexp('%o')).to eq '@0+1'
65
+
66
+ set = root[1]
67
+ expect(set.strfregexp('%o')).to eq '@1+3'
68
+ end
69
+
70
+ specify('#strfregexp type token') do
71
+ root = RP.parse(/a[b](c)/)
72
+
73
+ expect(root.strfregexp('%y')).to eq 'expression'
74
+ expect(root.strfregexp('%k')).to eq 'root'
75
+ expect(root.strfregexp('%i')).to eq 'expression:root'
76
+ expect(root.strfregexp('%c')).to eq 'Regexp::Expression::Root'
77
+
78
+ a = root.first
79
+ expect(a.strfregexp('%y')).to eq 'literal'
80
+ expect(a.strfregexp('%k')).to eq 'literal'
81
+ expect(a.strfregexp('%i')).to eq 'literal:literal'
82
+ expect(a.strfregexp('%c')).to eq 'Regexp::Expression::Literal'
83
+
84
+ set = root[1]
85
+ expect(set.strfregexp('%y')).to eq 'set'
86
+ expect(set.strfregexp('%k')).to eq 'character'
87
+ expect(set.strfregexp('%i')).to eq 'set:character'
88
+ expect(set.strfregexp('%c')).to eq 'Regexp::Expression::CharacterSet'
89
+
90
+ group = root.last
91
+ expect(group.strfregexp('%y')).to eq 'group'
92
+ expect(group.strfregexp('%k')).to eq 'capture'
93
+ expect(group.strfregexp('%i')).to eq 'group:capture'
94
+ expect(group.strfregexp('%c')).to eq 'Regexp::Expression::Group::Capture'
95
+ end
96
+
97
+ specify('#strfregexp quantifier') do
98
+ root = RP.parse(/a+[b](c)?d{3,4}/)
99
+
100
+ expect(root.strfregexp('%q')).to eq '{1}'
101
+ expect(root.strfregexp('%Q')).to eq ''
102
+ expect(root.strfregexp('%z, %Z')).to eq '1, 1'
103
+
104
+ a = root.first
105
+ expect(a.strfregexp('%q')).to eq '{1, or-more}'
106
+ expect(a.strfregexp('%Q')).to eq '+'
107
+ expect(a.strfregexp('%z, %Z')).to eq '1, -1'
108
+
109
+ set = root[1]
110
+ expect(set.strfregexp('%q')).to eq '{1}'
111
+ expect(set.strfregexp('%Q')).to eq ''
112
+ expect(set.strfregexp('%z, %Z')).to eq '1, 1'
113
+
114
+ group = root[2]
115
+ expect(group.strfregexp('%q')).to eq '{0, 1}'
116
+ expect(group.strfregexp('%Q')).to eq '?'
117
+ expect(group.strfregexp('%z, %Z')).to eq '0, 1'
118
+
119
+ d = root.last
120
+ expect(d.strfregexp('%q')).to eq '{3, 4}'
121
+ expect(d.strfregexp('%Q')).to eq '{3,4}'
122
+ expect(d.strfregexp('%z, %Z')).to eq '3, 4'
123
+ end
124
+
125
+ specify('#strfregexp text') do
126
+ root = RP.parse(/a(b(c))|[d-gk-p]+/)
127
+
128
+ expect(root.strfregexp('%t')).to eq 'a(b(c))|[d-gk-p]+'
129
+ expect(root.strfregexp('%~t')).to eq 'expression:root'
130
+
131
+ alt = root.first
132
+ expect(alt.strfregexp('%t')).to eq 'a(b(c))|[d-gk-p]+'
133
+ expect(alt.strfregexp('%T')).to eq 'a(b(c))|[d-gk-p]+'
134
+ expect(alt.strfregexp('%~t')).to eq 'meta:alternation'
135
+
136
+ seq_1 = alt.first
137
+ expect(seq_1.strfregexp('%t')).to eq 'a(b(c))'
138
+ expect(seq_1.strfregexp('%T')).to eq 'a(b(c))'
139
+ expect(seq_1.strfregexp('%~t')).to eq 'expression:sequence'
140
+
141
+ group = seq_1[1]
142
+ expect(group.strfregexp('%t')).to eq '(b(c))'
143
+ expect(group.strfregexp('%T')).to eq '(b(c))'
144
+ expect(group.strfregexp('%~t')).to eq 'group:capture'
145
+
146
+ seq_2 = alt.last
147
+ expect(seq_2.strfregexp('%t')).to eq '[d-gk-p]+'
148
+ expect(seq_2.strfregexp('%T')).to eq '[d-gk-p]+'
149
+
150
+ set = seq_2.first
151
+ expect(set.strfregexp('%t')).to eq '[d-gk-p]'
152
+ expect(set.strfregexp('%T')).to eq '[d-gk-p]+'
153
+ expect(set.strfregexp('%~t')).to eq 'set:character'
154
+ end
155
+
156
+ specify('#strfregexp combined') do
157
+ root = RP.parse(/a{5}|[b-d]+/)
158
+
159
+ expect(root.strfregexp('%b')).to eq '@0+11 expression:root'
160
+ expect(root.strfregexp('%b')).to eq root.strfregexp('%o %i')
161
+
162
+ expect(root.strfregexp('%m')).to eq '@0+11 expression:root {1}'
163
+ expect(root.strfregexp('%m')).to eq root.strfregexp('%b %q')
164
+
165
+ expect(root.strfregexp('%a')).to eq '@0+11 expression:root {1} a{5}|[b-d]+'
166
+ expect(root.strfregexp('%a')).to eq root.strfregexp('%m %t')
167
+ end
168
+
169
+ specify('#strfregexp conditional') do
170
+ root = RP.parse('(?<A>a)(?(<A>)b|c)', 'ruby/2.0')
171
+
172
+ expect { root.strfregexp }.not_to(raise_error)
173
+ end
174
+
175
+ specify('#strfregexp_tree') do
176
+ root = RP.parse(/a[b-d]*(e(f+))?/)
177
+
178
+ expect(root.strfregexp_tree('%>%o %~t')).to eq(
179
+ "@0+15 expression:root\n" +
180
+ " @0+1 a\n" +
181
+ " @1+6 set:character\n" +
182
+ " @2+3 set:range\n" +
183
+ " @2+1 b\n" +
184
+ " @4+1 d\n" +
185
+ " @7+8 group:capture\n" +
186
+ " @8+1 e\n" +
187
+ " @9+4 group:capture\n" +
188
+ " @10+2 f+"
189
+ )
190
+ end
191
+
192
+ specify('#strfregexp_tree separator') do
193
+ root = RP.parse(/a[b-d]*(e(f+))?/)
194
+
195
+ expect(root.strfregexp_tree('%>%o %~t', true, '-SEP-')).to eq(
196
+ "@0+15 expression:root-SEP-" +
197
+ " @0+1 a-SEP-" +
198
+ " @1+6 set:character-SEP-" +
199
+ " @2+3 set:range-SEP-" +
200
+ " @2+1 b-SEP-" +
201
+ " @4+1 d-SEP-" +
202
+ " @7+8 group:capture-SEP-" +
203
+ " @8+1 e-SEP-" +
204
+ " @9+4 group:capture-SEP-" +
205
+ " @10+2 f+"
206
+ )
207
+ end
208
+
209
+ specify('#strfregexp_tree excluding self') do
210
+ root = RP.parse(/a[b-d]*(e(f+))?/)
211
+
212
+ expect(root.strfregexp_tree('%>%o %~t', false)).to eq(
213
+ "@0+1 a\n" +
214
+ "@1+6 set:character\n" +
215
+ " @2+3 set:range\n" +
216
+ " @2+1 b\n" +
217
+ " @4+1 d\n" +
218
+ "@7+8 group:capture\n" +
219
+ " @8+1 e\n" +
220
+ " @9+4 group:capture\n" +
221
+ " @10+2 f+"
222
+ )
223
+ end
224
+ end
@@ -0,0 +1,97 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('ExpressionTests') do
4
+ specify('#type?') do
5
+ root = RP.parse(/abcd|(ghij)|[klmn]/)
6
+
7
+ alt = root.first
8
+
9
+ expect(alt.type?(:meta)).to be true
10
+ expect(alt.type?(:escape)).to be false
11
+ expect(alt.type?(%i[meta escape])).to be true
12
+ expect(alt.type?(%i[literal escape])).to be false
13
+ expect(alt.type?(:*)).to be true
14
+ expect(alt.type?([:*])).to be true
15
+ expect(alt.type?(%i[literal escape *])).to be true
16
+
17
+ seq_1 = alt[0]
18
+ expect(seq_1.type?(:expression)).to be true
19
+ expect(seq_1.first.type?(:literal)).to be true
20
+
21
+ seq_2 = alt[1]
22
+ expect(seq_2.type?(:*)).to be true
23
+ expect(seq_2.first.type?(:group)).to be true
24
+
25
+ seq_3 = alt[2]
26
+ expect(seq_3.first.type?(:set)).to be true
27
+ end
28
+
29
+ specify('#is?') do
30
+ root = RP.parse(/.+|\.?/)
31
+
32
+ expect(root.is?(:*)).to be true
33
+
34
+ alt = root.first
35
+ expect(alt.is?(:*)).to be true
36
+ expect(alt.is?(:alternation)).to be true
37
+ expect(alt.is?(:alternation, :meta)).to be true
38
+
39
+ seq_1 = alt[0]
40
+ expect(seq_1.is?(:sequence)).to be true
41
+ expect(seq_1.is?(:sequence, :expression)).to be true
42
+
43
+ expect(seq_1.first.is?(:dot)).to be true
44
+ expect(seq_1.first.is?(:dot, :escape)).to be false
45
+ expect(seq_1.first.is?(:dot, :meta)).to be true
46
+ expect(seq_1.first.is?(:dot, %i[escape meta])).to be true
47
+
48
+ seq_2 = alt[1]
49
+ expect(seq_2.first.is?(:dot)).to be true
50
+ expect(seq_2.first.is?(:dot, :escape)).to be true
51
+ expect(seq_2.first.is?(:dot, :meta)).to be false
52
+ expect(seq_2.first.is?(:dot, %i[meta escape])).to be true
53
+ end
54
+
55
+ specify('#one_of?') do
56
+ root = RP.parse(/\Aab(c[\w])d|e.\z/)
57
+
58
+ expect(root.one_of?(:*)).to be true
59
+ expect(root.one_of?(:* => :*)).to be true
60
+ expect(root.one_of?(:* => [:*])).to be true
61
+
62
+ alt = root.first
63
+ expect(alt.one_of?(:*)).to be true
64
+ expect(alt.one_of?(:meta)).to be true
65
+ expect(alt.one_of?(:meta, :alternation)).to be true
66
+ expect(alt.one_of?(meta: %i[dot bogus])).to be false
67
+ expect(alt.one_of?(meta: %i[dot alternation])).to be true
68
+
69
+ seq_1 = alt[0]
70
+ expect(seq_1.one_of?(:expression)).to be true
71
+ expect(seq_1.one_of?(expression: :sequence)).to be true
72
+
73
+ expect(seq_1.first.one_of?(:anchor)).to be true
74
+ expect(seq_1.first.one_of?(anchor: :bos)).to be true
75
+ expect(seq_1.first.one_of?(anchor: :eos)).to be false
76
+ expect(seq_1.first.one_of?(anchor: %i[escape meta bos])).to be true
77
+ expect(seq_1.first.one_of?(anchor: %i[escape meta eos])).to be false
78
+
79
+ seq_2 = alt[1]
80
+ expect(seq_2.first.one_of?(:literal)).to be true
81
+
82
+ expect(seq_2[1].one_of?(:meta)).to be true
83
+ expect(seq_2[1].one_of?(meta: :dot)).to be true
84
+ expect(seq_2[1].one_of?(meta: :alternation)).to be false
85
+ expect(seq_2[1].one_of?(meta: [:dot])).to be true
86
+
87
+ expect(seq_2.last.one_of?(:group)).to be false
88
+ expect(seq_2.last.one_of?(group: [:*])).to be false
89
+ expect(seq_2.last.one_of?(group: [:*], meta: :*)).to be false
90
+
91
+ expect(seq_2.last.one_of?(:meta => [:*], :* => :*)).to be true
92
+ expect(seq_2.last.one_of?(meta: [:*], anchor: :*)).to be true
93
+ expect(seq_2.last.one_of?(meta: [:*], anchor: :eos)).to be true
94
+ expect(seq_2.last.one_of?(meta: [:*], anchor: [:bos])).to be false
95
+ expect(seq_2.last.one_of?(meta: [:*], anchor: %i[bos eos])).to be true
96
+ end
97
+ end