Codebase list lua-ldoc / 5fbe671
table field inference borked by recent changes; tests/simple/tables.lua is now behaving better steve donovan 11 years ago
3 changed file(s) with 71 addition(s) and 27 deletion(s). Raw diff Collapse all Expand all
66 local path = require 'pl.path'
77 local utils = require 'pl.utils'
88 local tablex = require 'pl.tablex'
9 local stringx = require 'pl.stringx'
910 local tools = {}
1011 local M = tools
1112 local append = table.insert
261262
262263 local tnext = lexer.skipws
263264
264 local function type_of (tok) return tok[1] end
265 local function type_of (tok) return tok and tok[1] or 'end' end
265266 local function value_of (tok) return tok[2] end
266267
267268 -- This parses Lua formal argument lists. It will return a list of argument
278279 if not ltl or #ltl[1] == 0 then return args end -- no arguments
279280
280281 local function set_comment (idx,tok)
281 local text = value_of(tok):gsub('%s*$','')
282 local text = stringx.rstrip(value_of(tok)) --
282283 local current_comment = args.comments[args[idx]]
283284 if current_comment then
284285 text = text:match("%s*%-%-+%s*(.*)")
288289 end
289290 end
290291
292 local function fetch_comment (tl)
293 return
294 end
295
291296 for i = 1,#ltl do
292 --print('check',i,ltl[i],#ltl[i])
293 local tl = ltl[i]
297 local tl = ltl[i] -- token list for argument
294298 if #tl > 0 then
295 for j = 1, #tl - 1 do
296 if type_of(tl[j]) ~= "comment" then
297 return nil, "Couldn't parse function arguments"
299 local j = 1
300 if type_of(tl[1]) == 'comment' then
301 -- the comments for the i-1 th arg are in the i th arg...
302 if i > 1 then
303 while type_of(tl[j]) == 'comment' do
304 set_comment(i-1,tl[j])
305 j = j + 1
306 end
298307 end
299 set_comment(i-1,tl[j])
308 if #tl > 1 then
309 args:append(value_of(tl[j]))
310 end
311 else
312 args:append(value_of(tl[1]))
300313 end
301 if type_of(tl[#tl]) ~= "iden" and type_of(tl[#tl]) ~= "..." then
302 return nil, "Couldn't parse function arguments"
303 end
304 args:append(value_of(tl[#tl]))
305 if i == #ltl then
306 local last_tok = tl[#tl]
307 if #tl > 1 and type_of(last_tok) == 'comment' then
308 set_comment(i,last_tok)
314 if i == #ltl and #tl > 1 then
315 while j <= #tl and type_of(tl[j]) ~= 'comment' do
316 j = j + 1
317 end
318 if j > #tl then break end -- was no comments!
319 while type_of(tl[j]) == 'comment' do
320 set_comment(i,tl[j])
321 j = j + 1
309322 end
310323 end
311 end
312 end
313
314 if #args == 1 or next(args.comments) then -- we had argument comments
315 -- but the last one may be outside the parens! (Geoff style)
324 else
325 return nil,"empty argument"
326 end
327 end
328
329 ----[[
330 -- we had argument comments
331 -- but the last one may be outside the parens! (Geoff style)
332 -- (only try this stunt if it's a function parameter list!)
333 if (not endtoken or endtoken == ')') and (#args > 0 or next(args.comments)) then
316334 local n = #args
317 if not args.comments[n] then
335 local last_arg = args[n]
336 if not args.comments[last_arg] then
318337 while true do
319338 local t = {tok()}
320339 if type_of(t) == 'comment' then
325344 end
326345 end
327346 end
328
347 --]]
329348 return args
330349 end
331350
2222 -rm -r $(LUA_SHAREDIR)/ldoc
2323 -rm $(LUA_BINDIR)/ldoc
2424
25 test: test-basic test-example test-md test-tables
2526
26 test: test-basic test-example test-md
27
28 RUN=&& ldoc . && diff -r docs cdocs && echo ok
27 # cp -r docs cdocs in these directories first...
28 RUN=&& ldoc . && diff -r docs cdocs && echo ok
2929
3030 test-basic:
3131 cd tests $(RUN)
3535
3636 test-md:
3737 cd tests && cd md-test $(RUN)
38
39 test-tables:
40 cd tests && cd simple $(RUN)
00 ------------
11 -- A module containing tables.
22 -- Shows how Lua table definitions can be conveniently parsed.
3 --
4 -- There may be multiple comment lines per field/parameter, and
5 -- such comments may begin with `TYPE:`
6 --
7 -- Functions also can be commented in a similar way, and the last
8 -- parameter's comment may be outside the parens.
9 --
310 -- @alias M
411
512 local tables = {}
613 local M = tables
714
8 --- first table
15 --- a function.
16 function M.one(
17 bonzo, -- dog
18 -- has its day!
19 frodo) --baggins
20 end
21
22 --- first table.
923 -- @table one
1024 M.one = {
1125 A = 1, -- alpha
1226 B = 2; -- beta
1327 }
1428
29 --- second table.
30 -- we don't need an explicit table tag, since it
31 -- can be inferred from the context.
32 M.two = {
33 N = 10, -- int: no. of cases
34 L = 'label' -- string: case label
35 }
36
1537 return M
1638