-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenum.lua
69 lines (59 loc) · 1.79 KB
/
enum.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
include 'common/convert_case'
include 'common/split_namespace'
function make_enum (full_name, datatype, constants, class)
local data = { }
local map = { }
local last_value
local max_length = 0
for i = 1, #constants do
local constant = constants[i]
if type(constant) ~= 'table' then
constant = {
name = constant
}
end
if not constant.name then
error('Invalid enum constant name: ' .. constant.name)
end
if map[constant.name] then
error('Duplicate enum constant: ' .. constant.name)
end
max_length = math.max(max_length, #constant.name)
if not constant.value or (type(constant.value) == 'string' and #constant.value == 0) then
constant.value = (last_value or -1) + 1
if last_value == nil or constant.value ~= (last_value + 1) then
constant.assign_value = constant.value
end
else
constant.assign_value = constant.value
end
if tonumber(constant.value) ~= nil then
last_value = constant.value
else
local other = map[constant.value]
if not other then
last_value = nil
else
last_value = other.value
end
end
data[#data + 1] = constant
map[constant.name] = constant
end
local name, ns = split_namespace(full_name)
return {
class = class,
full_name = full_name,
name = name,
ns = ns,
namespace = table.concat(ns, '::'),
snake_case_name = to_snake_case(name),
type = datatype,
constants = data,
constant_map = map,
max_constant_name_length = max_length
}
end
function make_enum_class (full_name, datatype, constants)
return make_enum(full_name, datatype, constants, true)
end