Sha256: 43aa2d872eb34d1cfee09fa4d0b0d2dd470cdfbca5877435453d6dc01f7422da

Contents?: true

Size: 1.99 KB

Versions: 11

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

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) }

        node
      end
    end # class
  end # module
end # module

# End of file

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
rley-0.8.11 lib/rley/parse_rep/cst_builder.rb
rley-0.8.10 lib/rley/parse_rep/cst_builder.rb
rley-0.8.09 lib/rley/parse_rep/cst_builder.rb
rley-0.8.08 lib/rley/parse_rep/cst_builder.rb
rley-0.8.06 lib/rley/parse_rep/cst_builder.rb
rley-0.8.05 lib/rley/parse_rep/cst_builder.rb
rley-0.8.03 lib/rley/parse_rep/cst_builder.rb
rley-0.8.02 lib/rley/parse_rep/cst_builder.rb
rley-0.8.01 lib/rley/parse_rep/cst_builder.rb
rley-0.8.00 lib/rley/parse_rep/cst_builder.rb
rley-0.7.08 lib/rley/parse_rep/cst_builder.rb