#include "Parsodus/driver.h" #include "Parsodus/inputparser.h" #include "Parsodus/config.h" #include 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 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 { return std::unique_ptr(new std::ofstream(m_outputdir + "/" + filename)); }, m_lexername, config); return 0; } }