Sha256: df563b66f6b42d2885d94d65beda54e3fe018823af4249b5388f1be72e2bcb65

Contents?: true

Size: 1.04 KB

Versions: 8

Compression:

Stored size: 1.04 KB

Contents

module Furnace::AST
  module Visitor
    def visit(node)
      replacements = {}

      node.children.each_with_index do |child, index|
        if child.is_a? Node
          visit child

          if child.type == :expand
            replacements[index] = child.children
          end
        end
      end

      if replacements.any?
        new_children = []

        node.children.each_with_index do |child, index|
          if replacements[index]
            new_children.concat replacements[index]
          else
            new_children.push child
          end
        end

        node.children.replace new_children
      end

      node.children.delete_if do |child|
        if child.is_a? Node
          child.parent = node

          child.type == :remove
        end
      end

      # Invoke a specific handler
      on_handler = :"on_#{node.type}"
      if respond_to? on_handler
        send on_handler, node
      end

      # Invoke a generic handler
      if respond_to? :on_any
        send :on_any, node
      end

      node
    end
  end
end

Version data entries

8 entries across 8 versions & 1 rubygems

Version Path
furnace-0.2.5 lib/furnace/ast/visitor.rb
furnace-0.2.4 lib/furnace/ast/visitor.rb
furnace-0.2.3 lib/furnace/ast/visitor.rb
furnace-0.2.2 lib/furnace/ast/visitor.rb
furnace-0.2.1 lib/furnace/ast/visitor.rb
furnace-0.2.0 lib/furnace/ast/visitor.rb
furnace-0.1.2 lib/furnace/ast/visitor.rb
furnace-0.1.1 lib/furnace/ast/visitor.rb