summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/debug_lint.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-06-22 23:52:44 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-06-22 23:52:44 +0200
commit5b1a49fdd3bef559991242a4ad7d3d1ed9cb48c8 (patch)
tree9e74edadd344247e805f6bd3588a34fdf0dbfa21 /lib/coderay/encoders/debug_lint.rb
parent69246fc8ed0344eae4dab35286813a00010a08cb (diff)
parent2abfc49bdc9a9f4e86c90aa968c302ca76c20812 (diff)
downloadcoderay-5b1a49fdd3bef559991242a4ad7d3d1ed9cb48c8.tar.gz
Merge branch 'master' into lua-scanner
Conflicts: lib/coderay/styles/alpha.rb lib/coderay/token_kinds.rb
Diffstat (limited to 'lib/coderay/encoders/debug_lint.rb')
-rw-r--r--lib/coderay/encoders/debug_lint.rb55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/coderay/encoders/debug_lint.rb b/lib/coderay/encoders/debug_lint.rb
new file mode 100644
index 0000000..eeb2a92
--- /dev/null
+++ b/lib/coderay/encoders/debug_lint.rb
@@ -0,0 +1,55 @@
+module CodeRay
+module Encoders
+
+ # = Debug Lint Encoder
+ #
+ # Debug encoder with additional checks for:
+ #
+ # - empty tokens
+ # - incorrect nesting
+ #
+ # It will raise an InvalidTokenStream exception when any of the above occurs.
+ #
+ # See also: Encoders::Debug
+ class DebugLint < Debug
+
+ register_for :debug_lint
+
+ InvalidTokenStream = Class.new StandardError
+ EmptyToken = Class.new InvalidTokenStream
+ IncorrectTokenGroupNesting = Class.new InvalidTokenStream
+
+ def initialize options = {}
+ super
+ @opened = []
+ end
+
+ def text_token text, kind
+ raise EmptyToken, 'empty token' if text.empty?
+ super
+ end
+
+ def begin_group kind
+ @opened << kind
+ super
+ end
+
+ def end_group kind
+ raise IncorrectTokenGroupNesting, "We are inside #{@opened.inspect}, not #{kind}" if @opened.pop != kind
+ super
+ end
+
+ def begin_line kind
+ @opened << kind
+ super
+ end
+
+ def end_line kind
+ raise IncorrectTokenGroupNesting, "We are inside #{@opened.inspect}, not #{kind}" if @opened.pop != kind
+ super
+ end
+
+ end
+
+end
+end