diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-10 18:03:18 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-10 18:03:18 +0200 |
commit | 2fb89ea3abfd8ee6c4eed8defb805a1abd7187d9 (patch) | |
tree | f8722f1e51dc5a44715cdf747ed3879af2808b00 /lib | |
parent | cd7433c5a4d9114e8a398d4fd3eeeadecf290260 (diff) | |
download | coderay-2fb89ea3abfd8ee6c4eed8defb805a1abd7187d9.tar.gz |
refactor Scanner a bit (#135)
Diffstat (limited to 'lib')
-rw-r--r-- | lib/coderay/scanner.rb | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb index 907cf00..80ccc5d 100644 --- a/lib/coderay/scanner.rb +++ b/lib/coderay/scanner.rb @@ -184,14 +184,8 @@ module CodeRay options = @options.merge(options) @tokens = options[:tokens] || @tokens || Tokens.new @tokens.scanner = self if @tokens.respond_to? :scanner= - case source - when Array - self.string = self.class.normalize(source.join) - when nil - reset - else - self.string = self.class.normalize(source) - end + + set_string_from_source source begin scan_tokens @tokens, options @@ -261,6 +255,17 @@ module CodeRay def setup # :doc: end + def set_string_from_source source + case source + when Array + self.string = self.class.normalize(source.join) + when nil + reset + else + self.string = self.class.normalize(source) + end + end + # This is the central method, and commonly the only one a # subclass implements. # @@ -277,9 +282,7 @@ module CodeRay @binary_string = nil if defined? @binary_string end - # Scanner error with additional status information - def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller - raise ScanError, <<-EOE % [ + SCAN_ERROR_MESSAGE = <<-MESSAGE ***ERROR in %s: %s (after %d tokens) @@ -297,7 +300,11 @@ surrounding code: ***ERROR*** - EOE + MESSAGE + + # Scanner error with additional status information + def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller + raise ScanError, SCAN_ERROR_MESSAGE % [ File.basename(caller[0]), msg, tokens.respond_to?(:size) ? tokens.size : 0, |