Use the Lexesis-file in the same directory as Parsodus-file

This commit is contained in:
Thomas Avé 2017-01-28 13:16:09 +01:00
parent ecc187c51b
commit a0ab7722b6
5 changed files with 50 additions and 21 deletions

View File

@ -18,12 +18,12 @@ namespace pds {
* Constructor
*
* @param backends The backendmanager, prepared with all needed supported backends
* @param inputfile An istream which should be read to be used as token rules specifications
* @param inputfile A string containing the path of the `pds` file.
* @param outputdir A string representing the directory where generated files should be places
* @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)
*/
Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername, bool debug=true);
Driver(std::unique_ptr<BackendManager> backends, std::string inputfile, std::string outputdir, std::string language, std::string parsername, bool debug=true);
/**
* Destructor
@ -39,7 +39,7 @@ namespace pds {
private:
std::unique_ptr<BackendManager> m_backends;
std::istream& m_inputfile;
std::string m_inputfile;
std::string m_outputdir;
std::string m_language;
std::string m_parsername;

View File

@ -11,7 +11,7 @@ namespace pds {
class InputParser {
public:
static Config parseInput(std::istream& is);
static Config parseInput(std::istream& is, std::string current_dir);
};

View File

@ -10,12 +10,20 @@
#include <sys/stat.h>
#if defined(_WIN32)
#include <io.h>
#define isatty(x) _isatty(x)
#define fileno(x) _fileno(x)
#define mkdir(x) _mkdir(x)
#include <direct.h>
#define PATHSEP '\\'
#else
#define mkdir(x) mkdir(x, 0755)
#include <unistd.h>
#define PATHSEP '/'
#endif
namespace {
@ -31,6 +39,28 @@ namespace {
return s;
}
std::string stripDirectory(std::string filename) {
std::string dir = filename;
std::size_t pos;
// '/' can be used on most platforms (even windows)
pos = dir.find_last_of('/');
if (pos != dir.npos) {
dir = dir.substr(0,pos + 1);
}
// strip platform specific as well
pos = dir.find_last_of(PATHSEP);
if (pos != dir.npos) {
dir = dir.substr(0,pos + 1);
}
if(dir == filename) {
dir = "";
}
return dir;
}
/**
* G3Log sink
*/
@ -63,7 +93,7 @@ namespace {
namespace pds {
Driver::Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string parsername, bool debug):
Driver::Driver(std::unique_ptr<BackendManager> backends, std::string 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_debug(debug) {
}
@ -73,11 +103,7 @@ namespace pds {
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
status = mkdir(m_outputdir.c_str());
if(status !=0 && errno != EEXIST){
throw DriverException("The folder " + m_outputdir + " does not exist and we're unable to create it.");
}
@ -91,7 +117,17 @@ namespace pds {
g3::initializeLogging(logworker.get());
if (!m_parsername.length()) throw DriverException("no valid parser name possible");
Config config = InputParser::parseInput(m_inputfile);
/* Open parsodus config file, if possible */
std::ifstream infile(m_inputfile);
if (!infile.good()) {
LOG(WARNING) << "Could not open file '" << m_inputfile << "' for reading" << std::endl;
return 1;
}
Config config = InputParser::parseInput(infile, stripDirectory(m_inputfile));
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);
back->generateParser([this](std::string filename) -> std::unique_ptr<std::ostream> {

View File

@ -14,13 +14,13 @@ namespace pds {
const char* InputParserException::what() const throw() {
return m_what.c_str();
}
Config InputParser::parseInput(std::istream& is) {
Config InputParser::parseInput(std::istream& is, std::string current_dir) {
ParsodusLexer lex(is);
Parser parser(lex);
Config cnf = *parser.parse();
if (!cnf.lexesisFile.empty()) {
std::fstream file(cnf.lexesisFile);
std::fstream file(current_dir + cnf.lexesisFile);
auto terminals = lxs::input::InputParser::getTokens(file);
for(auto& terminal : terminals)
cnf.grammar.terminals.insert(terminal);

View File

@ -26,13 +26,6 @@ int main(int argc, char** argv) {
return 1;
}
/* Open parsodus config file, if possible */
std::ifstream infile(args[0]);
if (!infile.good()) {
std::cerr << "Could not open file '" << args[0] << "' for reading" << std::endl;
return 1;
}
#ifdef _WIN32
const char PATHSEP = '\\';
#else
@ -69,7 +62,7 @@ int main(int argc, char** argv) {
/* Create parserfile, if possible */
try {
pds::Driver driver(std::move(backendManager), infile, options["outputdir"], options["language"], parsername, options.get("debug"));
pds::Driver driver(std::move(backendManager), args[0], options["outputdir"], options["language"], parsername, options.get("debug"));
return driver.run();
} catch (std::exception &err) {
std::cout << err.what() << std::endl;