Repository navigation
Expand file tree
/
Copy pathyaml.cpp
More file actions
353 lines (310 loc) · 11.3 KB
/
Copy pathyaml.cpp
File metadata and controls
353 lines (310 loc) · 11.3 KB
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
#include "engine/framework/io/yaml.h"
#include <yaml.h>
#include <sstream>
#include <stdexcept>
#include <string>
#include <string_view>
namespace engine::io::yaml {
namespace {
std::string parser_error_message(const yaml_parser_t & parser) {
std::ostringstream message;
message << "yaml parse error";
if (parser.problem != nullptr) {
message << ": " << parser.problem;
}
if (parser.problem_mark.line != 0 || parser.problem_mark.column != 0 || parser.problem_mark.index != 0) {
message << " at line " << (parser.problem_mark.line + 1)
<< ", column " << (parser.problem_mark.column + 1);
}
return message.str();
}
const yaml_node_t & require_node(yaml_document_t & document, int index, const std::string & context) {
yaml_node_t * node = yaml_document_get_node(&document, index);
if (node == nullptr) {
throw std::runtime_error("yaml document is missing node for " + context);
}
return *node;
}
std::string require_scalar_value(yaml_document_t & document, int index, const std::string & context) {
const yaml_node_t & node = require_node(document, index, context);
if (node.type != YAML_SCALAR_NODE) {
throw std::runtime_error("yaml " + context + " must be a scalar");
}
return std::string(reinterpret_cast<const char *>(node.data.scalar.value), node.data.scalar.length);
}
std::string join_path(std::string_view prefix, std::string_view key) {
if (prefix.empty()) {
return std::string(key);
}
std::string path(prefix);
path += '.';
path += key;
return path;
}
void flatten_node(
yaml_document_t & document,
int index,
std::string_view path,
FlattenedDocument & flattened) {
const std::string context(path);
const yaml_node_t & node = require_node(document, index, context);
switch (node.type) {
case YAML_SCALAR_NODE: {
if (path.empty()) {
throw std::runtime_error("yaml document root must be a mapping");
}
flattened.scalars[std::string(path)] = std::string(
reinterpret_cast<const char *>(node.data.scalar.value),
node.data.scalar.length);
return;
}
case YAML_MAPPING_NODE: {
for (yaml_node_pair_t * pair = node.data.mapping.pairs.start;
pair != node.data.mapping.pairs.top;
++pair) {
const std::string key = require_scalar_value(document, pair->key, "mapping key");
if (key.empty()) {
continue;
}
flatten_node(document, pair->value, join_path(path, key), flattened);
}
return;
}
case YAML_SEQUENCE_NODE: {
if (path.empty()) {
throw std::runtime_error("yaml document root sequence is not supported");
}
auto & values = flattened.lists[std::string(path)];
for (yaml_node_item_t * item = node.data.sequence.items.start;
item != node.data.sequence.items.top;
++item) {
values.push_back(require_scalar_value(document, *item, "sequence item for key '" + std::string(path) + "'"));
}
return;
}
case YAML_NO_NODE:
throw std::runtime_error("yaml document is missing node data");
default:
throw std::runtime_error("yaml alias nodes are not supported");
}
}
class Parser {
public:
explicit Parser(std::string_view text) {
if (!yaml_parser_initialize(&parser_)) {
throw std::runtime_error("failed to initialize yaml parser");
}
initialized_ = true;
yaml_parser_set_input_string(
&parser_,
reinterpret_cast<const unsigned char *>(text.data()),
text.size());
}
~Parser() {
if (primary_loaded_) {
yaml_document_delete(&primary_document_);
}
if (initialized_) {
yaml_parser_delete(&parser_);
}
}
yaml_document_t & load_primary() {
load_document(primary_document_, primary_loaded_);
return primary_document_;
}
private:
void load_document(yaml_document_t & document, bool & loaded_flag) {
if (loaded_flag) {
return;
}
if (!yaml_parser_load(&parser_, &document)) {
throw std::runtime_error(parser_error_message(parser_));
}
loaded_flag = true;
}
yaml_parser_t parser_{};
bool initialized_ = false;
yaml_document_t primary_document_{};
bool primary_loaded_ = false;
};
class EventParser {
public:
explicit EventParser(std::string_view text) {
if (!yaml_parser_initialize(&parser_)) {
throw std::runtime_error("failed to initialize yaml parser");
}
initialized_ = true;
yaml_parser_set_input_string(
&parser_,
reinterpret_cast<const unsigned char *>(text.data()),
text.size());
}
~EventParser() {
if (initialized_) {
yaml_parser_delete(&parser_);
}
}
yaml_parser_t & parser() noexcept {
return parser_;
}
private:
yaml_parser_t parser_{};
bool initialized_ = false;
};
void require_single_document(std::string_view text) {
EventParser parser(text);
int documents = 0;
bool done = false;
while (!done) {
yaml_event_t event{};
if (!yaml_parser_parse(&parser.parser(), &event)) {
throw std::runtime_error(parser_error_message(parser.parser()));
}
const auto event_type = event.type;
yaml_event_delete(&event);
if (event_type == YAML_DOCUMENT_START_EVENT) {
++documents;
if (documents > 1) {
throw std::runtime_error("yaml config must contain exactly one document");
}
} else if (event_type == YAML_STREAM_END_EVENT) {
done = true;
}
}
}
} // namespace
ConfigMap parse_scalar_mapping(std::string_view text) {
require_single_document(text);
Parser parser(text);
yaml_document_t & document = parser.load_primary();
yaml_node_t * root = yaml_document_get_root_node(&document);
if (root == nullptr) {
return {};
}
if (root->type != YAML_MAPPING_NODE) {
throw std::runtime_error("yaml config root must be a mapping");
}
ConfigMap config;
const auto pair_count = static_cast<size_t>(root->data.mapping.pairs.top - root->data.mapping.pairs.start);
config.reserve(pair_count);
for (yaml_node_pair_t * pair = root->data.mapping.pairs.start;
pair != root->data.mapping.pairs.top;
++pair) {
const std::string key = require_scalar_value(document, pair->key, "mapping key");
const std::string value = require_scalar_value(document, pair->value, "mapping value for key '" + key + "'");
if (!key.empty()) {
config[key] = value;
}
}
return config;
}
FlattenedDocument parse_flattened_document(std::string_view text) {
require_single_document(text);
Parser parser(text);
yaml_document_t & document = parser.load_primary();
yaml_node_t * root = yaml_document_get_root_node(&document);
if (root == nullptr) {
return {};
}
if (root->type != YAML_MAPPING_NODE) {
throw std::runtime_error("yaml config root must be a mapping");
}
FlattenedDocument flattened;
for (yaml_node_pair_t * pair = root->data.mapping.pairs.start;
pair != root->data.mapping.pairs.top;
++pair) {
const std::string key = require_scalar_value(document, pair->key, "mapping key");
if (key.empty()) {
continue;
}
flatten_node(document, pair->value, key, flattened);
}
return flattened;
}
bool parse_bool_scalar(const std::string & value, const std::string & key) {
if (value == "true") {
return true;
}
if (value == "false") {
return false;
}
throw std::runtime_error("yaml boolean key has invalid value: " + key);
}
std::string require_string(const FlattenedDocument & document, const std::string & key) {
const auto it = document.scalars.find(key);
if (it == document.scalars.end()) {
throw std::runtime_error("Missing yaml scalar key: " + key);
}
return it->second;
}
std::string optional_string(const FlattenedDocument & document, const std::string & key, std::string default_value) {
const auto it = document.scalars.find(key);
return it == document.scalars.end() ? std::move(default_value) : it->second;
}
int require_int(const FlattenedDocument & document, const std::string & key) {
return std::stoi(require_string(document, key));
}
int64_t require_i64(const FlattenedDocument & document, const std::string & key) {
return std::stoll(require_string(document, key));
}
float require_float(const FlattenedDocument & document, const std::string & key) {
return std::stof(require_string(document, key));
}
bool require_bool(const FlattenedDocument & document, const std::string & key) {
return parse_bool_scalar(require_string(document, key), key);
}
bool optional_bool(const FlattenedDocument & document, const std::string & key, bool default_value) {
const auto it = document.scalars.find(key);
return it == document.scalars.end() ? default_value : parse_bool_scalar(it->second, key);
}
std::optional<int> optional_int(const FlattenedDocument & document, const std::string & key) {
const auto it = document.scalars.find(key);
if (it == document.scalars.end()) {
return std::nullopt;
}
return std::stoi(it->second);
}
std::optional<float> optional_float(const FlattenedDocument & document, const std::string & key) {
const auto it = document.scalars.find(key);
if (it == document.scalars.end()) {
return std::nullopt;
}
return std::stof(it->second);
}
float optional_f32(const FlattenedDocument & document, const std::string & key, float default_value) {
const auto value = optional_float(document, key);
return value.value_or(default_value);
}
std::optional<float> optional_nullable_f32(const FlattenedDocument & document, const std::string & key) {
const auto it = document.scalars.find(key);
if (it == document.scalars.end() || it->second == "None" || it->second == "null") {
return std::nullopt;
}
return std::stof(it->second);
}
std::vector<std::string> require_list_strings(const FlattenedDocument & document, const std::string & key) {
const auto it = document.lists.find(key);
if (it == document.lists.end() || it->second.empty()) {
throw std::runtime_error("Missing yaml list key: " + key);
}
return it->second;
}
std::vector<int> require_list_int(const FlattenedDocument & document, const std::string & key) {
const auto values = require_list_strings(document, key);
std::vector<int> out;
out.reserve(values.size());
for (const auto & value : values) {
out.push_back(std::stoi(value));
}
return out;
}
std::vector<int64_t> require_list_i64(const FlattenedDocument & document, const std::string & key) {
const auto values = require_list_strings(document, key);
std::vector<int64_t> out;
out.reserve(values.size());
for (const auto & value : values) {
out.push_back(std::stoll(value));
}
return out;
}
} // namespace engine::io::yaml