Use references instead of pointers for exceptions + catch regex parsing exceptions in inputparser and add more info before rethrowing
This commit is contained in:
parent
eb93293551
commit
dbc79353e7
|
@ -24,12 +24,12 @@ namespace lxs {
|
|||
/**
|
||||
* parse the lines and return pairs of (Token type, regex)
|
||||
*/
|
||||
static std::vector<std::pair<std::string,std::string> > parseLines(std::istream &is);
|
||||
static std::vector<std::pair<int, std::pair<std::string,std::string> > > parseLines(std::istream &is);
|
||||
|
||||
/**
|
||||
* Convert the lines from `parseLines` to ENFA's
|
||||
*/
|
||||
static std::vector<ENFA> linesToEnfa(std::vector<std::pair<std::string,std::string> > &input);
|
||||
static std::vector<ENFA> linesToEnfa(std::vector<std::pair<int, std::pair<std::string,std::string> > > &input);
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -37,9 +37,9 @@ namespace lxs {
|
|||
{}
|
||||
|
||||
int Driver::run() {
|
||||
if (!m_lexername.length()) throw new DriverException("no valid lexer name possible");
|
||||
if (!m_lexername.length()) throw DriverException("no valid lexer name possible");
|
||||
Backend* back = m_backends->findBackendForLang(m_language);
|
||||
if (!back) throw new DriverException("Could not find a valid backend for language " + m_language );
|
||||
if (!back) throw DriverException("Could not find a valid backend for language " + m_language );
|
||||
|
||||
DFA dfa = InputParser::parseInput(m_inputfile);
|
||||
|
||||
|
|
|
@ -23,43 +23,47 @@ namespace lxs {
|
|||
return dfa;
|
||||
}
|
||||
|
||||
std::vector<std::pair<std::string,std::string> > InputParser::parseLines(std::istream &is) {
|
||||
std::vector<std::pair<int, std::pair<std::string,std::string> > > InputParser::parseLines(std::istream &is) {
|
||||
std::string line;
|
||||
std::vector<std::pair<std::string,std::string> > result;
|
||||
std::vector<std::pair<int, std::pair<std::string,std::string> > > result;
|
||||
unsigned int i=0;
|
||||
while(std::getline(is,line)) {
|
||||
i++;
|
||||
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) 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!");
|
||||
if(loc == std::string::npos) throw InputParserException("Invalid syntax on line " + std::to_string(i) + ": no '=' found!");
|
||||
if(start == loc) throw InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!");
|
||||
std::string tokenname = line.substr(start, loc);
|
||||
if(tokenname.length() == 0) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid tokenname specified!");
|
||||
if(tokenname.length() == 0) throw 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) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!");
|
||||
if(regex.length() == 0) throw 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) throw new InputParserException("Invalid syntax on line " + std::to_string(i) + ": no valid regex specified!");
|
||||
if(start == std::string::npos) throw 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));
|
||||
result.push_back(std::make_pair(i,std::make_pair(tokenname,regex)));
|
||||
}
|
||||
if(result.size() == 0) throw new InputParserException("No valid rules found in the input file!");
|
||||
if(result.size() == 0) throw InputParserException("No valid rules found in the input file!");
|
||||
return result;
|
||||
}
|
||||
|
||||
std::vector<ENFA> InputParser::linesToEnfa(std::vector<std::pair<std::string,std::string> > &input) {
|
||||
std::vector<ENFA> InputParser::linesToEnfa(std::vector<std::pair<int, std::pair<std::string,std::string> > > &input) {
|
||||
std::vector<ENFA> result;
|
||||
for(unsigned int i=0;i<input.size();i++) {
|
||||
std::shared_ptr<lxs::RE> re = lxs::parseRE(input[i].second);
|
||||
ENFA enfa;
|
||||
re->toENFA(enfa,0);
|
||||
enfa.numStates++;
|
||||
enfa.starting = 0;
|
||||
enfa.priority[(State) *enfa.accepting.begin()] = (Priority) i;
|
||||
enfa.acceptingToken[(State) *enfa.accepting.begin()] = input[i].first;
|
||||
result.push_back(enfa);
|
||||
try {
|
||||
std::shared_ptr<lxs::RE> re = lxs::parseRE(input[i].second.second);
|
||||
ENFA enfa;
|
||||
re->toENFA(enfa,0);
|
||||
enfa.numStates++;
|
||||
enfa.starting = 0;
|
||||
enfa.priority[(State) *enfa.accepting.begin()] = (Priority) i;
|
||||
enfa.acceptingToken[(State) *enfa.accepting.begin()] = input[i].first;
|
||||
result.push_back(enfa);
|
||||
} catch(SyntaxError &err) {
|
||||
throw InputParserException("Error when parsing regex on line " + std::to_string(input[i].first) + ":\n\t" + err.what());
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
|
|
@ -65,9 +65,8 @@ int main(int argc, char** argv) {
|
|||
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;
|
||||
} catch (std::exception &err) {
|
||||
std::cout << err.what() << std::endl;
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue