bison-patches
[Top][All Lists]
Advanced

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

[PATCH] Simplify the i18n of the error messages.


From: Akim Demaille
Subject: [PATCH] Simplify the i18n of the error messages.
Date: Thu, 11 Dec 2008 00:59:13 +0100

        * data/lalr1.cc: Comment changes.
        * data/yacc.c (yysyntax_error): Rewrite, using a switch as in
        lalr1.cc instead of building dynamically the format string.
---
 ChangeLog     |    7 +++++
 data/lalr1.cc |    3 +-
 data/yacc.c   |   76 +++++++++++++++++++++++++-------------------------------
 3 files changed, 43 insertions(+), 43 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 719437a..25814e4 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2008-12-11  Akim Demaille  <address@hidden>
+
+       Simplify the i18n of the error messages.
+       * data/lalr1.cc: Comment changes.
+       * data/yacc.c (yysyntax_error): Rewrite, using a switch as in
+       lalr1.cc instead of building dynamically the format string.
+
 2008-12-08  Akim Demaille  <address@hidden>
 
        Fix portability issue in the test suite.
diff --git a/data/lalr1.cc b/data/lalr1.cc
index f19df60..49f4bf7 100644
--- a/data/lalr1.cc
+++ b/data/lalr1.cc
@@ -1393,7 +1393,8 @@ b4_error_verbose_if([int yystate, int yytoken],
        int yychecklim = yylast_ - yyn + 1;
        int yyxend = yychecklim < yyntokens_ ? yychecklim : yyntokens_;
 
-        // Number of "expected" tokens.
+        // Number of reported tokens (one for the "unexpected", one per
+        // "expected").
        size_t yycount = 0;
         // Its maximum.
         enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
diff --git a/data/yacc.c b/data/yacc.c
index 8b7a060..da7c72c 100644
--- a/data/yacc.c
+++ b/data/yacc.c
@@ -843,28 +843,10 @@ yysyntax_error (char *yyresult, int yystate, int yychar)
       YYSIZE_T yysize1;
       int yysize_overflow = 0;
       enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
+      /* Internationalized format string. */
+      const char *yyformat = 0;
+      /* Arguments of yyformat. */
       char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
-      int yyx;
-
-# if 0
-      /* This is so xgettext sees the translatable formats that are
-        constructed on the fly.  */
-      YY_("syntax error, unexpected %s");
-      YY_("syntax error, unexpected %s, expecting %s");
-      YY_("syntax error, unexpected %s, expecting %s or %s");
-      YY_("syntax error, unexpected %s, expecting %s or %s or %s");
-      YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s");
-# endif
-      char *yyfmt;
-      char const *yyf;
-      static char const yyunexpected[] = "syntax error, unexpected %s";
-      static char const yyexpecting[] = ", expecting %s";
-      static char const yyor[] = " or %s";
-      char yyformat[sizeof yyunexpected
-                   + sizeof yyexpecting - 1
-                   + ((YYERROR_VERBOSE_ARGS_MAXIMUM - 2)
-                      * (sizeof yyor - 1))];
-      char const *yyprefix = yyexpecting;
 
       /* Start YYX at -YYN if negative to avoid negative indexes in
         YYCHECK.  */
@@ -873,10 +855,12 @@ yysyntax_error (char *yyresult, int yystate, int yychar)
       /* Stay within bounds of both yycheck and yytname.  */
       int yychecklim = YYLAST - yyn + 1;
       int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
-      int yycount = 1;
+      /* Number of reported tokens (one for the "unexpected", one per
+         "expected"). */
+      int yycount = 0;
+      int yyx;
 
-      yyarg[0] = yytname[yytype];
-      yyfmt = yystpcpy (yyformat, yyunexpected);
+      yyarg[yycount++] = yytname[yytype];
 
       for (yyx = yyxbegin; yyx < yyxend; ++yyx)
        if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR)
@@ -885,19 +869,29 @@ yysyntax_error (char *yyresult, int yystate, int yychar)
              {
                yycount = 1;
                yysize = yysize0;
-               yyformat[sizeof yyunexpected - 1] = '\0';
                break;
              }
            yyarg[yycount++] = yytname[yyx];
            yysize1 = yysize + yytnamerr (0, yytname[yyx]);
            yysize_overflow |= (yysize1 < yysize);
            yysize = yysize1;
-           yyfmt = yystpcpy (yyfmt, yyprefix);
-           yyprefix = yyor;
          }
 
-      yyf = YY_(yyformat);
-      yysize1 = yysize + yystrlen (yyf);
+        switch (yycount)
+        {
+#define YYCASE_(N, S)                           \
+          case N:                               \
+            yyformat = S;                       \
+          break
+          YYCASE_(1, YY_("syntax error, unexpected %s"));
+          YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
+          YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
+          YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or 
%s"));
+          YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or 
%s or %s"));
+#undef YYCASE_
+        }
+
+      yysize1 = yysize + yystrlen (yyformat);
       yysize_overflow |= (yysize1 < yysize);
       yysize = yysize1;
 
@@ -911,19 +905,17 @@ yysyntax_error (char *yyresult, int yystate, int yychar)
             produced a string with the wrong number of "%s"s.  */
          char *yyp = yyresult;
          int yyi = 0;
-         while ((*yyp = *yyf) != '\0')
-           {
-             if (*yyp == '%' && yyf[1] == 's' && yyi < yycount)
-               {
-                 yyp += yytnamerr (yyp, yyarg[yyi++]);
-                 yyf += 2;
-               }
-             else
-               {
-                 yyp++;
-                 yyf++;
-               }
-           }
+         while ((*yyp = *yyformat) != '\0')
+            if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
+              {
+                yyp += yytnamerr (yyp, yyarg[yyi++]);
+                yyformat += 2;
+              }
+            else
+              {
+                yyp++;
+                yyformat++;
+              }
        }
       return yysize;
     }
-- 
1.6.0.4.790.gaa14a





reply via email to

[Prev in Thread] Current Thread [Next in Thread]