Sha256: 794d4b08030eaac58e0de4d7feb5cbb183638c560ea06a6a55d464500f69a431

Contents?: true

Size: 1.24 KB

Versions: 7

Compression:

Stored size: 1.24 KB

Contents

module Mutant
  # AST helpers
  module AST

    # Walk all ast nodes
    #
    # @param [Parser::AST::Node] root
    # @param [Array<Parser::AST::Node>] stack
    #
    # @yield [Parser::AST::Node]
    #   all nodes recursively including root
    #
    # @return [self]
    #
    # @api private
    #
    def self.walk(node, stack, &block)
      fail ArgumentError, 'block expected' unless block_given?

      block.call(node, stack)
      node.children.grep(Parser::AST::Node).each do |child|
        stack.push(child)
        walk(child, stack, &block)
        stack.pop
      end

      self
    end
    private_class_method :walk

    # Find last node satisfying predicate (as block)
    #
    # @return [Parser::AST::Node]
    #   if satisfying node is found
    #
    # @yield [Parser::AST::Node]
    #
    # @yieldreturn [Boolean]
    #   true in case node satisfies predicate
    #
    # @return [nil]
    #   otherwise
    #
    # @api private
    #
    def self.find_last_path(node, &predicate)
      fail ArgumentError, 'block expected' unless block_given?
      path = []
      walk(node, [node]) do |candidate, stack|
        if predicate.call(candidate, &predicate)
          path = stack.dup
        end
      end
      path
    end

  end # AST
end # Mutant

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
mutant-0.8.0 lib/mutant/ast.rb
mutant-0.7.9 lib/mutant/ast.rb
mutant-0.7.8 lib/mutant/ast.rb
mutant-0.7.7 lib/mutant/ast.rb
mutant-0.7.6 lib/mutant/ast.rb
mutant-0.7.5 lib/mutant/ast.rb
mutant-0.7.4 lib/mutant/ast.rb