Class: Rley::ParseRep::ParseTreeBuilder

Inherits:
Object
  • Object
show all
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

ASTBaseBuilder, CSTBuilder

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(theTokens) ⇒ ParseTreeBuilder

Create a new builder instance.

Parameters:

  • theTokens (Array<Token>)

    The sequence of input tokens.



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

#resultObject (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

#tokensArray<Token> (readonly)

Returns The sequence of input tokens.

Returns:

  • (Array<Token>)

    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.

Parameters:

  • anEvent (Syntax::Symbol)

    Kind of visit event. Should be: :visit

  • anEntry (ParseEntry)

    The entry being visited

  • anIndex (anIndex)

    The token index associated with anEntry



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