summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/debug_lint.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2013-06-23 13:29:01 +0200
committerKornelius Kalnbach <murphy@rubychan.de>2013-06-23 13:29:01 +0200
commit1d445c70edf8d5c878f9a830dcd29f5a3e179923 (patch)
tree75ad0b7db37981acd3bf54984353e70229e351ad /lib/coderay/encoders/debug_lint.rb
parentd9d6dd5f4a73363ea5d353ecda142f77ed4eba5a (diff)
parent1e330f16c21c45eff375ba3b12f966c76ba0b393 (diff)
downloadcoderay-1d445c70edf8d5c878f9a830dcd29f5a3e179923.tar.gz
Merge branch 'master' into upstream
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..0ac89ef
--- /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} (end_group)" 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} (end_line)" if @opened.pop != kind
+ super
+ end
+
+ end
+
+end
+end