Class: Rley::ParseRep::ASTBaseBuilder

Inherits:
ParseTreeBuilder show all
Defined in:
lib/rley/parse_rep/ast_base_builder.rb

Overview

Abstract class (to be subclassed). The purpose of an ASTBaseBuilder is to build piece by piece an AST (Abstract Syntax Tree) from a sequence of input tokens and visit events produced by walking over a GFGParsing object. It is an implementation of 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

Notation::ASTBuilder

Instance Attribute Summary

Attributes inherited from ParseTreeBuilder

#result, #tokens

Instance Method Summary collapse

Methods inherited from ParseTreeBuilder

#done!, #initialize, #receive_event

Constructor Details

This class inherits a constructor from Rley::ParseRep::ParseTreeBuilder

Instance Method Details

#method_name(aProductionName) ⇒ String

Default method name to invoke when production with given name is invoked. Override this method for other method naming convention.

Parameters:

  • aProductionName (String)

Returns:

  • (String)


39
40
41
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 39

def method_name(aProductionName)
  "reduce_#{aProductionName}"
end

#return_epsilon(_range, _tokens, _children) ⇒ Object

Simply return an epsilon symbol

Parameters:



74
75
76
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 74

def return_epsilon(_range, _tokens, _children)
  nil
end

#return_first_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the first child node

Parameters:



48
49
50
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 48

def return_first_child(_range, _tokens, theChildren)
  theChildren[0]
end

#return_last_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the last child node

Parameters:



66
67
68
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 66

def return_last_child(_range, _tokens, theChildren)
  theChildren[-1]
end

#return_second_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the second child node

Parameters:



57
58
59
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 57

def return_second_child(_range, _tokens, theChildren)
  theChildren[1]
end

#terminal2nodeHash{String => Class}, Hash{String => Hash{String => Class}}

Method to override in subclass. Returns a Hash

Returns:

  • (Hash{String => Class}, Hash{String => Hash{String => Class}})

    Returned hash contains pairs of the form: terminal name => Class implementing the terminal tokens terminal name => Hash with pairs: production name => Class

Raises:

  • (NotImplementedError)


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

def terminal2node
  raise NotImplementedError
end

#terminalnode_classClass

Method to override in subclass. Default class for representing terminal nodes.

Returns:

  • (Class)


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

def terminalnode_class
  PTree::TerminalNode
end