2020-03-12 15:23:25 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) 2019-2020 Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
2020-04-10 16:46:00 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-03-12 15:23:25 +00:00
|
|
|
|
2020-01-04 14:21:38 +00:00
|
|
|
#pragma once
|
2020-04-09 08:13:12 +00:00
|
|
|
//# {{
|
2020-04-10 16:46:00 +00:00
|
|
|
#include "toml_preprocessor.h"
|
2020-04-09 08:13:12 +00:00
|
|
|
#if !TOML_PARSER
|
|
|
|
#error This header cannot not be included when TOML_PARSER is disabled.
|
|
|
|
#endif
|
|
|
|
//# }}
|
2020-01-04 14:21:38 +00:00
|
|
|
#include "toml_table.h"
|
2020-04-09 08:13:12 +00:00
|
|
|
#include "toml_utf8_streams.h"
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_PUSH_WARNINGS
|
|
|
|
TOML_DISABLE_PADDING_WARNINGS
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
namespace toml
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-04-10 16:46:00 +00:00
|
|
|
#if TOML_DOXYGEN || !TOML_EXCEPTIONS
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ABI_NAMESPACE_START(parse_noex)
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief The result of a parsing operation.
|
|
|
|
///
|
|
|
|
/// \detail A parse_result is effectively a discriminated union containing either a toml::table
|
|
|
|
/// or a toml::parse_error. Most member functions assume a particular one of these two states,
|
|
|
|
/// and calling them when in the wrong state will cause errors (e.g. attempting to access the
|
|
|
|
/// error object when parsing was successful). \cpp
|
|
|
|
/// parse_result result = toml::parse_file("config.toml");
|
|
|
|
/// if (result)
|
|
|
|
/// do_stuff_with_a_table(result); //implicitly converts to table&
|
|
|
|
/// else
|
|
|
|
/// std::cerr << "Parse failed:\n"sv << result.error() << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// example output:
|
|
|
|
///
|
|
|
|
/// Parse failed:
|
|
|
|
/// Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
|
|
|
|
/// (error occurred at line 1, column 13 of 'config.toml')
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-04-03 13:33:02 +00:00
|
|
|
/// Getting node_views (`operator[]`) and using the iterator accessor functions (`begin(), end()` etc.) are
|
|
|
|
/// unconditionally safe; when parsing fails these just return 'empty' values. A ranged-for loop on a failed
|
|
|
|
/// parse_result is also safe since `begin()` and `end()` return the same iterator and will not lead to any
|
|
|
|
/// dereferences and iterations.
|
|
|
|
///
|
2020-03-08 21:54:57 +00:00
|
|
|
/// \attention <strong>This type only exists when exceptions are not enabled.</strong>
|
2020-03-01 14:56:40 +00:00
|
|
|
/// Otherwise parse_result is just an alias for toml::table: \cpp
|
2020-03-08 21:54:57 +00:00
|
|
|
/// #if TOML_EXCEPTIONS
|
2020-03-01 14:56:40 +00:00
|
|
|
/// using parse_result = table;
|
|
|
|
/// #else
|
|
|
|
/// class parse_result final { // ...
|
|
|
|
/// #endif
|
|
|
|
/// \ecpp
|
|
|
|
class parse_result final
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::aligned_storage_t<
|
|
|
|
(sizeof(table) < sizeof(parse_error) ? sizeof(parse_error) : sizeof(table)),
|
|
|
|
(alignof(table) < alignof(parse_error) ? alignof(parse_error) : alignof(table))
|
|
|
|
> storage;
|
|
|
|
bool is_err;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
void destroy() noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
if (is_err)
|
2020-03-18 13:28:00 +00:00
|
|
|
TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage))->~parse_error();
|
2020-01-04 14:21:38 +00:00
|
|
|
else
|
2020-03-18 13:28:00 +00:00
|
|
|
TOML_LAUNDER(reinterpret_cast<table*>(&storage))->~table();
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
public:
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-03 13:33:02 +00:00
|
|
|
/// \brief A BidirectionalIterator for iterating over key-value pairs in a wrapped toml::table.
|
|
|
|
using iterator = table_iterator;
|
|
|
|
|
|
|
|
/// \brief A BidirectionalIterator for iterating over const key-value pairs in a wrapped toml::table.
|
|
|
|
using const_iterator = const_table_iterator;
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns true if parsing succeeeded.
|
|
|
|
[[nodiscard]] bool succeeded() const noexcept { return !is_err; }
|
|
|
|
/// \brief Returns true if parsing failed.
|
|
|
|
[[nodiscard]] bool failed() const noexcept { return is_err; }
|
|
|
|
/// \brief Returns true if parsing succeeeded.
|
|
|
|
[[nodiscard]] explicit operator bool() const noexcept { return !is_err; }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::table.
|
|
|
|
[[nodiscard]] table& get() & noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(!is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return *TOML_LAUNDER(reinterpret_cast<table*>(&storage));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::table (rvalue overload).
|
|
|
|
[[nodiscard]] table&& get() && noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(!is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return std::move(*TOML_LAUNDER(reinterpret_cast<table*>(&storage)));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::table (const lvalue overload).
|
|
|
|
[[nodiscard]] const table& get() const& noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(!is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return *TOML_LAUNDER(reinterpret_cast<const table*>(&storage));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::parse_error.
|
|
|
|
[[nodiscard]] parse_error& error() & noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return *TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::parse_error (rvalue overload).
|
|
|
|
[[nodiscard]] parse_error&& error() && noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return std::move(*TOML_LAUNDER(reinterpret_cast<parse_error*>(&storage)));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::parse_error (const lvalue overload).
|
|
|
|
[[nodiscard]] const parse_error& error() const& noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_ASSERT(is_err);
|
2020-03-18 13:28:00 +00:00
|
|
|
return *TOML_LAUNDER(reinterpret_cast<const parse_error*>(&storage));
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::table.
|
|
|
|
[[nodiscard]] operator table& () noexcept { return get(); }
|
|
|
|
/// \brief Returns the internal toml::table (rvalue overload).
|
|
|
|
[[nodiscard]] operator table&& () noexcept { return std::move(get()); }
|
|
|
|
/// \brief Returns the internal toml::table (const lvalue overload).
|
|
|
|
[[nodiscard]] operator const table& () const noexcept { return get(); }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Returns the internal toml::parse_error.
|
|
|
|
[[nodiscard]] explicit operator parse_error& () noexcept { return error(); }
|
|
|
|
/// \brief Returns the internal toml::parse_error (rvalue overload).
|
|
|
|
[[nodiscard]] explicit operator parse_error && () noexcept { return std::move(error()); }
|
|
|
|
/// \brief Returns the internal toml::parse_error (const lvalue overload).
|
|
|
|
[[nodiscard]] explicit operator const parse_error& () const noexcept { return error(); }
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
explicit parse_result(table&& tbl) noexcept
|
|
|
|
: is_err{ false }
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
::new (&storage) table{ std::move(tbl) };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
explicit parse_result(parse_error&& err) noexcept
|
|
|
|
: is_err{ true }
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
::new (&storage) parse_error{ std::move(err) };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Move constructor.
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_result(parse_result&& res) noexcept
|
|
|
|
: is_err{ res.is_err }
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
if (is_err)
|
|
|
|
::new (&storage) parse_error{ std::move(res).error() };
|
2020-01-04 14:21:38 +00:00
|
|
|
else
|
2020-03-01 14:56:40 +00:00
|
|
|
::new (&storage) table{ std::move(res).get() };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
/// \brief Move-assignment operator.
|
|
|
|
parse_result& operator=(parse_result&& rhs) noexcept
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
if (is_err != rhs.is_err)
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
destroy();
|
|
|
|
is_err = rhs.is_err;
|
|
|
|
if (is_err)
|
|
|
|
::new (&storage) parse_error{ std::move(rhs).error() };
|
|
|
|
else
|
|
|
|
::new (&storage) table{ std::move(rhs).get() };
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-01 14:56:40 +00:00
|
|
|
if (is_err)
|
|
|
|
error() = std::move(rhs).error();
|
|
|
|
else
|
|
|
|
get() = std::move(rhs).get();
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Destructor.
|
|
|
|
~parse_result() noexcept
|
|
|
|
{
|
|
|
|
destroy();
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-04-03 13:33:02 +00:00
|
|
|
|
|
|
|
/// \brief Gets a node_view for the selected key-value pair in the wrapped table.
|
|
|
|
///
|
|
|
|
/// \param key The key used for the lookup.
|
|
|
|
///
|
|
|
|
/// \returns A view of the value at the given key if parsing was successful and a matching key existed,
|
|
|
|
/// or an empty node view.
|
|
|
|
///
|
|
|
|
/// \see toml::node_view
|
|
|
|
[[nodiscard]] node_view<node> operator[] (string_view key) noexcept
|
|
|
|
{
|
|
|
|
return is_err ? node_view<node>{} : get()[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Gets a node_view for the selected key-value pair in the wrapped table (const overload).
|
|
|
|
[[nodiscard]] node_view<const node> operator[] (string_view key) const noexcept
|
|
|
|
{
|
|
|
|
return is_err ? node_view<const node>{} : get()[key];
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to the first key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] table_iterator begin() noexcept
|
|
|
|
{
|
|
|
|
return is_err ? table_iterator{} : get().begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to the first key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] const_table_iterator begin() const noexcept
|
|
|
|
{
|
|
|
|
return is_err ? const_table_iterator{} : get().begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to the first key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] const_table_iterator cbegin() const noexcept
|
|
|
|
{
|
|
|
|
return is_err ? const_table_iterator{} : get().cbegin();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to one-past-the-last key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] table_iterator end() noexcept
|
|
|
|
{
|
|
|
|
return is_err ? table_iterator{} : get().end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to one-past-the-last key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] const_table_iterator end() const noexcept
|
|
|
|
{
|
|
|
|
return is_err ? const_table_iterator{} : get().end();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns an iterator to one-past-the-last key-value pair in the wrapped table.
|
|
|
|
/// \remarks Returns a default-constructed 'nothing' iterator if the parsing failed.
|
|
|
|
[[nodiscard]] const_table_iterator cend() const noexcept
|
|
|
|
{
|
|
|
|
return is_err ? const_table_iterator{} : get().cend();
|
|
|
|
}
|
2020-03-01 14:56:40 +00:00
|
|
|
};
|
|
|
|
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ABI_NAMESPACE_END
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
#else
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
using parse_result = table;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
#endif
|
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
|
|
|
|
namespace toml::impl
|
2020-03-01 14:56:40 +00:00
|
|
|
{
|
2020-04-10 16:46:00 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
TOML_ABI_NAMESPACE_START(impl_ex)
|
|
|
|
#else
|
|
|
|
TOML_ABI_NAMESPACE_START(impl_noex)
|
2020-03-28 16:56:59 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-02 10:14:54 +00:00
|
|
|
[[nodiscard]] TOML_API
|
2020-03-01 14:56:40 +00:00
|
|
|
parse_result do_parse(utf8_reader_interface&&) TOML_MAY_THROW;
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ABI_NAMESPACE_END // TOML_EXCEPTIONS
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-03-28 16:56:59 +00:00
|
|
|
|
|
|
|
namespace toml
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
2020-04-10 16:46:00 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
TOML_ABI_NAMESPACE_START(parse_ex)
|
|
|
|
#else
|
|
|
|
TOML_ABI_NAMESPACE_START(parse_noex)
|
2020-03-28 16:56:59 +00:00
|
|
|
#endif
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Parses a TOML document from a string view.
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse("a = 3"sv);
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
/// If you don't have a path (or you have no intention of using paths in diagnostics)
|
|
|
|
/// then this parameter can safely be left blank.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-01 14:56:40 +00:00
|
|
|
parse_result parse(std::string_view doc, std::string_view source_path = {}) TOML_MAY_THROW;
|
2020-02-03 09:12:43 +00:00
|
|
|
|
|
|
|
/// \brief Parses a TOML document from a string view.
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse("a = 3"sv, "foo.toml");
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-01 14:56:40 +00:00
|
|
|
parse_result parse(std::string_view doc, std::string&& source_path) TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-07 19:34:54 +00:00
|
|
|
#ifdef __cpp_lib_char8_t
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Parses a TOML document from a char8_t string view.
|
2020-02-03 09:12:43 +00:00
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse(u8"a = 3"sv);
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
/// If you don't have a path (or you have no intention of using paths in diagnostics)
|
|
|
|
/// then this parameter can safely be left blank.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
|
|
|
/// \attention This overload is not available if your compiler does not support char8_t-based strings.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-01 14:56:40 +00:00
|
|
|
parse_result parse(std::u8string_view doc, std::string_view source_path = {}) TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Parses a TOML document from a char8_t string view.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// auto tbl = toml::parse(u8"a = 3"sv, "foo.toml");
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
|
|
|
/// \attention This overload is not available if your compiler does not support char8_t-based strings.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-01 14:56:40 +00:00
|
|
|
parse_result parse(std::u8string_view doc, std::string&& source_path) TOML_MAY_THROW;
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-04-07 19:34:54 +00:00
|
|
|
#endif // __cpp_lib_char8_t
|
2020-01-04 14:21:38 +00:00
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Parses a TOML document from a stream.
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// std::stringstream ss;
|
|
|
|
/// ss << "a = 3"sv;
|
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse(ss);
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-04-02 21:39:21 +00:00
|
|
|
/// \tparam Char The stream's underlying character type. Must be 1 byte in size.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
/// If you don't have a path (or you have no intention of using paths in diagnostics)
|
|
|
|
/// then this parameter can safely be left blank.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char>
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-04-08 13:33:57 +00:00
|
|
|
parse_result parse(std::basic_istream<Char>& doc, std::string_view source_path = {}) TOML_MAY_THROW
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
static_assert(
|
2020-04-02 21:39:21 +00:00
|
|
|
sizeof(Char) == 1,
|
2020-01-04 14:21:38 +00:00
|
|
|
"The stream's underlying character type must be 1 byte in size."
|
|
|
|
);
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
return impl::do_parse(impl::utf8_reader{ doc, source_path });
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
|
|
|
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \brief Parses a TOML document from a stream.
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// std::stringstream ss;
|
|
|
|
/// ss << "a = 3"sv;
|
|
|
|
///
|
|
|
|
/// auto tbl = toml::parse(ss, "foo.toml");
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-04-02 21:39:21 +00:00
|
|
|
/// \tparam Char The stream's underlying character type. Must be 1 byte in size.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param doc The TOML document to parse. Must be valid UTF-8.
|
|
|
|
/// \param source_path The path used to initialize each node's `source().path`.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char>
|
2020-01-04 14:21:38 +00:00
|
|
|
[[nodiscard]]
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-04-08 13:33:57 +00:00
|
|
|
parse_result parse(std::basic_istream<Char>& doc, std::string&& source_path) TOML_MAY_THROW
|
2020-01-04 14:21:38 +00:00
|
|
|
{
|
|
|
|
static_assert(
|
2020-04-02 21:39:21 +00:00
|
|
|
sizeof(Char) == 1,
|
2020-01-04 14:21:38 +00:00
|
|
|
"The stream's underlying character type must be 1 byte in size."
|
|
|
|
);
|
|
|
|
|
2020-03-01 14:56:40 +00:00
|
|
|
return impl::do_parse(impl::utf8_reader{ doc, std::move(source_path) });
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-02-03 09:12:43 +00:00
|
|
|
|
|
|
|
/// \brief Parses a TOML document from a file.
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \detail \cpp
|
|
|
|
/// #include <fstream>
|
|
|
|
///
|
|
|
|
/// toml::parse_result get_foo_toml()
|
|
|
|
/// {
|
|
|
|
/// return toml::parse_file("foo.toml");
|
|
|
|
/// }
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-04-02 21:39:21 +00:00
|
|
|
/// \tparam Char The path's character type. Must be 1 byte in size.
|
2020-02-03 09:12:43 +00:00
|
|
|
/// \param file_path The TOML document to parse. Must be valid UTF-8.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-02-03 09:12:43 +00:00
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \attention You must `#include <fstream>` to use this function (toml++
|
2020-02-03 09:12:43 +00:00
|
|
|
/// does not transitively include it for you).
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char>
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
2020-04-09 08:13:12 +00:00
|
|
|
TOML_EXTERNAL_LINKAGE
|
2020-04-08 13:33:57 +00:00
|
|
|
parse_result parse_file(std::basic_string_view<Char> file_path) TOML_MAY_THROW
|
2020-02-03 09:12:43 +00:00
|
|
|
{
|
|
|
|
static_assert(
|
2020-04-02 21:39:21 +00:00
|
|
|
sizeof(Char) == 1,
|
2020-02-03 09:12:43 +00:00
|
|
|
"The path's character type must be 1 byte in size."
|
|
|
|
);
|
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
auto str = std::string(reinterpret_cast<const char*>(file_path.data()), file_path.length());
|
2020-04-02 21:39:21 +00:00
|
|
|
auto ifs = std::basic_ifstream<Char>{ str };
|
2020-03-03 21:28:24 +00:00
|
|
|
return parse( ifs, std::move(str) );
|
|
|
|
}
|
|
|
|
|
2020-04-08 13:33:57 +00:00
|
|
|
#if !TOML_ALL_INLINE
|
|
|
|
extern template TOML_API parse_result parse(std::istream&, std::string_view) TOML_MAY_THROW;
|
|
|
|
extern template TOML_API parse_result parse(std::istream&, std::string&&) TOML_MAY_THROW;
|
|
|
|
extern template TOML_API parse_result parse_file(std::string_view) TOML_MAY_THROW;
|
|
|
|
#ifdef __cpp_lib_char8_t
|
|
|
|
extern template TOML_API parse_result parse_file(std::u8string_view) TOML_MAY_THROW;
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2020-03-03 21:28:24 +00:00
|
|
|
// Q: "why are the parse_file functions templated??"
|
|
|
|
// A: I don't want to force users to drag in <fstream> if they're not going to do
|
|
|
|
// any parsing directly from files. Keeping them templated delays their instantiation
|
|
|
|
// until they're actually required, so only those users wanting to use parse_file()
|
|
|
|
// are burdened by the <fstream> overhead.
|
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char>
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
2020-04-02 21:39:21 +00:00
|
|
|
inline parse_result parse_file(const std::basic_string<Char>& file_path) TOML_MAY_THROW
|
2020-03-03 21:28:24 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
return parse_file(std::basic_string_view<Char>{ file_path });
|
2020-03-03 21:28:24 +00:00
|
|
|
}
|
|
|
|
|
2020-04-02 21:39:21 +00:00
|
|
|
template <typename Char>
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
2020-04-02 21:39:21 +00:00
|
|
|
inline parse_result parse_file(const Char* file_path) TOML_MAY_THROW
|
2020-03-03 21:28:24 +00:00
|
|
|
{
|
2020-04-02 21:39:21 +00:00
|
|
|
return parse_file(std::basic_string_view<Char>{ file_path });
|
2020-02-03 09:12:43 +00:00
|
|
|
}
|
2020-02-16 13:11:57 +00:00
|
|
|
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ABI_NAMESPACE_END // TOML_EXCEPTIONS
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Convenience literal operators for working with TOML++.
|
|
|
|
///
|
2020-02-22 14:10:32 +00:00
|
|
|
/// \detail This namespace exists so you can safely hoist the UDL operators into another scope
|
|
|
|
/// without dragging in everything in the toml namespace: \cpp
|
|
|
|
///
|
|
|
|
/// #include <toml++/toml.h>
|
2020-04-02 21:39:21 +00:00
|
|
|
/// using namespace toml::literals;
|
2020-02-22 14:10:32 +00:00
|
|
|
///
|
|
|
|
/// int main()
|
|
|
|
/// {
|
|
|
|
/// auto tbl = "vals = [1, 2, 3]"_toml;
|
2020-03-01 14:56:40 +00:00
|
|
|
///
|
|
|
|
/// // ... do stuff with the table generated by the "_toml" UDL ...
|
|
|
|
///
|
2020-02-22 14:10:32 +00:00
|
|
|
/// return 0;
|
|
|
|
/// }
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
inline namespace literals
|
|
|
|
{
|
2020-04-10 16:46:00 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
TOML_ABI_NAMESPACE_START(lit_ex)
|
|
|
|
#else
|
|
|
|
TOML_ABI_NAMESPACE_START(lit_noex)
|
2020-03-28 16:56:59 +00:00
|
|
|
#endif
|
|
|
|
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \brief Parses TOML data from a string.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// using namespace toml::literals;
|
|
|
|
///
|
|
|
|
/// auto tbl = "a = 3"_toml;
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \param str The string data.
|
|
|
|
/// \param len The string length.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-28 16:56:59 +00:00
|
|
|
parse_result operator"" _toml(const char* str, size_t len) TOML_MAY_THROW;
|
2020-02-16 13:11:57 +00:00
|
|
|
|
2020-04-07 19:34:54 +00:00
|
|
|
#ifdef __cpp_lib_char8_t
|
2020-02-16 13:11:57 +00:00
|
|
|
|
|
|
|
/// \brief Parses TOML data from a string.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// using namespace toml::literals;
|
|
|
|
///
|
|
|
|
/// auto tbl = u8"a = 3"_toml;
|
|
|
|
/// std::cout << tbl["a"] << std::endl;
|
|
|
|
///
|
|
|
|
/// \ecpp
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \out
|
|
|
|
/// 3
|
|
|
|
/// \eout
|
|
|
|
///
|
2020-02-16 13:11:57 +00:00
|
|
|
/// \param str The string data.
|
|
|
|
/// \param len The string length.
|
|
|
|
///
|
2020-02-18 21:29:59 +00:00
|
|
|
/// \returns <strong><em>With exceptions:</em></strong> A toml::table. <br>
|
|
|
|
/// <strong><em>Without exceptions:</em></strong> A toml::parse_result detailing the parsing outcome.
|
2020-02-16 13:11:57 +00:00
|
|
|
///
|
|
|
|
/// \attention This overload is not available if your compiler does not support char8_t-based strings.
|
2020-04-08 13:33:57 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
TOML_API
|
2020-03-28 16:56:59 +00:00
|
|
|
parse_result operator"" _toml(const char8_t* str, size_t len) TOML_MAY_THROW;
|
2020-02-16 13:11:57 +00:00
|
|
|
|
2020-04-07 19:34:54 +00:00
|
|
|
#endif // __cpp_lib_char8_t
|
2020-02-16 13:11:57 +00:00
|
|
|
|
2020-04-10 16:46:00 +00:00
|
|
|
TOML_ABI_NAMESPACE_END // TOML_EXCEPTIONS
|
2020-02-16 13:11:57 +00:00
|
|
|
}
|
2020-01-04 14:21:38 +00:00
|
|
|
}
|
2020-03-28 16:56:59 +00:00
|
|
|
|
2020-04-13 20:23:11 +00:00
|
|
|
TOML_POP_WARNINGS // TOML_DISABLE_PADDING_WARNINGS
|