lib/eturem/base.rb in eturem-0.3.1 vs lib/eturem/base.rb in eturem-0.3.2

- old
+ new

@@ -5,11 +5,11 @@ # @param [String] file script file # @return [Eturem::Base] if exception raised # @return [nil] if exception did not raise def self.load(file) begin - Kernel.load file + Kernel.load(File.expand_path(file)) rescue Exception => exception return @eturem_class.new(exception) unless exception.is_a? SystemExit end return nil end @@ -79,11 +79,11 @@ def initialize(exception) @exception = exception @exception_s = exception.to_s eturem_path = File.dirname(File.absolute_path(__FILE__)) - @backtrace_locations = @exception.backtrace_locations.reject do |location| + @backtrace_locations = (@exception.backtrace_locations || []).reject do |location| path = File.absolute_path(location.path) path.start_with?(eturem_path) || path.end_with?("/rubygems/core_ext/kernel_require.rb") end @backtrace_locations.each do |location| if $0 == location.path @@ -91,11 +91,11 @@ super.sub("<top (required)>", "<main>") end end end - if @exception.is_a?(SyntaxError) && @exception_s.match(/\A(?<path>[^:]+?)\:(?<lineno>\d+)/) + if @exception.is_a?(SyntaxError) && @exception_s.match(/\A(?<path>.+?)\:(?<lineno>\d+)/) @path = Regexp.last_match(:path) @lineno = Regexp.last_match(:lineno).to_i else backtrace_locations_shift end @@ -127,15 +127,14 @@ end def exception_inspect inspect_methods = self.class.inspect_methods inspect_methods.keys.reverse_each do |key| - case key - when Class - return public_send(inspect_methods[key]) if @exception.is_a? key - when String - return public_send(inspect_methods[key]) if @exception.class.to_s == key + if (key.is_a?(Class) && @exception.is_a?(key)) || + (key.is_a?(String) && @exception.class.to_s == key) + method = inspect_methods[key] + return method ? public_send(method) : nil end end return nil end @@ -179,13 +178,16 @@ end end def prepare_syntax_error @unexpected = @exception_s.match(/unexpected (?<unexpected>(?:','|[^,])+)/) ? - Regexp.last_match(:unexpected) : "end-of-input" + Regexp.last_match(:unexpected) : nil @expected = @exception_s.match(/[,\s]expecting (?<expected>\S+)/) ? - Regexp.last_match(:expected) : "end-of-input" + Regexp.last_match(:expected) : nil + if !@expected && @exception_s.match(/(?<invalid>(?:break|next|retry|redo|yield))/) + @invalid = Regexp.last_match(:invalid) + end end def prepare_name_error highlight!(@script_lines[@lineno], @exception.name.to_s, "\e[1;31m\e[4m") return unless @exception_s.match(/Did you mean\?/) @@ -242,10 +244,10 @@ def location_inspect(location) "from #{location.path}:#{location.lineno}:in `#{location.label}'" end def load_script - @script = "" + @script ||= "" if @path && File.exist?(@path) @script = File.binread(@path) encoding = "utf-8" if @script.match(/\A(?:#!.*\R)?#.*coding *[:=] *(?<encoding>[^\s:]+)/) encoding = Regexp.last_match(:encoding)