lua-users home
lua-l archive

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


On 23 September 2014 19:13, Paul K <paul@zerobrane.com> wrote:
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.

Check the rest of the arguments to your fold function :)