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
|
package.path = package.path .. ';3rd/love-api/?.lua'
local lua51 = require 'lua51'
local api = lua51.require 'love_api'
local fs = require 'bee.filesystem'
local fsu = require 'fs-utility'
local metaPath = fs.path 'meta/3rd/love2d'
local libraryPath = metaPath / 'library'
fs.create_directories(libraryPath)
local knownTypes = {
['nil'] = 'nil',
['any'] = 'any',
['boolean'] = 'boolean',
['number'] = 'number',
['integer'] = 'integer',
['string'] = 'string',
['table'] = 'table',
['function'] = 'function',
['userdata'] = 'userdata',
['lightuserdata'] = 'lightuserdata',
['thread'] = 'thread',
['cdata'] = 'ffi.cdata*',
['light userdata'] = 'lightuserdata',
['Variant'] = 'any',
}
local function trim(name)
name = name:gsub('^%s+', '')
name = name:gsub('%s+$', '')
return name
end
---@param names string
local function getTypeName(names)
local types = {}
names = names:gsub('%sor%s', '|')
for name in names:gmatch '[^|]+' do
name = trim(name)
types[#types+1] = knownTypes[name] or ('love.' .. name)
end
return table.concat(types, '|')
end
local function formatIndex(key)
if key:match '^[%a_][%w_]*$' then
return key
end
return ('[%q]'):format(key)
end
local function getOptional(param)
if param.type == 'table' then
if not param.table then
return ''
end
for _, field in ipairs(param.table) do
if field.default == nil then
return ''
end
end
return '?'
else
return (param.default ~= nil) and '?' or ''
end
end
local buildType
local function buildDocTable(tbl)
local fields = {}
for _, field in ipairs(tbl) do
if field.name ~= '...' then
fields[#fields+1] = ('%s: %s'):format(formatIndex(field.name), buildType(field))
end
end
return ('{%s}'):format(table.concat(fields, ', '))
end
function buildType(param)
if param.table then
return buildDocTable(param.table)
end
return getTypeName(param.type)
end
local function buildSuper(tp)
if not tp.supertypes then
return ''
end
local parents = {}
for _, parent in ipairs(tp.supertypes) do
parents[#parents+1] = getTypeName(parent)
end
return (': %s'):format(table.concat(parents, ', '))
end
local function buildMD(desc)
return desc:gsub('([\r\n])', '%1---')
:gsub('%. ', '.\n---\n---')
end
---@param desc any
---@param notes any
---@param wikiPage string?
---@return string
local function buildDescription(desc, notes, wikiPage)
local lines = {}
if desc then
lines[#lines+1] = '---'
lines[#lines+1] = '---' .. buildMD(desc)
lines[#lines+1] = '---'
end
if notes then
lines[#lines+1] = '---'
lines[#lines+1] = '---### NOTE:'
lines[#lines+1] = '---' .. buildMD(notes)
lines[#lines+1] = '---'
end
if wikiPage then
lines[#lines+1] = '---'
lines[#lines+1] = ("---[Open in Browser](https://love2d.org/wiki/%s)"):format(wikiPage)
lines[#lines+1] = '---'
end
return table.concat(lines, '\n')
end
local function buildDocFunc(variant, overload)
local params = {}
local returns = {}
if overload then
params[1] = ('self: %s'):format(overload)
end
for _, param in ipairs(variant.arguments or {}) do
if param.name == '...' then
params[#params+1] = '...'
else
if param.name:find '^[\'"]' then
params[#params+1] = ('%s%s: %s|%s'):format(param.name:sub(2, -2), getOptional(param), getTypeName(param.type), param.name)
else
params[#params+1] = ('%s%s: %s'):format(param.name, getOptional(param), getTypeName(param.type))
end
end
end
for _, rtn in ipairs(variant.returns or {}) do
returns[#returns+1] = ('%s'):format(getTypeName(rtn.type))
end
return ('fun(%s)%s'):format(
table.concat(params, ', '),
#returns > 0 and (':' .. table.concat(returns, ', ')) or ''
)
end
local function buildMultiDocFunc(tp)
local cbs = {}
for _, variant in ipairs(tp.variants) do
cbs[#cbs+1] = buildDocFunc(variant)
end
return table.concat(cbs, '|')
end
local function buildFunction(func, node, typeName)
local text = {}
text[#text+1] = buildDescription(func.description, func.notes, node..func.name)
for i = 2, #func.variants do
local variant = func.variants[i]
text[#text+1] = ('---@overload %s'):format(buildDocFunc(variant, typeName))
end
local params = {}
for _, param in ipairs(func.variants[1].arguments or {}) do
for paramName in param.name:gmatch '[%a_][%w_]*' do
params[#params+1] = paramName
text[#text+1] = ('---@param %s%s %s # %s'):format(
paramName,
getOptional(param),
buildType(param),
param.description
)
end
if param.name == "..." then
params[#params+1] = param.name
text[#text+1] = ('---@vararg %s # %s'):format(
buildType(param),
param.description
)
end
end
for _, rtn in ipairs(func.variants[1].returns or {}) do
for returnName in rtn.name:gmatch '[%a_][%w_]*' do
text[#text+1] = ('---@return %s %s # %s'):format(
buildType(rtn),
returnName,
rtn.description
)
end
end
text[#text+1] = ('function %s%s(%s) end'):format(
node,
func.name,
table.concat(params, ', ')
)
return table.concat(text, '\n')
end
local function buildFile(class, defs)
local filePath = libraryPath / (class:gsub('%.', '/') .. '.lua')
local text = {}
text[#text+1] = '---@meta'
text[#text+1] = ''
if defs.version then
text[#text+1] = ('-- version: %s'):format(defs.version)
end
text[#text+1] = buildDescription(defs.description, defs.notes, class)
text[#text+1] = ('---@class %s'):format(class)
text[#text+1] = ('%s = {}'):format(class)
for _, func in ipairs(defs.functions or {}) do
text[#text+1] = ''
text[#text+1] = buildFunction(func, class .. '.')
end
for _, tp in ipairs(defs.types or {}) do
local mark = {}
text[#text+1] = ''
text[#text+1] = buildDescription(tp.description, tp.notes, class)
text[#text+1] = ('---@class %s%s'):format(getTypeName(tp.name), buildSuper(tp))
text[#text+1] = ('local %s = {}'):format(tp.name)
for _, func in ipairs(tp.functions or {}) do
if not mark[func.name] then
mark[func.name] = true
text[#text+1] = ''
text[#text+1] = buildFunction(func, tp.name .. ':', getTypeName(tp.name))
end
end
end
for _, cb in ipairs(defs.callbacks or {}) do
text[#text+1] = ''
text[#text+1] = buildDescription(cb.description, cb.notes)
text[#text+1] = ('---@alias %s %s'):format(getTypeName(cb.name), buildMultiDocFunc(cb))
end
for _, enum in ipairs(defs.enums or {}) do
text[#text+1] = ''
text[#text+1] = buildDescription(enum.description, enum.notes, enum.name)
text[#text+1] = ('---@alias %s'):format(getTypeName(enum.name))
for _, constant in ipairs(enum.constants) do
text[#text+1] = buildDescription(constant.description, constant.notes)
text[#text+1] = ([[---| %q]]):format(constant.name)
end
end
if defs.version then
text[#text+1] = ''
text[#text+1] = ('return %s'):format(class)
end
text[#text+1] = ''
fs.create_directories(filePath:parent_path())
fsu.saveFile(filePath, table.concat(text, '\n'))
end
buildFile('love', api)
for _, module in ipairs(api.modules) do
buildFile('love.' .. module.name, module)
end
|