Centralized the error messages to one location.

This commit is contained in:
Jesse Beder 2008-07-08 20:31:48 +00:00
parent c0c55fe50b
commit 03e6b5b991
9 changed files with 71 additions and 46 deletions

View file

@ -18,4 +18,39 @@ namespace YAML
// representation exceptions
class InvalidScalar: public RepresentationException {};
class BadDereference: public RepresentationException {};
// error messages
namespace ErrorMsg
{
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";
}
}

View file

@ -19,7 +19,7 @@ namespace YAML
else if('0' <= ch && ch <= '9')
digit = ch - '0';
else
throw ParserException(line, column, "bad character found while scanning hex number");
throw ParserException(line, column, ErrorMsg::INVALID_HEX);
value = (value << 4) + digit;
}
@ -48,7 +48,7 @@ namespace YAML
// legal unicode?
if((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
std::stringstream msg;
msg << "invalid unicode: " << value;
msg << ErrorMsg::INVALID_UNICODE << value;
throw ParserException(in.line, in.column, msg.str());
}
@ -106,8 +106,7 @@ namespace YAML
}
std::stringstream msg;
msg << "unknown escape character: " << ch;
throw ParserException(in.line, in.column, msg.str());
throw ParserException(in.line, in.column, ErrorMsg::INVALID_ESCAPE + ch);
}
}
}

10
map.cpp
View file

@ -57,10 +57,10 @@ namespace YAML
while(1) {
Token *pToken = pScanner->PeekNextToken();
if(!pToken)
throw ParserException(-1, -1, "end of map not found");
throw ParserException(-1, -1, ErrorMsg::END_OF_MAP);
if(pToken->type != TT_KEY && pToken->type != TT_BLOCK_END)
throw ParserException(pToken->line, pToken->column, "end of map not found");
throw ParserException(pToken->line, pToken->column, ErrorMsg::END_OF_MAP);
pScanner->PopNextToken();
if(pToken->type == TT_BLOCK_END)
@ -96,7 +96,7 @@ namespace YAML
while(1) {
Token *pToken = pScanner->PeekNextToken();
if(!pToken)
throw ParserException(-1, -1, "end of map flow not found");
throw ParserException(-1, -1, ErrorMsg::END_OF_MAP_FLOW);
// first check for end
if(pToken->type == TT_FLOW_MAP_END) {
@ -106,7 +106,7 @@ namespace YAML
// now it better be a key
if(pToken->type != TT_KEY)
throw ParserException(pToken->line, pToken->column, "end of map flow not found");
throw ParserException(pToken->line, pToken->column, ErrorMsg::END_OF_MAP_FLOW);
pScanner->PopNextToken();
@ -128,7 +128,7 @@ namespace YAML
if(pToken->type == TT_FLOW_ENTRY)
pScanner->EatNextToken();
else if(pToken->type != TT_FLOW_MAP_END)
throw ParserException(pToken->line, pToken->column, "end of map flow not found");
throw ParserException(pToken->line, pToken->column, ErrorMsg::END_OF_MAP_FLOW);
m_data[pKey] = pValue;
} catch(Exception& e) {

View file

@ -85,7 +85,7 @@ namespace YAML
{
Token *pToken = pScanner->PeekNextToken();
if(m_tag != "")
throw ParserException(pToken->line, pToken->column, "cannot assign multiple tags to the same node");
throw ParserException(pToken->line, pToken->column, ErrorMsg::MULTIPLE_TAGS);
m_tag = state.TranslateTag(pToken->value);
@ -98,7 +98,7 @@ namespace YAML
{
Token *pToken = pScanner->PeekNextToken();
if(m_anchor != "")
throw ParserException(pToken->line, pToken->column, "cannot assign multiple anchors to the same node");
throw ParserException(pToken->line, pToken->column, ErrorMsg::MULTIPLE_ANCHORS);
m_anchor = pToken->value;
m_alias = false;
@ -109,9 +109,9 @@ namespace YAML
{
Token *pToken = pScanner->PeekNextToken();
if(m_anchor != "")
throw ParserException(pToken->line, pToken->column, "cannot assign multiple aliases to the same node");
throw ParserException(pToken->line, pToken->column, ErrorMsg::MULTIPLE_ALIASES);
if(m_tag != "")
throw ParserException(pToken->line, pToken->column, "aliases can't have any content, *including* tags");
throw ParserException(pToken->line, pToken->column, ErrorMsg::ALIAS_CONTENT);
m_anchor = pToken->value;
m_alias = true;

View file

@ -90,17 +90,17 @@ namespace YAML
void Parser::HandleYamlDirective(Token *pToken)
{
if(pToken->params.size() != 1)
throw ParserException(pToken->line, pToken->column, "YAML directives must have exactly one argument");
throw ParserException(pToken->line, pToken->column, ErrorMsg::YAML_DIRECTIVE_ARGS);
std::stringstream str(pToken->params[0]);
str >> m_state.version.major;
str.get();
str >> m_state.version.minor;
if(!str || str.peek() != EOF)
throw ParserException(pToken->line, pToken->column, "bad YAML version: " + pToken->params[0]);
throw ParserException(pToken->line, pToken->column, ErrorMsg::YAML_VERSION + pToken->params[0]);
if(m_state.version.major > 1)
throw ParserException(pToken->line, pToken->column, "YAML major version > 1");
throw ParserException(pToken->line, pToken->column, ErrorMsg::YAML_MAJOR_VERSION);
// TODO: warning on major == 1, minor > 2?
}
@ -110,7 +110,7 @@ namespace YAML
void Parser::HandleTagDirective(Token *pToken)
{
if(pToken->params.size() != 2)
throw ParserException(pToken->line, pToken->column, "TAG directives must have exactly two arguments");
throw ParserException(pToken->line, pToken->column, ErrorMsg::TAG_DIRECTIVE_ARGS);
std::string handle = pToken->params[0], prefix = pToken->params[1];
m_state.tags[handle] = prefix;

View file

@ -157,7 +157,7 @@ namespace YAML
return ScanPlainScalar();
// don't know what it is!
throw ParserException(INPUT.line, INPUT.column, "unknown token");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::UNKNOWN_TOKEN);
}
// ScanToNextToken

View file

@ -35,7 +35,7 @@ namespace YAML
if(params.onDocIndicator == BREAK)
break;
else if(params.onDocIndicator == THROW)
throw ParserException(INPUT.line, INPUT.column, "illegal document indicator in scalar");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::DOC_IN_SCALAR);
}
foundNonEmptyLine = true;
@ -61,7 +61,7 @@ namespace YAML
// eof? if we're looking to eat something, then we throw
if(INPUT.peek() == EOF) {
if(params.eatEnd)
throw ParserException(INPUT.line, INPUT.column, "illegal EOF in scalar");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::EOF_IN_SCALAR);
break;
}
@ -97,7 +97,7 @@ namespace YAML
while(Exp::Blank.Matches(INPUT)) {
// we check for tabs that masquerade as indentation
if(INPUT.peek() == '\t'&& INPUT.column < params.indent && params.onTabInIndentation == THROW)
throw ParserException(INPUT.line, INPUT.column, "illegal tab when looking for indentation");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::TAB_IN_INDENTATION);
if(!params.eatLeadingWhitespace)
break;

View file

@ -100,7 +100,7 @@ namespace YAML
void Scanner::ScanFlowEnd()
{
if(m_flowLevel == 0)
throw ParserException(INPUT.line, INPUT.column, "illegal flow end");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::FLOW_END);
m_flowLevel--;
m_simpleKeyAllowed = false;
@ -128,11 +128,11 @@ namespace YAML
{
// we better be in the block context!
if(m_flowLevel > 0)
throw ParserException(INPUT.line, INPUT.column, "illegal block entry");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::BLOCK_ENTRY);
// can we put it here?
if(!m_simpleKeyAllowed)
throw ParserException(INPUT.line, INPUT.column, "illegal block entry");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::BLOCK_ENTRY);
PushIndentTo(INPUT.column, true);
m_simpleKeyAllowed = true;
@ -149,7 +149,7 @@ namespace YAML
// handle keys diffently in the block context (and manage indents)
if(m_flowLevel == 0) {
if(!m_simpleKeyAllowed)
throw ParserException(INPUT.line, INPUT.column, "illegal map key");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::MAP_KEY);
PushIndentTo(INPUT.column, false);
}
@ -177,7 +177,7 @@ namespace YAML
// handle values diffently in the block context (and manage indents)
if(m_flowLevel == 0) {
if(!m_simpleKeyAllowed)
throw ParserException(INPUT.line, INPUT.column, "illegal map value");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::MAP_VALUE);
PushIndentTo(INPUT.column, false);
}
@ -216,21 +216,12 @@ namespace YAML
name += INPUT.get();
// we need to have read SOMETHING!
if(name.empty()) {
std::stringstream msg;
msg << (alias ? "alias" : "anchor");
msg << " not found after ";
msg << (alias ? "*" : "&");
throw ParserException(INPUT.line, INPUT.column, msg.str());
}
if(name.empty())
throw ParserException(INPUT.line, INPUT.column, alias ? ErrorMsg::ALIAS_NOT_FOUND : ErrorMsg::ANCHOR_NOT_FOUND);
// and needs to end correctly
if(INPUT.peek() != EOF && !Exp::AnchorEnd.Matches(INPUT)) {
std::stringstream msg;
msg << "illegal character found while scanning ";
msg << (alias ? "alias" : "anchor");
throw ParserException(INPUT.line, INPUT.column, msg.str());
}
if(INPUT.peek() != EOF && !Exp::AnchorEnd.Matches(INPUT))
throw ParserException(INPUT.line, INPUT.column, alias ? ErrorMsg::CHAR_IN_ALIAS : ErrorMsg::CHAR_IN_ANCHOR);
// and we're done
Token *pToken = new Token(alias ? TT_ALIAS : TT_ANCHOR, line, column);
@ -306,7 +297,7 @@ namespace YAML
// finally, we can't have any colons in a scalar, so if we ended on a colon, there
// had better be a break after it
if(Exp::IllegalColonInScalar.Matches(INPUT))
throw ParserException(INPUT.line, INPUT.column, "illegal character in scalar");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::CHAR_IN_SCALAR);
Token *pToken = new Token(TT_SCALAR, line, column);
pToken->value = scalar;
@ -374,7 +365,7 @@ namespace YAML
params.chomp = STRIP;
else if(Exp::Digit.Matches(ch)) {
if(ch == '0')
throw ParserException(INPUT.line, INPUT.column, "cannot set zero indentation for a block scalar");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::ZERO_INDENT_IN_BLOCK);
params.indent = ch - '0';
params.detectIndent = false;
@ -392,7 +383,7 @@ namespace YAML
// if it's not a line break, then we ran into a bad character inline
if(INPUT && !Exp::Break.Matches(INPUT))
throw ParserException(INPUT.line, INPUT.column, "unexpected character in block scalar");
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::CHAR_IN_BLOCK);
// set the initial indentation
if(m_indents.top() >= 0)

View file

@ -68,10 +68,10 @@ namespace YAML
while(1) {
Token *pToken = pScanner->PeekNextToken();
if(!pToken)
throw ParserException(-1, -1, "end of sequence not found");
throw ParserException(-1, -1, ErrorMsg::END_OF_SEQ);
if(pToken->type != TT_BLOCK_ENTRY && pToken->type != TT_BLOCK_END)
throw ParserException(pToken->line, pToken->column, "end of sequence not found");
throw ParserException(pToken->line, pToken->column, ErrorMsg::END_OF_SEQ);
pScanner->PopNextToken();
if(pToken->type == TT_BLOCK_END)
@ -111,7 +111,7 @@ namespace YAML
while(1) {
Token *pToken = pScanner->PeekNextToken();
if(!pToken)
throw ParserException(-1, -1, "end of sequence flow not found");
throw ParserException(-1, -1, ErrorMsg::END_OF_SEQ_FLOW);
// first check for end
if(pToken->type == TT_FLOW_SEQ_END) {
@ -129,7 +129,7 @@ namespace YAML
if(pToken->type == TT_FLOW_ENTRY)
pScanner->EatNextToken();
else if(pToken->type != TT_FLOW_SEQ_END)
throw ParserException(pToken->line, pToken->column, "end of sequence flow not found");
throw ParserException(pToken->line, pToken->column, ErrorMsg::END_OF_SEQ_FLOW);
}
}