Class: Ruby2Ruby
- Inherits:
-
SexpProcessor
- Object
- SexpProcessor
- Ruby2Ruby
- Defined in:
- lib/ruby2ruby.rb
Overview
Generate ruby code from a sexp.
Constant Summary collapse
- VERSION =
:nodoc:
"2.3.0"- LINE_LENGTH =
cutoff for one-liners
78- BINARY =
binary operation messages
[:<=>, :==, :<, :>, :<=, :>=, :-, :+, :*, :/, :%, :<<, :>>, :**, :'!=']
- ASSIGN_NODES =
Nodes that represent assignment and probably need () around them.
TODO: this should be replaced with full precedence support :/
[ :dasgn, :flip2, :flip3, :lasgn, :masgn, :attrasgn, :op_asgn1, :op_asgn2, :op_asgn_and, :op_asgn_or, :return, :if, # HACK :rescue, ]
- HASH_VAL_NO_PAREN =
Some sexp types are OK without parens when appearing as hash values. This list can include ‘:call`s because they’re always printed with parens around their arguments. For example:
{ :foo => (("baz")) } # The outer parens are unnecessary { :foo => ("baz") } # This is the normal code style [ :call, :false, :lit, :lvar, :nil, :str, :true ]
Instance Method Summary collapse
-
#cond_loop(exp, name) ⇒ Object
Generate a post-or-pre conditional loop.
-
#dthing_escape(type, lit) ⇒ Object
Utility method to escape something interpolated.
-
#finish(exp) ⇒ Object
Process all the remaining stuff in
expand return the results sans-nils. -
#indent(s) ⇒ Object
Indent all lines of
sto the current indent level. -
#initialize ⇒ Ruby2Ruby
constructor
:nodoc:.
-
#parenthesize(exp) ⇒ Object
Wrap appropriate expressions in matching parens.
-
#process_alias(exp) ⇒ Object
Processors.
-
#process_and(exp) ⇒ Object
:nodoc:.
-
#process_arglist(exp) ⇒ Object
custom made node # :nodoc:.
-
#process_args(exp) ⇒ Object
:nodoc:.
-
#process_array(exp) ⇒ Object
:nodoc:.
-
#process_attrasgn(exp) ⇒ Object
:nodoc:.
-
#process_back_ref(exp) ⇒ Object
:nodoc:.
-
#process_begin(exp) ⇒ Object
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin.
-
#process_block(exp) ⇒ Object
:nodoc:.
-
#process_block_pass(exp) ⇒ Object
:nodoc:.
-
#process_break(exp) ⇒ Object
:nodoc:.
-
#process_call(exp, safe_call = false) ⇒ Object
:nodoc:.
-
#process_case(exp) ⇒ Object
:nodoc:.
-
#process_cdecl(exp) ⇒ Object
:nodoc:.
-
#process_class(exp) ⇒ Object
:nodoc:.
-
#process_colon2(exp) ⇒ Object
:nodoc:.
-
#process_colon3(exp) ⇒ Object
:nodoc:.
-
#process_const(exp) ⇒ Object
:nodoc:.
-
#process_cvar(exp) ⇒ Object
:nodoc:.
-
#process_cvasgn(exp) ⇒ Object
:nodoc:.
-
#process_cvdecl(exp) ⇒ Object
:nodoc:.
-
#process_defined(exp) ⇒ Object
:nodoc:.
-
#process_defn(exp) ⇒ Object
:nodoc:.
-
#process_defs(exp) ⇒ Object
:nodoc:.
-
#process_dot2(exp) ⇒ Object
:nodoc:.
-
#process_dot3(exp) ⇒ Object
:nodoc:.
-
#process_dregx(exp) ⇒ Object
:nodoc:.
-
#process_dregx_once(exp) ⇒ Object
:nodoc:.
-
#process_dstr(exp) ⇒ Object
:nodoc:.
-
#process_dsym(exp) ⇒ Object
:nodoc:.
-
#process_dxstr(exp) ⇒ Object
:nodoc:.
-
#process_ensure(exp) ⇒ Object
:nodoc:.
-
#process_evstr(exp) ⇒ Object
:nodoc:.
-
#process_false(exp) ⇒ Object
:nodoc:.
-
#process_flip2(exp) ⇒ Object
:nodoc:.
-
#process_flip3(exp) ⇒ Object
:nodoc:.
-
#process_for(exp) ⇒ Object
:nodoc:.
-
#process_gasgn(exp) ⇒ Object
:nodoc:.
-
#process_gvar(exp) ⇒ Object
:nodoc:.
-
#process_hash(exp) ⇒ Object
:nodoc:.
-
#process_iasgn(exp) ⇒ Object
:nodoc:.
-
#process_if(exp) ⇒ Object
:nodoc:.
-
#process_iter(exp) ⇒ Object
:nodoc:.
-
#process_ivar(exp) ⇒ Object
:nodoc:.
- #process_kwsplat(exp) ⇒ Object
-
#process_lasgn(exp) ⇒ Object
:nodoc:.
-
#process_lit(exp) ⇒ Object
:nodoc:.
-
#process_lvar(exp) ⇒ Object
:nodoc:.
-
#process_masgn(exp) ⇒ Object
:nodoc:.
-
#process_match(exp) ⇒ Object
:nodoc:.
-
#process_match2(exp) ⇒ Object
:nodoc:.
-
#process_match3(exp) ⇒ Object
:nodoc:.
-
#process_module(exp) ⇒ Object
:nodoc:.
-
#process_next(exp) ⇒ Object
:nodoc:.
-
#process_nil(exp) ⇒ Object
:nodoc:.
-
#process_not(exp) ⇒ Object
:nodoc:.
-
#process_nth_ref(exp) ⇒ Object
:nodoc:.
-
#process_op_asgn1(exp) ⇒ Object
:nodoc:.
-
#process_op_asgn2(exp) ⇒ Object
:nodoc:.
-
#process_op_asgn_and(exp) ⇒ Object
:nodoc:.
-
#process_op_asgn_or(exp) ⇒ Object
:nodoc:.
-
#process_or(exp) ⇒ Object
:nodoc:.
-
#process_postexe(exp) ⇒ Object
:nodoc:.
-
#process_redo(exp) ⇒ Object
:nodoc:.
-
#process_resbody(exp) ⇒ Object
:nodoc:.
-
#process_rescue(exp) ⇒ Object
:nodoc:.
-
#process_retry(exp) ⇒ Object
:nodoc:.
-
#process_return(exp) ⇒ Object
:nodoc:.
-
#process_safe_attrasgn(exp) ⇒ Object
:nodoc:.
-
#process_safe_call(exp) ⇒ Object
:nodoc:.
-
#process_sclass(exp) ⇒ Object
:nodoc:.
-
#process_self(exp) ⇒ Object
:nodoc:.
-
#process_splat(exp) ⇒ Object
:nodoc:.
-
#process_str(exp) ⇒ Object
:nodoc:.
-
#process_super(exp) ⇒ Object
:nodoc:.
-
#process_svalue(exp) ⇒ Object
:nodoc:.
-
#process_to_ary(exp) ⇒ Object
:nodoc:.
-
#process_true(exp) ⇒ Object
:nodoc:.
-
#process_undef(exp) ⇒ Object
:nodoc:.
-
#process_until(exp) ⇒ Object
:nodoc:.
-
#process_valias(exp) ⇒ Object
:nodoc:.
-
#process_when(exp) ⇒ Object
:nodoc:.
-
#process_while(exp) ⇒ Object
:nodoc:.
-
#process_xstr(exp) ⇒ Object
:nodoc:.
-
#process_yield(exp) ⇒ Object
:nodoc:.
-
#process_zsuper(exp) ⇒ Object
:nodoc:.
-
#re_opt(options) ⇒ Object
Return the appropriate regexp flags for a given numeric code.
-
#rewrite_attrasgn(exp) ⇒ Object
Rewriters:.
-
#rewrite_ensure(exp) ⇒ Object
:nodoc:.
-
#rewrite_resbody(exp) ⇒ Object
:nodoc:.
-
#rewrite_rescue(exp) ⇒ Object
:nodoc:.
-
#rewrite_svalue(exp) ⇒ Object
:nodoc:.
-
#splat(sym) ⇒ Object
Return a splatted symbol for
sym. -
#util_dthing(type, exp) ⇒ Object
Utility method to generate something interpolated.
-
#util_module_or_class(exp, is_class = false) ⇒ Object
Utility method to generate ether a module or class.
Constructor Details
#initialize ⇒ Ruby2Ruby
:nodoc:
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/ruby2ruby.rb', line 81 def initialize # :nodoc: super @indent = " " self.auto_shift_type = true self.strict = true self.expected = String @calls = [] # self.debug[:defn] = /zsuper/ end |
Instance Method Details
#cond_loop(exp, name) ⇒ Object
Generate a post-or-pre conditional loop.
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 |
# File 'lib/ruby2ruby.rb', line 1012 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 |
#dthing_escape(type, lit) ⇒ Object
Utility method to escape something interpolated.
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
# File 'lib/ruby2ruby.rb', line 1035 def dthing_escape type, lit lit = lit.gsub(/\n/, '\n') case type when :dregx then lit.gsub(/(\A|[^\\])\//, '\1\/') when :dstr, :dsym then lit.gsub(/"/, '\"') when :dxstr then lit.gsub(/`/, '\`') else raise "unsupported type #{type.inspect}" end end |
#finish(exp) ⇒ Object
Process all the remaining stuff in exp and return the results sans-nils.
1053 1054 1055 1056 1057 1058 1059 |
# File 'lib/ruby2ruby.rb', line 1053 def finish exp # REFACTOR: work this out of the rest of the processors body = [] until exp.empty? do body << process(exp.shift) end body.compact end |
#indent(s) ⇒ Object
Indent all lines of s to the current indent level.
1064 1065 1066 |
# File 'lib/ruby2ruby.rb', line 1064 def indent(s) s.to_s.split(/\n/).map{|line| @indent + line}.join("\n") end |
#parenthesize(exp) ⇒ Object
Wrap appropriate expressions in matching parens.
1071 1072 1073 1074 1075 1076 1077 1078 |
# File 'lib/ruby2ruby.rb', line 1071 def parenthesize exp case self.context[1] when nil, :defn, :defs, :class, :sclass, :if, :iter, :resbody, :when, :while then exp else "(#{exp})" end end |
#process_alias(exp) ⇒ Object
Processors
96 97 98 |
# File 'lib/ruby2ruby.rb', line 96 def process_alias(exp) # :nodoc: parenthesize "alias #{process(exp.shift)} #{process(exp.shift)}" end |
#process_and(exp) ⇒ Object
:nodoc:
100 101 102 |
# File 'lib/ruby2ruby.rb', line 100 def process_and(exp) # :nodoc: parenthesize "#{process exp.shift} and #{process exp.shift}" end |
#process_arglist(exp) ⇒ Object
custom made node # :nodoc:
104 105 106 107 108 109 110 111 112 113 |
# File 'lib/ruby2ruby.rb', line 104 def process_arglist(exp) # custom made node # :nodoc: code = [] until exp.empty? do arg = exp.shift to_wrap = arg.first == :rescue arg_code = process arg code << (to_wrap ? "(#{arg_code})" : arg_code) end code.join ', ' end |
#process_args(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 115 def process_args(exp) # :nodoc: args = [] until exp.empty? do arg = exp.shift case arg when Symbol then args << arg when Sexp then case arg.first when :lasgn then args << process(arg) when :masgn then args << process(arg) when :kwarg then _, k, v = arg args << "#{k}: #{process v}" else raise "unknown arg type #{arg.first.inspect}" end else raise "unknown arg type #{arg.inspect}" end end "(#{args.join ', '})" end |
#process_array(exp) ⇒ Object
:nodoc:
143 144 145 |
# File 'lib/ruby2ruby.rb', line 143 def process_array(exp) # :nodoc: "[#{process_arglist(exp)}]" end |
#process_attrasgn(exp) ⇒ Object
:nodoc:
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/ruby2ruby.rb', line 147 def process_attrasgn(exp) # :nodoc: receiver = process exp.shift name = exp.shift rhs = exp.pop args = s(:array, *exp) exp.clear case name when :[]= then args = process args "#{receiver}#{args} = #{process rhs}" else raise "dunno what to do: #{args.inspect}" unless args.size == 1 # s(:array) name = name.to_s.sub(/=$/, '') if rhs && rhs != s(:arglist) then "#{receiver}.#{name} = #{process(rhs)}" else raise "dunno what to do: #{rhs.inspect}" end end end |
#process_back_ref(exp) ⇒ Object
:nodoc:
169 170 171 |
# File 'lib/ruby2ruby.rb', line 169 def process_back_ref(exp) # :nodoc: "$#{exp.shift}" end |
#process_begin(exp) ⇒ Object
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
174 175 176 177 178 179 180 181 182 183 184 |
# File 'lib/ruby2ruby.rb', line 174 def process_begin(exp) # :nodoc: code = [] code << "begin" until exp.empty? src = process(exp.shift) src = indent(src) unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues code << src end code << "end" return code.join("\n") end |
#process_block(exp) ⇒ Object
:nodoc:
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ruby2ruby.rb', line 186 def process_block(exp) # :nodoc: result = [] exp << nil if exp.empty? until exp.empty? do code = exp.shift if code.nil? or code.first == :nil then result << "# do nothing\n" else result << process(code) end end result = parenthesize result.join "\n" result += "\n" unless result.start_with? "(" return result end |
#process_block_pass(exp) ⇒ Object
:nodoc:
205 206 207 208 209 |
# File 'lib/ruby2ruby.rb', line 205 def process_block_pass exp # :nodoc: raise "huh?: #{exp.inspect}" if exp.size > 1 "&#{process exp.shift}" end |
#process_break(exp) ⇒ Object
:nodoc:
211 212 213 214 215 216 217 218 219 |
# File 'lib/ruby2ruby.rb', line 211 def process_break(exp) # :nodoc: val = exp.empty? ? nil : process(exp.shift) # HACK "break" + (val ? " #{val}" : "") if val then "break #{val}" else "break" end end |
#process_call(exp, safe_call = false) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 221 def process_call(exp, safe_call = false) # :nodoc: receiver_node_type = exp.first.nil? ? nil : exp.first.first receiver = process exp.shift receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type name = exp.shift args = [] # this allows us to do both old and new sexp forms: exp.push(*exp.pop[1..-1]) if exp.size == 1 && exp.first.first == :arglist @calls.push name in_context :arglist do until exp.empty? do arg_type = exp.first.sexp_type is_empty_hash = (exp.first == s(:hash)) arg = process exp.shift next if arg.empty? strip_hash = (arg_type == :hash and not BINARY.include? name and not is_empty_hash and (exp.empty? or exp.first.sexp_type == :splat)) wrap_arg = Ruby2Ruby::ASSIGN_NODES.include? arg_type arg = arg[2..-3] if strip_hash arg = "(#{arg})" if wrap_arg args << arg end end case name when *BINARY then if safe_call "#{receiver}&.#{name}(#{args.join(', ')})" else "(#{receiver} #{name} #{args.join(', ')})" end when :[] then receiver ||= "self" "#{receiver}[#{args.join(', ')}]" when :[]= then receiver ||= "self" rhs = args.pop "#{receiver}[#{args.join(', ')}] = #{rhs}" when :"!" then "(not #{receiver})" when :"-@" then "-#{receiver}" when :"+@" then "+#{receiver}" else args = nil if args.empty? args = "(#{args.join(', ')})" if args receiver = "#{receiver}." if receiver and not safe_call receiver = "#{receiver}&." if receiver and safe_call "#{receiver}#{name}#{args}" end ensure @calls.pop end |
#process_case(exp) ⇒ Object
:nodoc:
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/ruby2ruby.rb', line 291 def process_case(exp) # :nodoc: 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 |
#process_cdecl(exp) ⇒ Object
:nodoc:
313 314 315 316 317 318 319 320 321 322 |
# File 'lib/ruby2ruby.rb', line 313 def process_cdecl(exp) # :nodoc: lhs = exp.shift lhs = process lhs if Sexp === lhs unless exp.empty? then rhs = process(exp.shift) "#{lhs} = #{rhs}" else lhs.to_s end end |
#process_class(exp) ⇒ Object
:nodoc:
324 325 326 |
# File 'lib/ruby2ruby.rb', line 324 def process_class(exp) # :nodoc: "#{exp.comments}class #{util_module_or_class(exp, true)}" end |
#process_colon2(exp) ⇒ Object
:nodoc:
328 329 330 |
# File 'lib/ruby2ruby.rb', line 328 def process_colon2(exp) # :nodoc: "#{process(exp.shift)}::#{exp.shift}" end |
#process_colon3(exp) ⇒ Object
:nodoc:
332 333 334 |
# File 'lib/ruby2ruby.rb', line 332 def process_colon3(exp) # :nodoc: "::#{exp.shift}" end |
#process_const(exp) ⇒ Object
:nodoc:
336 337 338 |
# File 'lib/ruby2ruby.rb', line 336 def process_const(exp) # :nodoc: exp.shift.to_s end |
#process_cvar(exp) ⇒ Object
:nodoc:
340 341 342 |
# File 'lib/ruby2ruby.rb', line 340 def process_cvar(exp) # :nodoc: "#{exp.shift}" end |
#process_cvasgn(exp) ⇒ Object
:nodoc:
344 345 346 |
# File 'lib/ruby2ruby.rb', line 344 def process_cvasgn(exp) # :nodoc: "#{exp.shift} = #{process(exp.shift)}" end |
#process_cvdecl(exp) ⇒ Object
:nodoc:
348 349 350 |
# File 'lib/ruby2ruby.rb', line 348 def process_cvdecl(exp) # :nodoc: "#{exp.shift} = #{process(exp.shift)}" end |
#process_defined(exp) ⇒ Object
:nodoc:
352 353 354 |
# File 'lib/ruby2ruby.rb', line 352 def process_defined(exp) # :nodoc: "defined? #{process(exp.shift)}" end |
#process_defn(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 356 def process_defn(exp) # :nodoc: type1 = exp[1].first type2 = exp[2].first rescue nil expect = [:ivar, :iasgn, :attrset] # s(name, args, ivar|iasgn|attrset) if exp.size == 3 and type1 == :args and expect.include? type2 then name = exp.first # don't shift in case we pass through case type2 when :ivar then ivar_name = exp.ivar.last meth_name = ivar_name.to_s[1..-1].to_sym expected = s(meth_name, s(:args), s(:ivar, ivar_name)) if exp == expected then exp.clear return "attr_reader #{name.inspect}" end when :attrset then # TODO: deprecate? this is a PT relic exp.clear return "attr_writer :#{name.to_s[0..-2]}" when :iasgn then ivar_name = exp.iasgn[1] meth_name = "#{ivar_name.to_s[1..-1]}=".to_sym arg_name = exp.args.last expected = s(meth_name, s(:args, arg_name), s(:iasgn, ivar_name, s(:lvar, arg_name))) if exp == expected then exp.clear return "attr_writer :#{name.to_s[0..-2]}" end else raise "Unknown defn type: #{exp.inspect}" end end comm = exp.comments name = exp.shift args = process exp.shift args = "" if args == "()" exp.shift if exp == s(s(:nil)) # empty it out of a default nil expression # REFACTOR: use process_block but get it happier wrt parenthesize body = [] until exp.empty? do body << process(exp.shift) end body << "# do nothing" if body.empty? body = body.join("\n") body = body.lines.to_a[1..-2].join("\n") if body =~ /^\Abegin/ && body =~ /^end\z/ body = indent(body) unless body =~ /(^|\n)rescue/ return "#{comm}def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n") end |
#process_defs(exp) ⇒ Object
:nodoc:
417 418 419 420 421 422 423 424 425 426 427 |
# File 'lib/ruby2ruby.rb', line 417 def process_defs(exp) # :nodoc: lhs = exp.shift var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.first name = exp.shift lhs = process(lhs) lhs = "(#{lhs})" unless var exp.unshift "#{lhs}.#{name}" process_defn(exp) end |
#process_dot2(exp) ⇒ Object
:nodoc:
429 430 431 |
# File 'lib/ruby2ruby.rb', line 429 def process_dot2(exp) # :nodoc: "(#{process exp.shift}..#{process exp.shift})" end |
#process_dot3(exp) ⇒ Object
:nodoc:
433 434 435 |
# File 'lib/ruby2ruby.rb', line 433 def process_dot3(exp) # :nodoc: "(#{process exp.shift}...#{process exp.shift})" end |
#process_dregx(exp) ⇒ Object
:nodoc:
437 438 439 440 |
# File 'lib/ruby2ruby.rb', line 437 def process_dregx(exp) # :nodoc: = re_opt exp.pop if Fixnum === exp.last "/" << util_dthing(:dregx, exp) << "/#{}" end |
#process_dregx_once(exp) ⇒ Object
:nodoc:
442 443 444 |
# File 'lib/ruby2ruby.rb', line 442 def process_dregx_once(exp) # :nodoc: process_dregx(exp) + "o" end |
#process_dstr(exp) ⇒ Object
:nodoc:
446 447 448 |
# File 'lib/ruby2ruby.rb', line 446 def process_dstr(exp) # :nodoc: "\"#{util_dthing(:dstr, exp)}\"" end |
#process_dsym(exp) ⇒ Object
:nodoc:
450 451 452 |
# File 'lib/ruby2ruby.rb', line 450 def process_dsym(exp) # :nodoc: ":\"#{util_dthing(:dsym, exp)}\"" end |
#process_dxstr(exp) ⇒ Object
:nodoc:
454 455 456 |
# File 'lib/ruby2ruby.rb', line 454 def process_dxstr(exp) # :nodoc: "`#{util_dthing(:dxstr, exp)}`" end |
#process_ensure(exp) ⇒ Object
:nodoc:
458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/ruby2ruby.rb', line 458 def process_ensure(exp) # :nodoc: body = process exp.shift ens = exp.shift ens = nil if ens == s(:nil) ens = process(ens) || "# do nothing" ens = "begin\n#{ens}\nend\n" if ens =~ /(^|\n)rescue/ body.sub!(/\n\s*end\z/, '') body = indent(body) unless body =~ /(^|\n)rescue/ return "#{body}\nensure\n#{indent ens}" end |
#process_evstr(exp) ⇒ Object
:nodoc:
471 472 473 |
# File 'lib/ruby2ruby.rb', line 471 def process_evstr(exp) # :nodoc: exp.empty? ? '' : process(exp.shift) end |
#process_false(exp) ⇒ Object
:nodoc:
475 476 477 |
# File 'lib/ruby2ruby.rb', line 475 def process_false(exp) # :nodoc: "false" end |
#process_flip2(exp) ⇒ Object
:nodoc:
479 480 481 |
# File 'lib/ruby2ruby.rb', line 479 def process_flip2(exp) # :nodoc: "#{process(exp.shift)}..#{process(exp.shift)}" end |
#process_flip3(exp) ⇒ Object
:nodoc:
483 484 485 |
# File 'lib/ruby2ruby.rb', line 483 def process_flip3(exp) # :nodoc: "#{process(exp.shift)}...#{process(exp.shift)}" end |
#process_for(exp) ⇒ Object
:nodoc:
487 488 489 490 491 492 493 494 495 496 497 |
# File 'lib/ruby2ruby.rb', line 487 def process_for(exp) # :nodoc: 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 |
#process_gasgn(exp) ⇒ Object
:nodoc:
499 500 501 |
# File 'lib/ruby2ruby.rb', line 499 def process_gasgn(exp) # :nodoc: process_iasgn(exp) end |
#process_gvar(exp) ⇒ Object
:nodoc:
503 504 505 |
# File 'lib/ruby2ruby.rb', line 503 def process_gvar(exp) # :nodoc: return exp.shift.to_s end |
#process_hash(exp) ⇒ Object
:nodoc:
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 |
# File 'lib/ruby2ruby.rb', line 507 def process_hash(exp) # :nodoc: result = [] until exp.empty? s = exp.shift t = s.sexp_type lhs = process s case t when :kwsplat then result << lhs else rhs = exp.shift t = rhs.first rhs = process rhs rhs = "(#{rhs})" unless HASH_VAL_NO_PAREN.include? t result << "#{lhs} => #{rhs}" end end return result.empty? ? "{}" : "{ #{result.join(', ')} }" end |
#process_iasgn(exp) ⇒ Object
:nodoc:
531 532 533 534 535 536 537 538 |
# File 'lib/ruby2ruby.rb', line 531 def process_iasgn(exp) # :nodoc: lhs = exp.shift if exp.empty? then # part of an masgn lhs.to_s else "#{lhs} = #{process exp.shift}" end end |
#process_if(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 540 def process_if(exp) # :nodoc: = 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 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 elsif f unless then r = "#{f} unless #{c}" return r if (@indent+r).size < LINE_LENGTH and r !~ /\n/ end "unless #{c} then\n#{indent(f)}\nend" else # empty if statement, just do it in case of side effects from condition "if #{c} then\n#{indent '# do nothing'}\nend" end end |
#process_iter(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 576 def process_iter(exp) # :nodoc: iter = process exp.shift args = exp.shift body = exp.empty? ? nil : process(exp.shift) args = case args when 0 then "" else " |#{process(args)[1..-2]}|" end b, e = if iter == "END" then [ "{", "}" ] else [ "do", "end" ] end iter.sub!(/\(\)$/, '') # REFACTOR: ugh result = [] result << "#{iter} {" result << 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 result << "\n" if body then result << indent(body.strip) result << "\n" end result << e result.join end |
#process_ivar(exp) ⇒ Object
:nodoc:
621 622 623 |
# File 'lib/ruby2ruby.rb', line 621 def process_ivar(exp) # :nodoc: exp.shift.to_s end |
#process_kwsplat(exp) ⇒ Object
625 626 627 |
# File 'lib/ruby2ruby.rb', line 625 def process_kwsplat(exp) "**#{process exp.shift}" end |
#process_lasgn(exp) ⇒ Object
:nodoc:
629 630 631 632 633 |
# File 'lib/ruby2ruby.rb', line 629 def process_lasgn(exp) # :nodoc: s = "#{exp.shift}" s += " = #{process exp.shift}" unless exp.empty? s end |
#process_lit(exp) ⇒ Object
:nodoc:
635 636 637 638 639 640 641 642 643 |
# File 'lib/ruby2ruby.rb', line 635 def process_lit(exp) # :nodoc: obj = exp.shift case obj when Range then "(#{obj.inspect})" else obj.inspect end end |
#process_lvar(exp) ⇒ Object
:nodoc:
645 646 647 |
# File 'lib/ruby2ruby.rb', line 645 def process_lvar(exp) # :nodoc: exp.shift.to_s end |
#process_masgn(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 649 def process_masgn(exp) # :nodoc: # s(:masgn, s(:array, s(:lasgn, :var), ...), s(:to_ary, <val>, ...)) # s(:iter, <call>, s(:args, s(:masgn, :a, :b)), <body>) case exp.first when Sexp then lhs = exp.shift rhs = exp.empty? ? nil : exp.shift case lhs.first when :array then lhs.shift # node type lhs = lhs.map do |l| case l.first when :masgn then "(#{process(l)})" else process(l) end end else raise "no clue: #{lhs.inspect}" end unless rhs.nil? then t = rhs.first rhs = process rhs rhs = rhs[1..-2] if t == :array # FIX: bad? I dunno return "#{lhs.join(", ")} = #{rhs}" else return lhs.join(", ") end when Symbol then # block arg list w/ masgn result = exp.join ", " exp.clear "(#{result})" else raise "unknown masgn: #{exp.inspect}" end end |
#process_match(exp) ⇒ Object
:nodoc:
690 691 692 |
# File 'lib/ruby2ruby.rb', line 690 def process_match(exp) # :nodoc: "#{process(exp.shift)}" end |
#process_match2(exp) ⇒ Object
:nodoc:
694 695 696 697 698 |
# File 'lib/ruby2ruby.rb', line 694 def process_match2(exp) # :nodoc: lhs = process(exp.shift) rhs = process(exp.shift) "#{lhs} =~ #{rhs}" end |
#process_match3(exp) ⇒ Object
:nodoc:
700 701 702 703 704 705 706 707 708 709 710 |
# File 'lib/ruby2ruby.rb', line 700 def process_match3(exp) # :nodoc: rhs = process(exp.shift) left_type = exp.first.sexp_type lhs = process(exp.shift) if ASSIGN_NODES.include? left_type then "(#{lhs}) =~ #{rhs}" else "#{lhs} =~ #{rhs}" end end |
#process_module(exp) ⇒ Object
:nodoc:
712 713 714 |
# File 'lib/ruby2ruby.rb', line 712 def process_module(exp) # :nodoc: "#{exp.comments}module #{util_module_or_class(exp)}" end |
#process_next(exp) ⇒ Object
:nodoc:
716 717 718 719 720 721 722 723 |
# File 'lib/ruby2ruby.rb', line 716 def process_next(exp) # :nodoc: val = exp.empty? ? nil : process(exp.shift) if val then "next #{val}" else "next" end end |
#process_nil(exp) ⇒ Object
:nodoc:
725 726 727 |
# File 'lib/ruby2ruby.rb', line 725 def process_nil(exp) # :nodoc: "nil" end |
#process_not(exp) ⇒ Object
:nodoc:
729 730 731 |
# File 'lib/ruby2ruby.rb', line 729 def process_not(exp) # :nodoc: "(not #{process exp.shift})" end |
#process_nth_ref(exp) ⇒ Object
:nodoc:
733 734 735 |
# File 'lib/ruby2ruby.rb', line 733 def process_nth_ref(exp) # :nodoc: "$#{exp.shift}" end |
#process_op_asgn1(exp) ⇒ Object
:nodoc:
737 738 739 740 741 742 743 744 745 |
# File 'lib/ruby2ruby.rb', line 737 def process_op_asgn1(exp) # :nodoc: # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]] lhs = process(exp.shift) index = process(exp.shift) msg = exp.shift rhs = process(exp.shift) "#{lhs}[#{index}] #{msg}= #{rhs}" end |
#process_op_asgn2(exp) ⇒ Object
:nodoc:
747 748 749 750 751 752 753 754 755 756 |
# File 'lib/ruby2ruby.rb', line 747 def process_op_asgn2(exp) # :nodoc: # [[: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 |
#process_op_asgn_and(exp) ⇒ Object
:nodoc:
758 759 760 761 762 763 |
# File 'lib/ruby2ruby.rb', line 758 def process_op_asgn_and(exp) # :nodoc: # a &&= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] exp.shift process(exp.shift).sub(/\=/, '&&=') end |
#process_op_asgn_or(exp) ⇒ Object
:nodoc:
765 766 767 768 769 770 |
# File 'lib/ruby2ruby.rb', line 765 def process_op_asgn_or(exp) # :nodoc: # a ||= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] exp.shift process(exp.shift).sub(/\=/, '||=') end |
#process_or(exp) ⇒ Object
:nodoc:
772 773 774 |
# File 'lib/ruby2ruby.rb', line 772 def process_or(exp) # :nodoc: "(#{process exp.shift} or #{process exp.shift})" end |
#process_postexe(exp) ⇒ Object
:nodoc:
776 777 778 |
# File 'lib/ruby2ruby.rb', line 776 def process_postexe(exp) # :nodoc: "END" end |
#process_redo(exp) ⇒ Object
:nodoc:
780 781 782 |
# File 'lib/ruby2ruby.rb', line 780 def process_redo(exp) # :nodoc: "redo" end |
#process_resbody(exp) ⇒ Object
:nodoc:
784 785 786 787 788 789 790 791 792 793 794 795 796 |
# File 'lib/ruby2ruby.rb', line 784 def process_resbody exp # :nodoc: args = exp.shift body = finish(exp) body << "# do nothing" if body.empty? name = args.lasgn true name ||= args.iasgn true args = process(args)[1..-2] args = " #{args}" unless args.empty? args += " => #{name[1]}" if name "rescue#{args}\n#{indent body.join("\n")}" end |
#process_rescue(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 798 def process_rescue exp # :nodoc: body = process(exp.shift) unless exp.first.first == :resbody els = process(exp.pop) unless exp.last.first == :resbody body ||= "# do nothing" simple = exp.size == 1 && exp.resbody.size <= 3 && !exp.resbody.block && !exp.resbody.return resbodies = [] until exp.empty? do resbody = exp.shift simple &&= resbody[1] == s(:array) simple &&= resbody[2] != nil && resbody[2].node_type != :block resbodies << process(resbody) end if els then "#{indent body}\n#{resbodies.join("\n")}\nelse\n#{indent els}" elsif simple then resbody = resbodies.first.sub(/\n\s*/, ' ') "#{body} #{resbody}" else "#{indent body}\n#{resbodies.join("\n")}" end end |
#process_retry(exp) ⇒ Object
:nodoc:
825 826 827 |
# File 'lib/ruby2ruby.rb', line 825 def process_retry(exp) # :nodoc: "retry" end |
#process_return(exp) ⇒ Object
:nodoc:
829 830 831 832 833 834 835 836 837 |
# File 'lib/ruby2ruby.rb', line 829 def process_return(exp) # :nodoc: # HACK return "return" + (exp.empty? ? "" : " #{process exp.shift}") if exp.empty? then return "return" else return "return #{process exp.shift}" end end |
#process_safe_attrasgn(exp) ⇒ Object
:nodoc:
839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 |
# File 'lib/ruby2ruby.rb', line 839 def process_safe_attrasgn(exp) # :nodoc: receiver = process exp.shift name = exp.shift rhs = exp.pop args = exp.pop # should be nil exp.clear raise "dunno what to do: #{args.inspect}" if args name = name.to_s.sub(/=$/, '') if rhs && rhs != s(:arglist) then "#{receiver}&.#{name} = #{process(rhs)}" else raise "dunno what to do: #{rhs.inspect}" end end |
#process_safe_call(exp) ⇒ Object
:nodoc:
287 288 289 |
# File 'lib/ruby2ruby.rb', line 287 def process_safe_call(exp) # :nodoc: process_call(exp, :safe) end |
#process_sclass(exp) ⇒ Object
:nodoc:
857 858 859 |
# File 'lib/ruby2ruby.rb', line 857 def process_sclass(exp) # :nodoc: "class << #{process(exp.shift)}\n#{indent(process_block(exp))}\nend" end |
#process_self(exp) ⇒ Object
:nodoc:
861 862 863 |
# File 'lib/ruby2ruby.rb', line 861 def process_self(exp) # :nodoc: "self" end |
#process_splat(exp) ⇒ Object
:nodoc:
865 866 867 868 869 870 871 |
# File 'lib/ruby2ruby.rb', line 865 def process_splat(exp) # :nodoc: if exp.empty? then "*" else "*#{process(exp.shift)}" end end |
#process_str(exp) ⇒ Object
:nodoc:
873 874 875 |
# File 'lib/ruby2ruby.rb', line 873 def process_str(exp) # :nodoc: return exp.shift.dump end |
#process_super(exp) ⇒ Object
:nodoc:
877 878 879 880 881 |
# File 'lib/ruby2ruby.rb', line 877 def process_super(exp) # :nodoc: args = finish exp "super(#{args.join(', ')})" end |
#process_svalue(exp) ⇒ Object
:nodoc:
883 884 885 886 887 888 889 |
# File 'lib/ruby2ruby.rb', line 883 def process_svalue(exp) # :nodoc: code = [] until exp.empty? do code << process(exp.shift) end code.join(", ") end |
#process_to_ary(exp) ⇒ Object
:nodoc:
891 892 893 |
# File 'lib/ruby2ruby.rb', line 891 def process_to_ary(exp) # :nodoc: process(exp.shift) end |
#process_true(exp) ⇒ Object
:nodoc:
895 896 897 |
# File 'lib/ruby2ruby.rb', line 895 def process_true(exp) # :nodoc: "true" end |
#process_undef(exp) ⇒ Object
:nodoc:
899 900 901 |
# File 'lib/ruby2ruby.rb', line 899 def process_undef(exp) # :nodoc: "undef #{process(exp.shift)}" end |
#process_until(exp) ⇒ Object
:nodoc:
903 904 905 |
# File 'lib/ruby2ruby.rb', line 903 def process_until(exp) # :nodoc: cond_loop(exp, 'until') end |
#process_valias(exp) ⇒ Object
:nodoc:
907 908 909 |
# File 'lib/ruby2ruby.rb', line 907 def process_valias(exp) # :nodoc: "alias #{exp.shift} #{exp.shift}" end |
#process_when(exp) ⇒ Object
:nodoc:
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 |
# File 'lib/ruby2ruby.rb', line 911 def process_when(exp) # :nodoc: 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(finish(exp).join("\n")) code = indent "# do nothing" if code =~ /\A\s*\Z/ src << "when #{cond} then\n#{code.chomp}" end src.join("\n") end |
#process_while(exp) ⇒ Object
:nodoc:
930 931 932 |
# File 'lib/ruby2ruby.rb', line 930 def process_while(exp) # :nodoc: cond_loop(exp, 'while') end |
#process_xstr(exp) ⇒ Object
:nodoc:
934 935 936 |
# File 'lib/ruby2ruby.rb', line 934 def process_xstr(exp) # :nodoc: "`#{process_str(exp)[1..-2]}`" end |
#process_yield(exp) ⇒ Object
:nodoc:
938 939 940 941 942 943 944 945 946 947 948 949 |
# File 'lib/ruby2ruby.rb', line 938 def process_yield(exp) # :nodoc: args = [] until exp.empty? do args << process(exp.shift) end unless args.empty? then "yield(#{args.join(', ')})" else "yield" end end |
#process_zsuper(exp) ⇒ Object
:nodoc:
951 952 953 |
# File 'lib/ruby2ruby.rb', line 951 def process_zsuper(exp) # :nodoc: "super" end |
#re_opt(options) ⇒ Object
Return the appropriate regexp flags for a given numeric code.
1083 1084 1085 1086 1087 |
# File 'lib/ruby2ruby.rb', line 1083 def re_opt bits = (0..8).map { |n| [n] * 2**n } bits.delete 0 bits.map { |n| Regexp::CODES[n] }.join end |
#rewrite_attrasgn(exp) ⇒ Object
Rewriters:
958 959 960 961 962 963 964 965 |
# File 'lib/ruby2ruby.rb', line 958 def rewrite_attrasgn exp # :nodoc: if context.first(2) == [:array, :masgn] then exp[0] = :call exp[2] = exp[2].to_s.sub(/=$/, '').to_sym end exp end |
#rewrite_ensure(exp) ⇒ Object
:nodoc:
967 968 969 970 |
# File 'lib/ruby2ruby.rb', line 967 def rewrite_ensure exp # :nodoc: exp = s(:begin, exp) unless context.first == :begin exp end |
#rewrite_resbody(exp) ⇒ Object
:nodoc:
972 973 974 975 976 977 |
# File 'lib/ruby2ruby.rb', line 972 def rewrite_resbody exp # :nodoc: raise "no exception list in #{exp.inspect}" unless exp.size > 2 && exp[1] raise exp[1].inspect if exp[1][0] != :array # for now, do nothing, just check and freak if we see an errant structure exp end |
#rewrite_rescue(exp) ⇒ Object
:nodoc:
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 |
# File 'lib/ruby2ruby.rb', line 979 def rewrite_rescue exp # :nodoc: complex = false complex ||= exp.size > 3 complex ||= exp.resbody.block complex ||= exp.resbody.size > 3 complex ||= exp.find_nodes(:resbody).any? { |n| n[1] != s(:array) } complex ||= exp.find_nodes(:resbody).any? { |n| n.last.nil? } complex ||= exp.find_nodes(:resbody).any? { |n| n[2] and n[2].node_type == :block } handled = context.first == :ensure exp = s(:begin, exp) if complex unless handled exp end |
#rewrite_svalue(exp) ⇒ Object
:nodoc:
995 996 997 998 999 1000 1001 1002 1003 1004 |
# File 'lib/ruby2ruby.rb', line 995 def rewrite_svalue(exp) # :nodoc: case exp.last.first when :array s(:svalue, *exp[1][1..-1]) when :splat exp else raise "huh: #{exp.inspect}" end end |
#splat(sym) ⇒ Object
Return a splatted symbol for sym.
1092 1093 1094 |
# File 'lib/ruby2ruby.rb', line 1092 def splat(sym) :"*#{sym}" end |
#util_dthing(type, exp) ⇒ Object
Utility method to generate something interpolated.
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 |
# File 'lib/ruby2ruby.rb', line 1099 def util_dthing(type, exp) s = [] # first item in sexp is a string literal s << dthing_escape(type, exp.shift) until exp.empty? pt = exp.shift case pt when Sexp then case pt.first when :str then s << dthing_escape(type, pt.last) when :evstr then s << '#{' << process(pt) << '}' # do not use interpolation here else raise "unknown type: #{pt.inspect}" end else raise "unhandled value in d-thing: #{pt.inspect}" end end s.join end |
#util_module_or_class(exp, is_class = false) ⇒ Object
Utility method to generate ether a module or class.
1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 |
# File 'lib/ruby2ruby.rb', line 1128 def util_module_or_class(exp, is_class=false) result = [] name = exp.shift name = process name if Sexp === name result << name if is_class then superk = process(exp.shift) result << " < #{superk}" if superk end result << "\n" body = [] begin code = process(exp.shift) unless exp.empty? body << code.chomp unless code.nil? or code.chomp.empty? end until exp.empty? unless body.empty? then body = indent(body.join("\n\n")) + "\n" else body = "" end result << body result << "end" result.join end |