Tomato
TomatoParser.hxx
Go to the documentation of this file.
1 
7 #ifndef Tomato_TomatoPARSER_HXX
8 #define Tomato_TomatoPARSER_HXX
9 
10 
11 namespace Ox {
12 
13  template<typename MeasureType>
14  int
15  TomatoParser<MeasureType>
16  ::parse() {
17 
18  FILE *fh = fopen(_filePath.c_str(), "rb");
19 
20  yaml_parser_t parser;
21  yaml_token_t token;
22 
23  /* Initialize parser */
24  if (!yaml_parser_initialize(&parser))
25  throw std::runtime_error("Failed to initialize parser!\n");
26  if (fh == NULL) {
27  throw std::runtime_error("\'" + _filePath + "\' - failed to open file!\n");
28  }
29 
30  /* Set input file */
31  yaml_parser_set_input_file(&parser, fh);
32 
33  bool flagKeyToken = false;
34  //bool flagFlowSequenceStartToken = false;
35  bool flagFlowEntryToken = false;
36  bool flagBlockEntryToken = false;
37  std::string lastKeyTokenValue;
38  std::string lastFlowEntryTokenValue;
39  std::string lastBlockEntryTokenValue;
40 
41  std::vector<std::string> temp;
42 
43  do {
44  yaml_parser_scan(&parser, &token);
45  switch (token.type) {
46  /* Token types (read before actual token) */
47  case YAML_KEY_TOKEN: {
48  flagKeyToken = true;
49  break;
50  }
51  case YAML_VALUE_TOKEN: {
52  break;
53  }
54  case YAML_FLOW_SEQUENCE_START_TOKEN: {
55  flagFlowEntryToken = true;
56  break;
57  }
58  case YAML_FLOW_SEQUENCE_END_TOKEN: {
59  flagFlowEntryToken = false;
60  if (_sequences.find(lastKeyTokenValue) != _sequences.end()) {
61  _sequences[lastKeyTokenValue] = temp;
62  }
63  temp.clear();
64  lastFlowEntryTokenValue = "";
65  break;
66  }
67  case YAML_FLOW_ENTRY_TOKEN: {
68  flagFlowEntryToken = true;
69  break;
70  }
71  case YAML_BLOCK_SEQUENCE_START_TOKEN: {
72  flagBlockEntryToken = true;
73  break;
74  }
75  case YAML_BLOCK_END_TOKEN: {
76  flagBlockEntryToken = false;
77  if ((_sequences.find(lastKeyTokenValue) != _sequences.end()) && (!temp.empty())) {
78  _sequences[lastKeyTokenValue] = temp;
79  }
80  temp.clear();
81  lastBlockEntryTokenValue = "";
82  break;
83  }
84  case YAML_BLOCK_ENTRY_TOKEN: {
85  flagBlockEntryToken = true;
86  break;
87  }
88  case YAML_SCALAR_TOKEN: {
89  std::string scalar((char *) token.data.scalar.value);
90 
91  // store the token name in lastKeyTokenValue or in _scalars
92  if (flagKeyToken) {
93  // end flow sequence
94  if (!lastFlowEntryTokenValue.empty()){
95  flagFlowEntryToken = false;
96  if (_sequences.find(lastKeyTokenValue) != _sequences.end()) {
97  _sequences[lastKeyTokenValue] = temp;
98  }
99  temp.clear();
100  lastFlowEntryTokenValue = "";
101  }
102  // end block sequence
103  if (!lastBlockEntryTokenValue.empty()) {
104  flagBlockEntryToken = false;
105  if ((_sequences.find(lastKeyTokenValue) != _sequences.end()) && (!temp.empty())) {
106  _sequences[lastKeyTokenValue] = temp;
107  }
108  temp.clear();
109  lastBlockEntryTokenValue = "";
110  }
111  flagKeyToken = false;
112  lastKeyTokenValue = scalar;
113  } else {
114  if (_scalars.find(lastKeyTokenValue.c_str()) != _scalars.end()) {
115  _scalars[lastKeyTokenValue] = scalar;
116  }
117  }
118 
119  // add flow entry
120  if (flagFlowEntryToken) {
121  flagFlowEntryToken = false;
122  lastFlowEntryTokenValue = scalar;
123  temp.push_back(scalar);
124  }
125 
126  // add block entry
127  if (flagBlockEntryToken) {
128  flagBlockEntryToken = false;
129  lastBlockEntryTokenValue = scalar;
130  temp.push_back(scalar);
131  }
132  break;
133  }
134  default:
135  break;
136  }
137  if (token.type != YAML_STREAM_END_TOKEN) yaml_token_delete(&token);
138 
139  } while (token.type != YAML_STREAM_END_TOKEN);
140  yaml_token_delete(&token);
141  /* END new code */
142 
143  /* Cleanup */
144  yaml_parser_delete(&parser);
145  fclose(fh);
146 
147  return 0; // EXIT_SUCCESS
148  }
149 
150 } // namespace Ox
151 
152 #endif //Tomato_TomatoPARSER_H
Definition: OxCalculator.h:19