1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
|
#!/usr/bin/env ruby -w
begin require 'rubygems'; rescue LoadError; end
require 'parse_tree'
require 'sexp_processor'
require 'unified_ruby'
class Ruby2Ruby < SexpProcessor
include UnifiedRuby
VERSION = '1.1.8'
LINE_LENGTH = 78
##
# Nodes that represent assignment and probably need () around them.
ASSIGN_NODES = [
:dasgn,
:flip2,
:flip3,
:lasgn,
:masgn,
:op_asgn1,
:op_asgn2,
:op_asgn_and,
:op_asgn_or,
:return,
]
def self.translate(klass_or_str, method = nil)
self.new.process(ParseTree.translate(klass_or_str, method))
end
def initialize
super
@indent = " "
self.auto_shift_type = true
self.strict = true
self.expected = String
# self.debug[:defn] = /zsuper/
end
def process exp
exp = Sexp.from_array(exp) if Array === exp unless Sexp === exp
super exp
end
############################################################
# Processors
def process_alias(exp)
"alias_method #{process(exp.shift)}, #{process(exp.shift)}"
end
def process_and(exp)
"(#{process exp.shift} and #{process exp.shift})"
end
def process_args(exp)
args = []
until exp.empty? do
arg = exp.shift
case arg
when Symbol then
args << arg
when Array then
case arg.first
when :block then
asgns = {}
arg[1..-1].each do |lasgn|
asgns[lasgn[1]] = process(lasgn)
end
args.each_with_index do |name, index|
args[index] = asgns[name] if asgns.has_key? name
end
when :block_arg then
args << "&#{arg.last}"
else
raise "unknown arg type #{arg.first.inspect}"
end
else
raise "unknown arg type #{arg.inspect}"
end
end
return "(#{args.join ', '})"
end
def process_arglist(exp) # custom made node
code = []
until exp.empty? do
code << process(exp.shift)
end
code.join ', '
end
def process_argscat(exp)
args = []
ary = exp.shift
ary.shift # :array
until ary.empty? do
args << process(ary.shift)
end
args << "*#{process(exp.shift)}"
args << process(exp.shift) unless exp.empty? # optional block arg
args.join ', '
end
def process_argspush(exp)
process_arglist(exp)
end
def process_array(exp)
"[#{process_arglist(exp)}]"
end
def process_attrasgn(exp)
receiver = process exp.shift
name = exp.shift
args = exp.empty? ? nil : exp.shift
case name
when :[]= then
rhs = process args.pop
args[0] = :arglist if args[0] == :array
"#{receiver}[#{process(args)}] = #{rhs}"
else
if args then
"#{receiver}.#{name.to_s[0..-2]} = #{process(args)[1..-2]}"
else
"#{receiver}.#{name.to_s[0..-2]}"
end
end
end
def process_back_ref(exp)
"$#{exp.shift}"
end
def process_begin(exp)
is_rescue = exp.first.first == :rescue rescue false
code = []
code << "begin"
until exp.empty?
src = process(exp.shift)
src = indent(src) unless src =~ /(^|\n)rescue/ # ensures no level 0 rescues
code << src
end
code << "end" unless is_rescue
return code.join("\n")
end
def process_block(exp)
result = []
exp << nil if exp.empty?
until exp.empty? do
code = exp.shift
if code.nil? or code.first == :nil then
result << "# do nothing"
else
result << process(code)
end
end
result = result.join "\n"
result = case self.context[1]
when nil, :scope, :if, :iter, :resbody, :when, :while then
result + "\n"
else
"(#{result})"
end
return result
end
def process_block_arg(exp)
"&#{exp.shift}"
end
def process_block_pass(exp)
bname = s(:block_arg, process(exp.shift)) # FIX
call = exp.shift
if Array === call.last then # HACK - I _really_ need rewrites to happen first
case call.last.first
when :splat then
call << [:array, call.pop]
when :array then
# do nothing
else
has_args = Array === call.last and call.last.first == :array
call << [:array] unless has_args
end
call.last << bname
else
call << [:array, bname]
end
process(call)
end
def process_break(exp)
val = exp.empty? ? nil : process(exp.shift)
# HACK "break" + (val ? " #{val}" : "")
if val then
"break #{val}"
else
"break"
end
end
def process_call(exp)
receiver_node_type = exp.first.nil? ? nil : exp.first.first
receiver = process exp.shift
receiver = "(#{receiver})" if
Ruby2Ruby::ASSIGN_NODES.include? receiver_node_type
name = exp.shift
args_exp = exp.shift rescue nil
if args_exp && args_exp.first == :array # FIX
args = "#{process(args_exp)[1..-2]}"
else
args = process args_exp
args = nil if args.empty?
end
case name
when :<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :** then
"(#{receiver} #{name} #{args})"
when :[] then
"#{receiver}[#{args}]"
when :"-@" then
"-#{receiver}"
when :"+@" then
"+#{receiver}"
else
unless receiver.nil? then
"#{receiver}.#{name}#{args ? "(#{args})" : args}"
else
"#{name}#{args ? "(#{args})" : args}"
end
end
end
def process_case(exp)
result = []
expr = process exp.shift
if expr then
result << "case #{expr}"
else
result << "case"
end
until exp.empty?
pt = exp.shift
if pt and pt.first == :when
result << "#{process(pt)}"
else
code = indent(process(pt))
code = indent("# do nothing") if code =~ /^\s*$/
result << "else\n#{code}"
end
end
result << "end"
result.join("\n")
end
def process_cdecl(exp)
"#{exp.shift} = #{process(exp.shift)}"
end
def process_class(exp)
"class #{util_module_or_class(exp, true)}"
end
def process_colon2(exp)
"#{process(exp.shift)}::#{exp.shift}"
end
def process_colon3(exp)
"::#{exp.shift}"
end
def process_const(exp)
exp.shift.to_s
end
def process_cvar(exp)
"#{exp.shift}"
end
def process_cvasgn(exp)
"#{exp.shift} = #{process(exp.shift)}"
end
def process_cvdecl(exp)
"#{exp.shift} = #{process(exp.shift)}"
end
# (a, lit1) => "a = 1"
# (a, (b, lit2)) => "a = b = 2"
# (a, (b)) => ""
def process_dasgn_curr(exp)
lhs = exp.shift.to_s
rhs = (exp.empty? ? nil : exp.shift)
if rhs.nil? then
if self.context[1] == :block then
return ''
end
return lhs
end
return "#{lhs} = #{process rhs}" unless rhs.first == :dasgn_curr
# keep recursing. ensure that the leaf node assigns to _something_
"#{lhs} = #{process rhs}"
end
def process_dasgn(exp)
if exp.size == 1 then
exp.shift.to_s
else
"#{exp.shift} = #{process(exp.shift)}"
end
end
def process_defined(exp)
"defined? #{process(exp.shift)}"
end
def process_defn(exp)
type1 = exp[1].first
type2 = exp[2].first rescue nil
if type1 == :args and [:ivar, :attrset].include? type2 then
name = exp.shift
case type2
when :ivar then
exp.clear
return "attr_reader #{name.inspect}"
when :attrset then
exp.clear
return "attr_writer :#{name.to_s[0..-2]}"
else
raise "Unknown defn type: #{exp.inspect}"
end
end
case type1
when :scope, :args then
name = exp.shift
args = process(exp.shift)
args = "" if args == "()"
body = indent(process(exp.shift))
return "def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n")
else
raise "Unknown defn type: #{type1} for #{exp.inspect}"
end
end
def process_defs(exp)
exp.unshift "#{process(exp.shift)}.#{exp.shift}"
process_defn(exp)
end
def process_dot2(exp)
"(#{process exp.shift}..#{process exp.shift})"
end
def process_dot3(exp)
"(#{process exp.shift}...#{process exp.shift})"
end
def process_dregx(exp)
"/" << util_dthing(exp, true) << "/"
end
def process_dregx_once(exp)
process_dregx(exp) + "o"
end
def process_dstr(exp)
"\"#{util_dthing(exp)}\""
end
def process_dsym(exp)
":#{process_dstr(exp)}"
end
def process_dvar(exp)
exp.shift.to_s
end
def process_dxstr(exp)
"`#{process_dstr(exp)[1..-2]}`"
end
def process_ensure(exp)
body = process exp.shift
ens = process exp.shift
return "#{body}\nensure\n#{indent ens}"
end
def process_evstr(exp)
process exp.shift
end
def process_false(exp)
"false"
end
# TODO: remove for unified
def process_fcall(exp)
recv = exp.shift unless Symbol === exp.first # HACK conditional - some not getting rewritten?
name = exp.shift.to_s
args = exp.shift
code = []
unless args.nil? then
args[0] = :arglist if args.first == :array
code << process(args)
end
return code.empty? ? name : "#{name}(#{code.join(', ')})"
end
def process_flip2(exp)
"#{process(exp.shift)}..#{process(exp.shift)}"
end
def process_flip3(exp)
"#{process(exp.shift)}...#{process(exp.shift)}"
end
def process_for(exp)
recv = process exp.shift
iter = process exp.shift
body = exp.empty? ? nil : process(exp.shift)
result = ["for #{iter} in #{recv} do"]
result << indent(body ? body : "# do nothing")
result << "end"
result.join("\n")
end
def process_gasgn(exp)
process_iasgn(exp)
end
def process_gvar(exp)
return exp.shift.to_s
end
def process_hash(exp)
result = []
until exp.empty?
result << "#{process(exp.shift)} => #{process(exp.shift)}"
end
case self.context[1]
when :arglist, :argscat then
return "#{result.join(', ')}" # HACK - this will break w/ 2 hashes as args
else
return "{ #{result.join(', ')} }"
end
end
def process_iasgn(exp)
lhs = exp.shift
if exp.empty? then # part of an masgn
lhs.to_s
else
"#{lhs} = #{process exp.shift}"
end
end
def process_if(exp)
expand = Ruby2Ruby::ASSIGN_NODES.include? exp.first.first
c = process exp.shift
t = process exp.shift
f = process exp.shift
c = "(#{c.chomp})" if c =~ /\n/
if t then
unless expand then
if f then
r = "#{c} ? (#{t}) : (#{f})"
r = nil if r =~ /return/ # HACK - need contextual awareness or something
else
r = "#{t} if #{c}"
end
return r if r and (@indent+r).size < LINE_LENGTH and r !~ /\n/
end
r = "if #{c} then\n#{indent(t)}\n"
r << "else\n#{indent(f)}\n" if f
r << "end"
r
else
unless expand then
r = "#{f} unless #{c}"
return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/
end
"unless #{c} then\n#{indent(f)}\nend"
end
end
def process_iter(exp)
iter = process exp.shift
args = exp.shift
args = (args == 0) ? '' : process(args)
body = exp.empty? ? nil : process(exp.shift)
b, e = if iter == "END" then
[ "{", "}" ]
else
[ "do", "end" ]
end
iter.sub!(/\(\)$/, '')
# REFACTOR: ugh
result = []
result << "#{iter} {"
result << " |#{args}|" if args
if body then
result << " #{body.strip} "
else
result << ' '
end
result << "}"
result = result.join
return result if result !~ /\n/ and result.size < LINE_LENGTH
result = []
result << "#{iter} #{b}"
result << " |#{args}|" if args
result << "\n"
result << indent(body.strip)
result << "\n"
result << e
result.join
end
def process_ivar(exp)
exp.shift.to_s
end
def process_lasgn(exp)
s = "#{exp.shift}"
s += " = #{process exp.shift}" unless exp.empty?
s
end
def process_lit(exp)
obj = exp.shift
case obj
when Range then
"(#{obj.inspect})"
else
obj.inspect
end
end
def process_lvar(exp)
exp.shift.to_s
end
def splat(sym)
:"*#{sym}"
end
def process_masgn(exp)
lhs = exp.shift
rhs = exp.empty? ? nil : exp.shift
unless exp.empty? then
rhs[-1] = splat(rhs[-1]) unless rhs == s(:splat)
lhs << rhs
rhs = exp.shift
end
case lhs.first
when :array then
lhs.shift
lhs = lhs.map do |l|
case l.first
when :masgn then
"(#{process(l)})"
else
process(l)
end
end
when :dasgn_curr then
lhs = [ splat(lhs.last) ]
when :splat then
lhs = [ :"*" ]
else
raise "no clue: #{lhs.inspect}"
end
if context[1] == :iter and rhs then
lhs << splat(rhs.last)
rhs = nil
end
unless rhs.nil? then
t = rhs.first
rhs = if t == :argscat then
rhs.shift
process_argscat(rhs)
else
r = process(rhs)
r = r[1..-2] if t != :to_ary
r
end
return "#{lhs.join(", ")} = #{rhs}"
else
return lhs.join(", ")
end
end
def process_match(exp)
"#{process(exp.shift)}"
end
def process_match2(exp)
lhs = process(exp.shift)
rhs = process(exp.shift)
"#{lhs} =~ #{rhs}"
end
def process_match3(exp)
rhs = process(exp.shift)
lhs = process(exp.shift)
"#{lhs} =~ #{rhs}"
end
def process_module(exp)
"module #{util_module_or_class(exp)}"
end
def process_next(exp)
val = exp.empty? ? nil : process(exp.shift)
if val then
"next #{val}"
else
"next"
end
end
def process_nil(exp)
"nil"
end
def process_not(exp)
"(not #{process exp.shift})"
end
def process_nth_ref(exp)
"$#{exp.shift}"
end
def process_op_asgn1(exp)
# [[:lvar, :b], [:array, [:lit, 1]], :"||", [:lit, 10]]
lhs = process(exp.shift)
index = process(exp.shift)
msg = exp.shift
rhs = process(exp.shift)
"#{lhs}#{index} #{msg}= #{rhs}"
end
def process_op_asgn2(exp)
# [[:lvar, :c], :var=, :"||", [:lit, 20]]
lhs = process(exp.shift)
index = exp.shift.to_s[0..-2]
msg = exp.shift
rhs = process(exp.shift)
"#{lhs}.#{index} #{msg}= #{rhs}"
end
def process_op_asgn_or(exp)
# a ||= 1
# [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
exp.shift
process(exp.shift).sub(/=/, '||=')
end
def process_op_asgn_and(exp)
# a &&= 1
# [[:lvar, :a], [:lasgn, :a, [:lit, 1]]]
exp.shift
process(exp.shift).sub(/=/, '&&=')
end
def process_or(exp)
"(#{process exp.shift} or #{process exp.shift})"
end
def process_postexe(exp)
"END"
end
def process_redo(exp)
"redo"
end
def process_resbody(exp) # TODO: rewrite this fucker
code = []
sexp = exp
until exp.empty? and (sexp.nil? or sexp.empty?)
list = sexp.shift
body = sexp.shift
var = if list and
list.size > 1 and
[:lasgn, :dasgn, :dasgn_curr].include? list.last.first then
list.pop[1]
else
nil
end
# FIX: omg this is horrid. I should be punished
var = body.delete_at(1)[1] if
[:dasgn_curr, :dasgn].include? body[1][0] unless
var or body.nil? rescue nil
if list and list.size > 1 then
list[0] = :arglist
code << "rescue #{process(list)}"
else
code << "rescue"
end
code.last << " => #{var}" if var
if body then
code << indent(process(body)).chomp
else
code << indent("# do nothing")
end
unless exp.empty? then
sexp = exp.shift
assert_type sexp, :resbody
sexp.shift
end
end
code.join("\n")
end
def process_rescue(exp)
# TODO: rewrite this
#
# a = b rescue c => [lasgn a [rescue b c]]
# begin; a = b; rescue c => [begin [rescue [lasgn a b] c]]
current = self.context[1]
case current
when :begin, :ensure, :block then
body = (exp.first.first == :resbody) ? nil : process(exp.shift)
resbody = exp.empty? ? '' : process(exp.shift)
els = exp.empty? ? nil : process(exp.shift)
code = []
code << indent(body) if body
code << resbody
if els then
code << "else"
code << indent(els)
else
unless [:block].include? current then
code << "end\n" unless current == :ensure
else
r = [body, resbody.gsub(/rescue\n\s+/, 'rescue ')].compact.join(' ')
code = [r] if (@indent+r).size < LINE_LENGTH and r !~ /\n/
end
end
code.join("\n").chomp
else # a rescue b and others
body = process exp.shift
assert_type exp.first, :resbody
resbody = exp.shift
resbody.shift # resbody
resbody.shift # nil (no types for expression form)
resbody = resbody.shift # actual code
resbody = process resbody
code = "#{body} rescue #{resbody}"
case current
when :hash then # HACK move to process_hash
"(#{code})"
else
code
end
end
end
def process_retry(exp)
"retry"
end
def process_return(exp)
# HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}")
if exp.empty? then
return "return"
else
return "return #{process exp.shift}"
end
end
def process_sclass(exp)
"class << #{process(exp.shift)}\n#{indent(process(exp.shift))}\nend"
end
def process_scope(exp)
exp.empty? ? "" : process(exp.shift)
end
def process_self(exp)
"self"
end
def process_splat(exp)
if exp.empty? then
"*"
else
"*#{process(exp.shift)}"
end
end
def process_str(exp)
return exp.shift.dump
end
def process_super(exp)
args = exp.shift
args[0] = :arglist if args[0] == :array
"super(#{process(args)})"
end
def process_svalue(exp)
process(exp.shift)
end
def process_to_ary(exp)
process(exp.shift)
end
def process_true(exp)
"true"
end
def process_undef(exp)
"undef #{process(exp.shift)}"
end
def process_until(exp)
cond_loop(exp, 'until')
end
def process_valias(exp)
"alias #{exp.shift} #{exp.shift}"
end
def process_when(exp)
src = []
if self.context[1] == :array then # ugh. matz! why not an argscat?!?
val = process(exp.shift)
exp.shift # empty body
return "*#{val}"
end
until exp.empty?
cond = process(exp.shift).to_s[1..-2]
code = indent(process(exp.shift))
code = indent "# do nothing" if code =~ /\A\s*\Z/
src << "when #{cond} then\n#{code.chomp}"
end
src.join("\n")
end
def process_while(exp)
cond_loop(exp, 'while')
end
def process_xstr(exp)
"`#{process_str(exp)[1..-2]}`"
end
def process_yield(exp)
args = exp.empty? ? nil : exp.shift
if args then
args[0] = :arglist if args.first == :array
args = process(args)
end
# "yield" + (args ? "(#{args})" : "")
if args then
"yield(#{args})"
else
"yield"
end
end
def process_zarray(exp)
"[]"
end
def process_zsuper(exp)
"super"
end
def cond_loop(exp, name)
cond = process(exp.shift)
body = process(exp.shift)
head_controlled = exp.shift
body = indent(body).chomp if body
code = []
if head_controlled then
code << "#{name} #{cond} do"
code << body if body
code << "end"
else
code << "begin"
code << body if body
code << "end #{name} #{cond}"
end
code.join("\n")
end
############################################################
# Rewriters:
def rewrite_rescue exp
exp = s(:begin, exp) if
context[1] == :block unless
context[2] == :scope and [:defn, :defs].include? context[3]
exp
end
############################################################
# Utility Methods:
def util_dthing(exp, regx = false)
s = []
suck = true
if suck then
x = exp.shift.gsub(/"/, '\"').gsub(/\n/, '\n')
else
x = exp.shift.dump[1..-2]
end
x.gsub!(/\//, '\/') if regx
s << x
until exp.empty?
pt = exp.shift
case pt
when Sexp then
case pt.first
when :str then
if suck then
x = pt.last.gsub(/"/, '\"').gsub(/\n/, '\n')
else
x = pt.last.dump[1..-2]
end
x.gsub!(/\//, '\/') if regx
s << x
else
s << '#{' << process(pt) << '}' # do not use interpolation here
end
else
# do nothing - yet
end
end
s.join
end
def util_module_or_class(exp, is_class=false)
s = "#{exp.shift}"
if is_class then
superk = process(exp.shift)
s << " < #{superk}" if superk
end
s << "\n"
body = []
begin
code = process(exp.shift).chomp
body << code unless code.nil? or code.empty?
end until exp.empty?
unless body.empty? then
body = indent(body.join("\n\n")) + "\n"
else
body = ""
end
s + body + "end"
end
def indent(s)
s.to_s.map{|line| @indent + line}.join
end
end
RubyToRuby = Ruby2Ruby # For backwards compatibilty... TODO: remove 2008-03-28
class Method
def with_class_and_method_name
if self.inspect =~ /<Method: (.*)\#(.*)>/ then
klass = eval $1
method = $2.intern
raise "Couldn't determine class from #{self.inspect}" if klass.nil?
return yield(klass, method)
else
raise "Can't parse signature: #{self.inspect}"
end
end
def to_sexp
with_class_and_method_name do |klass, method|
ParseTree.new(false).parse_tree_for_method(klass, method)
end
end
def to_ruby
Ruby2Ruby.new.process(self.to_sexp)
end
end
class ProcStoreTmp
@@n = 0
def self.name
@@n += 1
return :"myproc#{@@n}"
end
end
class UnboundMethod
def to_ruby
name = ProcStoreTmp.name
ProcStoreTmp.send(:define_method, name, self)
m = ProcStoreTmp.new.method(name)
result = m.to_ruby.sub(/def #{name}(?:\(([^\)]*)\))?/,
'proc { |\1|').sub(/end\Z/, '}')
return result
end
end
class Proc
def to_method
name = ProcStoreTmp.name
ProcStoreTmp.send(:define_method, name, self)
ProcStoreTmp.new.method(name)
end
def to_sexp
body = self.to_method.to_sexp[2][1..-1]
[:proc, *body]
end
def to_ruby
ruby = self.to_method.to_ruby
ruby.sub!(/\A(def \S+)\(([^\)]*)\)/, '\1 |\2|') # move args
ruby.sub!(/\Adef[^\n\|]+/, 'proc { ') # strip def name
ruby.sub!(/end\Z/, '}') # strip end
ruby.gsub!(/\s+$/, '') # trailing WS bugs me
ruby
end
end
|