diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb index 9a5be584..68451ae3 100644 --- a/lib/coderay/scanner.rb +++ b/lib/coderay/scanner.rb @@ -289,7 +289,7 @@ def reset_instance SCAN_ERROR_MESSAGE = <<-MESSAGE -***ERROR in %s: %s (after %d tokens) +***ERROR in %s: %s (after %s tokens) tokens: %s @@ -311,8 +311,8 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle raise ScanError, SCAN_ERROR_MESSAGE % [ File.basename(caller[0]), msg, - tokens.respond_to?(:size) ? tokens.size : '[tokens.size undefined]', - tokens.respond_to?(:last) ? tokens.last(10).map(&:inspect).join("\n") : '[tokens.last undefined]', + tokens_size, + tokens_last(10).map(&:inspect).join("\n"), line, column, pos, matched, state || 'No state given!', bol?, eos?, @@ -321,6 +321,14 @@ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = calle ], backtrace end + def tokens_size + tokens.size if tokens.respond_to?(:size) + end + + def tokens_last n + tokens.respond_to?(:last) ? tokens.last(n) : [] + end + # Shorthand for scan_until(/\z/). # This method also avoids a JRuby 1.9 mode bug. def scan_rest