lua-users home
lua-l archive

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


Deal all,

as far as I understand the documentation on fold captures in LPEG (http://www.inf.puc-rio.br/~roberto/lpeg/#cap-f), the folding function should be called if there is at least one capture in the pattern passed to lpeg.Cf ().

However, the real minimum seems to be two.

The following code
tostring (lpeg.P{'sum'
	  	, sum	= lpeg.Cf ( lpeg.V'operand' * lpeg.Cg ( lpeg.V'operator' * lpeg.V'operand' ) ^ 0, function (arg1, op, arg2)
	  		if op == '+' then
	  			return arg1 + arg2
	  		elseif op == '-' then
	  			return arg1 - arg2
	  		elseif op == nil then
				-- Special case: there is only one number in the _expression_:
				return 0
	  		end
		end )
	  , operand	= lpeg.C ( lpeg.R'09' ^ 1 / tonumber)
	  , operator = lpeg.C (lpeg.S'+-')
	}:match '4')

returns '4' (the whole line) rather than '0' whivh it is supposed to return if the string consists of only one number without arithmetc signs, which means that the folding function was never called.

Alexander Mashin