module RipperRubyParser::SexpHandlers::Operators

Sexp handlers for operators

Constants

BINARY_OPERATOR_MAP
NEGATED_BINARY_OPERATOR_MAP
SHIFT_OPERATORS
UNARY_OPERATOR_MAP

Public Instance Methods

process_binary(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 24
def process_binary(exp)
  _, left, op, right = exp.shift 4

  if op == :=~
    make_regexp_match_operator(left, op, right)
  elsif (mapped = NEGATED_BINARY_OPERATOR_MAP[op])
    s(:not, make_regexp_match_operator(left, mapped, right))
  elsif (mapped = BINARY_OPERATOR_MAP[op])
    make_boolean_operator(left, mapped, right)
  elsif SHIFT_OPERATORS.include? op
    s(:call, unwrap_begin(process(left)), op, unwrap_begin(process(right)))
  else
    s(:call, process(left), op, process(right))
  end
end
process_dot2(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 47
def process_dot2(exp)
  _, left, right = exp.shift 3
  left = process(left)
  right = process(right)
  if integer_literal?(left) && integer_literal?(right)
    with_line_number(left.line, s(:lit, Range.new(left[1], right[1])))
  else
    s(:dot2, left, right)
  end
end
process_dot3(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 58
def process_dot3(exp)
  _, left, right = exp.shift 3
  left = process(left)
  right = process(right)
  if integer_literal?(left) && integer_literal?(right)
    with_line_number(left.line, s(:lit, Range.new(left[1], right[1], true)))
  else
    s(:dot3, left, right)
  end
end
process_ifop(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 69
def process_ifop(exp)
  _, cond, truepart, falsepart = exp.shift 4
  s(:if,
    process(cond),
    process(truepart),
    process(falsepart))
end
process_unary(exp) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 40
def process_unary(exp)
  _, op, arg = exp.shift 3
  arg = process(arg)
  op = UNARY_OPERATOR_MAP[op] || op
  s(:call, arg, op)
end

Private Instance Methods

make_boolean_operator(left, operator, right) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 79
def make_boolean_operator(left, operator, right)
  _, left, _, right = rebalance_binary(left, operator, right)
  s(operator, unwrap_begin(process(left)), process(right))
end
make_regexp_match_operator(left, operator, right) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 84
def make_regexp_match_operator(left, operator, right)
  if left.sexp_type == :regexp_literal
    s(:match2, process(left), process(right))
  elsif right.sexp_type == :regexp_literal
    s(:match3, process(right), process(left))
  else
    s(:call, process(left), operator, process(right))
  end
end
rebalance_binary(left, operator, right) click to toggle source
# File lib/ripper_ruby_parser/sexp_handlers/operators.rb, line 94
def rebalance_binary(left, operator, right)
  if BINARY_OPERATOR_MAP[operator] == BINARY_OPERATOR_MAP[left[2]]
    _, left, _, middle = rebalance_binary(*left.sexp_body)
    right = rebalance_binary(middle, operator, right)
  end
  s(:binary, left, operator, right)
end