Repository navigation
Expand file tree
/
Copy pathyaml.hpp
More file actions
4330 lines (3610 loc) · 159 KB
/
Copy pathyaml.hpp
File metadata and controls
4330 lines (3610 loc) · 159 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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
<
8
div data
10BC8
-line-number="588" class="react-line-number react-code-text" style="padding-right:16px">588589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* MIT License
*
* Copyright(c) 2023 Jimmie Bergmann
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files(the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
#ifndef MINIYAML_HEADER_FILE
#define MINIYAML_HEADER_FILE
#include <memory>
#include <array>
#include <vector>
#include <map>
#include <string>
#include <limits>
#include <algorithm>
#include <type_traits>
#include <fstream>
#include <cstdlib>
#include <cstdint>
#include <cmath>
#include <climits>
#include <errno.h>
#if defined(MINIYAML_CUSTOM_NAMESPACE)
#define MINIYAML_NAMESPACE MINIYAML_CUSTOM_NAMESPACE
#else
#define MINIYAML_NAMESPACE yaml
#endif
#if defined(__cpp_inline_variables)
#define MINIYAML_INLINE_VARIABLE inline
#else
#define MINIYAML_INLINE_VARIABLE
#endif
#define MINIYAML_NODISCARD
#if defined(__has_cpp_attribute)
#if __has_cpp_attribute(nodiscard)
#undef MINIYAML_NODISCARD
#define MINIYAML_NODISCARD [[nodiscard]]
#endif
#endif
#if defined(__cpp_if_constexpr)
#define MINIYAML_HAS_IF_CONSTEXPR true
#else
#define MINIYAML_HAS_IF_CONSTEXPR false
#endif
#if defined(__cpp_concepts)
#define MINIYAML_HAS_CONCEPTS true
#else
#define MINIYAML_HAS_CONCEPTS false
#endif
#if defined(__cpp_lib_string_view) && !defined(MINIYAML_NO_STD_STRING_VIEW)
#include <string_view>
#define MINIYAML_HAS_STD_STRING_VIEW true
namespace MINIYAML_NAMESPACE {
template<typename T>
using basic_string_view = std::basic_string_view<T>;
using string_view = std::string_view;
}
#else
#define MINIYAML_HAS_STD_STRING_VIEW false
#include <cstring>
namespace MINIYAML_NAMESPACE {
template<typename T>
class basic_string_view {
public:
using value_type = T;
using pointer = T*;
using const_pointer = const T*;
using reference = T&;
using const_reference = const T&;
using size_type = std::size_t;
basic_string_view() :
m_data(nullptr),
m_size(0)
{}
basic_string_view(const_pointer s) :
m_data(s),
m_size(std::strlen(s))
{}
basic_string_view(const_pointer s, size_type count) :
m_data(s),
m_size(count)
{}
value_type operator [](size_type index) const {
return m_data[index];
}
bool operator == (const basic_string_view& rhs) const {
return m_size == rhs.m_size && std::strncmp(m_data, rhs.m_data, m_size) == 0;
}
MINIYAML_NODISCARD const_pointer data() const { return m_data; };
MINIYAML_NODISCARD size_type size() const { return m_size; };
MINIYAML_NODISCARD bool empty() const { return m_size == 0; };
private:
const_pointer m_data;
size_type m_size;
};
using string_view = basic_string_view<char>;
}
#endif
namespace MINIYAML_NAMESPACE {
enum class read_result_code {
success,
cannot_open_file,
reached_max_depth,
reached_max_document_count,
not_implemented,
forbidden_tab_indentation,
bad_indentation,
expected_line_break,
expected_key,
expected_sequence,
unexpected_eof,
unexpected_key,
unexpected_token,
tag_duplication,
};
template<typename T>
struct token {
MINIYAML_INLINE_VARIABLE constexpr static T eof = '\0';
MINIYAML_INLINE_VARIABLE constexpr static T document_start = '-';
MINIYAML_INLINE_VARIABLE constexpr static T document_end = '.';
MINIYAML_INLINE_VARIABLE constexpr static T space = ' ';
MINIYAML_INLINE_VARIABLE constexpr static T tab = '\t';
MINIYAML_INLINE_VARIABLE constexpr static T carriage = '\r';
MINIYAML_INLINE_VARIABLE constexpr static T newline = '\n';
MINIYAML_INLINE_VARIABLE constexpr static T comment = '#';
MINIYAML_INLINE_VARIABLE constexpr static T escape = '\\';
MINIYAML_INLINE_VARIABLE constexpr static T double_quote = '\"';
MINIYAML_INLINE_VARIABLE constexpr static T single_quote = '\'';
MINIYAML_INLINE_VARIABLE constexpr static T object = ':';
//MINIYAML_INLINE_VARIABLE constexpr static T flow_object_start = '{';
//MINIYAML_INLINE_VARIABLE constexpr static T flow_object_end = '}';
MINIYAML_INLINE_VARIABLE constexpr static T sequence = '-';
//MINIYAML_INLINE_VARIABLE constexpr static T flow_sequence_start = '[';
//MINIYAML_INLINE_VARIABLE constexpr static T flow_sequence_end = ']';
MINIYAML_INLINE_VARIABLE constexpr static T null = '~';
MINIYAML_INLINE_VARIABLE constexpr static T literal_block = '|';
MINIYAML_INLINE_VARIABLE constexpr static T folded_block = '>';
MINIYAML_INLINE_VARIABLE constexpr static T chomping_strip = '-';
MINIYAML_INLINE_VARIABLE constexpr static T chomping_keep = '+';
MINIYAML_INLINE_VARIABLE constexpr static T tag = '!';
};
enum class scalar_style_type {
none, // ' '
literal, // | Keep newlines.
folded, // > Replace newlines with spaces.
double_quoted, // ""
single_quoted // ''
};
enum class chomping_type {
clip, // ' ' Single newline at end.
strip, // '-' No newline at end
keep, // '+' All newlines from end
};
}
// SAX declarations.
namespace MINIYAML_NAMESPACE {
namespace sax {
/** SAX reader results. */
template<typename Tchar>
struct read_document_result {
read_result_code result_code = read_result_code::success;
basic_string_view<Tchar> remaining_input = {};
int64_t current_line = 0;
const Tchar* current_line_ptr = nullptr;
operator bool() const;
};
template<typename Tchar>
struct read_document_file_result {
read_result_code result_code = read_result_code::success;
int64_t current_line = 0;
operator bool() const;
};
template<typename Tchar>
using read_documents_result = read_document_result<Tchar>;
template<typename Tchar>
using read_documents_file_result = read_document_file_result<Tchar>;
/** SAX reader options. */
struct reader_options {
size_t max_depth = 128;
size_t max_document_count = 128;
int64_t start_line_number = 0;
};
/** Helper function for parsing via SAX style API. */
template<typename Tchar, typename Tsax_handler>
read_document_result<Tchar> read_document(
const Tchar* raw_input,
size_t size,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_document_result<Tchar> read_document(
const std::basic_string<Tchar>& string,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_document_result<Tchar> read_document(
basic_string_view<Tchar> string_view,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_document_file_result<Tchar> read_document_from_file(
const std::string& filename,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_documents_result<Tchar> read_documents(
const Tchar* raw_input,
size_t size,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_documents_result<Tchar> read_documents(
const std::basic_string<Tchar>& string,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_documents_result<Tchar> read_documents(
basic_string_view<Tchar> string_view,
Tsax_handler& handler,
const reader_options& options = {});
template<typename Tchar, typename Tsax_handler>
read_documents_file_result<Tchar> read_documents_from_file(
const std::string& filename,
Tsax_handler& handler,
const reader_options& options = {});
/** Helper base struct for SAX handler. */
struct handler_base {
virtual void start_scalar(scalar_style_type, chomping_type) {}
virtual void end_scalar() {}
virtual void start_object() {}
virtual void end_object() {}
virtual void start_array() {}
virtual void end_array() {}
virtual void null() {}
virtual void string(basic_string_view<uint8_t>) {}
virtual void key(basic_string_view<uint8_t>) {}
virtual void comment(basic_string_view<uint8_t>) {}
};
/** SAX reader. */
template<typename Tchar, typename Tsax_handler>
class reader {
public:
using char_type = Tchar;
using pointer = char_type*;
using const_pointer = const char_type*;
using size_type = std::size_t;
using string_type = std::basic_string<char_type>;
using string_view_type = MINIYAML_NAMESPACE::basic_string_view<char_type>;
using sax_handler_type = Tsax_handler;
using token_type = token<Tchar>;
using read_document_result_type = read_document_result<char_type>;
using read_document_file_result_type = read_document_file_result<char_type>;
using read_documents_result_type = read_documents_result<char_type>;
using read_documents_file_result_type = read_documents_file_result<char_type>;
explicit reader(sax_handler_type& sax_handler, const reader_options& options = {});
read_document_result_type read_document(const_pointer raw_input, size_type size);
read_document_result_type read_document(const string_type& string);
read_document_result_type read_document(string_view_type string_view);
read_document_file_result_type read_document_from_file(const std::string& filename);
read_documents_result_type read_documents(const_pointer raw_input, size_type size);
read_documents_result_type read_documents(const string_type& string);
read_documents_result_type read_documents(string_view_type string_view);
read_documents_file_result_type read_documents_from_file(const std::string& filename);
private:
using state_function_t = void (reader::*)();
enum class stack_type {
unknown,
scalar,
object,
sequence
};
struct stack_item_t {
stack_item_t() = default;
stack_item_t(const stack_item_t&) = delete;
stack_item_t(stack_item_t&&) = default;
~stack_item_t() = default;
stack_item_t& operator = (stack_item_t&&) = default;
stack_item_t& operator = (const stack_item_t&) = delete;
bool is_flow_value() const {
return flow_value_token != token_type::eof;
}
state_function_t state_function = nullptr;
stack_type type = stack_type::unknown;
int64_t type_indention = 0;
int64_t processed_lines = 0;
char_type flow_value_token = token_type::eof;
bool has_tag = false;
};
using stack_t = std::vector<stack_item_t>;
using stack_iterator_t = typename stack_t::iterator;
using stack_reverse_iterator_t = typename stack_t::reverse_iterator;
const_pointer m_current_ptr;
const_pointer m_end_ptr;
const_pointer m_begin_ptr;
stack_t m_stack;
read_result_code m_current_result_code;
sax_handler_type& m_sax_handler;
int64_t m_current_line;
const_pointer m_current_line_ptr;
int64_t m_current_line_indention;
const_pointer m_current_line_indention_ptr;
bool m_current_is_new_line;
const reader_options& m_options;
static const_pointer skip_utf8_bom(const_pointer begin, const_pointer end);
void initialize_process(const_pointer raw_input, size_type size);
read_result_code process_document();
read_result_code process_documents();
read_document_result_type create_read_document_result(read_result_code result_code) const;
read_document_file_result_type create_read_document_file_result(read_result_code result_code) const;
read_documents_result_type create_read_documents_result(read_result_code result_code) const;
read_documents_file_result_type create_read_documents_file_result(read_result_code result_code) const;
void execute_find_value();
void execute_read_scalar();
void execute_read_scalar_block();
void execute_read_key();
void execute_read_sequence();
void execute_read_flow_scalar_quote();
void register_newline();
void register_line_indentation();
void read_comment_until_newline();
void consume_whitespaces_until_any();
bool consume_whitespaces_until_newline();
bool consume_only_whitespaces_until_newline();
bool consume_only_whitespaces_until_newline_or_comment();
bool has_min_tokens_left(size_t count) const;
bool is_next_token(size_t increments, const char_type value) const;
bool is_prev_token_whitespace(int decrements = 1) const;
bool is_next_token_whitespace(int increments = 1) const;
bool is_current_stack_flow_value() const;
void signal_start_document();
void signal_end_document();
void signal_start_scalar(scalar_style_type style, chomping_type chomping);
void signal_end_scalar();
void signal_start_object();
void signal_end_object();
void signal_start_array();
void signal_end_array();
void signal_null();
void signal_index(size_t value);
void signal_string(string_view_type value);
void signal_key(string_view_type value);
void signal_tag(string_view_type value);
void signal_comment(string_view_type value);
void error(const read_result_code result_code);
stack_item_t& push_stack(state_function_t state_function);
void pop_stack();
void pop_stack_if_not_root();
void pop_stack_from(stack_iterator_t it);
void signal_stack_item_pop(const stack_item_t& stack_item);
};
} }
// DOM declarations.
namespace MINIYAML_NAMESPACE {
namespace dom {
enum class node_type {
null,
scalar,
object,
array
};
template<typename Tchar, typename Ttraits> class node;
template<typename Tchar, typename Ttraits> class scalar_node;
template<typename Tchar, typename Ttraits> class object_node;
template<typename Tchar, typename Ttraits> class array_node;
template<typename Tchar, typename Tstring = std::basic_string<Tchar>>
struct default_node_traits {
using char_type = Tchar;
using node_value = node<char_type, default_node_traits>;
using node_reference = node_value&;
using node_pointer = node_value*;
using node_owned_pointer = std::unique_ptr<node_value>;
using scalar_node_type = scalar_node<char_type, default_node_traits>;
using object_node_type = object_node<char_type, default_node_traits>;
using array_node_type = array_node<char_type, default_node_traits>;
using string_type = Tstring;
using object_collection = std::map<string_type, node_owned_pointer>;
using array_collection = std::vector<node_owned_pointer>;
};
template<typename Tchar = char>
using default_node_view_traits = default_node_traits<Tchar, MINIYAML_NAMESPACE::basic_string_view<Tchar>>;
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
class node {
public:
using traits = Ttraits;
using scalar_node_type = typename traits::scalar_node_type;
using object_node_type = typename traits::object_node_type;
using array_node_type = typename traits::array_node_type;
using string_type = typename traits::string_type;
static node create_scalar(scalar_style_type style = scalar_style_type::none, chomping_type chomping = chomping_type::strip);
static node create_object();
static node create_array();
node();
~node();
node(node&&) noexcept;
node& operator = (node&&) noexcept(false);
node(const node&) = delete;
node& operator = (const node&) = delete;
MINIYAML_NODISCARD node_type type() const;
MINIYAML_NODISCARD bool is_null() const;
MINIYAML_NODISCARD bool is_scalar() const;
MINIYAML_NODISCARD bool is_object() const;
MINIYAML_NODISCARD bool is_array() const;
MINIYAML_NODISCARD scalar_node_type& as_scalar();
MINIYAML_NODISCARD const scalar_node_type& as_scalar() const;
MINIYAML_NODISCARD object_node_type& as_object();
MINIYAML_NODISCARD const object_node_type& as_object() const;
MINIYAML_NODISCARD array_node_type& as_array();
MINIYAML_NODISCARD const array_node_type& as_array() const;
template<typename Tvalue>
MINIYAML_NODISCARD Tvalue as(Tvalue default_value = Tvalue{}) const;
MINIYAML_NODISCARD string_type& tag();
MINIYAML_NODISCARD const string_type& tag() const;
void tag(string_type tag);
MINIYAML_NODISCARD bool empty() const;
MINIYAML_NODISCARD size_t size() const;
MINIYAML_NODISCARD bool contains(string_type key) const;
MINIYAML_NODISCARD bool contains(size_t index) const;
MINIYAML_NODISCARD node& at(string_type key);
MINIYAML_NODISCARD const node& at(string_type key) const;
MINIYAML_NODISCARD node& at(size_t index);
MINIYAML_NODISCARD const node& at(size_t index) const;
MINIYAML_NODISCARD node& operator[](string_type key);
MINIYAML_NODISCARD node& operator[](size_t index);
MINIYAML_NODISCARD const node& operator[](size_t index) const;
private:
void destroy_underlying_node();
void assign_this_overlying_to_underlying_node();
node_type m_node_type;
union underlying_node {
scalar_node_type* scalar;
object_node_type* object;
array_node_type* array;
} m_underlying_node;
string_type m_tag;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
class scalar_node {
public:
using traits = Ttraits;
using node_value = typename traits::node_value;
using string_type = typename traits::string_type;
using lines_collection = std::vector<string_type>;
using iterator = typename lines_collection::iterator;
using const_iterator = typename lines_collection::const_iterator;
using reverse_iterator = typename lines_collection::reverse_iterator;
using const_reverse_iterator = typename lines_collection::const_reverse_iterator;
scalar_node(node_value& overlying_node, scalar_style_type style, chomping_type chomping);
scalar_node(const scalar_node&) = delete;
scalar_node(scalar_node&&) = delete;
scalar_node& operator = (const scalar_node&) = delete;
scalar_node& operator = (scalar_node&&) = delete;
template<typename Tvalue>
MINIYAML_NODISCARD Tvalue as(Tvalue default_value = Tvalue{}) const;
MINIYAML_NODISCARD scalar_style_type& style();
MINIYAML_NODISCARD scalar_style_type style() const;
void style(scalar_style_type value);
MINIYAML_NODISCARD chomping_type& chomping();
MINIYAML_NODISCARD chomping_type chomping() const;
void chomping(const chomping_type value);
MINIYAML_NODISCARD bool empty() const;
MINIYAML_NODISCARD size_t size() const;
MINIYAML_NODISCARD iterator begin();
MINIYAML_NODISCARD const_iterator begin() const;
MINIYAML_NODISCARD const_iterator cbegin() const;
MINIYAML_NODISCARD iterator end();
MINIYAML_NODISCARD const_iterator end() const;
MINIYAML_NODISCARD const_iterator cend() const;
MINIYAML_NODISCARD reverse_iterator rbegin();
MINIYAML_NODISCARD const_reverse_iterator rbegin() const;
MINIYAML_NODISCARD const_reverse_iterator crbegin() const;
MINIYAML_NODISCARD reverse_iterator rend();
MINIYAML_NODISCARD const_reverse_iterator rend() const;
MINIYAML_NODISCARD const_reverse_iterator crend() const;
MINIYAML_NODISCARD string_type& at(size_t index);
MINIYAML_NODISCARD const string_type& at(size_t index) const;
MINIYAML_NODISCARD string_type& operator[](size_t index);
MINIYAML_NODISCARD const string_type& operator[](size_t index) const;
iterator insert(const_iterator pos, string_type string);
void push_back(string_type string);
void push_front(string_type string);
void pop_back();
void pop_front();
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
MINIYAML_NODISCARD node_value& overlying_node();
MINIYAML_NODISCARD const node_value& overlying_node() const;
private:
template<typename Tchar_Other, typename Ttraits_Other>
friend class node;
MINIYAML_NODISCARD std::basic_string<Tchar> as_non_block_string() const;
MINIYAML_NODISCARD std::basic_string<Tchar> as_literal_string() const;
MINIYAML_NODISCARD std::basic_string<Tchar> as_folded_string() const;
lines_collection m_lines;
scalar_style_type m_style;
chomping_type m_chomping;
node_value* m_overlying_node;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
class object_node {
public:
using traits = Ttraits;
using node_value = typename traits::node_value;
using node_owned_pointer = typename traits::node_owned_pointer;
using string_type = typename traits::string_type;
using collection = typename traits::object_collection;
using iterator = typename collection::iterator;
using const_iterator = typename collection::const_iterator;
using reverse_iterator = typename collection::reverse_iterator;
using const_reverse_iterator = typename collection::const_reverse_iterator;
using insert_return_type = std::pair<iterator, bool>;
explicit object_node(node_value& overlying_node);
~object_node() = default;
object_node(const object_node&) = delete;
object_node(object_node&&) = delete;
object_node& operator = (const object_node&) = delete;
object_node& operator = (object_node&&) = delete;
MINIYAML_NODISCARD bool empty() const;
MINIYAML_NODISCARD size_t size() const;
MINIYAML_NODISCARD bool contains(const string_type& key) const;
MINIYAML_NODISCARD iterator begin();
MINIYAML_NODISCARD const_iterator begin() const;
MINIYAML_NODISCARD const_iterator cbegin() const;
MINIYAML_NODISCARD iterator end();
MINIYAML_NODISCARD const_iterator end() const;
MINIYAML_NODISCARD const_iterator cend() const;
MINIYAML_NODISCARD reverse_iterator rbegin();
MINIYAML_NODISCARD const_reverse_iterator rbegin() const;
MINIYAML_NODISCARD const_reverse_iterator crbegin() const;
MINIYAML_NODISCARD reverse_iterator rend();
MINIYAML_NODISCARD const_reverse_iterator rend() const;
MINIYAML_NODISCARD const_reverse_iterator crend() const;
MINIYAML_NODISCARD iterator find(const string_type& key);
MINIYAML_NODISCARD const_iterator find(const string_type& key) const;
MINIYAML_NODISCARD node_value& at(const string_type& key);
MINIYAML_NODISCARD const node_value& at(const string_type& key) const;
MINIYAML_NODISCARD node_value& operator [] (const string_type& key);
insert_return_type insert(const string_type& key);
insert_return_type insert(const string_type& key, node_value&& node);
size_t erase(const string_type& key);
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
MINIYAML_NODISCARD node_value& overlying_node();
MINIYAML_NODISCARD const node_value& overlying_node() const;
private:
template<typename Tchar_Other, typename Ttraits_Other>
friend class node;
collection m_map;
node_value* m_overlying_node;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
class array_node {
public:
using traits = Ttraits;
using node_value = typename traits::node_value;
using node_owned_pointer = typename traits::node_owned_pointer;
using collection = typename traits::array_collection;
using iterator = typename collection::iterator;
using const_iterator = typename collection::const_iterator;
using reverse_iterator = typename collection::reverse_iterator;
using const_reverse_iterator = typename collection::const_reverse_iterator;
explicit array_node(node_value& overlying_node);
array_node(const array_node&) = delete;
array_node(array_node&&) = delete;
array_node& operator = (const array_node&) = delete;
array_node& operator = (array_node&&) = delete;
MINIYAML_NODISCARD bool empty() const;
MINIYAML_NODISCARD size_t size() const;
MINIYAML_NODISCARD bool contains(size_t index) const;
MINIYAML_NODISCARD iterator begin();
MINIYAML_NODISCARD const_iterator begin() const;
MINIYAML_NODISCARD const_iterator cbegin() const;
MINIYAML_NODISCARD iterator end();
MINIYAML_NODISCARD const_iterator end() const;
MINIYAML_NODISCARD const_iterator cend() const;
MINIYAML_NODISCARD reverse_iterator rbegin();
MINIYAML_NODISCARD const_reverse_iterator rbegin() const;
MINIYAML_NODISCARD const_reverse_iterator crbegin() const;
MINIYAML_NODISCARD reverse_iterator rend();
MINIYAML_NODISCARD const_reverse_iterator rend() const;
MINIYAML_NODISCARD const_reverse_iterator crend() const;
MINIYAML_NODISCARD node_value& at(size_t index);
MINIYAML_NODISCARD const node_value& at(size_t index) const;
MINIYAML_NODISCARD node_value& operator[](size_t index);
MINIYAML_NODISCARD const node_value& operator[](size_t index) const;
iterator insert(const_iterator pos);
iterator insert(const_iterator pos, node_value&& node);
void push_back();
void push_back(node_value&& node);
void pop_back();
iterator erase(iterator pos);
iterator erase(iterator first, iterator last);
iterator erase(const_iterator pos);
iterator erase(const_iterator first, const_iterator last);
MINIYAML_NODISCARD node_value& overlying_node();
MINIYAML_NODISCARD const node_value& overlying_node() const;
private:
template<typename Tchar_Other, typename Ttraits_Other>
friend class node;
collection m_list;
node_value* m_overlying_node;
};
/** DOM reader results. */
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
struct read_document_result {
using node_value = node<Tchar, Ttraits>;
read_result_code result_code = read_result_code::success;
basic_string_view<Tchar> remaining_input = {};
int64_t current_line = 0;
const Tchar* current_line_ptr = nullptr;
node_value root_node = {};
operator bool() const;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
struct read_document_file_result {
using node_value = node<Tchar, Ttraits>;
read_result_code result_code = read_result_code::success;
int64_t current_line = 0;
node_value root_node = {};
operator bool() const;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
struct read_documents_result {
using node_value = node<Tchar, Ttraits>;
using node_values = std::vector<node_value>;
read_result_code result_code = read_result_code::success;
basic_string_view<Tchar> remaining_input = {};
int64_t current_line = 0;
const Tchar* current_line_ptr = nullptr;
node_values root_nodes = {};
operator bool() const;
};
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
struct read_documents_file_result {
using node_value = node<Tchar, Ttraits>;
using node_values = std::vector<node_value>;
read_result_code result_code = read_result_code::success;
int64_t current_line = 0;
node_values root_nodes = {};
operator bool() const;
};
/** DOM reader options. */
using reader_options = sax::reader_options;
/** Helper function for parsing via DOM style API. */
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_document_result<Tchar, Ttraits> read_document(
const Tchar* raw_input,
size_t size,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_document_result<Tchar, Ttraits> read_document(
const std::basic_string<Tchar>& string,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_document_result<Tchar, Ttraits> read_document(
basic_string_view<Tchar> string_view,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_document_file_result<Tchar, Ttraits> read_document_from_file(
const std::string& filename,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_documents_result<Tchar, Ttraits> read_documents(
const Tchar* raw_input,
size_t size,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_documents_result<Tchar, Ttraits> read_documents(
const std::basic_string<Tchar>& string,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_documents_result<Tchar, Ttraits> read_documents(
basic_string_view<Tchar> string_view,
const reader_options& options = {});
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
read_documents_file_result<Tchar, Ttraits> read_documents_from_file(
const std::string& filename,
const reader_options& options = {});
/** DOM reader. */
template<typename Tchar = char, typename Ttraits = default_node_traits<Tchar>>
class reader {
public:
using char_type = Tchar;
using pointer = char_type*;
using const_pointer = const char_type*;
using size_type = std::size_t;
using node_value = typename Ttraits::node_value;
using string_type = std::basic_string<char_type>;
using string_view_type = MINIYAML_NAMESPACE::basic_string_view<char_type>;
using token_type = token<Tchar>;
using read_document_result_type = read_document_result<char_type, Ttraits>;
using read_document_file_result_type = read_document_file_result<char_type, Ttraits>;
using read_documents_result_type = read_documents_result<char_type, Ttraits>;
using read_documents_file_result_type = read_documents_file_result<char_type, Ttraits>;
explicit reader(const reader_options& options = {});
read_document_result_type read_document(const_pointer raw_input, size_type size);
read_document_result_type read_document(const string_type& string);
read_document_result_type read_document(string_view_type string_view);
read_document_file_result_type read_document_from_file(const std::string& filename);
read_documents_result_type read_documents(const_pointer raw_input, size_type size);
read_documents_result_type read_documents(const string_type& string);
read_documents_result_type read_documents(string_view_type string_view);
read_documents_file_result_type read_documents_from_file(const std::string& filename);
private:
using node_values = std::vector<node_value>;
class sax_handler {
public:
sax_handler();
void initialize();
void start_document();
void end_document();
void start_scalar(scalar_style_type, chomping_type);
void end_scalar();
void start_object();
void end_object();
void start_array();
void end_array();
void null();
void index(size_t);
void string(string_view_type);
void key(string_view_type);
void tag(string_view_type);
void comment(string_view_type);
using node_stack_t = std::vector<node_value*>;
using string_view_list = std::vector<string_view_type>;
using string_view_list_iterator = typename string_view_list::iterator;
void push_stack(node_value* node);
void pop_stack();
node_values m_documents;
node_value* m_current_node;
node_stack_t m_node_stack;
string_view_list m_string_views;
scalar_style_type m_current_style;
chomping_type m_current_chomping;
};
using sax_handler_type = sax_handler;
using sax_read_document_result_type = sax::read_document_result<Tchar>;
using sax_read_document_file_result_type = sax::read_document_file_result<Tchar>;
using sax_read_documents_result_type = sax::read_documents_result<Tchar>;
using sax_read_documents_file_result_type = sax::read_documents_file_result<Tchar>;
static read_document_result_type create_read_document_result(sax_read_document_result_type sax_result, node_value&& node);
static read_document_file_result_type create_read_document_file_result(sax_read_document_file_result_type sax_result, node_value&& node);
static read_documents_result_type create_read_documents_result(sax_read_documents_result_type sax_result, node_values&& nodes);
static read_documents_file_result_type create_read_documents_file_result(sax_read_documents_file_result_type sax_result, node_values&& nodes);
sax_handler_type m_sax_handler;
sax::reader<Tchar, sax_handler_type> m_sax_reader;
};
} }
// Hidden implementation details.
namespace MINIYAML_NAMESPACE {
namespace impl {
enum class read_file_result_code {
success,
cannot_open_file
};
template<typename Tchar>
struct read_file_result {
read_file_result_code result_code = read_file_result_code::success;
std::vector<Tchar> data = {};
};
template<typename Tchar>
read_file_result<Tchar> read_file(const std::string& filename) {
static_assert(sizeof(Tchar) == sizeof(char), "Tchar is not compatible with ifstream::read.");
auto result = read_file_result<Tchar>{};