Class: Dendroid::Parsing::WalkProgress

Inherits:
Object
  • Object
show all
Defined in:
lib/dendroid/parsing/walk_progress.rb

Overview

This object holds the current state of the visit of a Chart by one ChartWalker through one single visit path. A path corresponds to a chain from the current item back to the initial item(s) through the predecessors links. It is used to construct (part of) the parse tree beginning from the root node.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_rank, start_item, parents) ⇒ WalkProgress

Returns a new instance of WalkProgress.

Parameters:



34
35
36
37
38
39
40
# File 'lib/dendroid/parsing/walk_progress.rb', line 34

def initialize(start_rank, start_item, parents)
  @state = :New
  @curr_rank = start_rank
  @curr_item = start_item
  @predecessor = nil
  @parents = parents
end

Instance Attribute Details

#curr_itemDendroid::Recognizer::EItem

Returns the chart entry being visited.

Returns:



22
23
24
# File 'lib/dendroid/parsing/walk_progress.rb', line 22

def curr_item
  @curr_item
end

#curr_rankInteger

Returns rank of the item set from the chart being visited.

Returns:

  • (Integer)

    rank of the item set from the chart being visited



19
20
21
# File 'lib/dendroid/parsing/walk_progress.rb', line 19

def curr_rank
  @curr_rank
end

#parentsArray<Dendroid::Parsing::CompositeParseNode> (readonly)

Returns The ancestry of current parse node.

Returns:



29
30
31
# File 'lib/dendroid/parsing/walk_progress.rb', line 29

def parents
  @parents
end

#predecessorDendroid::Recognizer::EItem|NilClass

When not nil, override the predecessors links of the current item

Returns:



26
27
28
# File 'lib/dendroid/parsing/walk_progress.rb', line 26

def predecessor
  @predecessor
end

#stateSymbol

Returns One of: :New, :Running, :Waiting, :Complete, :Forking, :Delegating.

Returns:

  • (Symbol)

    One of: :New, :Running, :Waiting, :Complete, :Forking, :Delegating



16
17
18
# File 'lib/dendroid/parsing/walk_progress.rb', line 16

def state
  @state
end

Instance Method Details

#add_child_node(aNode) ⇒ Dendroid::Parsing::ParseNode

Add the given node as a child of the last parent node.



115
116
117
118
# File 'lib/dendroid/parsing/walk_progress.rb', line 115

def add_child_node(aNode)
  parents.last.add_child(aNode, curr_item.position - 1) unless parents.empty?
  aNode
end

#add_node_empty(anEntry) ⇒ Dendroid::Parsing::EmptyRuleNode

Add a child leaf node for the given chart entry that corresponds to an empty rule.



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

def add_node_empty(anEntry)
  node_empty = EmptyRuleNode.new(anEntry, curr_rank)
  add_child_node(node_empty)
end

#add_terminal_node(token) ⇒ Dendroid::Parsing::TerminalNode

Add a leaf terminal node for the token at current rank as a child of last parent.



79
80
81
82
83
# File 'lib/dendroid/parsing/walk_progress.rb', line 79

def add_terminal_node(token)
  @curr_rank -= 1
  term_node = TerminalNode.new(curr_item.prev_symbol, token, curr_rank)
  add_child_node(term_node)
end

#fork(thePredecessor) ⇒ Object

Current item has multiple predecessors: set the state to Forking and force one of the predecessor to be the next entry to visit.

Parameters:



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

def fork(thePredecessor)
  @state = :Forking
  @predecessor = thePredecessor
end

#initialize_copy(orig) ⇒ Object

Factory method.



43
44
45
46
47
48
49
# File 'lib/dendroid/parsing/walk_progress.rb', line 43

def initialize_copy(orig)
  @state = orig.state
  @curr_rank = orig.curr_rank
  @curr_item = orig.curr_item
  @predecessor = nil
  @parents = orig.parents.dup
end

#match_parent?(entries, stop_at_first) ⇒ Array<Array<EItem, Integer>>

Do the given EItems match one of the parent? Matching = corresponds to the same rule and range

Parameters:

Returns:

  • (Array<Array<EItem, Integer>>)


128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/dendroid/parsing/walk_progress.rb', line 128

def match_parent?(entries, stop_at_first)
  matching = []
  min_origin = entries[0].origin
  first_iteration = true
  offset = 0

  parents.reverse_each do |node|
    if node.is_a?(OrNode)
      offset += 1
      next
    end
    entries.each do |ent|
      min_origin = ent.origin if first_iteration && ent.origin < min_origin
      next unless node.match(ent)

      matching << [ent, offset]
      break if stop_at_first
    end
    first_iteration = false
    break if stop_at_first && !matching.empty?

    # Stop loop when parent.origin < min(entries.origin)
    break if node.range.begin < min_origin

    offset += 1
  end

  matching
end

#push_and_node(anEntry) ⇒ Dendroid::Parsing::AndNode

Make an AND node for the given entry as a child of last parent and push this node in the ancestry

Parameters:

Returns:

Raises:

  • (StandardError)


89
90
91
92
93
94
95
96
97
# File 'lib/dendroid/parsing/walk_progress.rb', line 89

def push_and_node(anEntry)
  node = AndNode.new(anEntry, curr_rank)
  raise StandardError unless anEntry.rule == node.rule # Fails

  add_child_node(node)
  parents.push(node)

  node
end

#push_or_node(origin, arity) ⇒ Dendroid::Parsing::OrNode

Make an OR node as a child of last parent and push this node in the ancestry. #param arity [Integer] The number of alternative derivations

Parameters:

  • origin (Integer)

    Start rank

Returns:



104
105
106
107
108
109
110
# File 'lib/dendroid/parsing/walk_progress.rb', line 104

def push_or_node(origin, arity)
  node = OrNode.new(curr_item.prev_symbol, origin, curr_rank, arity)
  add_child_node(node)
  parents.push(node)

  node
end