PhoenixYml  0.5.0
Yml parser for Phoenix
Loading...
Searching...
No Matches
parser_yml.cpp File Reference
#include "PFileParser.h"
#include "vec_value_utils.h"
#include "parser_yml.h"
+ Include dependency graph for parser_yml.cpp:

Go to the source code of this file.

Classes

struct  PYmlParserData
 Data used to parse a yml file. More...
 

Functions

PYmlParserData default_PYmlParserData ()
 Default value of PYmlParserData.
 
bool parse_yml_all (VecValue &parent, PFileParser &parser, PYmlParserData &data)
 Parse all yml features.
 
bool parse_yml_compactDicoContent (VecValue &parent, PFileParser &parser, PYmlParserData &data)
 Parse compact dico content.
 
bool parse_yml_compactListContent (VecValue &parent, PFileParser &parser, PYmlParserData &data)
 Parse compact dico content.
 
bool parse_yml_dicoContent (VecValue &parent, PFileParser &parser, PYmlParserData &data)
 Parse dico content.
 
void parse_yml_dicoSetValue (VecValue *vecVal, const PString &value)
 Set a value into a VecValue.
 
void parse_yml_incrementCurrentChar (PFileParser &parser, PYmlParserData &data)
 Increment the current character.
 
bool parse_yml_isParse (const PYmlParserData &data)
 Say if the file parsing is enable.
 
bool parse_yml_key (PString &key, size_t &keyIndentation, PFileParser &parser)
 Parse key.
 
bool parse_yml_listContent (VecValue &parent, PFileParser &parser, PYmlParserData &data)
 Parse dico content.
 
void parse_yml_playCurrentText (PYmlParserData &data)
 Play the current parsed text.
 
void parse_yml_stopParsing (PYmlParserData &data)
 Stop the file parsing.
 
bool parse_yml_string (PString &str, PFileParser &parser)
 Parse string value.
 
bool parse_yml_stringValue (PFileParser &parser, PYmlParserData &data)
 Parse compact dico content.
 
void parse_yml_updateIndentation (std::vector< size_t > &vecIndentation, size_t currentIndentation)
 Update the indentation vector by respect to the given indentation.
 
bool parser_yml (DicoValue &dico, const PPath &fileName)
 Parse a yml file and update the given DicoValue.
 
bool parser_yml_fileParser (VecValue &dico, PFileParser &parser)
 Parse a yml file and update the given VecValue.
 

Function Documentation

◆ default_PYmlParserData()

PYmlParserData default_PYmlParserData ( )

Default value of PYmlParserData.

Returns
default PYmlParserData

Definition at line 31 of file parser_yml.cpp.

31 {
32 PYmlParserData data;
33 data.isRun = true;
34 data.compactMode = false;
35 data.currentLine = 1lu;
36 data.currentText = "";
37 data.currentlyParsedKeyValue = NULL;
38 return data;
39}
Data used to parse a yml file.
bool compactMode
True if the compact mode is activated.
bool isRun
True to continue the parsing, false to stop.
size_t currentLine
Current line number.
PString currentText
Current parsed text.
VecValue * currentlyParsedKeyValue
Currently parsed key value.

References PYmlParserData::compactMode, PYmlParserData::currentLine, PYmlParserData::currentlyParsedKeyValue, PYmlParserData::currentText, and PYmlParserData::isRun.

Referenced by parser_yml_fileParser().

+ Here is the caller graph for this function:

◆ parse_yml_all()

bool parse_yml_all ( VecValue & parent,
PFileParser & parser,
PYmlParserData & data )

Parse all yml features.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Definition at line 304 of file parser_yml.cpp.

304 {
305 if(parser.isMatch("#")){parser.getUntilKeyWithoutPatern("\n");} //Skip the comment
306 else if(parse_yml_compactListContent(parent, parser, data)){}
307 else if(parse_yml_compactDicoContent(parent, parser, data)){}
308 else if(parse_yml_listContent(parent, parser, data)){}
309 else if(parse_yml_dicoContent(parent, parser, data)){}
310 else if(parse_yml_stringValue(parser, data)){}
311 else{
312 parse_yml_incrementCurrentChar(parser, data);
313 }
314 return true;
315}
bool parse_yml_compactDicoContent(VecValue &parent, PFileParser &parser, PYmlParserData &data)
Parse compact dico content.
void parse_yml_incrementCurrentChar(PFileParser &parser, PYmlParserData &data)
Increment the current character.
bool parse_yml_dicoContent(VecValue &parent, PFileParser &parser, PYmlParserData &data)
Parse dico content.
bool parse_yml_listContent(VecValue &parent, PFileParser &parser, PYmlParserData &data)
Parse dico content.
bool parse_yml_compactListContent(VecValue &parent, PFileParser &parser, PYmlParserData &data)
Parse compact dico content.
bool parse_yml_stringValue(PFileParser &parser, PYmlParserData &data)
Parse compact dico content.

References parse_yml_compactDicoContent(), parse_yml_compactListContent(), parse_yml_dicoContent(), parse_yml_incrementCurrentChar(), parse_yml_listContent(), and parse_yml_stringValue().

Referenced by parse_yml_compactDicoContent(), and parser_yml_fileParser().

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

◆ parse_yml_compactDicoContent()

bool parse_yml_compactDicoContent ( VecValue & parent,
PFileParser & parser,
PYmlParserData & data )

Parse compact dico content.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Definition at line 175 of file parser_yml.cpp.

175 {
176 if(!parser.isMatch("{", "$§_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789/*+-.")){return false;}
177 std::vector<size_t> saveVecIndentation = data.vecIndentation;
179 bool oldMode = data.compactMode;
180 data.compactMode = true;
181
182 VecValue & dico = *data.currentlyParsedKeyValue;
183 data.vecIndentation.clear();
184 data.vecIndentation.push_back(0lu);
185 parser.getStrComposedOf(" \t\n"); //Skip all blank characters
186 while(parse_yml_isParse(data) && !parser.isMatch("}")){
187 if(!parse_yml_all(dico, parser, data)){
188 std::cerr << "parse_yml_compactDicoContent : error at " << parser.getLocation() << std::endl;
189 std::cerr << "\tunexpected token '"<<parser.getNextToken()<<"'" << std::endl;
191 }
192 parser.isMatch(","); //Skip comma if there is one
193 }
195 data.vecIndentation = saveVecIndentation;
196 data.compactMode = oldMode;
197 return true;
198}
Vector of keys and values.
Definition VecValue.h:15
void parse_yml_playCurrentText(PYmlParserData &data)
Play the current parsed text.
void parse_yml_stopParsing(PYmlParserData &data)
Stop the file parsing.
bool parse_yml_isParse(const PYmlParserData &data)
Say if the file parsing is enable.
bool parse_yml_all(VecValue &parent, PFileParser &parser, PYmlParserData &data)
Parse all yml features.
std::vector< size_t > vecIndentation
Vector of previous line indentations.

References PYmlParserData::compactMode, PYmlParserData::currentlyParsedKeyValue, parse_yml_all(), parse_yml_isParse(), parse_yml_playCurrentText(), parse_yml_stopParsing(), and PYmlParserData::vecIndentation.

Referenced by parse_yml_all().

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

◆ parse_yml_compactListContent()

bool parse_yml_compactListContent ( VecValue & parent,
PFileParser & parser,
PYmlParserData & data )

Parse compact dico content.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Definition at line 243 of file parser_yml.cpp.

243 {
244 if(!parser.isMatch("[")){return false;}
246 VecVecValue & listValue = data.currentlyParsedKeyValue->getVecChild();
247
248 while(parse_yml_isParse(data) && !parser.isMatch("]")){
249 PString tmpStr("");
250 if(parse_yml_string(tmpStr, parser)){
251 VecValue vecTmp;
252 vecTmp.setValue(tmpStr);
253 listValue.push_back(vecTmp);
254 }else{
255 while(parse_yml_isParse(data) && !parser.isMatchRewind("]") && !parser.isMatchRewind(",")){
256 parse_yml_incrementCurrentChar(parser, data);
257 }
258 VecValue vecTmp;
259 vecTmp.setValue(data.currentText.eraseFirstChar(" \t\n"));
260 listValue.push_back(vecTmp);
261 data.currentText = "";
262 }
263 if(!parser.isMatch(",") && !parser.isMatchRewind("]")){
265 std::cerr << "parser_yml_fileParser : error at " << parser.getLocation() << std::endl;
266 std::cerr << "\tunexpected token '"<<parser.getNextToken()<<"'" << std::endl;
267 std::cerr << "\texpect token ',' or ']'" << std::endl;
268 return true;
269 }
270 }
271 return true;
272}
void setValue(const PString &value)
Sets the value of the VecValue.
Definition VecValue.cpp:41
const std::vector< VecValue > & getVecChild() const
Gets the vecChild of the VecValue.
Definition VecValue.cpp:104
bool parse_yml_string(PString &str, PFileParser &parser)
Parse string value.
std::vector< VecValue > VecVecValue

References PYmlParserData::currentlyParsedKeyValue, PYmlParserData::currentText, VecValue::getVecChild(), parse_yml_incrementCurrentChar(), parse_yml_isParse(), parse_yml_playCurrentText(), parse_yml_stopParsing(), parse_yml_string(), and VecValue::setValue().

Referenced by parse_yml_all().

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

◆ parse_yml_dicoContent()

bool parse_yml_dicoContent ( VecValue & parent,
PFileParser & parser,
PYmlParserData & data )

Parse dico content.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Let's update the current VecValue to be completd with values and lists

Definition at line 205 of file parser_yml.cpp.

205 {
206 parser.pushPosition();
207 PString keyName("");
208 size_t keyIndentation(0lu);
209 if(!parse_yml_key(keyName, keyIndentation, parser)){ //Check if there is a key
210 parser.popPosition(); //This is not a key
211 return false;
212 }
214 VecValue dico;
215 dico.setKey(keyName);
217 if(data.compactMode){
218 keyIndentation = -1lu;
219 }
220 dico.setIndentation(keyIndentation);
221 //Add the dico into its parent and get back the pointer of the current dico we want to fill
222 VecValue * ptrDico = addChildToParentVecValue(parent, data.vecIndentation, data.compactMode, dico, keyIndentation);
223
224 //Update indentation to be used to fill the VecValue in the current dico
225 parse_yml_updateIndentation(data.vecIndentation, keyIndentation);
226
228 data.currentlyParsedKeyValue = ptrDico;
229
230 //We do not need to parse the dico content, because its content will depend on what comes next (key, value, list)
231
232 //OK, on pourrait dire que les dicos se regroupent par leur indentation donc ça fonctionne
233 //Et que les valeurs associée (string, listes) sont ajoutées en utilisant data.currentlyParsedKeyValue
234
235 return true;
236}
void setIndentation(size_t indentation)
Sets the indentation of the VecValue.
Definition VecValue.cpp:69
void setKey(const PString &key)
Sets the key of the VecValue.
Definition VecValue.cpp:48
void setType(const VecValueType::VecValueType &type)
Sets the type of the VecValue.
Definition VecValue.cpp:62
void parse_yml_updateIndentation(std::vector< size_t > &vecIndentation, size_t currentIndentation)
Update the indentation vector by respect to the given indentation.
bool parse_yml_key(PString &key, size_t &keyIndentation, PFileParser &parser)
Parse key.
VecValue * addChildToParentVecValue(VecValue &mainVecValue, const std::vector< size_t > &vecIndentation, bool isCompactMode, const VecValue &child, size_t currentIndentation)
Add the given child to the main VecValue and return a pointer to the added child.

References addChildToParentVecValue(), PYmlParserData::compactMode, PYmlParserData::currentlyParsedKeyValue, VecValueType::KEY, parse_yml_key(), parse_yml_playCurrentText(), parse_yml_updateIndentation(), VecValue::setIndentation(), VecValue::setKey(), VecValue::setType(), and PYmlParserData::vecIndentation.

Referenced by parse_yml_all().

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

◆ parse_yml_dicoSetValue()

void parse_yml_dicoSetValue ( VecValue * vecVal,
const PString & value )

Set a value into a VecValue.

Parameters
[out]vecVal: vector of value
value: value to be used

Definition at line 140 of file parser_yml.cpp.

140 {
141 if(vecVal == NULL){return;}
142 vecVal->setValue(value);
144}

References VecValue::setType(), VecValue::setValue(), and VecValueType::VALUE.

Referenced by parse_yml_stringValue().

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

◆ parse_yml_incrementCurrentChar()

void parse_yml_incrementCurrentChar ( PFileParser & parser,
PYmlParserData & data )

Increment the current character.

Parameters
[out]parser: parser to be used
data: parsing data

Definition at line 85 of file parser_yml.cpp.

85 {
86 //If nothing is known I need to save the current char in the MACRO TEXT
87 char ch = parser.getCurrentCh();
88 data.currentText += ch;
89 parser.getNextChar();
90}

References PYmlParserData::currentText.

Referenced by parse_yml_all(), and parse_yml_compactListContent().

+ Here is the caller graph for this function:

◆ parse_yml_isParse()

bool parse_yml_isParse ( const PYmlParserData & data)

Say if the file parsing is enable.

Parameters
data: parsing data

Definition at line 53 of file parser_yml.cpp.

53 {
54 return data.isRun;
55}

References PYmlParserData::isRun.

Referenced by parse_yml_compactDicoContent(), parse_yml_compactListContent(), and parser_yml_fileParser().

+ Here is the caller graph for this function:

◆ parse_yml_key()

bool parse_yml_key ( PString & key,
size_t & keyIndentation,
PFileParser & parser )

Parse key.

Parameters
[out]key: parsed key
[out]keyIndentation: indentation of the key
[out]parser: PFileParser to be used

Definition at line 109 of file parser_yml.cpp.

109 {
110 parser.pushPosition();
111 PString possibleKey(parser.getStrComposedOf("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"));
112 if(possibleKey != "" && parser.isMatch(":")){
113 key = possibleKey;
114 keyIndentation = parser.getColumn() - possibleKey.size() - 1lu;
115 return true;
116 }
117 parser.popPosition();
118 return false;
119}

Referenced by parse_yml_dicoContent().

+ Here is the caller graph for this function:

◆ parse_yml_listContent()

bool parse_yml_listContent ( VecValue & parent,
PFileParser & parser,
PYmlParserData & data )

Parse dico content.

Parameters
[out]parent: parent VecValue
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Let's update the current VecValue to be completd with values and lists

Definition at line 279 of file parser_yml.cpp.

279 {
280 if(!parser.isMatch("- ")){return false;} //There is no extra space, this is exactly the syntax
281 size_t currentIndentation(parser.getColumn() - 1lu);
283 VecValue dashList;
284 dashList.setKey("");
286 dashList.setIndentation(currentIndentation);
287// std::cerr << "parse_yml_listContent : Add LIST_ITEM at " << parser.getLocation() << std::endl;
288 //Add the dico into its parent and get back the pointer of the current dico we want to fill
289 VecValue * ptrDashList = addChildToParentVecValueAddListItem(parent, data.vecIndentation, data.compactMode, dashList, currentIndentation);
290
291 //Update indentation to be used to fill the VecValue in the current dico
292 parse_yml_updateIndentation(data.vecIndentation, currentIndentation);
293
295 data.currentlyParsedKeyValue = ptrDashList;
296 return true;
297}
VecValue * addChildToParentVecValueAddListItem(VecValue &mainVecValue, const std::vector< size_t > &vecIndentation, bool isCompactMode, const VecValue &child, size_t currentIndentation)
Add the given child to the main VecValue and return a pointer to the added child.

References addChildToParentVecValueAddListItem(), PYmlParserData::compactMode, PYmlParserData::currentlyParsedKeyValue, VecValueType::LIST_ITEM, parse_yml_playCurrentText(), parse_yml_updateIndentation(), VecValue::setIndentation(), VecValue::setKey(), VecValue::setType(), and PYmlParserData::vecIndentation.

Referenced by parse_yml_all().

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

◆ parse_yml_playCurrentText()

void parse_yml_playCurrentText ( PYmlParserData & data)

Play the current parsed text.

Parameters
[out]data: parsing data

Definition at line 95 of file parser_yml.cpp.

95 {
96 PString value(data.currentText.eraseFirstLastChar(" \t\n"));
97
98 if(data.currentlyParsedKeyValue != NULL && value != ""){
100 }
101 data.currentText = "";
102}

References PYmlParserData::currentlyParsedKeyValue, PYmlParserData::currentText, and VecValue::setValue().

Referenced by parse_yml_compactDicoContent(), parse_yml_compactListContent(), parse_yml_dicoContent(), parse_yml_listContent(), and parser_yml_fileParser().

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

◆ parse_yml_stopParsing()

void parse_yml_stopParsing ( PYmlParserData & data)

Stop the file parsing.

Parameters
[out]data: parsing data

Definition at line 46 of file parser_yml.cpp.

46 {
47 data.isRun = false;
48}

References PYmlParserData::isRun.

Referenced by parse_yml_compactDicoContent(), parse_yml_compactListContent(), and parser_yml_fileParser().

+ Here is the caller graph for this function:

◆ parse_yml_string()

bool parse_yml_string ( PString & str,
PFileParser & parser )

Parse string value.

Parameters
[out]str: parsed string value
[out]parser: PFileParser to be used

Definition at line 125 of file parser_yml.cpp.

125 {
126 if(parser.isMatch("\"")){
127 str = "\"" + parser.getUntilKey("\"");
128 }else if(parser.isMatch("'")){
129 str = "'" + parser.getUntilKey("'");
130 }else{
131 return false;
132 }
133 return true;
134}

Referenced by parse_yml_compactListContent().

+ Here is the caller graph for this function:

◆ parse_yml_stringValue()

bool parse_yml_stringValue ( PFileParser & parser,
PYmlParserData & data )

Parse compact dico content.

Parameters
[out]parser: PFileParser to be used
[out]data: extra parser data to be used

Definition at line 150 of file parser_yml.cpp.

150 {
151 if(parser.isMatch("\'")){
152 PString str("\'" + parser.getUntilKey("\'"));
154 }else if(parser.isMatch("\"")){
155 PString str("\"" + parser.getUntilKey("\""));
157 }else{
158// PString strValue(parser.getStrComposedOf("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789%$£?.*/+=-&~#(){}[]|!<>@°"));
159// parser.skipWhiteSpace();
160// PString strValue(parser.getStrComposedOf("_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789{}§"));
161// if(strValue != ""){
162// parse_yml_dicoSetValue(data.currentlyParsedKeyValue, strValue);
163// }else{
164 return false;
165// }
166 }
167 return true;
168}
void parse_yml_dicoSetValue(VecValue *vecVal, const PString &value)
Set a value into a VecValue.

References PYmlParserData::currentlyParsedKeyValue, and parse_yml_dicoSetValue().

Referenced by parse_yml_all().

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

◆ parse_yml_updateIndentation()

void parse_yml_updateIndentation ( std::vector< size_t > & vecIndentation,
size_t currentIndentation )

Update the indentation vector by respect to the given indentation.

Parameters
[out]vecIndentation: vector of indentation to be updated
currentIndentation: current indentation

Definition at line 61 of file parser_yml.cpp.

61 {
62 if(currentIndentation == 0lu || vecIndentation.size() == 0lu){ //If there is no indentation
63 vecIndentation.clear();
64 vecIndentation.push_back(currentIndentation);
65 return;
66 }
67 std::vector<size_t> vecOutIndentation;
68 std::vector<size_t>::const_iterator it(vecIndentation.begin());
69 bool isCurrentLower(true);
70 while(isCurrentLower && it != vecIndentation.end()){ //Get previous indentation until the current one
71 isCurrentLower = *it < currentIndentation;
72 if(isCurrentLower){
73 vecOutIndentation.push_back(*it);
74 }
75 ++it;
76 }
77 vecOutIndentation.push_back(currentIndentation); //Add the current indentation
78 vecIndentation = vecOutIndentation;
79}

Referenced by parse_yml_dicoContent(), and parse_yml_listContent().

+ Here is the caller graph for this function:

◆ parser_yml()

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

Parse a yml file and update the given DicoValue.

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

Definition at line 343 of file parser_yml.cpp.

343 {
344 PFileParser parser;
345 parser.setWhiteSpace("");
346 parser.setSeparator(":-'\",{}[]>|#");
347 parser.setEscapeChar('\\');
348 if(!parser.open(fileName)){
349 std::cerr << "parser_yml : cannot open file '"<<fileName<<"'" << std::endl;
350 return false;
351 }
352 VecValue vecValue;
353 vecValue.setType(VecValueType::MAIN);
354 vecValue.setIndentation(parser.getLocation().getColumn());
355 bool b(parser_yml_fileParser(vecValue, parser));
356 vecValueToDicoValue(dico, vecValue);
357 return b;
358}
bool parser_yml_fileParser(VecValue &dico, PFileParser &parser)
Parse a yml file and update the given VecValue.
void vecValueToDicoValue(DicoValue &dicoValue, const VecValue &vecVal, bool isMainValue)
Convert a VecValue into a DicoValue.

References VecValueType::MAIN, parser_yml_fileParser(), VecValue::setIndentation(), VecValue::setType(), and vecValueToDicoValue().

+ Here is the call graph for this function:

◆ parser_yml_fileParser()

bool parser_yml_fileParser ( VecValue & dico,
PFileParser & parser )

Parse a yml file and update the given VecValue.

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

Definition at line 322 of file parser_yml.cpp.

322 {
324 data.currentlyParsedKeyValue = &dico;
325 parser.getStrComposedOf(" \t\n"); //Skip all blank characters
326 while(!parser.isEndOfFile() && parse_yml_isParse(data)){
327 if(!parse_yml_all(dico, parser, data)){
328 std::cerr << "parser_yml_fileParser : error at " << parser.getLocation() << std::endl;
329 std::cerr << "\tunexpected token '"<<parser.getNextToken()<<"'" << std::endl;
331 }
332 }
334 return data.isRun;
335}
PYmlParserData default_PYmlParserData()
Default value of PYmlParserData.

References PYmlParserData::currentlyParsedKeyValue, default_PYmlParserData(), PYmlParserData::isRun, parse_yml_all(), parse_yml_isParse(), parse_yml_playCurrentText(), and parse_yml_stopParsing().

Referenced by parser_yml().

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