Sha256: 83a44fbfc6870490c3d73e3c20c4bc303797cb18458168c56f1d17337e185ccc

Contents?: true

Size: 1.99 KB

Versions: 11

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true

require_relative '../parser/parse_walker_factory'

module Rley # This module is used as a namespace
  module ParseRep # This module is used as a namespace
    # Utility class that helps to create a representation of a parse from
    # a given Parsing object.
    class ParseRepCreator
      # @return [Rley::Parser::GFGParsing]
      #  Link to Parsing object (= results of recognizer)
      attr_reader(:parsing)

      # Constructor. Creates and initialize a ParseRepCreator instance.
      # @return [ParseRepCreator]
      def initialize(aParsingResult)
        @parsing = aParsingResult
      end

      # Factory method that produces the representation of the parse.
      # @return [Rley::PTree::ParseTree, Rley::SPPF::ParseForest]
      #   The parse representation.
      def create(aBuilder = nil)
        a_walker = walker(parsing)
        a_builder = builder(parsing, aBuilder)

        begin
          loop do
            event = a_walker.next
            # puts "EVENT #{event[0]} #{event[1]}"
            a_builder.receive_event(*event)
          end
        rescue StopIteration
          # Do nothing: gobble the exception
        rescue StandardError => e
          if e.message =~ /^Ambiguous/
            $stderr.puts parsing
          end
          raise e
        end

        a_builder.done!

        a_builder.result
      end

      private

      # Create a Parsing walker, that is, an object
      # that will iterate over the relevant nodes (= parsing entries)
      # of a GFGParsing
      def walker(aParseResult)
        walker_factory = Parser::ParseWalkerFactory.new
        accept_entry = aParseResult.accepting_entry
        accept_index = aParseResult.chart.last_index
        walker_factory.build_walker(accept_entry, accept_index, jump_to_start)
      end

      # By default, when a end vertex is re-visited don't jump
      # its corresponding start vertex.
      def jump_to_start
        false
      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/parse_rep_creator.rb
rley-0.8.10 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.09 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.08 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.06 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.05 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.03 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.02 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.01 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.8.00 lib/rley/parse_rep/parse_rep_creator.rb
rley-0.7.08 lib/rley/parse_rep/parse_rep_creator.rb