lua-users home
lua-l archive

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


Hi Rostislav,

> Problem is in combination lpeg.Cmt (used in debugger) and folder capture.
> lpeg.Cmt nested returned capture values inside a group capture (like
> lpeg.Cg). lpeg.Cf takes only first capture from this as accumulator and
> not call folder function, because remaining list of captures is empty.
> This is correct behaviour of lpeg.

Thank you for the explanation. It took me some time to parse it, but
the way I understand it is that it's equivalent to this example:

local grammar = { "Sum";
  Number = lpeg.R"09"^1 / tonumber;
  List = lpeg.Cmt(lpeg.V("Number") * ("," * lpeg.V("Number"))^0,
function(s,p,...) print("runtime", ...) return true, ... end);
  Sum = lpeg.Cf(lpeg.V("List"), function(acc, newvalue)
      print("folding", acc, newvalue)
      return acc + newvalue end);
}
print(lpeg.match(lpeg.P(grammar),"10,30,43")) --> 83

Because lpeg.Cmt() packs all the captures that would be folded into
it's own capture, there is nothing left to fold (only one element 10,
which is returned).

Is there any way to "unpack" it? Essentially, I would like to be able
to use run-time processing (don't need run-time capture per se) on
patterns that may be folded. It would be something like Cmt in terms
of run-time processing, but without the capture part.

Paul.