bison-patches
[Top][All Lists]
Advanced

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

[PATCH 08/16] c++: always define symbol_name


From: Akim Demaille
Subject: [PATCH 08/16] c++: always define symbol_name
Date: Sun, 26 Apr 2020 16:40:43 +0200

* data/skeletons/lalr1.cc (symbol_name): Always define it, even when
it's actually yytname which is used.
---
 data/skeletons/lalr1.cc | 84 ++++++++++++++++++++++++++---------------
 doc/bison.texi          |  4 +-
 2 files changed, 56 insertions(+), 32 deletions(-)

diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc
index a44090af..a7c5d684 100644
--- a/data/skeletons/lalr1.cc
+++ b/data/skeletons/lalr1.cc
@@ -305,27 +305,36 @@ m4_define([b4_shared_declarations],
     /// In theory \a t should be a token_kind_type, but character literals
     /// are valid, yet not members of the token_type enum.
     static symbol_kind_type yytranslate_ (int t);
-]b4_parse_error_bmatch([custom\|detailed], [[
-   /// The user-facing name of the symbol whose (internal) number is
-   /// YYSYMBOL.  No bounds checking.
-   static const char *symbol_name (symbol_kind_type yysymbol);
-]])[
 
-    // Tables.
-]b4_parser_tables_declare[
-]b4_parse_error_case([verbose], [[
-    /// Convert the symbol name \a n to a form suitable for a diagnostic.
-    static std::string yytnamerr_ (const char *n);
+]b4_parse_error_bmatch(
+[custom\|detailed],
+[[    /// The user-facing name of the symbol whose (internal) number is
+    /// YYSYMBOL.  No bounds checking.
+    static const char *symbol_name (symbol_kind_type yysymbol);]],
+[simple],
+[[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
+    /// The user-facing name of the symbol whose (internal) number is
+    /// YYSYMBOL.  No bounds checking.
+    static const char *symbol_name (symbol_kind_type yysymbol);
 
     /// For a symbol, its name in clear.
     static const char* const yytname_[];
+#endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
 ]],
-[b4_token_table_if([], [[
-#if ]b4_api_PREFIX[DEBUG
+[verbose],
+[[    /// The user-facing name of the symbol whose (internal) number is
+    /// YYSYMBOL.  No bounds checking.
+    static std::string symbol_name (symbol_kind_type yysymbol);
+
+    /// Convert the symbol name \a n to a form suitable for a diagnostic.
+    static std::string yytnamerr_ (const char *yystr);
+
     /// For a symbol, its name in clear.
     static const char* const yytname_[];
-#endif
-]])])[
+]])[
+
+    // Tables.
+]b4_parser_tables_declare[
 
 #if ]b4_api_PREFIX[DEBUG
 ]b4_integral_parser_table_declare([rline], [b4_rline],
@@ -587,16 +596,15 @@ m4_if(b4_prefix, [yy], [],
 #define YYERROR         goto yyerrorlab
 #define YYRECOVERING()  (!!yyerrstatus_)
 
-]b4_namespace_open[]b4_parse_error_bmatch([custom\|detailed], [[
-  /* The user-facing name of the symbol whose (internal) number is
-     YYSYMBOL.  No bounds checking. */
-  const char *
+]b4_namespace_open[
+]b4_parse_error_bmatch([custom\|detailed],
+[[  const char *
   ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
   {
     static const char *const yy_sname[] =
     {
     ]b4_symbol_names[
-    };]m4_ifdef([b4_translatable], [[
+    };]b4_has_translations_if([[
     /* YYTRANSLATABLE[SYMBOL-NUM] -- Whether YY_SNAME[SYMBOL-NUM] is
        internationalizable.  */
     static ]b4_int_type_for([b4_translatable])[ yytranslatable[] =
@@ -608,10 +616,18 @@ m4_if(b4_prefix, [yy], [],
             : yy_sname[yysymbol]);]], [[
     return yy_sname[yysymbol];]])[
   }
-]])
-b4_parse_error_case([verbose], [[
-
-  /* Return YYSTR after stripping away unnecessary quotes and
+]],
+[simple],
+[[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
+  const char *
+  ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
+  {
+    return yytname_[yysymbol];
+  }
+#endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[
+]],
+[verbose],
+[[  /* 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).
@@ -650,6 +666,12 @@ b4_parse_error_case([verbose], [[
 
     return yystr;
   }
+
+  std::string
+  ]b4_parser_class[::symbol_name (symbol_kind_type yysymbol)
+  {
+    return yytnamerr_ (yytname_[yysymbol]);
+  }
 ]])[
 
   /// Build a parser object.
@@ -786,7 +808,7 @@ b4_parse_error_case([verbose], [[
       std::abort ();
 #endif
     yyo << (yykind < YYNTOKENS ? "token" : "nterm")
-        << ' ' << yytname_[yykind] << " ("]b4_locations_if([
+        << ' ' << symbol_name (yykind) << " ("]b4_locations_if([
         << yysym.location << ": "])[;
     ]b4_symbol_actions([printer])[
     yyo << ')';
@@ -1298,7 +1320,7 @@ b4_dollar_popdef])[]dnl
     yylac_stack_.clear ();
     // Reduce until we encounter a shift and thereby accept the token.
 #if ]b4_api_PREFIX[DEBUG
-    YYCDEBUG << "LAC: checking lookahead " << yytname_[yytoken] << ':';
+    YYCDEBUG << "LAC: checking lookahead " << symbol_name (yytoken) << ':';
 #endif
     std::ptrdiff_t lac_top = 0;
     while (true)
@@ -1398,7 +1420,7 @@ b4_dollar_popdef])[]dnl
       {
 #if ]b4_api_PREFIX[DEBUG
         YYCDEBUG << "LAC: initial context established for "
-                 << yytname_[yytoken] << '\n';
+                 << symbol_name (yytoken) << '\n';
 #endif
         yy_lac_established_ = true;
         return yy_lac_check_ (yytoken);
@@ -1507,9 +1529,7 @@ b4_dollar_popdef])[]dnl
     for (char const* yyp = yyformat; *yyp; ++yyp)
       if (yyp[0] == '%' && yyp[1] == 's' && yyi < yycount)
         {
-          yyres += ]b4_parse_error_case([verbose],
-                [[yytnamerr_ (yytname_[yyarg[yyi++]])]],
-                [[symbol_name (yyarg[yyi++])]])[;
+          yyres += symbol_name (yyarg[yyi++]);
           ++yyp;
         }
       else
@@ -1524,7 +1544,8 @@ b4_dollar_popdef])[]dnl
 
 ]b4_parser_tables_define[
 
-]b4_tname_if([], [[#if ]b4_api_PREFIX[DEBUG]])[
+]b4_parse_error_bmatch([simple\|verbose],
+[[#if ]b4_api_PREFIX[DEBUG]b4_tname_if([[ || 1]])[
   // YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
   // First, the terminals, then, starting at \a YYNTOKENS, nonterminals.
   const char*
@@ -1532,7 +1553,8 @@ b4_dollar_popdef])[]dnl
   {
   ]b4_tname[
   };
-]b4_tname_if([], [[#endif]])[
+#endif
+]])[
 
 #if ]b4_api_PREFIX[DEBUG][
 ]b4_integral_parser_table_define([rline], [b4_rline])[
diff --git a/doc/bison.texi b/doc/bison.texi
index 7fae3b36..84186b72 100644
--- a/doc/bison.texi
+++ b/doc/bison.texi
@@ -7510,8 +7510,10 @@ If @var{argv} is null, return the size needed to store 
all the possible
 values, which is always less than @code{YYNTOKENS}.
 @end deftypefun
 
-@deftypefun {const char *} yysymbol_name (@code{symbol_kind_t} @var{symbol})
+@deftypefun {@r{string type}} yysymbol_name (@code{symbol_kind_t} @var{symbol})
 The name of the symbol whose kind is @var{symbol}, possibly translated.
+Depending on the options, may return a @code{const char*} or a
+@code{std::string}.
 @end deftypefun
 
 A custom syntax error function looks as follows.  This implementation is
-- 
2.26.2




reply via email to

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