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.

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)


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

def method_name(aProductionName)
  return 'reduce_' + aProductionName
end

#return_epsilon(_range, _tokens, _children) ⇒ Object

Simply return an epsilon symbol

Parameters:



72
73
74
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 72

def return_epsilon(_range, _tokens, _children)
  return nil
end

#return_first_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the first child node

Parameters:



46
47
48
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 46

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

#return_last_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the last child node

Parameters:



64
65
66
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 64

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

#return_second_child(_range, _tokens, theChildren) ⇒ Object

Utility method. Simply return the second child node

Parameters:



55
56
57
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 55

def return_second_child(_range, _tokens, theChildren)
  return 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: rule name => Class

Raises:

  • (NotImplementedError)


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

def terminal2node()
  raise NotImplementedError
end

#terminalnode_classClass

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

Returns:

  • (Class)


28
29
30
# File 'lib/rley/parse_rep/ast_base_builder.rb', line 28

def terminalnode_class()
  PTree::TerminalNode
end