Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
yaml_error.h
1#ifndef SOURCEMETA_CORE_YAML_ERROR_H_
2#define SOURCEMETA_CORE_YAML_ERROR_H_
3
4#ifndef SOURCEMETA_CORE_YAML_EXPORT
5#include <sourcemeta/core/yaml_export.h>
6#endif
7
8#include <cstdint> // std::uint64_t
9#include <exception> // std::exception
10#include <filesystem> // std::filesystem::path
11#include <string> // std::string
12#include <string_view> // std::string_view
13#include <utility> // std::move
14
15namespace sourcemeta::core {
16
17// Exporting symbols that depends on the standard C++ library is considered
18// safe
19// https://learn.microsoft.com/en-us/cpp/error-messages/compiler-warnings/compiler-warning-level-2-c4275?view=msvc-170&redirectedfrom=MSDN
20#if defined(_MSC_VER)
21#pragma warning(disable : 4251 4275)
22#endif
23
26class SOURCEMETA_CORE_YAML_EXPORT YAMLError : public std::exception {
27public:
29 YAMLError(const char *message) : message_{message} {}
30 YAMLError(std::string message) = delete;
31 YAMLError(std::string &&message) = delete;
32 YAMLError(std::string_view message) = delete;
33
34 [[nodiscard]] auto what() const noexcept -> const char * override {
35 return this->message_;
36 }
37
38private:
39 const char *message_;
40};
41
44class SOURCEMETA_CORE_YAML_EXPORT YAMLParseError : public std::exception {
45public:
47 YAMLParseError(const std::uint64_t line, const std::uint64_t column)
48 : line_{line}, column_{column},
49 message_{"Failed to parse the YAML document"} {}
50
52 YAMLParseError(const std::uint64_t line, const std::uint64_t column,
53 const char *message)
54 : line_{line}, column_{column}, message_{message} {}
55 YAMLParseError(const std::uint64_t line, const std::uint64_t column,
56 std::string message) = delete;
57 YAMLParseError(const std::uint64_t line, const std::uint64_t column,
58 std::string &&message) = delete;
59 YAMLParseError(const std::uint64_t line, const std::uint64_t column,
60 std::string_view message) = delete;
61
62 [[nodiscard]] auto what() const noexcept -> const char * override {
63 return this->message_;
64 }
65
67 [[nodiscard]] auto line() const noexcept -> std::uint64_t {
68 return this->line_;
69 }
70
72 [[nodiscard]] auto column() const noexcept -> std::uint64_t {
73 return this->column_;
74 }
75
76private:
77 std::uint64_t line_;
78 std::uint64_t column_;
79 const char *message_;
80};
81
84class SOURCEMETA_CORE_YAML_EXPORT YAMLFileParseError : public YAMLParseError {
85public:
87 YAMLFileParseError(std::filesystem::path path, const std::uint64_t line,
88 const std::uint64_t column, const char *message)
89 : YAMLParseError{line, column, message}, path_{std::move(path)} {}
90 YAMLFileParseError(std::filesystem::path path, const std::uint64_t line,
91 const std::uint64_t column, std::string message) = delete;
92 YAMLFileParseError(std::filesystem::path path, const std::uint64_t line,
93 const std::uint64_t column,
94 std::string &&message) = delete;
95 YAMLFileParseError(std::filesystem::path path, const std::uint64_t line,
96 const std::uint64_t column,
97 std::string_view message) = delete;
98
100 YAMLFileParseError(std::filesystem::path path, const YAMLParseError &parent)
101 : YAMLParseError{parent.line(), parent.column(), parent.what()},
102 path_{std::move(path)} {}
103
105 [[nodiscard]] auto path() const noexcept -> const std::filesystem::path & {
106 return this->path_;
107 }
108
109private:
110 std::filesystem::path path_;
111};
112
115class SOURCEMETA_CORE_YAML_EXPORT YAMLUnknownAnchorError
116 : public YAMLParseError {
117public:
119 YAMLUnknownAnchorError(const std::string_view anchor_name,
120 const std::uint64_t line, const std::uint64_t column)
121 : YAMLParseError{line, column, "YAML alias references undefined anchor"},
122 anchor_name_{anchor_name} {}
123
125 [[nodiscard]] auto anchor() const noexcept -> std::string_view {
126 return this->anchor_name_;
127 }
128
129private:
130 std::string anchor_name_;
131};
132
137class SOURCEMETA_CORE_YAML_EXPORT YAMLDuplicateKeyError
138 : public YAMLParseError {
139public:
141 YAMLDuplicateKeyError(const std::string_view key_name,
142 const std::uint64_t line, const std::uint64_t column)
143 : YAMLParseError{line, column, "Duplicate key in YAML mapping"},
144 key_name_{key_name} {}
145
147 [[nodiscard]] auto key() const noexcept -> std::string_view {
148 return this->key_name_;
149 }
150
151private:
152 std::string key_name_;
153};
154
155#if defined(_MSC_VER)
156#pragma warning(default : 4251 4275)
157#endif
158
159} // namespace sourcemeta::core
160
161#endif
auto line() const noexcept -> std::uint64_t
Get the line number of the error.
Definition yaml_error.h:67
YAMLParseError(const std::uint64_t line, const std::uint64_t column)
Create a parsing error.
Definition yaml_error.h:47
YAMLFileParseError(std::filesystem::path path, const YAMLParseError &parent)
Create a file parsing error from a parse error.
Definition yaml_error.h:100
YAMLFileParseError(std::filesystem::path path, const std::uint64_t line, const std::uint64_t column, const char *message)
Create a file parsing error.
Definition yaml_error.h:87
YAMLError(const char *message)
Create a general error.
Definition yaml_error.h:29
YAMLUnknownAnchorError(const std::string_view anchor_name, const std::uint64_t line, const std::uint64_t column)
Create an unknown anchor error.
Definition yaml_error.h:119
YAMLDuplicateKeyError(const std::string_view key_name, const std::uint64_t line, const std::uint64_t column)
Create a duplicate key error.
Definition yaml_error.h:141
auto anchor() const noexcept -> std::string_view
Get the anchor name of the error.
Definition yaml_error.h:125
auto path() const noexcept -> const std::filesystem::path &
Get the file path of the error.
Definition yaml_error.h:105
YAMLParseError(const std::uint64_t line, const std::uint64_t column, const char *message)
Create a parsing error with a custom error.
Definition yaml_error.h:52
auto key() const noexcept -> std::string_view
Get the key name of the error.
Definition yaml_error.h:147
auto column() const noexcept -> std::uint64_t
Get the column number of the error.
Definition yaml_error.h:72
Definition yaml_error.h:26
Definition yaml_error.h:84
Definition yaml_error.h:44