PhoenixGenerator  2.0.0
Set of tools to generate code
main.cpp
Go to the documentation of this file.
1 
2 /***************************************
3  Auteur : Pierre Aubert
4  Mail : pierre.aubert@lapp.in2p3.fr
5  Licence : CeCILL-C
6 ****************************************/
7 
8 #include "phoenix_assert.h"
9 #include "phoenix_check.h"
10 #include "PFileParser.h"
11 
14  PFileParser parser;
15  parser.setFileContent("Les GPUs\\dots{} pour quoi faire ?}\\label{chapGPU}\n");
16 
17  PString res(parser.getUntilKeyWithoutPatern("}"));
18  phoenix_assert(phoenix_check("testPFileParserPartern", res, "Les GPUs\\dots{"));
19 }
20 
23  PFileParser parser;
24  parser.setFileContent("Les GPUs\\dots{} pour quoi faire ?}\\label{chapGPU}\n");
25 
26  PString res(parser.getUntilKeyWithoutPaternExclude("}", "{"));
27  phoenix_assert(phoenix_check("testPFileParserParternExclude", res, "Les GPUs\\dots{} pour quoi faire ?"));
28 }
29 
32  PFileParser parser;
33  parser.setFileContent("Les GPUs\\dots{} pour quoi faire ?}\\label{chapGPU}\n");
34 
35  PString res(parser.getUntilKeyWithoutPaternRecurse("}", "{"));
36  phoenix_assert(phoenix_check("testPFileParserParternRecurse", res, "Les GPUs\\dots{} pour quoi faire ?"));
37 }
38 
41  PFileParser parser;
42  parser.setFileContent("des trucs \"a dire\" et d'autres chose");
43 
44  PString res(parser.getUntilKeyWithoutPaternRecurse("\"", "\""));
45  phoenix_assert(phoenix_check("testPFileParserParternRecurse2", res, "des trucs "));
46 }
47 
50  PFileParser parser;
51  parser.setFileContent("Les GPUs\\dots{} pour quoi faire ?}\\label{chapGPU}\n");
52 
53  PString res(parser.getUntilKeyWithoutPaternRecurse("}", "{", "} \\abcdefhijklmnopqrstuvwxyz"));
54  phoenix_assert(phoenix_check("testPFileParserParternRecurseAllowChar", res, "Les GPUs\\dots{} pour quoi faire ?"));
55 }
56 
59  PFileParser parser;
60  parser.setFileContent("des trucs \\\"a\\\" dire\" et d'autres chose");
61 
62  PString res(parser.getUntilKeyWithoutPaternRecurseExclude("\"", "\"", "\\"));
63  phoenix_assert(phoenix_check("testPFileParserParternRecurseExcludeEnd", res, "des trucs \\\"a\\\" dire"));
64 }
65 
68  PFileParser parser;
69  parser.setFileContent("des trucs \"a dire\" et d'autres chose");
70 
71  PString res(parser.getUntilKeyWithoutPaternRecurseExclude("\"", "\"", "\\"));
72  phoenix_assert(phoenix_check("testPFileParserParternRecurseExcludeEnd2", res, "des trucs "));
73 }
74 
77  PParseSeq seq;
78  PParseStep stepBegin;
79  stepBegin.setIsOptional(false);
80  PParseCmd cmdBegin;
81  cmdBegin.setIsMatch(true);
82  cmdBegin.setStr("\\begin");
83  stepBegin.getVecCmd().push_back(cmdBegin);
84  seq.getVecStep().push_back(stepBegin);
85  PParseStep stepOpenBrace;
86  stepOpenBrace.setIsOptional(false);
87  PParseCmd cmdOpenBrace;
88  cmdOpenBrace.setIsMatch(true);
89  cmdOpenBrace.setStr("{");
90  stepOpenBrace.getVecCmd().push_back(cmdOpenBrace);
91  seq.getVecStep().push_back(stepOpenBrace);
92  PParseStep stepEnvName;
93  stepEnvName.setIsOptional(false);
94  PParseCmd cmdEnvName;
95  cmdEnvName.setIsMatch(true);
96  cmdEnvName.setStr("envName");
97  stepEnvName.getVecCmd().push_back(cmdEnvName);
98  seq.getVecStep().push_back(stepEnvName);
99  PParseStep stepEndBrace;
100  stepEndBrace.setIsOptional(false);
101  PParseCmd cmdEndBrace;
102  cmdEndBrace.setIsMatch(true);
103  cmdEndBrace.setStr("}");
104  stepEndBrace.getVecCmd().push_back(cmdEndBrace);
105  seq.getVecStep().push_back(stepEndBrace);
106 
107  PFileParser parser;
108  parser.setFileContent("\\begin { envName } des trucs\n");
109  PString res = parser.isMatch(seq);
110  phoenix_assert(phoenix_check("testPFileParserSeq", res, "\\begin{envName}"));
111 }
112 
115  PVecString vecPatern;
116  vecPatern.push_back("\\begin");
117  vecPatern.push_back("{");
118  vecPatern.push_back("envName");
119  vecPatern.push_back("}");
120 
121  PFileParser parser;
122  parser.setFileContent("\\begin { envName } des trucs\n");
123  phoenix_assert(parser.isMatchSeq(vecPatern));
124 }
125 
126 
129  PParseSeq seq;
130  PParseStep step;
131  step.setIsOptional(false);
132  PParseCmd cmdBegin;
133  cmdBegin.setIsMatch(true);
134  cmdBegin.setStr("\\begin");
135  step.getVecCmd().push_back(cmdBegin);
136  PParseCmd cmdEnvName;
137  cmdEnvName.setIsMatch(true);
138  cmdEnvName.setStr("envName");
139  step.getVecCmd().push_back(cmdEnvName);
140  PParseCmd cmdEndBrace;
141  cmdEndBrace.setIsMatch(true);
142  cmdEndBrace.setStr("}");
143  step.getVecCmd().push_back(cmdEndBrace);
144  seq.getVecStep().push_back(step);
145 
146  PFileParser parser;
147  parser.setFileContent("\\begin { envName } des trucs\n");
148  PString res = parser.isMatch(seq);
149  phoenix_assert( phoenix_check("testPFileParserSeqOneStep", res, "\\begin"));
150 }
151 
154  PFileParser parser;
155  parser.setFileContent(" Les GPUs\\dots{} pour quoi faire ?}\\label{chapGPU}\n");
156  phoenix_assert(!parser.isMatch("Autre"));
157  phoenix_assert(parser.isMatch("Les"));
158  phoenix_assert(parser.isMatch("GPU"));
159 }
160 
162 
167 bool testPFileParserGetUntilKey(const PString & inputStr, const PString & search, const PString & result){
168  bool b(true);
169  PFileParser parser2;
170  parser2.setFileContent(inputStr);
171 
172  PString strFound(parser2.getUntilKey(search));
173 // std::cout << "testPFileParserGetUntilKey : inputStr = '" << inputStr << "', search = '"<<search<<"', strFound = '" << strFound << "', expectedResult = '" << result << "'" << std::endl;
174 
175  b &= phoenix_check("testPFileParserGetUntilKey", strFound, result);
176 // phoenix_functionOk("testPFileParserGetUntilKey", b);
177  return b;
178 }
179 
182  PFileParser parser;
183  parser.setFileContent("hello");
184 
185  phoenix_assert(parser.getCurrentCh() == 'h');
186  phoenix_assert(parser.getNextChar() == 'e');
187  phoenix_assert(parser.getNextChar() == 'l');
188  phoenix_assert(parser.getNextChar() == 'l');
189  phoenix_assert(parser.getNextChar() == 'o');
190 }
191 
194  PFileParser parser;
195  parser.setFileContent("e :e");
196  parser.setSeparator(":");
197  parser.setWhiteSpace(" ");
198  parser.setLocation(PLocation(PPath("fileName"), 42, 23));
199  parser.setLine(24);
200  parser.setColumn(2);
201  parser.popPosition();
202  parser.getEscapeChar();
203  parser.getFileName();
204  parser.clear();
205 
206  phoenix_assert(!parser.isChSpace());
207  phoenix_assert(!parser.isChSeparator());
208  std::cout << "testPFileParserBase : next char '" << parser.getNextChar() << "'" << std::endl;
209  phoenix_assert(parser.isChSpace());
210  std::cout << "testPFileParserBase : next char '" << parser.getNextChar() << "'" << std::endl;
211  phoenix_assert(parser.isChSeparator());
212  std::cout << "testPFileParserBase : next char '" << parser.getNextChar() << "'" << std::endl;
213  phoenix_assert(!parser.isChSpace());
214  phoenix_assert(!parser.isChSeparator());
215 }
216 
219  PFileParser parser;
220  parser.setFileContent("one row\none other row");
221  phoenix_assert(parser.getCurrentRow() == "one row");
222 }
223 
226  PFileParser parser;
227  parser.setFileContent("one row\none other row");
228 
229  std::vector<PString> vecToken;
230  phoenix_assert(parser.isMatch(vecToken) == "");
231  vecToken.push_back("other");
232  phoenix_assert(parser.isMatch(vecToken) == "");
233  vecToken.push_back("one");
234  phoenix_assert(parser.isMatch(vecToken) == "one");
235 }
236 
239  PFileParser parser;
240  parser.setFileContent("one row\none other row");
241 
242  std::vector<PString> vecToken;
243  phoenix_assert(parser.isMatchToken(vecToken) == "");
244  vecToken.push_back("other");
245  phoenix_assert(parser.isMatchToken(vecToken) == "");
246  vecToken.push_back("one");
247  phoenix_assert(parser.isMatchToken(vecToken) == "one");
248 }
249 
252  PFileParser parser;
253  parser.setFileContent("one row\none other row");
254 
255  std::map<PString, int> mapToken;
256  PString matchKey("");
257  int matchValue(0);
258  phoenix_assert(!parser.isMatchToken(matchKey, matchValue, mapToken));
259  mapToken["other"] = 23;
260  phoenix_assert(!parser.isMatchToken(matchKey, matchValue, mapToken));
261 
262  mapToken["one"] = 1;
263  phoenix_assert(parser.isMatchToken(matchKey, matchValue, mapToken));
264  phoenix_assert(matchKey == "one");
265  phoenix_assert(matchValue == 1);
266 }
267 
270  PFileParser parser;
271  parser.setFileContent("one row\none other row");
272 
273  std::vector<std::vector<PString> > patern;
274  phoenix_assert(parser.isMatch(patern) == "");
275  PVecString vecPatern;
276  vecPatern.push_back("other");
277  patern.push_back(vecPatern);
278  phoenix_assert(parser.isMatch(patern) == "");
279  vecPatern.push_back("one");
280  patern.push_back(vecPatern);
281  phoenix_assert(parser.isMatch(patern) == "one");
282  phoenix_assert(parser.isWhiteSpace());
283  phoenix_assert(parser.getSeparator() != "");
284  phoenix_assert(parser.getCurrentCh() == 'r');
285  phoenix_assert(parser.getPrevCh() == ' ');
286  phoenix_assert(parser.getLine() == 1lu);
287  phoenix_assert(parser.getCurrentCharIdx() == 4lu);
288  std::cout << parser << std::endl;
289 }
290 
293  PFileParser parser;
294  parser.setFileContent("e : e");
295  parser.setWhiteSpace(" ");
296  parser.setSeparator(":");
297 
298  PString token1(parser.getNextToken());
299  phoenix_assert(token1 == "e");
300 // std::cout << "testPFileParserGetNextToken : b = " << b << ", token1 = '" << token1 << "'" << std::endl;
301  PString token2(parser.getNextToken());
302  phoenix_assert(token2 == ":");
303 // std::cout << "testPFileParserGetNextToken : b = " << b << ", token2 = '" << token2 << "'" << std::endl;
304  PString token3(parser.getNextToken());
305  phoenix_assert(token3 == "e");
306 // std::cout << "testPFileParserGetNextToken : b = " << b << ", token3 = '" << token3 << "'" << std::endl;
307  phoenix_assert(parser.getNextToken() == "");
308 }
309 
312  PFileParser parser;
313  parser.setFileContent("e : e");
314  parser.setWhiteSpace(" ");
315  parser.setSeparator(":");
316 
317  PString skippedStr("");
318  PString token1(parser.getNextToken(skippedStr));
319  phoenix_assert(token1 == "e");
320 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", token1 = '" << token1 << "'" << std::endl;
321  phoenix_assert(skippedStr == "");
322 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", skippedStr = '" << skippedStr << "'" << std::endl;
323 
324  skippedStr = "";
325  PString token2(parser.getNextToken(skippedStr));
326  phoenix_assert(token2 == ":");
327 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", token2 = '" << token2 << "'" << std::endl;
328  phoenix_assert(skippedStr == " ");
329 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", skippedStr = '" << skippedStr << "'" << std::endl;
330 
331  skippedStr = "";
332  PString token3(parser.getNextToken(skippedStr));
333  phoenix_assert(token3 == "e");
334 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", token3 = '" << token3 << "'" << std::endl;
335  phoenix_assert(skippedStr == " ");
336 // std::cout << "testPFileParserGetNextToken2 : b = " << b << ", skippedStr = '" << skippedStr << "'" << std::endl;
337  phoenix_assert(parser.getNextToken(skippedStr) == "");
338 }
339 
340 int main(int argc, char** argv){
352  testNextChar();
354 
355  phoenix_assert(testPFileParserGetUntilKey("let's test a string", "", ""));
356  phoenix_assert(testPFileParserGetUntilKey("let's test a string", "some", "let's test a stringsome"));
357  phoenix_assert(testPFileParserGetUntilKey("let's test a string", "test", "let's test"));
358 
366  return 0;
367 }
368 
369 
std::vector< PString > PVecString
Definition: PString.h:96
int main(int argc, char **argv)
Definition: main.cpp:19
void testPFileParserIsMatch()
Test the PFileParser.
Definition: main.cpp:153
void testPFileParserParternRecurseExcludeEnd()
Test the PFileParser.
Definition: main.cpp:58
void testPFileParserSeq()
Test the PFileParser.
Definition: main.cpp:76
void testPFileParserBase()
Test the PFileParser.
Definition: main.cpp:193
void testPFileParserSeqOneStep()
Test the PFileParser.
Definition: main.cpp:128
void testPFileParserMapIsMatchToken()
Check the PFileParser.
Definition: main.cpp:251
void testPFileParserGetCurrentRow()
Check the PFileParser.
Definition: main.cpp:218
void testPFileParserMatchSeq()
Test the PFileParser.
Definition: main.cpp:114
void testPFileParserVecVecIsMatch()
Check the PFileParser.
Definition: main.cpp:269
void testPFileParserParternRecurse2()
Test the PFileParser.
Definition: main.cpp:40
void testPFileParserParternExclude()
Test the PFileParser.
Definition: main.cpp:22
void testPFileParserGetNextToken2()
Test the getNextToken method of the PFileParser.
Definition: main.cpp:311
void testNextChar()
Test the PFileParser getNextChar method.
Definition: main.cpp:181
void testPFileParserVecIsMatch()
Check the PFileParser.
Definition: main.cpp:225
void testPFileParserGetNextToken()
Test the getNextToken method of the PFileParser.
Definition: main.cpp:292
void testPFileParserParternRecurse()
Test the PFileParser.
Definition: main.cpp:31
void testPFileParserParternRecurseAllowChar()
Test the PFileParser.
Definition: main.cpp:49
bool testPFileParserGetUntilKey(const PString &inputStr, const PString &search, const PString &result)
Test the PFileParser.
Definition: main.cpp:167
void testPFileParserPartern()
Test the PFileParser.
Definition: main.cpp:13
void testPFileParserVecIsMatchToken()
Check the PFileParser.
Definition: main.cpp:238
void testPFileParserParternRecurseExcludeEnd2()
Test the PFileParser.
Definition: main.cpp:67
classe qui permet de parser des fichiers texte en renvoyant les tokens les uns après les autres
Definition: PFileParser.h:20
void setSeparator(const PString &separator)
Initialise la liste des caractères séparateurs.
Definition: PFileParser.cpp:43
size_t getLine() const
Fonction qui renvoie le numéro de la ligne courante.
bool isWhiteSpace()
Says if the current char is a white space.
bool isMatchSeq(const PVecString &patern, bool alwaysPopBack=false)
Match a sequence of token in a vector.
bool isChSpace() const
Dis si le caractère courant est un caractère blanc.
PString getUntilKeyWithoutPaternExclude(const PString &patern, const PString &strNotBeforeEndPatern)
Parse a string until the patern is found, only if it has not strNotBeforeEndPatern before it.
PString getCurrentRow() const
Get the current parsed row.
PString getNextToken()
Get the next token.
PString getUntilKeyWithoutPatern(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern exclu.
void clear()
Clear the save position of the parser in ther current file.
PString getSeparator() const
renvoie la liste des caractères séparateurs
PString getUntilKeyWithoutPaternRecurse(const PString &patern, const PString &beginPatern, const PString &allowedCharAfterBegin)
Get the string until end sequence and take account recursive patern (embeded strings)
void setLine(size_t currentLine)
Set the current line of the PFileParser.
Definition: PFileParser.cpp:74
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
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.
char getCurrentCh() const
Renvoie le caractère courant.
void popPosition()
Get to the last saved position of the PFileParser in the current file.
Definition: PFileParser.cpp:99
PString getUntilKey(const PString &patern)
Renvoie la chaine de caractère du caractère courant jusqu'à patern comprise.
PPath getFileName() const
Fonction qui renvoie le nom du fichier que l'on a ouvert.
bool isChSeparator() const
Dis si le caractère courant est un séparateur.
void setColumn(size_t currentCol)
Set the current column of the PFileParser.
Definition: PFileParser.cpp:81
void setLocation(const PLocation &location)
Set the current location of the PFileParser.
Definition: PFileParser.cpp:65
void setFileContent(const PString &fileContent)
Set the file content.
Definition: PFileParser.cpp:50
PString getUntilKeyWithoutPaternRecurseExclude(const PString &patern, const PString &beginPatern, const PString &echapExpr)
Get the string until end sequence and take account recursive patern (embeded strings)
char getPrevCh() const
Renvoie le caractère courant.
size_t getCurrentCharIdx() const
Return the index of the current character.
char getEscapeChar() const
Gets the escape character of the PFileParser.
char getNextChar()
Fonction qui renvoie le prochain caractère du fichier courant.
Classe qui permet de décrire une localisation, avec un nom de fichier et une ligne.
Definition: PLocation.h:15
Parser command.
Definition: PParseSeq.h:33
void setIsMatch(bool isMatch)
Set the variable p_isMatch, of type 'bool'.
Definition: PParseSeq.cpp:51
void setStr(const PString &str)
Set the variable p_str, of type 'PString'.
Definition: PParseSeq.cpp:58
Parsing sequence.
Definition: PParseSeq.h:77
const std ::vector< PParseStep > & getVecStep() const
Get the variable p_vecStep.
Definition: PParseSeq.cpp:256
Describes a parsing step.
Definition: PParseSeq.h:55
void setIsOptional(bool isOptional)
Set the variable p_isOptional, of type 'bool'.
Definition: PParseSeq.cpp:150
const std ::vector< PParseCmd > & getVecCmd() const
Get the variable p_vecCmd.
Definition: PParseSeq.cpp:178
Path of a directory or a file.
Definition: PPath.h:17
Extends the std::string.
Definition: PString.h:16
#define phoenix_assert(isOk)
bool phoenix_check(const std::string &testName, const std::string &val, const std::string &reference)
Check two string.