docs: move type serializer into doctree

This commit is contained in:
Daniel Kolesa 2016-08-17 13:42:29 +01:00
parent 066a2f9fd7
commit d11cc5ddb5
3 changed files with 61 additions and 59 deletions

View File

@ -573,6 +573,23 @@ M.EnumField = Node:clone {
end
}
local wrap_type_attrs = function(tp, str)
if tp:is_const() then
str = "const(" .. str .. ")"
end
if tp:is_own() then
str = "own(" .. str .. ")"
end
local ffunc = tp:free_func_get()
if ffunc then
str = "free(" .. str .. ", " .. ffunc .. ")"
end
if tp:is_ref() then
str = "ref(" .. str .. ")"
end
return str
end
M.Type = Node:clone {
UNKNOWN = eolian.type_type.UNKNOWN,
VOID = eolian.type_type.VOID,
@ -667,6 +684,45 @@ M.Type = Node:clone {
free_func_get = function(self)
return self.type:free_func_get()
end,
-- utils
serialize = function(self)
local tpt = self:type_get()
if tpt == self.UNKNOWN then
error("unknown type: " .. self:full_name_get())
elseif tpt == self.VOID then
return wrap_type_attrs(self, "void")
elseif tpt == self.UNDEFINED then
return wrap_type_attrs(self, "__undefined_type")
elseif tpt == self.REGULAR or tpt == self.CLASS then
return wrap_type_attrs(self, self:full_name_get())
elseif tpt == self.COMPLEX then
local stypes = {}
local stp = self:base_type_get()
while stp do
stypes[#stypes + 1] = stp:serialize()
stp = stp:next_type_get()
end
return wrap_type_attrs(self, self:full_name_get() .. "<"
.. table.concat(stypes, ", ") .. ">")
elseif tpt == self.POINTER then
local btp = self:base_type_get()
local suffix = " *"
if btp:type_get() == self.POINTER then
suffix = "*"
end
return wrap_type_attrs(self, btp:serialize() .. suffix)
elseif tpt == self.STATIC_ARRAY then
return wrap_type_attrs(self, "static_array<"
.. self:base_type_get():serialize() .. ", "
.. self:array_size_get() .. ">")
elseif tpt == self.TERMINATED_ARRAY then
return wrap_type_attrs(self, "terminated_array<"
.. self:base_type_get():serialize() .. ">")
end
error("unhandled type type: " .. tpt)
end
}

View File

@ -3,60 +3,6 @@ local dtree = require("docgen.doctree")
local M = {}
local wrap_type_attrs = function(tp, str)
if tp:is_const() then
str = "const(" .. str .. ")"
end
if tp:is_own() then
str = "own(" .. str .. ")"
end
local ffunc = tp:free_func_get()
if ffunc then
str = "free(" .. str .. ", " .. ffunc .. ")"
end
if tp:is_ref() then
str = "ref(" .. str .. ")"
end
return str
end
M.get_type_str = function(tp)
local tpt = tp:type_get()
if tpt == tp.UNKNOWN then
error("unknown type: " .. tp:full_name_get())
elseif tpt == tp.VOID then
return wrap_type_attrs(tp, "void")
elseif tpt == tp.UNDEFINED then
return wrap_type_attrs(tp, "__undefined_type")
elseif tpt == tp.REGULAR or tpt == tp.CLASS then
return wrap_type_attrs(tp, tp:full_name_get())
elseif tpt == tp.COMPLEX then
local stypes = {}
local stp = tp:base_type_get()
while stp do
stypes[#stypes + 1] = M.get_type_str(stp)
stp = stp:next_type_get()
end
return wrap_type_attrs(tp, tp:full_name_get() .. "<"
.. table.concat(stypes, ", ") .. ">")
elseif tpt == tp.POINTER then
local btp = tp:base_type_get()
local suffix = " *"
if btp:type_get() == tp.POINTER then
suffix = "*"
end
return wrap_type_attrs(tp, M.get_type_str(btp) .. suffix)
elseif tpt == tp.STATIC_ARRAY then
return wrap_type_attrs(tp, "static_array<"
.. M.get_type_str(tp:base_type_get()) .. ", "
.. tp:array_size_get() .. ">")
elseif tpt == tp.TERMINATED_ARRAY then
return wrap_type_attrs(tp, "terminated_array<"
.. M.get_type_str(tp:base_type_get()) .. ">")
end
error("unhandled type type: " .. tpt)
end
local add_typedecl_attrs = function(tp, buf)
if tp:is_extern() then
buf[#buf + 1] = "@extern "
@ -92,7 +38,7 @@ M.get_typedecl_str = function(tp)
buf[#buf + 1] = " "
buf[#buf + 1] = fld:name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = M.get_type_str(fld:type_get())
buf[#buf + 1] = fld:type_get():serialize()
buf[#buf + 1] = ";\n"
end
buf[#buf + 1] = "}"
@ -128,7 +74,7 @@ M.get_typedecl_str = function(tp)
add_typedecl_attrs(tp, buf)
buf[#buf + 1] = tp:full_name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = M.get_type_str(tp:base_type_get())
buf[#buf + 1] = tp:base_type_get():serialize()
buf[#buf + 1] = ";"
return table.concat(buf)
end

View File

@ -134,7 +134,7 @@ local gen_func_param = function(fp, buf, nodir)
if not nodir then buf[#buf + 1] = dirs[fp:direction_get()] end
buf[#buf + 1] = fp:name_get()
buf[#buf + 1] = ": "
buf[#buf + 1] = ser.get_type_str(fp:type_get())
buf[#buf + 1] = fp:type_get():serialize()
local dval = fp:default_value_get()
if dval then
buf[#buf + 1] = " ("
@ -160,7 +160,7 @@ local gen_func_return = function(fp, ftype, buf, indent)
end
buf[#buf + 1] = indent and (" "):rep(indent) or " "
buf[#buf + 1] = "return: "
buf[#buf + 1] = ser.get_type_str(rett)
buf[#buf + 1] = rett:serialize()
local dval = fp:return_default_value_get(ftype)
if dval then
buf[#buf + 1] = " ("
@ -1025,7 +1025,7 @@ build_event = function(ev, cl)
local etp = ev:type_get()
if etp then
buf[#buf + 1] = ": "
buf[#buf + 1] = ser.get_type_str(etp)
buf[#buf + 1] = etp:serialize()
end
buf[#buf + 1] = ";"