Debug flag added
This commit is contained in:
parent
4eb9451235
commit
dd895152f7
|
@ -4,7 +4,7 @@ _Parsodus_completion ()
|
||||||
langs='c++ cpp cxx'
|
langs='c++ cpp cxx'
|
||||||
|
|
||||||
local opts
|
local opts
|
||||||
opts='-h --help --version -d --outputdir -l --language -n --name'
|
opts='-h --help --version -d --outputdir -l --language -n --name --debug'
|
||||||
# local prev
|
# local prev
|
||||||
COMPREPLY=()
|
COMPREPLY=()
|
||||||
local cur
|
local cur
|
||||||
|
|
|
@ -23,7 +23,7 @@ namespace pds {
|
||||||
* @param language The language to generate output for (backends is queried for this language)
|
* @param language The language to generate output for (backends is queried for this language)
|
||||||
* @param parsername The name to give to the generated parser, this gets cleaned to only contains alphanumeric chars or underscore and start with a non-digit (AKA a valid identifier)
|
* @param parsername The name to give to the generated parser, this gets cleaned to only contains alphanumeric chars or underscore and start with a non-digit (AKA a valid identifier)
|
||||||
*/
|
*/
|
||||||
Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername);
|
Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername, bool debug=true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Destructor
|
* Destructor
|
||||||
|
@ -43,6 +43,7 @@ namespace pds {
|
||||||
std::string m_outputdir;
|
std::string m_outputdir;
|
||||||
std::string m_language;
|
std::string m_language;
|
||||||
std::string m_parsername;
|
std::string m_parsername;
|
||||||
|
bool m_debug;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -1,12 +1,21 @@
|
||||||
#include "Parsodus/driver.h"
|
#include "Parsodus/driver.h"
|
||||||
#include "Parsodus/inputparser.h"
|
#include "Parsodus/inputparser.h"
|
||||||
#include "Parsodus/config.h"
|
#include "Parsodus/config.h"
|
||||||
|
|
||||||
|
#include "g3log/g3log.hpp"
|
||||||
|
#include "g3log/logworker.hpp"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
|
|
||||||
|
|
||||||
#if defined(_WIN32)
|
#if defined(_WIN32)
|
||||||
|
#include <io.h>
|
||||||
|
#define isatty(x) _isatty(x)
|
||||||
|
#define fileno(x) _fileno(x)
|
||||||
#include <direct.h>
|
#include <direct.h>
|
||||||
|
#else
|
||||||
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
@ -21,35 +30,71 @@ namespace {
|
||||||
}
|
}
|
||||||
return s;
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* G3Log sink
|
||||||
|
*/
|
||||||
|
class Sink {
|
||||||
|
public:
|
||||||
|
Sink(std::unique_ptr<std::ostream>&& infoOut) : m_infoOut(std::move(infoOut)) {}
|
||||||
|
void ReceiveLogMessage(g3::LogMessageMover logEntry) {
|
||||||
|
auto level = logEntry.get().level();
|
||||||
|
|
||||||
|
std::string prefix = "";
|
||||||
|
std::string suffix = "";
|
||||||
|
|
||||||
|
if (isatty(fileno(stdin))) {
|
||||||
|
prefix = "\033[38;5;202;1m";
|
||||||
|
suffix = "\033[0m";
|
||||||
|
}
|
||||||
|
|
||||||
|
if (level == WARNING.text) {
|
||||||
|
std::cerr << prefix << logEntry.get().message() << suffix;
|
||||||
|
} else if (level == INFO.text || level == DEBUG.text) {
|
||||||
|
if (m_infoOut)
|
||||||
|
*m_infoOut << logEntry.get().message();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
private:
|
||||||
|
std::unique_ptr<std::ostream> m_infoOut;
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
namespace pds {
|
namespace pds {
|
||||||
|
|
||||||
Driver::Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername):
|
Driver::Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername, bool debug):
|
||||||
m_backends(std::move(backends)), m_inputfile(inputfile), m_outputdir(outputdir), m_language(language), m_parsername(clean(parsername)) {
|
m_backends(std::move(backends)), m_inputfile(inputfile), m_outputdir(outputdir), m_language(language), m_parsername(clean(parsername)), m_debug(debug) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Driver::~Driver(){}
|
Driver::~Driver(){}
|
||||||
|
|
||||||
int Driver::run() {
|
int Driver::run() {
|
||||||
|
struct stat sb;
|
||||||
|
if(stat(m_outputdir.c_str(), &sb) != 0) {
|
||||||
|
int status;
|
||||||
|
#if defined(_WIN32)
|
||||||
|
status = _mkdir(m_outputdir.c_str());
|
||||||
|
#else
|
||||||
|
status = mkdir(m_outputdir.c_str(), 0755);
|
||||||
|
#endif
|
||||||
|
if(status !=0 && errno != EEXIST){
|
||||||
|
throw DriverException("The folder " + m_outputdir + " does not exist and we're unable to create it.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
std::unique_ptr<g3::LogWorker> logworker{ g3::LogWorker::createLogWorker() };
|
||||||
|
if (m_debug)
|
||||||
|
logworker->addSink(std::make_unique<Sink>(std::make_unique<std::ofstream>(m_outputdir + "/debug.log")), &Sink::ReceiveLogMessage);
|
||||||
|
else
|
||||||
|
logworker->addSink(std::make_unique<Sink>(nullptr), &Sink::ReceiveLogMessage);
|
||||||
|
g3::initializeLogging(logworker.get());
|
||||||
|
|
||||||
if (!m_parsername.length()) throw DriverException("no valid parser name possible");
|
if (!m_parsername.length()) throw DriverException("no valid parser name possible");
|
||||||
Config config = InputParser::parseInput(m_inputfile);
|
Config config = InputParser::parseInput(m_inputfile);
|
||||||
Backend* back = m_backends->findBackend(m_language, config.parserType);
|
Backend* back = m_backends->findBackend(m_language, config.parserType);
|
||||||
if (!back) throw DriverException("Could not find a valid backend for language " + m_language + " and parser type: " + config.parserType);
|
if (!back) throw DriverException("Could not find a valid backend for language " + m_language + " and parser type: " + config.parserType);
|
||||||
back->generateParser([this](std::string filename) -> std::unique_ptr<std::ostream> {
|
back->generateParser([this](std::string filename) -> std::unique_ptr<std::ostream> {
|
||||||
struct stat sb;
|
|
||||||
if(stat(m_outputdir.c_str(), &sb) != 0) {
|
|
||||||
int status;
|
|
||||||
#if defined(_WIN32)
|
|
||||||
status = _mkdir(m_outputdir.c_str());
|
|
||||||
#else
|
|
||||||
status = mkdir(m_outputdir.c_str(), 0755);
|
|
||||||
#endif
|
|
||||||
if(status !=0 && errno != EEXIST){
|
|
||||||
throw DriverException("The folder " + m_outputdir + " does not exist and we're unable to create it.");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return std::unique_ptr<std::ostream>(new std::ofstream(m_outputdir + "/" + filename));
|
return std::unique_ptr<std::ostream>(new std::ofstream(m_outputdir + "/" + filename));
|
||||||
}, m_parsername, config);
|
}, m_parsername, config);
|
||||||
|
|
||||||
|
|
|
@ -10,12 +10,13 @@
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
|
|
||||||
/* Set and parse command line arguments */
|
/* Set and parse command line arguments */
|
||||||
optparse::OptionParser parser = optparse::OptionParser().description("Parsodus").usage("Parsodus [-d <outputdir>] [-l <language>] [-n <parsername>] <inputfile.pds>");
|
optparse::OptionParser parser = optparse::OptionParser().description("Parsodus").usage("Parsodus [-d <outputdir>] [-l <language>] [-n <parsername>] [--debug] <inputfile.pds>");
|
||||||
parser.add_help_option(true);
|
parser.add_help_option(true);
|
||||||
parser.version("%prog 1.0");
|
parser.version("%prog 1.0");
|
||||||
parser.add_option("-d", "--outputdir").dest("outputdir").help("Output the generated files to this directory\n[default: .]").metavar("<directory>").set_default(".");
|
parser.add_option("-d", "--outputdir").dest("outputdir").help("Output the generated files to this directory\n[default: .]").metavar("<directory>").set_default(".");
|
||||||
parser.add_option("-l", "--lang", "--language").dest("language").help("The programming language to generate source files for\n[default: c++]").metavar("<language>").set_default("c++");
|
parser.add_option("-l", "--lang", "--language").dest("language").help("The programming language to generate source files for\n[default: c++]").metavar("<language>").set_default("c++");
|
||||||
parser.add_option("-n", "--name").dest("parsername").help("Use this name for the generated parser, the default is based on the input file name").metavar("<parsername>");
|
parser.add_option("-n", "--name").dest("parsername").help("Use this name for the generated parser, the default is based on the input file name").metavar("<parsername>");
|
||||||
|
parser.add_option("--debug").dest("debug").action("store_true").set_default(false).help("Output debug logging");
|
||||||
optparse::Values options = parser.parse_args(argc, argv);
|
optparse::Values options = parser.parse_args(argc, argv);
|
||||||
std::vector<std::string> args = parser.args();
|
std::vector<std::string> args = parser.args();
|
||||||
|
|
||||||
|
@ -68,7 +69,7 @@ int main(int argc, char** argv) {
|
||||||
|
|
||||||
/* Create parserfile, if possible */
|
/* Create parserfile, if possible */
|
||||||
try {
|
try {
|
||||||
pds::Driver driver(std::move(backendManager), infile, options["outputdir"], options["language"], parsername);
|
pds::Driver driver(std::move(backendManager), infile, options["outputdir"], options["language"], parsername, options.get("debug"));
|
||||||
return driver.run();
|
return driver.run();
|
||||||
} catch (std::exception &err) {
|
} catch (std::exception &err) {
|
||||||
std::cout << err.what() << std::endl;
|
std::cout << err.what() << std::endl;
|
||||||
|
|
Loading…
Reference in New Issue