Sha256: e3091c53002cef042c4a04ad92b8b015a5d9fe3460c27bfb7665b268bf4ad5a0

Contents?: true

Size: 1.35 KB

Versions: 3

Compression:

Stored size: 1.35 KB

Contents

require 'modl/parser/throwing_error_listener'
require 'modl/parser/parsed'

module Modl
  module Parser

    # This class converts the input string into a Modl:Parser::Parsed object for further processing.
    class Parser
      def self.parse(str, global = nil)
        begin
          lexer = Modl::Parser::MODLLexer.new(Antlr4::Runtime::CharStreams.from_string(str, 'String'))
          lexer.remove_error_listeners
          lexer.add_error_listener ThrowingErrorListener.instance

          tokens = Antlr4::Runtime::CommonTokenStream.new(lexer)

          parser = Modl::Parser::MODLParser.new(tokens)
          parser.remove_error_listeners
          parser.add_error_listener ThrowingErrorListener.instance

          global = GlobalParseContext.new if global.nil?

          parsed = Parsed.new(global)
          parser.modl.enter_rule(parsed)
          parsed
        rescue StandardError => e
          puts e.message
          puts e.backtrace

          if global.syntax_version > global.interpreter_syntax_version
            raise InterpreterError, 'MODL Version ' +
                global.interpreter_syntax_version.to_s +
                ' interpreter cannot process this MODL Version ' +
                global.syntax_version.to_s + ' file.'
          end

          raise InterpreterError, 'Parser Error: ' + e.message
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
modl-0.3.2 lib/modl/parser/parser.rb
modl-0.3.1 lib/modl/parser/parser.rb
modl-0.3.0 lib/modl/parser/parser.rb