8000 mini-yaml/yaml/yaml.hpp at dev · Renal-Of-Loon/mini-yaml · GitHub
[go: up one dir, main page]

Skip to content

Latest commit

 

History

History
4330 lines (3610 loc) · 159 KB

yaml.hpp

File metadata and controls

4330 lines (3610 loc) · 159 KB
/*
* 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>{};
0