regexp_parser 0.1.6 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/ChangeLog +57 -0
- data/Gemfile +8 -0
- data/LICENSE +1 -1
- data/README.md +225 -206
- data/Rakefile +9 -3
- data/lib/regexp_parser.rb +7 -11
- data/lib/regexp_parser/expression.rb +72 -14
- data/lib/regexp_parser/expression/classes/alternation.rb +3 -16
- data/lib/regexp_parser/expression/classes/conditional.rb +57 -0
- data/lib/regexp_parser/expression/classes/free_space.rb +17 -0
- data/lib/regexp_parser/expression/classes/keep.rb +7 -0
- data/lib/regexp_parser/expression/classes/set.rb +28 -7
- data/lib/regexp_parser/expression/methods/strfregexp.rb +113 -0
- data/lib/regexp_parser/expression/methods/tests.rb +116 -0
- data/lib/regexp_parser/expression/methods/traverse.rb +63 -0
- data/lib/regexp_parser/expression/quantifier.rb +10 -0
- data/lib/regexp_parser/expression/sequence.rb +45 -0
- data/lib/regexp_parser/expression/subexpression.rb +29 -1
- data/lib/regexp_parser/lexer.rb +31 -8
- data/lib/regexp_parser/parser.rb +118 -45
- data/lib/regexp_parser/scanner.rb +1745 -1404
- data/lib/regexp_parser/scanner/property.rl +57 -3
- data/lib/regexp_parser/scanner/scanner.rl +161 -34
- data/lib/regexp_parser/syntax.rb +12 -2
- data/lib/regexp_parser/syntax/ruby/1.9.1.rb +3 -3
- data/lib/regexp_parser/syntax/ruby/1.9.3.rb +2 -7
- data/lib/regexp_parser/syntax/ruby/2.0.0.rb +4 -1
- data/lib/regexp_parser/syntax/ruby/2.1.4.rb +13 -0
- data/lib/regexp_parser/syntax/ruby/2.1.5.rb +13 -0
- data/lib/regexp_parser/syntax/ruby/2.1.rb +2 -2
- data/lib/regexp_parser/syntax/ruby/2.2.0.rb +16 -0
- data/lib/regexp_parser/syntax/ruby/2.2.rb +8 -0
- data/lib/regexp_parser/syntax/tokens.rb +19 -2
- data/lib/regexp_parser/syntax/tokens/conditional.rb +22 -0
- data/lib/regexp_parser/syntax/tokens/keep.rb +14 -0
- data/lib/regexp_parser/syntax/tokens/unicode_property.rb +45 -4
- data/lib/regexp_parser/token.rb +23 -8
- data/lib/regexp_parser/version.rb +5 -0
- data/regexp_parser.gemspec +35 -0
- data/test/expression/test_all.rb +6 -1
- data/test/expression/test_base.rb +19 -0
- data/test/expression/test_conditionals.rb +114 -0
- data/test/expression/test_free_space.rb +33 -0
- data/test/expression/test_set.rb +61 -0
- data/test/expression/test_strfregexp.rb +214 -0
- data/test/expression/test_subexpression.rb +24 -0
- data/test/expression/test_tests.rb +99 -0
- data/test/expression/test_to_h.rb +48 -0
- data/test/expression/test_to_s.rb +46 -0
- data/test/expression/test_traverse.rb +164 -0
- data/test/lexer/test_all.rb +16 -3
- data/test/lexer/test_conditionals.rb +101 -0
- data/test/lexer/test_keep.rb +24 -0
- data/test/lexer/test_literals.rb +51 -51
- data/test/lexer/test_nesting.rb +62 -62
- data/test/lexer/test_refcalls.rb +18 -20
- data/test/parser/test_all.rb +18 -3
- data/test/parser/test_alternation.rb +11 -14
- data/test/parser/test_conditionals.rb +148 -0
- data/test/parser/test_escapes.rb +29 -5
- data/test/parser/test_free_space.rb +139 -0
- data/test/parser/test_groups.rb +40 -0
- data/test/parser/test_keep.rb +21 -0
- data/test/scanner/test_all.rb +8 -2
- data/test/scanner/test_conditionals.rb +166 -0
- data/test/scanner/test_escapes.rb +8 -5
- data/test/scanner/test_free_space.rb +133 -0
- data/test/scanner/test_groups.rb +28 -0
- data/test/scanner/test_keep.rb +33 -0
- data/test/scanner/test_properties.rb +4 -0
- data/test/scanner/test_scripts.rb +71 -1
- data/test/syntax/ruby/test_1.9.3.rb +2 -2
- data/test/syntax/ruby/test_2.0.0.rb +38 -0
- data/test/syntax/ruby/test_2.2.0.rb +38 -0
- data/test/syntax/ruby/test_all.rb +1 -8
- data/test/syntax/ruby/test_files.rb +104 -0
- data/test/test_all.rb +2 -1
- data/test/token/test_all.rb +2 -0
- data/test/token/test_token.rb +109 -0
- metadata +75 -21
- data/VERSION.yml +0 -5
- data/lib/regexp_parser/ctype.rb +0 -48
- data/test/syntax/ruby/test_2.x.rb +0 -46
@@ -0,0 +1,33 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class ExpressionFreeSpace < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_expression_white_space_quantify_raises_error
|
6
|
+
regexp = %r{
|
7
|
+
a # Comment
|
8
|
+
}x
|
9
|
+
|
10
|
+
root = RP.parse(regexp)
|
11
|
+
|
12
|
+
space = root[0]
|
13
|
+
assert_equal( FreeSpace::WhiteSpace, space.class )
|
14
|
+
assert_raise( RuntimeError ) {
|
15
|
+
space.quantify(:dummy, '#')
|
16
|
+
}
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_expression_comment_quantify_raises_error
|
20
|
+
regexp = %r{
|
21
|
+
a # Comment
|
22
|
+
}x
|
23
|
+
|
24
|
+
root = RP.parse(regexp)
|
25
|
+
|
26
|
+
comment = root[3]
|
27
|
+
assert_equal( FreeSpace::Comment, comment.class )
|
28
|
+
assert_raise( RuntimeError ) {
|
29
|
+
comment.quantify(:dummy, '#')
|
30
|
+
}
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class ExpressionSet < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_expression_set_exapnd_members_digit
|
6
|
+
set = RP.parse('[\d]').first
|
7
|
+
|
8
|
+
assert_equal( ['0-9'], set.expand_members )
|
9
|
+
assert_equal( ['\p{Digit}'], set.expand_members(true) )
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_expression_set_exapnd_members_nondigit
|
13
|
+
set = RP.parse('[\D]').first
|
14
|
+
|
15
|
+
assert_equal( ['^0-9'], set.expand_members )
|
16
|
+
assert_equal( ['\P{Digit}'], set.expand_members(true) )
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_expression_set_exapnd_members_word
|
20
|
+
set = RP.parse('[\w]').first
|
21
|
+
|
22
|
+
assert_equal( ['A-Za-z0-9_'], set.expand_members )
|
23
|
+
assert_equal( ['\p{Word}'], set.expand_members(true) )
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_expression_set_exapnd_members_nonword
|
27
|
+
set = RP.parse('[\W]').first
|
28
|
+
|
29
|
+
assert_equal( ['^A-Za-z0-9_'], set.expand_members )
|
30
|
+
assert_equal( ['\P{Word}'], set.expand_members(true) )
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_expression_set_exapnd_members_space
|
34
|
+
set = RP.parse('[\s]').first
|
35
|
+
|
36
|
+
assert_equal( [' \t\f\v\n\r'], set.expand_members )
|
37
|
+
assert_equal( ['\p{Space}'], set.expand_members(true) )
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_expression_set_exapnd_members_nonspace
|
41
|
+
set = RP.parse('[\S]').first
|
42
|
+
|
43
|
+
assert_equal( ['^ \t\f\v\n\r'], set.expand_members )
|
44
|
+
assert_equal( ['\P{Space}'], set.expand_members(true) )
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_expression_set_exapnd_members_xdigit
|
48
|
+
set = RP.parse('[\h]').first
|
49
|
+
|
50
|
+
assert_equal( ['0-9A-Fa-f'], set.expand_members )
|
51
|
+
assert_equal( ['\p{Xdigit}'], set.expand_members(true) )
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_expression_set_exapnd_members_nonxdigit
|
55
|
+
set = RP.parse('[\H]').first
|
56
|
+
|
57
|
+
assert_equal( ['^0-9A-Fa-f'], set.expand_members )
|
58
|
+
assert_equal( ['\P{Xdigit}'], set.expand_members(true) )
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
@@ -0,0 +1,214 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class Expressionstrfregexp < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_expression_strfre_alias
|
6
|
+
assert_equal( true, RP.parse(/a/).respond_to?(:strfre) )
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_expression_strfregexp_level
|
10
|
+
root = RP.parse(/a(b(c))/)
|
11
|
+
|
12
|
+
assert_equal( 'root', root.strfregexp('%l') )
|
13
|
+
|
14
|
+
a = root.first
|
15
|
+
assert_equal( '%0', a.strfregexp('%%l') )
|
16
|
+
|
17
|
+
b = root[1].first
|
18
|
+
assert_equal( '<1>', b.strfregexp('<%l>') )
|
19
|
+
|
20
|
+
c = root[1][1].first
|
21
|
+
assert_equal( '[at: 2]', c.strfregexp('[at: %l]') )
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_expression_strfregexp_start_end
|
25
|
+
root = RP.parse(/a(b(c))/)
|
26
|
+
|
27
|
+
assert_equal( '0', root.strfregexp('%s') )
|
28
|
+
assert_equal( '7', root.strfregexp('%e') )
|
29
|
+
|
30
|
+
a = root.first
|
31
|
+
assert_equal( '%0', a.strfregexp('%%s') )
|
32
|
+
assert_equal( '1', a.strfregexp('%e') )
|
33
|
+
|
34
|
+
group_1 = root[1]
|
35
|
+
assert_equal( 'GRP:1', group_1.strfregexp('GRP:%s') )
|
36
|
+
assert_equal( '7', group_1.strfregexp('%e') )
|
37
|
+
|
38
|
+
b = group_1.first
|
39
|
+
assert_equal( '<@2>', b.strfregexp('<@%s>') )
|
40
|
+
assert_equal( '3', b.strfregexp('%e') )
|
41
|
+
|
42
|
+
c = group_1.last.first
|
43
|
+
assert_equal( '[at: 4]', c.strfregexp('[at: %s]') )
|
44
|
+
assert_equal( '5', c.strfregexp('%e') )
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_expression_strfregexp_length
|
48
|
+
root = RP.parse(/a[b]c/)
|
49
|
+
|
50
|
+
assert_equal( '5', root.strfregexp('%S') )
|
51
|
+
|
52
|
+
a = root.first
|
53
|
+
assert_equal( '1', a.strfregexp('%S') )
|
54
|
+
|
55
|
+
set = root[1]
|
56
|
+
assert_equal( '3', set.strfregexp('%S') )
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_expression_strfregexp_coded_offset
|
60
|
+
root = RP.parse(/a[b]c/)
|
61
|
+
|
62
|
+
assert_equal( '@0+5', root.strfregexp('%o') )
|
63
|
+
|
64
|
+
a = root.first
|
65
|
+
assert_equal( '@0+1', a.strfregexp('%o') )
|
66
|
+
|
67
|
+
set = root[1]
|
68
|
+
assert_equal( '@1+3', set.strfregexp('%o') )
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_expression_strfregexp_type_token
|
72
|
+
root = RP.parse(/a[b](c)/)
|
73
|
+
|
74
|
+
assert_equal( 'expression', root.strfregexp('%y') )
|
75
|
+
assert_equal( 'root', root.strfregexp('%k') )
|
76
|
+
assert_equal( 'expression:root', root.strfregexp('%i') )
|
77
|
+
assert_equal( 'Regexp::Expression::Root', root.strfregexp('%c') )
|
78
|
+
|
79
|
+
a = root.first
|
80
|
+
assert_equal( 'literal', a.strfregexp('%y') )
|
81
|
+
assert_equal( 'literal', a.strfregexp('%k') )
|
82
|
+
assert_equal( 'literal:literal', a.strfregexp('%i') )
|
83
|
+
assert_equal( 'Regexp::Expression::Literal', a.strfregexp('%c') )
|
84
|
+
|
85
|
+
set = root[1]
|
86
|
+
assert_equal( 'set', set.strfregexp('%y') )
|
87
|
+
assert_equal( 'character', set.strfregexp('%k') )
|
88
|
+
assert_equal( 'set:character', set.strfregexp('%i') )
|
89
|
+
assert_equal( 'Regexp::Expression::CharacterSet', set.strfregexp('%c') )
|
90
|
+
|
91
|
+
group = root.last
|
92
|
+
assert_equal( 'group', group.strfregexp('%y') )
|
93
|
+
assert_equal( 'capture', group.strfregexp('%k') )
|
94
|
+
assert_equal( 'group:capture', group.strfregexp('%i') )
|
95
|
+
assert_equal( 'Regexp::Expression::Group::Capture', group.strfregexp('%c') )
|
96
|
+
end
|
97
|
+
|
98
|
+
def test_expression_strfregexp_quantifier
|
99
|
+
root = RP.parse(/a+[b](c)?d{3,4}/)
|
100
|
+
|
101
|
+
assert_equal( '{1}', root.strfregexp('%q') )
|
102
|
+
assert_equal( '', root.strfregexp('%Q') )
|
103
|
+
assert_equal( '1, 1', root.strfregexp('%z, %Z') )
|
104
|
+
|
105
|
+
a = root.first
|
106
|
+
assert_equal( '{1, or-more}', a.strfregexp('%q') )
|
107
|
+
assert_equal( '+', a.strfregexp('%Q') )
|
108
|
+
assert_equal( '1, -1', a.strfregexp('%z, %Z') )
|
109
|
+
|
110
|
+
set = root[1]
|
111
|
+
assert_equal( '{1}', set.strfregexp('%q') )
|
112
|
+
assert_equal( '', set.strfregexp('%Q') )
|
113
|
+
assert_equal( '1, 1', set.strfregexp('%z, %Z') )
|
114
|
+
|
115
|
+
group = root[2]
|
116
|
+
assert_equal( '{0, 1}', group.strfregexp('%q') )
|
117
|
+
assert_equal( '?', group.strfregexp('%Q') )
|
118
|
+
assert_equal( '0, 1', group.strfregexp('%z, %Z') )
|
119
|
+
|
120
|
+
d = root.last
|
121
|
+
assert_equal( '{3, 4}', d.strfregexp('%q') )
|
122
|
+
assert_equal( '{3,4}', d.strfregexp('%Q') )
|
123
|
+
assert_equal( '3, 4', d.strfregexp('%z, %Z') )
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_expression_strfregexp_text
|
127
|
+
root = RP.parse(/a(b(c))|[d-gk-p]+/)
|
128
|
+
|
129
|
+
assert_equal( 'a(b(c))|[d-gk-p]+', root.strfregexp('%t') )
|
130
|
+
assert_equal( 'expression:root', root.strfregexp('%~t') )
|
131
|
+
|
132
|
+
alt = root.first
|
133
|
+
assert_equal( 'a(b(c))|[d-gk-p]+', alt.strfregexp('%t') )
|
134
|
+
assert_equal( 'a(b(c))|[d-gk-p]+', alt.strfregexp('%T') )
|
135
|
+
assert_equal( 'meta:alternation', alt.strfregexp('%~t') )
|
136
|
+
|
137
|
+
seq_1 = alt.first
|
138
|
+
assert_equal( 'a(b(c))', seq_1.strfregexp('%t') )
|
139
|
+
assert_equal( 'a(b(c))', seq_1.strfregexp('%T') )
|
140
|
+
assert_equal( 'expression:sequence', seq_1.strfregexp('%~t') )
|
141
|
+
|
142
|
+
group = seq_1[1]
|
143
|
+
assert_equal( '(b(c))', group.strfregexp('%t') )
|
144
|
+
assert_equal( '(b(c))', group.strfregexp('%T') )
|
145
|
+
assert_equal( 'group:capture', group.strfregexp('%~t') )
|
146
|
+
|
147
|
+
seq_2 = alt.last
|
148
|
+
assert_equal( '[d-gk-p]+', seq_2.strfregexp('%t') )
|
149
|
+
assert_equal( '[d-gk-p]+', seq_2.strfregexp('%T') )
|
150
|
+
|
151
|
+
set = seq_2.first
|
152
|
+
assert_equal( '[d-gk-p]', set.strfregexp('%t') )
|
153
|
+
assert_equal( '[d-gk-p]+', set.strfregexp('%T') )
|
154
|
+
assert_equal( '[d-gk-p]+', set.strfregexp('%~t') )
|
155
|
+
end
|
156
|
+
|
157
|
+
def test_expression_strfregexp_combined
|
158
|
+
root = RP.parse(/a{5}|[b-d]+/)
|
159
|
+
|
160
|
+
assert_equal( '@0+11 expression:root', root.strfregexp('%b') )
|
161
|
+
assert_equal( root.strfregexp('%o %i'), root.strfregexp('%b') )
|
162
|
+
|
163
|
+
assert_equal( '@0+11 expression:root {1}', root.strfregexp('%m') )
|
164
|
+
assert_equal( root.strfregexp('%b %q'), root.strfregexp('%m') )
|
165
|
+
|
166
|
+
assert_equal( '@0+11 expression:root {1} a{5}|[b-d]+', root.strfregexp('%a') )
|
167
|
+
assert_equal( root.strfregexp('%m %t'), root.strfregexp('%a') )
|
168
|
+
end
|
169
|
+
|
170
|
+
def test_expression_strfregexp_tree
|
171
|
+
root = RP.parse(/a[b-d]*(e(f+))?/)
|
172
|
+
|
173
|
+
assert_equal(
|
174
|
+
"@0+15 expression:root\n" +
|
175
|
+
" @0+1 a\n" +
|
176
|
+
" @1+6 [b-d]*\n" +
|
177
|
+
" @7+8 group:capture\n" +
|
178
|
+
" @8+1 e\n" +
|
179
|
+
" @9+4 group:capture\n" +
|
180
|
+
" @10+2 f+",
|
181
|
+
root.strfregexp_tree('%>%o %~t')
|
182
|
+
)
|
183
|
+
end
|
184
|
+
|
185
|
+
def test_expression_strfregexp_tree_separator
|
186
|
+
root = RP.parse(/a[b-d]*(e(f+))?/)
|
187
|
+
|
188
|
+
assert_equal(
|
189
|
+
"@0+15 expression:root-SEP-" +
|
190
|
+
" @0+1 a-SEP-" +
|
191
|
+
" @1+6 [b-d]*-SEP-" +
|
192
|
+
" @7+8 group:capture-SEP-" +
|
193
|
+
" @8+1 e-SEP-" +
|
194
|
+
" @9+4 group:capture-SEP-" +
|
195
|
+
" @10+2 f+",
|
196
|
+
root.strfregexp_tree('%>%o %~t', true, '-SEP-')
|
197
|
+
)
|
198
|
+
end
|
199
|
+
|
200
|
+
def test_expression_strfregexp_tree_exclude_self
|
201
|
+
root = RP.parse(/a[b-d]*(e(f+))?/)
|
202
|
+
|
203
|
+
assert_equal(
|
204
|
+
"@0+1 a\n" +
|
205
|
+
"@1+6 [b-d]*\n" +
|
206
|
+
"@7+8 group:capture\n" +
|
207
|
+
" @8+1 e\n" +
|
208
|
+
" @9+4 group:capture\n" +
|
209
|
+
" @10+2 f+",
|
210
|
+
root.strfregexp_tree('%>%o %~t', false)
|
211
|
+
)
|
212
|
+
end
|
213
|
+
|
214
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class ExpressionSubexpression < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_subexpression_ts_te
|
6
|
+
regx = /abcd|ghij|klmn|pqur/
|
7
|
+
root = RP.parse(regx)
|
8
|
+
|
9
|
+
alt = root.first
|
10
|
+
|
11
|
+
{
|
12
|
+
0 => [ 0, 4 ],
|
13
|
+
1 => [ 5, 9 ],
|
14
|
+
2 => [ 10, 14 ],
|
15
|
+
3 => [ 15, 19 ],
|
16
|
+
}.each do |index, span|
|
17
|
+
sequence = alt[index]
|
18
|
+
|
19
|
+
assert_equal( span[0], sequence.ts )
|
20
|
+
assert_equal( span[1], sequence.te )
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
@@ -0,0 +1,99 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class ExpressionTests < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_expression_type?
|
6
|
+
root = RP.parse(/abcd|(ghij)|[klmn]/)
|
7
|
+
|
8
|
+
alt = root.first
|
9
|
+
|
10
|
+
assert_equal( true, alt.type?(:meta) )
|
11
|
+
assert_equal( false, alt.type?(:escape) )
|
12
|
+
assert_equal( true, alt.type?([:meta, :escape]) )
|
13
|
+
assert_equal( false, alt.type?([:literal, :escape]) )
|
14
|
+
assert_equal( true, alt.type?(:*) )
|
15
|
+
assert_equal( true, alt.type?([:*]) )
|
16
|
+
assert_equal( true, alt.type?([:literal, :escape, :*]) )
|
17
|
+
|
18
|
+
seq_1 = alt[0]
|
19
|
+
assert_equal( true, seq_1.type?(:expression) )
|
20
|
+
assert_equal( true, seq_1.first.type?(:literal) )
|
21
|
+
|
22
|
+
seq_2 = alt[1]
|
23
|
+
assert_equal( true, seq_2.type?(:*) )
|
24
|
+
assert_equal( true, seq_2.first.type?(:group) )
|
25
|
+
|
26
|
+
seq_3 = alt[2]
|
27
|
+
assert_equal( true, seq_3.first.type?(:set) )
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_expression_is?
|
31
|
+
root = RP.parse(/.+|\.?/)
|
32
|
+
|
33
|
+
assert_equal( true, root.is?(:*) )
|
34
|
+
|
35
|
+
alt = root.first
|
36
|
+
assert_equal( true, alt.is?(:*) )
|
37
|
+
assert_equal( true, alt.is?(:alternation) )
|
38
|
+
assert_equal( true, alt.is?(:alternation, :meta) )
|
39
|
+
|
40
|
+
seq_1 = alt[0]
|
41
|
+
assert_equal( true, seq_1.is?(:sequence) )
|
42
|
+
assert_equal( true, seq_1.is?(:sequence, :expression) )
|
43
|
+
|
44
|
+
assert_equal( true, seq_1.first.is?(:dot) )
|
45
|
+
assert_equal( false, seq_1.first.is?(:dot, :escape) )
|
46
|
+
assert_equal( true, seq_1.first.is?(:dot, :meta) )
|
47
|
+
assert_equal( true, seq_1.first.is?(:dot, [:escape, :meta]) )
|
48
|
+
|
49
|
+
seq_2 = alt[1]
|
50
|
+
assert_equal( true, seq_2.first.is?(:dot) )
|
51
|
+
assert_equal( true, seq_2.first.is?(:dot, :escape) )
|
52
|
+
assert_equal( false, seq_2.first.is?(:dot, :meta) )
|
53
|
+
assert_equal( true, seq_2.first.is?(:dot, [:meta, :escape]) )
|
54
|
+
end
|
55
|
+
|
56
|
+
def test_expression_one_of?
|
57
|
+
root = RP.parse(/\Aab(c[\w])d|e.\z/)
|
58
|
+
|
59
|
+
assert_equal( true, root.one_of?(:*) )
|
60
|
+
assert_equal( true, root.one_of?(:* => :*) )
|
61
|
+
assert_equal( true, root.one_of?(:* => [:*]) )
|
62
|
+
|
63
|
+
alt = root.first
|
64
|
+
assert_equal( true, alt.one_of?(:*) )
|
65
|
+
assert_equal( true, alt.one_of?(:meta) )
|
66
|
+
assert_equal( true, alt.one_of?(:meta, :alternation) )
|
67
|
+
assert_equal( false, alt.one_of?(:meta => [:dot, :bogus]) )
|
68
|
+
assert_equal( true, alt.one_of?(:meta => [:dot, :alternation]) )
|
69
|
+
|
70
|
+
seq_1 = alt[0]
|
71
|
+
assert_equal( true, seq_1.one_of?(:expression) )
|
72
|
+
assert_equal( true, seq_1.one_of?(:expression => :sequence) )
|
73
|
+
|
74
|
+
assert_equal( true, seq_1.first.one_of?(:anchor) )
|
75
|
+
assert_equal( true, seq_1.first.one_of?(:anchor => :bos) )
|
76
|
+
assert_equal( false, seq_1.first.one_of?(:anchor => :eos) )
|
77
|
+
assert_equal( true, seq_1.first.one_of?(:anchor => [:escape, :meta, :bos]) )
|
78
|
+
assert_equal( false, seq_1.first.one_of?(:anchor => [:escape, :meta, :eos]) )
|
79
|
+
|
80
|
+
seq_2 = alt[1]
|
81
|
+
assert_equal( true, seq_2.first.one_of?(:literal) )
|
82
|
+
|
83
|
+
assert_equal( true, seq_2[1].one_of?(:meta) )
|
84
|
+
assert_equal( true, seq_2[1].one_of?(:meta => :dot) )
|
85
|
+
assert_equal( false, seq_2[1].one_of?(:meta => :alternation) )
|
86
|
+
assert_equal( true, seq_2[1].one_of?(:meta => [:dot]) )
|
87
|
+
|
88
|
+
assert_equal( false, seq_2.last.one_of?(:group) )
|
89
|
+
assert_equal( false, seq_2.last.one_of?(:group => [:*]) )
|
90
|
+
assert_equal( false, seq_2.last.one_of?(:group => [:*], :meta => :*) )
|
91
|
+
|
92
|
+
assert_equal( true, seq_2.last.one_of?(:meta => [:*], :* => :*) )
|
93
|
+
assert_equal( true, seq_2.last.one_of?(:meta => [:*], :anchor => :*) )
|
94
|
+
assert_equal( true, seq_2.last.one_of?(:meta => [:*], :anchor => :eos) )
|
95
|
+
assert_equal( false, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos]) )
|
96
|
+
assert_equal( true, seq_2.last.one_of?(:meta => [:*], :anchor => [:bos, :eos]) )
|
97
|
+
end
|
98
|
+
|
99
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require File.expand_path("../../helpers", __FILE__)
|
2
|
+
|
3
|
+
class ExpressionToH < Test::Unit::TestCase
|
4
|
+
|
5
|
+
def test_expression_to_h
|
6
|
+
h = RP.parse('abc').to_h
|
7
|
+
|
8
|
+
assert_equal( h, {
|
9
|
+
:token => :root,
|
10
|
+
:type => :expression,
|
11
|
+
:text => 'abc',
|
12
|
+
:starts_at => 0,
|
13
|
+
:length => 3,
|
14
|
+
:quantifier => nil,
|
15
|
+
:options => nil,
|
16
|
+
:level => nil,
|
17
|
+
:set_level => nil,
|
18
|
+
:conditional_level => nil,
|
19
|
+
:expressions => [
|
20
|
+
{
|
21
|
+
:token => :literal,
|
22
|
+
:type => :literal,
|
23
|
+
:text => 'abc',
|
24
|
+
:starts_at => 0,
|
25
|
+
:length => 3,
|
26
|
+
:quantifier => nil,
|
27
|
+
:options => nil,
|
28
|
+
:level => 0,
|
29
|
+
:set_level => 0,
|
30
|
+
:conditional_level => 0
|
31
|
+
}
|
32
|
+
]
|
33
|
+
})
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_expression_quantifier_to_h
|
37
|
+
h = RP.parse('a{2,4}')[0].quantifier.to_h
|
38
|
+
|
39
|
+
assert_equal( h, {
|
40
|
+
:max => 4,
|
41
|
+
:min => 2,
|
42
|
+
:mode => :greedy,
|
43
|
+
:text => '{2,4}',
|
44
|
+
:token => :interval
|
45
|
+
})
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|