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.



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

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

Instance Attribute Details

#curr_pathObject (readonly)

Link to current path



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

def curr_path
  @curr_path
end

#entry2nodeObject (readonly)

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



32
33
34
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 32

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



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

def entry2path_to_alt
  @entry2path_to_alt
end

#last_visiteeObject (readonly)

The last parse entry visited



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

def last_visitee
  @last_visitee
end

#resultObject (readonly)

Link to forest object (being) built



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

def result
  @result
end

#tokensObject (readonly)

The sequence of input tokens



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

def tokens
  @tokens
end

Instance Method Details

#curr_parentObject

Return the current_parent node



67
68
69
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 67

def curr_parent
  curr_path.last
end

#done!Object

Notify the builder that the construction is over



47
48
49
# File 'lib/rley/parse_rep/parse_forest_builder.rb', line 47

def done!
  result.done!
end

#receive_event(anEvent, anEntry, anIndex) ⇒ Object



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

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