Sha256: 9a86e983300b5e3287b86a6b3ee11b761f24584269a23634e252aec8f0890fda

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# encoding: utf-8
# frozen_string_literal: true

module Carbon
  module Compiler
    module Visitor
      # A visitor base.
      module Base
        # The class methods.
        module ClassMethods
          def matches
            @_matches ||= Concurrent::Hash.new
          end

          def on(nodes)
            matches.merge!(nodes)
          end
        end

        # The instance methods.
        module InstanceMethods
          def accept(node, *params)
            visit(node, *params)
          end

          def visit(node, *params)
            return unless node
            begin
              action = self.class.matches.fetch(node.class)
            rescue KeyError
              visit_missing(node, *params)
            end
            public_send(action, node, *params)
          end

          def visit_missing(node, *)
            fail ArgumentError, "Cannot visit #{node.class}"
          end

          def visit_ignore(node)
            node
          end
        end

        def self.included(base)
          base.include InstanceMethods
          base.extend ClassMethods
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
carbon-compiler-0.2.0 lib/carbon/compiler/visitor/base.rb