Sha256: 970db1120320614ba5340e0c0dc6924387fdbd1624d01e56fa4264e59403a326

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# encoding: utf-8

module Unparser
  class Emitter
    # Emitter control flow modifiers
    class FlowModifier < self

      MAP = {
        return: K_RETURN,
        next:   K_NEXT,
        break:  K_BREAK
      }

      handle(*MAP.keys)

    private

      # Perform dispatch
      #
      # @return [undefined]
      #
      # @api private
      #
      def dispatch
        conditional_parentheses((parent_type == :or || parent_type == :and) && children.any?) do
          write(MAP.fetch(node.type))
          emit_arguments if children.any?
        end
      end

      # Emit break or return arguments
      #
      # @return [undefined]
      #
      # @api private
      #
      def emit_arguments
        ws
        head, *tail = children
        emit_argument(head)
        tail.each do |node|
          write(DEFAULT_DELIMITER)
          emit_argument(node)
        end
      end

      PARENS = [:if, :case, :begin].to_set.freeze

      # Emit argument
      #
      # @param [Parser::AST::Node] node
      #
      # @api private
      #
      def emit_argument(node)
        conditional_parentheses(PARENS.include?(node.type)) do
          visit(node)
        end
      end

    end # Return
  end # Emitter
end # Unparser

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
unparser-0.1.14 lib/unparser/emitter/flow_modifier.rb
unparser-0.1.13 lib/unparser/emitter/flow_modifier.rb
unparser-0.1.12 lib/unparser/emitter/flow_modifier.rb
unparser-0.1.11 lib/unparser/emitter/flow_modifier.rb
unparser-0.1.10 lib/unparser/emitter/flow_modifier.rb