2020-04-09 08:13:12 +00:00
|
|
|
//# This file is a part of toml++ and is subject to the the terms of the MIT license.
|
2021-01-02 15:48:47 +00:00
|
|
|
//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
|
2020-04-09 08:13:12 +00:00
|
|
|
//# 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-04-09 08:13:12 +00:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
//# {{
|
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
|
|
|
|
//# }}
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_DISABLE_WARNINGS;
|
2020-04-09 08:13:12 +00:00
|
|
|
#if TOML_EXCEPTIONS
|
|
|
|
#include <stdexcept>
|
|
|
|
#endif
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_ENABLE_WARNINGS;
|
2020-04-09 08:13:12 +00:00
|
|
|
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_PUSH_WARNINGS;
|
|
|
|
TOML_DISABLE_INIT_WARNINGS;
|
2020-04-09 08:13:12 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
TOML_NAMESPACE_START
|
2020-04-09 08:13:12 +00:00
|
|
|
{
|
2021-05-06 08:03:25 +00:00
|
|
|
TOML_ABI_NAMESPACE_BOOL(TOML_EXCEPTIONS, ex, noex);
|
2020-04-10 16:46:00 +00:00
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#if defined(DOXYGEN) || !TOML_EXCEPTIONS
|
2020-04-09 08:13:12 +00:00
|
|
|
|
2020-06-24 11:28:20 +00:00
|
|
|
/// \brief An error generated when parsing fails.
|
2020-04-09 08:13:12 +00:00
|
|
|
///
|
|
|
|
/// \remarks This class inherits from std::runtime_error when exceptions are enabled.
|
|
|
|
/// The public interface is the same regardless of exception mode.
|
|
|
|
class parse_error final
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
std::string description_;
|
|
|
|
source_region source_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, source_region&& src) noexcept
|
|
|
|
: description_{ std::move(desc) },
|
|
|
|
source_{ std::move(src) }
|
|
|
|
{}
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, const source_region& src) noexcept
|
|
|
|
: parse_error{ std::move(desc), source_region{ src } }
|
|
|
|
{}
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
|
|
|
parse_error(std::string&& desc, const source_position& position, const source_path_ptr& path = {}) noexcept
|
|
|
|
: parse_error{ std::move(desc), source_region{ position, position, path } }
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
|
|
/// \brief Returns a textual description of the error.
|
2020-06-24 11:28:20 +00:00
|
|
|
/// \remark The backing string is guaranteed to be null-terminated.
|
2020-04-09 08:13:12 +00:00
|
|
|
[[nodiscard]]
|
|
|
|
std::string_view description() const noexcept
|
|
|
|
{
|
|
|
|
return description_;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// \brief Returns the region of the source document responsible for the error.
|
|
|
|
[[nodiscard]]
|
|
|
|
const source_region& source() const noexcept
|
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
class parse_error final
|
|
|
|
: public std::runtime_error
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
source_region source_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_ATTR(nonnull)
|
2020-04-09 08:13:12 +00:00
|
|
|
parse_error(const char* desc, source_region&& src) noexcept
|
|
|
|
: std::runtime_error{ desc },
|
|
|
|
source_{ std::move(src) }
|
|
|
|
{}
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_ATTR(nonnull)
|
2020-04-09 08:13:12 +00:00
|
|
|
parse_error(const char* desc, const source_region& src) noexcept
|
|
|
|
: parse_error{ desc, source_region{ src } }
|
|
|
|
{}
|
|
|
|
|
|
|
|
TOML_NODISCARD_CTOR
|
2020-07-05 15:08:28 +00:00
|
|
|
TOML_ATTR(nonnull)
|
2020-04-09 08:13:12 +00:00
|
|
|
parse_error(const char* desc, const source_position& position, const source_path_ptr& path = {}) noexcept
|
|
|
|
: parse_error{ desc, source_region{ position, position, path } }
|
|
|
|
{}
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
std::string_view description() const noexcept
|
|
|
|
{
|
|
|
|
return std::string_view{ what() };
|
|
|
|
}
|
|
|
|
|
|
|
|
[[nodiscard]]
|
|
|
|
const source_region& source() const noexcept
|
|
|
|
{
|
|
|
|
return source_;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
|
2020-04-09 08:13:12 +00:00
|
|
|
|
|
|
|
/// \brief Prints a parse_error to a stream.
|
|
|
|
///
|
|
|
|
/// \detail \cpp
|
|
|
|
/// try
|
|
|
|
/// {
|
|
|
|
/// auto tbl = toml::parse("enabled = trUe"sv);
|
|
|
|
/// }
|
|
|
|
/// catch (const toml::parse_error & err)
|
|
|
|
/// {
|
2020-08-11 13:34:03 +00:00
|
|
|
/// std::cerr << "Parsing failed:\n"sv << err << "\n";
|
2020-04-09 08:13:12 +00:00
|
|
|
/// }
|
|
|
|
/// \ecpp
|
|
|
|
///
|
|
|
|
/// \out
|
|
|
|
/// Parsing failed:
|
|
|
|
/// Encountered unexpected character while parsing boolean; expected 'true', saw 'trU'
|
|
|
|
/// (error occurred at line 1, column 13)
|
|
|
|
/// \eout
|
|
|
|
///
|
|
|
|
/// \tparam Char The output stream's underlying character type. Must be 1 byte in size.
|
|
|
|
/// \param lhs The stream.
|
|
|
|
/// \param rhs The parse_error.
|
|
|
|
///
|
|
|
|
/// \returns The input stream.
|
|
|
|
template <typename Char>
|
2020-07-18 12:10:19 +00:00
|
|
|
inline std::basic_ostream<Char>& operator << (std::basic_ostream<Char>& lhs, const parse_error& rhs)
|
2020-04-09 08:13:12 +00:00
|
|
|
{
|
|
|
|
lhs << rhs.description();
|
|
|
|
lhs << "\n\t(error occurred at "sv;
|
|
|
|
lhs << rhs.source();
|
|
|
|
lhs << ")"sv;
|
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2020-07-25 17:37:30 +00:00
|
|
|
#if !defined(DOXYGEN) && !TOML_HEADER_ONLY
|
2020-04-09 08:13:12 +00:00
|
|
|
extern template TOML_API std::ostream& operator << (std::ostream&, const parse_error&);
|
|
|
|
#endif
|
|
|
|
}
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_NAMESPACE_END;
|
2020-04-09 08:13:12 +00:00
|
|
|
|
2021-04-18 12:04:46 +00:00
|
|
|
TOML_POP_WARNINGS; // TOML_DISABLE_INIT_WARNINGS
|