cpp2ffi and generator: allow parsing of header lines

This commit is contained in:
Victor Bombi
2020-09-24 16:21:11 +02:00
parent 3270e6b0f2
commit abe5b56fd6
2 changed files with 18 additions and 10 deletions

View File

@@ -297,7 +297,7 @@ local function isLeaf(re)
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,loca) local function parseItems(txt,dumpit,loca,linenumdict)
--assert(loca) --assert(loca)
--dumpit = true --dumpit = true
local res,resN = getRE() local res,resN = getRE()
@@ -349,6 +349,10 @@ local function parseItems(txt,dumpit,loca)
local comments = table.concat(outercomms,"\n") --..inercoms local comments = table.concat(outercomms,"\n") --..inercoms
if comments=="" then comments=nil end if comments=="" then comments=nil end
outercomms = {} outercomms = {}
if linenumdict then
local itemfirstline = item:match("[^\n]+")
loca = linenumdict[itemfirstline]
end
table.insert(itemarr,{re_name=re_name,item=item,locat=loca})--,comments=comments}) table.insert(itemarr,{re_name=re_name,item=item,locat=loca})--,comments=comments})
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)
@@ -951,12 +955,12 @@ function M.Parser()
end end
end end
--recursive item parsing --recursive item parsing
function par:parseItemsR2(txt,doprint,locat) function par:parseItemsR2(txt,doprint,locat,linenumdict)
local itsarr,its = parseItems(txt,false,locat) local itsarr,its = parseItems(txt,false,locat,linenumdict)
for i,it in ipairs(itsarr) do for i,it in ipairs(itsarr) do
if not isLeaf(it.re_name) then if not isLeaf(it.re_name) then
local inner = strip_end(it.item:match("%b{}"):sub(2,-2)) local inner = strip_end(it.item:match("%b{}"):sub(2,-2))
it.childs = par:parseItemsR2(inner,doprint,locat) it.childs = par:parseItemsR2(inner,doprint,locat,linenumdict)
for j,child in ipairs(it.childs) do for j,child in ipairs(it.childs) do
child.parent = it child.parent = it
end end
@@ -991,14 +995,17 @@ function M.Parser()
self.itemsarr = all_itemsarr self.itemsarr = all_itemsarr
itemsarr = self.itemsarr itemsarr = self.itemsarr
else else
self.linenumdict = {}
local cdefs2 = {} local cdefs2 = {}
for i,cdef in ipairs(cdefs) do for i,cdef in ipairs(cdefs) do
self.linenumdict[cdef[1]]=cdef[2]
table.insert(cdefs2,cdef[1]) table.insert(cdefs2,cdef[1])
end end
local txt = table.concat(cdefs2,"\n") local txt = table.concat(cdefs2,"\n")
self.itemsarr = par:parseItemsR2(txt) self.itemsarr = par:parseItemsR2(txt,false,nil,self.linenumdict)
itemsarr = self.itemsarr itemsarr = self.itemsarr
end end
end end
@@ -1609,9 +1616,8 @@ local function location(file,locpathT,defines,COMPILER)
return nil return nil
end end
end end
if #line==0 then --nothing on emptyline
elseif not line:match("%S") then --nothing if only spaces if line:sub(1,1) == "#" then
elseif line:sub(1,1) == "#" then
--elseif line:match"^%s*#" then --elseif line:match"^%s*#" then
-- Is this a location pragma? -- Is this a location pragma?
local loc_num_t,location_match = line:match(location_re) local loc_num_t,location_match = line:match(location_re)
@@ -1641,6 +1647,7 @@ local function location(file,locpathT,defines,COMPILER)
local loc_num_real = loc_num + loc_num_incr local loc_num_real = loc_num + loc_num_incr
loc_num_incr = loc_num_incr + 1 loc_num_incr = loc_num_incr + 1
--if doprint then print(which_locationold,which_location) end --if doprint then print(which_locationold,which_location) end
if line:match("%S") then --nothing on emptyline
if (which_locationold~=which_location) or (loc_num_realold and loc_num_realold < loc_num_real) then if (which_locationold~=which_location) or (loc_num_realold and loc_num_realold < loc_num_real) then
--old line complete --old line complete
--doprint = false --doprint = false
@@ -1652,6 +1659,7 @@ local function location(file,locpathT,defines,COMPILER)
which_locationold,loc_num_realold = which_location,loc_num_real which_locationold,loc_num_realold = which_location,loc_num_real
--return line,loc_num_real, which_location --return line,loc_num_real, which_location
end end
end
end end
until false --forever until false --forever
end end

View File

@@ -360,7 +360,7 @@ local function parseImGuiHeader(header,names)
--prepare parser --prepare parser
local parser = cpp2ffi.Parser() local parser = cpp2ffi.Parser()
parser.separate_locations = function(self,cdefs) parser.separate_locationsNO = function(self,cdefs)
local imguicdefs = {} local imguicdefs = {}
local othercdefs = {} local othercdefs = {}
for i,cdef in ipairs(cdefs) do for i,cdef in ipairs(cdefs) do
@@ -399,7 +399,7 @@ local function parseImGuiHeader(header,names)
cpp2ffi.save_data("cdefs1.lua",table.concat(tableo,"\n")) cpp2ffi.save_data("cdefs1.lua",table.concat(tableo,"\n"))
--]] --]]
for line,loca,loca2 in iterator(pipe,names,{},COMPILER) do for line,loca,loca2 in iterator(pipe,names,{},COMPILER) do
parser:insert(line, loca) parser:insert(line, tostring(loca)..":"..tostring(loca2))
--table.insert(tableo,line) --table.insert(tableo,line)
--print(loca,loca2) --print(loca,loca2)
end end