# encoding: utf-8 require 'pry' module Salt # A class that is used to help walk the AST generated by Salt (or # even any AST that uses arrays to define Nodes and Tokens). class Walker attr_reader :ast def initialize(ast) @ast = ast @stack = [] end def perform to([@ast]) until @stack.empty? yield self @stack.shift end end def on(action) if action.is_a?(Hash) && action.key?(current_type) action[current_type].each do |index| to(current[index]) end elsif !action.is_a?(Hash) yield current[1..-1] if current_type == action end end def to(sub) return unless sub @stack.concat(sub) end def current @stack.first end def current_type current[0] end end end