PhoenixToml  0.4.0
Toml parser for Phoenix
Loading...
Searching...
No Matches
parser_toml.cpp File Reference
#include "parse_generic_string.h"
#include "parser_toml.h"
+ Include dependency graph for parser_toml.cpp:

Go to the source code of this file.

Classes

struct  PTomlParserData
 Data used to parse a toml file. More...
 

Functions

PTomlParserData default_PTomlParserData ()
 Default value of PTomlParserData.
 
DicoValue * parse_get_parent_dico (DicoValue &parent, const PVecString &vecDicoName)
 Get the parent dictionary by respect to the vecDicoName.
 
bool parse_toml_all (DicoValue &parent, PFileParser &parser, PTomlParserData &data)
 
bool parse_toml_dico_def (DicoValue &parent, PFileParser &parser, PTomlParserData &data)
 Parse a dico definition.
 
bool parse_toml_isParse (const PTomlParserData &data)
 Say if the file parsing is enable.
 
void parse_toml_stopParsing (PTomlParserData &data)
 Stop the file parsing.
 
bool parse_toml_table_def (DicoValue &parent, PFileParser &parser, PTomlParserData &data)
 Parse a dico definition.
 
bool parse_toml_var (DicoValue &dico, PFileParser &parser, PTomlParserData &data)
 Parse a toml var name.
 
bool parse_toml_varBase (DicoValue &var, PFileParser &parser, PTomlParserData &data)
 Parse a toml var name.
 
bool parse_toml_varName (PString &varName, PFileParser &parser)
 Parse a toml var name.
 
bool parse_toml_varTable (DicoValue &dico, PFileParser &parser, PTomlParserData &data)
 Parse a toml var name.
 
bool parse_toml_varValue (DicoValue &var, PFileParser &parser, PTomlParserData &data)
 Parse a toml var name.
 
bool parse_tomlCompactDico (DicoValue &parent, PFileParser &parser, PTomlParserData &data)
 Parse a compact dico definition.
 
bool parser_toml (DicoValue &dico, const PPath &fileName)
 Parse a toml file and update the given DicoValue.
 
bool parser_toml_fileParser (DicoValue &dico, PFileParser &parser)
 Parse a yml file and update the given VecValue.
 

Function Documentation

◆ default_PTomlParserData()

PTomlParserData default_PTomlParserData ( )

Default value of PTomlParserData.

Returns
default PTomlParserData

Definition at line 19 of file parser_toml.cpp.

19 {
20 PTomlParserData data;
21 data.isRun = true;
22 return data;
23}
Data used to parse a toml file.
bool isRun
True to continue the parsing, false to stop.

References PTomlParserData::isRun.

Referenced by parser_toml_fileParser().

+ Here is the caller graph for this function:

◆ parse_get_parent_dico()

DicoValue * parse_get_parent_dico ( DicoValue & parent,
const PVecString & vecDicoName )

Get the parent dictionary by respect to the vecDicoName.

Parameters
parent: main DicoValue
vecDicoName: name of the parent DicoValue of the following attributes
Returns
pointer to the parent DicoValue of the following attributes

Definition at line 180 of file parser_toml.cpp.

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

Referenced by parse_toml_dico_def(), and parse_toml_table_def().

+ Here is the caller graph for this function:

◆ parse_toml_all()

bool parse_toml_all ( DicoValue & parent,
PFileParser & parser,
PTomlParserData & data )

◆ parse_toml_dico_def()

bool parse_toml_dico_def ( DicoValue & parent,
PFileParser & parser,
PTomlParserData & data )

Parse a dico definition.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 195 of file parser_toml.cpp.

195 {
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}
DicoValue * parse_get_parent_dico(DicoValue &parent, const PVecString &vecDicoName)
Get the parent dictionary by respect to the vecDicoName.
bool parse_toml_isParse(const PTomlParserData &data)
Say if the file parsing is enable.
bool parse_toml_var(DicoValue &dico, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.

References parse_get_parent_dico(), parse_toml_isParse(), and parse_toml_var().

Referenced by parser_toml_fileParser().

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

◆ parse_toml_isParse()

bool parse_toml_isParse ( const PTomlParserData & data)

Say if the file parsing is enable.

Parameters
data: parsing data

Definition at line 38 of file parser_toml.cpp.

38 {
39 return data.isRun;
40}

References PTomlParserData::isRun.

Referenced by parse_toml_dico_def(), parse_toml_table_def(), parse_toml_varValue(), parse_tomlCompactDico(), and parser_toml_fileParser().

+ Here is the caller graph for this function:

◆ parse_toml_stopParsing()

void parse_toml_stopParsing ( PTomlParserData & data)

Stop the file parsing.

Parameters
[out]data: parsing data

Definition at line 31 of file parser_toml.cpp.

31 {
32 data.isRun = false;
33}

References PTomlParserData::isRun.

Referenced by parse_toml_varBase(), parse_toml_varValue(), parse_tomlCompactDico(), and parser_toml_fileParser().

+ Here is the caller graph for this function:

◆ parse_toml_table_def()

bool parse_toml_table_def ( DicoValue & parent,
PFileParser & parser,
PTomlParserData & data )

Parse a dico definition.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 223 of file parser_toml.cpp.

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

References parse_get_parent_dico(), parse_toml_isParse(), and parse_toml_var().

Referenced by parser_toml_fileParser().

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

◆ parse_toml_var()

bool parse_toml_var ( DicoValue & dico,
PFileParser & parser,
PTomlParserData & data )

Parse a toml var name.

Parameters
[out]dico: DicoValue to be used
[out]parser: parser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 155 of file parser_toml.cpp.

155 {
156 DicoValue var;
157 if(!parse_toml_varBase(var, parser, data)){return false;}
158 dico.getMapChild()[var.getKey()] = var;
159 return true;
160}
bool parse_toml_varBase(DicoValue &var, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.

References parse_toml_varBase().

Referenced by parse_toml_dico_def(), parse_toml_table_def(), and parse_tomlCompactDico().

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

◆ parse_toml_varBase()

bool parse_toml_varBase ( DicoValue & var,
PFileParser & parser,
PTomlParserData & data )

Parse a toml var name.

Parameters
[out]var: variable DicoValue to be used
[out]parser: parser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 133 of file parser_toml.cpp.

133 {
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}
void parse_toml_stopParsing(PTomlParserData &data)
Stop the file parsing.
bool parse_toml_varName(PString &varName, PFileParser &parser)
Parse a toml var name.
bool parse_toml_varValue(DicoValue &var, PFileParser &parser, PTomlParserData &data)
Parse a toml var name.
bool parse_tomlCompactDico(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a compact dico definition.

References parse_toml_stopParsing(), parse_toml_varName(), parse_toml_varValue(), and parse_tomlCompactDico().

Referenced by parse_toml_var(), and parse_toml_varTable().

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

◆ parse_toml_varName()

bool parse_toml_varName ( PString & varName,
PFileParser & parser )

Parse a toml var name.

Parameters
[out]varName: variable name
parser: parser to be used
Returns
true on success, false otherwise

Definition at line 47 of file parser_toml.cpp.

47 {
48 varName = parser.getStrComposedOf("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_");
49 return varName != "";
50}

Referenced by parse_toml_varBase().

+ Here is the caller graph for this function:

◆ parse_toml_varTable()

bool parse_toml_varTable ( DicoValue & dico,
PFileParser & parser,
PTomlParserData & data )

Parse a toml var name.

Parameters
[out]dico: DicoValue to be used
[out]parser: parser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 168 of file parser_toml.cpp.

168 {
169 DicoValue var;
170 if(!parse_toml_varBase(var, parser, data)){return false;}
171 dico.getVecChild().push_back(var);
172 return true;
173}

References parse_toml_varBase().

+ Here is the call graph for this function:

◆ parse_toml_varValue()

bool parse_toml_varValue ( DicoValue & var,
PFileParser & parser,
PTomlParserData & data )

Parse a toml var name.

Parameters
[out]var: Value to be updated
[out]parser: parser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 58 of file parser_toml.cpp.

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

References parse_toml_isParse(), parse_toml_stopParsing(), and parse_toml_varValue().

Referenced by parse_toml_varBase(), and parse_toml_varValue().

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

◆ parse_tomlCompactDico()

bool parse_tomlCompactDico ( DicoValue & parent,
PFileParser & parser,
PTomlParserData & data )

Parse a compact dico definition.

Parameters
[out]parent: parent of parsed dico
[out]parser: parser to be used
[out]data: extra parser data to be used
Returns
true on success, false otherwise

Definition at line 106 of file parser_toml.cpp.

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

References parse_toml_isParse(), parse_toml_stopParsing(), and parse_toml_var().

Referenced by parse_toml_varBase().

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

◆ parser_toml()

bool parser_toml ( DicoValue & dico,
const PPath & fileName )

Parse a toml file and update the given DicoValue.

Parameters
[out]dico: dictionary of values
fileName: name of the file to be parsed
Returns
true on success, false otherwise

Definition at line 276 of file parser_toml.cpp.

276 {
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}
bool parser_toml_fileParser(DicoValue &dico, PFileParser &parser)
Parse a yml file and update the given VecValue.

References parser_toml_fileParser().

+ Here is the call graph for this function:

◆ parser_toml_fileParser()

bool parser_toml_fileParser ( DicoValue & dico,
PFileParser & parser )

Parse a yml file and update the given VecValue.

Parameters
[out]dico: dictionary of values
parser: PFileParser to be used
Returns
true on success, false otherwise

Definition at line 252 of file parser_toml.cpp.

252 {
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}
bool parse_toml_dico_def(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a dico definition.
bool parse_toml_table_def(DicoValue &parent, PFileParser &parser, PTomlParserData &data)
Parse a dico definition.
PTomlParserData default_PTomlParserData()
Default value of PTomlParserData.

References default_PTomlParserData(), PTomlParserData::isRun, parse_toml_dico_def(), parse_toml_isParse(), parse_toml_stopParsing(), and parse_toml_table_def().

Referenced by parser_toml().

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