Skip to content
This repository was archived by the owner on Feb 6, 2018. It is now read-only.

Commit c386e04

Browse files
committed
refactor Scanner a bit more (rubychan#135)
1 parent 2fb89ea commit c386e04

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

lib/coderay/scanner.rb

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,8 @@ def file_extension
182182
# Scan the code and returns all tokens in a Tokens object.
183183
def tokenize source = nil, options = {}
184184
options = @options.merge(options)
185-
@tokens = options[:tokens] || @tokens || Tokens.new
186-
@tokens.scanner = self if @tokens.respond_to? :scanner=
187185

186+
set_tokens_from_options options
188187
set_string_from_source source
189188

190189
begin
@@ -266,6 +265,11 @@ def set_string_from_source source
266265
end
267266
end
268267

268+
def set_tokens_from_options options
269+
@tokens = options[:tokens] || @tokens || Tokens.new
270+
@tokens.scanner = self if @tokens.respond_to? :scanner=
271+
end
272+
269273
# This is the central method, and commonly the only one a
270274
# subclass implements.
271275
#
@@ -292,7 +296,7 @@ def reset_instance
292296
293297
current line: %d column: %d pos: %d
294298
matched: %p state: %p
295-
bol? = %p, eos? = %p
299+
bol?: %p, eos?: %p
296300
297301
surrounding code:
298302
%p ~~ %p
@@ -303,14 +307,15 @@ def reset_instance
303307
MESSAGE
304308

305309
# Scanner error with additional status information
306-
def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller
310+
def raise_inspect msg, tokens, state = self.state, ambit = 30, backtrace = caller
307311
raise ScanError, SCAN_ERROR_MESSAGE % [
308312
File.basename(caller[0]),
309313
msg,
310-
tokens.respond_to?(:size) ? tokens.size : 0,
311-
tokens.respond_to?(:last) ? tokens.last(10).map { |t| t.inspect }.join("\n") : '',
314+
tokens.respond_to?(:size) ? tokens.size : '[tokens.size undefined]',
315+
tokens.respond_to?(:last) ? tokens.last(10).map(&:inspect).join("\n") : '[tokens.last undefined]',
312316
line, column, pos,
313-
matched, state, bol?, eos?,
317+
matched, state || 'No state given!',
318+
bol?, eos?,
314319
binary_string[pos - ambit, ambit],
315320
binary_string[pos, ambit],
316321
], backtrace

0 commit comments

Comments
 (0)