2011-08-28 15:17:58 +02:00
|
|
|
/*
|
2014-04-01 07:36:42 +02:00
|
|
|
* Copyright (c) 2010-2014 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"
|
2011-08-19 20:53:23 +02:00
|
|
|
#include "otmlexception.h"
|
2012-07-23 22:50:34 +02:00
|
|
|
#include <boost/tokenizer.hpp>
|
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-19 20:53:23 +02:00
|
|
|
if(depth == -1)
|
|
|
|
return;
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
// remove line sides spaces
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::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
|
2012-08-01 09:49:09 +02:00
|
|
|
if(stdext::starts_with(line, "//"))
|
2011-08-14 04:09:11 +02:00
|
|
|
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)
|
2012-07-29 05:34:40 +02:00
|
|
|
currentParent = parentMap[currentParent];
|
2011-08-14 04:09:11 +02:00
|
|
|
// 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-19 20:53:23 +02:00
|
|
|
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);
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::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;
|
|
|
|
}
|
|
|
|
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::trim(tag);
|
|
|
|
stdext::trim(value);
|
2011-07-13 23:12:36 +02:00
|
|
|
|
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
|
2012-08-01 09:49:09 +02:00
|
|
|
stdext::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
|
2011-08-19 20:53:23 +02:00
|
|
|
OTMLNodePtr node = OTMLNode::create(tag);
|
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
node->setUnique(dotsPos != std::string::npos);
|
|
|
|
node->setTag(tag);
|
2012-05-28 15:06:26 +02:00
|
|
|
node->setSource(doc->source() + ":" + stdext::unsafe_cast<std::string>(nodeLine));
|
2011-08-19 20:53:23 +02:00
|
|
|
|
|
|
|
// ~ is considered the null value
|
|
|
|
if(value == "~")
|
|
|
|
node->setNull(true);
|
2012-07-23 22:50:34 +02:00
|
|
|
else {
|
2012-08-01 09:49:09 +02:00
|
|
|
if(stdext::starts_with(value, "[") && stdext::ends_with(value, "]")) {
|
2012-07-23 22:50:34 +02:00
|
|
|
std::string tmp = value.substr(1, value.length()-2);
|
|
|
|
boost::tokenizer<boost::escaped_list_separator<char>> tokens(tmp);
|
2012-08-01 09:49:09 +02:00
|
|
|
for(std::string v : tokens) {
|
|
|
|
stdext::trim(v);
|
|
|
|
node->writeIn(v);
|
|
|
|
}
|
2012-07-23 22:50:34 +02:00
|
|
|
} else
|
|
|
|
node->setValue(value);
|
|
|
|
}
|
2011-08-19 20:53:23 +02:00
|
|
|
|
2011-08-14 04:09:11 +02:00
|
|
|
currentParent->addChild(node);
|
2012-07-29 05:34:40 +02:00
|
|
|
parentMap[node] = currentParent;
|
2011-08-14 04:09:11 +02:00
|
|
|
previousNode = node;
|
2011-07-13 23:12:36 +02:00
|
|
|
}
|