1 --[[ @file enum_generator.lua
3 This script can be used to generate an enum in game_share.
5 require("enum_generator")
6 enum_generator.build("name", { "VAL" })
8 Name parameter can be a table of strings, in which case each string will be
9 treated as a word and concatenated in various ways. Example:
10 enum_generator.build({ "composed", "name" }, { "VAL" })
12 The name of the generated file will be composed_name.h and composed_name.cpp,
13 and thus your lua script should be composed_name.lua.
15 Default templates for h and cpp files are respectively in
16 enum_generator_template_h.lua and enum_generator_template_cpp.lua. You should
17 only modify these templates in a generic way to avoid breaking existing
18 generated enums. If you need specific templates you can provide your own to
19 the build method. Example:
20 enum_generator.build(name, enum, "int %name1%[];", "int %name1%[] = { %enum1% };" )
26 local function format_name(name
, sep
, formater
)
27 if type(name
)=="string" then
29 elseif type(name
)=="table" and table.getn(name
)>0 then
30 local str
= formater(name
[1])
31 for i
=2, table.getn(name
) do
32 str
= str
..sep
..formater(name
[i
])
40 -- this one take a formater for first word and another for following words
41 local function format_name2(name
, sep
, formater1
, formater2
)
42 if type(name
)=="string" then
43 return formater1(name
)
44 elseif type(name
)=="table" and table.getn(name
)>0 then
45 local str
= formater1(name
[1])
46 for i
=2, table.getn(name
) do
47 str
= str
..sep
..formater2(name
[i
])
55 local word_formater
= {}
56 function word_formater
.lower(word
)
57 return string.lower(word
)
59 function word_formater
.upper(word
)
60 return string.upper(word
)
62 function word_formater
.initial(word
)
63 local head
= string.sub(word
, 1, 1)
64 local tail
= string.sub(word
, 2)
65 return string.upper(head
)..string.lower(tail
)
68 local name_formater
= {}
69 name_formater
["name1"] = function(name
)
70 return format_name(name
, "_", word_formater
.lower
)
72 name_formater
["name2"] = function(name
)
73 return format_name(name
, "_", word_formater
.upper
)
75 name_formater
["name3"] = function(name
)
76 return format_name(name
, "", word_formater
.initial
)
78 name_formater
["name4"] = function(name
)
79 return format_name(name
, " ", word_formater
.lower
)
81 name_formater
["name5"] = function(name
)
82 return format_name2(name
, "", word_formater
.lower
, word_formater
.initial
)
85 local enum_formater
= {}
86 enum_formater
["enum1"] = function(enum
)
88 for i
,val
in pairs(enum
) do
89 str
= str
..string.gsub("\t\t<val>,", "<val>", val
).."\n"
93 enum_formater
["enum2"] = function(enum
)
95 for i
,val
in pairs(enum
) do
96 str
= str
..string.gsub("\t\t { \"<val>\", <val> },", "<val>", val
).."\n"
101 require("enum_generator_template_h")
102 require("enum_generator_template_cpp")
104 function enum_generator
.build(name
, enum
, template_h
, template_cpp
)
107 files
[".h"] = template_h
or enum_generator
.template_h
108 files
[".cpp"] = template_cpp
or enum_generator
.template_cpp
111 for k
,formater
in pairs(name_formater
) do
112 names
[k
] = formater(name
)
116 for k
,formater
in pairs(enum_formater
) do
117 enums
[k
] = formater(enum
)
120 for ext
,str
in pairs(files
) do
121 for k
,v
in pairs(names
) do
122 str
= string.gsub(str
, "%%"..k
.."%%", v
)
124 for k
,v
in pairs(enums
) do
125 str
= string.gsub(str
, "%%"..k
.."%%", v
)
127 local file
= assert(io
.open(names
["name1"]..ext
, "w"))