mirror of
https://github.com/cimgui/cimgui.git
synced 2025-08-10 20:08:31 +01:00
apply ocornut suggested cimgui structure
This commit is contained in:
997
generator/generator.lua
Normal file
997
generator/generator.lua
Normal file
@@ -0,0 +1,997 @@
|
||||
--------------------------------------------------------------------------
|
||||
--script for auto_funcs.h and auto_funcs.cpp generation
|
||||
--expects Lua 5.1 or luajit
|
||||
--------------------------------------------------------------------------
|
||||
local script_args = {...}
|
||||
local implementations = {}
|
||||
for i=2,#script_args do table.insert(implementations,script_args[i]) end
|
||||
-- first script argument to use gcc or not
|
||||
local USEGCC = script_args[1] == "true"
|
||||
--------------------------------------------------------------------------
|
||||
--this table has the functions to be skipped in generation
|
||||
--------------------------------------------------------------------------
|
||||
local cimgui_manuals = {
|
||||
igLogText = true,
|
||||
ImGuiTextBuffer_appendf = true,
|
||||
}
|
||||
--------------------------------------------------------------------------
|
||||
--this table is a dictionary to force a naming of function overloading (instead of algorythmic generated)
|
||||
--first level is cimguiname without postfix, second level is the signature of the function, value is the
|
||||
--desired name
|
||||
---------------------------------------------------------------------------
|
||||
local cimgui_overloads = {
|
||||
igPushID = {
|
||||
--["(const char*)"] = "igPushIDStr",
|
||||
["(const char*,const char*)"] = "igPushIDRange",
|
||||
--["(const void*)"] = "igPushIDPtr",
|
||||
--["(int)"] = "igPushIDInt"
|
||||
},
|
||||
ImDrawList_AddText = {
|
||||
["(const ImVec2,ImU32,const char*,const char*)"] = "ImDrawList_AddText",
|
||||
},
|
||||
igGetColorU32 = {
|
||||
["(ImGuiCol,float)"] = "igGetColorU32",
|
||||
},
|
||||
igCollapsingHeader = {
|
||||
["(const char*,ImGuiTreeNodeFlags)"] = "igCollapsingHeader",
|
||||
},
|
||||
igCombo = {
|
||||
["(const char*,int*,const char* const[],int,int)"] = "igCombo",
|
||||
},
|
||||
igPlotLines = {
|
||||
["(const char*,const float*,int,int,const char*,float,float,ImVec2,int)"] = "igPlotLines",
|
||||
},
|
||||
igBeginChild = {
|
||||
["(const char*,const ImVec2,bool,ImGuiWindowFlags)"] = "igBeginChild",
|
||||
},
|
||||
igSelectable = {
|
||||
["(const char*,bool,ImGuiSelectableFlags,const ImVec2)"] = "igSelectable"
|
||||
},
|
||||
igPushStyleColor = {
|
||||
["(ImGuiCol,const ImVec4)"] = "igPushStyleColor"
|
||||
}
|
||||
}
|
||||
--------------------------------------------------------------------------
|
||||
--helper functions
|
||||
--------------------------------------------------------------------------
|
||||
--iterates lines from a .h file and discards between #if.. and #endif
|
||||
local function filelines(file)
|
||||
local iflevels = {}
|
||||
local function location_it()
|
||||
repeat
|
||||
local line = file:read"*l"
|
||||
if not line then return nil end
|
||||
if line:sub(1,1) == "#" then
|
||||
if line:match("#if") then
|
||||
iflevels[#iflevels +1 ] = true
|
||||
elseif line:match("#endif") then
|
||||
iflevels[#iflevels] = nil
|
||||
end
|
||||
-- skip
|
||||
elseif #iflevels == 0 then
|
||||
-- drop IMGUI_APIX
|
||||
line = line:gsub("IMGUI_APIX","")
|
||||
-- drop IMGUI_API
|
||||
line = line:gsub("IMGUI_API","")
|
||||
return line
|
||||
end
|
||||
until false
|
||||
end
|
||||
return location_it
|
||||
end
|
||||
--iterates lines from a gcc -E in a specific location
|
||||
local function location(file,locpathT)
|
||||
local location_re = '^# %d+ "([^"]*)"'
|
||||
local path_reT = {}
|
||||
for i,locpath in ipairs(locpathT) do
|
||||
table.insert(path_reT,'^(.*[\\/])('..locpath..')%.h$')
|
||||
end
|
||||
local in_location = false
|
||||
local which_location = ""
|
||||
local function location_it()
|
||||
repeat
|
||||
local line = file:read"*l"
|
||||
if not line then return nil end
|
||||
if line:sub(1,1) == "#" then
|
||||
-- Is this a location pragma?
|
||||
local location_match = line:match(location_re)
|
||||
if location_match then
|
||||
in_location = false
|
||||
for i,path_re in ipairs(path_reT) do
|
||||
if location_match:match(path_re) then
|
||||
in_location = true;
|
||||
which_location = locpathT[i]
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
elseif in_location then
|
||||
return line, which_location
|
||||
end
|
||||
until false
|
||||
end
|
||||
return location_it
|
||||
end
|
||||
------serializeTable("anyname",table) gives a string that recreates the table with dofile(generated_string)
|
||||
local function serializeTable(name, value, saved)
|
||||
|
||||
local function basicSerialize (o)
|
||||
if type(o) == "number" or type(o)=="boolean" then
|
||||
return tostring(o)
|
||||
elseif type(o) == "string" then
|
||||
return string.format("%q", o)
|
||||
else
|
||||
return "nil"
|
||||
end
|
||||
end
|
||||
|
||||
local string_table = {}
|
||||
if not saved then
|
||||
table.insert(string_table, "local "..name.." = ")
|
||||
else
|
||||
table.insert(string_table, name.." = ")
|
||||
end
|
||||
|
||||
saved = saved or {} -- initial value
|
||||
|
||||
if type(value) == "number" or type(value) == "string" or type(value)=="boolean" then
|
||||
table.insert(string_table,basicSerialize(value).."\n")
|
||||
elseif type(value) == "table" then
|
||||
if saved[value] then -- value already saved?
|
||||
table.insert(string_table,saved[value].."\n")
|
||||
else
|
||||
saved[value] = name -- save name for next time
|
||||
table.insert(string_table, "{}\n")
|
||||
for k,v in pairs(value) do -- save its fields
|
||||
local fieldname = string.format("%s[%s]", name,basicSerialize(k))
|
||||
table.insert(string_table, serializeTable(fieldname, v, saved))
|
||||
end
|
||||
end
|
||||
--else
|
||||
--error("cannot save a " .. type(value))
|
||||
end
|
||||
|
||||
return table.concat(string_table)
|
||||
end
|
||||
|
||||
local function save_data(filename,data)
|
||||
local file = io.open(filename,"w")
|
||||
file:write(data)
|
||||
file:close()
|
||||
end
|
||||
|
||||
local function strip(cad)
|
||||
return cad:gsub("^%s*(.-)%s*$","%1") --remove initial and final spaces
|
||||
end
|
||||
local function clean_spaces(cad)
|
||||
cad = strip(cad)
|
||||
--cad = cad:gsub("%s+","%s") --not more than one space
|
||||
cad = cad:gsub("%s*([%(%),=])%s*","%1") --not spaces with ( , )
|
||||
return cad
|
||||
end
|
||||
local function split_comment(line)
|
||||
local comment = line:match("(%s*//.*)") or ""
|
||||
return line:gsub("%s*//.*",""),comment
|
||||
end
|
||||
local function get_manuals(def)
|
||||
return cimgui_manuals[def.ov_cimguiname] or cimgui_manuals[def.cimguiname]
|
||||
end
|
||||
|
||||
local function getcimguiname(stname,funcname)
|
||||
if #stname == 0 then return funcname end --top level
|
||||
local pre = (stname == "ImGui") and "ig" or stname.."_"
|
||||
return pre..funcname
|
||||
end
|
||||
|
||||
local function getcimguiname_overload(stname,funcname,signature)
|
||||
local cname = getcimguiname(stname,funcname)
|
||||
local ov_cname = cimgui_overloads[cname] and cimgui_overloads[cname][signature] --or cname
|
||||
return ov_cname
|
||||
end
|
||||
|
||||
|
||||
local function struct_parser()
|
||||
local function_re = "(%a*%w+%b())" --"(%a*%w+%s+%w+%b())"
|
||||
local function_closing_re = "}"
|
||||
local function_closed_re = "[;}]$"
|
||||
local operator_re = "operator.-%b()"
|
||||
local functype_re = "(%(%*)[%w_]+(%)%([^%(%)]*%))"
|
||||
local initial_comment_re = [[^%s*//.*]]
|
||||
|
||||
local in_functionst = false
|
||||
local structcdefs = {}
|
||||
local STP = {}
|
||||
STP.lines = structcdefs
|
||||
function STP.insert(line,comment)
|
||||
|
||||
--dont process initial comments but keep it
|
||||
if line:match(initial_comment_re) then
|
||||
table.insert(structcdefs,line)
|
||||
return
|
||||
end
|
||||
|
||||
--if in_function discard
|
||||
if in_functionst then
|
||||
if line:match(function_closing_re) then
|
||||
in_functionst = false
|
||||
print("in function:",line)
|
||||
end
|
||||
return
|
||||
end
|
||||
|
||||
|
||||
|
||||
if (line:match(function_re) or line:match(operator_re)) and not line:match("typedef.*%b().*%b().*")
|
||||
and not line:match(functype_re) then
|
||||
if not line:match(function_closed_re) then
|
||||
print("match:",line)
|
||||
in_functionst = true
|
||||
end
|
||||
--else
|
||||
--table.insert(structcdefs,linecommented)
|
||||
elseif line:match("template") then
|
||||
--nothing
|
||||
elseif line:match("public:") then
|
||||
--nothing
|
||||
else
|
||||
local linea = line:gsub("%S+",{class="struct",mutable=""})
|
||||
linea = linea:gsub("(%b<>)","/%*%1%*/") --comment template parameters
|
||||
table.insert(structcdefs,linea..comment)
|
||||
--]]
|
||||
end
|
||||
return
|
||||
end
|
||||
return STP
|
||||
end
|
||||
|
||||
local function typetoStr(typ)
|
||||
--typ = typ:gsub("[^%(%)]+%(%*?(.+)%).+","%1") -- funcs
|
||||
typ = typ:gsub("[^%(%)]+%(%*?(.+)%).+","FnPtr")
|
||||
typ = typ:gsub("[%w_]+%[(%d*)%]","arr%1")
|
||||
typ = typ:gsub("%*","Ptr")
|
||||
typ = typ:gsub("void","")
|
||||
typ = typ:gsub("unsigned%s","u")
|
||||
typ = typ:gsub("const%s","")--"c")
|
||||
typ = typ:gsub("%s+","_")
|
||||
typ = typ:gsub("charPtr","Str")
|
||||
typ = typ:gsub("int","Int")
|
||||
typ = typ:gsub("bool","Bool")
|
||||
typ = typ:gsub("float","Float")
|
||||
typ = typ:gsub("uInt","Uint")
|
||||
typ = typ:gsub("ImGui","")
|
||||
typ = typ:gsub("Im","")
|
||||
typ = typ:gsub("[<>]","")
|
||||
return typ
|
||||
end
|
||||
local function name_overloadsAlgo(v)
|
||||
local aa = {}
|
||||
local bb = {}
|
||||
local done = {}
|
||||
local maxnum = 0
|
||||
for i,t in ipairs(v) do
|
||||
bb[i] = ""
|
||||
local signature = t.signature:sub(2,-2) -- without parenthesis
|
||||
aa[i] = {}
|
||||
local num = 1
|
||||
--for typec in t.signature:gmatch(".-([^,%(%s]+)[,%)]") do
|
||||
--for typec in t.signature:gmatch(".-([^,%(]+)[,%)]") do
|
||||
--for typec in signature:gmatch(".-([^,]+),?") do
|
||||
for typec in signature:gsub("(%(.-%))", function(x) return x:gsub(",","\0") end):gmatch(".-([^,]+),?") do
|
||||
--typec = typec:gsub
|
||||
aa[i][num] = typec:gsub("%z+", ",")
|
||||
num = num + 1
|
||||
end
|
||||
num = num - 1
|
||||
maxnum = (num > maxnum) and num or maxnum
|
||||
end
|
||||
|
||||
for l=1,maxnum do
|
||||
local keys = {}
|
||||
local diferent = true
|
||||
local equal = true
|
||||
for i=1,#v do
|
||||
aa[i][l] = aa[i][l] or "nil"
|
||||
keys[aa[i][l]] = 1 + (aa[i][l] and keys[aa[i][l]] or 0)
|
||||
if not done[i] then
|
||||
for j=i+1,#v do
|
||||
if not done[j] then
|
||||
if aa[i][l] == aa[j][l] then
|
||||
diferent = false
|
||||
else
|
||||
equal = false
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
if not equal then -- not all the same
|
||||
for i=1,#v do
|
||||
if not done[i] then
|
||||
bb[i] = bb[i]..(aa[i][l]=="nil" and "" or aa[i][l])
|
||||
if keys[aa[i][l]] == 1 then
|
||||
done[i] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
return aa,bb
|
||||
end
|
||||
|
||||
local function func_parser()
|
||||
local function_closing_re = "}"
|
||||
local function_re = "(%a*%w+%b())" --"(%a*%w+%s+%w+%b())"
|
||||
local function_closed_re = "[;}]$"
|
||||
local namespace_re = "namespace ([^%s]+)"
|
||||
local namespace_closing_re = "^}"
|
||||
local struct_re = "^struct%s+([^%s;]+)$"
|
||||
local struct_closing_re = "};"
|
||||
local struct_op_close_re = "%b{}"
|
||||
local functype_re = "^%s*[%w%s]+(%(%*)[%w_]+(%)%([^%(%)]*%))"
|
||||
|
||||
local in_function = false
|
||||
local in_namespace = false
|
||||
local cdefs = {}
|
||||
local structnames = {}
|
||||
local embeded_structs = {}
|
||||
local stname = ""
|
||||
local defsT = {}
|
||||
local ImVector_templates = {}
|
||||
|
||||
|
||||
local FP = {}
|
||||
FP.cdefs = cdefs
|
||||
FP.embeded_structs = embeded_structs
|
||||
FP.defsT = defsT
|
||||
FP.ImVector_templates = ImVector_templates
|
||||
|
||||
function FP.insert(line,comment,locat)
|
||||
line = clean_spaces(line)
|
||||
if line:match"template" then return end
|
||||
line = line:gsub("%S+",{class="struct",mutable=""}) --class -> struct
|
||||
|
||||
if in_function then
|
||||
if line:match(function_closing_re) then
|
||||
in_function = false
|
||||
end
|
||||
return --discard
|
||||
end
|
||||
if line:match(function_re) and not line:match("typedef.*%b().*%b().*") then
|
||||
if not line:match(function_closed_re) then
|
||||
in_function = true
|
||||
end
|
||||
end
|
||||
if line:match(namespace_re) then
|
||||
in_namespace = true
|
||||
stname = line:match(namespace_re)
|
||||
end
|
||||
if in_namespace then
|
||||
if line:match(namespace_closing_re) then
|
||||
in_namespace = false
|
||||
stname = ""
|
||||
end
|
||||
end
|
||||
structnames[#structnames + 1] = line:match(struct_re)
|
||||
if #structnames > 0 then
|
||||
if line:match(struct_closing_re) and not line:match(struct_op_close_re) then
|
||||
structnames[#structnames] = nil
|
||||
end
|
||||
stname = structnames[#structnames] or ""
|
||||
if #structnames > 1 then
|
||||
local embeded_name = table.concat(structnames,"::")
|
||||
embeded_structs[stname] = embeded_name
|
||||
end
|
||||
end
|
||||
local func = line:match(function_re)
|
||||
if func and not in_function and not line:match("typedef.*%b().*%b().*")
|
||||
and not line:match(functype_re)
|
||||
then
|
||||
--if line:match(functype_re) then print("ft",line) end
|
||||
if stname~="ImVector"
|
||||
--and stname~="GlyphRangesBuilder" and stname~="CustomRect" and stname~="TextRange" and stname~="Pair"
|
||||
and not line:match("operator") then
|
||||
|
||||
--clean implemetation
|
||||
line = line:gsub("%s*%b{}","")
|
||||
--clean attribute
|
||||
line = line:gsub("%s*__attribute__%b()","")
|
||||
--clean static
|
||||
line = line:gsub("static","")
|
||||
|
||||
local ret = line:match("([^%(%)]+[%*%s])%s?~?[_%w]+%b()")
|
||||
local funcname, args = line:match("(~?[_%w]+)(%b())")
|
||||
|
||||
local argscsinpars = args:gsub("(=[^,%(%)]*)(%b())","%1")
|
||||
argscsinpars = argscsinpars:gsub("(=[^,%(%)]*)([,%)])","%2")
|
||||
argscsinpars = argscsinpars:gsub("&","")
|
||||
|
||||
local template = argscsinpars:match("ImVector<([%w_]+)>")
|
||||
if template then
|
||||
ImVector_templates[template] = true
|
||||
end
|
||||
|
||||
argscsinpars = argscsinpars:gsub("<([%w_]+)>","_%1") --ImVector
|
||||
|
||||
local signature = argscsinpars:gsub("([%w%s%*_]+)%s[%w_]+%s*([,%)])","%1%2")
|
||||
signature = signature:gsub("%s*([,%)])","%1") --space before , and )
|
||||
signature = signature:gsub(",%s*",",")--space after ,
|
||||
signature = signature:gsub("([%w_]+)%s[%w_]+(%[%d*%])","%1%2") -- float[2]
|
||||
signature = signature:gsub("(%(%*)[%w_]+(%)%([^%(%)]*%))","%1%2") --func defs
|
||||
|
||||
local call_args = argscsinpars:gsub("([%w_]+%s[%w_]+)%[%d*%]","%1") --float[2]
|
||||
call_args = call_args:gsub("%(%*([%w_]+)%)%([^%(%)]*%)"," %1") --func type
|
||||
call_args = call_args:gsub("[^%(].-([%w_]+)%s*([,%)])","%1%2")
|
||||
|
||||
if not ret then --must be constructors
|
||||
if not (stname == funcname or "~"..stname==funcname) then --break end
|
||||
print("false constructor:",line);
|
||||
print("b2:",ret,stname,funcname,args)
|
||||
return --are function defs
|
||||
end
|
||||
end
|
||||
|
||||
local cimguiname = getcimguiname(stname,funcname)
|
||||
table.insert(cdefs,{stname=stname,funcname=funcname,args=args,argsc=argscsinpars,signature=signature,cimguiname=cimguiname,call_args=call_args,ret =ret,comment=comment})
|
||||
|
||||
defsT[cimguiname] = defsT[cimguiname] or {}
|
||||
table.insert(defsT[cimguiname],{})
|
||||
local defT = defsT[cimguiname][#defsT[cimguiname]]
|
||||
defT.defaults = {}
|
||||
--for k,def in args:gmatch("([%w%s%*_]+)=([%w_%(%)%s,%*]+)[,%)]") do
|
||||
--for k,def in args:gmatch("([%w_]+)=([%w_%(%)%s,%*%.%-]+)[,%)]") do
|
||||
for k,def in args:gmatch('([%w_]+)=([%w_%(%)%s,%*%.%-%%"]+)[,%)]') do
|
||||
defT.defaults[k]=def
|
||||
end
|
||||
defT.cimguiname = cimguiname
|
||||
defT.stname = stname
|
||||
defT.funcname = funcname
|
||||
defT.argsoriginal = args
|
||||
defT.args=argscsinpars
|
||||
defT.signature = signature
|
||||
defT.call_args = call_args
|
||||
defT.isvararg = signature:match("%.%.%.%)$")
|
||||
defT.location = locat
|
||||
defT.comment = comment
|
||||
if ret then
|
||||
defT.ret = ret:gsub("&","*")
|
||||
defT.retref = ret:match("&")
|
||||
end
|
||||
defsT[cimguiname][signature] = defT
|
||||
end
|
||||
elseif line=="" and stname~="" then -- not funcdef must be comment
|
||||
table.insert(cdefs,{stname=stname,comment=comment})
|
||||
end
|
||||
end
|
||||
FP.alltypes = {}
|
||||
local function get_types(v)
|
||||
for i,t in ipairs(v) do
|
||||
local signature = t.signature:sub(2,-2) -- without parenthesis
|
||||
for typec in signature:gsub("(%(.-%))", function(x) return x:gsub(",","\0") end):gmatch(".-([^,]+),?") do
|
||||
local key = typec:gsub("%z+", ",")
|
||||
FP.alltypes[key] = true
|
||||
end
|
||||
end
|
||||
end
|
||||
function FP:dump_alltypes()
|
||||
for k,v in pairs(self.alltypes) do print(k, typetoStr(k) ) end
|
||||
end
|
||||
function FP:compute_overloads()
|
||||
local numoverloaded = 0
|
||||
FP.alltypes = {}
|
||||
print"----------------overloadings---------------------------"
|
||||
for k,v in pairs(FP.defsT) do
|
||||
get_types(v)
|
||||
if #v > 1 then
|
||||
numoverloaded = numoverloaded + #v
|
||||
print(k,#v)
|
||||
local typesc,post = name_overloadsAlgo(v)
|
||||
for i,t in ipairs(v) do
|
||||
t.ov_cimguiname = getcimguiname_overload(t.stname,t.funcname,t.signature) or k..typetoStr(post[i])
|
||||
print(i,t.signature,t.ret,t.ov_cimguiname,post[i])--,typetoStr(post[i]))
|
||||
--prtable(typesc[i])
|
||||
end
|
||||
end
|
||||
end
|
||||
print(numoverloaded, "overloaded")
|
||||
end
|
||||
return FP
|
||||
end
|
||||
|
||||
local function gen_structs_and_enums_table(cdefs)
|
||||
local function_closing_re = "}"
|
||||
local namespace_re = "namespace"
|
||||
local in_namespace = false
|
||||
local struct_re = "^%s*struct%s+([^%s;]+)$"
|
||||
local in_struct = false
|
||||
local struct_closed_re = "^%s*struct%s+([^%s]+);$"
|
||||
local struct_closing_re = "};"
|
||||
local struct_op_close_re = "%b{}"
|
||||
local structnames = {}
|
||||
local enumnames = {}
|
||||
local enums_re = "^%s*enum%s+([^%s;]+)"
|
||||
local outtab = {structs={},enums={}}
|
||||
|
||||
for i,line in ipairs(cdefs) do
|
||||
repeat -- simulating continue with break
|
||||
-- separate comments from code
|
||||
local linecom = line
|
||||
local line, comment = split_comment(line)
|
||||
line = clean_spaces(line)
|
||||
|
||||
if line:match(namespace_re) then
|
||||
in_namespace = true
|
||||
end
|
||||
|
||||
local structbegin = line:match(struct_re)
|
||||
if structbegin then
|
||||
structnames[#structnames + 1] = structbegin
|
||||
outtab.structs[structbegin] = outtab.structs[structbegin] or {}
|
||||
break
|
||||
end
|
||||
|
||||
local enumname = line:match(enums_re)
|
||||
if enumname then
|
||||
enumnames[#enumnames + 1] = enumname
|
||||
outtab.enums[enumname] = outtab.enums[enumname] or {}
|
||||
break
|
||||
end
|
||||
|
||||
if in_namespace then
|
||||
if line:match(function_closing_re) then
|
||||
in_namespace = false
|
||||
end
|
||||
break -- dont write anything inside
|
||||
end
|
||||
|
||||
if (#enumnames > 0) then
|
||||
assert(#structnames==0,"enum in struct")
|
||||
if line:match(struct_closing_re) and not line:match(struct_op_close_re) then
|
||||
enumnames[#enumnames] = nil
|
||||
break
|
||||
end
|
||||
if line=="" or line:match("^{") then
|
||||
break
|
||||
else
|
||||
local name,value = line:match("%s*([%w_]+)%s*=%s*([^,]+)")
|
||||
if value then
|
||||
table.insert(outtab.enums[enumnames[#enumnames]],{name=name,value=value})
|
||||
else
|
||||
local name = line:match("%s*([^,]+)")
|
||||
local value = #outtab.enums[enumnames[#enumnames]]
|
||||
table.insert(outtab.enums[enumnames[#enumnames]],{name=name,value=value})
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if (#structnames > 0) then
|
||||
if line:match(struct_closing_re) and not line:match(struct_op_close_re) then
|
||||
structnames[#structnames] = nil
|
||||
break
|
||||
end
|
||||
if line=="" or line:match("^{") then
|
||||
break
|
||||
elseif structnames[#structnames] ~="ImVector" then --avoid ImVector
|
||||
--local functype_re = "^%s*[%w%s%*]+(%(%*)[%w_]+(%)%([^%(%)]*%))"
|
||||
local functype_re = "^%s*[%w%s%*]+%(%*[%w_]+%)%([^%(%)]*%)"
|
||||
local functype_reex = "^(%s*[%w%s%*]+%(%*)([%w_]+)(%)%([^%(%)]*%))"
|
||||
if line:match(functype_re) then
|
||||
local t1,name,t2 = line:match(functype_reex)
|
||||
table.insert(outtab.structs[structnames[#structnames]],{type=t1..t2,name=name})
|
||||
break
|
||||
end
|
||||
--split type name1,name2; in several lines
|
||||
local typen,rest = line:match("([^,]+)%s(%S+[,;])")
|
||||
for name in rest:gmatch("([^%s,;]+)%s?[,;]") do
|
||||
table.insert(outtab.structs[structnames[#structnames]],{type=typen,name=name})
|
||||
end
|
||||
end
|
||||
end
|
||||
until true
|
||||
end
|
||||
return outtab
|
||||
end
|
||||
|
||||
|
||||
local function gen_structs_and_enums(cdefs)
|
||||
local function_closing_re = "}"
|
||||
local namespace_re = "namespace"
|
||||
local in_namespace = false
|
||||
local struct_re = "^%s*struct%s+([^%s;]+)$"
|
||||
local struct_closed_re = "^%s*struct%s+([^%s]+);$"
|
||||
local struct_closing_re = "};"
|
||||
local struct_op_close_re = "%b{}"
|
||||
local structnames = {}
|
||||
local innerstructs = {}
|
||||
local typedefs_table = {}
|
||||
local outtab = {}
|
||||
-- Output the file
|
||||
table.insert(outtab,"/////////////// BEGIN AUTOGENERATED SEGMENT\n")
|
||||
if not USEGCC then
|
||||
table.insert(outtab,[[typedef unsigned short ImDrawIdx;]])
|
||||
table.insert(outtab,[[typedef void* ImTextureID;]])
|
||||
end
|
||||
for i,line in ipairs(cdefs) do
|
||||
repeat -- simulating continue with break
|
||||
-- separate comments from code and try to add them with same tab
|
||||
local line, comment = split_comment(line)
|
||||
local linelen = #line
|
||||
local desired_linelen = (linelen==0) and 0 or math.max(math.ceil(linelen/10)*10,40)
|
||||
local spaces_to_add = 0 --desired_linelen - linelen
|
||||
local linecom = line..string.rep(" ",spaces_to_add)..comment
|
||||
|
||||
if line:match(namespace_re) then
|
||||
in_namespace = true
|
||||
end
|
||||
local structbegin = line:match(struct_re)
|
||||
if structbegin then
|
||||
structnames[#structnames + 1] = structbegin
|
||||
if #structnames < 2 and structbegin~= "ImVector" then --not inner and not ImVector
|
||||
table.insert(outtab,linecom.."\n")
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if structnames[#structnames] == "ImVector" then
|
||||
if line:match(struct_closing_re) then
|
||||
table.insert(outtab,[[struct ImVector
|
||||
{
|
||||
int Size;
|
||||
int Capacity;
|
||||
void* Data;
|
||||
};
|
||||
typedef struct ImVector ImVector;]])
|
||||
structnames[#structnames] = nil
|
||||
end
|
||||
break -- dont write
|
||||
end
|
||||
|
||||
if in_namespace then
|
||||
if line:match(function_closing_re) then
|
||||
in_namespace = false
|
||||
end
|
||||
break -- dont write anything inside
|
||||
end
|
||||
|
||||
if #structnames < 2 then -- not inner
|
||||
if (#structnames > 0) then
|
||||
if line:match("typedef") then --dont allow inner typedefs
|
||||
break
|
||||
elseif not line:match("^{$") and not line:match(struct_closing_re) then --avoid tab { and };
|
||||
--line = " "..line
|
||||
end
|
||||
end
|
||||
table.insert(outtab,linecom.."\n")
|
||||
local struct_closed_name = line:match(struct_closed_re)
|
||||
if struct_closed_name then
|
||||
table.insert(typedefs_table,"typedef struct "..struct_closed_name.." "..struct_closed_name..";\n")
|
||||
end
|
||||
end
|
||||
|
||||
if #structnames > 0 then
|
||||
if #structnames > 1 then --inner structs
|
||||
innerstructs[structnames[#structnames]] = innerstructs[structnames[#structnames]] or {}
|
||||
local st = innerstructs[structnames[#structnames]]
|
||||
-- if not line:match("struct") and not line:match("^{$") and not line:match(struct_closing_re) then --avoid tab in struct { and };
|
||||
--line = " "..line
|
||||
-- end
|
||||
st[#st + 1] = line
|
||||
if line:match(struct_closing_re) and not line:match(struct_op_close_re) then
|
||||
local structname = structnames[#structnames]
|
||||
--st[#st + 1] = string.format("typedef struct %s %s;\n",structname,structname)
|
||||
table.insert(typedefs_table,string.format("typedef struct %s %s;\n",structname,structname))
|
||||
structnames[#structnames] = nil
|
||||
end
|
||||
elseif line:match(struct_closing_re) and not line:match(struct_op_close_re) then
|
||||
local structname = structnames[#structnames]
|
||||
--table.insert(outtab,"typedef struct "..structname.." "..structname..";\n")
|
||||
table.insert(typedefs_table,"typedef struct "..structname.." "..structname..";\n")
|
||||
structnames[#structnames] = nil
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
until true
|
||||
end
|
||||
for k,v in pairs(innerstructs) do
|
||||
for i,line in ipairs(v) do
|
||||
table.insert(outtab,line.."\n")
|
||||
end
|
||||
end
|
||||
|
||||
table.insert(outtab,"//////////////// END AUTOGENERATED SEGMENT \n")
|
||||
for i,l in ipairs(typedefs_table) do
|
||||
table.insert(outtab,2,l)
|
||||
end
|
||||
return outtab
|
||||
end
|
||||
|
||||
local function func_header_impl_generate(FP)
|
||||
|
||||
local outtab = {}
|
||||
|
||||
for _,t in ipairs(FP.cdefs) do
|
||||
if t.cimguiname then
|
||||
local cimf = FP.defsT[t.cimguiname]
|
||||
local def = cimf[t.signature]
|
||||
if def.ret then --not constructor
|
||||
local addcoment = def.comment or ""
|
||||
if def.stname == "" then --ImGui namespace or top level
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..";"..addcoment.."\n")
|
||||
else
|
||||
error("class function in implementations")
|
||||
end
|
||||
end
|
||||
else --not cimguiname
|
||||
table.insert(outtab,t.comment:gsub("%%","%%%%").."\n")-- %% substitution for gsub
|
||||
end
|
||||
end
|
||||
--hfile:close()
|
||||
return outtab
|
||||
end
|
||||
local function func_header_generate(FP)
|
||||
|
||||
local outtab = {}
|
||||
table.insert(outtab,"#ifndef CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
|
||||
for k,v in pairs(FP.embeded_structs) do
|
||||
--print(k,v)
|
||||
table.insert(outtab,"typedef "..v.." "..k..";\n")
|
||||
end
|
||||
for k,v in pairs(FP.ImVector_templates) do
|
||||
table.insert(outtab,"typedef ImVector<"..k.."> ImVector_"..k..";\n")
|
||||
end
|
||||
table.insert(outtab,"#else //CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
|
||||
for k,v in pairs(FP.ImVector_templates) do
|
||||
table.insert(outtab,"typedef ImVector ImVector_"..k..";\n")
|
||||
end
|
||||
table.insert(outtab,"#endif //CIMGUI_DEFINE_ENUMS_AND_STRUCTS\n")
|
||||
for _,t in ipairs(FP.cdefs) do
|
||||
if t.cimguiname then
|
||||
local cimf = FP.defsT[t.cimguiname]
|
||||
local def = cimf[t.signature]
|
||||
local manual = get_manuals(def)
|
||||
if not manual and def.ret then --not constructor
|
||||
local addcoment = def.comment or ""
|
||||
if def.stname == "ImGui" or def.stname == "" then --ImGui namespace or top level
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..";"..addcoment.."\n")
|
||||
else
|
||||
local empty = def.args:match("^%(%)") --no args
|
||||
--local imgui_stname = embeded_structs[def.stname] or def.stname
|
||||
local imgui_stname = def.stname
|
||||
local args = def.args:gsub("^%(","("..imgui_stname.."* self"..(empty and "" or ","))
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..args..";"..addcoment.."\n")
|
||||
end
|
||||
end
|
||||
else --not cimguiname
|
||||
table.insert(outtab,t.comment:gsub("%%","%%%%").."\n")-- %% substitution for gsub
|
||||
end
|
||||
end
|
||||
--hfile:close()
|
||||
return outtab
|
||||
end
|
||||
local function func_implementation(FP)
|
||||
|
||||
local outtab = {}
|
||||
for _,t in ipairs(FP.cdefs) do
|
||||
repeat -- continue simulation
|
||||
if not t.cimguiname then break end
|
||||
local cimf = FP.defsT[t.cimguiname]
|
||||
local def = cimf[t.signature]
|
||||
local manual = get_manuals(def)
|
||||
if not manual and def.ret and def.stname~="" then --not constructor or manual or top level
|
||||
local ptret = def.retref and "&" or ""
|
||||
-- local castret = def.ret:gsub("[^%s]+",function(x)
|
||||
-- local y = x:gsub("%*","")
|
||||
-- local typ = embeded_structs[y]
|
||||
-- if typ then return "("..x..")" else return "" end
|
||||
-- end)
|
||||
local castret = ""
|
||||
if def.stname == "ImGui" then
|
||||
if def.isvararg then
|
||||
local call_args = def.call_args:gsub("%.%.%.","args")
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args.."\n")
|
||||
table.insert(outtab,"{\n")
|
||||
table.insert(outtab," va_list args;\n")
|
||||
table.insert(outtab," va_start(args, fmt);\n")
|
||||
table.insert(outtab," ImGui::"..def.funcname.."V"..call_args..";\n")
|
||||
table.insert(outtab," va_end(args);\n")
|
||||
--cppfile:write(" return ImGui::",def.funcname,def.call_args,";\n")
|
||||
table.insert(outtab,"}\n")
|
||||
else
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args.."\n")
|
||||
table.insert(outtab,"{\n")
|
||||
table.insert(outtab," return "..castret..ptret.."ImGui::"..def.funcname..def.call_args..";\n")
|
||||
table.insert(outtab,"}\n")
|
||||
end
|
||||
else
|
||||
local empty = def.args:match("^%(%)") --no args
|
||||
--local imgui_stname = embeded_structs[def.stname] or def.stname
|
||||
local imgui_stname = def.stname
|
||||
local args = def.args:gsub("^%(","("..imgui_stname.."* self"..(empty and "" or ","))
|
||||
if def.isvararg then
|
||||
local call_args = def.call_args:gsub("%.%.%.","args")
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..args.."\n")
|
||||
table.insert(outtab,"{\n")
|
||||
table.insert(outtab," va_list args;\n")
|
||||
table.insert(outtab," va_start(args, fmt);\n")
|
||||
table.insert(outtab," self->"..def.funcname.."V"..call_args..";\n")
|
||||
table.insert(outtab," va_end(args);\n")
|
||||
--cppfile:write(" return self->",def.funcname,def.call_args,";\n")
|
||||
table.insert(outtab,"}\n")
|
||||
else
|
||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..args.."\n")
|
||||
table.insert(outtab,"{\n")
|
||||
table.insert(outtab," return "..castret..ptret.."self->"..def.funcname..def.call_args..";\n")
|
||||
table.insert(outtab,"}\n")
|
||||
end
|
||||
end
|
||||
end
|
||||
until true
|
||||
end
|
||||
--cppfile:close()
|
||||
return outtab
|
||||
end
|
||||
--------------------------------------------------------
|
||||
-----------------------------do it----------------------
|
||||
--------------------------------------------------------
|
||||
print("USEGCC",USEGCC)
|
||||
|
||||
local pipe,err
|
||||
if USEGCC then
|
||||
pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ../../imgui/imgui.h]],"r")
|
||||
|
||||
if not pipe then
|
||||
error("could not execute gcc "..err)
|
||||
end
|
||||
else
|
||||
pipe,err = io.open("../../imgui/imgui.h","r")
|
||||
if not pipe then
|
||||
error("could not execute gcc "..err)
|
||||
end
|
||||
end
|
||||
print"goint to iterate"
|
||||
local STP = struct_parser()
|
||||
local FP = func_parser()
|
||||
|
||||
local iterator = (USEGCC and location) or filelines
|
||||
|
||||
for line in iterator(pipe,{"imgui"}) do
|
||||
local line, comment = split_comment(line)
|
||||
STP.insert(line,comment)
|
||||
FP.insert(line,comment)
|
||||
end
|
||||
pipe:close()
|
||||
|
||||
--output after insert
|
||||
-- local hfile = io.open("./outstructs.h","w")
|
||||
-- hfile:write(table.concat(STP.lines,"\n"))
|
||||
-- hfile:close()
|
||||
--do return end
|
||||
|
||||
FP:compute_overloads()
|
||||
|
||||
local cstructs = gen_structs_and_enums(STP.lines)
|
||||
local cfuncs = func_header_generate(FP)
|
||||
|
||||
--merge it in cimgui_template.h to cimgui.h
|
||||
local hfile = io.open("./cimgui_template.h","r")
|
||||
local hstrfile = hfile:read"*a"
|
||||
hfile:close()
|
||||
local cstructsstr = table.concat(cstructs)
|
||||
cstructsstr = cstructsstr:gsub("\n+","\n") --several empty lines to one empty line
|
||||
hstrfile = hstrfile:gsub([[#include "imgui_structs%.h"]],cstructsstr)
|
||||
local cfuncsstr = table.concat(cfuncs)
|
||||
cfuncsstr = cfuncsstr:gsub("\n+","\n") --several empty lines to one empty line
|
||||
hstrfile = hstrfile:gsub([[#include "auto_funcs%.h"]],cfuncsstr)
|
||||
save_data("./cimgui.h",hstrfile)
|
||||
|
||||
|
||||
--merge it in cimgui_template.cpp to cimgui.cpp
|
||||
local cimplem = func_implementation(FP)
|
||||
local hfile = io.open("./cimgui_template.cpp","r")
|
||||
local hstrfile = hfile:read"*a"
|
||||
hfile:close()
|
||||
hstrfile = hstrfile:gsub([[#include "auto_funcs%.cpp"]],table.concat(cimplem))
|
||||
save_data("./cimgui.cpp",hstrfile)
|
||||
|
||||
----------save fundefs in definitions.lua for using in bindings
|
||||
save_data("./definitions.lua",serializeTable("defs",FP.defsT).."\nreturn defs")
|
||||
|
||||
----------save struct and enums lua table in structs_and_enums.lua for using in bindings
|
||||
save_data("./structs_and_enums.lua",serializeTable("defs",gen_structs_and_enums_table(STP.lines)).."\nreturn defs")
|
||||
|
||||
--=================================Now implementations
|
||||
|
||||
|
||||
local iFP,iSTP
|
||||
|
||||
|
||||
if #implementations > 0 then
|
||||
|
||||
iFP = func_parser()
|
||||
iSTP = struct_parser()
|
||||
|
||||
for i,impl in ipairs(implementations) do
|
||||
local source = [[../../imgui/examples/imgui_impl_]].. impl .. ".h "
|
||||
local locati = [[imgui_impl_]].. impl
|
||||
local pipe,err
|
||||
if USEGCC then
|
||||
pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ]] ..source,"r")
|
||||
else
|
||||
pipe,err = io.open(source,"r")
|
||||
end
|
||||
if not pipe then
|
||||
error("could not get file: "..err)
|
||||
end
|
||||
|
||||
for line,locat in iterator(pipe,{locati}) do
|
||||
local line, comment = split_comment(line)
|
||||
iSTP.insert(line,comment)
|
||||
iFP.insert(line,comment,locat)
|
||||
end
|
||||
pipe:close()
|
||||
end
|
||||
|
||||
-- save ./cimgui_impl.h
|
||||
local impl_cfuncs = func_header_impl_generate(iFP)
|
||||
local impl_cstructs = gen_structs_and_enums(iSTP.lines)
|
||||
local cstructstr = table.concat(impl_cstructs)
|
||||
cstructstr = cstructstr:gsub("\n+","\n") --several empty lines to one empty line
|
||||
local cfuncsstr = table.concat(impl_cfuncs)
|
||||
cfuncsstr = cfuncsstr:gsub("\n+","\n") --several empty lines to one empty line
|
||||
save_data("./cimgui_impl.h",cstructstr..cfuncsstr)
|
||||
|
||||
----------save fundefs in impl_definitions.lua for using in bindings
|
||||
save_data("./impl_definitions.lua",serializeTable("defs",iFP.defsT).."\nreturn defs")
|
||||
|
||||
end -- #implementations > 0 then
|
||||
|
||||
-------------------------------json saving
|
||||
--avoid mixed tables (with string and integer keys)
|
||||
local function json_prepare(defs)
|
||||
--delete signatures in function
|
||||
for k,def in pairs(defs) do
|
||||
for k2,v in pairs(def) do
|
||||
if type(k2)=="string" then
|
||||
def[k2] = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
return defs
|
||||
end
|
||||
|
||||
local json = require"json"
|
||||
save_data("./definitions.json",json.encode(json_prepare(FP.defsT)))
|
||||
save_data("./structs_and_enums.json",json.encode(structs_and_enums_table))
|
||||
if iFP then
|
||||
save_data("./impl_definitions.json",json.encode(json_prepare(iFP.defsT)))
|
||||
end
|
||||
|
||||
print"all done!!"
|
||||
--[[
|
||||
---dump some infos-----------------------------------------------------------------------
|
||||
------------------------------------------------------------------------------------
|
||||
print"//-------alltypes--------------------------------------------------------------------"
|
||||
FP:dump_alltypes()
|
||||
print"//embeded_structs---------------------------------------------------------------------------"
|
||||
for k,v in pairs(FP.embeded_structs) do
|
||||
--print(k,v)
|
||||
io.write("typedef ",v," ",k,";\n")
|
||||
end
|
||||
print"//templates---------------------------------------------------------------------------"
|
||||
for k,v in pairs(FP.ImVector_templates) do
|
||||
--print(k,v)
|
||||
io.write("typedef ImVector<",k,"> ImVector_",k,";\n")
|
||||
end
|
||||
|
||||
print"//constructors------------------------------------------------------------------"
|
||||
for i,t in ipairs(FP.cdefs) do
|
||||
if t.cimguiname and not t.ret then
|
||||
print(t.cimguiname,"\t",t.signature,"\t",t.args,"\t",t.argsc,"\t",t.call_args,"\t",t.ret)
|
||||
end
|
||||
end
|
||||
print"//-------------------------------------------------------------------------------------"
|
||||
for i,t in ipairs(FP.cdefs) do
|
||||
--print(t.cimguiname," ",t.funcname,"\t",t.signature,"\t",t.args,"\t",t.argsc,"\t",t.call_args,"\t",t.ret)
|
||||
end
|
||||
---------------------------------------------------------------------------------------------
|
||||
--]]
|
Reference in New Issue
Block a user