regexp_parser 0.3.4 → 0.3.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (58) hide show
  1. checksums.yaml +4 -4
  2. data/ChangeLog +6 -0
  3. data/lib/regexp_parser/syntax/ruby/1.9.1.rb +1 -1
  4. data/lib/regexp_parser/syntax/tokens/escape.rb +2 -0
  5. data/lib/regexp_parser/version.rb +1 -1
  6. data/test/expression/test_base.rb +32 -34
  7. data/test/expression/test_clone.rb +49 -47
  8. data/test/expression/test_conditionals.rb +40 -40
  9. data/test/expression/test_free_space.rb +4 -2
  10. data/test/expression/test_set.rb +16 -16
  11. data/test/expression/test_strfregexp.rb +74 -74
  12. data/test/expression/test_subexpression.rb +2 -2
  13. data/test/expression/test_tests.rb +57 -57
  14. data/test/expression/test_to_h.rb +11 -6
  15. data/test/expression/test_to_s.rb +22 -15
  16. data/test/expression/test_traverse.rb +26 -29
  17. data/test/lexer/test_all.rb +9 -7
  18. data/test/lexer/test_conditionals.rb +35 -11
  19. data/test/lexer/test_keep.rb +6 -6
  20. data/test/lexer/test_literals.rb +20 -10
  21. data/test/lexer/test_nesting.rb +14 -7
  22. data/test/lexer/test_refcalls.rb +12 -5
  23. data/test/parser/test_all.rb +15 -13
  24. data/test/parser/test_alternation.rb +29 -26
  25. data/test/parser/test_anchors.rb +7 -8
  26. data/test/parser/test_conditionals.rb +43 -41
  27. data/test/parser/test_escapes.rb +18 -16
  28. data/test/parser/test_free_space.rb +33 -33
  29. data/test/parser/test_groups.rb +46 -46
  30. data/test/parser/test_keep.rb +4 -4
  31. data/test/parser/test_properties.rb +18 -16
  32. data/test/parser/test_quantifiers.rb +184 -163
  33. data/test/parser/test_refcalls.rb +48 -32
  34. data/test/parser/test_sets.rb +102 -88
  35. data/test/parser/test_types.rb +7 -8
  36. data/test/scanner/test_all.rb +6 -4
  37. data/test/scanner/test_anchors.rb +8 -5
  38. data/test/scanner/test_conditionals.rb +38 -20
  39. data/test/scanner/test_escapes.rb +8 -6
  40. data/test/scanner/test_free_space.rb +89 -65
  41. data/test/scanner/test_groups.rb +27 -32
  42. data/test/scanner/test_keep.rb +24 -22
  43. data/test/scanner/test_literals.rb +11 -7
  44. data/test/scanner/test_meta.rb +11 -7
  45. data/test/scanner/test_properties.rb +16 -14
  46. data/test/scanner/test_quantifiers.rb +8 -9
  47. data/test/scanner/test_refcalls.rb +26 -23
  48. data/test/scanner/test_scripts.rb +11 -10
  49. data/test/scanner/test_sets.rb +8 -5
  50. data/test/scanner/test_types.rb +17 -15
  51. data/test/scanner/test_unicode_blocks.rb +11 -10
  52. data/test/syntax/ruby/test_1.8.rb +4 -8
  53. data/test/syntax/ruby/test_1.9.1.rb +1 -7
  54. data/test/syntax/ruby/test_1.9.3.rb +3 -7
  55. data/test/syntax/ruby/test_2.0.0.rb +1 -7
  56. data/test/syntax/ruby/test_2.2.0.rb +1 -7
  57. data/test/token/test_token.rb +29 -31
  58. metadata +3 -3
@@ -92,15 +92,22 @@ class LexerNesting < Test::Unit::TestCase
92
92
  },
93
93
  }
94
94
 
95
- count = 0
96
- tests.each do |pattern, checks|
97
- define_method "test_lex_nesting_#{count+=1}" do
98
-
95
+ tests.each_with_index do |(pattern, checks), count|
96
+ define_method "test_lex_nesting_#{count}" do
99
97
  tokens = RL.lex(pattern, 'ruby/1.9')
100
- checks.each do |offset, token|
101
- assert_equal( token, tokens[offset].to_a )
102
- end
103
98
 
99
+ checks.each do |offset, (type, token, text, ts, te, level, set_level, conditional_level)|
100
+ struct = tokens.at(offset)
101
+
102
+ assert_equal type, struct.type
103
+ assert_equal token, struct.token
104
+ assert_equal text, struct.text
105
+ assert_equal ts, struct.ts
106
+ assert_equal te, struct.te
107
+ assert_equal level, struct.level
108
+ assert_equal set_level, struct.set_level
109
+ assert_equal conditional_level, struct.conditional_level
110
+ end
104
111
  end
105
112
  end
106
113
 
@@ -31,12 +31,19 @@ class LexerRefCalls < Test::Unit::TestCase
31
31
  "(abc)\\k'1-0'" => [3, :backref, :number_nest_ref, "\\k'1-0'", 5, 12, 0, 0, 0],
32
32
  }
33
33
 
34
- count = 0
35
- tests.each do |pattern, test|
36
- define_method "test_lexer_#{test[1]}_#{test[2]}_#{count+=1}" do
37
-
34
+ tests.each_with_index do |(pattern, (index, type, token, text, ts, te, level, set_level, conditional_level)), count|
35
+ define_method "test_lexer_#{type}_#{token}_#{count}" do
38
36
  tokens = RL.lex(pattern, 'ruby/1.9')
39
- assert_equal( test[1,8], tokens[test[0]].to_a)
37
+ struct = tokens.at(index)
38
+
39
+ assert_equal type, struct.type
40
+ assert_equal token, struct.token
41
+ assert_equal text, struct.text
42
+ assert_equal ts, struct.ts
43
+ assert_equal te, struct.te
44
+ assert_equal level, struct.level
45
+ assert_equal set_level, struct.set_level
46
+ assert_equal conditional_level, struct.conditional_level
40
47
  end
41
48
  end
42
49
 
@@ -16,36 +16,38 @@ end
16
16
  class TestParser < Test::Unit::TestCase
17
17
 
18
18
  def test_parse_returns_a_root_expression
19
- assert_instance_of( Regexp::Expression::Root, RP.parse('abc'))
19
+ assert_instance_of Regexp::Expression::Root, RP.parse('abc')
20
20
  end
21
21
 
22
22
 
23
23
  def test_parse_root_contains_expressions
24
24
  root = RP.parse(/^a.c+[^one]{2,3}\b\d\\\C-C$/)
25
25
 
26
- assert( root.expressions.all?{|exp|
27
- exp.kind_of?(Regexp::Expression::Base)},
28
- "Not all nodes are instances of Regexp::Expression")
26
+ all_base = root.expressions.all? do |exp|
27
+ exp.kind_of?(Regexp::Expression::Base)
28
+ end
29
+
30
+ assert all_base, "Not all nodes are instances of Regexp::Expression"
29
31
  end
30
32
 
31
33
 
32
34
  def test_parse_node_types
33
35
  root = RP.parse('^(one){2,3}([^d\]efm-qz\,\-]*)(ghi)+$')
34
36
 
35
- assert( root.expressions[1].expressions[0].is_a?(Literal),
36
- "Not a literal node, but should be")
37
+ assert root.expressions[1].expressions[0].is_a?(Literal),
38
+ "Not a literal node, but should be"
37
39
 
38
- assert( root.expressions[1].quantified?, "Not quanfified, but should be")
40
+ assert root.expressions[1].quantified?, "Not quanfified, but should be"
39
41
 
40
- assert( root.expressions[2].expressions[0].is_a?(CharacterSet),
41
- "Not a caracter set, but it should be")
42
+ assert root.expressions[2].expressions[0].is_a?(CharacterSet),
43
+ "Not a caracter set, but it should be"
42
44
 
43
- assert_equal( false, root.expressions[2].quantified? )
45
+ assert_equal false, root.expressions[2].quantified?
44
46
 
45
- assert( root.expressions[3].is_a?(Group::Capture),
46
- "Not a group, but should be")
47
+ assert root.expressions[3].is_a?(Group::Capture),
48
+ "Not a group, but should be"
47
49
 
48
- assert_equal( true, root.expressions[3].quantified? )
50
+ assert_equal true, root.expressions[3].quantified?
49
51
  end
50
52
 
51
53
  def test_parse_no_quantifier_target_raises_error
@@ -8,43 +8,43 @@ class ParserAlternation < Test::Unit::TestCase
8
8
 
9
9
  def test_parse_alternation_root
10
10
  e = @root.expressions[0]
11
- assert_equal( true, e.is_a?(Alternation) )
11
+ assert_equal true, e.is_a?(Alternation)
12
12
  end
13
13
 
14
14
  def test_parse_alternation_alts
15
15
  alts = @root.expressions[0].alternatives
16
16
 
17
- assert_equal( true, alts[0].is_a?(Alternative) )
18
- assert_equal( true, alts[1].is_a?(Alternative) )
17
+ assert_equal true, alts[0].is_a?(Alternative)
18
+ assert_equal true, alts[1].is_a?(Alternative)
19
19
 
20
- assert_equal( true, alts[0][0].is_a?(Group::Capture) )
21
- assert_equal( true, alts[1][0].is_a?(Group::Capture) )
20
+ assert_equal true, alts[0][0].is_a?(Group::Capture)
21
+ assert_equal true, alts[1][0].is_a?(Group::Capture)
22
22
 
23
- assert_equal( 2, alts.length )
23
+ assert_equal 2, alts.length
24
24
  end
25
25
 
26
26
  def test_parse_alternation_nested
27
27
  e = @root[0].alternatives[0][0][0]
28
28
 
29
- assert_equal( true, e.is_a?(Alternation) )
29
+ assert_equal true, e.is_a?(Alternation)
30
30
  end
31
31
 
32
32
  def test_parse_alternation_nested_sequence
33
33
  alts = @root.expressions[0][0]
34
34
  nested = alts.expressions[0][0][0]
35
35
 
36
- assert_equal( true, nested.is_a?(Alternative) )
36
+ assert_equal true, nested.is_a?(Alternative)
37
37
 
38
- assert_equal( true, nested.expressions[0].is_a?(Literal) )
39
- assert_equal( true, nested.expressions[1].is_a?(Literal) )
40
- assert_equal( 2, nested.expressions.length )
38
+ assert_equal true, nested.expressions[0].is_a?(Literal)
39
+ assert_equal true, nested.expressions[1].is_a?(Literal)
40
+ assert_equal 2, nested.expressions.length
41
41
  end
42
42
 
43
43
  def test_parse_alternation_nested_groups
44
44
  root = RP.parse('(i|ey|([ougfd]+)|(ney))')
45
45
 
46
46
  alts = root.expressions[0][0].alternatives
47
- assert_equal( 4, alts.length )
47
+ assert_equal 4, alts.length
48
48
  end
49
49
 
50
50
  def test_parse_alternation_grouped_alts
@@ -52,11 +52,12 @@ class ParserAlternation < Test::Unit::TestCase
52
52
 
53
53
  alts = root.expressions[1][0].alternatives
54
54
 
55
- assert_equal( 4, alts.length )
56
- assert_equal( true, alts[0].is_a?(Alternative) )
57
- assert_equal( true, alts[1].is_a?(Alternative) )
58
- assert_equal( true, alts[2].is_a?(Alternative) )
59
- assert_equal( true, alts[3].is_a?(Alternative) )
55
+ assert_equal 4, alts.length
56
+
57
+ assert_equal true, alts[0].is_a?(Alternative)
58
+ assert_equal true, alts[1].is_a?(Alternative)
59
+ assert_equal true, alts[2].is_a?(Alternative)
60
+ assert_equal true, alts[3].is_a?(Alternative)
60
61
  end
61
62
 
62
63
  def test_parse_alternation_nested_grouped_alts
@@ -64,15 +65,17 @@ class ParserAlternation < Test::Unit::TestCase
64
65
 
65
66
  alts = root.expressions[1][0].alternatives
66
67
 
67
- assert_equal( 2, alts.length )
68
- assert_equal( true, alts[0].is_a?(Alternative) )
69
- assert_equal( true, alts[1].is_a?(Alternative) )
68
+ assert_equal 2, alts.length
69
+
70
+ assert_equal true, alts[0].is_a?(Alternative)
71
+ assert_equal true, alts[1].is_a?(Alternative)
70
72
 
71
73
  subalts = root.expressions[1][0][0][0][0].alternatives
72
74
 
73
- assert_equal( 2, alts.length )
74
- assert_equal( true, subalts[0].is_a?(Alternative) )
75
- assert_equal( true, subalts[1].is_a?(Alternative) )
75
+ assert_equal 2, alts.length
76
+
77
+ assert_equal true, subalts[0].is_a?(Alternative)
78
+ assert_equal true, subalts[1].is_a?(Alternative)
76
79
  end
77
80
 
78
81
  def test_parse_alternation_continues_after_nesting
@@ -80,10 +83,10 @@ class ParserAlternation < Test::Unit::TestCase
80
83
 
81
84
  seq = root.expressions[0][1].expressions
82
85
 
83
- assert_equal( 2, seq.length )
86
+ assert_equal 2, seq.length
84
87
 
85
- assert_equal( true, seq[0].is_a?(Group::Capture) )
86
- assert_equal( true, seq[1].is_a?(Literal) )
88
+ assert_equal true, seq[0].is_a?(Group::Capture)
89
+ assert_equal true, seq[1].is_a?(Literal)
87
90
  end
88
91
 
89
92
  end
@@ -18,17 +18,16 @@ class TestParserAnchors < Test::Unit::TestCase
18
18
  "\\\\Aa" => [0, :escape, :backslash, EscapeSequence::Literal],
19
19
  }
20
20
 
21
- count = 0
22
- tests.each do |pattern, test|
23
- define_method "test_parse_anchor_#{test[2]}_#{count+=1}" do
21
+ tests.each_with_index do |(pattern, (index, type, token, klass)), count|
22
+ define_method "test_parse_anchor_#{token}_#{count}" do
24
23
  root = RP.parse(pattern, 'ruby/1.9')
25
- exp = root.expressions[test[0]]
24
+ exp = root.expressions.at(index)
26
25
 
27
- assert( exp.is_a?( test[3] ),
28
- "Expected #{test[3]}, but got #{exp.class.name}")
26
+ assert exp.is_a?(klass),
27
+ "Expected #{klass}, but got #{exp.class.name}"
29
28
 
30
- assert_equal( test[1], exp.type )
31
- assert_equal( test[2], exp.token )
29
+ assert_equal type, exp.type
30
+ assert_equal token, exp.token
32
31
  end
33
32
  end
34
33
 
@@ -8,12 +8,12 @@ class TestParserConditionals < Test::Unit::TestCase
8
8
  root = RP.parse(regexp, 'ruby/2.0')
9
9
  exp = root.expressions[1]
10
10
 
11
- assert( exp.is_a?( Conditional::Expression ),
12
- "Expected Condition, but got #{exp.class.name}")
11
+ assert exp.is_a?(Conditional::Expression),
12
+ "Expected Condition, but got #{exp.class.name}"
13
13
 
14
- assert_equal( exp.type, :conditional )
15
- assert_equal( exp.token, :open )
16
- assert_equal( exp.text, '(?' )
14
+ assert_equal exp.type, :conditional
15
+ assert_equal exp.token, :open
16
+ assert_equal exp.text, '(?'
17
17
  end
18
18
 
19
19
 
@@ -23,12 +23,12 @@ class TestParserConditionals < Test::Unit::TestCase
23
23
  root = RP.parse(regexp, 'ruby/2.0')
24
24
  exp = root[1].condition
25
25
 
26
- assert( exp.is_a?( Conditional::Condition ),
27
- "Expected Condition, but got #{exp.class.name}")
26
+ assert exp.is_a?(Conditional::Condition),
27
+ "Expected Condition, but got #{exp.class.name}"
28
28
 
29
- assert_equal( exp.type, :conditional )
30
- assert_equal( exp.token, :condition )
31
- assert_equal( exp.text, '(<A>)' )
29
+ assert_equal exp.type, :conditional
30
+ assert_equal exp.token, :condition
31
+ assert_equal exp.text, '(<A>)'
32
32
  end
33
33
 
34
34
 
@@ -37,35 +37,37 @@ class TestParserConditionals < Test::Unit::TestCase
37
37
 
38
38
  root = RP.parse(regexp, 'ruby/2.0')
39
39
 
40
- assert_equal( regexp.source, root.to_s )
40
+ assert_equal regexp.source, root.to_s
41
41
 
42
42
  group = root.first
43
- assert_equal( Group::Capture, group.class )
43
+ assert_equal Group::Capture, group.class
44
44
 
45
45
  alt = group.first
46
- assert_equal( Alternation, alt.class )
47
- assert_equal( 3, alt.length )
46
+ assert_equal Alternation, alt.class
47
+ assert_equal 3, alt.length
48
48
 
49
- assert_equal( true, alt.all? {|exp|
50
- exp.first.is_a?(Group::Capture) }
51
- )
49
+ all_captures = alt.all? do |exp|
50
+ exp.first.is_a?(Group::Capture)
51
+ end
52
+
53
+ assert_equal true, all_captures
52
54
 
53
55
  subgroup = alt[2].first
54
56
  conditional = subgroup.first
55
57
 
56
- assert_equal( Conditional::Expression, conditional.class )
57
- assert_equal( 3, conditional.length )
58
+ assert_equal Conditional::Expression, conditional.class
59
+ assert_equal 3, conditional.length
58
60
 
59
- assert_equal( Conditional::Condition, conditional[0].class )
60
- assert_equal( '(2)', conditional[0].text )
61
+ assert_equal Conditional::Condition, conditional[0].class
62
+ assert_equal '(2)', conditional[0].text
61
63
 
62
64
  condition = conditional.condition
63
- assert_equal( Conditional::Condition, condition.class )
64
- assert_equal( '(2)', condition.text )
65
+ assert_equal Conditional::Condition, condition.class
66
+ assert_equal '(2)', condition.text
65
67
 
66
68
  branches = conditional.branches
67
- assert_equal( 2, branches.length )
68
- assert_equal( Array, branches.class )
69
+ assert_equal 2, branches.length
70
+ assert_equal Array, branches.class
69
71
  end
70
72
 
71
73
  def test_parse_conditional_nested
@@ -73,7 +75,7 @@ class TestParserConditionals < Test::Unit::TestCase
73
75
 
74
76
  root = RP.parse(regexp, 'ruby/2.0')
75
77
 
76
- assert_equal( regexp.source, root.to_s )
78
+ assert_equal regexp.source, root.to_s
77
79
 
78
80
  { 1 => [2, root[1]],
79
81
  2 => [2, root[1][1][0]],
@@ -83,9 +85,9 @@ class TestParserConditionals < Test::Unit::TestCase
83
85
  }.each do |index, test|
84
86
  branch_count, exp = test
85
87
 
86
- assert_equal( Conditional::Expression, exp.class )
87
- assert_equal( "(#{index})", exp.condition.text )
88
- assert_equal( branch_count, exp.branches.length )
88
+ assert_equal Conditional::Expression, exp.class
89
+ assert_equal "(#{index})", exp.condition.text
90
+ assert_equal branch_count, exp.branches.length
89
91
  end
90
92
  end
91
93
 
@@ -95,9 +97,9 @@ class TestParserConditionals < Test::Unit::TestCase
95
97
 
96
98
  root = RP.parse(regexp, 'ruby/2.0')
97
99
 
98
- assert_equal( regexp.source, root.to_s )
100
+ assert_equal regexp.source, root.to_s
99
101
 
100
- assert_equal( Alternation, root.first.class )
102
+ assert_equal Alternation, root.first.class
101
103
 
102
104
  [ [3, 'b|c|d', root[0][0][1][1][0][0]],
103
105
  [3, 'e|f|g', root[0][0][1][2][0][0]],
@@ -106,9 +108,9 @@ class TestParserConditionals < Test::Unit::TestCase
106
108
  ].each do |test|
107
109
  alt_count, alt_text, exp = test
108
110
 
109
- assert_equal( Alternation, exp.class )
110
- assert_equal( alt_text, exp.to_s )
111
- assert_equal( alt_count, exp.alternatives.length )
111
+ assert_equal Alternation, exp.class
112
+ assert_equal alt_text, exp.to_s
113
+ assert_equal alt_count, exp.alternatives.length
112
114
  end
113
115
  end
114
116
 
@@ -119,20 +121,20 @@ class TestParserConditionals < Test::Unit::TestCase
119
121
  root = RP.parse(regexp, 'ruby/2.0')
120
122
  branches = root[1].branches
121
123
 
122
- assert_equal( 2, branches.length )
124
+ assert_equal 2, branches.length
123
125
 
124
126
  seq_1, seq_2 = branches
125
127
 
126
128
  [seq_1, seq_2].each do |seq|
127
- assert( seq.is_a?( Sequence ),
128
- "Expected Condition, but got #{seq.class.name}")
129
+ assert seq.is_a?( Sequence ),
130
+ "Expected Condition, but got #{seq.class.name}"
129
131
 
130
- assert_equal( :expression, seq.type )
131
- assert_equal( :sequence, seq.token )
132
+ assert_equal :expression, seq.type
133
+ assert_equal :sequence, seq.token
132
134
  end
133
135
 
134
- assert_equal( 'T', seq_1.to_s )
135
- assert_equal( '', seq_2.to_s )
136
+ assert_equal 'T', seq_1.to_s
137
+ assert_equal '', seq_2.to_s
136
138
  end
137
139
 
138
140
 
@@ -28,48 +28,50 @@ class TestParserEscapes < Test::Unit::TestCase
28
28
 
29
29
  # unicode escapes
30
30
  /a\u{9879}/ => [1, :escape, :codepoint_list, EscapeSequence::Literal],
31
+
32
+ # hex escapes
33
+ /a\xFF/n => [1, :escape, :hex, EscapeSequence::Literal],
31
34
  }
32
35
 
33
- count = 0
34
- tests.each do |pattern, test|
35
- define_method "test_parse_escape_#{test[2]}_#{count+=1}" do
36
+ tests.each_with_index do |(pattern, (index, type, token, klass)), count|
37
+ define_method "test_parse_escape_#{token}_#{count+=1}" do
36
38
  root = RP.parse(pattern, 'ruby/1.9')
37
- exp = root.expressions[test[0]]
39
+ exp = root.expressions.at(index)
38
40
 
39
- assert( exp.is_a?( test[3] ),
40
- "Expected #{test[3]}, but got #{exp.class.name}")
41
+ assert exp.is_a?(klass),
42
+ "Expected #{klass}, but got #{exp.class.name}"
41
43
 
42
- assert_equal( test[1], exp.type )
43
- assert_equal( test[2], exp.token )
44
+ assert_equal type, exp.type
45
+ assert_equal token, exp.token
44
46
  end
45
47
  end
46
48
 
47
49
  def test_parse_escape_control_sequence_lower
48
50
  root = RP.parse(/a\\\c2b/)
49
51
 
50
- assert_equal( EscapeSequence::Control, root[2].class )
51
- assert_equal( '\\c2', root[2].text )
52
+ assert_equal EscapeSequence::Control, root[2].class
53
+ assert_equal '\\c2', root[2].text
52
54
  end
53
55
 
54
56
  def test_parse_escape_control_sequence_upper
55
57
  root = RP.parse(/\d\\\C-C\w/)
56
58
 
57
- assert_equal( EscapeSequence::Control, root[2].class )
58
- assert_equal( '\\C-C', root[2].text )
59
+ assert_equal EscapeSequence::Control, root[2].class
60
+ assert_equal '\\C-C', root[2].text
59
61
  end
60
62
 
61
63
  def test_parse_escape_meta_sequence
62
64
  root = RP.parse(/\Z\\\M-Z/n)
63
65
 
64
- assert_equal( EscapeSequence::Meta, root[2].class )
65
- assert_equal( '\\M-Z', root[2].text )
66
+ assert_equal EscapeSequence::Meta, root[2].class
67
+ assert_equal '\\M-Z', root[2].text
66
68
  end
67
69
 
68
70
  def test_parse_escape_meta_control_sequence
69
71
  root = RP.parse(/\A\\\M-\C-X/n)
70
72
 
71
- assert_equal( EscapeSequence::MetaControl, root[2].class )
72
- assert_equal( '\\M-\\C-X', root[2].text )
73
+ assert_equal EscapeSequence::MetaControl, root[2].class
74
+ assert_equal '\\M-\\C-X', root[2].text
73
75
  end
74
76
 
75
77
  end