bison-patches
[Top][All Lists]
Advanced

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

[PATCH 2/8] parsers: revamp the interface of yytnamerr


From: Akim Demaille
Subject: [PATCH 2/8] parsers: revamp the interface of yytnamerr
Date: Sat, 29 Dec 2018 17:30:21 +0100

Currently yytnamerr takes a string, and strips its quotes when
possible.  It is actually more convenient to take a token number and
to transform the token name.

* data/skeletons/glr.c, data/skeletons/lalr1.cc, data/skeletons/lalr1.d,
* data/skeletons/lalr1.java, data/skeletons/yacc.c
(yytnamerr): Take a token number instead of a string.
(yysyntax_error): Adjust: keep format arguments as a list of token
numbers, instead of a list of strings.
---
 data/skeletons/glr.c      | 21 +++++++++++----------
 data/skeletons/lalr1.cc   | 20 ++++++++------------
 data/skeletons/lalr1.d    |  9 +++++----
 data/skeletons/lalr1.java |  9 +++++----
 data/skeletons/yacc.c     | 17 +++++++++--------
 5 files changed, 38 insertions(+), 38 deletions(-)

diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c
index f7f3c73b..ef26c391 100644
--- a/data/skeletons/glr.c
+++ b/data/skeletons/glr.c
@@ -558,7 +558,7 @@ yystpcpy (char *yydest, const char *yysrc)
 # endif
 
 # ifndef yytnamerr
-/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
+/* Copy to YYRES the name of YYTOKEN after stripping away unnecessary
    quotes and backslashes, so that it's suitable for yyerror.  The
    heuristic is that double-quoting is unnecessary unless the string
    contains an apostrophe, a comma, or backslash (other than
@@ -566,8 +566,9 @@ yystpcpy (char *yydest, const char *yysrc)
    null, do not copy; instead, return the length of what the result
    would have been.  */
 static size_t
-yytnamerr (char *yyres, const char *yystr)
+yytnamerr (char *yyres, int yytoken)
 {
+  const char *yystr = yytname[yytoken];
   if (*yystr == '"')
     {
       size_t yyn = 0;
@@ -727,8 +728,8 @@ yytokenName (yySymbol yytoken)
 {
   if (yytoken == YYEMPTY)
     return "";
-
-  return yytname[yytoken];
+  else
+    return yytname[yytoken];
 }
 #endif
 
@@ -2001,15 +2002,15 @@ yyreportSyntaxError (yyGLRStack* 
yystackp]b4_user_formals[)
 #else
   {
   yySymbol yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
-  size_t yysize0 = yytnamerr (YY_NULLPTR, yytokenName (yytoken));
+  size_t yysize0 = yytnamerr (YY_NULLPTR, yytoken);
   size_t yysize = yysize0;
   yybool yysize_overflow = yyfalse;
   char* yymsg = YY_NULLPTR;
   enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
   /* Internationalized format string. */
   const char *yyformat = YY_NULLPTR;
-  /* Arguments of yyformat. */
-  char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
+  /* Arguments of yyformat as symbol numbers. */
+  int yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
   /* Number of reported tokens (one for the "unexpected", one per
      "expected").  */
   int yycount = 0;
@@ -2040,7 +2041,7 @@ yyreportSyntaxError (yyGLRStack* 
yystackp]b4_user_formals[)
   if (yytoken != YYEMPTY)
     {
       int yyn = yypact[yystackp->yytops.yystates[0]->yylrState];
-      yyarg[yycount++] = yytokenName (yytoken);
+      yyarg[yycount++] = yytoken;
       if (!yypact_value_is_default (yyn))
         {
           /* Start YYX at -YYN if negative to avoid negative indexes in
@@ -2061,9 +2062,9 @@ yyreportSyntaxError (yyGLRStack* 
yystackp]b4_user_formals[)
                     yysize = yysize0;
                     break;
                   }
-                yyarg[yycount++] = yytokenName (yyx);
+                yyarg[yycount++] = yyx;
                 {
-                  size_t yysz = yysize + yytnamerr (YY_NULLPTR, yytokenName 
(yyx));
+                  size_t yysz = yysize + yytnamerr (YY_NULLPTR, yyx);
                   if (yysz < yysize)
                     yysize_overflow = yytrue;
                   yysize = yysz;
diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc
index 2d82172e..5a6091b1 100644
--- a/data/skeletons/lalr1.cc
+++ b/data/skeletons/lalr1.cc
@@ -253,8 +253,8 @@ m4_define([b4_shared_declarations],
     // Tables.
 ]b4_parser_tables_declare[]b4_error_verbose_if([
 
-    /// Convert the symbol name \a n to a form suitable for a diagnostic.
-    static std::string yytnamerr_ (const char *n);])[
+    /// The symbol name of \a yytoken in a form suitable for a diagnostic.
+    static std::string yytnamerr_ (int yytoken);])[
 
 ]b4_token_table_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
     /// For a symbol, its name in clear.
@@ -509,14 +509,10 @@ m4_if(b4_prefix, [yy], [],
 
 ]b4_namespace_open[]b4_error_verbose_if([[
 
-  /* Return YYSTR after stripping away unnecessary quotes and
-     backslashes, so that it's suitable for yyerror.  The heuristic is
-     that double-quoting is unnecessary unless the string contains an
-     apostrophe, a comma, or backslash (other than backslash-backslash).
-     YYSTR is taken from yytname.  */
   std::string
-  ]b4_parser_class_name[::yytnamerr_ (const char *yystr)
+  ]b4_parser_class_name[::yytnamerr_ (int yytoken)
   {
+    const char *yystr = yytname_[yytoken];
     if (*yystr == '"')
       {
         std::string yyr;
@@ -1092,8 +1088,8 @@ b4_error_verbose_if([state_type yystate, const 
symbol_type& yyla],
     size_t yycount = 0;
     // Its maximum.
     enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
-    // Arguments of yyformat.
-    char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
+    // Arguments of yyformat as symbol numbers.
+    int yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
 
     /* There are many possibilities here to consider:
        - If this state is a consistent state with a default action, then
@@ -1123,7 +1119,7 @@ b4_error_verbose_if([state_type yystate, const 
symbol_type& yyla],
     if (!yyla.empty ())
       {
         int yytoken = yyla.type_get ();
-        yyarg[yycount++] = yytname_[yytoken];
+        yyarg[yycount++] = yytoken;
         int yyn = yypact_[yystate];
         if (!yy_pact_value_is_default_ (yyn))
           {
@@ -1144,7 +1140,7 @@ b4_error_verbose_if([state_type yystate, const 
symbol_type& yyla],
                       break;
                     }
                   else
-                    yyarg[yycount++] = yytname_[yyx];
+                    yyarg[yycount++] = yyx;
                 }
           }
       }
diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d
index c65370c6..6f8ef552 100644
--- a/data/skeletons/lalr1.d
+++ b/data/skeletons/lalr1.d
@@ -407,13 +407,14 @@ b4_lexer_if([[
     return YYNEWSTATE;
   }
 
-  /* Return YYSTR after stripping away unnecessary quotes and
+  /* The name of YYTOKEN after stripping away unnecessary quotes and
      backslashes, so that it's suitable for yyerror.  The heuristic is
      that double-quoting is unnecessary unless the string contains an
      apostrophe, a comma, or backslash (other than backslash-backslash).
      YYSTR is taken from yytname.  */
-  private final string yytnamerr_ (string yystr)
+  private final string yytnamerr_ (int yytoken)
   {
+    string yystr = yytname_[yytoken];
     if (yystr[0] == '"')
       {
         string yyr;
@@ -766,7 +767,7 @@ m4_popdef([b4_at_dollar])])dnl
         // FIXME: This method of building the message is not compatible
         // with internationalization.
         string res = "syntax error, unexpected ";
-        res ~= yytnamerr_ (yytname_[tok]);
+        res ~= yytnamerr_ (tok);
         int yyn = yypact_[yystate];
         if (!yy_pact_value_is_default_ (yyn))
         {
@@ -791,7 +792,7 @@ m4_popdef([b4_at_dollar])])dnl
                      && !yy_table_value_is_error_ (yytable_[x + yyn]))
                  {
                     res ~= count++ == 0 ? ", expecting " : " or ";
-                    res ~= yytnamerr_ (yytname_[x]);
+                    res ~= yytnamerr_ (x);
                  }
             }
         }
diff --git a/data/skeletons/lalr1.java b/data/skeletons/lalr1.java
index 5424c58f..a4e48c05 100644
--- a/data/skeletons/lalr1.java
+++ b/data/skeletons/lalr1.java
@@ -501,13 +501,14 @@ b4_define_state])[
   }
 
 ]b4_error_verbose_if([[
-  /* Return YYSTR after stripping away unnecessary quotes and
+  /* The name of YYTOKEN after stripping away unnecessary quotes and
      backslashes, so that it's suitable for yyerror.  The heuristic is
      that double-quoting is unnecessary unless the string contains an
      apostrophe, a comma, or backslash (other than backslash-backslash).
      YYSTR is taken from yytname.  */
-  private final String yytnamerr_ (String yystr)
+  private final String yytnamerr_ (int yytoken)
   {
+    String yystr = yytname_[yytoken];
     if (yystr.charAt (0) == '"')
       {
         StringBuffer yyr = new StringBuffer ();
@@ -946,7 +947,7 @@ b4_both_if([[
                with internationalization.  */
             StringBuffer res =
               new StringBuffer ("syntax error, unexpected ");
-            res.append (yytnamerr_ (yytname_[tok]));
+            res.append (yytnamerr_ (tok));
             int yyn = yypact_[yystate];
             if (!yy_pact_value_is_default_ (yyn))
               {
@@ -971,7 +972,7 @@ b4_both_if([[
                           && !yy_table_value_is_error_ (yytable_[x + yyn]))
                         {
                           res.append (count++ == 0 ? ", expecting " : " or ");
-                          res.append (yytnamerr_ (yytname_[x]));
+                          res.append (yytnamerr_ (x));
                         }
                   }
               }
diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c
index 3920e8d3..5ec843e1 100644
--- a/data/skeletons/yacc.c
+++ b/data/skeletons/yacc.c
@@ -1041,7 +1041,7 @@ yy_lac (yytype_int16 *yyesa, yytype_int16 **yyes,
 # endif
 
 # ifndef yytnamerr
-/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
+/* Copy to YYRES the name of YYTOKEN after stripping away unnecessary
    quotes and backslashes, so that it's suitable for yyerror.  The
    heuristic is that double-quoting is unnecessary unless the string
    contains an apostrophe, a comma, or backslash (other than
@@ -1049,8 +1049,9 @@ yy_lac (yytype_int16 *yyesa, yytype_int16 **yyes,
    null, do not copy; instead, return the length of what the result
    would have been.  */
 static YYSIZE_T
-yytnamerr (char *yyres, const char *yystr)
+yytnamerr (char *yyres, int yytoken)
 {
+  const char *yystr = yytname[yytoken];
   if (*yystr == '"')
     {
       YYSIZE_T yyn = 0;
@@ -1103,13 +1104,13 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
                 ]b4_lac_if([[yytype_int16 *yyesa, yytype_int16 **yyes,
                 YYSIZE_T *yyes_capacity, ]])[yytype_int16 *yyssp, int yytoken)
 {
-  YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
+  YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytoken);
   YYSIZE_T yysize = yysize0;
   enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
   /* Internationalized format string. */
   const char *yyformat = YY_NULLPTR;
-  /* Arguments of yyformat. */
-  char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
+  /* Arguments of yyformat as symbol numbers. */
+  int yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
   /* Number of reported tokens (one for the "unexpected", one per
      "expected"). */
   int yycount = 0;
@@ -1146,7 +1147,7 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
     {
       int yyn = yypact[*yyssp];]b4_lac_if([[
       YYDPRINTF ((stderr, "Constructing syntax error message\n"));]])[
-      yyarg[yycount++] = yytname[yytoken];
+      yyarg[yycount++] = yytoken;
       if (!yypact_value_is_default (yyn))
         {]b4_lac_if([], [[
           /* Start YYX at -YYN if negative to avoid negative indexes in
@@ -1180,9 +1181,9 @@ yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
                     yysize = yysize0;
                     break;
                   }
-                yyarg[yycount++] = yytname[yyx];
+                yyarg[yycount++] = yyx;
                 {
-                  YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, 
yytname[yyx]);
+                  YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yyx);
                   if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)
                     yysize = yysize1;
                   else
-- 
2.20.0




reply via email to

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