glr2.cc: make yyparse a member function

Amusingly enough, glr2.cc still had its core function, yyparse, being
a free function instead of a member function.

* data/skeletons/glr2.cc (yyparse): Remove this free function called
from yyparser::parse.  Inline its body into...
(yyparser::parse): this member function.
This requires moving a bit the yychar, etc. macros.
Access to token can be simplified (the
b4_namespace_ref::b4_parser_class prefix is no longer needed).
This commit is contained in:
Akim Demaille
2020-12-12 19:41:35 +01:00
parent 178a033e8b
commit 855d46678a

View File

@@ -2849,15 +2849,48 @@ yypreference (const semantic_option& y0, const semantic_option& y1)
} while (0) } while (0)
/*----------. /* DEBUGGING ONLY */
| yyparse. | #if ]b4_api_PREFIX[DEBUG
`----------*/ static void
yypstack (glr_stack* yystackp, size_t yyk)
{
yystackp->yypstack(create_state_set_index(static_cast<std::ptrdiff_t>(yyk)));
}
static void yypdumpstack (glr_stack* yystackp) {
yystackp->yypdumpstack();
}
#endif
]b4_namespace_open[
]dnl In this section, the parse params are the original parse_params.
m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
[ /// Build a parser object.
]b4_parser_class::b4_parser_class[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
:])[
#if ]b4_api_PREFIX[DEBUG
]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
#endif]b4_parse_param_cons[
{}
]b4_parser_class::~b4_parser_class[ ()
{}
]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW
{}
int int
yyparse (]b4_namespace_ref[::]b4_parser_class[& yyparser]b4_user_formals[) ]b4_parser_class[::operator() ()
{ {
return parse ();
}
int
]b4_parser_class[::parse ()
{
]b4_parser_class[ &yyparser = *this;
int yyresult; int yyresult;
glr_stack yystack(YYINITDEPTH, yyparser]b4_user_args[); glr_stack yystack(YYINITDEPTH, *this]b4_user_args[);
glr_stack* const yystackp = &yystack; glr_stack* const yystackp = &yystack;
size_t yyposn; size_t yyposn;
@@ -2914,7 +2947,7 @@ b4_dollar_popdef])[]dnl
if (yyisShiftAction (yyaction)) if (yyisShiftAction (yyaction))
{ {
YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc); YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[; yychar = token::]b4_symbol(empty, id)[;
yyposn += 1; yyposn += 1;
yystack.yyglrShift (create_state_set_index(0), yyaction, yyposn, yylval]b4_locations_if([, &yylloc])[); yystack.yyglrShift (create_state_set_index(0), yyaction, yyposn, yylval]b4_locations_if([, &yylloc])[);
if (0 < yystack.yyerrState) if (0 < yystack.yyerrState)
@@ -2925,7 +2958,7 @@ b4_dollar_popdef])[]dnl
yystack.yyerror_range[1].getState().yyloc = yylloc;]])[ yystack.yyerror_range[1].getState().yyloc = 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 (yychar != ]b4_namespace_ref::b4_parser_class::token::b4_symbol(error, id)[) if (yychar != token::]b4_symbol(error, id)[)
yystack.yyreportSyntaxError (); yystack.yyreportSyntaxError ();
goto yyuser_error; goto yyuser_error;
} }
@@ -2937,7 +2970,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, yychar != ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[); yystackp->yystateStack.yytops.setLookaheadNeeds(yys, yychar != token::]b4_symbol(empty, id)[);
/* yyprocessOneStack returns one of three things: /* yyprocessOneStack returns one of three things:
@@ -2979,7 +3012,7 @@ b4_dollar_popdef])[]dnl
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 (yychar); yysymbol_kind_t yytoken_to_shift = YYTRANSLATE (yychar);
yychar = ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[; 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)
{ {
@@ -3023,7 +3056,7 @@ b4_dollar_popdef])[]dnl
goto yyreturn; goto yyreturn;
yyexhaustedlab: yyexhaustedlab:
yyparser.error (]b4_locations_if([yylloc, ])[YY_("memory exhausted")); error (]b4_locations_if([yylloc, ])[YY_("memory exhausted"));
yyresult = 2; yyresult = 2;
goto yyreturn; goto yyreturn;
@@ -3031,65 +3064,10 @@ b4_dollar_popdef])[]dnl
return yyresult; return yyresult;
} }
/* DEBUGGING ONLY */
#if ]b4_api_PREFIX[DEBUG
static void
yypstack (glr_stack* yystackp, size_t yyk)
{
yystackp->yypstack(create_state_set_index(static_cast<std::ptrdiff_t>(yyk)));
}
static void yypdumpstack (glr_stack* yystackp) {
yystackp->yypdumpstack();
}
#endif
#undef yylval #undef yylval
#undef yychar #undef yychar
#undef yynerrs]b4_locations_if([ #undef yynerrs]b4_locations_if([
#undef yylloc]) #undef yylloc])[
m4_if(b4_prefix, [yy], [],
[[/* Substitute the variable and function names. */
#define yyparse ]b4_prefix[parse
#define yylex ]b4_prefix[lex
#define yyerror ]b4_prefix[error
#define yylval ]b4_prefix[lval
#define yychar ]b4_prefix[char
#define yydebug ]b4_prefix[debug
#define yynerrs ]b4_prefix[nerrs]b4_locations_if([[
#define yylloc ]b4_prefix[lloc]])])[
]b4_namespace_open[
]dnl In this section, the parse params are the original parse_params.
m4_pushdef([b4_parse_param], m4_defn([b4_parse_param_orig]))dnl
[ /// Build a parser object.
]b4_parser_class::b4_parser_class[ (]b4_parse_param_decl[)]m4_ifset([b4_parse_param], [
:])[
#if ]b4_api_PREFIX[DEBUG
]m4_ifset([b4_parse_param], [ ], [ :])[yycdebug_ (&std::cerr)]m4_ifset([b4_parse_param], [,])[
#endif]b4_parse_param_cons[
{}
]b4_parser_class::~b4_parser_class[ ()
{}
]b4_parser_class[::syntax_error::~syntax_error () YY_NOEXCEPT YY_NOTHROW
{}
int
]b4_parser_class[::operator() ()
{
return parse ();
}
int
]b4_parser_class[::parse ()
{
return ::yyparse (*this]b4_user_args[);
}
]b4_parse_error_bmatch([custom\|detailed], ]b4_parse_error_bmatch([custom\|detailed],
[[ const char * [[ const char *