From ac0aaba306f2db5bb1c5508377030ff4ba3555aa Mon Sep 17 00:00:00 2001 From: sonoro1234 Date: Mon, 25 Jun 2018 16:47:01 +0200 Subject: [PATCH] add json saving --- cimgui/generator/generator.lua | 106 +++++---- cimgui/generator/json.lua | 400 +++++++++++++++++++++++++++++++++ 2 files changed, 467 insertions(+), 39 deletions(-) create mode 100644 cimgui/generator/json.lua diff --git a/cimgui/generator/generator.lua b/cimgui/generator/generator.lua index 16788ea..ac2dfc2 100644 --- a/cimgui/generator/generator.lua +++ b/cimgui/generator/generator.lua @@ -87,7 +87,7 @@ local function location(file,locpathT) table.insert(path_reT,'^(.*[\\/])('..locpath..')%.h$') end local in_location = false - local which_location = "" + local which_location = "" local function location_it() repeat local line = file:read"*l" @@ -99,10 +99,10 @@ local function location(file,locpathT) in_location = false for i,path_re in ipairs(path_reT) do if location_match:match(path_re) then - in_location = true; - which_location = locpathT[i] - break - end + in_location = true; + which_location = locpathT[i] + break + end end end elseif in_location then @@ -338,7 +338,7 @@ local function func_parser() FP.embeded_structs = embeded_structs FP.defsT = defsT FP.ImVector_templates = ImVector_templates - + function FP.insert(line,comment,locat) line = clean_spaces(line) if line:match"template" then return end @@ -444,7 +444,7 @@ local function func_parser() defT.signature = signature defT.call_args = call_args defT.isvararg = signature:match("%.%.%.%)$") - defT.location = locat + defT.location = locat defT.comment = comment if ret then defT.ret = ret:gsub("&","*") @@ -705,16 +705,16 @@ local function func_header_impl_generate(FP) for _,t in ipairs(FP.cdefs) do if t.cimguiname then - local cimf = FP.defsT[t.cimguiname] - local def = cimf[t.signature] - if def.ret then --not constructor - local addcoment = def.comment or "" - if def.stname == "" then --ImGui namespace or top level - table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..";"..addcoment.."\n") - else - error("class function in implementations") - end - end + local cimf = FP.defsT[t.cimguiname] + local def = cimf[t.signature] + if def.ret then --not constructor + local addcoment = def.comment or "" + if def.stname == "" then --ImGui namespace or top level + table.insert(outtab,"CIMGUI_API".." "..def.ret.." "..(def.ov_cimguiname or def.cimguiname)..def.args..";"..addcoment.."\n") + else + error("class function in implementations") + end + end else --not cimguiname table.insert(outtab,t.comment:gsub("%%","%%%%").."\n")-- %% substitution for gsub end @@ -831,16 +831,16 @@ print("USEGCC",USEGCC) local pipe,err if USEGCC then - pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ../../imgui/imgui.h]],"r") + pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ../../imgui/imgui.h]],"r") - if not pipe then - error("could not execute gcc "..err) - end + if not pipe then + error("could not execute gcc "..err) + end else - pipe,err = io.open("../../imgui/imgui.h","r") - if not pipe then - error("could not execute gcc "..err) - end + pipe,err = io.open("../../imgui/imgui.h","r") + if not pipe then + error("could not execute gcc "..err) + end end print"goint to iterate" local STP = struct_parser() @@ -893,41 +893,43 @@ outfile:close() ----------save fundefs in definitions.lua for using in bindings local hfile = io.open("./definitions.lua","w") -local ser = serializeTable("defs",FP.defsT) -hfile:write(ser.."\nreturn defs") +local ser_funs = serializeTable("defs",FP.defsT) +hfile:write(ser_funs.."\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",gen_structs_and_enums_table(STP.lines)) -hfile:write(ser.."\nreturn defs") +local structs_and_enums_table = gen_structs_and_enums_table(STP.lines) +local ser_struct = serializeTable("defs",structs_and_enums_table) +hfile:write(ser_struct.."\nreturn defs") hfile:close() --=================================Now implementations local sources = {} local impl_locs = {} +local iFP for i,impl in ipairs(implementations) do - table.insert(sources,[[../../imgui/examples/imgui_impl_]].. impl .. ".h ") - table.insert(impl_locs,[[imgui_impl_]].. impl ) + table.insert(sources,[[../../imgui/examples/imgui_impl_]].. impl .. ".h ") + table.insert(impl_locs,[[imgui_impl_]].. impl ) end if #sources > 0 then local pipe = nil if USEGCC then - pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ]] ..table.concat(sources),"r") + pipe,err = io.popen([[gcc -E -C -DIMGUI_DISABLE_OBSOLETE_FUNCTIONS -DIMGUI_API="" -DIMGUI_IMPL_API="" ]] ..table.concat(sources),"r") - if not pipe then - error("could not execute gcc "..err) - end + if not pipe then + error("could not execute gcc "..err) + end end -local iFP = func_parser() +iFP = func_parser() local iSTP = struct_parser() for line,locat in iterator(pipe,impl_locs) do local line, comment = split_comment(line) - iSTP.insert(line,comment) + iSTP.insert(line,comment) iFP.insert(line,comment,locat) end pipe:close() @@ -950,12 +952,38 @@ outfile:close() ----------save fundefs in impl_definitions.lua for using in bindings local hfile = io.open("./impl_definitions.lua","w") -local ser = serializeTable("defs",iFP.defsT) -hfile:write(ser.."\nreturn defs") +local ser_impl = serializeTable("defs",iFP.defsT) +hfile:write(ser_impl.."\nreturn defs") hfile:close() end -- #sources > 0 then +-------------------------------json saving +--avoid mixed tables (with string and integer keys) +local function json_prepare(defs) + --delete signatures in function + for k,def in pairs(defs) do + for k2,v in pairs(def) do + if type(k2)=="string" then + def[k2] = nil + end + end + end + return defs +end +local function save_data(filename,data) + local file = io.open(filename,"w") + file:write(data) + file:close() +end +local json = require"json" +save_data("./definitions.json",json.encode(json_prepare(FP.defsT))) +save_data("./structs_and_enums.json",json.encode(structs_and_enums_table)) +if iFP then + save_data("./impl_definitions.json",json.encode(json_prepare(iFP.defsT))) +end + +print"all done!!" --[[ ---dump some infos----------------------------------------------------------------------- ------------------------------------------------------------------------------------ diff --git a/cimgui/generator/json.lua b/cimgui/generator/json.lua new file mode 100644 index 0000000..8a853d3 --- /dev/null +++ b/cimgui/generator/json.lua @@ -0,0 +1,400 @@ +-- +-- json.lua +-- +-- Copyright (c) 2018 rxi +-- +-- Permission is hereby granted, free of charge, to any person obtaining a copy of +-- this software and associated documentation files (the "Software"), to deal in +-- the Software without restriction, including without limitation the rights to +-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies +-- of the Software, and to permit persons to whom the Software is furnished to do +-- so, subject to the following conditions: +-- +-- The above copyright notice and this permission notice shall be included in all +-- copies or substantial portions of the Software. +-- +-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +-- SOFTWARE. +-- + +local json = { _version = "0.1.1" } + +------------------------------------------------------------------------------- +-- Encode +------------------------------------------------------------------------------- + +local encode + +local escape_char_map = { + [ "\\" ] = "\\\\", + [ "\"" ] = "\\\"", + [ "\b" ] = "\\b", + [ "\f" ] = "\\f", + [ "\n" ] = "\\n", + [ "\r" ] = "\\r", + [ "\t" ] = "\\t", +} + +local escape_char_map_inv = { [ "\\/" ] = "/" } +for k, v in pairs(escape_char_map) do + escape_char_map_inv[v] = k +end + + +local function escape_char(c) + return escape_char_map[c] or string.format("\\u%04x", c:byte()) +end + + +local function encode_nil(val) + return "null" +end + + +local function encode_table(val, stack) + local res = {} + stack = stack or {} + + -- Circular reference? + if stack[val] then error("circular reference") end + + stack[val] = true + + if val[1] ~= nil or next(val) == nil then + -- Treat as array -- check keys are valid and it is not sparse + local n = 0 + for k in pairs(val) do + if type(k) ~= "number" then + error("invalid table: mixed or invalid key types") + end + n = n + 1 + end + if n ~= #val then + error("invalid table: sparse array") + end + -- Encode + for i, v in ipairs(val) do + table.insert(res, encode(v, stack)) + end + stack[val] = nil + return "[" .. table.concat(res, ",") .. "]" + + else + -- Treat as an object + for k, v in pairs(val) do + if type(k) ~= "string" then + error("invalid table: mixed or invalid key types") + end + table.insert(res, encode(k, stack) .. ":" .. encode(v, stack)) + end + stack[val] = nil + return "{" .. table.concat(res, ",") .. "}" + end +end + + +local function encode_string(val) + return '"' .. val:gsub('[%z\1-\31\\"]', escape_char) .. '"' +end + + +local function encode_number(val) + -- Check for NaN, -inf and inf + if val ~= val or val <= -math.huge or val >= math.huge then + error("unexpected number value '" .. tostring(val) .. "'") + end + return string.format("%.14g", val) +end + + +local type_func_map = { + [ "nil" ] = encode_nil, + [ "table" ] = encode_table, + [ "string" ] = encode_string, + [ "number" ] = encode_number, + [ "boolean" ] = tostring, +} + + +encode = function(val, stack) + local t = type(val) + local f = type_func_map[t] + if f then + return f(val, stack) + end + error("unexpected type '" .. t .. "'") +end + + +function json.encode(val) + return ( encode(val) ) +end + + +------------------------------------------------------------------------------- +-- Decode +------------------------------------------------------------------------------- + +local parse + +local function create_set(...) + local res = {} + for i = 1, select("#", ...) do + res[ select(i, ...) ] = true + end + return res +end + +local space_chars = create_set(" ", "\t", "\r", "\n") +local delim_chars = create_set(" ", "\t", "\r", "\n", "]", "}", ",") +local escape_chars = create_set("\\", "/", '"', "b", "f", "n", "r", "t", "u") +local literals = create_set("true", "false", "null") + +local literal_map = { + [ "true" ] = true, + [ "false" ] = false, + [ "null" ] = nil, +} + + +local function next_char(str, idx, set, negate) + for i = idx, #str do + if set[str:sub(i, i)] ~= negate then + return i + end + end + return #str + 1 +end + + +local function decode_error(str, idx, msg) + local line_count = 1 + local col_count = 1 + for i = 1, idx - 1 do + col_count = col_count + 1 + if str:sub(i, i) == "\n" then + line_count = line_count + 1 + col_count = 1 + end + end + error( string.format("%s at line %d col %d", msg, line_count, col_count) ) +end + + +local function codepoint_to_utf8(n) + -- http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&id=iws-appendixa + local f = math.floor + if n <= 0x7f then + return string.char(n) + elseif n <= 0x7ff then + return string.char(f(n / 64) + 192, n % 64 + 128) + elseif n <= 0xffff then + return string.char(f(n / 4096) + 224, f(n % 4096 / 64) + 128, n % 64 + 128) + elseif n <= 0x10ffff then + return string.char(f(n / 262144) + 240, f(n % 262144 / 4096) + 128, + f(n % 4096 / 64) + 128, n % 64 + 128) + end + error( string.format("invalid unicode codepoint '%x'", n) ) +end + + +local function parse_unicode_escape(s) + local n1 = tonumber( s:sub(3, 6), 16 ) + local n2 = tonumber( s:sub(9, 12), 16 ) + -- Surrogate pair? + if n2 then + return codepoint_to_utf8((n1 - 0xd800) * 0x400 + (n2 - 0xdc00) + 0x10000) + else + return codepoint_to_utf8(n1) + end +end + + +local function parse_string(str, i) + local has_unicode_escape = false + local has_surrogate_escape = false + local has_escape = false + local last + for j = i + 1, #str do + local x = str:byte(j) + + if x < 32 then + decode_error(str, j, "control character in string") + end + + if last == 92 then -- "\\" (escape char) + if x == 117 then -- "u" (unicode escape sequence) + local hex = str:sub(j + 1, j + 5) + if not hex:find("%x%x%x%x") then + decode_error(str, j, "invalid unicode escape in string") + end + if hex:find("^[dD][89aAbB]") then + has_surrogate_escape = true + else + has_unicode_escape = true + end + else + local c = string.char(x) + if not escape_chars[c] then + decode_error(str, j, "invalid escape char '" .. c .. "' in string") + end + has_escape = true + end + last = nil + + elseif x == 34 then -- '"' (end of string) + local s = str:sub(i + 1, j - 1) + if has_surrogate_escape then + s = s:gsub("\\u[dD][89aAbB]..\\u....", parse_unicode_escape) + end + if has_unicode_escape then + s = s:gsub("\\u....", parse_unicode_escape) + end + if has_escape then + s = s:gsub("\\.", escape_char_map_inv) + end + return s, j + 1 + + else + last = x + end + end + decode_error(str, i, "expected closing quote for string") +end + + +local function parse_number(str, i) + local x = next_char(str, i, delim_chars) + local s = str:sub(i, x - 1) + local n = tonumber(s) + if not n then + decode_error(str, i, "invalid number '" .. s .. "'") + end + return n, x +end + + +local function parse_literal(str, i) + local x = next_char(str, i, delim_chars) + local word = str:sub(i, x - 1) + if not literals[word] then + decode_error(str, i, "invalid literal '" .. word .. "'") + end + return literal_map[word], x +end + + +local function parse_array(str, i) + local res = {} + local n = 1 + i = i + 1 + while 1 do + local x + i = next_char(str, i, space_chars, true) + -- Empty / end of array? + if str:sub(i, i) == "]" then + i = i + 1 + break + end + -- Read token + x, i = parse(str, i) + res[n] = x + n = n + 1 + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "]" then break end + if chr ~= "," then decode_error(str, i, "expected ']' or ','") end + end + return res, i +end + + +local function parse_object(str, i) + local res = {} + i = i + 1 + while 1 do + local key, val + i = next_char(str, i, space_chars, true) + -- Empty / end of object? + if str:sub(i, i) == "}" then + i = i + 1 + break + end + -- Read key + if str:sub(i, i) ~= '"' then + decode_error(str, i, "expected string for key") + end + key, i = parse(str, i) + -- Read ':' delimiter + i = next_char(str, i, space_chars, true) + if str:sub(i, i) ~= ":" then + decode_error(str, i, "expected ':' after key") + end + i = next_char(str, i + 1, space_chars, true) + -- Read value + val, i = parse(str, i) + -- Set + res[key] = val + -- Next token + i = next_char(str, i, space_chars, true) + local chr = str:sub(i, i) + i = i + 1 + if chr == "}" then break end + if chr ~= "," then decode_error(str, i, "expected '}' or ','") end + end + return res, i +end + + +local char_func_map = { + [ '"' ] = parse_string, + [ "0" ] = parse_number, + [ "1" ] = parse_number, + [ "2" ] = parse_number, + [ "3" ] = parse_number, + [ "4" ] = parse_number, + [ "5" ] = parse_number, + [ "6" ] = parse_number, + [ "7" ] = parse_number, + [ "8" ] = parse_number, + [ "9" ] = parse_number, + [ "-" ] = parse_number, + [ "t" ] = parse_literal, + [ "f" ] = parse_literal, + [ "n" ] = parse_literal, + [ "[" ] = parse_array, + [ "{" ] = parse_object, +} + + +parse = function(str, idx) + local chr = str:sub(idx, idx) + local f = char_func_map[chr] + if f then + return f(str, idx) + end + decode_error(str, idx, "unexpected character '" .. chr .. "'") +end + + +function json.decode(str) + if type(str) ~= "string" then + error("expected argument of type string, got " .. type(str)) + end + local res, idx = parse(str, next_char(str, 1, space_chars, true)) + idx = next_char(str, idx, space_chars, true) + if idx <= #str then + decode_error(str, idx, "trailing garbage") + end + return res +end + + +return json \ No newline at end of file