[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPEG: P(fct) seems not to consume input
- From: Dirk Laurie <dirk.laurie@...>
- Date: Wed, 31 Oct 2018 22:18:42 +0200
Op Wo., 31 Okt. 2018 om 21:49 het Sean Conner <sean@conman.org> geskryf:
>
> It was thus said that the Great Dirk Laurie once stated:
> > I define a function to be used with LPEG that simply returns the first
> > character. It _cannot_ match the empty string:
> >
> > function fct(str)
> > if #str==0 then return false
> > else return 2,str:sub(1,1)
> > end
> > end
> So the function should be:
>
> function fct(subject,position,capture)
> if #subject == 0 then
> return false
> else
> return position,subject:sub(position-1,position-1)
> end
> end
Aha! I don't get *str+pos, I get *str,pos.
Let me try that in my notation:
function fct(str,pos)
if #str<pos then return false
else return pos+1, str:sub(pos,pos)
end
end
patt=lpeg.P(fct)
(patt^-4):match"abc" --> a b c [as expected, no fourth value]
But:
(patt^0):match"abc" --> stdin:1: loop body may accept empty string
I don't understand why I get that.