glr2.cc: misc cleanups

* data/skeletons/glr2.cc: Use 'const' on variables and applicable
member functions.
Improve comments.
Use references where applicable.
Enforce names_like_this, notLikeThis.
Reduce scopes.
This commit is contained in:
Valentin Tolmer
2020-09-30 17:43:29 +02:00
committed by Akim Demaille
parent 2ec6df3b07
commit 1b85ac4586

View File

@@ -806,7 +806,7 @@ public:
} }
} }
/** Type tag for If true, yysval applies, otherwise /** Type tag for the semantic value. If true, yysval applies, otherwise
* yyfirstVal applies. */ * yyfirstVal applies. */
bool yyresolved; bool yyresolved;
/** Number of corresponding LALR(1) machine state. */ /** Number of corresponding LALR(1) machine state. */
@@ -986,7 +986,7 @@ class glr_state_set {
state_set_index state_set_index
yysplitStack (state_set_index yyk) yysplitStack (state_set_index yyk)
{ {
size_t k = yyk.uget(); const size_t k = yyk.uget();
yystates.push_back(yystates[k]); yystates.push_back(yystates[k]);
yylookaheadNeeds.push_back(yylookaheadNeeds[k]); yylookaheadNeeds.push_back(yylookaheadNeeds[k]);
return create_state_set_index(static_cast<std::ptrdiff_t>(yystates.size() - 1)); return create_state_set_index(static_cast<std::ptrdiff_t>(yystates.size() - 1));
@@ -1147,7 +1147,8 @@ class semantic_option {
YYLTYPE yyloc;]])[ YYLTYPE yyloc;]])[
}; };
/** Type of the items in the GLR stack. The is_state_ field /** Type of the items in the GLR stack.
* It can be either a glr_state or a semantic_union. The is_state_ field
* indicates which item of the union is valid. */ * indicates which item of the union is valid. */
class glr_stack_item class glr_stack_item
{ {
@@ -1301,9 +1302,9 @@ void glr_state::destroy (char const *yymsg, ]b4_namespace_ref[::]b4_parser_class
if (firstVal() != YY_NULLPTR) if (firstVal() != YY_NULLPTR)
{ {
semantic_option *yyoption = firstVal(); semantic_option& yyoption = *firstVal();
glr_state *yyrh = yyoption->state(); glr_state *yyrh = yyoption.state();
for (int yyn = yyrhsLength (yyoption->yyrule); yyn > 0; yyn -= 1) for (int yyn = yyrhsLength (yyoption.yyrule); yyn > 0; yyn -= 1)
{ {
yyrh->destroy (yymsg, yyparser]b4_user_args[); yyrh->destroy (yymsg, yyparser]b4_user_args[);
yyrh = yyrh->pred(); yyrh = yyrh->pred();
@@ -1373,7 +1374,7 @@ class state_stack {
} }
#endif #endif
static bool yyGLRStateNotNull(glr_state* s) { static bool glr_state_not_null(glr_state* s) {
return s != YY_NULLPTR; return s != YY_NULLPTR;
} }
@@ -1381,8 +1382,8 @@ class state_stack {
reduceToOneStack() { reduceToOneStack() {
const std::vector<glr_state*>::iterator yybegin = yytops.begin(); const std::vector<glr_state*>::iterator yybegin = yytops.begin();
const std::vector<glr_state*>::iterator yyend = yytops.end(); const std::vector<glr_state*>::iterator yyend = yytops.end();
std::vector<glr_state*>::iterator yyit = const std::vector<glr_state*>::iterator yyit =
std::find_if(yybegin, yyend, yyGLRStateNotNull); std::find_if(yybegin, yyend, glr_state_not_null);
if (yyit == yyend) if (yyit == yyend)
return false; return false;
for (state_set_index yyk = create_state_set_index(yyit + 1 - yybegin); for (state_set_index yyk = create_state_set_index(yyit + 1 - yybegin);
@@ -1393,19 +1394,25 @@ class state_stack {
return true; return true;
} }
/** Called when returning to deterministic operation to clean up the extra
* stacks. */
void void
yycompressStack () yycompressStack ()
{ {
if (yytops.size() != 1 || !isSplit()) if (yytops.size() != 1 || !isSplit())
return; return;
// yyr is the state after the split point.
glr_state* yyr = YY_NULLPTR; glr_state* yyr = YY_NULLPTR;
for (glr_state *yyp = firstTop(), *yyq = yyp->pred(); for (glr_state *yyp = firstTop(), *yyq = yyp->pred();
yyp != yysplitPoint; yyp != yysplitPoint;
yyr = yyp, yyp = yyq, yyq = yyp->pred()) yyr = yyp, yyp = yyq, yyq = yyp->pred())
yyp->setPred(yyr); yyp->setPred(yyr);
glr_stack_item* nextFreeItem = yysplitPoint->asItem() + 1; // This const_cast is okay, since anyway we have access to the mutable
// yyitems into which yysplitPoint points.
glr_stack_item* nextFreeItem =
const_cast<glr_state*>(yysplitPoint)->asItem() + 1;
yysplitPoint = YY_NULLPTR; yysplitPoint = YY_NULLPTR;
yytops.clearLastDeleted(); yytops.clearLastDeleted();
@@ -1450,11 +1457,11 @@ class state_stack {
return yytops.size(); return yytops.size();
} }
glr_state* firstTop() { glr_state* firstTop() const {
return yytops[create_state_set_index(0)]; return yytops[create_state_set_index(0)];
} }
glr_state* topAt(state_set_index i) { glr_state* topAt(state_set_index i) const {
return yytops[i]; return yytops[i];
} }
@@ -1745,7 +1752,8 @@ class state_stack {
public: public:
std::vector<glr_stack_item> yyitems; std::vector<glr_stack_item> yyitems;
glr_state* yysplitPoint; // Where the stack splits. Anything below this address is deterministic.
const glr_state* yysplitPoint;
glr_state_set yytops; glr_state_set yytops;
}; };
@@ -2295,7 +2303,7 @@ public:
for (state_set_index yyi = create_state_set_index(0); yyi.uget() < yystateStack.numTops(); ++yyi) for (state_set_index yyi = create_state_set_index(0); yyi.uget() < yystateStack.numTops(); ++yyi)
if (yyi != yyk && yystateStack.topAt(yyi) != YY_NULLPTR) if (yyi != yyk && yystateStack.topAt(yyi) != YY_NULLPTR)
{ {
glr_state* yysplit = yystateStack.yysplitPoint; const glr_state* yysplit = yystateStack.yysplitPoint;
glr_state* yyp = yystateStack.topAt(yyi); glr_state* yyp = yystateStack.topAt(yyi);
while (yyp != yys && yyp != yysplit while (yyp != yys && yyp != yysplit
&& yyp->yyposn >= yyposn) && yyp->yyposn >= yyposn)
@@ -2510,7 +2518,6 @@ public:
{ {
glr_state* yyoptState = yyopt.state(); glr_state* yyoptState = yyopt.state();
YYASSERT(yyoptState != YY_NULLPTR); YYASSERT(yyoptState != YY_NULLPTR);
glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1];
int yynrhs = yyrhsLength (yyopt.yyrule); int yynrhs = yyrhsLength (yyopt.yyrule);
YYRESULTTAG yyflag = YYRESULTTAG yyflag =
yyresolveStates (*yyoptState, yynrhs); yyresolveStates (*yyoptState, yynrhs);
@@ -2521,6 +2528,7 @@ public:
return yyflag; return yyflag;
} }
glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1];
yyrhsVals[YYMAXRHS + YYMAXLEFT].getState().setPred(yyopt.state());]b4_locations_if([[ yyrhsVals[YYMAXRHS + YYMAXLEFT].getState().setPred(yyopt.state());]b4_locations_if([[
if (yynrhs == 0) if (yynrhs == 0)
/* Set default location. */ /* Set default location. */
@@ -2837,8 +2845,6 @@ b4_dollar_popdef])[]dnl
while (true) while (true)
{ {
yysymbol_kind_t yytoken_to_shift;
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 != ]b4_namespace_ref::b4_parser_class::token::b4_symbol(empty, id)[);
@@ -2881,7 +2887,7 @@ 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. */
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 = ]b4_namespace_ref::b4_parser_class::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)