free_mutant/lib/mutant/walker.rb

54 lines
866 B
Ruby
Raw Normal View History

2014-03-08 13:01:17 -05:00
# encoding: UTF-8
module Mutant
# Walker for all ast nodes
class Walker
# Run walkter
#
# @param [Parser::AST::Node] root
#
# @return [self]
#
# @api private
#
def self.run(root, &block)
new(root, block)
self
end
private_class_method :new
# Initialize and run walker
#
# @param [Parser::AST::Node] root
# @param [#call(node)] block
#
# @return [undefined]
#
# @api private
#
def initialize(root, block)
@root, @block = root, block
dispatch(root)
end
private
# Perform dispatch
#
# @param [Parser::AST::Node] node
#
# @return [undefined]
#
# @api private
#
def dispatch(node)
@block.call(node)
node.children.grep(Parser::AST::Node).each(&method(:dispatch))
end
end # Walker
end # Mutant