Sha256: 36acb62b384904a26e55fa80f17c9c72a1bb42eb05ad8dc3b7a229ba781b0007

Contents?: true

Size: 1.81 KB

Versions: 1

Compression:

Stored size: 1.81 KB

Contents

# frozen_string_literal: true

module RipperRubyParser
  module SexpHandlers
    # Sexp handlers for literals
    module Literals
      # character literals
      def process_at_CHAR(exp)
        _, val, pos = exp.shift 3
        with_position(pos, s(:str, fix_encoding(unescape(val[1..]))))
      end

      def process_array(exp)
        _, elems = exp.shift 2
        return s(:array) if elems.nil?

        process(elems).tap { |arr| arr.sexp_type = :array }
      end

      # 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, ...))
      def process_hash(exp)
        _, body = exp.shift 2
        return s(:hash) unless body

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

      # @example
      #   s(:assoc_splat, s(:vcall, s(:@ident, "bar")))
      def process_assoc_splat(exp)
        _, param = exp.shift 2
        s(:kwsplat, process(param))
      end

      # number literals
      def process_at_int(exp)
        make_literal(exp) { |val| Integer(val) }
      end

      def process_at_float(exp)
        make_literal(exp, &:to_f)
      end

      def process_at_rational(exp)
        make_literal(exp, &:to_r)
      end

      def process_at_imaginary(exp)
        make_literal(exp, &:to_c)
      end

      private

      # Process list of items that can be either :assoc_new or :assoc_splat
      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
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
ripper_ruby_parser-1.10.0 lib/ripper_ruby_parser/sexp_handlers/literals.rb