-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ConvTypesToNew.lua
416 lines (361 loc) · 8.59 KB
/
_ConvTypesToNew.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
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
require "_Utils"
local common = require "_ConvCommon"
local types = require "_ConvCommonTypes"
local convert = require "_ConvCommonConvert"
local function TestCategory(cat)
return function(node)
return (node.type == "element") and
node.name == "type" and
node.attr.category == cat
end
end
--May be an attribute `name` or a `name` child element.
local function GetIncludeName(node)
if(node.attr.name) then
return node.attr.name
end
local elem = common.FindChildElement(node, "name")
if(elem) then
return common.ExtractFullText(elem)
end
assert(false)
end
local child_include =
{ test = TestCategory("include"),
element =
{ name = "include",
attribs =
{
name = GetIncludeName,
["need-ext"] = function(node)
local name = GetIncludeName(node)
if(name:match("%.h$")) then
return nil
else
return "true"
end
end,
style = function(node)
local include_stmt = common.FindNextText(node).value
local inc_char = include_stmt:match("%#include%s+([\"<])")
assert(inc_char)
if(inc_char == '<') then
return "bracket"
else
return "quote"
end
end
},
},
}
--The `requires` node is not required.
local child_require =
{ test = TestCategory(nil),
element =
{ name = "reference",
map_attribs =
{
name = "name",
requires = "include",
},
},
children =
{
convert.toNotation,
},
}
local child_define =
{ test = TestCategory("define"),
element =
{ name = "define",
attribs =
{
name = function(node)
if(node.attr.name) then
return node.attr.name
else
local name = common.ExtractTextFromChild(node, "name")
assert(#name > 0)
return name
end
end,
replace = function(node)
if(node.attr.name) then
return true
else
return nil
end
end,
},
proc = function(writer, node)
--DON'T WRITE ELEMENTS UNTIL THE END.
--Search for any `type` child nodes. These are references to definitions.
local defrefs = {}
for _, child in ipairs(node.el) do
if(child.name == "type") then
local name = common.ExtractFullText(child)
defrefs[#defrefs + 1] = name
end
end
--Search for a prefixing comment in the overall text.
local whole_text = common.ExtractFullText(node)
local text_lines = {}
for str in whole_text:gmatch("([^\n]+)") do
text_lines[#text_lines + 1] = str
end
local comment = {}
local real_lines = {}
local storeCommentsInLines = false
local fullyCommented = true
for _, str in ipairs(text_lines) do
local test = str:match("%s*//%s*(.+)")
if(test) then
--If the comment is a #define, then it's the actual data.
if(storeCommentsInLines or test:match("^#define")) then
storeCommentsInLines = true
real_lines[#real_lines + 1] = test
else
comment[#comment + 1] = test
end
else
real_lines[#real_lines + 1] = str
fullyCommented = false
end
end
if(#real_lines == 0) then
real_lines = text_lines
end
if(fullyCommented) then
writer:AddAttribute("disabled", "true")
end
--Extract the actual C-expression.
local c_expression
local params = {}
if(node.attr.name) then
--Replace the whole thing.
c_expression = table.concat(text_lines, "\n")
else
--Find the #define in the real_lines.
local relevant_text
for _, line in ipairs(real_lines) do
local check = line:match("#define%s+([_%a][_%w]*)")
if(check) then
relevant_text = table.concat(real_lines, "\n", _)
break
end
end
--See if there are parameters
local param_text, expr = relevant_text:match("#define%s+[_%a][_%w]*(%b())%s*(.+)")
if(not param_text) then
expr = relevant_text:match("#define%s+[_%a][_%w]*%s*(.+)")
else
--Remove parens
param_text = param_text:sub(2, #param_text - 1)
for param in param_text:gmatch("([_%a][_%w]+)") do
params[#params + 1] = param
end
end
if(param_text) then
c_expression = expr
else
local num = expr:match("^%s*(%d+)%s*$")
if(num) then
writer:AddAttribute("value", num)
else
c_expression = expr
end
end
end
--WRITE ELEMENTS PAST THIS POINT.
if(#comment > 0) then
common.WriteTextElement(writer, "comment", table.concat(comment, "\n"))
end
for _, defref in ipairs(defrefs) do
common.WriteTextElement(writer, "defref", defref)
did_write_element = true
end
for _, param in ipairs(params) do
common.WriteTextElement(writer, "param", param)
end
if(c_expression) then
common.WriteTextElement(writer, "c-expression", c_expression)
end
return data
end
},
}
local child_typedef =
{ test = TestCategory("basetype"),
element =
{ name = "typedef",
attribs =
{
name = function(node) return common.ExtractTextFromChild(node, "name") end,
basetype = function(node) return common.ExtractTextFromChild(node, "type") end,
},
},
children =
{
convert.toNotation,
},
}
local child_bitmask =
{ test = TestCategory("bitmask"),
element =
{ name = "bitmask",
map_attribs =
{
requires = "enumref",
},
attribs = child_typedef.element.attribs,
},
children =
{
convert.toNotation,
},
}
local child_handle =
{ test = TestCategory("handle"),
element =
{ name = "handle",
map_attribs =
{
parent = "parent"
},
attribs =
{
name = function(node) return common.ExtractTextFromChild(node, "name") end,
type = function(node)
local handle_type = common.ExtractTextFromChild(node, "type")
if(handle_type == "VK_DEFINE_HANDLE") then
return "dispatch"
elseif(handle_type == "VK_DEFINE_NON_DISPATCHABLE_HANDLE") then
return "nodispatch"
else
assert(false, common.ExtractTextFromChild(node, "name"))
end
end,
},
},
children =
{
convert.toNotation,
},
}
local child_enum =
{ test = TestCategory("enum"),
element =
{ name = "enumeration",
map_attribs = { name = "name" },
},
children =
{
convert.toNotation,
},
}
local child_funcptr =
{ test = TestCategory("funcpointer"),
element =
{ name = "funcptr",
attribs =
{
name = function(node) return common.ExtractTextFromChild(node, "name") end,
},
proc = function(writer, node)
local return_text = common.FindNextText(node).value
local return_string = return_text:match("typedef *(.+)%(")
local return_type = types.ParseTextType(return_string, true)
writer:PushElement("return-type")
common.WriteTblAsAttribs(writer, return_type)
writer:PopElement()
local full_text = common.ExtractFullText(node)
--Get paren-enclosed data.
local _, param_seq = full_text:match("(%b())%s*(%b())")
--Remove the two parens.
param_seq = param_seq:sub(2, #param_seq - 1)
--Special-case: if entire parameter list is just "void", then no parameters.
if(param_seq ~= "void") then
--Process the coma-separated sequence.
for param in param_seq:gmatch("%s*([^,]+)") do
local parameter = types.ParseTextType(param, false)
writer:PushElement("param")
common.WriteTblAsAttribs(writer, parameter)
writer:PopElement()
end
end
end
},
}
local struct_member =
{ test = "member",
element =
{ name = "member",
proc = function(writer, node)
local member = types.ParseMemberParam(node)
common.WriteTblAsAttribs(writer, member)
end
},
children =
{
convert.toNotation,
},
}
local child_struct =
{ test = TestCategory("struct"),
element =
{ name = "struct",
map_attribs =
{
name = "name",
comment = "notation",
returnedonly = "is-return",
structextends = "extends",
},
},
children =
{
struct_member,
convert.toNewValidity,
convert.toNotation,
},
}
local child_union =
{ test = TestCategory("union"),
element =
{ name = "union",
map_attribs =
{
name = "name",
comment = "notation",
},
},
children =
{
struct_member,
convert.toNewValidity,
convert.toNotation,
},
}
local children =
{
child_include,
child_require,
child_define,
child_typedef,
child_bitmask,
child_handle,
child_enum,
child_funcptr,
child_struct,
child_union,
convert.toNotation,
}
return { test = "types",
element =
{ name = "definitions",
map_attribs =
{ comment = "notation",
},
},
children = children,
}