class RipperRubyParser::SexpProcessor

Processes the sexp created by Ripper to what RubyParser would produce.

Attributes

extra_compatible[RW]
filename[RW]

Public Instance Methods

process(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 37
def process exp
  return nil if exp.nil?
  exp.fix_empty_type

  result = super
  trickle_up_line_numbers result
  trickle_down_line_numbers result
end
process_BEGIN(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 132
def process_BEGIN exp
  _, body = exp.shift 2
  s(:iter, s(:preexe), s(:args), *map_body(body))
end
process_END(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 137
def process_END exp
  _, body = exp.shift 2
  s(:iter, s(:postexe), s(:args), *map_body(body))
end
process_at_CHAR(exp) click to toggle source

character literals

# File lib/ripper_ruby_parser/sexp_processor.rb, line 152
def process_at_CHAR exp
  _, val, pos = exp.shift 3
  with_position(pos, s(:str, unescape(val[1..-1])))
end
process_at_backref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 199
def process_at_backref exp
  _, str, pos = exp.shift 3
  name = str[1..-1]
  with_position pos do
    if name =~ /[0-9]/
      s(:nth_ref, name.to_i)
    else
      s(:back_ref, name.to_sym)
    end
  end
end
process_at_const(exp) click to toggle source

symbol-like sexps

# File lib/ripper_ruby_parser/sexp_processor.rb, line 162
def process_at_const exp
  make_identifier(:const, exp)
end
process_at_cvar(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 166
def process_at_cvar exp
  make_identifier(:cvar, exp)
end
process_at_float(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 147
def process_at_float exp
  make_literal(exp) {|val| val.to_f }
end
process_at_gvar(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 170
def process_at_gvar exp
  make_identifier(:gvar, exp)
end
process_at_ident(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 178
def process_at_ident exp
  make_identifier(:lvar, exp)
end
process_at_int(exp) click to toggle source

number literals

# File lib/ripper_ruby_parser/sexp_processor.rb, line 143
def process_at_int exp
  make_literal(exp) {|val| Integer(val) }
end
process_at_ivar(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 174
def process_at_ivar exp
  make_identifier(:ivar, exp)
end
process_at_kw(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 186
def process_at_kw exp
  sym, pos = extract_node_symbol_with_position(exp)
  result = case sym
           when :__FILE__
             s(:str, @filename)
           when :__LINE__
             s(:lit, pos[0])
           else
             s(sym)
           end
  with_position(pos, result)
end
process_at_label(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 157
def process_at_label exp
  make_literal(exp) {|val| val.chop.to_sym }
end
process_at_op(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 182
def process_at_op exp
  make_identifier(:op, exp)
end
process_class(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 62
def process_class exp
  _, const_ref, parent, body = exp.shift 4
  const, line = const_ref_to_const_with_line_number const_ref
  parent = process(parent)
  with_line_number(line,
                   s(:class, const, parent, *class_or_module_body(body)))
end
process_comment(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 125
def process_comment exp
  _, comment, inner = exp.shift 3
  sexp = process(inner)
  sexp.comments = comment
  sexp
end
process_const_path_field(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 95
def process_const_path_field exp
  s(:const, process_const_path_ref(exp))
end
process_const_path_ref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 90
def process_const_path_ref exp
  _, left, right = exp.shift 3
  s(:colon2, process(left), extract_node_symbol(right))
end
process_const_ref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 99
def process_const_ref exp
  _, ref = exp.shift 3
  process(ref)
end
process_module(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 55
def process_module exp
  _, const_ref, body = exp.shift 3
  const, line = const_ref_to_const_with_line_number const_ref
  with_line_number(line,
                   s(:module, const, *class_or_module_body(body)))
end
process_paren(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 113
def process_paren exp
  _, body = exp.shift 2
  if body.size == 0
    s()
  elsif body.first.is_a? Symbol
    process body
  else
    body.map! {|it| convert_void_stmt_to_nil process it }
    safe_wrap_in_block body
  end
end
process_program(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 48
def process_program exp
  _, content = exp.shift 2

  statements = content.map { |sub_exp| process(sub_exp) }
  safe_wrap_in_block statements
end
process_sclass(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 70
def process_sclass exp
  _, klass, block = exp.shift 3
  s(:sclass, process(klass), *class_or_module_body(block))
end
process_top_const_field(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 109
def process_top_const_field exp
  s(:const, process_top_const_ref(exp))
end
process_top_const_ref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 104
def process_top_const_ref exp
  _, ref = exp.shift 2
  s(:colon3, extract_node_symbol(ref))
end
process_var_alias(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 85
def process_var_alias exp
  _, left, right = exp.shift 3
  s(:valias, left[1].to_sym, right[1].to_sym)
end
process_var_field(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 80
def process_var_field exp
  _, contents = exp.shift 2
  process(contents)
end
process_var_ref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 75
def process_var_ref exp
  _, contents = exp.shift 2
  process(contents)
end

Public Class Methods

new() click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 11
def initialize
  super

  # TODO: Find these automatically

  @processors[:@int] = :process_at_int
  @processors[:@float] = :process_at_float
  @processors[:@CHAR] = :process_at_CHAR
  @processors[:@label] = :process_at_label

  @processors[:@const] = :process_at_const
  @processors[:@ident] = :process_at_ident
  @processors[:@cvar] = :process_at_cvar
  @processors[:@gvar] = :process_at_gvar
  @processors[:@ivar] = :process_at_ivar
  @processors[:@kw] = :process_at_kw
  @processors[:@op] = :process_at_op
  @processors[:@backref] = :process_at_backref

  @processors[:@tstring_content] = :process_at_tstring_content

  @errors = []

  @in_method_body = false
end