Skip to content

Commit 2fb89ea

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

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

lib/coderay/scanner.rb

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,8 @@ def tokenize source = nil, options = {}
184184
options = @options.merge(options)
185185
@tokens = options[:tokens] || @tokens || Tokens.new
186186
@tokens.scanner = self if @tokens.respond_to? :scanner=
187-
case source
188-
when Array
189-
self.string = self.class.normalize(source.join)
190-
when nil
191-
reset
192-
else
193-
self.string = self.class.normalize(source)
194-
end
187+
188+
set_string_from_source source
195189

196190
begin
197191
scan_tokens @tokens, options
@@ -261,6 +255,17 @@ def binary_string
261255
def setup # :doc:
262256
end
263257

258+
def set_string_from_source source
259+
case source
260+
when Array
261+
self.string = self.class.normalize(source.join)
262+
when nil
263+
reset
264+
else
265+
self.string = self.class.normalize(source)
266+
end
267+
end
268+
264269
# This is the central method, and commonly the only one a
265270
# subclass implements.
266271
#
@@ -277,9 +282,7 @@ def reset_instance
277282
@binary_string = nil if defined? @binary_string
278283
end
279284

280-
# Scanner error with additional status information
281-
def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller
282-
raise ScanError, <<-EOE % [
285+
SCAN_ERROR_MESSAGE = <<-MESSAGE
283286
284287
285288
***ERROR in %s: %s (after %d tokens)
@@ -297,7 +300,11 @@ def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit =
297300
298301
***ERROR***
299302
300-
EOE
303+
MESSAGE
304+
305+
# Scanner error with additional status information
306+
def raise_inspect msg, tokens, state = self.state || 'No state given!', ambit = 30, backtrace = caller
307+
raise ScanError, SCAN_ERROR_MESSAGE % [
301308
File.basename(caller[0]),
302309
msg,
303310
tokens.respond_to?(:size) ? tokens.size : 0,

0 commit comments

Comments
 (0)