diff options
author | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-30 05:58:31 +0200 |
---|---|---|
committer | Kornelius Kalnbach <murphy@rubychan.de> | 2013-06-30 05:58:31 +0200 |
commit | a2c625b78d9cb7ff4e89176ade6869364b4f2f9b (patch) | |
tree | 8eeca64a75e814477c670fbcb9e607cd9bcdd5be /lib/coderay/encoders/lint.rb | |
parent | dd9ec43a3ea4cfce8eb1e8dd38504a9d8f24df54 (diff) | |
parent | dc57601571af8024700991b6a80129285a980e9e (diff) | |
download | coderay-a2c625b78d9cb7ff4e89176ade6869364b4f2f9b.tar.gz |
Merge branch 'master' into go-scanner
Diffstat (limited to 'lib/coderay/encoders/lint.rb')
-rw-r--r-- | lib/coderay/encoders/lint.rb | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/lib/coderay/encoders/lint.rb b/lib/coderay/encoders/lint.rb new file mode 100644 index 0000000..4601e90 --- /dev/null +++ b/lib/coderay/encoders/lint.rb @@ -0,0 +1,57 @@ +module CodeRay +module Encoders + + # = Lint Encoder + # + # Checks for: + # + # - empty tokens + # - incorrect nesting + # + # It will raise an InvalidTokenStream exception when any of the above occurs. + # + # See also: Encoders::DebugLint + class Lint < Debug + + register_for :lint + + InvalidTokenStream = Class.new StandardError + EmptyToken = Class.new InvalidTokenStream + IncorrectTokenGroupNesting = Class.new InvalidTokenStream + + def text_token text, kind + raise EmptyToken, 'empty token' if text.empty? + end + + def begin_group kind + @opened << kind + end + + def end_group kind + raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind + @opened.pop + end + + def begin_line kind + @opened << kind + end + + def end_line kind + raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind + @opened.pop + end + + protected + + def setup options + @opened = [] + end + + def finish options + raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty? + end + + end + +end +end |