From bf98d94f4f1cce09671160cce22774c09d089cd0 Mon Sep 17 00:00:00 2001 From: Akim Demaille Date: Sun, 10 May 2020 09:58:30 +0200 Subject: [PATCH] c++: provide yy::parser::symbol_type::name * data/skeletons/c++.m4 (yy::parser::basic_symbol::name): New. * data/skeletons/lalr1.cc (yy_print_): Use it. * doc/bison.texi: Document. * tests/c++.at: Check. --- NEWS | 4 ++++ data/skeletons/c++.m4 | 23 +++++++++++++++++++++++ data/skeletons/lalr1.cc | 5 ++--- doc/bison.texi | 8 ++++++++ tests/c++.at | 1 + 5 files changed, 38 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 509766f7..b229e169 100644 --- a/NEWS +++ b/NEWS @@ -8,6 +8,10 @@ GNU Bison NEWS In C++, yy::parser::symbol_name is now a public member, as was intended. +** New features + + In C++, yy::parser::symbol_type now has a public name() member function. + * Noteworthy changes in release 3.6 (2020-05-08) [stable] ** Backward incompatible changes diff --git a/data/skeletons/c++.m4 b/data/skeletons/c++.m4 index 7ce45078..b862bf3c 100644 --- a/data/skeletons/c++.m4 +++ b/data/skeletons/c++.m4 @@ -357,6 +357,29 @@ m4_define([b4_symbol_type_define], Base::clear (); } +]b4_parse_error_bmatch( +[custom\|detailed], +[[ /// The user-facing name of this symbol. + const char *name () const YY_NOEXCEPT + { + return ]b4_parser_class[::symbol_name (this->kind ()); + }]], +[simple], +[[#if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ + /// The user-facing name of this symbol. + const char *name () const YY_NOEXCEPT + { + return ]b4_parser_class[::symbol_name (this->kind ()); + } +#endif // #if ]b4_api_PREFIX[DEBUG || ]b4_token_table_flag[ +]], +[verbose], +[[ /// The user-facing name of this symbol. + std::string name () const YY_NOEXCEPT + { + return ]b4_parser_class[::symbol_name (this->kind ()); + }]])[ + /// Backward compatibility (Bison 3.6). symbol_kind_type type_get () const YY_NOEXCEPT; diff --git a/data/skeletons/lalr1.cc b/data/skeletons/lalr1.cc index 6fa5674e..2c4b17c0 100644 --- a/data/skeletons/lalr1.cc +++ b/data/skeletons/lalr1.cc @@ -256,8 +256,7 @@ m4_define([b4_shared_declarations], [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); -]])[ + static std::string symbol_name (symbol_kind_type yysymbol);]])[ ]b4_token_constructor_define[ ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[ @@ -822,7 +821,7 @@ m4_if(b4_prefix, [yy], [], { symbol_kind_type yykind = yysym.kind (); yyo << (yykind < YYNTOKENS ? "token" : "nterm") - << ' ' << symbol_name (yykind) << " ("]b4_locations_if([ + << ' ' << yysym.name () << " ("]b4_locations_if([ << yysym.location << ": "])[; ]b4_symbol_actions([printer])[ yyo << ')'; diff --git a/doc/bison.texi b/doc/bison.texi index 7e51633b..726cbf22 100644 --- a/doc/bison.texi +++ b/doc/bison.texi @@ -12154,6 +12154,8 @@ values, which is always less than @code{YYNTOKENS}. @deftypemethod {parser} {const char *} symbol_name (@code{symbol_kind_t} @var{symbol}) @code{const} The name of the symbol whose kind is @var{symbol}, possibly translated. + +Returns a @code{std::string} when @code{parse.error} is @code{verbose}. @end deftypemethod A custom syntax error function looks as follows. This implementation is @@ -12286,6 +12288,12 @@ applicable) location. The kind of this symbol. @end deftypemethod +@deftypemethod {symbol_type} {const char *} name () @code{const} +The name of the kind of this symbol. + +Returns a @code{std::string} when @code{parse.error} is @code{verbose}. +@end deftypemethod + @sp 1 For each token kind, Bison generates named constructors as follows. diff --git a/tests/c++.at b/tests/c++.at index d3f52513..5789c8b0 100644 --- a/tests/c++.at +++ b/tests/c++.at @@ -162,6 +162,7 @@ int main() parser::symbol_type s = parser::make_INT (12); assert_eq (s.kind (), parser::symbol_kind::S_INT); assert_eq (parser::symbol_name (s.kind ()), "\"int\""); + assert_eq (s.name (), "\"int\""); assert_eq (s.value.as (), 12); }