Sha256: beaa854d1254a3633c60cc4e3df5be2829c9ccebc8b13a5688e3c059518af450

Contents?: true

Size: 1.92 KB

Versions: 4

Compression:

Stored size: 1.92 KB

Contents

module Yoda
  module Parsing
    class Traverser
      module QueryInterface
        # @return [Traverser, nil]
        def query(**kwargs, &predicate)
          result = select(**kwargs, &predicate).first
          result ? Traverser.new(result) : nil
        end

        # @return [ResultSet]
        def query_all(**kwargs, &predicate)
          ResultSet.new(select(**kwargs, &predicate))
        end

        # @return [Traverser, nil]
        def query_ancestor(**kwargs, &predicate)
          result = select_ancestors(**kwargs, &predicate).first
          result ? Traverser.new(result) : nil
        end

        # @return [Traverser, nil]
        def query_ancestors(**kwargs, &predicate)
          ResultSet.new(select_ancestors(**kwargs, &predicate))
        end

        private

        # @return [Enumerable<AST::Node>]
        def select(**kwargs, &predicate)
          matcher = Matcher.new(**kwargs, &predicate)
          all_nodes.select { |node| matcher.match?(node) }
        end

        # @return [Enumerable<AST::Node>]
        def select_ancestors(**kwargs, &predicate)
          matcher = Matcher.new(**kwargs, &predicate)
          nesting.select { |node| matcher.match?(node) }
        end

        # @param node [AST::Node]
        # @return [Enumerable<AST::Node>]
        def all_nodes_for(node)
          Enumerator.new { |yielder| repeat_for(node, yielder) }.lazy
        end

        # @param node [AST::Node]
        # @param yielder [Enumerator::Yielder]
        def repeat_for(node, yielder)
          yielder << node
          node.children.select { |node| node.is_a?(AST::Node) }.each { |node| repeat_for(node, yielder) }
        end

        # @abstract
        # @return [Enumerable<AST::Node>]
        def all_nodes
          fail NotImplementedError
        end

        # @abstract
        # @return [Enumerable<AST::Node>]
        def nesting
          fail NotImplementedError
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yoda-language-server-0.10.1 lib/yoda/parsing/traverser/query_interface.rb
yoda-language-server-0.10.0 lib/yoda/parsing/traverser/query_interface.rb
yoda-language-server-0.9.0 lib/yoda/parsing/traverser/query_interface.rb
yoda-language-server-0.8.0 lib/yoda/parsing/traverser/query_interface.rb