exceptions.h 5.34 KB
Newer Older
1
2
3
4
#pragma once

#include <exception>
#include <string>
5
#include <sstream>
6
7
8
9
10
11

namespace YAML
{
	// error messages
	namespace ErrorMsg
	{
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
		const std::string YAML_DIRECTIVE_ARGS    = "YAML directives must have exactly one argument";
		const std::string YAML_VERSION           = "bad YAML version: ";
		const std::string YAML_MAJOR_VERSION     = "YAML major version too large";
		const std::string TAG_DIRECTIVE_ARGS     = "TAG directives must have exactly two arguments";
		const std::string END_OF_MAP             = "end of map not found";
		const std::string END_OF_MAP_FLOW        = "end of map flow not found";
		const std::string END_OF_SEQ             = "end of sequence not found";
		const std::string END_OF_SEQ_FLOW        = "end of sequence flow not found";
		const std::string MULTIPLE_TAGS          = "cannot assign multiple tags to the same node";
		const std::string MULTIPLE_ANCHORS       = "cannot assign multiple anchors to the same node";
		const std::string MULTIPLE_ALIASES       = "cannot assign multiple aliases to the same node";
		const std::string ALIAS_CONTENT          = "aliases can't have any content, *including* tags";
		const std::string INVALID_HEX            = "bad character found while scanning hex number";
		const std::string INVALID_UNICODE        = "invalid unicode: ";
		const std::string INVALID_ESCAPE         = "unknown escape character: ";
		const std::string UNKNOWN_TOKEN          = "unknown token";
		const std::string DOC_IN_SCALAR          = "illegal document indicator in scalar";
		const std::string EOF_IN_SCALAR          = "illegal EOF in scalar";
		const std::string CHAR_IN_SCALAR         = "illegal character in scalar";
		const std::string TAB_IN_INDENTATION     = "illegal tab when looking for indentation";
		const std::string FLOW_END               = "illegal flow end";
		const std::string BLOCK_ENTRY            = "illegal block entry";
		const std::string MAP_KEY                = "illegal map key";
		const std::string MAP_VALUE              = "illegal map value";
		const std::string ALIAS_NOT_FOUND        = "alias not found after *";
		const std::string ANCHOR_NOT_FOUND       = "anchor not found after &";
		const std::string CHAR_IN_ALIAS          = "illegal character found while scanning alias";
		const std::string CHAR_IN_ANCHOR         = "illegal character found while scanning anchor";
		const std::string ZERO_INDENT_IN_BLOCK   = "cannot set zero indentation for a block scalar";
		const std::string CHAR_IN_BLOCK          = "unexpected character in block scalar";
42
43
		const std::string AMBIGUOUS_ANCHOR     = "cannot assign the same alias to multiple nodes";
		const std::string UNKNOWN_ANCHOR       = "the referenced anchor is not defined";
44

45
46
47
48
49
50
51
52
53
54
55
56
57
58
		const std::string INVALID_SCALAR         = "invalid scalar";
		const std::string KEY_NOT_FOUND          = "key not found";
		const std::string BAD_DEREFERENCE        = "bad dereference";
		
		const std::string UNMATCHED_GROUP_TAG    = "unmatched group tag";
		const std::string UNEXPECTED_END_SEQ     = "unexpected end sequence token";
		const std::string UNEXPECTED_END_MAP     = "unexpected end map token";
		const std::string SINGLE_QUOTED_CHAR     = "invalid character in single-quoted string";
		const std::string INVALID_ANCHOR         = "invalid anchor";
		const std::string INVALID_ALIAS          = "invalid alias";
		const std::string EXPECTED_KEY_TOKEN     = "expected key token";
		const std::string EXPECTED_VALUE_TOKEN   = "expected value token";
		const std::string UNEXPECTED_KEY_TOKEN   = "unexpected key token";
		const std::string UNEXPECTED_VALUE_TOKEN = "unexpected value token";
59
	}
60
61
62
63
64

	class Exception: public std::exception {
	public:
		Exception(int line_, int column_, const std::string& msg_)
			: line(line_), column(column_), msg(msg_) {}
65
		virtual ~Exception() throw() {}
66
67
68
69
70
		virtual const char *what() const throw() {
			std::stringstream output;
			output << "Error at line " << line+1 << ", column " << column+1 << ": " << msg;
			return output.str().c_str();
		}
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100

		int line, column;
		std::string msg;
	};

	class ParserException: public Exception {
	public:
		ParserException(int line_, int column_, const std::string& msg_)
			: Exception(line_, column_, msg_) {}
	};

	class RepresentationException: public Exception {
	public:
		RepresentationException(int line_, int column_, const std::string& msg_)
			: Exception(line_, column_, msg_) {}
	};

	// representation exceptions
	class InvalidScalar: public RepresentationException {
	public:
		InvalidScalar(int line_, int column_)
			: RepresentationException(line_, column_, ErrorMsg::INVALID_SCALAR) {}
	};

	class KeyNotFound: public RepresentationException {
	public:
		KeyNotFound(int line_, int column_)
			: RepresentationException(line_, column_, ErrorMsg::KEY_NOT_FOUND) {}
	};

101
102
103
104
105
	template <typename T>
	class TypedKeyNotFound: public KeyNotFound {
	public:
		TypedKeyNotFound(int line_, int column_, const T& key_)
			: KeyNotFound(line_, column_), key(key_) {}
106
		~TypedKeyNotFound() throw() {}
107
108
109
110
111
112
113
114
115

		T key;
	};

	template <typename T>
	TypedKeyNotFound <T> MakeTypedKeyNotFound(int line, int column, const T& key) {
		return TypedKeyNotFound <T> (line, column, key);
	}

116
117
118
119
120
	class BadDereference: public RepresentationException {
	public:
		BadDereference()
			: RepresentationException(-1, -1, ErrorMsg::BAD_DEREFERENCE) {}
	};
121
122
123
124
125
126
	
	class EmitterException: public Exception {
	public:
		EmitterException(const std::string& msg_)
		: Exception(-1, -1, msg_) {}
	};
127
}