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 {
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) :
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);

View File

@ -6,8 +6,15 @@
#include <string>
#include <iostream>
#include <memory>
#include <exception>
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;
}

View File

@ -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 <fstream>
@ -62,6 +62,12 @@ int main(int argc, char** argv) {
std::unique_ptr<lxs::BackendManager> backends(new lxs::BackendManager());
backends->registerBackend(std::unique_ptr<lxs::Backend>(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;
}
}