00001 #pragma once
00002
00003 #ifndef EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00004 #define EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66
00005
00006
00007 #include "mark.h"
00008 #include "traits.h"
00009 #include <exception>
00010 #include <string>
00011 #include <sstream>
00012
00013 namespace YAML
00014 {
00015
00016 namespace ErrorMsg
00017 {
00018 const std::string YAML_DIRECTIVE_ARGS = "YAML directives must have exactly one argument";
00019 const std::string YAML_VERSION = "bad YAML version: ";
00020 const std::string YAML_MAJOR_VERSION = "YAML major version too large";
00021 const std::string REPEATED_YAML_DIRECTIVE= "repeated YAML directive";
00022 const std::string TAG_DIRECTIVE_ARGS = "TAG directives must have exactly two arguments";
00023 const std::string REPEATED_TAG_DIRECTIVE = "repeated TAG directive";
00024 const std::string CHAR_IN_TAG_HANDLE = "illegal character found while scanning tag handle";
00025 const std::string TAG_WITH_NO_SUFFIX = "tag handle with no suffix";
00026 const std::string END_OF_VERBATIM_TAG = "end of verbatim tag not found";
00027 const std::string END_OF_MAP = "end of map not found";
00028 const std::string END_OF_MAP_FLOW = "end of map flow not found";
00029 const std::string END_OF_SEQ = "end of sequence not found";
00030 const std::string END_OF_SEQ_FLOW = "end of sequence flow not found";
00031 const std::string MULTIPLE_TAGS = "cannot assign multiple tags to the same node";
00032 const std::string MULTIPLE_ANCHORS = "cannot assign multiple anchors to the same node";
00033 const std::string MULTIPLE_ALIASES = "cannot assign multiple aliases to the same node";
00034 const std::string ALIAS_CONTENT = "aliases can't have any content, *including* tags";
00035 const std::string INVALID_HEX = "bad character found while scanning hex number";
00036 const std::string INVALID_UNICODE = "invalid unicode: ";
00037 const std::string INVALID_ESCAPE = "unknown escape character: ";
00038 const std::string UNKNOWN_TOKEN = "unknown token";
00039 const std::string DOC_IN_SCALAR = "illegal document indicator in scalar";
00040 const std::string EOF_IN_SCALAR = "illegal EOF in scalar";
00041 const std::string CHAR_IN_SCALAR = "illegal character in scalar";
00042 const std::string TAB_IN_INDENTATION = "illegal tab when looking for indentation";
00043 const std::string FLOW_END = "illegal flow end";
00044 const std::string BLOCK_ENTRY = "illegal block entry";
00045 const std::string MAP_KEY = "illegal map key";
00046 const std::string MAP_VALUE = "illegal map value";
00047 const std::string ALIAS_NOT_FOUND = "alias not found after *";
00048 const std::string ANCHOR_NOT_FOUND = "anchor not found after &";
00049 const std::string CHAR_IN_ALIAS = "illegal character found while scanning alias";
00050 const std::string CHAR_IN_ANCHOR = "illegal character found while scanning anchor";
00051 const std::string ZERO_INDENT_IN_BLOCK = "cannot set zero indentation for a block scalar";
00052 const std::string CHAR_IN_BLOCK = "unexpected character in block scalar";
00053 const std::string AMBIGUOUS_ANCHOR = "cannot assign the same alias to multiple nodes";
00054 const std::string UNKNOWN_ANCHOR = "the referenced anchor is not defined";
00055
00056 const std::string INVALID_SCALAR = "invalid scalar";
00057 const std::string KEY_NOT_FOUND = "key not found";
00058 const std::string BAD_DEREFERENCE = "bad dereference";
00059
00060 const std::string UNMATCHED_GROUP_TAG = "unmatched group tag";
00061 const std::string UNEXPECTED_END_SEQ = "unexpected end sequence token";
00062 const std::string UNEXPECTED_END_MAP = "unexpected end map token";
00063 const std::string SINGLE_QUOTED_CHAR = "invalid character in single-quoted string";
00064 const std::string INVALID_ANCHOR = "invalid anchor";
00065 const std::string INVALID_ALIAS = "invalid alias";
00066 const std::string INVALID_TAG = "invalid tag";
00067 const std::string EXPECTED_KEY_TOKEN = "expected key token";
00068 const std::string EXPECTED_VALUE_TOKEN = "expected value token";
00069 const std::string UNEXPECTED_KEY_TOKEN = "unexpected key token";
00070 const std::string UNEXPECTED_VALUE_TOKEN = "unexpected value token";
00071
00072 template <typename T>
00073 inline const std::string KEY_NOT_FOUND_WITH_KEY(const T&, typename disable_if<is_numeric<T> >::type * = 0) {
00074 return KEY_NOT_FOUND;
00075 }
00076
00077 inline const std::string KEY_NOT_FOUND_WITH_KEY(const std::string& key) {
00078 return KEY_NOT_FOUND + ": " + key;
00079 }
00080
00081 template <typename T>
00082 inline const std::string KEY_NOT_FOUND_WITH_KEY(const T& key, typename enable_if<is_numeric<T> >::type * = 0) {
00083 std::stringstream stream;
00084 stream << KEY_NOT_FOUND << ": " << key;
00085 return stream.str();
00086 }
00087 }
00088
00089 class Exception: public std::exception {
00090 public:
00091 Exception(const Mark& mark_, const std::string& msg_)
00092 : mark(mark_), msg(msg_) {
00093 std::stringstream output;
00094 output << "yaml-cpp: error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg;
00095 what_ = output.str();
00096 }
00097 virtual ~Exception() throw() {}
00098 virtual const char *what() const throw() { return what_.c_str(); }
00099
00100 Mark mark;
00101 std::string msg;
00102
00103 private:
00104 std::string what_;
00105 };
00106
00107 class ParserException: public Exception {
00108 public:
00109 ParserException(const Mark& mark_, const std::string& msg_)
00110 : Exception(mark_, msg_) {}
00111 };
00112
00113 class RepresentationException: public Exception {
00114 public:
00115 RepresentationException(const Mark& mark_, const std::string& msg_)
00116 : Exception(mark_, msg_) {}
00117 };
00118
00119
00120 class InvalidScalar: public RepresentationException {
00121 public:
00122 InvalidScalar(const Mark& mark_)
00123 : RepresentationException(mark_, ErrorMsg::INVALID_SCALAR) {}
00124 };
00125
00126 class KeyNotFound: public RepresentationException {
00127 public:
00128 template <typename T>
00129 KeyNotFound(const Mark& mark_, const T& key_)
00130 : RepresentationException(mark_, ErrorMsg::KEY_NOT_FOUND_WITH_KEY(key_)) {}
00131 };
00132
00133 template <typename T>
00134 class TypedKeyNotFound: public KeyNotFound {
00135 public:
00136 TypedKeyNotFound(const Mark& mark_, const T& key_)
00137 : KeyNotFound(mark_, key_), key(key_) {}
00138 virtual ~TypedKeyNotFound() throw() {}
00139
00140 T key;
00141 };
00142
00143 template <typename T>
00144 inline TypedKeyNotFound <T> MakeTypedKeyNotFound(const Mark& mark, const T& key) {
00145 return TypedKeyNotFound <T> (mark, key);
00146 }
00147
00148 class BadDereference: public RepresentationException {
00149 public:
00150 BadDereference()
00151 : RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {}
00152 };
00153
00154 class EmitterException: public Exception {
00155 public:
00156 EmitterException(const std::string& msg_)
00157 : Exception(Mark::null(), msg_) {}
00158 };
00159 }
00160
00161 #endif // EXCEPTIONS_H_62B23520_7C8E_11DE_8A39_0800200C9A66