2021-10-23 09:22:41 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
|
|
|
//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
#pragma once
|
2021-10-24 22:04:23 +00:00
|
|
|
|
2021-10-30 12:56:14 +00:00
|
|
|
#include "preprocessor.h"
|
2021-11-02 20:13:09 +00:00
|
|
|
#if TOML_ENABLE_FORMATTERS
|
2021-10-30 12:56:14 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
#include "std_vector.h"
|
2021-11-06 16:59:47 +00:00
|
|
|
#include "formatter.h"
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_start.h"
|
2021-10-23 09:22:41 +00:00
|
|
|
|
|
|
|
TOML_NAMESPACE_START
|
|
|
|
{
|
|
|
|
/// \brief A wrapper for printing TOML objects out to a stream as formatted TOML.
|
|
|
|
///
|
2021-11-02 20:13:09 +00:00
|
|
|
/// \availability This class is only available when #TOML_ENABLE_FORMATTERS is enabled.
|
2021-10-30 12:56:14 +00:00
|
|
|
///
|
2021-10-23 09:22:41 +00:00
|
|
|
/// \remarks You generally don't need to create an instance of this class explicitly; the stream
|
|
|
|
/// operators of the TOML node types already print themselves out using this formatter.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
2021-10-26 18:03:56 +00:00
|
|
|
/// auto tbl = toml::table{
|
2021-10-23 09:22:41 +00:00
|
|
|
/// { "description", "This is some TOML, yo." },
|
|
|
|
/// { "fruit", toml::array{ "apple", "orange", "pear" } },
|
|
|
|
/// { "numbers", toml::array{ 1, 2, 3, 4, 5 } },
|
2021-10-26 18:03:56 +00:00
|
|
|
/// { "table", toml::table{ { "foo", "bar" } } }
|
|
|
|
/// };
|
2021-10-23 09:22:41 +00:00
|
|
|
///
|
|
|
|
/// // these two lines are equivalent:
|
2021-10-30 12:56:14 +00:00
|
|
|
/// std::cout << toml::toml_formatter{ tbl } << "\n";
|
2021-10-23 09:22:41 +00:00
|
|
|
/// std::cout << tbl << "\n";
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// description = "This is some TOML, yo."
|
|
|
|
/// fruit = ["apple", "orange", "pear"]
|
|
|
|
/// numbers = [1, 2, 3, 4, 5]
|
|
|
|
///
|
|
|
|
/// [table]
|
|
|
|
/// foo = "bar"
|
|
|
|
/// \eout
|
2022-02-12 12:25:43 +00:00
|
|
|
class TOML_EXPORTED_CLASS toml_formatter : impl::formatter
|
2021-10-23 09:22:41 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// \cond
|
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
using base = impl::formatter;
|
2021-11-10 09:17:15 +00:00
|
|
|
std::vector<const key*> key_path_;
|
2021-10-23 09:22:41 +00:00
|
|
|
bool pending_table_separator_ = false;
|
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print_pending_table_separator();
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-11-10 09:17:15 +00:00
|
|
|
void print(const key&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print_inline(const toml::table&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const toml::array&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print(const toml::table&);
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-02-12 12:25:43 +00:00
|
|
|
TOML_EXPORTED_MEMBER_FUNCTION
|
2021-10-24 22:04:23 +00:00
|
|
|
void print();
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2022-01-04 14:23:45 +00:00
|
|
|
static constexpr impl::formatter_constants constants = { format_flags::none, // mandatory
|
|
|
|
format_flags::none, // ignored
|
2021-11-02 20:13:09 +00:00
|
|
|
"inf"sv,
|
|
|
|
"-inf"sv,
|
|
|
|
"nan"sv,
|
|
|
|
"true"sv,
|
|
|
|
"false"sv };
|
2021-10-27 13:03:05 +00:00
|
|
|
|
2021-10-23 09:22:41 +00:00
|
|
|
/// \endcond
|
|
|
|
|
|
|
|
public:
|
2021-10-30 12:56:14 +00:00
|
|
|
/// \brief The default flags for a toml_formatter.
|
2021-11-02 20:13:09 +00:00
|
|
|
static constexpr format_flags default_flags = constants.mandatory_flags //
|
|
|
|
| format_flags::allow_literal_strings //
|
|
|
|
| format_flags::allow_multi_line_strings //
|
2022-01-04 14:23:45 +00:00
|
|
|
| format_flags::allow_unicode_strings //
|
2021-11-03 16:19:48 +00:00
|
|
|
| format_flags::allow_real_tabs_in_strings //
|
2021-11-02 20:13:09 +00:00
|
|
|
| format_flags::allow_binary_integers //
|
|
|
|
| format_flags::allow_octal_integers //
|
|
|
|
| format_flags::allow_hexadecimal_integers //
|
2021-10-27 13:03:05 +00:00
|
|
|
| format_flags::indentation;
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 12:56:14 +00:00
|
|
|
/// \brief Constructs a TOML formatter and binds it to a TOML object.
|
2021-10-23 09:22:41 +00:00
|
|
|
///
|
|
|
|
/// \param source The source TOML object.
|
|
|
|
/// \param flags Format option flags.
|
|
|
|
TOML_NODISCARD_CTOR
|
2021-10-30 12:56:14 +00:00
|
|
|
explicit toml_formatter(const toml::node& source, format_flags flags = default_flags) noexcept
|
2021-11-02 20:13:09 +00:00
|
|
|
: base{ &source, nullptr, constants, { flags, " "sv } }
|
2021-10-23 09:22:41 +00:00
|
|
|
{}
|
|
|
|
|
2021-10-30 12:56:14 +00:00
|
|
|
#if defined(DOXYGEN) || (TOML_ENABLE_PARSER && !TOML_EXCEPTIONS)
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-30 12:56:14 +00:00
|
|
|
/// \brief Constructs a TOML formatter and binds it to a toml::parse_result.
|
2021-10-23 09:22:41 +00:00
|
|
|
///
|
|
|
|
/// \availability This constructor is only available when exceptions are disabled.
|
|
|
|
///
|
|
|
|
/// \attention Formatting a failed parse result will simply dump the error message out as-is.
|
|
|
|
/// This will not be valid TOML, but at least gives you something to log or show up in diagnostics:
|
|
|
|
/// \cpp
|
2021-10-30 12:56:14 +00:00
|
|
|
/// std::cout << toml::toml_formatter{ toml::parse("a = 'b'"sv) } // ok
|
2021-10-23 09:22:41 +00:00
|
|
|
/// << "\n\n"
|
2021-10-30 12:56:14 +00:00
|
|
|
/// << toml::toml_formatter{ toml::parse("a = "sv) } // malformed
|
2021-10-23 09:22:41 +00:00
|
|
|
/// << "\n";
|
|
|
|
/// \ecpp
|
|
|
|
/// \out
|
|
|
|
/// a = 'b'
|
|
|
|
///
|
|
|
|
/// Error while parsing key-value pair: encountered end-of-file
|
|
|
|
/// (error occurred at line 1, column 5)
|
|
|
|
/// \eout
|
|
|
|
/// Use the library with exceptions if you want to avoid this scenario.
|
|
|
|
///
|
|
|
|
/// \param result The parse result.
|
|
|
|
/// \param flags Format option flags.
|
|
|
|
TOML_NODISCARD_CTOR
|
2021-10-30 12:56:14 +00:00
|
|
|
explicit toml_formatter(const toml::parse_result& result, format_flags flags = default_flags) noexcept
|
2021-11-02 20:13:09 +00:00
|
|
|
: base{ nullptr, &result, constants, { flags, " "sv } }
|
2021-10-23 09:22:41 +00:00
|
|
|
{}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
/// \brief Prints the bound TOML object out to the stream as formatted TOML.
|
2021-10-30 12:56:14 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& lhs, toml_formatter& rhs)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
rhs.attach(lhs);
|
|
|
|
rhs.key_path_.clear();
|
|
|
|
rhs.print();
|
|
|
|
rhs.detach();
|
|
|
|
return lhs;
|
|
|
|
}
|
2021-10-23 09:22:41 +00:00
|
|
|
|
2021-10-24 22:04:23 +00:00
|
|
|
/// \brief Prints the bound TOML object out to the stream as formatted TOML (rvalue overload).
|
2021-10-30 12:56:14 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& lhs, toml_formatter&& rhs)
|
2021-10-24 22:04:23 +00:00
|
|
|
{
|
|
|
|
return lhs << rhs; // as lvalue
|
|
|
|
}
|
|
|
|
};
|
2021-10-23 09:22:41 +00:00
|
|
|
}
|
|
|
|
TOML_NAMESPACE_END;
|
|
|
|
|
2021-10-24 10:21:32 +00:00
|
|
|
#include "header_end.h"
|
2021-11-02 20:13:09 +00:00
|
|
|
#endif // TOML_ENABLE_FORMATTERS
|