  lua-l archive

• Subject: Re: lpeg accumulator
• From: Hisham <h@...>
• Date: Wed, 18 Sep 2013 17:15:58 -0300

```On 18 September 2013 16:17, Gavin Wraith <gavin@wra1th.plus.com> wrote:
> I tried to use lpeg.Cf with a non-numeric accumulator (in
> lpeg 0.12) thus:
>
>   #!lua
>   local digit = lpeg.R "09"
>   local num = lpeg.C(digit^1)/tonumber
>   local comma = lpeg.C ","
>   local acc = function (t,x,y)
>                 t = t + x
>                 t = t + y
>               end
>   local vec = lpeg.Cf (num * comma * num, acc)
>   local pat = lpeg.Cc { 0, 0} * (vec + 1)^0
>   local t = pat:match "Try (3,4) plus (100,200)"
>   print ("(", t, ",", t, ")")

I think this does what you want:

local digit = lpeg.R "09"
local num = lpeg.C(digit^1)/tonumber
local comma = lpeg.P ","
local acc = function (t, pair)
t = t + pair
t = t + pair
return t
end
local vec = lpeg.Ct( num * comma * num ) -- capture pair
local pat = lpeg.Cf( lpeg.Cc {0,0} * (vec + 1)^0 , acc)
local t = pat:match "Try (3,4) plus (100,200)"
print ("(", t, ",", t, ")")

-- Hisham

```