-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_ConvCommonTypes.lua
319 lines (269 loc) · 8.01 KB
/
_ConvCommonTypes.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
--Utility functions for dealing with converting to/from
-- variables,
-- return types,
-- function params,
-- and other things dealing with C types.
require "_Utils"
local common = require "_ConvCommon"
local funcs = {}
-------------------------------------------------------
-- OLD TO NEW
--Also returns the remainder of the string after the match.
--Or the entire string if nothing matched.
local function CheckPtrs(str)
local tests =
{
{
ref = "pointer-const-pointer",
pttn = "%*%s*const%s*%*",
},
{
ref = "pointer-pointer",
pttn = "%*%s*%*",
},
{
ref = "pointer",
pttn = "%*",
},
}
for _, test in ipairs(tests) do
local match = str:match(test.pttn)
if(match) then
return test.ref, str:sub(#match)
end
end
return nil, str
end
--Processes a variable that's written in plain text (no elements).
--If `type_only` is true, then the string is just the type, with no name or anything else.
--Returns a table containing:
-- `const = true`: if it is a const type.
-- `struct = true`: if the basetype needs the `struct` prefix.
-- `bastype`: The basic type
-- `reference`: One of the reference strings, if any.
-- `name`: The name of the variable, if any.
--Also returns the unparsed portion
function funcs.ParseTextType(str, type_only)
local typedef = {}
local pattern = "%s*([^%s*]+)(.*)";
local token, next = str:match(pattern)
while(token) do
if(token == "const") then
typedef.const = "true"
elseif(token == "struct") then
typedef.struct = "true"
else
typedef.basetype = token
break
end
token, next = next:match(pattern)
end
local reference
reference, next = CheckPtrs(next)
typedef.reference = reference
if(not type_only) then
token, next = next:match("%s*([%a_][%w_]+)(.*)")
typedef.name = token
--Parse array sizes?
end
return typedef, next
end
--Advances pos to the next child of `mem_node` (pos may be zero)
--Skips any node that is a `comment` element.
local function AdvanceAndSkipComments(mem_node, pos)
repeat
pos = pos + 1
local sub_node = mem_node.kids[pos]
until((not sub_node) or sub_node.type ~= "element" or sub_node.name ~= "comment")
return mem_node.kids[pos], pos
end
--Returns a table containing (in accord with reg.struct.member.attlist:
-- `const = true`: if it is a const type.
-- `struct = true`: if the basetype needs the `struct` prefix.
-- `bastype`: The basic type
-- `reference`: One of the reference strings, if any.
-- `name`: The name of the variable, if any.
-- `array`
-- `size`
-- `size-enumref`
-- `c-size`
-- `sync`
-- `extension-structs`
-- `auto-validity`
-- `type-enums`
--A second return value is a table of `comment` strings, or nil if there are no comments.
function funcs.ParseMemberParam(mem_node)
local member = {}
--Get member flags and fields.
member.optional = mem_node.attr.optional
member["c-size"] = mem_node.attr.altlen
if(mem_node.attr.len) then
--Length is a comma-separated list.
for len_data in mem_node.attr.len:gmatch("[^,]+") do
if(len_data == "null-terminated") then
assert(member["null-terminate"] == nil)
member["null-terminate"] = true
member.array = "dynamic"
else
assert(member.size == nil)
member.size = len_data
member.array = "dynamic"
end
end
end
if(mem_node.attr.externsync) then
if(mem_node.attr.externsync == "true") then
member.sync = true
else
member.sync = mem_node.attr.externsync
end
end
if(mem_node.attr.noautovalidity) then
--Simple true/false
member["auto-validity"] = mem_node.attr.noautovalidity ~= "true"
end
if(mem_node.attr.validextensionstructs) then
member["extension-structs"] = mem_node.attr.validextensionstructs
end
if(mem_node.attr.values) then
member["type-enums"] = mem_node.attr.values
end
local sub_node, pos = AdvanceAndSkipComments(mem_node, 0)
--Check for const and/or struct.
if(sub_node.type == "text") then
local test = sub_node.value:match("const")
if(test) then
member.const = true
end
test = sub_node.value:match("struct")
if(test) then
member.struct = true
end
assert(member.const or member.struct)
sub_node, pos = AdvanceAndSkipComments(mem_node, pos)
end
--Extract type.
assert(sub_node.type == "element" and sub_node.name == "type")
member.basetype = common.ExtractFullText(sub_node)
sub_node, pos = AdvanceAndSkipComments(mem_node, pos)
--Extract pointer/references.
--Sometimes, no text between `type` and `name`.
if(sub_node.type == "text") then
local reference = CheckPtrs(sub_node.value)
if(reference) then
member.reference = reference
end
sub_node, pos = AdvanceAndSkipComments(mem_node, pos)
end
--Extract the member name.
assert(sub_node.type == "element" and sub_node.name == "name")
member.name = common.ExtractFullText(sub_node)
--Extract static arrays.
sub_node, pos = AdvanceAndSkipComments(mem_node, pos, member.name)
if(sub_node) then
assert(sub_node.type == "text")
--Cannot already have an array
assert(member.array == nil)
member.array = "static"
local match = sub_node.value:match("%[(.+)%]")
if(match) then
member.size = assert(tonumber(match))
else
pos = pos + 1
sub_node = mem_node.kids[pos]
assert(sub_node.type == "element")
member["size-enumref"] = common.ExtractFullText(sub_node)
end
end
--[[
--Find all comment children.
local comments = {}
for _, elem in ipairs(node.el) do
if(elem.name == "comment") then
comments[#comments + 1] = common.ExtractFullText(elem))
end
end
if(#comments == 0) then
comments = nil
end
]]
return member
end
-------------------------------------------------------
-- NEW TO OLD
local old_reference_map =
{
["pointer"] = "*",
["pointer-const-pointer"] = "* const*",
["pointer-pointer"] = "**",
}
--Writes the text before the name of the value.
--Does not write any array information.
--`wrapInType` if true, then the basetype will be wrapped in a <type> node
function funcs.OldWritePrenameType(writer, type_node, wrapInType)
if(type_node.attr.const == "true") then
writer:AddText("const ")
end
if(type_node.attr.struct == "true") then
writer:AddText("struct ")
end
if(wrapInType) then
writer:PushElement("type")
end
writer:AddText(type_node.attr.basetype)
if(wrapInType) then
writer:PopElement()
end
if(type_node.attr.reference) then
local ref = old_reference_map[type_node.attr.reference]
assert(ref)
writer:AddText(ref)
end
end
function funcs.OldWriteVariable(writer, node, rawName)
--Write the typed.variable.model attributes.
common.CopyAttribIfPresent(writer, node, "optional")
common.CopyAttribIfPresent(writer, node, "sync", "externsync")
if(node.attr["auto-validity"] ~= nil) then
writer:AddAttribute("noautovalidity", tostring(node.attr["auto-validity"] == "false"))
end
--`len` is complex.
if(node.attr.array == "dynamic") then
--There is a `len` of some form.
local length = {}
if(node.attr.size) then
length[#length + 1] = node.attr.size
end
if(node.attr["null-terminate"]) then
length[#length + 1] = "null-terminated"
end
writer:AddAttribute("len", table.concat(length, ","))
end
if(node.attr["c-size"]) then
writer:AddAttribute("altlen", node.attr["c-size"])
end
--Now, write the typing information.
funcs.OldWritePrenameType(writer, node, true)
--Insert the name.
writer:AddText(" ")
if(rawName) then
writer:AddText(node.attr.name)
else
common.WriteTextElement(writer, "name", node.attr.name)
end
--Add any static array stuff.
if(node.attr.array == "static") then
--Static array numeric sizes don't need an element.
--Non-numeric sizes do.
writer:AddText("[")
if(node.attr.size) then
writer:AddText(node.attr.size)
else
writer:PushElement("enum")
writer:AddText(node.attr["size-enumref"])
writer:PopElement()
end
writer:AddText("]")
end
end
return funcs