[Patches for Dlang support] modifications in examples

Adela Vais <[email protected]> Sat, 16 Jan 2021 23:19:00 +0200
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <CAPk8xGdfXPOVB7KEsufNrhzhoBrJuYmnD8DvhC7JFMKrLXdMwA@mail.gmail.com>
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.

I want to demonstrate the new code version because currently, the D example
makes a copy of the input, calls parse (which consumes the input), and then
iterates through the copy up until it has the same start character as the
input. It is not the most efficient way to do this task, but it's the only
correct method I could find way back in September.

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.

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.

Adela
0001-d-examples-simple-stylefix-for-paranthesis.patch (application/octet-stream, 1.3 KB)
From 4c6e4f897ff3fc82eef1545341bb7c90a8aaba09 Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Tue, 12 Jan 2021 20:49:41 +0200
Subject: [PATCH for Dlang support 1/3] d: examples: simple: stylefix for
 paranthesis

* examples/d/simple/calc.y: Here.
---
 examples/d/simple/calc.y | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)

diff --git a/examples/d/simple/calc.y b/examples/d/simple/calc.y
index e302036b..fafa4f17 100644
--- a/examples/d/simple/calc.y
+++ b/examples/d/simple/calc.y
@@ -115,16 +115,16 @@ if (isInputRange!R && is(ElementType!R : dchar))
 
     // Numbers.
     if (input.front.isNumber)
-      {
-        import std.conv : parse;
-        return Symbol(TokenKind.NUM, input.parse!int);
-      }
+    {
+      import std.conv : parse;
+      return Symbol(TokenKind.NUM, input.parse!int);
+    }
 
     // Individual characters
     auto ch = input.front;
     input.popFront;
     switch (ch)
-      {
+    {
       case '+':  return Symbol(TokenKind.PLUS);
       case '-':  return Symbol(TokenKind.MINUS);
       case '*':  return Symbol(TokenKind.STAR);
@@ -133,7 +133,7 @@ if (isInputRange!R && is(ElementType!R : dchar))
       case ')':  return Symbol(TokenKind.RPAR);
       case '\n': return Symbol(TokenKind.EOL);
       default: assert(0);
-      }
+    }
   }
 }
 
-- 
2.17.1
0002-d-examples-calc-demonstrate-the-use-of-std.conv.pars.patch (application/octet-stream, 1.7 KB)
From 1b2c111a9bcc341249ef914c37d246197693c7fe Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Sat, 2 Jan 2021 23:38:30 +0200
Subject: [PATCH for Dlang support 2/3] d: examples: calc: demonstrate the use
 of std.conv.parse for location

From Dlang v2.095.0 onwards, std.conv.parse reports the number of
consumed characters.

* examples/d/calc/calc.y: Here.
---
 examples/d/calc/calc.y | 26 ++++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y
index d17ee8d6..09e871b3 100644
--- a/examples/d/calc/calc.y
+++ b/examples/d/calc/calc.y
@@ -129,13 +129,27 @@ if (isInputRange!R && is(ElementType!R : dchar))
     if (input.front.isNumber)
     {
       int lenChars = 0;
-      auto copy = input;
-      import std.conv : parse;
-      value_.ival = input.parse!int;
-      while (!input.empty && copy.front != input.front)
+      import std.compiler : version_minor;
+      static if (version_minor >= 95)
       {
-        lenChars++;
-        copy.popFront;
+        // from Dlang v2.095.0 onwards std.conv.parse reports
+        // the number of consumed characters
+        import std.typecons : Flag, Yes;
+        import std.conv : parse;
+        auto parsed = parse!(int, R, Yes.doCount)(input);
+        value_.ival = parsed.data;
+        lenChars = cast(int) parsed.count;
+      }
+      else
+      {
+        auto copy = input;
+        import std.conv : parse;
+        value.ival = input.parse!int;
+        while (!input.empty && copy.front != input.front)
+        {
+          lenChars++;
+          copy.popFront;
+        }
       }
       location.begin = location.end;
       location.end.column += lenChars;
-- 
2.17.1
0003-d-examples-calc-remove-Value-from-Lexer.patch (application/octet-stream, 1.9 KB)
From 6270b7d8e4c5d7bb91c34f00a7a8f85db89bd33d Mon Sep 17 00:00:00 2001
From: Adela Vais <[email protected]>
Date: Sat, 16 Jan 2021 15:20:09 +0200
Subject: [PATCH for Dlang support 3/3] d: examples: calc: remove Value from
 Lexer

The Symbol constructor does not use it, so it is easier to not use Value at all.

* examples/d/calc/calc.y: Here.
---
 examples/d/calc/calc.y | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/examples/d/calc/calc.y b/examples/d/calc/calc.y
index 09e871b3..8d0e1e6e 100644
--- a/examples/d/calc/calc.y
+++ b/examples/d/calc/calc.y
@@ -108,8 +108,6 @@ if (isInputRange!R && is(ElementType!R : dchar))
     stderr.writeln(loc.toString(), ": ", s);
   }
 
-  Value value_;
-
   Symbol yylex()
   {
     import std.uni : isWhite, isNumber;
@@ -128,6 +126,7 @@ 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)
@@ -137,14 +136,14 @@ if (isInputRange!R && is(ElementType!R : dchar))
         import std.typecons : Flag, Yes;
         import std.conv : parse;
         auto parsed = parse!(int, R, Yes.doCount)(input);
-        value_.ival = parsed.data;
+        ival = parsed.data;
         lenChars = cast(int) parsed.count;
       }
       else
       {
         auto copy = input;
         import std.conv : parse;
-        value.ival = input.parse!int;
+        ival = input.parse!int;
         while (!input.empty && copy.front != input.front)
         {
           lenChars++;
@@ -153,7 +152,7 @@ if (isInputRange!R && is(ElementType!R : dchar))
       }
       location.begin = location.end;
       location.end.column += lenChars;
-      return Symbol(TokenKind.NUM, value_.ival, location);
+      return Symbol(TokenKind.NUM, ival, location);
     }
 
     // Individual characters
-- 
2.17.1