[PATCH 06/10] glr2.cc: misc cleanups

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
From: Valentin Tolmer <[email protected]>

* 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.
---
 data/skeletons/glr2.cc | 42 ++++++++++++++++++++++++------------------
 1 file changed, 24 insertions(+), 18 deletions(-)

diff --git a/data/skeletons/glr2.cc b/data/skeletons/glr2.cc
index f334478e..a1194b61 100644
--- a/data/skeletons/glr2.cc
+++ b/data/skeletons/glr2.cc
@@ -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.  */
   bool yyresolved;
   /** Number of corresponding LALR(1) machine state.  */
@@ -986,7 +986,7 @@ class glr_state_set {
   state_set_index
   yysplitStack (state_set_index yyk)
   {
-    size_t k = yyk.uget();
+    const size_t k = yyk.uget();
     yystates.push_back(yystates[k]);
     yylookaheadNeeds.push_back(yylookaheadNeeds[k]);
     return create_state_set_index(static_cast<std::ptrdiff_t>(yystates.size() - 1));
@@ -1147,7 +1147,8 @@ class semantic_option {
   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.  */
 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)
         {
-          semantic_option *yyoption = firstVal();
-          glr_state *yyrh = yyoption->state();
-          for (int yyn = yyrhsLength (yyoption->yyrule); yyn > 0; yyn -= 1)
+          semantic_option& yyoption = *firstVal();
+          glr_state *yyrh = yyoption.state();
+          for (int yyn = yyrhsLength (yyoption.yyrule); yyn > 0; yyn -= 1)
             {
               yyrh->destroy (yymsg, yyparser]b4_user_args[);
               yyrh = yyrh->pred();
@@ -1373,7 +1374,7 @@ class state_stack {
   }
 #endif
 
-  static bool yyGLRStateNotNull(glr_state* s) {
+  static bool glr_state_not_null(glr_state* s) {
     return s != YY_NULLPTR;
   }
 
@@ -1381,8 +1382,8 @@ class state_stack {
   reduceToOneStack() {
     const std::vector<glr_state*>::iterator yybegin = yytops.begin();
     const std::vector<glr_state*>::iterator yyend = yytops.end();
-    std::vector<glr_state*>::iterator yyit =
-      std::find_if(yybegin, yyend, yyGLRStateNotNull);
+    const std::vector<glr_state*>::iterator yyit =
+      std::find_if(yybegin, yyend, glr_state_not_null);
     if (yyit == yyend)
       return false;
     for (state_set_index yyk = create_state_set_index(yyit + 1 - yybegin);
@@ -1393,19 +1394,25 @@ class state_stack {
     return true;
   }
 
+  /** Called when returning to deterministic operation to clean up the extra
+   * stacks. */
   void
   yycompressStack ()
   {
     if (yytops.size() != 1 || !isSplit())
       return;
 
+    // yyr is the state after the split point.
     glr_state* yyr = YY_NULLPTR;
     for (glr_state *yyp = firstTop(), *yyq = yyp->pred();
          yyp != yysplitPoint;
          yyr = yyp, yyp = yyq, yyq = yyp->pred())
       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;
     yytops.clearLastDeleted();
 
@@ -1450,11 +1457,11 @@ class state_stack {
     return yytops.size();
   }
 
-  glr_state* firstTop() {
+  glr_state* firstTop() const {
     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];
   }
 
@@ -1745,7 +1752,8 @@ class state_stack {
  public:
 
   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;
 };
 
@@ -2295,7 +2303,7 @@ public:
         for (state_set_index yyi = create_state_set_index(0); yyi.uget() < yystateStack.numTops(); ++yyi)
           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);
               while (yyp != yys && yyp != yysplit
                      && yyp->yyposn >= yyposn)
@@ -2510,7 +2518,6 @@ public:
   {
     glr_state* yyoptState = yyopt.state();
     YYASSERT(yyoptState != YY_NULLPTR);
-    glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1];
     int yynrhs = yyrhsLength (yyopt.yyrule);
     YYRESULTTAG yyflag =
       yyresolveStates (*yyoptState, yynrhs);
@@ -2521,6 +2528,7 @@ public:
         return yyflag;
       }
 
+    glr_stack_item yyrhsVals[YYMAXRHS + YYMAXLEFT + 1];
     yyrhsVals[YYMAXRHS + YYMAXLEFT].getState().setPred(yyopt.state());]b4_locations_if([[
     if (yynrhs == 0)
       /* Set default location.  */
@@ -2837,8 +2845,6 @@ b4_dollar_popdef])[]dnl
 
       while (true)
         {
-          yysymbol_kind_t yytoken_to_shift;
-
           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)[);
 
@@ -2881,7 +2887,7 @@ b4_dollar_popdef])[]dnl
              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
              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)[;
           yyposn += 1;
           for (state_set_index yys = create_state_set_index(0); yys.uget() < yystack.yystateStack.numTops(); ++yys)
-- 
2.29.2
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.