Subject: Re: bug#28461: erreurs

From: Assaf Gordon <[email-masked]>
Date: Fri, 15 Sep 2017 00:41:56 -0600

Hello,

On 2017-09-14 11:45 PM, Bernhard Voelker wrote:
>> On 2017-09-14 11:43 AM, Eric Mariaud wrote:
>>> eric@Eric-Xub:~/Bureau$ expr 2 * 3
>>> expr: erreur de syntaxe
> 
> Well, we could try to improve the error diagnostics.
> 
> WDYT?

Very good idea.

Perhaps something like the attached? (not fully tested...)

-assaf
>From 4bb5771832a397e2db3704feaf0ece52cdd65a55 Mon Sep 17 00:00:00 2001
From: Assaf Gordon <[email-masked]>
Date: Fri, 15 Sep 2017 00:36:13 -0600
Subject: [PATCH] expr: add detailed syntax error messages

Show offending parameter instead of a generic 'syntax error' message.
Suggested by Bernhard Voelker in https://bugs.gnu.org/28461#13 .

* src/expr.c (syntax_error): Remove.
(required_more_args): New function.
(eval7, main): Replace syntax_error call with detailed die message.
* tests/misc/expr.pl: Add tests for new messages.
---
 src/expr.c         | 36 +++++++++++++++++++++++-------------
 tests/misc/expr.pl | 17 ++++++++++++++++-
 2 files changed, 39 insertions(+), 14 deletions(-)

diff --git a/src/expr.c b/src/expr.c
index 194b54b..8d796ea 100644
--- a/src/expr.c
+++ b/src/expr.c
@@ -413,12 +413,6 @@ or 0, 2 if EXPRESSION is syntactically invalid, and 3 if an error occurred.\n\
   exit (status);
 }
 
-/* Report a syntax error and exit.  */
-static void
-syntax_error (void)
-{
-  die (EXPR_INVALID, 0, _("syntax error"));
-}
 
 #if ! HAVE_GMP
 /* Report an integer overflow for operation OP and exit.  */
@@ -465,7 +459,9 @@ main (int argc, char **argv)
 
   v = eval (true);
   if (!nomoreargs ())
-    syntax_error ();
+    die (EXPR_INVALID, 0, _("syntax error: unexpected parameter %s"),
+	 quotearg_n_style (0, locale_quoting_style, *args));
+
   printv (v);
 
   return null (v);
@@ -659,6 +655,18 @@ nomoreargs (void)
   return *args == 0;
 }
 
+/* Report missing operand.
+   There is an implicit assumption that there was a previous argument,
+   and (args-1) is valid. */
+static void
+require_more_args (void)
+{
+  if (nomoreargs ())
+    die (EXPR_INVALID, 0, _("syntax error: missing more parameters after %s"),
+	 quotearg_n_style (0, locale_quoting_style, *(args-1)));
+}
+
+
 #ifdef EVAL_TRACE
 /* Print evaluation trace and args remaining.  */
 
@@ -759,19 +767,22 @@ eval7 (bool evaluate)
 #ifdef EVAL_TRACE
   trace ("eval7");
 #endif
-  if (nomoreargs ())
-    syntax_error ();
+  require_more_args ();
 
   if (nextarg ("("))
     {
       v = eval (evaluate);
+      if (nomoreargs ())
+	die (EXPR_INVALID, 0, _("syntax error: expecting ')' after %s"),
+	     quotearg_n_style (0, locale_quoting_style, *(args-1)));
       if (!nextarg (")"))
-        syntax_error ();
+	die (EXPR_INVALID, 0, _("syntax error: expecting ')' instead of %s"),
+	     quotearg_n_style (0, locale_quoting_style, *args));
       return v;
     }
 
   if (nextarg (")"))
-    syntax_error ();
+    die (EXPR_INVALID, 0, _("syntax error: unexpected ')'"));
 
   return str_value (*args++);
 }
@@ -792,8 +803,7 @@ eval6 (bool evaluate)
 #endif
   if (nextarg ("+"))
     {
-      if (nomoreargs ())
-        syntax_error ();
+      require_more_args ();
       return str_value (*args++);
     }
   else if (nextarg ("length"))
diff --git a/tests/misc/expr.pl b/tests/misc/expr.pl
index c0d6f7c..1ebfe63 100755
--- a/tests/misc/expr.pl
+++ b/tests/misc/expr.pl
@@ -164,7 +164,7 @@ my @Tests =
      ['bre61', '"acd" : "a\\(b\\)?c\\1d"', {OUT => ''}, {EXIT => 1}],
      ['bre62', '-- "-5" : "-\\{0,1\\}[0-9]*\$"', {OUT => '2'}],
 
-     ['fail-b', '9 9', {ERR => "$prog: syntax error\n"},
+     ['fail-b', '9 9', {ERR => "$prog: syntax error: unexpected parameter '9'\n"},
       {EXIT => 2}],
      ['fail-c', {ERR => "$prog: missing operand\n"
                  . "Try '$prog --help' for more information.\n"},
@@ -176,6 +176,21 @@ my @Tests =
      ['bignum-sub2', "$big_sum - $big", {OUT => $big_p1}],
      ['bignum-mul', "$big_p1 '*' $big", {OUT => $big_prod}],
      ['bignum-div', "$big_prod / $big", {OUT => $big_p1}],
+
+
+     # Test syntax error messages
+     ['se1', "2 a", {EXIT=>2},
+      {ERR=>"$prog: syntax error: unexpected parameter 'a'\n"}],
+     ['se2', "2 '+'", {EXIT=>2},
+      {ERR=>"$prog: syntax error: missing more parameters after '+'\n"}],
+     ['se3', "2 :", {EXIT=>2},
+      {ERR=>"$prog: syntax error: missing more parameters after ':'\n"}],
+     ['se4', "length", {EXIT=>2},
+      {ERR=>"$prog: syntax error: missing more parameters after 'length'\n"}],
+     ['se5', "'(' 2 ", {EXIT=>2},
+      {ERR=>"$prog: syntax error: expecting ')' after '2'\n"}],
+     ['se6', "'(' 2 a", {EXIT=>2},
+      {ERR=>"$prog: syntax error: expecting ')' instead of 'a'\n"}],
     );
 
 # If using big numbers fails, remove all /^bignum-/ tests
-- 
2.7.4