ripper2ruby 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (111) hide show
  1. data/MIT-LICENSE +20 -0
  2. data/README.markdown +10 -0
  3. data/lib/core_ext/array/flush.rb +5 -0
  4. data/lib/core_ext/hash/delete_at.rb +5 -0
  5. data/lib/core_ext/object/meta_class.rb +5 -0
  6. data/lib/core_ext/object/try.rb +12 -0
  7. data/lib/erb/stripper.rb +48 -0
  8. data/lib/highlighters/ansi.rb +29 -0
  9. data/lib/ripper/event_log.rb +45 -0
  10. data/lib/ripper/ruby_builder.rb +168 -0
  11. data/lib/ripper/ruby_builder/buffer.rb +34 -0
  12. data/lib/ripper/ruby_builder/events/args.rb +40 -0
  13. data/lib/ripper/ruby_builder/events/array.rb +71 -0
  14. data/lib/ripper/ruby_builder/events/assignment.rb +55 -0
  15. data/lib/ripper/ruby_builder/events/block.rb +80 -0
  16. data/lib/ripper/ruby_builder/events/call.rb +123 -0
  17. data/lib/ripper/ruby_builder/events/case.rb +17 -0
  18. data/lib/ripper/ruby_builder/events/const.rb +47 -0
  19. data/lib/ripper/ruby_builder/events/for.rb +13 -0
  20. data/lib/ripper/ruby_builder/events/hash.rb +24 -0
  21. data/lib/ripper/ruby_builder/events/identifier.rb +41 -0
  22. data/lib/ripper/ruby_builder/events/if.rb +37 -0
  23. data/lib/ripper/ruby_builder/events/lexer.rb +159 -0
  24. data/lib/ripper/ruby_builder/events/literal.rb +47 -0
  25. data/lib/ripper/ruby_builder/events/method.rb +21 -0
  26. data/lib/ripper/ruby_builder/events/operator.rb +23 -0
  27. data/lib/ripper/ruby_builder/events/statements.rb +50 -0
  28. data/lib/ripper/ruby_builder/events/string.rb +117 -0
  29. data/lib/ripper/ruby_builder/events/symbol.rb +22 -0
  30. data/lib/ripper/ruby_builder/events/while.rb +27 -0
  31. data/lib/ripper/ruby_builder/queue.rb +33 -0
  32. data/lib/ripper/ruby_builder/stack.rb +125 -0
  33. data/lib/ripper/ruby_builder/token.rb +91 -0
  34. data/lib/ripper2ruby.rb +1 -0
  35. data/lib/ruby.rb +28 -0
  36. data/lib/ruby/aggregate.rb +71 -0
  37. data/lib/ruby/alternation/args.rb +25 -0
  38. data/lib/ruby/alternation/hash.rb +25 -0
  39. data/lib/ruby/alternation/list.rb +19 -0
  40. data/lib/ruby/args.rb +36 -0
  41. data/lib/ruby/array.rb +27 -0
  42. data/lib/ruby/assignment.rb +32 -0
  43. data/lib/ruby/assoc.rb +17 -0
  44. data/lib/ruby/block.rb +42 -0
  45. data/lib/ruby/call.rb +34 -0
  46. data/lib/ruby/case.rb +30 -0
  47. data/lib/ruby/const.rb +49 -0
  48. data/lib/ruby/for.rb +18 -0
  49. data/lib/ruby/hash.rb +14 -0
  50. data/lib/ruby/if.rb +35 -0
  51. data/lib/ruby/list.rb +40 -0
  52. data/lib/ruby/literal.rb +45 -0
  53. data/lib/ruby/method.rb +19 -0
  54. data/lib/ruby/node.rb +47 -0
  55. data/lib/ruby/node/composite.rb +68 -0
  56. data/lib/ruby/node/conversions.rb +66 -0
  57. data/lib/ruby/node/position.rb +35 -0
  58. data/lib/ruby/node/source.rb +29 -0
  59. data/lib/ruby/node/text.rb +121 -0
  60. data/lib/ruby/node/traversal.rb +82 -0
  61. data/lib/ruby/operator.rb +49 -0
  62. data/lib/ruby/params.rb +41 -0
  63. data/lib/ruby/statements.rb +45 -0
  64. data/lib/ruby/string.rb +40 -0
  65. data/lib/ruby/symbol.rb +27 -0
  66. data/lib/ruby/token.rb +51 -0
  67. data/lib/ruby/while.rb +32 -0
  68. data/test/all.rb +3 -0
  69. data/test/builder/stack_test.rb +67 -0
  70. data/test/builder/text_test.rb +118 -0
  71. data/test/context_test.rb +54 -0
  72. data/test/erb_stripper_test.rb +29 -0
  73. data/test/fixtures/all.rb.src +150 -0
  74. data/test/fixtures/source_1.rb +16 -0
  75. data/test/fixtures/source_2.rb +1 -0
  76. data/test/fixtures/stuff.rb +371 -0
  77. data/test/fixtures/template.html.erb +22 -0
  78. data/test/fixtures/tmp.rb +6 -0
  79. data/test/lib_test.rb +92 -0
  80. data/test/lib_test_helper.rb +103 -0
  81. data/test/libs.txt +227 -0
  82. data/test/nodes/args_test.rb +100 -0
  83. data/test/nodes/array_test.rb +141 -0
  84. data/test/nodes/assignment_test.rb +49 -0
  85. data/test/nodes/block_test.rb +125 -0
  86. data/test/nodes/call_test.rb +229 -0
  87. data/test/nodes/case_test.rb +68 -0
  88. data/test/nodes/comments_test.rb +25 -0
  89. data/test/nodes/const_test.rb +46 -0
  90. data/test/nodes/conversions_test.rb +9 -0
  91. data/test/nodes/for_test.rb +34 -0
  92. data/test/nodes/hash_test.rb +71 -0
  93. data/test/nodes/heredoc_test.rb +202 -0
  94. data/test/nodes/identifier_test.rb +51 -0
  95. data/test/nodes/if_test.rb +100 -0
  96. data/test/nodes/literals_test.rb +63 -0
  97. data/test/nodes/method_test.rb +92 -0
  98. data/test/nodes/namespaces_test.rb +65 -0
  99. data/test/nodes/node_test.rb +74 -0
  100. data/test/nodes/nodes_test.rb +23 -0
  101. data/test/nodes/operator_test.rb +241 -0
  102. data/test/nodes/separators_test.rb +97 -0
  103. data/test/nodes/statements_test.rb +70 -0
  104. data/test/nodes/string_test.rb +92 -0
  105. data/test/nodes/symbol_test.rb +57 -0
  106. data/test/nodes/unless_test.rb +42 -0
  107. data/test/nodes/until_test.rb +61 -0
  108. data/test/nodes/while_test.rb +71 -0
  109. data/test/test_helper.rb +51 -0
  110. data/test/traversal_test.rb +53 -0
  111. metadata +163 -0
@@ -0,0 +1,23 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class NodesTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ lines = File.read(File.dirname(__FILE__) + '/../fixtures/all.rb.src').split("\n")
7
+ lines.each do |line|
8
+ next if line =~ /^#/ || line.empty?
9
+ define_method :"test: to_ruby(#{line}) compiles to the original src" do
10
+ line.gsub!('\n', "\n")
11
+ assert_build(line)
12
+ end
13
+ end
14
+
15
+ Dir["#{File.dirname(__FILE__)}/../fixtures/**/*.rb"].sort.each do |filename|
16
+ # puts filename
17
+ define_method :"test: #{filename} compiles to the original src" do
18
+ next if filename.index('tmp.rb')
19
+ src = File.read(filename)
20
+ assert_build(src)
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,241 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class OperatorTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def assert_operator(expr, src, options)
7
+ row = options[:row] || 0
8
+ column = options[:column] || 0
9
+ length = options[:length] || src.length
10
+
11
+ assert expr.root.is_a?(Ruby::Program)
12
+ assert_equal src, expr.to_ruby
13
+ assert_equal src, expr.src
14
+ assert_equal row, expr.row
15
+ assert_equal column, expr.column
16
+ assert_equal length, expr.length
17
+ end
18
+
19
+ def assert_unary_operator(operator, options = {})
20
+ src = options[:src] || "#{operator}1"
21
+ expr = build(src).first
22
+
23
+ assert_equal Ruby::Unary, expr.class
24
+ assert_equal operator.to_s, expr.operator.token
25
+ # assert_equal options[:value] || 1, expr.operand.value
26
+
27
+ assert_operator(expr, src, options)
28
+ end
29
+
30
+ def assert_binary_operator(operator, options = {})
31
+ src = options[:src] || "1 #{operator} 2"
32
+ expr = build(src).first
33
+
34
+ assert_equal Ruby::Binary, expr.class
35
+ assert_equal operator.to_s, expr.operator.token
36
+ assert_equal options[:left] || 1, expr.left.value
37
+ assert_equal options[:right] || 2, expr.right.value
38
+
39
+ assert_operator(expr, src, options)
40
+ end
41
+
42
+ # UNARY
43
+
44
+ define_method :'test operator: +1 (+)' do
45
+ assert_unary_operator(:+, :src => '+a')
46
+ end
47
+
48
+ define_method :'test operator: -1 (-)' do
49
+ assert_unary_operator(:-, :src => '-b')
50
+ end
51
+
52
+ define_method :'test operator: !1 (!)' do
53
+ assert_unary_operator(:!)
54
+ end
55
+
56
+ define_method :'test operator: not 1 (not, without parentheses)' do
57
+ assert_unary_operator(:not, :src => 'not 1')
58
+ end
59
+
60
+ define_method :'test operator: not(1) (not, with parentheses)' do
61
+ assert_unary_operator(:not, :src => 'not(1)')
62
+ end
63
+
64
+ define_method :'test operator: ~1 (complement/bitwise-not)' do
65
+ assert_unary_operator(:~)
66
+ end
67
+
68
+ define_method :'test operator: (!1) (complement/bitwise-not, enclosing parentheses)' do
69
+ src = '(!1)'
70
+ assert_build(src)
71
+ end
72
+
73
+ define_method :"test operator: enclosing parentheses with leading space" do
74
+ src = 'not (@main or @sub)'
75
+ assert_build(src)
76
+ end
77
+
78
+ # BINARY
79
+
80
+ # arithmetical
81
+
82
+ define_method :'test operator: 1 + 2 (plus)' do
83
+ assert_binary_operator :'+'
84
+ end
85
+
86
+ define_method :'test operator: 1 - 2 (minus)' do
87
+ assert_binary_operator :'-'
88
+ end
89
+
90
+ define_method :'test operator: 1 * 2 (times)' do
91
+ assert_binary_operator :'*'
92
+ end
93
+
94
+ define_method :'test operator: 1 / 2 (over)' do
95
+ assert_binary_operator :'/'
96
+ end
97
+
98
+ define_method :'test operator: 1 ** 2 (exponentiation)' do
99
+ assert_binary_operator :'**'
100
+ end
101
+
102
+ define_method :'test operator: 1 % 2 (% / modulo)' do
103
+ assert_binary_operator :'%'
104
+ end
105
+
106
+ define_method :'test operator: (1 + 2) (plus, parentheses)' do
107
+ src = '(1+2)'
108
+ assert_build(src)
109
+ end
110
+
111
+ # logical
112
+
113
+ define_method :'test operator: 1 && 2 (&& / strong logical and)' do
114
+ assert_binary_operator :'&&'
115
+ end
116
+
117
+ define_method :'test operator: 1 || 2 (|| / strong logical or)' do
118
+ assert_binary_operator :'||'
119
+ end
120
+
121
+ define_method :'test binary: 1 and 2 (and / weak logical and)' do
122
+ assert_binary_operator :'and'
123
+ end
124
+
125
+ define_method :'test binary: 1 or 2 (or / weak logical or)' do
126
+ assert_binary_operator :'or'
127
+ end
128
+
129
+ define_method :'test operator: (1 and 2) (and, parentheses)' do
130
+ src = '(1 and 2)'
131
+ assert_build(src)
132
+ end
133
+
134
+ # bitwise
135
+
136
+ define_method :'test operator: 1 << 2 (<< / left-shift)' do
137
+ assert_binary_operator :'<<'
138
+ end
139
+
140
+ define_method :'test operator: 1 >> 2 (>> / right-shift)' do
141
+ assert_binary_operator :'>>'
142
+ end
143
+
144
+ define_method :'test binary: 1 & 2 (& / bit-wise and)' do
145
+ assert_binary_operator :'&'
146
+ end
147
+
148
+ define_method :'test binary: 1 | 2 (| / bit-wise or)' do
149
+ assert_binary_operator :'|'
150
+ end
151
+
152
+ define_method :'test binary: 1 ^ 2 (^ / bit-wise xor)' do
153
+ assert_binary_operator :'^'
154
+ end
155
+
156
+ # comparsion, equality, matching
157
+
158
+ define_method :'test binary: 1 < 2 (comparsion, smaller)' do
159
+ assert_binary_operator :'<'
160
+ end
161
+
162
+ define_method :'test binary: 1 <= 2 (comparsion, smaller or equal)' do
163
+ assert_binary_operator :'<='
164
+ end
165
+
166
+ define_method :'test binary: 1 > 2 (comparsion, greater)' do
167
+ assert_binary_operator :'>'
168
+ end
169
+
170
+ define_method :'test binary: 1 >= 2 (comparsion, greater or equal)' do
171
+ assert_binary_operator :'>='
172
+ end
173
+
174
+ define_method :'test binary: 1 <=> 2 (spaceship)' do
175
+ assert_binary_operator :'<=>'
176
+ end
177
+
178
+ define_method :'test binary: 1 == 2 (equals)' do
179
+ assert_binary_operator :'=='
180
+ end
181
+
182
+ define_method :'test binary: 1 != 2 (not equals)' do
183
+ assert_binary_operator :'!='
184
+ end
185
+
186
+ define_method :'test binary: 1 === 2 (threequals)' do
187
+ assert_binary_operator :'==='
188
+ end
189
+
190
+ define_method :'test binary: 1 =~ 2 (matches)' do
191
+ assert_binary_operator :'=~'
192
+ end
193
+
194
+ define_method :'test binary: 1 !~ 2 (not matches)' do
195
+ assert_binary_operator :'=~'
196
+ end
197
+
198
+ # TERNARY
199
+
200
+ define_method :'test ternary: 1 == 1 ? 2 : 3 (ifop)' do
201
+ src = '1 == 1 ? 2 : 3'
202
+ assert_build(src) do |node|
203
+ assert_equal Ruby::IfOp, node.first.class
204
+ end
205
+ end
206
+
207
+ define_method :'test ternary: ((1) ? 2 : (3)) (ifop, parentheses)' do
208
+ src = '((1) ? 2 : (3))'
209
+ assert_build(src)
210
+ end
211
+
212
+ # ======
213
+
214
+ define_method :'test operator order in an expression with multiple identical operators' do
215
+ src = 'foo + bar + baz'
216
+ assert_build(src)
217
+ # FIXME positions are messed up!
218
+ # assert_equal [0, 4], expr.left.operator.position.to_a
219
+ # assert_equal [0, 10], expr.operator.position.to_a
220
+ end
221
+
222
+ define_method :'test operator order in an expression with multiple different operators' do
223
+ src = 'foo + bar - baz'
224
+ assert_build(src)
225
+ end
226
+
227
+ define_method :'test operator order in an expression with multiple ternary operators (1)' do
228
+ src = "a ? b : c ? d : e"
229
+ assert_build(src)
230
+ end
231
+
232
+ define_method :'test operator order in an expression with multiple ternary operators (2)' do
233
+ src = "a ? b ? c : d : e"
234
+ assert_build(src)
235
+ end
236
+
237
+ # define_method :'test ambiguous argument' do
238
+ # src = 'a+ a +a'
239
+ # assert_equal src, build(src).to_ruby(true)
240
+ # end
241
+ end
@@ -0,0 +1,97 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class SeparatorsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test separators in method definition (1)" do
7
+ src = "def foo\n ;bar\nend"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :"test separators in method definition (2)" do
12
+ src = "def foo;\nend"
13
+ assert_build(src)
14
+ end
15
+ end
16
+
17
+ # def foo
18
+ # ;
19
+ # end
20
+ #
21
+ # def foo
22
+ # bar;
23
+ # end
24
+ #
25
+ # def foo
26
+ # bar
27
+ # ;
28
+ # end
29
+ #
30
+ # def foo
31
+ # ;bar
32
+ # end
33
+ #
34
+ # def foo
35
+ # ;
36
+ # bar
37
+ # end
38
+ #
39
+ # def foo
40
+ # ;bar;
41
+ # end
42
+ #
43
+ # def foo
44
+ # ;
45
+ # bar
46
+ # ;
47
+ # end
48
+ #
49
+ # def foo(a, b)
50
+ # end
51
+ #
52
+ # foo { |a, b| }
53
+ # foo { |a, b| ; }
54
+ # foo { |a, b| bar; }
55
+ # foo { |a, b| ;bar }
56
+ #
57
+ # foo do |a, b|
58
+ # ;
59
+ # end
60
+ #
61
+ # foo do
62
+ # bar;
63
+ # end
64
+ #
65
+ # foo do
66
+ # bar
67
+ # ;
68
+ # end
69
+ #
70
+ # foo do
71
+ # ;bar
72
+ # end
73
+ #
74
+ # foo do
75
+ # ;
76
+ # bar
77
+ # end
78
+ #
79
+ # foo do
80
+ # ;bar;
81
+ # end
82
+ #
83
+ # foo do
84
+ # ;
85
+ # bar
86
+ # ;
87
+ # end
88
+ #
89
+ # begin
90
+ # ;
91
+ # rescue
92
+ # ;
93
+ # else
94
+ # ;
95
+ # ensure
96
+ # ;
97
+ # end
@@ -0,0 +1,70 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class StatementsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test parse error raises a ParseError" do
7
+ src = '[idx [rows, cols]]'
8
+ assert_raises(Ripper::RubyBuilder::ParseError) { build(src) }
9
+ end
10
+
11
+ define_method :"test a body with a single call, no semicolon" do
12
+ src = 'foo'
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :"test a body with a single call, lefthand semicolon" do
17
+ src = ';foo'
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :"test a body with a single call, righthand semicolon" do
22
+ src = 'foo;'
23
+ assert_build(src)
24
+ end
25
+
26
+ define_method :"test a body with a single call, two semicolons" do
27
+ src = ';foo;'
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :"test a body with a few calls and a few semicolons (1)" do
32
+ src = ';foo;;bar;baz;'
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :"test a body with a few calls and a few semicolons (2)" do
37
+ src = ';foo;;bar;;;;baz;'
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :"test a body with a few calls and a few semicolons (3)" do
42
+ src = ';;;foo;;bar;baz;;;;'
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :"test a nested body with a single call, no semicolon (1)" do
47
+ src = '(foo)'
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :"test a nested body with a single call, no semicolon (2)" do
52
+ src = '(((foo)); (bar))'
53
+ assert_build(src)
54
+ end
55
+
56
+ define_method :"test a nested body with a single call, no semicolon (2)" do
57
+ src = '(((foo)); ((bar); (baz)));'
58
+ assert_build(src)
59
+ end
60
+
61
+ define_method :"test a statement with preceding and succeding whitespace" do
62
+ src = "\n foo \n \n"
63
+ assert_build(src)
64
+ end
65
+
66
+ define_method :"test ruby code using the __END__ keyword" do
67
+ src = "foo\n__END__\nbar"
68
+ assert_build(src)
69
+ end
70
+ end
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class StringTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def assert_string(src, value = nil, klass = Ruby::String)
7
+ assert_build(src) do |node|
8
+ assert_equal klass, node.first.class
9
+ assert_equal value, node.first.value if value
10
+ end
11
+ end
12
+
13
+ define_method :"test an empty string" do
14
+ src = '""'
15
+ assert_string(src, '')
16
+ end
17
+
18
+ define_method :"test a double quoted string" do
19
+ src = '"foo"'
20
+ assert_string(src, 'foo')
21
+ end
22
+
23
+ define_method :"test a single quoted string" do
24
+ src = "'foo'"
25
+ assert_string(src, 'foo')
26
+ end
27
+
28
+ define_method :"test a percent-parens delimited string" do
29
+ src = "%(foo)"
30
+ assert_string(src, 'foo')
31
+ end
32
+
33
+ define_method :"test a percent-dot delimited string" do
34
+ src = "%.foo."
35
+ assert_string(src, 'foo')
36
+ end
37
+
38
+ define_method :"test a percent-pipe delimited string" do
39
+ src = "%|foo|"
40
+ assert_string(src, 'foo')
41
+ end
42
+
43
+ define_method :"test a double-quoted string with an embedded expression" do
44
+ src = '"foo#{bar}"'
45
+ assert_string(src)
46
+ end
47
+
48
+ define_method :"test a percent-parentheses delimited string with an embedded expression" do
49
+ src = '%(foo #{bar})'
50
+ assert_string(src)
51
+ end
52
+
53
+ define_method :"test a percent-parentheses delimited string after a word-list" do
54
+ src = "%w(a)\n%(b)"
55
+ assert_build(src)
56
+ end
57
+
58
+ define_method :"test a backtick delimited string" do
59
+ src = "`foo`"
60
+ assert_build(src)
61
+ end
62
+
63
+ define_method :"test a percent-x delimited string" do
64
+ src = "%x(foo)"
65
+ assert_build(src)
66
+ end
67
+
68
+ define_method :"test a slash delimited regexp" do
69
+ src = "/foo/"
70
+ assert_build(src)
71
+ end
72
+
73
+ define_method :"test a percent-r delimited regexp" do
74
+ src = "%r(foo)"
75
+ assert_build(src)
76
+ end
77
+
78
+ define_method :"test a string with a backreference" do
79
+ src = '"#{$1}"'
80
+ assert_build(src)
81
+ end
82
+
83
+ define_method :"test a string with a dvar" do
84
+ src = '"#$0"'
85
+ assert_build(src)
86
+ end
87
+
88
+ define_method :'test string concat' do
89
+ src = "'a' 'b'"
90
+ assert_build(src)
91
+ end
92
+ end