diff --git a/include/exceptions.h b/include/exceptions.h index 427088a..5dc0062 100644 --- a/include/exceptions.h +++ b/include/exceptions.h @@ -5,23 +5,6 @@ namespace YAML { - class Exception: public std::exception {}; - class ParserException: public Exception { - public: - ParserException(int line_, int column_, const std::string& msg_) - : line(line_), column(column_), msg(msg_) {} - virtual ~ParserException() throw () {} - - int line, column; - std::string msg; - }; - - class RepresentationException: public Exception {}; - - // representation exceptions - class InvalidScalar: public RepresentationException {}; - class BadDereference: public RepresentationException {}; - // error messages namespace ErrorMsg { @@ -55,5 +38,50 @@ namespace YAML 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"; + + const std::string INVALID_SCALAR = "invalid scalar"; + const std::string KEY_NOT_FOUND = "key not found"; + const std::string BAD_DEREFERENCE = "bad dereference"; } + + class Exception: public std::exception { + public: + Exception(int line_, int column_, const std::string& msg_) + : line(line_), column(column_), msg(msg_) {} + virtual ~Exception() throw () {} + + 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) {} + }; + + class BadDereference: public RepresentationException { + public: + BadDereference() + : RepresentationException(-1, -1, ErrorMsg::BAD_DEREFERENCE) {} + }; } diff --git a/include/node.h b/include/node.h index ebfcdd9..cb627fe 100644 --- a/include/node.h +++ b/include/node.h @@ -27,6 +27,10 @@ namespace YAML CONTENT_TYPE GetType() const; + // file location of start of this node + int GetLine() const { return m_line; } + int GetColumn() const { return m_column; } + // accessors Iterator begin() const; Iterator end() const; @@ -81,6 +85,7 @@ namespace YAML void ParseAlias(Scanner *pScanner, const ParserState& state); private: + int m_line, m_column; std::string m_anchor, m_tag; Content *m_pContent; bool m_alias; @@ -97,7 +102,7 @@ namespace YAML inline void operator >> (const Node& node, T& value) { if(!Read(node, value)) - throw InvalidScalar(); + throw InvalidScalar(node.m_line, node.m_column); } template @@ -114,7 +119,7 @@ namespace YAML } } - throw BadDereference(); + throw KeyNotFound(m_line, m_column); } template diff --git a/src/node.cpp b/src/node.cpp index 48b81ec..a7a3573 100644 --- a/src/node.cpp +++ b/src/node.cpp @@ -37,6 +37,10 @@ namespace YAML { Clear(); + // save location + m_line = pScanner->peek().line; + m_column = pScanner->peek().column; + ParseHeader(pScanner, state); // is this an alias? if so, it can have no content