gen with comments

This commit is contained in:
sonoro1234
2018-06-15 14:24:25 +02:00
parent fa5d113cdb
commit 017b935afd
2 changed files with 95 additions and 16 deletions

View File

@@ -3,7 +3,11 @@ rem set your PATH if necessary for gcc and lua with:
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
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 -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

View File

@@ -60,6 +60,46 @@ local function location(file,locpath)
return location_it
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)
return cad:gsub("^%s*(.-)%s*$","%1") --remove initial and final spaces
@@ -93,6 +133,7 @@ local function struct_parser()
local function_closed_re = "[;}]$"
local operator_re = "operator.-%b()"
local functype_re = "(%(%*)[%w_]+(%)%([^%(%)]*%))"
local initial_comment_re = [[^%s*//.*]]
local in_functionst = false
local structcdefs = {}
@@ -100,27 +141,43 @@ local function struct_parser()
STP.lines = structcdefs
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_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
elseif line:match("public:") then
--nothing
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
table.insert(structcdefs,linea)
--]]
end
return
end
@@ -227,7 +284,7 @@ local function func_parser()
FP.embeded_structs = embeded_structs
FP.defsT = defsT
FP.ImVector_templates = ImVector_templates
function FP.insert(line)
function FP.insert(line,comment)
line = clean_spaces(line)
if line:match"template" then return end
line = line:gsub("%S+",{class="struct",mutable=""}) --class -> struct
@@ -313,13 +370,14 @@ local function func_parser()
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})
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%s%*_]+)=([%w_%(%)%s,%*]+)[,%)]") do
for k,def in args:gmatch("([%w_]+)=([%w_%(%)%s,%*]+)[,%)]") do
defT.defaults[k]=def
end
defT.cimguiname = cimguiname
@@ -329,6 +387,7 @@ local function func_parser()
defT.signature = signature
defT.call_args = call_args
defT.isvararg = signature:match("%.%.%.%)$")
defT.comment = comment
if ret then
defT.ret = ret:gsub("&","*")
defT.retref = ret:match("&")
@@ -383,16 +442,17 @@ local function gen_structs_and_enums(cdefs)
local struct_op_close_re = "%b{}"
local structnames = {}
local innerstructs = {}
--local hfile = io.open("./imgui_structs2.h","w")
local outtab = {}
-- Output the file
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
repeat -- simulating continue with break
local linecom = line
line = line:gsub("%s*//.*","")
if line:match(namespace_re) then
in_namespace = true
end
@@ -400,7 +460,7 @@ local function gen_structs_and_enums(cdefs)
if structbegin then
structnames[#structnames + 1] = structbegin
if #structnames < 2 and structbegin~= "ImVector" then --not inner and not ImVector
table.insert(outtab,line.."\n")
table.insert(outtab,linecom.."\n")
break
end
end
@@ -434,7 +494,7 @@ typedef struct ImVector ImVector;]])
--line = " "..line
end
end
table.insert(outtab,line.."\n")
table.insert(outtab,linecom.."\n")
local struct_closed_name = line:match(struct_closed_re)
if struct_closed_name then
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 manual = get_manuals(def)
if not manual and def.ret then --not constructor
local addcoment = def.comment or ""
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
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..";\n")
table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..args..addcoment..";\n")
end
end
end
@@ -577,9 +638,18 @@ local FP = func_parser()
for line in location(io.input(),"imgui") do
STP.insert(line)
FP.insert(line)
local linecom = line
local comment = line:match("(%s*//.*)") or ""
line = line:gsub("%s*//.*","")
FP.insert(line,comment)
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()
local cstructs = gen_structs_and_enums(STP.lines)
@@ -606,6 +676,11 @@ local outfile = io.open("./cimgui.cpp","w")
outfile:write(hstrfile)
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-----------------------------------------------------------------------