-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
luacheck_config_helper.lua
executable file
·145 lines (125 loc) · 4.09 KB
/
luacheck_config_helper.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
#!/usr/bin/lua
---
-- Script to extract names and the functions themselves from NodeMCU modules and
-- to help creating luacheck configuration.
-- Usage: <in modules catalog> ../../tools/luacheck_config_helper.lua *.c (or filename for single module)
local M = {}
-- Recursive object dumper, for debugging.
-- (c) 2010 David Manura, MIT License.
-- From: https://github.com/davidm/lua-inspect/blob/master/lib/luainspect/dump.lua
local ignore_keys_ = {lineinfo = true}
local norecurse_keys_ = {parent = true, ast = true}
local function dumpstring_key_(k, isseen, newindent)
local ks = type(k) == 'string' and k:match'^[%a_][%w_]*$' and k or
'[' .. M.dumpstring(k, isseen, newindent) .. ']'
return ks
end
local function sort_keys_(a, b)
if type(a) == 'number' and type(b) == 'number' then
return a < b
elseif type(a) == 'number' then
return false
elseif type(b) == 'number' then
return true
elseif type(a) == 'string' and type(b) == 'string' then
return a < b
else
return tostring(a) < tostring(b) -- arbitrary
end
end
function M.dumpstring(o, isseen, indent, key)
isseen = isseen or {}
indent = indent or ''
if type(o) == 'table' then
if isseen[o] or norecurse_keys_[key] then
return (type(o.tag) == 'string' and '`' .. o.tag .. ':' or '') .. tostring(o)
else isseen[o] = true end -- avoid recursion
local used = {}
local tag = o.tag
local s = '{'
if type(o.tag) == 'string' then
s = '`' .. tag .. s; used['tag'] = true
end
local newindent = indent .. ' '
local ks = {}; for k in pairs(o) do ks[#ks+1] = k end
table.sort(ks, sort_keys_)
local forcenummultiline
for k in pairs(o) do
if type(k) == 'number' and type(o[k]) == 'table' then forcenummultiline = true end
end
-- inline elements
for _,k in ipairs(ks) do
if ignore_keys_[k] then used[k] = true
elseif (type(k) ~= 'number' or not forcenummultiline) and
type(k) ~= 'table' and (type(o[k]) ~= 'table' or norecurse_keys_[k])
then
s = s .. dumpstring_key_(k, isseen, newindent) .. ' = ' .. M.dumpstring(o[k], isseen, newindent, k) .. ', '
used[k] = true
end
end
-- elements on separate lines
local done
for _,k in ipairs(ks) do
if not used[k] then
if not done then s = s .. '\n'; done = true end
s = s .. newindent .. dumpstring_key_(k, isseen) .. ' = ' .. M.dumpstring(o[k], isseen, newindent, k) .. ',\n'
end
end
s = s:gsub(',(%s*)$', '%1')
s = s .. (done and indent or '') .. '}'
return s
elseif type(o) == 'string' then
return string.format('%q', o)
else
return tostring(o)
end
end
-- End of dump.lua code
local function printTables(fileName)
local findBegin, field
if type(fileName) ~= "string" then error("Wrong argument") end
local file = io.open(fileName, "r")
if not file then error("Can't open file") end
local result = {}
result.fields = {}
for line in file:lines() do
findBegin, _, field = string.find(line, "LROT_BEGIN%((%g+)%)")
if findBegin then
io.write(field.." = ")
end
findBegin, _, field = string.find(line, "LROT_PUBLIC_BEGIN%((%g+)%)")
if findBegin then
print(field.." = ")
end
findBegin, _, field = string.find(line, "LROT_FUNCENTRY%(%s?(%g+),")
if not findBegin then
findBegin, _, field = string.find(line, "LROT_NUMENTRY%(%s?(%g+),")
end
if not findBegin then
findBegin, _, field = string.find(line, "LROT_TABENTRY%(%s?(%g+),")
end
if not findBegin then
findBegin, _, field = string.find(line, "LROT_FUNCENTRY_S%(%s?(%g+),")
end
if not findBegin then
findBegin, _, field = string.find(line, "LROT_FUNCENTRY_F%(%s?(%g+),")
end
if findBegin then
if not string.find(field, "__") then
result.fields[field] = {}
end
end
findBegin = string.find(line, "LROT_END")
if findBegin then
print(string.gsub(M.dumpstring(result), "{}", "empty")..',')
result = {}
result.fields = {}
end
end
end
local function main()
for i = 1, #arg do
printTables(arg[i])
end
end
main()