Sha256: 24eb817dea428618080f6a79d7d71ace4635775fe2e4b24c050d933f24eca871

Contents?: true

Size: 1.97 KB

Versions: 14

Compression:

Stored size: 1.97 KB

Contents

require_relative 'parse_tree_builder'
require_relative '../ptree/non_terminal_node'
require_relative '../ptree/terminal_node'
require_relative '../ptree/parse_tree'

module Rley # This module is used as a namespace
  module ParseRep # This module is used as a namespace
    # The purpose of a CSTBuilder is to build piece by piece a CST
    # (Concrete 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 CSTBuilder < ParseTreeBuilder
      protected
      
      # Method to override
      # Create a parse tree object with given
      # node as root node.
      def create_tree(aRootNode)
        return Rley::PTree::ParseTree.new(aRootNode)
      end      

      # Method to override
      # Factory method for creating a node object for the given
      # input token.
      # @param _terminal [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(_production, _terminal, aTokenPosition, aToken)
        PTree::TerminalNode.new(aToken, aTokenPosition)
      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 _tokens [Array] The input tokens
      # @param theChildren [Array] Children nodes (one per rhs symbol)
      def new_parent_node(aProduction, aRange, _tokens, theChildren)
        node = Rley::PTree::NonTerminalNode.new(aProduction.lhs, aRange)
        theChildren.reverse_each { |child| node.add_subnode(child) }
        return node
      end
    end # class
  end # module
end # module

# End of file

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
rley-0.7.03 lib/rley/parse_rep/cst_builder.rb
rley-0.7.02 lib/rley/parse_rep/cst_builder.rb
rley-0.7.01 lib/rley/parse_rep/cst_builder.rb
rley-0.7.00 lib/rley/parse_rep/cst_builder.rb
rley-0.6.09 lib/rley/parse_rep/cst_builder.rb
rley-0.6.08 lib/rley/parse_rep/cst_builder.rb
rley-0.6.07 lib/rley/parse_rep/cst_builder.rb
rley-0.6.06 lib/rley/parse_rep/cst_builder.rb
rley-0.6.05 lib/rley/parse_rep/cst_builder.rb
rley-0.6.04 lib/rley/parse_rep/cst_builder.rb
rley-0.6.03 lib/rley/parse_rep/cst_builder.rb
rley-0.6.02 lib/rley/parse_rep/cst_builder.rb
rley-0.6.01 lib/rley/parse_rep/cst_builder.rb
rley-0.6.00 lib/rley/parse_rep/cst_builder.rb