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
issue#70
Houtan Bastani 2020-02-05 15:07:03 +01:00
parent c4a9f93d40
commit 1dbbd87d3d
No known key found for this signature in database
GPG Key ID: 000094FB955BE169
7 changed files with 69 additions and 74 deletions

View File

@ -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<pair<string, string>> &defines,
vector<filesystem::path> &paths, stringstream &macro_output);
bool line_macro, const vector<pair<string, string>> &defines, vector<filesystem::path> &paths, stringstream &macro_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<variable>[=<value>]] [-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=<equation_tag_list_or_file>] [include_eqs=<equation_tag_list_or_file>]"
@ -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)

View File

@ -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 <fstream>
#include <filesystem>
#include <algorithm>
#include <regex>
#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<pair<string, string>> &defines,
vector<filesystem::path> &paths, stringstream &macro_output)
bool line_macro, const vector<pair<string, string>> &defines, vector<filesystem::path> &paths, stringstream &macro_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());

View File

@ -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<filesystem::path> &paths)
Eval::interpret(ostream &output, vector<filesystem::path> &paths)
{
try
{
@ -43,7 +43,7 @@ Eval::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &p
}
void
Include::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Include::interpret(ostream &output, vector<filesystem::path> &paths)
{
using namespace filesystem;
try
@ -71,7 +71,7 @@ Include::interpret(ostream &output, bool no_line_macro, vector<filesystem::path>
+". 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<filesystem::path>
{
error(StackTrace("@#include", e.what(), location));
}
printLineInfo(output, no_line_macro);
printLineInfo(output);
}
void
IncludePath::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
IncludePath::interpret(ostream &output, vector<filesystem::path> &paths)
{
using namespace filesystem;
try
@ -117,7 +117,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro, vector<filesystem::p
}
void
Define::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Define::interpret(ostream &output, vector<filesystem::path> &paths)
{
try
{
@ -140,7 +140,7 @@ Define::interpret(ostream &output, bool no_line_macro, vector<filesystem::path>
}
void
Echo::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Echo::interpret(ostream &output, vector<filesystem::path> &paths)
{
try
{
@ -155,11 +155,11 @@ Echo::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &p
{
error(StackTrace("@#echo", e.what(), location));
}
printEndLineInfo(output, no_line_macro);
printEndLineInfo(output);
}
void
Error::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
Error::interpret(ostream &output, vector<filesystem::path> &paths)
{
try
{
@ -177,17 +177,17 @@ Error::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &
}
void
EchoMacroVars::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
EchoMacroVars::interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths)
For::interpret(ostream &output, vector<filesystem::path> &paths)
{
ArrayPtr ap;
try
@ -233,17 +233,17 @@ For::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &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<filesystem::path> &paths)
If::interpret(ostream &output, vector<filesystem::path> &paths)
{
for (const auto & [expr, body] : expr_and_body)
try
@ -256,7 +256,7 @@ If::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &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<filesystem::path> &pat
{
error(StackTrace("@#if", e.what(), location));
}
printEndLineInfo(output, no_line_macro);
printEndLineInfo(output);
}
void
If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
If::interpretBody(const vector<DirectivePtr> &body, ostream &output, vector<filesystem::path> &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<filesystem::path> &paths)
Ifdef::interpret(ostream &output, vector<filesystem::path> &paths)
{
for (const auto & [expr, body] : expr_and_body)
if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr);
dynamic_pointer_cast<BaseType>(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<filesystem::path> &paths)
Ifndef::interpret(ostream &output, vector<filesystem::path> &paths)
{
for (const auto & [expr, body] : expr_and_body)
if (VariablePtr vp = dynamic_pointer_cast<Variable>(expr);
!(dynamic_pointer_cast<BaseType>(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);
}

View File

@ -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<filesystem::path> &paths) = 0;
virtual void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override { output << text; }
inline void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &paths) override;
};
@ -142,7 +142,7 @@ namespace macro
EchoMacroVars(bool save_arg, vector<string> 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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &paths) override;
};
@ -179,9 +179,10 @@ namespace macro
If(vector<pair<ExpressionPtr, vector<DirectivePtr>>> 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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &paths) override;
protected:
void interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro, vector<filesystem::path> &paths);
void interpretBody(const vector<DirectivePtr> &body, ostream &output,
vector<filesystem::path> &paths);
};
@ -191,7 +192,7 @@ namespace macro
Ifdef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> 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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &paths) override;
};
@ -201,7 +202,7 @@ namespace macro
Ifndef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> 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<filesystem::path> &paths) override;
void interpret(ostream &output, vector<filesystem::path> &paths) override;
};
}
#endif

View File

@ -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);
}
}

View File

@ -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<DirectivePtr> statements;
stack<vector<DirectivePtr>> 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;

View File

@ -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;
}
};