1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
# frozen_string_literal: true
module RuboCop
module AST
class NodePattern
class Compiler
# Base class for subcompilers
# Implements visitor pattern
#
# Doc on how this fits in the compiling process:
# /docs/modules/ROOT/pages/node_pattern.adoc
class Subcompiler
attr_reader :compiler
def initialize(compiler)
@compiler = compiler
@node = nil
end
def compile(node)
prev = @node
@node = node
do_compile
ensure
@node = prev
end
# @api private
private
attr_reader :node
def do_compile
send(self.class.registry.fetch(node.type, :visit_other_type))
end
@registry = {}
class << self
attr_reader :registry
def method_added(method)
@registry[Regexp.last_match(1).to_sym] = method if method =~ /^visit_(.*)/
super
end
def inherited(base)
us = self
base.class_eval { @registry = us.registry.dup }
super
end
end
end
end
end
end
end
|