class RipperRubyParser::SexpProcessor

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

@api private

Attributes

extra_compatible[R]
filename[R]

Public Class Methods

new(filename: nil, extra_compatible: nil) click to toggle source
Calls superclass method
# File lib/ripper_ruby_parser/sexp_processor.rb, line 16
def initialize(filename: nil, extra_compatible: nil)
  super()

  public_methods.each do |name|
    if name =~ /^process_at_(.*)/
      @processors["@#{Regexp.last_match(1)}".to_sym] = name.to_sym
    end
  end

  @filename = filename
  @extra_compatible = extra_compatible

  @errors = []

  @in_method_body = false
  @kwrest = []
  @block_kwrest = []

  @kept_comment = nil
end

Public Instance Methods

process_BEGIN(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 136
def process_BEGIN(exp)
  _, body, pos = exp.shift 3
  body = reject_void_stmt map_process_list body.sexp_body
  with_position pos, s(:iter, s(:preexe), 0, *body)
end
process_END(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 142
def process_END(exp)
  _, body, pos = exp.shift 3
  body = map_process_list_compact body.sexp_body
  with_position pos, s(:iter, s(:postexe), 0, *body)
end
process_at_backref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 196
def process_at_backref(exp)
  _, str, pos = exp.shift 3
  name = str[1..]
  with_position pos do
    if /[0-9]/.match?(name)
      s(:nth_ref, name.to_i)
    else
      s(:back_ref, name.to_sym)
    end
  end
end
process_at_backtick(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 177
def process_at_backtick(exp)
  make_identifier(:backtick, exp)
end
process_at_const(exp) click to toggle source

symbol-like sexps

# File lib/ripper_ruby_parser/sexp_processor.rb, line 153
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 157
def process_at_cvar(exp)
  make_identifier(:cvar, exp)
end
process_at_gvar(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 161
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 169
def process_at_ident(exp)
  make_identifier(:lvar, exp)
end
process_at_ivar(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 165
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 181
def process_at_kw(exp)
  sym, pos = extract_node_symbol_with_position(exp)
  result = case sym
           when :__ENCODING__
             s(:colon2, s(:const, :Encoding), :UTF_8)
           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 148
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 173
def process_at_op(exp)
  make_identifier(:op, exp)
end
process_at_period(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 208
def process_at_period(exp)
  _, period, = exp.shift 3
  s(:period, period)
end
process_class(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 52
def process_class(exp)
  _, const_ref, parent, body, pos = exp.shift 5
  const = const_ref_to_const const_ref
  parent = process(parent)
  with_position(pos,
                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 122
def process_comment(exp)
  _, comment, inner = exp.shift 3
  comment = @kept_comment + comment if @kept_comment
  @kept_comment = nil
  sexp = process(inner)
  case sexp.sexp_type
  when :defs, :defn, :module, :class, :sclass
    sexp.comments = comment
  else
    @kept_comment = comment
  end
  sexp
end
process_const_path_field(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 98
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 93
def process_const_path_ref(exp)
  _, left, right = exp.shift 3
  s(:colon2, process(left), extract_node_symbol(process(right)))
end
process_const_ref(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 102
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 45
def process_module(exp)
  _, const_ref, body, pos = exp.shift 4
  const = const_ref_to_const const_ref
  with_position(pos,
                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 116
def process_paren(exp)
  _, body = exp.shift 2
  result = process body
  convert_void_stmt_to_nil_symbol result
end
process_program(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 39
def process_program(exp)
  _, content = exp.shift 2

  process content
end
process_sclass(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 60
def process_sclass(exp)
  _, klass, block, pos = exp.shift 4
  with_position pos, s(:sclass, process(klass), *class_or_module_body(block))
end
process_stmts(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 65
def process_stmts(exp)
  _, *statements = shift_all(exp)
  statements = map_unwrap_begin_list map_process_list statements
  line = statements.first.line
  statements = reject_void_stmt statements
  wrap_in_block(statements, line)
end
process_top_const_field(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 112
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 107
def process_top_const_ref(exp)
  _, ref = exp.shift 2
  s(:colon3, extract_node_symbol(process(ref)))
end
process_var_alias(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 83
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 78
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 73
def process_var_ref(exp)
  _, contents = exp.shift 2
  process(contents)
end
process_void_stmt(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 88
def process_void_stmt(exp)
  _, pos = exp.shift 2
  with_position pos, s(:void_stmt)
end

Private Instance Methods

class_or_module_body(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 221
def class_or_module_body(exp)
  body = process(exp)

  return [] if body.sexp_type == :void_stmt

  unwrap_block body
end
const_ref_to_const(const_ref) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 215
def const_ref_to_const(const_ref)
  const = process(const_ref)
  const = const[1] if const.sexp_type == :const
  const
end
make_identifier(type, exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 229
def make_identifier(type, exp)
  with_position_from_node_symbol(exp) do |ident|
    s(type, ident)
  end
end
make_literal(exp) { |val| ... } click to toggle source
# File lib/ripper_ruby_parser/sexp_processor.rb, line 235
def make_literal(exp)
  _, val, pos = exp.shift 3
  with_position(pos, s(:lit, yield(val)))
end