make use of exceptions to pass errors to main instead of cout + exit(1)

This commit is contained in:
Thomas Ave 2016-05-29 00:42:21 +02:00
parent e2517cf16a
commit f87ce737fc
3 changed files with 30 additions and 36 deletions

View File

@ -19,6 +19,12 @@ namespace {
} }
namespace lxs { 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<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string lexername) : Driver::Driver(std::unique_ptr<BackendManager> backends, std::istream& inputfile, std::string outputdir, std::string language, std::string lexername) :
m_backends(std::move(backends)), m_backends(std::move(backends)),
m_inputfile(inputfile), m_inputfile(inputfile),
@ -31,16 +37,9 @@ namespace lxs {
{} {}
int Driver::run() { int Driver::run() {
if (!m_lexername.length()) { if (!m_lexername.length()) throw new DriverException("no valid lexer name possible");
std::cerr << "No valid lexer name possible" << std::endl;
return 1;
}
Backend* back = m_backends->findBackendForLang(m_language); Backend* back = m_backends->findBackendForLang(m_language);
if (!back) { if (!back) throw new DriverException("Could not find a valid backend for language " + m_language );
std::cerr << "Could not find a valid backend for language " << m_language << std::endl;
return 1;
}
DFA dfa = InputParser::parseInput(m_inputfile); DFA dfa = InputParser::parseInput(m_inputfile);

View File

@ -6,8 +6,15 @@
#include <string> #include <string>
#include <iostream> #include <iostream>
#include <memory> #include <memory>
#include <exception>
namespace lxs { 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) { DFA InputParser::parseInput(std::istream &is) {
auto enfavect = parseLines(is); auto enfavect = parseLines(is);
auto enfas = linesToEnfa(enfavect); auto enfas = linesToEnfa(enfavect);
@ -25,38 +32,20 @@ namespace lxs {
size_t start = line.find_first_not_of(" \t\v\f\r"); 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; if(line.length() == 0 || (start != std::string::npos && line.length() > start && line[start] == '#')) continue;
std::size_t loc = line.find_first_of('='); std::size_t loc = line.find_first_of('=');
if(loc == std::string::npos) { if(loc == std::string::npos) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no '=' found!");
std::cerr << "Invalid syntax on line " << i << ": no '=' found!" << std::endl; if(start == loc) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!");
exit(1);
}
if(start == loc) {
std::cerr << "Invalid syntax on line " << i << ": no valid tokenname specified!" << std::endl;
exit(1);
}
std::string tokenname = line.substr(start, loc); std::string tokenname = line.substr(start, loc);
if(tokenname.length() == 0) { if(tokenname.length() == 0) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!");
std::cerr << "Invalid syntax on line " << i << ": no valid tokenname specified!" << std::endl;
exit(1);
}
std::size_t end = tokenname.find_last_not_of(" \t\v\f\r"); std::size_t end = tokenname.find_last_not_of(" \t\v\f\r");
tokenname = tokenname.substr(0,end + 1); tokenname = tokenname.substr(0,end + 1);
std::string regex = line.substr(loc+1); std::string regex = line.substr(loc+1);
if(regex.length() == 0) { if(regex.length() == 0) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!");
std::cerr << "Invalid syntax on line " << i << ": no valid regex specified!" << std::endl;
exit(1);
}
start = regex.find_first_not_of(" \t\v\f\r"); start = regex.find_first_not_of(" \t\v\f\r");
if(start == std::string::npos) { if(start == std::string::npos) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!");
std::cerr << "Invalid syntax on line " << i << ": no valid regex specified!" << std::endl;
exit(1);
}
regex = regex.substr(start); regex = regex.substr(start);
result.push_back(std::make_pair(tokenname,regex)); result.push_back(std::make_pair(tokenname,regex));
} }
if(result.size() == 0) { if(result.size() == 0) throw new InputParserException("No valid rules found in the input file!");
std::cerr << "No valid rules found in the input file!" << std::endl;
exit(1);
}
return result; return result;
} }

View File

@ -1,7 +1,7 @@
#include "Lexesis/backendmanager.h" #include "Lexesis/backendmanager.h"
#include "Lexesis/backends/cpp.h" #include "Lexesis/backends/cpp.h"
#include "Lexesis/driver.h" #include "Lexesis/driver.h"
#include "Lexesis/inputparser.h"
#include "optparse.h" #include "optparse.h"
#include <fstream> #include <fstream>
@ -62,6 +62,12 @@ int main(int argc, char** argv) {
std::unique_ptr<lxs::BackendManager> backends(new lxs::BackendManager()); std::unique_ptr<lxs::BackendManager> backends(new lxs::BackendManager());
backends->registerBackend(std::unique_ptr<lxs::Backend>(new lxs::backends::CppBackend())); backends->registerBackend(std::unique_ptr<lxs::Backend>(new lxs::backends::CppBackend()));
lxs::Driver driver(std::move(backends), infile, options["outputdir"], options["language"], lexername); try {
return driver.run(); 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;
}
} }