Skip to content

Commit

Permalink
Collected pos, line, and column into a Mark struct
Browse files Browse the repository at this point in the history
  • Loading branch information
jbeder committed Jul 27, 2009
1 parent 9b78bd4 commit b1ce042
Show file tree
Hide file tree
Showing 17 changed files with 144 additions and 136 deletions.
37 changes: 19 additions & 18 deletions include/exceptions.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include "mark.h"
#include <exception>
#include <string>
#include <sstream>
Expand Down Expand Up @@ -60,16 +61,16 @@ namespace YAML

class Exception: public std::exception {
public:
Exception(int line_, int column_, const std::string& msg_)
: line(line_), column(column_), msg(msg_) {
Exception(const Mark& mark_, const std::string& msg_)
: mark(mark_), msg(msg_) {
std::stringstream output;
output << "Error at line " << line+1 << ", column " << column+1 << ": " << msg;
output << "Error at line " << mark.line+1 << ", column " << mark.column+1 << ": " << msg;
what_ = output.str();
}
virtual ~Exception() throw() {}
virtual const char *what() const throw() { return what_.c_str(); }

int line, column;
Mark mark;
std::string msg;

private:
Expand All @@ -78,53 +79,53 @@ namespace YAML

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

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

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

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

template <typename T>
class TypedKeyNotFound: public KeyNotFound {
public:
TypedKeyNotFound(int line_, int column_, const T& key_)
: KeyNotFound(line_, column_), key(key_) {}
TypedKeyNotFound(const Mark& mark_, const T& key_)
: KeyNotFound(mark_), key(key_) {}
~TypedKeyNotFound() throw() {}

T key;
};

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

class BadDereference: public RepresentationException {
public:
BadDereference()
: RepresentationException(-1, -1, ErrorMsg::BAD_DEREFERENCE) {}
: RepresentationException(Mark::null(), ErrorMsg::BAD_DEREFERENCE) {}
};

class EmitterException: public Exception {
public:
EmitterException(const std::string& msg_)
: Exception(-1, -1, msg_) {}
: Exception(Mark::null(), msg_) {}
};
}
16 changes: 8 additions & 8 deletions include/node.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

#include <string>
#include <vector>
#include <map>
#include "parserstate.h"
#include "conversion.h"
#include "exceptions.h"
#include "iterator.h"
#include "conversion.h"
#include "mark.h"
#include "noncopyable.h"
#include "parserstate.h"
#include <iostream>
#include <string>
#include <vector>
#include <map>

namespace YAML
{
Expand All @@ -30,8 +31,7 @@ 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; }
const Mark GetMark() const { return m_mark; }

// accessors
Iterator begin() const;
Expand Down Expand Up @@ -85,7 +85,7 @@ namespace YAML
void ParseAlias(Scanner *pScanner, const ParserState& state);

private:
int m_line, m_column;
Mark m_mark;
std::string m_anchor, m_tag;
Content *m_pContent;
bool m_alias;
Expand Down
4 changes: 2 additions & 2 deletions include/nodeimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace YAML
template <typename T>
inline void operator >> (const Node& node, T& value) {
if(!node.Read(value))
throw InvalidScalar(node.m_line, node.m_column);
throw InvalidScalar(node.m_mark);
}

template <typename T>
Expand Down Expand Up @@ -51,7 +51,7 @@ namespace YAML
}
}

throw MakeTypedKeyNotFound(m_line, m_column, key);
throw MakeTypedKeyNotFound(m_mark, key);
}

template <typename T>
Expand Down
10 changes: 5 additions & 5 deletions src/exp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace YAML
{
namespace Exp
{
unsigned ParseHex(const std::string& str, int line, int column)
unsigned ParseHex(const std::string& str, const Mark& mark)
{
unsigned value = 0;
for(unsigned i=0;i<str.size();i++) {
Expand All @@ -20,7 +20,7 @@ namespace YAML
else if('0' <= ch && ch <= '9')
digit = ch - '0';
else
throw ParserException(line, column, ErrorMsg::INVALID_HEX);
throw ParserException(mark, ErrorMsg::INVALID_HEX);

value = (value << 4) + digit;
}
Expand All @@ -44,13 +44,13 @@ namespace YAML
str += in.get();

// get the value
unsigned value = ParseHex(str, in.line, in.column);
unsigned value = ParseHex(str, in.mark());

// legal unicode?
if((value >= 0xD800 && value <= 0xDFFF) || value > 0x10FFFF) {
std::stringstream msg;
msg << ErrorMsg::INVALID_UNICODE << value;
throw ParserException(in.line, in.column, msg.str());
throw ParserException(in.mark(), msg.str());
}

// now break it up into chars
Expand Down Expand Up @@ -107,7 +107,7 @@ namespace YAML
}

std::stringstream msg;
throw ParserException(in.line, in.column, ErrorMsg::INVALID_ESCAPE + ch);
throw ParserException(in.mark(), ErrorMsg::INVALID_ESCAPE + ch);
}
}
}
10 changes: 5 additions & 5 deletions src/map.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ namespace YAML

while(1) {
if(pScanner->empty())
throw ParserException(-1, -1, ErrorMsg::END_OF_MAP);
throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP);

Token token = pScanner->peek();
if(token.type != TT_KEY && token.type != TT_BLOCK_END)
throw ParserException(token.line, token.column, ErrorMsg::END_OF_MAP);
throw ParserException(token.mark, ErrorMsg::END_OF_MAP);

pScanner->pop();
if(token.type == TT_BLOCK_END)
Expand Down Expand Up @@ -91,7 +91,7 @@ namespace YAML

while(1) {
if(pScanner->empty())
throw ParserException(-1, -1, ErrorMsg::END_OF_MAP_FLOW);
throw ParserException(Mark::null(), ErrorMsg::END_OF_MAP_FLOW);

Token& token = pScanner->peek();
// first check for end
Expand All @@ -102,7 +102,7 @@ namespace YAML

// now it better be a key
if(token.type != TT_KEY)
throw ParserException(token.line, token.column, ErrorMsg::END_OF_MAP_FLOW);
throw ParserException(token.mark, ErrorMsg::END_OF_MAP_FLOW);

pScanner->pop();

Expand All @@ -122,7 +122,7 @@ namespace YAML
if(nextToken.type == TT_FLOW_ENTRY)
pScanner->pop();
else if(nextToken.type != TT_FLOW_MAP_END)
throw ParserException(nextToken.line, nextToken.column, ErrorMsg::END_OF_MAP_FLOW);
throw ParserException(nextToken.mark, ErrorMsg::END_OF_MAP_FLOW);

// assign the map with the actual pointers
m_data[pKey.release()] = pValue.release();
Expand Down
11 changes: 5 additions & 6 deletions src/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ namespace YAML
return;

// save location
m_line = pScanner->peek().line;
m_column = pScanner->peek().column;
m_mark = pScanner->peek().mark;

ParseHeader(pScanner, state);

Expand Down Expand Up @@ -119,7 +118,7 @@ namespace YAML
{
Token& token = pScanner->peek();
if(m_tag != "")
throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_TAGS);
throw ParserException(token.mark, ErrorMsg::MULTIPLE_TAGS);

m_tag = state.TranslateTag(token.value);

Expand All @@ -132,7 +131,7 @@ namespace YAML
{
Token& token = pScanner->peek();
if(m_anchor != "")
throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_ANCHORS);
throw ParserException(token.mark, ErrorMsg::MULTIPLE_ANCHORS);

m_anchor = token.value;
m_alias = false;
Expand All @@ -143,9 +142,9 @@ namespace YAML
{
Token& token = pScanner->peek();
if(m_anchor != "")
throw ParserException(token.line, token.column, ErrorMsg::MULTIPLE_ALIASES);
throw ParserException(token.mark, ErrorMsg::MULTIPLE_ALIASES);
if(m_tag != "")
throw ParserException(token.line, token.column, ErrorMsg::ALIAS_CONTENT);
throw ParserException(token.mark, ErrorMsg::ALIAS_CONTENT);

m_anchor = token.value;
m_alias = true;
Expand Down
8 changes: 4 additions & 4 deletions src/parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,17 +98,17 @@ namespace YAML
void Parser::HandleYamlDirective(Token *pToken)
{
if(pToken->params.size() != 1)
throw ParserException(pToken->line, pToken->column, ErrorMsg::YAML_DIRECTIVE_ARGS);
throw ParserException(pToken->mark, 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, ErrorMsg::YAML_VERSION + pToken->params[0]);
throw ParserException(pToken->mark, ErrorMsg::YAML_VERSION + pToken->params[0]);

if(m_state.version.major > 1)
throw ParserException(pToken->line, pToken->column, ErrorMsg::YAML_MAJOR_VERSION);
throw ParserException(pToken->mark, ErrorMsg::YAML_MAJOR_VERSION);

// TODO: warning on major == 1, minor > 2?
}
Expand All @@ -118,7 +118,7 @@ namespace YAML
void Parser::HandleTagDirective(Token *pToken)
{
if(pToken->params.size() != 2)
throw ParserException(pToken->line, pToken->column, ErrorMsg::TAG_DIRECTIVE_ARGS);
throw ParserException(pToken->mark, ErrorMsg::TAG_DIRECTIVE_ARGS);

std::string handle = pToken->params[0], prefix = pToken->params[1];
m_state.tags[handle] = prefix;
Expand Down
27 changes: 13 additions & 14 deletions src/scanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ namespace YAML
VerifySimpleKey();

// maybe need to end some blocks
PopIndentTo(INPUT.column);
PopIndentTo(INPUT.column());

// *****
// And now branch based on the next few characters!
Expand All @@ -108,14 +108,14 @@ namespace YAML
if(!INPUT)
return EndStream();

if(INPUT.column == 0 && INPUT.peek() == Keys::Directive)
if(INPUT.column() == 0 && INPUT.peek() == Keys::Directive)
return ScanDirective();

// document token
if(INPUT.column == 0 && Exp::DocStart.Matches(INPUT))
if(INPUT.column() == 0 && Exp::DocStart.Matches(INPUT))
return ScanDocStart();

if(INPUT.column == 0 && Exp::DocEnd.Matches(INPUT))
if(INPUT.column() == 0 && Exp::DocEnd.Matches(INPUT))
return ScanDocEnd();

// flow start/end/entry
Expand Down Expand Up @@ -158,7 +158,7 @@ namespace YAML
return ScanPlainScalar();

// don't know what it is!
throw ParserException(INPUT.line, INPUT.column, ErrorMsg::UNKNOWN_TOKEN);
throw ParserException(INPUT.mark(), ErrorMsg::UNKNOWN_TOKEN);
}

// ScanToNextToken
Expand Down Expand Up @@ -230,8 +230,8 @@ namespace YAML
void Scanner::EndStream()
{
// force newline
if(INPUT.column > 0)
INPUT.column = 0;
if(INPUT.column() > 0)
INPUT.ResetColumn();

PopIndentTo(-1);
VerifyAllSimpleKeys();
Expand All @@ -257,9 +257,9 @@ namespace YAML
// now push
m_indents.push(column);
if(sequence)
m_tokens.push(Token(TT_BLOCK_SEQ_START, INPUT.line, INPUT.column));
m_tokens.push(Token(TT_BLOCK_SEQ_START, INPUT.mark()));
else
m_tokens.push(Token(TT_BLOCK_MAP_START, INPUT.line, INPUT.column));
m_tokens.push(Token(TT_BLOCK_MAP_START, INPUT.mark()));

return &m_tokens.back();
}
Expand All @@ -276,7 +276,7 @@ namespace YAML
// now pop away
while(!m_indents.empty() && m_indents.top() > column) {
m_indents.pop();
m_tokens.push(Token(TT_BLOCK_END, INPUT.line, INPUT.column));
m_tokens.push(Token(TT_BLOCK_END, INPUT.mark()));
}
}

Expand Down Expand Up @@ -309,13 +309,12 @@ namespace YAML
// . Does not parse any more tokens.
void Scanner::ThrowParserException(const std::string& msg) const
{
int line = -1, column = -1;
Mark mark = Mark::null();
if(!m_tokens.empty()) {
const Token& token = m_tokens.front();
line = token.line;
column = token.column;
mark = token.mark;
}
throw ParserException(line, column, msg);
throw ParserException(mark, msg);
}

void Scanner::ClearAnchors()
Expand Down
Loading

0 comments on commit b1ce042

Please sign in to comment.