Class: Rley::ParseRep::ParseTreeBuilder
- Inherits:
-
Object
- Object
- Rley::ParseRep::ParseTreeBuilder
- Defined in:
- lib/rley/parse_rep/parse_tree_builder.rb
Overview
The purpose of a ParseTreeBuilder is to build piece by piece a parse tree from a sequence of input tokens and visit events produced by walking over a GFGParsing object. Uses the Builder GoF pattern. The Builder pattern creates a complex object (say, a parse tree) from simpler objects (terminal and non-terminal nodes) and using a step by step approach.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#result ⇒ Object
readonly
Link to Parse tree object (being) built.
-
#tokens ⇒ Array<Token>
readonly
The sequence of input tokens.
Instance Method Summary collapse
-
#done! ⇒ Object
Notify the builder that the parse tree construction is complete.
-
#initialize(theTokens) ⇒ ParseTreeBuilder
constructor
Create a new builder instance.
-
#receive_event(anEvent, anEntry, anIndex) ⇒ Object
Receive events resulting from a visit of GFGParsing object.
Constructor Details
#initialize(theTokens) ⇒ ParseTreeBuilder
Create a new builder instance.
46 47 48 49 50 |
# File 'lib/rley/parse_rep/parse_tree_builder.rb', line 46 def initialize(theTokens) @tokens = theTokens @stack = [] @dummy_node = Object.new.freeze end |
Instance Attribute Details
#result ⇒ Object (readonly)
Link to Parse tree object (being) built.
41 42 43 |
# File 'lib/rley/parse_rep/parse_tree_builder.rb', line 41 def result @result end |
#tokens ⇒ Array<Token> (readonly)
Returns The sequence of input tokens.
38 39 40 |
# File 'lib/rley/parse_rep/parse_tree_builder.rb', line 38 def tokens @tokens end |
Instance Method Details
#done! ⇒ Object
Notify the builder that the parse tree construction is complete.
53 54 55 |
# File 'lib/rley/parse_rep/parse_tree_builder.rb', line 53 def done! result.done! end |
#receive_event(anEvent, anEntry, anIndex) ⇒ Object
Receive events resulting from a visit of GFGParsing object. These events are produced by a specialized Enumerator created with a ParseWalkerFactory instance.
63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/rley/parse_rep/parse_tree_builder.rb', line 63 def receive_event(anEvent, anEntry, anIndex) # puts "Event: #{anEvent} #{anEntry} #{anIndex}" if anEntry.dotted_entry? # N => alpha . beta pattern? process_item_entry(anEvent, anEntry, anIndex) elsif anEntry.start_entry? # .N pattern? process_start_entry(anEvent, anEntry, anIndex) elsif anEntry.end_entry? # N. pattern? process_end_entry(anEvent, anEntry, anIndex) else raise NotImplementedError end @last_visitee = anEntry end |