Sha256: 33a44aa7a753a3624a76a800750dc0a1df2718773a67d63d1505998cad295bb5

Contents?: true

Size: 1.67 KB

Versions: 1

Compression:

Stored size: 1.67 KB

Contents

module RipperRubyParser
  module SexpHandlers
    module Literals
      def process_string_literal exp
        _, content = exp.shift 2
        process(content)
      end

      def process_string_content exp
        _, inner = exp.shift 2

        string = extract_inner_string(inner)
        rest = []

        if string.sexp_type == :str
          string = string[1]
        else
          rest << string
          string = ""
        end

        string = unescape(string)

        until exp.empty? do
          result = process(exp.shift)
          if result.sexp_type == :str
            result[1] = unescape(result[1])
          end
          rest << result
        end

        if rest.empty?
          s(:str, string)
        else
          s(:dstr, string, *rest)
        end
      end

      def process_string_embexpr exp
        _, list = exp.shift 2
        s(:evstr, process(list.first))
      end

      def process_regexp_literal exp
        _, content, _ = exp.shift 3

        string = extract_inner_string content[0]

        s(:lit, Regexp.new(string[1]))
      end

      def process_symbol_literal exp
        _, symbol = exp.shift 2
        sym = symbol[1]
        s(:lit, extract_node_symbol(sym))
      end

      def process_dyna_symbol exp
        _, list = exp.shift 2

        string = process list[0]
        s(:lit, string[1].to_sym)
      end

      def process_at_tstring_content exp
        _, string, _ = exp.shift 3
        s(:str, string)
      end

      private

      def extract_inner_string exp
        process(exp) || s(:str, "")
      end

      def unescape string
        string.gsub /(\\[n\\"])/ do
          eval "\"#{$1}\""
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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