basic support for documenting C. If you put 'parse_extra={C=true}' in config.ld, then it will attempt to extract function name, arguments and types from the source

This commit is contained in:
Steve Donovan 2013-09-20 13:33:06 +02:00
parent 47e6a2094e
commit d2e40e2f2f
6 changed files with 98 additions and 7 deletions

View File

@ -198,7 +198,7 @@ local ldoc_contents = {
'readme','all','manual_url', 'ignore', 'colon', 'sort', 'module_file','vars', 'readme','all','manual_url', 'ignore', 'colon', 'sort', 'module_file','vars',
'boilerplate','merge', 'wrap', 'not_luadoc', 'template_escape','merge_error_groups', 'boilerplate','merge', 'wrap', 'not_luadoc', 'template_escape','merge_error_groups',
'no_return_or_parms','no_summary','full_description','backtick_references', 'custom_see_handler', 'no_return_or_parms','no_summary','full_description','backtick_references', 'custom_see_handler',
'no_space_before_args', 'no_space_before_args','parse_extra',
} }
ldoc_contents = tablex.makeset(ldoc_contents) ldoc_contents = tablex.makeset(ldoc_contents)
@ -355,6 +355,7 @@ local function process_file (f, flist)
local ftype = file_types[ext] local ftype = file_types[ext]
if ftype then if ftype then
if args.verbose then print(f) end if args.verbose then print(f) end
ftype.extra = ldoc.parse_extra or {}
local F,err = parse.file(f,ftype,args) local F,err = parse.file(f,ftype,args)
if err then if err then
if F then if F then
@ -544,6 +545,7 @@ end)
ldoc.single = modcount == 1 and first_module or nil ldoc.single = modcount == 1 and first_module or nil
--do return end
-------- three ways to dump the object graph after processing ----- -------- three ways to dump the object graph after processing -----

View File

@ -659,6 +659,21 @@ function Item:finish()
self:warning("undocumented formal argument: "..quote(fargs[i])) self:warning("undocumented formal argument: "..quote(fargs[i]))
end end end end
end end
-- formal arguments may come with types, inferred by the
-- appropriate code in ldoc.lang
if fargs.types then
self.modifiers[field] = List()
for t in fargs.types:iter() do
self:add_type(field,t)
end
if fargs.return_type then
if not self.ret then -- type, but no comment; no worries
self.ret = List{''}
end
self.modifiers['return'] = List()
self:add_type('return',fargs.return_type)
end
end
end -- #fargs > 0 end -- #fargs > 0
end -- fargs end -- fargs
@ -701,6 +716,10 @@ function Item:finish()
end end
end end
function Item:add_type(field,type)
self.modifiers[field]:append {type = type}
end
-- ldoc allows comments in the formal arg list to be used, if they aren't specified with @param -- ldoc allows comments in the formal arg list to be used, if they aren't specified with @param
-- Further, these comments may start with a type followed by a colon, and are then equivalent -- Further, these comments may start with a type followed by a colon, and are then equivalent
-- to a @tparam -- to a @tparam
@ -709,7 +728,7 @@ function Item:parse_argument_comment (comment,field)
comment = comment:gsub('^%-+%s*','') comment = comment:gsub('^%-+%s*','')
local type,rest = comment:match '([^:]+):(.*)' local type,rest = comment:match '([^:]+):(.*)'
if type then if type then
self.modifiers[field]:append {type = type} self:add_type(field,type)
comment = rest comment = rest
end end
end end

View File

@ -266,6 +266,54 @@ function CC:grab_block_comment(v,tok)
return 'comment',v:sub(1,-3) return 'comment',v:sub(1,-3)
end end
--- here the argument name is always last, and the type is composed of any tokens before
function CC:extract_arg (tl,idx)
idx = idx or 1
local res = List()
for i = idx,#tl-1 do
res:append(tl[i][2])
end
local type = res:join ' '
return tl[#tl][2], type
end
function CC:item_follows (t,v,tok)
if not self.extra.C then
return false
end
if t == 'iden' or t == 'keyword' then --
if v == self.extra.export then -- this is not part of the return type!
t,v = tnext(tok)
end
-- TBD collecting types which are not single tokens (may contain '*')
local return_type = v
t,v = tnext(tok)
if t == 'iden' or t=='keyword' then
local name = v
t,v = tnext(tok)
if t ~= '(' then
return_type = return_type .. ' ' .. name
name = v
t,v = tnext(tok)
end
print ('got',name,t,v,return_type)
return function(tags,tok)
if not tags.name then
tags:add('name',name)
end
tags:add('class','function')
if t == '(' then
tags.formal_args,t,v = tools.get_parameters(tok,')',',',self)
if return_type ~= 'void' then
tags.formal_args.return_type = return_type
end
end
end
end
end
return false
end
local Moon = class(Lua) local Moon = class(Lua)
function Moon:_init() function Moon:_init()

View File

@ -371,6 +371,7 @@ function lexer.cpp(s,filter,options)
{'^|=',tdump}, {'^|=',tdump},
{'^%^=',tdump}, {'^%^=',tdump},
{'^::',tdump}, {'^::',tdump},
{'^%.%.%.',tdump},
{'^.',tdump} {'^.',tdump}
} }
end end

2
ldoc/project.ldoc.mode Normal file
View File

@ -0,0 +1,2 @@
mode=lua
tabs=use=false,size=3

View File

@ -284,7 +284,7 @@ local function value_of (tok) return tok[2] end
-- following the arguments. ldoc will use these in addition to explicit -- following the arguments. ldoc will use these in addition to explicit
-- param tags. -- param tags.
function M.get_parameters (tok,endtoken,delim) function M.get_parameters (tok,endtoken,delim,lang)
tok = M.space_skip_getter(tok) tok = M.space_skip_getter(tok)
local args = List() local args = List()
args.comments = {} args.comments = {}
@ -292,9 +292,19 @@ function M.get_parameters (tok,endtoken,delim)
if not ltl or not ltl[1] or #ltl[1] == 0 then return args end -- no arguments if not ltl or not ltl[1] or #ltl[1] == 0 then return args end -- no arguments
local function strip_comment (text) local strip_comment, extract_arg
if lang then
strip_comment = utils.bind1(lang.trim_comment,lang)
extract_arg = utils.bind1(lang.extract_arg,lang)
else
strip_comment = function(text)
return text:match("%s*%-%-+%s*(.*)") return text:match("%s*%-%-+%s*(.*)")
end end
extract_arg = function(tl,idx)
return value_of(tl[idx or 1])
end
end
local function set_comment (idx,tok) local function set_comment (idx,tok)
local text = stringx.rstrip(value_of(tok)) local text = stringx.rstrip(value_of(tok))
@ -307,6 +317,15 @@ function M.get_parameters (tok,endtoken,delim)
args.comments[arg] = text args.comments[arg] = text
end end
local function add_arg (tl,idx)
local name, type = extract_arg(tl,idx)
args:append(name)
if type then
if not args.types then args.types = List() end
args.types:append(type)
end
end
for i = 1,#ltl do for i = 1,#ltl do
local tl = ltl[i] -- token list for argument local tl = ltl[i] -- token list for argument
if #tl > 0 then if #tl > 0 then
@ -323,10 +342,10 @@ function M.get_parameters (tok,endtoken,delim)
j = j + 1 j = j + 1
end end
if #tl > 1 then if #tl > 1 then
args:append(value_of(tl[j])) add_arg(tl,j)
end end
else else
args:append(value_of(tl[1])) add_arg(tl,1)
end end
if i == #ltl and #tl > 1 then if i == #ltl and #tl > 1 then
while j <= #tl and type_of(tl[j]) ~= 'comment' do while j <= #tl and type_of(tl[j]) ~= 'comment' do