[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPeg and function capture
- From: Roberto Ierusalimschy <roberto@...>
- Date: Sun, 20 May 2012 11:11:19 -0300
> Posting in the same thread, as it is related to my experiments.
> I am puzzled by a result I got.
> This works as expected:
>
> local cap = m.R'AZ'
> local findCap = (1 - cap) + m.C(cap)
> local toMatch = "Stairway To Heaven"
> local patt = findCap^0 / function (a, b, c)
> return string.lower(a .. b .. c)
> end
> print(m.match(patt, toMatch))
>
> But the following doesn't, the function gets all chars, not only the
> capital ones (those supposed to be captured):
>
> local patt = (findCap / function (a) return string.lower(a) end)^0
> print(m.match(patt, toMatch))
>
> Why the behavior of the pattern is changed by the associated function?
In all cases, the function is called once everytime there is a match.
In the second case, there is match for each character (because of the
(1 - cap) in 'findCap'). When the pattern of a function capture
produces no captures, the entire match is given as the captured value.
Maybe this is what you whant:
local findCap = (1 - cap) + m.C(cap) / string.lower
Now the function is called only when 'm.C(cap)' matches.
-- Roberto