forked from rouge-ruby/rouge
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer_spec.rb
164 lines (134 loc) · 3.61 KB
/
lexer_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# -*- coding: utf-8 -*- #
describe Rouge::Lexer do
include Support::Lexing
it 'guesses the lexer with Lexer.guess' do
assert { Rouge::Lexer.guess(filename: 'foo.rb').tag == 'ruby' }
end
it 'guesses lexers with Lexer.guesses' do
assert { Rouge::Lexer.guesses(filename: 'foo.pl').map { |c| c.tag }.sort == ['perl', 'prolog'].sort }
end
it 'raises errors in .guess by default' do
assert { (Rouge::Lexer.guess(filename: 'foo.pl') rescue nil) == nil }
end
it 'customizes ambiguous cases in .guess' do
assert { Rouge::Lexer.guess(filename: 'foo.pl') { :fallback } == :fallback }
end
it 'makes a simple lexer' do
a_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule /a/, 'A'
rule /b/, 'B'
end
end
# consolidation
result = a_lexer.lex('aa').to_a
assert { result.size == 1 }
assert { result == [['A', 'aa']] }
result = a_lexer.lex('abab').to_a
assert { result.size == 4 }
assert { result == [['A', 'a'], ['B', 'b']] * 2 }
end
it 'pushes and pops states' do
a_lexer = Class.new(Rouge::RegexLexer) do
state :brace do
rule /b/, 'B'
rule /}/, 'Brace', :pop!
end
state :root do
rule /{/, 'Brace', :brace
rule /a/, 'A'
end
end
result = a_lexer.lex('a{b}a').to_a
assert { result.size == 5 }
# failed parses
t = Rouge::Token
assert {
a_lexer.lex('{a}').to_a ==
[['Brace', '{'], [t['Error'], 'a'], ['Brace', '}']]
}
assert { a_lexer.lex('b').to_a == [[t['Error'], 'b']] }
assert { a_lexer.lex('}').to_a == [[t['Error'], '}']] }
end
it 'does callbacks and grouping' do
callback_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule /(a)(b)/ do |s|
groups('A', 'B')
end
end
end
result = callback_lexer.lex('ab').to_a
assert { result.size == 2 }
assert { result[0] == ['A', 'a'] }
assert { result[1] == ['B', 'b'] }
end
it 'pops from the callback' do
callback_lexer = Class.new(Rouge::RegexLexer) do
state :root do
rule /a/, 'A', :a
rule /d/, 'D'
end
state :a do
rule /b/, 'B', :b
end
state :b do
rule /c/ do |ss|
token 'C'
pop!; pop! # go back to the root
end
end
end
assert_no_errors 'abcd', callback_lexer
end
it 'supports stateful lexes' do
stateful = Class.new(Rouge::RegexLexer) do
def incr
@count += 1
end
state :root do
rule /\d+/ do |ss|
token 'digit'
@count = ss[0].to_i
end
rule /\+/ do |ss|
incr
token(@count <= 5 ? 'lt' : 'gt')
end
end
end
result = stateful.lex('4++')
types = result.map { |(t,_)| t }
assert { types == %w(digit lt gt) }
end
it 'delegates' do
class MasterLexer < Rouge::RegexLexer
state :root do
rule /a/, 'A'
rule /{(.*?)}/ do |m|
token 'brace', '{'
delegate BracesLexer.new, m[1]
token 'brace', '}'
end
end
end
class BracesLexer < Rouge::RegexLexer
state :root do
rule /b/, 'B'
end
end
assert_no_errors 'a{b}a', MasterLexer
end
it 'detects the beginnings of lines with ^ rules' do
class MyLexer < Rouge::RegexLexer
state :root do
rule /^a/, 'start'
rule /a/, 'not-start'
end
end
assert_has_token('start', 'a', MyLexer)
assert_has_token('start', "\na", MyLexer)
deny_has_token('not-start', 'a', MyLexer)
assert_has_token('not-start', 'aa', MyLexer)
end
end