class Metamorpher::Matcher::MatchingVisitor

Attributes

other[RW]

Public Class Methods

new(other) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 20
def initialize(other)
  @other = other
end

Public Instance Methods

visit_derived(_derived) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 53
def visit_derived(_derived)
  fail MatchingError, "Cannot match against a derived variable."
end
visit_literal(literal) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 33
def visit_literal(literal)
  if other.name == literal.name && expected_number_of_children?(literal)
    literal.children
      .zip(other.children)
      .map { |child, other_child| child.match(other_child) }
      .reduce(Matcher::Match.new(root: other), :combine)
  else
    Matcher::NoMatch.new
  end
end
visit_termset(termset) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 44
def visit_termset(termset)
  matches = termset.terms.map { |term| term.match(other) }.select(&:matches?)
  if matches.any?
    matches.first
  else
    Matcher::NoMatch.new
  end
end
visit_variable(variable) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 24
def visit_variable(variable)
  captured = variable.greedy? ? other.with_younger_siblings : other
  if variable.condition.call(captured)
    Matcher::Match.new(root: captured, substitution: { variable.name => captured })
  else
    Matcher::NoMatch.new
  end
end

Private Instance Methods

expected_number_of_children?(literal) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 59
def expected_number_of_children?(literal)
  other.children.size == literal.children.size || greedy_child?(literal)
end
greedy_child?(literal) click to toggle source
# File lib/metamorpher/matcher/matching.rb, line 63
def greedy_child?(literal)
  literal.children.any? { |c| c.is_a?(Terms::Variable) && c.greedy? }
end