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,55 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Assignment
4
+ def on_assign(left, right)
5
+ Ruby::Assignment.new(left, right, pop_token(:'@='))
6
+ end
7
+
8
+ def on_massign(left, right)
9
+ Ruby::Assignment.new(left, right, pop_token(:'@='))
10
+ end
11
+
12
+ def on_opassign(left, operator, right)
13
+ Ruby::Assignment.new(left, right, pop_assignment_operator)
14
+ end
15
+
16
+ def on_mlhs_new
17
+ Ruby::MultiAssignment.new(:left, nil, pop_token(:@lparen))
18
+ end
19
+
20
+ def on_mlhs_add(assignment, ref)
21
+ assignment << ref
22
+ assignment
23
+ end
24
+
25
+ def on_mlhs_paren(arg)
26
+ arg.rdelim ||= pop_token(:@rparen) if arg.respond_to?(:rdelim)
27
+ arg.ldelim ||= pop_token(:@lparen) if arg.respond_to?(:ldelim)
28
+ arg
29
+ end
30
+
31
+ def on_mrhs_new
32
+ Ruby::MultiAssignment.new(:right, nil)
33
+ end
34
+
35
+ def on_mrhs_new_from_args(args)
36
+ Ruby::MultiAssignment.new(:right, args.elements)
37
+ end
38
+
39
+ def on_mrhs_add(assignment, ref)
40
+ assignment << ref
41
+ assignment
42
+ end
43
+
44
+ def on_mlhs_add_star(assignment, ref)
45
+ assignment << Ruby::Arg.new(ref, pop_token(:'@*'))
46
+ assignment
47
+ end
48
+
49
+ def on_mrhs_add_star(assignment, ref)
50
+ assignment << Ruby::Arg.new(ref, pop_token(:'@*'))
51
+ assignment
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,80 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Block
4
+ def on_method_add_block(call, block)
5
+ call.block = block
6
+ call
7
+ end
8
+
9
+ def on_do_block(params, statements)
10
+ rdelim = pop_token(:@end)
11
+ ldelim = pop_token(:@do)
12
+ statements.to_block(params, ldelim, rdelim)
13
+ end
14
+
15
+ def on_brace_block(params, statements)
16
+ rdelim = pop_token(:@rbrace)
17
+ ldelim = pop_token(:@lbrace)
18
+ statements.to_block(params, ldelim, rdelim)
19
+ end
20
+
21
+ def on_begin(body)
22
+ body = body.to_chained_block unless body.is_a?(Ruby::ChainedBlock)
23
+ body.rdelim = pop_token(:@end)
24
+ body.identifier = pop_token(:@begin)
25
+ body
26
+ end
27
+
28
+ def on_rescue(types, var, statements, block)
29
+ identifier = pop_token(:@rescue)
30
+ ldelim = pop_token(:@then)
31
+ operator = pop_token(:'@=>', :left => identifier)
32
+ params = Ruby::RescueParams.new(types, var, operator)
33
+ statements.to_chained_block(identifier, block, params, ldelim)
34
+ end
35
+
36
+ def on_ensure(statements)
37
+ identifier = pop_token(:@ensure)
38
+ Ruby::NamedBlock.new(identifier, statements)
39
+ end
40
+
41
+ def on_rescue_mod(expression, statements)
42
+ args = Ruby::ArgsList.new(args) unless args.is_a?(Ruby::List)
43
+ statements, expression = *[statements, expression].sort
44
+ identifier = pop_token(:@rescue)
45
+ Ruby::RescueMod.new(identifier, expression, statements)
46
+ end
47
+
48
+ def on_block_var(params, something)
49
+ params.rdelim = pop_token(:'@|')
50
+ params.ldelim = pop_token(:'@|')
51
+ params.ldelim, params.rdelim = empty_block_params_delimiters(params) if stack.last.type == :'@||'
52
+ params
53
+ end
54
+
55
+ def on_params(params, optionals, rest, something, block)
56
+ optionals = to_assignments(optionals) if optionals
57
+ params = (Array(params) + Array(optionals) << rest << block).flatten.compact
58
+ Ruby::Params.new(params)
59
+ end
60
+
61
+ def on_rest_param(param)
62
+ Ruby::Param.new(param, pop_token(:'@*'))
63
+ end
64
+
65
+ protected
66
+
67
+ def to_assignments(array)
68
+ array.reverse.map { |left, right| Ruby::Assignment.new(left, right, pop_token(:'@=')) }.reverse
69
+ end
70
+
71
+ # gosh, yes. ripper regards empty block param delims (as in in do || ; end) as an operator
72
+ def empty_block_params_delimiters(params)
73
+ op = pop_token(:'@||')
74
+ ldelim = Ruby::Token.new('|', op.position, op.prolog)
75
+ rdelim = Ruby::Token.new('|', op.position).tap { |o| o.position.col += 1 }
76
+ [ldelim, rdelim]
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,123 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Call
4
+ def on_command(identifier, args)
5
+ Ruby::Call.new(nil, nil, identifier, args)
6
+ end
7
+
8
+ def on_command_call(target, separator, identifier, args)
9
+ # happens for identifiers that are operators, e.g. a.<<(foo)
10
+ identifier = pop_identifier(identifier.type) if identifier.try(:known?)
11
+ separator = pop_token(:@period, :"@::")
12
+ Ruby::Call.new(target, separator, identifier, args)
13
+ end
14
+
15
+ def on_call(target, separator, identifier)
16
+ # happens for identifiers that are also keywords, e.g. :if
17
+ identifier = pop_identifier(identifier.type) if identifier.try(:known?)
18
+ separator = pop_token(:@period, :"@::", :left => target, :right => identifier)
19
+ Ruby::Call.new(target, separator, identifier)
20
+ end
21
+
22
+ def on_fcall(identifier)
23
+ Ruby::Call.new(nil, nil, identifier)
24
+ end
25
+
26
+ # assignment methods, e.g. a.b = :c
27
+ def on_field(target, separator, identifier)
28
+ separator = pop_token(:@period, :"@::")
29
+ Ruby::Call.new(target, separator, identifier)
30
+ end
31
+
32
+ def on_zsuper(*args)
33
+ identifier = pop_identifier(:@super)
34
+ Ruby::Call.new(nil, nil, identifier)
35
+ end
36
+
37
+ def on_super(args)
38
+ identifier = pop_identifier(:@super)
39
+ Ruby::Call.new(nil, nil, identifier, args)
40
+ end
41
+
42
+ def on_yield(args)
43
+ identifier = pop_identifier(:@yield)
44
+ Ruby::Call.new(nil, nil, identifier, args)
45
+ end
46
+
47
+ def on_yield0
48
+ identifier = pop_identifier(:@yield)
49
+ Ruby::Call.new(nil, nil, identifier)
50
+ end
51
+
52
+ def on_alias(*args)
53
+ args.map! { |arg| arg.try(:to_identifier) || arg }
54
+ identifier = pop_identifier(:@alias)
55
+ Ruby::Alias.new(identifier, args)
56
+ end
57
+
58
+ def on_var_alias(*args)
59
+ args.map! { |arg| arg.try(:to_identifier) || arg }
60
+ identifier = pop_identifier(:@alias)
61
+ Ruby::Alias.new(identifier, args)
62
+ end
63
+
64
+ def on_undef(args)
65
+ args = Ruby::ArgsList.new(args, pop_tokens(:@comma))
66
+ identifier = pop_identifier(:@undef)
67
+ Ruby::Call.new(nil, nil, identifier, args)
68
+ end
69
+
70
+ def on_return0
71
+ identifier = pop_identifier(:@return)
72
+ Ruby::Call.new(nil, nil, identifier, nil)
73
+ end
74
+
75
+ def on_return(args)
76
+ identifier = pop_identifier(:@return)
77
+ Ruby::Call.new(nil, nil, identifier, args)
78
+ end
79
+
80
+ def on_next(args)
81
+ identifier = pop_identifier(:@next)
82
+ Ruby::Call.new(nil, nil, identifier, args)
83
+ end
84
+
85
+ def on_break(args)
86
+ identifier = pop_identifier(:@break)
87
+ Ruby::Call.new(nil, nil, identifier, args)
88
+ end
89
+
90
+ def on_redo
91
+ identifier = pop_identifier(:@redo)
92
+ Ruby::Call.new(nil, nil, identifier)
93
+ end
94
+
95
+ def on_retry
96
+ identifier = pop_identifier(:@retry)
97
+ Ruby::Call.new(nil, nil, identifier)
98
+ end
99
+
100
+ def on_defined(ref)
101
+ identifer = pop_identifier(:@defined, :pass => true)
102
+ ldelim = pop_token(:@lparen, :left => identifer)
103
+ rdelim = pop_token(:@rparen) if ldelim
104
+ args = Ruby::ArgsList.new(ref, ldelim, rdelim)
105
+ Ruby::Call.new(nil, nil, identifer, args)
106
+ end
107
+
108
+ def on_BEGIN(statements)
109
+ statements.ldelim = pop_token(:@lbrace)
110
+ statements.rdelim = pop_token(:@rbrace)
111
+ identifier = pop_identifier(:@BEGIN)
112
+ Ruby::Call.new(nil, nil, identifier, nil, statements)
113
+ end
114
+
115
+ def on_END(statements)
116
+ statements.ldelim = pop_token(:@lbrace)
117
+ statements.rdelim = pop_token(:@rbrace)
118
+ identifier = pop_identifier(:@END)
119
+ Ruby::Call.new(nil, nil, identifier, nil, statements)
120
+ end
121
+ end
122
+ end
123
+ end
@@ -0,0 +1,17 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Case
4
+ def on_case(args, when_block)
5
+ rdelim = pop_token(:@end)
6
+ identifier = pop_token(:@case)
7
+ Ruby::Case.new(identifier, args, when_block, rdelim)
8
+ end
9
+
10
+ def on_when(expression, statements, next_block)
11
+ ldelim = pop_token(:@then)
12
+ identifier = pop_token(:@when)
13
+ Ruby::When.new(identifier, expression, statements, ldelim, next_block)
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,47 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Const
4
+ def on_class(const, super_class, body)
5
+ rdelim = pop_token(:@end)
6
+ operator = super_class ? pop_token(:'@<') : nil
7
+ ldelim = pop_token(:@class)
8
+ Ruby::Class.new(const, operator, super_class, body, ldelim, rdelim)
9
+ end
10
+
11
+ def on_sclass(super_class, body)
12
+ rdelim = pop_token(:@end)
13
+ operator = pop_token(:'@<<')
14
+ ldelim = pop_token(:@class)
15
+ Ruby::Class.new(nil, operator, super_class, body, ldelim, rdelim)
16
+ end
17
+
18
+ def on_module(const, body)
19
+ rdelim = pop_token(:@end)
20
+ ldelim = pop_token(:@module)
21
+ Ruby::Module.new(const, body, ldelim, rdelim)
22
+ end
23
+
24
+ def on_const_path_ref(namespace, const)
25
+ const.prolog.unshift(pop_token(:'@::'))
26
+ const.namespace = namespace
27
+ const
28
+ end
29
+
30
+ def on_const_path_field(namespace, const)
31
+ const.prolog.unshift(pop_token(:'@::'))
32
+ const.namespace = namespace
33
+ const
34
+ end
35
+
36
+ def on_top_const_ref(const)
37
+ const.prolog.unshift(pop_token(:'@::'))
38
+ const
39
+ end
40
+
41
+ def on_top_const_field(const)
42
+ const.prolog.unshift(pop_token(:'@::'))
43
+ const
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,13 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module For
4
+ def on_for(variable, range, statements)
5
+ rdelim = pop_token(:@end)
6
+ identifier = pop_token(:@for, :pass => true)
7
+ ldelim = pop_token(:@do, :left => identifier, :right => rdelim)
8
+ operator = pop_token(:@in, :pass => true)
9
+ Ruby::For.new(identifier, variable, operator, range, statements, ldelim, rdelim)
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,24 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Hash
4
+ def on_hash(assocs)
5
+ rdelim = pop_token(:@rbrace)
6
+ ldelim = pop_token(:@lbrace)
7
+ Ruby::Hash.new(assocs, ldelim, rdelim)
8
+ end
9
+
10
+ def on_assoclist_from_args(args)
11
+ args
12
+ end
13
+
14
+ def on_bare_assoc_hash(assocs)
15
+ Ruby::Hash.new(assocs)
16
+ end
17
+
18
+ def on_assoc_new(key, value)
19
+ Ruby::Assoc.new(key, value, pop_token(:'@=>'))
20
+ end
21
+ end
22
+ end
23
+ end
24
+
@@ -0,0 +1,41 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module Identifier
4
+ def on_const(token)
5
+ push(super)
6
+ token = pop_token(:@const)
7
+ Ruby::Const.new(token.token, token.position, token.prolog)
8
+ end
9
+
10
+ def on_ident(token)
11
+ push(super)
12
+ token = pop_token(:@ident)
13
+ Ruby::Identifier.new(token.token, token.position, token.prolog)
14
+ end
15
+
16
+ def on_cvar(token)
17
+ push(super)
18
+ token = pop_token(:@cvar)
19
+ Ruby::Variable.new(token.token, token.position, token.prolog)
20
+ end
21
+
22
+ def on_ivar(token)
23
+ push(super)
24
+ token = pop_token(:@ivar)
25
+ Ruby::Variable.new(token.token, token.position, token.prolog)
26
+ end
27
+
28
+ def on_gvar(token)
29
+ push(super)
30
+ token = pop_token(:@gvar)
31
+ Ruby::Variable.new(token.token, token.position, token.prolog)
32
+ end
33
+
34
+ def on_backref(arg)
35
+ push(super)
36
+ token = pop_token(:@backref)
37
+ Ruby::Variable.new(token.token, token.position, token.prolog)
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,37 @@
1
+ class Ripper
2
+ class RubyBuilder < Ripper::SexpBuilder
3
+ module If
4
+ def build_if(klass, type, expression, statements, else_block)
5
+ rdelim = pop_token(:@end)
6
+ ldelim = pop_token(:@then)
7
+ identifier = pop_token(type)
8
+ klass.new(identifier, expression, statements, ldelim, rdelim, else_block)
9
+ end
10
+
11
+ def on_if(expression, statements, else_block)
12
+ build_if(Ruby::If, :@if, expression, statements, else_block)
13
+ end
14
+
15
+ def on_unless(expression, statements, else_block)
16
+ build_if(Ruby::Unless, :@unless, expression, statements, else_block)
17
+ end
18
+
19
+ def on_elsif(expression, statements, else_block)
20
+ build_if(Ruby::If, :@elsif, expression, statements, else_block)
21
+ end
22
+
23
+ def on_else(statements)
24
+ keyword = pop_token(:@else)
25
+ block = Ruby::Else.new(keyword, statements)
26
+ end
27
+
28
+ def on_if_mod(expression, statement)
29
+ Ruby::IfMod.new(pop_token(:@if), expression, statement)
30
+ end
31
+
32
+ def on_unless_mod(expression, statement)
33
+ Ruby::UnlessMod.new(pop_token(:@unless), expression, statement)
34
+ end
35
+ end
36
+ end
37
+ end