glr2.cc: use references to the stack rather than pointers

Now that the lookahead macros (that used yystackp) are out of the way,
there is no reason to continue using a pointer.

* data/skeletons/glr2.cc: Use yystack, a reference, rather that
yystackp, a pointer.
Fix tons of const-correctness issues.
This commit is contained in:
Akim Demaille
2020-12-27 08:50:10 +01:00
parent e9b7641cca
commit 70b3c8fb20

View File

@@ -37,7 +37,7 @@ m4_define([b4_parse_param_orig], m4_defn([b4_parse_param]))
# Hijack the initial action to initialize the locations. # Hijack the initial action to initialize the locations.
]b4_bison_locations_if([m4_define([b4_initial_action], ]b4_bison_locations_if([m4_define([b4_initial_action],
[yystackp->yylloc.initialize ();]m4_ifdef([b4_initial_action], [ [yystack.yylloc.initialize ();]m4_ifdef([b4_initial_action], [
m4_defn([b4_initial_action])]))])[ m4_defn([b4_initial_action])]))])[
]m4_define([b4_define_symbol_kind], ]m4_define([b4_define_symbol_kind],
@@ -130,8 +130,8 @@ m4_define([b4_lex],
[b4_function_call([yylex], [b4_function_call([yylex],
[symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))], [symbol_type], m4_ifdef([b4_lex_param], b4_lex_param))],
[b4_function_call([yylex], [int], [b4_function_call([yylex], [int],
[b4_api_PREFIX[STYPE*], [&yystackp->yylval]][]dnl [b4_api_PREFIX[STYPE*], [&yystack.yylval]][]dnl
b4_locations_if([, [[location*], [&yystackp->yylloc]]])dnl b4_locations_if([, [[location*], [&yystack.yylloc]]])dnl
m4_ifdef([b4_lex_param], [, ]b4_lex_param))])]) m4_ifdef([b4_lex_param], [, ]b4_lex_param))])])
@@ -605,9 +605,9 @@ enum YYRESULTTAG { yyok, yyaccept, yyabort, yyerr };
int yydebug; int yydebug;
class glr_stack; class glr_stack;
static void yypstack (glr_stack* yystackp, size_t yyk) static void yypstack (const glr_stack& yystack, size_t yyk)
YY_ATTRIBUTE_UNUSED; YY_ATTRIBUTE_UNUSED;
static void yypdumpstack (glr_stack* yystackp) static void yypdumpstack (const glr_stack& yystack)
YY_ATTRIBUTE_UNUSED; YY_ATTRIBUTE_UNUSED;
#else /* !]b4_api_PREFIX[DEBUG */ #else /* !]b4_api_PREFIX[DEBUG */
@@ -669,7 +669,7 @@ state_set_index create_state_set_index(std::ptrdiff_t value) {
]b4_table_value_equals([[table]], [[Yytable_value]], [b4_table_ninf], [YYTABLE_NINF])[ ]b4_table_value_equals([[table]], [[Yytable_value]], [b4_table_ninf], [YYTABLE_NINF])[
static inline yysymbol_kind_t static inline yysymbol_kind_t
yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_stack* yystackp]b4_user_formals[); yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_stack& yystack]b4_user_formals[);
static inline bool static inline bool
yyisShiftAction (int yyaction) yyisShiftAction (int yyaction)
@@ -846,7 +846,7 @@ public:
} }
#endif #endif
std::ptrdiff_t indexIn (glr_stack_item* array); std::ptrdiff_t indexIn (const glr_stack_item* array) const;
glr_stack_item* asItem () glr_stack_item* asItem ()
{]b4_parse_assert_if([[ {]b4_parse_assert_if([[
@@ -1071,10 +1071,11 @@ public:
const glr_state* state() const; const glr_state* state() const;
void setState(const glr_state* s); void setState(const glr_state* s);
semantic_option* next(); const semantic_option* next () const;
void setNext(const semantic_option* s); semantic_option* next ();
void setNext (const semantic_option* s);
std::ptrdiff_t indexIn(glr_stack_item* array); std::ptrdiff_t indexIn (const glr_stack_item* array) const;
/** True iff YYY0 and YYY1 represent identical options at the top level. /** True iff YYY0 and YYY1 represent identical options at the top level.
* That is, they represent the same rule applied to RHS symbols * That is, they represent the same rule applied to RHS symbols
@@ -1408,13 +1409,13 @@ void glr_state::setFirstVal (const semantic_option* option)
yyfirstVal = option ? asItem(this) - asItem(option) : 0; yyfirstVal = option ? asItem(this) - asItem(option) : 0;
} }
std::ptrdiff_t glr_state::indexIn (glr_stack_item* array) std::ptrdiff_t glr_state::indexIn (const glr_stack_item* array) const
{]b4_parse_assert_if([[ {]b4_parse_assert_if([[
check_ ();]])[ check_ ();]])[
return asItem(this) - array; return asItem(this) - array;
} }
std::ptrdiff_t semantic_option::indexIn (glr_stack_item* array) std::ptrdiff_t semantic_option::indexIn (const glr_stack_item* array) const
{ {
return asItem(this) - array; return asItem(this) - array;
} }
@@ -1436,6 +1437,11 @@ void semantic_option::setState (const glr_state* s)
yystate = s ? asItem(this) - asItem(s) : 0; yystate = s ? asItem(this) - asItem(s) : 0;
} }
const semantic_option* semantic_option::next () const
{
return yynext ? &(asItem(this) - yynext)->getOption() : YY_NULLPTR;
}
semantic_option* semantic_option::next () semantic_option* semantic_option::next ()
{ {
return yynext ? &(asItem(this) - yynext)->getOption() : YY_NULLPTR; return yynext ? &(asItem(this) - yynext)->getOption() : YY_NULLPTR;
@@ -1787,14 +1793,14 @@ public:
#define YYINDEX(YYX) \ #define YYINDEX(YYX) \
((YYX) == YY_NULLPTR ? -1 : (YYX)->indexIn(yyitems.data())) ((YYX) == YY_NULLPTR ? -1 : (YYX)->indexIn (yyitems.data ()))
void void
dumpStack () dumpStack () const
{ {
for (size_t yyi = 0; yyi < size(); ++yyi) for (size_t yyi = 0; yyi < size(); ++yyi)
{ {
glr_stack_item& item = yyitems[yyi]; const glr_stack_item& item = yyitems[yyi];
std::cerr << std::setw(3) << yyi << ". "; std::cerr << std::setw(3) << yyi << ". ";
if (item.is_state()) if (item.is_state())
{ {
@@ -1848,7 +1854,7 @@ public:
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
/* Print YYS (possibly NULL) and its predecessors. */ /* Print YYS (possibly NULL) and its predecessors. */
void void
yypstates (const glr_state* yys) yypstates (const glr_state* yys) const
{ {
if (yys != YY_NULLPTR) if (yys != YY_NULLPTR)
yys->yy_yypstack(); yys->yy_yypstack();
@@ -1967,7 +1973,8 @@ public:
} }
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
void yypdumpstack () { void yypdumpstack () const
{
yystateStack.dumpStack(); yystateStack.dumpStack();
} }
#endif #endif
@@ -2104,7 +2111,7 @@ public:
yytoken, &yylval]b4_locations_if([, &yylloc])[); yytoken, &yylval]b4_locations_if([, &yylloc])[);
this->yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[; this->yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[;
} }
yysymbol_kind_t yytoken = yygetToken (this->yychar, yyparser, this]b4_user_args[); yysymbol_kind_t yytoken = yygetToken (this->yychar, yyparser, *this]b4_user_args[);
int yyj = yypact[firstTopState()->yylrState]; int yyj = yypact[firstTopState()->yylrState];
if (yypact_value_is_default (yyj)) if (yypact_value_is_default (yyj))
return; return;
@@ -2192,7 +2199,7 @@ public:
else else
{ {
yystateStack.yytops.setLookaheadNeeds(yyk, true); yystateStack.yytops.setLookaheadNeeds(yyk, true);
const yysymbol_kind_t yytoken = yygetToken (this->yychar, yyparser, this]b4_user_args[); const yysymbol_kind_t yytoken = yygetToken (this->yychar, yyparser, *this]b4_user_args[);
const short* yyconflicts; const short* yyconflicts;
const int yyaction = yygetLRActions (yystate, yytoken, yyconflicts); const int yyaction = yygetLRActions (yystate, yytoken, yyconflicts);
@@ -2484,7 +2491,7 @@ public:
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
void void
yypstack (state_set_index yyk) yypstack (state_set_index yyk) const
{ {
yystateStack.yypstates (yystateStack.topAt (yyk)); yystateStack.yypstates (yystateStack.topAt (yyk));
} }
@@ -2739,7 +2746,7 @@ private:
/** If yychar is empty, fetch the next token. */ /** If yychar is empty, fetch the next token. */
static inline yysymbol_kind_t static inline yysymbol_kind_t
yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_stack* yystackp]b4_user_formals[) yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_stack& yystack]b4_user_formals[)
{ {
yysymbol_kind_t yytoken; yysymbol_kind_t yytoken;
]b4_parse_param_use()dnl ]b4_parse_param_use()dnl
@@ -2756,8 +2763,8 @@ yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_s
catch (const ]b4_namespace_ref[::]b4_parser_class[::syntax_error& yyexc) catch (const ]b4_namespace_ref[::]b4_parser_class[::syntax_error& yyexc)
{ {
YYCDEBUG << "Caught exception: " << yyexc.what () << '\n';]b4_locations_if([ YYCDEBUG << "Caught exception: " << yyexc.what () << '\n';]b4_locations_if([
yystackp->yylloc = yyexc.location;])[ yystack.yylloc = yyexc.location;])[
yyparser.error (]b4_locations_if([yystackp->yylloc, ])[yyexc.what ()); yyparser.error (]b4_locations_if([yystack.yylloc, ])[yyexc.what ());
// Map errors caught in the scanner to the error token, so that error // Map errors caught in the scanner to the error token, so that error
// handling is started. // handling is started.
yycharp = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(error, id)[; yycharp = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(error, id)[;
@@ -2773,7 +2780,7 @@ yygetToken (int& yycharp, ]b4_namespace_ref[::]b4_parser_class[& yyparser, glr_s
else else
{ {
yytoken = YYTRANSLATE (yycharp); yytoken = YYTRANSLATE (yycharp);
YY_SYMBOL_PRINT ("Next token is", yytoken, &yystackp->yylval, &yystackp->yylloc); YY_SYMBOL_PRINT ("Next token is", yytoken, &yystack.yylval, &yystack.yylloc);
} }
return yytoken; return yytoken;
} }
@@ -2885,12 +2892,13 @@ yypreference (const semantic_option& y0, const semantic_option& y1)
/* DEBUGGING ONLY */ /* DEBUGGING ONLY */
#if ]b4_api_PREFIX[DEBUG #if ]b4_api_PREFIX[DEBUG
static void static void
yypstack (glr_stack* yystackp, size_t yyk) yypstack (const glr_stack& yystack, size_t yyk)
{ {
yystackp->yypstack(create_state_set_index(static_cast<std::ptrdiff_t>(yyk))); yystack.yypstack (create_state_set_index (static_cast<std::ptrdiff_t> (yyk)));
} }
static void yypdumpstack (glr_stack* yystackp) { static void yypdumpstack (const glr_stack& yystack)
yystackp->yypdumpstack(); {
yystack.yypdumpstack ();
} }
#endif #endif
@@ -2924,16 +2932,15 @@ m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
]b4_parser_class[ &yyparser = *this; ]b4_parser_class[ &yyparser = *this;
int yyresult; int yyresult;
glr_stack yystack(YYINITDEPTH, *this]b4_user_args[); glr_stack yystack(YYINITDEPTH, *this]b4_user_args[);
glr_stack* const yystackp = &yystack;
size_t yyposn; size_t yyposn;
YYCDEBUG << "Starting parse\n"; YYCDEBUG << "Starting parse\n";
yystackp->yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[; yystack.yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[;
yystackp->yylval = yyval_default;]b4_locations_if([ yystack.yylval = yyval_default;]b4_locations_if([
yystackp->yylloc = yyloc_default;])[ yystack.yylloc = yyloc_default;])[
]m4_ifdef([b4_initial_action], [ ]m4_ifdef([b4_initial_action], [
b4_dollar_pushdef([yystackp->yylval], [], [], [yystackp->yylloc])dnl b4_dollar_pushdef([yystack.yylval], [], [], [yystack.yylloc])dnl
b4_user_initial_action b4_user_initial_action
b4_dollar_popdef])[]dnl b4_dollar_popdef])[]dnl
[ [
@@ -2944,7 +2951,7 @@ b4_dollar_popdef])[]dnl
case 2: goto yyexhaustedlab; case 2: goto yyexhaustedlab;
default: goto yybuglab; default: goto yybuglab;
} }
yystack.yyglrShift (create_state_set_index(0), 0, 0, yystackp->yylval]b4_locations_if([, yystackp->yylloc])[); yystack.yyglrShift (create_state_set_index(0), 0, 0, yystack.yylval]b4_locations_if([, yystack.yylloc])[);
yyposn = 0; yyposn = 0;
while (true) while (true)
@@ -2964,7 +2971,7 @@ b4_dollar_popdef])[]dnl
const rule_num yyrule = yydefaultAction (yystate); const rule_num yyrule = yydefaultAction (yystate);
if (yyrule == 0) if (yyrule == 0)
{]b4_locations_if([[ {]b4_locations_if([[
yystack.yyerror_range[1].getState().yyloc = yystackp->yylloc;]])[ yystack.yyerror_range[1].getState().yyloc = yystack.yylloc;]])[
yystack.yyreportSyntaxError (); yystack.yyreportSyntaxError ();
goto yyuser_error; goto yyuser_error;
} }
@@ -2972,26 +2979,26 @@ b4_dollar_popdef])[]dnl
} }
else else
{ {
const yysymbol_kind_t yytoken = yygetToken (yystackp->yychar, yyparser, yystackp]b4_user_args[); const yysymbol_kind_t yytoken = yygetToken (yystack.yychar, yyparser, yystack]b4_user_args[);
const short* yyconflicts; const short* yyconflicts;
const int yyaction = yygetLRActions (yystate, yytoken, yyconflicts); const int yyaction = yygetLRActions (yystate, yytoken, yyconflicts);
if (*yyconflicts != 0) if (*yyconflicts != 0)
break; break;
if (yyisShiftAction (yyaction)) if (yyisShiftAction (yyaction))
{ {
YY_SYMBOL_PRINT ("Shifting", yytoken, &yystackp->yylval, &yystackp->yylloc); YY_SYMBOL_PRINT ("Shifting", yytoken, &yystack.yylval, &yystack.yylloc);
yystackp->yychar = token::]b4_symbol(empty, id)[; yystack.yychar = token::]b4_symbol(empty, id)[;
yyposn += 1; yyposn += 1;
yystack.yyglrShift (create_state_set_index(0), yyaction, yyposn, yystackp->yylval]b4_locations_if([, yystackp->yylloc])[); yystack.yyglrShift (create_state_set_index(0), yyaction, yyposn, yystack.yylval]b4_locations_if([, yystack.yylloc])[);
if (0 < yystack.yyerrState) if (0 < yystack.yyerrState)
yystack.yyerrState -= 1; yystack.yyerrState -= 1;
} }
else if (yyisErrorAction (yyaction)) else if (yyisErrorAction (yyaction))
{]b4_locations_if([[ {]b4_locations_if([[
yystack.yyerror_range[1].getState().yyloc = yystackp->yylloc;]])[ yystack.yyerror_range[1].getState().yyloc = yystack.yylloc;]])[
/* Don't issue an error message again for exceptions /* Don't issue an error message again for exceptions
thrown from the scanner. */ thrown from the scanner. */
if (yystackp->yychar != token::]b4_symbol(error, id)[) if (yystack.yychar != token::]b4_symbol(error, id)[)
yystack.yyreportSyntaxError (); yystack.yyreportSyntaxError ();
goto yyuser_error; goto yyuser_error;
} }
@@ -3003,7 +3010,7 @@ b4_dollar_popdef])[]dnl
while (true) while (true)
{ {
for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys) for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys)
yystackp->yystateStack.yytops.setLookaheadNeeds(yys, yystackp->yychar != token::]b4_symbol(empty, id)[); yystack.yystateStack.yytops.setLookaheadNeeds(yys, yystack.yychar != token::]b4_symbol(empty, id)[);
/* yyprocessOneStack returns one of three things: /* yyprocessOneStack returns one of three things:
@@ -3025,16 +3032,16 @@ b4_dollar_popdef])[]dnl
on yylval in the event of memory exhaustion. */ on yylval in the event of memory exhaustion. */
for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys) for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys)
YYCHK1 (yystack.yyprocessOneStack (yys, yyposn]b4_locations_if([, &yystackp->yylloc])[)); YYCHK1 (yystack.yyprocessOneStack (yys, yyposn]b4_locations_if([, &yystack.yylloc])[));
yystack.yystateStack.yytops.yyremoveDeletes (); yystack.yystateStack.yytops.yyremoveDeletes ();
if (yystack.yystateStack.yytops.size() == 0) if (yystack.yystateStack.yytops.size() == 0)
{ {
yystack.yystateStack.yytops.yyundeleteLastStack (); yystack.yystateStack.yytops.yyundeleteLastStack ();
if (yystack.yystateStack.yytops.size() == 0) if (yystack.yystateStack.yytops.size() == 0)
yystack.yyFail (]b4_locations_if([&yystackp->yylloc, ])[YY_("syntax error")); yystack.yyFail (]b4_locations_if([&yystack.yylloc, ])[YY_("syntax error"));
YYCHK1 (yystack.yyresolveStack ()); YYCHK1 (yystack.yyresolveStack ());
YYCDEBUG << "Returning to deterministic operation.\n";]b4_locations_if([[ YYCDEBUG << "Returning to deterministic operation.\n";]b4_locations_if([[
yystack.yyerror_range[1].getState().yyloc = yystackp->yylloc;]])[ yystack.yyerror_range[1].getState().yyloc = yystack.yylloc;]])[
yystack.yyreportSyntaxError (); yystack.yyreportSyntaxError ();
goto yyuser_error; goto yyuser_error;
} }
@@ -3044,8 +3051,8 @@ b4_dollar_popdef])[]dnl
failure in the following loop. Thus, yychar is set to ]b4_symbol(empty, id)[ failure in the following loop. Thus, yychar is set to ]b4_symbol(empty, id)[
before the loop to make sure the user destructor for yylval isn't before the loop to make sure the user destructor for yylval isn't
called twice. */ called twice. */
yysymbol_kind_t yytoken_to_shift = YYTRANSLATE (yystackp->yychar); yysymbol_kind_t yytoken_to_shift = YYTRANSLATE (yystack.yychar);
yystackp->yychar = token::]b4_symbol(empty, id)[; yystack.yychar = token::]b4_symbol(empty, id)[;
yyposn += 1; yyposn += 1;
for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys) for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys)
{ {
@@ -3055,8 +3062,8 @@ b4_dollar_popdef])[]dnl
yyconflicts); yyconflicts);
/* Note that yyconflicts were handled by yyprocessOneStack. */ /* Note that yyconflicts were handled by yyprocessOneStack. */
YYCDEBUG << "On stack " << yys.get() << ", "; YYCDEBUG << "On stack " << yys.get() << ", ";
YY_SYMBOL_PRINT ("shifting", yytoken_to_shift, &yystackp->yylval, &yystackp->yylloc); YY_SYMBOL_PRINT ("shifting", yytoken_to_shift, &yystack.yylval, &yystack.yylloc);
yystack.yyglrShift (yys, yyaction, yyposn, yystackp->yylval]b4_locations_if([, yystackp->yylloc])[); yystack.yyglrShift (yys, yyaction, yyposn, yystack.yylval]b4_locations_if([, yystack.yylloc])[);
YYCDEBUG << "Stack " << yys.get() << " now in state #" YYCDEBUG << "Stack " << yys.get() << " now in state #"
<< yystack.topState(yys)->yylrState << '\n'; << yystack.topState(yys)->yylrState << '\n';
} }
@@ -3071,7 +3078,7 @@ b4_dollar_popdef])[]dnl
} }
continue; continue;
yyuser_error: yyuser_error:
yystack.yyrecoverSyntaxError (]b4_locations_if([&yystackp->yylloc])[); yystack.yyrecoverSyntaxError (]b4_locations_if([&yystack.yylloc])[);
yyposn = yystack.firstTopState()->yyposn; yyposn = yystack.firstTopState()->yyposn;
} }
@@ -3088,7 +3095,7 @@ b4_dollar_popdef])[]dnl
goto yyreturn; goto yyreturn;
yyexhaustedlab: yyexhaustedlab:
error (]b4_locations_if([yystackp->yylloc, ])[YY_("memory exhausted")); error (]b4_locations_if([yystack.yylloc, ])[YY_("memory exhausted"));
yyresult = 2; yyresult = 2;
goto yyreturn; goto yyreturn;