Skip to content

Commit 5d6bee7

Browse files
committed
tweak Debug scanners again, introduce :unknown token kind
1 parent 2ab42c7 commit 5d6bee7

File tree

4 files changed

+25
-26
lines changed

4 files changed

+25
-26
lines changed

lib/coderay/scanners/debug.rb

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ module Scanners
55

66
# = Debug Scanner
77
#
8-
# Interprets the output of the Encoders::Debug encoder.
8+
# Interprets the output of the Encoders::Debug encoder (basically the inverse function).
99
class Debug < Scanner
1010

1111
register_for :debug
@@ -31,21 +31,24 @@ def scan_tokens encoder, options
3131
if @known_token_kinds.include? self[1]
3232
encoder.text_token self[2].gsub(/\\(.)/m, '\1'), self[1].to_sym
3333
else
34-
encoder.text_token matched, :error
34+
encoder.text_token matched, :unknown
3535
end
3636

3737
elsif match = scan(/ (\w+) ([<\[]) /x)
3838
if @known_token_kinds.include? self[1]
3939
kind = self[1].to_sym
40-
opened_tokens << kind
41-
case self[2]
42-
when '<'
43-
encoder.begin_group kind
44-
when '['
45-
encoder.begin_line kind
46-
else
47-
raise 'CodeRay bug: This case should not be reached.'
48-
end
40+
else
41+
kind = :unknown
42+
end
43+
44+
opened_tokens << kind
45+
case self[2]
46+
when '<'
47+
encoder.begin_group kind
48+
when '['
49+
encoder.begin_line kind
50+
else
51+
raise 'CodeRay bug: This case should not be reached.'
4952
end
5053

5154
elsif !opened_tokens.empty? && match = scan(/ > /x)

lib/coderay/scanners/raydebug.rb

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
module CodeRay
44
module Scanners
55

6-
# = Debug Scanner
6+
# = Raydebug Scanner
77
#
8-
# Parses the output of the Encoders::Debug encoder.
8+
# Highlights the output of the Encoders::Debug encoder.
99
class Raydebug < Scanner
1010

1111
register_for :raydebug
@@ -43,17 +43,12 @@ def scan_tokens encoder, options
4343
encoder.text_token match, :operator if match = scan(/\)/)
4444

4545
elsif match = scan(/ (\w+) ([<\[]) /x)
46-
kind = self[1]
47-
case self[2]
48-
when '<'
49-
encoder.text_token kind, :class
50-
when '['
51-
encoder.text_token kind, :class
46+
encoder.text_token self[1], :class
47+
if @known_token_kinds.include? self[1]
48+
kind = self[1].to_sym
5249
else
53-
raise 'CodeRay bug: This case should not be reached.'
50+
kind = :unknown
5451
end
55-
# FIXME: cache attack
56-
kind = kind.to_sym
5752
opened_tokens << kind
5853
encoder.begin_group kind
5954
encoder.text_token self[2], :operator

lib/coderay/token_kinds.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,5 +80,6 @@ module CodeRay
8080
:plain => false # almost all scanners
8181
)
8282

83-
TokenKinds[:method] = TokenKinds[:function]
83+
TokenKinds[:method] = TokenKinds[:function]
84+
TokenKinds[:unknown] = TokenKinds[:plain]
8485
end

test/unit/debug.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def test_creation
5151
end
5252

5353
TEST_INPUT = <<-'DEBUG'.chomp
54-
integer(10)operator((\\\))string<content(test)>head[
54+
integer(10)operator((\\\))string<content(test)>test[
5555
5656
5757
method([])]
@@ -62,10 +62,10 @@ def test_creation
6262
[:begin_group, :string],
6363
['test', :content],
6464
[:end_group, :string],
65-
[:begin_line, :head],
65+
[:begin_line, :unknown],
6666
["\n\n \t \n", :space],
6767
["[]", :method],
68-
[:end_line, :head],
68+
[:end_line, :unknown],
6969
].flatten
7070

7171
def test_filtering_text_tokens

0 commit comments

Comments
 (0)