NeBuild dev
Loading...
Searching...
No Matches
parse_error.hpp
Go to the documentation of this file.
1//# This file is a part of toml++ and is subject to the the terms of the MIT license.
2//# Copyright (c) Mark Gillard <mark.gillard@outlook.com.au>
3//# See https://github.com/marzer/tomlplusplus/blob/master/LICENSE for the full license text.
4// SPDX-License-Identifier: MIT
5#pragma once
6
7#include "preprocessor.hpp"
8#if TOML_ENABLE_PARSER
9
10#include "std_except.hpp"
11#include "source_region.hpp"
12#include "print_to_stream.hpp"
13#include "header_start.hpp"
14
15#if TOML_DOXYGEN || !TOML_EXCEPTIONS
16#define TOML_PARSE_ERROR_BASE
17#else
18#define TOML_PARSE_ERROR_BASE : public std::runtime_error
19#endif
20
22{
24
29 class parse_error TOML_PARSE_ERROR_BASE
30 {
31 private:
32#if !TOML_EXCEPTIONS
33 std::string description_;
34#endif
36
37 public:
38#if TOML_EXCEPTIONS
39
41 TOML_ATTR(nonnull)
42 parse_error(const char* desc, source_region&& src) noexcept //
43 : std::runtime_error{ desc },
44 source_{ std::move(src) }
45 {}
46
48 TOML_ATTR(nonnull)
49 parse_error(const char* desc, const source_region& src) noexcept //
50 : parse_error{ desc, source_region{ src } }
51 {}
52
54 TOML_ATTR(nonnull)
55 parse_error(const char* desc, const source_position& position, const source_path_ptr& path = {}) noexcept
56 : parse_error{ desc, source_region{ position, position, path } }
57 {}
58
59#else
60
62 parse_error(std::string&& desc, source_region&& src) noexcept //
63 : description_{ std::move(desc) },
64 source_{ std::move(src) }
65 {}
66
68 parse_error(std::string&& desc, const source_region& src) noexcept //
69 : parse_error{ std::move(desc), source_region{ src } }
70 {}
71
73 parse_error(std::string&& desc, const source_position& position, const source_path_ptr& path = {}) noexcept
74 : parse_error{ std::move(desc), source_region{ position, position, path } }
75 {}
76
77#endif
78
82 std::string_view description() const noexcept
83 {
84#if TOML_EXCEPTIONS
85 return std::string_view{ what() };
86#else
87 return description_;
88#endif
89 }
90
93 const source_region& source() const noexcept
94 {
95 return source_;
96 }
97
122 friend std::ostream& operator<<(std::ostream& lhs, const parse_error& rhs)
123 {
124 impl::print_to_stream(lhs, rhs.description());
125 impl::print_to_stream(lhs, "\n\t(error occurred at "sv);
126 impl::print_to_stream(lhs, rhs.source());
127 impl::print_to_stream(lhs, ")"sv);
128 return lhs;
129 }
130 };
131
132 TOML_ABI_NAMESPACE_END; // TOML_EXCEPTIONS
133}
135
136#undef TOML_PARSE_ERROR_BASE
137
138#include "header_end.hpp"
139#endif // TOML_ENABLE_PARSER
An error generated when parsing fails.
Definition parse_error.hpp:30
TOML_NODISCARD_CTOR parse_error(std::string &&desc, source_region &&src) noexcept
Definition parse_error.hpp:62
TOML_NODISCARD_CTOR parse_error(std::string &&desc, const source_region &src) noexcept
Definition parse_error.hpp:68
friend std::ostream & operator<<(std::ostream &lhs, const parse_error &rhs)
Prints a parse_error to a stream.
Definition parse_error.hpp:122
std::string description_
Definition parse_error.hpp:33
TOML_NODISCARD const source_region & source() const noexcept
Returns the region of the source document responsible for the error.
Definition parse_error.hpp:93
TOML_NODISCARD_CTOR parse_error(std::string &&desc, const source_position &position, const source_path_ptr &path={}) noexcept
Definition parse_error.hpp:73
TOML_NODISCARD std::string_view description() const noexcept
Returns a textual description of the error.
Definition parse_error.hpp:82
source_region source_
Definition parse_error.hpp:35
A TOML path.
Definition path.hpp:239
#define TOML_EXCEPTIONS
Sets whether the library uses exceptions to report parsing failures. \detail Defaults to 1 or 0 accor...
Definition preprocessor.hpp:1126
Definition json.h:5363
TOML_NAMESPACE_START
Definition parse_error.hpp:22
TOML_ABI_NAMESPACE_END
Definition parse_error.hpp:132
TOML_NAMESPACE_END
Definition parse_error.hpp:134
#define TOML_NODISCARD_CTOR
Definition preprocessor.hpp:446
#define TOML_ABI_NAMESPACE_BOOL(cond, T, F)
Definition preprocessor.hpp:1323
#define TOML_NODISCARD
Definition preprocessor.hpp:439
#define TOML_ATTR(...)
Definition preprocessor.hpp:316
std::shared_ptr< const std::string > source_path_ptr
A pointer to a shared string resource containing a source path.
Definition source_region.hpp:19
A source document line-and-column pair.
Definition source_region.hpp:43
A source document region.
Definition source_region.hpp:167