keep function locations

keep locations
This commit is contained in:
Victor Bombi
2020-06-05 12:26:27 +02:00
parent 981eafaa3e
commit 0f2a41e273
2 changed files with 50 additions and 15 deletions

View File

@@ -280,7 +280,8 @@ local function getRE()
end end
M.getRE = getRE M.getRE = getRE
--takes preprocesed file in table cdefsor and returns items --takes preprocesed file in table cdefsor and returns items
local function parseItems(txt,dumpit) local function parseItems(txt,dumpit,loca)
--dumpit = true --dumpit = true
local res,resN = getRE() local res,resN = getRE()
@@ -297,7 +298,7 @@ local function parseItems(txt,dumpit)
item = txt:sub(i,e) item = txt:sub(i,e)
--if re~=functionD_re then --skip defined functions --if re~=functionD_re then --skip defined functions
item = item:gsub("extern __attribute__%(%(dllexport%)%) ","") item = item:gsub("extern __attribute__%(%(dllexport%)%) ","")
table.insert(itemarr,{re_name=re_name,item=item}) table.insert(itemarr,{re_name=re_name,item=item,locat=loca})
--end --end
items[re_name] = items[re_name] or {} items[re_name] = items[re_name] or {}
table.insert(items[re_name],item) table.insert(items[re_name],item)
@@ -412,7 +413,8 @@ local function typetoStr(typ)
typ = typ:gsub("[<>]","") typ = typ:gsub("[<>]","")
return typ return typ
end end
local function parseFunction(self,stname,lineorig,namespace) local function parseFunction(self,stname,lineorig,namespace,locat)
line = clean_spaces(lineorig) line = clean_spaces(lineorig)
--move * --move *
line = line:gsub("%s*%*","%*") line = line:gsub("%s*%*","%*")
@@ -769,8 +771,8 @@ function M.Parser()
par.manuals = {} par.manuals = {}
par.UDTs = {} par.UDTs = {}
function par:insert(line) function par:insert(line,loca)
table.insert(cdefs,line) table.insert(cdefs,{line,loca})
end end
function par.getCname(stname,funcname, namespace) function par.getCname(stname,funcname, namespace)
if #stname == 0 then return funcname end --top level if #stname == 0 then return funcname end --top level
@@ -795,7 +797,8 @@ function M.Parser()
end end
function par:parseItems() function par:parseItems()
--typedefs dictionary --typedefs dictionary
for i,line in ipairs(cdefs) do for i,cdef in ipairs(cdefs) do
local line = cdef[1]
if line:match("typedef") then if line:match("typedef") then
line = clean_spaces(line) line = clean_spaces(line)
local value,key = line:match("typedef%s+(.+)%s+([%w_]+);") local value,key = line:match("typedef%s+(.+)%s+([%w_]+);")
@@ -815,8 +818,26 @@ function M.Parser()
end end
end end
end end
local txt = table.concat(cdefs,"\n")
itemsarr,items = parseItems(txt) itemsarr = {}
if self.separate_locations then
local located_cdefs = self:separate_locations(cdefs)
for i,lcdef in ipairs(located_cdefs) do
local txt = table.concat(lcdef[2],"\n")
local itemsarrT,itemsT = parseItems(txt,false,lcdef[1])
for i,it in ipairs(itemsarrT) do
table.insert(itemsarr,it)
end
end
else
local cdefs2 = {}
for i,cdef in ipairs(cdefs) do
table.insert(cdefs2,cdef[1])
end
local txt = table.concat(cdefs2,"\n")
itemsarr,items = parseItems(txt,false)
end
self.itemsarr , self.items = itemsarr,items self.itemsarr , self.items = itemsarr,items
end end
function par:printItems() function par:printItems()
@@ -828,14 +849,14 @@ function M.Parser()
function par:parseFunctions() function par:parseFunctions()
for i,it in ipairs(itemsarr) do for i,it in ipairs(itemsarr) do
if it.re_name == "function_re" or it.re_name == "functionD_re" then if it.re_name == "function_re" or it.re_name == "functionD_re" then
self:parseFunction("",it.item) self:parseFunction("",it.item,nil,it.locat)
elseif it.re_name == "namespace_re" then elseif it.re_name == "namespace_re" then
local nsp = it.item:match("%b{}"):sub(2,-2) local nsp = it.item:match("%b{}"):sub(2,-2)
local namespace = it.item:match("namespace%s+(%S+)") local namespace = it.item:match("namespace%s+(%S+)")
local nspparr,itemsnsp = parseItems(nsp) local nspparr,itemsnsp = parseItems(nsp,false,it.locat)
for insp,itnsp in ipairs(nspparr) do for insp,itnsp in ipairs(nspparr) do
if itnsp.re_name == "function_re" or itnsp.re_name == "functionD_re" then if itnsp.re_name == "function_re" or itnsp.re_name == "functionD_re" then
self:parseFunction("",itnsp.item,namespace) self:parseFunction("",itnsp.item,namespace,itnsp.locat)
end end
end end
elseif it.re_name == "struct_re" then elseif it.re_name == "struct_re" then
@@ -847,20 +868,20 @@ function M.Parser()
self.typenames = self.typenames or {} self.typenames = self.typenames or {}
self.typenames[stname] = typename self.typenames[stname] = typename
end end
local nspparr,itemsnsp = parseItems(nsp) local nspparr,itemsnsp = parseItems(nsp,false,it.locat)
for insp,itnsp in ipairs(nspparr) do for insp,itnsp in ipairs(nspparr) do
if itnsp.re_name == "function_re" or itnsp.re_name == "functionD_re" then if itnsp.re_name == "function_re" or itnsp.re_name == "functionD_re" then
self:parseFunction(stname,itnsp.item) self:parseFunction(stname,itnsp.item,nil,itnsp.locat)
elseif itnsp.re_name == "struct_re" then elseif itnsp.re_name == "struct_re" then
--get embeded_structs --get embeded_structs
local embededst = itnsp.item:match("struct%s+(%S+)") local embededst = itnsp.item:match("struct%s+(%S+)")
self.embeded_structs[embededst] = stname.."::"..embededst self.embeded_structs[embededst] = stname.."::"..embededst
local nsp2 = strip_end(itnsp.item:match("%b{}"):sub(2,-2)) local nsp2 = strip_end(itnsp.item:match("%b{}"):sub(2,-2))
local itemsemarr,itemsem = parseItems(nsp2) local itemsemarr,itemsem = parseItems(nsp2,false,itnsp.locat)
assert(not itemsem.struct_re,"two level embed struct") assert(not itemsem.struct_re,"two level embed struct")
for iemb,itemb in ipairs(itemsemarr) do for iemb,itemb in ipairs(itemsemarr) do
if itemb.re_name == "function_re" or itemb.re_name == "functionD_re" then if itemb.re_name == "function_re" or itemb.re_name == "functionD_re" then
self:parseFunction(embededst,itemb.item) self:parseFunction(embededst,itemb.item,nil,itemb.locat)
end end
end end
end end

View File

@@ -428,6 +428,20 @@ gdefines = get_defines{"IMGUI_VERSION","FLT_MAX"}
local function parseImGuiHeader(header,names) local function parseImGuiHeader(header,names)
--prepare parser --prepare parser
local parser = cpp2ffi.Parser() local parser = cpp2ffi.Parser()
parser.separate_locations = function(self,cdefs)
local imguicdefs = {}
local othercdefs = {}
for i,cdef in ipairs(cdefs) do
if cdef[2]=="imgui" then
table.insert(imguicdefs,cdef[1])
else
table.insert(othercdefs,cdef[1])
end
end
return {{"imgui",imguicdefs},{"internal",othercdefs}}
end
parser.getCname = function(stname,funcname,namespace) parser.getCname = function(stname,funcname,namespace)
local pre = (stname == "") and (namespace and (namespace=="ImGui" and "ig" or namespace.."_") or "ig") or stname.."_" local pre = (stname == "") and (namespace and (namespace=="ImGui" and "ig" or namespace.."_") or "ig") or stname.."_"
return pre..funcname return pre..funcname