]> git.saurik.com Git - bison.git/blobdiff - data/yacc.c
Alexandre Duret-Lutz <adl@gnu.org>
[bison.git] / data / yacc.c
index cf6c43297ba489259031831bb684af1da6ceb210..2e9c9fd0b0d04859b5fa1f8fad95e97b3b19fb82 100644 (file)
@@ -501,20 +501,50 @@ do                                                                \
     }                                                          \
 while (0)
 
+
 #define YYTERROR       1
 #define YYERRCODE      256
 
-/* YYLLOC_DEFAULT -- Compute the default location (before the actions
-   are run).  */
+
+/* YYLLOC_DEFAULT -- Set CURRENT to span from RHS[1] to RHS[N].
+   If N is 0, then set CURRENT to the empty location which ends
+   the previous symbol: RHS[0] (always defined).  */
 
 #ifndef YYLLOC_DEFAULT
-# define YYLLOC_DEFAULT(Current, Rhs, N)               \
-   ((Current).first_line   = (Rhs)[1].first_line,      \
-    (Current).first_column = (Rhs)[1].first_column,    \
-    (Current).last_line    = (Rhs)[N].last_line,       \
-    (Current).last_column  = (Rhs)[N].last_column)
+# define YYLLOC_DEFAULT(Current, Rhs, N)                                    \
+do {                                                                        \
+  if (N)                                                                    \
+    {                                                                       \
+      (Current).first_line   = (Rhs)[1].first_line;                         \
+      (Current).first_column = (Rhs)[1].first_column;                       \
+      (Current).last_line    = (Rhs)[N].last_line;                          \
+      (Current).last_column  = (Rhs)[N].last_column;                        \
+    }                                                                       \
+  else                                                                      \
+    {                                                                       \
+      (Current).first_line   = (Current).last_line   = (Rhs)[0].last_line;   \
+      (Current).first_column = (Current).last_column = (Rhs)[0].last_column; \
+    }                                                                       \
+} while(0)
+#endif
+
+
+/* YY_LOCATION_PRINT -- Print the location on the stream.
+   This macro was not mandated originally: define only if we know
+   we won't break user code: when these are the locations we know.  */
+
+#ifndef YY_LOCATION_PRINT
+# if YYLTYPE_IS_TRIVIAL
+#  define YY_LOCATION_PRINT(File, Loc)                 \
+     fprintf (File, "%d.%d-%d.%d",                     \
+              (Loc).first_line, (Loc).first_column,    \
+              (Loc).last_line,  (Loc).last_column)
+# else
+#  define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+# endif
 #endif
 
+
 /* YYLEX -- calling `yylex' with the right arguments.  */
 
 #ifdef YYLEX_PARAM
@@ -770,7 +800,8 @@ b4_c_function_def([yyparse], [int], b4_parse_param)
   YYLTYPE yylsa[YYINITDEPTH];
   YYLTYPE *yyls = yylsa;
   YYLTYPE *yylsp;
-  YYLTYPE *yylerrsp;]])[
+  /* The locations where the error started and ended. */
+  YYLTYPE yyerror_range[2];]])[
 
 #define YYPOPSTACK   (yyvsp--, yyssp--]b4_location_if([, yylsp--])[)
 
@@ -799,17 +830,26 @@ b4_c_function_def([yyparse], [int], b4_parse_param)
 
   yyssp = yyss;
   yyvsp = yyvs;
-]b4_location_if([  yylsp = yyls;])[
-]m4_ifdef([b4_initial_action], [
-m4_pushdef([b4_at_dollar],     [(*yylsp)])dnl
-m4_pushdef([b4_dollar_dollar], [(*yyvsp)])dnl
+]b4_location_if([[  yylsp = yyls;
+#if YYLTYPE_IS_TRIVIAL
+  /* Initialize the default location before parsing starts.  */
+  yylloc.first_line   = yylloc.last_line   = 1;
+  yylloc.first_column = yylloc.last_column = 0;
+#endif
+]])
+m4_ifdef([b4_initial_action], [
+m4_pushdef([b4_at_dollar],     [yylloc])dnl
+m4_pushdef([b4_dollar_dollar], [yylval])dnl
   /* User initialization code. */
   b4_initial_action
 m4_popdef([b4_dollar_dollar])dnl
 m4_popdef([b4_at_dollar])dnl
 /* Line __line__ of yacc.c.  */
 b4_syncline([@oline@], [@ofile@])])dnl
-
+[
+  yyvsp[0] = yylval;
+]b4_location_if([[    yylsp[0] = yylloc;
+]])
 [  goto yysetstate;
 
 /*------------------------------------------------------------.
@@ -992,8 +1032,8 @@ yyreduce:
   yyval = yyvsp[1-yylen];
 
 ]b4_location_if(
-[  /* Default location. */
-  YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);])[
+[[  /* Default location. */
+  YYLLOC_DEFAULT (yyloc, yylsp - yylen, yylen);]])[
   YY_REDUCE_PRINT (yyn);
   switch (yyn)
     ]{
@@ -1097,7 +1137,7 @@ yyerrlab:
        yyerror (]b4_yyerror_args["syntax error");
     }
 
-]b4_location_if([  yylerrsp = yylsp;])[
+]b4_location_if([[  yyerror_range[0] = yylloc;]])[
 
   if (yyerrstatus == 3)
     {
@@ -1111,6 +1151,7 @@ yyerrlab:
          if (yychar == YYEOF)
             for (;;)
               {
+]b4_location_if([[                 yyerror_range[0] = *yylsp;]])[
                 YYPOPSTACK;
                 if (yyssp == yyss)
                   YYABORT;
@@ -1122,7 +1163,6 @@ yyerrlab:
        {
          yydestruct ("Error: discarding", yytoken, &yylval]b4_location_if([, &yylloc])[);
          yychar = YYEMPTY;
-]b4_location_if([        *++yylerrsp = yylloc;])[
        }
     }
 
@@ -1143,13 +1183,12 @@ yyerrorlab:
      goto yyerrorlab;
 #endif
 
-  yyvsp -= yylen;
+]b4_location_if([[  yyerror_range[0] = yylsp[1-yylen];
+  yylsp -= yylen;
+  ]])[yyvsp -= yylen;
   yyssp -= yylen;
   yystate = *yyssp;
-  ]b4_location_if([yylerrsp = yylsp;
-  *++yylerrsp = yyloc;
-  yylsp -= yylen;
-  ])[goto yyerrlab1;
+  goto yyerrlab1;
 
 
 /*-------------------------------------------------------------.
@@ -1176,6 +1215,7 @@ yyerrlab1:
       if (yyssp == yyss)
        YYABORT;
 
+]b4_location_if([[      yyerror_range[0] = *yylsp;]])[
       yydestruct ("Error: popping", yystos[yystate], yyvsp]b4_location_if([, yylsp])[);
       YYPOPSTACK;
       yystate = *yyssp;
@@ -1186,8 +1226,12 @@ yyerrlab1:
     YYACCEPT;
 
   *++yyvsp = yylval;
-]b4_location_if([  YYLLOC_DEFAULT (yyloc, yylsp, yylerrsp - yylsp);
-  *++yylsp = yyloc;])[
+]b4_location_if([[
+  yyerror_range[1] = yylloc;
+  /* Using YYLLOC is tempting, but would change the location of
+     the look-ahead.  YYLOC is available though. */
+  YYLLOC_DEFAULT (yyloc, yyerror_range - 1, 2);
+  *++yylsp = yyloc;]])[
 
   /* Shift the error token. */
   YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);