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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
|
--[[--------------------------------------------------------------------
test_llex.lua
Test for llex.lua
This file is part of Yueliang.
Copyright (c) 2006 Kein-Hong Man <khman@users.sf.net>
The COPYRIGHT file describes the conditions
under which this software may be distributed.
See the ChangeLog for more information.
----------------------------------------------------------------------]]
------------------------------------------------------------------------
-- if BRIEF is not set to false, auto-test will silently succeed
------------------------------------------------------------------------
BRIEF = true -- if set to true, messages are less verbose
local zio_init = require("../lzio_mk4")
local lex_init = require("../llex_mk4")
------------------------------------------------------------------------
-- simple manual tests
------------------------------------------------------------------------
--[[
local function dump(z, source)
local luaX = lex_init(z, source)
while true do
local tok, seminfo = luaX:lex()
if tok == "<name>" then
seminfo = " "..seminfo
elseif tok == "<number>" then
seminfo = " "..seminfo
elseif tok == "<string>" then
seminfo = " '"..seminfo.."'"
else
seminfo = ""
end
io.stdout:write(tok..seminfo.."\n")
if tok == "<eof>" then break end
end
end
local function try_string(chunk)
dump(zio_init(chunk), "=string")
end
local function try_file(filename)
local f = "@"..filename
dump(zio_init(f), f)
end
z = try_string("local c = z:getc()")
z = try_file("test_lzio_mk2.lua")
z = try_file("test_llex_mk2.lua")
os.exit()
--]]
------------------------------------------------------------------------
-- auto-testing of simple test cases to validate lexer behaviour:
-- * NOTE coverage has not been checked; not comprehensive
-- * only test cases with non-empty comments are processed
-- * if no result, then the output is displayed for manual decision
-- (output may be used to set expected success or fail text)
-- * cases expected to be successful may be a partial match
-- * cases expected to fail may also be a partial match
------------------------------------------------------------------------
-- [[
local function auto_test()
local PASS, FAIL = true, false
------------------------------------------------------------------
-- table of test cases
------------------------------------------------------------------
local test_cases =
{
-------------------------------------------------------------
--{ "comment", -- comment about the test
-- "chunk", -- chunk to test
-- PASS, -- PASS or FAIL outcome
-- "output", -- output to compare against
--},
-------------------------------------------------------------
{ "empty chunk string, test EOS",
"",
PASS, "1 <eof>",
},
-------------------------------------------------------------
{ "line number counting",
"\n\n\r\n",
PASS, "4 <eof>",
},
-------------------------------------------------------------
{ "various whitespaces",
" \n\t\t\n \t \t \n\n",
PASS, "5 <eof>",
},
-------------------------------------------------------------
{ "short comment ending in EOS",
"-- moo moo",
PASS, "1 <eof>",
},
-------------------------------------------------------------
{ "short comment ending in newline",
"-- moo moo\n",
PASS, "2 <eof>",
},
-------------------------------------------------------------
{ "several lines of short comments",
"--moo\n-- moo moo\n\n--\tmoo\n",
PASS, "5 <eof>",
},
-------------------------------------------------------------
{ "basic block comment",
"--[[bovine]]",
PASS, "1 <eof>",
},
-------------------------------------------------------------
{ "unterminated block comment 1",
"--[[bovine",
FAIL, ":1: unfinished long comment",
},
-------------------------------------------------------------
{ "unterminated block comment 2",
"--[[bovine]",
FAIL, ":1: unfinished long comment",
},
-------------------------------------------------------------
{ "unterminated block comment 3",
"--[[bovine\nmoo moo\nwoof",
FAIL, ":3: unfinished long comment",
},
-------------------------------------------------------------
{ "basic long string",
"\n[[bovine]]\n",
PASS, "2 <string> = bovine\n3 <eof>",
},
-------------------------------------------------------------
{ "first newline consumed in long string",
"[[\nmoo]]",
PASS, "2 <string> = moo\n2 <eof>",
},
-------------------------------------------------------------
{ "multiline long string",
"[[moo\nmoo moo\n]]",
PASS, "3 <string> = moo\nmoo moo\n\n3 <eof>",
},
-------------------------------------------------------------
{ "unterminated long string 1",
"\n[[\nbovine",
FAIL, ":3: unfinished long string",
},
-------------------------------------------------------------
{ "unterminated long string 2",
"[[bovine]",
FAIL, ":1: unfinished long string",
},
-------------------------------------------------------------
{ "unterminated long string 3",
"[[[[ \n",
FAIL, ":2: unfinished long string",
},
-------------------------------------------------------------
{ "nested long string 1",
"[[moo[[moo]]moo]]",
PASS, "moo[[moo]]moo",
},
-------------------------------------------------------------
{ "nested long string 2",
"[[moo[[moo[[[[]]]]moo]]moo]]",
PASS, "moo[[moo[[[[]]]]moo]]moo",
},
-------------------------------------------------------------
{ "nested long string 3",
"[[[[[[]]]][[[[]]]]]]",
PASS, "[[[[]]]][[[[]]]]",
},
-------------------------------------------------------------
{ "brackets in long strings 1",
"[[moo[moo]]",
PASS, "moo[moo",
},
-------------------------------------------------------------
{ "brackets in long strings 2",
"[[moo[[moo]moo]]moo]]",
PASS, "moo[[moo]moo]]moo",
},
-------------------------------------------------------------
{ "unprocessed escapes in long strings",
[[ [[\a\b\f\n\r\t\v\123]] ]],
PASS, [[\a\b\f\n\r\t\v\123]],
},
-------------------------------------------------------------
{ "unbalanced long string",
"[[moo]]moo]]",
PASS, "1 <string> = moo\n1 <name> = moo\n1 CHAR = ']'\n1 CHAR = ']'\n1 <eof>",
},
-------------------------------------------------------------
{ "keywords 1",
"and break do else",
PASS, "1 and\n1 break\n1 do\n1 else\n1 <eof>",
},
-------------------------------------------------------------
{ "keywords 2",
"elseif end false for",
PASS, "1 elseif\n1 end\n1 false\n1 for\n1 <eof>",
},
-------------------------------------------------------------
{ "keywords 3",
"function if in local nil",
PASS, "1 function\n1 if\n1 in\n1 local\n1 nil\n1 <eof>",
},
-------------------------------------------------------------
{ "keywords 4",
"not or repeat return",
PASS, "1 not\n1 or\n1 repeat\n1 return\n1 <eof>",
},
-------------------------------------------------------------
{ "keywords 5",
"then true until while",
PASS, "1 then\n1 true\n1 until\n1 while\n1 <eof>",
},
-------------------------------------------------------------
{ "concat and dots",
".. ...",
PASS, "1 ..\n1 ...\n1 <eof>",
},
-------------------------------------------------------------
{ "shbang handling 1",
"#blahblah",
PASS, "1 <eof>",
},
-------------------------------------------------------------
{ "shbang handling 2",
"#blahblah\nmoo moo\n",
PASS, "2 <name> = moo\n2 <name> = moo\n3 <eof>",
},
-------------------------------------------------------------
{ "empty string",
[['']],
PASS, "1 <string> = \n1 <eof>",
},
-------------------------------------------------------------
{ "single-quoted string",
[['bovine']],
PASS, "1 <string> = bovine\n1 <eof>",
},
-------------------------------------------------------------
{ "double-quoted string",
[["bovine"]],
PASS, "1 <string> = bovine\n1 <eof>",
},
-------------------------------------------------------------
{ "unterminated string 1",
[['moo ]],
FAIL, ":1: unfinished string",
},
-------------------------------------------------------------
{ "unterminated string 2",
[["moo \n]],
FAIL, ":1: unfinished string",
},
-------------------------------------------------------------
{ "escaped newline in string, line number counted",
"\"moo\\\nmoo\\\nmoo\"",
PASS, "3 <string> = moo\nmoo\nmoo\n3 <eof>",
},
-------------------------------------------------------------
{ "escaped characters in string 1",
[["moo\amoo"]],
PASS, "1 <string> = moo\amoo",
},
-------------------------------------------------------------
{ "escaped characters in string 2",
[["moo\bmoo"]],
PASS, "1 <string> = moo\bmoo",
},
-------------------------------------------------------------
{ "escaped characters in string 3",
[["moo\f\n\r\t\vmoo"]],
PASS, "1 <string> = moo\f\n\r\t\vmoo",
},
-------------------------------------------------------------
{ "escaped characters in string 4",
[["\\ \" \' \? \[ \]"]],
PASS, "1 <string> = \\ \" \' \? \[ \]",
},
-------------------------------------------------------------
{ "escaped characters in string 5",
[["\z \k \: \;"]],
PASS, "1 <string> = z k : ;",
},
-------------------------------------------------------------
{ "escaped characters in string 6",
[["\8 \65 \160 \180K \097097"]],
PASS, "1 <string> = \8 \65 \160 \180K \097097\n",
},
-------------------------------------------------------------
{ "escaped characters in string 7",
[["\666"]],
FAIL, ":1: escape sequence too large",
},
-------------------------------------------------------------
{ "simple numbers",
"123 123+",
PASS, "1 <number> = 123\n1 <number> = 123\n1 CHAR = '+'\n1 <eof>",
},
-------------------------------------------------------------
{ "longer numbers",
"1234567890 12345678901234567890",
PASS, "1 <number> = 1234567890\n1 <number> = 1.2345678901235e+19\n",
},
-------------------------------------------------------------
{ "fractional numbers",
".123 .12345678901234567890",
PASS, "1 <number> = 0.123\n1 <number> = 0.12345678901235\n",
},
-------------------------------------------------------------
{ "more numbers with decimal points",
"12345.67890 1.1.",
PASS, "1 <number> = 12345.6789\n1 <number> = 1.1\n1 CHAR = '.'\n",
},
-------------------------------------------------------------
{ "double decimal points",
".1.1",
FAIL, ":1: malformed number",
},
-------------------------------------------------------------
{ "double dots within numbers",
"1..1",
FAIL, ":1: ambiguous syntax (dots follows digits)",
},
-------------------------------------------------------------
{ "incomplete exponential numbers",
"123e",
FAIL, ":1: malformed number",
},
-------------------------------------------------------------
{ "exponential numbers 1",
"1234e5 1234e5.",
PASS, "1 <number> = 123400000\n1 <number> = 123400000\n1 CHAR = '.'",
},
-------------------------------------------------------------
{ "exponential numbers 2",
"1234e56 1.23e123",
PASS, "1 <number> = 1.234e+59\n1 <number> = 1.23e+123\n",
},
-------------------------------------------------------------
{ "exponential numbers 3",
"12.34e+",
FAIL, ":1: malformed number",
},
-------------------------------------------------------------
{ "exponential numbers 4",
"12.34e+5 123.4e-5 1234.E+5",
PASS, "1 <number> = 1234000\n1 <number> = 0.001234\n1 <number> = 123400000\n",
},
-------------------------------------------------------------
{ "single character symbols 1",
"= > < ~",
PASS, "1 CHAR = '='\n1 CHAR = '>'\n1 CHAR = '<'\n1 CHAR = '~'\n",
},
-------------------------------------------------------------
{ "double character symbols",
"== >= <= ~=",
PASS, "1 ==\n1 >=\n1 <=\n1 ~=\n",
},
-------------------------------------------------------------
{ "simple identifiers",
"abc ABC",
PASS, "1 <name> = abc\n1 <name> = ABC\n1 <eof>",
},
-------------------------------------------------------------
{ "more identifiers",
"_abc _ABC",
PASS, "1 <name> = _abc\n1 <name> = _ABC\n1 <eof>",
},
-------------------------------------------------------------
{ "still more identifiers",
"_aB_ _123",
PASS, "1 <name> = _aB_\n1 <name> = _123\n1 <eof>",
},
-------------------------------------------------------------
{ "invalid control character",
"\4",
FAIL, ":1: invalid control char",
},
-------------------------------------------------------------
{ "single character symbols 2",
"` ! @ $ %",
PASS, "1 CHAR = '`'\n1 CHAR = '!'\n1 CHAR = '@'\n1 CHAR = '$'\n1 CHAR = '%'\n",
},
-------------------------------------------------------------
{ "single character symbols 3",
"^ & * ( )",
PASS, "1 CHAR = '^'\n1 CHAR = '&'\n1 CHAR = '*'\n1 CHAR = '('\n1 CHAR = ')'\n",
},
-------------------------------------------------------------
{ "single character symbols 4",
"_ - + \\ |",
PASS, "1 <name> = _\n1 CHAR = '-'\n1 CHAR = '+'\n1 CHAR = '\\'\n1 CHAR = '|'\n",
},
-------------------------------------------------------------
{ "single character symbols 5",
"{ } [ ] :",
PASS, "1 CHAR = '{'\n1 CHAR = '}'\n1 CHAR = '['\n1 CHAR = ']'\n1 CHAR = ':'\n",
},
-------------------------------------------------------------
{ "single character symbols 6",
"; , . / ?",
PASS, "1 CHAR = ';'\n1 CHAR = ','\n1 CHAR = '.'\n1 CHAR = '/'\n1 CHAR = '?'\n",
},
-------------------------------------------------------------
}
------------------------------------------------------------------
-- perform a test case
------------------------------------------------------------------
function do_test_case(count, test_case)
if comment == "" then return end -- skip empty entries
local comment, chunk, outcome, matcher = unpack(test_case)
local result = PASS
local output = ""
-- initialize lexer
local z = zio_init(chunk)
local luaX = lex_init(z, "=test")
-- lexer test loop
local status, token, seminfo
repeat
-- protected call
status, token, seminfo = pcall(luaX.lex, luaX)
output = output..luaX.ln.." "
if status then
-- successful call
if string.len(token) > 1 then
if token == "<name>"
or token == "<number>"
or token == "<string>" then
token = token.." = "..seminfo
end
elseif string.byte(token) >= 32 then -- displayable chars
token = "CHAR = '"..token.."'"
else -- control characters
token = "CHAR = (".. string.byte(token)..")"
end
output = output..token.."\n"
else
-- failed call
output = output..token -- token is the error message
result = FAIL
break
end
until token == "<eof>"
-- decision making and reporting
local head = "Test "..count..": "..comment
if matcher == "" then
-- nothing to check against, display for manual check
print(head.."\nMANUAL please check manually"..
"\n--chunk---------------------------------\n"..chunk..
"\n--actual--------------------------------\n"..output..
"\n\n")
return
else
if outcome == PASS then
-- success expected, may be a partial match
if string.find(output, matcher, 1, 1) and result == PASS then
if not BRIEF then print(head.."\nOK expected success\n") end
return
end
else
-- failure expected, may be a partial match
if string.find(output, matcher, 1, 1) and result == FAIL then
if not BRIEF then print(head.."\nOK expected failure\n") end
return
end
end
-- failed because of unmatched string or boolean result
local function passfail(status)
if status == PASS then return "PASS" else return "FAIL" end
end
print(head.." *FAILED*"..
"\noutcome="..passfail(outcome)..
"\nactual= "..passfail(result)..
"\n--chunk---------------------------------\n"..chunk..
"\n--expected------------------------------\n"..matcher..
"\n--actual--------------------------------\n"..output..
"\n\n")
end
end
------------------------------------------------------------------
-- perform auto testing
------------------------------------------------------------------
for i,test_case in ipairs(test_cases) do
do_test_case(i, test_case)
end
end
auto_test()
--]]
|