summaryrefslogtreecommitdiff
path: root/lib/coderay/scanner.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-06-10 18:15:17 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-06-10 18:15:17 +0200
commitc386e044cff195ae922a0b7625dfa498954bf772 (patch)
tree3cd06acc49cf56497f29fedf893a94b9d6505abb /lib/coderay/scanner.rb
parent2fb89ea3abfd8ee6c4eed8defb805a1abd7187d9 (diff)
downloadcoderay-c386e044cff195ae922a0b7625dfa498954bf772.tar.gz
refactor Scanner a bit more (#135)
Diffstat (limited to 'lib/coderay/scanner.rb')
-rw-r--r--lib/coderay/scanner.rb19
1 files changed, 12 insertions, 7 deletions
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index 80ccc5d..9a5be58 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -182,9 +182,8 @@ module CodeRay
# Scan the code and returns all tokens in a Tokens object.
def tokenize source = nil, options = {}
options = @options.merge(options)
- @tokens = options[:tokens] || @tokens || Tokens.new
- @tokens.scanner = self if @tokens.respond_to? :scanner=
+ set_tokens_from_options options
set_string_from_source source
begin
@@ -266,6 +265,11 @@ module CodeRay
end
end
+ def set_tokens_from_options options
+ @tokens = options[:tokens] || @tokens || Tokens.new
+ @tokens.scanner = self if @tokens.respond_to? :scanner=
+ end
+
# This is the central method, and commonly the only one a
# subclass implements.
#
@@ -292,7 +296,7 @@ tokens:
current line: %d column: %d pos: %d
matched: %p state: %p
-bol? = %p, eos? = %p
+bol?: %p, eos?: %p
surrounding code:
%p ~~ %p
@@ -303,14 +307,15 @@ surrounding code:
MESSAGE
# Scanner error with additional status information
- def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller
+ def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = caller
raise ScanError, SCAN_ERROR_MESSAGE % [
File.basename(caller[0]),
msg,
- tokens.respond_to?(:size) ? tokens.size : 0,
- tokens.respond_to?(:last) ? tokens.last(10).map { |t| t.inspect }.join("\n") : '',
+ tokens.respond_to?(:size) ? tokens.size : '[tokens.size undefined]',
+ tokens.respond_to?(:last) ? tokens.last(10).map(&:inspect).join("\n") : '[tokens.last undefined]',
line, column, pos,
- matched, state, bol?, eos?,
+ matched, state || 'No state given!',
+ bol?, eos?,
binary_string[pos - ambit, ambit],
binary_string[pos, ambit],
], backtrace