regexp_parser 1.3.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (172) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +72 -1
  3. data/Gemfile +3 -3
  4. data/README.md +12 -19
  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 +1228 -1367
  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 +101 -194
  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/delimiters_spec.rb +68 -0
  54. data/spec/lexer/escapes_spec.rb +14 -0
  55. data/spec/lexer/keep_spec.rb +10 -0
  56. data/spec/lexer/literals_spec.rb +89 -0
  57. data/spec/lexer/nesting_spec.rb +99 -0
  58. data/spec/lexer/refcalls_spec.rb +55 -0
  59. data/spec/parser/all_spec.rb +43 -0
  60. data/spec/parser/alternation_spec.rb +88 -0
  61. data/spec/parser/anchors_spec.rb +17 -0
  62. data/spec/parser/conditionals_spec.rb +179 -0
  63. data/spec/parser/errors_spec.rb +30 -0
  64. data/spec/parser/escapes_spec.rb +121 -0
  65. data/spec/parser/free_space_spec.rb +130 -0
  66. data/spec/parser/groups_spec.rb +108 -0
  67. data/spec/parser/keep_spec.rb +6 -0
  68. data/spec/parser/posix_classes_spec.rb +8 -0
  69. data/spec/parser/properties_spec.rb +115 -0
  70. data/spec/parser/quantifiers_spec.rb +52 -0
  71. data/spec/parser/refcalls_spec.rb +112 -0
  72. data/spec/parser/set/intersections_spec.rb +127 -0
  73. data/spec/parser/set/ranges_spec.rb +111 -0
  74. data/spec/parser/sets_spec.rb +178 -0
  75. data/spec/parser/types_spec.rb +18 -0
  76. data/spec/scanner/all_spec.rb +18 -0
  77. data/spec/scanner/anchors_spec.rb +21 -0
  78. data/spec/scanner/conditionals_spec.rb +128 -0
  79. data/spec/scanner/delimiters_spec.rb +52 -0
  80. data/spec/scanner/errors_spec.rb +67 -0
  81. data/spec/scanner/escapes_spec.rb +53 -0
  82. data/spec/scanner/free_space_spec.rb +133 -0
  83. data/spec/scanner/groups_spec.rb +52 -0
  84. data/spec/scanner/keep_spec.rb +10 -0
  85. data/spec/scanner/literals_spec.rb +49 -0
  86. data/spec/scanner/meta_spec.rb +18 -0
  87. data/spec/scanner/properties_spec.rb +64 -0
  88. data/spec/scanner/quantifiers_spec.rb +20 -0
  89. data/spec/scanner/refcalls_spec.rb +36 -0
  90. data/spec/scanner/sets_spec.rb +102 -0
  91. data/spec/scanner/types_spec.rb +14 -0
  92. data/spec/spec_helper.rb +15 -0
  93. data/{test → spec}/support/runner.rb +9 -8
  94. data/spec/support/shared_examples.rb +77 -0
  95. data/{test → spec}/support/warning_extractor.rb +5 -7
  96. data/spec/syntax/syntax_spec.rb +48 -0
  97. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  98. data/spec/syntax/versions/1.8.6_spec.rb +17 -0
  99. data/spec/syntax/versions/1.9.1_spec.rb +10 -0
  100. data/spec/syntax/versions/1.9.3_spec.rb +9 -0
  101. data/spec/syntax/versions/2.0.0_spec.rb +13 -0
  102. data/spec/syntax/versions/2.2.0_spec.rb +9 -0
  103. data/spec/syntax/versions/aliases_spec.rb +37 -0
  104. data/spec/token/token_spec.rb +85 -0
  105. metadata +151 -146
  106. data/test/expression/test_all.rb +0 -12
  107. data/test/expression/test_base.rb +0 -90
  108. data/test/expression/test_clone.rb +0 -89
  109. data/test/expression/test_conditionals.rb +0 -113
  110. data/test/expression/test_free_space.rb +0 -35
  111. data/test/expression/test_set.rb +0 -84
  112. data/test/expression/test_strfregexp.rb +0 -230
  113. data/test/expression/test_subexpression.rb +0 -58
  114. data/test/expression/test_tests.rb +0 -99
  115. data/test/expression/test_to_h.rb +0 -59
  116. data/test/expression/test_to_s.rb +0 -104
  117. data/test/expression/test_traverse.rb +0 -161
  118. data/test/helpers.rb +0 -10
  119. data/test/lexer/test_all.rb +0 -41
  120. data/test/lexer/test_conditionals.rb +0 -127
  121. data/test/lexer/test_keep.rb +0 -24
  122. data/test/lexer/test_literals.rb +0 -130
  123. data/test/lexer/test_nesting.rb +0 -132
  124. data/test/lexer/test_refcalls.rb +0 -56
  125. data/test/parser/set/test_intersections.rb +0 -127
  126. data/test/parser/set/test_ranges.rb +0 -111
  127. data/test/parser/test_all.rb +0 -64
  128. data/test/parser/test_alternation.rb +0 -92
  129. data/test/parser/test_anchors.rb +0 -34
  130. data/test/parser/test_conditionals.rb +0 -187
  131. data/test/parser/test_errors.rb +0 -63
  132. data/test/parser/test_escapes.rb +0 -134
  133. data/test/parser/test_free_space.rb +0 -139
  134. data/test/parser/test_groups.rb +0 -289
  135. data/test/parser/test_keep.rb +0 -21
  136. data/test/parser/test_posix_classes.rb +0 -27
  137. data/test/parser/test_properties.rb +0 -133
  138. data/test/parser/test_quantifiers.rb +0 -301
  139. data/test/parser/test_refcalls.rb +0 -186
  140. data/test/parser/test_sets.rb +0 -179
  141. data/test/parser/test_types.rb +0 -50
  142. data/test/scanner/test_all.rb +0 -38
  143. data/test/scanner/test_anchors.rb +0 -38
  144. data/test/scanner/test_conditionals.rb +0 -184
  145. data/test/scanner/test_errors.rb +0 -91
  146. data/test/scanner/test_escapes.rb +0 -56
  147. data/test/scanner/test_free_space.rb +0 -200
  148. data/test/scanner/test_groups.rb +0 -79
  149. data/test/scanner/test_keep.rb +0 -35
  150. data/test/scanner/test_literals.rb +0 -89
  151. data/test/scanner/test_meta.rb +0 -40
  152. data/test/scanner/test_properties.rb +0 -312
  153. data/test/scanner/test_quantifiers.rb +0 -37
  154. data/test/scanner/test_refcalls.rb +0 -52
  155. data/test/scanner/test_scripts.rb +0 -53
  156. data/test/scanner/test_sets.rb +0 -119
  157. data/test/scanner/test_types.rb +0 -35
  158. data/test/scanner/test_unicode_blocks.rb +0 -30
  159. data/test/support/disable_autotest.rb +0 -8
  160. data/test/syntax/test_all.rb +0 -6
  161. data/test/syntax/test_syntax.rb +0 -61
  162. data/test/syntax/test_syntax_token_map.rb +0 -25
  163. data/test/syntax/versions/test_1.8.rb +0 -55
  164. data/test/syntax/versions/test_1.9.1.rb +0 -36
  165. data/test/syntax/versions/test_1.9.3.rb +0 -32
  166. data/test/syntax/versions/test_2.0.0.rb +0 -37
  167. data/test/syntax/versions/test_2.2.0.rb +0 -32
  168. data/test/syntax/versions/test_aliases.rb +0 -129
  169. data/test/syntax/versions/test_all.rb +0 -5
  170. data/test/test_all.rb +0 -5
  171. data/test/token/test_all.rb +0 -2
  172. data/test/token/test_token.rb +0 -107
@@ -1,58 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionSubexpression < Test::Unit::TestCase
4
-
5
- def test_subexpression_ts_te
6
- regx = /abcd|ghij|klmn|pqur/
7
- root = RP.parse(regx)
8
-
9
- alt = root.first
10
-
11
- {
12
- 0 => [ 0, 4 ],
13
- 1 => [ 5, 9 ],
14
- 2 => [ 10, 14 ],
15
- 3 => [ 15, 19 ],
16
- }.each do |index, span|
17
- sequence = alt[index]
18
-
19
- assert_equal span[0], sequence.ts
20
- assert_equal span[1], sequence.te
21
- end
22
- end
23
-
24
- def test_subexpression_nesting_level
25
- root = RP.parse(/a(b(c\d|[ef-g[h]]))/)
26
-
27
- tests = {
28
- 'a' => 1,
29
- 'b' => 2,
30
- '|' => 3,
31
- 'c\d' => 4, # first alternative
32
- 'c' => 5,
33
- '\d' => 5,
34
- '[ef-g[h]]' => 4, # second alternative
35
- 'e' => 5,
36
- '-' => 5,
37
- 'f' => 6,
38
- 'g' => 6,
39
- 'h' => 6,
40
- }
41
-
42
- root.each_expression do |exp|
43
- next unless (expected_nesting_level = tests.delete(exp.text))
44
- assert_equal exp.nesting_level, expected_nesting_level
45
- end
46
-
47
- assert tests.empty?
48
- end
49
-
50
- def test_subexpression_dig
51
- root = RP.parse(/(((a)))/)
52
-
53
- assert_equal '(((a)))', root.dig(0).to_s
54
- assert_equal 'a', root.dig(0, 0, 0, 0).to_s
55
- assert_nil root.dig(0, 0, 0, 0, 0)
56
- assert_nil root.dig(3, 7)
57
- end
58
- end
@@ -1,99 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionTests < Test::Unit::TestCase
4
-
5
- def test_expression_type?
6
- root = RP.parse(/abcd|(ghij)|[klmn]/)
7
-
8
- alt = root.first
9
-
10
- assert_equal true, alt.type?(:meta)
11
- assert_equal false, alt.type?(:escape)
12
- assert_equal true, alt.type?([:meta, :escape])
13
- assert_equal false, alt.type?([:literal, :escape])
14
- assert_equal true, alt.type?(:*)
15
- assert_equal true, alt.type?([:*])
16
- assert_equal true, alt.type?([:literal, :escape, :*])
17
-
18
- seq_1 = alt[0]
19
- assert_equal true, seq_1.type?(:expression)
20
- assert_equal true, seq_1.first.type?(:literal)
21
-
22
- seq_2 = alt[1]
23
- assert_equal true, seq_2.type?(:*)
24
- assert_equal true, seq_2.first.type?(:group)
25
-
26
- seq_3 = alt[2]
27
- assert_equal true, seq_3.first.type?(:set)
28
- end
29
-
30
- def test_expression_is?
31
- root = RP.parse(/.+|\.?/)
32
-
33
- assert_equal true, root.is?(:*)
34
-
35
- alt = root.first
36
- assert_equal true, alt.is?(:*)
37
- assert_equal true, alt.is?(:alternation)
38
- assert_equal true, alt.is?(:alternation, :meta)
39
-
40
- seq_1 = alt[0]
41
- assert_equal true, seq_1.is?(:sequence)
42
- assert_equal true, seq_1.is?(:sequence, :expression)
43
-
44
- assert_equal true, seq_1.first.is?(:dot)
45
- assert_equal false, seq_1.first.is?(:dot, :escape)
46
- assert_equal true, seq_1.first.is?(:dot, :meta)
47
- assert_equal true, seq_1.first.is?(:dot, [:escape, :meta])
48
-
49
- seq_2 = alt[1]
50
- assert_equal true, seq_2.first.is?(:dot)
51
- assert_equal true, seq_2.first.is?(:dot, :escape)
52
- assert_equal false, seq_2.first.is?(:dot, :meta)
53
- assert_equal true, seq_2.first.is?(:dot, [:meta, :escape])
54
- end
55
-
56
- def test_expression_one_of?
57
- root = RP.parse(/\Aab(c[\w])d|e.\z/)
58
-
59
- assert_equal true, root.one_of?(:*)
60
- assert_equal true, root.one_of?(:* => :*)
61
- assert_equal true, root.one_of?(:* => [:*])
62
-
63
- alt = root.first
64
- assert_equal true, alt.one_of?(:*)
65
- assert_equal true, alt.one_of?(:meta)
66
- assert_equal true, alt.one_of?(:meta, :alternation)
67
- assert_equal false, alt.one_of?(:meta => [:dot, :bogus])
68
- assert_equal true, alt.one_of?(:meta => [:dot, :alternation])
69
-
70
- seq_1 = alt[0]
71
- assert_equal true, seq_1.one_of?(:expression)
72
- assert_equal true, seq_1.one_of?(:expression => :sequence)
73
-
74
- assert_equal true, seq_1.first.one_of?(:anchor)
75
- assert_equal true, seq_1.first.one_of?(:anchor => :bos)
76
- assert_equal false, seq_1.first.one_of?(:anchor => :eos)
77
- assert_equal true, seq_1.first.one_of?(:anchor => [:escape, :meta, :bos])
78
- assert_equal false, seq_1.first.one_of?(:anchor => [:escape, :meta, :eos])
79
-
80
- seq_2 = alt[1]
81
- assert_equal true, seq_2.first.one_of?(:literal)
82
-
83
- assert_equal true, seq_2[1].one_of?(:meta)
84
- assert_equal true, seq_2[1].one_of?(:meta => :dot)
85
- assert_equal false, seq_2[1].one_of?(:meta => :alternation)
86
- assert_equal true, seq_2[1].one_of?(:meta => [:dot])
87
-
88
- assert_equal false, seq_2.last.one_of?(:group)
89
- assert_equal false, seq_2.last.one_of?(:group => [:*])
90
- assert_equal false, seq_2.last.one_of?(:group => [:*], :meta => :*)
91
-
92
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :* => :*)
93
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => :*)
94
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => :eos)
95
- assert_equal false, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos])
96
- assert_equal true, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos, :eos])
97
- end
98
-
99
- end
@@ -1,59 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionToH < Test::Unit::TestCase
4
-
5
- def test_expression_to_h
6
- root = RP.parse('abc')
7
-
8
- hash = root.to_h
9
-
10
- assert_equal hash, {
11
- :token => :root,
12
- :type => :expression,
13
- :text => 'abc',
14
- :starts_at => 0,
15
- :length => 3,
16
- :quantifier => nil,
17
- :options => {},
18
- :level => nil,
19
- :set_level => nil,
20
- :conditional_level => nil,
21
- :expressions => [
22
- {
23
- :token => :literal,
24
- :type => :literal,
25
- :text => 'abc',
26
- :starts_at => 0,
27
- :length => 3,
28
- :quantifier => nil,
29
- :options => {},
30
- :level => 0,
31
- :set_level => 0,
32
- :conditional_level => 0
33
- }
34
- ]
35
- }
36
- end
37
-
38
- def test_expression_quantifier_to_h
39
- root = RP.parse('a{2,4}')
40
- exp = root.expressions.at(0)
41
-
42
- hash = exp.quantifier.to_h
43
-
44
- assert_equal hash, {
45
- :max => 4,
46
- :min => 2,
47
- :mode => :greedy,
48
- :text => '{2,4}',
49
- :token => :interval
50
- }
51
- end
52
-
53
- # regression test
54
- def test_conditional_expression_to_h
55
- root = RP.parse('(?<A>a)(?(<A>)b|c)', 'ruby/2.0')
56
-
57
- assert_nothing_raised { root.to_h }
58
- end
59
- end
@@ -1,104 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class ExpressionToS < Test::Unit::TestCase
4
-
5
- def test_expression_to_s_literal_alternation
6
- pattern = 'abcd|ghij|klmn|pqur'
7
-
8
- assert_equal pattern, RP.parse(pattern).to_s
9
- end
10
-
11
- def test_expression_to_s_quantified_alternations
12
- pattern = '(?:a?[b]+(c){2}|d+[e]*(f)?)|(?:g+[h]?(i){2,3}|j*[k]{3,5}(l)?)'
13
-
14
- assert_equal pattern, RP.parse(pattern).to_s
15
- end
16
-
17
- def test_expression_to_s_quantified_sets
18
- pattern = '[abc]+|[^def]{3,6}'
19
-
20
- assert_equal pattern, RP.parse(pattern).to_s
21
- end
22
-
23
- def test_expression_to_s_property_sets
24
- pattern = '[\a\b\p{Lu}\P{Z}\c\d]+'
25
-
26
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
27
- end
28
-
29
- def test_expression_to_s_groups
30
- pattern = "(a(?>b(?:c(?<n>d(?'N'e)??f)+g)*+h)*i)++"
31
-
32
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
33
- end
34
-
35
- def test_expression_to_s_assertions
36
- pattern = '(a+(?=b+(?!c+(?<=d+(?<!e+)?f+)?g+)?h+)?i+)?'
37
-
38
- assert_equal pattern, RP.parse(pattern, 'ruby/1.9').to_s
39
- end
40
-
41
- def test_expression_to_s_comments
42
- pattern = '(?#start)a(?#middle)b(?#end)'
43
-
44
- assert_equal pattern, RP.parse(pattern).to_s
45
- end
46
-
47
- def test_expression_to_s_options
48
- pattern = '(?mix:start)a(?-mix:middle)b(?i-mx:end)'
49
-
50
- assert_equal pattern, RP.parse(pattern).to_s
51
- end
52
-
53
- def test_expression_to_s_url
54
- pattern = '(^$)|(^(http|https):\/\/[a-z0-9]+([\-\.]{1}[a-z0-9]+)*'+
55
- '\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$)'
56
-
57
- assert_equal pattern, RP.parse(pattern).to_s
58
- end
59
-
60
- def test_expression_to_s_multiline_source
61
- multiline = %r{
62
- \A
63
- a? # One letter
64
- b{2,5} # Another one
65
- [c-g]+ # A set
66
- \z
67
- }x
68
-
69
- assert_equal multiline.source, RP.parse(multiline).to_s
70
- end
71
-
72
- def test_expression_to_s_multiline_to_s
73
- multiline = %r{
74
- \A
75
- a? # One letter
76
- b{2,5} # Another one
77
- [c-g]+ # A set
78
- \z
79
- }x
80
-
81
- assert_equal multiline.to_s, RP.parse(multiline.to_s).to_s
82
- end
83
-
84
- # Free spacing expressions that use spaces between quantifiers and their
85
- # targets do not produce identical results due to the way quantifiers are
86
- # applied to expressions (members, not nodes) and the merging of consecutive
87
- # space nodes. This tests that they produce equivalent results.
88
- def test_expression_to_s_multiline_equivalence
89
- multiline = %r{
90
- \A
91
- a ? # One letter
92
- b {2,5} # Another one
93
- [c-g] + # A set
94
- \z
95
- }x
96
-
97
- str = 'bbbcged'
98
- root = RP.parse(multiline)
99
-
100
- assert_equal multiline.match(str)[0],
101
- Regexp.new(root.to_s, Regexp::EXTENDED).match(str)[0]
102
- end
103
-
104
- end
@@ -1,161 +0,0 @@
1
- require File.expand_path("../../helpers", __FILE__)
2
-
3
- class SubexpressionTraverse < Test::Unit::TestCase
4
-
5
- def test_subexpression_traverse
6
- root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
7
-
8
- enters = 0
9
- visits = 0
10
- exits = 0
11
-
12
- root.traverse {|event, exp, index|
13
- enters += 1 if event == :enter
14
- visits += 1 if event == :visit
15
- exits += 1 if event == :exit
16
- }
17
-
18
- assert_equal 9, enters
19
- assert_equal exits, enters
20
-
21
- assert_equal 9, visits
22
- end
23
-
24
- def test_subexpression_traverse_include_self
25
- root = RP.parse(/a(b(c(d)))|g[h-i]j|klmn/)
26
-
27
- enters = 0
28
- visits = 0
29
- exits = 0
30
-
31
- root.traverse(true) {|event, exp, index|
32
- enters += 1 if event == :enter
33
- visits += 1 if event == :visit
34
- exits += 1 if event == :exit
35
- }
36
-
37
- assert_equal 10, enters
38
- assert_equal exits, enters
39
-
40
- assert_equal 9, visits
41
- end
42
-
43
- def test_subexpression_walk_alias
44
- root = RP.parse(/abc/)
45
-
46
- assert_equal true, root.respond_to?(:walk)
47
- end
48
-
49
- def test_subexpression_each_expression
50
- root = RP.parse(/a(?x:b(c))|g[h-k]/)
51
-
52
- count = 0
53
- root.each_expression {|exp, index|
54
- count += 1
55
- }
56
-
57
- assert_equal 13, count
58
- end
59
-
60
- def test_subexpression_each_expression_include_self
61
- root = RP.parse(/a(?x:b(c))|g[h-k]/)
62
-
63
- count = 0
64
- root.each_expression(true) {|exp, index|
65
- count += 1
66
- }
67
-
68
- assert_equal 14, count
69
- end
70
-
71
- def test_subexpression_each_expression_indices
72
- root = RP.parse(/a(b)c/)
73
-
74
- indices = []
75
- root.each_expression {|exp, index| indices << index}
76
-
77
- assert_equal [0, 1, 0, 2], indices
78
- end
79
-
80
- def test_subexpression_each_expression_indices_include_self
81
- root = RP.parse(/a(b)c/)
82
-
83
- indices = []
84
- root.each_expression(true) {|exp, index| indices << index}
85
-
86
- assert_equal [0, 0, 1, 0, 2], indices
87
- end
88
-
89
- def test_subexpression_flat_map_without_block
90
- root = RP.parse(/a(b([c-e]+))?/)
91
-
92
- array = root.flat_map
93
-
94
- assert_equal Array, array.class
95
- assert_equal 8, array.length
96
-
97
- array.each do |item|
98
- assert_equal Array, item.class
99
- assert_equal 2, item.length
100
- assert_equal true, item.first.is_a?(Regexp::Expression::Base)
101
- assert_equal true, item.last.is_a?(Integer)
102
- end
103
- end
104
-
105
- def test_subexpression_flat_map_without_block_include_self
106
- root = RP.parse(/a(b([c-e]+))?/)
107
-
108
- array = root.flat_map(true)
109
-
110
- assert_equal Array, array.class
111
- assert_equal 9, array.length
112
- end
113
-
114
- def test_subexpression_flat_map_indices
115
- root = RP.parse(/a(b([c-e]+))?f*g/)
116
-
117
- indices = root.flat_map {|exp, index| index}
118
-
119
- assert_equal [0, 1, 0, 1, 0, 0, 0, 1, 2, 3], indices
120
- end
121
-
122
- def test_subexpression_flat_map_indices_include_self
123
- root = RP.parse(/a(b([c-e]+))?f*g/)
124
-
125
- indices = root.flat_map(true) {|exp, index| index}
126
-
127
- assert_equal [0, 0, 1, 0, 1, 0, 0, 0, 1, 2, 3], indices
128
- end
129
-
130
- def test_subexpression_flat_map_expressions
131
- root = RP.parse(/a(b(c(d)))/)
132
-
133
- levels = root.flat_map {|exp, index|
134
- [exp.level, exp.text] if exp.terminal?
135
- }.compact
136
-
137
- assert_equal [
138
- [0, 'a'], [1, 'b'], [2, 'c'], [3, 'd']
139
- ], levels
140
- end
141
-
142
- def test_subexpression_flat_map_expressions_include_self
143
- root = RP.parse(/a(b(c(d)))/)
144
-
145
- levels = root.flat_map(true) {|exp, index|
146
- [exp.level, exp.to_s]
147
- }.compact
148
-
149
- assert_equal [
150
- [nil, 'a(b(c(d)))'],
151
- [0, 'a'],
152
- [0, '(b(c(d)))'],
153
- [1, 'b'],
154
- [1, '(c(d))'],
155
- [2, 'c'],
156
- [2, '(d)'],
157
- [3, 'd']
158
- ], levels
159
- end
160
-
161
- end