Combined the myriad ScannerExceptions and ParserExceptions to a single ParserException class that has a message and a line/column position in the file where the error occurred.

This commit is contained in:
beder
2008-07-08 05:48:38 +00:00
parent 1acc0e4982
commit 2f5c19fa00
12 changed files with 114 additions and 113 deletions

View File

@@ -3,6 +3,7 @@
#include "exceptions.h"
#include "exp.h"
#include "scanscalar.h"
#include <sstream>
namespace YAML
{
@@ -22,7 +23,8 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat indicator
// store pos and eat indicator
int line = INPUT.line, column = INPUT.column;
INPUT.eat(1);
// read name
@@ -47,7 +49,7 @@ namespace YAML
params.push_back(param);
}
Token *pToken = new Token(TT_DIRECTIVE);
Token *pToken = new Token(TT_DIRECTIVE, line, column);
pToken->value = name;
pToken->params = params;
m_tokens.push(pToken);
@@ -61,8 +63,9 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(3);
m_tokens.push(new Token(TT_DOC_START));
m_tokens.push(new Token(TT_DOC_START, line, column));
}
// DocEnd
@@ -73,8 +76,9 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(3);
m_tokens.push(new Token(TT_DOC_END));
m_tokens.push(new Token(TT_DOC_END, line, column));
}
// FlowStart
@@ -86,24 +90,26 @@ namespace YAML
m_simpleKeyAllowed = true;
// eat
int line = INPUT.line, column = INPUT.column;
char ch = INPUT.get();
TOKEN_TYPE type = (ch == Keys::FlowSeqStart ? TT_FLOW_SEQ_START : TT_FLOW_MAP_START);
m_tokens.push(new Token(type));
m_tokens.push(new Token(type, line, column));
}
// FlowEnd
void Scanner::ScanFlowEnd()
{
if(m_flowLevel == 0)
throw IllegalFlowEnd();
throw ParserException(INPUT.line, INPUT.column, "illegal flow end");
m_flowLevel--;
m_simpleKeyAllowed = false;
// eat
int line = INPUT.line, column = INPUT.column;
char ch = INPUT.get();
TOKEN_TYPE type = (ch == Keys::FlowSeqEnd ? TT_FLOW_SEQ_END : TT_FLOW_MAP_END);
m_tokens.push(new Token(type));
m_tokens.push(new Token(type, line, column));
}
// FlowEntry
@@ -112,8 +118,9 @@ namespace YAML
m_simpleKeyAllowed = true;
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(1);
m_tokens.push(new Token(TT_FLOW_ENTRY));
m_tokens.push(new Token(TT_FLOW_ENTRY, line, column));
}
// BlockEntry
@@ -121,18 +128,19 @@ namespace YAML
{
// we better be in the block context!
if(m_flowLevel > 0)
throw IllegalBlockEntry();
throw ParserException(INPUT.line, INPUT.column, "illegal block entry");
// can we put it here?
if(!m_simpleKeyAllowed)
throw IllegalBlockEntry();
throw ParserException(INPUT.line, INPUT.column, "illegal block entry");
PushIndentTo(INPUT.column, true);
m_simpleKeyAllowed = true;
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(1);
m_tokens.push(new Token(TT_BLOCK_ENTRY));
m_tokens.push(new Token(TT_BLOCK_ENTRY, line, column));
}
// Key
@@ -141,7 +149,7 @@ namespace YAML
// handle keys diffently in the block context (and manage indents)
if(m_flowLevel == 0) {
if(!m_simpleKeyAllowed)
throw IllegalMapKey();
throw ParserException(INPUT.line, INPUT.column, "illegal map key");
PushIndentTo(INPUT.column, false);
}
@@ -153,8 +161,9 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(1);
m_tokens.push(new Token(TT_KEY));
m_tokens.push(new Token(TT_KEY, line, column));
}
// Value
@@ -168,7 +177,7 @@ namespace YAML
// handle values diffently in the block context (and manage indents)
if(m_flowLevel == 0) {
if(!m_simpleKeyAllowed)
throw IllegalMapValue();
throw ParserException(INPUT.line, INPUT.column, "illegal map value");
PushIndentTo(INPUT.column, false);
}
@@ -181,8 +190,9 @@ namespace YAML
}
// eat
int line = INPUT.line, column = INPUT.column;
INPUT.eat(1);
m_tokens.push(new Token(TT_VALUE));
m_tokens.push(new Token(TT_VALUE, line, column));
}
// AnchorOrAlias
@@ -197,6 +207,7 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat the indicator
int line = INPUT.line, column = INPUT.column;
char indicator = INPUT.get();
alias = (indicator == Keys::Alias);
@@ -205,15 +216,24 @@ namespace YAML
name += INPUT.get();
// we need to have read SOMETHING!
if(name.empty())
throw AnchorNotFound();
if(name.empty()) {
std::stringstream msg;
msg << (alias ? "alias" : "anchor");
msg << " not found after ";
msg << (alias ? "*" : "&");
throw ParserException(INPUT.line, INPUT.column, msg.str());
}
// and needs to end correctly
if(INPUT.peek() != EOF && !Exp::AnchorEnd.Matches(INPUT))
throw IllegalCharacterInAnchor();
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());
}
// and we're done
Token *pToken = new Token(alias ? TT_ALIAS : TT_ANCHOR);
Token *pToken = new Token(alias ? TT_ALIAS : TT_ANCHOR, line, column);
pToken->value = name;
m_tokens.push(pToken);
}
@@ -229,6 +249,7 @@ namespace YAML
m_simpleKeyAllowed = false;
// eat the indicator
int line = INPUT.line, column = INPUT.column;
handle += INPUT.get();
// read the handle
@@ -249,7 +270,7 @@ namespace YAML
handle = "!";
}
Token *pToken = new Token(TT_TAG);
Token *pToken = new Token(TT_TAG, line, column);
pToken->value = handle;
pToken->params.push_back(suffix);
m_tokens.push(pToken);
@@ -276,6 +297,7 @@ namespace YAML
if(m_simpleKeyAllowed)
InsertSimpleKey();
int line = INPUT.line, column = INPUT.column;
scalar = ScanScalar(INPUT, params);
// can have a simple key only if we ended the scalar by starting a new line
@@ -284,9 +306,9 @@ 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 IllegalScalar();
throw ParserException(INPUT.line, INPUT.column, "illegal character in scalar");
Token *pToken = new Token(TT_SCALAR);
Token *pToken = new Token(TT_SCALAR, line, column);
pToken->value = scalar;
m_tokens.push(pToken);
}
@@ -316,10 +338,11 @@ namespace YAML
if(m_simpleKeyAllowed)
InsertSimpleKey();
int line = INPUT.line, column = INPUT.column;
scalar = ScanScalar(INPUT, params);
m_simpleKeyAllowed = false;
Token *pToken = new Token(TT_SCALAR);
Token *pToken = new Token(TT_SCALAR, line, column);
pToken->value = scalar;
m_tokens.push(pToken);
}
@@ -337,6 +360,7 @@ namespace YAML
params.detectIndent = true;
// eat block indicator ('|' or '>')
int line = INPUT.line, column = INPUT.column;
char indicator = INPUT.get();
params.fold = (indicator == Keys::FoldedScalar);
@@ -350,7 +374,7 @@ namespace YAML
params.chomp = STRIP;
else if(Exp::Digit.Matches(ch)) {
if(ch == '0')
throw ZeroIndentationInBlockScalar();
throw ParserException(INPUT.line, INPUT.column, "cannot set zero indentation for a block scalar");
params.indent = ch - '0';
params.detectIndent = false;
@@ -368,7 +392,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 UnexpectedCharacterInBlockScalar();
throw ParserException(INPUT.line, INPUT.column, "unexpected character in block scalar");
// set the initial indentation
if(m_indents.top() >= 0)
@@ -383,7 +407,7 @@ namespace YAML
// simple keys always ok after block scalars (since we're gonna start a new line anyways)
m_simpleKeyAllowed = true;
Token *pToken = new Token(TT_SCALAR);
Token *pToken = new Token(TT_SCALAR, line, column);
pToken->value = scalar;
m_tokens.push(pToken);
}