ruby2ruby 2.0.4 → 2.0.5

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.tar.gz.sig CHANGED
Binary file
data/History.txt CHANGED
@@ -1,3 +1,10 @@
1
+ === 2.0.5 / 2013-04-25
2
+
3
+ * 2 bug fixes:
4
+
5
+ * Fixed attrasgn w/ multiple keys: a[x, y] = z. (derula)
6
+ * Fixed error w/ attr_* detection when more than 1 ivar/iasgn exist in body.
7
+
1
8
  === 2.0.4 / 2013-03-28
2
9
 
3
10
  * 1 bug fix:
data/README.txt CHANGED
@@ -13,7 +13,7 @@ processors in ruby easier than ever!
13
13
 
14
14
  * Clean, simple SexpProcessor generates ruby code from RubyParser compatible sexps.
15
15
 
16
- == SYNOPSYS:
16
+ == SYNOPSIS:
17
17
 
18
18
  require 'rubygems'
19
19
  require 'ruby2ruby'
data/lib/ruby2ruby.rb CHANGED
@@ -3,6 +3,7 @@
3
3
  require 'rubygems'
4
4
  require 'sexp_processor'
5
5
 
6
+ # :stopdoc:
6
7
  # REFACTOR: stolen from ruby_parser
7
8
  class Regexp
8
9
  unless defined? ENC_NONE then
@@ -22,11 +23,18 @@ class Regexp
22
23
  }
23
24
  end
24
25
  end
26
+ # :startdoc:
27
+
28
+ ##
29
+ # Generate ruby code from a sexp.
25
30
 
26
31
  class Ruby2Ruby < SexpProcessor
27
- VERSION = "2.0.4"
32
+ VERSION = "2.0.5" # :nodoc:
33
+
34
+ # cutoff for one-liners
28
35
  LINE_LENGTH = 78
29
36
 
37
+ # binary operation messages
30
38
  BINARY = [:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**]
31
39
 
32
40
  ##
@@ -49,7 +57,7 @@ class Ruby2Ruby < SexpProcessor
49
57
  :if, # HACK
50
58
  ]
51
59
 
52
- def initialize
60
+ def initialize # :nodoc:
53
61
  super
54
62
  @indent = " "
55
63
  self.auto_shift_type = true
@@ -64,15 +72,15 @@ class Ruby2Ruby < SexpProcessor
64
72
  ############################################################
65
73
  # Processors
66
74
 
67
- def process_alias(exp)
75
+ def process_alias(exp) # :nodoc:
68
76
  parenthesize "alias #{process(exp.shift)} #{process(exp.shift)}"
69
77
  end
70
78
 
71
- def process_and(exp)
79
+ def process_and(exp) # :nodoc:
72
80
  parenthesize "#{process exp.shift} and #{process exp.shift}"
73
81
  end
74
82
 
75
- def process_arglist(exp) # custom made node
83
+ def process_arglist(exp) # custom made node # :nodoc:
76
84
  code = []
77
85
  until exp.empty? do
78
86
  code << process(exp.shift)
@@ -80,7 +88,7 @@ class Ruby2Ruby < SexpProcessor
80
88
  code.join ', '
81
89
  end
82
90
 
83
- def process_args(exp)
91
+ def process_args(exp) # :nodoc:
84
92
  args = []
85
93
 
86
94
  until exp.empty? do
@@ -103,33 +111,38 @@ class Ruby2Ruby < SexpProcessor
103
111
  "(#{args.join ', '})"
104
112
  end
105
113
 
106
- def process_array(exp)
114
+ def process_array(exp) # :nodoc:
107
115
  "[#{process_arglist(exp)}]"
108
116
  end
109
117
 
110
- def process_attrasgn(exp)
118
+ def process_attrasgn(exp) # :nodoc:
111
119
  receiver = process exp.shift
112
120
  name = exp.shift
113
- args = exp.empty? ? nil : exp.shift
121
+ rhs = exp.pop
122
+ args = s(:array, *exp)
123
+ exp.clear
114
124
 
115
125
  case name
116
126
  when :[]= then
117
- rhs = exp.empty? ? nil : exp.shift
118
- "#{receiver}[#{process args}] = #{process rhs}"
127
+ args = process args
128
+ "#{receiver}#{args} = #{process rhs}"
119
129
  else
130
+ raise "dunno what to do: #{args.inspect}" unless args.size == 1 # s(:array)
120
131
  name = name.to_s.sub(/=$/, '')
121
- if args && args != s(:arglist) then
122
- "#{receiver}.#{name} = #{process(args)}"
132
+ if rhs && rhs != s(:arglist) then
133
+ "#{receiver}.#{name} = #{process(rhs)}"
134
+ else
135
+ raise "dunno what to do: #{rhs.inspect}"
123
136
  end
124
137
  end
125
138
  end
126
139
 
127
- def process_back_ref(exp)
140
+ def process_back_ref(exp) # :nodoc:
128
141
  "$#{exp.shift}"
129
142
  end
130
143
 
131
144
  # TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
132
- def process_begin(exp)
145
+ def process_begin(exp) # :nodoc:
133
146
  code = []
134
147
  code << "begin"
135
148
  until exp.empty?
@@ -141,7 +154,7 @@ class Ruby2Ruby < SexpProcessor
141
154
  return code.join("\n")
142
155
  end
143
156
 
144
- def process_block(exp)
157
+ def process_block(exp) # :nodoc:
145
158
  result = []
146
159
 
147
160
  exp << nil if exp.empty?
@@ -160,22 +173,13 @@ class Ruby2Ruby < SexpProcessor
160
173
  return result
161
174
  end
162
175
 
163
- def parenthesize exp
164
- case self.context[1]
165
- when nil, :defn, :defs, :class, :sclass, :if, :iter, :resbody, :when, :while then
166
- exp
167
- else
168
- "(#{exp})"
169
- end
170
- end
171
-
172
- def process_block_pass exp
176
+ def process_block_pass exp # :nodoc:
173
177
  raise "huh?: #{exp.inspect}" if exp.size > 1
174
178
 
175
179
  "&#{process exp.shift}"
176
180
  end
177
181
 
178
- def process_break(exp)
182
+ def process_break(exp) # :nodoc:
179
183
  val = exp.empty? ? nil : process(exp.shift)
180
184
  # HACK "break" + (val ? " #{val}" : "")
181
185
  if val then
@@ -185,7 +189,7 @@ class Ruby2Ruby < SexpProcessor
185
189
  end
186
190
  end
187
191
 
188
- def process_call(exp)
192
+ def process_call(exp) # :nodoc:
189
193
  receiver_node_type = exp.first.nil? ? nil : exp.first.first
190
194
  receiver = process exp.shift
191
195
  receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type
@@ -246,7 +250,7 @@ class Ruby2Ruby < SexpProcessor
246
250
  @calls.pop
247
251
  end
248
252
 
249
- def process_case(exp)
253
+ def process_case(exp) # :nodoc:
250
254
  result = []
251
255
  expr = process exp.shift
252
256
  if expr then
@@ -268,7 +272,7 @@ class Ruby2Ruby < SexpProcessor
268
272
  result.join("\n")
269
273
  end
270
274
 
271
- def process_cdecl(exp)
275
+ def process_cdecl(exp) # :nodoc:
272
276
  lhs = exp.shift
273
277
  lhs = process lhs if Sexp === lhs
274
278
  unless exp.empty? then
@@ -279,43 +283,45 @@ class Ruby2Ruby < SexpProcessor
279
283
  end
280
284
  end
281
285
 
282
- def process_class(exp)
286
+ def process_class(exp) # :nodoc:
283
287
  "#{exp.comments}class #{util_module_or_class(exp, true)}"
284
288
  end
285
289
 
286
- def process_colon2(exp)
290
+ def process_colon2(exp) # :nodoc:
287
291
  "#{process(exp.shift)}::#{exp.shift}"
288
292
  end
289
293
 
290
- def process_colon3(exp)
294
+ def process_colon3(exp) # :nodoc:
291
295
  "::#{exp.shift}"
292
296
  end
293
297
 
294
- def process_const(exp)
298
+ def process_const(exp) # :nodoc:
295
299
  exp.shift.to_s
296
300
  end
297
301
 
298
- def process_cvar(exp)
302
+ def process_cvar(exp) # :nodoc:
299
303
  "#{exp.shift}"
300
304
  end
301
305
 
302
- def process_cvasgn(exp)
306
+ def process_cvasgn(exp) # :nodoc:
303
307
  "#{exp.shift} = #{process(exp.shift)}"
304
308
  end
305
309
 
306
- def process_cvdecl(exp)
310
+ def process_cvdecl(exp) # :nodoc:
307
311
  "#{exp.shift} = #{process(exp.shift)}"
308
312
  end
309
313
 
310
- def process_defined(exp)
314
+ def process_defined(exp) # :nodoc:
311
315
  "defined? #{process(exp.shift)}"
312
316
  end
313
317
 
314
- def process_defn(exp)
318
+ def process_defn(exp) # :nodoc:
315
319
  type1 = exp[1].first
316
320
  type2 = exp[2].first rescue nil
321
+ expect = [:ivar, :iasgn, :attrset]
317
322
 
318
- if type1 == :args and [:ivar, :iasgn, :attrset].include? type2 then
323
+ # s(name, args, ivar|iasgn|attrset)
324
+ if exp.size == 3 and type1 == :args and expect.include? type2 then
319
325
  name = exp.first # don't shift in case we pass through
320
326
  case type2
321
327
  when :ivar then
@@ -367,7 +373,7 @@ class Ruby2Ruby < SexpProcessor
367
373
  return "#{comm}def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n")
368
374
  end
369
375
 
370
- def process_defs(exp)
376
+ def process_defs(exp) # :nodoc:
371
377
  lhs = exp.shift
372
378
  var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first
373
379
  name = exp.shift
@@ -379,42 +385,36 @@ class Ruby2Ruby < SexpProcessor
379
385
  process_defn(exp)
380
386
  end
381
387
 
382
- def process_dot2(exp)
388
+ def process_dot2(exp) # :nodoc:
383
389
  "(#{process exp.shift}..#{process exp.shift})"
384
390
  end
385
391
 
386
- def process_dot3(exp)
392
+ def process_dot3(exp) # :nodoc:
387
393
  "(#{process exp.shift}...#{process exp.shift})"
388
394
  end
389
395
 
390
- def re_opt options
391
- bits = (0..8).map { |n| options[n] * 2**n }
392
- bits.delete 0
393
- bits.map { |n| Regexp::CODES[n] }.join
394
- end
395
-
396
- def process_dregx(exp)
396
+ def process_dregx(exp) # :nodoc:
397
397
  options = re_opt exp.pop if Fixnum === exp.last
398
398
  "/" << util_dthing(:dregx, exp) << "/#{options}"
399
399
  end
400
400
 
401
- def process_dregx_once(exp)
401
+ def process_dregx_once(exp) # :nodoc:
402
402
  process_dregx(exp) + "o"
403
403
  end
404
404
 
405
- def process_dstr(exp)
405
+ def process_dstr(exp) # :nodoc:
406
406
  "\"#{util_dthing(:dstr, exp)}\""
407
407
  end
408
408
 
409
- def process_dsym(exp)
409
+ def process_dsym(exp) # :nodoc:
410
410
  ":\"#{util_dthing(:dsym, exp)}\""
411
411
  end
412
412
 
413
- def process_dxstr(exp)
413
+ def process_dxstr(exp) # :nodoc:
414
414
  "`#{util_dthing(:dxstr, exp)}`"
415
415
  end
416
416
 
417
- def process_ensure(exp)
417
+ def process_ensure(exp) # :nodoc:
418
418
  body = process exp.shift
419
419
  ens = exp.shift
420
420
  ens = nil if ens == s(:nil)
@@ -427,23 +427,23 @@ class Ruby2Ruby < SexpProcessor
427
427
  return "#{body}\nensure\n#{indent ens}"
428
428
  end
429
429
 
430
- def process_evstr(exp)
430
+ def process_evstr(exp) # :nodoc:
431
431
  exp.empty? ? '' : process(exp.shift)
432
432
  end
433
433
 
434
- def process_false(exp)
434
+ def process_false(exp) # :nodoc:
435
435
  "false"
436
436
  end
437
437
 
438
- def process_flip2(exp)
438
+ def process_flip2(exp) # :nodoc:
439
439
  "#{process(exp.shift)}..#{process(exp.shift)}"
440
440
  end
441
441
 
442
- def process_flip3(exp)
442
+ def process_flip3(exp) # :nodoc:
443
443
  "#{process(exp.shift)}...#{process(exp.shift)}"
444
444
  end
445
445
 
446
- def process_for(exp)
446
+ def process_for(exp) # :nodoc:
447
447
  recv = process exp.shift
448
448
  iter = process exp.shift
449
449
  body = exp.empty? ? nil : process(exp.shift)
@@ -455,15 +455,15 @@ class Ruby2Ruby < SexpProcessor
455
455
  result.join("\n")
456
456
  end
457
457
 
458
- def process_gasgn(exp)
458
+ def process_gasgn(exp) # :nodoc:
459
459
  process_iasgn(exp)
460
460
  end
461
461
 
462
- def process_gvar(exp)
462
+ def process_gvar(exp) # :nodoc:
463
463
  return exp.shift.to_s
464
464
  end
465
465
 
466
- def process_hash(exp)
466
+ def process_hash(exp) # :nodoc:
467
467
  result = []
468
468
 
469
469
  until exp.empty?
@@ -479,7 +479,7 @@ class Ruby2Ruby < SexpProcessor
479
479
  return result.empty? ? "{}" : "{ #{result.join(', ')} }"
480
480
  end
481
481
 
482
- def process_iasgn(exp)
482
+ def process_iasgn(exp) # :nodoc:
483
483
  lhs = exp.shift
484
484
  if exp.empty? then # part of an masgn
485
485
  lhs.to_s
@@ -488,7 +488,7 @@ class Ruby2Ruby < SexpProcessor
488
488
  end
489
489
  end
490
490
 
491
- def process_if(exp)
491
+ def process_if(exp) # :nodoc:
492
492
  expand = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first
493
493
  c = process exp.shift
494
494
  t = process exp.shift
@@ -524,7 +524,7 @@ class Ruby2Ruby < SexpProcessor
524
524
  end
525
525
  end
526
526
 
527
- def process_iter(exp)
527
+ def process_iter(exp) # :nodoc:
528
528
  iter = process exp.shift
529
529
  args = exp.shift
530
530
  body = exp.empty? ? nil : process(exp.shift)
@@ -571,17 +571,17 @@ class Ruby2Ruby < SexpProcessor
571
571
  result.join
572
572
  end
573
573
 
574
- def process_ivar(exp)
574
+ def process_ivar(exp) # :nodoc:
575
575
  exp.shift.to_s
576
576
  end
577
577
 
578
- def process_lasgn(exp)
578
+ def process_lasgn(exp) # :nodoc:
579
579
  s = "#{exp.shift}"
580
580
  s += " = #{process exp.shift}" unless exp.empty?
581
581
  s
582
582
  end
583
583
 
584
- def process_lit(exp)
584
+ def process_lit(exp) # :nodoc:
585
585
  obj = exp.shift
586
586
  case obj
587
587
  when Range then
@@ -591,15 +591,11 @@ class Ruby2Ruby < SexpProcessor
591
591
  end
592
592
  end
593
593
 
594
- def process_lvar(exp)
594
+ def process_lvar(exp) # :nodoc:
595
595
  exp.shift.to_s
596
596
  end
597
597
 
598
- def splat(sym)
599
- :"*#{sym}"
600
- end
601
-
602
- def process_masgn(exp)
598
+ def process_masgn(exp) # :nodoc:
603
599
  lhs = exp.shift
604
600
  rhs = exp.empty? ? nil : exp.shift
605
601
 
@@ -637,17 +633,17 @@ class Ruby2Ruby < SexpProcessor
637
633
  end
638
634
  end
639
635
 
640
- def process_match(exp)
636
+ def process_match(exp) # :nodoc:
641
637
  "#{process(exp.shift)}"
642
638
  end
643
639
 
644
- def process_match2(exp)
640
+ def process_match2(exp) # :nodoc:
645
641
  lhs = process(exp.shift)
646
642
  rhs = process(exp.shift)
647
643
  "#{lhs} =~ #{rhs}"
648
644
  end
649
645
 
650
- def process_match3(exp)
646
+ def process_match3(exp) # :nodoc:
651
647
  rhs = process(exp.shift)
652
648
  left_type = exp.first.sexp_type
653
649
  lhs = process(exp.shift)
@@ -659,11 +655,11 @@ class Ruby2Ruby < SexpProcessor
659
655
  end
660
656
  end
661
657
 
662
- def process_module(exp)
658
+ def process_module(exp) # :nodoc:
663
659
  "#{exp.comments}module #{util_module_or_class(exp)}"
664
660
  end
665
661
 
666
- def process_next(exp)
662
+ def process_next(exp) # :nodoc:
667
663
  val = exp.empty? ? nil : process(exp.shift)
668
664
  if val then
669
665
  "next #{val}"
@@ -672,19 +668,19 @@ class Ruby2Ruby < SexpProcessor
672
668
  end
673
669
  end
674
670
 
675
- def process_nil(exp)
671
+ def process_nil(exp) # :nodoc:
676
672
  "nil"
677
673
  end
678
674
 
679
- def process_not(exp)
675
+ def process_not(exp) # :nodoc:
680
676
  "(not #{process exp.shift})"
681
677
  end
682
678
 
683
- def process_nth_ref(exp)
679
+ def process_nth_ref(exp) # :nodoc:
684
680
  "$#{exp.shift}"
685
681
  end
686
682
 
687
- def process_op_asgn1(exp)
683
+ def process_op_asgn1(exp) # :nodoc:
688
684
  # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]]
689
685
  lhs = process(exp.shift)
690
686
  index = process(exp.shift)
@@ -694,7 +690,7 @@ class Ruby2Ruby < SexpProcessor
694
690
  "#{lhs}[#{index}] #{msg}= #{rhs}"
695
691
  end
696
692
 
697
- def process_op_asgn2(exp)
693
+ def process_op_asgn2(exp) # :nodoc:
698
694
  # [[:lvar, :c], :var=, :"||", [:lit, 20]]
699
695
  lhs = process(exp.shift)
700
696
  index = exp.shift.to_s[0..-2]
@@ -705,33 +701,33 @@ class Ruby2Ruby < SexpProcessor
705
701
  "#{lhs}.#{index} #{msg}= #{rhs}"
706
702
  end
707
703
 
708
- def process_op_asgn_and(exp)
704
+ def process_op_asgn_and(exp) # :nodoc:
709
705
  # a &&= 1
710
706
  # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
711
707
  exp.shift
712
708
  process(exp.shift).sub(/\=/, '&&=')
713
709
  end
714
710
 
715
- def process_op_asgn_or(exp)
711
+ def process_op_asgn_or(exp) # :nodoc:
716
712
  # a ||= 1
717
713
  # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
718
714
  exp.shift
719
715
  process(exp.shift).sub(/\=/, '||=')
720
716
  end
721
717
 
722
- def process_or(exp)
718
+ def process_or(exp) # :nodoc:
723
719
  "(#{process exp.shift} or #{process exp.shift})"
724
720
  end
725
721
 
726
- def process_postexe(exp)
722
+ def process_postexe(exp) # :nodoc:
727
723
  "END"
728
724
  end
729
725
 
730
- def process_redo(exp)
726
+ def process_redo(exp) # :nodoc:
731
727
  "redo"
732
728
  end
733
729
 
734
- def process_resbody exp
730
+ def process_resbody exp # :nodoc:
735
731
  args = exp.shift
736
732
  body = finish(exp)
737
733
  body << "# do nothing" if body.empty?
@@ -745,7 +741,7 @@ class Ruby2Ruby < SexpProcessor
745
741
  "rescue#{args}\n#{indent body.join("\n")}"
746
742
  end
747
743
 
748
- def process_rescue exp
744
+ def process_rescue exp # :nodoc:
749
745
  body = process(exp.shift) unless exp.first.first == :resbody
750
746
  els = process(exp.pop) unless exp.last.first == :resbody
751
747
 
@@ -771,11 +767,11 @@ class Ruby2Ruby < SexpProcessor
771
767
  end
772
768
  end
773
769
 
774
- def process_retry(exp)
770
+ def process_retry(exp) # :nodoc:
775
771
  "retry"
776
772
  end
777
773
 
778
- def process_return(exp)
774
+ def process_return(exp) # :nodoc:
779
775
  # HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}")
780
776
 
781
777
  if exp.empty? then
@@ -785,15 +781,15 @@ class Ruby2Ruby < SexpProcessor
785
781
  end
786
782
  end
787
783
 
788
- def process_sclass(exp)
784
+ def process_sclass(exp) # :nodoc:
789
785
  "class << #{process(exp.shift)}\n#{indent(process(exp.shift))}\nend"
790
786
  end
791
787
 
792
- def process_self(exp)
788
+ def process_self(exp) # :nodoc:
793
789
  "self"
794
790
  end
795
791
 
796
- def process_splat(exp)
792
+ def process_splat(exp) # :nodoc:
797
793
  if exp.empty? then
798
794
  "*"
799
795
  else
@@ -801,17 +797,17 @@ class Ruby2Ruby < SexpProcessor
801
797
  end
802
798
  end
803
799
 
804
- def process_str(exp)
800
+ def process_str(exp) # :nodoc:
805
801
  return exp.shift.dump
806
802
  end
807
803
 
808
- def process_super(exp)
804
+ def process_super(exp) # :nodoc:
809
805
  args = finish exp
810
806
 
811
807
  "super(#{args.join(', ')})"
812
808
  end
813
809
 
814
- def process_svalue(exp)
810
+ def process_svalue(exp) # :nodoc:
815
811
  code = []
816
812
  until exp.empty? do
817
813
  code << process(exp.shift)
@@ -819,27 +815,27 @@ class Ruby2Ruby < SexpProcessor
819
815
  code.join(", ")
820
816
  end
821
817
 
822
- def process_to_ary(exp)
818
+ def process_to_ary(exp) # :nodoc:
823
819
  process(exp.shift)
824
820
  end
825
821
 
826
- def process_true(exp)
822
+ def process_true(exp) # :nodoc:
827
823
  "true"
828
824
  end
829
825
 
830
- def process_undef(exp)
826
+ def process_undef(exp) # :nodoc:
831
827
  "undef #{process(exp.shift)}"
832
828
  end
833
829
 
834
- def process_until(exp)
830
+ def process_until(exp) # :nodoc:
835
831
  cond_loop(exp, 'until')
836
832
  end
837
833
 
838
- def process_valias(exp)
834
+ def process_valias(exp) # :nodoc:
839
835
  "alias #{exp.shift} #{exp.shift}"
840
836
  end
841
837
 
842
- def process_when(exp)
838
+ def process_when(exp) # :nodoc:
843
839
  src = []
844
840
 
845
841
  if self.context[1] == :array then # ugh. matz! why not an argscat?!?
@@ -858,15 +854,15 @@ class Ruby2Ruby < SexpProcessor
858
854
  src.join("\n")
859
855
  end
860
856
 
861
- def process_while(exp)
857
+ def process_while(exp) # :nodoc:
862
858
  cond_loop(exp, 'while')
863
859
  end
864
860
 
865
- def process_xstr(exp)
861
+ def process_xstr(exp) # :nodoc:
866
862
  "`#{process_str(exp)[1..-2]}`"
867
863
  end
868
864
 
869
- def process_yield(exp)
865
+ def process_yield(exp) # :nodoc:
870
866
  args = []
871
867
  until exp.empty? do
872
868
  args << process(exp.shift)
@@ -879,34 +875,14 @@ class Ruby2Ruby < SexpProcessor
879
875
  end
880
876
  end
881
877
 
882
- def process_zsuper(exp)
878
+ def process_zsuper(exp) # :nodoc:
883
879
  "super"
884
880
  end
885
881
 
886
- def cond_loop(exp, name)
887
- cond = process(exp.shift)
888
- body = process(exp.shift)
889
- head_controlled = exp.shift
890
-
891
- body = indent(body).chomp if body
892
-
893
- code = []
894
- if head_controlled then
895
- code << "#{name} #{cond} do"
896
- code << body if body
897
- code << "end"
898
- else
899
- code << "begin"
900
- code << body if body
901
- code << "end #{name} #{cond}"
902
- end
903
- code.join("\n")
904
- end
905
-
906
882
  ############################################################
907
883
  # Rewriters:
908
884
 
909
- def rewrite_attrasgn exp
885
+ def rewrite_attrasgn exp # :nodoc:
910
886
  if context.first(2) == [:array, :masgn] then
911
887
  exp[0] = :call
912
888
  exp[2] = exp[2].to_s.sub(/=$/, '').to_sym
@@ -915,19 +891,19 @@ class Ruby2Ruby < SexpProcessor
915
891
  exp
916
892
  end
917
893
 
918
- def rewrite_ensure exp
894
+ def rewrite_ensure exp # :nodoc:
919
895
  exp = s(:begin, exp) unless context.first == :begin
920
896
  exp
921
897
  end
922
898
 
923
- def rewrite_resbody exp
899
+ def rewrite_resbody exp # :nodoc:
924
900
  raise "no exception list in #{exp.inspect}" unless exp.size > 2 && exp[1]
925
901
  raise exp[1].inspect if exp[1][0] != :array
926
902
  # for now, do nothing, just check and freak if we see an errant structure
927
903
  exp
928
904
  end
929
905
 
930
- def rewrite_rescue exp
906
+ def rewrite_rescue exp # :nodoc:
931
907
  complex = false
932
908
  complex ||= exp.size > 3
933
909
  complex ||= exp.resbody.block
@@ -942,7 +918,7 @@ class Ruby2Ruby < SexpProcessor
942
918
  exp
943
919
  end
944
920
 
945
- def rewrite_svalue(exp)
921
+ def rewrite_svalue(exp) # :nodoc:
946
922
  case exp.last.first
947
923
  when :array
948
924
  s(:svalue, *exp[1][1..-1])
@@ -956,14 +932,32 @@ class Ruby2Ruby < SexpProcessor
956
932
  ############################################################
957
933
  # Utility Methods:
958
934
 
959
- def finish exp # REFACTOR: work this out of the rest of the processors
960
- body = []
961
- until exp.empty? do
962
- body << process(exp.shift)
935
+ ##
936
+ # Generate a post-or-pre conditional loop.
937
+
938
+ def cond_loop(exp, name)
939
+ cond = process(exp.shift)
940
+ body = process(exp.shift)
941
+ head_controlled = exp.shift
942
+
943
+ body = indent(body).chomp if body
944
+
945
+ code = []
946
+ if head_controlled then
947
+ code << "#{name} #{cond} do"
948
+ code << body if body
949
+ code << "end"
950
+ else
951
+ code << "begin"
952
+ code << body if body
953
+ code << "end #{name} #{cond}"
963
954
  end
964
- body.compact
955
+ code.join("\n")
965
956
  end
966
957
 
958
+ ##
959
+ # Utility method to escape something interpolated.
960
+
967
961
  def dthing_escape type, lit
968
962
  lit = lit.gsub(/\n/, '\n')
969
963
  case type
@@ -978,6 +972,56 @@ class Ruby2Ruby < SexpProcessor
978
972
  end
979
973
  end
980
974
 
975
+ ##
976
+ # Process all the remaining stuff in +exp+ and return the results
977
+ # sans-nils.
978
+
979
+ def finish exp # REFACTOR: work this out of the rest of the processors
980
+ body = []
981
+ until exp.empty? do
982
+ body << process(exp.shift)
983
+ end
984
+ body.compact
985
+ end
986
+
987
+ ##
988
+ # Indent all lines of +s+ to the current indent level.
989
+
990
+ def indent(s)
991
+ s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
992
+ end
993
+
994
+ ##
995
+ # Wrap appropriate expressions in matching parens.
996
+
997
+ def parenthesize exp
998
+ case self.context[1]
999
+ when nil, :defn, :defs, :class, :sclass, :if, :iter, :resbody, :when, :while then
1000
+ exp
1001
+ else
1002
+ "(#{exp})"
1003
+ end
1004
+ end
1005
+
1006
+ ##
1007
+ # Return the appropriate regexp flags for a given numeric code.
1008
+
1009
+ def re_opt options
1010
+ bits = (0..8).map { |n| options[n] * 2**n }
1011
+ bits.delete 0
1012
+ bits.map { |n| Regexp::CODES[n] }.join
1013
+ end
1014
+
1015
+ ##
1016
+ # Return a splatted symbol for +sym+.
1017
+
1018
+ def splat(sym)
1019
+ :"*#{sym}"
1020
+ end
1021
+
1022
+ ##
1023
+ # Utility method to generate something interpolated.
1024
+
981
1025
  def util_dthing(type, exp)
982
1026
  s = []
983
1027
 
@@ -1004,6 +1048,9 @@ class Ruby2Ruby < SexpProcessor
1004
1048
  s.join
1005
1049
  end
1006
1050
 
1051
+ ##
1052
+ # Utility method to generate ether a module or class.
1053
+
1007
1054
  def util_module_or_class(exp, is_class=false)
1008
1055
  result = []
1009
1056
 
@@ -1035,8 +1082,4 @@ class Ruby2Ruby < SexpProcessor
1035
1082
 
1036
1083
  result.join
1037
1084
  end
1038
-
1039
- def indent(s)
1040
- s.to_s.split(/\n/).map{|line| @indent + line}.join("\n")
1041
- end
1042
1085
  end
@@ -92,6 +92,12 @@ class TestRuby2Ruby < R2RTestCase
92
92
  util_compare inn, out
93
93
  end
94
94
 
95
+ def test_attr_reader_double
96
+ inn = s(:defn, :same, s(:args), s(:ivar, :@same), s(:ivar, :@diff))
97
+ out = "def same\n @same\n @diff\nend"
98
+ util_compare inn, out
99
+ end
100
+
95
101
  def test_attr_reader_same_name_diff_body
96
102
  inn = s(:defn, :same, s(:args), s(:not, s(:ivar, :@same)))
97
103
  out = "def same\n (not @same)\nend"
@@ -104,6 +110,13 @@ class TestRuby2Ruby < R2RTestCase
104
110
  util_compare inn, out
105
111
  end
106
112
 
113
+ def test_attr_writer_double
114
+ inn = s(:defn, :same=, s(:args, :o),
115
+ s(:iasgn, :@same, s(:lvar, :o)), s(:iasgn, :@diff, s(:lvar, :o)))
116
+ out = "def same=(o)\n @same = o\n @diff = o\nend"
117
+ util_compare inn, out
118
+ end
119
+
107
120
  def test_attr_writer_same_name_diff_body
108
121
  inn = s(:defn, :same=, s(:args, :o), s(:iasgn, :@same, s(:lit, 42)))
109
122
  out = "def same=(o)\n @same = 42\nend"
@@ -143,10 +156,15 @@ class TestRuby2Ruby < R2RTestCase
143
156
  end
144
157
 
145
158
  def test_call_self_index_equals
146
- util_compare(s(:call, nil, :[]=, s(:lit, 42), s(:lit, 24)),
159
+ util_compare(s(:attrasgn, s(:self), :[]=, s(:lit, 42), s(:lit, 24)),
147
160
  "self[42] = 24")
148
161
  end
149
162
 
163
+ def test_call_self_index_equals_array
164
+ util_compare(s(:attrasgn, s(:self), :[]=, s(:lit, 1), s(:lit, 2), s(:lit, 3)),
165
+ "self[1, 2] = 3")
166
+ end
167
+
150
168
  def test_call_arglist_hash_first
151
169
  inn = s(:call, nil, :method,
152
170
  s(:hash, s(:lit, :a), s(:lit, 1)),
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby2ruby
3
3
  version: !ruby/object:Gem::Version
4
- hash: 7
4
+ hash: 5
5
5
  prerelease:
6
6
  segments:
7
7
  - 2
8
8
  - 0
9
- - 4
10
- version: 2.0.4
9
+ - 5
10
+ version: 2.0.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ryan Davis
@@ -36,7 +36,7 @@ cert_chain:
36
36
  FBHgymkyj/AOSqKRIpXPhjC6
37
37
  -----END CERTIFICATE-----
38
38
 
39
- date: 2013-03-28 00:00:00 Z
39
+ date: 2013-04-25 00:00:00 Z
40
40
  dependencies:
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: sexp_processor
@@ -91,11 +91,11 @@ dependencies:
91
91
  requirements:
92
92
  - - ~>
93
93
  - !ruby/object:Gem::Version
94
- hash: 19
94
+ hash: 27
95
95
  segments:
96
- - 3
97
- - 10
98
- version: "3.10"
96
+ - 4
97
+ - 0
98
+ version: "4.0"
99
99
  type: :development
100
100
  version_requirements: *id004
101
101
  - !ruby/object:Gem::Dependency
@@ -106,11 +106,11 @@ dependencies:
106
106
  requirements:
107
107
  - - ~>
108
108
  - !ruby/object:Gem::Version
109
- hash: 13
109
+ hash: 11
110
110
  segments:
111
111
  - 3
112
- - 5
113
- version: "3.5"
112
+ - 6
113
+ version: "3.6"
114
114
  type: :development
115
115
  version_requirements: *id005
116
116
  description: |-
metadata.gz.sig CHANGED
@@ -1,3 +1,3 @@
1
- V��>\5{�j[�������{
2
- �Ԑ8Ed�[�4p�`�BhZ������=Ĵ -J�� 9���]۹�������ENE#ō�S��%�L�C��RsOB��Ll��t��^����id�#j��~�[OD���S~e��b�F.RGI��;f��
3
- �:|��)��ܝ��'[��8G��a��F���������2���)�l�L�[Q�y���X�ӠDN�0��Wj"�ڄd3�LЭ�
1
+ _�䓿4��
2
+ 8��
3
+ ��Uo0��9����:�WV