From d6fe4e777a4f543c8828dbf77e955ab38e6c2803 Mon Sep 17 00:00:00 2001 From: murphy Date: Wed, 22 Jun 2011 07:22:23 +0000 Subject: #309 Improved highlighting of Ruby inside diffs --- lib/coderay/scanner.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'lib/coderay/scanner.rb') diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb index 925acf7..ec2b3aa 100644 --- a/lib/coderay/scanner.rb +++ b/lib/coderay/scanner.rb @@ -61,6 +61,8 @@ module CodeRay KINDS_NOT_LOC = [:comment, :doctype, :docstring] + attr_accessor :state + class << self # Normalizes the given code into a string with UNIX newlines, in the @@ -190,7 +192,14 @@ module CodeRay else raise ArgumentError, 'expected String, Array, or nil' end - scan_tokens @tokens, options + + begin + scan_tokens @tokens, options + rescue => e + message = "Error in %s#scan_tokens, initial state was: %p" % [self.class, defined?(state) && state] + raise_inspect e.message, @tokens, message, 30, e.backtrace + end + @cached_tokens = @tokens if source.is_a? Array @tokens.split_into_parts(*source.map { |part| part.size }) @@ -260,7 +269,7 @@ module CodeRay end # Scanner error with additional status information - def raise_inspect msg, tokens, state = 'No state given!', ambit = 30 + def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller raise ScanError, <<-EOE % [ @@ -288,7 +297,7 @@ surrounding code: matched, state, bol?, eos?, string[pos - ambit, ambit], string[pos, ambit], - ] + ], backtrace end # Shorthand for scan_until(/\z/). -- cgit v1.2.1