From 894f5cda461491973e91ec204dd654d9d6d33ac9 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Tue, 19 Apr 2022 16:40:18 +0200
Subject: [PATCH] Macro-processor: more robust handling of empty files

The support for empty files at the macro-processor level, as implemented in
1291320053888523bab9f5f471340b704be79376, was relying on
basic_streambuf::in_avail(), which does not seem to behave consistently across
platforms, and which may not be the right tool for the job.

Rather use the Bison grammar to support empty files.

Closes: #93
---
 src/macro/Driver.cc | 6 +-----
 src/macro/Parser.yy | 8 ++++++--
 2 files changed, 7 insertions(+), 7 deletions(-)

diff --git a/src/macro/Driver.cc b/src/macro/Driver.cc
index c0419e02..9fb99b26 100644
--- a/src/macro/Driver.cc
+++ b/src/macro/Driver.cc
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019-2020 Dynare Team
+ * Copyright © 2019-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -40,10 +40,6 @@ Driver::parse(const string &file_arg, const string &basename_arg, const istream
       m.parse("command_line_defines", "command_line_defines", is, debug, {}, env, paths, output);
     }
 
-  // Handle empty files
-  if (modfile.rdbuf()->in_avail() == 0)
-    return;
-
   stringstream file_with_endl;
   file_with_endl << modfile.rdbuf() << endl;
 
diff --git a/src/macro/Parser.yy b/src/macro/Parser.yy
index bf029618..be7cc71e 100644
--- a/src/macro/Parser.yy
+++ b/src/macro/Parser.yy
@@ -1,6 +1,6 @@
 // -*- C++ -*-
 /*
- * Copyright © 2019-2021 Dynare Team
+ * Copyright © 2019-2022 Dynare Team
  *
  * This file is part of Dynare.
  *
@@ -105,7 +105,11 @@ using namespace macro;
 
 %%
 
-%start statements;
+%start statements_or_empty_file;
+
+statements_or_empty_file : %empty
+                         | statements
+                         ;
 
 statements : statement
              {
-- 
GitLab