class Rley::ParseRep::ASTBaseBuilder
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.
Public Instance Methods
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 39 def method_name(aProductionName) "reduce_#{aProductionName}" end
Default method name to invoke when production with given name is invoked. Override this method for other method naming convention. @param aProductionName [String] @return [String]
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 74 def return_epsilon(_range, _tokens, _children) nil end
Simply return an epsilon symbol @param _range [Lexical::TokenRange] @param _tokens [Array<Lexical::Token>] @param _children [Array<Object>]
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 48 def return_first_child(_range, _tokens, theChildren) theChildren[0] end
Utility method. Simply return the first child node @param _range [Lexical::TokenRange] @param _tokens [Array<Lexical::Token>] @param theChildren [Array<Object>]
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 66 def return_last_child(_range, _tokens, theChildren) theChildren[-1] end
Utility method. Simply return the last child node @param _range [Lexical::TokenRange] @param _tokens [Array<Lexical::Token>] @param theChildren [Array<Object>]
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 57 def return_second_child(_range, _tokens, theChildren) theChildren[1] end
Utility method. Simply return the second child node @param _range [Lexical::TokenRange] @param _tokens [Array<Lexical::Token>] @param theChildren [Array<Object>]
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 23 def terminal2node raise NotImplementedError end
Method to override in subclass. Returns a Hash @return [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
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 30 def terminalnode_class PTree::TerminalNode end
Method to override in subclass. Default class for representing terminal nodes. @return [Class]
Protected Instance Methods
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 83 def create_tree(aRootNode) Rley::PTree::ParseTree.new(aRootNode) end
Overriding method. Create a parse tree object with given node as root node.
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 93 def new_leaf_node(aProduction, aTerminal, aTokenPosition, aToken) klass = terminal2node.fetch(aTerminal.name, terminalnode_class) if klass.is_a?(Hash) # Lexical ambiguity... klass = klass.fetch(aProduction.name) # steep:ignore ArgumentTypeMismatch end klass.new(aToken, aTokenPosition) # steep:ignore end
Factory method for creating a node object for the given input token. @param aProduction [Syntax::Production] Relevant production rule @param aTerminal [Syntax::Terminal] Terminal associated with the token @param aTokenPosition [Integer] Position of token in the input stream @param aToken [Lexical::Token] The input token
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 109 def new_parent_node(aProduction, aRange, theTokens, theChildren) mth_name = method_name(aProduction.name) # steep:ignore ArgumentTypeMismatch if respond_to?(mth_name, true) node = send(mth_name, aProduction, aRange, theTokens, theChildren) else # Default action... node = case aProduction.rhs.size when 0 return_epsilon(aRange, theTokens, theChildren) when 1 return_first_child(aRange, theTokens, theChildren) else msg = "Don't know production '#{aProduction.name}'" raise StandardError, msg end end return node end
Method to override. Factory method for creating a parent node object. @param aProduction [Production] Production rule @param aRange [Range] Range of tokens matched by the rule @param theTokens [Array] The input tokens @param theChildren [Array] Children nodes (one per rhs symbol)
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 155 def reduce__plus_more(_production, _range, _tokens, theChildren) theChildren[0] << theChildren[1] # steep:ignore NoMethod theChildren[0] end
Implicit rule generated for + modifier rule(‘X’) => ‘X item’.as ‘_plus_more’
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 162 def reduce__plus_one(_production, _range, _tokens, theChildren) [theChildren[0]] end
Implicit rule generated for + modifier rule(‘X’) => ‘item’.as ‘_plus_one’
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 142 def reduce__star_more(_production, _range, _tokens, theChildren) theChildren[0] << theChildren[1] # steep:ignore NoMethod theChildren[0] end
Implicit rule generated for * modifier rule(‘X’) => ‘X item’.as ‘_star_more’
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 149 def reduce__star_none(_production, _range, _tokens, _children) [] end
Implicit rule generated for * modifier rule(‘X’) => ”.as ‘_star_none’
Source
# File lib/rley/parse_rep/ast_base_builder.rb, line 168 def reduce_return_children(_production, _range, _tokens, theChildren) theChildren end
Implicit rule generated for + modifier rule(‘X’) => ‘item’.as ‘_plus_one’