Added line/column data for nodes so they can give better invalid scalar exceptions.

This commit is contained in:
Jesse Beder
2008-11-18 04:20:07 +00:00
parent 9969ff512e
commit 043bbddcf0
3 changed files with 56 additions and 19 deletions

View File

@@ -5,23 +5,6 @@
namespace YAML 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 // error messages
namespace ErrorMsg namespace ErrorMsg
{ {
@@ -55,5 +38,50 @@ namespace YAML
const std::string CHAR_IN_ANCHOR = "illegal character found while scanning anchor"; 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 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 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) {}
};
} }

View File

@@ -27,6 +27,10 @@ namespace YAML
CONTENT_TYPE GetType() const; CONTENT_TYPE GetType() const;
// file location of start of this node
int GetLine() const { return m_line; }
int GetColumn() const { return m_column; }
// accessors // accessors
Iterator begin() const; Iterator begin() const;
Iterator end() const; Iterator end() const;
@@ -81,6 +85,7 @@ namespace YAML
void ParseAlias(Scanner *pScanner, const ParserState& state); void ParseAlias(Scanner *pScanner, const ParserState& state);
private: private:
int m_line, m_column;
std::string m_anchor, m_tag; std::string m_anchor, m_tag;
Content *m_pContent; Content *m_pContent;
bool m_alias; bool m_alias;
@@ -97,7 +102,7 @@ namespace YAML
inline void operator >> (const Node& node, T& value) inline void operator >> (const Node& node, T& value)
{ {
if(!Read(node, value)) if(!Read(node, value))
throw InvalidScalar(); throw InvalidScalar(node.m_line, node.m_column);
} }
template <typename T> template <typename T>
@@ -114,7 +119,7 @@ namespace YAML
} }
} }
throw BadDereference(); throw KeyNotFound(m_line, m_column);
} }
template <typename T> template <typename T>

View File

@@ -37,6 +37,10 @@ namespace YAML
{ {
Clear(); Clear();
// save location
m_line = pScanner->peek().line;
m_column = pScanner->peek().column;
ParseHeader(pScanner, state); ParseHeader(pScanner, state);
// is this an alias? if so, it can have no content // is this an alias? if so, it can have no content