Sha256: e8cbaf8bece8b40fdafedaaef70cbf1a0817af07b702f5c95257c7940a8d7e95

Contents?: true

Size: 1.25 KB

Versions: 12

Compression:

Stored size: 1.25 KB

Contents

# frozen_string_literal: true

module Synvert::Core
  # QueryScope finds out nodes by using node query language, then changes its scope to matching node.
  class Rewriter::QueryScope < Rewriter::Scope
    # Initialize a QueryScope.
    #
    # @param instance [Synvert::Core::Rewriter::Instance]
    # @param query_string [String]
    # @yield run on all matching nodes
    def initialize(instance, query_string, &block)
      super(instance, &block)
      @query_string = query_string
    end

    # Find out the matching nodes.
    #
    # It checks the current node and iterates all child nodes,
    # then run the block code on each matching node.
    # @raise [Synvert::Core::NodeQuery::Compiler::ParseError] if the query string is invalid.
    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
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
synvert-core-1.4.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.3.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.3.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.2.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.2.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.1.1 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.1.0 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.0.5 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.0.4 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.0.3 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.0.2 lib/synvert/core/rewriter/scope/query_scope.rb
synvert-core-1.0.1 lib/synvert/core/rewriter/scope/query_scope.rb