bison-patches
[Top][All Lists]
Advanced

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

[PATCH 08/12] yacc.c: let custom error messages see the location


From: Akim Demaille
Subject: [PATCH 08/12] yacc.c: let custom error messages see the location
Date: Thu, 16 Jan 2020 07:58:19 +0100

* data/skeletons/yacc.c (yyparse_context_t): Add yylloc when
applicable.
(yyparse_context_location): New.
* tests/local.at (AT_YYERROR_DEFINE(c)): Handle the location.
* tests/calc.at: Check it.
---
 data/skeletons/yacc.c | 19 ++++++++++++++-----
 tests/calc.at         |  1 +
 tests/local.at        |  4 +++-
 3 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/data/skeletons/yacc.c b/data/skeletons/yacc.c
index 5c7a938d..b6485258 100644
--- a/data/skeletons/yacc.c
+++ b/data/skeletons/yacc.c
@@ -1050,7 +1050,8 @@ yy_lac (yy_state_t *yyesa, yy_state_t **yyes,
 [[typedef struct
 {
   yy_state_t *yyssp;
-  int yytoken;]b4_lac_if([[
+  int yytoken;]b4_locations_if([[
+  YYLTYPE *yylloc;]])[]b4_lac_if([[
   yy_state_t *yyesa;
   yy_state_t **yyes_p;
   YYPTRDIFF_T *yyes_capacity_p;]])[
@@ -1165,7 +1166,15 @@ yysyntax_error_arguments (const yyparse_context_t *yyctx,
 
 ]m4_case(b4_percent_define_get([[parse.error]]),
          [custom],
-[[static int
+[b4_locations_if([[/*  The location of this context.  */
+static YYLTYPE *
+yyparse_context_location (const yyparse_context_t *yyctx)
+{
+  return yyctx->yylloc;
+}]])[
+
+/* User defined funtion to report a syntax error.  */
+static int
 yyreport_syntax_error (const yyparse_context_t *yyctx);]],
          [verbose],
 [[# ifndef yystrlen
@@ -1820,10 +1829,10 @@ yyerrlab:
          [custom],
 [[      {
         yyparse_context_t yyctx
-          = {yyssp, yytoken]b4_lac_if([[, yyesa, &yyes, 
&yyes_capacity]])[};]b4_lac_if([[
+          = {yyssp, yytoken]b4_locations_if([[, &yylloc]])[]b4_lac_if([[, 
yyesa, &yyes, &yyes_capacity]])[};]b4_lac_if([[
         if (yychar != YYEMPTY)
           YY_LAC_ESTABLISH;]])[
-        if (yyreport_syntax_error (]b4_yyerror_args[&yyctx) == 2)
+        if (yyreport_syntax_error (&yyctx) == 2)
           goto yyexhaustedlab;
       }]],
          [simple],
@@ -1832,7 +1841,7 @@ yyerrlab:
 [[      {
         char const *yymsgp = YY_("syntax error");
         yyparse_context_t yyctx
-          = {yyssp, yytoken]b4_lac_if([[, yyesa, &yyes, &yyes_capacity]])[};
+          = {yyssp, yytoken]b4_locations_if([[, &yylloc]])[]b4_lac_if([[, 
yyesa, &yyes, &yyes_capacity]])[};
         int yysyntax_error_status;]b4_lac_if([[
         if (yychar != YYEMPTY)
           YY_LAC_ESTABLISH;]])[
diff --git a/tests/calc.at b/tests/calc.at
index 6b36c521..0ed9709c 100644
--- a/tests/calc.at
+++ b/tests/calc.at
@@ -788,6 +788,7 @@ AT_CHECK_CALC_LALR([%no-lines %define api.pure %define 
parse.error verbose %debu
 
 
 AT_CHECK_CALC_LALR([%define parse.error custom])
+AT_CHECK_CALC_LALR([%define parse.error custom %locations])
 
 # ----------------------- #
 # Simple GLR Calculator.  #
diff --git a/tests/local.at b/tests/local.at
index 05b2ed9c..e314b8bf 100644
--- a/tests/local.at
+++ b/tests/local.at
@@ -619,7 +619,9 @@ yyreport_syntax_error (const yyparse_context_t *ctx)
   if (n == -2)
     return 2;
   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)
       {
-- 
2.24.1




reply via email to

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