regexp_parser 1.4.0 → 1.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (171) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +66 -1
  3. data/Gemfile +3 -3
  4. data/README.md +11 -18
  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 +15 -1
  27. data/lib/regexp_parser/scanner/properties/short.yml +5 -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 +30 -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 +2 -2
  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 +161 -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 +161 -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/delimiters_spec.rb +68 -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 +52 -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/delimiters_spec.rb +52 -0
  79. data/spec/scanner/errors_spec.rb +67 -0
  80. data/spec/scanner/escapes_spec.rb +53 -0
  81. data/spec/scanner/free_space_spec.rb +133 -0
  82. data/spec/scanner/groups_spec.rb +52 -0
  83. data/spec/scanner/keep_spec.rb +10 -0
  84. data/spec/scanner/literals_spec.rb +49 -0
  85. data/spec/scanner/meta_spec.rb +18 -0
  86. data/spec/scanner/properties_spec.rb +64 -0
  87. data/spec/scanner/quantifiers_spec.rb +20 -0
  88. data/spec/scanner/refcalls_spec.rb +36 -0
  89. data/spec/scanner/sets_spec.rb +102 -0
  90. data/spec/scanner/types_spec.rb +14 -0
  91. data/spec/spec_helper.rb +15 -0
  92. data/{test → spec}/support/runner.rb +9 -8
  93. data/spec/support/shared_examples.rb +77 -0
  94. data/{test → spec}/support/warning_extractor.rb +5 -7
  95. data/spec/syntax/syntax_spec.rb +48 -0
  96. data/spec/syntax/syntax_token_map_spec.rb +23 -0
  97. data/spec/syntax/versions/1.8.6_spec.rb +17 -0
  98. data/spec/syntax/versions/1.9.1_spec.rb +10 -0
  99. data/spec/syntax/versions/1.9.3_spec.rb +9 -0
  100. data/spec/syntax/versions/2.0.0_spec.rb +13 -0
  101. data/spec/syntax/versions/2.2.0_spec.rb +9 -0
  102. data/spec/syntax/versions/aliases_spec.rb +37 -0
  103. data/spec/token/token_spec.rb +85 -0
  104. metadata +149 -144
  105. data/test/expression/test_all.rb +0 -12
  106. data/test/expression/test_base.rb +0 -90
  107. data/test/expression/test_clone.rb +0 -89
  108. data/test/expression/test_conditionals.rb +0 -113
  109. data/test/expression/test_free_space.rb +0 -35
  110. data/test/expression/test_set.rb +0 -84
  111. data/test/expression/test_strfregexp.rb +0 -230
  112. data/test/expression/test_subexpression.rb +0 -58
  113. data/test/expression/test_tests.rb +0 -99
  114. data/test/expression/test_to_h.rb +0 -59
  115. data/test/expression/test_to_s.rb +0 -104
  116. data/test/expression/test_traverse.rb +0 -161
  117. data/test/helpers.rb +0 -10
  118. data/test/lexer/test_all.rb +0 -41
  119. data/test/lexer/test_conditionals.rb +0 -127
  120. data/test/lexer/test_keep.rb +0 -24
  121. data/test/lexer/test_literals.rb +0 -130
  122. data/test/lexer/test_nesting.rb +0 -132
  123. data/test/lexer/test_refcalls.rb +0 -56
  124. data/test/parser/set/test_intersections.rb +0 -127
  125. data/test/parser/set/test_ranges.rb +0 -111
  126. data/test/parser/test_all.rb +0 -64
  127. data/test/parser/test_alternation.rb +0 -92
  128. data/test/parser/test_anchors.rb +0 -34
  129. data/test/parser/test_conditionals.rb +0 -187
  130. data/test/parser/test_errors.rb +0 -63
  131. data/test/parser/test_escapes.rb +0 -134
  132. data/test/parser/test_free_space.rb +0 -139
  133. data/test/parser/test_groups.rb +0 -289
  134. data/test/parser/test_keep.rb +0 -21
  135. data/test/parser/test_posix_classes.rb +0 -27
  136. data/test/parser/test_properties.rb +0 -134
  137. data/test/parser/test_quantifiers.rb +0 -301
  138. data/test/parser/test_refcalls.rb +0 -186
  139. data/test/parser/test_sets.rb +0 -179
  140. data/test/parser/test_types.rb +0 -50
  141. data/test/scanner/test_all.rb +0 -38
  142. data/test/scanner/test_anchors.rb +0 -38
  143. data/test/scanner/test_conditionals.rb +0 -184
  144. data/test/scanner/test_errors.rb +0 -91
  145. data/test/scanner/test_escapes.rb +0 -56
  146. data/test/scanner/test_free_space.rb +0 -200
  147. data/test/scanner/test_groups.rb +0 -79
  148. data/test/scanner/test_keep.rb +0 -35
  149. data/test/scanner/test_literals.rb +0 -89
  150. data/test/scanner/test_meta.rb +0 -40
  151. data/test/scanner/test_properties.rb +0 -312
  152. data/test/scanner/test_quantifiers.rb +0 -37
  153. data/test/scanner/test_refcalls.rb +0 -52
  154. data/test/scanner/test_scripts.rb +0 -53
  155. data/test/scanner/test_sets.rb +0 -119
  156. data/test/scanner/test_types.rb +0 -35
  157. data/test/scanner/test_unicode_blocks.rb +0 -30
  158. data/test/support/disable_autotest.rb +0 -8
  159. data/test/syntax/test_all.rb +0 -6
  160. data/test/syntax/test_syntax.rb +0 -61
  161. data/test/syntax/test_syntax_token_map.rb +0 -25
  162. data/test/syntax/versions/test_1.8.rb +0 -55
  163. data/test/syntax/versions/test_1.9.1.rb +0 -36
  164. data/test/syntax/versions/test_1.9.3.rb +0 -32
  165. data/test/syntax/versions/test_2.0.0.rb +0 -37
  166. data/test/syntax/versions/test_2.2.0.rb +0 -32
  167. data/test/syntax/versions/test_aliases.rb +0 -129
  168. data/test/syntax/versions/test_all.rb +0 -5
  169. data/test/test_all.rb +0 -5
  170. data/test/token/test_all.rb +0 -2
  171. data/test/token/test_token.rb +0 -107
@@ -0,0 +1,20 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Quantifier scanning') do
4
+ include_examples 'scan', 'a?', 1 => [:quantifier, :zero_or_one, '?', 1, 2]
5
+ include_examples 'scan', 'a??', 1 => [:quantifier, :zero_or_one_reluctant, '??', 1, 3]
6
+ include_examples 'scan', 'a?+', 1 => [:quantifier, :zero_or_one_possessive, '?+', 1, 3]
7
+
8
+ include_examples 'scan', 'a*', 1 => [:quantifier, :zero_or_more, '*', 1, 2]
9
+ include_examples 'scan', 'a*?', 1 => [:quantifier, :zero_or_more_reluctant, '*?', 1, 3]
10
+ include_examples 'scan', 'a*+', 1 => [:quantifier, :zero_or_more_possessive, '*+', 1, 3]
11
+
12
+ include_examples 'scan', 'a+', 1 => [:quantifier, :one_or_more, '+', 1, 2]
13
+ include_examples 'scan', 'a+?', 1 => [:quantifier, :one_or_more_reluctant, '+?', 1, 3]
14
+ include_examples 'scan', 'a++', 1 => [:quantifier, :one_or_more_possessive, '++', 1, 3]
15
+
16
+ include_examples 'scan', 'a{2}', 1 => [:quantifier, :interval, '{2}', 1, 4]
17
+ include_examples 'scan', 'a{2,}', 1 => [:quantifier, :interval, '{2,}', 1, 5]
18
+ include_examples 'scan', 'a{,2}', 1 => [:quantifier, :interval, '{,2}', 1, 5]
19
+ include_examples 'scan', 'a{2,4}', 1 => [:quantifier, :interval, '{2,4}', 1, 6]
20
+ end
@@ -0,0 +1,36 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('RefCall scanning') do
4
+ # Traditional numerical group back-reference
5
+ include_examples 'scan', '(abc)\1' , 3 => [:backref, :number, '\1', 5, 7]
6
+
7
+ # Group back-references, named, numbered, and relative
8
+ include_examples 'scan', '(?<X>abc)\k<X>', 3 => [:backref, :name_ref_ab, '\k<X>', 9, 14]
9
+ include_examples 'scan', "(?<X>abc)\\k'X'", 3 => [:backref, :name_ref_sq, "\\k'X'", 9, 14]
10
+
11
+ include_examples 'scan', '(abc)\k<1>', 3 => [:backref, :number_ref_ab, '\k<1>', 5, 10]
12
+ include_examples 'scan', "(abc)\\k'1'", 3 => [:backref, :number_ref_sq, "\\k'1'", 5, 10]
13
+
14
+ include_examples 'scan', '(abc)\k<-1>', 3 => [:backref, :number_rel_ref_ab, '\k<-1>', 5, 11]
15
+ include_examples 'scan', "(abc)\\k'-1'", 3 => [:backref, :number_rel_ref_sq, "\\k'-1'", 5, 11]
16
+
17
+ # Sub-expression invocation, named, numbered, and relative
18
+ include_examples 'scan', '(?<X>abc)\g<X>', 3 => [:backref, :name_call_ab, '\g<X>', 9, 14]
19
+ include_examples 'scan', "(?<X>abc)\\g'X'", 3 => [:backref, :name_call_sq, "\\g'X'", 9, 14]
20
+
21
+ include_examples 'scan', '(abc)\g<1>', 3 => [:backref, :number_call_ab, '\g<1>', 5, 10]
22
+ include_examples 'scan', "(abc)\\g'1'", 3 => [:backref, :number_call_sq, "\\g'1'", 5, 10]
23
+
24
+ include_examples 'scan', '(abc)\g<-1>', 3 => [:backref, :number_rel_call_ab, '\g<-1>', 5, 11]
25
+ include_examples 'scan', "(abc)\\g'-1'", 3 => [:backref, :number_rel_call_sq, "\\g'-1'", 5, 11]
26
+
27
+ include_examples 'scan', '\g<+1>(abc)', 0 => [:backref, :number_rel_call_ab, '\g<+1>', 0, 6]
28
+ include_examples 'scan', "\\g'+1'(abc)", 0 => [:backref, :number_rel_call_sq, "\\g'+1'", 0, 6]
29
+
30
+ # Group back-references, with recursion level
31
+ include_examples 'scan', '(?<X>abc)\k<X-0>', 3 => [:backref, :name_recursion_ref_ab, '\k<X-0>', 9, 16]
32
+ include_examples 'scan', "(?<X>abc)\\k'X-0'", 3 => [:backref, :name_recursion_ref_sq, "\\k'X-0'", 9, 16]
33
+
34
+ include_examples 'scan', '(abc)\k<1-0>', 3 => [:backref, :number_recursion_ref_ab, '\k<1-0>', 5, 12]
35
+ include_examples 'scan', "(abc)\\k'1-0'", 3 => [:backref, :number_recursion_ref_sq, "\\k'1-0'", 5, 12]
36
+ end
@@ -0,0 +1,102 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Set scanning') do
4
+ include_examples 'scan', /[a]/, 0 => [:set, :open, '[', 0, 1]
5
+ include_examples 'scan', /[b]/, 2 => [:set, :close, ']', 2, 3]
6
+ include_examples 'scan', /[^n]/, 1 => [:set, :negate, '^', 1, 2]
7
+
8
+ include_examples 'scan', /[c]/, 1 => [:literal, :literal, 'c', 1, 2]
9
+ include_examples 'scan', /[\b]/, 1 => [:escape, :backspace, '\b', 1, 3]
10
+ include_examples 'scan', /[A\bX]/, 2 => [:escape, :backspace, '\b', 2, 4]
11
+
12
+ include_examples 'scan', /[.]/, 1 => [:literal, :literal, '.', 1, 2]
13
+ include_examples 'scan', /[?]/, 1 => [:literal, :literal, '?', 1, 2]
14
+ include_examples 'scan', /[*]/, 1 => [:literal, :literal, '*', 1, 2]
15
+ include_examples 'scan', /[+]/, 1 => [:literal, :literal, '+', 1, 2]
16
+ include_examples 'scan', /[{]/, 1 => [:literal, :literal, '{', 1, 2]
17
+ include_examples 'scan', /[}]/, 1 => [:literal, :literal, '}', 1, 2]
18
+ include_examples 'scan', /[<]/, 1 => [:literal, :literal, '<', 1, 2]
19
+ include_examples 'scan', /[>]/, 1 => [:literal, :literal, '>', 1, 2]
20
+
21
+ include_examples 'scan', /[äöü]/, 2 => [:literal, :literal, 'ö', 3, 5]
22
+
23
+ include_examples 'scan', /[\x20]/, 1 => [:escape, :hex, '\x20', 1, 5]
24
+
25
+ include_examples 'scan', '[\.]', 1 => [:escape, :dot, '\.', 1, 3]
26
+ include_examples 'scan', '[\!]', 1 => [:escape, :literal, '\!', 1, 3]
27
+ include_examples 'scan', '[\#]', 1 => [:escape, :literal, '\#', 1, 3]
28
+ include_examples 'scan', '[\\]]', 1 => [:escape, :set_close, '\]', 1, 3]
29
+ include_examples 'scan', '[\\\\]', 1 => [:escape, :backslash, '\\\\', 1, 3]
30
+ include_examples 'scan', '[\A]', 1 => [:escape, :literal, '\A', 1, 3]
31
+ include_examples 'scan', '[\z]', 1 => [:escape, :literal, '\z', 1, 3]
32
+ include_examples 'scan', '[\g]', 1 => [:escape, :literal, '\g', 1, 3]
33
+ include_examples 'scan', '[\K]', 1 => [:escape, :literal, '\K', 1, 3]
34
+ include_examples 'scan', '[\R]', 1 => [:escape, :literal, '\R', 1, 3]
35
+ include_examples 'scan', '[\X]', 1 => [:escape, :literal, '\X', 1, 3]
36
+ include_examples 'scan', '[\c2]', 1 => [:escape, :literal, '\c', 1, 3]
37
+ include_examples 'scan', '[\B]', 1 => [:escape, :literal, '\B', 1, 3]
38
+ include_examples 'scan', '[a\-c]', 2 => [:escape, :literal, '\-', 2, 4]
39
+
40
+ include_examples 'scan', /[\d]/, 1 => [:type, :digit, '\d', 1, 3]
41
+ include_examples 'scan', /[\da-z]/, 1 => [:type, :digit, '\d', 1, 3]
42
+ include_examples 'scan', /[\D]/, 1 => [:type, :nondigit, '\D', 1, 3]
43
+
44
+ include_examples 'scan', /[\h]/, 1 => [:type, :hex, '\h', 1, 3]
45
+ include_examples 'scan', /[\H]/, 1 => [:type, :nonhex, '\H', 1, 3]
46
+
47
+ include_examples 'scan', /[\s]/, 1 => [:type, :space, '\s', 1, 3]
48
+ include_examples 'scan', /[\S]/, 1 => [:type, :nonspace, '\S', 1, 3]
49
+
50
+ include_examples 'scan', /[\w]/, 1 => [:type, :word, '\w', 1, 3]
51
+ include_examples 'scan', /[\W]/, 1 => [:type, :nonword, '\W', 1, 3]
52
+
53
+ include_examples 'scan', /[a-b]/, 1 => [:literal, :literal, 'a', 1, 2]
54
+ include_examples 'scan', /[a-c]/, 2 => [:set, :range, '-', 2, 3]
55
+ include_examples 'scan', /[a-d]/, 3 => [:literal, :literal, 'd', 3, 4]
56
+ include_examples 'scan', /[a-b-]/, 4 => [:literal, :literal, '-', 4, 5]
57
+ include_examples 'scan', /[-a]/, 1 => [:literal, :literal, '-', 1, 2]
58
+ include_examples 'scan', /[a-c^]/, 4 => [:literal, :literal, '^', 4, 5]
59
+ include_examples 'scan', /[a-bd-f]/, 2 => [:set, :range, '-', 2, 3]
60
+ include_examples 'scan', /[a-cd-f]/, 5 => [:set, :range, '-', 5, 6]
61
+
62
+ include_examples 'scan', /[a[:digit:]c]/, 2 => [:posixclass, :digit, '[:digit:]', 2, 11]
63
+ include_examples 'scan', /[[:digit:][:space:]]/, 2 => [:posixclass, :space, '[:space:]', 10, 19]
64
+ include_examples 'scan', /[[:^digit:]]/, 1 => [:nonposixclass, :digit, '[:^digit:]', 1, 11]
65
+
66
+ include_examples 'scan', /[a[.a-b.]c]/, 2 => [:set, :collation, '[.a-b.]', 2, 9]
67
+ include_examples 'scan', /[a[=e=]c]/, 2 => [:set, :equivalent, '[=e=]', 2, 7]
68
+
69
+ include_examples 'scan', /[a-d&&g-h]/, 4 => [:set, :intersection, '&&', 4, 6]
70
+ include_examples 'scan', /[a&&]/, 2 => [:set, :intersection, '&&', 2, 4]
71
+ include_examples 'scan', /[&&z]/, 1 => [:set, :intersection, '&&', 1, 3]
72
+
73
+ include_examples 'scan', /[a\p{digit}c]/, 2 => [:property, :digit, '\p{digit}', 2, 11]
74
+ include_examples 'scan', /[a\P{digit}c]/, 2 => [:nonproperty, :digit, '\P{digit}', 2, 11]
75
+ include_examples 'scan', /[a\p{^digit}c]/, 2 => [:nonproperty, :digit, '\p{^digit}', 2, 12]
76
+ include_examples 'scan', /[a\P{^digit}c]/, 2 => [:property, :digit, '\P{^digit}', 2, 12]
77
+
78
+ include_examples 'scan', /[a\p{ALPHA}c]/, 2 => [:property, :alpha, '\p{ALPHA}', 2, 11]
79
+ include_examples 'scan', /[a\p{P}c]/, 2 => [:property, :punctuation,'\p{P}', 2, 7]
80
+ include_examples 'scan', /[a\p{P}\P{P}c]/, 3 => [:nonproperty, :punctuation,'\P{P}', 7, 12]
81
+
82
+ include_examples 'scan', /[\x20-\x27]/,
83
+ 1 => [:escape, :hex, '\x20', 1, 5],
84
+ 2 => [:set, :range, '-', 5, 6],
85
+ 3 => [:escape, :hex, '\x27', 6, 10]
86
+
87
+ include_examples 'scan', /[a-w&&[^c-g]z]/,
88
+ 5 => [:set, :open, '[', 6, 7],
89
+ 6 => [:set, :negate, '^', 7, 8],
90
+ 8 => [:set, :range, '-', 9, 10],
91
+ 10=> [:set, :close, ']', 11, 12]
92
+
93
+ specify('set literal encoding') do
94
+ text = RS.scan('[a]')[1][2].to_s
95
+ expect(text).to eq 'a'
96
+ expect(text.encoding.to_s).to eq 'UTF-8'
97
+
98
+ text = RS.scan("[\u{1F632}]")[1][2].to_s
99
+ expect(text).to eq "\u{1F632}"
100
+ expect(text.encoding.to_s).to eq 'UTF-8'
101
+ end
102
+ end
@@ -0,0 +1,14 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe('Type scanning') do
4
+ include_examples 'scan', 'a\\dc', 1 => [:type, :digit, '\\d', 1, 3]
5
+ include_examples 'scan', 'a\\Dc', 1 => [:type, :nondigit, '\\D', 1, 3]
6
+ include_examples 'scan', 'a\\hc', 1 => [:type, :hex, '\\h', 1, 3]
7
+ include_examples 'scan', 'a\\Hc', 1 => [:type, :nonhex, '\\H', 1, 3]
8
+ include_examples 'scan', 'a\\sc', 1 => [:type, :space, '\\s', 1, 3]
9
+ include_examples 'scan', 'a\\Sc', 1 => [:type, :nonspace, '\\S', 1, 3]
10
+ include_examples 'scan', 'a\\wc', 1 => [:type, :word, '\\w', 1, 3]
11
+ include_examples 'scan', 'a\\Wc', 1 => [:type, :nonword, '\\W', 1, 3]
12
+ include_examples 'scan', 'a\\Rc', 1 => [:type, :linebreak, '\\R', 1, 3]
13
+ include_examples 'scan', 'a\\Xc', 1 => [:type, :xgrapheme, '\\X', 1, 3]
14
+ end
@@ -0,0 +1,15 @@
1
+ require 'regexp_parser'
2
+ require 'regexp_property_values'
3
+ require_relative 'support/shared_examples'
4
+
5
+ RS = Regexp::Scanner
6
+ RL = Regexp::Lexer
7
+ RP = Regexp::Parser
8
+ RE = Regexp::Expression
9
+ T = Regexp::Syntax::Token
10
+
11
+ include Regexp::Expression
12
+
13
+ def ruby_version_at_least(version)
14
+ Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new(version)
15
+ end
@@ -1,6 +1,7 @@
1
1
  require 'pathname'
2
+ require 'rspec'
2
3
 
3
- module RegexpParserTest
4
+ module RegexpParserSpec
4
5
  class Runner
5
6
  def initialize(arguments, warning_whitelist)
6
7
  @arguments = arguments
@@ -8,14 +9,14 @@ module RegexpParserTest
8
9
  end
9
10
 
10
11
  def run
11
- test_status = nil
12
+ spec_status = nil
12
13
 
13
14
  Warning::Filter.new(warning_whitelist).assert_expected_warnings_only do
14
15
  setup
15
- test_status = run_test_unit
16
+ spec_status = run_rspec
16
17
  end
17
18
 
18
- test_status
19
+ spec_status
19
20
  end
20
21
 
21
22
  private
@@ -23,14 +24,14 @@ module RegexpParserTest
23
24
  def setup
24
25
  $VERBOSE = true
25
26
 
26
- test_files.each(&method(:require))
27
+ spec_files.each(&method(:require))
27
28
  end
28
29
 
29
- def run_test_unit
30
- Test::Unit::AutoRunner.run
30
+ def run_rspec
31
+ RSpec::Core::Runner.run([])
31
32
  end
32
33
 
33
- def test_files
34
+ def spec_files
34
35
  arguments
35
36
  .map { |path| Pathname.new(path).expand_path.freeze }
36
37
  .select(&:file?)
@@ -0,0 +1,77 @@
1
+ RSpec.shared_examples 'syntax' do |klass, opts|
2
+ opts[:implements].each do |type, tokens|
3
+ tokens.each do |token|
4
+ it("implements #{token} #{type}") do
5
+ expect(klass.implements?(type, token)).to be true
6
+ end
7
+ end
8
+ end
9
+
10
+ opts[:excludes] && opts[:excludes].each do |type, tokens|
11
+ tokens.each do |token|
12
+ it("does not implement #{token} #{type}") do
13
+ expect(klass.implements?(type, token)).to be false
14
+ end
15
+ end
16
+ end
17
+ end
18
+
19
+ RSpec.shared_examples 'scan' do |pattern, checks|
20
+ context "given the pattern #{pattern}" do
21
+ before(:all) { @tokens = Regexp::Scanner.scan(pattern) }
22
+
23
+ checks.each do |index, (type, token, text, ts, te)|
24
+ it "scans token #{index} as #{token} #{type} at #{ts}..#{te}" do
25
+ result = @tokens.at(index)
26
+
27
+ expect(result[0]).to eq type
28
+ expect(result[1]).to eq token
29
+ expect(result[2]).to eq text
30
+ expect(result[3]).to eq ts
31
+ expect(result[4]).to eq te
32
+ end
33
+ end
34
+ end
35
+ end
36
+
37
+ RSpec.shared_examples 'lex' do |pattern, checks|
38
+ context "given the pattern #{pattern}" do
39
+ before(:all) { @tokens = Regexp::Lexer.lex(pattern) }
40
+
41
+ checks.each do |index, (type, token, text, ts, te, lvl, set_lvl, cond_lvl)|
42
+ it "lexes token #{index} as #{token} #{type} at #{lvl}, #{set_lvl}, #{cond_lvl}" do
43
+ struct = @tokens.at(index)
44
+
45
+ expect(struct.type).to eq type
46
+ expect(struct.token).to eq token
47
+ expect(struct.text).to eq text
48
+ expect(struct.ts).to eq ts
49
+ expect(struct.te).to eq te
50
+ expect(struct.level).to eq lvl
51
+ expect(struct.set_level).to eq set_lvl
52
+ expect(struct.conditional_level).to eq cond_lvl
53
+ end
54
+ end
55
+ end
56
+ end
57
+
58
+ RSpec.shared_examples 'parse' do |pattern, checks|
59
+ context "given the pattern #{pattern}" do
60
+ before(:all) { @root = Regexp::Parser.parse(pattern, '*') }
61
+
62
+ checks.each do |path, (type, token, klass, attributes)|
63
+ it "parses expression at #{path} as #{klass}" do
64
+ exp = @root.dig(*path)
65
+
66
+ expect(exp).to be_instance_of(klass)
67
+ expect(exp.type).to eq type
68
+ expect(exp.token).to eq token
69
+
70
+ attributes && attributes.each do |method, value|
71
+ expect(exp.send(method)).to eq(value),
72
+ "expected expression at #{path} to have #{method} #{value}"
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
@@ -1,7 +1,7 @@
1
1
  require 'set'
2
2
  require 'delegate'
3
3
 
4
- module RegexpParserTest
4
+ module RegexpParserSpec
5
5
  class Warning
6
6
  class UnexpectedWarnings < StandardError
7
7
  MSG = 'Unexpected warnings: %s'.freeze
@@ -22,22 +22,20 @@ module RegexpParserTest
22
22
 
23
23
  yield
24
24
 
25
- if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new('2.0.0')
26
- assert_no_warnings($stderr.warnings)
27
- end
25
+ assert_no_warnings($stderr.warnings)
28
26
  ensure
29
27
  $stderr = original
30
28
  end
31
29
 
32
- private
30
+ private
33
31
 
34
32
  def assert_no_warnings(warnings)
35
- fail UnexpectedWarnings, warnings.to_a if warnings.any?
33
+ raise UnexpectedWarnings, warnings.to_a if warnings.any?
36
34
  end
37
35
  end
38
36
 
39
37
  class Extractor < DelegateClass(IO)
40
- PATTERN = /\A(?:.+):(?:\d+): warning: (?:.+)\n\z/.freeze
38
+ PATTERN = /\A(?:.+):(?:\d+): warning: (?:.+)\n\z/
41
39
 
42
40
  def initialize(io, whitelist)
43
41
  @whitelist = whitelist
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Syntax) do
4
+ specify('unknown name') do
5
+ expect { Regexp::Syntax.new('ruby/1.0') }.to raise_error(Regexp::Syntax::UnknownSyntaxNameError)
6
+ end
7
+
8
+ specify('new') do
9
+ expect(Regexp::Syntax.new('ruby/1.9.3')).to be_instance_of(Regexp::Syntax::V1_9_3)
10
+ end
11
+
12
+ specify('new any') do
13
+ expect(Regexp::Syntax.new('any')).to be_instance_of(Regexp::Syntax::Any)
14
+ expect(Regexp::Syntax.new('*')).to be_instance_of(Regexp::Syntax::Any)
15
+ end
16
+
17
+ specify('not implemented') do
18
+ expect { RP.parse('\\p{alpha}', 'ruby/1.8') }.to raise_error(Regexp::Syntax::NotImplementedError)
19
+ end
20
+
21
+ specify('supported?') do
22
+ expect(Regexp::Syntax.supported?('ruby/1.1.1')).to be false
23
+ expect(Regexp::Syntax.supported?('ruby/2.4.3')).to be true
24
+ expect(Regexp::Syntax.supported?('ruby/2.5')).to be true
25
+ end
26
+
27
+ specify('invalid version') do
28
+ expect { Regexp::Syntax.version_class('2.0.0') }.to raise_error(Regexp::Syntax::InvalidVersionNameError)
29
+
30
+ expect { Regexp::Syntax.version_class('ruby/20') }.to raise_error(Regexp::Syntax::InvalidVersionNameError)
31
+ end
32
+
33
+ specify('version class tiny version') do
34
+ expect(Regexp::Syntax.version_class('ruby/1.9.3')).to eq Regexp::Syntax::V1_9_3
35
+
36
+ expect(Regexp::Syntax.version_class('ruby/2.3.1')).to eq Regexp::Syntax::V2_3_1
37
+ end
38
+
39
+ specify('version class minor version') do
40
+ expect(Regexp::Syntax.version_class('ruby/1.9')).to eq Regexp::Syntax::V1_9
41
+
42
+ expect(Regexp::Syntax.version_class('ruby/2.3')).to eq Regexp::Syntax::V2_3
43
+ end
44
+
45
+ specify('raises for unknown constant lookups') do
46
+ expect { Regexp::Syntax::V1 }.to raise_error(/V1/)
47
+ end
48
+ end
@@ -0,0 +1,23 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Syntax::Token::Map) do
4
+ let(:map) { Regexp::Syntax::Token::Map }
5
+
6
+ specify('is complete') do
7
+ latest_syntax = Regexp::Syntax.new('ruby/2.9')
8
+
9
+ latest_syntax.features.each do |type, tokens|
10
+ tokens.each { |token| expect(map[type]).to include(token) }
11
+ end
12
+ end
13
+
14
+ specify('contains no duplicate type/token combinations') do
15
+ combinations = map.flat_map do |type, tokens|
16
+ tokens.map { |token| "#{type} #{token}" }
17
+ end
18
+
19
+ non_uniq = combinations.group_by { |str| str }.select { |_, v| v.count > 1 }
20
+
21
+ expect(non_uniq.keys).to be_empty
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Syntax::V1_8_6) do
4
+ include_examples 'syntax', Regexp::Syntax.new('ruby/1.8.6'),
5
+ implements: {
6
+ assertion: T::Assertion::Lookahead,
7
+ backref: [:number],
8
+ escape: T::Escape::Basic + T::Escape::ASCII + T::Escape::Meta + T::Escape::Control,
9
+ group: T::Group::V1_8_6,
10
+ quantifier: T::Quantifier::Greedy + T::Quantifier::Reluctant + T::Quantifier::Interval + T::Quantifier::IntervalReluctant
11
+ },
12
+ excludes: {
13
+ assertion: T::Assertion::Lookbehind,
14
+ backref: T::Backreference::All - [:number] + T::SubexpressionCall::All,
15
+ quantifier: T::Quantifier::Possessive
16
+ }
17
+ end
@@ -0,0 +1,10 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe(Regexp::Syntax::V1_9_1) do
4
+ include_examples 'syntax', Regexp::Syntax.new('ruby/1.9.1'),
5
+ implements: {
6
+ escape: T::Escape::Hex + T::Escape::Octal + T::Escape::Unicode,
7
+ type: T::CharacterType::Hex,
8
+ quantifier: T::Quantifier::Greedy + T::Quantifier::Reluctant + T::Quantifier::Possessive
9
+ }
10
+ end