Commit c2bd6350 authored by Pierre Aubert's avatar Pierre Aubert
Browse files

Add base of config parsing

parent f7470962
......@@ -4,7 +4,7 @@
Licence : CeCILL-C
****************************************/
#include "string_utils.h"
#include "ConfigParser.h"
///Default constructor of ConfigParser
......@@ -47,12 +47,144 @@ void ConfigParser::copyConfigParser(const ConfigParser & other){
p_vecSource = other.p_vecSource;
}
///Parse the input file
/** @return true on success, false otherwise
*/
bool ConfigParser::parseFile(){
if(!p_run) return false;
PSource source;
source.setName(getFileName(p_parser->getFileName()));
//To parse the file we need to read char by char until we get something we know
while(!p_parser->isEndOfFile() && p_run){ //If we are not at the end of the file
PTable table;
if(parseTable(table)){
source.getVecTable().push_back(table);
}else{
errorAt();
std::cerr << "ConfigParser::parseFile : unexpected token '"<<p_parser->getNextToken()<<"'" << std::endl;
}
}
p_vecSource.push_back(source);
return true;
}
///Initialisation to be done just before loading a file
void ConfigParser::preLoadFile(){
//Save the current source
p_parser->setWhiteSpace(" \t\n");
p_parser->setSeparator(",;{}[]()");
}
///Initialisation to be done just after loading a file
void ConfigParser::postLoadFile(){
}
///Initialisation function of the class ConfigParser
void ConfigParser::initialisationConfigParser(){
}
///Parse a PTable
/** @param[out] table : PTable to be parsed
* @return true on success, false otherwise
*/
bool ConfigParser::parseTable(PTable & table){
std::string tableName(p_parser->getNextToken());
std::string dataSetName(tableName);
if(p_parser->isMatch("[")){
dataSetName = p_parser->getUntilKeyWithoutPatern("]");
}
if(!p_parser->isMatch("{")){
errorAt();
std::cerr << "ConfigParser::parseTable : expect '{' after table name '"<<tableName<<"'" << std::endl;
return true;
}
table.setName(tableName);
table.setDataSetName(dataSetName);
while(!p_parser->isEndOfFile() && !p_parser->isMatch("}") && p_run){
PAttribute attr;
if(parseAttribute(attr)){
table.getVecAttribute().push_back(attr);
}else{
errorAt();
std::cerr << "ConfigParser::parseTable : cannot parse attribute" << std::endl;
return true;
}
}
return true;
}
///Parse a PAttribute
/** @param[out] attr : PAttribute to be parsed
* @return true on success, false otherwise
*/
bool ConfigParser::parseAttribute(PAttribute & attr){
if(p_parser->isMatchRewind("}")){return true;}
std::string type("");
if(p_parser->isMatch("Tensor")){
if(!p_parser->isMatch("(")){
errorAt();
std::cerr << "ConfigParser::parseAttribute : expect '(' after token 'Tensor'" << std::endl;
return true;
}
type = p_parser->getNextToken();
if(!p_parser->isMatch(",")){
errorAt();
std::cerr << "ConfigParser::parseAttribute : expect ',' after type '"<<type<<"'" << std::endl;
return true;
}
while(!p_parser->isMatch(")") && !p_parser->isEndOfFile() && p_run){
PDimention dim;
if(parseDimention(dim)){
attr.getVecDim().push_back(dim);
if(!p_parser->isMatch(",") && !p_parser->isMatchRewind(")")){
errorAt();
std::cerr << "ConfigParser::parseAttribute : expect ',' after dimention '"<<ph5_dimentionToStr(dim)<<"'" << std::endl;
return true;
}
}else{
errorAt();
std::cerr << "ConfigParser::parseAttribute : cannot parse expected dimention" << std::endl;
return true;
}
}
}else{
type = p_parser->getNextToken();
}
std::string varName(p_parser->getNextToken());
std::string colName(varName);
if(p_parser->isMatch("[")){
colName = p_parser->getUntilKeyWithoutPatern("]");
}
if(!p_parser->isMatch(";")){
errorAt();
std::cerr << "ConfigParser::parseAttribute : expect ';' at the end of the definition of the '"<<varName<<"' attribute" << std::endl;
return true;
}
attr.setName(varName);
attr.setColName(colName);
return true;
}
///Parse a PDimention
/** @param[out] dim : PDimention to be parsed
* @return true on success, false otherwise
*/
bool ConfigParser::parseDimention(PDimention & dim){
if(p_parser->isMatchRewind(")")){return true;}
std::string token = p_parser->getNextToken();
bool isNumber = isStrNumber(token);
dim.setIsValue(isNumber);
if(isNumber){
dim.setValue(atol(token.c_str()));
}else{
dim.setName(token);
}
return true;
}
......@@ -7,10 +7,11 @@
#ifndef __CONFIGPARSER_H__
#define __CONFIGPARSER_H__
#include "PMultiFileParser.h"
#include "Representation/representation.h"
///@brief Parse the configuration a create the vector of sources
class ConfigParser{
class ConfigParser : public PMultiFileParser{
public:
ConfigParser();
ConfigParser(const ConfigParser & other);
......@@ -22,9 +23,17 @@ class ConfigParser{
protected:
void copyConfigParser(const ConfigParser & other);
virtual bool parseFile();
virtual void preLoadFile();
virtual void postLoadFile();
private:
void initialisationConfigParser();
bool parseTable(PTable & table);
bool parseAttribute(PAttribute & attr);
bool parseDimention(PDimention & dim);
///Vector of parsed PSource
PVecSource p_vecSource;
};
......
......@@ -4,10 +4,19 @@
Licence : CeCILL-C
****************************************/
#include "convertToString.h"
#include "representation.h"
///Convert a PDimention into a string
/** @param dim : dimention to be converted
* @return corresponding string
*/
std::string ph5_dimentionToStr(const PDimention & dim){
if(dim.getIsValue()){
return convertToString(dim.getValue());
}else{
return dim.getName();
}
}
......@@ -14,6 +14,6 @@ typedef std::vector<PAttribute> PVecAttribute;
typedef std::vector<PTable> PVecTable;
typedef std::vector<PSource> PVecSource;
std::string ph5_dimentionToStr(const PDimention & dim);
#endif
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment