Class: Metamorpher::Matcher::MatchingVisitor

Inherits:
Visitable::Visitor show all
Defined in:
lib/metamorpher/matcher/matching.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Visitable::Visitor

#visit

Constructor Details

#initialize(other) ⇒ MatchingVisitor

Returns a new instance of MatchingVisitor.



20
21
22
# File 'lib/metamorpher/matcher/matching.rb', line 20

def initialize(other)
  @other = other
end

Instance Attribute Details

#otherObject

Returns the value of attribute other.



18
19
20
# File 'lib/metamorpher/matcher/matching.rb', line 18

def other
  @other
end

Instance Method Details

#visit_derived(_derived) ⇒ Object



53
54
55
# File 'lib/metamorpher/matcher/matching.rb', line 53

def visit_derived(_derived)
  fail MatchingError, "Cannot match against a derived variable."
end

#visit_literal(literal) ⇒ Object



33
34
35
36
37
38
39
40
41
42
# 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) ⇒ Object



44
45
46
47
48
49
50
51
# 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) ⇒ Object



24
25
26
27
28
29
30
31
# 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