aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/dataloader/NodeTools.cpp
blob: 4deffe12e6c9e741e76325cada97371201005aa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "NodeTools.hpp"

using namespace OpenVic;
using namespace OpenVic::NodeTools;

template<typename T>
static node_callback_t _expect_type(callback_t<T const&> callback) {
   return [callback](ast::NodeCPtr node) -> bool {
      if (node != nullptr) {
         T const* cast_node = node->cast_to<T>();
         if (cast_node != nullptr) {
            return callback(*cast_node);
         }
         Logger::error("Invalid node type ", node->get_type(), " when expecting ", T::get_type_static());
      } else {
         Logger::error("Null node when expecting ", T::get_type_static());
      }
      return false;
   };
}

template<typename T>
requires(std::derived_from<T, ast::AbstractStringNode>)
static callback_t<T const&> _abstract_string_node_callback(callback_t<std::string_view> callback) {
   return [callback](T const& node) -> bool {
      return callback(node._name);
   };
}

node_callback_t NodeTools::expect_identifier(callback_t<std::string_view> callback) {
   return _expect_type<ast::IdentifierNode>(_abstract_string_node_callback<ast::IdentifierNode>(callback));
}

node_callback_t NodeTools::expect_string(callback_t<std::string_view> callback) {
   return _expect_type<ast::StringNode>(_abstract_string_node_callback<ast::StringNode>(callback));
}

node_callback_t NodeTools::expect_identifier_or_string(callback_t<std::string_view> callback) {
   return [callback](ast::NodeCPtr node) -> bool {
      if (node != nullptr) {
         ast::AbstractStringNode const* cast_node = node->cast_to<ast::IdentifierNode>();
         if (cast_node == nullptr) {
            cast_node = node->cast_to<ast::StringNode>();
         }
         if (cast_node != nullptr) {
            return _abstract_string_node_callback<ast::AbstractStringNode>(callback)(*cast_node);
         }
         Logger::error("Invalid node type ", node->get_type(), " when expecting ", ast::IdentifierNode::get_type_static(), " or ", ast::StringNode::get_type_static());
      } else {
         Logger::error("Null node when expecting ", ast::IdentifierNode::get_type_static(), " or ", ast::StringNode::get_type_static());
      }
      return false;
   };
}

node_callback_t NodeTools::expect_bool(callback_t<bool> callback) {
   return expect_identifier(
      [callback](std::string_view identifier) -> bool {
         if (identifier == "yes") {
            return callback(true);
         } else if (identifier == "no") {
            return callback(false);
         }
         Logger::error("Invalid bool identifier text: ", identifier);
         return false;
      }
   );
}

node_callback_t NodeTools::expect_int(callback_t<int64_t> callback) {
   return expect_identifier(
      [callback](std::string_view identifier) -> bool {
         bool successful = false;
         const int64_t val = StringUtils::string_to_int64(identifier, &successful, 10);
         if (successful) {
            return callback(val);
         }
         Logger::error("Invalid int identifier text: ", identifier);
         return false;
      }
   );
}

node_callback_t NodeTools::expect_uint(callback_t<uint64_t> callback) {
   return expect_identifier(
      [callback](std::string_view identifier) -> bool {
         bool successful = false;
         const uint64_t val = StringUtils::string_to_uint64(identifier, &successful, 10);
         if (successful) {
            return callback(val);
         }
         Logger::error("Invalid uint identifier text: ", identifier);
         return false;
      }
   );
}

node_callback_t NodeTools::expect_fixed_point(callback_t<fixed_point_t> callback) {
   return expect_identifier(
      [callback](std::string_view identifier) -> bool {
         bool successful = false;
         const fixed_point_t val = fixed_point_t::parse(identifier.data(), identifier.length(), &successful);
         if (successful) {
            return callback(val);
         }
         Logger::error("Invalid fixed point identifier text: ", identifier);
         return false;
      }
   );
}

node_callback_t NodeTools::expect_colour(callback_t<colour_t> callback) {
   return [callback](ast::NodeCPtr node) -> bool {
      colour_t col = NULL_COLOUR;
      uint32_t components = 0;
      bool ret = expect_list_of_length(3,
         expect_fixed_point(
            [&col, &components](fixed_point_t val) -> bool {
               components++;
               col <<= 8;
               if (val < 0 || val > 255) {
                  Logger::error("Invalid colour component: ", val);
                  return false;
               } else {
                  if (val <= 1) val *= 255;
                  col |= val.to_int32_t();
                  return true;
               }
            }
         )
      )(node);
      ret &= callback(col << 8 * (3 - components));
      return ret;
   };
}

node_callback_t NodeTools::expect_date(callback_t<Date> callback) {
   return expect_identifier(
      [callback](std::string_view identifier) -> bool {
         bool successful = false;
         const Date date = Date::from_string(identifier, &successful);
         if (successful) {
            return callback(date);
         }
         Logger::error("Invalid date identifier text: ", identifier);
         return false;
      }
   );
}

template<typename T, node_callback_t (*expect_func)(callback_t<T>)>
node_callback_t _expect_vec2(callback_t<vec2_t<T>> callback) {
   return [callback](ast::NodeCPtr node) -> bool {
      vec2_t<T> vec;
      bool ret = expect_dictionary_keys(
         "x", ONE_EXACTLY, expect_func(assign_variable_callback(vec.x)),
         "y", ONE_EXACTLY, expect_func(assign_variable_callback(vec.y))
      )(node);
      ret &= callback(vec);
      return ret;
   };
}

node_callback_t NodeTools::expect_ivec2(callback_t<ivec2_t> callback) {
   return _expect_vec2<int64_t, expect_int>(callback);
}

node_callback_t NodeTools::expect_fvec2(callback_t<fvec2_t> callback) {
   return _expect_vec2<fixed_point_t, expect_fixed_point>(callback);
}

node_callback_t NodeTools::expect_assign(key_value_callback_t callback) {
   return _expect_type<ast::AssignNode>(
      [callback](ast::AssignNode const& assign_node) -> bool {
         return callback(assign_node._name, assign_node._initializer.get());
      }
   );
}

node_callback_t NodeTools::expect_list_and_length(length_callback_t length_callback, node_callback_t callback) {
   return _expect_type<ast::AbstractListNode>(
      [length_callback, callback](ast::AbstractListNode const& list_node) -> bool {
         std::vector<ast::NodeUPtr> const& list = list_node._statements;
         bool ret = true;
         size_t size = length_callback(list.size());
         if (size > list.size()) {
            Logger::error("Trying to read more values than the list contains: ", size, " > ", list.size());
            size = list.size();
            ret = false;
         }
         std::for_each(list.begin(), list.begin() + size,
            [callback, &ret](ast::NodeUPtr const& sub_node) -> void {
               ret &= callback(sub_node.get());
            }
         );
         return ret;
      }
   );
}

node_callback_t NodeTools::expect_list_of_length(size_t length, node_callback_t callback) {
   return [length, callback](ast::NodeCPtr node) -> bool {
      bool ret = true;
      ret &= expect_list_and_length(
         [length, &ret](size_t size) -> size_t {
            if (size != length) {
               Logger::error("List length ", size, " does not match expected length ", length);
               ret = false;
               if (length < size) return length;
            }
            return size;
         },
         callback
      )(node);
      return ret;
   };
}

node_callback_t NodeTools::expect_list(node_callback_t callback) {
   return expect_list_and_length(default_length_callback, callback);
}

node_callback_t NodeTools::expect_length(callback_t<size_t> callback) {
   return [callback](ast::NodeCPtr node) -> bool {
      bool ret = true;
      ret &= expect_list_and_length(
         [callback, &ret](size_t size) -> size_t {
            ret &= callback(size);
            return 0;
         },
         success_callback
      )(node);
      return ret;
   };
}

node_callback_t NodeTools::expect_dictionary_and_length(length_callback_t length_callback, key_value_callback_t callback) {
   return expect_list_and_length(length_callback, expect_assign(callback));
}

node_callback_t NodeTools::expect_dictionary(key_value_callback_t callback) {
   return expect_dictionary_and_length(default_length_callback, callback);
}

void NodeTools::add_key_map_entry(key_map_t& key_map, std::string_view key, dictionary_entry_t::expected_count_t expected_count, node_callback_t callback) {
   if (key_map.find(key) == key_map.end()) {
      key_map.emplace(key, dictionary_entry_t { expected_count, callback });
   } else {
      Logger::error("Duplicate expected dictionary key: ", key);
   }
}

key_value_callback_t NodeTools::dictionary_keys_callback(key_map_t& key_map, bool allow_other_keys) {
   return [&key_map, allow_other_keys](std::string_view key, ast::NodeCPtr value) -> bool {
      const key_map_t::iterator it = key_map.find(key);
      if (it == key_map.end()) {
         if (allow_other_keys) return true;
         Logger::error("Invalid dictionary key: ", key);
         return false;
      }
      dictionary_entry_t& entry = it->second;
      if (++entry.count > 1 && !entry.can_repeat()) {
         Logger::error("Invalid repeat of dictionary key: ", key);
         return false;
      }
      return entry.callback(value);
   };
}

bool NodeTools::check_key_map_counts(key_map_t const& key_map) {
   bool ret = true;
   for (key_map_t::value_type const& key_entry : key_map) {
      dictionary_entry_t const& entry = key_entry.second;
      if (entry.must_appear() && entry.count < 1) {
         Logger::error("Mandatory dictionary key not present: ", key_entry.first);
         ret = false;
      }
   }
   return ret;
}

node_callback_t NodeTools::_expect_dictionary_keys_and_length(length_callback_t length_callback, bool allow_other_keys, key_map_t&& key_map) {
   return [length_callback, allow_other_keys, key_map = std::move(key_map)](ast::NodeCPtr node) mutable -> bool {
      bool ret = expect_dictionary_and_length(
         length_callback, dictionary_keys_callback(key_map, allow_other_keys)
      )(node);
      ret &= check_key_map_counts(key_map);
      return ret;
   };
}

node_callback_t NodeTools::name_list_callback(std::vector<std::string>& list) {
   return expect_list_reserve_length(
      list,
      expect_identifier_or_string(
         [&list](std::string_view str) -> bool {
            if (!str.empty()) {
               list.push_back(std::string { str });
               return true;
            }
            Logger::error("Empty identifier or string");
            return false;
         }
      )
   );
}