examples/general/SRL/lib/ast_builder.rb in rley-0.5.14 vs examples/general/SRL/lib/ast_builder.rb in rley-0.6.00

- old
+ new

@@ -1,58 +1,35 @@ require 'stringio' -require_relative 'ast_building' require_relative 'regex_repr' # The purpose of a ASTBuilder 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. # 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. -class ASTBuilder < Rley::Parser::ParseTreeBuilder - include ASTBuilding +class ASTBuilder < Rley::ParseRep::ASTBaseBuilder Terminal2NodeClass = { }.freeze attr_reader :options protected + + def terminal2node() + Terminal2NodeClass + end # Overriding method. # Factory method for creating a node object for the given # input token. # @param aTerminal [Terminal] Terminal symbol associated with the token # @param aTokenPosition [Integer] Position of token in the input stream # @param aToken [Token] The input token def new_leaf_node(aProduction, aTerminal, aTokenPosition, aToken) node = Rley::PTree::TerminalNode.new(aToken, aTokenPosition) - 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) - def new_parent_node(aProduction, aRange, theTokens, theChildren) - short_name = aProduction.name - method_name = 'reduce_' + short_name - if self.respond_to?(method_name, true) - node = send(method_name, aProduction, aRange, theTokens, theChildren) - else - # Default action... - node = case aProduction.rhs.size - when 0 - nil - when 1 - return_first_child(aRange, theTokens, theChildren) - else - raise StandardError, "Don't know production '#{aProduction.name}'" - end - end return node end def multiplicity(lowerBound, upperBound) return SRL::Regex::Multiplicity.new(lowerBound, upperBound, :greedy)