summaryrefslogtreecommitdiff
path: root/lib/coderay/scanner.rb
diff options
context:
space:
mode:
authormurphy <murphy@rubychan.de>2010-06-01 21:38:46 +0000
committermurphy <murphy@rubychan.de>2010-06-01 21:38:46 +0000
commit39eb4c9f60e296f9f6c51784ef7e0a234966755d (patch)
tree6e36a60c98972fd1e9e9054b8c2a4e1630cff1f8 /lib/coderay/scanner.rb
parentf9e4f054f7aa6129bf400188f58f7a8c359e2b2f (diff)
downloadcoderay-39eb4c9f60e296f9f6c51784ef7e0a234966755d.tar.gz
Added ability to scan an Array of Strings as one with Scanner#tokenize.
See issue #222.
Diffstat (limited to 'lib/coderay/scanner.rb')
-rw-r--r--lib/coderay/scanner.rb19
1 files changed, 16 insertions, 3 deletions
diff --git a/lib/coderay/scanner.rb b/lib/coderay/scanner.rb
index 7d154da..64b3c83 100644
--- a/lib/coderay/scanner.rb
+++ b/lib/coderay/scanner.rb
@@ -147,14 +147,27 @@ module CodeRay
end
# Scans the code and returns all tokens in a Tokens object.
- def tokenize new_string=nil, options = {}
+ def tokenize source = nil, options = {}
options = @options.merge(options)
@tokens = options[:tokens] || @tokens || Tokens.new
@tokens.scanner = self if @tokens.respond_to? :scanner=
- self.string = new_string if new_string
- reset unless new_string
+ case source
+ when String
+ self.string = source
+ when Array
+ self.string = source.join
+ when nil
+ reset
+ else
+ raise ArgumentError, 'expected String, Array, or nil'
+ end
scan_tokens @tokens, options
@cached_tokens = @tokens
+ if source.is_a? Array
+ @tokens.split_into_parts(*source.map { |part| part.size })
+ else
+ @tokens
+ end
end
# Caches the result of tokenize.