Sha256: 0bd8b08b779b9896b9d2c19c81a826c7922e572edcbb94c0ab4ad863ea185303

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

require 'gobstones/parser/treetop_parser'
require 'gobstones/parser/parse_error'
require 'gobstones/cli/printer'

include Gobstones::Parser

module Gobstones

  module CLI

    class Runner

      def self.run(file_name)
        new(File.read(file_name)).run
      end

      def initialize(code)
        @code = code
        @parser = Gobstones::Parser::TreetopParser.new
      end

      def run
        print_program_result parse_program.evaluate
      rescue Gobstones::Parser::ParseError => parse_error
        handle_parse_error parse_error
      rescue Exception => e
        # TODO handle more gobstones exceptions
        raise e
      end

      private

      def print_program_result(context)
        Printer.new(context).print
      end

      def parse_program
        @parser.parse(@code)
      end

      def handle_parse_error(parse_error)
        parse_error.parser.failure_reason =~ /^(Expected .+) after/m
        puts "#{$1.gsub("\n", '$NEWLINE')}:"
        puts parse_error.code.lines.to_a[parse_error.parser.failure_line - 1]
        puts "#{'~' * (parse_error.parser.failure_column - 1)}^"
      end

    end

  end

end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gobstones-0.0.1.1 lib/gobstones/cli/runner.rb