Sha256: 30bafef80c2909fb83254d9ab3889b69770748fb8a9f8d17b9647c8540b99552

Contents?: true

Size: 842 Bytes

Versions: 1

Compression:

Stored size: 842 Bytes

Contents

# 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

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
salt-0.0.2 lib/salt/walker.rb