ldoc.alias can specify a function which must return tag, value, modifiers like Item:check_tag. The alias error works with return groups

This commit is contained in:
steve donovan 2013-08-24 15:19:45 +02:00
parent 9af4bae066
commit 3c72ea112e
4 changed files with 48 additions and 28 deletions

View File

@ -149,18 +149,12 @@ function ldoc.tparam_alias (name,type)
ldoc.alias(name,{'param',modifiers={type=type}}) ldoc.alias(name,{'param',modifiers={type=type}})
end end
ldoc.alias ('error',function(tags,value,modifiers) ldoc.alias ('error',function(tags,value)
local t = List{'nil','error message'} local g = '2'
local oret = tags['return'] tags:add('return','',{[g]=true,type='nil'})
if not oret or type(oret) == 'string' then return 'return', value, {[g]=true,type='string'}
if oret then t:insert(1,oret) end
tags:add('return',t)
else
tags['return']:extend(t)
end
end) end)
ldoc.tparam_alias 'string' ldoc.tparam_alias 'string'
ldoc.tparam_alias 'number' ldoc.tparam_alias 'number'
ldoc.tparam_alias 'int' ldoc.tparam_alias 'int'

View File

@ -405,7 +405,7 @@ function Item:_init(tags,file,line)
self.tags = {} self.tags = {}
self.formal_args = tags.formal_args self.formal_args = tags.formal_args
tags.formal_args = nil tags.formal_args = nil
local iter = tags.iter or Map.Iter local iter = tags.iter or Map.iter
for tag in iter(tags) do for tag in iter(tags) do
self:set_tag(tag,tags[tag]) self:set_tag(tag,tags[tag])
end end
@ -494,7 +494,7 @@ function Item.check_tag(tags,tag, value, modifiers)
end end
end end
else -- has to be a function else -- has to be a function
alias(tags,value,modifiers) return alias(tags,value,modifiers)
end end
end end
local ttype = known_tags[tag] local ttype = known_tags[tag]
@ -793,7 +793,6 @@ function Item:build_return_groups()
for i,ret in ipairs(self.ret) do for i,ret in ipairs(self.ret) do
local mods = retmod[i] local mods = retmod[i]
local g = integer_keys(mods) local g = integer_keys(mods)
print(g,lastg)
if g ~= lastg then if g ~= lastg then
group = List() group = List()
groups:append(group) groups:append(group)
@ -801,7 +800,6 @@ function Item:build_return_groups()
end end
group:append({text=ret, type = mods.type or ''}) group:append({text=ret, type = mods.type or ''})
end end
print(groups)
self.retgroups = groups self.retgroups = groups
end end

View File

@ -70,7 +70,8 @@ local function parse_colon_tags (text)
return preamble,tag_items return preamble,tag_items
end end
-- Tags are stored as an ordered map -- Tags are stored as an ordered multi map from strings to strings
-- If the same key is used, then the value becomes a list
local Tags = {} local Tags = {}
Tags.__index = Tags Tags.__index = Tags
@ -89,9 +90,22 @@ function Tags.new (t,name)
return tags return tags
end end
function Tags:add (tag,value) function Tags:add (tag,value,modifiers)
if modifiers then -- how modifiers are encoded
value = {value,modifiers=modifiers}
end
local ovalue = rawget(self,tag)
if ovalue then -- previous value?
if getmetatable(ovalue) ~= List then
ovalue = List{ovalue}
end
ovalue:append(value)
value = ovalue
end
rawset(self,tag,value) rawset(self,tag,value)
self._order:append(tag) if not ovalue then
self._order:append(tag)
end
end end
function Tags:iter () function Tags:iter ()
@ -128,17 +142,8 @@ local function extract_tags (s,args)
if not value:match '\n[^\n]+\n' then if not value:match '\n[^\n]+\n' then
value = strip(value) value = strip(value)
end end
if modifiers then value = { value, modifiers=modifiers } end tags:add(tag,value,modifiers)
local old_value = tags[tag]
if not old_value then -- first element
tags:add(tag,value)
elseif type(old_value)=='table' and old_value.append then -- append to existing list
old_value :append (value)
else -- upgrade string->list
tags:add(tag,List{old_value, value})
end
end end
return tags --Map(tags) return tags --Map(tags)
end end

23
tests/styles/multiple.lua Normal file
View File

@ -0,0 +1,23 @@
------
-- Various ways of indicating errors
-- @module multiple
-----
-- function with return groups.
-- @treturn[1] string result
-- @return[2] nil
-- @return[2] error message
function mul1 () end
-----
-- function with return and error tag
-- @return result
-- @error message
function mul2 () end
-----
-- function that raises an error.
-- @string filename
-- @treturn string result
-- @raise 'file not found'
function mul3(filename) end