PhoenixFileParser  1.0.0
Set of tools to ease file parsing
Loading...
Searching...
No Matches
PMultiFileParser Class Referenceabstract

Class used to parse file with possibility of inclusion. More...

#include <PMultiFileParser.h>

+ Collaboration diagram for PMultiFileParser:

Public Member Functions

void addCommentConfig (const PMultiCommentConfig &commentConfig)
 Adds a comment config for the parser.
 
void addCommentConfig (const PString &beginStringComment, const PString &endStringComment="\n")
 Adds a comment config for the parser.
 
bool checkExpectedMatch (const PString &tokenExpected, const PString &tokenBefore="")
 Check if the tokenExpected match.
 
bool checkExpectedToken (const PString &tokenExpected, const PString &tokenBefore="")
 Check if the p_currentToken == tokenExpected.
 
void clearComment ()
 Clear comment.
 
void errorAt ()
 Write a parsing error.
 
bool fullParsing ()
 Perform the full parsing pf data.
 
PFileParsergetCurrentParser ()
 Gets the current parser.
 
void getCurrentTokenWithoutComment ()
 Get the current token and skip the comment.
 
PString & getLastComment ()
 Get the last comment.
 
const PString & getLastComment () const
 Get the last comment.
 
PString getStrComposedOf (const PString &charset)
 Get the string composed of charset charcters.
 
bool isMatch (const PString &patern, const PString &forbiddenCharBefore)
 Says if the patern match with the current caracters of the PFileParser.
 
bool isMatch (const PString &token)
 Check if the given token matches the current read file.
 
PString isMatch (const PVecString &vecToken)
 Check if the one entry of the vector of token matches.
 
bool isMatchRewind (const PString &token)
 Check if the given token matches the current read file and goes back even if the token matches.
 
bool isMatchSeq (const PVecString &patern, bool alwaysPopBack=false)
 Match a sequence of token in a vector.
 
PString isMatchToken (const PVecString &vecToken)
 Check the matching between the current caracters and all the string in the vector but treats the string as a token (cannot be part of a word)
 
bool load (const PPath &configFile)
 Load the PMultiFileParser with the configFile.
 
 PMultiFileParser (const PPath &inputDirectory=PPath(), const PPath &outputDirectory=PPath())
 Default constructeur of PMultiFileParser.
 
void pointAtRow ()
 Point the problem.
 
void setFileContent (const PString &fileContent)
 Set the file content to be parsed.
 
void skipComment ()
 Skip comment.
 
void stopParsing ()
 Stop the parsing of all the files.
 
void unexpectedToken ()
 Print unexpected token error.
 
virtual ~PMultiFileParser ()
 Destructeur of PMultiFileParser.
 

Protected Member Functions

virtual bool parseFile ()=0
 
virtual void postLoadFile ()
 Post load file.
 
virtual void preLoadFile ()
 Pre load file.
 

Protected Attributes

PString p_currentToken
 CurrentToken parsed.
 
PPath p_inputDirectory
 Input directory of the parser.
 
PString p_lastComment
 last comment
 
std::list< PFileParserp_listFileParser
 List of all the parsers for all the loaded files.
 
PPath p_outputDirectory
 Output directory of the parser.
 
PFileParserp_parser
 Parser helper for the config file.
 
bool p_run
 Run the parsing if true.
 

Private Member Functions

void initialisationPMultiFileParser (const PPath &inputDirectory, const PPath &outputDirectory)
 Initialisation function of the class PMultiFileParser.
 

Private Attributes

PListMultiCommentConfig p_listCommentConfig
 Defines the differents comments we allow in the parsing.
 

Detailed Description

Class used to parse file with possibility of inclusion.

Definition at line 28 of file PMultiFileParser.h.

Constructor & Destructor Documentation

◆ PMultiFileParser()

PMultiFileParser::PMultiFileParser ( const PPath & inputDirectory = PPath(),
const PPath & outputDirectory = PPath() )

Default constructeur of PMultiFileParser.

Parameters
inputDirectory: input directory of the PMultiFileParser
outputDirectory: output directory of the PMultiFileParser

Definition at line 14 of file PMultiFileParser.cpp.

14 {
15 initialisationPMultiFileParser(inputDirectory, outputDirectory);
16}
void initialisationPMultiFileParser(const PPath &inputDirectory, const PPath &outputDirectory)
Initialisation function of the class PMultiFileParser.

References initialisationPMultiFileParser().

+ Here is the call graph for this function:

◆ ~PMultiFileParser()

PMultiFileParser::~PMultiFileParser ( )
virtual

Destructeur of PMultiFileParser.

Definition at line 19 of file PMultiFileParser.cpp.

19 {
20
21}

Member Function Documentation

◆ addCommentConfig() [1/2]

void PMultiFileParser::addCommentConfig ( const PMultiCommentConfig & commentConfig)

Adds a comment config for the parser.

Parameters
commentConfig: comment config for the PMultiFileParser

Definition at line 84 of file PMultiFileParser.cpp.

84 {
85 p_listCommentConfig.push_back(commentConfig);
86}
PListMultiCommentConfig p_listCommentConfig
Defines the differents comments we allow in the parsing.

References p_listCommentConfig.

◆ addCommentConfig() [2/2]

void PMultiFileParser::addCommentConfig ( const PString & beginStringComment,
const PString & endStringComment = "\n" )

Adds a comment config for the parser.

Parameters
beginStringComment: string which defines the begining of a comment
endStringComment: string which defines the ending of a comment

Definition at line 92 of file PMultiFileParser.cpp.

92 {
93 p_listCommentConfig.push_back(PMultiCommentConfig(beginStringComment, endStringComment));
94}
std::pair< PString, PString > PMultiCommentConfig
Describes a comment config.

References p_listCommentConfig.

◆ checkExpectedMatch()

bool PMultiFileParser::checkExpectedMatch ( const PString & tokenExpected,
const PString & tokenBefore = "" )

Check if the tokenExpected match.

Parameters
tokenExpected: token we expect
tokenBefore: token before the exprected one
Returns
true if the p_currentToken == tokenExpected, false otherwise with an error message

Definition at line 166 of file PMultiFileParser.cpp.

166 {
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}
void errorAt()
Write a parsing error.
PFileParser * p_parser
Parser helper for the config file.
void stopParsing()
Stop the parsing of all the files.

References errorAt(), p_parser, and stopParsing().

+ Here is the call graph for this function:

◆ checkExpectedToken()

bool PMultiFileParser::checkExpectedToken ( const PString & tokenExpected,
const PString & tokenBefore = "" )

Check if the p_currentToken == tokenExpected.

Parameters
tokenExpected: token we expect
tokenBefore: token before the exprected one
Returns
true if the p_currentToken == tokenExpected, false otherwise with an error message

Definition at line 151 of file PMultiFileParser.cpp.

151 {
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}
PString p_currentToken
CurrentToken parsed.

References errorAt(), p_currentToken, and stopParsing().

+ Here is the call graph for this function:

◆ clearComment()

void PMultiFileParser::clearComment ( )

Clear comment.

Definition at line 193 of file PMultiFileParser.cpp.

193 {
194 p_lastComment = "";
195}
PString p_lastComment
last comment

References p_lastComment.

Referenced by initialisationPMultiFileParser().

+ Here is the caller graph for this function:

◆ errorAt()

void PMultiFileParser::errorAt ( )

Write a parsing error.

Definition at line 126 of file PMultiFileParser.cpp.

126 {
127 std::cerr << "\033[31mError at " << p_parser->getLocation() << " :\033[0m" << std::endl;
128}

References p_parser.

Referenced by checkExpectedMatch(), checkExpectedToken(), and unexpectedToken().

+ Here is the caller graph for this function:

◆ fullParsing()

bool PMultiFileParser::fullParsing ( )

Perform the full parsing pf data.

Returns
true on success, false otherwise

Definition at line 57 of file PMultiFileParser.cpp.

57 {
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 }
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}
virtual void postLoadFile()
Post load file.
virtual bool parseFile()=0
virtual void preLoadFile()
Pre load file.
std::list< PFileParser > p_listFileParser
List of all the parsers for all the loaded files.
void unexpectedToken()
Print unexpected token error.
void pointAtRow()
Point the problem.
bool p_run
Run the parsing if true.

References p_listFileParser, p_parser, p_run, parseFile(), pointAtRow(), postLoadFile(), preLoadFile(), and unexpectedToken().

Referenced by load().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getCurrentParser()

PFileParser * PMultiFileParser::getCurrentParser ( )

Gets the current parser.

Returns
pointer to the current parser

Definition at line 303 of file PMultiFileParser.cpp.

303 {
304 return p_parser;
305}

References p_parser.

◆ getCurrentTokenWithoutComment()

void PMultiFileParser::getCurrentTokenWithoutComment ( )

Get the current token and skip the comment.

Definition at line 280 of file PMultiFileParser.cpp.

280 {
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 }
297 p_currentToken = p_parser->getNextToken();
298}

References p_currentToken, p_lastComment, p_listCommentConfig, p_parser, and p_run.

◆ getLastComment() [1/2]

PString & PMultiFileParser::getLastComment ( )

Get the last comment.

Returns
last comment

Definition at line 106 of file PMultiFileParser.cpp.

106 {
107 return p_lastComment;
108}

References p_lastComment.

◆ getLastComment() [2/2]

const PString & PMultiFileParser::getLastComment ( ) const

Get the last comment.

Returns
last comment

Definition at line 99 of file PMultiFileParser.cpp.

99 {
100 return p_lastComment;
101}

References p_lastComment.

◆ getStrComposedOf()

PString PMultiFileParser::getStrComposedOf ( const PString & charset)

Get the string composed of charset charcters.

Parameters
charset: set of allowed characters
Returns
corresponding string composed of characters in the given charset

Definition at line 272 of file PMultiFileParser.cpp.

272 {
273 //Remove comments
274 skipComment();
275 //Check if the token matches
276 return p_parser->getStrComposedOf(charset);
277}
void skipComment()
Skip comment.

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ initialisationPMultiFileParser()

void PMultiFileParser::initialisationPMultiFileParser ( const PPath & inputDirectory,
const PPath & outputDirectory )
private

Initialisation function of the class PMultiFileParser.

Parameters
inputDirectory: input directory of the PMultiFileParser
outputDirectory: output directory of the PMultiFileParser

Definition at line 311 of file PMultiFileParser.cpp.

311 {
312 p_run = true;
313 p_inputDirectory = inputDirectory;
314 p_outputDirectory = outputDirectory;
315 p_currentToken = "";
316 clearComment();
317 p_parser = NULL;
318}
PPath p_inputDirectory
Input directory of the parser.
void clearComment()
Clear comment.
PPath p_outputDirectory
Output directory of the parser.

References clearComment(), p_currentToken, p_inputDirectory, p_outputDirectory, p_parser, and p_run.

Referenced by PMultiFileParser().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ isMatch() [1/3]

bool PMultiFileParser::isMatch ( const PString & patern,
const PString & forbiddenCharBefore )

Says if the patern match with the current caracters of the PFileParser.

Parameters
patern: patern we want to check (this patern should not begin with white caracters)
forbiddenCharBefore: lisr of characters which cannot be just before the first character of the patern
Returns
true if the patern match, false otherwise If the patern match, the current char will be in the next char of the patern

Definition at line 239 of file PMultiFileParser.cpp.

239 {
240 //Remove comments
241 skipComment();
242 //Check if the token matches
243 return p_parser->isMatch(patern, forbiddenCharBefore);
244}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ isMatch() [2/3]

bool PMultiFileParser::isMatch ( const PString & token)

Check if the given token matches the current read file.

Parameters
token: token to be checked
Returns
true on success, false otherwise This function isMatch takes account the comments

Definition at line 202 of file PMultiFileParser.cpp.

202 {
203 //Remove comments
204 skipComment();
205 //Check if the token matches
206 return p_parser->isMatch(token);
207}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ isMatch() [3/3]

PString PMultiFileParser::isMatch ( const PVecString & vecToken)

Check if the one entry of the vector of token matches.

Parameters
vecToken: vector of token
Returns
matched string, or empty string if there is no match

Definition at line 250 of file PMultiFileParser.cpp.

250 {
251 //Remove comments
252 skipComment();
253 //Check if the token matches
254 return p_parser->isMatch(vecToken);
255}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ isMatchRewind()

bool PMultiFileParser::isMatchRewind ( const PString & token)

Check if the given token matches the current read file and goes back even if the token matches.

Parameters
token: token to be checked
Returns
true on success, false otherwise This function isMatch takes account the comments

Definition at line 214 of file PMultiFileParser.cpp.

214 {
215 //Remove comments
216 skipComment();
217 //Check if the token matches
218 return p_parser->isMatchRewind(token);
219}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ isMatchSeq()

bool PMultiFileParser::isMatchSeq ( const PVecString & patern,
bool alwaysPopBack = false )

Match a sequence of token in a vector.

Parameters
patern: set of token to match in this order and totally
alwaysPopBack: true to make the PFileParser at the exact same place before the check even is the sequence matches
Returns
true if the full sequence matches, false otherwise

Definition at line 226 of file PMultiFileParser.cpp.

226 {
227 //Remove comments
228 skipComment();
229 //Check if the token matches
230 return p_parser->isMatchSeq(patern, alwaysPopBack);
231}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ isMatchToken()

PString PMultiFileParser::isMatchToken ( const PVecString & vecToken)

Check the matching between the current caracters and all the string in the vector but treats the string as a token (cannot be part of a word)

Parameters
vecToken: vector of token
Returns
matched string, or empty string if there is no match

Definition at line 261 of file PMultiFileParser.cpp.

261 {
262 //Remove comments
263 skipComment();
264 //Check if the token matches
265 return p_parser->isMatchToken(vecToken);
266}

References p_parser, and skipComment().

+ Here is the call graph for this function:

◆ load()

bool PMultiFileParser::load ( const PPath & configFile)

Load the PMultiFileParser with the configFile.

Parameters
configFile: file name of the cnofiguration file
Returns
true on success, false otherwise

Definition at line 27 of file PMultiFileParser.cpp.

27 {
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}
#define MULTI_PARSER_SEPARATORS_STRING
bool fullParsing()
Perform the full parsing pf data.

References fullParsing(), MULTI_PARSER_SEPARATORS_STRING, p_listFileParser, and p_parser.

+ Here is the call graph for this function:

◆ parseFile()

virtual bool PMultiFileParser::parseFile ( )
protectedpure virtual

Referenced by fullParsing().

+ Here is the caller graph for this function:

◆ pointAtRow()

void PMultiFileParser::pointAtRow ( )

Point the problem.

Definition at line 138 of file PMultiFileParser.cpp.

138 {
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}

References p_parser.

Referenced by fullParsing().

+ Here is the caller graph for this function:

◆ postLoadFile()

void PMultiFileParser::postLoadFile ( )
protectedvirtual

Post load file.

Definition at line 116 of file PMultiFileParser.cpp.

116 {
117
118}

Referenced by fullParsing().

+ Here is the caller graph for this function:

◆ preLoadFile()

void PMultiFileParser::preLoadFile ( )
protectedvirtual

Pre load file.

Definition at line 111 of file PMultiFileParser.cpp.

111 {
112
113}

Referenced by fullParsing().

+ Here is the caller graph for this function:

◆ setFileContent()

void PMultiFileParser::setFileContent ( const PString & fileContent)

Set the file content to be parsed.

Parameters
fileContent: file content to be parsed

Definition at line 43 of file PMultiFileParser.cpp.

43 {
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}

References MULTI_PARSER_SEPARATORS_STRING, p_listFileParser, and p_parser.

◆ skipComment()

void PMultiFileParser::skipComment ( )

Skip comment.

Definition at line 177 of file PMultiFileParser.cpp.

177 {
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}

References p_lastComment, p_listCommentConfig, p_parser, and p_run.

Referenced by getStrComposedOf(), isMatch(), isMatch(), isMatch(), isMatchRewind(), isMatchSeq(), and isMatchToken().

+ Here is the caller graph for this function:

◆ stopParsing()

void PMultiFileParser::stopParsing ( )

Stop the parsing of all the files.

Definition at line 121 of file PMultiFileParser.cpp.

121 {
122 p_run = false;
123}

References p_run.

Referenced by checkExpectedMatch(), checkExpectedToken(), and unexpectedToken().

+ Here is the caller graph for this function:

◆ unexpectedToken()

void PMultiFileParser::unexpectedToken ( )

Print unexpected token error.

Definition at line 131 of file PMultiFileParser.cpp.

131 {
132 errorAt();
133 std::cerr << "PMultiFileParser::parseFile : unexpected token '"<<p_parser->getNextToken()<<"'" << std::endl;
134 stopParsing();
135}

References errorAt(), p_parser, and stopParsing().

Referenced by fullParsing().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

Member Data Documentation

◆ p_currentToken

PString PMultiFileParser::p_currentToken
protected

CurrentToken parsed.

Definition at line 78 of file PMultiFileParser.h.

Referenced by checkExpectedToken(), getCurrentTokenWithoutComment(), and initialisationPMultiFileParser().

◆ p_inputDirectory

PPath PMultiFileParser::p_inputDirectory
protected

Input directory of the parser.

Definition at line 80 of file PMultiFileParser.h.

Referenced by initialisationPMultiFileParser().

◆ p_lastComment

PString PMultiFileParser::p_lastComment
protected

◆ p_listCommentConfig

PListMultiCommentConfig PMultiFileParser::p_listCommentConfig
private

Defines the differents comments we allow in the parsing.

Definition at line 88 of file PMultiFileParser.h.

Referenced by addCommentConfig(), addCommentConfig(), getCurrentTokenWithoutComment(), and skipComment().

◆ p_listFileParser

std::list<PFileParser> PMultiFileParser::p_listFileParser
protected

List of all the parsers for all the loaded files.

Definition at line 72 of file PMultiFileParser.h.

Referenced by fullParsing(), load(), and setFileContent().

◆ p_outputDirectory

PPath PMultiFileParser::p_outputDirectory
protected

Output directory of the parser.

Definition at line 82 of file PMultiFileParser.h.

Referenced by initialisationPMultiFileParser().

◆ p_parser

◆ p_run

bool PMultiFileParser::p_run
protected

Run the parsing if true.

Definition at line 74 of file PMultiFileParser.h.

Referenced by fullParsing(), getCurrentTokenWithoutComment(), initialisationPMultiFileParser(), skipComment(), and stopParsing().


The documentation for this class was generated from the following files: