2023-07-18 04:24:53 -07:00
|
|
|
-----------------------------------------------------------------------------
|
|
|
|
-- Copyright (C) 2012 by Simon Dales --
|
|
|
|
-- simon@purrsoft.co.uk --
|
2019-03-29 13:54:34 -07:00
|
|
|
-- --
|
|
|
|
-- This program is free software; you can redistribute it and/or modify --
|
|
|
|
-- it under the terms of the GNU General Public License as published by --
|
|
|
|
-- the Free Software Foundation; either version 2 of the License, or --
|
|
|
|
-- (at your option) any later version. --
|
|
|
|
-- --
|
|
|
|
-- This program is distributed in the hope that it will be useful, --
|
|
|
|
-- but WITHOUT ANY WARRANTY; without even the implied warranty of --
|
|
|
|
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the --
|
|
|
|
-- GNU General Public License for more details. --
|
|
|
|
-- --
|
|
|
|
-- You should have received a copy of the GNU General Public License --
|
|
|
|
-- along with this program; if not, write to the --
|
|
|
|
-- Free Software Foundation, Inc., --
|
|
|
|
-- 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. --
|
2023-07-18 04:24:53 -07:00
|
|
|
-----------------------------------------------------------------------------
|
2019-03-29 13:54:34 -07:00
|
|
|
|
|
|
|
--[[!
|
2019-12-31 07:52:14 -07:00
|
|
|
Lua-to-Doxygen converter
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
Partially from lua2dox
|
|
|
|
http://search.cpan.org/~alec/Doxygen-Lua-0.02/lib/Doxygen/Lua.pm
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-03-03 05:49:22 -07:00
|
|
|
RUNNING
|
2019-03-29 13:54:34 -07:00
|
|
|
-------
|
|
|
|
|
2023-03-03 05:49:22 -07:00
|
|
|
This script "lua2dox.lua" gets called by "gen_vimdoc.py".
|
2023-03-02 12:46:59 -07:00
|
|
|
|
2023-03-03 05:49:22 -07:00
|
|
|
DEBUGGING/DEVELOPING
|
|
|
|
---------------------
|
|
|
|
|
|
|
|
1. To debug, run gen_vimdoc.py with --keep-tmpfiles:
|
|
|
|
python3 scripts/gen_vimdoc.py -t treesitter --keep-tmpfiles
|
|
|
|
2. The filtered result will be written to ./tmp-lua2dox-doc/….lua.c
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
Doxygen must be on your system. You can experiment like so:
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2019-12-31 07:52:14 -07:00
|
|
|
- Run "doxygen -g" to create a default Doxyfile.
|
2021-09-20 19:00:50 -07:00
|
|
|
- Then alter it to let it recognise lua. Add the following line:
|
2019-12-31 07:52:14 -07:00
|
|
|
FILE_PATTERNS = *.lua
|
|
|
|
- Then run "doxygen".
|
2019-03-29 13:54:34 -07:00
|
|
|
|
|
|
|
The core function reads the input file (filename or stdin) and outputs some pseudo C-ish language.
|
|
|
|
It only has to be good enough for doxygen to see it as legal.
|
|
|
|
|
|
|
|
One limitation is that each line is treated separately (except for long comments).
|
|
|
|
The implication is that class and function declarations must be on the same line.
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
There is hack that will insert the "missing" close paren.
|
2019-03-29 13:54:34 -07:00
|
|
|
The effect is that you will get the function documented, but not with the parameter list you might expect.
|
|
|
|
]]
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
local TYPES = { 'integer', 'number', 'string', 'table', 'list', 'boolean', 'function' }
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
local luacats_parser = require('src/nvim/generators/luacats_grammar')
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
local debug_outfile = nil --- @type string?
|
|
|
|
local debug_output = {}
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- write to stdout
|
|
|
|
--- @param str? string
|
|
|
|
local function write(str)
|
|
|
|
if not str then
|
|
|
|
return
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
|
|
|
io.write(str)
|
|
|
|
if debug_outfile then
|
|
|
|
table.insert(debug_output, str)
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- write to stdout
|
|
|
|
--- @param str? string
|
|
|
|
local function writeln(str)
|
|
|
|
write(str)
|
|
|
|
write('\n')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- an input file buffer
|
|
|
|
--- @class StreamRead
|
|
|
|
--- @field currentLine string?
|
|
|
|
--- @field contentsLen integer
|
|
|
|
--- @field currentLineNo integer
|
|
|
|
--- @field filecontents string[]
|
|
|
|
local StreamRead = {}
|
|
|
|
|
|
|
|
--- @return StreamRead
|
|
|
|
--- @param filename string
|
|
|
|
function StreamRead.new(filename)
|
|
|
|
assert(filename, ('invalid file: %s'):format(filename))
|
2019-03-29 13:54:34 -07:00
|
|
|
-- get lines from file
|
2021-09-19 16:35:38 -07:00
|
|
|
-- syphon lines to our table
|
2023-07-18 04:24:53 -07:00
|
|
|
local filecontents = {} --- @type string[]
|
|
|
|
for line in io.lines(filename) do
|
2023-12-04 13:38:31 -07:00
|
|
|
filecontents[#filecontents + 1] = line
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
return setmetatable({
|
|
|
|
filecontents = filecontents,
|
|
|
|
contentsLen = #filecontents,
|
|
|
|
currentLineNo = 1,
|
|
|
|
}, { __index = StreamRead })
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-03-02 12:46:59 -07:00
|
|
|
-- get a line
|
2023-07-18 04:24:53 -07:00
|
|
|
function StreamRead:getLine()
|
|
|
|
if self.currentLine then
|
|
|
|
self.currentLine = nil
|
|
|
|
return self.currentLine
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
|
|
|
-- get line
|
|
|
|
if self.currentLineNo <= self.contentsLen then
|
|
|
|
local line = self.filecontents[self.currentLineNo]
|
|
|
|
self.currentLineNo = self.currentLineNo + 1
|
|
|
|
return line
|
|
|
|
end
|
|
|
|
|
|
|
|
return ''
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-03-02 12:46:59 -07:00
|
|
|
-- save line fragment
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @param line_fragment string
|
|
|
|
function StreamRead:ungetLine(line_fragment)
|
|
|
|
self.currentLine = line_fragment
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-03-02 12:46:59 -07:00
|
|
|
-- is it eof?
|
2023-07-18 04:24:53 -07:00
|
|
|
function StreamRead:eof()
|
|
|
|
return not self.currentLine and self.currentLineNo > self.contentsLen
|
|
|
|
end
|
|
|
|
|
|
|
|
-- input filter
|
|
|
|
--- @class Lua2DoxFilter
|
2024-01-13 16:36:30 -07:00
|
|
|
local Lua2DoxFilter = {
|
|
|
|
generics = {}, --- @type table<string,string>
|
|
|
|
block_ignore = false, --- @type boolean
|
|
|
|
}
|
2023-07-18 04:24:53 -07:00
|
|
|
setmetatable(Lua2DoxFilter, { __index = Lua2DoxFilter })
|
|
|
|
|
2024-01-13 16:36:30 -07:00
|
|
|
function Lua2DoxFilter:reset()
|
|
|
|
self.generics = {}
|
|
|
|
self.block_ignore = false
|
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- trim comment off end of string
|
|
|
|
---
|
|
|
|
--- @param line string
|
|
|
|
--- @return string, string?
|
|
|
|
local function removeCommentFromLine(line)
|
|
|
|
local pos_comment = line:find('%-%-')
|
|
|
|
if not pos_comment then
|
|
|
|
return line
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
return line:sub(1, pos_comment - 1), line:sub(pos_comment)
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
--- @param parsed luacats.Return
|
|
|
|
--- @return string
|
|
|
|
local function get_return_type(parsed)
|
|
|
|
local elems = {} --- @type string[]
|
|
|
|
for _, v in ipairs(parsed) do
|
|
|
|
local e = v.type --- @type string
|
|
|
|
if v.name then
|
|
|
|
e = e .. ' ' .. v.name --- @type string
|
|
|
|
end
|
|
|
|
elems[#elems + 1] = e
|
|
|
|
end
|
|
|
|
return '(' .. table.concat(elems, ', ') .. ')'
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param name string
|
|
|
|
--- @return string
|
|
|
|
local function process_name(name, optional)
|
|
|
|
if optional then
|
|
|
|
name = name:sub(1, -2) --- @type string
|
|
|
|
end
|
|
|
|
return name
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param ty string
|
|
|
|
--- @param generics table<string,string>
|
|
|
|
--- @return string
|
|
|
|
local function process_type(ty, generics, optional)
|
|
|
|
-- replace generic types
|
|
|
|
for k, v in pairs(generics) do
|
|
|
|
ty = ty:gsub(k, v) --- @type string
|
|
|
|
end
|
|
|
|
|
|
|
|
-- strip parens
|
|
|
|
ty = ty:gsub('^%((.*)%)$', '%1')
|
|
|
|
|
|
|
|
if optional and not ty:find('nil') then
|
|
|
|
ty = ty .. '?'
|
|
|
|
end
|
|
|
|
|
|
|
|
-- remove whitespace in unions
|
|
|
|
ty = ty:gsub('%s*|%s*', '|')
|
|
|
|
|
|
|
|
-- replace '|nil' with '?'
|
|
|
|
ty = ty:gsub('|nil', '?')
|
|
|
|
ty = ty:gsub('nil|(.*)', '%1?')
|
|
|
|
|
|
|
|
return '(`' .. ty .. '`)'
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param parsed luacats.Param
|
|
|
|
--- @param generics table<string,string>
|
|
|
|
--- @return string
|
|
|
|
local function process_param(parsed, generics)
|
|
|
|
local name, ty = parsed.name, parsed.type
|
|
|
|
local optional = vim.endswith(name, '?')
|
|
|
|
|
|
|
|
return table.concat({
|
|
|
|
'/// @param',
|
|
|
|
process_name(name, optional),
|
|
|
|
process_type(ty, generics, optional),
|
|
|
|
parsed.desc,
|
|
|
|
}, ' ')
|
|
|
|
end
|
|
|
|
|
|
|
|
--- @param parsed luacats.Return
|
|
|
|
--- @param generics table<string,string>
|
|
|
|
--- @return string
|
|
|
|
local function process_return(parsed, generics)
|
|
|
|
local ty, name --- @type string, string
|
|
|
|
if #parsed == 1 then
|
|
|
|
ty, name = parsed[1].type, parsed[1].name or ''
|
|
|
|
else
|
|
|
|
ty, name = get_return_type(parsed), ''
|
|
|
|
end
|
|
|
|
|
|
|
|
local optional = vim.endswith(name, '?')
|
|
|
|
|
|
|
|
return table.concat({
|
|
|
|
'/// @return',
|
|
|
|
process_type(ty, generics, optional),
|
|
|
|
process_name(name, optional),
|
|
|
|
parsed.desc,
|
|
|
|
}, ' ')
|
|
|
|
end
|
|
|
|
|
2023-08-03 04:41:47 -07:00
|
|
|
--- Processes "@…" directives in a docstring line.
|
|
|
|
---
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @param line string
|
|
|
|
--- @return string?
|
2024-01-13 16:36:30 -07:00
|
|
|
function Lua2DoxFilter:process_magic(line)
|
2023-07-18 04:24:53 -07:00
|
|
|
line = line:gsub('^%s+@', '@')
|
|
|
|
line = line:gsub('@package', '@private')
|
2023-07-18 07:42:30 -07:00
|
|
|
line = line:gsub('@nodoc', '@private')
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2024-01-13 16:36:30 -07:00
|
|
|
if self.block_ignore then
|
|
|
|
return '// gg:" ' .. line .. '"'
|
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
if not vim.startswith(line, '@') then -- it's a magic comment
|
|
|
|
return '/// ' .. line
|
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
local magic_split = vim.split(line, ' ', { plain = true })
|
2023-07-18 04:24:53 -07:00
|
|
|
local directive = magic_split[1]
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-12-04 13:38:31 -07:00
|
|
|
if
|
|
|
|
vim.list_contains({
|
2024-01-09 10:36:46 -07:00
|
|
|
'@cast',
|
|
|
|
'@diagnostic',
|
|
|
|
'@overload',
|
|
|
|
'@meta',
|
|
|
|
'@type',
|
2023-12-04 13:38:31 -07:00
|
|
|
}, directive)
|
|
|
|
then
|
2023-07-18 04:24:53 -07:00
|
|
|
-- Ignore LSP directives
|
|
|
|
return '// gg:"' .. line .. '"'
|
2024-01-09 10:36:46 -07:00
|
|
|
elseif directive == '@defgroup' or directive == '@addtogroup' then
|
2023-07-18 04:24:53 -07:00
|
|
|
-- Can't use '.' in defgroup, so convert to '--'
|
2024-01-09 10:36:46 -07:00
|
|
|
return '/// ' .. line:gsub('%.', '-dot-')
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2024-01-13 16:36:30 -07:00
|
|
|
if directive == '@alias' then
|
|
|
|
-- this contiguous block should be all ignored.
|
|
|
|
self.block_ignore = true
|
|
|
|
return '// gg:"' .. line .. '"'
|
|
|
|
end
|
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
-- preprocess line before parsing
|
|
|
|
if directive == '@param' or directive == '@return' then
|
2023-07-18 04:24:53 -07:00
|
|
|
for _, type in ipairs(TYPES) do
|
2024-01-09 10:36:46 -07:00
|
|
|
line = line:gsub('^@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. ')%)', '@param %1 %2')
|
|
|
|
line = line:gsub('^@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. '|nil)%)', '@param %1 %2')
|
2024-01-09 05:47:57 -07:00
|
|
|
line = line:gsub('^@param%s+([a-zA-Z_?]+)%s+.*%((' .. type .. '%?)%)', '@param %1 %2')
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
line = line:gsub('^@return%s+.*%((' .. type .. ')%)', '@return %1')
|
|
|
|
line = line:gsub('^@return%s+.*%((' .. type .. '|nil)%)', '@return %1')
|
2024-01-09 05:47:57 -07:00
|
|
|
line = line:gsub('^@return%s+.*%((' .. type .. '%?)%)', '@return %1')
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
2024-01-09 10:36:46 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
local parsed = luacats_parser:match(line)
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
if not parsed then
|
|
|
|
return '/// ' .. line
|
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
local kind = parsed.kind
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
if kind == 'generic' then
|
2024-01-13 16:36:30 -07:00
|
|
|
self.generics[parsed.name] = parsed.type or 'any'
|
2024-01-09 10:36:46 -07:00
|
|
|
return
|
|
|
|
elseif kind == 'param' then
|
2024-01-13 16:36:30 -07:00
|
|
|
return process_param(parsed --[[@as luacats.Param]], self.generics)
|
2024-01-09 10:36:46 -07:00
|
|
|
elseif kind == 'return' then
|
2024-01-13 16:36:30 -07:00
|
|
|
return process_return(parsed --[[@as luacats.Return]], self.generics)
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-09 10:36:46 -07:00
|
|
|
error(string.format('unhandled parsed line %q: %s', line, parsed))
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @param line string
|
|
|
|
--- @param in_stream StreamRead
|
|
|
|
--- @return string
|
2024-01-13 16:36:30 -07:00
|
|
|
function Lua2DoxFilter:process_block_comment(line, in_stream)
|
2023-07-18 04:24:53 -07:00
|
|
|
local comment_parts = {} --- @type string[]
|
|
|
|
local done --- @type boolean?
|
|
|
|
|
|
|
|
while not done and not in_stream:eof() do
|
|
|
|
local thisComment --- @type string?
|
|
|
|
local closeSquare = line:find(']]')
|
|
|
|
if not closeSquare then -- need to look on another line
|
|
|
|
thisComment = line .. '\n'
|
|
|
|
line = in_stream:getLine()
|
2019-03-29 13:54:34 -07:00
|
|
|
else
|
2023-07-18 04:24:53 -07:00
|
|
|
thisComment = line:sub(1, closeSquare - 1)
|
|
|
|
done = true
|
|
|
|
|
|
|
|
-- unget the tail of the line
|
|
|
|
-- in most cases it's empty. This may make us less efficient but
|
|
|
|
-- easier to program
|
|
|
|
in_stream:ungetLine(vim.trim(line:sub(closeSquare + 2)))
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-12-04 13:38:31 -07:00
|
|
|
comment_parts[#comment_parts + 1] = thisComment
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
|
|
|
local comment = table.concat(comment_parts)
|
|
|
|
|
|
|
|
if comment:sub(1, 1) == '@' then -- it's a long magic comment
|
|
|
|
return '/*' .. comment .. '*/ '
|
|
|
|
end
|
|
|
|
|
|
|
|
-- discard
|
|
|
|
return '/* zz:' .. comment .. '*/ '
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @param line string
|
|
|
|
--- @return string
|
2024-01-13 16:36:30 -07:00
|
|
|
function Lua2DoxFilter:process_function_header(line)
|
2023-07-18 04:24:53 -07:00
|
|
|
local pos_fn = assert(line:find('function'))
|
|
|
|
-- we've got a function
|
|
|
|
local fn = removeCommentFromLine(vim.trim(line:sub(pos_fn + 8)))
|
|
|
|
|
|
|
|
if fn:sub(1, 1) == '(' then
|
|
|
|
-- it's an anonymous function
|
2023-12-04 13:38:31 -07:00
|
|
|
return '// ZZ: ' .. line
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
|
|
|
-- fn has a name, so is interesting
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
-- want to fix for iffy declarations
|
|
|
|
if fn:find('[%({]') then
|
|
|
|
-- we might have a missing close paren
|
|
|
|
if not fn:find('%)') then
|
|
|
|
fn = fn .. ' ___MissingCloseParenHere___)'
|
|
|
|
end
|
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
-- Big hax
|
|
|
|
if fn:find(':') then
|
|
|
|
fn = fn:gsub(':', '.', 1)
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
local paren_start = fn:find('(', 1, true)
|
|
|
|
local paren_finish = fn:find(')', 1, true)
|
|
|
|
|
|
|
|
-- Nothing in between the parens
|
|
|
|
local comma --- @type string
|
|
|
|
if paren_finish == paren_start + 1 then
|
|
|
|
comma = ''
|
|
|
|
else
|
|
|
|
comma = ', '
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2023-12-04 13:38:31 -07:00
|
|
|
fn = fn:sub(1, paren_start) .. 'self' .. comma .. fn:sub(paren_start + 1)
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
if line:match('local') then
|
|
|
|
-- Special: tell gen_vimdoc.py this is a local function.
|
|
|
|
return 'local_function ' .. fn .. '{}'
|
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
-- add vanilla function
|
|
|
|
return 'function ' .. fn .. '{}'
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2022-09-28 04:22:08 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @param line string
|
|
|
|
--- @param in_stream StreamRead
|
|
|
|
--- @return string?
|
2024-01-13 16:36:30 -07:00
|
|
|
function Lua2DoxFilter:process_line(line, in_stream)
|
2023-07-18 07:42:30 -07:00
|
|
|
local line_raw = line
|
|
|
|
line = vim.trim(line)
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
if vim.startswith(line, '---') then
|
2024-01-13 16:36:30 -07:00
|
|
|
return Lua2DoxFilter:process_magic(line:sub(4))
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
2023-02-04 07:58:38 -07:00
|
|
|
|
2023-12-04 13:38:31 -07:00
|
|
|
if vim.startswith(line, '--' .. '[[') then -- it's a long comment
|
2024-01-13 16:36:30 -07:00
|
|
|
return Lua2DoxFilter:process_block_comment(line:sub(5), in_stream)
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
2022-09-28 04:22:08 -07:00
|
|
|
|
2023-09-16 15:10:30 -07:00
|
|
|
-- Hax... I'm sorry
|
|
|
|
-- M.fun = vim.memoize(function(...)
|
|
|
|
-- ->
|
|
|
|
-- function M.fun(...)
|
|
|
|
line = line:gsub('^(.+) = .*_memoize%([^,]+, function%((.*)%)$', 'function %1(%2)')
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
if line:find('^function') or line:find('^local%s+function') then
|
2024-01-13 16:36:30 -07:00
|
|
|
return Lua2DoxFilter:process_function_header(line)
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
if not line:match('^local') then
|
|
|
|
local v = line_raw:match('^([A-Za-z][.a-zA-Z_]*)%s+%=')
|
|
|
|
if v and v:match('%.') then
|
|
|
|
-- Special: this lets gen_vimdoc.py handle tables.
|
2023-12-04 13:38:31 -07:00
|
|
|
return 'table ' .. v .. '() {}'
|
2023-07-18 07:42:30 -07:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
if #line > 0 then -- we don't know what this line means, so just comment it out
|
|
|
|
return '// zz: ' .. line
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
|
|
|
|
return ''
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
-- Processes the file and writes filtered output to stdout.
|
|
|
|
---@param filename string
|
|
|
|
function Lua2DoxFilter:filter(filename)
|
|
|
|
local in_stream = StreamRead.new(filename)
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2024-02-06 08:08:17 -07:00
|
|
|
local last_was_magic = false
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
while not in_stream:eof() do
|
2023-07-18 07:42:30 -07:00
|
|
|
local line = in_stream:getLine()
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2024-01-13 16:36:30 -07:00
|
|
|
local out_line = self:process_line(line, in_stream)
|
2023-07-18 04:24:53 -07:00
|
|
|
|
2023-07-18 07:42:30 -07:00
|
|
|
if not vim.startswith(vim.trim(line), '---') then
|
2024-01-13 16:36:30 -07:00
|
|
|
self:reset()
|
2023-07-18 04:24:53 -07:00
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
if out_line then
|
2024-02-06 08:08:17 -07:00
|
|
|
-- Ensure all magic blocks associate with some object to prevent doxygen
|
|
|
|
-- from getting confused.
|
|
|
|
if vim.startswith(out_line, '///') then
|
|
|
|
last_was_magic = true
|
|
|
|
else
|
|
|
|
if last_was_magic and out_line:match('^// zz: [^-]+') then
|
|
|
|
writeln('local_function _ignore() {}')
|
|
|
|
end
|
|
|
|
last_was_magic = false
|
|
|
|
end
|
2023-07-18 04:24:53 -07:00
|
|
|
writeln(out_line)
|
|
|
|
end
|
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
--- @class TApp
|
|
|
|
--- @field timestamp string|osdate
|
|
|
|
--- @field name string
|
|
|
|
--- @field version string
|
|
|
|
--- @field copyright string
|
|
|
|
--- this application
|
|
|
|
local TApp = {
|
|
|
|
timestamp = os.date('%c %Z', os.time()),
|
|
|
|
name = 'Lua2DoX',
|
|
|
|
version = '0.2 20130128',
|
2023-12-04 13:38:31 -07:00
|
|
|
copyright = 'Copyright (c) Simon Dales 2012-13',
|
2023-07-18 04:24:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
setmetatable(TApp, { __index = TApp })
|
|
|
|
|
|
|
|
function TApp:getRunStamp()
|
|
|
|
return self.name .. ' (' .. self.version .. ') ' .. self.timestamp
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
function TApp:getVersion()
|
|
|
|
return self.name .. ' (' .. self.version .. ') '
|
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
|
|
|
--main
|
|
|
|
|
2023-03-03 05:49:22 -07:00
|
|
|
if arg[1] == '--help' then
|
2023-07-18 04:24:53 -07:00
|
|
|
writeln(TApp:getVersion())
|
|
|
|
writeln(TApp.copyright)
|
|
|
|
writeln([[
|
2019-03-29 13:54:34 -07:00
|
|
|
run as:
|
2021-09-20 19:00:50 -07:00
|
|
|
nvim -l scripts/lua2dox.lua <param>
|
2019-03-29 13:54:34 -07:00
|
|
|
--------------
|
|
|
|
Param:
|
|
|
|
<filename> : interprets filename
|
|
|
|
--version : show version/copyright info
|
|
|
|
--help : this help text]])
|
2023-03-03 05:49:22 -07:00
|
|
|
elseif arg[1] == '--version' then
|
2023-07-18 04:24:53 -07:00
|
|
|
writeln(TApp:getVersion())
|
|
|
|
writeln(TApp.copyright)
|
2023-12-04 13:38:31 -07:00
|
|
|
else -- It's a filter.
|
2023-03-03 05:49:22 -07:00
|
|
|
local filename = arg[1]
|
|
|
|
|
|
|
|
if arg[2] == '--outdir' then
|
|
|
|
local outdir = arg[3]
|
2023-12-04 13:38:31 -07:00
|
|
|
if
|
|
|
|
type(outdir) ~= 'string'
|
|
|
|
or (0 ~= vim.fn.filereadable(outdir) and 0 == vim.fn.isdirectory(outdir))
|
|
|
|
then
|
2023-03-03 05:49:22 -07:00
|
|
|
error(('invalid --outdir: "%s"'):format(tostring(outdir)))
|
|
|
|
end
|
|
|
|
vim.fn.mkdir(outdir, 'p')
|
2023-07-18 04:24:53 -07:00
|
|
|
debug_outfile = string.format('%s/%s.c', outdir, vim.fs.basename(filename))
|
2023-03-03 05:49:22 -07:00
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
Lua2DoxFilter:filter(filename)
|
2023-03-03 05:49:22 -07:00
|
|
|
|
2023-07-18 04:24:53 -07:00
|
|
|
-- output the tail
|
|
|
|
writeln('// #######################')
|
|
|
|
writeln('// app run:' .. TApp:getRunStamp())
|
|
|
|
writeln('// #######################')
|
|
|
|
writeln()
|
|
|
|
|
|
|
|
if debug_outfile then
|
|
|
|
local f = assert(io.open(debug_outfile, 'w'))
|
|
|
|
f:write(table.concat(debug_output))
|
2023-03-03 05:49:22 -07:00
|
|
|
f:close()
|
|
|
|
end
|
2019-03-29 13:54:34 -07:00
|
|
|
end
|