Re: [PATCH for Dlang support 1/2] d: create the Parser.Context class

Adela Vais <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <CAPk8xGfn-t3=Nc8X8FJ739-OpEEFYKO_j38_+2mUvvW16cxNHw@mail.gmail.com>
Hello,

Thank you for the explanations, H.S.! I followed your suggestion.

I modified as requested.

From 269f37d3d2f5d449e6530c825d26a7c3a9a8a38c Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Sun, 1 Nov 2020 22:13:42 +0200
Subject: [PATCH for Dlang support 1/2] d: fix Context class

All methods are now declared as const. Method getExpectedTokens() has now
the
functionality of reporting only the number of expected tokens.

* data/skeletons/lalr1.d: Fix Context class.
---
 data/skeletons/lalr1.d | 31 ++++++++++++++++---------------
 1 file changed, 16 insertions(+), 15 deletions(-)

diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d
index 9b7c786c..4259473d 100644
--- a/data/skeletons/lalr1.d
+++ b/data/skeletons/lalr1.d
@@ -721,9 +721,9 @@ m4_popdef([b4_at_dollar])])dnl
   public static final class Context
   {

-    private YYStack yystack;
+    private const(YYStack) yystack;
     private SymbolKind yytoken;]b4_locations_if([[
-    private ]b4_location_type[ yylocation;]])[
+    private const(]b4_location_type[) yylocation;]])[

     this(YYStack stack, SymbolKind kind]b4_locations_if([[,
]b4_location_type[ loc]])[)
     {
@@ -737,7 +737,7 @@ m4_popdef([b4_at_dollar])])dnl
       return yytoken;
     }]b4_locations_if([[

-    final ]b4_location_type[ getLocation()
+    final const(]b4_location_type[) getLocation() const
     {
       return yylocation;
     }]])[
@@ -747,12 +747,12 @@ m4_popdef([b4_at_dollar])])dnl
      * YYARG is null, return the number of expected tokens (guaranteed to
      * be less than YYNTOKENS).
      */
-    int getExpectedTokens(SymbolKind[] yyarg, int yyargn)
+    int getExpectedTokens(SymbolKind[] yyarg, int yyargn) const
     {
       return getExpectedTokens(yyarg, 0, yyargn);
     }

-    int getExpectedTokens(SymbolKind[] yyarg, int yyoffset, int yyargn)
+    int getExpectedTokens(SymbolKind[] yyarg, int yyoffset, int yyargn)
const
     {
       int yycount = yyoffset;
       int yyn = yypact_[this.yystack.stateAt(0)];
@@ -769,16 +769,17 @@ m4_popdef([b4_at_dollar])])dnl
         for (int yyx = yyxbegin; yyx < yyxend; ++yyx)
           if (yycheck_[yyx + yyn] == yyx && yyx != ]b4_symbol(1, kind)[
               && !yyTableValueIsError(yytable_[yyx + yyn]))
-            yycount++;
-        if (yycount < yyargn)
-        {
-          yycount = 0;
-          for (int x = yyxbegin; x < yyxend; ++x)
-            if (yycheck_[x + yyn] == x && x != ]b4_symbol(1, kind)[
-                && !yyTableValueIsError(yytable_[x + yyn]))
-              yyarg[yycount++] = SymbolKind(x);
-        }
+          {
+            if (yyarg is null)
+              ++yycount;
+            else if (yycount == yyargn)
+              return 0;
+            else
+              yyarg[yycount++] = SymbolKind(yyx);
+          }
       }
+      if (yyarg !is null && yycount == yyoffset && yyoffset < yyargn)
+        yyarg[yyoffset] = ]b4_symbol(empty, kind)[;
       return yycount - yyoffset;
     }
   }
@@ -897,7 +898,7 @@ m4_popdef([b4_at_dollar])])dnl
       stack.length -= num;
     }

-    public final int stateAt (int i)
+    public final int stateAt (int i) const
     {
       return stack[$-i-1].state;
     }
-- 
2.17.1

În mie., 28 oct. 2020 la 07:43, Akim Demaille <[email protected]> a scris:

> Hi H.S.,
>
> > Le 27 oct. 2020 à 17:55, H. S. Teoh <[email protected]> a écrit :
> >
> > On Tue, Oct 27, 2020 at 07:38:46AM +0100, Akim Demaille wrote:
> > [...]
> >>> +  /**
> >>> +   * Information needed to get the list of expected tokens and to
> forge
> >>> +   * a syntax error diagnostic.
> >>> +   */
> >>> +  public static final class Context
> >>> +  {
> >>> +
> >>> +    private YYStack yystack;
> >>
> >> Is this a copy of the stack?  It does not look like a
> >> reference/pointer to the this.  We must not copy it, it's big, and
> >> readonly here.  It should be "const", but I don't know how that is
> >> spelled in D.
> > [...]
> >
> > Is YYStack a struct or class? Or an array? If it's an array or class,
> > it's a reference type by default.
>
> It's a struct.  But I guess it's not so bad as it contains only an
> array:
>
>   private final struct YYStack {
>     private YYStackElement[] stack = [];
>   ...
>
> so after all, it is not so big, I expect the inner array to be
> "copied" by reference.
>
>
> > W.r.t. const, there are several ways to spell it, depending on the
> > intended semantics.  If it should be an immutable reference, then you
> > could write it as:
> >
> >       private const(YYStack) yystack;
> >
> > In this case, the variable cannot be reassigned after initialization,
> > and if it's a reference type, it cannot be rebound.  Also, any elements
> > you retrieve from it will be const (const in D is transitive).
>
> I believe this is what we need here.
>
> > If it's an array, you can optionally have a mutable slice of immutable
> > elements (i.e., a view that can change, but the underlying elements are
> > not changeable):
> >
> >       private const(E)[] yystack;     // where E is the element type of
> YYStack
>
> Now I see why const takes parens.
>
> Thanks a lot for your explanations!
>
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.