summaryrefslogtreecommitdiff
path: root/lib/coderay/encoders/debug_lint.rb
diff options
context:
space:
mode:
authorKornelius Kalnbach <murphy@rubychan.de>2016-02-13 16:12:48 +0100
committerKornelius Kalnbach <murphy@rubychan.de>2016-02-13 16:12:48 +0100
commit0b8c69cfb7a65bec04c44e58e5776e323d2aa1af (patch)
treefd81bf6229bfc0d173f5b744534a76b5c70eb440 /lib/coderay/encoders/debug_lint.rb
parent916711c9983483c39f9a68c29e21a0ed40004bd2 (diff)
parent0a1f500d524ff0fb5eeafef051ccbb641954a87a (diff)
downloadcoderay-paint-integration.tar.gz
Merge branch 'master' into paint-integrationpaint-integration
Diffstat (limited to 'lib/coderay/encoders/debug_lint.rb')
-rw-r--r--lib/coderay/encoders/debug_lint.rb63
1 files changed, 63 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..a4eba2c
--- /dev/null
+++ b/lib/coderay/encoders/debug_lint.rb
@@ -0,0 +1,63 @@
+module CodeRay
+module Encoders
+
+ load :lint
+
+ # = 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
+
+ def text_token text, kind
+ raise Lint::EmptyToken, 'empty token for %p' % [kind] if text.empty?
+ raise Lint::UnknownTokenKind, 'unknown token kind %p (text was %p)' % [kind, text] unless TokenKinds.has_key? kind
+ super
+ end
+
+ def begin_group kind
+ @opened << kind
+ super
+ end
+
+ def end_group kind
+ raise Lint::IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
+ @opened.pop
+ super
+ end
+
+ def begin_line kind
+ @opened << kind
+ super
+ end
+
+ def end_line kind
+ raise Lint::IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
+ @opened.pop
+ super
+ end
+
+ protected
+
+ def setup options
+ super
+ @opened = []
+ end
+
+ def finish options
+ raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
+ super
+ end
+
+ end
+
+end
+end