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 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:



37
38
39
40
41
42
43
# File 'lib/dendroid/parsing/walk_progress.rb', line 37

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, :Forking,.

Returns:

  • (Symbol)

    One of: :New, :Forking,



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.



118
119
120
121
# File 'lib/dendroid/parsing/walk_progress.rb', line 118

def add_child_node(aNode)
  parents.last.add_child(aNode, curr_item.position - 1)
  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.



74
75
76
77
# File 'lib/dendroid/parsing/walk_progress.rb', line 74

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.



82
83
84
85
86
# File 'lib/dendroid/parsing/walk_progress.rb', line 82

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:



57
58
59
60
# File 'lib/dendroid/parsing/walk_progress.rb', line 57

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

#initialize_copy(orig) ⇒ Object

Factory method.



46
47
48
49
50
51
52
# File 'lib/dendroid/parsing/walk_progress.rb', line 46

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<EItem>

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

Parameters:

Returns:

  • (Array<EItem>)


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)


92
93
94
95
96
97
98
99
100
# File 'lib/dendroid/parsing/walk_progress.rb', line 92

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:



107
108
109
110
111
112
113
# File 'lib/dendroid/parsing/walk_progress.rb', line 107

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