Module:Module introspection: Difference between revisions

Ganaram inukshuk (talk | contribs)
mNo edit summary
Ganaram inukshuk (talk | contribs)
find function params
Line 137: Line 137:


-- Helper function
-- Helper function
-- Find functions provided by a module, ignoring nested/local functions
-- Find functions provided by a module, ignoring nested/local functions.
-- For each function found this way, find the params for that function.
function p.find_functions(code)
function p.find_functions(code)
local funcs = {}
local funcs = {}
Line 146: Line 147:


-- Functions of the form module_name.func are considered.
-- Functions of the form module_name.func are considered.
-- module_name is usually p, but could be something else, such as arguments
-- module_name is usually p, but it may be something else. Find that package
-- so detect that alternate name, or fall back to p.
-- name, or fall back to p.
for var in code:gmatch([[local%s+([%w_]+)%s*=%s*{}]]) do
for var in code:gmatch([[local%s+([%w_]+)%s*=%s*{}]]) do
module_name = var
module_name = var
Line 157: Line 158:
line_num = line_num + 1
line_num = line_num + 1


-- Match functions defined as: function p.name(
-- Match functions defined as: function p.name(param1, param2)
local name = line:match("function%s+" .. module_name .. "%.([%w_]+)%s*%(")
local name, params_str = line:match("function%s+" .. module_name .. "%.([%w_]+)%s*%(([^)]*)%)")
if name then
if name then
table.insert(funcs, { name = name, line = line_num })
local params = {}
for param in params_str:gmatch("([%w_]+)") do
table.insert(params, param)
end
table.insert(funcs, { name = name, line = line_num, params = params })
end
end


-- Match functions defined as: p.name = function(
-- Match functions defined as: p.name = function(param1, param2)
name = line:match(module_name .. "%.([%w_]+)%s*=%s*function%s*%(")
name, params_str = line:match(module_name .. "%.([%w_]+)%s*=%s*function%s*%(([^)]*)%)")
if name then
if name then
table.insert(funcs, { name = name, line = line_num })
local params = {}
for param in params_str:gmatch("([%w_]+)") do
table.insert(params, param)
end
table.insert(funcs, { name = name, line = line_num, params = params })
end
end
end
end