/
tc_lexer.rb
216 lines (164 loc) · 4.02 KB
/
tc_lexer.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# Author: Chris Wailes <chris.wailes@gmail.com>
# Project: Ruby Language Toolkit
# Date: 2011/04/06
# Description: This file contains unit tests for the RLTK::Lexer class.
############
# Requires #
############
# Gems
require 'minitest/autorun'
# Ruby Language Toolkit
require 'rltk/token'
require 'rltk/lexer'
require 'rltk/lexers/calculator'
require 'rltk/lexers/ebnf'
#######################
# Classes and Modules #
#######################
class LexerTester < Minitest::Test
class ABLongest < RLTK::Lexer
rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }
rule(/a+b+/) { :APLUSBPLUS }
end
class ABFirst < RLTK::Lexer
match_first
rule(/a+/) { :APLUS }
rule(/b+/) { :BPLUS }
rule(/a+b+/) { :APLUSBPLUS }
end
class ENVLexer < RLTK::Lexer
rule(/a/) { [:A, next_value] }
class Environment < Environment
def initialize(*args)
super(*args)
@value = -1
end
def next_value
@value += 1
end
end
end
class FlagLexer < RLTK::Lexer
rule(/a/) { set_flag(:a); :A }
rule(/\s/)
rule(/b/, :default, [:a]) { set_flag(:b); :B }
rule(/c/, :default, [:a, :b]) { :C }
end
class StateLexer < RLTK::Lexer
rule(/a/) { :A }
rule(/\s/)
rule(/\(\*/) { push_state(:comment) }
rule(/\(\*/, :comment) { push_state(:comment) }
rule(/\*\)/, :comment) { pop_state }
rule(/./, :comment)
end
class MatchDataLexer < RLTK::Lexer
rule(/a(b*)(c+)/) { [:FOO, match[1,2]] }
end
def test_calc
expected = [
RLTK::Token.new(:NUM, 1),
RLTK::Token.new(:PLS),
RLTK::Token.new(:SUB),
RLTK::Token.new(:MUL),
RLTK::Token.new(:DIV),
RLTK::Token.new(:LPAREN),
RLTK::Token.new(:RPAREN),
RLTK::Token.new(:EOS)
]
actual = RLTK::Lexers::Calculator.lex('1 + - * / ( )')
assert_equal(expected, actual)
end
def test_ebnf
expected = [
RLTK::Token.new(:NONTERM, :aaa),
RLTK::Token.new(:TERM, :BBB),
RLTK::Token.new(:STAR),
RLTK::Token.new(:PLUS),
RLTK::Token.new(:QUESTION),
RLTK::Token.new(:EOS)
]
actual = RLTK::Lexers::EBNF.lex('aaa BBB * + ?')
assert_equal(expected, actual)
end
def test_environment
expected = [
RLTK::Token.new(:A, 0),
RLTK::Token.new(:A, 1),
RLTK::Token.new(:A, 2),
RLTK::Token.new(:EOS)
]
actual = ENVLexer.lex('aaa')
assert_equal(expected, actual)
lexer = ENVLexer.new
assert_equal(expected, lexer.lex('aaa'))
expected = [
RLTK::Token.new(:A, 3),
RLTK::Token.new(:A, 4),
RLTK::Token.new(:A, 5),
RLTK::Token.new(:EOS)
]
assert_equal(expected, lexer.lex('aaa'))
end
def test_first_match
expected = [
RLTK::Token.new(:APLUS),
RLTK::Token.new(:BPLUS),
RLTK::Token.new(:EOS)
]
actual = ABFirst.lex('aaabbb')
assert_equal(expected, actual)
end
def test_flags
assert_raises(RLTK::LexingError) { FlagLexer.lex('b') }
assert_raises(RLTK::LexingError) { FlagLexer.lex('ac') }
expected = [
RLTK::Token.new(:A),
RLTK::Token.new(:B),
RLTK::Token.new(:C),
RLTK::Token.new(:EOS)
]
actual = FlagLexer.lex('abc')
assert_equal(expected, actual)
expected = [
RLTK::Token.new(:A),
RLTK::Token.new(:B),
RLTK::Token.new(:C),
RLTK::Token.new(:A),
RLTK::Token.new(:B),
RLTK::Token.new(:C),
RLTK::Token.new(:EOS)
]
actual = FlagLexer.lex('abcabc')
assert_equal(expected, actual)
end
def test_lex
assert_raises(RLTK::LexingError) { ABFirst.lex('aaabbbCCC') }
assert_raises(RLTK::LexingError) { ABLongest.lex('aaabbbCCC') }
end
def test_longest_match
expected = [
RLTK::Token.new(:APLUSBPLUS),
RLTK::Token.new(:EOS)
]
actual = ABLongest.lex('aaabbb')
assert_equal(expected, actual)
end
def test_match_data
expected = [RLTK::Token.new(:FOO, ['', 'ccc']), RLTK::Token.new(:EOS)]
actual = MatchDataLexer.lex('accc')
assert_equal(expected, actual)
end
def test_state
expected = [
RLTK::Token.new(:A),
RLTK::Token.new(:A),
RLTK::Token.new(:EOS)
]
actual = StateLexer.lex('a (* bbb *) a')
assert_equal(expected, actual)
actual = StateLexer.lex('a (* b (* ccc *) b *) a')
assert_equal(expected, actual)
end
end