lua-users home
lua-l archive

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

You might be interested in reusing some code I wrote recently. It is a
Lua/LPEG creation to parse C++ source files, extract information, and
then output it in wiki markup. For example, given a C++ file looking
something along the lines of:

static int l_map_getsize(lua_State *L)
    THMap* pMap = luaT_testuserdata<THMap>(L);
    lua_pushinteger(L, pMap->getWidth());
    lua_pushinteger(L, pMap->getHeight());
    return 2;
// Then later on:
luaT_class(THMap, l_map_new, "map", iMapMT);
luaT_setfunction(l_map_getsize, "size");
// ...

The Lua/LPEG script can parse the above, and work out that the Lua
class "map" wraps the C++ class "THMap", and that the "size" method on
a Lua "map" class calls the "l_map_getsize" glue function, which in
turn calls THMap::getWidth and THMap::getHeight. The final output
would be the following Google Code wiki markup:

=== ===
Wraps the C++ class *{{{THMap}}}*.
|| *Lua method* || *C++ function(s)* || *Glue function* ||
|| *{{{size}}}* || *{{{THMap::getWidth}}}*, *{{{THMap::getHeight}}}*
|| *{{{l_map_getsize}}}* ||

You can find the source code at -
there are three Lua files there:
c_tokenise.lua - The LPEG grammar for turning a C/C++ file into an
array of tokens. Also has code for pretty printing a token array as
HTML, but that code can be ignored.
helpers.lua - A collection of helper functions. The most important of
these is tokens_gfind, which provides functionality similar to
string.gmatch - it finds a string of tokens in an array of tokens,
ignoring whitespace tokens.
main.lua - The actual application logic

Let me know if it proves useful,

On Fri, Sep 18, 2009 at 12:43 PM, Ben Harper <> wrote:
> I want to parse C++ header files (not for classes, just Plain Old C
> functions), and automatically generate entry points for another language.
> Has anybody created an LPEG grammar capable of parsing a reasonable subset
> of C++?
> Thanks,
> Ben