From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 26301 invoked by alias); 29 Oct 2013 10:16:59 -0000 Mailing-List: contact zsh-workers-help@zsh.org; run by ezmlm Precedence: bulk X-No-Archive: yes List-Id: Zsh Workers List List-Post: List-Help: X-Seq: 31926 Received: (qmail 18270 invoked from network); 29 Oct 2013 10:16:44 -0000 X-Spam-Checker-Version: SpamAssassin 3.3.2 (2011-06-06) on f.primenet.com.au X-Spam-Level: X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_HI, SPF_HELO_PASS autolearn=ham version=3.3.2 X-AuditID: cbfec7f4-b7f0a6d000007b1b-be-526f8b08d59f Date: Tue, 29 Oct 2013 10:16:39 +0000 From: Peter Stephenson To: Zsh Hackers' List Subject: PATCH: math error messages Message-id: <20131029101639.0007bc11@pwslap01u.europe.root.pri> In-reply-to: <20131028175505.584149cc@pwslap01u.europe.root.pri> References: <877gcxe1to.fsf@ixian.com> <20131028175505.584149cc@pwslap01u.europe.root.pri> Organization: Samsung Cambridge Solution Centre X-Mailer: Claws Mail 3.7.9 (GTK+ 2.22.0; i386-redhat-linux-gnu) MIME-version: 1.0 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7bit X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFupiluLIzCtJLcpLzFFi42I5/e/4VV2O7vwgg91HRCwONj9kcmD0WHXw A1MAYxSXTUpqTmZZapG+XQJXxt5NT5gLvotXXF0n1cA4VaiLkZNDQsBEYkr/biYIW0ziwr31 bF2MXBxCAksZJdZ/28IE4Sxnkti1t5sZpIpFQFXi/fr/rCA2m4ChxNRNsxm7GDk4RAS0Jdo/ ioGYwgLKEteuCoBU8ArYS+x+Np0FxOYUcJB4cOEq2C4hgRiJ5R8fg8X5BfQlrv79BHWDvcTM K2cYIXoFJX5MvgdWwyygJbF5WxMrhC0vsXnNW+YJjAKzkJTNQlI2C0nZAkbmVYyiqaXJBcVJ 6bmGesWJucWleel6yfm5mxgh4fdlB+PiY1aHGAU4GJV4eC3Y8oOEWBPLiitzDzFKcDArifA2 NwKFeFMSK6tSi/Lji0pzUosPMTJxcALDcR7npafOZxfOZtqa7jyP3UXrQNOLrDe3TGYvnKta Xa+0NeL0L/07b6oi/im+lYlzN3238O1zjf1/dpzwPMDw3T+CZ7pU4Wueaap3p3P+dX887USh wlL+b9IneS76ft4zf7uO5oO4o9daYye+FOqsuirY9WbT4+LPnO+3i3hIVLkK+LjpNsadL1Bi Kc5INNRiLipOBACr65mDHQIAAA== On Mon, 28 Oct 2013 17:55:05 +0000 Peter Stephenson wrote: > > zsh: bad math expression: operand expected at `' > > The error you got comes from it being empty, so there's no > number, which is what it's complaining about, slightly cryptically. Is anyone particularly attached to the current error messages? I think the following are a bit clearer for the two specific cases, although I'm not sure about the word "string". I used it in order to keep the standard math error prefix and avoid repeating "expression". I could say "argument" but that means a lot of possible things, most commonly a command line argument which would be inaccurate. % print $foo[] zsh: bad math expression: empty string % print $foo[3+] zsh: bad math expression: operand expected at end of string I have not changed any behaviour, only messages. I've noted that while $(()) returns 0, $foo[$unset] is an error. I'm happy with the latter and I think the former was forced on us by standards. diff --git a/Src/math.c b/Src/math.c index eae283d..b21a3ad 100644 --- a/Src/math.c +++ b/Src/math.c @@ -1374,6 +1374,19 @@ mathevalarg(char *s, char **ss) mnumber x; int xmtok = mtok; + /* + * At this entry point we don't allow an empty expression, + * whereas we do with matheval(). I'm not sure if this + * difference is deliberate, but it does mean that e.g. + * $array[$ind] where ind hasn't been set produces an error, + * which is probably safe. + * + * To avoid a more opaque error further in, bail out here. + */ + if (!*s) { + zerr("bad math expression: empty string"); + return (zlong)0; + } x = mathevall(s, MPREC_ARG, ss); if (mtok == COMMA) (*ss)--; @@ -1401,6 +1414,7 @@ checkunary(int mtokc, char *mptr) } if (errmsg) { int len, over = 0; + char *errtype = errmsg == 2 ? "operator" : "operand"; while (inblank(*mptr)) mptr++; len = ztrlen(mptr); @@ -1408,9 +1422,12 @@ checkunary(int mtokc, char *mptr) len = 10; over = 1; } - zerr("bad math expression: %s expected at `%l%s'", - errmsg == 2 ? "operator" : "operand", - mptr, len, over ? "..." : ""); + if (!*mptr) + zerr("bad math expression: %s expected at end of string", + errtype); + else + zerr("bad math expression: %s expected at `%l%s'", + errtype, mptr, len, over ? "..." : ""); } unary = !(tp & OP_OPF); } diff --git a/Test/C01arith.ztst b/Test/C01arith.ztst index 71c8a19..c19135c 100644 --- a/Test/C01arith.ztst +++ b/Test/C01arith.ztst @@ -134,11 +134,11 @@ print $(( a = )) 1:empty assignment -?(eval):1: bad math expression: operand expected at `' +?(eval):1: bad math expression: operand expected at end of string print $(( 3, )) 1:empty right hand of comma -?(eval):1: bad math expression: operand expected at `' +?(eval):1: bad math expression: operand expected at end of string print $(( 3,,4 )) 1:empty middle of comma pws