-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.lua
158 lines (130 loc) · 3.3 KB
/
util.lua
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
local lfs = require'lfs'
local re = require 'relabel'
local ast = require'pegparser.ast'
local verbose = false
local function setVerbose (v)
verbose = v
end
local function getFileName (s)
local i = 1
local j = string.find(s, '/', i)
while j ~= nil do
i = j + 1
j = string.find(s, '/', i)
end
return string.sub(s, i)
end
local function getPath (s)
local i = 1
local j = string.find(s, '/', i)
while j ~= nil do
i = j + 1
j = string.find(s, '/', i)
end
local path = string.sub(s, 1, i - 1)
if #path == 0 then
return "."
else
return path
end
end
local function removeSpace (s)
return s:gsub("[ \t\n\r]", "")
end
local function isSource (file, ext)
return string.sub(file, 1, 1) ~= '.' and string.sub(file, #file - #ext + 1) == ext
end
local function getFiles (dir, ext)
local t = {}
for file in lfs.dir(dir) do
if isSource(file, ext) then
table.insert(t, file)
end
end
table.sort(t)
return t
end
local function getText (file)
local f = io.open(file)
local s = f:read('a')
f:close()
return s
end
local function testFile (file, p)
local s = getText(file)
return p:match(s)
end
local function matchlabel (s1, s2)
return string.match(string.lower(tostring(s1)), string.lower(tostring(s2)))
end
local function testYes (dir, ext, p)
for i, file in ipairs(getFiles(dir, ext)) do
print("Yes: ", file)
local r, lab, pos = testFile(dir .. file, p)
local line, col = '', ''
if not r then
line, col = re.calcline(getText(dir .. file), pos)
end
assert(r ~= nil, file .. ': Label: ' .. tostring(lab) .. ' Line: ' .. line .. ' Col: ' .. col)
end
end
local function testNo (dir, ext, p, strict, special)
for i, file in ipairs(getFiles(dir, ext)) do
print("No: ", file)
local r, lab, pos = testFile(dir .. file, p)
if lab ~= nil then
io.write('r = ' .. tostring(r) .. ' lab = ' .. tostring(lab))
end
local line, col = '', ''
if not r then
line, col = re.calcline(getText(dir .. file), pos)
io.write(' line: ' .. line .. ' col: ' .. col)
end
if strict then
assert(r == nil and (matchlabel(file, lab) or matchlabel(file, special)), file .. ': Label: ' .. tostring(lab) .. ' Line: ' .. line .. ' Col: ' .. col)
else
assert(r == nil, file .. ': Label: ' .. tostring(lab) .. ' Line: ' .. line .. ' Col: ' .. col)
end
io.write('\n')
end
end
local function testNoRec (dir, ext, p)
local irec, ifail = 0, 0
local tfail = {}
for i, file in ipairs(getFiles(dir, ext)) do
print("No: ", file)
local r, lab, pos = testFile(dir .. file, p)
if not r then
local line, col = re.calcline(getText(dir .. file), pos)
io.write('r = ' .. tostring(r) .. ' lab = ' .. tostring(lab))
io.write(' line: ' .. line .. ' col: ' .. col)
io.write('\n')
ifail = ifail + 1
tfail[ifail] = { file = file, lab = lab, line = line, col = col }
else
if verbose then
io.write(getText(dir .. file))
end
irec = irec + 1
if type(r) == 'table' then
ast.printAST(r)
io.write('\n')
io.write('\n')
end
end
end
print('irec: ', irec, ' ifail: ', ifail)
for i, v in ipairs(tfail) do
print(v.file, v.lab, 'line: ', v.line, 'col: ', v.col)
end
end
return {
testYes = testYes,
testNo = testNo,
testNoRec = testNoRec,
testFile = testFile,
setVerbose = setVerbose,
getFileName = getFileName,
getPath = getPath,
removeSpace = removeSpace
}