From 4ad7de7bb939d11d8dd0e033d15a524557ef5916 Mon Sep 17 00:00:00 2001
From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152>
Date: Tue, 9 Dec 2008 16:13:03 +0000
Subject: [PATCH] trunk preprocessor/macro: report the location of @#for or
 @#if statement when not matched by an @#endfor or an @#endif

git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2304 ac1d8469-bf42-47a9-8791-bf33cf982152
---
 macro/MacroDriver.hh |  4 ++++
 macro/MacroFlex.ll   | 13 ++++++++++---
 2 files changed, 14 insertions(+), 3 deletions(-)

diff --git a/macro/MacroDriver.hh b/macro/MacroDriver.hh
index cb8a29e2..1b95a9f3 100644
--- a/macro/MacroDriver.hh
+++ b/macro/MacroDriver.hh
@@ -81,6 +81,8 @@ private:
   string for_body_tmp;
   //! Temporary variable used in FOR_BODY mode
   Macro::parser::location_type for_body_loc_tmp;
+  //! Temporary variable used in FOR_BODY mode. Keeps track of the location of the @#for statement, for reporting messages
+  Macro::parser::location_type for_stmt_loc_tmp;
   //! Temporary variable used in FOR_BODY mode. Keeps track of number of nested @#for/@#endfor
   int nested_for_nb;
   //! Set to true while parsing a FOR statement (only the statement, not the loop body)
@@ -92,6 +94,8 @@ private:
   string then_body_tmp;
   //! Temporary variable used in THEN_BODY mode
   Macro::parser::location_type then_body_loc_tmp;
+  //! Temporary variable used in THEN_BODY mode. Keeps track of the location of the @#if statement, for reporting messages
+  Macro::parser::location_type if_stmt_loc_tmp;
   //! Temporary variable used in ELSE_BODY mode
   string else_body_tmp;
   //! Temporary variable used in ELSE_BODY mode
diff --git a/macro/MacroFlex.ll b/macro/MacroFlex.ll
index 557b9733..291d30df 100644
--- a/macro/MacroFlex.ll
+++ b/macro/MacroFlex.ll
@@ -92,6 +92,13 @@ CONT \\\\
 
 <STMT>{CONT}{SPC}*{EOL}     { yylloc->lines(1); yylloc->step(); }
 <STMT>{EOL}                 {
+                              /* If parsing a @#for or an @#if, keep the location
+                                 for reporting message in case of error */
+                              if (reading_for_statement)
+                                for_stmt_loc_tmp = *yylloc;
+                              else if (reading_if_statement)
+                                if_stmt_loc_tmp = *yylloc;
+
                               yylloc->lines(1);
                               yylloc->step();
                               if (reading_for_statement)
@@ -180,7 +187,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <FOR_BODY>.                 { for_body_tmp.append(yytext); yylloc->step(); }
-<FOR_BODY><<EOF>>           { driver.error(*yylloc, "Unexpected end of file: @#for loop not matched by an @#endfor"); }
+<FOR_BODY><<EOF>>           { driver.error(for_stmt_loc_tmp, "@#for loop not matched by an @#endfor (unexpected end of file)"); }
 <FOR_BODY>^{SPC}*@#{SPC}*endfor{SPC}*{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -212,7 +219,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <THEN_BODY>.                { then_body_tmp.append(yytext); yylloc->step(); }
-<THEN_BODY><<EOF>>          { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
+<THEN_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
 <THEN_BODY>^{SPC}*@#{SPC}*else{SPC}*{EOL} {
                               yylloc->lines(1);
                               yylloc->step();
@@ -254,7 +261,7 @@ CONT \\\\
                               yylloc->step();
                             }
 <ELSE_BODY>.                { else_body_tmp.append(yytext); yylloc->step(); }
-<ELSE_BODY><<EOF>>          { driver.error(*yylloc, "Unexpected end of file: @#if not matched by an @#endif"); }
+<ELSE_BODY><<EOF>>          { driver.error(if_stmt_loc_tmp, "@#if not matched by an @#endif (unexpected end of file)"); }
 
 <ELSE_BODY>^{SPC}*@#{SPC}*endif{SPC}*{EOL} {
                               yylloc->lines(1);
-- 
GitLab