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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/lib/regexp_parser/expression.rb +6 -43
- data/lib/regexp_parser/expression/classes/conditional.rb +3 -2
- data/lib/regexp_parser/expression/classes/escape.rb +0 -4
- data/lib/regexp_parser/expression/methods/match.rb +13 -0
- data/lib/regexp_parser/expression/methods/options.rb +35 -0
- data/lib/regexp_parser/expression/methods/strfregexp.rb +0 -1
- data/lib/regexp_parser/expression/methods/tests.rb +6 -15
- data/lib/regexp_parser/expression/sequence.rb +3 -2
- data/lib/regexp_parser/expression/sequence_operation.rb +2 -6
- data/lib/regexp_parser/lexer.rb +0 -21
- data/lib/regexp_parser/parser.rb +22 -21
- data/lib/regexp_parser/scanner.rb +1159 -1329
- data/lib/regexp_parser/scanner/char_type.rl +0 -3
- data/lib/regexp_parser/scanner/scanner.rl +82 -190
- data/lib/regexp_parser/version.rb +1 -1
- data/spec/expression/base_spec.rb +14 -0
- data/spec/expression/methods/match_length_spec.rb +13 -0
- data/spec/expression/methods/match_spec.rb +25 -0
- data/spec/expression/methods/tests_spec.rb +2 -0
- data/spec/expression/options_spec.rb +128 -0
- data/spec/expression/root_spec.rb +9 -0
- data/spec/expression/sequence_spec.rb +9 -0
- data/spec/lexer/conditionals_spec.rb +49 -119
- data/spec/lexer/escapes_spec.rb +8 -32
- data/spec/lexer/keep_spec.rb +5 -17
- data/spec/lexer/literals_spec.rb +73 -110
- data/spec/lexer/nesting_spec.rb +86 -117
- data/spec/lexer/refcalls_spec.rb +51 -50
- data/spec/parser/all_spec.rb +13 -1
- data/spec/parser/anchors_spec.rb +9 -23
- data/spec/parser/conditionals_spec.rb +9 -9
- data/spec/parser/errors_spec.rb +22 -43
- data/spec/parser/escapes_spec.rb +33 -44
- data/spec/parser/groups_spec.rb +98 -257
- data/spec/parser/keep_spec.rb +2 -15
- data/spec/parser/posix_classes_spec.rb +5 -24
- data/spec/parser/properties_spec.rb +42 -54
- data/spec/parser/quantifiers_spec.rb +41 -283
- data/spec/parser/refcalls_spec.rb +60 -185
- data/spec/parser/set/intersections_spec.rb +17 -17
- data/spec/parser/set/ranges_spec.rb +17 -17
- data/spec/parser/sets_spec.rb +5 -5
- data/spec/parser/types_spec.rb +11 -36
- data/spec/scanner/anchors_spec.rb +13 -28
- data/spec/scanner/conditionals_spec.rb +121 -173
- data/spec/scanner/errors_spec.rb +65 -87
- data/spec/scanner/escapes_spec.rb +49 -50
- data/spec/scanner/free_space_spec.rb +102 -165
- data/spec/scanner/groups_spec.rb +45 -64
- data/spec/scanner/keep_spec.rb +5 -28
- data/spec/scanner/literals_spec.rb +45 -81
- data/spec/scanner/meta_spec.rb +13 -33
- data/spec/scanner/properties_spec.rb +43 -286
- data/spec/scanner/quantifiers_spec.rb +13 -28
- data/spec/scanner/refcalls_spec.rb +32 -48
- data/spec/scanner/sets_spec.rb +88 -102
- data/spec/scanner/types_spec.rb +10 -25
- data/spec/spec_helper.rb +1 -0
- data/spec/support/shared_examples.rb +77 -0
- data/spec/syntax/syntax_spec.rb +4 -0
- data/spec/syntax/versions/1.8.6_spec.rb +12 -33
- data/spec/syntax/versions/1.9.1_spec.rb +5 -18
- data/spec/syntax/versions/1.9.3_spec.rb +4 -17
- data/spec/syntax/versions/2.0.0_spec.rb +8 -23
- data/spec/syntax/versions/2.2.0_spec.rb +4 -17
- data/spec/syntax/versions/aliases_spec.rb +25 -109
- metadata +14 -6
- data/spec/scanner/scripts_spec.rb +0 -49
- 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
|
-
|
5
|
-
|
6
|
-
|
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
|
-
|
10
|
-
|
11
|
-
|
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
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
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
|
data/spec/scanner/sets_spec.rb
CHANGED
@@ -1,108 +1,94 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe('Set scanning') do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
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
|
data/spec/scanner/types_spec.rb
CHANGED
@@ -1,29 +1,14 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
RSpec.describe('Type scanning') do
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
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
|
data/spec/spec_helper.rb
CHANGED
@@ -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
|