ruby2ruby 1.2.1 → 1.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.autotest +5 -3
- data/History.txt +15 -0
- data/bin/r2r_show +8 -4
- data/lib/ruby2ruby.rb +26 -27
- data/test/test_ruby2ruby.rb +64 -20
- metadata +3 -3
data/.autotest
CHANGED
@@ -16,6 +16,8 @@ Autotest.add_hook :initialize do |at|
|
|
16
16
|
end
|
17
17
|
end
|
18
18
|
|
19
|
-
|
20
|
-
|
21
|
-
Autotest::RCov.
|
19
|
+
if ENV['RCOV'] then
|
20
|
+
require 'autotest/rcov'
|
21
|
+
Autotest::RCov.command = 'rcov_info'
|
22
|
+
Autotest::RCov.pattern = 'test/test_ruby2ruby.rb'
|
23
|
+
end
|
data/History.txt
CHANGED
@@ -1,3 +1,18 @@
|
|
1
|
+
=== 1.2.2 / 2009-01-20
|
2
|
+
|
3
|
+
* 3 minor enhancements:
|
4
|
+
|
5
|
+
* Added -s to display sexp before printing r2r
|
6
|
+
* Added a bunch of backslash and masgn tests.
|
7
|
+
* Refactored tests.
|
8
|
+
|
9
|
+
* 4 bug fixes:
|
10
|
+
|
11
|
+
* Fixed iters to deal with empty bodies.
|
12
|
+
* Fixed process_call for a number of cases incl [], []=, and args processing.
|
13
|
+
* Fixed process_hash to always generate braces if in arglist.
|
14
|
+
* Switched process_alias to producing alias again, needed for globals.
|
15
|
+
|
1
16
|
=== 1.2.1 / 2008-11-04
|
2
17
|
|
3
18
|
* 1 bug fix:
|
data/bin/r2r_show
CHANGED
@@ -1,15 +1,17 @@
|
|
1
|
-
#!/usr/
|
1
|
+
#!/usr/bin/ruby -ws
|
2
2
|
|
3
|
-
|
3
|
+
require 'rubygems'
|
4
4
|
require 'ruby2ruby'
|
5
5
|
require 'parse_tree'
|
6
6
|
|
7
7
|
$h ||= false
|
8
|
+
$s ||= false
|
8
9
|
|
9
10
|
if $h then
|
10
11
|
puts "usage: #{File.basename $0} [options] [file...]"
|
11
12
|
puts "options:"
|
12
|
-
puts "-h
|
13
|
+
puts "-h : display usage"
|
14
|
+
puts "-s : print the sexp before displaying the translated ruby"
|
13
15
|
|
14
16
|
exit 1
|
15
17
|
end
|
@@ -22,7 +24,9 @@ ruby2ruby = Ruby2Ruby.new
|
|
22
24
|
ARGV.each do |file|
|
23
25
|
ruby = file == "-" ? $stdin.read : File.read(file)
|
24
26
|
|
25
|
-
sexp = parse_tree.
|
27
|
+
sexp = parse_tree.process(ruby, nil, file)
|
28
|
+
|
29
|
+
p sexp if $s
|
26
30
|
|
27
31
|
puts ruby2ruby.process(sexp)
|
28
32
|
end
|
data/lib/ruby2ruby.rb
CHANGED
@@ -5,7 +5,7 @@ require 'sexp_processor'
|
|
5
5
|
require 'unified_ruby'
|
6
6
|
|
7
7
|
class Ruby2Ruby < SexpProcessor
|
8
|
-
VERSION = '1.2.
|
8
|
+
VERSION = '1.2.2'
|
9
9
|
LINE_LENGTH = 78
|
10
10
|
|
11
11
|
##
|
@@ -54,7 +54,7 @@ class Ruby2Ruby < SexpProcessor
|
|
54
54
|
# Processors
|
55
55
|
|
56
56
|
def process_alias(exp)
|
57
|
-
"
|
57
|
+
"alias #{process(exp.shift)} #{process(exp.shift)}"
|
58
58
|
end
|
59
59
|
|
60
60
|
def process_and(exp)
|
@@ -186,29 +186,29 @@ class Ruby2Ruby < SexpProcessor
|
|
186
186
|
Ruby2Ruby::ASSIGN_NODES.include? receiver_node_type
|
187
187
|
|
188
188
|
name = exp.shift
|
189
|
-
|
190
|
-
if args_exp && args_exp.first == :array # FIX
|
191
|
-
args = "#{process(args_exp)[1..-2]}"
|
192
|
-
else
|
193
|
-
args = process args_exp
|
194
|
-
args = nil if args.empty?
|
195
|
-
end
|
189
|
+
args = exp.shift rescue nil
|
196
190
|
|
197
191
|
case name
|
198
192
|
when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
|
199
|
-
"(#{receiver} #{name} #{args})"
|
193
|
+
"(#{receiver} #{name} #{process args})"
|
200
194
|
when :[] then
|
201
|
-
"
|
195
|
+
receiver = "self" if receiver.nil?
|
196
|
+
"#{receiver}[#{process args}]"
|
197
|
+
when :[]= then
|
198
|
+
receiver = "self" if receiver.nil?
|
199
|
+
lhs = args.pop
|
200
|
+
"#{receiver}[#{process args}] = #{process lhs}"
|
202
201
|
when :"-@" then
|
203
202
|
"-#{receiver}"
|
204
203
|
when :"+@" then
|
205
204
|
"+#{receiver}"
|
206
205
|
else
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
206
|
+
args = process args
|
207
|
+
args = nil if args.empty?
|
208
|
+
args = "(#{args})" if args
|
209
|
+
receiver = "#{receiver}." if receiver
|
210
|
+
|
211
|
+
"#{receiver}#{name}#{args}"
|
212
212
|
end
|
213
213
|
end
|
214
214
|
|
@@ -408,7 +408,11 @@ class Ruby2Ruby < SexpProcessor
|
|
408
408
|
|
409
409
|
case self.context[1]
|
410
410
|
when :arglist, :argscat then
|
411
|
-
|
411
|
+
unless result.empty? then
|
412
|
+
return "#{result.join(', ')}" # HACK - this will break w/ 2 hashes as args
|
413
|
+
else
|
414
|
+
return "{}"
|
415
|
+
end
|
412
416
|
else
|
413
417
|
return "{ #{result.join(', ')} }"
|
414
418
|
end
|
@@ -487,8 +491,10 @@ class Ruby2Ruby < SexpProcessor
|
|
487
491
|
result << "#{iter} #{b}"
|
488
492
|
result << " |#{args}|" if args
|
489
493
|
result << "\n"
|
490
|
-
|
491
|
-
|
494
|
+
if body then
|
495
|
+
result << indent(body.strip)
|
496
|
+
result << "\n"
|
497
|
+
end
|
492
498
|
result << e
|
493
499
|
result.join
|
494
500
|
end
|
@@ -525,12 +531,6 @@ class Ruby2Ruby < SexpProcessor
|
|
525
531
|
lhs = exp.shift
|
526
532
|
rhs = exp.empty? ? nil : exp.shift
|
527
533
|
|
528
|
-
unless exp.empty? then
|
529
|
-
rhs[1] = splat(rhs[1]) unless rhs == s(:splat)
|
530
|
-
lhs << rhs
|
531
|
-
rhs = exp.shift
|
532
|
-
end
|
533
|
-
|
534
534
|
case lhs.first
|
535
535
|
when :array then
|
536
536
|
lhs.shift
|
@@ -558,12 +558,11 @@ class Ruby2Ruby < SexpProcessor
|
|
558
558
|
unless rhs.nil? then
|
559
559
|
t = rhs.first
|
560
560
|
rhs = process rhs
|
561
|
-
rhs = rhs[1..-2] if t
|
561
|
+
rhs = rhs[1..-2] if t == :array # FIX: bad? I dunno
|
562
562
|
return "#{lhs.join(", ")} = #{rhs}"
|
563
563
|
else
|
564
564
|
return lhs.join(", ")
|
565
565
|
end
|
566
|
-
|
567
566
|
end
|
568
567
|
|
569
568
|
def process_match(exp)
|
data/test/test_ruby2ruby.rb
CHANGED
@@ -54,41 +54,85 @@ class TestRuby2Ruby < R2RTestCase
|
|
54
54
|
def test_dregx_slash
|
55
55
|
inn = util_thingy(:dregx)
|
56
56
|
out = "/blah\\\"blah#\{(1 + 1)}blah\\\"blah\\/blah/"
|
57
|
-
|
58
|
-
assert_equal out, @processor.process(inn)
|
59
|
-
|
60
|
-
r = eval(out)
|
61
|
-
assert_equal(/blah\"blah2blah\"blah\/blah/, r)
|
57
|
+
util_compare inn, out, /blah\"blah2blah\"blah\/blah/
|
62
58
|
end
|
63
59
|
|
64
60
|
def test_dstr_quote
|
65
61
|
inn = util_thingy(:dstr)
|
66
62
|
out = "\"blah\\\"blah#\{(1 + 1)}blah\\\"blah/blah\""
|
67
|
-
|
68
|
-
assert_equal out, @processor.process(inn)
|
69
|
-
|
70
|
-
r = eval(out)
|
71
|
-
assert_equal "blah\"blah2blah\"blah/blah", r
|
63
|
+
util_compare inn, out, "blah\"blah2blah\"blah/blah"
|
72
64
|
end
|
73
65
|
|
74
66
|
def test_dsym_quote
|
75
67
|
inn = util_thingy(:dsym)
|
76
68
|
out = ":\"blah\\\"blah#\{(1 + 1)}blah\\\"blah/blah\""
|
69
|
+
util_compare inn, out, :"blah\"blah2blah\"blah/blah"
|
70
|
+
end
|
77
71
|
|
78
|
-
|
72
|
+
def test_lit_regexp_slash
|
73
|
+
util_compare s(:lit, /blah\/blah/), '/blah\/blah/', /blah\/blah/
|
74
|
+
end
|
79
75
|
|
80
|
-
|
81
|
-
|
76
|
+
def test_call_self_index
|
77
|
+
util_compare s(:call, nil, :[], s(:arglist, s(:lit, 42))), "self[42]"
|
78
|
+
end
|
79
|
+
|
80
|
+
def test_call_self_index_equals
|
81
|
+
util_compare(s(:call, nil, :[]=, s(:arglist, s(:lit, 42), s(:lit, 24))),
|
82
|
+
"self[42] = 24")
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_masgn_wtf
|
86
|
+
inn = s(:block,
|
87
|
+
s(:masgn,
|
88
|
+
s(:array, s(:lasgn, :k), s(:lasgn, :v)),
|
89
|
+
s(:array,
|
90
|
+
s(:splat,
|
91
|
+
s(:call,
|
92
|
+
s(:call, nil, :line, s(:arglist)),
|
93
|
+
:split,
|
94
|
+
s(:arglist, s(:lit, /\=/), s(:lit, 2)))))),
|
95
|
+
s(:attrasgn,
|
96
|
+
s(:self),
|
97
|
+
:[]=,
|
98
|
+
s(:arglist, s(:lvar, :k),
|
99
|
+
s(:call, s(:lvar, :v), :strip, s(:arglist)))))
|
100
|
+
|
101
|
+
out = "k, v = *line.split(/\\=/, 2)\nself[k] = v.strip\n"
|
102
|
+
|
103
|
+
util_compare inn, out
|
82
104
|
end
|
83
105
|
|
84
|
-
def test_lit_regexp_slash
|
85
|
-
inn = s(:lit, /blah\/blah/)
|
86
|
-
out = '/blah\/blah/'
|
87
106
|
|
88
|
-
|
107
|
+
def test_masgn_splat_wtf
|
108
|
+
inn = s(:masgn,
|
109
|
+
s(:array, s(:lasgn, :k), s(:lasgn, :v)),
|
110
|
+
s(:array,
|
111
|
+
s(:splat,
|
112
|
+
s(:call,
|
113
|
+
s(:call, nil, :line, s(:arglist)),
|
114
|
+
:split,
|
115
|
+
s(:arglist, s(:lit, /\=/), s(:lit, 2))))))
|
116
|
+
out = 'k, v = *line.split(/\\=/, 2)'
|
117
|
+
util_compare inn, out
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_splat_call
|
121
|
+
inn = s(:call, nil, :x,
|
122
|
+
s(:arglist,
|
123
|
+
s(:splat,
|
124
|
+
s(:call,
|
125
|
+
s(:call, nil, :line, s(:arglist)),
|
126
|
+
:split,
|
127
|
+
s(:arglist, s(:lit, /=/), s(:lit, 2))))))
|
128
|
+
|
129
|
+
out = 'x(*line.split(/=/, 2))'
|
130
|
+
util_compare inn, out
|
131
|
+
end
|
89
132
|
|
90
|
-
|
91
|
-
assert_equal(
|
133
|
+
def util_compare sexp, expected_ruby, expected_eval = nil
|
134
|
+
assert_equal expected_ruby, @processor.process(sexp)
|
135
|
+
assert_equal expected_eval, eval(expected_ruby) if expected_eval
|
92
136
|
end
|
93
137
|
|
94
138
|
def util_setup_inline
|
@@ -100,7 +144,7 @@ class TestRuby2Ruby < R2RTestCase
|
|
100
144
|
def util_thingy(type)
|
101
145
|
s(type,
|
102
146
|
'blah"blah',
|
103
|
-
s(:call, s(:lit, 1), :+, s(:
|
147
|
+
s(:call, s(:lit, 1), :+, s(:arglist, s(:lit, 1))),
|
104
148
|
s(:str, 'blah"blah/blah'))
|
105
149
|
end
|
106
150
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby2ruby
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.2.
|
4
|
+
version: 1.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ryan Davis
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date:
|
12
|
+
date: 2009-01-20 00:00:00 -08:00
|
13
13
|
default_executable:
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
@@ -30,7 +30,7 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 1.8.
|
33
|
+
version: 1.8.2
|
34
34
|
version:
|
35
35
|
description: ruby2ruby provides a means of generating pure ruby code easily from ParseTree's Sexps. This makes making dynamic language processors much easier in ruby than ever before.
|
36
36
|
email:
|