[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: LPEG: P(fct) seems not to consume input
- From: Sean Conner <sean@...>
- Date: Wed, 31 Oct 2018 16:43:21 -0400
It was thus said that the Great Dirk Laurie once stated:
> 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
function fct(str,pos)
if #str < pos then
return false
else
return pos + 1 , str:sub(pos,pos)
end
end
There, fixed that for you 8-P
> 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.
I don't either. When I get that error, I start making changes to the code
until the error goes away and the code does what I want. You could try:
(patt^1):match"abc" + lpeg.Cc(false)
-spc