From 81f3c022c8618530f6448ac73373a5992e2cf5f9 Mon Sep 17 00:00:00 2001 From: Thomas Ave Date: Fri, 27 May 2016 00:15:06 +0200 Subject: [PATCH] Make inputparser more robust + add minimize --- src/inputparser.cpp | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/inputparser.cpp b/src/inputparser.cpp index a328e66..00e3f6f 100644 --- a/src/inputparser.cpp +++ b/src/inputparser.cpp @@ -12,24 +12,51 @@ namespace lxs { auto enfavect = parseLines(is); auto enfas = linesToEnfa(enfavect); auto enfa = merge(enfas); - auto dfa = mssc(enfa); + auto dfa = minimize(mssc(enfa)); return dfa; } std::vector > InputParser::parseLines(std::istream &is) { std::string line; std::vector > result; + unsigned int i=0; while(std::getline(is,line)) { - int loc = line.find_first_of('='); - int start = line.find_first_not_of(" \t\v\f\r"); + if(line.length() == 0) continue; + i++; + 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); + } + size_t start = line.find_first_not_of(" \t\v\f\r"); + 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); - int end = tokenname.find_last_not_of(" \t\v\f\r"); + if(tokenname.length() == 0) { + 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"); tokenname = tokenname.substr(0,end + 1); std::string regex = line.substr(loc+1); - start = regex.find_first_not_of(" "); + if(regex.length() == 0) { + 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"); + if(start == std::string::npos) { + std::cerr << "Invalid syntax on line " << i << ": no valid regex specified!" << std::endl; + exit(1); + } 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); + } return result; }