tibia-client/src/framework/otml/otmlparser.cpp

161 lines
4.6 KiB
C++
Raw Normal View History

2011-07-13 23:12:36 +02:00
#include "otmlparser.h"
2011-08-14 04:09:11 +02:00
#include "otmldocument.h"
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
OTMLParser::OTMLParser(OTMLDocumentPtr doc, std::istream& in) :
currentDepth(0), currentLine(0),
doc(doc), currentParent(doc), previousNode(0),
in(in)
2011-07-13 23:12:36 +02:00
{
}
2011-08-14 04:09:11 +02:00
void OTMLParser::parse()
2011-07-13 23:12:36 +02:00
{
2011-08-14 04:09:11 +02:00
if(!in.good())
throw OTMLException(doc, "cannot read from input stream");
while(!in.eof())
parseLine(getNextLine());
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
std::string OTMLParser::getNextLine()
2011-07-13 23:12:36 +02:00
{
2011-08-14 04:09:11 +02:00
currentLine++;
std::string line;
std::getline(in, line);
return line;
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
int OTMLParser::getLineDepth(const std::string& line, bool multilining)
2011-07-13 23:12:36 +02:00
{
2011-08-14 04:09:11 +02:00
// count number of spaces at the line beginning
std::size_t spaces = 0;
while(line[spaces] == ' ')
spaces++;
// pre calculate depth
int depth = spaces / 2;
if(!multilining || depth <= currentDepth) {
// check the next character is a tab
if(line[spaces] == '\t')
throw OTMLException(doc, "indentation with tabs are not allowed", currentLine);
// must indent every 2 spaces
if(spaces % 2 != 0)
throw OTMLException(doc, "must indent every 2 spaces", currentLine);
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
return depth;
2011-07-13 23:12:36 +02:00
}
void OTMLParser::parseLine(std::string line)
{
2011-08-14 04:09:11 +02:00
int depth = getLineDepth(line);
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// remove line sides spaces
boost::trim(line);
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
// skip empty lines
if(line.empty())
2011-07-13 23:12:36 +02:00
return;
2011-08-14 04:09:11 +02:00
// skip comments
if(boost::starts_with(line, "//"))
return;
2011-07-13 23:12:36 +02:00
// a depth above, change current parent to the previous added node
2011-08-14 04:09:11 +02:00
if(depth == currentDepth+1) {
currentParent = previousNode;
// a depth below, change parent to previous parent
} else if(depth < currentDepth) {
for(int i=0;i<currentDepth-depth;++i)
currentParent = currentParent->parent();
// if it isn't the current depth, it's a syntax error
} else if(depth != currentDepth)
throw OTMLException(doc, "invalid indentation depth, are you indenting correctly?", currentLine);
// sets current depth
currentDepth = depth;
// alright, new depth is set, the line is not empty and it isn't a comment
// then it must be a node, so we parse it
parseNode(line);
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
void OTMLParser::parseNode(const std::string& data)
2011-07-13 23:12:36 +02:00
{
std::string tag;
std::string value;
std::size_t dotsPos = data.find_first_of(':');
2011-08-14 04:09:11 +02:00
// node that has no tag and may have a value
2011-07-13 23:12:36 +02:00
if(!data.empty() && data[0] == '-') {
value = data.substr(1);
boost::trim(value);
2011-08-14 04:09:11 +02:00
// node that has tag and possible a value
} else if(dotsPos != std::string::npos) {
2011-07-13 23:12:36 +02:00
tag = data.substr(0, dotsPos);
2011-08-14 04:09:11 +02:00
if(data.size() > dotsPos+1)
value = data.substr(dotsPos+1);
// node that has only a tag
} else {
2011-07-13 23:12:36 +02:00
tag = data;
}
boost::trim(tag);
boost::trim(value);
2011-08-14 04:09:11 +02:00
// process multitine values
if(value == "|" || value == "|-" || value == "|+") {
// reads next lines until we can a value below the same depth
2011-07-13 23:12:36 +02:00
std::string multiLineData;
do {
2011-08-14 04:09:11 +02:00
size_t lastPos = in.tellg();
std::string line = getNextLine();
int depth = getLineDepth(line, true);
2011-07-13 23:12:36 +02:00
// depth above current depth, add the text to the multiline
2011-08-14 04:09:11 +02:00
if(depth > currentDepth) {
multiLineData += line.substr((currentDepth+1)*2);
// it has contents below the current depth
} else {
// if not empty, its a node
2011-07-13 23:12:36 +02:00
boost::trim(line);
2011-08-14 04:09:11 +02:00
if(!line.empty()) {
// rewind and break
in.seekg(lastPos, std::ios::beg);
currentLine--;
break;
}
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
multiLineData += "\n";
} while(!in.eof());
/* determine how to treat new lines at the end
* | strip all new lines at the end and add just a new one
* |- strip all new lines at the end
* |+ keep all the new lines at the end (the new lines until next node)
*/
if(value == "|" || value == "|-") {
2011-07-13 23:12:36 +02:00
// remove all new lines at the end
2011-08-14 04:09:11 +02:00
int lastPos = multiLineData.length();
while(multiLineData[--lastPos] == '\n')
multiLineData.erase(lastPos, 1);
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
if(value == "|")
multiLineData.append("\n");
} // else it's |+
2011-07-13 23:12:36 +02:00
2011-08-14 04:09:11 +02:00
value = multiLineData;
2011-07-13 23:12:36 +02:00
}
2011-08-14 04:09:11 +02:00
// create the node
OTMLNodePtr node(new OTMLNode);
node->setUnique(dotsPos != std::string::npos);
node->setTag(tag);
node->setValue(value);
2011-08-15 16:06:15 +02:00
node->setSource(doc->source() + ":" + fw::safe_cast<std::string>(currentLine));
2011-08-14 04:09:11 +02:00
currentParent->addChild(node);
previousNode = node;
2011-07-13 23:12:36 +02:00
}