-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidl.lua
146 lines (125 loc) · 3.19 KB
/
idl.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
local function trim(s)
return (s:gsub("^%s*(.-)%s*$", "%1"))
end
local function sort_methods(a, b)
return tonumber(a.id) < tonumber(b.id)
end
local function getinnertype(typ)
if string.sub(typ, 1, 4) == "List" then
return string.sub(typ, 6, #typ - 1)
end
return typ
end
local function addfieldtype(t)
local innertype = getinnertype(t.type)
if innertype == "Integer" then
t.fieldType = "FieldType.INT32"
elseif innertype == "Long" then
t.fieldType = "FieldType.INT64"
elseif innertype == "Boolean" then
t.fieldType = "FieldType.BOOL"
end
return t
end
local function typedef(_, name)
local t = { type = name }
t = addfieldtype(t)
local function def(self, val)
self.value = val
return function(str)
self.comment = str
local nt = {}
nt.type = self.type
nt.fieldType = self.fieldType
nt.value = self.value
nt.comment = self.comment
return nt
end
end
setmetatable(t, { __call = def })
return t
end
local idl = setmetatable({}, { __index = typedef })
idl.getinnertype = getinnertype
local function add_comment(comments, str)
comments[#comments + 1] = str
end
idl.comment = setmetatable({}, { __call = add_comment })
local function add_method(m, t)
for id, method in pairs(t) do
method.id = id
local id_t = idl.int "id" "ID"
id_t.default = id
if method.req then
table.insert(method.req, 1, id_t)
end
if method.res then
table.insert(method.res, 1, id_t)
end
m.methods[#m.methods + 1] = method
end
table.sort(m.methods, sort_methods)
end
local function moddef(m, modname)
m.modname = modname
m.methods = {}
return function(v)
m.comment = v
return function(t)
add_method(m, t)
end
end
end
idl.mod = setmetatable({}, { __call = moddef })
local function apidef(_, funcname)
local f = { name = funcname }
local function add_val(t)
for _, v in pairs(t) do
if v.type == "req" then
f.req = v.value
elseif v.type == "res" then
f.res = v.value
else
f.desc = v
end
end
return f
end
return function(str)
f.comment = str
return add_val
end
end
idl.api = setmetatable({}, { __call = apidef })
function idl.req(t)
return { type = "req", value = t }
end
function idl.res(t)
return { type = "res", value = t }
end
local clz = {}
idl.clz = clz
local function classdef(_, name)
name = trim(name)
local t = { type = "classdef", name = name }
return function(value)
t.value = value
clz[t.name] = t
end
end
local function classcreate(_, classname)
local name = clz[classname].name
return idl[name]
end
idl.class = setmetatable({}, { __index = classcreate, __call = classdef })
function idl.list(E)
if type(E) == "table" then
E.type = "List<" .. E.type .. ">"
return E
else
E = trim(E)
local name = "List<" .. E .. ">"
return idl[name]
end
end
return idl