lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


I also had a need to retrieve the source for a given lua function.
The two approaches I could see were:

1) Write a lua parser
Given the line start, you could just parse the code until you get to the function end, and lua isn't all that complex a language...

2) Modify the lua distribution to expose both the line start AND the line end for a given function. This is the approach I went with. Lua's parser has both of those pieces of information available, but only stores the line start for the debug info.

In the function
static void body (LexState *ls, expdesc *e, int needself, int line) // lparser.c, Lua 5 source code after the call to check_match(ls, TK_END, TK_FUNCTION, line), ls->lastline contains the line number of the end of the function. I simply added a lineDefined_end field in addition to the already existing lineDefined field in the function metadata... grep for lineDefined in the source and you can see the obvious changes you'd need to make. There aren't too many. Note: if you modify ldump and lundump to store this extra data field (I did not), you will introduce a binary incompatibility with a stock lua version, which you may or may not care about.


I'm not happy with making the changes, and will remove them if a better alternative comes up... Can anyone think of another option? Is lua's own code parser exposed to lua code for use?

--
Tim Gogolin


On Jul 17, 2004, at 4:08 AM, Adrian Sietsma wrote:

I wish to store configurations for a process as a set of lua callbacks (functions). This works perfectly.

Now, i wish to use a c++ gui interface to create / view these callbacks. I can load them into a table, retrieve their names, etc, but not the source.

I have used the debug info to get the source file and start line, but it doesn't give me the end line. I can see how i can solve this via the debug hooks, but before i try this -

1/ Has anyone already solved this one ?
2/ Is there a simpler way ?

ps i have a simple sax-type c++ xml parser that i have built a lua wrapper for (with lua callbacks), if anyone's interested. The default implementation parses xml into a lua table using names such as _tag, _attribs, etc. There is also a wrapper function which allows addressing the table using the xml node names.

Adrian