Class: Dendroid::Parsing::WalkProgress

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_rank, start_item, parents) ⇒ WalkProgress

rubocop: disable Metrics/CyclomaticComplexity rubocop: disable Metrics/PerceivedComplexity



20
21
22
23
24
25
26
# File 'lib/dendroid/parsing/walk_progress.rb', line 20

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_itemObject

Returns the value of attribute curr_item.



13
14
15
# File 'lib/dendroid/parsing/walk_progress.rb', line 13

def curr_item
  @curr_item
end

#curr_rankObject

Returns the value of attribute curr_rank.



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

def curr_rank
  @curr_rank
end

#parentsObject (readonly)

Returns the value of attribute parents.



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

def parents
  @parents
end

#predecessorObject

Returns the value of attribute predecessor.



14
15
16
# File 'lib/dendroid/parsing/walk_progress.rb', line 14

def predecessor
  @predecessor
end

#stateObject

Returns the value of attribute state.



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

def state
  @state
end

Instance Method Details

#add_child_node(aNode) ⇒ Object



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

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

#add_node_empty(anEntry) ⇒ Object



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

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

#add_terminal_node(token) ⇒ Object

Add a terminal node for terminal at current rank as a child of last parent



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

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



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

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

#initialize_copy(orig) ⇒ Object

Factory method.



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

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

Returns:

  • (Array<EItem>)


87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/dendroid/parsing/walk_progress.rb', line 87

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[0] < min_origin

    offset += 1
  end

  matching
end

#push_and_node(anEntry) ⇒ Object

Add an AND node for given entry as a child of last parent

Raises:

  • (StandardError)


61
62
63
64
65
66
67
68
69
# File 'lib/dendroid/parsing/walk_progress.rb', line 61

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) ⇒ Object



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

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