summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/coderay.rb3
-rw-r--r--lib/coderay/tokens_proxy.rb27
2 files changed, 29 insertions, 1 deletions
diff --git a/lib/coderay.rb b/lib/coderay.rb
index 503de7d..c897220 100644
--- a/lib/coderay.rb
+++ b/lib/coderay.rb
@@ -134,6 +134,7 @@ module CodeRay
# Tokens
autoload :Tokens, 'coderay/tokens'
+ autoload :TokensProxy, 'coderay/tokens_proxy'
autoload :TokenKinds, 'coderay/token_kinds'
# Plugin system
@@ -159,7 +160,7 @@ module CodeRay
# See also demo/demo_simple.
def scan code, lang, options = {}, &block
# FIXME: return a proxy for direct-stream encoding
- scanner(lang, options, &block).tokenize code
+ TokensProxy.new code, lang, options, block
end
# Scans +filename+ (a path to a code file) with the Scanner for +lang+.
diff --git a/lib/coderay/tokens_proxy.rb b/lib/coderay/tokens_proxy.rb
new file mode 100644
index 0000000..598ad2e
--- /dev/null
+++ b/lib/coderay/tokens_proxy.rb
@@ -0,0 +1,27 @@
+module CodeRay
+
+ class TokensProxy < Struct.new :code, :lang, :options, :block
+
+ def method_missing method, *args, &blk
+ tokens.send(method, *args, &blk)
+ end
+
+ def tokens
+ @tokens ||= scanner.tokenize(code)
+ end
+
+ def each *args, &blk
+ tokens.each(*args, &blk)
+ end
+
+ def count
+ tokens.count
+ end
+
+ def scanner
+ @scanner ||= CodeRay.scanner(lang, options, &block)
+ end
+
+ end
+
+end