From 487c372900e66adf37f801ca3ac0bb271ccb0030 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Fri, 19 Nov 2021 14:32:55 +0100
Subject: [PATCH] Macroprocessor: fix newlines in output when under Windows and
 input uses CR+LF convention
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

If the input .mod file uses CR+LF convention, and if the user is under Windows,
then the output of the macroprocessor (as given by the “savemacro” option) had
incorrect end of lines: those would be CR+CR+LF.

The reason is that some TextNode(s) internally created by the macroprocessor
would themselves contain CR+LF sequences, which would then be transformed into
CR+CR+LF in the output (because MinGW transforms LF into CR+LF in output
streams).

The fix consists in changing the nature of the EOL token: the parsed text is no
longer attached to it, so that the Bison file now systematically turns it into
a LF inside TextNode(s).

Closes: #80
---
 src/macro/Parser.yy    | 7 ++++---
 src/macro/Tokenizer.ll | 6 ++----
 2 files changed, 6 insertions(+), 7 deletions(-)

diff --git a/src/macro/Parser.yy b/src/macro/Parser.yy
index 92f6a04d..bf029618 100644
--- a/src/macro/Parser.yy
+++ b/src/macro/Parser.yy
@@ -1,6 +1,6 @@
 // -*- C++ -*-
 /*
- * Copyright © 2019-2020 Dynare Team
+ * Copyright © 2019-2021 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -89,7 +89,8 @@ using namespace macro;
 %precedence CAST
 %nonassoc POWER
 
-%token <string> NAME TEXT QUOTED_STRING NUMBER EOL
+%token EOL
+%token <string> NAME TEXT QUOTED_STRING NUMBER
 
 %type <DirectivePtr> statement
 %type <DirectivePtr> directive directive_one_line directive_multiline for if ifdef ifndef text eval
@@ -284,7 +285,7 @@ else : else_begin EOL
 text : TEXT
        { $$ = make_shared<TextNode>($1, @$); }
      | EOL
-       { $$ = make_shared<TextNode>($1, @$); }
+       { $$ = make_shared<TextNode>("\n", @$); }
      ;
 
 eval : BEGIN_EVAL expr END_EVAL
diff --git a/src/macro/Tokenizer.ll b/src/macro/Tokenizer.ll
index 439fcb12..c72561e4 100644
--- a/src/macro/Tokenizer.ll
+++ b/src/macro/Tokenizer.ll
@@ -1,6 +1,6 @@
 /* -*- C++ -*- */
 /*
- * Copyright © 2019 Dynare Team
+ * Copyright © 2019-2021 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -180,7 +180,6 @@ CONT \\\\{SPC}*
 
 <expr,end_line>{CONT}("//".*)?{SPC}*{EOL} { yylloc->lines(1); yylloc->step(); }
 <expr,end_line>{SPC}*("//".*)?{EOL}       {
-                                            yylval->build<string>("\n");
                                             yylloc->lines(1);
                                             BEGIN(INITIAL);
                                             return token::EOL;
@@ -188,8 +187,7 @@ CONT \\\\{SPC}*
 
 <INITIAL>^{SPC}*@#{SPC}*                  { BEGIN(directive); }
 <INITIAL>@\{                              { BEGIN(eval); return token::BEGIN_EVAL; }
-<INITIAL>{SPC}*{EOL}                      {
-                                            yylval->build<string>(yytext);
+<INITIAL>{EOL}                            {
                                             yylloc->lines(1);
                                             return token::EOL;
                                           }
-- 
GitLab