2015-08-31 14:15:29 +02:00
|
|
|
/*
|
2020-02-05 15:07:03 +01:00
|
|
|
* Copyright © 2015-2020 Dynare Team
|
2015-08-31 14:15:29 +02:00
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2021-06-09 16:52:20 +02:00
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
2015-08-31 14:15:29 +02:00
|
|
|
*/
|
|
|
|
|
2020-02-05 16:38:32 +01:00
|
|
|
#include <regex>
|
2015-08-31 14:15:29 +02:00
|
|
|
#include <sstream>
|
|
|
|
#include <fstream>
|
2019-10-03 13:02:00 +02:00
|
|
|
#include <filesystem>
|
2019-12-18 11:53:20 +01:00
|
|
|
#include <algorithm>
|
2015-08-31 14:15:29 +02:00
|
|
|
|
2019-05-30 20:40:56 +02:00
|
|
|
#include "macro/Driver.hh"
|
2015-08-31 14:15:29 +02:00
|
|
|
|
2020-02-05 16:38:32 +01:00
|
|
|
stringstream
|
|
|
|
macroExpandModFile(const string &filename, const string &basename, const istream &modfile,
|
|
|
|
bool debug, bool save_macro, string save_macro_file, bool line_macro,
|
|
|
|
const vector<pair<string, string>> &defines,
|
|
|
|
vector<filesystem::path> paths)
|
2015-08-31 14:15:29 +02:00
|
|
|
{
|
|
|
|
// Do macro processing
|
2020-02-05 16:38:32 +01:00
|
|
|
stringstream macro_output;
|
2019-05-30 20:40:56 +02:00
|
|
|
macro::Environment env = macro::Environment();
|
2020-12-07 17:28:29 +01:00
|
|
|
macro::Driver m;
|
|
|
|
m.parse(filename, basename, modfile, debug, defines, env, paths, macro_output);
|
2015-08-31 14:15:29 +02:00
|
|
|
if (save_macro)
|
|
|
|
{
|
|
|
|
if (save_macro_file.empty())
|
|
|
|
save_macro_file = basename + "-macroexp.mod";
|
2022-07-11 16:09:07 +02:00
|
|
|
ofstream macro_output_file{save_macro_file};
|
2015-08-31 14:15:29 +02:00
|
|
|
if (macro_output_file.fail())
|
|
|
|
{
|
|
|
|
cerr << "Cannot open " << save_macro_file << " for macro output" << endl;
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2017-12-07 18:47:21 +01:00
|
|
|
|
2019-12-20 16:59:30 +01:00
|
|
|
string str(macro_output.str());
|
2020-02-05 15:07:03 +01:00
|
|
|
if (!line_macro)
|
2019-04-09 15:10:47 +02:00
|
|
|
{
|
2021-11-19 16:34:29 +01:00
|
|
|
/* Remove the @#line directives.
|
|
|
|
Unfortunately GCC 11 does not yet support std::regex::multiline
|
|
|
|
(despite it being in the C++17 standard), so we are forced to use
|
|
|
|
a trick to emulate the “usual” behaviour of the caret ^;
|
|
|
|
here, the latter only matches the beginning of file.
|
|
|
|
This also means that we are forced to remove the EOL before the
|
|
|
|
@#line, and not the one after it (matching the EOL before and the
|
|
|
|
EOL after in the same regexp does not work). */
|
|
|
|
str = regex_replace(str, regex(R"((^|\r?\n)@#line.*)"), "");
|
|
|
|
/* Remove the EOLs at the beginning of the output, the first one
|
|
|
|
being a remnant of the first @#line directive. */
|
|
|
|
str = regex_replace(str, regex(R"(^(\r?\n)+)"), "");
|
|
|
|
/* Replace sequences of several newlines by a single newline (in
|
|
|
|
both LF and CR+LF conventions). */
|
|
|
|
str = regex_replace(str, regex(R"(\n{2,})"), "\n");
|
|
|
|
str = regex_replace(str, regex(R"((\r\n){2,})"), "\r\n");
|
2019-04-09 15:10:47 +02:00
|
|
|
}
|
2017-12-07 18:47:21 +01:00
|
|
|
macro_output_file << str;
|
2015-08-31 14:15:29 +02:00
|
|
|
macro_output_file.close();
|
|
|
|
}
|
2020-02-05 16:38:32 +01:00
|
|
|
return macro_output;
|
2015-08-31 14:15:29 +02:00
|
|
|
}
|