module RipperRubyParser::SexpHandlers::Literals

Sexp handlers for literals

Public Instance Methods

process_array(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 13
def process_array(exp)
  _, elems = exp.shift 2
  return s(:array) if elems.nil?

  process(elems).tap { |arr| arr.sexp_type = :array }
end
process_assoc_splat(exp) click to toggle source

@example

s(:assoc_splat, s(:vcall, s(:@ident, "bar")))
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 37
def process_assoc_splat(exp)
  _, param = exp.shift 2
  s(:kwsplat, process(param))
end
process_at_CHAR(exp) click to toggle source

character literals

# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 8
def process_at_CHAR(exp)
  _, val, pos = exp.shift 3
  with_position(pos, s(:str, fix_encoding(unescape(val[1..]))))
end
process_at_float(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 47
def process_at_float(exp)
  make_literal(exp, &:to_f)
end
process_at_imaginary(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 55
def process_at_imaginary(exp)
  make_literal(exp, &:to_c)
end
process_at_int(exp) click to toggle source

number literals

# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 43
def process_at_int(exp)
  make_literal(exp) { |val| Integer(val) }
end
process_at_rational(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 51
def process_at_rational(exp)
  make_literal(exp, &:to_r)
end
process_hash(exp) click to toggle source

Handle hash literals sexps. These can be either empty, or contain a nested :assoclist_from_args Sexp.

@example Empty hash

s(:hash, nil)

@example Hash with contents

s(:hash, s(:assoclist_from_args, ...))
# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 27
def process_hash(exp)
  _, body = exp.shift 2
  return s(:hash) unless body

  _, elems = body
  s(:hash, *make_hash_items(elems))
end

Private Instance Methods

make_hash_items(elems) click to toggle source

Process list of items that can be either :assoc_new or :assoc_splat

# File lib/ripper_ruby_parser/sexp_handlers/literals.rb, line 62
def make_hash_items(elems)
  result = s()
  elems.each do |sub_exp|
    if sub_exp.sexp_type == :assoc_new
      result += process(sub_exp).sexp_body
    else # :assoc_splat
      result << process(sub_exp)
    end
  end
  result
end