6 | 6 |
local path = require 'pl.path'
|
7 | 7 |
local utils = require 'pl.utils'
|
8 | 8 |
local tablex = require 'pl.tablex'
|
|
9 |
local stringx = require 'pl.stringx'
|
9 | 10 |
local tools = {}
|
10 | 11 |
local M = tools
|
11 | 12 |
local append = table.insert
|
|
261 | 262 |
|
262 | 263 |
local tnext = lexer.skipws
|
263 | 264 |
|
264 | |
local function type_of (tok) return tok[1] end
|
|
265 |
local function type_of (tok) return tok and tok[1] or 'end' end
|
265 | 266 |
local function value_of (tok) return tok[2] end
|
266 | 267 |
|
267 | 268 |
-- This parses Lua formal argument lists. It will return a list of argument
|
|
278 | 279 |
if not ltl or #ltl[1] == 0 then return args end -- no arguments
|
279 | 280 |
|
280 | 281 |
local function set_comment (idx,tok)
|
281 | |
local text = value_of(tok):gsub('%s*$','')
|
|
282 |
local text = stringx.rstrip(value_of(tok)) --
|
282 | 283 |
local current_comment = args.comments[args[idx]]
|
283 | 284 |
if current_comment then
|
284 | 285 |
text = text:match("%s*%-%-+%s*(.*)")
|
|
288 | 289 |
end
|
289 | 290 |
end
|
290 | 291 |
|
|
292 |
local function fetch_comment (tl)
|
|
293 |
return
|
|
294 |
end
|
|
295 |
|
291 | 296 |
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
|
294 | 298 |
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
|
298 | 307 |
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]))
|
300 | 313 |
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
|
309 | 322 |
end
|
310 | 323 |
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
|
316 | 334 |
local n = #args
|
317 | |
if not args.comments[n] then
|
|
335 |
local last_arg = args[n]
|
|
336 |
if not args.comments[last_arg] then
|
318 | 337 |
while true do
|
319 | 338 |
local t = {tok()}
|
320 | 339 |
if type_of(t) == 'comment' then
|
|
325 | 344 |
end
|
326 | 345 |
end
|
327 | 346 |
end
|
328 | |
|
|
347 |
--]]
|
329 | 348 |
return args
|
330 | 349 |
end
|
331 | 350 |
|