1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
|
/*
This file is a part of KMC software distributed under GNU GPL 3 licence.
The homepage of the KMC project is http://sun.aei.polsl.pl/kmc
Authors: Marek Kokot
Version: 3.2.4
Date : 2024-02-09
*/
#include "parser.h"
#include "tokenizer.h"
#include "output_parser.h"
#include "config.h"
/*****************************************************************************************************************************/
/******************************************************** CONSTRUCTOR ********************************************************/
/*****************************************************************************************************************************/
CParser::CParser(const std::string& src):
config(CConfig::GetInstance())
{
line_no = 0;
file.open(src);
if (!file.is_open())
{
std::cerr << "Error: cannot open file: " << src << "\n";
exit(1);
}
//input_line_pattern = "\\s*(\\w*)\\s*=\\s*(.*)$";
input_line_pattern = "^\\s*([\\w+-]*)\\s*=\\s*(.*)$";
output_line_pattern = "^\\s*(.*)\\s*=\\s*(.*)$"; //TODO: consider valid file name
empty_line_pattern = "^\\s*$";
}
/*****************************************************************************************************************************/
/********************************************************** PUBLIC ***********************************************************/
/*****************************************************************************************************************************/
void CParser::ParseInputs()
{
std::string line;
while (true)
{
if (!nextLine(line))
{
std::cerr << "Error: 'INPUT:' missing\n";
exit(1);
}
if (line.find("INPUT:") != std::string::npos)
break;
}
if (!nextLine(line) || line.find("OUTPUT:") != std::string::npos)
{
std::cerr << "Error: None input was defined\n";
exit(1);
}
while (true)
{
parseInputLine(line);
if (!nextLine(line))
{
std::cerr << "Error: 'OUTPUT:' missing\n";
exit(1);
}
if (line.find("OUTPUT:") != std::string::npos)
break;
}
}
//************************************************************************************************************
void CParser::ParseOutput()
{
std::string line;
if (!nextLine(line) || line.find("OUTPUT_PARAMS:") != std::string::npos)
{
std::cerr << "Error: None output was defined\n";
exit(1);
}
parseOutputLine(line);
while (nextLine(line))
{
if (line.find("OUTPUT_PARAMS:") != std::string::npos)
{
parseOtuputParamsLine();
break;
}
}
}
/*****************************************************************************************************************************/
/********************************************************** PRIVATE **********************************************************/
/*****************************************************************************************************************************/
/*****************************************************************************************************************************/
void CParser::parseInputLine(const std::string& line)
{
std::smatch match;
if (std::regex_search(line, match, input_line_pattern))
{
#ifdef ENABLE_DEBUG
std::cout << "\ninput name: " << match[1];
std::cout << "\nafter = " << match[2];
#endif
if (input.find(match[1]) != input.end())
{
std::cerr << "Error: Name redefinition(" << match[1] << ")" << " line: " << line_no << "\n";
exit(1);
}
if (CTokenizer::GetKeywords().find(match[1]) != CTokenizer::GetKeywords().end())
{
std::cerr << "Error: `" << match[1] << "` is not valid name, line: " << line_no << "\n";
exit(1);
}
else
{
std::string file_name;
std::istringstream stream(match[2]);
CInputDesc desc;
if (!(stream >> desc.file_src))
{
std::cerr << "Error: file name for " << match[1] << " was not specified, line: "<< line_no <<"\n";
exit(1);
}
std::string tmp;
while (stream >> tmp)
{
if (strncmp(tmp.c_str(), "-ci", 3) == 0)
{
desc.cutoff_min = atoi(tmp.c_str() + 3);
continue;
}
else if (strncmp(tmp.c_str(), "-cx", 3) == 0)
{
desc.cutoff_max = atoi(tmp.c_str() + 3);
continue;
}
std::cerr << "Error: Unknown parameter " << tmp << " for variable " << match[1] << ", line: "<< line_no <<"\n";
exit(1);
}
config.input_desc.push_back(desc);
input[match[1]] = (uint32)(config.input_desc.size() - 1);
}
}
else
{
std::cerr << "Error: wrong line format, line: " << line_no << "\n";
exit(1);
}
}
//************************************************************************************************************
void CParser::parseOutputLine(const std::string& line)
{
std::smatch match;
if (std::regex_search(line, match, output_line_pattern))
{
#ifdef ENABLE_DEBUG
std::cout << "out file name " << match[1] << "\n";
std::cout << "rest of output " << match[2] << "\n";
std::cout << "Tokenize resf of output\n";
#endif
config.output_desc.file_src = match[1];
//trim whitespaces at the end
static const std::string whitespace = " \t\r\n\v\f";
auto end = config.output_desc.file_src.find_last_not_of(whitespace);
config.output_desc.file_src.erase(end + 1);
if (config.output_desc.file_src == "")
{
std::cerr << "Error: wrong line format, line: " << line_no << " (output file name is not specified)\n";
exit(1);
}
tokenizer.Tokenize(match[2], tokens);
}
else
{
std::cerr << "Error: wrong line format, line: " << line_no << "\n";
exit(1);
}
}
/*****************************************************************************************************************************/
void CParser::parseOtuputParamsLine()
{
std::string line;
if (!nextLine(line))
{
std::cerr << "Warning: OUTPUT_PARAMS exists, but no parameters are defined\n";
}
else
{
std::istringstream stream(line);
std::string tmp;
while (stream >> tmp)
{
if (strncmp(tmp.c_str(), "-ci", 3) == 0)
{
config.output_desc.cutoff_min = atoi(tmp.c_str() + 3);
continue;
}
else if (strncmp(tmp.c_str(), "-cx", 3) == 0)
{
config.output_desc.cutoff_max = atoi(tmp.c_str() + 3);
continue;
}
else if ((strncmp(tmp.c_str(), "-cs", 3) == 0))
{
config.output_desc.counter_max = atoi(tmp.c_str() + 3);
continue;
}
else if ((strncmp(tmp.c_str(), "-o", 2) == 0))
{
if (strncmp(tmp.c_str() + 2, "kff", 3) == 0)
config.output_desc.output_type = OutputType::KFF1;
else if (strncmp(tmp.c_str() + 2, "kmc", 3) == 0)
config.output_desc.output_type = OutputType::KMC1;
else
{
std::cerr << "Error: Unknown output type: " << tmp.c_str() + 2 << "\n";
exit(1);
}
continue;
}
std::cerr << "Error: Unknown parameter " << tmp << " for variable " << tmp << ", line: " << line_no << "\n";
exit(1);
}
}
}
/*****************************************************************************************************************************/
bool CParser::nextLine(std::string& line)
{
while (true)
{
if (file.eof())
return false;
std::getline(file, line);
++line_no;
std::smatch match;
if (!std::regex_search(line, match, empty_line_pattern))
return true;
}
}
// ***** EOF
|