summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/debug_lint.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-06-13 04:46:11 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-06-13 04:46:11 +0200
commit88b812274a808f69675445a9265dab1d4e41cc44 (patch)
tree1c3cb1a6f87aa5c85d3ffbf2575df6dd68c3b2a1 /lib/coderay/encoders/debug_lint.rb
parentdf21150ed089f1e3d1d35badde23b2d905a421cd (diff)
downloadcoderay-88b812274a808f69675445a9265dab1d4e41cc44.tar.gz
refactor check functionality to DebugLint encoder
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