Class: Rley::ParseRep::ParseForestBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/rley/parse_rep/parse_forest_builder.rb

Overview

Builder GoF pattern. Builder pattern builds a complex object (say, a parse forest) from simpler objects (terminal and non-terminal nodes) and using a step by step approach.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theTokens) ⇒ ParseForestBuilder

Returns a new instance of ParseForestBuilder.



37
38
39
40
41
42
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 37

def initialize(theTokens)
  @tokens = theTokens
  @curr_path = []
  @entry2node = {}
  @entry2path_to_alt = {}
end

Instance Attribute Details

#curr_pathObject (readonly)

Link to current path



24
25
26
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 24

def curr_path
  @curr_path
end

#entry2nodeObject (readonly)

A hash with pairs of the form: visited parse entry => forest node



30
31
32
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 30

def entry2node
  @entry2node
end

#entry2path_to_altObject (readonly)

A hash with pairs of the form: parent end entry => path to alternative node This is needed for synchronizing backtracking



35
36
37
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 35

def entry2path_to_alt
  @entry2path_to_alt
end

#last_visiteeObject (readonly)

The last parse entry visited



27
28
29
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 27

def last_visitee
  @last_visitee
end

#resultObject (readonly)

Link to forest object (being) built



21
22
23
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 21

def result
  @result
end

#tokensObject (readonly)

The sequence of input tokens



18
19
20
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 18

def tokens
  @tokens
end

Instance Method Details

#curr_parentObject

Return the current_parent node



65
66
67
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 65

def curr_parent()
  return curr_path.last
end

#done!Object

Notify the builder that the construction is over



45
46
47
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 45

def done!()
  result.done!
end

#receive_event(anEvent, anEntry, anIndex) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 49

def receive_event(anEvent, anEntry, anIndex)
  # puts "Event: #{anEvent} #{anEntry} #{anIndex}"
  if anEntry.dotted_entry?
    process_item_entry(anEvent, anEntry, anIndex)
  elsif anEntry.start_entry?
    process_start_entry(anEvent, anEntry, anIndex)
  elsif anEntry.end_entry?
    process_end_entry(anEvent, anEntry, anIndex)
  else
    raise NotImplementedError
  end

  @last_visitee = anEntry
end