Skip to content
Snippets Groups Projects
Verified Commit 031bc62f authored by Houtan Bastani's avatar Houtan Bastani
Browse files

macro processor: use filesystem after move to C++17, closes #31

ancillary to this change is the move of `paths` out of the Driver class into an argument to parsing/evaluation #32
parent d7e70a40
No related branches found
No related tags found
No related merge requests found
...@@ -52,7 +52,8 @@ void main2(stringstream &in, const string &basename, bool debug, bool clear_all, ...@@ -52,7 +52,8 @@ void main2(stringstream &in, const string &basename, bool debug, bool clear_all,
const filesystem::path &dynareroot, bool onlymodel); 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, 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, const vector<string> &path, stringstream &macro_output); bool no_line_macro, bool no_empty_line_macro, const vector<pair<string, string>> &defines,
vector<filesystem::path> &paths, stringstream &macro_output);
void void
usage() usage()
...@@ -155,7 +156,7 @@ main(int argc, char **argv) ...@@ -155,7 +156,7 @@ main(int argc, char **argv)
bool compute_xrefs = false; bool compute_xrefs = false;
bool transform_unary_ops = false; bool transform_unary_ops = false;
vector<pair<string, string>> defines; vector<pair<string, string>> defines;
vector<string> path; vector<filesystem::path> paths;
FileOutputType output_mode{FileOutputType::none}; FileOutputType output_mode{FileOutputType::none};
JsonOutputPointType json{JsonOutputPointType::nojson}; JsonOutputPointType json{JsonOutputPointType::nojson};
JsonFileOutputType json_output_mode{JsonFileOutputType::file}; JsonFileOutputType json_output_mode{JsonFileOutputType::file};
...@@ -284,7 +285,7 @@ main(int argc, char **argv) ...@@ -284,7 +285,7 @@ main(int argc, char **argv)
<< "must not be separated from -I by whitespace." << endl; << "must not be separated from -I by whitespace." << endl;
usage(); usage();
} }
path.push_back(s.substr(2)); paths.push_back(s.substr(2));
} }
else if (s.substr(0, 6) == "output") else if (s.substr(0, 6) == "output")
{ {
...@@ -409,12 +410,12 @@ main(int argc, char **argv) ...@@ -409,12 +410,12 @@ main(int argc, char **argv)
// it to paths before macroprocessing // it to paths before macroprocessing
vector<string> config_include_paths = config_file.getIncludePaths(); vector<string> config_include_paths = config_file.getIncludePaths();
for (const auto &it : config_include_paths) for (const auto &it : config_include_paths)
path.push_back(it); paths.emplace_back(it);
// Do macro processing // Do macro processing
stringstream macro_output; 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, no_line_macro, no_empty_line_macro,
defines, path, macro_output); defines, paths, macro_output);
if (only_macro) if (only_macro)
return EXIT_SUCCESS; return EXIT_SUCCESS;
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#include <sstream> #include <sstream>
#include <fstream> #include <fstream>
#include <filesystem>
#include "macro/Driver.hh" #include "macro/Driver.hh"
...@@ -28,14 +29,13 @@ bool compareNewline (int i, int j) { ...@@ -28,14 +29,13 @@ bool compareNewline (int i, int j) {
void void
main1(const string &filename, const string &basename, istream &modfile, bool debug, bool save_macro, string &save_macro_file, 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, const vector<string> &path, bool no_line_macro_arg, bool no_empty_line_macro, const vector<pair<string, string>> &defines,
stringstream &macro_output) vector<filesystem::path> &paths, stringstream &macro_output)
{ {
// Do macro processing // Do macro processing
macro::Environment env = macro::Environment(); macro::Environment env = macro::Environment();
vector<string> paths; macro::Driver m(env, no_line_macro_arg);
macro::Driver m(env, paths, no_line_macro_arg); m.parse(filename, basename, modfile, macro_output, debug, defines, paths);
m.parse(filename, basename, modfile, macro_output, debug, defines, path);
if (save_macro) if (save_macro)
{ {
if (save_macro_file.empty()) if (save_macro_file.empty())
......
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
using namespace macro; using namespace macro;
void void
Eval::interpret(ostream &output, bool no_line_macro) Eval::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
try try
{ {
...@@ -43,44 +43,37 @@ Eval::interpret(ostream &output, bool no_line_macro) ...@@ -43,44 +43,37 @@ Eval::interpret(ostream &output, bool no_line_macro)
} }
void void
Include::interpret(ostream &output, bool no_line_macro) Include::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
#ifdef _WIN32 using namespace filesystem;
string FILESEP = "\\";
#else
string FILESEP = "/";
#endif
try try
{ {
StringPtr msp = dynamic_pointer_cast<String>(expr->eval()); StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
if (!msp) if (!msp)
throw StackTrace("File name does not evaluate to a string"); throw StackTrace("File name does not evaluate to a string");
string filename = msp->to_string(); path filename = msp->to_string();
ifstream incfile(filename, ios::binary); ifstream incfile(filename, ios::binary);
if (incfile.fail()) if (incfile.fail())
{ {
ostringstream dirs; for (auto file : paths)
dirs << "." << FILESEP << endl;
for (const auto & path : paths)
{ {
string testfile = path + FILESEP + filename; file /= filename;
incfile = ifstream(testfile, ios::binary); incfile = ifstream(file, ios::binary);
if (incfile.good()) if (incfile.good())
break; break;
dirs << path << endl;
} }
if (incfile.fail()) if (incfile.fail())
error(StackTrace("@#includepath", "Could not open " + filename + {
". The following directories were searched:\n" + dirs.str(), location)); ostringstream errmsg;
errmsg << " * " << current_path().string() << endl;
for (auto & dir : paths)
errmsg << " * " << absolute(dir).string() << endl;
error(StackTrace("@#includepath", "Could not open " + filename.string() +
". The following directories were searched:\n" + errmsg.str(), location));
} }
}
string basename = filename; Driver m(env, no_line_macro);
size_t pos = basename.find_last_of('.'); m.parse(filename, filename.stem(), incfile, output, false, vector<pair<string, string>>{}, paths);
if (pos != string::npos)
basename.erase(pos);
Driver m(env, paths, no_line_macro);
m.parse(filename, basename, incfile, output, false, vector<pair<string, string>>{}, paths);
} }
catch (StackTrace &ex) catch (StackTrace &ex)
{ {
...@@ -94,13 +87,19 @@ Include::interpret(ostream &output, bool no_line_macro) ...@@ -94,13 +87,19 @@ Include::interpret(ostream &output, bool no_line_macro)
} }
void void
IncludePath::interpret(ostream &output, bool no_line_macro) IncludePath::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
using namespace filesystem;
try try
{ {
StringPtr msp = dynamic_pointer_cast<String>(expr->eval()); StringPtr msp = dynamic_pointer_cast<String>(expr->eval());
if (!msp) if (!msp)
throw StackTrace("File name does not evaluate to a string"); throw StackTrace("File name does not evaluate to a string");
path ip = static_cast<string>(*msp);
if (!is_directory(ip))
throw StackTrace(ip.string() + " does not evaluate to a valid directory");
if (!exists(ip))
warning(StackTrace("@#includepath", ip.string() + " does not exist", location));
paths.emplace_back(*msp); paths.emplace_back(*msp);
} }
catch (StackTrace &ex) catch (StackTrace &ex)
...@@ -115,7 +114,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro) ...@@ -115,7 +114,7 @@ IncludePath::interpret(ostream &output, bool no_line_macro)
} }
void void
Define::interpret(ostream &output, bool no_line_macro) Define::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
try try
{ {
...@@ -138,7 +137,7 @@ Define::interpret(ostream &output, bool no_line_macro) ...@@ -138,7 +137,7 @@ Define::interpret(ostream &output, bool no_line_macro)
} }
void void
Echo::interpret(ostream &output, bool no_line_macro) Echo::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
try try
{ {
...@@ -157,7 +156,7 @@ Echo::interpret(ostream &output, bool no_line_macro) ...@@ -157,7 +156,7 @@ Echo::interpret(ostream &output, bool no_line_macro)
} }
void void
Error::interpret(ostream &output, bool no_line_macro) Error::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
try try
{ {
...@@ -175,7 +174,7 @@ Error::interpret(ostream &output, bool no_line_macro) ...@@ -175,7 +174,7 @@ Error::interpret(ostream &output, bool no_line_macro)
} }
void void
EchoMacroVars::interpret(ostream &output, bool no_line_macro) EchoMacroVars::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
if (save) if (save)
env.print(output, vars, location.begin.line, true); env.print(output, vars, location.begin.line, true);
...@@ -185,7 +184,7 @@ EchoMacroVars::interpret(ostream &output, bool no_line_macro) ...@@ -185,7 +184,7 @@ EchoMacroVars::interpret(ostream &output, bool no_line_macro)
} }
void void
For::interpret(ostream &output, bool no_line_macro) For::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
ArrayPtr ap; ArrayPtr ap;
try try
...@@ -234,14 +233,14 @@ For::interpret(ostream &output, bool no_line_macro) ...@@ -234,14 +233,14 @@ For::interpret(ostream &output, bool no_line_macro)
statement->printLineInfo(output, no_line_macro); statement->printLineInfo(output, no_line_macro);
printLine = false; printLine = false;
} }
statement->interpret(output, no_line_macro); statement->interpret(output, no_line_macro, paths);
} }
} }
printEndLineInfo(output, no_line_macro); printEndLineInfo(output, no_line_macro);
} }
void void
If::interpret(ostream &output, bool no_line_macro) If::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
for (auto & it : expr_and_body) for (auto & it : expr_and_body)
try try
...@@ -254,7 +253,7 @@ If::interpret(ostream &output, bool no_line_macro) ...@@ -254,7 +253,7 @@ If::interpret(ostream &output, bool no_line_macro)
"The condition must evaluate to a boolean or a double", location)); "The condition must evaluate to a boolean or a double", location));
if ((bp && *bp) || (dp && *dp)) if ((bp && *bp) || (dp && *dp))
{ {
interpretBody(it.second, output, no_line_macro); interpretBody(it.second, output, no_line_macro, paths);
break; break;
} }
} }
...@@ -271,7 +270,7 @@ If::interpret(ostream &output, bool no_line_macro) ...@@ -271,7 +270,7 @@ If::interpret(ostream &output, bool no_line_macro)
} }
void void
If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro) If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
bool printLine = !no_line_macro; bool printLine = !no_line_macro;
for (auto & statement : body) for (auto & statement : body)
...@@ -281,12 +280,12 @@ If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_lin ...@@ -281,12 +280,12 @@ If::interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_lin
statement->printLineInfo(output, no_line_macro); statement->printLineInfo(output, no_line_macro);
printLine = false; printLine = false;
} }
statement->interpret(output, no_line_macro); statement->interpret(output, no_line_macro, paths);
} }
} }
void void
Ifdef::interpret(ostream &output, bool no_line_macro) Ifdef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
for (auto & it : expr_and_body) for (auto & it : expr_and_body)
{ {
...@@ -294,7 +293,7 @@ Ifdef::interpret(ostream &output, bool no_line_macro) ...@@ -294,7 +293,7 @@ Ifdef::interpret(ostream &output, bool no_line_macro)
if (dynamic_pointer_cast<BaseType>(it.first) if (dynamic_pointer_cast<BaseType>(it.first)
|| (vp && env.isVariableDefined(vp->getName()))) || (vp && env.isVariableDefined(vp->getName())))
{ {
interpretBody(it.second, output, no_line_macro); interpretBody(it.second, output, no_line_macro, paths);
break; break;
} }
} }
...@@ -302,7 +301,7 @@ Ifdef::interpret(ostream &output, bool no_line_macro) ...@@ -302,7 +301,7 @@ Ifdef::interpret(ostream &output, bool no_line_macro)
} }
void void
Ifndef::interpret(ostream &output, bool no_line_macro) Ifndef::interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths)
{ {
for (auto & it : expr_and_body) for (auto & it : expr_and_body)
{ {
...@@ -310,7 +309,7 @@ Ifndef::interpret(ostream &output, bool no_line_macro) ...@@ -310,7 +309,7 @@ Ifndef::interpret(ostream &output, bool no_line_macro)
if (!(dynamic_pointer_cast<BaseType>(it.first) if (!(dynamic_pointer_cast<BaseType>(it.first)
|| (vp && env.isVariableDefined(vp->getName())))) || (vp && env.isVariableDefined(vp->getName()))))
{ {
interpretBody(it.second, output, no_line_macro); interpretBody(it.second, output, no_line_macro, paths);
break; break;
} }
} }
......
...@@ -22,6 +22,8 @@ ...@@ -22,6 +22,8 @@
#include "Expressions.hh" #include "Expressions.hh"
#include <filesystem>
namespace macro namespace macro
{ {
class Directive : public Node class Directive : public Node
...@@ -30,7 +32,7 @@ namespace macro ...@@ -30,7 +32,7 @@ namespace macro
public: public:
Directive(Environment &env_arg, Tokenizer::location location_arg) : Node(env_arg, move(location_arg)) { } Directive(Environment &env_arg, Tokenizer::location location_arg) : Node(env_arg, move(location_arg)) { }
// Directives can be interpreted // Directives can be interpreted
virtual void interpret(ostream &output, bool no_line_macro) = 0; virtual void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) = 0;
}; };
...@@ -44,7 +46,7 @@ namespace macro ...@@ -44,7 +46,7 @@ namespace macro
public: public:
TextNode(string text_arg, Environment &env_arg, Tokenizer::location location_arg) : TextNode(string text_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), text{move(text_arg)} { } Directive(env_arg, move(location_arg)), text{move(text_arg)} { }
inline void interpret(ostream &output, bool no_line_macro) override { output << text; } inline void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override { output << text; }
}; };
...@@ -58,7 +60,7 @@ namespace macro ...@@ -58,7 +60,7 @@ namespace macro
public: public:
Eval(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) : Eval(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -66,11 +68,10 @@ namespace macro ...@@ -66,11 +68,10 @@ namespace macro
{ {
private: private:
const ExpressionPtr expr; const ExpressionPtr expr;
vector<string> &paths;
public: public:
Include(ExpressionPtr expr_arg, Environment &env_arg, vector<string> &paths_arg, Tokenizer::location location_arg) : Include(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)}, paths{paths_arg} { } Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -78,11 +79,10 @@ namespace macro ...@@ -78,11 +79,10 @@ namespace macro
{ {
private: private:
const ExpressionPtr expr; const ExpressionPtr expr;
vector<string> &paths;
public: public:
IncludePath(ExpressionPtr expr_arg, Environment &env_arg, vector<string> &paths_arg, Tokenizer::location location_arg) : IncludePath(ExpressionPtr expr_arg, Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)}, paths{paths_arg} { } Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -101,7 +101,7 @@ namespace macro ...@@ -101,7 +101,7 @@ namespace macro
ExpressionPtr value_arg, ExpressionPtr value_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), func{move(func_arg)}, value{move(value_arg)} { } Directive(env_arg, move(location_arg)), func{move(func_arg)}, value{move(value_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -113,7 +113,7 @@ namespace macro ...@@ -113,7 +113,7 @@ namespace macro
Echo(ExpressionPtr expr_arg, Echo(ExpressionPtr expr_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -125,7 +125,7 @@ namespace macro ...@@ -125,7 +125,7 @@ namespace macro
Error(ExpressionPtr expr_arg, Error(ExpressionPtr expr_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { } Directive(env_arg, move(location_arg)), expr{move(expr_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -141,7 +141,7 @@ namespace macro ...@@ -141,7 +141,7 @@ namespace macro
EchoMacroVars(bool save_arg, vector<string> vars_arg, EchoMacroVars(bool save_arg, vector<string> vars_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), save{save_arg}, vars{move(vars_arg)} { } Directive(env_arg, move(location_arg)), save{save_arg}, vars{move(vars_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -158,7 +158,7 @@ namespace macro ...@@ -158,7 +158,7 @@ namespace macro
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), index_vec{move(index_vec_arg)}, Directive(env_arg, move(location_arg)), index_vec{move(index_vec_arg)},
index_vals{move(index_vals_arg)}, statements{move(statements_arg)} { } index_vals{move(index_vals_arg)}, statements{move(statements_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -178,9 +178,9 @@ namespace macro ...@@ -178,9 +178,9 @@ namespace macro
If(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg, If(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
Directive(env_arg, move(location_arg)), expr_and_body{move(expr_and_body_arg)} { } Directive(env_arg, move(location_arg)), expr_and_body{move(expr_and_body_arg)} { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
protected: protected:
void interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro); void interpretBody(const vector<DirectivePtr> &body, ostream &output, bool no_line_macro, vector<filesystem::path> &paths);
}; };
...@@ -190,7 +190,7 @@ namespace macro ...@@ -190,7 +190,7 @@ namespace macro
Ifdef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg, Ifdef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
If(move(expr_and_body_arg), env_arg, move(location_arg)) { } If(move(expr_and_body_arg), env_arg, move(location_arg)) { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
...@@ -200,7 +200,7 @@ namespace macro ...@@ -200,7 +200,7 @@ namespace macro
Ifndef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg, Ifndef(vector<pair<ExpressionPtr, vector<DirectivePtr>>> expr_and_body_arg,
Environment &env_arg, Tokenizer::location location_arg) : Environment &env_arg, Tokenizer::location location_arg) :
If(move(expr_and_body_arg), env_arg, move(location_arg)) { } If(move(expr_and_body_arg), env_arg, move(location_arg)) { }
void interpret(ostream &output, bool no_line_macro) override; void interpret(ostream &output, bool no_line_macro, vector<filesystem::path> &paths) override;
}; };
} }
#endif #endif
...@@ -24,11 +24,10 @@ using namespace macro; ...@@ -24,11 +24,10 @@ using namespace macro;
void void
Driver::parse(const string &file_arg, const string &basename_arg, istream &modfile, Driver::parse(const string &file_arg, const string &basename_arg, istream &modfile,
ostream &output, bool debug, const vector<pair<string, string>> &defines, ostream &output, bool debug, const vector<pair<string, string>> &defines,
vector<string> paths_arg) vector<filesystem::path> &paths)
{ {
file = file_arg; file = file_arg;
basename = basename_arg; basename = basename_arg;
paths = move(paths_arg);
if (!defines.empty()) if (!defines.empty())
{ {
...@@ -47,7 +46,7 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi ...@@ -47,7 +46,7 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi
else else
command_line_defines_with_endl << "@#define " << define.first << " = \"" << define.second << "\"" << endl; command_line_defines_with_endl << "@#define " << define.first << " = \"" << define.second << "\"" << endl;
} }
Driver m(env, paths, true); Driver m(env, true);
istream is(command_line_defines_with_endl.rdbuf()); istream is(command_line_defines_with_endl.rdbuf());
m.parse("command_line_defines", "command_line_defines", is, output, debug, vector<pair<string, string>>{}, paths); m.parse("command_line_defines", "command_line_defines", is, output, debug, vector<pair<string, string>>{}, paths);
} }
...@@ -72,7 +71,7 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi ...@@ -72,7 +71,7 @@ Driver::parse(const string &file_arg, const string &basename_arg, istream &modfi
statement->printLineInfo(output, no_line_macro); statement->printLineInfo(output, no_line_macro);
printLine = false; printLine = false;
} }
statement->interpret(output, no_line_macro); statement->interpret(output, no_line_macro, paths);
} }
} }
......
...@@ -29,6 +29,7 @@ ...@@ -29,6 +29,7 @@
#include "Expressions.hh" #include "Expressions.hh"
#include <stack> #include <stack>
#include <filesystem>
// Declare TokenizerFlexLexer class // Declare TokenizerFlexLexer class
#ifndef __FLEX_LEXER_H #ifndef __FLEX_LEXER_H
...@@ -63,15 +64,13 @@ namespace macro ...@@ -63,15 +64,13 @@ namespace macro
{ {
public: public:
Environment &env; Environment &env;
//! The paths to search when looking for .mod files
vector<string> &paths;
private: private:
bool no_line_macro; bool no_line_macro;
vector<DirectivePtr> statements; vector<DirectivePtr> statements;
stack<vector<DirectivePtr>> directive_stack; stack<vector<DirectivePtr>> directive_stack;
public: public:
Driver(Environment &env_arg, vector<string> &paths_arg, bool no_line_macro_arg) : Driver(Environment &env_arg, bool no_line_macro_arg) :
env{env_arg}, paths{paths_arg}, no_line_macro(no_line_macro_arg) { } env{env_arg}, no_line_macro(no_line_macro_arg) { }
Driver(const Driver &) = delete; Driver(const Driver &) = delete;
Driver(Driver &&) = delete; Driver(Driver &&) = delete;
Driver & operator=(const Driver &) = delete; Driver & operator=(const Driver &) = delete;
...@@ -90,7 +89,7 @@ namespace macro ...@@ -90,7 +89,7 @@ namespace macro
//! Starts parsing a file, returns output in out //! Starts parsing a file, returns output in out
void parse(const string &file_arg, const string &basename_arg, istream &modfile, void parse(const string &file_arg, const string &basename_arg, istream &modfile,
ostream &output, bool debug, const vector<pair<string, string>> &defines, ostream &output, bool debug, const vector<pair<string, string>> &defines,
vector<string> paths_arg); vector<filesystem::path> &paths_arg);
//! Name of main file being parsed //! Name of main file being parsed
string file; string file;
......
...@@ -94,6 +94,10 @@ namespace macro ...@@ -94,6 +94,10 @@ namespace macro
cerr << endl << "Macro-processing error: backtrace..." << endl << e.trace(); cerr << endl << "Macro-processing error: backtrace..." << endl << e.trace();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
inline void warning(const StackTrace &e) const noexcept
{
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, bool no_line_macro) const noexcept
{ {
if (!no_line_macro) if (!no_line_macro)
......
...@@ -126,9 +126,9 @@ directive : directive_one_line EOL ...@@ -126,9 +126,9 @@ directive : directive_one_line EOL
; ;
directive_one_line : INCLUDE expr directive_one_line : INCLUDE expr
{ $$ = make_shared<Include>($2, driver.env, driver.paths, @$); } { $$ = make_shared<Include>($2, driver.env, @$); }
| INCLUDEPATH expr | INCLUDEPATH expr
{ $$ = make_shared<IncludePath>($2, driver.env, driver.paths, @$); } { $$ = make_shared<IncludePath>($2, driver.env, @$); }
| DEFINE symbol EQUAL expr | DEFINE symbol EQUAL expr
{ $$ = make_shared<Define>($2, $4, driver.env, @$); } { $$ = make_shared<Define>($2, $4, driver.env, @$); }
| DEFINE function EQUAL expr | DEFINE function EQUAL expr
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment