lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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.