From 804d31e8af36fdcb7fdf9de5ac5cc3fbe3c56dc7 Mon Sep 17 00:00:00 2001
From: Houtan Bastani <houtan@dynare.org>
Date: Wed, 14 Sep 2016 14:03:03 +0200
Subject: [PATCH] preprocessor: allow shock group names to contain spaces.
 closes #1280

---
 doc/dynare.texi             |  5 +++--
 preprocessor/DynareBison.yy |  5 +++--
 preprocessor/Shocks.cc      | 30 +++++++++++++++++++++++-------
 3 files changed, 29 insertions(+), 11 deletions(-)

diff --git a/doc/dynare.texi b/doc/dynare.texi
index 40aba5129b..9ddb19c4c8 100644
--- a/doc/dynare.texi
+++ b/doc/dynare.texi
@@ -6867,7 +6867,8 @@ of the shock groups is written in a block delimited by @code{shock_groups} and
 Each line defines a group of shock as a list of exogenous variables:
 
 @example
-SHOCK_GROUP_NAME =  VARIABLE_1 [[,] VARIABLE_2 [,]@dots{}];
+SHOCK_GROUP_NAME   = VARIABLE_1 [[,] VARIABLE_2 [,]@dots{}];
+`SHOCK GROUP NAME' = VARIABLE_1 [[,] VARIABLE_2 [,]@dots{}];
 @end example
 
 @optionshead
@@ -6891,7 +6892,7 @@ varexo e_a, e_b, e_c, e_d;
 
 shock_groups(name=group1);
 supply = e_a, e_b;
-demand = e_c, e_d;
+`aggregate demand' = e_c, e_d;
 end;
 
 shocks_decomposition(use_shock_groups=group1);
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index c6dd2fe7b9..7f22d45d35 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -2685,9 +2685,10 @@ shock_group_list : shock_group_list shock_group_element
                  | shock_group_element
                  ;
 
-shock_group_element : symbol EQUAL shock_name_list ';' {driver.add_shock_group($1);}
+shock_group_element : symbol EQUAL shock_name_list ';' { driver.add_shock_group($1); }
+                    | QUOTED_STRING EQUAL shock_name_list ';' { driver.add_shock_group($1); }
                     ;
-                    
+
 shock_name_list : shock_name_list COMMA symbol {driver.add_shock_group_element($3);}
                 | shock_name_list symbol {driver.add_shock_group_element($2);}
                 | symbol {driver.add_shock_group_element($1);}
diff --git a/preprocessor/Shocks.cc b/preprocessor/Shocks.cc
index 1216a40b46..af40262414 100644
--- a/preprocessor/Shocks.cc
+++ b/preprocessor/Shocks.cc
@@ -463,13 +463,29 @@ ShockGroupsStatement::ShockGroupsStatement(const group_t &shock_groups_arg, cons
 void
 ShockGroupsStatement::writeOutput(ostream &output, const string &basename, bool minimal_workspace) const
 {
-  for (vector<Group>::const_iterator it = shock_groups.begin(); it != shock_groups.end(); it++)
+  int i = 1;
+  bool unique_label = true;
+  for (vector<Group>::const_iterator it = shock_groups.begin(); it != shock_groups.end(); it++, unique_label=true)
     {
-      output << "M_.shock_groups." << name
-             << "." << it->name << " = {";
-      for ( vector<string>::const_iterator it1 = it->list.begin(); it1 != it->list.end(); it1++)
-        output << " '" << *it1 << "'";
-      output << "};" << endl;
+      for (vector<Group>::const_iterator it1 = it+1; it1 != shock_groups.end(); it1++)
+        if (it->name == it1->name)
+          {
+            unique_label = false;
+            cerr << "Warning: shock group label '" << it->name << "' has been reused. "
+                 << "Only using the last definition." << endl;
+            break;
+          }
+
+      if (unique_label)
+        {
+          output << "M_.shock_groups." << name
+                 << ".group" << i << ".label = '" << it->name << "';" << endl
+                 << "M_.shock_groups." << name
+                 << ".group" << i << ".shocks = {";
+          for ( vector<string>::const_iterator it1 = it->list.begin(); it1 != it->list.end(); it1++)
+            output << " '" << *it1 << "'";
+          output << "};" << endl;
+          i++;
+        }
     }
 }
-  
-- 
GitLab