diff options
| -rw-r--r-- | diceparser.cpp | 26 | ||||
| -rw-r--r-- | diceparser.pri | 6 | ||||
| -rw-r--r-- | node/stringnode.cpp | 115 | ||||
| -rw-r--r-- | node/stringnode.h | 18 | ||||
| -rw-r--r-- | parsingtoolbox.cpp | 52 | ||||
| -rw-r--r-- | parsingtoolbox.h | 7 |
6 files changed, 216 insertions, 8 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index b6081a7..809a557 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -41,6 +41,8 @@ #include "node/mergenode.h" #include "node/ifnode.h" #include "node/paintnode.h" +#include "node/stringnode.h" + #define DEFAULT_FACES_NUMBER 10 @@ -171,9 +173,8 @@ bool DiceParser::parseLine(QString str) if(keepParsing) { // keepParsing = - readOperator(str,m_current); - - m_current = getLatestNode(m_current); + readOperator(str,m_current); + m_current = getLatestNode(m_current); } } @@ -990,6 +991,14 @@ bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, nodeif->setValidator(validator); previous->setNextNode(nodeif); } + else + { + delete nodeif; + } + } + else + { + delete nodeif; } } @@ -1073,7 +1082,7 @@ QString DiceParser::humanReadableError() bool DiceParser::readOperand(QString& str,ExecutionNode* & node) { qint64 myNumber=1; - + QString resultStr; if(m_parsingToolbox->readNumber(str,myNumber)) { NumberNode* numberNode = new NumberNode(); @@ -1082,6 +1091,15 @@ bool DiceParser::readOperand(QString& str,ExecutionNode* & node) node = numberNode; return true; } + else if(m_parsingToolbox->readString(str,resultStr)) + { + StringNode* strNode = new StringNode(); + strNode->setString(resultStr); + node = strNode; + return true; + } + ///! @todo Make this operator working + return false; } void DiceParser::writeDownDotTree(QString filepath) diff --git a/diceparser.pri b/diceparser.pri index ef975a0..c3d64b1 100644 --- a/diceparser.pri +++ b/diceparser.pri @@ -16,7 +16,8 @@ SOURCES += $$PWD/diceparser.cpp \ $$PWD/result/stringresult.cpp \ $$PWD/compositevalidator.cpp \ $$PWD/dicealias.cpp \ - $$PWD/operationcondition.cpp + $$PWD/operationcondition.cpp \ + $$PWD/node/stringnode.cpp HEADERS += \ @@ -33,7 +34,8 @@ HEADERS += \ $$PWD/result/stringresult.h \ $$PWD/compositevalidator.h \ $$PWD/dicealias.h \ - $$PWD/operationcondition.h + $$PWD/operationcondition.h \ + $$PWD/node/stringnode.h HEADERS += \ diff --git a/node/stringnode.cpp b/node/stringnode.cpp index 5dc99fd..157f595 100644 --- a/node/stringnode.cpp +++ b/node/stringnode.cpp @@ -1,6 +1,121 @@ #include "stringnode.h" StringNode::StringNode() + : m_stringResult(new StringResult()) { + m_result = m_stringResult; +} + +void StringNode::run(ExecutionNode *previous) +{ + m_previousNode = previous; + if(NULL!=previous) + { + m_result->setPrevious(previous->getResult()); + } + if(NULL!=m_nextNode) + { + m_nextNode->run(this); + } +} + +void StringNode::setString(QString str) +{ + m_data = str; + m_stringResult->setText(m_data); +} +QString StringNode::toString(bool withLabel) const +{ + if(withLabel) + { + return QString("%1 [label=\"StringNode %2\"]").arg(m_id).arg(m_data); + } + else + { + return m_id; + } +} +/*void StringNode::getScalarResult() +{ + QString scalarText; + + if(m_diceParser->hasIntegerResultNotInFirst()) + { + scalarText = QStringLiteral("%1").arg(m_diceParser->getLastIntegerResult()); + } + else if(hasDiceList) + { + scalarText = QStringLiteral("%1").arg(m_diceParser->getSumOfDiceResult()); + } +}*/ + +/*bool StringNode::getMessageResult(QString& value, QString& command, QString& list) +{ + QString scalarText; + QString diceText; + //QString pattern(""); + + + + bool hasDiceList = false; + if(m_diceParser->hasDiceResult()) + { + ExportedDiceResult diceList; + bool ok; + m_diceParser->getLastDiceResult(diceList,ok);//fills the ExportedDiceResult + diceText = diceToText(diceList); + hasDiceList= true; + } + if(m_diceParser->hasSeparator()) + { + bool ok; + QStringList allStringlist = m_diceParser->getAllDiceResult(ok); + if(ok) + { + QString patternColor("<span class=\"dice\">%1</span>"); + list = patternColor.arg(allStringlist.join(' ')); + scalarText = list; + } + } + else if(m_diceParser->hasIntegerResultNotInFirst()) + { + scalarText = QStringLiteral("%1").arg(m_diceParser->getLastIntegerResult()); + } + else if(hasDiceList) + { + scalarText = QStringLiteral("%1").arg(m_diceParser->getSumOfDiceResult()); + } + value=scalarText; + list = diceText.trimmed(); + command = m_diceParser->getDiceCommand().toHtmlEscaped(); + if(m_diceParser->hasStringResult()) + { + bool ok; + QStringList allStringlist = m_diceParser->getAllStringResult(ok); + if(ok) + { + QString patternColor("<span class=\"dice\">%1</span>"); + list = patternColor.arg(allStringlist.join(' ')); + value = list; + } + else + { + value = m_diceParser->getStringResult().replace("\n","<br/>"); + list = allStringlist.join(' '); + return true; + } + } + + return false; +}*/ +qint64 StringNode::getPriority() const +{ + qint64 priority=0; + if(NULL!=m_nextNode) + { + priority = m_nextNode->getPriority(); + } + + return priority; } diff --git a/node/stringnode.h b/node/stringnode.h index 6f01fe8..cf70d02 100644 --- a/node/stringnode.h +++ b/node/stringnode.h @@ -1,11 +1,25 @@ #ifndef STRINGNODE_H #define STRINGNODE_H +#include "node/executionnode.h" +#include "result/stringresult.h" -class StringNode +/** + * @brief The StringNode class is an ExecutionNode. It is dedicated to store string and display result. + */ +class StringNode : public ExecutionNode { public: StringNode(); + void run(ExecutionNode* previous); + void setString(QString str); + virtual QString toString(bool withLabel)const; + virtual qint64 getPriority() const; + +private: + QString m_data; + StringResult* m_stringResult; + }; -#endif // STRINGNODE_H
\ No newline at end of file +#endif // STRINGNODE_H diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 212c006..cc1a800 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -282,6 +282,7 @@ bool ParsingToolBox::readNumber(QString& str, qint64& myNumber) if(str.isEmpty()) return false; + QString number; int i=0; while(i<str.length() && ((str[i].isNumber()) || ( (i==0) && (str[i]=='-')))) @@ -306,6 +307,57 @@ bool ParsingToolBox::readNumber(QString& str, qint64& myNumber) return false; } +bool ParsingToolBox::readString(QString &str, QString& strResult) +{ + if(str.isEmpty()) + return false; + + + if(str.startsWith('"')) + { + str=str.remove(0,1); + } + + int i=0; + int j=0; + bool previousEscape=false; + QString result; + /*&& + (((!previousEscape) && !(str[i]=='"')) || (previousEscape) && !(str[i]=='"')) + || (str[i]=='\\'))*/ + while(i<str.length() && (!(!previousEscape && (str[i]=='"')) || (previousEscape && str[i]!='"'))) + { + if(str[i]=='\\') + { + previousEscape = true; + } + else + { + if(previousEscape && str[i]!='\"') + { + result += '\\'; + ++j; + } + result+=str[i]; + previousEscape = false; + } + ++i; + } + qDebug() << "previous" << previousEscape << str[i]; + + if(!result.isEmpty()) + { + str=str.remove(0,i); + strResult = result; + if(str.startsWith('"')) + { + str=str.remove(0,1); + return true; + } + } + return false; +} + bool ParsingToolBox::readVariable(QString &str, qint64 &myNumber) { if(str.isEmpty()) diff --git a/parsingtoolbox.h b/parsingtoolbox.h index e5f7fa8..7c5bc53 100644 --- a/parsingtoolbox.h +++ b/parsingtoolbox.h @@ -96,6 +96,13 @@ public: static bool readNumber(QString& str, qint64& myNumber); /** + * @brief readString + * @param str + * @param strResult + * @return + */ + static bool readString(QString& str, QString& strresult); + /** * @brief readVariable * @param str * @param myNumber |