PhoenixLecture  2.0.0
Set of tools to make lectures
parser_toml.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 "parse_generic_string.h"
8 #include "parser_toml.h"
9 
13  bool isRun;
14 };
15 
17 
20  PTomlParserData data;
21  data.isRun = true;
22  return data;
23 }
24 
25 bool parse_toml_var(DicoValue & dico, PFileParser & parser, PTomlParserData & data);
26 bool parse_toml_all(DicoValue & parent, PFileParser & parser, PTomlParserData & data);
27 
29 
32  data.isRun = false;
33 }
34 
36 
39  return data.isRun;
40 }
41 
43 
47 bool parse_toml_varName(PString & varName, PFileParser & parser){
48  varName = parser.getStrComposedOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
49  return varName != "";
50 }
51 
53 
59  if(parser.isMatch("[")){ //The value is a list
60  while(!parser.isEndOfFile() && !parser.isMatch("]") && parse_toml_isParse(data)){ //Let's parse the values of the Variable
61  DicoValue subValue;
62  if(parse_toml_varValue(subValue, parser, data)){
63  var.getVecChild().push_back(subValue);
64 
65  if(!parser.isMatch(",") && !parser.isMatchRewind("]")){ //We expect one element or a comma to add more
66  std::cerr << "parse_toml_varValue : expect ',' or ']' forunclosed list of values at " << parser.getLocation() << std::endl;
68  return false;
69  }
70  }else{
71  if(!parser.isMatch("]")){
72  std::cerr << "parse_toml_varBase : missing ']' token to close empty list of value at " << parser.getLocation() << std::endl;
74  return false;
75  }
76  }
77  }
78  }else{
79  PString strValue("");
80  if(parse_generic_string(strValue, parser)){
81  var.setValue(strValue);
82  }else if(parser.isMatch("true")){
83  var.setValue("true");
84  }else if(parser.isMatch("false")){
85  var.setValue("false");
86  }else{
87  PString valueNumber(parser.getStrComposedOf("0123456789.-+e"));
88  if(valueNumber != ""){
89  var.setValue(valueNumber);
90  }else{
91  std::cerr << "parse_toml_varValue : missing value of variable '"<<var.getKey()<<"' at " << parser.getLocation() << std::endl;
93  return false;
94  }
95  }
96  }
97  return true;
98 }
99 
101 
107  if(!parser.isMatch("{")){return false;}
108  while(!parser.isEndOfFile() && parse_toml_isParse(data) && !parser.isMatch("}")){ //Let's parse the vatiables of the Dico
109  if(parser.isMatch("#")){parser.getUntilKeyWithoutPatern("\n");}
110  else if(parse_toml_var(parent, parser, data)){
111  if(!parser.isMatch(",") && !parser.isMatchRewind("}")){ //We expect one element or a comma to add more
112  std::cerr << "parse_tomlCompactDico : expect ',' or '}' for unclosed dictionary of values at " << parser.getLocation() << std::endl;
114  return false;
115  }
116  }else{
117  if(!parser.isMatch("}")){
118  std::cerr << "parse_tomlCompactDico : missing '}' token to close empty dictionary of value at " << parser.getLocation() << std::endl;
120  return false;
121  }
122  }
123  }
124  return true;
125 }
126 
128 
134  PString varName("");
135  if(!parse_toml_varName(varName, parser)){return false;}
136  if(!parser.isMatch("=")){
137  std::cerr << "parse_toml_varBase : missing '=' token to define value of variable '"<<varName<<"' at " << parser.getLocation() << std::endl;
139  return false;
140  }
141  var.setKey(varName);
142  bool b(true);
143  if(parse_tomlCompactDico(var, parser, data)){}
144  else if(parse_toml_varValue(var, parser, data)){}
145  else{b = false;}
146  return b;
147 }
148 
150 
155 bool parse_toml_var(DicoValue & dico, PFileParser & parser, PTomlParserData & data){
156  DicoValue var;
157  if(!parse_toml_varBase(var, parser, data)){return false;}
158  dico.getMapChild()[var.getKey()] = var;
159  return true;
160 }
161 
163 
169  DicoValue var;
170  if(!parse_toml_varBase(var, parser, data)){return false;}
171  dico.getVecChild().push_back(var);
172  return true;
173 }
174 
176 
180 DicoValue * parse_get_parent_dico(DicoValue & parent, const PVecString & vecDicoName){
181  DicoValue * output = &parent;
182  for(PVecString::const_iterator it(vecDicoName.begin()); it != vecDicoName.end(); ++it){
183  output = &(output->getMapChild()[*it]);
184  output->setKey(*it);
185  }
186  return output;
187 }
188 
190 
195 bool parse_toml_dico_def(DicoValue & parent, PFileParser & parser, PTomlParserData & data){
196  if(!parser.isMatch("[")){return false;}
197  PString dicoName(parser.getUntilKeyWithoutPatern("]"));
198  PVecString vecDicoName = dicoName.split('.');
199  DicoValue * dicoDef = parse_get_parent_dico(parent, vecDicoName);
200 
201 // dicoDef.setKey(dicoName);
202  while(!parser.isEndOfFile() && parse_toml_isParse(data) && !parser.isMatchRewind("[")){ //Let's parse the vatiables of the Dico
203  if(parser.isMatch("#")){parser.getUntilKeyWithoutPatern("\n");}
204  else if(parse_toml_var(*dicoDef, parser, data)){
205 // parent.getMapChild()[dicoName] = *dicoDef;
206  }
207 // else{
208 // std::cerr << "parse_toml_dico_def : error at " << parser.getLocation() << std::endl;
209 // std::cerr << "\tcannot parse [definition]" << std::endl;
210 // parse_toml_stopParsing(data);
211 // return true;
212 // }
213  }
214  return true;
215 }
216 
218 
224  if(!parser.isMatch("[[")){return false;}
225  PString dicoName(parser.getUntilKeyWithoutPatern("]]"));
226  PVecString vecDicoName = dicoName.split('.');
227  DicoValue * dicoDef = parse_get_parent_dico(parent, vecDicoName);
228 // DicoValue dicoDef;
229 // dicoDef.setKey(dicoName);
230  DicoValue table;
231  while(!parser.isEndOfFile() && parse_toml_isParse(data) && !parser.isMatchRewind("[")){ //Let's parse the vatiables of the Dico
232  if(parser.isMatch("#")){parser.getUntilKeyWithoutPatern("\n");}
233  else if(parse_toml_var(table, parser, data)){
234 // parent.getMapChild()[dicoName] = *dicoDef;
235  }
236 // else{
237 // std::cerr << "parse_toml_table_def : error at " << parser.getLocation() << std::endl;
238 // std::cerr << "\tcannot parse [[table_definition]]" << std::endl;
239 // parse_toml_stopParsing(data);
240 // return true;
241 // }
242  }
243  dicoDef->getVecChild().push_back(table);
244  return true;
245 }
246 
248 
252 bool parser_toml_fileParser(DicoValue & dico, PFileParser & parser){
254  parser.getStrComposedOf(" \t\n"); //Skip all blank characters
255  while(!parser.isEndOfFile() && parse_toml_isParse(data)){
256  if(parser.isMatch("#")){parser.getUntilKeyWithoutPatern("\n");}
257  else if(parse_toml_table_def(dico, parser, data)){}
258  else if(parse_toml_dico_def(dico, parser, data)){}
259  else{
260  PString nextToken(parser.getNextToken());
261  if(nextToken.size() != 0lu){ //If the token is empty, we are at the end of the file
262  std::cerr << "parser_toml_fileParser : error at " << parser.getLocation() << std::endl;
263  std::cerr << "\tunexpected token '"<<nextToken<<"'" << std::endl;
265  }
266  }
267  }
268  return data.isRun;
269 }
270 
272 
276 bool parser_toml(DicoValue & dico, const PPath & fileName){
277  PFileParser parser;
278  parser.setWhiteSpace(" \n\t");
279  parser.setSeparator(":-'\",{}[]>|");
280  parser.setEscapeChar('\\');
281  if(!parser.open(fileName)){
282  std::cerr << "parser_toml : cannot open file '"<<fileName<<"'" << std::endl;
283  return false;
284  }
285  bool b(parser_toml_fileParser(dico, parser));
286  return b;
287 }
288 
std::vector< PString > PVecString
Definition: PString.h:96
Dictionnary of values.
Definition: DicoValue.h:17
const PString & getKey() const
Gets the key of the DicoValue.
Definition: DicoValue.cpp:190
void setKey(const PString &key)
Sets the key of the DicoValue.
Definition: DicoValue.cpp:148
const std::vector< DicoValue > & getVecChild() const
Gets the vecChild of the DicoValue.
Definition: DicoValue.cpp:204
void setValue(const PString &value)
Sets the value of the DicoValue.
Definition: DicoValue.cpp:141
const std::map< PString, DicoValue > & getMapChild() const
Gets the mapChild of the DicoValue.
Definition: DicoValue.cpp:218
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
bool open(const PPath &fileName)
Fonction qui ouvre le fichier que l'on va parser.
Definition: PFileParser.cpp:24
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 setEscapeChar(char escapeChar)
Sets the escape character of the PFileParser.
Definition: PFileParser.cpp:58
PString getStrComposedOf(const PString &charset)
Get string composed of the characters in the string charset.
void setWhiteSpace(const PString &whiteSpace)
Initialise la liste des caractères blancs.
Definition: PFileParser.cpp:35
bool isMatchRewind(const PString &patern)
Do a isMatch and then go back at the previous position.
bool isMatch(const PString &patern)
Says if the patern match with the current caracters of the PFileParser.
PLocation getLocation() const
Fonction qui renvoie la PLocation du PFileParser.
bool isEndOfFile() const
Dit si on est à la fin du fichier.
Definition: PFileParser.cpp:88
Path of a directory or a file.
Definition: PPath.h:17
Extends the std::string.
Definition: PString.h:16
std::vector< PString > split(char separator) const
Cut a PString on the given separator char.
Definition: PString.cpp:420
bool parse_generic_string(PString &str, PFileParser &parser)
Parse string value.
void parse_toml_stopParsing(PTomlParserData &data)
Stop the file parsing.
Definition: parser_toml.cpp:31
bool parse_toml_dico_def(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a dico definition.
bool parse_toml_varBase(DicoValue &var, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.
bool parse_toml_isParse(const PTomlParserData &data)
Say if the file parsing is enable.
Definition: parser_toml.cpp:38
bool parse_toml_varName(PString &varName, PFileParser &parser)
Parse a toml var name.
Definition: parser_toml.cpp:47
bool parse_toml_varValue(DicoValue &var, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.
Definition: parser_toml.cpp:58
DicoValue * parse_get_parent_dico(DicoValue &parent, const PVecString &vecDicoName)
Get the parent dictionary by respect to the vecDicoName.
bool parse_toml_varTable(DicoValue &dico, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.
bool parse_toml_table_def(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a dico definition.
bool parse_tomlCompactDico(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a compact dico definition.
PTomlParserData default_PTomlParserData()
Default value of PTomlParserData.
Definition: parser_toml.cpp:19
bool parse_toml_all(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
bool parse_toml_var(DicoValue &dico, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.
bool parser_toml(DicoValue &dico, const PPath &fileName)
Data used to parse a toml file.
Definition: parser_toml.cpp:11
bool isRun
True to continue the parsing, false to stop.
Definition: parser_toml.cpp:13