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,51 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class IdentifierTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test a variable" do
7
+ src = 'foo'
8
+ identifier = build(src).first
9
+ assert_build(src) do |node|
10
+ assert_equal Ruby::Variable, node.first.class
11
+ end
12
+ end
13
+
14
+ define_method :"test an instance variable" do
15
+ src = '@foo'
16
+ assert_build(src) do |node|
17
+ assert_equal Ruby::Variable, node.first.class
18
+ end
19
+ end
20
+
21
+ define_method :"test a class variable" do
22
+ src = '@@foo'
23
+ assert_build(src) do |node|
24
+ assert_equal Ruby::Variable, node.first.class
25
+ end
26
+ end
27
+
28
+ define_method :"test a global variable" do
29
+ src = '$foo'
30
+ assert_build(src) do |node|
31
+ assert_equal Ruby::Variable, node.first.class
32
+ end
33
+ end
34
+
35
+ define_method :"test keyword __FILE__" do
36
+ src = '__FILE__'
37
+ assert_build(src) do |node|
38
+ assert_equal Ruby::Keyword, node.first.class
39
+ end
40
+ end
41
+
42
+ define_method :"test keyword __LINE__" do
43
+ src = '__LINE__'
44
+ assert_build(src)
45
+ end
46
+
47
+ define_method :"test keyword __ENCODING__" do
48
+ src = '__ENCODING__'
49
+ assert_build(src)
50
+ end
51
+ end
@@ -0,0 +1,100 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class IfTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :'test if block, semicolon separated' do
7
+ src = "if true; false end"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :'test if block, newline separated' do
12
+ src = "if true\n false\n end"
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :'test if block, newline separated' do
17
+ src = "if true\n false\n end"
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :'test if block w/ then, not separated' do
22
+ src = "if true then false end"
23
+ assert_build(src)
24
+ end
25
+
26
+ define_method :'test if block w/ else block, semicolon separated' do
27
+ src = "if true; false; else; true end"
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :'test if block w/ else block, newline separated' do
32
+ src = "if true\n false\n else\n true end"
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :'test if block w/ elsif block, semicolon separated' do
37
+ src = "if true; false; elsif false; true end"
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :'test if block w/ elsif block, newline separated' do
42
+ src = "if true\n false\n elsif false\n true end"
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :'test if block w/ then, else block and elsif block, semicolon separated' do
47
+ src = "if true then false; elsif false then true; else nil end"
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :'test if block w/ then, else block and elsif block, newline separated' do
52
+ src = "if a == 1 then b\nelsif b == c then d\ne\nf\nelse e end"
53
+ assert_build(src)
54
+ end
55
+
56
+ define_method :'test if modifier' do
57
+ src = "foo if true"
58
+ assert_build(src)
59
+ end
60
+
61
+ define_method :'test if modifier after a keyword statement' do
62
+ src = "return if true"
63
+ assert_build(src)
64
+ end
65
+
66
+ define_method :'test if modifier after an assignment to call' do
67
+ src = 'foo.bar += bar if bar'
68
+ assert_build(src)
69
+ end
70
+
71
+ define_method :'test if modifier after call multiassignment with a splat' do
72
+ src = 'foo, bar = *baz if bum'
73
+ assert_build(src)
74
+ end
75
+
76
+ define_method :'test if modifier after call with a splat argument' do
77
+ src = 'foo *args if bar?'
78
+ assert_build(src)
79
+ end
80
+
81
+ define_method :'test if modifier after array access' do
82
+ src = 'pos[1] if pos'
83
+ assert_build(src)
84
+ end
85
+
86
+ define_method :'test if modifier testing defined?' do
87
+ src = 'a if (defined? a)'
88
+ assert_build(src)
89
+ end
90
+
91
+ define_method :'test rescue modifier' do
92
+ src = 'rescued rescue rescuing'
93
+ assert_build(src)
94
+ end
95
+
96
+ define_method :'test rescue modifier after assignment' do
97
+ src = 'rescued = assigned rescue rescueing'
98
+ assert_build(src)
99
+ end
100
+ end
@@ -0,0 +1,63 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class LiteralsTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test an integer" do
7
+ src = '1'
8
+ assert_build(src) do |node|
9
+ assert_equal Ruby::Integer, node.first.class
10
+ end
11
+ end
12
+
13
+ define_method :"test a float" do
14
+ src = '1.1'
15
+ assert_build(src) do |node|
16
+ assert_equal Ruby::Float, node.first.class
17
+ end
18
+ end
19
+
20
+ define_method :"test nil" do
21
+ src = 'nil'
22
+ assert_build(src) do |node|
23
+ assert_equal Ruby::Nil, node.first.class
24
+ end
25
+ end
26
+
27
+ define_method :"test true" do
28
+ src = 'true'
29
+ assert_build(src) do |node|
30
+ assert_equal Ruby::True, node.first.class
31
+ end
32
+ end
33
+
34
+ define_method :"test false" do
35
+ src = 'false'
36
+ assert_build(src) do |node|
37
+ assert_equal Ruby::False, node.first.class
38
+ end
39
+ end
40
+
41
+ define_method :"test range (..)" do
42
+ src = '1..2'
43
+ assert_build(src) do |node|
44
+ assert_equal Ruby::Range, node.first.class
45
+ end
46
+ end
47
+
48
+ define_method :"test range (..)" do
49
+ src = '1...2'
50
+ literal = build(src).first
51
+ assert literal.is_a?(Ruby::Range)
52
+ assert_equal 1...2, literal.value
53
+ assert_equal src, literal.to_ruby
54
+ end
55
+
56
+ define_method :"test character" do
57
+ src = '?a'
58
+ literal = build(src).first
59
+ assert literal.is_a?(Ruby::Char)
60
+ assert_equal 'a', literal.value
61
+ assert_equal src, literal.to_ruby
62
+ end
63
+ end
@@ -0,0 +1,92 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class MethodTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :"test a method definition" do
7
+ src = "def foo(a, b = nil, c = :foo, *d, &block)\n bar\n baz\n end"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :"test a method definition with an options hash" do
12
+ src = "def foo(a, b = {})\nend"
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :"test a method definition without parentheses" do
17
+ src = "def foo a, b = nil, c = :foo, *d, &block\n bar\n baz\n end"
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :"test a class method definition (using self, keyword)" do
22
+ src = "def self.for(options)\nend"
23
+ assert_build(src)
24
+ end
25
+
26
+ define_method :"test method definition: def foo(b = :b, c=:c); end (optional args w/ whitespace differences)" do
27
+ src = 'def foo(a = 1, b=2); end'
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :"test method definition: def t(a = []); end" do
32
+ src = "def t(a = []) end"
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :"test: method definition: def t(*) end (a single splat)" do
37
+ src = "def t(*) end"
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :"test method definition: def <<()" do
42
+ src = "def <<(arg) end"
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :"test method definition: def | " do
47
+ src = "def | ; end"
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :"test method definition: def |(foo) " do
52
+ src = "class A < B; def |(foo); end; end"
53
+ assert_build(src)
54
+ end
55
+
56
+ define_method :"test method body w/ statements separated by semicolon" do
57
+ src = "def <<(arg) foo; bar; end"
58
+ assert_build(src)
59
+ end
60
+
61
+ define_method :"test method body rescue with error var" do
62
+ src = "def t\nrescue => e\nend"
63
+ assert_build(src)
64
+ end
65
+
66
+ define_method :"test method body rescue and ensure block" do
67
+ src = "def a(b, c)\n d\nrescue A\n e\nensure\nb\nend"
68
+ assert_build(src)
69
+ end
70
+
71
+ define_method :"test a method definition with a semicolon and a block in the body" do
72
+ src = "def foo ; bar { |k, v| k } end"
73
+ assert_build(src)
74
+ end
75
+
76
+ define_method :"test a method definition with an identifier that is also a keyword (?) (class)" do
77
+ src = "class A\n def class\n end\nend"
78
+ assert_build(src)
79
+ end
80
+
81
+ define_method :"test a method definition with an identifier that is also a keyword (?) (end)" do
82
+ src = "def end\nend"
83
+ assert_build(src)
84
+ end
85
+
86
+ define_method :"test a method definition that returns a number" do
87
+ src = "def def; 234; end"
88
+ assert_build(src)
89
+ end
90
+
91
+
92
+ end
@@ -0,0 +1,65 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class NamespacesTest < Test::Unit::TestCase
4
+ include TestHelper
5
+
6
+ define_method :'test method definition and call: def A::foo() B::foo(); end (multiple :: separators)' do
7
+ src = "def A::foo() B::foo(); end"
8
+ assert_build(src)
9
+ end
10
+
11
+ define_method :'test method call: A::B::\nfoo() (multiple :: separators)' do
12
+ src = "C::\nD::foo()"
13
+ assert_build(src)
14
+ end
15
+
16
+ define_method :"test a const path" do
17
+ src = 'A::B::C'
18
+ assert_build(src)
19
+ end
20
+
21
+ define_method :"test a const path" do
22
+ src = "A::B::C::\nD"
23
+ assert_build(src)
24
+ end
25
+
26
+ define_method :"test a const path with toplevel const ref" do
27
+ src = '::A::B'
28
+ assert_build(src)
29
+ end
30
+
31
+ define_method :"test namespace separator with method call and const argument" do
32
+ src = 'A::foo(B)'
33
+ assert_build(src)
34
+ end
35
+
36
+ define_method :"test namespace separator in a method definition" do
37
+ src = 'def A::foo(); end'
38
+ assert_build(src)
39
+ end
40
+
41
+ define_method :"test namespaced const after namespace separator in a method definition" do
42
+ src = "def A::foo(); end\n::B::C"
43
+ assert_build(src)
44
+ end
45
+
46
+ define_method :"test use const after namespace separator in a method definition" do
47
+ src = "def A::foo()\n a = B\n end"
48
+ assert_build(src)
49
+ end
50
+
51
+ define_method :"test a nested namespaced class" do
52
+ src = "class A < B::C\nclass D\nend\nend"
53
+ assert_build(src)
54
+ end
55
+
56
+ define_method :"test a class method definition calling a class method (namespace seps)" do
57
+ src = "def A::foo()\nB::foo()\n end"
58
+ assert_build(src)
59
+ end
60
+
61
+ define_method :"test a class method definition (using const) containing a class method call (using ::)" do
62
+ src = "def A.foo(a, b = nil, c = :foo, *d, &block)\n A::B()\n baz\n end"
63
+ assert_build(src)
64
+ end
65
+ end
@@ -0,0 +1,74 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+
3
+ class NodeTest < Test::Unit::TestCase
4
+ def statements(src)
5
+ Ripper::RubyBuilder.build(src).statements
6
+ end
7
+
8
+ # def test_src_pos
9
+ # statements = statements(" aaaaaaa\nbbbbb\ncccc(:c)\nddd\nee\nf")
10
+ # assert_equal 2, statements[0].src_pos
11
+ # assert_equal 10, statements[1].src_pos
12
+ # assert_equal 16, statements[2].src_pos
13
+ # assert_equal 21, statements[2].arguments.first.src_pos
14
+ # assert_equal 25, statements[3].src_pos
15
+ # assert_equal 29, statements[4].src_pos
16
+ # assert_equal 32, statements[5].src_pos
17
+ # end
18
+
19
+ # def test_update_position
20
+ # program = Ripper::RubyBuilder.build("a\nb\nc(:c); d(:d); e(:e)\nf")
21
+ #
22
+ # b = program.statements[1]
23
+ # c = program.statements[2]
24
+ # d = program.statements[3]
25
+ # e = program.statements[4]
26
+ # f = program.statements[5]
27
+ #
28
+ # assert_equal [1, 0], b.position
29
+ # assert_equal [2, 0], c.position
30
+ # assert_equal [2, 7], d.position
31
+ # assert_equal [2, 14], e.position
32
+ # assert_equal [3, 0], f.position
33
+ #
34
+ # program.send(:update_positions, 2, c.length, 3)
35
+ #
36
+ # assert_equal [1, 0], b.position
37
+ # assert_equal [2, 0], c.position
38
+ # assert_equal [2, 10], d.position
39
+ # assert_equal [2, 17], e.position
40
+ # assert_equal [3, 0], f.position
41
+ # end
42
+ #
43
+ # def test_update_children_positions
44
+ # program = Ripper::RubyBuilder.build("a(:a); b(:b, ['b', 1], { :b => :'b' })")
45
+ #
46
+ # a = program.statements.select { |s| s.identifier.token == 'a' if s }.first
47
+ # b = program.statements.select { |s| s.identifier.token == 'b' if s }.first
48
+ #
49
+ # assert_equal [0, 0], a.position
50
+ # assert_equal [0, 7], b.position
51
+ # assert_equal [0, 9], b.arguments[0].position
52
+ # assert_equal [0, 13], b.arguments[1].position
53
+ # assert_equal [0, 14], b.arguments[1][0].position
54
+ # assert_equal [0, 19], b.arguments[1][1].position
55
+ # assert_equal [0, 23], b.arguments[2].position
56
+ # assert_equal [0, 25], b.arguments[2].assocs[0].position
57
+ # assert_equal [0, 25], b.arguments[2].assocs[0].key.position
58
+ # assert_equal [0, 31], b.arguments[2].assocs[0].value.position
59
+ # v = b.arguments[2].assocs[0].value
60
+ #
61
+ # program.send(:update_positions, 0, a.length, 3)
62
+ #
63
+ # assert_equal [0, 0], a.position
64
+ # assert_equal [0, 10], b.position
65
+ # assert_equal [0, 12], b.arguments[0].position
66
+ # assert_equal [0, 16], b.arguments[1].position
67
+ # assert_equal [0, 17], b.arguments[1][0].position
68
+ # assert_equal [0, 22], b.arguments[1][1].position
69
+ # assert_equal [0, 26], b.arguments[2].position
70
+ # assert_equal [0, 28], b.arguments[2].assocs[0].position
71
+ # assert_equal [0, 28], b.arguments[2].assocs[0].key.position
72
+ # assert_equal [0, 34], b.arguments[2].assocs[0].value.position
73
+ # end
74
+ end