mirror of
https://github.com/cimgui/cimgui.git
synced 2025-08-18 15:08:31 +01:00
gen with comments
This commit is contained in:
@@ -3,7 +3,11 @@ rem set your PATH if necessary for gcc and lua with:
|
|||||||
set PATH=%PATH%;C:\mingw32\bin;C:\luaGL;
|
set PATH=%PATH%;C:\mingw32\bin;C:\luaGL;
|
||||||
|
|
||||||
rem gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h | luajit.exe ./generator.lua > out.txt
|
rem gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h | luajit.exe ./generator.lua > out.txt
|
||||||
gcc -E -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h | luajit.exe ./generator.lua > out.txt
|
rem gcc -E -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h | luajit.exe ./generator.lua > out.txt
|
||||||
rem gcc -E -CC -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h > imgui_structs2.raw
|
rem gcc -E -CC -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h > imgui_structs2.raw
|
||||||
|
rem gcc -E -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h > 1.txt
|
||||||
|
rem gcc -E -CC -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h > 1CC.txt
|
||||||
|
rem gcc -E -P -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS ../../imgui/imgui.h > 1P.txt
|
||||||
|
type 1.txt | luajit.exe ./generator.lua > out.txt
|
||||||
cmd /k
|
cmd /k
|
||||||
|
|
||||||
|
@@ -60,6 +60,46 @@ local function location(file,locpath)
|
|||||||
return location_it
|
return location_it
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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 strip(cad)
|
local function strip(cad)
|
||||||
return cad:gsub("^%s*(.-)%s*$","%1") --remove initial and final spaces
|
return cad:gsub("^%s*(.-)%s*$","%1") --remove initial and final spaces
|
||||||
@@ -93,6 +133,7 @@ local function struct_parser()
|
|||||||
local function_closed_re = "[;}]$"
|
local function_closed_re = "[;}]$"
|
||||||
local operator_re = "operator.-%b()"
|
local operator_re = "operator.-%b()"
|
||||||
local functype_re = "(%(%*)[%w_]+(%)%([^%(%)]*%))"
|
local functype_re = "(%(%*)[%w_]+(%)%([^%(%)]*%))"
|
||||||
|
local initial_comment_re = [[^%s*//.*]]
|
||||||
|
|
||||||
local in_functionst = false
|
local in_functionst = false
|
||||||
local structcdefs = {}
|
local structcdefs = {}
|
||||||
@@ -100,27 +141,43 @@ local function struct_parser()
|
|||||||
STP.lines = structcdefs
|
STP.lines = structcdefs
|
||||||
function STP.insert(line)
|
function STP.insert(line)
|
||||||
|
|
||||||
|
--drop initial comments
|
||||||
|
if line:match(initial_comment_re) then
|
||||||
|
--print("coment:",line)
|
||||||
|
return
|
||||||
|
end
|
||||||
|
|
||||||
|
local linecommented = line
|
||||||
|
line = line:gsub("%s*//.*","")
|
||||||
|
|
||||||
--if in_function discard
|
--if in_function discard
|
||||||
if in_functionst then
|
if in_functionst then
|
||||||
if line:match(function_closing_re) then
|
if line:match(function_closing_re) then
|
||||||
in_functionst = false
|
in_functionst = false
|
||||||
|
print("in function:",line)
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if (line:match(function_re) or line:match(operator_re)) and not line:match("typedef.*%b().*%b().*")
|
if (line:match(function_re) or line:match(operator_re)) and not line:match("typedef.*%b().*%b().*")
|
||||||
and not line:match(functype_re) then
|
and not line:match(functype_re) then
|
||||||
if not line:match(function_closed_re) then
|
if not line:match(function_closed_re) then
|
||||||
|
print("match:",line)
|
||||||
in_functionst = true
|
in_functionst = true
|
||||||
end
|
end
|
||||||
|
--else
|
||||||
|
--table.insert(structcdefs,linecommented)
|
||||||
elseif line:match("template") then
|
elseif line:match("template") then
|
||||||
--nothing
|
--nothing
|
||||||
elseif line:match("public") then
|
elseif line:match("public:") then
|
||||||
--nothing
|
--nothing
|
||||||
else
|
else
|
||||||
local linea = line:gsub("%S+",{class="struct",mutable=""})
|
local linea = linecommented:gsub("%S+",{class="struct",mutable=""})
|
||||||
linea = linea:gsub("(%b<>)","/%*%1%*/") --comment template parameters
|
linea = linea:gsub("(%b<>)","/%*%1%*/") --comment template parameters
|
||||||
table.insert(structcdefs,linea)
|
table.insert(structcdefs,linea)
|
||||||
|
--]]
|
||||||
end
|
end
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
@@ -227,7 +284,7 @@ local function func_parser()
|
|||||||
FP.embeded_structs = embeded_structs
|
FP.embeded_structs = embeded_structs
|
||||||
FP.defsT = defsT
|
FP.defsT = defsT
|
||||||
FP.ImVector_templates = ImVector_templates
|
FP.ImVector_templates = ImVector_templates
|
||||||
function FP.insert(line)
|
function FP.insert(line,comment)
|
||||||
line = clean_spaces(line)
|
line = clean_spaces(line)
|
||||||
if line:match"template" then return end
|
if line:match"template" then return end
|
||||||
line = line:gsub("%S+",{class="struct",mutable=""}) --class -> struct
|
line = line:gsub("%S+",{class="struct",mutable=""}) --class -> struct
|
||||||
@@ -313,13 +370,14 @@ local function func_parser()
|
|||||||
end
|
end
|
||||||
|
|
||||||
local cimguiname = getcimguiname(stname,funcname)
|
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})
|
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 {}
|
defsT[cimguiname] = defsT[cimguiname] or {}
|
||||||
table.insert(defsT[cimguiname],{})
|
table.insert(defsT[cimguiname],{})
|
||||||
local defT = defsT[cimguiname][#defsT[cimguiname]]
|
local defT = defsT[cimguiname][#defsT[cimguiname]]
|
||||||
defT.defaults = {}
|
defT.defaults = {}
|
||||||
for k,def in args:gmatch("([%w%s%*]+)=([%w%(%)%s,%*]+)[,%)]") do
|
--for k,def in args:gmatch("([%w%s%*_]+)=([%w_%(%)%s,%*]+)[,%)]") do
|
||||||
|
for k,def in args:gmatch("([%w_]+)=([%w_%(%)%s,%*]+)[,%)]") do
|
||||||
defT.defaults[k]=def
|
defT.defaults[k]=def
|
||||||
end
|
end
|
||||||
defT.cimguiname = cimguiname
|
defT.cimguiname = cimguiname
|
||||||
@@ -329,6 +387,7 @@ local function func_parser()
|
|||||||
defT.signature = signature
|
defT.signature = signature
|
||||||
defT.call_args = call_args
|
defT.call_args = call_args
|
||||||
defT.isvararg = signature:match("%.%.%.%)$")
|
defT.isvararg = signature:match("%.%.%.%)$")
|
||||||
|
defT.comment = comment
|
||||||
if ret then
|
if ret then
|
||||||
defT.ret = ret:gsub("&","*")
|
defT.ret = ret:gsub("&","*")
|
||||||
defT.retref = ret:match("&")
|
defT.retref = ret:match("&")
|
||||||
@@ -383,16 +442,17 @@ local function gen_structs_and_enums(cdefs)
|
|||||||
local struct_op_close_re = "%b{}"
|
local struct_op_close_re = "%b{}"
|
||||||
local structnames = {}
|
local structnames = {}
|
||||||
local innerstructs = {}
|
local innerstructs = {}
|
||||||
--local hfile = io.open("./imgui_structs2.h","w")
|
|
||||||
local outtab = {}
|
local outtab = {}
|
||||||
-- Output the file
|
-- Output the file
|
||||||
table.insert(outtab,"/////////////// BEGIN AUTOGENERATED SEGMENT\n")
|
table.insert(outtab,"/////////////// BEGIN AUTOGENERATED SEGMENT\n")
|
||||||
--table.insert(outtab,"#ifndef IMGUI_STRUCTS_INCLUDED\n")
|
|
||||||
--table.insert(outtab,"#define IMGUI_STRUCTS_INCLUDED\n")
|
|
||||||
|
|
||||||
for i,line in ipairs(cdefs) do
|
for i,line in ipairs(cdefs) do
|
||||||
repeat -- simulating continue with break
|
repeat -- simulating continue with break
|
||||||
|
local linecom = line
|
||||||
|
line = line:gsub("%s*//.*","")
|
||||||
|
|
||||||
if line:match(namespace_re) then
|
if line:match(namespace_re) then
|
||||||
in_namespace = true
|
in_namespace = true
|
||||||
end
|
end
|
||||||
@@ -400,7 +460,7 @@ local function gen_structs_and_enums(cdefs)
|
|||||||
if structbegin then
|
if structbegin then
|
||||||
structnames[#structnames + 1] = structbegin
|
structnames[#structnames + 1] = structbegin
|
||||||
if #structnames < 2 and structbegin~= "ImVector" then --not inner and not ImVector
|
if #structnames < 2 and structbegin~= "ImVector" then --not inner and not ImVector
|
||||||
table.insert(outtab,line.."\n")
|
table.insert(outtab,linecom.."\n")
|
||||||
break
|
break
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -434,7 +494,7 @@ typedef struct ImVector ImVector;]])
|
|||||||
--line = " "..line
|
--line = " "..line
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
table.insert(outtab,line.."\n")
|
table.insert(outtab,linecom.."\n")
|
||||||
local struct_closed_name = line:match(struct_closed_re)
|
local struct_closed_name = line:match(struct_closed_re)
|
||||||
if struct_closed_name then
|
if struct_closed_name then
|
||||||
table.insert(outtab,"typedef struct "..struct_closed_name.." "..struct_closed_name..";\n")
|
table.insert(outtab,"typedef struct "..struct_closed_name.." "..struct_closed_name..";\n")
|
||||||
@@ -496,14 +556,15 @@ local function func_header_generate(FP)
|
|||||||
local def = cimf[t.signature]
|
local def = cimf[t.signature]
|
||||||
local manual = get_manuals(def)
|
local manual = get_manuals(def)
|
||||||
if not manual and def.ret then --not constructor
|
if not manual and def.ret then --not constructor
|
||||||
|
local addcoment = def.comment or ""
|
||||||
if def.stname == "ImGui" then
|
if def.stname == "ImGui" then
|
||||||
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..";\n")
|
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..addcoment..";\n")
|
||||||
else
|
else
|
||||||
local empty = def.args:match("^%(%)") --no args
|
local empty = def.args:match("^%(%)") --no args
|
||||||
--local imgui_stname = embeded_structs[def.stname] or def.stname
|
--local imgui_stname = embeded_structs[def.stname] or def.stname
|
||||||
local imgui_stname = def.stname
|
local imgui_stname = def.stname
|
||||||
local args = def.args:gsub("^%(","("..imgui_stname.."* self"..(empty and "" or ","))
|
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..";\n")
|
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..args..addcoment..";\n")
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -577,9 +638,18 @@ local FP = func_parser()
|
|||||||
|
|
||||||
for line in location(io.input(),"imgui") do
|
for line in location(io.input(),"imgui") do
|
||||||
STP.insert(line)
|
STP.insert(line)
|
||||||
FP.insert(line)
|
local linecom = line
|
||||||
|
local comment = line:match("(%s*//.*)") or ""
|
||||||
|
line = line:gsub("%s*//.*","")
|
||||||
|
FP.insert(line,comment)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--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()
|
FP:compute_overloads()
|
||||||
|
|
||||||
local cstructs = gen_structs_and_enums(STP.lines)
|
local cstructs = gen_structs_and_enums(STP.lines)
|
||||||
@@ -606,6 +676,11 @@ local outfile = io.open("./cimgui.cpp","w")
|
|||||||
outfile:write(hstrfile)
|
outfile:write(hstrfile)
|
||||||
outfile:close()
|
outfile:close()
|
||||||
|
|
||||||
|
----------save defs
|
||||||
|
local hfile = io.open("./definitions.lua","w")
|
||||||
|
local ser = serializeTable("defs",FP.defsT)
|
||||||
|
hfile:write(ser.."\nreturn defs")
|
||||||
|
hfile:close()
|
||||||
|
|
||||||
|
|
||||||
---dump infos-----------------------------------------------------------------------
|
---dump infos-----------------------------------------------------------------------
|
||||||
|
Reference in New Issue
Block a user