My Project
exceptions.h
1 #ifndef EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
2 #define EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
3 
4 #if defined(_MSC_VER) || \
5  (defined(__GNUC__) && (__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || \
6  (__GNUC__ >= 4)) // GCC supports "pragma once" correctly since 3.4
7 #pragma once
8 #endif
9 
10 #include "yaml-cpp/mark.h"
11 #include "yaml-cpp/noexcept.h"
12 #include "yaml-cpp/traits.h"
13 #include <sstream>
14 #include <stdexcept>
15 #include <string>
16 
17 namespace YAML {
18 // error messages
19 namespace ErrorMsg {
20 const char* const YAML_DIRECTIVE_ARGS =
21  "YAML directives must have exactly one argument";
22 const char* const YAML_VERSION = "bad YAML version: ";
23 const char* const YAML_MAJOR_VERSION = "YAML major version too large";
24 const char* const REPEATED_YAML_DIRECTIVE = "repeated YAML directive";
25 const char* const TAG_DIRECTIVE_ARGS =
26  "TAG directives must have exactly two arguments";
27 const char* const REPEATED_TAG_DIRECTIVE = "repeated TAG directive";
28 const char* const CHAR_IN_TAG_HANDLE =
29  "illegal character found while scanning tag handle";
30 const char* const TAG_WITH_NO_SUFFIX = "tag handle with no suffix";
31 const char* const END_OF_VERBATIM_TAG = "end of verbatim tag not found";
32 const char* const END_OF_MAP = "end of map not found";
33 const char* const END_OF_MAP_FLOW = "end of map flow not found";
34 const char* const END_OF_SEQ = "end of sequence not found";
35 const char* const END_OF_SEQ_FLOW = "end of sequence flow not found";
36 const char* const MULTIPLE_TAGS =
37  "cannot assign multiple tags to the same node";
38 const char* const MULTIPLE_ANCHORS =
39  "cannot assign multiple anchors to the same node";
40 const char* const MULTIPLE_ALIASES =
41  "cannot assign multiple aliases to the same node";
42 const char* const ALIAS_CONTENT =
43  "aliases can't have any content, *including* tags";
44 const char* const INVALID_HEX = "bad character found while scanning hex number";
45 const char* const INVALID_UNICODE = "invalid unicode: ";
46 const char* const INVALID_ESCAPE = "unknown escape character: ";
47 const char* const UNKNOWN_TOKEN = "unknown token";
48 const char* const DOC_IN_SCALAR = "illegal document indicator in scalar";
49 const char* const EOF_IN_SCALAR = "illegal EOF in scalar";
50 const char* const CHAR_IN_SCALAR = "illegal character in scalar";
51 const char* const UNEXPECTED_SCALAR = "unexpected scalar";
52 const char* const UNEXPECTED_FLOW = "plain value cannot start with flow indicator character";
53 const char* const TAB_IN_INDENTATION =
54  "illegal tab when looking for indentation";
55 const char* const FLOW_END = "illegal flow end";
56 const char* const BLOCK_ENTRY = "illegal block entry";
57 const char* const MAP_KEY = "illegal map key";
58 const char* const MAP_VALUE = "illegal map value";
59 const char* const ALIAS_NOT_FOUND = "alias not found after *";
60 const char* const ANCHOR_NOT_FOUND = "anchor not found after &";
61 const char* const CHAR_IN_ALIAS =
62  "illegal character found while scanning alias";
63 const char* const CHAR_IN_ANCHOR =
64  "illegal character found while scanning anchor";
65 const char* const ZERO_INDENT_IN_BLOCK =
66  "cannot set zero indentation for a block scalar";
67 const char* const CHAR_IN_BLOCK = "unexpected character in block scalar";
68 const char* const AMBIGUOUS_ANCHOR =
69  "cannot assign the same alias to multiple nodes";
70 const char* const UNKNOWN_ANCHOR = "the referenced anchor is not defined: ";
71 
72 const char* const INVALID_NODE =
73  "invalid node; this may result from using a map iterator as a sequence "
74  "iterator, or vice-versa";
75 const char* const INVALID_SCALAR = "invalid scalar";
76 const char* const KEY_NOT_FOUND = "key not found";
77 const char* const BAD_CONVERSION = "bad conversion";
78 const char* const BAD_DEREFERENCE = "bad dereference";
79 const char* const BAD_SUBSCRIPT = "operator[] call on a scalar";
80 const char* const BAD_PUSHBACK = "appending to a non-sequence";
81 const char* const BAD_INSERT = "inserting in a non-convertible-to-map";
82 
83 const char* const UNMATCHED_GROUP_TAG = "unmatched group tag";
84 const char* const UNEXPECTED_END_SEQ = "unexpected end sequence token";
85 const char* const UNEXPECTED_END_MAP = "unexpected end map token";
86 const char* const SINGLE_QUOTED_CHAR =
87  "invalid character in single-quoted string";
88 const char* const INVALID_ANCHOR = "invalid anchor";
89 const char* const INVALID_ALIAS = "invalid alias";
90 const char* const INVALID_TAG = "invalid tag";
91 const char* const BAD_FILE = "bad file";
92 const char* const UNEXPECTED_TOKEN_AFTER_DOC = "unexpected token after end of document";
93 const char* const NON_UNIQUE_MAP_KEY = "map keys must be unique";
94 
95 template <typename T>
96 inline const std::string KEY_NOT_FOUND_WITH_KEY(
97  const T&, typename disable_if<is_numeric<T>>::type* = 0) {
98  return KEY_NOT_FOUND;
99 }
100 
101 inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
102  std::stringstream stream;
103  stream << KEY_NOT_FOUND << ": " << key;
104  return stream.str();
105 }
106 
107 inline const std::string KEY_NOT_FOUND_WITH_KEY(const char* key) {
108  std::stringstream stream;
109  stream << KEY_NOT_FOUND << ": " << key;
110  return stream.str();
111 }
112 
113 template <typename T>
114 inline const std::string KEY_NOT_FOUND_WITH_KEY(
115  const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
116  std::stringstream stream;
117  stream << KEY_NOT_FOUND << ": " << key;
118  return stream.str();
119 }
120 
121 template <typename T>
122 inline const std::string BAD_SUBSCRIPT_WITH_KEY(
123  const T&, typename disable_if<is_numeric<T>>::type* = nullptr) {
124  return BAD_SUBSCRIPT;
125 }
126 
127 inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) {
128  std::stringstream stream;
129  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
130  return stream.str();
131 }
132 
133 inline const std::string BAD_SUBSCRIPT_WITH_KEY(const char* key) {
134  std::stringstream stream;
135  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
136  return stream.str();
137 }
138 
139 template <typename T>
140 inline const std::string BAD_SUBSCRIPT_WITH_KEY(
141  const T& key, typename enable_if<is_numeric<T>>::type* = nullptr) {
142  std::stringstream stream;
143  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
144  return stream.str();
145 }
146 
147 inline const std::string INVALID_NODE_WITH_KEY(const std::string& key) {
148  std::stringstream stream;
149  if (key.empty()) {
150  return INVALID_NODE;
151  }
152  stream << "invalid node; first invalid key: \"" << key << "\"";
153  return stream.str();
154 }
155 } // namespace ErrorMsg
156 
157 class YAML_CPP_API Exception : public std::runtime_error {
158  public:
159  Exception(const Mark& mark_, const std::string& msg_)
160  : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {}
161  ~Exception() YAML_CPP_NOEXCEPT override;
162 
163  Exception(const Exception&) = default;
164 
165  Mark mark;
166  std::string msg;
167 
168  private:
169  static const std::string build_what(const Mark& mark,
170  const std::string& msg) {
171  if (mark.is_null()) {
172  return msg;
173  }
174 
175  std::stringstream output;
176  output << "yaml-cpp: error at line " << mark.line + 1 << ", column "
177  << mark.column + 1 << ": " << msg;
178  return output.str();
179  }
180 };
181 
182 class YAML_CPP_API ParserException : public Exception {
183  public:
184  ParserException(const Mark& mark_, const std::string& msg_)
185  : Exception(mark_, msg_) {}
186  ParserException(const ParserException&) = default;
187  ~ParserException() YAML_CPP_NOEXCEPT override;
188 };
189 
190 class YAML_CPP_API RepresentationException : public Exception {
191  public:
192  RepresentationException(const Mark& mark_, const std::string& msg_)
193  : Exception(mark_, msg_) {}
195  ~RepresentationException() YAML_CPP_NOEXCEPT override;
196 };
197 
198 // representation exceptions
199 class YAML_CPP_API InvalidScalar : public RepresentationException {
200  public:
201  InvalidScalar(const Mark& mark_)
202  : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {}
203  InvalidScalar(const InvalidScalar&) = default;
204  ~InvalidScalar() YAML_CPP_NOEXCEPT override;
205 };
206 
207 class YAML_CPP_API KeyNotFound : public RepresentationException {
208  public:
209  template <typename T>
210  KeyNotFound(const Mark& mark_, const T& key_)
211  : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {
212  }
213  KeyNotFound(const KeyNotFound&) = default;
214  ~KeyNotFound() YAML_CPP_NOEXCEPT override;
215 };
216 
217 template <typename T>
218 class YAML_CPP_API TypedKeyNotFound : public KeyNotFound {
219  public:
220  TypedKeyNotFound(const Mark& mark_, const T& key_)
221  : KeyNotFound(mark_, key_), key(key_) {}
222  ~TypedKeyNotFound() YAML_CPP_NOEXCEPT override = default;
223 
224  T key;
225 };
226 
227 template <typename T>
228 inline TypedKeyNotFound<T> MakeTypedKeyNotFound(const Mark& mark,
229  const T& key) {
230  return TypedKeyNotFound<T>(mark, key);
231 }
232 
233 class YAML_CPP_API InvalidNode : public RepresentationException {
234  public:
235  InvalidNode(const std::string& key)
236  : RepresentationException(Mark::null_mark(),
237  ErrorMsg::INVALID_NODE_WITH_KEY(key)) {}
238  InvalidNode(const InvalidNode&) = default;
239  ~InvalidNode() YAML_CPP_NOEXCEPT override;
240 };
241 
242 class YAML_CPP_API BadConversion : public RepresentationException {
243  public:
244  explicit BadConversion(const Mark& mark_)
245  : RepresentationException(mark_, ErrorMsg::BAD_CONVERSION) {}
246  BadConversion(const BadConversion&) = default;
247  ~BadConversion() YAML_CPP_NOEXCEPT override;
248 };
249 
250 template <typename T>
252  public:
253  explicit TypedBadConversion(const Mark& mark_) : BadConversion(mark_) {}
254 };
255 
256 class YAML_CPP_API BadDereference : public RepresentationException {
257  public:
259  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_DEREFERENCE) {}
260  BadDereference(const BadDereference&) = default;
261  ~BadDereference() YAML_CPP_NOEXCEPT override;
262 };
263 
264 class YAML_CPP_API BadSubscript : public RepresentationException {
265  public:
266  template <typename Key>
267  BadSubscript(const Mark& mark_, const Key& key)
268  : RepresentationException(mark_, ErrorMsg::BAD_SUBSCRIPT_WITH_KEY(key)) {}
269  BadSubscript(const BadSubscript&) = default;
270  ~BadSubscript() YAML_CPP_NOEXCEPT override;
271 };
272 
273 class YAML_CPP_API BadPushback : public RepresentationException {
274  public:
275  BadPushback()
276  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_PUSHBACK) {}
277  BadPushback(const BadPushback&) = default;
278  ~BadPushback() YAML_CPP_NOEXCEPT override;
279 };
280 
281 class YAML_CPP_API BadInsert : public RepresentationException {
282  public:
283  BadInsert()
284  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_INSERT) {}
285  BadInsert(const BadInsert&) = default;
286  ~BadInsert() YAML_CPP_NOEXCEPT override;
287 };
288 
289 class YAML_CPP_API EmitterException : public Exception {
290  public:
291  EmitterException(const std::string& msg_)
292  : Exception(Mark::null_mark(), msg_) {}
293  EmitterException(const EmitterException&) = default;
294  ~EmitterException() YAML_CPP_NOEXCEPT override;
295 };
296 
297 class YAML_CPP_API BadFile : public Exception {
298  public:
299  explicit BadFile(const std::string& filename)
300  : Exception(Mark::null_mark(),
301  std::string(ErrorMsg::BAD_FILE) + ": " + filename) {}
302  BadFile(const BadFile&) = default;
303  ~BadFile() YAML_CPP_NOEXCEPT override;
304 };
305 
306 class YAML_CPP_API NonUniqueMapKey : public RepresentationException {
307  public:
308  template <typename Key>
309  NonUniqueMapKey(const Mark& mark_, const Key& /*key*/)
310  : RepresentationException(mark_, ErrorMsg::NON_UNIQUE_MAP_KEY) {}
311  NonUniqueMapKey(const NonUniqueMapKey&) = default;
312  ~NonUniqueMapKey() YAML_CPP_NOEXCEPT override;
313 };
314 
315 } // namespace YAML
316 
317 #if defined(_MSC_VER)
318 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS __declspec(no_sanitize_address)
319 #elif defined(__has_cpp_attribute) && __has_cpp_attribute(gnu::no_sanitize)
320 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS [[gnu::no_sanitize("address")]]
321 #elif defined(__has_cpp_attribute) && __has_cpp_attribute(clang::no_sanitize)
322 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS [[clang::no_sanitize("address")]]
323 #elif defined(__has_attribute) && __has_attribute(no_sanitize)
324 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS \
325  __attribute__((no_sanitize("address")))
326 #elif defined(__has_attribute) && __has_attribute(no_sanitize_address)
327 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS __attribute__((no_sanitize_address))
328 #else
329 #define YAML_ATTRIBUTE_NO_SANITIZE_ADDRESS
330 #endif
331 
332 #endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: exceptions.h:281
Definition: mark.h:13
Definition: exceptions.h:199
Definition: exceptions.h:289
Definition: exceptions.h:264
Definition: exceptions.h:297
Definition: exceptions.h:273
Definition: exceptions.h:242
Definition: exceptions.h:182
Definition: exceptions.h:251
Definition: exceptions.h:207
Definition: exceptions.h:157
Definition: exceptions.h:306
Definition: exceptions.h:233
Definition: anchor.h:12
Definition: exceptions.h:218
Definition: exceptions.h:256
Definition: exceptions.h:190