[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Lpeg query
- From: Gavin Wraith <gavin@...>
- Date: Sun, 19 Jan 2014 15:36:02 GMT
Have I have got this right? For X an lpeg pattern and s a string, then,
by default, X:match (s) returns a number (index of first failure) unless
X has captures, in which case the captures are returned. So in the code:
require "lpeg"
local P, C = lpeg.P, lpeg.C
local SPACE, END = P " ", P (-1)
local BLANK, WORD = SPACE^0, (1 - SPACE)^1
local WORDS = BLANK*(C(WORD)*BLANK)^0*END
the table
{ WORDS:match " alpha beta " }
is { "alpha", "beta" } but the table
{ WORDS:match " " }
is { 2 }. I would have liked { } for smoothness of notation. This seems
to be a trap for unwary persons like myself.
What is the most elegant way to amend WORDS so that { WORDS:match " " }
has the value { }? Or is that not possible seeing that the operation
of composing values has no unit; i.e. no value @ so that @,x and x,@ are
just x?
Does it make any sense to ask for a variant of Lpeg in which lpeg.match
does not return a number but nil by default? The capture lpeg.Cp could
then be used to produce the current behaviour.
If this has been discussed or explained before, I apologize for the
noise.
--
Gavin Wraith (gavin@wra1th.plus.com)
Home page: http://www.wra1th.plus.com/