Sha256: 2ce8aa9ce06ac71b3bd58302d65da32c4a040058028270fa4b50e7cb2aa10dff

Contents?: true

Size: 1.86 KB

Versions: 45

Compression:

Stored size: 1.86 KB

Contents

# frozen_string_literal: true

require_relative 'scanner'
require_relative 'grammar'
require_relative '../ast/ast_builder'

module Loxxy
  module FrontEnd
    # A Lox parser that produce concrete parse trees.
    # Concrete parse trees are the default kind of parse tree
    # generated by the Rley library.
    # They consist of two node types only:
    # - NonTerminalNode
    # - TerminalNode
    # A NonTerminalNode has zero or more child nodes (called subnodes)
    # A TerminalNode is leaf node, that is, it has no child node.
    # While concrete parse tree nodes can be generated out of the box,
    # they have the following drawbacks:
    # - Generic node classes that aren't always suited for the needs of
    #     the language being processing.
    # - Concrete parse tree tend to be deeply nested, which may complicate
    #   further processing.
    class Parser
      # @return [Rley::Engine] A facade object for the Rley parsing library
      attr_reader(:engine)

      def initialize
        # Create a Rley facade object
        @engine = Rley::Engine.new do |cfg|
          cfg.diagnose = true
          cfg.repr_builder = Ast::ASTBuilder
        end

        # Step 1. Load Lox grammar
        @engine.use_grammar(Loxxy::FrontEnd::Grammar)
      end

      # Parse the given Lox program into a parse tree.
      # @param source [String] Lox program to parse
      # @return [Rley::ParseTree] A parse tree equivalent to the Lox input.
      def parse(source)
        lexer = Scanner.new(source)
        result = engine.parse(lexer.tokens)

        unless result.success?
          # Stop if the parse failed...
          line1 = "Parsing failed\n"
          line2 = "Reason: #{result.failure_reason.message}"
          raise StandardError, line1 + line2
        end

        return engine.convert(result) # engine.to_ptree(result)
      end
    end # class
  end # module
end # module

Version data entries

45 entries across 45 versions & 1 rubygems

Version Path
loxxy-0.2.03 lib/loxxy/front_end/parser.rb
loxxy-0.2.02 lib/loxxy/front_end/parser.rb
loxxy-0.2.01 lib/loxxy/front_end/parser.rb
loxxy-0.2.00 lib/loxxy/front_end/parser.rb
loxxy-0.1.17 lib/loxxy/front_end/parser.rb
loxxy-0.1.16 lib/loxxy/front_end/parser.rb
loxxy-0.1.15 lib/loxxy/front_end/parser.rb
loxxy-0.1.14 lib/loxxy/front_end/parser.rb
loxxy-0.1.13 lib/loxxy/front_end/parser.rb
loxxy-0.1.12 lib/loxxy/front_end/parser.rb
loxxy-0.1.11 lib/loxxy/front_end/parser.rb
loxxy-0.1.10 lib/loxxy/front_end/parser.rb
loxxy-0.1.09 lib/loxxy/front_end/parser.rb
loxxy-0.1.08 lib/loxxy/front_end/parser.rb
loxxy-0.1.07 lib/loxxy/front_end/parser.rb
loxxy-0.1.06 lib/loxxy/front_end/parser.rb
loxxy-0.1.05 lib/loxxy/front_end/parser.rb
loxxy-0.1.04 lib/loxxy/front_end/parser.rb
loxxy-0.1.03 lib/loxxy/front_end/parser.rb
loxxy-0.1.02 lib/loxxy/front_end/parser.rb