refactor(lua2dox): tidy

This commit is contained in:
Lewis Russell 2022-10-10 12:21:19 +01:00
parent a6d889eae1
commit c401b06fe9

View File

@ -50,88 +50,28 @@ However I have put in a hack that will insert the "missing" close paren.
The effect is that you will get the function documented, but not with the parameter list you might expect. The effect is that you will get the function documented, but not with the parameter list you might expect.
]] ]]
local function class(BaseClass, ClassInitialiser) local function class()
local newClass = {} -- a new class newClass local newClass = {} -- a new class newClass
if not ClassInitialiser and type(BaseClass) == 'function' then
ClassInitialiser = BaseClass
BaseClass = nil
elseif type(BaseClass) == 'table' then
-- our new class is a shallow copy of the base class!
for i, v in pairs(BaseClass) do
newClass[i] = v
end
newClass._base = BaseClass
end
-- the class will be the metatable for all its newInstanceects, -- the class will be the metatable for all its newInstanceects,
-- and they will look up their methods in it. -- and they will look up their methods in it.
newClass.__index = newClass newClass.__index = newClass
-- expose a constructor which can be called by <classname>(<args>) -- expose a constructor which can be called by <classname>(<args>)
local classMetatable = {} setmetatable(newClass, {
classMetatable.__call = function(class_tbl, ...) __call = function(class_tbl, ...)
local newInstance = {} local newInstance = {}
setmetatable(newInstance, newClass) setmetatable(newInstance, newClass)
--if init then --if init then
-- init(newInstance,...) -- init(newInstance,...)
if class_tbl.init then if class_tbl.init then
class_tbl.init(newInstance, ...) class_tbl.init(newInstance, ...)
else
-- make sure that any stuff from the base class is initialized!
if BaseClass and BaseClass.init then
BaseClass.init(newInstance, ...)
end end
return newInstance
end end
return newInstance })
end
newClass.init = ClassInitialiser
newClass.is_a = function(this, klass)
local thisMetatable = getmetatable(this)
while thisMetatable do
if thisMetatable == klass then
return true
end
thisMetatable = thisMetatable._base
end
return false
end
setmetatable(newClass, classMetatable)
return newClass return newClass
end end
--! \class TCore_Clock
--! \brief a clock
local TCore_Clock = class()
--! \brief get the current time
function TCore_Clock.GetTimeNow()
local gettimeofday = os.gettimeofday -- luacheck: ignore 143 Accessing an undefined field of a global variable.
if gettimeofday then
return gettimeofday()
else
return os.time()
end
end
--! \brief constructor
function TCore_Clock.init(this, T0)
if T0 then
this.t0 = T0
else
this.t0 = TCore_Clock.GetTimeNow()
end
end
--! \brief get time string
function TCore_Clock.getTimeStamp(this, T0)
local t0
if T0 then
t0 = T0
else
t0 = this.t0
end
return os.date('%c %Z', t0)
end
--! \brief write to stdout --! \brief write to stdout
local function TCore_IO_write(Str) local function TCore_IO_write(Str)
if Str then if Str then
@ -210,7 +150,6 @@ function TStream_Read.getContents(this, Filename)
assert(Filename) assert(Filename)
-- get lines from file -- get lines from file
-- syphon lines to our table -- syphon lines to our table
--TCore_Debug_show_var('Filename',Filename)
local filecontents = {} local filecontents = {}
for line in io.lines(Filename) do for line in io.lines(Filename) do
table.insert(filecontents, line) table.insert(filecontents, line)
@ -392,8 +331,6 @@ function TLua2DoX_filter.readfile(this, AppStamp, Filename)
local l = 0 local l = 0
while not (inStream:eof()) do while not (inStream:eof()) do
line = string_trim(inStream:getLine()) line = string_trim(inStream:getLine())
-- TCore_Debug_show_var('inStream',inStream)
-- TCore_Debug_show_var('line',line )
l = l + 1 l = l + 1
if string.sub(line, 1, 2) == '--' then -- it's a comment if string.sub(line, 1, 2) == '--' then -- it's a comment
-- Allow people to write style similar to EmmyLua (since they are basically the same) -- Allow people to write style similar to EmmyLua (since they are basically the same)
@ -510,12 +447,6 @@ function TLua2DoX_filter.readfile(this, AppStamp, Filename)
-- ....v... -- ....v...
if pos_fn then if pos_fn then
-- we've got a function -- we've got a function
local fn_type
if string.find(line, '^local%s+') then
fn_type = '' --'static ' -- static functions seem to be excluded
else
fn_type = ''
end
local fn = TString_removeCommentFromLine(string_trim(string.sub(line, pos_fn + 8))) local fn = TString_removeCommentFromLine(string_trim(string.sub(line, pos_fn + 8)))
if fn_magic then if fn_magic then
fn = fn_magic fn = fn_magic
@ -561,7 +492,7 @@ function TLua2DoX_filter.readfile(this, AppStamp, Filename)
end end
-- add vanilla function -- add vanilla function
outStream:writeln(fn_type .. 'function ' .. fn .. '{}') outStream:writeln('function ' .. fn .. '{}')
end end
else else
this:warning(inStream:getLineNo(), 'something weird here') this:warning(inStream:getLineNo(), 'something weird here')
@ -596,8 +527,7 @@ local TApp = class()
--! \brief constructor --! \brief constructor
function TApp.init(this) function TApp.init(this)
local t0 = TCore_Clock() this.timestamp = os.date('%c %Z', os.time())
this.timestamp = t0:getTimeStamp()
this.name = 'Lua2DoX' this.name = 'Lua2DoX'
this.version = '0.2 20130128' this.version = '0.2 20130128'
this.copyright = 'Copyright (c) Simon Dales 2012-13' this.copyright = 'Copyright (c) Simon Dales 2012-13'