feat: added support for specifying types for lua2dox

This commit is contained in:
Folke Lemaitre
2022-09-28 13:22:08 +02:00
committed by Lewis Russell
parent 24a1c7f556
commit 1da7b4eb69
4 changed files with 78 additions and 65 deletions

View File

@@ -159,6 +159,7 @@ end
--! \param Str
--! \param Pattern
--! \returns table of string fragments
---@return string[]
local function string_split(Str, Pattern)
local splitStr = {}
local fpat = "(.-)" .. Pattern
@@ -369,6 +370,9 @@ local function checkComment4fn(Fn_magic,MagicLines)
return fn_magic
end
local types = {"number", "string", "table", "list", "boolean", "function"}
--! \brief run the filter
function TLua2DoX_filter.readfile(this,AppStamp,Filename)
local inStream = TStream_Read()
@@ -408,6 +412,19 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename)
local magic = string.sub(line, 4 + offset)
local magic_split = string_split(magic, ' ')
if magic_split[1] == 'param' then
for _, type in ipairs(types) do
magic = magic:gsub("^param%s+([a-zA-Z_?]+)%s+.*%((" .. type .. ")%)", "param %1 %2" )
magic = magic:gsub("^param%s+([a-zA-Z_?]+)%s+.*%((" .. type .. "|nil)%)", "param %1 %2" )
end
magic_split = string_split(magic, ' ')
elseif magic_split[1] == 'return' then
for _, type in ipairs(types) do
magic = magic:gsub("^return%s+.*%((" .. type .. ")%)", "return %1" )
magic = magic:gsub("^return%s+.*%((" .. type .. "|nil)%)", "return %1" )
end
magic_split = string_split(magic, ' ')
end
if magic_split[1] == "generic" then
local generic_name, generic_type = line:match("@generic%s*(%w+)%s*:?%s*(.*)")
@@ -415,39 +432,35 @@ function TLua2DoX_filter.readfile(this,AppStamp,Filename)
generic_type = "any"
end
generic[generic_name] = generic_type
end
local type_index = 2
if magic_split[1] == 'param' then
type_index = type_index + 1
if magic_split[type_index] and magic_split[2]:find("%?$") then
magic_split[type_index] = magic_split[type_index] .. "|nil"
magic_split[2] = magic_split[2]:sub(1, -2)
else
local type_index = 2
if magic_split[1] == 'param' then
type_index = type_index + 1
end
end
if magic_split[type_index] then
for k, v in pairs(generic) do
magic_split[type_index] = magic_split[type_index]:gsub(k, v)
if magic_split[type_index] then
-- fix optional parameters
if magic_split[type_index] and magic_split[2]:find("%?$") then
if not magic_split[type_index]:find("nil") then
magic_split[type_index] = magic_split[type_index] .. "|nil"
end
magic_split[2] = magic_split[2]:sub(1, -2)
end
-- replace generic types
if magic_split[type_index] then
for k, v in pairs(generic) do
magic_split[type_index] = magic_split[type_index]:gsub(k, v)
end
end
-- surround some types by ()
for _, type in ipairs(types) do
magic_split[type_index] = magic_split[type_index]:gsub("^(" .. type .. "|nil):?$", "(%1)")
magic_split[type_index] = magic_split[type_index]:gsub("^(" .. type .. "):?$", "(%1)")
end
end
end
if magic_split[type_index] == 'number' or
magic_split[type_index] == 'number|nil' or
magic_split[type_index] == 'string' or
magic_split[type_index] == 'string|nil' or
magic_split[type_index] == 'table' or
magic_split[type_index] == 'table|nil' or
magic_split[type_index] == 'boolean' or
magic_split[type_index] == 'boolean|nil' or
magic_split[type_index] == 'function' or
magic_split[type_index] == 'function|nil'
then
magic_split[type_index] = '(' .. magic_split[type_index] .. ')'
end
magic = table.concat(magic_split, ' ')
magic = table.concat(magic_split, ' ')
if magic_split[1] ~= "generic" then
outStream:writeln('/// @' .. magic)
fn_magic = checkComment4fn(fn_magic,magic)
end