From 9a1339ad11f1dcb49ecf94f484763c1a12175364 Mon Sep 17 00:00:00 2001
From: sebastien <sebastien@ac1d8469-bf42-47a9-8791-bf33cf982152>
Date: Wed, 2 Sep 2009 14:37:59 +0000
Subject: [PATCH] Added support for equation tags (thanks to Pablo for
 providing a patch)

git-svn-id: https://www.dynare.org/svn/dynare/trunk@2880 ac1d8469-bf42-47a9-8791-bf33cf982152
---
 preprocessor/DynamicModel.cc  |  8 ++++++++
 preprocessor/DynareBison.yy   | 12 ++++++++++++
 preprocessor/ModelTree.cc     |  6 ++++++
 preprocessor/ModelTree.hh     |  6 +++++-
 preprocessor/ParsingDriver.cc |  9 +++++++++
 preprocessor/ParsingDriver.hh |  2 ++
 6 files changed, 42 insertions(+), 1 deletion(-)

diff --git a/preprocessor/DynamicModel.cc b/preprocessor/DynamicModel.cc
index 49e8e82f95..4241aab323 100644
--- a/preprocessor/DynamicModel.cc
+++ b/preprocessor/DynamicModel.cc
@@ -1923,6 +1923,14 @@ DynamicModel::writeOutput(ostream &output, const string &basename, bool block, b
       }
     output << "]';" << endl;
 
+    // Write equation tags
+    output << "M_.equations_tags = {" << endl;
+    for (int i = 0; i < equation_tags.size(); i++)
+      output << "  " << equation_tags[i].first + 1 << " , '"
+             << equation_tags[i].second.first << "' , '"
+             << equation_tags[i].second.second << "' ;" << endl;
+    output << "};" << endl;
+
     //In case of sparse model, writes the block structure of the model
     if (block)
       {
diff --git a/preprocessor/DynareBison.yy b/preprocessor/DynareBison.yy
index 827d961b5f..7df8142b77 100644
--- a/preprocessor/DynareBison.yy
+++ b/preprocessor/DynareBison.yy
@@ -474,6 +474,18 @@ equation : hand_side EQUAL hand_side ';'
            { $$ = driver.add_model_equal($1, $3); }
          | hand_side ';'
            { $$ = driver.add_model_equal_with_zero_rhs($1); }
+         | '[' tags_list ']' hand_side EQUAL hand_side ';'
+           { $$ = driver.add_model_equal($4, $6); }
+         | '[' tags_list ']' hand_side ';'
+           { $$ = driver.add_model_equal_with_zero_rhs($4); }
+         ;
+
+tags_list : tags_list COMMA tag_pair
+          | tag_pair
+          ;
+
+tag_pair : NAME EQUAL QUOTED_STRING
+           { driver.add_equation_tags($1, $3); }
          ;
 
 hand_side : '(' hand_side ')'
diff --git a/preprocessor/ModelTree.cc b/preprocessor/ModelTree.cc
index e7dd0e8af5..d2a88e30b2 100644
--- a/preprocessor/ModelTree.cc
+++ b/preprocessor/ModelTree.cc
@@ -279,3 +279,9 @@ ModelTree::addEquation(NodeID eq)
 
   equations.push_back(beq);
 }
+
+void
+ModelTree::addEquationTags(int i, const string &key, const string &value)
+{
+  equation_tags.push_back(make_pair(i, make_pair(key, value)));
+}
diff --git a/preprocessor/ModelTree.hh b/preprocessor/ModelTree.hh
index 4293104da8..29e10f765f 100644
--- a/preprocessor/ModelTree.hh
+++ b/preprocessor/ModelTree.hh
@@ -36,6 +36,9 @@ protected:
   //! Stores declared equations
   vector<BinaryOpNode *> equations;
 
+  //! Stores equation tags
+  vector<pair<int, pair<string, string> > > equation_tags;
+
   //! Number of non-zero derivatives
   int NNZDerivatives[3];
 
@@ -47,7 +50,6 @@ protected:
   */
   first_derivatives_type first_derivatives;
 
-
   typedef map<pair<int, pair<int, int> >, NodeID> second_derivatives_type;
   //! Second order derivatives
   /*! First index is equation number, second and third are variables w.r. to which is computed the derivative.
@@ -98,6 +100,8 @@ public:
   ModelTree(SymbolTable &symbol_table_arg, NumericalConstants &num_constants);
   //! Declare a node as an equation of the model
   void addEquation(NodeID eq);
+  //! Adds tags to equation number i
+  void addEquationTags(int i, const string &key, const string &value);
   //! Returns the number of equations in the model
   int equation_number() const;
 };
diff --git a/preprocessor/ParsingDriver.cc b/preprocessor/ParsingDriver.cc
index 4fcce29162..3c32058bc9 100644
--- a/preprocessor/ParsingDriver.cc
+++ b/preprocessor/ParsingDriver.cc
@@ -144,6 +144,15 @@ ParsingDriver::declare_parameter(string *name, string *tex_name)
   declare_symbol(name, eParameter, tex_name);
 }
 
+void
+ParsingDriver::add_equation_tags(string *key, string *value)
+{
+  int n = model_tree->equation_number();
+  model_tree->addEquationTags(n, *key, *value);
+  delete key;
+  delete value;
+}
+
 NodeID
 ParsingDriver::add_constant(string *constant)
 {
diff --git a/preprocessor/ParsingDriver.hh b/preprocessor/ParsingDriver.hh
index 8d6b1f5847..8d4faefd65 100644
--- a/preprocessor/ParsingDriver.hh
+++ b/preprocessor/ParsingDriver.hh
@@ -193,6 +193,8 @@ public:
   void declare_and_init_model_local_variable(string *name, NodeID rhs);
   //! Changes type of a symbol
   void change_type(SymbolType new_type, vector<string *> *var_list);
+  //! Adds a list of tags for the current equation
+  void add_equation_tags(string *key, string *value); 
   //! Adds a constant to DataTree
   NodeID add_constant(string *constant);
   //! Adds a NaN constant to DataTree
-- 
GitLab