Class: Synvert::Core::Rewriter::QueryScope

Inherits:
Scope
  • Object
show all
Defined in:
lib/synvert/core/rewriter/scope/query_scope.rb

Overview

QueryScope finds out nodes by using node query language, then changes its scope to matching node.

Instance Method Summary collapse

Constructor Details

#initialize(instance, query_string) { ... } ⇒ QueryScope

Initialize a QueryScope.

Parameters:

Yields:

  • run on all matching nodes



11
12
13
14
# File 'lib/synvert/core/rewriter/scope/query_scope.rb', line 11

def initialize(instance, query_string, &block)
  super(instance, &block)
  @query_string = query_string
end

Instance Method Details

#processObject

Find out the matching nodes.

It checks the current node and iterates all child nodes, then run the block code on each matching node.

Raises:



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/synvert/core/rewriter/scope/query_scope.rb', line 21

def process
  current_node = @instance.current_node
  return unless current_node

  @instance.process_with_node(current_node) do
    NodeQuery::Parser.new.parse(@query_string).query_nodes(current_node).each do |node|
      @instance.process_with_node(node) do
        @instance.instance_eval(&@block)
      end
    end
  end
rescue NodeQuery::Lexer::ScanError, Racc::ParseError => e
  raise NodeQuery::Compiler::ParseError, "Invalid query string: #{@query_string}"
end