Re: m4p: an implementation of GNU m4 in Python

Nikolaos Chatzikonstantinou <[email protected]> Mon, 12 May 2025 10:44:41 -0500
Newsgroups gmane.comp.gnu.m4.general
Message-ID <CAAQmekcvFDHBb-kJWZKiSDUyo9=m2qka3oTjWs7y5tV3vFd6Hw@mail.gmail.com>
--00000000000063b4c60634f23263
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

On Mon, May 12, 2025, 10:36=E2=80=AFAM Eric Blake <[email protected]> wrote=
:

> On Mon, May 12, 2025 at 10:24:12AM -0500, Eric Blake wrote:
> > On Mon, May 12, 2025 at 09:54:54AM -0500, Nikolaos Chatzikonstantinou
> wrote:
> > > > $ echo 'eval(1 || (1/0))' | m4-1.4.20
> > > > 1
> > > > $ echo 'eval(1 || (1/0))' | m4p
> > > > m4:stdin:1: divide by zero in eval: 1 || (1/0)
> > > > 1
> > > >
> > >
> > > I have a file where I noted some of these here:
> > >
> https://codeberg.org/annoyingusername/m4p/src/branch/main/tests/resources=
/eval_bugs.txt
> >
> > Useful.  The '1 || (0 / 0)' bug was indeed fixed in 1.4.20.  '1 || 2
> > &=3D 3' still complains about invalid operator, but that's intentional:
> > &=3D is not a supported operator.  But the complaint about '1 || (2 &=
=3D
> > 3)') complaining about a missing right parenthesis (instead of a bad
> > operator) is something I hadn't noticed before today, and something sti=
ll
> > present in 1.4.20.  The parser is correctly detecting that the
> > expression is not valid at the point it hits the invalid operator; but
> > perhaps the error message could be improved (by stating that it
> > expects ')' instead of '&=3D', for example).
> >
> > Writing a parser that handles only good expressions is easy.  Writing
> > a parser that provides sane error messages with useful suggestions
> > when the parse fails is hard.  Yours may be the first (indirect) bug
> > report against 1.4.20!
>
> And here's my first proposed patch for your project:
>
> diff --git i/m4p/m4_eval.py w/m4p/m4_eval.py
> index 91f97e7..6f2e565 100644
> --- i/m4p/m4_eval.py
> +++ w/m4p/m4_eval.py
> @@ -168,7 +168,7 @@ calc_grammar =3D """
>           | "++" atom        -> invalid_unary_operator
>           | atom "--"        -> invalid_unary_operator
>           | atom "++"        -> invalid_unary_operator
> -         | "(" equation ")"
> +         | "(" assignment ")"
>
>      %import common.WS_INLINE
>
>
> so that 'echo "eval((1&3))" | m4p' outputs 1 instead of an error.
>

Very nice! Thank you.

Regards,
Nikolaos Chatzikonstantinou

>

--00000000000063b4c60634f23263
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"auto"><div><div class=3D"gmail_quote gmail_quote_container"><di=
v dir=3D"ltr" class=3D"gmail_attr">On Mon, May 12, 2025, 10:36=E2=80=AFAM E=
ric Blake &lt;<a href=3D"mailto:[email protected]">[email protected]</a>&gt=
; wrote:<br></div><blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .=
8ex;border-left:1px #ccc solid;padding-left:1ex">On Mon, May 12, 2025 at 10=
:24:12AM -0500, Eric Blake wrote:<br>
&gt; On Mon, May 12, 2025 at 09:54:54AM -0500, Nikolaos Chatzikonstantinou =
wrote:<br>
&gt; &gt; &gt; $ echo &#39;eval(1 || (1/0))&#39; | m4-1.4.20<br>
&gt; &gt; &gt; 1<br>
&gt; &gt; &gt; $ echo &#39;eval(1 || (1/0))&#39; | m4p<br>
&gt; &gt; &gt; m4:stdin:1: divide by zero in eval: 1 || (1/0)<br>
&gt; &gt; &gt; 1<br>
&gt; &gt; &gt;<br>
&gt; &gt; <br>
&gt; &gt; I have a file where I noted some of these here:<br>
&gt; &gt; <a href=3D"https://codeberg.org/annoyingusername/m4p/src/branch/m=
ain/tests/resources/eval_bugs.txt" rel=3D"noreferrer noreferrer" target=3D"=
_blank">https://codeberg.org/annoyingusername/m4p/src/branch/main/tests/res=
ources/eval_bugs.txt</a><br>
&gt; <br>
&gt; Useful.=C2=A0 The &#39;1 || (0 / 0)&#39; bug was indeed fixed in 1.4.2=
0.=C2=A0 &#39;1 || 2<br>
&gt; &amp;=3D 3&#39; still complains about invalid operator, but that&#39;s=
 intentional:<br>
&gt; &amp;=3D is not a supported operator.=C2=A0 But the complaint about &#=
39;1 || (2 &amp;=3D<br>
&gt; 3)&#39;) complaining about a missing right parenthesis (instead of a b=
ad<br>
&gt; operator) is something I hadn&#39;t noticed before today, and somethin=
g still<br>
&gt; present in 1.4.20.=C2=A0 The parser is correctly detecting that the<br=
>
&gt; expression is not valid at the point it hits the invalid operator; but=
<br>
&gt; perhaps the error message could be improved (by stating that it<br>
&gt; expects &#39;)&#39; instead of &#39;&amp;=3D&#39;, for example).<br>
&gt; <br>
&gt; Writing a parser that handles only good expressions is easy.=C2=A0 Wri=
ting<br>
&gt; a parser that provides sane error messages with useful suggestions<br>
&gt; when the parse fails is hard.=C2=A0 Yours may be the first (indirect) =
bug<br>
&gt; report against 1.4.20!<br>
<br>
And here&#39;s my first proposed patch for your project:<br>
<br>
diff --git i/m4p/m4_eval.py w/m4p/m4_eval.py<br>
index 91f97e7..6f2e565 100644<br>
--- i/m4p/m4_eval.py<br>
+++ w/m4p/m4_eval.py<br>
@@ -168,7 +168,7 @@ calc_grammar =3D &quot;&quot;&quot;<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | &quot;++&quot; atom=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 -&gt; invalid_unary_operator<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | atom &quot;--&quot;=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 -&gt; invalid_unary_operator<br>
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 | atom &quot;++&quot;=C2=A0 =C2=A0 =C2=
=A0 =C2=A0 -&gt; invalid_unary_operator<br>
-=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| &quot;(&quot; equation &quot;)&quot;<b=
r>
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0| &quot;(&quot; assignment &quot;)&quot;=
<br>
<br>
=C2=A0 =C2=A0 =C2=A0%import common.WS_INLINE<br>
<br>
<br>
so that &#39;echo &quot;eval((1&amp;3))&quot; | m4p&#39; outputs 1 instead =
of an error.<br></blockquote></div></div><div dir=3D"auto"><br></div><div d=
ir=3D"auto">Very nice! Thank you.</div><div dir=3D"auto"><br></div><div dir=
=3D"auto">Regards,</div><div dir=3D"auto">Nikolaos Chatzikonstantinou</div>=
<div dir=3D"auto"><div class=3D"gmail_quote gmail_quote_container"><blockqu=
ote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1px #ccc s=
olid;padding-left:1ex">
</blockquote></div></div></div>

--00000000000063b4c60634f23263--