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
data/lib/ruby/args.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'ruby/aggregate'
2
+ require 'ruby/list'
3
+ require 'ruby/alternation/args'
4
+
5
+ module Ruby
6
+ class ArgsList < DelimitedList
7
+ include Alternation::ArgsList
8
+
9
+ def initialize(args = nil, ldelim = nil, rdelim = nil)
10
+ args = Array(args).map { |arg| arg.is_a?(Ruby::Arg) ? arg : Ruby::Arg.new(arg) }
11
+ super
12
+ end
13
+
14
+ def <<(arg)
15
+ arg = Ruby::Arg.new(arg) unless arg.is_a?(Ruby::Arg)
16
+ super
17
+ end
18
+ end
19
+
20
+ class Arg < DelimitedAggregate
21
+ child_accessor :arg
22
+
23
+ def initialize(arg, ldelim = nil)
24
+ self.arg = arg
25
+ super(ldelim)
26
+ end
27
+
28
+ def nodes
29
+ [ldelim, arg].compact
30
+ end
31
+
32
+ def method_missing(method, *args, &block)
33
+ arg.respond_to?(method) ? arg.send(method, *args, &block) : super
34
+ end
35
+ end
36
+ end
data/lib/ruby/array.rb ADDED
@@ -0,0 +1,27 @@
1
+ require 'ruby/list'
2
+
3
+ module Ruby
4
+ class Array < DelimitedList
5
+ def value
6
+ elements.map { |element| element.value }
7
+ end
8
+ end
9
+
10
+ class Range < Aggregate
11
+ child_accessor :left, :operator, :right
12
+
13
+ def initialize(left, operator, right)
14
+ self.left = left
15
+ self.operator = operator
16
+ self.right = right
17
+ end
18
+
19
+ def value
20
+ operator.token == '..' ? (left.value..right.value) : (left.value...right.value)
21
+ end
22
+
23
+ def nodes
24
+ [left, operator, right]
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,32 @@
1
+ require 'ruby/node'
2
+
3
+ module Ruby
4
+ class Assignment < Aggregate
5
+ child_accessor :left, :right, :operator
6
+
7
+ def initialize(left, right, operator)
8
+ self.left = left
9
+ self.right = right
10
+ self.operator = operator
11
+ end
12
+
13
+ def nodes
14
+ [left, operator, right].compact
15
+ end
16
+ end
17
+
18
+ class MultiAssignment < DelimitedList
19
+ attr_accessor :kind
20
+ child_accessor :splat
21
+
22
+ def initialize(kind, elements = [], ldelim = nil, rdelim = nil, splat = nil)
23
+ self.kind = kind
24
+ self.splat = splat
25
+ super(elements, ldelim, rdelim)
26
+ end
27
+
28
+ def nodes
29
+ [ldelim, splat, elements, rdelim].flatten.compact
30
+ end
31
+ end
32
+ end
data/lib/ruby/assoc.rb ADDED
@@ -0,0 +1,17 @@
1
+ require 'ruby/node'
2
+
3
+ module Ruby
4
+ class Assoc < Aggregate
5
+ child_accessor :key, :value, :operator
6
+
7
+ def initialize(key, value, operator)
8
+ self.key = key
9
+ self.value = value
10
+ self.operator = operator
11
+ end
12
+
13
+ def nodes
14
+ [key, operator, value].compact
15
+ end
16
+ end
17
+ end
data/lib/ruby/block.rb ADDED
@@ -0,0 +1,42 @@
1
+ require 'ruby/statements'
2
+
3
+ module Ruby
4
+ class Block < Statements
5
+ child_accessor :params
6
+
7
+ def initialize(statements, params = nil, ldelim = nil, rdelim = nil)
8
+ self.params = params
9
+ super(statements, ldelim, rdelim)
10
+ end
11
+
12
+ def nodes
13
+ [ldelim, params, elements, rdelim].flatten.compact
14
+ end
15
+ end
16
+
17
+ class NamedBlock < Block
18
+ child_accessor :identifier
19
+
20
+ def initialize(identifier, statements, params = nil, ldelim = nil, rdelim = nil)
21
+ self.identifier = identifier
22
+ super(statements, params, ldelim, rdelim)
23
+ end
24
+
25
+ def nodes
26
+ [identifier, super].flatten.compact
27
+ end
28
+ end
29
+
30
+ class ChainedBlock < NamedBlock
31
+ child_accessor :blocks
32
+
33
+ def initialize(identifier, blocks, statements, params = nil, ldelim = nil, rdelim = nil)
34
+ self.blocks = Array(blocks) || []
35
+ super(identifier, statements, params, ldelim, rdelim)
36
+ end
37
+
38
+ def nodes
39
+ [identifier, params, ldelim, elements, blocks, rdelim].flatten.compact
40
+ end
41
+ end
42
+ end
data/lib/ruby/call.rb ADDED
@@ -0,0 +1,34 @@
1
+ require 'ruby/args'
2
+
3
+ module Ruby
4
+ class Call < Aggregate
5
+ child_accessor :identifier, :separator, :target, :arguments, :block
6
+
7
+ def initialize(target, separator, identifier, arguments = nil, block = nil)
8
+ self.target = target
9
+ self.separator = separator
10
+ self.identifier = identifier
11
+ self.arguments = arguments
12
+ self.block = block
13
+ end
14
+
15
+ def token
16
+ identifier.token
17
+ end
18
+
19
+ def nodes
20
+ [target, separator, identifier, arguments, block].flatten.compact
21
+ end
22
+ end
23
+
24
+ class Alias < Call
25
+ def initialize(identifier, arguments)
26
+ self.identifier = identifier
27
+ self.arguments = arguments
28
+ end
29
+
30
+ def nodes
31
+ [identifier, arguments].flatten
32
+ end
33
+ end
34
+ end
data/lib/ruby/case.rb ADDED
@@ -0,0 +1,30 @@
1
+ require 'ruby/node'
2
+
3
+ module Ruby
4
+ class Case < NamedAggregate
5
+ child_accessor :expression, :block
6
+
7
+ def initialize(identifier, expression, block, rdelim)
8
+ self.expression = expression
9
+ self.block = block
10
+ super(identifier, nil, rdelim)
11
+ end
12
+
13
+ def nodes
14
+ [identifier, expression, block, rdelim].compact
15
+ end
16
+ end
17
+
18
+ class When < ChainedBlock
19
+ child_accessor :expression
20
+
21
+ def initialize(identifier, expression, statements, ldelim = nil, block = nil)
22
+ self.expression = expression
23
+ super(identifier, [block], statements, nil, ldelim)
24
+ end
25
+
26
+ def nodes
27
+ [identifier, expression, ldelim, elements, blocks, rdelim].flatten.compact
28
+ end
29
+ end
30
+ end
data/lib/ruby/const.rb ADDED
@@ -0,0 +1,49 @@
1
+ require 'ruby/aggregate'
2
+
3
+ module Ruby
4
+ class Const < DelimitedAggregate
5
+ child_accessor :identifier, :namespace
6
+
7
+ def initialize(token = nil, position = nil, prolog = nil, ldelim = nil)
8
+ self.identifier = Ruby::Identifier.new(token, position, prolog)
9
+ super(ldelim)
10
+ end
11
+
12
+ def position(*)
13
+ super
14
+ end
15
+
16
+ def nodes
17
+ [namespace, ldelim, identifier].compact
18
+ end
19
+ end
20
+
21
+ class Module < DelimitedAggregate
22
+ child_accessor :const, :body
23
+
24
+ def initialize(const, body, ldelim, rdelim)
25
+ self.const = const
26
+ self.body = body
27
+ super(ldelim, rdelim)
28
+ end
29
+
30
+ def nodes
31
+ [ldelim, const, body, rdelim].compact
32
+ end
33
+ end
34
+
35
+ class Class < NamedAggregate
36
+ child_accessor :operator, :super_class, :body
37
+
38
+ def initialize(const, operator, super_class, body, ldelim, rdelim)
39
+ self.operator = operator
40
+ self.super_class = super_class
41
+ self.body = body
42
+ super(const, ldelim, rdelim)
43
+ end
44
+
45
+ def nodes
46
+ [ldelim, identifier, operator, super_class, body, rdelim].compact
47
+ end
48
+ end
49
+ end
data/lib/ruby/for.rb ADDED
@@ -0,0 +1,18 @@
1
+ require 'ruby/statements'
2
+
3
+ module Ruby
4
+ class For < NamedBlock
5
+ child_accessor :variable, :operator, :range
6
+
7
+ def initialize(identifier, variable, operator, range, statements, ldelim = nil, rdelim = nil)
8
+ self.variable = variable
9
+ self.operator = operator
10
+ self.range = range
11
+ super(identifier, statements, nil, ldelim, rdelim)
12
+ end
13
+
14
+ def nodes
15
+ [identifier, variable, operator, range, ldelim, elements, rdelim].flatten.compact
16
+ end
17
+ end
18
+ end
data/lib/ruby/hash.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'ruby/assoc'
2
+ require 'ruby/alternation/hash'
3
+
4
+ module Ruby
5
+ class Hash < DelimitedList
6
+ include Alternation::Hash
7
+
8
+ def value
9
+ code = to_ruby(false)
10
+ code = "{#{code}}" unless ldelim
11
+ eval(code) rescue {}
12
+ end
13
+ end
14
+ end
data/lib/ruby/if.rb ADDED
@@ -0,0 +1,35 @@
1
+ require 'ruby/node'
2
+
3
+ module Ruby
4
+ class If < ChainedBlock
5
+ child_accessor :expression, :else_block
6
+
7
+ def initialize(identifier, expression, statements = nil, ldelim = nil, rdelim = nil, else_block = nil)
8
+ self.expression = expression
9
+ super(identifier, [else_block], statements, nil, ldelim, rdelim)
10
+ end
11
+
12
+ def nodes
13
+ [identifier, expression, ldelim, elements, blocks, rdelim].flatten.compact
14
+ end
15
+ end
16
+
17
+ class Unless < If; end
18
+ class Else < NamedBlock; end
19
+
20
+ class IfMod < NamedBlock
21
+ child_accessor :expression
22
+
23
+ def initialize(identifier, expression, statements)
24
+ self.expression = expression
25
+ super(identifier, statements)
26
+ end
27
+
28
+ def nodes
29
+ [elements, identifier, expression].flatten.compact
30
+ end
31
+ end
32
+
33
+ class UnlessMod < IfMod; end
34
+ class RescueMod < IfMod; end
35
+ end
data/lib/ruby/list.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'ruby/node'
2
+ require 'ruby/alternation/list'
3
+
4
+ module Ruby
5
+ class List < Aggregate
6
+ include Conversions::List
7
+ include Alternation::List
8
+
9
+ child_accessor :elements
10
+
11
+ def initialize(elements = nil)
12
+ self.elements = Array(elements)
13
+ end
14
+
15
+ def nodes
16
+ elements
17
+ end
18
+
19
+ def method_missing(method, *args, &block)
20
+ elements.respond_to?(method) ? elements.send(method, *args, &block) : super
21
+ end
22
+ end
23
+
24
+ class DelimitedList < List
25
+ child_accessor :ldelim, :rdelim
26
+
27
+ def initialize(elements = nil, ldelim = nil, rdelim = nil)
28
+ self.ldelim = ldelim
29
+ self.rdelim = rdelim
30
+ super(elements)
31
+ end
32
+
33
+ def nodes
34
+ ([ldelim] + super + [rdelim]).flatten.compact
35
+ end
36
+ end
37
+
38
+ class Prolog < List
39
+ end
40
+ end
@@ -0,0 +1,45 @@
1
+ require 'ruby/token'
2
+
3
+ module Ruby
4
+ class Nil < Token
5
+ def value
6
+ nil
7
+ end
8
+ end
9
+
10
+ class True < Token
11
+ def value
12
+ true
13
+ end
14
+ end
15
+
16
+ class False < Token
17
+ def value
18
+ false
19
+ end
20
+ end
21
+
22
+ class Integer < Token
23
+ def value
24
+ token.to_i
25
+ end
26
+ end
27
+
28
+ class Float < Token
29
+ def value
30
+ token.to_f
31
+ end
32
+ end
33
+
34
+ class Char < Token
35
+ def value
36
+ token[1]
37
+ end
38
+ end
39
+
40
+ class Label < Token
41
+ def value
42
+ token.gsub(':').to_sym
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,19 @@
1
+ require 'ruby/node'
2
+
3
+ module Ruby
4
+ class Method < NamedAggregate
5
+ child_accessor :target, :separator, :params, :block # TODO rename block to body
6
+
7
+ def initialize(target, separator, identifier, params, block, ldelim, rdelim)
8
+ self.target = target
9
+ self.separator = separator
10
+ self.params = params
11
+ self.block = block
12
+ super(identifier, ldelim, rdelim)
13
+ end
14
+
15
+ def nodes
16
+ [ldelim, target, separator, identifier, params, block, rdelim].compact
17
+ end
18
+ end
19
+ end