Class: Resyma::Core::PTNodeMatcher

Inherits:
Object
  • Object
show all
Includes:
Matchable
Defined in:
lib/resyma/core/algorithm/matcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type, value = nil) ⇒ PTNodeMatcher

Create a instance of parse tree node matcher

Parameters:

  • type (Symbol)

    Symbol of the node

  • value (Object) (defaults to: nil)

    Value of the node, indicating that the node is a token. Currently, ‘nil` means that the node is an non-leaf node or it is a leaf node but its value is unimportant



15
16
17
18
# File 'lib/resyma/core/algorithm/matcher.rb', line 15

def initialize(type, value = nil)
  @type = type
  @value = value
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



20
21
22
# File 'lib/resyma/core/algorithm/matcher.rb', line 20

def type
  @type
end

#valueObject (readonly)

Returns the value of attribute value.



20
21
22
# File 'lib/resyma/core/algorithm/matcher.rb', line 20

def value
  @value
end

Instance Method Details

#==(other) ⇒ Object



22
23
24
25
26
# File 'lib/resyma/core/algorithm/matcher.rb', line 22

def ==(other)
  other.is_a?(PTNodeMatcher) &&
    other.type == @type &&
    other.value == @value
end

#match_with_value?(parsetree) ⇒ true, false

Whether the matcher matches with the parse tree

Parameters:

Returns:

  • (true, false)

    Result



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/resyma/core/algorithm/matcher.rb', line 35

def match_with_value?(parsetree)
  if parsetree.is_a?(Resyma::Core::ParseTree)
    parsetree.symbol == @type &&
      (@value.nil? || (parsetree.leaf? &&
                       parsetree.children[0] == @value))
  elsif parsetree.is_a?(PTNodeMatcher)
    self == parsetree
  else
    false
  end
end