1 -------------------------------------------------------------------------------
2 -- @release $Id: standard.lua,v 1.39 2007/12/21 17:50:48 tomas Exp $
3 -------------------------------------------------------------------------------
5 local assert, pairs, tostring, type = assert, pairs, tostring, type
6 local io = require "io"
7 local posix = require "posix"
8 local luadoc = require "luadoc"
9 local util = require "luadoc.util"
10 local tags = require "luadoc.taglet.standard.tags"
11 local string = require "string"
12 local table = require "table"
14 module 'luadoc.taglet.standard'
16 -------------------------------------------------------------------------------
17 -- Creates an iterator for an array base on a class type.
18 -- @param t array to iterate over
19 -- @param class name of the class to iterate over
21 function class_iterator (t, class)
25 while t[i] and t[i].class ~= class do
35 -- Patterns for function recognition
36 local identifiers_list_pattern = "%s*(.-)%s*"
37 local identifier_pattern = "[^%(%s]+"
38 local function_patterns = {
39 "^()%s*function%s*("..identifier_pattern..")%s*%("..identifiers_list_pattern.."%)",
40 "^%s*(local%s)%s*function%s*("..identifier_pattern..")%s*%("..identifiers_list_pattern.."%)",
41 "^()%s*("..identifier_pattern..")%s*%=%s*function%s*%("..identifiers_list_pattern.."%)",
44 -------------------------------------------------------------------------------
45 -- Checks if the line contains a function definition
46 -- @param line string with line text
47 -- @return function information or nil if no function definition found
49 local function check_function (line)
50 line = util.trim(line)
52 local info = table.foreachi(function_patterns, function (_, pattern)
53 local r, _, l, id, param = string.find(line, pattern)
57 private = (l == "local"),
58 param = util.split("%s*,%s*", param),
63 -- TODO: remove these assert's?
65 assert(info.name, "function name undefined")
66 assert(info.param, string.format("undefined parameter list for function `%s'", info.name))
72 -------------------------------------------------------------------------------
73 -- Checks if the line contains a module definition.
74 -- @param line string with line text
75 -- @param currentmodule module already found, if any
76 -- @return the name of the defined module, or nil if there is no module
79 local function check_module (line, currentmodule)
80 line = util.trim(line)
90 local r, _, modulename = string.find(line, "^module%s*[%s\"'(%[]+([^,\"')%]]+)")
92 -- found module definition
93 logger:debug(string.format("found module `%s'", modulename))
99 -------------------------------------------------------------------------------
100 -- Extracts summary information from a description. The first sentence of each
101 -- doc comment should be a summary sentence, containing a concise but complete
102 -- description of the item. It is important to write crisp and informative
103 -- initial sentences that can stand on their own
104 -- @param description text with item description
105 -- @return summary string or nil if description is nil
107 local function parse_summary (description)
108 -- summary is never nil...
109 description = description or ""
111 -- append an " " at the end to make the pattern work in all cases
112 description = description.." "
114 -- read until the first period followed by a space or tab
115 local summary = string.match(description, "(.-%.)[%s\t]")
117 -- if pattern did not find the first sentence, summary is the whole description
118 summary = summary or description
123 -------------------------------------------------------------------------------
124 -- @param f file handle
125 -- @param line current line being parsed
126 -- @param modulename module already found, if any
127 -- @return current line
128 -- @return code block
129 -- @return modulename if found
131 local function parse_code (f, line, modulename)
134 if string.find(line, "^[\t ]*%-%-%-") then
135 -- reached another luadoc block, end this parsing
136 return line, code, modulename
138 -- look for a module definition
139 modulename = check_module(line, modulename)
141 table.insert(code, line)
145 -- reached end of file
146 return line, code, modulename
149 -------------------------------------------------------------------------------
150 -- Parses the information inside a block comment
151 -- @param block block with comment field
152 -- @return block parameter
154 local function parse_comment (block, first_line, modulename)
156 -- get the first non-empty line of code
157 local code = table.foreachi(block.code, function(_, line)
158 if not util.line_empty(line) then
159 -- `local' declarations are ignored in two cases:
160 -- when the `nolocals' option is turned on; and
161 -- when the first block of a file is parsed (this is
162 -- necessary to avoid confusion between the top
163 -- local declarations and the `module' definition.
164 if (options.nolocals or first_line) and line:find"^%s*local" then
171 -- parse first line of code
173 local func_info = check_function(code)
174 local module_name = check_module(code)
176 block.class = "function"
177 block.name = func_info.name
178 block.param = func_info.param
179 block.private = func_info.private
180 elseif module_name then
181 block.class = "module"
182 block.name = module_name
188 -- TODO: comment without any code. Does this means we are dealing
189 -- with a file comment?
193 local currenttag = "description"
196 table.foreachi(block.comment, function (_, line)
197 line = util.trim_comment(line)
199 local r, _, tag, text = string.find(line, "@([_%w%.]+)%s+(.*)")
201 -- found new tag, add previous one, and start a new one
202 -- TODO: what to do with invalid tags? issue an error? or log a warning?
203 tags.handle(currenttag, block, currenttext)
208 currenttext = util.concat(currenttext, line)
209 assert(string.sub(currenttext, 1, 1) ~= " ", string.format("`%s', `%s'", currenttext, line))
212 tags.handle(currenttag, block, currenttext)
214 -- extracts summary information from the description
215 block.summary = parse_summary(block.description)
216 assert(string.sub(block.description, 1, 1) ~= " ", string.format("`%s'", block.description))
218 if block.name and block.class == "module" then
219 modulename = block.name
222 return block, modulename
225 -------------------------------------------------------------------------------
226 -- Parses a block of comment, started with ---. Read until the next block of
228 -- @param f file handle
229 -- @param line being parsed
230 -- @param modulename module already found, if any
232 -- @return block parsed
233 -- @return modulename if found
235 local function parse_block (f, line, modulename, first)
242 if string.find(line, "^[\t ]*%-%-") == nil then
243 -- reached end of comment, read the code below it
244 -- TODO: allow empty lines
245 line, block.code, modulename = parse_code(f, line, modulename)
247 -- parse information in block comment
248 block, modulename = parse_comment(block, first, modulename)
250 return line, block, modulename
252 table.insert(block.comment, line)
256 -- reached end of file
258 -- parse information in block comment
259 block, modulename = parse_comment(block, first, modulename)
261 return line, block, modulename
264 -------------------------------------------------------------------------------
265 -- Parses a file documented following luadoc format.
266 -- @param filepath full path of file to parse
267 -- @param doc table with documentation
268 -- @return table with documentation
270 function parse_file (filepath, doc, handle, prev_line, prev_block, prev_modname)
271 local blocks = { prev_block }
272 local modulename = prev_modname
275 local f = handle or io.open(filepath, "r")
277 local line = prev_line or f:read()
281 if string.find(line, "^[\t ]*%-%-%-") then
282 -- reached a luadoc block
283 local block, newmodname
284 line, block, newmodname = parse_block(f, line, modulename, first)
286 if modulename and newmodname and newmodname ~= modulename then
287 doc = parse_file( nil, doc, f, line, block, newmodname )
289 table.insert(blocks, block)
290 modulename = newmodname
293 -- look for a module definition
294 local newmodname = check_module(line, modulename)
296 if modulename and newmodname and newmodname ~= modulename then
297 parse_file( nil, doc, f )
299 modulename = newmodname
302 -- TODO: keep beginning of file somewhere
315 -- store blocks in file hierarchy
316 assert(doc.files[filepath] == nil, string.format("doc for file `%s' already defined", filepath))
317 table.insert(doc.files, filepath)
318 doc.files[filepath] = {
322 -- functions = class_iterator(blocks, "function"),
323 -- tables = class_iterator(blocks, "table"),
326 local first = doc.files[filepath].doc[1]
327 if first and modulename then
328 doc.files[filepath].author = first.author
329 doc.files[filepath].copyright = first.copyright
330 doc.files[filepath].description = first.description
331 doc.files[filepath].release = first.release
332 doc.files[filepath].summary = first.summary
336 -- if module definition is found, store in module hierarchy
337 if modulename ~= nil then
338 if modulename == "..." then
339 assert( filepath, "Can't determine name for virtual module from filepatch" )
340 modulename = string.gsub (filepath, "%.lua$", "")
341 modulename = string.gsub (modulename, "/", ".")
343 if doc.modules[modulename] ~= nil then
344 -- module is already defined, just add the blocks
345 table.foreachi(blocks, function (_, v)
346 table.insert(doc.modules[modulename].doc, v)
349 -- TODO: put this in a different module
350 table.insert(doc.modules, modulename)
351 doc.modules[modulename] = {
355 -- functions = class_iterator(blocks, "function"),
356 -- tables = class_iterator(blocks, "table"),
357 author = first and first.author,
358 copyright = first and first.copyright,
360 release = first and first.release,
364 -- find module description
365 for m in class_iterator(blocks, "module")() do
366 doc.modules[modulename].description = util.concat(
367 doc.modules[modulename].description,
369 doc.modules[modulename].summary = util.concat(
370 doc.modules[modulename].summary,
373 doc.modules[modulename].author = m.author
376 doc.modules[modulename].copyright = m.copyright
379 doc.modules[modulename].release = m.release
382 doc.modules[modulename].name = m.name
385 doc.modules[modulename].description = doc.modules[modulename].description or (first and first.description) or ""
386 doc.modules[modulename].summary = doc.modules[modulename].summary or (first and first.summary) or ""
389 -- make functions table
390 doc.modules[modulename].functions = {}
391 for f in class_iterator(blocks, "function")() do
393 table.insert(doc.modules[modulename].functions, f.name)
394 doc.modules[modulename].functions[f.name] = f
399 doc.modules[modulename].tables = {}
400 for t in class_iterator(blocks, "table")() do
402 table.insert(doc.modules[modulename].tables, t.name)
403 doc.modules[modulename].tables[t.name] = t
409 -- make functions table
410 doc.files[filepath].functions = {}
411 for f in class_iterator(blocks, "function")() do
413 table.insert(doc.files[filepath].functions, f.name)
414 doc.files[filepath].functions[f.name] = f
419 doc.files[filepath].tables = {}
420 for t in class_iterator(blocks, "table")() do
422 table.insert(doc.files[filepath].tables, t.name)
423 doc.files[filepath].tables[t.name] = t
431 -------------------------------------------------------------------------------
432 -- Checks if the file is terminated by ".lua" or ".luadoc" and calls the
433 -- function that does the actual parsing
434 -- @param filepath full path of the file to parse
435 -- @param doc table with documentation
436 -- @return table with documentation
439 function file (filepath, doc)
440 local patterns = { "%.lua$", "%.luadoc$" }
441 local valid = table.foreachi(patterns, function (_, pattern)
442 if string.find(filepath, pattern) ~= nil then
448 logger:info(string.format("processing file `%s'", filepath))
449 doc = parse_file(filepath, doc)
455 -------------------------------------------------------------------------------
456 -- Recursively iterates through a directory, parsing each file
457 -- @param path directory to search
458 -- @param doc table with documentation
459 -- @return table with documentation
461 function directory (path, doc)
462 for f in posix.files(path) do
463 local fullpath = path .. "/" .. f
464 local attr = posix.stat(fullpath)
465 assert(attr, string.format("error stating file `%s'", fullpath))
467 if attr.type == "regular" then
468 doc = file(fullpath, doc)
469 elseif attr.type == "directory" and f ~= "." and f ~= ".." then
470 doc = directory(fullpath, doc)
476 -- Recursively sorts the documentation table
477 local function recsort (tab)
479 -- sort list of functions by name alphabetically
480 for f, doc in pairs(tab) do
481 if doc.functions then
482 table.sort(doc.functions)
485 table.sort(doc.tables)
490 -------------------------------------------------------------------------------
492 function start (files, doc)
493 assert(files, "file list not specified")
495 -- Create an empty document, or use the given one
500 assert(doc.files, "undefined `files' field")
501 assert(doc.modules, "undefined `modules' field")
503 table.foreachi(files, function (_, path)
504 local attr = posix.stat(path)
505 assert(attr, string.format("error stating path `%s'", path))
507 if attr.type == "regular" then
508 doc = file(path, doc)
509 elseif attr.type == "directory" then
510 doc = directory(path, doc)
514 -- order arrays alphabetically