forked from stanfordhpccenter/HTR-solver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
process_schema.rg
496 lines (454 loc) · 13.9 KB
/
process_schema.rg
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
local C = terralib.includecstring [[
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
]]
local JSON = terralib.includec('json.h')
local UTIL = require 'util-desugared'
-------------------------------------------------------------------------------
-- NOTE: Type constructors are placed in the global namespace, so the schema
-- file can access them without imports.
local isStruct
local isSchemaT
-------------------------------------------------------------------------------
local StringMT = {}
StringMT.__index = StringMT
-- int -> String
function String(maxLen)
assert(UTIL.isPosInt(maxLen))
return setmetatable({
maxLen = maxLen,
}, StringMT)
end
-- SchemaT -> bool
local function isString(typ)
return type(typ) == 'table' and getmetatable(typ) == StringMT
end
-------------------------------------------------------------------------------
local EnumMT = {}
EnumMT.__index = EnumMT
-- string* -> Enum
function Enum(...)
local enum = {}
for i,choice in ipairs({...}) do
assert(type(choice) == 'string')
enum[choice] = i-1
end
return setmetatable(enum, EnumMT)
end
-- SchemaT -> bool
local function isEnum(typ)
return type(typ) == 'table' and getmetatable(typ) == EnumMT
end
-------------------------------------------------------------------------------
local ArrayMT = {}
ArrayMT.__index = ArrayMT
-- int, SchemaT -> Array
function Array(num, elemType)
assert(UTIL.isPosInt(num))
assert(isSchemaT(elemType))
return setmetatable({
num = num,
elemType = elemType,
}, ArrayMT)
end
-- SchemaT -> bool
local function isArray(typ)
return type(typ) == 'table' and getmetatable(typ) == ArrayMT
end
-------------------------------------------------------------------------------
local UpToMT = {}
UpToMT.__index = UpToMT
-- int, SchemaT -> UpTo
function UpTo(max, elemType)
assert(UTIL.isPosInt(max))
assert(isSchemaT(elemType))
return setmetatable({
max = max,
elemType = elemType,
}, UpToMT)
end
-- SchemaT -> bool
local function isUpTo(typ)
return type(typ) == 'table' and getmetatable(typ) == UpToMT
end
-------------------------------------------------------------------------------
local UnionMT = {}
UnionMT.__index = UnionMT
-- map(string,Struct) -> Union
function Union(choices)
for name,fields in pairs(choices) do
assert(type(name) == 'string')
assert(isStruct(fields))
end
return setmetatable(UTIL.copyTable(choices), UnionMT)
end
-- SchemaT -> bool
local function isUnion(typ)
return type(typ) == 'table' and getmetatable(typ) == UnionMT
end
-------------------------------------------------------------------------------
-- Struct = map(string,SchemaT)
-- SchemaT -> bool
function isStruct(typ)
if type(typ) ~= 'table' or getmetatable(typ) ~= nil then
return false
end
for k,v in pairs(typ) do
if type(k) ~= 'string' or not isSchemaT(v) then
return false
end
end
return true
end
-------------------------------------------------------------------------------
-- SchemaT = 'bool' | 'int' | 'int64' | 'double' | String
-- | Enum | Array | UpTo | Union | Struct
-- A -> bool
function isSchemaT(typ)
return
typ == bool or
typ == int or
typ == int64 or
typ == double or
isString(typ) or
isEnum(typ) or
isArray(typ) or
isUpTo(typ) or
isUnion(typ) or
isStruct(typ)
end
-------------------------------------------------------------------------------
-- SchemaT, map(SchemaT,terralib.type) -> terralib.type
local function convertSchemaT(typ, cache)
if cache[typ] then
return cache[typ]
end
if typ == bool then
return bool
elseif typ == int then
return int
elseif typ == int64 then
return int64
elseif typ == double then
return double
elseif isString(typ) then
return int8[typ.maxLen]
elseif isEnum(typ) then
return int
elseif isArray(typ) then
return convertSchemaT(typ.elemType, cache)[typ.num]
elseif isUpTo(typ) then
return struct {
length : uint32;
values : convertSchemaT(typ.elemType, cache)[typ.max];
}
elseif isUnion(typ) then
local u = terralib.types.newstruct()
u.entries:insert(terralib.newlist())
for n,t in pairs(typ) do
u.entries[1]:insert({field=n, type=convertSchemaT(t, cache)})
end
local s = terralib.types.newstruct()
s.entries:insert({field='type', type=int})
s.entries:insert({field='u', type=u})
return s
elseif isStruct(typ) then
local s = terralib.types.newstruct()
if UTIL.isEmpty(typ) then
s.entries:insert({field='__dummy', type=int})
end
for n,t in pairs(typ) do
s.entries:insert({field=n, type=convertSchemaT(t, cache)})
end
return s
else assert(false) end
end
-- string, terralib.expr* -> terralib.quote
local function errorOut(msg, ...)
local args = {...}
return quote
var stderr = C.fdopen(2, 'w')
C.fprintf(stderr, [msg..'\n'], [args])
C.fflush(stderr)
C.exit(1)
end
end
-- string, terralib.expr -> terralib.quote
local function fldReadErr(msg, name)
return errorOut(msg..' for field %s', name)
end
-- terralib.symbol, terralib.expr, terralib.expr, SchemaT -> terralib.quote
local function emitValueParser(name, lval, rval, typ)
if typ == bool then
return quote
if [rval].type ~= JSON.json_boolean then
[fldReadErr('Wrong type', name)]
end
[lval] = [bool]([rval].u.boolean)
end
elseif typ == int then
return quote
if [rval].type ~= JSON.json_integer then
[fldReadErr('Wrong type', name)]
end
if [int]([rval].u.integer) ~= [rval].u.integer then
[fldReadErr('Integer value overflow', name)]
end
[lval] = [rval].u.integer
end
elseif typ == int64 then
return quote
if [rval].type ~= JSON.json_integer then
[fldReadErr('Wrong type', name)]
end
[lval] = [rval].u.integer
end
elseif typ == double then
return quote
if [rval].type ~= JSON.json_double then
[fldReadErr('Wrong type', name)]
end
[lval] = [rval].u.dbl
end
elseif isString(typ) then
return quote
if [rval].type ~= JSON.json_string then
[fldReadErr('Wrong type', name)]
end
if [rval].u.string.length >= [typ.maxLen] then
[fldReadErr('String too long', name)]
end
C.strncpy([lval], [rval].u.string.ptr, [typ.maxLen])
end
elseif isEnum(typ) then
return quote
if [rval].type ~= JSON.json_string then
[fldReadErr('Wrong type', name)]
end
var found = false
escape for choice,value in pairs(typ) do emit quote
if C.strcmp([rval].u.string.ptr, choice) == 0 then
[lval] = value
found = true
end
end end end
if not found then
[fldReadErr('Unexpected value', name)]
end
end
elseif isArray(typ) then
return quote
if [rval].type ~= JSON.json_array then
[fldReadErr('Wrong type', name)]
end
if [rval].u.array.length ~= [typ.num] then
[fldReadErr('Wrong length', name)]
end
for i = 0,[typ.num] do
var rval_i = [rval].u.array.values[i]
[emitValueParser(name..'[i]', `[lval][i], rval_i, typ.elemType)]
end
end
elseif isUpTo(typ) then
return quote
if [rval].type ~= JSON.json_array then
[fldReadErr('Wrong type', name)]
end
if [rval].u.array.length > [typ.max] then
[fldReadErr('Too many values', name)]
end
[lval].length = [rval].u.array.length
for i = 0,[rval].u.array.length do
var rval_i = [rval].u.array.values[i]
[emitValueParser(name..'[i]', `[lval].values[i], rval_i, typ.elemType)]
end
end
elseif isUnion(typ) then
return quote
if [rval].type ~= JSON.json_object then
[fldReadErr('Wrong type', name)]
end
var foundType = false
for i = 0,[rval].u.object.length do
var nodeName = [rval].u.object.values[i].name
var nodeValue = [rval].u.object.values[i].value
if C.strcmp(nodeName, 'type') == 0 then
foundType = true
if nodeValue.type ~= JSON.json_string then
[fldReadErr('Type field on union not a string', name)]
end
escape local j = 0; for choice,fields in pairs(typ) do emit quote
if C.strcmp(nodeValue.u.string.ptr, [choice]) == 0 then
[lval].type = [j]
[emitValueParser(name, `[lval].u.[choice], rval, fields)]
break
end
end j = j + 1; end end
[fldReadErr('Unrecognized type on union', name)]
end
end
if not foundType then
[fldReadErr('Missing type field on union', name)]
end
end
elseif isStruct(typ) then
return quote
var totalParsed = 0
if [rval].type ~= JSON.json_object then
[fldReadErr('Wrong type', name)]
end
for i = 0,[rval].u.object.length do
var nodeName = [rval].u.object.values[i].name
var nodeValue = [rval].u.object.values[i].value
var parsed = false
escape for fld,subTyp in pairs(typ) do emit quote
if C.strcmp(nodeName, fld) == 0 then
[emitValueParser(name..'.'..fld, `[lval].[fld], nodeValue, subTyp)]
parsed = true
end
end end end
if parsed then
totalParsed = totalParsed + 1
elseif C.strcmp(nodeName, 'type') ~= 0 then
var stderr = C.fdopen(2, 'w')
C.fprintf(
stderr, ['Warning: Ignoring option '..name..'.%s\n'], nodeName)
end
end
-- TODO: Assuming the json file contains no duplicate values
if totalParsed < [UTIL.tableSize(typ)] then
[errorOut('Missing fields from input file')]
end
end
else assert(false) end
end
-------------------------------------------------------------------------------
local ext = '.lua'
if #arg < 1 or not arg[1]:endswith(ext) then
print('Usage: '..arg[0]..' <schema'..ext..'>')
os.exit(1)
end
local baseName = arg[1]:sub(1, arg[1]:len() - ext:len())
local SCHEMA = dofile(arg[1]) -- map(string,SchemaT)
local type2name = {} -- map(Struct,string)
for name,typ in pairs(SCHEMA) do
if isStruct(typ) then
type2name[typ] = name
elseif isUnion(typ) then
type2name[typ] = name
end
end
local deps = {} -- map(string,string*)
for srcN,srcT in pairs(SCHEMA) do
if isStruct(srcT) or isUnion(srcT) then
deps[srcN] = terralib.newlist()
local function traverse(tgtT)
local tgtN = type2name[tgtT]
if tgtN and srcN ~= tgtN then
deps[srcN]:insert(tgtN)
return
end
if tgtT == bool then
-- Do nothing
elseif tgtT == int then
-- Do nothing
elseif tgtT == int64 then
-- Do nothing
elseif tgtT == double then
-- Do nothing
elseif isString(tgtT) then
-- Do nothing
elseif isEnum(tgtT) then
-- Do nothing
elseif isArray(tgtT) then
traverse(tgtT.elemType)
elseif isUpTo(tgtT) then
traverse(tgtT.elemType)
elseif isUnion(tgtT) then
for n,t in pairs(tgtT) do
traverse(t)
end
elseif isStruct(tgtT) then
for n,t in pairs(tgtT) do
traverse(t)
end
else assert(false) end
end
traverse(srcT)
end
end
local sortedNames = UTIL.revTopoSort(deps) -- string*
local type2terra = {} -- map(Struct,terralib.struct)
local parsers = {} -- map(string,terralib.function)
for _,name in ipairs(sortedNames) do
local typ = SCHEMA[name]
local st = convertSchemaT(typ, type2terra)
st.name = name
type2terra[typ] = st
parsers['parse_'..name] = terra(output : &st, fname : &int8)
var f = C.fopen(fname, 'r')
if f == nil then [errorOut('Cannot open %s', fname)] end
var res1 = C.fseek(f, 0, C.SEEK_END)
if res1 ~= 0 then [errorOut('Cannot seek to end of %s', fname)] end
var len = C.ftell(f)
if len < 0 then [errorOut('Cannot ftell %s', fname)] end
var res2 = C.fseek(f, 0, C.SEEK_SET)
if res2 ~= 0 then [errorOut('Cannot seek to start of %s', fname)] end
var buf = [&int8](C.malloc(len))
if buf == nil then [errorOut('Malloc error while parsing %s', fname)] end
var res3 = C.fread(buf, 1, len, f)
if res3 < len then [errorOut('Cannot read from %s', fname)] end
C.fclose(f)
var errMsg : int8[256]
var settings = JSON.json_settings{ 0, 0, nil, nil, nil, 0 }
settings.settings = JSON.json_enable_comments
var root = JSON.json_parse_ex(&settings, buf, len, errMsg)
if root == nil then [errorOut('JSON parsing error: %s', errMsg)] end
[emitValueParser(name, output, root, typ)]
JSON.json_value_free(root)
C.free(buf)
end
end
local hdrFile = io.open(baseName..'.h', 'w')
hdrFile:write('// DO NOT EDIT THIS FILE, IT IS AUTOMATICALLY GENERATED\n')
hdrFile:write('\n')
hdrFile:write('#ifndef __'..string.upper(baseName)..'_H__\n')
hdrFile:write('#define __'..string.upper(baseName)..'_H__\n')
hdrFile:write('\n')
hdrFile:write('#include <stdbool.h>\n')
hdrFile:write('#include <stdint.h>\n')
for name,typ in pairs(SCHEMA) do
if isEnum(typ) then
hdrFile:write('\n')
hdrFile:write('typedef int '..name..';\n')
for choice,value in pairs(typ) do
hdrFile:write('#define '..name..'_'..choice..' '..value..'\n')
end
elseif isUnion(typ) then
hdrFile:write('\n')
hdrFile:write('typedef int '..name..'Type;\n')
local i = 0
for choice,_ in pairs(typ) do
hdrFile:write('#define '..name..'_'..choice..' '..i..'\n')
i = i + 1
end
end
end
for _,name in ipairs(sortedNames) do
local st = type2terra[SCHEMA[name]]
hdrFile:write('\n')
hdrFile:write(UTIL.prettyPrintStruct(st, true)..';\n')
hdrFile:write('\n')
hdrFile:write('#ifdef __cplusplus\n')
hdrFile:write('extern "C" {\n')
hdrFile:write('#endif\n')
hdrFile:write('void parse_'..name..'(struct '..name..'*, char*);\n')
hdrFile:write('#ifdef __cplusplus\n')
hdrFile:write('}\n')
hdrFile:write('#endif\n')
end
hdrFile:write('\n')
hdrFile:write('#endif // __'..string.upper(baseName)..'_H__\n')
hdrFile:close()
terralib.saveobj(baseName..'.o', 'object', parsers)