bison-patches
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Patches for Dlang support] modifications in examples


From: Akim Demaille
Subject: Re: [Patches for Dlang support] modifications in examples
Date: Sun, 17 Jan 2021 08:50:52 +0100

Hi Adela,

> Le 16 janv. 2021 à 22:19, Adela Vais <adela.vais99@gmail.com> a écrit :
> 
> Hello!
> 
> From Dlang v2.095.0 onwards, the std.conv.parse function, used in the calc
> example, reports the number of consumed characters, so I modifed the calc
> example to use it.

Great, thanks!

FTR, location track character positions, not bytes.  So both are actually 
incorrect, if we were to accept say UTF-8.

None of the example are correct on this regard, because it's too hard to do 
portably.  But bison itself does it.  As a matter of fact, bison tracks both 
character and byte positions in its locations.

> Because it is a new addition to the language (Jan 1st 2021), I want to
> support both (before and after v2.095.0) versions of the code.

Yes, that it needed.  However, from a stylistic point of view, I think it would 
be better to have the call site use the modern way, but to install the missing 
feature elsewhere.  One should _try_ to avoid cluttering the code with 
portability issues, rather portability should be handled by providing the new 
feature before.

I'm not sure that's possible in this case though.


> I also made 2 small modifications for the examples: I removed the Value
> value from the Lexer class in the calc example and did style fix for
> paranthesis in the simple example.

One parenthesis, two parentheses ().  But actually you meant "braces" {}, not 
"parentheses".

Perfect, thanks a lot, installed.  I also installed this afterwards.

Cheers!


commit c20b6f1a8eed744cdc8add59df578e158e13fa55
Author: Akim Demaille <akim.demaille@gmail.com>
Date:   Sun Jan 17 08:17:50 2021 +0100

    d: examples: reduce scopes
    
    * data/skeletons/lalr1.d (YYLocation.step): New.
    * examples/d/calc/calc.y (Lexer): Reduce scopes to avoid uninitialized
    varibles.
    Factor the handling of locations.
    We don't need lenChars.

diff --git a/data/skeletons/lalr1.d b/data/skeletons/lalr1.d
index 06d8d4054..62b12d1c2 100644
--- a/data/skeletons/lalr1.d
+++ b/data/skeletons/lalr1.d
@@ -164,7 +164,8 @@ public struct ]b4_location_type[
    * Create a <code>Location</code> denoting an empty range located at
    * a given point.
    * @@param loc The position at which the range is anchored.  */
-  public this (Position loc) {
+  public this(Position loc)
+  {
     this.begin = this.end = loc;
   }
 
@@ -172,16 +173,25 @@ public struct ]b4_location_type[
    * Create a <code>Location</code> from the endpoints of the range.
    * @@param begin The first position included in the range.
    * @@param end   The first position beyond the range.  */
-  public this (Position begin, Position end)
+  public this(Position begin, Position end)
   {
     this.begin = begin;
     this.end = end;
   }
 
+  /**
+   * Reset initial location to final location.
+   */
+  public void step()
+  {
+    this.begin = this.end;
+  }
+
   /**
    * A representation of the location.
    */
-  public string toString () const {
+  public string toString() const
+  {
     auto end_col = 0 < end.column ? end.column - 1 : 0;
     auto res = begin.toString ();
     if (end.filename && begin.filename != end.filename)
diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y
index 83e378e49..d5d1274e6 100644
--- a/examples/d/calc/calc.y
+++ b/examples/d/calc/calc.y
@@ -115,10 +115,10 @@ if (isInputRange!R && is(ElementType!R : dchar))
     // Skip initial spaces
     while (!input.empty && input.front != '\n' && isWhite(input.front))
     {
-      location.begin = location.end;
       location.end.column++;
       input.popFront;
     }
+    location.step();
 
     if (input.empty)
       return Symbol(TokenKind.YYEOF, location);
@@ -126,8 +126,6 @@ if (isInputRange!R && is(ElementType!R : dchar))
     // Numbers.
     if (input.front.isNumber)
     {
-      int ival;
-      int lenChars = 0;
       import std.compiler : version_minor;
       static if (version_minor >= 95)
       {
@@ -136,29 +134,26 @@ if (isInputRange!R && is(ElementType!R : dchar))
         import std.typecons : Flag, Yes;
         import std.conv : parse;
         auto parsed = parse!(int, R, Yes.doCount)(input);
-        ival = parsed.data;
-        lenChars = cast(int) parsed.count;
+        int ival = parsed.data;
+        location.end.column += cast(int) parsed.count;
       }
       else
       {
         auto copy = input;
         import std.conv : parse;
-        ival = input.parse!int;
+        int ival = input.parse!int;
         while (!input.empty && copy.front != input.front)
         {
-          lenChars++;
+          location.end.column++;
           copy.popFront;
         }
       }
-      location.begin = location.end;
-      location.end.column += lenChars;
       return Symbol(TokenKind.NUM, ival, location);
     }
 
     // Individual characters
     auto ch = input.front;
     input.popFront;
-    location.begin = location.end;
     location.end.column++;
     switch (ch)
     {




reply via email to

[Prev in Thread] Current Thread [Next in Thread]