PhoenixPresentation  2.0.0
Set of cmake function to automate presentation generation
PMultiFileParser.cpp
Go to the documentation of this file.
1 /***************************************
2  Auteur : Pierre Aubert
3  Mail : pierre.aubert@lapp.in2p3.fr
4  Licence : CeCILL-C
5 ****************************************/
6 
7 #include "PMultiFileParser.h"
8 
9 
11 
14 PMultiFileParser::PMultiFileParser(const PPath & inputDirectory, const PPath & outputDirectory){
15  initialisationPMultiFileParser(inputDirectory, outputDirectory);
16 }
17 
20 
21 }
22 
24 
27 bool PMultiFileParser::load(const PPath & configFile){
28  if(configFile == "") return false;
29  PFileParser parser;
30  p_listFileParser.push_back(parser);
32  p_parser = &p_listFileParser.back();
33  if(!p_parser->open(configFile)){
34  std::cerr << "PMultiFileParser::load : can't open file '" << configFile << "'" << std::endl;
35  return false;
36  }
37  return fullParsing();
38 }
39 
41 
43 void PMultiFileParser::setFileContent(const PString & fileContent){
44  if(p_parser == NULL){
45  PFileParser parser;
46  p_listFileParser.push_back(parser);
48  p_parser = &p_listFileParser.back();
49  }
50  p_parser = &p_listFileParser.back();
51  p_parser->setFileContent(fileContent);
52 }
53 
55 
58  if(p_parser == NULL){
59  std::cerr << "PMultiFileParser::fullParsing : the parser is not initialised, please call PMultiFileParser::load or PMultiFileParser::setFileContent before this function" << std::endl;
60  return false;
61  }
62  preLoadFile();
63  bool isParseGood(true);
64  while(!p_parser->isEndOfFile() && isParseGood && p_run){
65  long unsigned int currentPos = p_parser->getCurrentCharIdx();
66  isParseGood = parseFile();
67  if(currentPos == p_parser->getCurrentCharIdx() && !p_parser->isEndOfFile()){
68  std::cerr << "PMultiFileParser::fullParsing : the parser is stucked at the position :" << std::endl << "\t" << p_parser->getLocation() << std::endl;
70  pointAtRow();
71  p_run = false;
72  }
73  }
74  if(p_run) postLoadFile();
75  p_listFileParser.pop_back();
76  if(p_listFileParser.size() > 0lu) p_parser = &p_listFileParser.back();
77  else p_parser = NULL;
78  return p_run;
79 }
80 
82 
85  p_listCommentConfig.push_back(commentConfig);
86 }
87 
89 
92 void PMultiFileParser::addCommentConfig(const PString & beginStringComment, const PString & endStringComment){
93  p_listCommentConfig.push_back(PMultiCommentConfig(beginStringComment, endStringComment));
94 }
95 
97 
100  return p_lastComment;
101 }
102 
104 
107  return p_lastComment;
108 }
109 
112 
113 }
114 
117 
118 }
119 
122  p_run = false;
123 }
124 
127  std::cerr << "\033[31mError at " << p_parser->getLocation() << " :\033[0m" << std::endl;
128 }
129 
132  errorAt();
133  std::cerr << "PMultiFileParser::parseFile : unexpected token '"<<p_parser->getNextToken()<<"'" << std::endl;
134  stopParsing();
135 }
136 
139  std::cerr << "\tAt row :\n" << p_parser->getCurrentRow() << std::endl;
140  for(size_t i(0lu); i < p_parser->getColumn(); ++i){
141  std::cerr << " ";
142  }
143  std::cerr << "^" << std::endl;
144 }
145 
147 
151 bool PMultiFileParser::checkExpectedToken(const PString & tokenExpected, const PString & tokenBefore){
152  if(tokenExpected == p_currentToken) return true;
153  errorAt();
154  std::cerr << "Unexpected token '"<<p_currentToken<<"'" << std::endl;
155  std::cerr << "Expected token '"<<tokenExpected<<"'" << std::endl;
156  if(tokenBefore != "") std::cerr << " after " << tokenBefore << std::endl;
157  stopParsing();
158  return false;
159 }
160 
162 
166 bool PMultiFileParser::checkExpectedMatch(const PString & tokenExpected, const PString & tokenBefore){
167  if(p_parser->isMatch(tokenExpected)) return true;
168  errorAt();
169  std::cerr << "Unexpected token '"<<p_parser->getNextToken()<<"'" << std::endl;
170  std::cerr << "Expected token '"<<tokenExpected<<"'" << std::endl;
171  if(tokenBefore != "") std::cerr << " after " << tokenBefore << std::endl;
172  stopParsing();
173  return false;
174 }
175 
178  bool isCommentFound(false);
179  do{
180  isCommentFound = false;
181  PListMultiCommentConfig::iterator it(p_listCommentConfig.begin());
182  while(it != p_listCommentConfig.end() && !isCommentFound && p_run && !p_parser->isEndOfFile()){
183  if(p_parser->isMatch(it->first)){
184  p_lastComment += it->first + p_parser->getUntilKey(it->second);
185  isCommentFound = true;
186  }
187  ++it;
188  }
189  }while(isCommentFound && p_run && !p_parser->isEndOfFile());
190 }
191 
194  p_lastComment = "";
195 }
196 
198 
202 bool PMultiFileParser::isMatch(const PString & token){
203  //Remove comments
204  skipComment();
205  //Check if the token matches
206  return p_parser->isMatch(token);
207 }
208 
210 
215  //Remove comments
216  skipComment();
217  //Check if the token matches
218  return p_parser->isMatchRewind(token);
219 }
220 
222 
226 bool PMultiFileParser::isMatchSeq(const PVecString & patern, bool alwaysPopBack){
227  //Remove comments
228  skipComment();
229  //Check if the token matches
230  return p_parser->isMatchSeq(patern, alwaysPopBack);
231 }
232 
234 
239 bool PMultiFileParser::isMatch(const PString & patern, const PString & forbiddenCharBefore){
240  //Remove comments
241  skipComment();
242  //Check if the token matches
243  return p_parser->isMatch(patern, forbiddenCharBefore);
244 }
245 
247 
251  //Remove comments
252  skipComment();
253  //Check if the token matches
254  return p_parser->isMatch(vecToken);
255 }
256 
258 
262  //Remove comments
263  skipComment();
264  //Check if the token matches
265  return p_parser->isMatchToken(vecToken);
266 }
267 
269 
273  //Remove comments
274  skipComment();
275  //Check if the token matches
276  return p_parser->getStrComposedOf(charset);
277 }
278 
281  if(!p_run) return;
282  p_lastComment = "";
283  if(p_listCommentConfig.size() != 0lu){
284  bool currentTokenIsComment(true);
285  while(currentTokenIsComment && p_run && !p_parser->isEndOfFile()){
286  PListMultiCommentConfig::iterator it(p_listCommentConfig.begin());
287  currentTokenIsComment = false;
288  while(it != p_listCommentConfig.end() && !currentTokenIsComment){
289  if(p_parser->isMatch(it->first)){
290  p_lastComment = p_parser->getUntilKey(it->second);
291  currentTokenIsComment = true;
292  }
293  ++it;
294  }
295  }
296  }
298 }
299 
301 
304  return p_parser;
305 }
306 
308 
311 void PMultiFileParser::initialisationPMultiFileParser(const PPath & inputDirectory, const PPath & outputDirectory){
312  p_run = true;
313  p_inputDirectory = inputDirectory;
314  p_outputDirectory = outputDirectory;
315  p_currentToken = "";
316  clearComment();
317  p_parser = NULL;
318 }
319 
320 
321 
322 
323 
std::pair< PString, PString > PMultiCommentConfig
Describes a comment config.
#define MULTI_PARSER_SEPARATORS_STRING
std::vector< PString > PVecString
Definition: PString.h:96
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
bool isMatchSeq(const PVecString &patern, bool alwaysPopBack=false)
Match a sequence of token in a vector.
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
PString getCurrentRow() const
Get the current parsed row.
size_t getColumn() const
Fonction qui renvoie le numéro de la colonne du caractère courant.
PString getNextToken()
Get the next token.
PString getStrComposedOf(const PString &charset)
Get string composed of the characters in the string charset.
bool isMatchRewind(const PString &patern)
Do a isMatch and then go back at the previous position.
bool isMatchToken(const PString &patern)
Says if the patern match with the current caracters of the PFileParser but treats the string as a tok...
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
PString getUntilKey(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern comprise.
PLocation getLocation() const
Fonction qui renvoie la PLocation du PFileParser.
void setFileContent(const PString &fileContent)
Set the file content.
Definition: PFileParser.cpp:50
size_t getCurrentCharIdx() const
Return the index of the current character.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
void setFileContent(const PString &fileContent)
Set the file content to be parsed.
bool isMatchSeq(const PVecString &patern, bool alwaysPopBack=false)
Match a sequence of token in a vector.
virtual ~PMultiFileParser()
Destructeur of PMultiFileParser.
void getCurrentTokenWithoutComment()
Get the current token and skip the comment.
const PString & getLastComment() const
Get the last comment.
virtual void postLoadFile()
Post load file.
PMultiFileParser(const PPath &inputDirectory=PPath(), const PPath &outputDirectory=PPath())
Default constructeur of PMultiFileParser.
PString isMatchToken(const PVecString &vecToken)
Check the matching between the current caracters and all the string in the vector but treats the stri...
PPath p_inputDirectory
Input directory of the parser.
bool load(const PPath &configFile)
Load the PMultiFileParser with the configFile.
virtual bool parseFile()=0
void clearComment()
Clear comment.
PString getStrComposedOf(const PString &charset)
Get the string composed of charset charcters.
virtual void preLoadFile()
Pre load file.
std::list< PFileParser > p_listFileParser
List of all the parsers for all the loaded files.
bool isMatch(const PString &token)
Check if the given token matches the current read file.
void errorAt()
Write a parsing error.
void initialisationPMultiFileParser(const PPath &inputDirectory, const PPath &outputDirectory)
Initialisation function of the class PMultiFileParser.
void unexpectedToken()
Print unexpected token error.
PString p_lastComment
last comment
void pointAtRow()
Point the problem.
bool checkExpectedToken(const PString &tokenExpected, const PString &tokenBefore="")
Check if the p_currentToken == tokenExpected.
PFileParser * p_parser
Parser helper for the config file.
bool fullParsing()
Perform the full parsing pf data.
bool p_run
Run the parsing if true.
void addCommentConfig(const PMultiCommentConfig &commentConfig)
Adds a comment config for the parser.
bool isMatchRewind(const PString &token)
Check if the given token matches the current read file and goes back even if the token matches.
PListMultiCommentConfig p_listCommentConfig
Defines the differents comments we allow in the parsing.
void stopParsing()
Stop the parsing of all the files.
bool checkExpectedMatch(const PString &tokenExpected, const PString &tokenBefore="")
Check if the tokenExpected match.
PFileParser * getCurrentParser()
Gets the current parser.
PString p_currentToken
CurrentToken parsed.
PPath p_outputDirectory
Output directory of the parser.
void skipComment()
Skip comment.
Path of a directory or a file.
Definition: PPath.h:17
Extends the std::string.
Definition: PString.h:16