tags:

views:

73

answers:

3

I'm generating some (non-html) documentation for a Lua library that I developed. I will generate the documentation by hand, but I'd appreciate some kind of automation if possible (i.e. generating skeletons for each function so I can fill them in)

I'd like to know if there's a way for lua to know the names of the parameters that a function takes, from outside it.

For example, is there a way to do this in Lua?

function foo(x,y)
  ... -- any code here
end

print( something ... foo ... something)
-- expected output: "x", "y"

Thanks a lot.

+1  A: 

Take a look at debug.getinfo, but you probably need a parser for this task. I don't know of any way to fetch the parameters of a function from within Lua without actually running the function and inspecting its environment table (see debug.debug and debug.getlocal).

Judge Maygarden
Thanks. Are those supposed to be links to somewhere?
egarcia
Yes, they are links to the appropriate sections of the online Lua reference manual.
Judge Maygarden
I'll try with debug.getinfo. Thanks!
egarcia
+1  A: 

Take a look at the luadoc utility. It is sort of like Doxygen, but for Lua. It is intended to allow the documentation to be written in-line with the source code, but it could certainly be used to produce a template of the documentation structure to be fleshed out separately. Of course, the template mechanism will leave you with a maintenance issue down the road...

RBerteig
My experiences with `luadoc` have been poor. I've instead been writing documentation as metadata within modules. For the poster's question, I think lhf's answer is definitive.
Norman Ramsey
Luadoc is, um, quirky for sure. Lua doesn't make it easy to get good documentation signatures for functions, and most of luadoc's quirks relate to that. I've had related problems with documentation for Lua libraries written in C where Doxygen is no help at all since all Lua-callable functions have the same opaque signature.
RBerteig
@Norman Ramsey: I'd like to know more about that "metatada in modules" you talk about. Do you have any sample/documentation?
egarcia
@Norman Ramsey: I made a different question for this: http://stackoverflow.com/questions/3100062/lua-metadata-for-documentation
egarcia
+2  A: 

Try my bytecode inspector library. In Lua 5.2 you'll be able to use debug.getlocal.

lhf
I think I'll stick with lua's debug functionalities for now. But thanks for your answer. I think it can be useful for other people finding this question.
egarcia