generate structs_and_enums.lua file to help in bindings.

This commit is contained in:
sonoro1234
2018-06-20 19:49:09 +02:00
parent 4be6a54634
commit 5df4f3a42a
3 changed files with 111 additions and 7 deletions

View File

@@ -4,6 +4,7 @@
This is a thin c-api wrapper programmatically generated for the excellent C++ intermediate gui [imgui](https://github.com/ocornut/imgui).
All functions are programmatically wrapped except contructors, destructors and ImVector.(Unless someone find a use case for them)
Generated files are: cimgui.cpp, cimgui.h for C compilation. Also for helping in bindings creation, definitions.lua with function definitions information and structs_and_enums.lua.
This library is intended as a intermediate layer to be able to use imgui from other languages that can interface with C (like D - see [D-binding](https://github.com/Extrawurst/DerelictImgui))
Notes:

View File

@@ -1,4 +1,6 @@
:: this is used to rebuild cimgui.h and cimgui.cpp and must be executed in this directory
:: also provides definitions.lua for function definitions
:: and structs_and_enums.lua with struct and enum information-definitions
:: set your PATH if necessary for gcc and lua 5.1 or luajit with:
set PATH=%PATH%;C:\mingw32\bin;C:\luaGL;
@@ -10,7 +12,7 @@ set PATH=%PATH%;C:\mingw32\bin;C:\luaGL;
:: gcc -E -P no #pragma location information
::generate preprocessed file
gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_APIX="" ../../imgui/imgui.h ../../imgui/examples/imgui_impl_glfw.h ../../imgui/examples/imgui_impl_opengl3.h > 1.txt
gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" ../../imgui/imgui.h ../../imgui/examples/imgui_impl_glfw.h ../../imgui/examples/imgui_impl_opengl3.h > 1.txt
::process preprocessed file
type 1.txt | luajit.exe ./generator.lua true imgui imgui_impl_glfw imgui_impl_opengl3 > out.txt

View File

@@ -108,7 +108,7 @@ local function location(file,locpathT)
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)
@@ -485,6 +485,101 @@ local function func_parser()
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"
@@ -718,16 +813,16 @@ end
--output after insert
--local hfile = io.open("./outstructs.h","w")
--hfile:write(table.concat(STP.lines,"\n"))
--hfile:close()
-- 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)
local cstructs_table = gen_structs_and_enums_table(STP.lines)
--merge it in cimgui_template.h to cimgui.h
local hfile = io.open("./cimgui_template.h","r")
@@ -760,9 +855,15 @@ local ser = serializeTable("defs",FP.defsT)
hfile:write(ser.."\nreturn defs")
hfile:close()
----------save struct and enums lua table in structs_and_enums.lua for using in bindings
local hfile = io.open("./structs_and_enums.lua","w")
local ser = serializeTable("defs",cstructs_table)
hfile:write(ser.."\nreturn defs")
hfile:close()
---dump infos-----------------------------------------------------------------------
---dump some infos-----------------------------------------------------------------------
------------------------------------------------------------------------------------
print"//-------alltypes--------------------------------------------------------------------"
FP:dump_alltypes()