Sha256: ce71b1752985728639405a8c86e7cd030ee961c9adc95f150a7ff0352b1e0715

Contents?: true

Size: 1.91 KB

Versions: 14

Compression:

Stored size: 1.91 KB

Contents

module Unparser
  class Emitter
    class Literal

      # Emitter for regexp literals
      class Regexp < self
        DELIMITER = '/'.freeze

        handle :regexp

      private

        # Perform dispatch
        #
        # @return [undefined]
        #
        # @api private
        #
        def dispatch
          parentheses(DELIMITER, DELIMITER) do
            visit(dynamic_body)
          end
          visit(children.last)
        end

        # Return dynamic body
        #
        # @return [undefined]
        #
        # @api private
        #
        def dynamic_body
          Parser::AST::Node.new(:dyn_regexp_body, dynamic_body_children)
        end

        # Return dynamic body children
        #
        # @return [Enumerable<Parser::AST::Node>]
        #
        # @api private
        #
        def dynamic_body_children
          children[0..-2].map do |child|
            escape(child)
          end
        end

        # Return escaped child
        #
        # @return [Parser::AST::Node]
        #
        # @api private
        #
        def escape(child)
          return child unless child.type == :str
          source = child.children.first
          Parser::AST::Node.new(:str, [Unparser.transquote(source, delimiter, DELIMITER)])
        end

        # Return closing delimiter
        #
        # @return [String]
        #
        # @api private
        #
        def delimiter
          location = node.location
          return DELIMITER unless location
          location.expression.source[-1]
        end
        memoize :delimiter

      end # Regexp

      # Emitter for regexp options
      class Regopt < self

        handle :regopt

      private

        # Perform dispatch
        #
        # @return [undefined]
        #
        # @api private
        #
        def dispatch
          write(children.map(&:to_s).join)
        end

      end # Regopt

    end # Literal
  end # Emitter
end # Unparser

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
unparser-0.0.15 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.14 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.13 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.12 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.11 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.10 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.8 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.7 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.6 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.5 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.4 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.3 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.2 lib/unparser/emitter/literal/regexp.rb
unparser-0.0.1 lib/unparser/emitter/literal/regexp.rb