[prev in list] [next in list] [prev in thread] [next in thread] 

List:       lua-l
Subject:    Re: lpeg.Cg, lpeg.Cb, and how to visualize what they do
From:       Patrick Donnelly <batrick () batbytes ! com>
Date:       2023-08-21 20:48:32
Message-ID: CACh33FqeRaV8gy6CNt3knjd2G4dSYKRbcP8kOLiJaQoNbVKE-g () mail ! gmail ! com
[Download RAW message or body]

Hi Roberto,

On Mon, Aug 21, 2023, 2:00 PM Roberto Ierusalimschy <roberto@inf.puc-rio.br>
wrote:

> > Anyway, in the code below the tests C1..C3 and D1..D4 are based in
> > your examples. In the block D1..D4 only D4 yields an error, and that
> > makes sense to me - but in the block C1..C3 the tests C1 and C3 yield
> > errors but C2 does not. Is this behavior of C1..C3 something that is
> > explained in the documentation? How? I admit that I'm still struggling
> > with some terms, sorry...
> >
> > The code:
> >
> >   [...]
> >
> >   (Cb"c"      )     :pm()                   -- C1
> >   (Cb"c":Cg"c")     :pm()                   -- C2
> >   (Cb"c":Cg"c"):Ct():pm()                   -- C3
>
> C2 is really subtle. I first thought that was a bug :-)
>
> All three cases are wrong. In C2, however, as the back reference is
> inside a named group, the named group just throws away its inner
> capture, without evaluating it. The manual allows that:
>
>   "Usually, LPeg does not specify when (and if) it evaluates its captures."
>
> As the back reference is not evaluated, no error is raised. (Again, this
> highlights that captures are computations; it is hard to express them
> statically...)
>

You know documentation is of high quality when the author consults it to
justify program behavior. :)

I would request that the manual be enhanced with:

  "Usually, LPeg does not specify if, when, or how many times it evaluates
its captures."

It bit me long ago that Cb may reevaluate Ct. I.e.:

Cg(Ct(), "foo") * Cb("foo")

May produce one or two unique tables. An example in the manual like the
above would be very useful to communicate that I think.

Please forgive terseness and possibly buggy example code as I'm on my phone.

Patrick

>

[Attachment #3 (text/html)]

<div dir="auto"><div>Hi Roberto,<br><br><div class="gmail_quote"><div dir="ltr" \
class="gmail_attr">On Mon, Aug 21, 2023, 2:00 PM Roberto Ierusalimschy &lt;<a \
href="mailto:roberto@inf.puc-rio.br">roberto@inf.puc-rio.br</a>&gt; \
wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex">&gt; Anyway, in the code below the \
tests C1..C3 and D1..D4 are based in<br> &gt; your examples. In the block D1..D4 only \
D4 yields an error, and that<br> &gt; makes sense to me - but in the block C1..C3 the \
tests C1 and C3 yield<br> &gt; errors but C2 does not. Is this behavior of C1..C3 \
something that is<br> &gt; explained in the documentation? How? I admit that I&#39;m \
still struggling<br> &gt; with some terms, sorry...<br>
&gt; <br>
&gt; The code:<br>
&gt; <br>
&gt;     [...]<br>
&gt; <br>
&gt;     (Cb&quot;c&quot;         )        :pm()                             -- \
C1<br> &gt;     (Cb&quot;c&quot;:Cg&quot;c&quot;)        :pm()                        \
-- C2<br> &gt;     (Cb&quot;c&quot;:Cg&quot;c&quot;):Ct():pm()                        \
-- C3<br> <br>
C2 is really subtle. I first thought that was a bug :-)<br>
<br>
All three cases are wrong. In C2, however, as the back reference is<br>
inside a named group, the named group just throws away its inner<br>
capture, without evaluating it. The manual allows that:<br>
<br>
   &quot;Usually, LPeg does not specify when (and if) it evaluates its \
captures.&quot;<br> <br>
As the back reference is not evaluated, no error is raised. (Again, this<br>
highlights that captures are computations; it is hard to express them<br>
statically...)<br></blockquote></div></div><div dir="auto"><br></div><div \
dir="auto">You know documentation is of high quality when the author consults it to \
justify program behavior. :)</div><div dir="auto"><br></div><div dir="auto">I would \
request that the manual be enhanced with:</div><div dir="auto"><br></div><div \
dir="auto">   &quot;Usually, LPeg does not specify if, when, or how many times it \
evaluates its captures.&quot;</div><div dir="auto"><br></div><div dir="auto">It bit \
me long ago that Cb may reevaluate Ct. I.e.:</div><div dir="auto"><br></div><div \
dir="auto">Cg(Ct(), &quot;foo&quot;) * Cb(&quot;foo&quot;)</div><div \
dir="auto"><br></div><div dir="auto">May produce one or two unique tables. An example \
in the manual like the above would be very useful to communicate that I \
think.</div><div dir="auto"><br></div><div dir="auto">Please forgive terseness and \
possibly buggy example code as I&#39;m on my phone.</div><div \
dir="auto"><br></div><div dir="auto">Patrick</div><div dir="auto"><div \
class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 \
.8ex;border-left:1px #ccc solid;padding-left:1ex"> </blockquote></div></div></div>



[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic