From cd39f64659c3dae6d66885910886b4133d890846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Av=C3=A9?= Date: Thu, 19 Jan 2017 17:37:35 +0100 Subject: [PATCH] Parser selection without enum --- include/Parsodus/backend.h | 2 +- include/Parsodus/backendmanager.h | 10 +++++----- include/Parsodus/backends/cppLR.h | 20 +++++++++++++------- include/Parsodus/config.h | 2 +- src/backend.cpp | 2 +- src/backendmanager.cpp | 2 +- src/driver.cpp | 2 +- src/inputparser.cpp | 9 +-------- 8 files changed, 24 insertions(+), 25 deletions(-) diff --git a/include/Parsodus/backend.h b/include/Parsodus/backend.h index 43163fd..ca3353c 100644 --- a/include/Parsodus/backend.h +++ b/include/Parsodus/backend.h @@ -46,7 +46,7 @@ namespace pds { * @return Can this backend generate this type of parser */ - virtual bool canGenerateParser(util::ParserType parserType); + virtual bool canGenerateParser(std::string parserType); /** * The function that gets called to generate the actual parser diff --git a/include/Parsodus/backendmanager.h b/include/Parsodus/backendmanager.h index 51ac58c..27a0695 100644 --- a/include/Parsodus/backendmanager.h +++ b/include/Parsodus/backendmanager.h @@ -30,10 +30,10 @@ namespace pds { template class T> void registerLR() { - registerBackend(std::make_unique>>(util::ParserType::LR_0)); - registerBackend(std::make_unique>>(util::ParserType::SLR_1)); - registerBackend(std::make_unique>>(util::ParserType::LR_1)); - registerBackend(std::make_unique>>(util::ParserType::LALR_1)); + registerBackend(std::make_unique>>("LR(0)")); + registerBackend(std::make_unique>>("SLR(1)")); + registerBackend(std::make_unique>>("LR(1)")); + registerBackend(std::make_unique>>("LALR(1)")); } /** @@ -44,7 +44,7 @@ namespace pds { * @param parserType the type of parser it should be able to produce (e.g. LALR_1, LR_0, etc..) * @returns A pointer to a Backend if it can find one, nullptr otherwise */ - Backend* findBackend(std::string lang, util::ParserType parserType); + Backend* findBackend(std::string lang, std::string parserType); private: std::vector > m_backends; ///< The list of registered backends diff --git a/include/Parsodus/backends/cppLR.h b/include/Parsodus/backends/cppLR.h index 3ac016a..bf6d011 100644 --- a/include/Parsodus/backends/cppLR.h +++ b/include/Parsodus/backends/cppLR.h @@ -20,21 +20,27 @@ namespace backends { */ template class CppLRBackend : public Backend { - public: - CppLRBackend(util::ParserType parserType): Backend(), m_parserType(parserType) {} + public: + CppLRBackend(std::string parserType): Backend() { + for (char& c : parserType) + c = std::tolower(c); + m_parserType = parserType; + } - ~CppLRBackend() {} + ~CppLRBackend() {} - std::string getName() { + std::string getName() { return "c++"; } - bool canProcessLang(std::string lang) { + bool canProcessLang(std::string lang) { for (char& c : lang) c = std::tolower(c); return lang == "c++" || lang == "cpp" || lang == "cxx"; } - bool canGenerateParser(util::ParserType parserType) { + bool canGenerateParser(std::string parserType) { + for (char& c : parserType) + c = std::tolower(c); return parserType == m_parserType; } @@ -134,7 +140,7 @@ namespace backends { this->doTemplate(*implOut, "c++/lr.cpp", topLevel); } private: - util::ParserType m_parserType; + std::string m_parserType; }; } } diff --git a/include/Parsodus/config.h b/include/Parsodus/config.h index 976c314..1082567 100644 --- a/include/Parsodus/config.h +++ b/include/Parsodus/config.h @@ -8,7 +8,7 @@ namespace pds { struct Config { - util::ParserType parserType; + std::string parserType; std::string lexesisFile; Grammar grammar; }; diff --git a/src/backend.cpp b/src/backend.cpp index 979dc8d..ca7eee2 100644 --- a/src/backend.cpp +++ b/src/backend.cpp @@ -12,7 +12,7 @@ namespace pds { return false; } - bool Backend::canGenerateParser(util::ParserType /*parserType*/) { + bool Backend::canGenerateParser(std::string /*parserType*/) { return false; } diff --git a/src/backendmanager.cpp b/src/backendmanager.cpp index 9202f0f..ac5ea4b 100644 --- a/src/backendmanager.cpp +++ b/src/backendmanager.cpp @@ -6,7 +6,7 @@ namespace pds { m_backends.push_back(std::move(backend)); } - Backend* BackendManager::findBackend(std::string lang, util::ParserType parserType) { + Backend* BackendManager::findBackend(std::string lang, std::string parserType) { for(std::unique_ptr &backend: m_backends) { if(backend->canProcessLang(lang) && backend->canGenerateParser(parserType)) return backend.get(); } diff --git a/src/driver.cpp b/src/driver.cpp index b42c7f8..f780624 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -36,7 +36,7 @@ namespace pds { 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"); + 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 { struct stat sb; if(stat(m_outputdir.c_str(), &sb) != 0) { diff --git a/src/inputparser.cpp b/src/inputparser.cpp index 8ab75e5..390d9e5 100644 --- a/src/inputparser.cpp +++ b/src/inputparser.cpp @@ -32,14 +32,7 @@ namespace pds { case ParsodusLexer::PARSER: lexColon(lex, token); if(token.type == ParsodusLexer::PARSERTYPE) { - if(token.content == "lalr(1)") { - config.parserType = util::ParserType::LALR_1; - } else if(token.content == "SLR(1)") { - config.parserType = util::ParserType::SLR_1; - } else if(token.content == "LR(1)") { - config.parserType = util::ParserType::LR_1; - } else - throw InputParserException("Unkown parser type"); + config.parserType = token.content; } else { throw InputParserException("inputfile malformed, no parser type found in parser section"); }