[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPEG: P(fct) seems not to consume input
- From: Albert Chan <albertmcchan@...>
- Date: Thu, 1 Nov 2018 08:01:02 -0400
> On Nov 1, 2018, at 1:02 AM, Dirk Laurie <dirk.laurie@gmail.com> wrote:
>> All this check is to avoid patt^n get into infinite loops.
>>
>> P(fct) does not know fct will skip forward.
>
> No, it doesn't — but it is obvious and easy for P(fct) to call
> fct("",1) and check that "false" is returned, and taking the message
> "may accept empty string" literally, that's all that the xhwxk should
> be worried about.
That will be hard to do, checking not just position 1, but all others.
Also, fct(s,i) first argument is the string to be matched, not ""
It is possible fct() might stop advancing for some (s,i).
I had a similar issue trying to patch Lpeg to go backward.
https://github.com/achan001/LPeg-anywhere
The solution is to assume moving back n positions also have fixedlen of -n.
Infinite loops can happen, but you gain flexibility with matching.
lua> lpeg = require 'lpeg' -- my patched lpeg
lua> -- 3 steps forward, 2 steps back, fixedlen = 3-2 = 1
lua> patt = lpeg.C(3) * lpeg.B(-2)
lua> lpeg.match(patt^0, '123456')
123 234 345 456