lua-users home
lua-l archive

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


> You can probably see what I am trying to do. Is the problem my 
> poor coding or the type restriction on lpeg.Cf? 
> Is the restriction necessary?

There is no such restriction. The problem is the coding (and the poor
documentation for the library ;) Follows a correct version:

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

Corrections:

- 'comma' should not capture the comma;
- 'acc' must return the new value for the accumulator, even if it
  is equal to the previous value;
- the pair of numbers captured in 'vec' must be inside a group,
  otherwise the accumulator will try to process one by one;
- The initial capture for 't' (the one created by 'Cc') must be
  inside 'Cf' (not outside).

-- Roberto