From 17c5e1935d825bb815059a0bfcf67ee71a98daee Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?S=C3=A9bastien=20Villemot?= <sebastien@dynare.org>
Date: Mon, 21 Feb 2022 16:43:18 +0100
Subject: [PATCH] =?UTF-8?q?Fix=20error=20message=20when=20=E2=80=9Cramsey?=
 =?UTF-8?q?=5Fpolicy=E2=80=9D=20follows=20a=20=E2=80=9Cramsey=5Fmodel?=
 =?UTF-8?q?=E2=80=9D=20statement?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

By the way, also fix error message when there are several “ramsey_model” or
“ramsey_policy” statements.

Closes: #90
---
 src/ParsingDriver.cc | 14 ++++++++++++++
 src/ParsingDriver.hh |  5 +++++
 2 files changed, 19 insertions(+)

diff --git a/src/ParsingDriver.cc b/src/ParsingDriver.cc
index 9b9741df..89d32bc8 100644
--- a/src/ParsingDriver.cc
+++ b/src/ParsingDriver.cc
@@ -2054,6 +2054,13 @@ ParsingDriver::end_planner_objective(expr_t expr)
 void
 ParsingDriver::ramsey_model()
 {
+  // Some checks to ensure correct error messages (see #90)
+  if (ramsey_policy_seen)
+    error("A 'ramsey_model' statement cannot follow a 'ramsey_policy' statement.");
+  if (ramsey_model_seen)
+    error("Several 'ramsey_model' statements cannot appear in a given .mod file.");
+  ramsey_model_seen = true;
+
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
     {
       if (!planner_discount)
@@ -2081,6 +2088,13 @@ ParsingDriver::ramsey_policy()
 {
   warning("The 'ramsey_policy' statement is deprecated. Please use 'ramsey_model', 'stoch_simul', and 'evaluate_planner_objective' instead.");
 
+  // Some checks to ensure correct error messages (see #90)
+  if (ramsey_model_seen)
+    error("A 'ramsey_policy' statement cannot follow a 'ramsey_model' statement.");
+  if (ramsey_policy_seen)
+    error("Several 'ramsey_policy' statements cannot appear in a given .mod file.");
+  ramsey_policy_seen = true;
+
   if (!mod_file->symbol_table.exists("optimal_policy_discount_factor"))
     {
       if (!planner_discount)
diff --git a/src/ParsingDriver.hh b/src/ParsingDriver.hh
index 78063f70..2d7f321e 100644
--- a/src/ParsingDriver.hh
+++ b/src/ParsingDriver.hh
@@ -274,6 +274,11 @@ private:
   //! True when parsing pac_model statement
   bool parsing_pac_model{false};
 
+  //! True if a ramsey_model statement has already been seen
+  bool ramsey_model_seen{false};
+  //! True if a ramsey_policy statement has already been seen
+  bool ramsey_policy_seen{false};
+
 public:
   ParsingDriver(WarningConsolidation &warnings_arg, bool nostrict_arg) :
     warnings{warnings_arg}, nostrict{nostrict_arg}
-- 
GitLab