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 
93 template <typename T>
94 inline const std::string KEY_NOT_FOUND_WITH_KEY(
95  const T&, typename disable_if<is_numeric<T>>::type* = 0) {
96  return KEY_NOT_FOUND;
97 }
98 
99 inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
100  std::stringstream stream;
101  stream << KEY_NOT_FOUND << ": " << key;
102  return stream.str();
103 }
104 
105 inline const std::string KEY_NOT_FOUND_WITH_KEY(const char* key) {
106  std::stringstream stream;
107  stream << KEY_NOT_FOUND << ": " << key;
108  return stream.str();
109 }
110 
111 template <typename T>
112 inline const std::string KEY_NOT_FOUND_WITH_KEY(
113  const T& key, typename enable_if<is_numeric<T>>::type* = 0) {
114  std::stringstream stream;
115  stream << KEY_NOT_FOUND << ": " << key;
116  return stream.str();
117 }
118 
119 template <typename T>
120 inline const std::string BAD_SUBSCRIPT_WITH_KEY(
121  const T&, typename disable_if<is_numeric<T>>::type* = nullptr) {
122  return BAD_SUBSCRIPT;
123 }
124 
125 inline const std::string BAD_SUBSCRIPT_WITH_KEY(const std::string& key) {
126  std::stringstream stream;
127  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
128  return stream.str();
129 }
130 
131 inline const std::string BAD_SUBSCRIPT_WITH_KEY(const char* key) {
132  std::stringstream stream;
133  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
134  return stream.str();
135 }
136 
137 template <typename T>
138 inline const std::string BAD_SUBSCRIPT_WITH_KEY(
139  const T& key, typename enable_if<is_numeric<T>>::type* = nullptr) {
140  std::stringstream stream;
141  stream << BAD_SUBSCRIPT << " (key: \"" << key << "\")";
142  return stream.str();
143 }
144 
145 inline const std::string INVALID_NODE_WITH_KEY(const std::string& key) {
146  std::stringstream stream;
147  if (key.empty()) {
148  return INVALID_NODE;
149  }
150  stream << "invalid node; first invalid key: \"" << key << "\"";
151  return stream.str();
152 }
153 } // namespace ErrorMsg
154 
155 class YAML_CPP_API Exception : public std::runtime_error {
156  public:
157  Exception(const Mark& mark_, const std::string& msg_)
158  : std::runtime_error(build_what(mark_, msg_)), mark(mark_), msg(msg_) {}
159  ~Exception() YAML_CPP_NOEXCEPT override;
160 
161  Exception(const Exception&) = default;
162 
163  Mark mark;
164  std::string msg;
165 
166  private:
167  static const std::string build_what(const Mark& mark,
168  const std::string& msg) {
169  if (mark.is_null()) {
170  return msg;
171  }
172 
173  std::stringstream output;
174  output << "yaml-cpp: error at line " << mark.line + 1 << ", column "
175  << mark.column + 1 << ": " << msg;
176  return output.str();
177  }
178 };
179 
180 class YAML_CPP_API ParserException : public Exception {
181  public:
182  ParserException(const Mark& mark_, const std::string& msg_)
183  : Exception(mark_, msg_) {}
184  ParserException(const ParserException&) = default;
185  ~ParserException() YAML_CPP_NOEXCEPT override;
186 };
187 
188 class YAML_CPP_API RepresentationException : public Exception {
189  public:
190  RepresentationException(const Mark& mark_, const std::string& msg_)
191  : Exception(mark_, msg_) {}
193  ~RepresentationException() YAML_CPP_NOEXCEPT override;
194 };
195 
196 // representation exceptions
197 class YAML_CPP_API InvalidScalar : public RepresentationException {
198  public:
199  InvalidScalar(const Mark& mark_)
200  : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {}
201  InvalidScalar(const InvalidScalar&) = default;
202  ~InvalidScalar() YAML_CPP_NOEXCEPT override;
203 };
204 
205 class YAML_CPP_API KeyNotFound : public RepresentationException {
206  public:
207  template <typename T>
208  KeyNotFound(const Mark& mark_, const T& key_)
209  : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {
210  }
211  KeyNotFound(const KeyNotFound&) = default;
212  ~KeyNotFound() YAML_CPP_NOEXCEPT override;
213 };
214 
215 template <typename T>
216 class YAML_CPP_API TypedKeyNotFound : public KeyNotFound {
217  public:
218  TypedKeyNotFound(const Mark& mark_, const T& key_)
219  : KeyNotFound(mark_, key_), key(key_) {}
220  ~TypedKeyNotFound() YAML_CPP_NOEXCEPT override = default;
221 
222  T key;
223 };
224 
225 template <typename T>
226 inline TypedKeyNotFound<T> MakeTypedKeyNotFound(const Mark& mark,
227  const T& key) {
228  return TypedKeyNotFound<T>(mark, key);
229 }
230 
231 class YAML_CPP_API InvalidNode : public RepresentationException {
232  public:
233  InvalidNode(const std::string& key)
234  : RepresentationException(Mark::null_mark(),
235  ErrorMsg::INVALID_NODE_WITH_KEY(key)) {}
236  InvalidNode(const InvalidNode&) = default;
237  ~InvalidNode() YAML_CPP_NOEXCEPT override;
238 };
239 
240 class YAML_CPP_API BadConversion : public RepresentationException {
241  public:
242  explicit BadConversion(const Mark& mark_)
243  : RepresentationException(mark_, ErrorMsg::BAD_CONVERSION) {}
244  BadConversion(const BadConversion&) = default;
245  ~BadConversion() YAML_CPP_NOEXCEPT override;
246 };
247 
248 template <typename T>
250  public:
251  explicit TypedBadConversion(const Mark& mark_) : BadConversion(mark_) {}
252 };
253 
254 class YAML_CPP_API BadDereference : public RepresentationException {
255  public:
257  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_DEREFERENCE) {}
258  BadDereference(const BadDereference&) = default;
259  ~BadDereference() YAML_CPP_NOEXCEPT override;
260 };
261 
262 class YAML_CPP_API BadSubscript : public RepresentationException {
263  public:
264  template <typename Key>
265  BadSubscript(const Mark& mark_, const Key& key)
266  : RepresentationException(mark_, ErrorMsg::BAD_SUBSCRIPT_WITH_KEY(key)) {}
267  BadSubscript(const BadSubscript&) = default;
268  ~BadSubscript() YAML_CPP_NOEXCEPT override;
269 };
270 
271 class YAML_CPP_API BadPushback : public RepresentationException {
272  public:
273  BadPushback()
274  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_PUSHBACK) {}
275  BadPushback(const BadPushback&) = default;
276  ~BadPushback() YAML_CPP_NOEXCEPT override;
277 };
278 
279 class YAML_CPP_API BadInsert : public RepresentationException {
280  public:
281  BadInsert()
282  : RepresentationException(Mark::null_mark(), ErrorMsg::BAD_INSERT) {}
283  BadInsert(const BadInsert&) = default;
284  ~BadInsert() YAML_CPP_NOEXCEPT override;
285 };
286 
287 class YAML_CPP_API EmitterException : public Exception {
288  public:
289  EmitterException(const std::string& msg_)
290  : Exception(Mark::null_mark(), msg_) {}
291  EmitterException(const EmitterException&) = default;
292  ~EmitterException() YAML_CPP_NOEXCEPT override;
293 };
294 
295 class YAML_CPP_API BadFile : public Exception {
296  public:
297  explicit BadFile(const std::string& filename)
298  : Exception(Mark::null_mark(),
299  std::string(ErrorMsg::BAD_FILE) + ": " + filename) {}
300  BadFile(const BadFile&) = default;
301  ~BadFile() YAML_CPP_NOEXCEPT override;
302 };
303 } // namespace YAML
304 
305 #endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
Definition: exceptions.h:279
Definition: mark.h:13
Definition: exceptions.h:197
Definition: exceptions.h:287
Definition: exceptions.h:262
Definition: exceptions.h:295
Definition: exceptions.h:271
Definition: exceptions.h:240
Definition: exceptions.h:180
Definition: exceptions.h:249
Definition: exceptions.h:205
Definition: exceptions.h:155
Definition: exceptions.h:231
Definition: anchor.h:12
Definition: exceptions.h:216
Definition: exceptions.h:254
Definition: exceptions.h:188