Class: Dendroid::Parsing::OrNode

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

Overview

A composite parse node that embodies multiple syntactical derivations of a right-hand side of a rule to a range of input tokens. Each child node corresponds to a distinct derivation.

Instance Attribute Summary collapse

Attributes inherited from CompositeParseNode

#children

Attributes inherited from ParseNode

#range

Instance Method Summary collapse

Constructor Details

#initialize(sym, lower, upper, arity) ⇒ OrNode

Returns a new instance of OrNode.

Parameters:

  • sym (Dendroid::Syntax::NonTerminal)
  • lower (Integer)

    lowest token rank matching start of the rule

  • upper (Integer)

    largest token rank matching start of the rule

  • arity (Integer)

    Number of derivations of the given rule



17
18
19
20
# File 'lib/dendroid/parsing/or_node.rb', line 17

def initialize(sym, lower, upper, arity)
  @symbol = sym
  super(lower, upper, arity)
end

Instance Attribute Details

#symbolDendroid::Syntax::NonTerminal (readonly)

Returns The non-terminal symbol at LHS of rule.

Returns:



11
12
13
# File 'lib/dendroid/parsing/or_node.rb', line 11

def symbol
  @symbol
end

Instance Method Details

#accept(aVisitor) ⇒ Object

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

Parameters:



61
62
63
# File 'lib/dendroid/parsing/or_node.rb', line 61

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

#add_child(child_node, _index) ⇒ Object

Add a child node as root of one derivation. Place it in an available child slot.

Parameters:

Raises:

  • (StandardError)


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

def add_child(child_node, _index)
  idx = children.find_index(&:nil?)
  raise StandardError unless idx

  # Use first found available slot...
  super(child_node, idx)
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,
- each child matches this chart entry

Returns:

  • (Boolean)

    true if the entry corresponds to this node.



40
41
42
43
44
45
# File 'lib/dendroid/parsing/or_node.rb', line 40

def match(anEItem)
  return false if range.begin != anEItem.origin

  dotted = anEItem.dotted_item
  (symbol == dotted.rule.lhs) && children.any? { |ch| ch.match(anEItem) }
end

#partial?FalseClass

Returns:

  • (FalseClass)


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

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

#to_sString

Return a String representation of itself

Returns:

  • (String)

    text representation of itself



55
56
57
# File 'lib/dendroid/parsing/or_node.rb', line 55

def to_s
  "OR: #{symbol.name} #{range_to_s}"
end