Skip to content
This repository was archived by the owner on Feb 6, 2018. It is now read-only.

Commit dc57601

Browse files
committed
add Lint encoder; do we still need DebugLint?
1 parent fb8c0db commit dc57601

File tree

2 files changed

+62
-7
lines changed

2 files changed

+62
-7
lines changed

lib/coderay/encoders/debug_lint.rb

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
module CodeRay
22
module Encoders
33

4+
load :lint
5+
46
# = Debug Lint Encoder
57
#
68
# Debug encoder with additional checks for:
@@ -15,12 +17,8 @@ class DebugLint < Debug
1517

1618
register_for :debug_lint
1719

18-
InvalidTokenStream = Class.new StandardError
19-
EmptyToken = Class.new InvalidTokenStream
20-
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
21-
2220
def text_token text, kind
23-
raise EmptyToken, 'empty token' if text.empty?
21+
raise Lint::EmptyToken, 'empty token' if text.empty?
2422
super
2523
end
2624

@@ -30,7 +28,7 @@ def begin_group kind
3028
end
3129

3230
def end_group kind
33-
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
31+
raise Lint::IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
3432
@opened.pop
3533
super
3634
end
@@ -41,7 +39,7 @@ def begin_line kind
4139
end
4240

4341
def end_line kind
44-
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
42+
raise Lint::IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
4543
@opened.pop
4644
super
4745
end

lib/coderay/encoders/lint.rb

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module CodeRay
2+
module Encoders
3+
4+
# = Lint Encoder
5+
#
6+
# Checks for:
7+
#
8+
# - empty tokens
9+
# - incorrect nesting
10+
#
11+
# It will raise an InvalidTokenStream exception when any of the above occurs.
12+
#
13+
# See also: Encoders::DebugLint
14+
class Lint < Debug
15+
16+
register_for :lint
17+
18+
InvalidTokenStream = Class.new StandardError
19+
EmptyToken = Class.new InvalidTokenStream
20+
IncorrectTokenGroupNesting = Class.new InvalidTokenStream
21+
22+
def text_token text, kind
23+
raise EmptyToken, 'empty token' if text.empty?
24+
end
25+
26+
def begin_group kind
27+
@opened << kind
28+
end
29+
30+
def end_group kind
31+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_group)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
32+
@opened.pop
33+
end
34+
35+
def begin_line kind
36+
@opened << kind
37+
end
38+
39+
def end_line kind
40+
raise IncorrectTokenGroupNesting, 'We are inside %s, not %p (end_line)' % [@opened.reverse.map(&:inspect).join(' < '), kind] if @opened.last != kind
41+
@opened.pop
42+
end
43+
44+
protected
45+
46+
def setup options
47+
@opened = []
48+
end
49+
50+
def finish options
51+
raise 'Some tokens still open at end of token stream: %p' % [@opened] unless @opened.empty?
52+
end
53+
54+
end
55+
56+
end
57+
end

0 commit comments

Comments
 (0)