Class: Dendroid::Parsing::AndNode
- Inherits:
-
CompositeParseNode
- Object
- ParseNode
- CompositeParseNode
- Dendroid::Parsing::AndNode
- 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
-
#alt_index ⇒ Integer
readonly
Index of the rule alternative.
-
#rule ⇒ WeakRef<Dendroid::Syntax::Rule>
readonly
Grammar rule.
Attributes inherited from CompositeParseNode
Attributes inherited from ParseNode
Instance Method Summary collapse
-
#accept(aVisitor) ⇒ Object
Part of the ‘visitee’ role in Visitor design pattern.
-
#add_child(child_node, index) ⇒ Object
Add a child a given available position.
-
#expecting?(symbol, position) ⇒ Boolean
Is this node expecting at given RHS index, the given symbol?.
-
#initialize(anEItem, rank) ⇒ AndNode
constructor
A new instance of AndNode.
-
#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,.
-
#partial? ⇒ Boolean
True if at least one of the children slots is free.
-
#to_s ⇒ String
Return a String representation of itself.
Constructor Details
#initialize(anEItem, rank) ⇒ AndNode
Returns a new instance of AndNode.
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_index ⇒ Integer (readonly)
Returns Index of the rule alternative.
15 16 17 |
# File 'lib/dendroid/parsing/and_node.rb', line 15 def alt_index @alt_index end |
#rule ⇒ WeakRef<Dendroid::Syntax::Rule> (readonly)
Returns Grammar rule.
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.
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.
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?
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,
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.
59 60 61 |
# File 'lib/dendroid/parsing/and_node.rb', line 59 def partial? children.any?(&:nil?) end |
#to_s ⇒ String
Return a String 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 |