bison-patches
[Top][All Lists]
Advanced

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

[PATCH 5/7] tests: yacc.c: avoid yysyntax_error_arguments


From: Akim Demaille
Subject: [PATCH 5/7] tests: yacc.c: avoid yysyntax_error_arguments
Date: Sat, 21 Mar 2020 12:53:13 +0100

Because glr.c shares the same testing routines, we also need to
convert it.

* data/skeletons/glr.c (yyparse_context_token): New.
* tests/local.at (yyreport_syntax_error): here.
---
 data/skeletons/glr.c | 18 +++++++++++++++++-
 tests/local.at       | 39 ++++++++++++++++++++++-----------------
 2 files changed, 39 insertions(+), 18 deletions(-)

diff --git a/data/skeletons/glr.c b/data/skeletons/glr.c
index e9a020b6..aec53417 100644
--- a/data/skeletons/glr.c
+++ b/data/skeletons/glr.c
@@ -2116,6 +2116,10 @@ yyexpected_tokens (const yyGLRStack* yystackp,
   return yycount;
 }
 
+static int
+yysyntax_error_arguments (const yyGLRStack* yystackp,
+                          int yyarg[], int yyargn) YY_ATTRIBUTE_UNUSED;
+
 static int
 yysyntax_error_arguments (const yyGLRStack* yystackp,
                           int yyarg[], int yyargn)
@@ -2172,7 +2176,19 @@ typedef yyGLRStack yyparse_context_t;
 static int
 yyreport_syntax_error (const yyGLRStack* yystackp]b4_user_formals[);
 
-]b4_locations_if([[/*  The location of this context.  */
+/* The token type of the lookahead of this context.  */
+static int
+yyparse_context_token (const yyGLRStack *yystackp) YY_ATTRIBUTE_UNUSED;
+
+static int
+yyparse_context_token (const yyGLRStack *yystackp)
+{
+  YYUSE (yystackp);
+  yySymbol yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
+  return yytoken;
+}
+
+]b4_locations_if([[/* The location of the lookahead of this context.  */
 static YYLTYPE *
 yyparse_context_location (const yyGLRStack *yystackp) YY_ATTRIBUTE_UNUSED;
 
diff --git a/tests/local.at b/tests/local.at
index 25f08462..42de48c6 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -624,31 +624,36 @@ location_print (FILE *yyo, ]AT_YYLTYPE[ const * const 
yylocp)
 int
 yyreport_syntax_error (const yyparse_context_t *ctx]AT_PARAM_IF([, 
AT_PARSE_PARAMS])[)
 {
-  /* Arguments of yyformat: reported tokens (one for the "unexpected",
-     one per "expected"). */
-  int arg[YYNTOKENS];
-  int n = yysyntax_error_arguments (ctx, arg, 
YYNTOKENS);]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
+  int res = 0;]AT_PARAM_IF([m4_bpatsubst(m4_defn([AT_PARSE_PARAMS]),
               [[^,]+[^A-Za-z_0-9]\([A-Za-z_][A-Za-z_0-9]*\),* *], [
   YYUSE (\1);])])[]m4_bmatch(m4_defn([AT_PARSE_PARAMS]), [nerrs],[[
   ++global_nerrs;
-  ++*nerrs;]])[
-  if (n < 0)
-    return n;
-  if (n)
-  {]AT_LOCATION_IF([[
-    LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
-    fprintf (stderr, ": ");]])[
-    fprintf (stderr, "syntax error on token [%s]", yysymbol_name (arg[0]));
-    if (1 < n)
+  ++*nerrs;]])[]AT_LOCATION_IF([[
+  LOCATION_PRINT (stderr, *yyparse_context_location (ctx));
+  fprintf (stderr, ": ");]])[
+  fprintf (stderr, "syntax error");
+  {
+    int la = yyparse_context_token (ctx);
+    if (la != YYEMPTY)
+      fprintf (stderr, " on token [%s]", yysymbol_name (la));
+  }
+  {
+    enum { TOKENMAX = 10 };
+    int expected[TOKENMAX];
+    int n = yyexpected_tokens (ctx, expected, TOKENMAX);
+    /* Forward errors to yyparse.  */
+    if (n < 0)
+      res = n;
+    else if (0 < n)
       {
         fprintf (stderr, " (expected:");
-        for (int i = 1; i < n; ++i)
-          fprintf (stderr, " [%s]", yysymbol_name (arg[i]));
+        for (int i = 0; i < n; ++i)
+          fprintf (stderr, " [%s]", yysymbol_name (expected[i]));
         fprintf (stderr, ")");
       }
-    fprintf (stderr, "\n");
   }
-  return 0;
+  fprintf (stderr, "\n");
+  return res;
 }
 ]])[
 
-- 
2.25.1




reply via email to

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