From 54c1c0dfb6eb962342a0f920f68bb173241ae785 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien.villemot@ens.fr>
Date: Mon, 30 Aug 2010 14:37:48 +0200
Subject: [PATCH] Preprocessor: fix line numbering problems due to native
 MATLAB statements (bug introduced in a0cd4b33aea0965d9baa9b4c66d0c875fa0aa7d0
 and 6582341799d35e80c59f6f3559dbd18e70443747)

---
 preprocessor/DynareFlex.ll    | 16 +++++++++++++---
 preprocessor/ParsingDriver.hh |  3 +++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/preprocessor/DynareFlex.ll b/preprocessor/DynareFlex.ll
index a9be762984..f2b1b7b970 100644
--- a/preprocessor/DynareFlex.ll
+++ b/preprocessor/DynareFlex.ll
@@ -53,7 +53,7 @@ string eofbuff;
 
 %option case-insensitive noyywrap nounput batch debug never-interactive
 
- /* NB: if new start conditions are defined, add them in the line for [\n]+ and <<EOF>>*/
+ /* NB: if new start conditions are defined, add them in the line for <<EOF>> */
 %x COMMENT
 %x DYNARE_STATEMENT
 %x DYNARE_BLOCK
@@ -65,7 +65,7 @@ string eofbuff;
 
 %{
 // Increments location counter for every token read
-#define YY_USER_ACTION yylloc->columns(yyleng);
+#define YY_USER_ACTION location_increment(yylloc, yytext);
 %}
 %%
  /* Code put at the beginning of yylex() */
@@ -88,7 +88,7 @@ string eofbuff;
 
  /* spaces, tabs and carriage returns are ignored */
 <*>[ \t\r\f]+  { yylloc->step(); }
-<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK,COMMENT,LINE1,LINE2,LINE3>[\n]+       { yylloc->lines(yyleng); yylloc->step(); }
+<INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK,COMMENT,LINE1,LINE2,LINE3>[\n]+       { yylloc->step(); }
 
  /* Comments */
 <INITIAL,DYNARE_STATEMENT,DYNARE_BLOCK>["%"].*
@@ -620,6 +620,16 @@ DynareFlex::DynareFlex(istream* in, ostream* out)
 {
 }
 
+void
+DynareFlex::location_increment(Dynare::parser::location_type *yylloc, const char *yytext)
+{
+  while (*yytext != 0)
+    if (*yytext++ == '\n')
+      yylloc->lines(1);
+    else
+      yylloc->columns(1);
+}
+
 /* This implementation of DynareFlexLexer::yylex() is required to fill the
  * vtable of the class DynareFlexLexer. We define the scanner's main yylex
  * function via YY_DECL to reside in the DynareFlex class instead. */
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index 3acb8460c7..490d2bbafe 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -68,6 +68,9 @@ public:
   //! The filename being parsed
   /*! The bison parser locations (begin and end) contain a pointer to that string */
   string filename;
+
+  //! Increment the location counter given a token
+  void location_increment(Dynare::parser::location_type *yylloc, const char *yytext);
 };
 
 //! Drives the scanning and parsing of the .mod file, and constructs its abstract representation
-- 
GitLab