regexp_parser 1.5.0 → 1.5.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +14 -0
  3. data/lib/regexp_parser/expression.rb +6 -43
  4. data/lib/regexp_parser/expression/classes/conditional.rb +3 -2
  5. data/lib/regexp_parser/expression/classes/escape.rb +0 -4
  6. data/lib/regexp_parser/expression/methods/match.rb +13 -0
  7. data/lib/regexp_parser/expression/methods/options.rb +35 -0
  8. data/lib/regexp_parser/expression/methods/strfregexp.rb +0 -1
  9. data/lib/regexp_parser/expression/methods/tests.rb +6 -15
  10. data/lib/regexp_parser/expression/sequence.rb +3 -2
  11. data/lib/regexp_parser/expression/sequence_operation.rb +2 -6
  12. data/lib/regexp_parser/lexer.rb +0 -21
  13. data/lib/regexp_parser/parser.rb +22 -21
  14. data/lib/regexp_parser/scanner.rb +1159 -1329
  15. data/lib/regexp_parser/scanner/char_type.rl +0 -3
  16. data/lib/regexp_parser/scanner/scanner.rl +82 -190
  17. data/lib/regexp_parser/version.rb +1 -1
  18. data/spec/expression/base_spec.rb +14 -0
  19. data/spec/expression/methods/match_length_spec.rb +13 -0
  20. data/spec/expression/methods/match_spec.rb +25 -0
  21. data/spec/expression/methods/tests_spec.rb +2 -0
  22. data/spec/expression/options_spec.rb +128 -0
  23. data/spec/expression/root_spec.rb +9 -0
  24. data/spec/expression/sequence_spec.rb +9 -0
  25. data/spec/lexer/conditionals_spec.rb +49 -119
  26. data/spec/lexer/escapes_spec.rb +8 -32
  27. data/spec/lexer/keep_spec.rb +5 -17
  28. data/spec/lexer/literals_spec.rb +73 -110
  29. data/spec/lexer/nesting_spec.rb +86 -117
  30. data/spec/lexer/refcalls_spec.rb +51 -50
  31. data/spec/parser/all_spec.rb +13 -1
  32. data/spec/parser/anchors_spec.rb +9 -23
  33. data/spec/parser/conditionals_spec.rb +9 -9
  34. data/spec/parser/errors_spec.rb +22 -43
  35. data/spec/parser/escapes_spec.rb +33 -44
  36. data/spec/parser/groups_spec.rb +98 -257
  37. data/spec/parser/keep_spec.rb +2 -15
  38. data/spec/parser/posix_classes_spec.rb +5 -24
  39. data/spec/parser/properties_spec.rb +42 -54
  40. data/spec/parser/quantifiers_spec.rb +41 -283
  41. data/spec/parser/refcalls_spec.rb +60 -185
  42. data/spec/parser/set/intersections_spec.rb +17 -17
  43. data/spec/parser/set/ranges_spec.rb +17 -17
  44. data/spec/parser/sets_spec.rb +5 -5
  45. data/spec/parser/types_spec.rb +11 -36
  46. data/spec/scanner/anchors_spec.rb +13 -28
  47. data/spec/scanner/conditionals_spec.rb +121 -173
  48. data/spec/scanner/errors_spec.rb +65 -87
  49. data/spec/scanner/escapes_spec.rb +49 -50
  50. data/spec/scanner/free_space_spec.rb +102 -165
  51. data/spec/scanner/groups_spec.rb +45 -64
  52. data/spec/scanner/keep_spec.rb +5 -28
  53. data/spec/scanner/literals_spec.rb +45 -81
  54. data/spec/scanner/meta_spec.rb +13 -33
  55. data/spec/scanner/properties_spec.rb +43 -286
  56. data/spec/scanner/quantifiers_spec.rb +13 -28
  57. data/spec/scanner/refcalls_spec.rb +32 -48
  58. data/spec/scanner/sets_spec.rb +88 -102
  59. data/spec/scanner/types_spec.rb +10 -25
  60. data/spec/spec_helper.rb +1 -0
  61. data/spec/support/shared_examples.rb +77 -0
  62. data/spec/syntax/syntax_spec.rb +4 -0
  63. data/spec/syntax/versions/1.8.6_spec.rb +12 -33
  64. data/spec/syntax/versions/1.9.1_spec.rb +5 -18
  65. data/spec/syntax/versions/1.9.3_spec.rb +4 -17
  66. data/spec/syntax/versions/2.0.0_spec.rb +8 -23
  67. data/spec/syntax/versions/2.2.0_spec.rb +4 -17
  68. data/spec/syntax/versions/aliases_spec.rb +25 -109
  69. metadata +14 -6
  70. data/spec/scanner/scripts_spec.rb +0 -49
  71. data/spec/scanner/unicode_blocks_spec.rb +0 -28
@@ -1,35 +1,20 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe('Quantifier scanning') do
4
- tests = {
5
- 'a?' => [:quantifier, :zero_or_one, '?'],
6
- 'a??' => [:quantifier, :zero_or_one_reluctant, '??'],
7
- 'a?+' => [:quantifier, :zero_or_one_possessive, '?+'],
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]
8
7
 
9
- 'a*' => [:quantifier, :zero_or_more, '*'],
10
- 'a*?' => [:quantifier, :zero_or_more_reluctant, '*?'],
11
- 'a*+' => [:quantifier, :zero_or_more_possessive, '*+'],
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]
12
11
 
13
- 'a+' => [:quantifier, :one_or_more, '+'],
14
- 'a+?' => [:quantifier, :one_or_more_reluctant, '+?'],
15
- 'a++' => [:quantifier, :one_or_more_possessive, '++'],
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]
16
15
 
17
- 'a{2}' => [:quantifier, :interval, '{2}'],
18
- 'a{2,}' => [:quantifier, :interval, '{2,}'],
19
- 'a{,2}' => [:quantifier, :interval, '{,2}'],
20
- 'a{2,4}' => [:quantifier, :interval, '{2,4}'],
21
- }
22
-
23
- tests.each_with_index do |(pattern, (type, token, text)), count|
24
- name = token == :interval ? "interval_#{count}" : token
25
-
26
- specify("scan_#{type}_#{name}") do
27
- tokens = RS.scan(pattern)
28
- result = tokens.last
29
-
30
- expect(result[0]).to eq type
31
- expect(result[1]).to eq token
32
- expect(result[2]).to eq text
33
- end
34
- end
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]
35
20
  end
@@ -1,52 +1,36 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe('RefCall scanning') do
4
- tests = {
5
- # Traditional numerical group back-reference
6
- '(abc)\1' => [3, :backref, :number, '\1', 5, 7],
7
-
8
- # Group back-references, named, numbered, and relative
9
- '(?<X>abc)\k<X>' => [3, :backref, :name_ref_ab, '\k<X>', 9, 14],
10
- "(?<X>abc)\\k'X'" => [3, :backref, :name_ref_sq, "\\k'X'", 9, 14],
11
-
12
- '(abc)\k<1>' => [3, :backref, :number_ref_ab, '\k<1>', 5, 10],
13
- "(abc)\\k'1'" => [3, :backref, :number_ref_sq, "\\k'1'", 5, 10],
14
-
15
- '(abc)\k<-1>' => [3, :backref, :number_rel_ref_ab, '\k<-1>', 5, 11],
16
- "(abc)\\k'-1'" => [3, :backref, :number_rel_ref_sq, "\\k'-1'", 5, 11],
17
-
18
- # Sub-expression invocation, named, numbered, and relative
19
- '(?<X>abc)\g<X>' => [3, :backref, :name_call_ab, '\g<X>', 9, 14],
20
- "(?<X>abc)\\g'X'" => [3, :backref, :name_call_sq, "\\g'X'", 9, 14],
21
-
22
- '(abc)\g<1>' => [3, :backref, :number_call_ab, '\g<1>', 5, 10],
23
- "(abc)\\g'1'" => [3, :backref, :number_call_sq, "\\g'1'", 5, 10],
24
-
25
- '(abc)\g<-1>' => [3, :backref, :number_rel_call_ab, '\g<-1>', 5, 11],
26
- "(abc)\\g'-1'" => [3, :backref, :number_rel_call_sq, "\\g'-1'", 5, 11],
27
-
28
- '\g<+1>(abc)' => [0, :backref, :number_rel_call_ab, '\g<+1>', 0, 6],
29
- "\\g'+1'(abc)" => [0, :backref, :number_rel_call_sq, "\\g'+1'", 0, 6],
30
-
31
- # Group back-references, with recursion level
32
- '(?<X>abc)\k<X-0>' => [3, :backref, :name_recursion_ref_ab, '\k<X-0>', 9, 16],
33
- "(?<X>abc)\\k'X-0'" => [3, :backref, :name_recursion_ref_sq, "\\k'X-0'", 9, 16],
34
-
35
- '(abc)\k<1-0>' => [3, :backref, :number_recursion_ref_ab, '\k<1-0>', 5, 12],
36
- "(abc)\\k'1-0'" => [3, :backref, :number_recursion_ref_sq, "\\k'1-0'", 5, 12],
37
- }
38
-
39
- tests.each_with_index do |(pattern, (index, type, token, text, ts, te)), count|
40
- specify("scanner_#{type}_#{token}_#{count}") do
41
- tokens = RS.scan(pattern)
42
- result = tokens[index]
43
-
44
- expect(result[0]).to eq type
45
- expect(result[1]).to eq token
46
- expect(result[2]).to eq text
47
- expect(result[3]).to eq ts
48
- expect(result[4]).to eq te
49
- expect(pattern[ts, te]).to eq text
50
- end
51
- end
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]
52
36
  end
@@ -1,108 +1,94 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe('Set scanning') do
4
- tests = {
5
- '[a]' => [0, :set, :open, '[', 0, 1],
6
- '[b]' => [2, :set, :close, ']', 2, 3],
7
- '[^n]' => [1, :set, :negate, '^', 1, 2],
8
-
9
- '[c]' => [1, :literal, :literal, 'c', 1, 2],
10
- '[\b]' => [1, :escape, :backspace, '\b', 1, 3],
11
- '[A\bX]' => [2, :escape, :backspace, '\b', 2, 4],
12
-
13
- '[.]' => [1, :literal, :literal, '.', 1, 2],
14
- '[?]' => [1, :literal, :literal, '?', 1, 2],
15
- '[*]' => [1, :literal, :literal, '*', 1, 2],
16
- '[+]' => [1, :literal, :literal, '+', 1, 2],
17
- '[{]' => [1, :literal, :literal, '{', 1, 2],
18
- '[}]' => [1, :literal, :literal, '}', 1, 2],
19
- '[<]' => [1, :literal, :literal, '<', 1, 2],
20
- '[>]' => [1, :literal, :literal, '>', 1, 2],
21
-
22
- '[äöü]' => [2, :literal, :literal, 'ö', 3, 5],
23
-
24
- '[\x20]' => [1, :escape, :hex, '\x20', 1, 5],
25
-
26
- '[\.]' => [1, :escape, :dot, '\.', 1, 3],
27
- '[\!]' => [1, :escape, :literal, '\!', 1, 3],
28
- '[\#]' => [1, :escape, :literal, '\#', 1, 3],
29
- '[\]]' => [1, :escape, :set_close, '\]', 1, 3],
30
- '[\\\]' => [1, :escape, :backslash, '\\\\', 1, 3],
31
- '[\A]' => [1, :escape, :literal, '\A', 1, 3],
32
- '[\z]' => [1, :escape, :literal, '\z', 1, 3],
33
- '[\g]' => [1, :escape, :literal, '\g', 1, 3],
34
- '[\K]' => [1, :escape, :literal, '\K', 1, 3],
35
- '[\c2]' => [1, :escape, :literal, '\c', 1, 3],
36
- '[\B]' => [1, :escape, :literal, '\B', 1, 3],
37
- '[a\-c]' => [2, :escape, :literal, '\-', 2, 4],
38
-
39
- '[\d]' => [1, :type, :digit, '\d', 1, 3],
40
- '[\da-z]' => [1, :type, :digit, '\d', 1, 3],
41
- '[\D]' => [1, :type, :nondigit, '\D', 1, 3],
42
-
43
- '[\h]' => [1, :type, :hex, '\h', 1, 3],
44
- '[\H]' => [1, :type, :nonhex, '\H', 1, 3],
45
-
46
- '[\s]' => [1, :type, :space, '\s', 1, 3],
47
- '[\S]' => [1, :type, :nonspace, '\S', 1, 3],
48
-
49
- '[\w]' => [1, :type, :word, '\w', 1, 3],
50
- '[\W]' => [1, :type, :nonword, '\W', 1, 3],
51
-
52
- '[\R]' => [1, :escape, :literal, '\R', 1, 3],
53
- '[\X]' => [1, :escape, :literal, '\X', 1, 3],
54
-
55
- '[a-b]' => [1, :literal, :literal, 'a', 1, 2],
56
- '[a-c]' => [2, :set, :range, '-', 2, 3],
57
- '[a-d]' => [3, :literal, :literal, 'd', 3, 4],
58
- '[a-b-]' => [4, :literal, :literal, '-', 4, 6],
59
- '[-a]' => [1, :literal, :literal, '-', 1, 2],
60
- '[a-c^]' => [4, :literal, :literal, '^', 4, 5],
61
- '[a-bd-f]' => [2, :set, :range, '-', 2, 3],
62
- '[a-cd-f]' => [5, :set, :range, '-', 5, 6],
63
-
64
- '[a[:digit:]c]' => [2, :posixclass, :digit, '[:digit:]', 2, 11],
65
- '[[:digit:][:space:]]' => [2, :posixclass, :space, '[:space:]', 10, 19],
66
- '[[:^digit:]]' => [1, :nonposixclass, :digit, '[:^digit:]', 1, 11],
67
-
68
- '[a[.a-b.]c]' => [2, :set, :collation, '[.a-b.]', 2, 9],
69
- '[a[=e=]c]' => [2, :set, :equivalent, '[=e=]', 2, 7],
70
-
71
- '[a-d&&g-h]' => [4, :set, :intersection, '&&', 4, 6],
72
- '[a&&]' => [2, :set, :intersection, '&&', 2, 4],
73
- '[&&z]' => [1, :set, :intersection, '&&', 1, 3],
74
-
75
- '[\\x20-\\x27]' => [1, :escape, :hex, '\x20', 1, 5],
76
- '[\\x20-\\x28]' => [2, :set, :range, '-', 5, 6],
77
- '[\\x20-\\x29]' => [3, :escape, :hex, '\x29', 6, 10],
78
-
79
- '[a\p{digit}c]' => [2, :property, :digit, '\p{digit}', 2, 11],
80
- '[a\P{digit}c]' => [2, :nonproperty, :digit, '\P{digit}', 2, 11],
81
- '[a\p{^digit}c]' => [2, :nonproperty, :digit, '\p{^digit}', 2, 12],
82
- '[a\P{^digit}c]' => [2, :property, :digit, '\P{^digit}', 2, 12],
83
-
84
- '[a\p{ALPHA}c]' => [2, :property, :alpha, '\p{ALPHA}', 2, 11],
85
- '[a\p{P}c]' => [2, :property, :punctuation,'\p{P}', 2, 7],
86
- '[a\p{P}\P{P}c]' => [3, :nonproperty, :punctuation,'\P{P}', 7, 12],
87
-
88
- '[a-w&&[^c-g]z]' => [5, :set, :open, '[', 6, 7],
89
- '[a-w&&[^c-h]z]' => [6, :set, :negate, '^', 7, 8],
90
- '[a-w&&[^c-i]z]' => [8, :set, :range, '-', 9, 10],
91
- '[a-w&&[^c-j]z]' => [10,:set, :close, ']', 11, 12],
92
- }
93
-
94
- tests.each_with_index do |(pattern, (index, type, token, text, ts, te)), count|
95
- specify("scanner_#{type}_#{token}_in_'#{pattern}'_#{count}") do
96
- tokens = RS.scan(pattern)
97
- result = tokens.at(index)
98
-
99
- expect(result[0]).to eq type
100
- expect(result[1]).to eq token
101
- expect(result[2]).to eq text
102
- expect(result[3]).to eq ts
103
- expect(result[4]).to eq te
104
- end
105
- end
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]
106
92
 
107
93
  specify('set literal encoding') do
108
94
  text = RS.scan('[a]')[1][2].to_s
@@ -1,29 +1,14 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  RSpec.describe('Type scanning') do
4
- tests = {
5
- 'a\\dc' => [1, :type, :digit, '\\d', 1, 3],
6
- 'a\\Dc' => [1, :type, :nondigit, '\\D', 1, 3],
7
- 'a\\hc' => [1, :type, :hex, '\\h', 1, 3],
8
- 'a\\Hc' => [1, :type, :nonhex, '\\H', 1, 3],
9
- 'a\\sc' => [1, :type, :space, '\\s', 1, 3],
10
- 'a\\Sc' => [1, :type, :nonspace, '\\S', 1, 3],
11
- 'a\\wc' => [1, :type, :word, '\\w', 1, 3],
12
- 'a\\Wc' => [1, :type, :nonword, '\\W', 1, 3],
13
- 'a\\Rc' => [1, :type, :linebreak, '\\R', 1, 3],
14
- 'a\\Xc' => [1, :type, :xgrapheme, '\\X', 1, 3],
15
- }
16
-
17
- tests.each do |(pattern, (index, type, token, text, ts, te))|
18
- specify("scanner_#{type}_#{token}") do
19
- tokens = RS.scan(pattern)
20
- result = tokens.at(index)
21
-
22
- expect(result[0]).to eq type
23
- expect(result[1]).to eq token
24
- expect(result[2]).to eq text
25
- expect(result[3]).to eq ts
26
- expect(result[4]).to eq te
27
- end
28
- end
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]
29
14
  end
@@ -1,5 +1,6 @@
1
1
  require 'regexp_parser'
2
2
  require 'regexp_property_values'
3
+ require_relative 'support/shared_examples'
3
4
 
4
5
  RS = Regexp::Scanner
5
6
  RL = Regexp::Lexer
@@ -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