From f87ce737fc06f58dc9bf65a93f2fad2effac518e Mon Sep 17 00:00:00 2001 From: Thomas Ave Date: Sun, 29 May 2016 00:42:21 +0200 Subject: [PATCH] make use of exceptions to pass errors to main instead of cout + exit(1) --- src/driver.cpp | 17 ++++++++--------- src/inputparser.cpp | 37 +++++++++++++------------------------ src/main.cpp | 12 +++++++++--- 3 files changed, 30 insertions(+), 36 deletions(-) diff --git a/src/driver.cpp b/src/driver.cpp index 08b2d68..2ffefed 100644 --- a/src/driver.cpp +++ b/src/driver.cpp @@ -19,6 +19,12 @@ namespace { } namespace lxs { + + DriverException::DriverException(std::string what): m_what(what) {} + const char* DriverException::what() const throw() { + return m_what.c_str(); + } + Driver::Driver(std::unique_ptr backends, std::istream& inputfile, std::string outputdir, std::string language, std::string lexername) : m_backends(std::move(backends)), m_inputfile(inputfile), @@ -31,16 +37,9 @@ namespace lxs { {} int Driver::run() { - if (!m_lexername.length()) { - std::cerr << "No valid lexer name possible" << std::endl; - return 1; - } - + if (!m_lexername.length()) throw new DriverException("no valid lexer name possible"); Backend* back = m_backends->findBackendForLang(m_language); - if (!back) { - std::cerr << "Could not find a valid backend for language " << m_language << std::endl; - return 1; - } + if (!back) throw new DriverException("Could not find a valid backend for language " + m_language ); DFA dfa = InputParser::parseInput(m_inputfile); diff --git a/src/inputparser.cpp b/src/inputparser.cpp index b63efaa..6e19bb1 100644 --- a/src/inputparser.cpp +++ b/src/inputparser.cpp @@ -6,8 +6,15 @@ #include #include #include +#include namespace lxs { + + InputParserException::InputParserException(std::string what): m_what(what) {} + const char* InputParserException::what() const throw() { + return m_what.c_str(); + } + DFA InputParser::parseInput(std::istream &is) { auto enfavect = parseLines(is); auto enfas = linesToEnfa(enfavect); @@ -25,38 +32,20 @@ namespace lxs { size_t start = line.find_first_not_of(" \t\v\f\r"); if(line.length() == 0 || (start != std::string::npos && line.length() > start && line[start] == '#')) continue; std::size_t loc = line.find_first_of('='); - if(loc == std::string::npos) { - std::cerr << "Invalid syntax on line " << i << ": no '=' found!" << std::endl; - exit(1); - } - if(start == loc) { - std::cerr << "Invalid syntax on line " << i << ": no valid tokenname specified!" << std::endl; - exit(1); - } + if(loc == std::string::npos) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no '=' found!"); + if(start == loc) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!"); std::string tokenname = line.substr(start, loc); - if(tokenname.length() == 0) { - std::cerr << "Invalid syntax on line " << i << ": no valid tokenname specified!" << std::endl; - exit(1); - } + if(tokenname.length() == 0) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!"); std::size_t end = tokenname.find_last_not_of(" \t\v\f\r"); tokenname = tokenname.substr(0,end + 1); std::string regex = line.substr(loc+1); - if(regex.length() == 0) { - std::cerr << "Invalid syntax on line " << i << ": no valid regex specified!" << std::endl; - exit(1); - } + if(regex.length() == 0) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!"); start = regex.find_first_not_of(" \t\v\f\r"); - if(start == std::string::npos) { - std::cerr << "Invalid syntax on line " << i << ": no valid regex specified!" << std::endl; - exit(1); - } + if(start == std::string::npos) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!"); regex = regex.substr(start); result.push_back(std::make_pair(tokenname,regex)); } - if(result.size() == 0) { - std::cerr << "No valid rules found in the input file!" << std::endl; - exit(1); - } + if(result.size() == 0) throw new InputParserException("No valid rules found in the input file!"); return result; } diff --git a/src/main.cpp b/src/main.cpp index fdac507..390760b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,7 +1,7 @@ #include "Lexesis/backendmanager.h" #include "Lexesis/backends/cpp.h" #include "Lexesis/driver.h" - +#include "Lexesis/inputparser.h" #include "optparse.h" #include @@ -62,6 +62,12 @@ int main(int argc, char** argv) { std::unique_ptr backends(new lxs::BackendManager()); backends->registerBackend(std::unique_ptr(new lxs::backends::CppBackend())); - lxs::Driver driver(std::move(backends), infile, options["outputdir"], options["language"], lexername); - return driver.run(); + try { + lxs::Driver driver(std::move(backends), infile, options["outputdir"], options["language"], lexername); + return driver.run(); + } catch (std::exception *err) { + std::cout << err->what() << std::endl; + delete err; + return 1; + } }