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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +72 -1
- data/Gemfile +3 -3
- data/README.md +12 -19
- data/Rakefile +3 -4
- data/lib/regexp_parser/expression.rb +28 -53
- data/lib/regexp_parser/expression/classes/backref.rb +18 -10
- data/lib/regexp_parser/expression/classes/conditional.rb +7 -2
- data/lib/regexp_parser/expression/classes/escape.rb +0 -4
- data/lib/regexp_parser/expression/classes/group.rb +4 -2
- data/lib/regexp_parser/expression/classes/keep.rb +1 -3
- data/lib/regexp_parser/expression/methods/match.rb +13 -0
- data/lib/regexp_parser/expression/methods/match_length.rb +172 -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/methods/traverse.rb +3 -1
- data/lib/regexp_parser/expression/quantifier.rb +2 -2
- data/lib/regexp_parser/expression/sequence.rb +3 -6
- data/lib/regexp_parser/expression/sequence_operation.rb +2 -6
- data/lib/regexp_parser/expression/subexpression.rb +3 -5
- data/lib/regexp_parser/lexer.rb +30 -44
- data/lib/regexp_parser/parser.rb +47 -24
- data/lib/regexp_parser/scanner.rb +1228 -1367
- data/lib/regexp_parser/scanner/char_type.rl +0 -3
- data/lib/regexp_parser/scanner/properties/long.yml +34 -1
- data/lib/regexp_parser/scanner/properties/short.yml +12 -0
- data/lib/regexp_parser/scanner/scanner.rl +101 -194
- data/lib/regexp_parser/syntax/tokens.rb +2 -10
- data/lib/regexp_parser/syntax/tokens/unicode_property.rb +72 -21
- data/lib/regexp_parser/syntax/versions/2.6.0.rb +10 -0
- data/lib/regexp_parser/syntax/versions/2.6.2.rb +10 -0
- data/lib/regexp_parser/syntax/versions/2.6.3.rb +10 -0
- data/lib/regexp_parser/version.rb +1 -1
- data/regexp_parser.gemspec +3 -3
- data/spec/expression/base_spec.rb +94 -0
- data/spec/expression/clone_spec.rb +120 -0
- data/spec/expression/conditional_spec.rb +89 -0
- data/spec/expression/free_space_spec.rb +27 -0
- data/spec/expression/methods/match_length_spec.rb +161 -0
- data/spec/expression/methods/match_spec.rb +25 -0
- data/spec/expression/methods/strfregexp_spec.rb +224 -0
- data/spec/expression/methods/tests_spec.rb +99 -0
- data/spec/expression/methods/traverse_spec.rb +161 -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/expression/subexpression_spec.rb +50 -0
- data/spec/expression/to_h_spec.rb +26 -0
- data/spec/expression/to_s_spec.rb +100 -0
- data/spec/lexer/all_spec.rb +22 -0
- data/spec/lexer/conditionals_spec.rb +53 -0
- data/spec/lexer/delimiters_spec.rb +68 -0
- data/spec/lexer/escapes_spec.rb +14 -0
- data/spec/lexer/keep_spec.rb +10 -0
- data/spec/lexer/literals_spec.rb +89 -0
- data/spec/lexer/nesting_spec.rb +99 -0
- data/spec/lexer/refcalls_spec.rb +55 -0
- data/spec/parser/all_spec.rb +43 -0
- data/spec/parser/alternation_spec.rb +88 -0
- data/spec/parser/anchors_spec.rb +17 -0
- data/spec/parser/conditionals_spec.rb +179 -0
- data/spec/parser/errors_spec.rb +30 -0
- data/spec/parser/escapes_spec.rb +121 -0
- data/spec/parser/free_space_spec.rb +130 -0
- data/spec/parser/groups_spec.rb +108 -0
- data/spec/parser/keep_spec.rb +6 -0
- data/spec/parser/posix_classes_spec.rb +8 -0
- data/spec/parser/properties_spec.rb +115 -0
- data/spec/parser/quantifiers_spec.rb +52 -0
- data/spec/parser/refcalls_spec.rb +112 -0
- data/spec/parser/set/intersections_spec.rb +127 -0
- data/spec/parser/set/ranges_spec.rb +111 -0
- data/spec/parser/sets_spec.rb +178 -0
- data/spec/parser/types_spec.rb +18 -0
- data/spec/scanner/all_spec.rb +18 -0
- data/spec/scanner/anchors_spec.rb +21 -0
- data/spec/scanner/conditionals_spec.rb +128 -0
- data/spec/scanner/delimiters_spec.rb +52 -0
- data/spec/scanner/errors_spec.rb +67 -0
- data/spec/scanner/escapes_spec.rb +53 -0
- data/spec/scanner/free_space_spec.rb +133 -0
- data/spec/scanner/groups_spec.rb +52 -0
- data/spec/scanner/keep_spec.rb +10 -0
- data/spec/scanner/literals_spec.rb +49 -0
- data/spec/scanner/meta_spec.rb +18 -0
- data/spec/scanner/properties_spec.rb +64 -0
- data/spec/scanner/quantifiers_spec.rb +20 -0
- data/spec/scanner/refcalls_spec.rb +36 -0
- data/spec/scanner/sets_spec.rb +102 -0
- data/spec/scanner/types_spec.rb +14 -0
- data/spec/spec_helper.rb +15 -0
- data/{test → spec}/support/runner.rb +9 -8
- data/spec/support/shared_examples.rb +77 -0
- data/{test → spec}/support/warning_extractor.rb +5 -7
- data/spec/syntax/syntax_spec.rb +48 -0
- data/spec/syntax/syntax_token_map_spec.rb +23 -0
- data/spec/syntax/versions/1.8.6_spec.rb +17 -0
- data/spec/syntax/versions/1.9.1_spec.rb +10 -0
- data/spec/syntax/versions/1.9.3_spec.rb +9 -0
- data/spec/syntax/versions/2.0.0_spec.rb +13 -0
- data/spec/syntax/versions/2.2.0_spec.rb +9 -0
- data/spec/syntax/versions/aliases_spec.rb +37 -0
- data/spec/token/token_spec.rb +85 -0
- metadata +151 -146
- data/test/expression/test_all.rb +0 -12
- data/test/expression/test_base.rb +0 -90
- data/test/expression/test_clone.rb +0 -89
- data/test/expression/test_conditionals.rb +0 -113
- data/test/expression/test_free_space.rb +0 -35
- data/test/expression/test_set.rb +0 -84
- data/test/expression/test_strfregexp.rb +0 -230
- data/test/expression/test_subexpression.rb +0 -58
- data/test/expression/test_tests.rb +0 -99
- data/test/expression/test_to_h.rb +0 -59
- data/test/expression/test_to_s.rb +0 -104
- data/test/expression/test_traverse.rb +0 -161
- data/test/helpers.rb +0 -10
- data/test/lexer/test_all.rb +0 -41
- data/test/lexer/test_conditionals.rb +0 -127
- data/test/lexer/test_keep.rb +0 -24
- data/test/lexer/test_literals.rb +0 -130
- data/test/lexer/test_nesting.rb +0 -132
- data/test/lexer/test_refcalls.rb +0 -56
- data/test/parser/set/test_intersections.rb +0 -127
- data/test/parser/set/test_ranges.rb +0 -111
- data/test/parser/test_all.rb +0 -64
- data/test/parser/test_alternation.rb +0 -92
- data/test/parser/test_anchors.rb +0 -34
- data/test/parser/test_conditionals.rb +0 -187
- data/test/parser/test_errors.rb +0 -63
- data/test/parser/test_escapes.rb +0 -134
- data/test/parser/test_free_space.rb +0 -139
- data/test/parser/test_groups.rb +0 -289
- data/test/parser/test_keep.rb +0 -21
- data/test/parser/test_posix_classes.rb +0 -27
- data/test/parser/test_properties.rb +0 -133
- data/test/parser/test_quantifiers.rb +0 -301
- data/test/parser/test_refcalls.rb +0 -186
- data/test/parser/test_sets.rb +0 -179
- data/test/parser/test_types.rb +0 -50
- data/test/scanner/test_all.rb +0 -38
- data/test/scanner/test_anchors.rb +0 -38
- data/test/scanner/test_conditionals.rb +0 -184
- data/test/scanner/test_errors.rb +0 -91
- data/test/scanner/test_escapes.rb +0 -56
- data/test/scanner/test_free_space.rb +0 -200
- data/test/scanner/test_groups.rb +0 -79
- data/test/scanner/test_keep.rb +0 -35
- data/test/scanner/test_literals.rb +0 -89
- data/test/scanner/test_meta.rb +0 -40
- data/test/scanner/test_properties.rb +0 -312
- data/test/scanner/test_quantifiers.rb +0 -37
- data/test/scanner/test_refcalls.rb +0 -52
- data/test/scanner/test_scripts.rb +0 -53
- data/test/scanner/test_sets.rb +0 -119
- data/test/scanner/test_types.rb +0 -35
- data/test/scanner/test_unicode_blocks.rb +0 -30
- data/test/support/disable_autotest.rb +0 -8
- data/test/syntax/test_all.rb +0 -6
- data/test/syntax/test_syntax.rb +0 -61
- data/test/syntax/test_syntax_token_map.rb +0 -25
- data/test/syntax/versions/test_1.8.rb +0 -55
- data/test/syntax/versions/test_1.9.1.rb +0 -36
- data/test/syntax/versions/test_1.9.3.rb +0 -32
- data/test/syntax/versions/test_2.0.0.rb +0 -37
- data/test/syntax/versions/test_2.2.0.rb +0 -32
- data/test/syntax/versions/test_aliases.rb +0 -129
- data/test/syntax/versions/test_all.rb +0 -5
- data/test/test_all.rb +0 -5
- data/test/token/test_all.rb +0 -2
- data/test/token/test_token.rb +0 -107
data/test/scanner/test_errors.rb
DELETED
@@ -1,91 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helpers", __FILE__)
|
2
|
-
|
3
|
-
class ScannerErrors < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def test_scanner_unbalanced_set
|
6
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('[[:alpha:]') }
|
7
|
-
end
|
8
|
-
|
9
|
-
def test_scanner_unbalanced_group
|
10
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('(abc') }
|
11
|
-
end
|
12
|
-
|
13
|
-
def test_scanner_unbalanced_interval
|
14
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('a{1,2') }
|
15
|
-
end
|
16
|
-
|
17
|
-
def test_scanner_eof_in_property
|
18
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\p{asci') }
|
19
|
-
end
|
20
|
-
|
21
|
-
def test_scanner_incomplete_property
|
22
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\p{ascii abc') }
|
23
|
-
end
|
24
|
-
|
25
|
-
def test_scanner_unknown_property
|
26
|
-
assert_raise( RS::UnknownUnicodePropertyError ) { RS.scan('\p{foobar}') }
|
27
|
-
end
|
28
|
-
|
29
|
-
def test_scanner_incomplete_options
|
30
|
-
assert_raise( RS::ScannerError ) { RS.scan('(?mix abc)') }
|
31
|
-
end
|
32
|
-
|
33
|
-
def test_scanner_eof_options
|
34
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('(?mix') }
|
35
|
-
end
|
36
|
-
|
37
|
-
def test_scanner_incorrect_options
|
38
|
-
assert_raise( RS::ScannerError ) { RS.scan('(?mix^bc') }
|
39
|
-
end
|
40
|
-
|
41
|
-
def test_scanner_eof_escape
|
42
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\\') }
|
43
|
-
end
|
44
|
-
|
45
|
-
def test_scanner_eof_in_hex_escape
|
46
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\x') }
|
47
|
-
end
|
48
|
-
|
49
|
-
def test_scanner_eof_in_codepoint_escape
|
50
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u') }
|
51
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u0') }
|
52
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u00') }
|
53
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u000') }
|
54
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u{') }
|
55
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u{00') }
|
56
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u{0000') }
|
57
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u{0000 ') }
|
58
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\u{0000 0000') }
|
59
|
-
end
|
60
|
-
|
61
|
-
def test_scanner_eof_in_control_sequence
|
62
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\c') }
|
63
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\c\M') }
|
64
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\c\M-') }
|
65
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\C') }
|
66
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\C-') }
|
67
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\C-\M') }
|
68
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\C-\M-') }
|
69
|
-
end
|
70
|
-
|
71
|
-
def test_scanner_eof_in_meta_sequence
|
72
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M') }
|
73
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M-') }
|
74
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M-\\') }
|
75
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M-\c') }
|
76
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M-\C') }
|
77
|
-
assert_raise( RS::PrematureEndError ) { RS.scan('\M-\C-') }
|
78
|
-
end
|
79
|
-
|
80
|
-
def test_scanner_invalid_hex_escape
|
81
|
-
assert_raise( RS::InvalidSequenceError ) { RS.scan('\xZ') }
|
82
|
-
assert_raise( RS::InvalidSequenceError ) { RS.scan('\xZ0') }
|
83
|
-
end
|
84
|
-
|
85
|
-
def test_scanner_invalid_named_group
|
86
|
-
assert_raise( RS::InvalidGroupError ) { RS.scan("(?'')") }
|
87
|
-
assert_raise( RS::InvalidGroupError ) { RS.scan("(?''empty-name)") }
|
88
|
-
assert_raise( RS::InvalidGroupError ) { RS.scan("(?<>)") }
|
89
|
-
assert_raise( RS::InvalidGroupError ) { RS.scan("(?<>empty-name)") }
|
90
|
-
end
|
91
|
-
end
|
@@ -1,56 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helpers", __FILE__)
|
2
|
-
|
3
|
-
class ScannerEscapes < Test::Unit::TestCase
|
4
|
-
|
5
|
-
tests = {
|
6
|
-
/c\at/ => [1, :escape, :bell, '\a', 1, 3],
|
7
|
-
|
8
|
-
# not an escape outside a character set
|
9
|
-
/c\bt/ => [1, :anchor, :word_boundary, '\b', 1, 3],
|
10
|
-
|
11
|
-
/c\ft/ => [1, :escape, :form_feed, '\f', 1, 3],
|
12
|
-
/c\nt/ => [1, :escape, :newline, '\n', 1, 3],
|
13
|
-
/c\tt/ => [1, :escape, :tab, '\t', 1, 3],
|
14
|
-
/c\vt/ => [1, :escape, :vertical_tab, '\v', 1, 3],
|
15
|
-
|
16
|
-
'c\qt' => [1, :escape, :literal, '\q', 1, 3],
|
17
|
-
|
18
|
-
'a\012c' => [1, :escape, :octal, '\012', 1, 5],
|
19
|
-
'a\0124' => [1, :escape, :octal, '\012', 1, 5],
|
20
|
-
'\712+7' => [0, :escape, :octal, '\712', 0, 4],
|
21
|
-
|
22
|
-
'a\x24c' => [1, :escape, :hex, '\x24', 1, 5],
|
23
|
-
'a\x0640c' => [1, :escape, :hex, '\x06', 1, 5],
|
24
|
-
|
25
|
-
'a\u0640c' => [1, :escape, :codepoint, '\u0640', 1, 7],
|
26
|
-
'a\u{640 0641}c' => [1, :escape, :codepoint_list, '\u{640 0641}', 1, 13],
|
27
|
-
'a\u{10FFFF}c' => [1, :escape, :codepoint_list, '\u{10FFFF}', 1, 11],
|
28
|
-
|
29
|
-
/a\cBc/ => [1, :escape, :control, '\cB', 1, 4],
|
30
|
-
/a\C-bc/ => [1, :escape, :control, '\C-b', 1, 5],
|
31
|
-
/a\c\M-Bc/n => [1, :escape, :control, '\c\M-B', 1, 7],
|
32
|
-
/a\C-\M-Bc/n => [1, :escape, :control, '\C-\M-B', 1, 8],
|
33
|
-
|
34
|
-
/a\M-Bc/n => [1, :escape, :meta_sequence, '\M-B', 1, 5],
|
35
|
-
/a\M-\C-Bc/n => [1, :escape, :meta_sequence, '\M-\C-B', 1, 8],
|
36
|
-
/a\M-\cBc/n => [1, :escape, :meta_sequence, '\M-\cB', 1, 7],
|
37
|
-
|
38
|
-
'ab\\\xcd' => [1, :escape, :backslash, '\\\\', 2, 4],
|
39
|
-
'ab\\\0cd' => [1, :escape, :backslash, '\\\\', 2, 4],
|
40
|
-
'ab\\\Kcd' => [1, :escape, :backslash, '\\\\', 2, 4],
|
41
|
-
}
|
42
|
-
|
43
|
-
tests.each_with_index do |(pattern, (index, type, token, text, ts, te)), count|
|
44
|
-
define_method "test_scanner_#{type}_#{token}_#{count}" do
|
45
|
-
tokens = RS.scan(pattern)
|
46
|
-
result = tokens.at(index)
|
47
|
-
|
48
|
-
assert_equal type, result[0]
|
49
|
-
assert_equal token, result[1]
|
50
|
-
assert_equal text, result[2]
|
51
|
-
assert_equal ts, result[3]
|
52
|
-
assert_equal te, result[4]
|
53
|
-
end
|
54
|
-
end
|
55
|
-
|
56
|
-
end
|
@@ -1,200 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helpers", __FILE__)
|
2
|
-
|
3
|
-
class ScannerFreeSpace < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def test_scan_free_space_tokens
|
6
|
-
regexp = %r{
|
7
|
-
a
|
8
|
-
b ? c *
|
9
|
-
d {2,3}
|
10
|
-
e + | f +
|
11
|
-
}x
|
12
|
-
|
13
|
-
tokens = RS.scan(regexp)
|
14
|
-
|
15
|
-
0.upto(24) do |i|
|
16
|
-
if i.even?
|
17
|
-
assert_equal :free_space, tokens[i][0]
|
18
|
-
assert_equal :whitespace, tokens[i][1]
|
19
|
-
else
|
20
|
-
refute_equal :free_space, tokens[i][0]
|
21
|
-
refute_equal :whitespace, tokens[i][1]
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
[0, 2, 10, 14].each do |i|
|
26
|
-
assert_equal "\n ", tokens[i][2]
|
27
|
-
end
|
28
|
-
|
29
|
-
[4, 6, 8, 12].each do |i|
|
30
|
-
assert_equal ' ', tokens[i][2]
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
|
-
def test_scan_free_space_comments
|
35
|
-
regexp = %r{
|
36
|
-
a + # A + comment
|
37
|
-
b ? # B ? comment
|
38
|
-
c {2,3} # C {2,3} comment
|
39
|
-
d + | e + # D|E comment
|
40
|
-
}x
|
41
|
-
|
42
|
-
tokens = RS.scan(regexp)
|
43
|
-
|
44
|
-
[
|
45
|
-
[ 5, :free_space, :comment, "# A + comment\n", 11, 25],
|
46
|
-
[11, :free_space, :comment, "# B ? comment\n", 37, 51],
|
47
|
-
[17, :free_space, :comment, "# C {2,3} comment\n", 66, 84],
|
48
|
-
[29, :free_space, :comment, "# D|E comment\n", 100, 114],
|
49
|
-
].each do |index, type, token, text, ts, te|
|
50
|
-
result = tokens[index]
|
51
|
-
|
52
|
-
assert_equal type, result[0]
|
53
|
-
assert_equal token, result[1]
|
54
|
-
assert_equal text, result[2]
|
55
|
-
assert_equal ts, result[3]
|
56
|
-
assert_equal te, result[4]
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
|
-
def test_scan_free_space_inlined
|
61
|
-
# Matches 'a bcdef g'
|
62
|
-
regexp = /a b(?x:c d e)f g/
|
63
|
-
tokens = RS.scan(regexp)
|
64
|
-
|
65
|
-
[
|
66
|
-
[0, :literal, :literal, 'a b', 0, 3],
|
67
|
-
[1, :group, :options, '(?x:', 3, 7],
|
68
|
-
[2, :literal, :literal, 'c', 7, 8],
|
69
|
-
[3, :free_space, :whitespace, ' ', 8, 9],
|
70
|
-
[4, :literal, :literal, 'd', 9, 10],
|
71
|
-
[5, :free_space, :whitespace, ' ', 10, 11],
|
72
|
-
[6, :literal, :literal, 'e', 11, 12],
|
73
|
-
[7, :group, :close, ')', 12, 13],
|
74
|
-
[8, :literal, :literal, 'f g', 13, 16]
|
75
|
-
].each do |index, type, token, text, ts, te|
|
76
|
-
result = tokens[index]
|
77
|
-
|
78
|
-
assert_equal type, result[0]
|
79
|
-
assert_equal token, result[1]
|
80
|
-
assert_equal text, result[2]
|
81
|
-
assert_equal ts, result[3]
|
82
|
-
assert_equal te, result[4]
|
83
|
-
end
|
84
|
-
end
|
85
|
-
|
86
|
-
def test_scan_free_space_nested
|
87
|
-
# Matches 'a bcde fghi j'
|
88
|
-
regexp = /a b(?x:c d(?-x:e f)g h)i j/
|
89
|
-
tokens = RS.scan(regexp)
|
90
|
-
|
91
|
-
[
|
92
|
-
[ 0, :literal, :literal, 'a b', 0, 3],
|
93
|
-
[ 1, :group, :options, '(?x:', 3, 7],
|
94
|
-
[ 2, :literal, :literal, 'c', 7, 8],
|
95
|
-
[ 3, :free_space, :whitespace, ' ', 8, 9],
|
96
|
-
[ 4, :literal, :literal, 'd', 9, 10],
|
97
|
-
[ 5, :group, :options, '(?-x:', 10, 15],
|
98
|
-
[ 6, :literal, :literal, 'e f', 15, 18],
|
99
|
-
[ 7, :group, :close, ')', 18, 19],
|
100
|
-
[ 8, :literal, :literal, 'g', 19, 20],
|
101
|
-
[ 9, :free_space, :whitespace, ' ', 20, 21],
|
102
|
-
[10, :literal, :literal, 'h', 21, 22],
|
103
|
-
[11, :group, :close, ')', 22, 23],
|
104
|
-
[12, :literal, :literal, 'i j', 23, 26]
|
105
|
-
].each do |index, type, token, text, ts, te|
|
106
|
-
result = tokens[index]
|
107
|
-
|
108
|
-
assert_equal type, result[0]
|
109
|
-
assert_equal token, result[1]
|
110
|
-
assert_equal text, result[2]
|
111
|
-
assert_equal ts, result[3]
|
112
|
-
assert_equal te, result[4]
|
113
|
-
end
|
114
|
-
end
|
115
|
-
|
116
|
-
def test_scan_free_space_nested_groups
|
117
|
-
# Matches 'a bcde f g hi j'
|
118
|
-
regexp = /(a (b(?x: (c d) (?-x:(e f) )g) h)i j)/
|
119
|
-
tokens = RS.scan(regexp)
|
120
|
-
|
121
|
-
[
|
122
|
-
[ 0, :group, :capture, '(', 0, 1],
|
123
|
-
[ 1, :literal, :literal, 'a ', 1, 3],
|
124
|
-
[ 2, :group, :capture, '(', 3, 4],
|
125
|
-
[ 3, :literal, :literal, 'b', 4, 5],
|
126
|
-
[ 4, :group, :options, '(?x:', 5, 9],
|
127
|
-
[ 5, :free_space, :whitespace, ' ', 9, 10],
|
128
|
-
[ 6, :group, :capture, '(', 10, 11],
|
129
|
-
[ 7, :literal, :literal, 'c', 11, 12],
|
130
|
-
[ 8, :free_space, :whitespace, ' ', 12, 13],
|
131
|
-
[ 9, :literal, :literal, 'd', 13, 14],
|
132
|
-
[10, :group, :close, ')', 14, 15],
|
133
|
-
[11, :free_space, :whitespace, ' ', 15, 16],
|
134
|
-
[12, :group, :options, '(?-x:', 16, 21],
|
135
|
-
[13, :group, :capture, '(', 21, 22],
|
136
|
-
[14, :literal, :literal, 'e f', 22, 25],
|
137
|
-
[15, :group, :close, ')', 25, 26],
|
138
|
-
[16, :literal, :literal, ' ', 26, 27],
|
139
|
-
[17, :group, :close, ')', 27, 28],
|
140
|
-
[18, :literal, :literal, 'g', 28, 29],
|
141
|
-
[19, :group, :close, ')', 29, 30],
|
142
|
-
[20, :literal, :literal, ' h', 30, 32],
|
143
|
-
[21, :group, :close, ')', 32, 33],
|
144
|
-
[22, :literal, :literal, 'i j', 33, 36],
|
145
|
-
[23, :group, :close, ')', 36, 37]
|
146
|
-
].each do |index, type, token, text, ts, te|
|
147
|
-
result = tokens[index]
|
148
|
-
|
149
|
-
assert_equal type, result[0]
|
150
|
-
assert_equal token, result[1]
|
151
|
-
assert_equal text, result[2]
|
152
|
-
assert_equal ts, result[3]
|
153
|
-
assert_equal te, result[4]
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
def test_scan_free_space_switch_groups
|
158
|
-
# Matches 'a bcde f g hi j'
|
159
|
-
regexp = /(a (b((?x) (c d) ((?-x)(e f) )g) h)i j)/
|
160
|
-
tokens = RS.scan(regexp)
|
161
|
-
[
|
162
|
-
[ 0, :group, :capture, '(', 0, 1],
|
163
|
-
[ 1, :literal, :literal, 'a ', 1, 3],
|
164
|
-
[ 2, :group, :capture, '(', 3, 4],
|
165
|
-
[ 3, :literal, :literal, 'b', 4, 5],
|
166
|
-
[ 4, :group, :capture, '(', 5, 6],
|
167
|
-
[ 5, :group, :options_switch, '(?x', 6, 9],
|
168
|
-
[ 6, :group, :close, ')', 9, 10],
|
169
|
-
[ 7, :free_space, :whitespace, ' ', 10, 11],
|
170
|
-
[ 8, :group, :capture, '(', 11, 12],
|
171
|
-
[ 9, :literal, :literal, 'c', 12, 13],
|
172
|
-
[10, :free_space, :whitespace, ' ', 13, 14],
|
173
|
-
[11, :literal, :literal, 'd', 14, 15],
|
174
|
-
[12, :group, :close, ')', 15, 16],
|
175
|
-
[13, :free_space, :whitespace, ' ', 16, 17],
|
176
|
-
[14, :group, :capture, '(', 17, 18],
|
177
|
-
[15, :group, :options_switch, '(?-x', 18, 22],
|
178
|
-
[16, :group, :close, ')', 22, 23],
|
179
|
-
[17, :group, :capture, '(', 23, 24],
|
180
|
-
[18, :literal, :literal, 'e f', 24, 27],
|
181
|
-
[19, :group, :close, ')', 27, 28],
|
182
|
-
[20, :literal, :literal, ' ', 28, 29],
|
183
|
-
[21, :group, :close, ')', 29, 30],
|
184
|
-
[22, :literal, :literal, 'g', 30, 31],
|
185
|
-
[23, :group, :close, ')', 31, 32],
|
186
|
-
[24, :literal, :literal, ' h', 32, 34],
|
187
|
-
[25, :group, :close, ')', 34, 35],
|
188
|
-
[26, :literal, :literal, 'i j', 35, 38],
|
189
|
-
[27, :group, :close, ')', 38, 39]
|
190
|
-
].each do |index, type, token, text, ts, te|
|
191
|
-
result = tokens[index]
|
192
|
-
|
193
|
-
assert_equal type, result[0]
|
194
|
-
assert_equal token, result[1]
|
195
|
-
assert_equal text, result[2]
|
196
|
-
assert_equal ts, result[3]
|
197
|
-
assert_equal te, result[4]
|
198
|
-
end
|
199
|
-
end
|
200
|
-
end
|
data/test/scanner/test_groups.rb
DELETED
@@ -1,79 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helpers", __FILE__)
|
2
|
-
|
3
|
-
class ScannerGroups < Test::Unit::TestCase
|
4
|
-
|
5
|
-
tests = {
|
6
|
-
# Options
|
7
|
-
'(?-mix:abc)' => [0, :group, :options, '(?-mix:', 0, 7],
|
8
|
-
'(?m-ix:abc)' => [0, :group, :options, '(?m-ix:', 0, 7],
|
9
|
-
'(?mi-x:abc)' => [0, :group, :options, '(?mi-x:', 0, 7],
|
10
|
-
'(?mix:abc)' => [0, :group, :options, '(?mix:', 0, 6],
|
11
|
-
'(?m:)' => [0, :group, :options, '(?m:', 0, 4],
|
12
|
-
'(?i:)' => [0, :group, :options, '(?i:', 0, 4],
|
13
|
-
'(?x:)' => [0, :group, :options, '(?x:', 0, 4],
|
14
|
-
'(?mix)' => [0, :group, :options_switch, '(?mix', 0, 5],
|
15
|
-
|
16
|
-
# Group types
|
17
|
-
'(?>abc)' => [0, :group, :atomic, '(?>', 0, 3],
|
18
|
-
'(abc)' => [0, :group, :capture, '(', 0, 1],
|
19
|
-
|
20
|
-
'(?<name>abc)' => [0, :group, :named_ab, '(?<name>', 0, 8],
|
21
|
-
"(?'name'abc)" => [0, :group, :named_sq, "(?'name'", 0, 8],
|
22
|
-
|
23
|
-
'(?<name_1>abc)' => [0, :group, :named_ab, '(?<name_1>', 0,10],
|
24
|
-
"(?'name_1'abc)" => [0, :group, :named_sq, "(?'name_1'", 0,10],
|
25
|
-
|
26
|
-
'(?:abc)' => [0, :group, :passive, '(?:', 0, 3],
|
27
|
-
'(?:)' => [0, :group, :passive, '(?:', 0, 3],
|
28
|
-
'(?::)' => [0, :group, :passive, '(?:', 0, 3],
|
29
|
-
|
30
|
-
# Comments
|
31
|
-
'(?#abc)' => [0, :group, :comment, '(?#abc)', 0, 7],
|
32
|
-
'(?#)' => [0, :group, :comment, '(?#)', 0, 4],
|
33
|
-
|
34
|
-
# Assertions
|
35
|
-
'(?=abc)' => [0, :assertion, :lookahead, '(?=', 0, 3],
|
36
|
-
'(?!abc)' => [0, :assertion, :nlookahead, '(?!', 0, 3],
|
37
|
-
'(?<=abc)' => [0, :assertion, :lookbehind, '(?<=', 0, 4],
|
38
|
-
'(?<!abc)' => [0, :assertion, :nlookbehind, '(?<!', 0, 4],
|
39
|
-
}
|
40
|
-
|
41
|
-
if RUBY_VERSION >= '2.0'
|
42
|
-
tests.merge!({
|
43
|
-
# New options
|
44
|
-
'(?d-mix:abc)' => [0, :group, :options, '(?d-mix:', 0, 8],
|
45
|
-
'(?a-mix:abc)' => [0, :group, :options, '(?a-mix:', 0, 8],
|
46
|
-
'(?u-mix:abc)' => [0, :group, :options, '(?u-mix:', 0, 8],
|
47
|
-
'(?da-m:abc)' => [0, :group, :options, '(?da-m:', 0, 7],
|
48
|
-
'(?du-x:abc)' => [0, :group, :options, '(?du-x:', 0, 7],
|
49
|
-
'(?dau-i:abc)' => [0, :group, :options, '(?dau-i:', 0, 8],
|
50
|
-
'(?dau:abc)' => [0, :group, :options, '(?dau:', 0, 6],
|
51
|
-
'(?d:)' => [0, :group, :options, '(?d:', 0, 4],
|
52
|
-
'(?a:)' => [0, :group, :options, '(?a:', 0, 4],
|
53
|
-
'(?u:)' => [0, :group, :options, '(?u:', 0, 4],
|
54
|
-
'(?dau)' => [0, :group, :options_switch, '(?dau', 0, 5],
|
55
|
-
})
|
56
|
-
end
|
57
|
-
|
58
|
-
if RUBY_VERSION >= '2.4.1'
|
59
|
-
tests.merge!({
|
60
|
-
# New absence operator
|
61
|
-
'(?~abc)' => [0, :group, :absence, '(?~', 0, 3],
|
62
|
-
})
|
63
|
-
end
|
64
|
-
|
65
|
-
tests.each_with_index do |(pattern, (index, type, token, text, ts, te)), count|
|
66
|
-
define_method "test_scanner_#{type}_#{token}_#{count}" do
|
67
|
-
tokens = RS.scan(pattern)
|
68
|
-
result = tokens[index]
|
69
|
-
|
70
|
-
assert_equal type, result[0]
|
71
|
-
assert_equal token, result[1]
|
72
|
-
assert_equal text, result[2]
|
73
|
-
assert_equal ts, result[3]
|
74
|
-
assert_equal te, result[4]
|
75
|
-
assert_equal text, pattern[ts, te]
|
76
|
-
end
|
77
|
-
end
|
78
|
-
|
79
|
-
end
|
data/test/scanner/test_keep.rb
DELETED
@@ -1,35 +0,0 @@
|
|
1
|
-
require File.expand_path("../../helpers", __FILE__)
|
2
|
-
|
3
|
-
class ScannerKeep < Test::Unit::TestCase
|
4
|
-
|
5
|
-
def test_scan_keep_token
|
6
|
-
tokens = RS.scan(/ab\Kcd/)
|
7
|
-
result = tokens.at(1)
|
8
|
-
|
9
|
-
assert_equal :keep, result[0]
|
10
|
-
assert_equal :mark, result[1]
|
11
|
-
assert_equal '\\K', result[2]
|
12
|
-
assert_equal 2, result[3]
|
13
|
-
assert_equal 4, result[4]
|
14
|
-
end
|
15
|
-
|
16
|
-
def test_scan_keep_nested
|
17
|
-
tokens = RS.scan(/(a\Kb)|(c\\\Kd)ef/)
|
18
|
-
|
19
|
-
first = tokens.at(2)
|
20
|
-
second = tokens.at(9)
|
21
|
-
|
22
|
-
assert_equal :keep, first[0]
|
23
|
-
assert_equal :mark, first[1]
|
24
|
-
assert_equal '\\K', first[2]
|
25
|
-
assert_equal 2, first[3]
|
26
|
-
assert_equal 4, first[4]
|
27
|
-
|
28
|
-
assert_equal :keep, second[0]
|
29
|
-
assert_equal :mark, second[1]
|
30
|
-
assert_equal '\\K', second[2]
|
31
|
-
assert_equal 11, second[3]
|
32
|
-
assert_equal 13, second[4]
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|