test spirit parser moved from classic to X3.
All checks were successful
Build and Deploy Documentation / build-and-deploy (push) Successful in 19s

This commit is contained in:
2025-12-27 12:32:01 +01:00
parent 3442ca7b62
commit 5d3981d8b2
11 changed files with 1072 additions and 1296 deletions

View File

@@ -31,9 +31,8 @@
#include <string>
#include <cassert>
#include <algorithm>
#include <fstream>
using namespace std;
#include <boost/algorithm/string.hpp>
@@ -51,8 +50,8 @@ PFunctionHandler::PFunctionHandler(char *fln, bool debug) : fDebug(debug), fFile
{
fValid = true;
cout << endl << "in PFunctionHandler(char *fln)";
cout << endl << "fFileName = " << fFileName;
std::cout << std::endl << "in PFunctionHandler(char *fln)";
std::cout << std::endl << "fFileName = " << fFileName;
fValid = ReadFile();
if (fValid)
@@ -67,12 +66,12 @@ PFunctionHandler::PFunctionHandler(char *fln, bool debug) : fDebug(debug), fFile
*
* \param lines
*/
PFunctionHandler::PFunctionHandler(vector<string> lines)
PFunctionHandler::PFunctionHandler(std::vector<std::string> lines)
{
fValid = true;
fFileName = "";
cout << endl << "in PFunctionHandler(vector<string> lines)";
std::cout << std::endl << "in PFunctionHandler(std::vector<std::string> lines)";
if (lines.size() == 0) {
fValid = false;
@@ -90,43 +89,43 @@ PFunctionHandler::PFunctionHandler(vector<string> lines)
continue;
boost::to_upper(lines[i]);
if (lines[i].find("PAR") == 0) {
cout << endl << "this is a parameter line ...";
std::cout << std::endl << "this is a parameter line ...";
status = sscanf(lines[i].c_str(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
cout << endl << "status=" << status;
std::cout << std::endl << "status=" << status;
if (status < 0) {
done = true;
fValid = false;
cout << endl << "invalid PAR line, sorry ...";
std::cout << std::endl << "invalid PAR line, sorry ...";
} else { // fill par
cout << endl << "PAR line, status = " << status;
std::cout << std::endl << "PAR line, status = " << status;
for (int i=0; i<status; i++)
fParam.push_back(dval[i]);
cout << endl << "fParam.size()=" << fParam.size() << endl;
std::cout << std::endl << "fParam.size()=" << fParam.size() << std::endl;
for (unsigned int i=0; i<fParam.size(); i++)
cout << endl << "Par" << i+1 << " = " << fParam[i];
cout << endl;
std::cout << std::endl << "Par" << i+1 << " = " << fParam[i];
std::cout << std::endl;
}
} else if (lines[i].find("MAP") == 0) {
cout << endl << "this is a map line ...";
std::cout << std::endl << "this is a map line ...";
status = sscanf(lines[i].c_str(), "MAP %d %d %d %d %d %d %d %d %d %d",
&val[0], &val[1], &val[2], &val[3], &val[4],
&val[5], &val[6], &val[7], &val[8], &val[9]);
if (status < 0) {
done = true;
fValid = false;
cout << endl << "invalid MAP line, sorry ...";
std::cout << std::endl << "invalid MAP line, sorry ...";
} else { // fill map
cout << endl << "MAP line, status = " << status;
std::cout << std::endl << "MAP line, status = " << status;
for (int i=0; i<status; i++)
fMap.push_back(val[i]);
}
} else if (lines[i].find("FUNCTIONS") == 0) {
cout << endl << "the functions block start ...";
std::cout << std::endl << "the functions block start ...";
inFcnBlock = true;
} else if (lines[i].find("END") == 0) {
cout << endl << "end tag found; rest will be ignored";
std::cout << std::endl << "end tag found; rest will be ignored";
done = true;
} else if (inFcnBlock) {
fLines.push_back(lines[i]);
@@ -136,20 +135,20 @@ PFunctionHandler::PFunctionHandler(vector<string> lines)
// check if all blocks are given
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
if (fMap.size() == 0)
cout << endl << "MAP block is missing ...";
std::cout << std::endl << "MAP block is missing ...";
if (fParam.size() == 0)
cout << endl << "PAR block is missing ...";
std::cout << std::endl << "PAR block is missing ...";
if (fLines.size() == 0)
cout << endl << "FUNCTION block is missing ...";
std::cout << std::endl << "FUNCTION block is missing ...";
fValid = false;
}
fValid = MapsAreValid();
if (fValid) {
cout << endl << "Functions: ";
std::cout << std::endl << "Functions: ";
for (unsigned int i=0; i<fLines.size(); i++)
cout << endl << fLines[i];
std::cout << std::endl << fLines[i];
}
}
@@ -162,7 +161,7 @@ PFunctionHandler::PFunctionHandler(vector<string> lines)
*/
PFunctionHandler::~PFunctionHandler()
{
cout << endl << "in ~PFunctionHandler()" << endl << endl;
std::cout << std::endl << "in ~PFunctionHandler()" << std::endl << std::endl;
fParam.clear();
fMap.clear();
fLines.clear();
@@ -179,22 +178,25 @@ PFunctionHandler::~PFunctionHandler()
*/
bool PFunctionHandler::DoParse()
{
cout << endl << "in PFunctionHandler::DoParse() ...";
std::cout << std::endl << "in PFunctionHandler::DoParse() ...";
bool success = true;
PFunctionGrammar function;
for (unsigned int i=0; i<fLines.size(); i++) {
cout << endl << "fLines[" << i << "] = '" << fLines[i] << "'";
std::cout << std::endl << "fLines[" << i << "] = '" << fLines[i] << "'";
tree_parse_info<> info = ast_parse(fLines[i].c_str(), function, space_p);
// Convert to uppercase (grammar expects uppercase tokens)
std::string line = fLines[i];
std::transform(line.begin(), line.end(), line.begin(), ::toupper);
if (info.full) {
cout << endl << "parse successfull ..." << endl;
PFunction func(info, fParam, fMap, fDebug);
// Use new Qi-based PFunction constructor
PFunction func(line, fParam, fMap, fDebug);
if (func.IsValid()) {
std::cout << std::endl << "parse successfull ..." << std::endl;
fFuncs.push_back(func);
} else {
cout << endl << "parse failed ... (" << i << ")" << endl;
std::cout << std::endl << "parse failed ... (" << i << ")" << std::endl;
success = false;
break;
}
@@ -205,8 +207,8 @@ bool PFunctionHandler::DoParse()
if (success) {
for (unsigned int i=0; i<fFuncs.size(); i++) {
if (!fFuncs[i].IsValid()) {
cout << endl << "**ERROR**: function fun" << fFuncs[i].GetFuncNo();
cout << " has a problem with either parameter or map out of range!";
std::cout << std::endl << "**ERROR**: function fun" << fFuncs[i].GetFuncNo();
std::cout << " has a problem with either parameter or map out of range!";
success = false;
break;
}
@@ -218,8 +220,8 @@ bool PFunctionHandler::DoParse()
for (unsigned int i=0; i<fFuncs.size(); i++) {
for (unsigned int j=i+1; j<fFuncs.size(); j++) {
if (fFuncs[i].GetFuncNo() == fFuncs[j].GetFuncNo()) {
cout << endl << "**ERROR**: function number " << fFuncs[i].GetFuncNo();
cout << " is at least twice present! Fix this first.";
std::cout << std::endl << "**ERROR**: function number " << fFuncs[i].GetFuncNo();
std::cout << " is at least twice present! Fix this first.";
success = false;
}
}
@@ -228,7 +230,7 @@ bool PFunctionHandler::DoParse()
if (success) {
for (unsigned int i=0; i<fFuncs.size(); i++)
cout << endl << "func number = " << fFuncs[i].GetFuncNo();
std::cout << std::endl << "func number = " << fFuncs[i].GetFuncNo();
}
return success;
@@ -245,13 +247,10 @@ bool PFunctionHandler::DoParse()
double PFunctionHandler::Eval(int i)
{
if (GetFuncIndex(i) == -1) {
cout << endl << "**ERROR**: Couldn't find FUN" << i << " for evaluation";
std::cout << std::endl << "**ERROR**: Couldn't find FUN" << i << " for evaluation";
return 0.0;
}
cout << endl << "PFunctionHandler::Eval: GetFuncIndex("<<i<<") = " << GetFuncIndex(i);
cout << endl;
return fFuncs[GetFuncIndex(i)].Eval();
}
@@ -280,24 +279,24 @@ unsigned int PFunctionHandler::GetFuncNo(unsigned int i)
*/
bool PFunctionHandler::ReadFile()
{
cout << endl << "in ~PFunctionHandler::ReadFile()";
std::cout << std::endl << "in ~PFunctionHandler::ReadFile()";
if (fFileName.length() == 0) {
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
cout << endl << " no file name given :-(. Will quit";
std::cout << std::endl << "PFunctionHandler::ReadFile(): **ERROR**";
std::cout << std::endl << " no file name given :-(. Will quit";
return false;
}
ifstream f;
f.open(fFileName.c_str(), ifstream::in);
std::ifstream f;
f.open(fFileName.c_str(), std::ifstream::in);
if (!f.is_open()) {
cout << endl << "PFunctionHandler::ReadFile(): **ERROR**";
cout << endl << " File '" << fFileName.c_str() << "' couldn't being opened.";
std::cout << std::endl << "PFunctionHandler::ReadFile(): **ERROR**";
std::cout << std::endl << " File '" << fFileName.c_str() << "' couldn't being opened.";
return false;
}
string line;
std::string line;
char c_line[128];
bool done = false;
bool success = true;
@@ -314,41 +313,41 @@ bool PFunctionHandler::ReadFile()
continue;
boost::to_upper(line);
if (line.find("PAR") == 0) {
cout << endl << "this is a parameter line ...";
std::cout << std::endl << "this is a parameter line ...";
status = sscanf(line.c_str(), "PAR %lf %lf %lf %lf %lf %lf %lf %lf %lf %lf",
&dval[0], &dval[1], &dval[2], &dval[3], &dval[4],
&dval[5], &dval[6], &dval[7], &dval[8], &dval[9]);
if (status < 0) {
done = true;
success = false;
cout << endl << "invalid PAR line, sorry ...";
std::cout << std::endl << "invalid PAR line, sorry ...";
} else { // fill map
cout << endl << "PAR line, status = " << status;
std::cout << std::endl << "PAR line, status = " << status;
for (int i=0; i<status; i++)
fParam.push_back(dval[i]);
for (unsigned int i=0; i<fParam.size(); i++)
cout << endl << "Par" << i+1 << " = " << fParam[i];
cout << endl;
std::cout << std::endl << "Par" << i+1 << " = " << fParam[i];
std::cout << std::endl;
}
} else if (line.find("MAP") == 0) {
cout << endl << "this is a map line ...";
std::cout << std::endl << "this is a map line ...";
status = sscanf(line.c_str(), "MAP %d %d %d %d %d %d %d %d %d %d",
&val[0], &val[1], &val[2], &val[3], &val[4],
&val[5], &val[6], &val[7], &val[8], &val[9]);
if (status < 0) {
done = true;
success = false;
cout << endl << "invalid MAP line, sorry ...";
std::cout << std::endl << "invalid MAP line, sorry ...";
} else { // fill map
cout << endl << "MAP line, status = " << status;
std::cout << std::endl << "MAP line, status = " << status;
for (int i=0; i<status; i++)
fMap.push_back(val[i]);
}
} else if (line.find("FUNCTIONS") == 0) {
cout << endl << "the functions block start ...";
std::cout << std::endl << "the functions block start ...";
inFcnBlock = true;
} else if (line.find("END") == 0) {
cout << endl << "end tag found; rest will be ignored";
std::cout << std::endl << "end tag found; rest will be ignored";
done = true;
} else if (inFcnBlock) {
fLines.push_back(line);
@@ -361,18 +360,18 @@ bool PFunctionHandler::ReadFile()
// check if all blocks are given
if ((fMap.size() == 0) || (fParam.size() == 0) || (fLines.size() == 0)) {
if (fMap.size() == 0)
cout << endl << "MAP block is missing ...";
std::cout << std::endl << "MAP block is missing ...";
if (fParam.size() == 0)
cout << endl << "PAR block is missing ...";
std::cout << std::endl << "PAR block is missing ...";
if (fLines.size() == 0)
cout << endl << "FUNCTION block is missing ...";
std::cout << std::endl << "FUNCTION block is missing ...";
success = false;
}
if (success) {
cout << endl << "Functions: ";
std::cout << std::endl << "Functions: ";
for (unsigned int i=0; i<fLines.size(); i++)
cout << endl << fLines[i];
std::cout << std::endl << fLines[i];
}
return success;
@@ -395,7 +394,7 @@ bool PFunctionHandler::MapsAreValid()
success = false;
if (!success)
cout << endl << "invalid MAP found ...";
std::cout << std::endl << "invalid MAP found ...";
return success;
}