docs: move all of serializers to doctree

This commit is contained in:
Daniel Kolesa 2016-08-17 13:50:01 +01:00
parent d11cc5ddb5
commit 2bee9169d9
5 changed files with 154 additions and 160 deletions

View File

@ -68,7 +68,6 @@ eluadocgendir = $(datadir)/elua/apps/docgen
eluadocgen_DATA = \
scripts/elua/apps/docgen/doctree.lua \
scripts/elua/apps/docgen/keyref.lua \
scripts/elua/apps/docgen/serializers.lua \
scripts/elua/apps/docgen/stats.lua \
scripts/elua/apps/docgen/util.lua \
scripts/elua/apps/docgen/writer.lua

View File

@ -2,6 +2,7 @@ local util = require("util")
local eolian = require("eolian")
local keyref = require("docgen.keyref")
local dutil = require("docgen.util")
-- writer has to be loaded late to prevent cycles
@ -739,6 +740,18 @@ M.type_cstr_get = function(tp, suffix)
end
end
local add_typedecl_attrs = function(tp, buf)
if tp:is_extern() then
buf[#buf + 1] = "@extern "
end
local ffunc = tp:free_func_get()
if ffunc then
buf[#buf + 1] = "@free("
buf[#buf + 1] = ffunc
buf[#buf + 1] = ") "
end
end
M.Typedecl = Node:clone {
UNKNOWN = eolian.typedecl_type.UNKNOWN,
STRUCT = eolian.typedecl_type.STRUCT,
@ -925,6 +938,144 @@ M.Typedecl = Node:clone {
return nil
end
return M.Typedecl(v)
end,
-- utils
serialize = function(self)
local tpt = self:type_get()
if tpt == self.UNKNOWN then
error("unknown typedecl: " .. self:full_name_get())
elseif tpt == self.STRUCT or
tpt == self.STRUCT_OPAQUE then
local buf = { "struct " }
add_typedecl_attrs(self, buf)
buf[#buf + 1] = self:full_name_get()
if tpt == self.STRUCT_OPAQUE then
buf[#buf + 1] = ";"
return table.concat(buf)
end
local fields = self:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = fld:type_get():serialize()
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == self.ENUM then
local buf = { "enum " }
add_typedecl_attrs(self, buf)
buf[#buf + 1] = self:full_name_get()
local fields = self:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
local val = fld:value_get()
if val then
buf[#buf + 1] = ": "
buf[#buf + 1] = val:serialize()
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == self.ALIAS then
local buf = { "type " }
add_typedecl_attrs(self, buf)
buf[#buf + 1] = self:full_name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = self:base_type_get():serialize()
buf[#buf + 1] = ";"
return table.concat(buf)
end
error("unhandled typedecl type: " .. tpt)
end,
serialize_c = function(self)
local tpt = self:type_get()
if tpt == self.UNKNOWN then
error("unknown typedecl: " .. self:full_name_get())
elseif tpt == self.STRUCT or
tpt == self.STRUCT_OPAQUE then
local buf = { "typedef struct " }
local fulln = self:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
buf[#buf + 1] = "_" .. fulln;
if tpt == self.STRUCT_OPAQUE then
buf[#buf + 1] = " " .. fulln .. ";"
return table.concat(buf)
end
local fields = self:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = M.type_cstr_get(fld:type_get(), fld:name_get())
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == self.ENUM then
local buf = { "typedef enum" }
local fulln = self:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
local fields = self:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
local cn = fld:c_name_get()
buf[#buf + 1] = cn
keyref.add(cn, "c")
local val = fld:value_get()
if val then
buf[#buf + 1] = " = "
local ev = val:eval_enum()
local lit = ev:to_literal()
buf[#buf + 1] = lit
local ser = val:serialize()
if ser and ser ~= lit then
buf[#buf + 1] = " /* " .. ser .. " */"
end
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == self.ALIAS then
local fulln = self:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
return "typedef "
.. M.type_cstr_get(self:base_type_get(), fulln) .. ";"
end
error("unhandled typedecl type: " .. tpt)
end
}

View File

@ -1,5 +1,3 @@
local writer = require("docgen.writer")
local M = {}
local key_refs = {}
@ -15,7 +13,7 @@ end
M.build = function()
for lang, rfs in pairs(key_refs) do
local f = writer.Writer({ "ref", lang, "keyword-list" })
local f = require("docgen.writer").Writer({ "ref", lang, "keyword-list" })
local arr = {}
for refn, v in pairs(rfs) do
arr[#arr + 1] = refn

View File

@ -1,153 +0,0 @@
local keyref = require("docgen.keyref")
local dtree = require("docgen.doctree")
local M = {}
local add_typedecl_attrs = function(tp, buf)
if tp:is_extern() then
buf[#buf + 1] = "@extern "
end
local ffunc = tp:free_func_get()
if ffunc then
buf[#buf + 1] = "@free("
buf[#buf + 1] = ffunc
buf[#buf + 1] = ") "
end
end
M.get_typedecl_str = function(tp)
local tpt = tp:type_get()
if tpt == dtree.Typedecl.UNKNOWN then
error("unknown typedecl: " .. tp:full_name_get())
elseif tpt == dtree.Typedecl.STRUCT or
tpt == dtree.Typedecl.STRUCT_OPAQUE then
local buf = { "struct " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
if tpt == dtree.Typedecl.STRUCT_OPAQUE then
buf[#buf + 1] = ";"
return table.concat(buf)
end
local fields = tp:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = fld:type_get():serialize()
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ENUM then
local buf = { "enum " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
local fields = tp:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {}"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
local val = fld:value_get()
if val then
buf[#buf + 1] = ": "
buf[#buf + 1] = val:serialize()
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "}"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ALIAS then
local buf = { "type " }
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = tp:base_type_get():serialize()
buf[#buf + 1] = ";"
return table.concat(buf)
end
error("unhandled typedecl type: " .. tpt)
end
M.get_typedecl_cstr = function(tp)
local tpt = tp:type_get()
if tpt == dtree.Typedecl.UNKNOWN then
error("unknown typedecl: " .. tp:full_name_get())
elseif tpt == dtree.Typedecl.STRUCT or
tpt == dtree.Typedecl.STRUCT_OPAQUE then
local buf = { "typedef struct " }
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
buf[#buf + 1] = "_" .. fulln;
if tpt == dtree.Typedecl.STRUCT_OPAQUE then
buf[#buf + 1] = " " .. fulln .. ";"
return table.concat(buf)
end
local fields = tp:struct_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
buf[#buf + 1] = dtree.type_cstr_get(fld:type_get(), fld:name_get())
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ENUM then
local buf = { "typedef enum" }
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
local fields = tp:enum_fields_get()
if #fields == 0 then
buf[#buf + 1] = " {} " .. fulln .. ";"
return table.concat(buf)
end
buf[#buf + 1] = " {\n"
for i, fld in ipairs(fields) do
buf[#buf + 1] = " "
local cn = fld:c_name_get()
buf[#buf + 1] = cn
keyref.add(cn, "c")
local val = fld:value_get()
if val then
buf[#buf + 1] = " = "
local ev = val:eval_enum()
local lit = ev:to_literal()
buf[#buf + 1] = lit
local ser = val:serialize()
if ser and ser ~= lit then
buf[#buf + 1] = " /* " .. ser .. " */"
end
end
if i == #fields then
buf[#buf + 1] = "\n"
else
buf[#buf + 1] = ",\n"
end
end
buf[#buf + 1] = "} " .. fulln .. ";"
return table.concat(buf)
elseif tpt == dtree.Typedecl.ALIAS then
local fulln = tp:full_name_get():gsub("%.", "_");
keyref.add(fulln, "c")
return "typedef " .. dtree.type_cstr_get(tp:base_type_get(), fulln) .. ";"
end
error("unhandled typedecl type: " .. tpt)
end
return M

View File

@ -6,7 +6,6 @@ local stats = require("docgen.stats")
local dutil = require("docgen.util")
local writer = require("docgen.writer")
local keyref = require("docgen.keyref")
local ser = require("docgen.serializers")
local dtree = require("docgen.doctree")
local propt_to_type = {
@ -747,11 +746,11 @@ local write_tsigs = function(f, tp)
f:write_h(tp:full_name_get(), 2)
f:write_h("Signature", 3)
f:write_code(ser.get_typedecl_str(tp))
f:write_code(tp:serialize())
f:write_nl()
f:write_h("C signature", 3)
f:write_code(ser.get_typedecl_cstr(tp), "c")
f:write_code(tp:serialize_c(), "c")
f:write_nl()
end