Sha256: 941eeebbcf320378d51d137df9de103f51ab8cb36d621b8c20432cc0bbd240b5

Contents?: true

Size: 1.15 KB

Versions: 1

Compression:

Stored size: 1.15 KB

Contents

# frozen_string_literal: true

module Synvert::Core::NodeQuery::Compiler
  # Expression represents a node query expression.
  class Expression
    # Initialize a Expression.
    # @param selector [Synvert::Core::NodeQuery::Compiler::Selector] the selector
    # @param rest [Synvert::Core::NodeQuery::Compiler::Expression] the rest expression
    def initialize(selector: nil, rest: nil)
      @selector = selector
      @rest = rest
    end

    # Check if the node matches the expression.
    # @param node [Parser::AST::Node] the node
    # @return [Boolean]
    def match?(node)
      !query_nodes(node).empty?
    end

    # Query nodes by the selector and the rest expression.
    #
    # @param node [Parser::AST::Node] node to match
    # @return [Array<Parser::AST::Node>] matching nodes.
    def query_nodes(node)
      matching_nodes = @selector.query_nodes(node)
      return matching_nodes if @rest.nil?

      matching_nodes.flat_map do |matching_node|
        @rest.query_nodes(matching_node)
      end
    end

    def to_s
      result = []
      result << @selector.to_s if @selector
      result << @rest.to_s if @rest
      result.join(' ')
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
synvert-core-1.4.0 lib/synvert/core/node_query/compiler/expression.rb