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,68 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class CaseTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :'test case expression (1)' do
7
+ src = "case a; when 1; 2 end"
8
+ assert_build(src) do |node|
9
+ assert_equal Ruby::Case, node.first.class
10
+ end
11
+
12
+ end
13
+
14
+ define_method :'test case expression (2)' do
15
+ src = "case (a) when 1; 2 end"
16
+ assert_build(src)
17
+ end
18
+
19
+ define_method :'test case expression (3)' do
20
+ src = "case (a;) when 1; 2 end"
21
+ assert_build(src)
22
+ end
23
+
24
+ define_method :'test case expression (4)' do
25
+ src = "case (a); when 1; 2 end"
26
+ assert_build(src)
27
+ end
28
+
29
+ define_method :'test case expression (5)' do
30
+ src = "case (a;); when 1; 2 end"
31
+ assert_build(src)
32
+ end
33
+
34
+ define_method :'test case expression (6)' do
35
+ src = "case (a;b); when 1; 2 end"
36
+ assert_build(src)
37
+ end
38
+
39
+ define_method :'test when expression (1)' do
40
+ src = "case a; when 1; 2 end"
41
+ assert_build(src)
42
+ end
43
+
44
+ define_method :'test when expression (2)' do
45
+ src = "case a; when 1, 2; 2 end"
46
+ assert_build(src)
47
+ end
48
+
49
+ define_method :'test when expression (3)' do
50
+ src = "case a; when (1; 2), (3; 4;); 2 end"
51
+ assert_build(src)
52
+ end
53
+
54
+ define_method :'test case block, semicolon separated (4)' do
55
+ src = "case (true); when 1; false; when 2, 3, 4; nil; else; nil; end"
56
+ assert_build(src)
57
+ end
58
+
59
+ define_method :'test case block, newline separated' do
60
+ src = "case true\n when 1\n false\n when 2\n nil\n else\n nil\n end"
61
+ assert_build(src)
62
+ end
63
+
64
+ define_method :'test case block, newline separated, using then' do
65
+ src = "case true\n when 1 then false\n when 2 then nil\n else\n nil\n end"
66
+ assert_build(src)
67
+ end
68
+ end
@@ -0,0 +1,25 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class CommentsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test a variable with a preceeding 3-line comment" do
7
+ src = "# 1\n# 2\n# 3\na"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :"test a variable with a succeeding comment" do
12
+ src = "a # 1"
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :"test a block with a variable with a succeeding comment" do
17
+ src = "begin\n a # 1 \nend"
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :"test a method definition with a variable with a succeeding comment" do
22
+ src = "def foo(a = 1)\n a # 1 \nend"
23
+ assert_build(src)
24
+ end
25
+ end
@@ -0,0 +1,46 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ConstTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test a const" do
7
+ src = 'A'
8
+ assert_build(src) do |node|
9
+ assert_equal Ruby::Const, node.first.class
10
+ end
11
+ end
12
+
13
+ define_method :"test a module" do
14
+ src = "module A::B ; end"
15
+ assert_build(src) do |node|
16
+ assert_equal Ruby::Module, node.first.class
17
+ end
18
+ end
19
+
20
+ define_method :"test a class" do
21
+ src = "class A::B < C ; end"
22
+ assert_build(src) do |node|
23
+ assert_equal Ruby::Class, node.first.class
24
+ end
25
+ end
26
+
27
+ define_method :"test const on self.class" do
28
+ src = 'self.class::A'
29
+ assert_build(src)
30
+ end
31
+
32
+ define_method :"test a metaclass" do
33
+ src = "class << self; self; end"
34
+ assert_build(src)
35
+ end
36
+
37
+ define_method :"test a metaclass inside of a call" do
38
+ src = "foo (class << @bar; self; end)"
39
+ assert_build(src)
40
+ end
41
+
42
+ define_method :"test a top-level const ref after a class def" do
43
+ src = "class A ; end\n::B = 3"
44
+ assert_build(src)
45
+ end
46
+ end
@@ -0,0 +1,9 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class ConversionsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test from_ruby" do
7
+ Ruby::Node.from_ruby('1')
8
+ end
9
+ end
@@ -0,0 +1,34 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ # for i in 1..5; ...; end
4
+
5
+ class ForTest < Test::Unit::TestCase
6
+ include TestHelper
7
+
8
+ define_method :'test for loop, semicolon separated' do
9
+ src = "for i in [1]; a; end"
10
+ assert_build(src) do |node|
11
+ assert_equal Ruby::For, node.first.class
12
+ end
13
+ end
14
+
15
+ define_method :'test for loop, newline separated' do
16
+ src = "for i in [1]\n a\n end"
17
+ assert_build(src)
18
+ end
19
+
20
+ define_method :'test for loop, do block' do
21
+ src = "for i in [1] do a\n end"
22
+ assert_build(src)
23
+ end
24
+
25
+ define_method :'test for loop contained in a do block' do
26
+ src = "lambda do\n for a in b\n end\nend"
27
+ assert_build(src)
28
+ end
29
+
30
+ define_method :'test nested for loop, newline separated' do
31
+ src = "\nfor i in 0...1 do\n for j in 0...2 do\n end\nend\n"
32
+ assert_build(src)
33
+ end
34
+ end
@@ -0,0 +1,71 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class HashTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ def hash(src)
7
+ build(src).first
8
+ end
9
+
10
+ define_method :'test a hash: { :foo => :bar }' do
11
+ src = '{ :foo => :bar }'
12
+ assert_build(src) do |node|
13
+ assert_equal Ruby::Hash, node.first.class
14
+ assert_equal({ :foo => :bar }, node.first.value)
15
+ end
16
+ end
17
+
18
+ define_method :'test a nested hash: { :a => { :b => { :b => :c, }, }, } (w/ dangling commas)' do
19
+ src = '{ :a => { :b => { :b => :c, }, }, }'
20
+ assert_build(src) do |node|
21
+ assert_equal Ruby::Hash, node.first.class
22
+ assert_equal({ :a => { :b => { :b => :c } } }, node.first.value)
23
+ end
24
+ end
25
+
26
+ define_method :'test a bare nested hash: t(:a => { :b => :b, :c => { :d => :d } })' do
27
+ src = 't(:a => { :b => :b, :c => { :d => :d } })'
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :'test a hash: { :if => :delete } (symbol that is a keyword)' do
32
+ src = "{ :if => :foo }"
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :'test a hash: { :if => :delete } (symbol that is a keyword)' do
37
+ src = '{ :a => :a, :b => "#{b 1}" }'
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :'test a hash: { foo: bar } (Ruby 1.9 labels)' do
42
+ src = "{ foo: bar }"
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :'test a bare hash: t(:foo => :bar)' do
47
+ src = 't(:a => :a, :b => :b)'
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :'test hash assignment' do
52
+ src = "foo[:bar] = :baz"
53
+ assert_build(src)
54
+ end
55
+
56
+ define_method :'test hash length: with and without whitespace' do
57
+ assert_equal 22, build(" {:a=>:a,:b=>:b,:c=>:c}").first.length
58
+ assert_equal 30, build(" {:a => :a, :b => :b, :c => :c}").first.length
59
+ assert_equal 30, build(" { :a=>:a , :b=>:b , :c=>:c }").first.length
60
+ assert_equal 32, build(" { :a => :a, :b => :b, :c => :c }").first.length
61
+ assert_equal 34, build(" { :a => :a, :b => :b, :c => :c }").first.length(true)
62
+ end
63
+
64
+ define_method :'test bare hash length: with and without whitespace' do
65
+ assert_equal 20, build(" t(:a=>:a,:b=>:b,:c=>:c)").first.arguments.first.length
66
+ assert_equal 28, build(" t(:a => :a, :b => :b, :c => :c)").first.arguments.first.length
67
+ assert_equal 26, build(" t( :a=>:a , :b=>:b , :c=>:c )").first.arguments.first.length
68
+ assert_equal 28, build(" t( :a => :a, :b => :b, :c => :c )").first.arguments.first.length
69
+ assert_equal 29, build(" t( :a => :a, :b => :b, :c => :c )").first.arguments.first.length(true)
70
+ end
71
+ end
@@ -0,0 +1,202 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class HeredocTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test an empty heredoc" do
7
+ src = "\n<<-eos\neos\n"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :"test a heredoc" do
12
+ src = "<<-eos\nfoo\neos"
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :"test a heredoc and strings" do
17
+ src = "'string'\n<<-eos\nheredoc\neos\n'string'"
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :"test a heredoc and dyna symbols" do
22
+ src = ":'symbol'\n<<-eos\nheredoc\neos\n:'symbol'"
23
+ assert_build(src)
24
+ end
25
+
26
+ define_method :"test a heredoc and words" do
27
+ src = "%w(words)\n<<-eoc\n heredoc\neoc\n%w(words)"
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :"test a heredoc and a call with words" do
32
+ src = "foo(%w(words))\n<<-eoc\n\neoc"
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :"test a heredoc with a semicolon" do
37
+ src = "\n<<-end;\nfoo\nend\n"
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :"test a quoted heredoc with extra whitespace and a semicolon" do
42
+ src = "\n<<-'end' ;\n foo\nend\nfoo;"
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :"test a heredoc with an embedded expression" do
47
+ src = "<<-eos\n foo \#{bar} baz\neos"
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :"test a heredoc with an embedded array access" do
52
+ src = "<<-end\n\#{a['b']}\nend"
53
+ src = "<<-end\n\#{'a'}\nend"
54
+ assert_build(src)
55
+ end
56
+
57
+ define_method :"test method call with heredoc" do
58
+ src = "foo(<<-eos)\n foo\neos"
59
+ assert_build(src)
60
+ end
61
+
62
+ define_method :"test method call with heredoc (2)" do
63
+ src = "foo(<<-eos\n foo\neos\n)"
64
+ assert_build(src)
65
+ end
66
+
67
+ define_method :"test method call with heredoc with arguments" do
68
+ src = "foo(<<-eos, __FILE__, __LINE__)\n foo\neos"
69
+ assert_build(src)
70
+ end
71
+
72
+ define_method :"test method call with heredoc with arguments / operator" do
73
+ src = "foo(<<-eos, __FILE__, __LINE__ + 1)\n foo\neos"
74
+ assert_build(src)
75
+ end
76
+
77
+ define_method :"test method call with heredoc with arguments / call" do
78
+ src = "foo(<<-eos, __FILE__, line)\n foo\neos"
79
+ assert_build(src)
80
+ end
81
+
82
+ define_method :"test method call with heredoc with a string arg with an embedded expression" do
83
+ src = "foo(<<-eos, \"\#{bar}\")\n foo\neos"
84
+ assert_build(src)
85
+ end
86
+
87
+ define_method :"test a begin block with a heredoc" do
88
+ src = "begin <<-src \n\n\nfoo\nsrc\n end"
89
+ assert_build(src)
90
+ end
91
+
92
+ define_method :"test a do block with a heredoc and an embedded expression" do
93
+ src = "each do\n <<-src \n\nfoo\n\#{bar}\nsrc\n end\n"
94
+ assert_build(src)
95
+ end
96
+
97
+ define_method :"test heredoc with a nested embedded expression" do
98
+ src = "<<-eos\n \#{\"\n \#{sym}\n \"}\neos"
99
+ assert_build(src)
100
+ end
101
+
102
+ define_method :"test heredoc with a multiline embedded expression" do
103
+ src = "<<-eos\n \t\#{ a\n }\neos"
104
+ assert_build(src)
105
+ end
106
+
107
+ define_method :"test heredoc with an embedded expression containing a dyna_symbol" do
108
+ src = "<<-eos\n\#{:'dyna_symbol'}\neos"
109
+ assert_build(src)
110
+ end
111
+
112
+ define_method :"test a heredoc with a comment on the first line" do
113
+ src = "<<-eos # comment\neos\nfoo\nfoo\n"
114
+ assert_build(src)
115
+ end
116
+
117
+ define_method :"test a heredoc with a comment on the first line, followed by a single-quoted string" do
118
+ src = "<<-eos # comment\nstring\neos\n'index'"
119
+ assert_build(src)
120
+ end
121
+
122
+ define_method :"test command_call w/ a heredoc with an if modifier on heredoc_beg" do
123
+ src = "foo <<-eos if bar\na\neos\nbaz"
124
+ assert_build(src)
125
+ end
126
+
127
+ define_method :"test a heredoc with a method call on the heredoc_beg token" do
128
+ src = "<<-eos.foo\neos\nbar\n"
129
+ assert_build(src)
130
+ end
131
+
132
+ define_method :"test a heredoc with an xstring as a heredoc_beg token" do
133
+ src = "foo = <<-`foo`\nfoo"
134
+ assert_build(src)
135
+ end
136
+
137
+ define_method :"test multiple heredocs" do
138
+ src = "<<-end\nend\n<<-end\n\nend\n"
139
+ assert_build(src)
140
+ end
141
+
142
+ define_method :"test a stacked quoted heredoc" do
143
+ src = 'heredocs = <<-"foo", <<-"bar"
144
+ I said foo.
145
+ foo
146
+ I said bar.
147
+ bar
148
+ '
149
+ assert_build(src)
150
+ end
151
+
152
+ define_method :"test a heredoc after an empty words list" do
153
+ src = "a = %w[]\n<<-eos\nfoo\neos"
154
+ assert_build(src)
155
+ end
156
+
157
+ define_method :"test a heredoc containing an __END__ keyword" do
158
+ src = "<<-eos\nfoo\n__END__\neos"
159
+ assert_build(src)
160
+ end
161
+
162
+ define_method :"test a stuffed heredoc (from rubylexer tests)" do
163
+ src = <<-eos
164
+ p <<stuff+'foobar'.tr('j-l','d-f') \\
165
+ +"more stuff"
166
+ 12345678
167
+ the quick brown fox jumped over the lazy dog
168
+ stuff
169
+ eos
170
+ assert_build(src)
171
+ end
172
+
173
+ define_method :"test a stuffed heredoc (from rubylexer tests) (2)" do
174
+ src = <<-eos
175
+ p(<<stuff+'foobar')\\
176
+ +"more stuff"
177
+ some stuff
178
+ stuff
179
+ eos
180
+ assert_build(src)
181
+ end
182
+
183
+ define_method :"test a stuffed heredoc (from rubylexer tests) (3)" do
184
+ src = <<-eos
185
+ p <<stuff+'foobar' \\
186
+ +"more stuff"
187
+ some stuff
188
+ stuff
189
+ eos
190
+ assert_build(src)
191
+ end
192
+
193
+ define_method :"test a stuffed heredoc (from rubylexer tests) (4)" do
194
+ src = <<-eos
195
+ p <<stuff+'foobar'+
196
+ some stuff
197
+ stuff
198
+ "more stuff"
199
+ eos
200
+ assert_build(src)
201
+ end
202
+ end