Class: Dendroid::Parsing::ANDNode

Inherits:
CompositeParseNode show all
Defined in:
lib/dendroid/parsing/and_node.rb

Instance Attribute Summary collapse

Attributes inherited from CompositeParseNode

#children, #range

Attributes inherited from ParseNode

#range

Instance Method Summary collapse

Constructor Details

#initialize(anEItem, rank) ⇒ ANDNode

Returns a new instance of ANDNode.



11
12
13
14
15
16
# File 'lib/dendroid/parsing/and_node.rb', line 11

def initialize(anEItem, rank)
  @rule = WeakRef.new(anEItem.dotted_item.rule)
  @alt_index = anEItem.dotted_item.alt_index
  upper_bound = rank
  super(anEItem.origin, upper_bound, rule.rhs[alt_index].size)
end

Instance Attribute Details

#alt_indexObject (readonly)

Returns the value of attribute alt_index.



9
10
11
# File 'lib/dendroid/parsing/and_node.rb', line 9

def alt_index
  @alt_index
end

#ruleObject (readonly)

Returns the value of attribute rule.



8
9
10
# File 'lib/dendroid/parsing/and_node.rb', line 8

def rule
  @rule
end

Instance Method Details

#accept(aVisitor) ⇒ Object

Part of the ‘visitee’ role in Visitor design pattern.

Parameters:



49
50
51
# File 'lib/dendroid/parsing/and_node.rb', line 49

def accept(aVisitor)
  aVisitor.visit_and_node(self)
end

#add_child(child_node, index) ⇒ Object

Raises:

  • (StandardError)


18
19
20
21
22
# File 'lib/dendroid/parsing/and_node.rb', line 18

def add_child(child_node, index)
  raise StandardError unless children[index].nil? # Is slot available?

  super(child_node, index)
end

#expecting?(symbol, position) ⇒ Boolean

Returns:

  • (Boolean)


34
35
36
37
# File 'lib/dendroid/parsing/and_node.rb', line 34

def expecting?(symbol, position)
  symb_seq = rule.rhs[alt_index]
  symb_seq[position] == symbol
end

#match(anEItem) ⇒ Object



24
25
26
27
28
29
30
31
32
# File 'lib/dendroid/parsing/and_node.rb', line 24

def match(anEItem)
  return false if range[0] != anEItem.origin

  dotted = anEItem.dotted_item
  same_rule = (rule.lhs == dotted.rule.lhs) && (alt_index == dotted.alt_index)
  return false unless same_rule

  dotted.initial_pos? ? true : partial?
end

#partial?Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/dendroid/parsing/and_node.rb', line 39

def partial?
  children.any?(&:nil?)
end

#to_sObject



43
44
45
# File 'lib/dendroid/parsing/and_node.rb', line 43

def to_s
  "#{rule.lhs} => #{rule.rhs[alt_index]} #{range}"
end