2011-07-30 11:01:42 +02:00
|
|
|
-- parsing code for doc comments
|
|
|
|
|
2012-10-28 18:51:00 +01:00
|
|
|
local List = require 'pl.List'
|
|
|
|
local Map = require 'pl.Map'
|
|
|
|
local stringio = require 'pl.stringio'
|
2011-07-30 11:01:42 +02:00
|
|
|
local lexer = require 'ldoc.lexer'
|
|
|
|
local tools = require 'ldoc.tools'
|
|
|
|
local doc = require 'ldoc.doc'
|
|
|
|
local Item,File = doc.Item,doc.File
|
|
|
|
|
|
|
|
------ Parsing the Source --------------
|
|
|
|
-- This uses the lexer from PL, but it should be possible to use Peter Odding's
|
|
|
|
-- excellent Lpeg based lexer instead.
|
|
|
|
|
|
|
|
local parse = {}
|
|
|
|
|
|
|
|
local tnext, append = lexer.skipws, table.insert
|
|
|
|
|
|
|
|
-- a pattern particular to LuaDoc tag lines: the line must begin with @TAG,
|
|
|
|
-- followed by the value, which may extend over several lines.
|
2011-09-19 13:34:01 +02:00
|
|
|
local luadoc_tag = '^%s*@(%a+)'
|
|
|
|
local luadoc_tag_value = luadoc_tag..'(.*)'
|
2011-10-04 14:54:08 +02:00
|
|
|
local luadoc_tag_mod_and_value = luadoc_tag..'%[(.*)%](.*)'
|
2011-07-30 11:01:42 +02:00
|
|
|
|
|
|
|
-- assumes that the doc comment consists of distinct tag lines
|
2013-02-13 13:15:29 +01:00
|
|
|
local function parse_at_tags(text)
|
2011-07-30 11:01:42 +02:00
|
|
|
local lines = stringio.lines(text)
|
|
|
|
local preamble, line = tools.grab_while_not(lines,luadoc_tag)
|
|
|
|
local tag_items = {}
|
|
|
|
local follows
|
|
|
|
while line do
|
2011-10-04 14:54:08 +02:00
|
|
|
local tag, mod_string, rest = line :match(luadoc_tag_mod_and_value)
|
|
|
|
if not tag then tag, rest = line :match (luadoc_tag_value) end
|
|
|
|
local modifiers
|
|
|
|
if mod_string then
|
|
|
|
modifiers = { }
|
|
|
|
for x in mod_string :gmatch "[^,]+" do
|
|
|
|
local k, v = x :match "^([^=]+)=(.*)$"
|
2013-03-19 14:00:57 +01:00
|
|
|
if not k then k, v = x, true end -- wuz x, x
|
2011-10-04 14:54:08 +02:00
|
|
|
modifiers[k] = v
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- follows: end of current tag
|
|
|
|
-- line: beginning of next tag (for next iteration)
|
2011-07-30 11:01:42 +02:00
|
|
|
follows, line = tools.grab_while_not(lines,luadoc_tag)
|
2011-10-04 14:54:08 +02:00
|
|
|
append(tag_items,{tag, rest .. '\n' .. follows, modifiers})
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
return preamble,tag_items
|
|
|
|
end
|
|
|
|
|
2012-12-31 13:52:00 +01:00
|
|
|
--local colon_tag = '%s*(%a+):%s'
|
|
|
|
local colon_tag = '%s*(%S-):%s'
|
2012-11-08 18:09:59 +01:00
|
|
|
local colon_tag_value = colon_tag..'(.*)'
|
|
|
|
|
2013-02-13 13:15:29 +01:00
|
|
|
local function parse_colon_tags (text)
|
2012-11-08 18:09:59 +01:00
|
|
|
local lines = stringio.lines(text)
|
|
|
|
local preamble, line = tools.grab_while_not(lines,colon_tag)
|
|
|
|
local tag_items, follows = {}
|
|
|
|
while line do
|
|
|
|
local tag, rest = line:match(colon_tag_value)
|
|
|
|
follows, line = tools.grab_while_not(lines,colon_tag)
|
2012-12-31 13:52:00 +01:00
|
|
|
local value = rest .. '\n' .. follows
|
|
|
|
if tag:match '^[%?!]' then
|
|
|
|
tag = tag:gsub('^!','')
|
|
|
|
value = tag .. ' ' .. value
|
|
|
|
tag = 'tparam'
|
|
|
|
end
|
|
|
|
append(tag_items,{tag, value})
|
2012-11-08 18:09:59 +01:00
|
|
|
end
|
|
|
|
return preamble,tag_items
|
|
|
|
end
|
|
|
|
|
2012-12-29 11:07:26 +01:00
|
|
|
local Tags = {}
|
|
|
|
Tags.__index = Tags
|
|
|
|
|
|
|
|
function Tags.new (t)
|
|
|
|
t._order = List()
|
|
|
|
return setmetatable(t,Tags)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Tags:add (tag,value)
|
|
|
|
self[tag] = value
|
|
|
|
--print('adding',tag,value)
|
|
|
|
self._order:append(tag)
|
|
|
|
end
|
|
|
|
|
|
|
|
function Tags:iter ()
|
|
|
|
return self._order:iter()
|
|
|
|
end
|
|
|
|
|
2011-07-30 11:01:42 +02:00
|
|
|
-- This takes the collected comment block, and uses the docstyle to
|
|
|
|
-- extract tags and values. Assume that the summary ends in a period or a question
|
|
|
|
-- mark, and everything else in the preamble is the description.
|
|
|
|
-- If a tag appears more than once, then its value becomes a list of strings.
|
|
|
|
-- Alias substitution and @TYPE NAME shortcutting is handled by Item.check_tag
|
2013-01-02 12:04:14 +01:00
|
|
|
local function extract_tags (s,args)
|
2012-11-08 18:09:59 +01:00
|
|
|
local preamble,tag_items
|
2011-07-30 11:01:42 +02:00
|
|
|
if s:match '^%s*$' then return {} end
|
2013-01-25 08:09:40 +01:00
|
|
|
if args.colon then --and s:match ':%s' and not s:match '@%a' then
|
2012-11-08 18:09:59 +01:00
|
|
|
preamble,tag_items = parse_colon_tags(s)
|
|
|
|
else
|
|
|
|
preamble,tag_items = parse_at_tags(s)
|
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
local strip = tools.strip
|
2011-08-22 13:26:27 +02:00
|
|
|
local summary, description = preamble:match('^(.-[%.?])(%s.+)')
|
|
|
|
if not summary then
|
|
|
|
-- perhaps the first sentence did not have a . or ? terminating it.
|
|
|
|
-- Then try split at linefeed
|
|
|
|
summary, description = preamble:match('^(.-\n\n)(.+)')
|
|
|
|
if not summary then
|
|
|
|
summary = preamble
|
|
|
|
end
|
|
|
|
end -- and strip(description) ?
|
2012-12-29 11:07:26 +01:00
|
|
|
local tags = Tags.new{summary=summary and strip(summary) or '',description=description or ''}
|
2011-07-30 11:01:42 +02:00
|
|
|
for _,item in ipairs(tag_items) do
|
2011-12-06 18:19:09 +01:00
|
|
|
local tag, value, modifiers = Item.check_tag(tags,unpack(item))
|
2013-03-05 16:00:33 +01:00
|
|
|
-- treat multiline values more gently..
|
|
|
|
if not value:match '\n[^\n]+\n' then
|
|
|
|
value = strip(value)
|
|
|
|
end
|
2011-12-06 18:19:09 +01:00
|
|
|
|
2011-10-04 14:54:08 +02:00
|
|
|
if modifiers then value = { value, modifiers=modifiers } end
|
2011-07-30 11:01:42 +02:00
|
|
|
local old_value = tags[tag]
|
2011-09-19 13:34:01 +02:00
|
|
|
|
2011-10-04 14:54:08 +02:00
|
|
|
if not old_value then -- first element
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add(tag,value)
|
2011-12-06 18:19:09 +01:00
|
|
|
elseif type(old_value)=='table' and old_value.append then -- append to existing list
|
|
|
|
old_value :append (value)
|
2011-10-04 14:54:08 +02:00
|
|
|
else -- upgrade string->list
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add(tag,List{old_value, value})
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
end
|
2012-12-29 11:07:26 +01:00
|
|
|
return tags --Map(tags)
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
|
2011-12-06 18:19:09 +01:00
|
|
|
local _xpcall = xpcall
|
|
|
|
if true then
|
|
|
|
_xpcall = function(f) return true, f() end
|
|
|
|
end
|
|
|
|
|
2011-07-30 11:01:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
-- parses a Lua or C file, looking for ldoc comments. These are like LuaDoc comments;
|
|
|
|
-- they start with multiple '-'. (Block commments are allowed)
|
|
|
|
-- If they don't define a name tag, then by default
|
|
|
|
-- it is assumed that a function definition follows. If it is the first comment
|
|
|
|
-- encountered, then ldoc looks for a call to module() to find the name of the
|
|
|
|
-- module if there isn't an explicit module name specified.
|
|
|
|
|
2012-12-10 13:45:54 +01:00
|
|
|
local function parse_file(fname, lang, package, args)
|
2011-07-30 11:01:42 +02:00
|
|
|
local line,f = 1
|
|
|
|
local F = File(fname)
|
|
|
|
local module_found, first_comment = false,true
|
2011-12-06 18:19:09 +01:00
|
|
|
local current_item, module_item
|
2011-07-30 11:01:42 +02:00
|
|
|
|
2013-01-28 10:23:31 +01:00
|
|
|
F.args = args
|
|
|
|
|
2012-12-10 13:45:54 +01:00
|
|
|
F.base = package
|
|
|
|
|
2011-07-30 11:01:42 +02:00
|
|
|
local tok,f = lang.lexer(fname)
|
2012-03-13 11:23:55 +01:00
|
|
|
if not tok then return nil end
|
2011-07-30 11:01:42 +02:00
|
|
|
|
2012-03-23 13:21:43 +01:00
|
|
|
local function lineno ()
|
2011-08-14 11:54:40 +02:00
|
|
|
return tok:lineno()
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
2011-08-14 11:54:40 +02:00
|
|
|
|
2012-03-23 13:21:43 +01:00
|
|
|
local function filename () return fname end
|
2011-07-30 11:01:42 +02:00
|
|
|
|
2011-08-28 15:37:06 +02:00
|
|
|
function F:warning (msg,kind,line)
|
2011-07-30 11:01:42 +02:00
|
|
|
kind = kind or 'warning'
|
2011-08-28 15:37:06 +02:00
|
|
|
line = line or lineno()
|
2011-12-09 14:43:37 +01:00
|
|
|
io.stderr:write(fname..':'..line..': '..msg,'\n')
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
function F:error (msg)
|
|
|
|
self:warning(msg,'error')
|
2011-12-09 14:43:37 +01:00
|
|
|
io.stderr:write('LDoc error\n')
|
2011-07-30 11:01:42 +02:00
|
|
|
os.exit(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
local function add_module(tags,module_found,old_style)
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('name',module_found)
|
|
|
|
tags:add('class','module')
|
2011-08-27 10:11:41 +02:00
|
|
|
local item = F:new_item(tags,lineno())
|
2011-07-30 11:01:42 +02:00
|
|
|
item.old_style = old_style
|
2011-12-06 18:19:09 +01:00
|
|
|
module_item = item
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local mod
|
|
|
|
local t,v = tnext(tok)
|
2013-01-02 12:04:14 +01:00
|
|
|
-- with some coding styles first comment is standard boilerplate; option to ignore this.
|
|
|
|
if args.boilerplate and t == 'comment' then
|
|
|
|
t,v = tnext(tok)
|
|
|
|
end
|
|
|
|
if t == '#' then -- skip Lua shebang line, if present
|
2012-03-16 13:42:58 +01:00
|
|
|
while t and t ~= 'comment' do t,v = tnext(tok) end
|
|
|
|
if t == nil then
|
|
|
|
F:warning('empty file')
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
if lang.parse_module_call and t ~= 'comment'then
|
|
|
|
while t and not (t == 'iden' and v == 'module') do
|
|
|
|
t,v = tnext(tok)
|
|
|
|
end
|
|
|
|
if not t then
|
2012-12-10 13:45:54 +01:00
|
|
|
if not args.ignore then
|
|
|
|
F:warning("no module() call found; no initial doc comment")
|
|
|
|
end
|
|
|
|
--return nil
|
2011-07-30 11:01:42 +02:00
|
|
|
else
|
|
|
|
mod,t,v = lang:parse_module_call(tok,t,v)
|
|
|
|
if mod ~= '...' then
|
|
|
|
add_module({summary='(no description)'},mod,true)
|
|
|
|
first_comment = false
|
|
|
|
module_found = true
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2011-12-06 18:19:09 +01:00
|
|
|
local ok, err = xpcall(function()
|
2011-07-30 11:01:42 +02:00
|
|
|
while t do
|
|
|
|
if t == 'comment' then
|
|
|
|
local comment = {}
|
|
|
|
local ldoc_comment,block = lang:start_comment(v)
|
|
|
|
|
|
|
|
if ldoc_comment and block then
|
|
|
|
t,v = lang:grab_block_comment(v,tok)
|
|
|
|
end
|
|
|
|
|
|
|
|
if lang:empty_comment(v) then -- ignore rest of empty start comments
|
|
|
|
t,v = tok()
|
|
|
|
end
|
|
|
|
|
|
|
|
while t and t == 'comment' do
|
|
|
|
v = lang:trim_comment(v)
|
|
|
|
append(comment,v)
|
|
|
|
t,v = tok()
|
|
|
|
if t == 'space' and not v:match '\n' then
|
|
|
|
t,v = tok()
|
|
|
|
end
|
|
|
|
end
|
2011-09-19 19:23:53 +02:00
|
|
|
|
2011-07-30 11:01:42 +02:00
|
|
|
if t == 'space' then t,v = tnext(tok) end
|
|
|
|
|
2011-09-19 19:23:53 +02:00
|
|
|
local item_follows, tags, is_local, case
|
2012-12-21 17:23:18 +01:00
|
|
|
if ldoc_comment then
|
2011-07-30 11:01:42 +02:00
|
|
|
comment = table.concat(comment)
|
2013-03-26 10:53:42 +01:00
|
|
|
if comment:match '^%s*$' then
|
|
|
|
ldoc_comment = nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if ldoc_comment then
|
2011-08-21 17:29:56 +02:00
|
|
|
if first_comment then
|
|
|
|
first_comment = false
|
|
|
|
else
|
2011-09-19 19:23:53 +02:00
|
|
|
item_follows, is_local, case = lang:item_follows(t,v,tok)
|
2011-08-21 17:29:56 +02:00
|
|
|
end
|
2012-12-31 13:52:00 +01:00
|
|
|
if item_follows or comment:find '@' or comment:find ': ' then
|
2013-01-02 12:04:14 +01:00
|
|
|
tags = extract_tags(comment,args)
|
2013-03-18 14:51:28 +01:00
|
|
|
-- explicitly named @module (which is recommended)
|
2011-07-30 11:01:42 +02:00
|
|
|
if doc.project_level(tags.class) then
|
|
|
|
module_found = tags.name
|
2013-03-18 14:51:28 +01:00
|
|
|
-- might be a module returning a single function!
|
|
|
|
if tags.param or tags['return'] then
|
|
|
|
local parms, ret, summ = tags.param, tags['return'],tags.summary
|
|
|
|
tags.param = nil
|
|
|
|
tags['return'] = nil
|
|
|
|
tags.summary = nil
|
|
|
|
add_module(tags,tags.name,false)
|
|
|
|
tags = {
|
|
|
|
summary = summ,
|
|
|
|
name = 'returns...',
|
|
|
|
class = 'function',
|
|
|
|
['return'] = ret,
|
|
|
|
param = parms
|
|
|
|
}
|
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
2011-09-18 20:45:36 +02:00
|
|
|
doc.expand_annotation_item(tags,current_item)
|
2011-09-17 19:13:47 +02:00
|
|
|
-- if the item has an explicit name or defined meaning
|
|
|
|
-- then don't continue to do any code analysis!
|
2011-07-30 11:04:54 +02:00
|
|
|
if tags.name then
|
2011-12-06 18:19:09 +01:00
|
|
|
if not tags.class then
|
|
|
|
F:warning("no type specified, assuming function: '"..tags.name.."'")
|
2012-12-29 11:07:26 +01:00
|
|
|
tags:add('class','function')
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
item_follows, is_local = false, false
|
2011-12-06 18:19:09 +01:00
|
|
|
elseif lang:is_module_modifier (tags) then
|
|
|
|
if not item_follows then
|
|
|
|
F:warning("@usage or @export followed by unknown code")
|
|
|
|
break
|
|
|
|
end
|
2011-09-19 15:53:00 +02:00
|
|
|
item_follows(tags,tok)
|
2011-12-06 18:19:09 +01:00
|
|
|
local res, value, tagname = lang:parse_module_modifier(tags,tok,F)
|
2011-12-09 14:43:37 +01:00
|
|
|
if not res then F:warning(value); break
|
2011-09-19 15:53:00 +02:00
|
|
|
else
|
2011-12-06 18:19:09 +01:00
|
|
|
if tagname then
|
2011-12-09 14:43:37 +01:00
|
|
|
module_item:set_tag(tagname,value)
|
2011-12-06 18:19:09 +01:00
|
|
|
end
|
2011-09-19 15:53:00 +02:00
|
|
|
-- don't continue to make an item!
|
|
|
|
ldoc_comment = false
|
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
-- some hackery necessary to find the module() call
|
|
|
|
if not module_found and ldoc_comment then
|
|
|
|
local old_style
|
|
|
|
module_found,t,v = lang:find_module(tok,t,v)
|
|
|
|
-- right, we can add the module object ...
|
|
|
|
old_style = module_found ~= nil
|
|
|
|
if not module_found or module_found == '...' then
|
|
|
|
-- we have to guess the module name
|
|
|
|
module_found = tools.this_module_name(package,fname)
|
|
|
|
end
|
2013-01-02 12:04:14 +01:00
|
|
|
if not tags then tags = extract_tags(comment,args) end
|
2011-07-30 11:01:42 +02:00
|
|
|
add_module(tags,module_found,old_style)
|
|
|
|
tags = nil
|
|
|
|
if not t then
|
2013-03-23 16:51:55 +01:00
|
|
|
F:warning('contains no items','warning',1)
|
2011-07-30 11:01:42 +02:00
|
|
|
break;
|
|
|
|
end -- run out of file!
|
|
|
|
-- if we did bump into a doc comment, then we can continue parsing it
|
|
|
|
end
|
|
|
|
|
|
|
|
-- end of a block of document comments
|
|
|
|
if ldoc_comment and tags then
|
2011-12-06 18:19:09 +01:00
|
|
|
local line = t ~= nil and lineno()
|
2011-07-30 11:01:42 +02:00
|
|
|
if t ~= nil then
|
|
|
|
if item_follows then -- parse the item definition
|
2012-11-08 18:09:59 +01:00
|
|
|
local err = item_follows(tags,tok)
|
|
|
|
if err then F:error(err) end
|
2011-07-30 11:01:42 +02:00
|
|
|
else
|
2011-09-19 19:23:53 +02:00
|
|
|
lang:parse_extra(tags,tok,case)
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
end
|
2011-12-06 18:19:09 +01:00
|
|
|
if is_local or tags['local'] then
|
|
|
|
tags['local'] = true
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
if tags.name then
|
2011-09-18 20:45:36 +02:00
|
|
|
current_item = F:new_item(tags,line)
|
|
|
|
current_item.inferred = item_follows ~= nil
|
2011-12-09 14:43:37 +01:00
|
|
|
if doc.project_level(tags.class) then
|
|
|
|
if module_item then
|
|
|
|
F:error("Module already declared!")
|
|
|
|
end
|
|
|
|
module_item = current_item
|
|
|
|
end
|
2011-07-30 11:01:42 +02:00
|
|
|
end
|
|
|
|
if not t then break end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
if t ~= 'comment' then t,v = tok() end
|
|
|
|
end
|
2011-12-06 18:19:09 +01:00
|
|
|
end,debug.traceback)
|
|
|
|
if not ok then return F, err end
|
2011-07-30 11:01:42 +02:00
|
|
|
if f then f:close() end
|
|
|
|
return F
|
|
|
|
end
|
|
|
|
|
|
|
|
function parse.file(name,lang, args)
|
2012-12-10 13:45:54 +01:00
|
|
|
local F,err = parse_file(name,lang,args.package,args)
|
2012-03-13 11:23:55 +01:00
|
|
|
if err or not F then return F,err end
|
2011-12-06 18:19:09 +01:00
|
|
|
local ok,err = xpcall(function() F:finish() end,debug.traceback)
|
|
|
|
if not ok then return F,err end
|
2011-07-30 11:01:42 +02:00
|
|
|
return F
|
|
|
|
end
|
|
|
|
|
|
|
|
return parse
|