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.6.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, :match3, :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, ]
- MUST_BE_CURLY =
%w[ BEGIN END ]
Instance Method Summary collapse
- #__var(name) ⇒ Object
-
#comments(exp) ⇒ Object
:nodoc:.
-
#cond_loop(exp, name) ⇒ Object
Generate a post-or-pre conditional loop.
-
#dthing_escape(type, lit) ⇒ Object
Utility method to escape something interpolated.
-
#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_array_pat(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_find_pat(exp) ⇒ Object
:nodoc:.
-
#process_flip2(exp) ⇒ Object
:nodoc:.
-
#process_flip3(exp) ⇒ Object
:nodoc:.
-
#process_for(exp) ⇒ Object
:nodoc:.
- #process_forward_args(exp) ⇒ Object
-
#process_gasgn(exp) ⇒ Object
:nodoc:.
-
#process_gvar(exp) ⇒ Object
:nodoc:.
-
#process_hash(exp) ⇒ Object
:nodoc:.
-
#process_hash_pat(exp) ⇒ Object
:nodoc:.
-
#process_iasgn(exp) ⇒ Object
:nodoc:.
-
#process_if(exp) ⇒ Object
:nodoc:.
-
#process_in(exp) ⇒ Object
:nodoc:.
-
#process_iter(exp) ⇒ Object
:nodoc:.
-
#process_ivar(exp) ⇒ Object
:nodoc:.
-
#process_kwrest(exp) ⇒ Object
:nodoc:.
- #process_kwsplat(exp) ⇒ Object
-
#process_lambda(exp) ⇒ Object
:nodoc:.
-
#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_asgn(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_preexe(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_safe_op_asgn(exp) ⇒ Object
:nodoc:.
-
#process_safe_op_asgn2(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_call(exp) ⇒ Object
:nodoc:.
-
#rewrite_ensure(exp) ⇒ Object
:nodoc:.
-
#rewrite_if(exp) ⇒ Object
:nodoc:.
-
#rewrite_resbody(exp) ⇒ Object
:nodoc:.
-
#rewrite_rescue(exp) ⇒ Object
:nodoc:.
-
#rewrite_svalue(exp) ⇒ Object
:nodoc:.
-
#rewrite_until(exp) ⇒ Object
:nodoc:.
-
#rewrite_while(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:
82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/ruby2ruby.rb', line 82 def initialize # :nodoc: super @indent = " " self.require_empty = false self.strict = true self.expected = String @calls = [] # self.debug[:defn] = /zsuper/ end |
Instance Method Details
#__var(name) ⇒ Object
383 384 385 386 387 388 389 390 391 392 |
# File 'lib/ruby2ruby.rb', line 383 def __var name case context[1] when /_pat$/ then "^#{name}" when :in then "^#{name}" else name.to_s end end |
#comments(exp) ⇒ Object
:nodoc:
353 354 355 356 357 |
# File 'lib/ruby2ruby.rb', line 353 def comments exp # :nodoc: comments = exp.comments comments += "\n" unless comments.end_with?("\n") if comments comments end |
#cond_loop(exp, name) ⇒ Object
Generate a post-or-pre conditional loop.
1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 |
# File 'lib/ruby2ruby.rb', line 1322 def cond_loop(exp, name) _, cond, body, head_controlled = exp cond = process cond body = process body 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.
1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 |
# File 'lib/ruby2ruby.rb', line 1347 def dthing_escape type, lit # TODO: this needs more testing case type when :dregx then lit.gsub(/(\A|[^\\])\//, '\1\/') when :dstr, :dsym then lit.dump[1..-2] when :dxstr then lit.gsub(/`/, '\`') else raise "unsupported type #{type.inspect}" end end |
#indent(s) ⇒ Object
Indent all lines of s to the current indent level.
1364 1365 1366 |
# File 'lib/ruby2ruby.rb', line 1364 def indent s s.to_s.split(/\n/).map{|line| @indent + line}.join("\n") end |
#parenthesize(exp) ⇒ Object
Wrap appropriate expressions in matching parens.
1371 1372 1373 1374 1375 1376 1377 1378 |
# File 'lib/ruby2ruby.rb', line 1371 def parenthesize exp case context[1] when nil, :defn, :defs, :class, :sclass, :if, :iter, :resbody, :when, :while then exp else "(#{exp})" end end |
#process_alias(exp) ⇒ Object
Processors
97 98 99 100 101 |
# File 'lib/ruby2ruby.rb', line 97 def process_alias exp # :nodoc: _, lhs, rhs = exp parenthesize "alias #{process lhs} #{process rhs}" end |
#process_and(exp) ⇒ Object
:nodoc:
103 104 105 106 107 |
# File 'lib/ruby2ruby.rb', line 103 def process_and exp # :nodoc: _, lhs, rhs = exp parenthesize "#{process lhs} and #{process rhs}" end |
#process_arglist(exp) ⇒ Object
custom made node # :nodoc:
109 110 111 112 113 114 115 116 |
# File 'lib/ruby2ruby.rb', line 109 def process_arglist exp # custom made node # :nodoc: _, *args = exp args.map { |arg| code = process arg arg.sexp_type == :rescue ? "(#{code})" : code }.join ", " end |
#process_args(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 118 def process_args exp # :nodoc: _, *args = exp shadow = [] args = args.map { |arg| case arg when Symbol then arg when Sexp then case arg.sexp_type when :lasgn then process(arg) when :masgn then process(arg) when :kwarg then _, k, v = arg "#{k}: #{process v}" when :shadow then shadow << arg.sexp_body next when :forward_args then "..." else raise "unknown arg type #{arg.first.inspect}" end when nil then "" else raise "unknown arg type #{arg.inspect}" end }.compact args = args.join(", ").strip shadow = shadow.join(", ").strip shadow = "; #{shadow}" unless shadow.empty? "(%s%s)" % [args, shadow] end |
#process_array(exp) ⇒ Object
:nodoc:
158 159 160 |
# File 'lib/ruby2ruby.rb', line 158 def process_array exp # :nodoc: "[#{process_arglist exp}]" end |
#process_array_pat(exp) ⇒ Object
:nodoc:
627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
# File 'lib/ruby2ruby.rb', line 627 def process_array_pat exp # :nodoc: _, const, *rest = exp const = process const if const rest = rest.map { |sexp| case sexp when Sexp process sexp else sexp end } "%s[%s]" % [const, rest.join(", ")] end |
#process_attrasgn(exp) ⇒ Object
:nodoc:
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/ruby2ruby.rb', line 162 def process_attrasgn exp # :nodoc: _, recv, name, *args = exp rhs = args.pop args = s(:array, *args) receiver = process recv case name when :[]= then args = process args "#{receiver}#{args} = #{process rhs}" else raise "dunno what to do: #{args.inspect}" if args.size != 1 # s(:array) name = name.to_s.chomp "=" 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:
184 185 186 187 188 |
# File 'lib/ruby2ruby.rb', line 184 def process_back_ref exp # :nodoc: _, n = exp "$#{n}" end |
#process_begin(exp) ⇒ Object
TODO: figure out how to do rescue and ensure ENTIRELY w/o begin
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/ruby2ruby.rb', line 191 def process_begin exp # :nodoc: _, *rest = exp code = rest.map { |sexp| src = process sexp src = indent src unless src =~ /(^|\n)(rescue|ensure)/ # ensure no level 0 rescues src } code.unshift "begin" code.push "end" code.join "\n" end |
#process_block(exp) ⇒ Object
:nodoc:
205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/ruby2ruby.rb', line 205 def process_block exp # :nodoc: _, *body = exp result = body.map { |sexp| process sexp } result << "# do nothing\n" if result.empty? result = parenthesize result.join "\n" result += "\n" unless result.start_with? "(" result end |
#process_block_pass(exp) ⇒ Object
:nodoc:
219 220 221 222 223 224 225 |
# File 'lib/ruby2ruby.rb', line 219 def process_block_pass exp # :nodoc: raise "huh?: #{exp.inspect}" if exp.size > 2 _, sexp = exp "&#{process sexp}" end |
#process_break(exp) ⇒ Object
:nodoc:
227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/ruby2ruby.rb', line 227 def process_break exp # :nodoc: _, arg = exp val = process arg if val then "break #{val}" else "break" end end |
#process_call(exp, safe_call = false) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 239 def process_call(exp, safe_call = false) # :nodoc: _, recv, name, *args = exp receiver_node_type = recv && recv.sexp_type receiver = process recv receiver = "(#{receiver})" if ASSIGN_NODES.include? receiver_node_type # 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 max = args.size - 1 args = args.map.with_index { |arg, i| arg_type = arg.sexp_type is_empty_hash = arg == s(:hash) arg = process arg next if arg.empty? strip_hash = (arg_type == :hash and not BINARY.include? name and not is_empty_hash and (i == max or args[i + 1].sexp_type == :splat)) wrap_arg = Ruby2Ruby::ASSIGN_NODES.include? arg_type arg = arg[2..-3] if strip_hash arg = "(#{arg})" if wrap_arg arg }.compact end case name when *BINARY then if safe_call "#{receiver}&.#{name}(#{args.join(", ")})" elsif args.length > 1 "#{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:
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 |
# File 'lib/ruby2ruby.rb', line 313 def process_case exp # :nodoc: _, expr, *rest = exp result = [] expr = process expr result << if expr then "case #{expr}" else "case" end result.concat rest.map { |pt| if pt and %i[when in].include? pt.sexp_type "#{process pt}" else next unless pt code = indent process pt code = indent("# do nothing") if code =~ /^\s*$/ "else\n#{code}" end } result << "end" result.compact.join "\n" end |
#process_cdecl(exp) ⇒ Object
:nodoc:
342 343 344 345 346 347 348 349 350 351 |
# File 'lib/ruby2ruby.rb', line 342 def process_cdecl exp # :nodoc: _, lhs, rhs = exp lhs = process lhs if Sexp === lhs if rhs then rhs = process rhs "#{lhs} = #{rhs}" else lhs.to_s end end |
#process_class(exp) ⇒ Object
:nodoc:
359 360 361 |
# File 'lib/ruby2ruby.rb', line 359 def process_class exp # :nodoc: "#{comments exp}class #{util_module_or_class(exp, true)}" end |
#process_colon2(exp) ⇒ Object
:nodoc:
363 364 365 366 367 |
# File 'lib/ruby2ruby.rb', line 363 def process_colon2 exp # :nodoc: _, lhs, rhs = exp "#{process lhs}::#{rhs}" end |
#process_colon3(exp) ⇒ Object
:nodoc:
369 370 371 372 373 |
# File 'lib/ruby2ruby.rb', line 369 def process_colon3 exp # :nodoc: _, rhs = exp "::#{rhs}" end |
#process_const(exp) ⇒ Object
:nodoc:
375 376 377 378 379 380 381 |
# File 'lib/ruby2ruby.rb', line 375 def process_const exp # :nodoc: _, name = exp name = process name if Sexp === name name.to_s end |
#process_cvar(exp) ⇒ Object
:nodoc:
394 395 396 397 398 |
# File 'lib/ruby2ruby.rb', line 394 def process_cvar exp # :nodoc: _, name = exp __var name end |
#process_cvasgn(exp) ⇒ Object
:nodoc:
400 401 402 403 404 |
# File 'lib/ruby2ruby.rb', line 400 def process_cvasgn exp # :nodoc: _, lhs, rhs = exp "#{lhs} = #{process rhs}" end |
#process_cvdecl(exp) ⇒ Object
:nodoc:
406 407 408 409 410 |
# File 'lib/ruby2ruby.rb', line 406 def process_cvdecl exp # :nodoc: _, lhs, rhs = exp "#{lhs} = #{process rhs}" end |
#process_defined(exp) ⇒ Object
:nodoc:
412 413 414 415 |
# File 'lib/ruby2ruby.rb', line 412 def process_defined exp # :nodoc: _, rhs = exp "defined? #{process rhs}" end |
#process_defn(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 417 def process_defn(exp) # :nodoc: _, name, args, *body = exp comm = comments exp args = process args args = "" if args == "()" body = s() if body == s(s(:nil)) # empty it out of a default nil expression # s(:defn, name, args, ivar|iasgn) case exp when s{ q(:defn, atom, t(:args), q(:ivar, atom)) } then # TODO: atom -> _ _, ivar = body.first ivar = ivar.to_s[1..-1] # remove leading @ reader = name.to_s return "attr_reader #{name.inspect}" if reader == ivar when s{ q(:defn, atom, t(:args), q(:iasgn, atom, q(:lvar, atom))) } then _, ivar, _val = body.first ivar = ivar.to_s[1..-1] # remove leading @ reader = name.to_s.chomp "=" return "attr_writer :#{reader}" if reader == ivar end body = body.map { |ssexp| process ssexp } simple = body.size <= 1 body << "# do nothing" if body.empty? body = body.join("\n") body = body.lines.to_a[1..-2].join("\n") if simple && body =~ /^\Abegin/ && body =~ /^end\z/ body = indent(body) unless simple && body =~ /(^|\n)rescue/ "#{comm}def #{name}#{args}\n#{body}\nend".gsub(/\n\s*\n+/, "\n") end |
#process_defs(exp) ⇒ Object
:nodoc:
455 456 457 458 459 460 461 462 463 464 465 |
# File 'lib/ruby2ruby.rb', line 455 def process_defs exp # :nodoc: _, lhs, name, args, *body = exp var = [:self, :cvar, :dvar, :ivar, :gvar, :lvar].include? lhs.sexp_type lhs = process lhs lhs = "(#{lhs})" unless var name = "#{lhs}.#{name}" process_defn s(:defn, name, args, *body) end |
#process_dot2(exp) ⇒ Object
:nodoc:
467 468 469 470 471 |
# File 'lib/ruby2ruby.rb', line 467 def process_dot2(exp) # :nodoc: _, lhs, rhs = exp "(#{process lhs}..#{process rhs})" end |
#process_dot3(exp) ⇒ Object
:nodoc:
473 474 475 476 477 |
# File 'lib/ruby2ruby.rb', line 473 def process_dot3(exp) # :nodoc: _, lhs, rhs = exp "(#{process lhs}...#{process rhs})" end |
#process_dregx(exp) ⇒ Object
:nodoc:
479 480 481 482 483 484 485 |
# File 'lib/ruby2ruby.rb', line 479 def process_dregx exp # :nodoc: _, str, *rest = exp = re_opt rest.pop if Integer === rest.last "/" + util_dthing(:dregx, s(:dregx, str, *rest)) << "/#{}" end |
#process_dregx_once(exp) ⇒ Object
:nodoc:
487 488 489 |
# File 'lib/ruby2ruby.rb', line 487 def process_dregx_once(exp) # :nodoc: process_dregx(exp) + "o" end |
#process_dstr(exp) ⇒ Object
:nodoc:
491 492 493 |
# File 'lib/ruby2ruby.rb', line 491 def process_dstr(exp) # :nodoc: "\"#{util_dthing(:dstr, exp)}\"" end |
#process_dsym(exp) ⇒ Object
:nodoc:
495 496 497 |
# File 'lib/ruby2ruby.rb', line 495 def process_dsym(exp) # :nodoc: ":\"#{util_dthing(:dsym, exp)}\"" end |
#process_dxstr(exp) ⇒ Object
:nodoc:
499 500 501 |
# File 'lib/ruby2ruby.rb', line 499 def process_dxstr(exp) # :nodoc: "`#{util_dthing(:dxstr, exp)}`" end |
#process_ensure(exp) ⇒ Object
:nodoc:
503 504 505 506 507 508 509 510 511 512 513 514 515 |
# File 'lib/ruby2ruby.rb', line 503 def process_ensure(exp) # :nodoc: _, body, ens = exp body = process body ens = nil if ens == s(:nil) ens = process(ens) || "# do nothing" ens = "begin\n#{ens}\nend\n" if ens =~ /(^|\n)rescue/ body = body.sub(/\n\s*end\z/, "") body = indent(body) unless body =~ /(^|\n)rescue/ "#{body}\nensure\n#{indent ens}" end |
#process_evstr(exp) ⇒ Object
:nodoc:
517 518 519 520 521 |
# File 'lib/ruby2ruby.rb', line 517 def process_evstr exp # :nodoc: _, x = exp x ? process(x) : "" end |
#process_false(exp) ⇒ Object
:nodoc:
523 524 525 |
# File 'lib/ruby2ruby.rb', line 523 def process_false exp # :nodoc: "false" end |
#process_find_pat(exp) ⇒ Object
:nodoc:
616 617 618 619 620 621 622 623 624 625 |
# File 'lib/ruby2ruby.rb', line 616 def process_find_pat exp # :nodoc: _, a, b, *c, d = exp a = process a # might be nil b = process b if Sexp === b c = c.map { |sexp| process sexp }.join(", ") d = process d if Sexp === d "%s[%s, %s, %s]" % [a, b, c, d] end |
#process_flip2(exp) ⇒ Object
:nodoc:
527 528 529 530 531 |
# File 'lib/ruby2ruby.rb', line 527 def process_flip2 exp # :nodoc: _, lhs, rhs = exp "#{process lhs}..#{process rhs}" end |
#process_flip3(exp) ⇒ Object
:nodoc:
533 534 535 536 537 |
# File 'lib/ruby2ruby.rb', line 533 def process_flip3 exp # :nodoc: _, lhs, rhs = exp "#{process lhs}...#{process rhs}" end |
#process_for(exp) ⇒ Object
:nodoc:
539 540 541 542 543 544 545 546 547 548 549 550 551 |
# File 'lib/ruby2ruby.rb', line 539 def process_for exp # :nodoc: _, recv, iter, body = exp recv = process recv iter = process iter body = process(body) || "# do nothing" result = ["for #{iter} in #{recv} do"] result << indent(body) result << "end" result.join "\n" end |
#process_forward_args(exp) ⇒ Object
553 554 555 |
# File 'lib/ruby2ruby.rb', line 553 def process_forward_args exp "..." end |
#process_gasgn(exp) ⇒ Object
:nodoc:
557 558 559 |
# File 'lib/ruby2ruby.rb', line 557 def process_gasgn exp # :nodoc: process_iasgn exp end |
#process_gvar(exp) ⇒ Object
:nodoc:
561 562 563 564 565 |
# File 'lib/ruby2ruby.rb', line 561 def process_gvar exp # :nodoc: _, name = exp __var name end |
#process_hash(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 567 def process_hash(exp) # :nodoc: _, *pairs = exp result = pairs.each_slice(2).map { |k, v| if k.sexp_type == :kwsplat then "%s" % process(k) elsif v.nil? # Shorthand hash syntax unless k.sexp_type == :lit and k.value.is_a? Symbol raise "Expected symbol for hash key, but got #{k.inspect}" end "#{k.value}:" else t = v.sexp_type lhs = process k rhs = process v rhs = "(#{rhs})" unless HASH_VAL_NO_PAREN.include? t "%s => %s" % [lhs, rhs] end } result.empty? ? "{}" : "{ #{result.join(", ")} }" end |
#process_hash_pat(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 650 def process_hash_pat exp # :nodoc: _, const, *rest = exp if const then const = process const kwrest = process rest.pop if rest.last && rest.last.sexp_type == :kwrest rest = rest.each_slice(2).map { |k, v| k = process(k).delete_prefix ":" v = process v "#{k}: #{v}".strip } rest << kwrest if kwrest rest.empty? ? "%s[]" % const : "%s[%s]" % [const, rest.join(", ")] else kwrest = process rest.pop if rest.last && rest.last.sexp_type == :kwrest rest = rest.each_slice(2).map { |k, v| k = process(k).delete_prefix ":" v = process v "#{k}: #{v}".strip } rest << kwrest if kwrest rest.empty? ? "{}" : "{ %s }" % [rest.join(", ")] end end |
#process_iasgn(exp) ⇒ Object
:nodoc:
594 595 596 597 598 599 600 601 602 |
# File 'lib/ruby2ruby.rb', line 594 def process_iasgn(exp) # :nodoc: _, lhs, rhs = exp if rhs then "#{lhs} = #{process rhs}" else # part of an masgn lhs.to_s end end |
#process_if(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 687 def process_if(exp) # :nodoc: _, c, t, f = exp = Ruby2Ruby::ASSIGN_NODES.include? c.sexp_type c = process c t = process t f = process f 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 = [] r << "if #{c} then\n#{indent(t)}\n" r << "else\n#{indent(f)}\n" if f r << "end" r.join 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_in(exp) ⇒ Object
:nodoc:
604 605 606 607 608 609 610 611 612 613 614 |
# File 'lib/ruby2ruby.rb', line 604 def process_in exp # :nodoc: _, lhs, *rhs = exp cond = process lhs body = rhs.compact.map { |sexp| indent process sexp } body << indent("# do nothing") if body.empty? body = body.join "\n" "in #{cond} then\n#{body.chomp}" end |
#process_iter(exp) ⇒ Object
:nodoc:
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 |
# File 'lib/ruby2ruby.rb', line 732 def process_iter(exp) # :nodoc: _, iter, args, body = exp is_lambda = iter.sexp_type == :lambda iter = process iter body = process body if body args = case when args == 0 then "" when is_lambda then " (#{process(args)[1..-2]})" else " |#{process(args)[1..-2]}|" end b, e = if MUST_BE_CURLY.include? iter then %w[ { } ] else %w[ do end ] end iter = iter.sub(/\(\)$/, "") # REFACTOR: ugh result = [] if is_lambda then result << iter result << args result << " {" else result << "#{iter} {" result << args end result << if body then " #{body.strip} " else " " end result << "}" result = result.join return result if result !~ /\n/ and result.size < LINE_LENGTH result = [] if is_lambda then result << iter result << args result << " #{b}" else result << "#{iter} #{b}" result << args end result << "\n" if body then result << indent(body.strip) result << "\n" end result << e result.join end |
#process_ivar(exp) ⇒ Object
:nodoc:
796 797 798 799 800 |
# File 'lib/ruby2ruby.rb', line 796 def process_ivar(exp) # :nodoc: _, name = exp __var name end |
#process_kwrest(exp) ⇒ Object
:nodoc:
644 645 646 647 648 |
# File 'lib/ruby2ruby.rb', line 644 def process_kwrest exp # :nodoc: _, name = exp name.to_s end |
#process_kwsplat(exp) ⇒ Object
802 803 804 805 |
# File 'lib/ruby2ruby.rb', line 802 def process_kwsplat(exp) _, kw = exp "**#{process kw}" end |
#process_lambda(exp) ⇒ Object
:nodoc:
726 727 728 |
# File 'lib/ruby2ruby.rb', line 726 def process_lambda exp # :nodoc: "->" end |
#process_lasgn(exp) ⇒ Object
:nodoc:
807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 |
# File 'lib/ruby2ruby.rb', line 807 def process_lasgn(exp) # :nodoc: _, name, value = exp value = process value if value then "%s %s %s" % case context[1] when /_pat$/ then [value, "=>", name] else [name, "=", value] end else "%s" % [name] end end |
#process_lit(exp) ⇒ Object
:nodoc:
824 825 826 827 828 829 830 831 832 |
# File 'lib/ruby2ruby.rb', line 824 def process_lit exp # :nodoc: _, obj = exp case obj when Range then "(#{obj.inspect})" else obj.inspect end end |
#process_lvar(exp) ⇒ Object
:nodoc:
834 835 836 837 838 |
# File 'lib/ruby2ruby.rb', line 834 def process_lvar(exp) # :nodoc: _, name = exp __var name end |
#process_masgn(exp) ⇒ Object
:nodoc:
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 |
# File 'lib/ruby2ruby.rb', line 840 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>) parenthesize = true result = exp.sexp_body.map { |sexp| case sexp when Sexp then if sexp.sexp_type == :array then parenthesize = context.grep(:masgn).size > 1 res = process sexp res[1..-2] else process sexp end when Symbol then sexp else raise "unknown masgn: #{sexp.inspect}" end } parenthesize ? "(#{result.join ", "})" : result.join(" = ") end |
#process_match(exp) ⇒ Object
:nodoc:
865 866 867 868 869 |
# File 'lib/ruby2ruby.rb', line 865 def process_match exp # :nodoc: _, rhs = exp "#{process rhs}" end |
#process_match2(exp) ⇒ Object
:nodoc:
871 872 873 874 875 876 877 878 879 |
# File 'lib/ruby2ruby.rb', line 871 def process_match2 exp # :nodoc: # s(:match2, s(:lit, /x/), s(:str, "blah")) _, lhs, rhs = exp lhs = process lhs rhs = process rhs "#{lhs} =~ #{rhs}" end |
#process_match3(exp) ⇒ Object
:nodoc:
881 882 883 884 885 886 887 888 889 890 891 892 893 |
# File 'lib/ruby2ruby.rb', line 881 def process_match3 exp # :nodoc: _, rhs, lhs = exp # yes, backwards left_type = lhs.sexp_type lhs = process lhs rhs = process rhs if ASSIGN_NODES.include? left_type then "(#{lhs}) =~ #{rhs}" else "#{lhs} =~ #{rhs}" end end |
#process_module(exp) ⇒ Object
:nodoc:
895 896 897 |
# File 'lib/ruby2ruby.rb', line 895 def process_module(exp) # :nodoc: "#{comments exp}module #{util_module_or_class(exp)}" end |
#process_next(exp) ⇒ Object
:nodoc:
899 900 901 902 903 904 905 906 907 908 |
# File 'lib/ruby2ruby.rb', line 899 def process_next(exp) # :nodoc: _, rhs = exp val = rhs && process(rhs) # maybe push down into if and test rhs? if val then "next #{val}" else "next" end end |
#process_nil(exp) ⇒ Object
:nodoc:
910 911 912 |
# File 'lib/ruby2ruby.rb', line 910 def process_nil(exp) # :nodoc: "nil" end |
#process_not(exp) ⇒ Object
:nodoc:
914 915 916 917 |
# File 'lib/ruby2ruby.rb', line 914 def process_not(exp) # :nodoc: _, sexp = exp "(not #{process sexp})" end |
#process_nth_ref(exp) ⇒ Object
:nodoc:
919 920 921 922 |
# File 'lib/ruby2ruby.rb', line 919 def process_nth_ref(exp) # :nodoc: _, n = exp "$#{n}" end |
#process_op_asgn(exp) ⇒ Object
:nodoc:
924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 |
# File 'lib/ruby2ruby.rb', line 924 def process_op_asgn exp # :nodoc: case exp.length when 4 _, lhs, op, rhs = exp lhs = process lhs rhs = process rhs "#{lhs} #{op}= #{rhs}" when 5 _, lhs, rhs, index, op = exp lhs = process lhs rhs = process rhs "#{lhs}.#{index} #{op}= #{rhs}" else raise ArgumentError, "Don't know how to process this length: %p" % [exp] end end |
#process_op_asgn1(exp) ⇒ Object
:nodoc:
945 946 947 948 949 950 951 952 953 954 |
# File 'lib/ruby2ruby.rb', line 945 def process_op_asgn1(exp) # :nodoc: # [[:lvar, :b], [:arglist, [:lit, 1]], :"||", [:lit, 10]] _, lhs, index, msg, rhs = exp lhs = process lhs index = process index rhs = process rhs "#{lhs}[#{index}] #{msg}= #{rhs}" end |
#process_op_asgn2(exp) ⇒ Object
:nodoc:
956 957 958 959 960 961 962 963 964 965 |
# File 'lib/ruby2ruby.rb', line 956 def process_op_asgn2 exp # :nodoc: # [[:lvar, :c], :var=, :"||", [:lit, 20]] _, lhs, index, msg, rhs = exp lhs = process lhs index = index.to_s[0..-2] rhs = process rhs "#{lhs}.#{index} #{msg}= #{rhs}" end |
#process_op_asgn_and(exp) ⇒ Object
:nodoc:
967 968 969 970 971 972 |
# File 'lib/ruby2ruby.rb', line 967 def process_op_asgn_and(exp) # :nodoc: # a &&= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] _, _lhs, rhs = exp process(rhs).sub(/\=/, "&&=") end |
#process_op_asgn_or(exp) ⇒ Object
:nodoc:
974 975 976 977 978 979 |
# File 'lib/ruby2ruby.rb', line 974 def process_op_asgn_or(exp) # :nodoc: # a ||= 1 # [[:lvar, :a], [:lasgn, :a, [:lit, 1]]] _, _lhs, rhs = exp process(rhs).sub(/\=/, "||=") end |
#process_or(exp) ⇒ Object
:nodoc:
981 982 983 984 985 986 987 988 989 990 991 992 993 |
# File 'lib/ruby2ruby.rb', line 981 def process_or(exp) # :nodoc: _, lhs, rhs = exp lhs = process lhs rhs = process rhs case context[1] when :in then "(#{lhs} | #{rhs})" else "(#{lhs} or #{rhs})" end end |
#process_postexe(exp) ⇒ Object
:nodoc:
995 996 997 |
# File 'lib/ruby2ruby.rb', line 995 def process_postexe(exp) # :nodoc: "END" end |
#process_preexe(exp) ⇒ Object
:nodoc:
683 684 685 |
# File 'lib/ruby2ruby.rb', line 683 def process_preexe exp # :nodoc: "BEGIN" end |
#process_redo(exp) ⇒ Object
:nodoc:
999 1000 1001 |
# File 'lib/ruby2ruby.rb', line 999 def process_redo(exp) # :nodoc: "redo" end |
#process_resbody(exp) ⇒ Object
:nodoc:
1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 |
# File 'lib/ruby2ruby.rb', line 1003 def process_resbody exp # :nodoc: # s(:resbody, s(:array), s(:return, s(:str, "a"))) _, args, *body = exp body = body.compact.map { |sexp| process sexp } 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:
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 |
# File 'lib/ruby2ruby.rb', line 1022 def process_rescue exp # :nodoc: _, *rest = exp body = process rest.shift unless rest.first.sexp_type == :resbody els = process rest.pop unless rest.last && rest.last.sexp_type == :resbody body ||= "# do nothing" # TODO: I don't like this using method_missing, but I need to ensure tests simple = rest.size == 1 && rest.first.size <= 3 && !rest.first.block && !rest.first.return resbodies = rest.map { |resbody| _, rb_args, rb_body, *rb_rest = resbody simple &&= rb_args == s(:array) simple &&= rb_rest.empty? && rb_body && rb_body.node_type != :block process resbody } 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:
1052 1053 1054 |
# File 'lib/ruby2ruby.rb', line 1052 def process_retry(exp) # :nodoc: "retry" end |
#process_return(exp) ⇒ Object
:nodoc:
1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 |
# File 'lib/ruby2ruby.rb', line 1056 def process_return exp # :nodoc: _, rhs = exp unless rhs then "return" else rhs_type = rhs.sexp_type rhs = process rhs rhs = "(#{rhs})" if ASSIGN_NODES.include? rhs_type "return #{rhs}" end end |
#process_safe_attrasgn(exp) ⇒ Object
:nodoc:
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 |
# File 'lib/ruby2ruby.rb', line 1069 def process_safe_attrasgn exp # :nodoc: _, receiver, name, *rest = exp receiver = process receiver rhs = rest.pop args = rest.pop # should be nil 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:
309 310 311 |
# File 'lib/ruby2ruby.rb', line 309 def process_safe_call exp # :nodoc: process_call exp, :safe end |
#process_safe_op_asgn(exp) ⇒ Object
:nodoc:
1087 1088 1089 1090 1091 1092 1093 1094 1095 |
# File 'lib/ruby2ruby.rb', line 1087 def process_safe_op_asgn exp # :nodoc: # [[:lvar, :x], [:call, nil, :z, [:lit, 1]], :y, :"||"] _, lhs, rhs, index, op = exp lhs = process lhs rhs = process rhs "#{lhs}&.#{index} #{op}= #{rhs}" end |
#process_safe_op_asgn2(exp) ⇒ Object
:nodoc:
1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 |
# File 'lib/ruby2ruby.rb', line 1097 def process_safe_op_asgn2(exp) # :nodoc: # [[:lvar, :c], :var=, :"||", [:lit, 20]] _, lhs, index, msg, rhs = exp lhs = process lhs index = index.to_s[0..-2] rhs = process rhs "#{lhs}&.#{index} #{msg}= #{rhs}" end |
#process_sclass(exp) ⇒ Object
:nodoc:
1109 1110 1111 1112 1113 1114 1115 1116 |
# File 'lib/ruby2ruby.rb', line 1109 def process_sclass(exp) # :nodoc: _, recv, *block = exp recv = process recv block = indent process_block s(:block, *block) "class << #{recv}\n#{block}\nend" end |
#process_self(exp) ⇒ Object
:nodoc:
1118 1119 1120 |
# File 'lib/ruby2ruby.rb', line 1118 def process_self(exp) # :nodoc: "self" end |
#process_splat(exp) ⇒ Object
:nodoc:
1122 1123 1124 1125 1126 1127 1128 1129 |
# File 'lib/ruby2ruby.rb', line 1122 def process_splat(exp) # :nodoc: _, arg = exp if arg.nil? then "*" else "*#{process arg}" end end |
#process_str(exp) ⇒ Object
:nodoc:
1131 1132 1133 1134 |
# File 'lib/ruby2ruby.rb', line 1131 def process_str(exp) # :nodoc: _, s = exp s.dump end |
#process_super(exp) ⇒ Object
:nodoc:
1136 1137 1138 1139 1140 1141 1142 1143 1144 |
# File 'lib/ruby2ruby.rb', line 1136 def process_super(exp) # :nodoc: _, *args = exp args = args.map { |arg| process arg } "super(#{args.join ", "})" end |
#process_svalue(exp) ⇒ Object
:nodoc:
1146 1147 1148 1149 1150 1151 1152 |
# File 'lib/ruby2ruby.rb', line 1146 def process_svalue(exp) # :nodoc: _, *args = exp args.map { |arg| process arg }.join ", " end |
#process_to_ary(exp) ⇒ Object
:nodoc:
1154 1155 1156 1157 1158 |
# File 'lib/ruby2ruby.rb', line 1154 def process_to_ary exp # :nodoc: _, sexp = exp process sexp end |
#process_true(exp) ⇒ Object
:nodoc:
1160 1161 1162 |
# File 'lib/ruby2ruby.rb', line 1160 def process_true(exp) # :nodoc: "true" end |
#process_undef(exp) ⇒ Object
:nodoc:
1164 1165 1166 1167 1168 |
# File 'lib/ruby2ruby.rb', line 1164 def process_undef(exp) # :nodoc: _, name = exp "undef #{process name}" end |
#process_until(exp) ⇒ Object
:nodoc:
1170 1171 1172 |
# File 'lib/ruby2ruby.rb', line 1170 def process_until(exp) # :nodoc: cond_loop(exp, "until") end |
#process_valias(exp) ⇒ Object
:nodoc:
1174 1175 1176 1177 1178 |
# File 'lib/ruby2ruby.rb', line 1174 def process_valias exp # :nodoc: _, lhs, rhs = exp "alias #{lhs} #{rhs}" end |
#process_when(exp) ⇒ Object
:nodoc:
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 |
# File 'lib/ruby2ruby.rb', line 1180 def process_when exp # :nodoc: _, lhs, *rhs = exp cond = process(lhs)[1..-2] rhs = rhs.compact.map { |sexp| indent process sexp } rhs << indent("# do nothing") if rhs.empty? rhs = rhs.join "\n" "when #{cond} then\n#{rhs.chomp}" end |
#process_while(exp) ⇒ Object
:nodoc:
1195 1196 1197 |
# File 'lib/ruby2ruby.rb', line 1195 def process_while(exp) # :nodoc: cond_loop exp, "while" end |
#process_xstr(exp) ⇒ Object
:nodoc:
1199 1200 1201 |
# File 'lib/ruby2ruby.rb', line 1199 def process_xstr(exp) # :nodoc: "`#{process_str(exp)[1..-2]}`" end |
#process_yield(exp) ⇒ Object
:nodoc:
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 |
# File 'lib/ruby2ruby.rb', line 1203 def process_yield(exp) # :nodoc: _, *args = exp args = args.map { |arg| process arg } unless args.empty? then "yield(#{args.join(", ")})" else "yield" end end |
#process_zsuper(exp) ⇒ Object
:nodoc:
1217 1218 1219 |
# File 'lib/ruby2ruby.rb', line 1217 def process_zsuper(exp) # :nodoc: "super" end |
#re_opt(options) ⇒ Object
Return the appropriate regexp flags for a given numeric code.
1383 1384 1385 1386 1387 |
# File 'lib/ruby2ruby.rb', line 1383 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:
1224 1225 1226 1227 1228 1229 1230 1231 1232 |
# File 'lib/ruby2ruby.rb', line 1224 def rewrite_attrasgn exp # :nodoc: if context.first(2) == [:array, :masgn] then _, recv, msg, *args = exp exp = s(:call, recv, msg.to_s.chomp("=").to_sym, *args) end exp end |
#rewrite_call(exp) ⇒ Object
:nodoc:
1234 1235 1236 1237 1238 1239 1240 |
# File 'lib/ruby2ruby.rb', line 1234 def rewrite_call exp # :nodoc: _, recv, msg, *args = exp exp = s(:not, recv) if msg == :! && args.empty? exp end |
#rewrite_ensure(exp) ⇒ Object
:nodoc:
1242 1243 1244 1245 |
# File 'lib/ruby2ruby.rb', line 1242 def rewrite_ensure exp # :nodoc: exp = s(:begin, exp) unless context.first == :begin exp end |
#rewrite_if(exp) ⇒ Object
:nodoc:
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 |
# File 'lib/ruby2ruby.rb', line 1247 def rewrite_if exp # :nodoc: _, c, t, f = exp if c.sexp_type == :not then _, nc = c exp = s(:if, nc, f, t) end exp end |
#rewrite_resbody(exp) ⇒ Object
:nodoc:
1258 1259 1260 1261 1262 1263 1264 |
# File 'lib/ruby2ruby.rb', line 1258 def rewrite_resbody exp # :nodoc: _, args, *_rest = exp raise "no exception list in #{exp.inspect}" unless exp.size > 2 && args raise args.inspect if args.sexp_type != :array # for now, do nothing, just check and freak if we see an errant structure exp end |
#rewrite_rescue(exp) ⇒ Object
:nodoc:
1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 |
# File 'lib/ruby2ruby.rb', line 1266 def rewrite_rescue exp # :nodoc: complex = false complex ||= exp.size > 3 complex ||= exp.resbody.block complex ||= exp.resbody.size > 3 resbodies = exp.find_nodes(:resbody) complex ||= resbodies.any? { |n| n[1] != s(:array) } complex ||= resbodies.any? { |n| n.last.nil? } complex ||= resbodies.any? { |(_, _, body)| body and body.node_type == :block } handled = context.first == :ensure exp = s(:begin, exp) if complex unless handled exp end |
#rewrite_svalue(exp) ⇒ Object
:nodoc:
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 |
# File 'lib/ruby2ruby.rb', line 1283 def rewrite_svalue(exp) # :nodoc: case exp.last.sexp_type when :array s(:svalue, *exp[1].sexp_body) when :splat exp else raise "huh: #{exp.inspect}" end end |
#rewrite_until(exp) ⇒ Object
:nodoc:
1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 |
# File 'lib/ruby2ruby.rb', line 1294 def rewrite_until exp # :nodoc: _, c, *body = exp if c.sexp_type == :not then _, nc = c exp = s(:while, nc, *body) end exp end |
#rewrite_while(exp) ⇒ Object
:nodoc:
1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 |
# File 'lib/ruby2ruby.rb', line 1305 def rewrite_while exp # :nodoc: _, c, *body = exp if c.sexp_type == :not then _, nc = c exp = s(:until, nc, *body) end exp end |
#splat(sym) ⇒ Object
Return a splatted symbol for sym.
1392 1393 1394 |
# File 'lib/ruby2ruby.rb', line 1392 def splat(sym) :"*#{sym}" end |
#util_dthing(type, exp) ⇒ Object
Utility method to generate something interpolated.
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 |
# File 'lib/ruby2ruby.rb', line 1399 def util_dthing(type, exp) _, str, *rest = exp # first item in sexp is a string literal str = dthing_escape(type, str) rest = rest.map { |pt| case pt.sexp_type when :str then dthing_escape(type, pt.last) when :evstr then '#{%s}' % [process(pt)] else raise "unknown type: #{pt.inspect}" end } [str, rest].join end |
#util_module_or_class(exp, is_class = false) ⇒ Object
Utility method to generate ether a module or class.
1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 |
# File 'lib/ruby2ruby.rb', line 1422 def util_module_or_class exp, is_class = false result = [] _, name, *body = exp superk = body.shift if is_class name = process name if Sexp === name result << name if superk then superk = process superk result << " < #{superk}" if superk end result << "\n" body = body.map { |sexp| process(sexp).chomp } body = unless body.empty? then indent(body.join("\n\n")) + "\n" else "" end result << body result << "end" result.join end |