mirror of
https://github.com/edubart/otclient.git
synced 2025-10-19 14:03:26 +02:00
use exceptions in FML
This commit is contained in:
@@ -76,14 +76,18 @@ inline std::ostream& operator<<(std::ostream& out, const Color& color)
|
||||
return out;
|
||||
}
|
||||
|
||||
inline void operator>>(const FML::Node& node, Color& color)
|
||||
inline bool operator>>(const FML::Node& node, Color& color)
|
||||
{
|
||||
int r, g, b, a;
|
||||
*node.at(0) >> r;
|
||||
*node.at(1) >> g;
|
||||
*node.at(2) >> b;
|
||||
*node.at(3) >> a;
|
||||
color.setRGBA(r,g,b,a);
|
||||
if(node.readAt(0, &r) &&
|
||||
node.readAt(1, &g) &&
|
||||
node.readAt(2, &b)) {
|
||||
a = 255;
|
||||
node.readAt(3, &a);
|
||||
color.setRGBA(r,g,b,a);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // COLOR_H
|
||||
|
@@ -38,6 +38,12 @@ bool fml_convert(const std::string& input, std::string& output) {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::string fml_int2str(int v)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << v;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Node
|
||||
@@ -48,6 +54,13 @@ Node::~Node()
|
||||
delete (*it);
|
||||
}
|
||||
|
||||
std::string Node::what() const
|
||||
{
|
||||
if(m_parser)
|
||||
return m_parser->what();
|
||||
return std::string();
|
||||
}
|
||||
|
||||
Node* Node::at(const std::string& childTag) const
|
||||
{
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
@@ -64,22 +77,6 @@ Node* Node::at(int pos) const
|
||||
return m_children[pos];
|
||||
}
|
||||
|
||||
std::string Node::value(const std::string& def) const
|
||||
{
|
||||
if(!m_value.empty())
|
||||
return m_value;
|
||||
return def;
|
||||
}
|
||||
|
||||
std::string Node::valueAt(const std::string childTag, const std::string& def) const
|
||||
{
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
if((*it)->tag() == childTag)
|
||||
return (*it)->value();
|
||||
}
|
||||
return def;
|
||||
}
|
||||
|
||||
void Node::addNode(Node *node)
|
||||
{
|
||||
if(node->hasTag() && node->hasValue()) {
|
||||
@@ -99,15 +96,101 @@ void Node::addNode(Node *node)
|
||||
std::string Node::generateErrorMessage(const std::string& message) const
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "FML error ";
|
||||
if(m_parser && !m_parser->getWhat().empty())
|
||||
ss << "in " << m_parser->getWhat();
|
||||
if(m_line > 0)
|
||||
ss << "at line " << m_line;
|
||||
ss << "FML error";
|
||||
if(!(what().empty()))
|
||||
ss << " in '" << what() << "'";
|
||||
if(m_line > 0) {
|
||||
ss << " at line " << m_line;
|
||||
if(hasTag())
|
||||
ss << ", in node '" << tag() << "'";
|
||||
}
|
||||
ss << ": " << message;
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
std::string Node::emitValue()
|
||||
{
|
||||
std::string tmpValue = value();
|
||||
|
||||
bool shouldQuote = false;
|
||||
if(tmpValue.find_first_of("\\") != std::string::npos) {
|
||||
boost::replace_all(tmpValue, "\\", "\\\\");
|
||||
shouldQuote = true;
|
||||
}
|
||||
if(tmpValue.find_first_of("\"") != std::string::npos) {
|
||||
boost::replace_all(tmpValue, "\"", "\\\"");
|
||||
shouldQuote = true;
|
||||
}
|
||||
if(tmpValue.find_first_of("\n") != std::string::npos) {
|
||||
boost::replace_all(tmpValue, "\n", "\\n");
|
||||
shouldQuote = true;
|
||||
}
|
||||
|
||||
if(shouldQuote) {
|
||||
tmpValue.append("\"");
|
||||
tmpValue.insert(0, "\"");
|
||||
}
|
||||
|
||||
return tmpValue;
|
||||
}
|
||||
|
||||
std::string Node::emit(int depth)
|
||||
{
|
||||
std::stringstream ss;
|
||||
std::stringstream inlinestr;
|
||||
bool shouldInline = false;
|
||||
|
||||
for(int i=1;i<depth;++i)
|
||||
ss << " ";
|
||||
|
||||
if(depth > 0) {
|
||||
shouldInline = true;
|
||||
|
||||
if(hasTag())
|
||||
ss << tag();
|
||||
|
||||
if(hasValue()) {
|
||||
if(hasTag())
|
||||
ss << ": ";
|
||||
else
|
||||
ss << "- ";
|
||||
ss << emitValue();
|
||||
ss << std::endl;
|
||||
shouldInline = false;
|
||||
}
|
||||
|
||||
if(size() > 8 || size() == 0)
|
||||
shouldInline = false;
|
||||
}
|
||||
|
||||
if(shouldInline) {
|
||||
inlinestr << "[";
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
Node* child = (*it);
|
||||
if(child->hasTag() || ss.str().length() > 31) {
|
||||
shouldInline = false;
|
||||
break;
|
||||
} else {
|
||||
if(it != m_children.begin())
|
||||
inlinestr << ", ";
|
||||
inlinestr << child->emitValue();
|
||||
}
|
||||
}
|
||||
inlinestr << "]";
|
||||
}
|
||||
|
||||
if(shouldInline) {
|
||||
ss << ": " << inlinestr.str() << std::endl;
|
||||
} else {
|
||||
if(!hasValue())
|
||||
ss << std::endl;
|
||||
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it)
|
||||
ss << (*it)->emit(depth+1);
|
||||
}
|
||||
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Parser
|
||||
@@ -118,7 +201,7 @@ Parser::~Parser()
|
||||
delete m_rootNode;
|
||||
}
|
||||
|
||||
bool Parser::load(std::istream& in)
|
||||
void Parser::load(std::istream& in)
|
||||
{
|
||||
// initialize root node
|
||||
if(m_rootNode)
|
||||
@@ -137,15 +220,11 @@ bool Parser::load(std::istream& in)
|
||||
std::string line;
|
||||
std::getline(in, line);
|
||||
parseLine(line);
|
||||
if(hasError())
|
||||
return false;
|
||||
}
|
||||
|
||||
// stop multilining if enabled
|
||||
if(isMultilining())
|
||||
stopMultilining();
|
||||
|
||||
return !hasError();
|
||||
}
|
||||
|
||||
void Parser::parseLine(std::string& line)
|
||||
@@ -173,7 +252,7 @@ void Parser::parseLine(std::string& line)
|
||||
depth = numSpaces / 2;
|
||||
// check for syntax error
|
||||
if(numSpaces % 2 != 0) {
|
||||
setErrorMessage("file must be idented every 2 whitespaces", m_currentLine);
|
||||
throwError("file must be idented every 2 whitespaces", m_currentLine);
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -189,7 +268,7 @@ void Parser::parseLine(std::string& line)
|
||||
m_currentParent = m_currentParent->parent();
|
||||
// else if nots the same depth it's a syntax error
|
||||
} else if(depth != m_currentDepth) {
|
||||
setErrorMessage("invalid indentation level", m_currentLine);
|
||||
throwError("invalid indentation level", m_currentLine);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -246,16 +325,17 @@ Node *Parser::parseNode(std::string& line)
|
||||
boost::trim(value);
|
||||
boost::tokenizer<boost::escaped_list_separator<char> > tokens(value);
|
||||
for(boost::tokenizer<boost::escaped_list_separator<char> >::iterator it = tokens.begin(); it != tokens.end(); ++it) {
|
||||
value = *it;
|
||||
boost::trim(value);
|
||||
|
||||
Node *child = new Node(this);
|
||||
child->setLine(m_currentLine);
|
||||
child->setValue(value);
|
||||
node->addNode(child);
|
||||
std::string tmp = (*it);
|
||||
boost::trim(tmp);
|
||||
if(!tmp.empty()) {
|
||||
Node *child = new Node(this);
|
||||
child->setLine(m_currentLine);
|
||||
child->setValue(tmp);
|
||||
node->addNode(child);
|
||||
}
|
||||
}
|
||||
} else
|
||||
setErrorMessage("missing ']' in sequence", m_currentLine);
|
||||
throwError("missing ']' in sequence", m_currentLine);
|
||||
}
|
||||
// text scalar
|
||||
else {
|
||||
@@ -291,7 +371,7 @@ void Parser::startMultilining(const std::string& param)
|
||||
m_multilineMode = MULTILINE_FOLD_FLOW;
|
||||
break;
|
||||
default:
|
||||
setErrorMessage("invalid multiline identifier", m_currentLine);
|
||||
throwError("invalid multiline identifier", m_currentLine);
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -343,16 +423,16 @@ bool Parser::parseMultiline(std::string line)
|
||||
return false;
|
||||
}
|
||||
|
||||
void Parser::setErrorMessage(const std::string& message, int line)
|
||||
void Parser::throwError(const std::string& message, int line)
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss << "FML syntax error in ";
|
||||
ss << "FML syntax error";
|
||||
if(!m_what.empty())
|
||||
ss << m_what << " ";
|
||||
ss << " in '" << m_what << "'";
|
||||
if(line > 0)
|
||||
ss << "at line " << line;
|
||||
ss << " at line " << line;
|
||||
ss << ": " << message;
|
||||
m_error = ss.str();
|
||||
throw Exception(ss.str());
|
||||
}
|
||||
|
||||
} // namespace FML {
|
||||
|
@@ -6,6 +6,9 @@
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <map>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <typeinfo>
|
||||
|
||||
#include <boost/utility.hpp>
|
||||
#include <boost/foreach.hpp>
|
||||
@@ -26,6 +29,16 @@ bool fml_convert(const V& in, T& out) {
|
||||
return !!ss;
|
||||
}
|
||||
|
||||
std::string fml_int2str(int v);
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Exception
|
||||
|
||||
class Exception : public std::runtime_error {
|
||||
public:
|
||||
explicit Exception(const std::string& what) : std::runtime_error(what) {}
|
||||
};
|
||||
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
// Node
|
||||
@@ -54,6 +67,7 @@ public:
|
||||
int line() const { return m_line; }
|
||||
int size() const { return m_children.size(); }
|
||||
Node* parent() { return m_parent; }
|
||||
std::string what() const;
|
||||
|
||||
// iterators
|
||||
typedef NodeList::iterator iterator;
|
||||
@@ -69,6 +83,7 @@ public:
|
||||
|
||||
// util for generating error message
|
||||
std::string generateErrorMessage(const std::string& message) const;
|
||||
void throwError(const std::string& message) const { throw Exception(generateErrorMessage(message)); }
|
||||
|
||||
// extracting values operator
|
||||
template <typename T>
|
||||
@@ -79,45 +94,95 @@ public:
|
||||
Node* at(int pos) const;
|
||||
|
||||
// get values
|
||||
std::string value(const std::string& def = std::string()) const;
|
||||
std::string valueAt(const std::string childTag, const std::string& def = std::string()) const;
|
||||
|
||||
// read values
|
||||
template <typename T>
|
||||
T read(T def = T()) const {
|
||||
T v = def;
|
||||
*this >> v;
|
||||
return v;
|
||||
std::string value(const std::string& def = std::string()) const {
|
||||
if(!m_value.empty())
|
||||
return m_value;
|
||||
return def;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool read(T* v) const {
|
||||
return (*this >> *v);
|
||||
std::string valueAt(const std::string childTag, const std::string& def = std::string()) const {
|
||||
if(Node* node = at(childTag))
|
||||
return node->value();
|
||||
return def;
|
||||
}
|
||||
|
||||
std::string valueAt(int pos, const std::string& def = std::string()) const {
|
||||
if(Node* node = at(pos))
|
||||
return node->value();
|
||||
return def;
|
||||
}
|
||||
|
||||
// read values into memory
|
||||
template <typename T>
|
||||
T readAt(const std::string& childTag, T def = T()) const {
|
||||
T v = def;
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
if((*it)->tag() == childTag) {
|
||||
*(*it) >> v;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return v;
|
||||
void read(T* v) const {
|
||||
if(!(*this >> *v))
|
||||
throw Exception(generateErrorMessage("failed to cast node value to type " + std::string(typeid(T).name())));
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool readAt(const std::string& childTag, T* v) const {
|
||||
for(NodeList::const_iterator it = m_children.begin(); it != m_children.end(); ++it) {
|
||||
if((*it)->tag() == childTag)
|
||||
return (*(*it) >> *v);
|
||||
if(Node* node = at(childTag)) {
|
||||
node->read<T>(v);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
bool readAt(int pos, T* v) const {
|
||||
if(Node* node = at(pos)) {
|
||||
node->read<T>(v);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
// read values
|
||||
template <typename T>
|
||||
T read() const {
|
||||
T v;
|
||||
read<T>(&v);
|
||||
return v;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T readAt(const std::string& childTag) const {
|
||||
if(Node* node = at(childTag))
|
||||
return node->read<T>();
|
||||
throw Exception(generateErrorMessage("child node " + childTag + " not found"));
|
||||
return T();
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T readAt(int pos) const {
|
||||
if(Node* node = at(pos))
|
||||
return node->read<T>();
|
||||
throw Exception(generateErrorMessage("child node at pos " + fml_int2str(pos) + " not found"));
|
||||
return T();
|
||||
}
|
||||
|
||||
// read values with defaults
|
||||
template <typename T>
|
||||
T readAt(const std::string& childTag, const T& def) const {
|
||||
if(Node* node = at(childTag))
|
||||
return node->read<T>();
|
||||
else
|
||||
return def;
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
T readAt(int pos, const T& def) const {
|
||||
if(Node* node = at(pos))
|
||||
return node->read<T>();
|
||||
else
|
||||
return def;
|
||||
}
|
||||
|
||||
void addNode(Node* node);
|
||||
|
||||
std::string emitValue();
|
||||
std::string emit(int depth = 0);
|
||||
|
||||
private:
|
||||
Parser* m_parser;
|
||||
Node* m_parent;
|
||||
@@ -140,32 +205,29 @@ bool operator >> (const Node& node, T& v)
|
||||
template <typename T>
|
||||
bool operator >> (const Node& node, std::vector<T>& v)
|
||||
{
|
||||
bool ret = true;
|
||||
v.clear();
|
||||
v.resize(node.size());
|
||||
for(unsigned i=0;i<node.size();++i) {
|
||||
if(!(*node.at(i) >> v[i]))
|
||||
ret = false;
|
||||
}
|
||||
return ret;
|
||||
std::vector<T>& tmp;
|
||||
tmp.resize(node.size());
|
||||
for(unsigned i=0;i<node.size();++i)
|
||||
v[i] = node.readAt<T>(i);
|
||||
v = tmp;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename K, typename T>
|
||||
bool operator >> (const Node& node, std::map<K, T>& m)
|
||||
{
|
||||
bool ret = true;
|
||||
m.clear();
|
||||
std::map<K, T> tmp;
|
||||
for(Node::const_iterator it = node.begin(); it != node.end(); ++it) {
|
||||
Node* child = (*it);
|
||||
K k;
|
||||
T v;
|
||||
if(fml_convert<std::string, K>(child->tag(), k)) {
|
||||
*child >> v;
|
||||
m[k] = v;
|
||||
ret = false;
|
||||
}
|
||||
if(fml_convert<std::string, K>((*it)->tag(), k)) {
|
||||
(*it)->read(&v);
|
||||
tmp[k] = v;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
return ret;
|
||||
m = tmp;
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -186,12 +248,10 @@ public:
|
||||
Parser(std::istream& in, std::string what = std::string()) : m_rootNode(0), m_what(what) { load(in); }
|
||||
~Parser();
|
||||
|
||||
bool load(std::istream& in);
|
||||
void load(std::istream& in);
|
||||
|
||||
Node* getDocument() const { return m_rootNode; }
|
||||
bool hasError() const { return !m_error.empty(); }
|
||||
std::string getErrorMessage() { return m_error; }
|
||||
std::string getWhat() { return m_what; }
|
||||
std::string what() { return m_what; }
|
||||
|
||||
protected:
|
||||
void parseLine(std::string& line);
|
||||
@@ -203,7 +263,7 @@ protected:
|
||||
bool parseMultiline(std::string line);
|
||||
bool isMultilining() { return m_multilineMode != DONT_MULTILINE; }
|
||||
|
||||
void setErrorMessage(const std::string& message, int line = 0);
|
||||
void throwError(const std::string& message, int line = 0);
|
||||
|
||||
private:
|
||||
int m_currentDepth;
|
||||
@@ -213,7 +273,6 @@ private:
|
||||
Node* m_rootNode;
|
||||
MultilineMode m_multilineMode;
|
||||
std::string m_multilineData;
|
||||
std::string m_error;
|
||||
std::string m_what;
|
||||
};
|
||||
|
||||
|
@@ -86,10 +86,15 @@ inline std::ostream& operator<<(std::ostream& out, const TPoint<T>& point)
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void operator>>(const FML::Node& node, TPoint<T>& point)
|
||||
inline bool operator>>(const FML::Node& node, TPoint<T>& point)
|
||||
{
|
||||
*node.at(0) >> point.x;
|
||||
*node.at(1) >> point.y;
|
||||
T x, y;
|
||||
if(node.readAt(0, &x) && node.readAt(1, &y)) {
|
||||
point.x = x;
|
||||
point.y = y;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -310,14 +310,17 @@ inline std::ostream& operator<<(std::ostream& out, const TRect<T>& rect)
|
||||
}
|
||||
|
||||
template <class T>
|
||||
inline void operator>>(const FML::Node& node, TRect<T>& rect)
|
||||
inline bool operator>>(const FML::Node& node, TRect<T>& rect)
|
||||
{
|
||||
T x, y, width, height;
|
||||
*node.at(0) >> x;
|
||||
*node.at(1) >> y;
|
||||
*node.at(2) >> width;
|
||||
*node.at(3) >> height;
|
||||
rect.setRect(x, y, width, height);
|
||||
if(node.readAt(0, &x) &&
|
||||
node.readAt(1, &y) &&
|
||||
node.readAt(2, &width) &&
|
||||
node.readAt(3, &height)) {
|
||||
rect.setRect(x, y, width, height);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif // RECT_H
|
||||
|
@@ -112,12 +112,14 @@ typedef TSize<int> Size;
|
||||
typedef TSize<float> SizeF;
|
||||
|
||||
template <class T>
|
||||
inline void operator>>(const FML::Node& node, TSize<T>& size)
|
||||
inline bool operator>>(const FML::Node& node, TSize<T>& size)
|
||||
{
|
||||
T w, h;
|
||||
*node.at(0) >> w;
|
||||
*node.at(1) >> h;
|
||||
size.setSize(w, h);
|
||||
if(node.readAt(0, &w) && node.readAt(1, &h)) {
|
||||
size.setSize(w, h);
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@@ -41,7 +41,7 @@ R convert_cast(T t)
|
||||
try {
|
||||
ret = boost::lexical_cast<R>(t);
|
||||
} catch(boost::bad_lexical_cast bad) {
|
||||
flogError("Error converting type: %s", bad.what());
|
||||
flogError("Error converting value: %s", bad.what());
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
Reference in New Issue
Block a user