Parsodus/src/driver.cpp

44 lines
1.6 KiB
C++

#include "Parsodus/driver.h"
#include "Parsodus/inputparser.h"
#include "Parsodus/config.h"
#include <fstream>
namespace {
/**
* Filter only valid identifier chars: alphanumeric, and not starting with a digit
*/
std::string clean(std::string in) {
std::string s;
for (char c : in) {
if ((s.length() && std::isalnum(c)) || std::isalpha(c) || c == '_')
s += c;
}
return s;
}
}
namespace pds {
Driver::Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string lexername, std::string parsername):
m_backends(std::move(backends)), m_inputfile(inputfile), m_outputdir(outputdir), m_language(language), m_lexername(clean(lexername)), m_parsername(clean(parsername)) {
}
Driver::~Driver(){}
int Driver::run() {
if (!m_lexername.length()) throw DriverException("no valid lexer name possible");
if (!m_parsername.length()) throw DriverException("no valid parser name possible");
Config config = InputParser::parseInput(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 the given type of parser");
back->generateParser([this](std::string filename) -> std::unique_ptr<std::ostream> {
return std::unique_ptr<std::ostream>(new std::ofstream(m_outputdir + "/" + filename));
},
m_lexername, config);
return 0;
}
}