glr2.cc: move glr_stack and glr_state into the parser class

In order to be able to link several glr2.cc parser together, we cannot
have glr_stack and glr_state be in no namespace.  Putting them in the
unnamed namespace is not doable, since we need to fwd declare them in
the parser.  Putting them in the specified namespace is not enough
either, since some users would like to be able to put several parsers
in the same name, only differing by the class name.

* data/skeletons/glr2.cc (glr_state, glr_stack): Move into yy::parser.
This commit is contained in:
Akim Demaille
2021-09-09 06:52:43 +02:00
parent 3e94b12ddd
commit 9babd05a90

View File

@@ -224,9 +224,6 @@ m4_define([b4_shared_declarations],
]b4_YYDEBUG_define[ ]b4_YYDEBUG_define[
class glr_stack;
class glr_state;
]b4_namespace_open[ ]b4_namespace_open[
]b4_bison_locations_if([m4_ifndef([b4_location_file], ]b4_bison_locations_if([m4_ifndef([b4_location_file],
@@ -239,6 +236,10 @@ class glr_state;
]b4_public_types_declare[ ]b4_public_types_declare[
]b4_symbol_type_define[ ]b4_symbol_type_define[
// FIXME: should be private eventually.
class glr_stack;
class glr_state;
/// Build a parser object. /// Build a parser object.
]b4_parser_class[ (]b4_parse_param_decl[); ]b4_parser_class[ (]b4_parse_param_decl[);
~]b4_parser_class[ (); ~]b4_parser_class[ ();
@@ -373,10 +374,8 @@ class glr_state;
location_type& yyloc]])[); location_type& yyloc]])[);
]b4_parse_param_vars[ ]b4_parse_param_vars[
// Needs access to report_syntax_error, etc. // Needs access to yy_destroy_, report_syntax_error, etc.
friend glr_stack; friend glr_stack;
// Needs access to yy_destroy_.
friend glr_state;
}; };
]b4_token_ctor_if([b4_yytranslate_define([$1])[ ]b4_token_ctor_if([b4_yytranslate_define([$1])[
@@ -635,6 +634,9 @@ int yydebug;
namespace namespace
{ {
using glr_stack = ]b4_namespace_ref[::]b4_parser_class[::glr_stack;
using glr_state = ]b4_namespace_ref[::]b4_parser_class[::glr_state;
void yypstack (const glr_stack& yystack, size_t yyk) void yypstack (const glr_stack& yystack, size_t yyk)
YY_ATTRIBUTE_UNUSED; YY_ATTRIBUTE_UNUSED;
void yypdumpstack (const glr_stack& yystack) void yypdumpstack (const glr_stack& yystack)
@@ -775,13 +777,11 @@ typedef int state_num;
typedef int rule_num; typedef int rule_num;
// Forward declarations. // Forward declarations.
class glr_state;
namespace namespace
{ {
class semantic_option; class semantic_option;
} }
class glr_stack_item; class glr_stack_item;
class glr_stack;
/** Accessing symbol of state YYSTATE. */ /** Accessing symbol of state YYSTATE. */
static inline yysymbol_kind_t static inline yysymbol_kind_t
@@ -804,9 +804,11 @@ yyrhsLength (rule_num yyrule)
return yyr2[yyrule]; return yyr2[yyrule];
} }
class glr_state namespace ]b4_namespace_ref[
{ {
public: class ]b4_parser_class[::glr_state
{
public:
typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind symbol_kind; typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind symbol_kind;
typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind_type symbol_kind_type; typedef ]b4_namespace_ref[::]b4_parser_class[::symbol_kind_type symbol_kind_type;
typedef ]b4_namespace_ref[::]b4_parser_class[::value_type value_type;]b4_locations_if([[ typedef ]b4_namespace_ref[::]b4_parser_class[::value_type value_type;]b4_locations_if([[
@@ -939,7 +941,7 @@ public:
destroy (char const *yymsg, ]b4_namespace_ref[::]b4_parser_class[& yyparser]b4_user_formals[); destroy (char const *yymsg, ]b4_namespace_ref[::]b4_parser_class[& yyparser]b4_user_formals[);
/* DEBUGGING ONLY */ /* DEBUGGING ONLY */
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
void yy_yypstack () const void yy_yypstack () const
{]b4_parse_assert_if([[ {]b4_parse_assert_if([[
check_ ();]])[ check_ ();]])[
@@ -950,7 +952,7 @@ public:
} }
std::cerr << yylrState << "@@" << yyposn; std::cerr << yylrState << "@@" << yyposn;
} }
#endif #endif
std::ptrdiff_t indexIn (const glr_stack_item* array) const; std::ptrdiff_t indexIn (const glr_stack_item* array) const;
@@ -966,7 +968,7 @@ public:
return asItem (this); return asItem (this);
} }
private: private:
template <typename T> template <typename T>
static const glr_stack_item* asItem (const T* state) static const glr_stack_item* asItem (const T* state)
{ {
@@ -1000,8 +1002,8 @@ private:
/** Source location for this state. */ /** Source location for this state. */
location_type yyloc;]])[ location_type yyloc;]])[
]b4_parse_assert_if([[ ]b4_parse_assert_if([[
public: public:
// Check invariants. // Check invariants.
void check_ () const void check_ () const
{ {
@@ -1013,7 +1015,9 @@ public:
// A magic number to check our pointer arithmetic is sane. // A magic number to check our pointer arithmetic is sane.
enum { MAGIC = 713705 }; enum { MAGIC = 713705 };
unsigned int magic_;]])[ unsigned int magic_;]])[
}; };
} // namespace ]b4_namespace_ref[
/** A stack of GLRState representing the different heads during /** A stack of GLRState representing the different heads during
* nondeterministic evaluation. */ * nondeterministic evaluation. */
@@ -2073,9 +2077,11 @@ public:
#undef YYFILL #undef YYFILL
#define YYFILL(N) yystateStack.yyfill (yyvsp, yylow, (N), yynormal) #define YYFILL(N) yystateStack.yyfill (yyvsp, yylow, (N), yynormal)
class glr_stack namespace ]b4_namespace_ref[
{ {
public: class ]b4_parser_class[::glr_stack
{
public:
typedef ]b4_namespace_ref[::]b4_parser_class[ parser_type; typedef ]b4_namespace_ref[::]b4_parser_class[ parser_type;
typedef parser_type::symbol_kind symbol_kind; typedef parser_type::symbol_kind symbol_kind;
typedef parser_type::symbol_kind_type symbol_kind_type; typedef parser_type::symbol_kind_type symbol_kind_type;
@@ -2083,11 +2089,11 @@ public:
typedef parser_type::value_type value_type;]b4_locations_if([[ typedef parser_type::value_type value_type;]b4_locations_if([[
typedef parser_type::location_type location_type;]])[ typedef parser_type::location_type location_type;]])[
]b4_parse_error_bmatch([custom\|detailed\|verbose], [[ ]b4_parse_error_bmatch([custom\|detailed\|verbose], [[
typedef parser_type::context context; typedef parser_type::context context;
// Needs access to yypact_value_is_default, etc. // Needs access to yypact_value_is_default, etc.
friend context; friend context;
]])[ ]])[
glr_stack (size_t yysize, ]b4_namespace_ref[::]b4_parser_class[& yyparser_yyarg]m4_ifset([b4_parse_param], [, b4_parse_param_decl])[) glr_stack (size_t yysize, ]b4_namespace_ref[::]b4_parser_class[& yyparser_yyarg]m4_ifset([b4_parse_param], [, b4_parse_param_decl])[)
: yyerrState (0) : yyerrState (0)
@@ -2106,7 +2112,7 @@ public:
} }
int yyerrState; int yyerrState;
]b4_locations_if([[ /* To compute the location of the error token. */ ]b4_locations_if([[ /* To compute the location of the error token. */
glr_stack_item yyerror_range[3];]])[ glr_stack_item yyerror_range[3];]])[
state_stack yystateStack; state_stack yystateStack;
int yyerrcnt; int yyerrcnt;
@@ -2114,7 +2120,7 @@ public:
YYJMP_BUF yyexception_buffer; YYJMP_BUF yyexception_buffer;
]b4_namespace_ref[::]b4_parser_class[& yyparser; ]b4_namespace_ref[::]b4_parser_class[& yyparser;
#define YYCHK1(YYE) \ #define YYCHK1(YYE) \
do { \ do { \
switch (YYE) { \ switch (YYE) { \
case yyok: \ case yyok: \
@@ -2139,11 +2145,11 @@ public:
YYCDEBUG << "Starting parse\n"; YYCDEBUG << "Starting parse\n";
this->yyla.clear (); this->yyla.clear ();
]m4_ifdef([b4_initial_action], [ ]m4_ifdef([b4_initial_action], [
b4_dollar_pushdef([yyla.value], [], [], [yyla.location])dnl b4_dollar_pushdef([yyla.value], [], [], [yyla.location])dnl
b4_user_initial_action b4_user_initial_action
b4_dollar_popdef])[]dnl b4_dollar_popdef])[]dnl
[ [
switch (YYSETJMP (this->yyexception_buffer)) switch (YYSETJMP (this->yyexception_buffer))
{ {
case 0: break; case 0: break;
@@ -2267,7 +2273,7 @@ b4_dollar_popdef])[]dnl
YYCDEBUG << "Stack " << yys.get() << " now in state " YYCDEBUG << "Stack " << yys.get() << " now in state "
<< this->topState(yys)->yylrState << '\n'; << this->topState(yys)->yylrState << '\n';
} }
]b4_variant_if([[ ]b4_variant_if([[
// FIXME: User destructors. // FIXME: User destructors.
// Value type destructor. // Value type destructor.
]b4_symbol_variant([[yytoken_to_shift]], [[this->yyla.value]], [[template destroy]])])[ ]b4_symbol_variant([[yytoken_to_shift]], [[this->yyla.value]], [[template destroy]])])[
@@ -2306,7 +2312,7 @@ b4_dollar_popdef])[]dnl
yyreturn: yyreturn:
return yyresult; return yyresult;
} }
#undef YYCHK1 #undef YYCHK1
void yyreserveGlrStack () void yyreserveGlrStack ()
{ {
@@ -2356,26 +2362,26 @@ b4_dollar_popdef])[]dnl
yyreserveGlrStack (); yyreserveGlrStack ();
} }
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
void yypdumpstack () const void yypdumpstack () const
{ {
yystateStack.dumpStack(); yystateStack.dumpStack();
} }
#endif #endif
void void
yyreportSyntaxError () yyreportSyntaxError ()
{ {
if (yyerrState != 0) if (yyerrState != 0)
return; return;
]b4_parse_error_case( ]b4_parse_error_case(
[simple], [[ [simple], [[
std::string msg = YY_("syntax error"); std::string msg = YY_("syntax error");
yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]], yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]],
[custom], [[ [custom], [[
context yyctx (*this, yyla); context yyctx (*this, yyla);
yyparser.report_syntax_error (yyctx);]], yyparser.report_syntax_error (yyctx);]],
[[ [[
context yyctx (*this, yyla); context yyctx (*this, yyla);
std::string msg = yyparser.yysyntax_error_ (yyctx); std::string msg = yyparser.yysyntax_error_ (yyctx);
yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]])[ yyparser.error (]b4_join(b4_locations_if([yyla.location]), [[YY_MOVE (msg)]])[);]])[
@@ -2580,7 +2586,7 @@ b4_dollar_popdef])[]dnl
return yyparser.error (]b4_locations_if([*yylocp, ])[YY_("syntax error: cannot back up")), \ return yyparser.error (]b4_locations_if([*yylocp, ])[YY_("syntax error: cannot back up")), \
yyerrok, yyerr yyerrok, yyerr
]b4_variant_if([[ ]b4_variant_if([[
/* Variants are always initialized to an empty instance of the /* Variants are always initialized to an empty instance of the
correct type. The default '$$ = $1' action is NOT applied correct type. The default '$$ = $1' action is NOT applied
when using variants. */ when using variants. */
@@ -2595,7 +2601,7 @@ b4_dollar_popdef])[]dnl
/* Default location. */ /* Default location. */
YYLLOC_DEFAULT ((*yylocp), (yyvsp - yyrhslen), yyrhslen); YYLLOC_DEFAULT ((*yylocp), (yyvsp - yyrhslen), yyrhslen);
yyerror_range[1].getState().yyloc = *yylocp; yyerror_range[1].getState().yyloc = *yylocp;
]])[ ]])[
/* If yyk == -1, we are running a deferred action on a temporary /* If yyk == -1, we are running a deferred action on a temporary
stack. In that case, YY_REDUCE_PRINT must not play with YYFILL, stack. In that case, YY_REDUCE_PRINT must not play with YYFILL,
so pretend the stack is "normal". */ so pretend the stack is "normal". */
@@ -2818,7 +2824,7 @@ b4_dollar_popdef])[]dnl
return yystateStack.firstTop(); return yystateStack.firstTop();
} }
private: private:
void popall_ () void popall_ ()
{ {
@@ -3215,9 +3221,9 @@ private:
return 0; return 0;
} }
]b4_parse_param_vars[ ]b4_parse_param_vars[
}; // class ]b4_parser_class[::glr_stack
}; } // namespace ]b4_namespace_ref[
/* DEBUGGING ONLY */ /* DEBUGGING ONLY */