Sha256: e190052b093cda15976eabc5b8fefae12fbdd5d27fd1f9d5e795d145b3a9fbd1

Contents?: true

Size: 1.97 KB

Versions: 16

Compression:

Stored size: 1.97 KB

Contents

# frozen_string_literal: true

module Unparser
  UnknownNodeError = Class.new(ArgumentError)

  # Emitter base class
  class Emitter
    include Adamantium, AbstractType, Constants, Generation, NodeHelpers
    include Anima.new(:buffer, :comments, :node, :local_variable_scope)

    public :node

    extend DSL

    # Registry for node emitters
    REGISTRY = {} # rubocop:disable Style/MutableConstant

    NO_INDENT = %i[ensure rescue].freeze

    module LocalVariableRoot
      # Return local variable root
      #
      # @return [Parser::AST::Node]
      #
      # @api private
      #
      def local_variable_scope
        AST::LocalVariableScope.new(node)
      end

      def self.included(descendant)
        descendant.class_eval do
          memoize :local_variable_scope
        end
      end
    end # LocalVariableRoot

    def node_type
      node.type
    end

    # Register emitter for type
    #
    # @param [Symbol] types
    #
    # @return [undefined]
    #
    # @api private
    #
    def self.handle(*types)
      types.each do |type|
        fail "Handler for type: #{type} already registered" if REGISTRY.key?(type)

        REGISTRY[type] = self
      end
    end
    private_class_method :handle

    def emit_mlhs
      dispatch
    end

    # Return emitter
    #
    # @return [Emitter]
    #
    # @api private
    #
    # rubocop:disable Metrics/ParameterLists
    def self.emitter(buffer:, comments:, node:, local_variable_scope:)
      type = node.type

      klass = REGISTRY.fetch(type) do
        fail UnknownNodeError, "Unknown node type: #{type.inspect}"
      end

      klass.new(
        buffer:               buffer,
        comments:             comments,
        local_variable_scope: local_variable_scope,
        node:                 node
      )
    end
    # rubocop:enable Metrics/ParameterLists

    # Dispatch node write as statement
    #
    # @return [undefined]
    #
    # @api private
    #
    abstract_method :dispatch

  end # Emitter
end # Unparser

Version data entries

16 entries across 16 versions & 2 rubygems

Version Path
unparser-0.6.15 lib/unparser/emitter.rb
unparser-0.6.14 lib/unparser/emitter.rb
unparser-0.6.13 lib/unparser/emitter.rb
unparser-0.6.12 lib/unparser/emitter.rb
unparser-0.6.10 lib/unparser/emitter.rb
unparser-0.6.9 lib/unparser/emitter.rb
unparser-0.6.8 lib/unparser/emitter.rb
devcycle-ruby-server-sdk-2.0.0 vendor/bundle/ruby/3.0.0/gems/unparser-0.6.7/lib/unparser/emitter.rb
unparser-0.6.7 lib/unparser/emitter.rb
unparser-0.6.6 lib/unparser/emitter.rb
unparser-0.6.5 lib/unparser/emitter.rb
unparser-0.6.4 lib/unparser/emitter.rb
unparser-0.6.3 lib/unparser/emitter.rb
unparser-0.6.2 lib/unparser/emitter.rb
unparser-0.6.1 lib/unparser/emitter.rb
unparser-0.6.0 lib/unparser/emitter.rb