class SyntaxTree::Search

Provides an interface for searching for a pattern of nodes against a subtree of an AST.

Attributes

pattern[R]

Public Class Methods

new(pattern) click to toggle source
# File lib/syntax_tree/search.rb, line 9
def initialize(pattern)
  @pattern = pattern
end

Public Instance Methods

scan(root) { |node| ... } click to toggle source
# File lib/syntax_tree/search.rb, line 13
def scan(root)
  return to_enum(__method__, root) unless block_given?
  queue = [root]

  until queue.empty?
    node = queue.shift
    next unless node

    yield node if pattern.call(node)
    queue += node.child_nodes
  end
end