Class: Dendroid::Parsing::AndNode

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

Overview

A composite parse node that matches the sequence of grammar symbols from a right-hand side of a rule to a range of input tokens. The child nodes correspond to the grammar symbols in the RHS of the rule.

Instance Attribute Summary collapse

Attributes inherited from CompositeParseNode

#children

Attributes inherited from ParseNode

#range

Instance Method Summary collapse

Constructor Details

#initialize(anEItem, rank) ⇒ AndNode

Returns a new instance of AndNode.

Parameters:

  • anEItem (Dendroid::Recognizer::EItem)

    An entry from the chart.

  • rank (Integer)

    rank of the last input token matched by this node



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

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_indexInteger (readonly)

Returns Index of the rule alternative.

Returns:

  • (Integer)

    Index of the rule alternative.



15
16
17
# File 'lib/dendroid/parsing/and_node.rb', line 15

def alt_index
  @alt_index
end

#ruleWeakRef<Dendroid::Syntax::Rule> (readonly)

Returns Grammar rule.

Returns:



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

def rule
  @rule
end

Instance Method Details

#accept(aVisitor) ⇒ Object

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

Parameters:



71
72
73
# File 'lib/dendroid/parsing/and_node.rb', line 71

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

#add_child(child_node, index) ⇒ Object

Add a child a given available position.

Parameters:

  • child_node (Dendroid::Parsing::ParseNode)

    Node to add as a child

  • index (Integer)

    position of the child node in the ‘children` array

Raises:

  • (StandardError)


29
30
31
32
33
# File 'lib/dendroid/parsing/and_node.rb', line 29

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

  super(child_node, index)
end

#expecting?(symbol, position) ⇒ Boolean

Is this node expecting at given RHS index, the given symbol?

Parameters:

Returns:

  • (Boolean)


53
54
55
56
# File 'lib/dendroid/parsing/and_node.rb', line 53

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

#match(anEItem) ⇒ Boolean

Is the given chart entry matching this node? The chart entry matches this node if:

- its origin equals to the start of the range; and,
- both rules are the same; and,

Returns:

  • (Boolean)

    true if the entry corresponds to this node.



40
41
42
43
44
45
46
47
48
# File 'lib/dendroid/parsing/and_node.rb', line 40

def match(anEItem)
  return false if range.begin != 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 true if at least one of the children slots is free.

Returns:

  • (Boolean)

    true if at least one of the children slots is free.



59
60
61
# File 'lib/dendroid/parsing/and_node.rb', line 59

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

#to_sString

Return a String representation of itself

Returns:

  • (String)

    text representation of itself



65
66
67
# File 'lib/dendroid/parsing/and_node.rb', line 65

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