SexpProcessor
Nodes that represent assignment and probably need () around them.
TODO: this should be replaced with full precedence support :/
# File lib/ruby2ruby.rb, line 865 865: def cond_loop(exp, name) 866: cond = process(exp.shift) 867: body = process(exp.shift) 868: head_controlled = exp.shift 869: 870: body = indent(body).chomp if body 871: 872: code = [] 873: if head_controlled then 874: code << "#{name} #{cond} do" 875: code << body if body 876: code << "end" 877: else 878: code << "begin" 879: code << body if body 880: code << "end #{name} #{cond}" 881: end 882: code.join("\n") 883: end
Utility Methods:
# File lib/ruby2ruby.rb, line 938 938: def dthing_escape type, lit 939: lit = lit.gsub(/\n/, '\n') 940: case type 941: when :dregx then 942: lit.gsub(/(\A|[^\\])\//, '\1\/') 943: when :dstr, :dsym then 944: lit.gsub(/"/, '\"') 945: when :dxstr then 946: lit.gsub(/`/, '\`') 947: else 948: raise "unsupported type #{type.inspect}" 949: end 950: end
# File lib/ruby2ruby.rb, line 1010 1010: def indent(s) 1011: s.to_s.split(/\n/).map{|line| @indent + line}.join("\n") 1012: end
# File lib/ruby2ruby.rb, line 170 170: def parenthesize exp 171: case self.context[1] 172: when nil, :scope, :if, :iter, :resbody, :when, :while then 173: exp 174: else 175: "(#{exp})" 176: end 177: end
Processors
# File lib/ruby2ruby.rb, line 67 67: def process_alias(exp) 68: parenthesize "alias #{process(exp.shift)} #{process(exp.shift)}" 69: end
# File lib/ruby2ruby.rb, line 71 71: def process_and(exp) 72: parenthesize "#{process exp.shift} and #{process exp.shift}" 73: end
# File lib/ruby2ruby.rb, line 75 75: def process_arglist(exp) # custom made node 76: code = [] 77: until exp.empty? do 78: code << process(exp.shift) 79: end 80: code.join ', ' 81: end
# File lib/ruby2ruby.rb, line 83 83: def process_args(exp) 84: args = [] 85: 86: until exp.empty? do 87: arg = exp.shift 88: case arg 89: when Symbol then 90: args << arg 91: when Array then 92: case arg.first 93: when :block then 94: asgns = {} 95: arg[1..1].each do |lasgn| 96: asgns[lasgn[1]] = process(lasgn) 97: end 98: 99: args.each_with_index do |name, index| 100: args[index] = asgns[name] if asgns.has_key? name 101: end 102: else 103: raise "unknown arg type #{arg.first.inspect}" 104: end 105: else 106: raise "unknown arg type #{arg.inspect}" 107: end 108: end 109: 110: return "(#{args.join ', '})" 111: end
# File lib/ruby2ruby.rb, line 113 113: def process_array(exp) 114: "[#{process_arglist(exp)}]" 115: end
# File lib/ruby2ruby.rb, line 117 117: def process_attrasgn(exp) 118: receiver = process exp.shift 119: name = exp.shift 120: args = exp.empty? ? nil : exp.shift 121: 122: case name 123: when :[]= then 124: rhs = process args.pop 125: "#{receiver}[#{process(args)}] = #{rhs}" 126: else 127: name = name.to_s.sub(/=$/, '') 128: if args && args != s(:arglist) then 129: "#{receiver}.#{name} = #{process(args)}" 130: end 131: end 132: end
# File lib/ruby2ruby.rb, line 134 134: def process_back_ref(exp) 135: "$#{exp.shift}" 136: end
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
# File lib/ruby2ruby.rb, line 139 139: def process_begin(exp) 140: code = [] 141: code << "begin" 142: until exp.empty? 143: src = process(exp.shift) 144: src = indent(src) unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues 145: code << src 146: end 147: code << "end" 148: return code.join("\n") 149: end
# File lib/ruby2ruby.rb, line 151 151: def process_block(exp) 152: result = [] 153: 154: exp << nil if exp.empty? 155: until exp.empty? do 156: code = exp.shift 157: if code.nil? or code.first == :nil then 158: result << "# do nothing\n" 159: else 160: result << process(code) 161: end 162: end 163: 164: result = parenthesize result.join "\n" 165: result += "\n" unless result.start_with? "(" 166: 167: return result 168: end
# File lib/ruby2ruby.rb, line 179 179: def process_block_pass exp 180: raise "huh?: #{exp.inspect}" if exp.size > 1 181: 182: "&#{process exp.shift}" 183: end
# File lib/ruby2ruby.rb, line 185 185: def process_break(exp) 186: val = exp.empty? ? nil : process(exp.shift) 187: # HACK "break" + (val ? " #{val}" : "") 188: if val then 189: "break #{val}" 190: else 191: "break" 192: end 193: end
# File lib/ruby2ruby.rb, line 195 195: def process_call(exp) 196: receiver_node_type = exp.first.nil? ? nil : exp.first.first 197: receiver = process exp.shift 198: receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type 199: 200: name = exp.shift 201: args = [] 202: 203: # this allows us to do both old and new sexp forms: 204: exp.push(*exp.pop[1..1]) if exp.size == 1 && exp.first.first == :arglist 205: 206: @calls.push name 207: 208: in_context :arglist do 209: until exp.empty? do 210: arg_type = exp.first.sexp_type 211: arg = process exp.shift 212: 213: next if arg.empty? 214: 215: strip_hash = (arg_type == :hash and 216: not BINARY.include? name and 217: (exp.empty? or exp.first.sexp_type == :splat)) 218: wrap_arg = Ruby2Ruby::ASSIGN_NODES.include? arg_type 219: 220: arg = arg[2..3] if strip_hash 221: arg = "(#{arg})" if wrap_arg 222: 223: args << arg 224: end 225: end 226: 227: case name 228: when *BINARY then 229: "(#{receiver} #{name} #{args.join(', ')})" 230: when :[] then 231: receiver ||= "self" 232: "#{receiver}[#{args.join(', ')}]" 233: when :[]= then 234: receiver ||= "self" 235: rhs = args.pop 236: "#{receiver}[#{args.join(', ')}] = #{rhs}" 237: when :"-@" then 238: "-#{receiver}" 239: when :"+@" then 240: "+#{receiver}" 241: else 242: args = nil if args.empty? 243: args = "(#{args.join(', ')})" if args 244: receiver = "#{receiver}." if receiver 245: 246: "#{receiver}#{name}#{args}" 247: end 248: ensure 249: @calls.pop 250: end
# File lib/ruby2ruby.rb, line 252 252: def process_case(exp) 253: result = [] 254: expr = process exp.shift 255: if expr then 256: result << "case #{expr}" 257: else 258: result << "case" 259: end 260: until exp.empty? 261: pt = exp.shift 262: if pt and pt.first == :when 263: result << "#{process(pt)}" 264: else 265: code = indent(process(pt)) 266: code = indent("# do nothing") if code =~ /^\s*$/ 267: result << "else\n#{code}" 268: end 269: end 270: result << "end" 271: result.join("\n") 272: end
# File lib/ruby2ruby.rb, line 274 274: def process_cdecl(exp) 275: lhs = exp.shift 276: lhs = process lhs if Sexp === lhs 277: unless exp.empty? then 278: rhs = process(exp.shift) 279: "#{lhs} = #{rhs}" 280: else 281: lhs.to_s 282: end 283: end
# File lib/ruby2ruby.rb, line 285 285: def process_class(exp) 286: "#{exp.comments}class #{util_module_or_class(exp, true)}" 287: end
# File lib/ruby2ruby.rb, line 289 289: def process_colon2(exp) 290: "#{process(exp.shift)}::#{exp.shift}" 291: end
# File lib/ruby2ruby.rb, line 293 293: def process_colon3(exp) 294: "::#{exp.shift}" 295: end
# File lib/ruby2ruby.rb, line 297 297: def process_const(exp) 298: exp.shift.to_s 299: end
# File lib/ruby2ruby.rb, line 301 301: def process_cvar(exp) 302: "#{exp.shift}" 303: end
# File lib/ruby2ruby.rb, line 305 305: def process_cvasgn(exp) 306: "#{exp.shift} = #{process(exp.shift)}" 307: end
# File lib/ruby2ruby.rb, line 309 309: def process_cvdecl(exp) 310: "#{exp.shift} = #{process(exp.shift)}" 311: end
# File lib/ruby2ruby.rb, line 313 313: def process_defined(exp) 314: "defined? #{process(exp.shift)}" 315: end
# File lib/ruby2ruby.rb, line 317 317: def process_defn(exp) 318: type1 = exp[1].first 319: type2 = exp[2].first rescue nil 320: 321: if type1 == :args and [:ivar, :attrset].include? type2 then 322: name = exp.shift 323: case type2 324: when :ivar then 325: exp.clear 326: return "attr_reader #{name.inspect}" 327: when :attrset then 328: exp.clear 329: return "attr_writer :#{name.to_s[0..-2]}" 330: else 331: raise "Unknown defn type: #{exp.inspect}" 332: end 333: end 334: 335: case type1 336: when :scope, :args then 337: name = exp.shift 338: args = process(exp.shift) 339: args = "" if args == "()" 340: body = [] 341: until exp.empty? do 342: body << indent(process(exp.shift)) 343: end 344: body = body.join("\n") 345: return "#{exp.comments}def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n") 346: else 347: raise "Unknown defn type: #{type1} for #{exp.inspect}" 348: end 349: end
# File lib/ruby2ruby.rb, line 351 351: def process_defs(exp) 352: lhs = exp.shift 353: var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first 354: name = exp.shift 355: 356: lhs = process(lhs) 357: lhs = "(#{lhs})" unless var 358: 359: exp.unshift "#{lhs}.#{name}" 360: process_defn(exp) 361: end
# File lib/ruby2ruby.rb, line 363 363: def process_dot2(exp) 364: "(#{process exp.shift}..#{process exp.shift})" 365: end
# File lib/ruby2ruby.rb, line 367 367: def process_dot3(exp) 368: "(#{process exp.shift}...#{process exp.shift})" 369: end
# File lib/ruby2ruby.rb, line 377 377: def process_dregx(exp) 378: options = re_opt exp.pop if Fixnum === exp.last 379: "/" << util_dthing(:dregx, exp) << "/#{options}" 380: end
# File lib/ruby2ruby.rb, line 382 382: def process_dregx_once(exp) 383: process_dregx(exp) + "o" 384: end
# File lib/ruby2ruby.rb, line 386 386: def process_dstr(exp) 387: "\"#{util_dthing(:dstr, exp)}\"" 388: end
# File lib/ruby2ruby.rb, line 390 390: def process_dsym(exp) 391: ":\"#{util_dthing(:dsym, exp)}\"" 392: end
# File lib/ruby2ruby.rb, line 394 394: def process_dxstr(exp) 395: "`#{util_dthing(:dxstr, exp)}`" 396: end
# File lib/ruby2ruby.rb, line 398 398: def process_ensure(exp) 399: body = process exp.shift 400: ens = exp.shift 401: ens = nil if ens == s(:nil) 402: ens = process(ens) || "# do nothing" 403: ens = "begin\n#{ens}\nend\n" if ens =~ /(^|\n)rescue/ 404: 405: body.sub!(/\n\s*end\z/, '') 406: body = indent(body) unless body =~ /(^|\n)rescue/ 407: 408: return "#{body}\nensure\n#{indent ens}" 409: end
# File lib/ruby2ruby.rb, line 411 411: def process_evstr(exp) 412: exp.empty? ? '' : process(exp.shift) 413: end
# File lib/ruby2ruby.rb, line 415 415: def process_false(exp) 416: "false" 417: end
# File lib/ruby2ruby.rb, line 419 419: def process_flip2(exp) 420: "#{process(exp.shift)}..#{process(exp.shift)}" 421: end
# File lib/ruby2ruby.rb, line 423 423: def process_flip3(exp) 424: "#{process(exp.shift)}...#{process(exp.shift)}" 425: end
# File lib/ruby2ruby.rb, line 427 427: def process_for(exp) 428: recv = process exp.shift 429: iter = process exp.shift 430: body = exp.empty? ? nil : process(exp.shift) 431: 432: result = ["for #{iter} in #{recv} do"] 433: result << indent(body ? body : "# do nothing") 434: result << "end" 435: 436: result.join("\n") 437: end
# File lib/ruby2ruby.rb, line 439 439: def process_gasgn(exp) 440: process_iasgn(exp) 441: end
# File lib/ruby2ruby.rb, line 443 443: def process_gvar(exp) 444: return exp.shift.to_s 445: end
# File lib/ruby2ruby.rb, line 447 447: def process_hash(exp) 448: result = [] 449: 450: until exp.empty? 451: lhs = process(exp.shift) 452: rhs = exp.shift 453: t = rhs.first 454: rhs = process rhs 455: rhs = "(#{rhs})" unless [:lit, :str].include? t # TODO: verify better! 456: 457: result << "#{lhs} => #{rhs}" 458: end 459: 460: return "{ #{result.join(', ')} }" 461: end
# File lib/ruby2ruby.rb, line 463 463: def process_iasgn(exp) 464: lhs = exp.shift 465: if exp.empty? then # part of an masgn 466: lhs.to_s 467: else 468: "#{lhs} = #{process exp.shift}" 469: end 470: end
# File lib/ruby2ruby.rb, line 472 472: def process_if(exp) 473: expand = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first 474: c = process exp.shift 475: t = process exp.shift 476: f = process exp.shift 477: 478: c = "(#{c.chomp})" if c =~ /\n/ 479: 480: if t then 481: unless expand then 482: if f then 483: r = "#{c} ? (#{t}) : (#{f})" 484: r = nil if r =~ /return/ # HACK - need contextual awareness or something 485: else 486: r = "#{t} if #{c}" 487: end 488: return r if r and (@indent+r).size < LINE_LENGTH and r !~ /\n/ 489: end 490: 491: r = "if #{c} then\n#{indent(t)}\n" 492: r << "else\n#{indent(f)}\n" if f 493: r << "end" 494: 495: r 496: elsif f 497: unless expand then 498: r = "#{f} unless #{c}" 499: return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/ 500: end 501: "unless #{c} then\n#{indent(f)}\nend" 502: else 503: # empty if statement, just do it in case of side effects from condition 504: "if #{c} then\n#{indent '# do nothing'}\nend" 505: end 506: end
# File lib/ruby2ruby.rb, line 508 508: def process_iter(exp) 509: iter = process exp.shift 510: args = exp.shift 511: args = (args == 0) ? '' : process(args) 512: body = exp.empty? ? nil : process(exp.shift) 513: 514: b, e = if iter == "END" then 515: [ "{", "}" ] 516: else 517: [ "do", "end" ] 518: end 519: 520: iter.sub!(/\(\)$/, '') 521: 522: # REFACTOR: ugh 523: result = [] 524: result << "#{iter} {" 525: result << " |#{args}|" if args 526: if body then 527: result << " #{body.strip} " 528: else 529: result << ' ' 530: end 531: result << "}" 532: result = result.join 533: return result if result !~ /\n/ and result.size < LINE_LENGTH 534: 535: result = [] 536: result << "#{iter} #{b}" 537: result << " |#{args}|" if args 538: result << "\n" 539: if body then 540: result << indent(body.strip) 541: result << "\n" 542: end 543: result << e 544: result.join 545: end
# File lib/ruby2ruby.rb, line 547 547: def process_ivar(exp) 548: exp.shift.to_s 549: end
# File lib/ruby2ruby.rb, line 551 551: def process_lasgn(exp) 552: s = "#{exp.shift}" 553: s += " = #{process exp.shift}" unless exp.empty? 554: s 555: end
# File lib/ruby2ruby.rb, line 557 557: def process_lit(exp) 558: obj = exp.shift 559: case obj 560: when Range then 561: "(#{obj.inspect})" 562: else 563: obj.inspect 564: end 565: end
# File lib/ruby2ruby.rb, line 567 567: def process_lvar(exp) 568: exp.shift.to_s 569: end
# File lib/ruby2ruby.rb, line 575 575: def process_masgn(exp) 576: lhs = exp.shift 577: rhs = exp.empty? ? nil : exp.shift 578: 579: case lhs.first 580: when :array then 581: lhs.shift 582: lhs = lhs.map do |l| 583: case l.first 584: when :masgn then 585: "(#{process(l)})" 586: else 587: process(l) 588: end 589: end 590: when :lasgn then 591: lhs = [ splat(lhs.last) ] 592: when :splat then 593: lhs = [ :"*" ] 594: else 595: raise "no clue: #{lhs.inspect}" 596: end 597: 598: if context[1] == :iter and rhs then 599: lhs << splat(rhs[1]) 600: rhs = nil 601: end 602: 603: unless rhs.nil? then 604: t = rhs.first 605: rhs = process rhs 606: rhs = rhs[1..2] if t == :array # FIX: bad? I dunno 607: return "#{lhs.join(", ")} = #{rhs}" 608: else 609: return lhs.join(", ") 610: end 611: end
# File lib/ruby2ruby.rb, line 613 613: def process_match(exp) 614: "#{process(exp.shift)}" 615: end
# File lib/ruby2ruby.rb, line 617 617: def process_match2(exp) 618: lhs = process(exp.shift) 619: rhs = process(exp.shift) 620: "#{lhs} =~ #{rhs}" 621: end
# File lib/ruby2ruby.rb, line 623 623: def process_match3(exp) 624: rhs = process(exp.shift) 625: left_type = exp.first.sexp_type 626: lhs = process(exp.shift) 627: 628: if ASSIGN_NODES.include? left_type then 629: "(#{lhs}) =~ #{rhs}" 630: else 631: "#{lhs} =~ #{rhs}" 632: end 633: end
# File lib/ruby2ruby.rb, line 635 635: def process_module(exp) 636: "#{exp.comments}module #{util_module_or_class(exp)}" 637: end
# File lib/ruby2ruby.rb, line 639 639: def process_next(exp) 640: val = exp.empty? ? nil : process(exp.shift) 641: if val then 642: "next #{val}" 643: else 644: "next" 645: end 646: end
# File lib/ruby2ruby.rb, line 648 648: def process_nil(exp) 649: "nil" 650: end
# File lib/ruby2ruby.rb, line 652 652: def process_not(exp) 653: "(not #{process exp.shift})" 654: end
# File lib/ruby2ruby.rb, line 656 656: def process_nth_ref(exp) 657: "$#{exp.shift}" 658: end
# File lib/ruby2ruby.rb, line 660 660: def process_op_asgn1(exp) 661: # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]] 662: lhs = process(exp.shift) 663: index = process(exp.shift) 664: msg = exp.shift 665: rhs = process(exp.shift) 666: 667: "#{lhs}[#{index}] #{msg}= #{rhs}" 668: end
# File lib/ruby2ruby.rb, line 670 670: def process_op_asgn2(exp) 671: # [[:lvar, :c], :var=, :"||", [:lit, 20]] 672: lhs = process(exp.shift) 673: index = exp.shift.to_s[0..2] 674: msg = exp.shift 675: 676: rhs = process(exp.shift) 677: 678: "#{lhs}.#{index} #{msg}= #{rhs}" 679: end
# File lib/ruby2ruby.rb, line 681 681: def process_op_asgn_and(exp) 682: # a &&= 1 683: # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] 684: exp.shift 685: process(exp.shift).sub(/\=/, '&&=') 686: end
# File lib/ruby2ruby.rb, line 688 688: def process_op_asgn_or(exp) 689: # a ||= 1 690: # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] 691: exp.shift 692: process(exp.shift).sub(/\=/, '||=') 693: end
# File lib/ruby2ruby.rb, line 695 695: def process_or(exp) 696: "(#{process exp.shift} or #{process exp.shift})" 697: end
# File lib/ruby2ruby.rb, line 699 699: def process_postexe(exp) 700: "END" 701: end
# File lib/ruby2ruby.rb, line 703 703: def process_redo(exp) 704: "redo" 705: end
# File lib/ruby2ruby.rb, line 707 707: def process_resbody exp 708: args = exp.shift 709: body = process(exp.shift) || "# do nothing" 710: 711: name = args.lasgn true 712: name ||= args.iasgn true 713: args = process(args)[1..2] 714: args = " #{args}" unless args.empty? 715: args += " => #{name[1]}" if name 716: 717: "rescue#{args}\n#{indent body}" 718: end
# File lib/ruby2ruby.rb, line 720 720: def process_rescue exp 721: body = process(exp.shift) unless exp.first.first == :resbody 722: els = process(exp.pop) unless exp.last.first == :resbody 723: 724: body ||= "# do nothing" 725: simple = exp.size == 1 && !exp.resbody.block 726: 727: 728: resbodies = [] 729: until exp.empty? do 730: resbody = exp.shift 731: simple &&= resbody[1] == s(:array) 732: simple &&= resbody[2] != nil && resbody[2].node_type != :block 733: resbodies << process(resbody) 734: end 735: 736: if els then 737: "#{indent body}\n#{resbodies.join("\n")}\nelse\n#{indent els}" 738: elsif simple then 739: resbody = resbodies.first.sub(/\n\s*/, ' ') 740: "#{body} #{resbody}" 741: else 742: "#{indent body}\n#{resbodies.join("\n")}" 743: end 744: end
# File lib/ruby2ruby.rb, line 746 746: def process_retry(exp) 747: "retry" 748: end
# File lib/ruby2ruby.rb, line 750 750: def process_return(exp) 751: # HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}") 752: 753: if exp.empty? then 754: return "return" 755: else 756: return "return #{process exp.shift}" 757: end 758: end
# File lib/ruby2ruby.rb, line 760 760: def process_sclass(exp) 761: "class << #{process(exp.shift)}\n#{indent(process(exp.shift))}\nend" 762: end
# File lib/ruby2ruby.rb, line 764 764: def process_scope(exp) 765: exp.empty? ? "" : process(exp.shift) 766: end
# File lib/ruby2ruby.rb, line 768 768: def process_self(exp) 769: "self" 770: end
# File lib/ruby2ruby.rb, line 772 772: def process_splat(exp) 773: if exp.empty? then 774: "*" 775: else 776: "*#{process(exp.shift)}" 777: end 778: end
# File lib/ruby2ruby.rb, line 780 780: def process_str(exp) 781: return exp.shift.dump 782: end
# File lib/ruby2ruby.rb, line 784 784: def process_super(exp) 785: args = [] 786: until exp.empty? do 787: args << process(exp.shift) 788: end 789: 790: "super(#{args.join(', ')})" 791: end
# File lib/ruby2ruby.rb, line 793 793: def process_svalue(exp) 794: code = [] 795: until exp.empty? do 796: code << process(exp.shift) 797: end 798: code.join(", ") 799: end
# File lib/ruby2ruby.rb, line 801 801: def process_to_ary(exp) 802: process(exp.shift) 803: end
# File lib/ruby2ruby.rb, line 805 805: def process_true(exp) 806: "true" 807: end
# File lib/ruby2ruby.rb, line 809 809: def process_undef(exp) 810: "undef #{process(exp.shift)}" 811: end
# File lib/ruby2ruby.rb, line 813 813: def process_until(exp) 814: cond_loop(exp, 'until') 815: end
# File lib/ruby2ruby.rb, line 817 817: def process_valias(exp) 818: "alias #{exp.shift} #{exp.shift}" 819: end
# File lib/ruby2ruby.rb, line 821 821: def process_when(exp) 822: src = [] 823: 824: if self.context[1] == :array then # ugh. matz! why not an argscat?!? 825: val = process(exp.shift) 826: exp.shift # empty body 827: return "*#{val}" 828: end 829: 830: until exp.empty? 831: cond = process(exp.shift).to_s[1..2] 832: code = indent(process(exp.shift)) 833: code = indent "# do nothing" if code =~ /\A\s*\Z/ 834: src << "when #{cond} then\n#{code.chomp}" 835: end 836: 837: src.join("\n") 838: end
# File lib/ruby2ruby.rb, line 840 840: def process_while(exp) 841: cond_loop(exp, 'while') 842: end
# File lib/ruby2ruby.rb, line 844 844: def process_xstr(exp) 845: "`#{process_str(exp)[1..-2]}`" 846: end
# File lib/ruby2ruby.rb, line 848 848: def process_yield(exp) 849: args = [] 850: until exp.empty? do 851: args << process(exp.shift) 852: end 853: 854: unless args.empty? then 855: "yield(#{args.join(', ')})" 856: else 857: "yield" 858: end 859: end
# File lib/ruby2ruby.rb, line 861 861: def process_zsuper(exp) 862: "super" 863: end
# File lib/ruby2ruby.rb, line 371 371: def re_opt options 372: bits = (0..8).map { |n| options[n] * 2**n } 373: bits.delete 0 374: bits.map { |n| Regexp::CODES[n] }.join 375: end
Rewriters:
# File lib/ruby2ruby.rb, line 888 888: def rewrite_attrasgn exp 889: if context.first(2) == [:array, :masgn] then 890: exp[0] = :call 891: exp[2] = exp[2].to_s.sub(/=$/, '').to_sym 892: end 893: 894: exp 895: end
# File lib/ruby2ruby.rb, line 897 897: def rewrite_ensure exp 898: exp = s(:begin, exp) unless context.first == :begin 899: exp 900: end
# File lib/ruby2ruby.rb, line 902 902: def rewrite_resbody exp 903: raise "no exception list in #{exp.inspect}" unless exp.size > 2 && exp[1] 904: raise exp[1].inspect if exp[1][0] != :array 905: # for now, do nothing, just check and freak if we see an errant structure 906: exp 907: end
# File lib/ruby2ruby.rb, line 909 909: def rewrite_rescue exp 910: complex = false 911: complex ||= exp.size > 3 912: complex ||= exp.resbody.block 913: complex ||= exp.find_nodes(:resbody).any? { |n| n[1] != s(:array) } 914: complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? } 915: complex ||= exp.find_nodes(:resbody).any? { |n| n[2] and n[2].node_type == :block } 916: 917: handled = context.first == :ensure 918: 919: exp = s(:begin, exp) if complex unless handled 920: 921: exp 922: end
# File lib/ruby2ruby.rb, line 924 924: def rewrite_svalue(exp) 925: case exp.last.first 926: when :array 927: s(:svalue, *exp[1][1..1]) 928: when :splat 929: exp 930: else 931: raise "huh: #{exp.inspect}" 932: end 933: end
# File lib/ruby2ruby.rb, line 571 571: def splat(sym) 572: :"*#{sym}" 573: end
# File lib/ruby2ruby.rb, line 952 952: def util_dthing(type, exp) 953: s = [] 954: 955: # first item in sexp is a string literal 956: s << dthing_escape(type, exp.shift) 957: 958: until exp.empty? 959: pt = exp.shift 960: case pt 961: when Sexp then 962: case pt.first 963: when :str then 964: s << dthing_escape(type, pt.last) 965: when :evstr then 966: s << '#{' << process(pt) << '}' # do not use interpolation here 967: else 968: raise "unknown type: #{pt.inspect}" 969: end 970: else 971: raise "unhandled value in d-thing: #{pt.inspect}" 972: end 973: end 974: 975: s.join 976: end
# File lib/ruby2ruby.rb, line 978 978: def util_module_or_class(exp, is_class=false) 979: result = [] 980: 981: name = exp.shift 982: name = process name if Sexp === name 983: 984: result << name 985: 986: if is_class then 987: superk = process(exp.shift) 988: result << " < #{superk}" if superk 989: end 990: 991: result << "\n" 992: 993: body = [] 994: begin 995: code = process(exp.shift) 996: body << code.chomp unless code.nil? or code.chomp.empty? 997: end until exp.empty? 998: 999: unless body.empty? then 1000: body = indent(body.join("\n\n")) + "\n" 1001: else 1002: body = "" 1003: end 1004: result << body 1005: result << "end" 1006: 1007: result.join 1008: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.