lua-users home
lua-l archive

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


Hi All,

I'm releasing the LPEG debugger I used while working on a "relaxed"
Lua parser. It's based on Patrick Donnelly's debugger [1] and includes
several improvements (thanks to the readers of this maillist who
helped with suggestions and explanations).

The module is available on github [2] and includes documentation with
simple examples and descriptions for supported options.

One of the issues I ran into is with function captures and folding
captures. As discussed earlier [3], it is to be expected, but I still
find it counterintuitive. Consider this simple example:

local lpeg = require('lpeg')
local grammar = { "Sum";
  Number = lpeg.R"09"^1 / tonumber;
  List = lpeg.V("Number") * ("," * lpeg.V("Number"))^0
    / function(...) print(select('#', ...)) return ... end;
  Sum = lpeg.Cf(lpeg.V("List"),
    function(acc, newvalue) return newvalue and acc + newvalue end);
}
print(lpeg.match(lpeg.P(grammar),"10,30,43"))

This prints "3\n10" as while the function capture for the List rule
gets three value (as there are three captures), there is no way to
return them as three captures; even when the function returns three
elements, they are seen by Cf() as one capture (so only the first
value is used). I think multiple values returned by function captures
need to be applied as the list of captures instead of one capture with
multiple values.

Paul.

[1] http://lua-users.org/lists/lua-l/2009-10/msg00774.html
[2] https://github.com/pkulchenko/PegDebug
[3] http://lua-users.org/lists/lua-l/2014-09/msg00119.html