-
Notifications
You must be signed in to change notification settings - Fork 4
/
test_scanner.rb
234 lines (218 loc) · 7.98 KB
/
test_scanner.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
require 'helper'
class ScannerTest < ZOMG::Test
def setup
@scanner = ZOMG::IDL::Scanner.new
end
def test_load_file
@scanner.load_file(simple('simple1.idl'))
tokens = []
while token = @scanner.next_token
tokens << token
end
assert tokens.length > 0
end
def test_typedef_object
assert_tokens([
[:T_TYPEDEF, "typedef"],
[:T_OBJECT, "Object"],
[:T_IDENTIFIER, "ObjectAlias"],
], 'typedef Object ObjectAlias;')
end
def test_const_long
assert_tokens([
[:T_CONST, "const"],
[:T_LONG, "long"],
[:T_IDENTIFIER, "ConstLong"],
[:T_EQUAL, "="],
[:T_MINUS_SIGN, "-"],
], 'const long ConstLong = -1234;')
end
def test_escaped_string
assert_tokens([
[:T_CONST, "const"],
[:T_CHAR, "char"],
[:T_IDENTIFIER, "ConstChar5"],
[:T_EQUAL, "="],
[:T_CHARACTER_LITERAL, "'\\''"],
], 'const char ConstChar5 = \'\\\'\'')
end
def test_vertical_pipe
assert_tokens([
[:T_CONST, "const"],
[:T_LONG, "long"],
[:T_IDENTIFIER, "ConstLong"],
[:T_EQUAL, "="],
[:T_LEFT_PARANTHESIS, "("],
[:T_INTEGER_LITERAL, "0xf"],
[:T_VERTICAL_LINE, "|"],
], "const long ConstLong = (0xf | 0xf000) & 0xfffe")
end
def test_instr
assert_tokens([
[:T_IN, "in"],
[:T_STRING, "string"],
[:T_IDENTIFIER, "inStr"],
], "in string inStr")
end
def test_comment
assert_tokens([[:T_COMMENT, "// boner"]], "// boner")
assert_tokens([[:T_COMMENT, "/* boner */"]], "/* boner */")
end
def test_pragma
assert_tokens([[:T_PRAGMA, "#pragma awesome\n"]], "#pragma awesome\n")
end
def test_preprocessor_directive
assert_tokens([[:T_PREPROCESSOR, "#awesome\n"]], "#awesome\n")
end
def test_curlies
assert_tokens([[:T_LEFT_CURLY_BRACKET, "{"]], "{")
assert_tokens([[:T_RIGHT_CURLY_BRACKET, "}"]], "}")
assert_tokens([
[:T_LEFT_CURLY_BRACKET, "{"],
[:T_RIGHT_CURLY_BRACKET, "}"]
], "{ }\n")
end
def test_squares
assert_tokens([[:T_LEFT_SQUARE_BRACKET, "["]], "[")
assert_tokens([[:T_RIGHT_SQUARE_BRACKET, "]"]], "]")
assert_tokens([
[:T_LEFT_SQUARE_BRACKET, "["],
[:T_RIGHT_SQUARE_BRACKET, "]"]
], " [ ] \n")
end
def test_parens
assert_tokens([[:T_LEFT_PARANTHESIS, "("]], "(")
assert_tokens([[:T_RIGHT_PARANTHESIS, ")"]], ")")
assert_tokens([
[:T_LEFT_PARANTHESIS, "("],
[:T_RIGHT_PARANTHESIS, ")"]
], " ( ) \n")
end
# Test literals
{
:const => [:T_CONST, 'const'],
:typedef => [:T_TYPEDEF, 'typedef'],
:float => [:T_FLOAT, 'float'],
:double => [:T_DOUBLE, 'double'],
:char => [:T_CHAR, 'char'],
:wchar => [:T_WCHAR, 'wchar'],
:fixed => [:T_FIXED, 'fixed'],
:boolean => [:T_BOOLEAN, 'boolean'],
:string => [:T_STRING, 'string'],
:wstring => [:T_WSTRING, 'wstring'],
:void => [:T_VOID, 'void'],
:unsigned => [:T_UNSIGNED, 'unsigned'],
:long => [:T_LONG, 'long'],
:short => [:T_SHORT, 'short'],
:struct => [:T_STRUCT, 'struct'],
:union => [:T_UNION, 'union'],
:switch => [:T_SWITCH, 'switch'],
:case => [:T_CASE, 'case'],
:default => [:T_DEFAULT, 'default'],
:enum => [:T_ENUM, 'enum'],
:in => [:T_IN, 'in'],
:out => [:T_OUT, 'out'],
:interface => [:T_INTERFACE, 'interface'],
:abstract => [:T_ABSTRACT, 'abstract'],
:valuetype => [:T_VALUETYPE, 'valuetype'],
:truncatable => [:T_TRUNCATABLE, 'truncatable'],
:supports => [:T_SUPPORTS, 'supports'],
:custom => [:T_CUSTOM, 'custom'],
:public => [:T_PUBLIC, 'public'],
:private => [:T_PRIVATE, 'private'],
:factory => [:T_FACTORY, 'factory'],
:native => [:T_NATIVE, 'native'],
:module => [:T_MODULE, 'module'],
:octet => [:T_OCTET, 'octet'],
:any => [:T_ANY, 'any'],
:sequence => [:T_SEQUENCE, 'sequence'],
:readonly => [:T_READONLY, 'readonly'],
:attribute => [:T_ATTRIBUTE, 'attribute'],
:exception => [:T_EXCEPTION, 'exception'],
:oneway => [:T_ONEWAY, 'oneway'],
:inout => [:T_INOUT, 'inout'],
:raises => [:T_RAISES, 'raises'],
:context => [:T_CONTEXT, 'context'],
}.each do |type, token|
define_method(:"test_#{type}") do
parse_text = token.length == 3 ? token.pop : token.last
assert_tokens([token], parse_text)
assert_tokens([
[:T_LEFT_SQUARE_BRACKET, "["],
token,
[:T_RIGHT_SQUARE_BRACKET, "]"]
], " [\n#{parse_text}] \n")
assert_tokens([
[:T_IN, "in"],
[:T_STRING, "string"],
[:T_IDENTIFIER, "#{parse_text}Str"],
], "in string #{parse_text}Str")
end
end
{
:minus => [:T_MINUS_SIGN, '-'],
:plus => [:T_PLUS_SIGN, '+'],
:shift_left => [:T_SHIFTLEFT, '<<'],
:shift_right => [:T_SHIFTRIGHT, '>>'],
:equal => [:T_EQUAL, '='],
:semicolon => [:T_SEMICOLON, ';'],
:comma => [:T_COMMA, ','],
:colon => [:T_COLON, ':'],
:asterisk => [:T_ASTERISK, '*'],
:solidus => [:T_SOLIDUS, '/'],
:percent => [:T_PERCENT_SIGN, '%'],
:tilde => [:T_TILDE, '~'],
:pipe => [:T_VERTICAL_LINE, '|'],
:caret => [:T_CIRCUMFLEX, '^'],
:ampersand => [:T_AMPERSAND, '&'],
:less_than => [:T_LESS_THAN_SIGN, '<'],
:greater_than => [:T_GREATER_THAN_SIGN, '>'],
:FALSE => [:T_FALSE, 'FALSE'],
:TRUE => [:T_TRUE, 'TRUE'],
:ValueBase => [:T_VALUEBASE, 'ValueBase'],
:scope => [:T_SCOPE, '::'],
:object => [:T_OBJECT, 'Object'],
:principal => [:T_PRINCIPAL, 'Principal'],
:identifier => [:T_IDENTIFIER, 'hello'],
:float_lit_1 => [:T_FLOATING_PT_LITERAL, '123.2e10'],
:float_lit_11 => [:T_FLOATING_PT_LITERAL, '123.2E10'],
:float_lit_2 => [:T_FLOATING_PT_LITERAL, '123.0E-5', '123.E-5'],
:float_lit_3 => [:T_FLOATING_PT_LITERAL, '123e10'],
:float_lit_31 => [:T_FLOATING_PT_LITERAL, '123E+10'],
:float_lit_4 => [:T_FLOATING_PT_LITERAL, '12.2'],
:float_lit_5 => [:T_FLOATING_PT_LITERAL, '12.0', '12.'],
:float_lit_6 => [:T_FLOATING_PT_LITERAL, '0.12e10', '.12e10'],
:float_lit_61 => [:T_FLOATING_PT_LITERAL, '0.12e+10', '.12e+10'],
:float_lit_7 => [:T_FLOATING_PT_LITERAL, '0.12', '.12'],
:fixed_lit_1 => [:T_FIXED_PT_LITERAL, '123', '123d'],
:fixed_lit_2 => [:T_FIXED_PT_LITERAL, '123', '123.d'],
:fixed_lit_21 => [:T_FIXED_PT_LITERAL, '123', '123.D'],
:fixed_lit_3 => [:T_FIXED_PT_LITERAL, '0.123', '.123d'],
:fixed_lit_31 => [:T_FIXED_PT_LITERAL, '0.123', '.123D'],
:fixed_lit_4 => [:T_FIXED_PT_LITERAL, '0.123', '0.123d'],
:fixed_lit_41 => [:T_FIXED_PT_LITERAL, '0.123', '0.123D'],
:hex_lit => [:T_INTEGER_LITERAL, '0x23'],
:hex_lit_1 => [:T_INTEGER_LITERAL, '0X23'],
:oct_lit => [:T_INTEGER_LITERAL, '023'],
:int_lit => [:T_INTEGER_LITERAL, '23'],
:char_lit => [:T_CHARACTER_LITERAL, "'a'"],
:string_lit => [:T_STRING_LITERAL, '"abc"'],
:unknown => [:T_UNKNOWN, '.'],
}.each do |type, token|
define_method(:"test_#{type}") do
parse_text = token.length == 3 ? token.pop : token.last
assert_tokens([token], parse_text)
assert_tokens([
[:T_LEFT_SQUARE_BRACKET, "["],
token,
[:T_RIGHT_SQUARE_BRACKET, "]"]
], " [\n#{parse_text}] \n")
end
end
def assert_tokens(tokens, string)
@scanner.scan_evaluate(string)
tokens.each do |token|
assert_equal(token, @scanner.next_token)
end
end
end