From 1dbbd87d3d4a44b2762e4779ccb4173b6ae9088f Mon Sep 17 00:00:00 2001 From: Houtan Bastani Date: Wed, 5 Feb 2020 15:07:03 +0100 Subject: [PATCH] Remove options `noemptylinemacro` and `nolinemacro`; add option `linemacro` This only concerns the situation when `savemacro` is also passed. When `linemacro` is passed, the macro expanded .mod file is the same as before When `linemacro` is not passed, the macro expanded .mod file is equivalent to what it was before when both `noemptylinemacro` and `nolinemacro` were passed. closes #44 closes #45 --- src/DynareMain.cc | 16 +++++------ src/DynareMain1.cc | 11 ++++---- src/macro/Directives.cc | 58 ++++++++++++++++++++-------------------- src/macro/Directives.hh | 31 ++++++++++----------- src/macro/Driver.cc | 6 ++--- src/macro/Driver.hh | 7 +++-- src/macro/Expressions.hh | 14 +++++----- 7 files changed, 69 insertions(+), 74 deletions(-) diff --git a/src/DynareMain.cc b/src/DynareMain.cc index 41c9c7a0..42506dac 100644 --- a/src/DynareMain.cc +++ b/src/DynareMain.cc @@ -52,13 +52,12 @@ void main2(stringstream &in, const string &basename, bool debug, bool clear_all, const filesystem::path &dynareroot, bool onlymodel); void main1(const string &filename, const string &basename, istream &modfile, bool debug, bool save_macro, string &save_macro_file, - bool no_line_macro, bool no_empty_line_macro, const vector> &defines, - vector &paths, stringstream ¯o_output); + bool line_macro, const vector> &defines, vector &paths, stringstream ¯o_output); void usage() { - cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [nolinemacro] [noemptylinemacro] [notmpterms] [nolog] [warn_uninit]" + cerr << "Dynare usage: dynare mod_file [debug] [noclearall] [onlyclearglobals] [savemacro[=macro_file]] [onlymacro] [linemacro] [notmpterms] [nolog] [warn_uninit]" << " [console] [nograph] [nointeractive] [parallel[=cluster_name]] [conffile=parallel_config_path_and_filename] [parallel_slave_open_mode] [parallel_test]" << " [-D[=]] [-I/path] [nostrict] [stochastic] [fast] [minimal_workspace] [compute_xrefs] [output=dynamic|first|second|third] [language=matlab|julia]" << " [params_derivs_order=0|1|2] [transform_unary_ops] [exclude_eqs=] [include_eqs=]" @@ -136,8 +135,7 @@ main(int argc, char **argv) bool debug = false; bool no_tmp_terms = false; bool only_macro = false; - bool no_line_macro = false; - bool no_empty_line_macro = false; + bool line_macro = false; bool no_log = false; bool no_warn = false; int params_derivs_order = 2; @@ -208,10 +206,8 @@ main(int argc, char **argv) save_macro_file = s.substr(10); } } - else if (s == "nolinemacro") - no_line_macro = true; - else if (s == "noemptylinemacro") - no_empty_line_macro = true; + else if (s == "linemacro") + line_macro = true; else if (s == "notmpterms") no_tmp_terms = true; else if (s == "nolog") @@ -433,7 +429,7 @@ main(int argc, char **argv) // Do macro processing stringstream macro_output; - main1(filename, basename, modfile, debug, save_macro, save_macro_file, no_line_macro, no_empty_line_macro, + main1(filename, basename, modfile, debug, save_macro, save_macro_file, line_macro, defines, paths, macro_output); if (only_macro) diff --git a/src/DynareMain1.cc b/src/DynareMain1.cc index 93e5c354..b8d9c8b7 100644 --- a/src/DynareMain1.cc +++ b/src/DynareMain1.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2015-2019 Dynare Team + * Copyright © 2015-2020 Dynare Team * * This file is part of Dynare. * @@ -21,17 +21,17 @@ #include #include #include +#include #include "macro/Driver.hh" void main1(const string &filename, const string &basename, istream &modfile, bool debug, bool save_macro, string &save_macro_file, - bool no_line_macro_arg, bool no_empty_line_macro, const vector> &defines, - vector &paths, stringstream ¯o_output) + bool line_macro, const vector> &defines, vector &paths, stringstream ¯o_output) { // Do macro processing macro::Environment env = macro::Environment(); - macro::Driver m(env, no_line_macro_arg); + macro::Driver m(env); m.parse(filename, basename, modfile, macro_output, debug, defines, paths); if (save_macro) { @@ -45,8 +45,9 @@ main1(const string &filename, const string &basename, istream &modfile, bool deb } string str(macro_output.str()); - if (no_empty_line_macro) + if (!line_macro) { + str = regex_replace(str, regex(R"((^|\n)\s*@#line.*)"), ""); auto compareNewline = [](char i, char j) { return i == '\n' && j == '\n'; }; str.erase(0, str.find_first_not_of('\n')); str.erase(unique(str.begin(), str.end(), compareNewline), str.end()); diff --git a/src/macro/Directives.cc b/src/macro/Directives.cc index faf8195a..e84b6421 100644 --- a/src/macro/Directives.cc +++ b/src/macro/Directives.cc @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Dynare Team + * Copyright © 2019-2020 Dynare Team * * This file is part of Dynare. * @@ -25,7 +25,7 @@ using namespace macro; void -Eval::interpret(ostream &output, bool no_line_macro, vector &paths) +Eval::interpret(ostream &output, vector &paths) { try { @@ -43,7 +43,7 @@ Eval::interpret(ostream &output, bool no_line_macro, vector &p } void -Include::interpret(ostream &output, bool no_line_macro, vector &paths) +Include::interpret(ostream &output, vector &paths) { using namespace filesystem; try @@ -71,7 +71,7 @@ Include::interpret(ostream &output, bool no_line_macro, vector +". The following directories were searched:\n" + errmsg.str(), location)); } } - Driver m(env, no_line_macro); + Driver m(env); // Calling `string()` method on filename and filename.stem() because of bug in // MinGW 8.3.0 that ignores implicit conversion to string from filename::path. // Test if bug exists when version of MinGW is upgraded on Debian runners @@ -86,11 +86,11 @@ Include::interpret(ostream &output, bool no_line_macro, vector { error(StackTrace("@#include", e.what(), location)); } - printLineInfo(output, no_line_macro); + printLineInfo(output); } void -IncludePath::interpret(ostream &output, bool no_line_macro, vector &paths) +IncludePath::interpret(ostream &output, vector &paths) { using namespace filesystem; try @@ -117,7 +117,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro, vector &paths) +Define::interpret(ostream &output, vector &paths) { try { @@ -140,7 +140,7 @@ Define::interpret(ostream &output, bool no_line_macro, vector } void -Echo::interpret(ostream &output, bool no_line_macro, vector &paths) +Echo::interpret(ostream &output, vector &paths) { try { @@ -155,11 +155,11 @@ Echo::interpret(ostream &output, bool no_line_macro, vector &p { error(StackTrace("@#echo", e.what(), location)); } - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } void -Error::interpret(ostream &output, bool no_line_macro, vector &paths) +Error::interpret(ostream &output, vector &paths) { try { @@ -177,17 +177,17 @@ Error::interpret(ostream &output, bool no_line_macro, vector & } void -EchoMacroVars::interpret(ostream &output, bool no_line_macro, vector &paths) +EchoMacroVars::interpret(ostream &output, vector &paths) { if (save) env.print(output, vars, location.begin.line, true); else env.print(cout, vars); - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } void -For::interpret(ostream &output, bool no_line_macro, vector &paths) +For::interpret(ostream &output, vector &paths) { ArrayPtr ap; try @@ -233,17 +233,17 @@ For::interpret(ostream &output, bool no_line_macro, vector &pa { if (printLine) { - statement->printLineInfo(output, no_line_macro); + statement->printLineInfo(output); printLine = false; } - statement->interpret(output, no_line_macro, paths); + statement->interpret(output, paths); } } - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } void -If::interpret(ostream &output, bool no_line_macro, vector &paths) +If::interpret(ostream &output, vector &paths) { for (const auto & [expr, body] : expr_and_body) try @@ -256,7 +256,7 @@ If::interpret(ostream &output, bool no_line_macro, vector &pat "The condition must evaluate to a boolean or a double", location)); if ((bp && *bp) || (dp && *dp)) { - interpretBody(body, output, no_line_macro, paths); + interpretBody(body, output, paths); break; } } @@ -269,48 +269,48 @@ If::interpret(ostream &output, bool no_line_macro, vector &pat { error(StackTrace("@#if", e.what(), location)); } - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } void -If::interpretBody(const vector &body, ostream &output, bool no_line_macro, vector &paths) +If::interpretBody(const vector &body, ostream &output, vector &paths) { - bool printLine = !no_line_macro; + bool printLine = true; for (const auto &statement : body) { if (printLine) { - statement->printLineInfo(output, no_line_macro); + statement->printLineInfo(output); printLine = false; } - statement->interpret(output, no_line_macro, paths); + statement->interpret(output, paths); } } void -Ifdef::interpret(ostream &output, bool no_line_macro, vector &paths) +Ifdef::interpret(ostream &output, vector &paths) { for (const auto & [expr, body] : expr_and_body) if (VariablePtr vp = dynamic_pointer_cast(expr); dynamic_pointer_cast(expr) || (vp && env.isVariableDefined(vp->getName()))) { - interpretBody(body, output, no_line_macro, paths); + interpretBody(body, output, paths); break; } - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } void -Ifndef::interpret(ostream &output, bool no_line_macro, vector &paths) +Ifndef::interpret(ostream &output, vector &paths) { for (const auto & [expr, body] : expr_and_body) if (VariablePtr vp = dynamic_pointer_cast(expr); !(dynamic_pointer_cast(expr) || (vp && env.isVariableDefined(vp->getName())))) { - interpretBody(body, output, no_line_macro, paths); + interpretBody(body, output, paths); break; } - printEndLineInfo(output, no_line_macro); + printEndLineInfo(output); } diff --git a/src/macro/Directives.hh b/src/macro/Directives.hh index 943bc6d4..db71ae3f 100644 --- a/src/macro/Directives.hh +++ b/src/macro/Directives.hh @@ -1,5 +1,5 @@ /* - * Copyright (C) 2019 Dynare Team + * Copyright (C) 2019-2020 Dynare Team * * This file is part of Dynare. * @@ -33,7 +33,7 @@ namespace macro Directive(Environment &env_arg, Tokenizer::location location_arg) : Node(env_arg, move(location_arg)) { } // Directives can be interpreted - virtual void interpret(ostream &output, bool no_line_macro, vector &paths) = 0; + virtual void interpret(ostream &output, vector &paths) = 0; }; @@ -47,7 +47,7 @@ namespace macro public: TextNode(string text_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), text{move(text_arg)} { } - inline void interpret(ostream &output, bool no_line_macro, vector &paths) override { output << text; } + inline void interpret(ostream &output, vector &paths) override { output << text; } }; @@ -61,7 +61,7 @@ namespace macro public: Eval(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -72,7 +72,7 @@ namespace macro public: Include(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -83,7 +83,7 @@ namespace macro public: IncludePath(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -102,7 +102,7 @@ namespace macro ExpressionPtr value_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), func{move(func_arg)}, value{move(value_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -114,7 +114,7 @@ namespace macro Echo(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -126,7 +126,7 @@ namespace macro Error(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -142,7 +142,7 @@ namespace macro EchoMacroVars(bool save_arg, vector vars_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), save{save_arg}, vars{move(vars_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -159,7 +159,7 @@ namespace macro Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), index_vec{move(index_vec_arg)}, index_vals{move(index_vals_arg)}, statements{move(statements_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -179,9 +179,10 @@ namespace macro If(vector>> expr_and_body_arg, Environment &env_arg, Tokenizer::location location_arg) : Directive(env_arg, move(location_arg)), expr_and_body{move(expr_and_body_arg)} { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; protected: - void interpretBody(const vector &body, ostream &output, bool no_line_macro, vector &paths); + void interpretBody(const vector &body, ostream &output, + vector &paths); }; @@ -191,7 +192,7 @@ namespace macro Ifdef(vector>> expr_and_body_arg, Environment &env_arg, Tokenizer::location location_arg) : If(move(expr_and_body_arg), env_arg, move(location_arg)) { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; @@ -201,7 +202,7 @@ namespace macro Ifndef(vector>> expr_and_body_arg, Environment &env_arg, Tokenizer::location location_arg) : If(move(expr_and_body_arg), env_arg, move(location_arg)) { } - void interpret(ostream &output, bool no_line_macro, vector &paths) override; + void interpret(ostream &output, vector &paths) override; }; } #endif diff --git a/src/macro/Driver.cc b/src/macro/Driver.cc index 76e3823c..25d12e2d 100644 --- a/src/macro/Driver.cc +++ b/src/macro/Driver.cc @@ -35,7 +35,7 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi stringstream command_line_defines_with_endl; for (const auto & [var, val] : defines) command_line_defines_with_endl << "@#define " << var << " = " << val << endl; - Driver m(env, true); + Driver m(env); istream is(command_line_defines_with_endl.rdbuf()); m.parse("command_line_defines", "command_line_defines", is, output, debug, {}, paths); } @@ -62,10 +62,10 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi { if (printLine) { - statement->printLineInfo(output, no_line_macro); + statement->printLineInfo(output); printLine = false; } - statement->interpret(output, no_line_macro, paths); + statement->interpret(output, paths); } } diff --git a/src/macro/Driver.hh b/src/macro/Driver.hh index ad85494f..180803ff 100644 --- a/src/macro/Driver.hh +++ b/src/macro/Driver.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Dynare Team + * Copyright © 2019-2020 Dynare Team * * This file is part of Dynare. * @@ -64,12 +64,11 @@ namespace macro public: Environment &env; private: - bool no_line_macro; vector statements; stack> directive_stack; public: - Driver(Environment &env_arg, bool no_line_macro_arg) : - env{env_arg}, no_line_macro(no_line_macro_arg) { } + Driver(Environment &env_arg) : + env{env_arg} { } Driver(const Driver &) = delete; Driver(Driver &&) = delete; Driver &operator=(const Driver &) = delete; diff --git a/src/macro/Expressions.hh b/src/macro/Expressions.hh index 153bc6ca..ec7c69f1 100644 --- a/src/macro/Expressions.hh +++ b/src/macro/Expressions.hh @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Dynare Team + * Copyright © 2019-2020 Dynare Team * * This file is part of Dynare. * @@ -98,16 +98,14 @@ namespace macro { cerr << endl << "Macro-processing warning: backtrace..." << endl << e.trace(); } - inline void printLineInfo(ostream &output, bool no_line_macro) const noexcept + inline void printLineInfo(ostream &output) const noexcept { - if (!no_line_macro) - output << R"(@#line ")" << *(location.begin.filename) << R"(" )" << location.begin.line << endl; + output << R"(@#line ")" << *(location.begin.filename) << R"(" )" << location.begin.line << endl; } - inline void printEndLineInfo(ostream &output, bool no_line_macro) const noexcept + inline void printEndLineInfo(ostream &output) const noexcept { - if (!no_line_macro) - // Add one to end line because we want to print the line number of the line *following* the end statement - output << R"(@#line ")" << *(location.begin.filename) << R"(" )" << location.end.line + 1 << endl; + // Add one to end line because we want to print the line number of the line *following* the end statement + output << R"(@#line ")" << *(location.begin.filename) << R"(" )" << location.end.line + 1 << endl; } };