module RipperRubyParser::SexpHandlers::Methods

Sexp handers for method definitions and related constructs

Constants

SPECIAL_ARG_MARKER

Public Instance Methods

process_alias(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 69
def process_alias(exp)
  _, left, right = exp.shift 3

  s(:alias, process(left), process(right))
end
process_args_forward(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 75
def process_args_forward(exp)
  _ = exp.shift
  s(:forward_args)
end
process_def(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 7
def process_def(exp)
  _, ident, params, body, pos = exp.shift 5

  ident, = extract_node_symbol_with_position ident

  in_method do
    params = convert_arguments(process(params))
    kwrest = kwrest_param(params)
    body = with_kwrest(kwrest) { method_body(body) }
  end

  with_position(pos, s(:defn, ident, params, *body))
end
process_defs(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 21
def process_defs(exp)
  _, receiver, _, ident, params, body, = exp.shift 7

  ident, = extract_node_symbol_with_position ident

  in_method do
    params = convert_arguments(process(params))
    kwrest = kwrest_param(params)
    body = with_kwrest(kwrest) { method_body(body) }
  end

  s(:defs, process(receiver), ident, params, *body)
end
process_return(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 35
def process_return(exp)
  _, arglist = exp.shift 2
  s(:return, handle_return_argument_list(arglist))
end
process_return0(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 40
def process_return0(exp)
  exp.shift
  s(:return)
end
process_undef(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 55
def process_undef(exp)
  _, args = exp.shift 2

  args.map! do |sub_exp|
    s(:undef, process(sub_exp))
  end

  if args.size == 1
    args.first
  else
    s(:block, *args)
  end
end
process_yield(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 45
def process_yield(exp)
  _, arglist = exp.shift 2
  s(:yield, *process(arglist).sexp_body)
end
process_yield0(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 50
def process_yield0(exp)
  exp.shift
  s(:yield)
end

Private Instance Methods

convert_argument(item) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 111
def convert_argument(item)
  case item.sexp_type
  when :lvar
    item.last
  when *SPECIAL_ARG_MARKER.keys
    convert_marked_argument(item)
  when :masgn
    convert_masgn_argument(item)
  else
    item
  end
end
convert_arguments(args) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 105
def convert_arguments(args)
  args.line ||= args.sexp_body.first&.line
  args.sexp_body = args.sexp_body.map { |item| convert_argument item }
  args
end
convert_destructuring_arguments(args) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 136
def convert_destructuring_arguments(args)
  args.map! do |item|
    case item.sexp_type
    when :splat
      convert_marked_argument(item)
    when :masgn
      convert_masgn_argument(item)
    when :lasgn
      item[1]
    end
  end
end
convert_marked_argument(item) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 124
def convert_marked_argument(item)
  marker = SPECIAL_ARG_MARKER[item.sexp_type]
  name = extract_node_symbol item.last
  :"#{marker}#{name}"
end
convert_masgn_argument(item) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 130
def convert_masgn_argument(item)
  args = item[1]
  args.shift
  s(:masgn, *convert_destructuring_arguments(args))
end
in_method() { || ... } click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 82
def in_method
  @in_method_body = true
  result = yield
  @in_method_body = false
  result
end
kwrest_arg?(method) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 161
def kwrest_arg?(method)
  @kwrest.include?(method)
end
kwrest_param(params) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 149
def kwrest_param(params)
  found = params.find { |param| param.to_s =~ /^\*\*(.+)/ }
  Regexp.last_match[1].to_sym if found
end
method_body(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 89
def method_body(exp)
  block = process exp
  case block.sexp_type
  when :void_stmt
    [s(:nil).line(block.line)]
  else
    unwrap_block block
  end
end
with_kwrest(kwrest) { || ... } click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/methods.rb, line 154
def with_kwrest(kwrest)
  @kwrest.push kwrest
  result = yield
  @kwrest.pop
  result
end