From abe5b56fd6ece7bb7f4cc397a798e2aea7edce69 Mon Sep 17 00:00:00 2001 From: Victor Bombi Date: Thu, 24 Sep 2020 16:21:11 +0200 Subject: [PATCH] cpp2ffi and generator: allow parsing of header lines --- generator/cpp2ffi.lua | 24 ++++++++++++++++-------- generator/generator.lua | 4 ++-- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/generator/cpp2ffi.lua b/generator/cpp2ffi.lua index 21a7459..f4f5669 100644 --- a/generator/cpp2ffi.lua +++ b/generator/cpp2ffi.lua @@ -297,7 +297,7 @@ local function isLeaf(re) end M.getRE = getRE --takes preprocesed file in table cdefsor and returns items -local function parseItems(txt,dumpit,loca) +local function parseItems(txt,dumpit,loca,linenumdict) --assert(loca) --dumpit = true local res,resN = getRE() @@ -349,6 +349,10 @@ local function parseItems(txt,dumpit,loca) local comments = table.concat(outercomms,"\n") --..inercoms if comments=="" then comments=nil end 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}) items[re_name] = items[re_name] or {} table.insert(items[re_name],item) @@ -951,12 +955,12 @@ function M.Parser() end end --recursive item parsing - function par:parseItemsR2(txt,doprint,locat) - local itsarr,its = parseItems(txt,false,locat) + function par:parseItemsR2(txt,doprint,locat,linenumdict) + local itsarr,its = parseItems(txt,false,locat,linenumdict) for i,it in ipairs(itsarr) do if not isLeaf(it.re_name) then 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 child.parent = it end @@ -991,14 +995,17 @@ function M.Parser() self.itemsarr = all_itemsarr itemsarr = self.itemsarr else + self.linenumdict = {} local cdefs2 = {} for i,cdef in ipairs(cdefs) do + self.linenumdict[cdef[1]]=cdef[2] table.insert(cdefs2,cdef[1]) end local txt = table.concat(cdefs2,"\n") - self.itemsarr = par:parseItemsR2(txt) + self.itemsarr = par:parseItemsR2(txt,false,nil,self.linenumdict) itemsarr = self.itemsarr + end end @@ -1609,9 +1616,8 @@ local function location(file,locpathT,defines,COMPILER) return nil end end - if #line==0 then --nothing on emptyline - elseif not line:match("%S") then --nothing if only spaces - elseif line:sub(1,1) == "#" then + + if line:sub(1,1) == "#" then --elseif line:match"^%s*#" then -- Is this a location pragma? 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 loc_num_incr = loc_num_incr + 1 --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 --old line complete --doprint = false @@ -1652,6 +1659,7 @@ local function location(file,locpathT,defines,COMPILER) which_locationold,loc_num_realold = which_location,loc_num_real --return line,loc_num_real, which_location end + end end until false --forever end diff --git a/generator/generator.lua b/generator/generator.lua index a719d11..3a9d6fa 100644 --- a/generator/generator.lua +++ b/generator/generator.lua @@ -360,7 +360,7 @@ local function parseImGuiHeader(header,names) --prepare parser local parser = cpp2ffi.Parser() - parser.separate_locations = function(self,cdefs) + parser.separate_locationsNO = function(self,cdefs) local imguicdefs = {} local othercdefs = {} 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")) --]] 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) --print(loca,loca2) end