-
Notifications
You must be signed in to change notification settings - Fork 1
/
generator.ts
297 lines (250 loc) · 7.88 KB
/
generator.ts
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
import { log, Severity } from "./logging.ts";
import { Docs, Field, Func, Library, Overrides, SFObject } from "./types.d.ts";
const DOCS_URL =
"https://raw.githubusercontent.com/thegrb93/StarfallEx/gh-pages/sf_doc.json";
const OVERRIDES = <Overrides>(
JSON.parse(await Deno.readTextFile("overrides.json"))
);
class Document {
body = "---@meta\n-- Generated with emmyfall\n";
tab_level = 0;
current_table = "";
emit_raw_text(text: string) {
this.body += text;
}
add_text(text: string) {
this.body += "\t".repeat(this.tab_level) + text + "\n";
}
add_comment(comment: string) {
// The docs currently have manual newlines placed for every
// description. We work around this by making a new emmylua
// comment (twice for a newline in the rendered tooltip) for every new line.
if (comment.includes("\n")) {
const lines = comment.split("\n");
for (const line of lines) {
// EmmyLua comments require an extra empty comment
// to render a newline.
this.add_text(`--- `);
this.add_text(`--- ${line}`);
}
} else {
this.add_text(`--- ${comment}`);
}
}
set_table(parent_name: string, name: string, fields?: Field[]) {
if (parent_name == "builtins") {
this.add_text(`_G.${name} = {`);
} else {
this.add_text(`_G.${parent_name}.${name} = {`);
}
if (!fields) {
this.emit_raw_text("}"); // Nothing to do, empty table.
return;
}
this.tab_level++;
for (const field of fields) {
// Unfortunately, there's no typing for fields.
// But everything I have seen appears to be a number, but-
// I'm not going to assume it, so it'll be untyped.
this.add_text(`---@type any`);
if (field.description && field.description.length > 0) {
this.add_comment(field.description);
}
this.add_text(`["${field.name}"] = nil,`);
}
this.tab_level--;
this.add_text("}");
}
begin_library(library: Library) {
this.add_comment(`${library.name}`);
this.add_comment(`${library.description}`);
const declaration =
library.name == "builtins" ? `_G = {}` : `_G.${library.name} = {}`;
this.add_text(declaration);
for (const idx in library.tables) {
const table = library.tables[idx];
this.add_comment(`${table.name} - ${table.realm}`);
this.add_comment(`${table.description}`);
this.set_table(library.name, table.name, table.fields);
}
if (library.fields) {
for (const idx in library.fields) {
const field = library.fields[idx];
if (field.description && field.description.length > 0) {
this.add_comment(field.description);
}
this.add_text(`---@type any`);
if (library.name == "builtins") {
this.add_text(`_G["${field.name}"] = nil`);
} else {
this.add_text(`_G.${library.name}["${field.name}"] = nil`);
}
}
}
this.tab_level++;
if (library.name != "builtins") {
this.current_table = "." + library.name;
} else {
// Doing this will make the functions assign globally.
this.current_table = "";
}
}
begin_object(object: SFObject) {
this.add_comment(object.description);
this.add_text(`---@class ${object.name}`);
// We also have to use sumneko annotations
// to get operations to work properly.
// This means we must scan the methods
// earlier to find metamethods and properly
// annotate them with ---@operator
// By the way, because this generates procedurally to whatever
// the metamethods look like for a specific object, so it will actually
// generate annotations for unsupported metamethods for Sumneko Lua.
// I'm gonna leave it in there, I mean they could get supported and
// emmyfall could support it off the bat.
// Unsupported metamethods: __eq, __index, __newindex, __tostring
for (const idx in object.methods) {
const metamethod = object.methods[idx];
if (metamethod.name.substring(0, 2) == "__") {
const normalName = metamethod.name.substring(2);
if (
metamethod.params &&
metamethod.returns &&
metamethod.returns.length == 1
) {
if (metamethod.params.length == 2) {
// __add, __div, __mul, __eq, etc..
this.add_text(
`---@operator ${normalName}(${metamethod.params[1].type}):${metamethod.returns[0].type}`
);
} else if (metamethod.params.length == 1) {
// __unm, __index
this.add_text(
`---@operator ${normalName}(${metamethod.params[0].type}):${metamethod.returns[0].type}`
);
} else {
// __newindex (TODO: does sumneko handle multiple operator parameters?)
// __newindex uses three.
log(
`Unable to handle metamethod '${metamethod.name}' for ${object.name}.`,
Severity.Warning
);
}
} else if (
metamethod.returns &&
metamethod.returns.length == 1
) {
// __tostring
this.add_text(
`---@operator ${normalName}:${metamethod.returns[0].type}`
);
} else {
log(
`Unable to handle metamethod '${metamethod.name}' for ${object.name}.`,
Severity.Warning
);
}
}
}
this.add_text(`_G.${object.name} = {}`);
this.tab_level++;
this.current_table = "." + object.name;
}
end() {
this.tab_level--;
this.current_table = "";
}
add_function(func: Func, is_class = false) {
const { path, returns, params, realm, name } = func;
this.add_comment(`${name} - ${realm} - ${path}`);
const funcType = is_class ? ":" : ".";
let signature = `function _G${this.current_table}${funcType}${func.name}(`;
if (params) {
const paramNames = [];
for (const param of params) {
let { name, type } = param;
if (name == "end") {
name = "_end";
// I understand this looks like a shitty hardcoded condition
// but it's literally the ONLY issue in the entirety
// of the starfall API. There's also only 2 occurrences of it.
}
type = type || "any"; // If it's an untyped parameter, then fallback to any.
const libraryOverrides =
OVERRIDES[this.current_table.substring(1)];
if (libraryOverrides) {
// Check for per-function overrides
const functionOverrides = libraryOverrides[func.name];
if (functionOverrides) {
// Check for per-parameter overrides
const paramOverride = functionOverrides[name];
if (paramOverride) {
type = paramOverride;
}
}
}
// Some functions are untyped.
this.add_text(`---@param ${name} ${type} ${param.description}`);
paramNames.push(name);
}
signature += paramNames.join(", ");
}
if (returns) {
for (const _return of returns) {
this.add_text(
`---@return ${_return.type || "any"} ${_return.name} ${
_return.description
}`
);
}
}
signature += ") end";
this.add_text(signature);
}
generate(): string {
return this.body;
}
}
export async function generate(): Promise<string> {
const response = await fetch(DOCS_URL);
const docs: Docs = await response.json();
const document = new Document();
log("Received starfall docs..");
log(`Using version: ${docs.Version}`);
// We need to add hooks as a sumneko-lua ---@alias
document.add_text("---@alias hooks");
for (const idx in docs.Hooks) {
const hook = docs.Hooks[idx];
log(`Adding '${hook.name}`);
document.add_text(
`---| '"${hook.name}"' # ${hook.description.split("\n").join(" ")}`
);
}
for (const idx in docs.Libraries) {
const lib = docs.Libraries[idx];
log(`Processing '${lib.name}'...`);
document.begin_library(lib);
for (const funcIdx in lib.methods) {
const func = lib.methods[funcIdx];
document.add_function(func);
}
document.end();
}
for (const idx in docs.Types) {
const object = docs.Types[idx];
log(`Creating '${object.name}'`);
document.begin_object(object);
for (const funcIdx in object.methods) {
const func = object.methods[funcIdx];
if (func.name.substring(0, 2) == "__") {
// Ignore metamethods as we already annotated them.
// It's also not correct to use them directly.
continue;
}
document.add_function(func, true);
}
document.end();
}
log("Done!");
return document.generate();
}