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

195 lines
6.0 KiB
C++
Raw Normal View History

2011-08-28 15:17:58 +02:00
/*
2012-01-02 17:58:37 +01:00
* Copyright (c) 2010-2012 OTClient <https://github.com/edubart/otclient>
2011-08-28 15:17:58 +02:00
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
2011-07-13 23:12:36 +02:00
#include "otmlparser.h"
2011-08-14 04:09:11 +02:00
#include "otmldocument.h"
#include "otmlexception.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
if(depth == -1)
return;
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(':');
int nodeLine = currentLine;
2011-07-13 23:12:36 +02:00
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 = OTMLNode::create(tag);
2011-08-14 04:09:11 +02:00
node->setUnique(dotsPos != std::string::npos);
node->setTag(tag);
node->setSource(doc->source() + ":" + stdext::unsafe_cast<std::string>(nodeLine));
// ~ is considered the null value
if(value == "~")
node->setNull(true);
else
node->setValue(value);
2011-08-14 04:09:11 +02:00
currentParent->addChild(node);
previousNode = node;
2011-07-13 23:12:36 +02:00
}