Merge branch 'fixes' into main/rendor-staging
[ryzomcore.git] / ryzom / common / src / game_share / enum_generator.lua
blobbf253f900ca0b6364459c0814762de7264e5f8a7
1 --[[ @file enum_generator.lua
3 This script can be used to generate an enum in game_share.
4 Minimal usage:
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% };" )
22 --]]
24 enum_generator = {}
26 local function format_name(name, sep, formater)
27 if type(name)=="string" then
28 return formater(name)
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])
33 end
34 return str
35 else
36 return nil
37 end
38 end
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])
48 end
49 return str
50 else
51 return nil
52 end
53 end
55 local word_formater = {}
56 function word_formater.lower(word)
57 return string.lower(word)
58 end
59 function word_formater.upper(word)
60 return string.upper(word)
61 end
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)
66 end
68 local name_formater = {}
69 name_formater["name1"] = function(name)
70 return format_name(name, "_", word_formater.lower)
71 end
72 name_formater["name2"] = function(name)
73 return format_name(name, "_", word_formater.upper)
74 end
75 name_formater["name3"] = function(name)
76 return format_name(name, "", word_formater.initial)
77 end
78 name_formater["name4"] = function(name)
79 return format_name(name, " ", word_formater.lower)
80 end
81 name_formater["name5"] = function(name)
82 return format_name2(name, "", word_formater.lower, word_formater.initial)
83 end
85 local enum_formater = {}
86 enum_formater["enum1"] = function(enum)
87 local str = ""
88 for i,val in pairs(enum) do
89 str = str..string.gsub("\t\t<val>,", "<val>", val).."\n"
90 end
91 return str
92 end
93 enum_formater["enum2"] = function(enum)
94 local str = ""
95 for i,val in pairs(enum) do
96 str = str..string.gsub("\t\t { \"<val>\", <val> },", "<val>", val).."\n"
97 end
98 return str
99 end
101 require("enum_generator_template_h")
102 require("enum_generator_template_cpp")
104 function enum_generator.build(name, enum, template_h, template_cpp)
106 local files = {}
107 files[".h"] = template_h or enum_generator.template_h
108 files[".cpp"] = template_cpp or enum_generator.template_cpp
110 local names = {}
111 for k,formater in pairs(name_formater) do
112 names[k] = formater(name)
115 local enums = {}
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"))
128 file:write(str)
129 file:close()