Re: [PATCH for Dlang support] d: fix b4_rhs_location

Akim Demaille <[email protected]>
Newsgroups gmane.comp.parsers.bison.patches
Message-ID <[email protected]>
Hi Adela,

Apologies for the delays.

> Le 14 sept. 2020 à 20:12, Adela Vais <[email protected]> a écrit :
> 
> It was calling yystack.locationAt() with two parameters instead of one.
> 
> * data/skeletons/d.m4: Fix.

Good catch, thanks!  But that's a bug fix without a regression test, so
I'm adding one.

Cheers!

commit f8cd049ecc493067720ab873747391795ce59c64
Author: Akim Demaille <[email protected]>
Date:   Sun Sep 20 17:06:04 2020 +0200

    tests: check the location of the right-hand side symbols
    
    The D skeleton was not properly supporting @1 etc.
    Reported by Adela Vais.
    https://lists.gnu.org/r/bison-patches/2020-09/msg00049.html
    
    * data/skeletons/d.m4 (b4_rhs_location): Fix it.
    * tests/calc.at: Check the support of @n for all the skeletons.

diff --git a/data/skeletons/d.m4 b/data/skeletons/d.m4
index 7a9d3efe..abaa4c7b 100644
--- a/data/skeletons/d.m4
+++ b/data/skeletons/d.m4
@@ -345,7 +345,7 @@ m4_define([b4_lhs_location],
 # Expansion of @POS, where the current rule has RULE-LENGTH symbols
 # on RHS.
 m4_define([b4_rhs_location],
-[yystack.locationAt ([$1], [$2])])
+[yystack.locationAt (b4_subtract($@))])
 
 
 # b4_lex_param
diff --git a/tests/calc.at b/tests/calc.at
index c3557aa6..7da61af4 100644
--- a/tests/calc.at
+++ b/tests/calc.at
@@ -577,7 +577,7 @@ exp:
       [c], [[
       {
         char buf[1024];
-        snprintf (buf, sizeof buf, "calc: error: %d != %d", $1, $3);]AT_YYERROR_ARG_LOC_IF([[
+        snprintf (buf, sizeof buf, "error: %d != %d", $1, $3);]AT_YYERROR_ARG_LOC_IF([[
         yyerror (&@$, ]AT_PARAM_IF([result, count, nerrs, ])[buf);]], [[
         {
           YYLTYPE old_yylloc = yylloc;
@@ -590,17 +590,39 @@ exp:
       [c++], [[
       {
         char buf[1024];
-        snprintf (buf, sizeof buf, "calc: error: %d != %d", $1, $3);
+        snprintf (buf, sizeof buf, "error: %d != %d", $1, $3);
         ]AT_GLR_IF([[yyparser.]])[error (]AT_LOCATION_IF([[@$, ]])[buf);
       }]],
       [d], [[
-      yyerror (]AT_LOCATION_IF([[@$, ]])[format ("calc: error: %d != %d", $1, $3));]])[
+      yyerror (]AT_LOCATION_IF([[@$, ]])[format ("error: %d != %d", $1, $3));]])[
     $$ = $1;
   }
 | exp '+' exp        { $$ = $1 + $3; }
 | exp '-' exp        { $$ = $1 - $3; }
 | exp '*' exp        { $$ = $1 * $3; }
-| exp '/' exp        { $$ = $1 / $3; }
+| exp '/' exp
+  {
+    if ($3 == 0)]AT_LANG_CASE(
+      [c], [[
+      {]AT_YYERROR_ARG_LOC_IF([[
+        yyerror (&@3, ]AT_PARAM_IF([result, count, nerrs, ])["error: null divisor");]], [[
+        {
+          YYLTYPE old_yylloc = yylloc;
+          yylloc = @3;
+          yyerror (]AT_PARAM_IF([result, count, nerrs, ])["error: null divisor");
+          yylloc = old_yylloc;
+        }
+        ]])[
+      }]],
+      [c++], [[
+      {
+        ]AT_GLR_IF([[yyparser.]])[error (]AT_LOCATION_IF([[@3, ]])["error: null divisor");
+      }]],
+      [d], [[
+      yyerror (]AT_LOCATION_IF([[@3, ]])["error: null divisor");]])[
+    else
+      $$ = $1 / $3;
+  }
 | '-' exp  %prec NEG { $$ = -$2; }
 | exp '^' exp        { $$ = power ($1, $3); }
 | '(' exp ')'        { $$ = $2; }
@@ -728,12 +750,18 @@ exp:
 | exp '=' exp
   {
     if ($1.intValue () != $3.intValue ())
-      yyerror (]AT_LOCATION_IF([[@$, ]])["calc: error: " + $1 + " != " + $3);
+      yyerror (]AT_LOCATION_IF([[@$, ]])["error: " + $1 + " != " + $3);
   }
 | exp '+' exp        { $$ = $1 + $3; }
 | exp '-' exp        { $$ = $1 - $3; }
 | exp '*' exp        { $$ = $1 * $3; }
-| exp '/' exp        { $$ = $1 / $3; }
+| exp '/' exp
+  {
+    if ($3.intValue () == 0)
+      yyerror (]AT_LOCATION_IF([[@3, ]])["error: null divisor");
+    else
+      $$ = $1 / $3;
+  }
 | '-' exp  %prec NEG { $$ = -$2; }
 | exp '^' exp        { $$ = (int) Math.pow ($1, $3); }
 | '(' exp ')'        { $$ = $2; }
@@ -1003,7 +1031,7 @@ _AT_CHECK_CALC_ERROR([$1], [0],
 ]AT_JAVA_IF([1.18-1.19], [1.18])[: syntax error on token [')'] (expected: [number] ['-'] ['('] ['!'])
 ]AT_JAVA_IF([1.23-1.24], [1.23])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
 ]AT_JAVA_IF([1.41-1.42], [1.41])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
-]AT_JAVA_IF([1.1-1.47], [1.1-46])[: calc: error: 4444 != 1]])
+]AT_JAVA_IF([1.1-1.47], [1.1-46])[: error: 4444 != 1]])
 
 # The same, but this time exercising explicitly triggered syntax errors.
 # POSIX says the lookahead causing the error should not be discarded.
@@ -1011,14 +1039,14 @@ _AT_CHECK_CALC_ERROR([$1], [0], [(!) + (1 2) = 1],
                      [[final: 2222 0 2]],
                      [102],
 [AT_JAVA_IF([1.10-1.11], [1.10])[: syntax error on token [number] (expected: ['='] ['-'] ['+'] ['*'] ['/'] ['^'] [')'])
-]AT_JAVA_IF([1.1-1.16], [1.1-15])[: calc: error: 2222 != 1]])
+]AT_JAVA_IF([1.1-1.16], [1.1-15])[: error: 2222 != 1]])
 
 _AT_CHECK_CALC_ERROR([$1], [0], [(- *) + (1 2) = 1],
                      [[final: 2222 0 3]],
                      [113],
 [AT_JAVA_IF([1.4-1.5], [1.4])[: syntax error on token ['*'] (expected: [number] ['-'] ['('] ['!'])
 ]AT_JAVA_IF([1.12-1.13], [1.12])[: syntax error on token [number] (expected: ['='] ['-'] ['+'] ['*'] ['/'] ['^'] [')'])
-]AT_JAVA_IF([1.1-1.18], [1.1-17])[: calc: error: 2222 != 1]])
+]AT_JAVA_IF([1.1-1.18], [1.1-17])[: error: 2222 != 1]])
 
 # Check that yyerrok works properly: second error is not reported,
 # third and fourth are.  Parse status is successful.
@@ -1056,7 +1084,10 @@ _AT_CHECK_CALC_ERROR([$1], [0], [(1 + # + 1) = 1111],
                      [102],
 [[1.6: syntax error: invalid character: '#']])
 
-
+_AT_CHECK_CALC_ERROR([$1], [0], [(1 + 1) / (1 - 1)],
+                     [[final: 2 0 1]],
+                     [102],
+[AT_JAVA_IF([1.11-1.18], [1.11-17])[: error: null divisor]])
 
 AT_BISON_OPTION_POPDEFS
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.