summaryrefslogtreecommitdiff
path: root/lib/coderay/scanner.rb
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2011-06-22 07:22:23 +0000
committermurphy <murphy@rubychan.de>2011-06-22 07:22:23 +0000
commitd6fe4e777a4f543c8828dbf77e955ab38e6c2803 (patch)
tree7f2a155c7645718f8936e649aee05574cd3d1b54 /lib/coderay/scanner.rb
parent90f70ee61e87e137aa192c5db97c382e1ec7d24b (diff)
downloadcoderay-d6fe4e777a4f543c8828dbf77e955ab38e6c2803.tar.gz
#309 Improved highlighting of Ruby inside diffs
Diffstat (limited to 'lib/coderay/scanner.rb')
-rw-r--r--lib/coderay/scanner.rb15
1 files changed, 12 insertions, 3 deletions
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/).