diff options
| author | 2018-12-15 12:06:01 +0100 | |
|---|---|---|
| committer | 2018-12-15 12:06:01 +0100 | |
| commit | ebcaa489e6748a1e38b256d44f5b41309076c57b (patch) | |
| tree | 0de104e76383f7ba4a34d07c8babe353d57de9b9 /diceparser.cpp | |
| parent | 1575a8098416dbd3878505cd3eb81c4a3d43ce78 (diff) | |
| download | OneRoll-ebcaa489e6748a1e38b256d44f5b41309076c57b.tar.gz OneRoll-ebcaa489e6748a1e38b256d44f5b41309076c57b.zip | |
Fix indent.
Diffstat (limited to 'diceparser.cpp')
| -rw-r--r-- | diceparser.cpp | 2102 |
1 files changed, 1051 insertions, 1051 deletions
diff --git a/diceparser.cpp b/diceparser.cpp index 2beb414..ce092d3 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -1,4 +1,4 @@ - /*************************************************************************** +/*************************************************************************** * Copyright (C) 2014 by Renaud Guezennec * * http://www.rolisteam.org/contact * * * @@ -19,1396 +19,1396 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ - #include "diceparser.h" - #include <QDebug> - #include <QStringList> - #include <QObject> - #include <QFile> - #include <functional> - - #include "node/startingnode.h" - #include "node/scalaroperatornode.h" - #include "node/filternode.h" - #include "node/numbernode.h" - #include "node/keepdiceexecnode.h" - #include "node/sortresult.h" - #include "node/countexecutenode.h" - #include "node/rerolldicenode.h" - #include "node/explodedicenode.h" - #include "node/parenthesesnode.h" - #include "node/helpnode.h" - #include "node/jumpbackwardnode.h" - #include "node/listsetrollnode.h" - #include "node/listaliasnode.h" - #include "node/mergenode.h" - #include "node/ifnode.h" - #include "node/paintnode.h" - #include "node/stringnode.h" - #include "node/splitnode.h" - #include "node/groupnode.h" - #include "node/variablenode.h" - #include "node/bind.h" - - #define DEFAULT_FACES_NUMBER 10 - - DiceParser::DiceParser() - //: //m_start(nullptr),m_current(nullptr) +#include "diceparser.h" +#include <QDebug> +#include <QStringList> +#include <QObject> +#include <QFile> +#include <functional> + +#include "node/startingnode.h" +#include "node/scalaroperatornode.h" +#include "node/filternode.h" +#include "node/numbernode.h" +#include "node/keepdiceexecnode.h" +#include "node/sortresult.h" +#include "node/countexecutenode.h" +#include "node/rerolldicenode.h" +#include "node/explodedicenode.h" +#include "node/parenthesesnode.h" +#include "node/helpnode.h" +#include "node/jumpbackwardnode.h" +#include "node/listsetrollnode.h" +#include "node/listaliasnode.h" +#include "node/mergenode.h" +#include "node/ifnode.h" +#include "node/paintnode.h" +#include "node/stringnode.h" +#include "node/splitnode.h" +#include "node/groupnode.h" +#include "node/variablenode.h" +#include "node/bind.h" + +#define DEFAULT_FACES_NUMBER 10 + +DiceParser::DiceParser() +//: //m_start(nullptr),m_current(nullptr) +{ + m_currentTreeHasSeparator =false; + m_parsingToolbox = new ParsingToolBox(); + ParsingToolBox::setStartNodes(&m_startNodes); + + + m_mapDiceOp = new QMap<QString,DiceOperator>(); + m_mapDiceOp->insert(QStringLiteral("D"),D); + m_mapDiceOp->insert(QStringLiteral("L"),L); + + m_OptionOp = new QMap<QString,OptionOperator>(); + m_OptionOp->insert(QStringLiteral("k"),Keep); + m_OptionOp->insert(QStringLiteral("K"),KeepAndExplode); + m_OptionOp->insert(QStringLiteral("s"),Sort); + m_OptionOp->insert(QStringLiteral("c"),Count); + m_OptionOp->insert(QStringLiteral("r"),Reroll); + m_OptionOp->insert(QStringLiteral("e"),Explode); + m_OptionOp->insert(QStringLiteral("R"),RerollUntil); + m_OptionOp->insert(QStringLiteral("a"),RerollAndAdd); + m_OptionOp->insert(QStringLiteral("m"),Merge); + m_OptionOp->insert(QStringLiteral("i"),ifOperator); + m_OptionOp->insert(QStringLiteral("p"),Painter); + m_OptionOp->insert(QStringLiteral("f"),Filter); + m_OptionOp->insert(QStringLiteral("u"),Split); + m_OptionOp->insert(QStringLiteral("g"),Group); + m_OptionOp->insert(QStringLiteral("b"),Bind); + + m_aliasList = new QList<DiceAlias*>(); + + m_nodeActionMap = new QMap<QString,NodeAction>(); + m_nodeActionMap->insert(QStringLiteral("@"),JumpBackward); + + m_commandList = new QStringList(); + m_commandList->append(QStringLiteral("help")); + m_commandList->append(QStringLiteral("la")); + +} +DiceParser::~DiceParser() +{ + if(nullptr!=m_commandList) { - m_currentTreeHasSeparator =false; - m_parsingToolbox = new ParsingToolBox(); - ParsingToolBox::setStartNodes(&m_startNodes); - - - m_mapDiceOp = new QMap<QString,DiceOperator>(); - m_mapDiceOp->insert(QStringLiteral("D"),D); - m_mapDiceOp->insert(QStringLiteral("L"),L); - - m_OptionOp = new QMap<QString,OptionOperator>(); - m_OptionOp->insert(QStringLiteral("k"),Keep); - m_OptionOp->insert(QStringLiteral("K"),KeepAndExplode); - m_OptionOp->insert(QStringLiteral("s"),Sort); - m_OptionOp->insert(QStringLiteral("c"),Count); - m_OptionOp->insert(QStringLiteral("r"),Reroll); - m_OptionOp->insert(QStringLiteral("e"),Explode); - m_OptionOp->insert(QStringLiteral("R"),RerollUntil); - m_OptionOp->insert(QStringLiteral("a"),RerollAndAdd); - m_OptionOp->insert(QStringLiteral("m"),Merge); - m_OptionOp->insert(QStringLiteral("i"),ifOperator); - m_OptionOp->insert(QStringLiteral("p"),Painter); - m_OptionOp->insert(QStringLiteral("f"),Filter); - m_OptionOp->insert(QStringLiteral("u"),Split); - m_OptionOp->insert(QStringLiteral("g"),Group); - m_OptionOp->insert(QStringLiteral("b"),Bind); - - m_aliasList = new QList<DiceAlias*>(); - - m_nodeActionMap = new QMap<QString,NodeAction>(); - m_nodeActionMap->insert(QStringLiteral("@"),JumpBackward); - - m_commandList = new QStringList(); - m_commandList->append(QStringLiteral("help")); - m_commandList->append(QStringLiteral("la")); - + delete m_commandList; + m_commandList = nullptr; } - DiceParser::~DiceParser() + if(nullptr!=m_nodeActionMap) { - if(nullptr!=m_commandList) - { - delete m_commandList; - m_commandList = nullptr; - } - if(nullptr!=m_nodeActionMap) - { - delete m_nodeActionMap; - m_nodeActionMap = nullptr; - } - if(nullptr!=m_OptionOp) - { - delete m_OptionOp; - m_OptionOp = nullptr; - } - if(nullptr!=m_mapDiceOp) - { - delete m_mapDiceOp; - m_mapDiceOp = nullptr; - } - if(nullptr!=m_parsingToolbox) - { - delete m_parsingToolbox; - m_parsingToolbox = nullptr; - } - if(nullptr!=m_aliasList) - { - delete m_aliasList; - m_aliasList = nullptr; - } - if(nullptr!=m_start) - { - delete m_start; - m_start = nullptr; - } + delete m_nodeActionMap; + m_nodeActionMap = nullptr; } - - QString DiceParser::convertAlias(QString str) + if(nullptr!=m_OptionOp) { - for(DiceAlias* cmd : *m_aliasList) - { - if(cmd->isEnable()) - { - cmd->resolved(str); - } - } - return str; + delete m_OptionOp; + m_OptionOp = nullptr; } - QList<DiceAlias*>* DiceParser::getAliases() + if(nullptr!=m_mapDiceOp) { - return m_aliasList; + delete m_mapDiceOp; + m_mapDiceOp = nullptr; } - void DiceParser::insertAlias(DiceAlias* dice, int i) + if(nullptr!=m_parsingToolbox) { - if(i>=m_aliasList->size()) - { - m_aliasList->insert(i, dice); - } + delete m_parsingToolbox; + m_parsingToolbox = nullptr; + } + if(nullptr!=m_aliasList) + { + delete m_aliasList; + m_aliasList = nullptr; } + if(nullptr!=m_start) + { + delete m_start; + m_start = nullptr; + } +} - bool DiceParser::parseLine(QString str, bool allowAlias) +QString DiceParser::convertAlias(QString str) +{ + for(DiceAlias* cmd : *m_aliasList) { - m_errorMap.clear(); - m_comment = QStringLiteral(""); - if(!m_startNodes.empty()) - { - qDeleteAll(m_startNodes); - m_startNodes.clear(); - } - m_currentTreeHasSeparator=false; - if(allowAlias) + if(cmd->isEnable()) { - str = convertAlias(str); + cmd->resolved(str); } - m_command = str; - bool hasInstruction = readInstructionList(str); + } + return str; +} +QList<DiceAlias*>* DiceParser::getAliases() +{ + return m_aliasList; +} +void DiceParser::insertAlias(DiceAlias* dice, int i) +{ + if(i>=m_aliasList->size()) + { + m_aliasList->insert(i, dice); + } +} - bool value = hasInstruction; - if(!hasInstruction) - { - m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " - "<a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); - } - else if(hasInstruction && !str.isEmpty()) - { - auto i = m_command.size()-str.size(); - m_warningMap.insert(ExecutionNode::UNEXPECTED_CHARACTER,QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); - } +bool DiceParser::parseLine(QString str, bool allowAlias) +{ + m_errorMap.clear(); + m_comment = QStringLiteral(""); + if(!m_startNodes.empty()) + { + qDeleteAll(m_startNodes); + m_startNodes.clear(); + } + m_currentTreeHasSeparator=false; + if(allowAlias) + { + str = convertAlias(str); + } + m_command = str; + bool hasInstruction = readInstructionList(str); - return value; + bool value = hasInstruction; + if(!hasInstruction) + { + m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " + "<a href=\"https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md\">https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md</a>")); + } + else if(hasInstruction && !str.isEmpty()) + { + auto i = m_command.size()-str.size(); + m_warningMap.insert(ExecutionNode::UNEXPECTED_CHARACTER,QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); } - bool DiceParser::readExpression(QString& str,ExecutionNode* & node) + return value; +} + +bool DiceParser::readExpression(QString& str,ExecutionNode* & node) +{ + ExecutionNode* operandNode=nullptr; + if(m_parsingToolbox->readOpenParentheses(str)) { - ExecutionNode* operandNode=nullptr; - if(m_parsingToolbox->readOpenParentheses(str)) + ExecutionNode* internalNode=nullptr; + if(readExpression(str,internalNode)) { - ExecutionNode* internalNode=nullptr; - if(readExpression(str,internalNode)) + ParenthesesNode* parentheseNode = new ParenthesesNode(); + parentheseNode->setInternelNode(internalNode); + node = parentheseNode; + if(m_parsingToolbox->readCloseParentheses(str)) { - ParenthesesNode* parentheseNode = new ParenthesesNode(); - parentheseNode->setInternelNode(internalNode); - node = parentheseNode; - if(m_parsingToolbox->readCloseParentheses(str)) + ExecutionNode* diceNode=nullptr; + ExecutionNode* operatorNode=nullptr; + if(readDice(str, diceNode)) { - ExecutionNode* diceNode=nullptr; - ExecutionNode* operatorNode=nullptr; - if(readDice(str, diceNode)) - { - parentheseNode->setNextNode(diceNode); - } - else if(readExpression(str,operatorNode)) - { - parentheseNode->setNextNode(operatorNode); - } - return true; + parentheseNode->setNextNode(diceNode); } - else + else if(readExpression(str,operatorNode)) { - m_warningMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Expected closing parenthesis - can't validate the inside.")); + parentheseNode->setNextNode(operatorNode); } + return true; } - } - else if(m_parsingToolbox->readOperand(str,operandNode)) - { - ExecutionNode* diceNode=nullptr; - if(readDice(str,diceNode)) + else { - operandNode->setNextNode(diceNode); + m_warningMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Expected closing parenthesis - can't validate the inside.")); } - node = operandNode; - - operandNode= ParsingToolBox::getLatestNode(operandNode); - //ExecutionNode* operatorNode=nullptr; - while(readOperator(str,operandNode)) - { - //operandNode->setNextNode(operatorNode); - operandNode= ParsingToolBox::getLatestNode(operandNode); - }; - return true; - } - else if(readCommand(str,operandNode)) - { - node = operandNode; - return true; } - else if(readNode(str,operandNode)) + } + else if(m_parsingToolbox->readOperand(str,operandNode)) + { + ExecutionNode* diceNode=nullptr; + if(readDice(str,diceNode)) { - node = operandNode; - return true; + operandNode->setNextNode(diceNode); } - else if(readOptionFromNull(str,operandNode)) + node = operandNode; + + operandNode= ParsingToolBox::getLatestNode(operandNode); + //ExecutionNode* operatorNode=nullptr; + while(readOperator(str,operandNode)) { - node = operandNode; - return true; - } - else if(readOperatorFromNull(str,operandNode)) + //operandNode->setNextNode(operatorNode); + operandNode= ParsingToolBox::getLatestNode(operandNode); + }; + return true; + } + else if(readCommand(str,operandNode)) + { + node = operandNode; + return true; + } + else if(readNode(str,operandNode)) + { + node = operandNode; + return true; + } + else if(readOptionFromNull(str,operandNode)) + { + node = operandNode; + return true; + } + else if(readOperatorFromNull(str,operandNode)) + { + node = operandNode; + return true; + } + else + { + ExecutionNode* diceNode=nullptr; + if(readDice(str,diceNode)) { - node = operandNode; + NumberNode* numberNode=new NumberNode(); + numberNode->setNumber(1); + numberNode->setNextNode(diceNode); + node = numberNode; return true; } else { - ExecutionNode* diceNode=nullptr; - if(readDice(str,diceNode)) - { - NumberNode* numberNode=new NumberNode(); - numberNode->setNumber(1); - numberNode->setNextNode(diceNode); - node = numberNode; - return true; - } - else - { - return false; - } + return false; } - return false; } + return false; +} - bool DiceParser::readOptionFromNull(QString& str,ExecutionNode* & node) +bool DiceParser::readOptionFromNull(QString& str,ExecutionNode* & node) +{ + StartingNode nodePrevious; + if(readOption(str,&nodePrevious)) { - StartingNode nodePrevious; - if(readOption(str,&nodePrevious)) - { - auto nodeNext = nodePrevious.getNextNode(); - nodePrevious.setNextNode(nullptr); - node = nodeNext; - return true; - } - return false; + auto nodeNext = nodePrevious.getNextNode(); + nodePrevious.setNextNode(nullptr); + node = nodeNext; + return true; } + return false; +} - bool DiceParser::readOperatorFromNull(QString& str,ExecutionNode* & node) +bool DiceParser::readOperatorFromNull(QString& str,ExecutionNode* & node) +{ + StartingNode nodePrevious; + if(readOperator(str,&nodePrevious)) { - StartingNode nodePrevious; - if(readOperator(str,&nodePrevious)) - { - auto nodeNext = nodePrevious.getNextNode(); - nodePrevious.setNextNode(nullptr); - node = nodeNext; - return true; - } - return false; + auto nodeNext = nodePrevious.getNextNode(); + nodePrevious.setNextNode(nullptr); + node = nodeNext; + return true; } + return false; +} - bool DiceParser::readNode(QString& str,ExecutionNode* & node) +bool DiceParser::readNode(QString& str,ExecutionNode* & node) +{ + QString key= str.left(1); + if(m_nodeActionMap->contains(key)) { - QString key= str.left(1); - if(m_nodeActionMap->contains(key)) - { - JumpBackwardNode* jumpNode = new JumpBackwardNode(); - node = jumpNode; - str=str.remove(0,1); - readOption(str,jumpNode); - return true; - } - return false; + JumpBackwardNode* jumpNode = new JumpBackwardNode(); + node = jumpNode; + str=str.remove(0,1); + readOption(str,jumpNode); + return true; } + return false; +} - void DiceParser::start() +void DiceParser::start() +{ + for(auto start : m_startNodes) { - for(auto start : m_startNodes) - { - start->run(); - } + start->run(); } +} - QString DiceParser::displayResult() +QString DiceParser::displayResult() +{ + QStringList resultList; + for(auto start : m_startNodes) { - QStringList resultList; - for(auto start : m_startNodes) + ExecutionNode* next = start; + int nodeCount=0; + int resulCount=0; + while(nullptr != next->getNextNode() ) { - ExecutionNode* next = start; - int nodeCount=0; - int resulCount=0; - while(nullptr != next->getNextNode() ) - { - next = next->getNextNode(); - ++nodeCount; - } - ////////////////////////////////// - // - // Display - // - ////////////////////////////////// - - QString str; - QTextStream stream(&str); - Result* result=next->getResult(); + next = next->getNextNode(); + ++nodeCount; + } + ////////////////////////////////// + // + // Display + // + ////////////////////////////////// + + QString str; + QTextStream stream(&str); + Result* result=next->getResult(); - QString totalValue("you got %1 ;"); - QString dieValue("D%1 : {%2} "); + QString totalValue("you got %1 ;"); + QString dieValue("D%1 : {%2} "); - bool scalarDone=false; - while(nullptr!=result) + bool scalarDone=false; + while(nullptr!=result) + { + ++resulCount; + if((result->hasResultOfType(Result::SCALAR))&&(!scalarDone)) { - ++resulCount; - if((result->hasResultOfType(Result::SCALAR))&&(!scalarDone)) - { - stream << totalValue.arg(result->getResult(Result::SCALAR).toReal()) << endl; //.arg(m_command) - scalarDone=true; - } - else if(result->hasResultOfType(Result::DICE_LIST)) + stream << totalValue.arg(result->getResult(Result::SCALAR).toReal()) << endl; //.arg(m_command) + scalarDone=true; + } + else if(result->hasResultOfType(Result::DICE_LIST)) + { + + DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=myDiceResult) { - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=myDiceResult) + QString resulStr; + quint64 face=0; + for(Die* die : myDiceResult->getResultList()) { - - QString resulStr; - quint64 face=0; - for(Die* die : myDiceResult->getResultList()) + if(!die->hasBeenDisplayed()) { - if(!die->hasBeenDisplayed()) - { - resulStr+=QStringLiteral("%1").arg(die->getValue()); - die->displayed(); - face = die->getFaces(); + resulStr+=QStringLiteral("%1").arg(die->getValue()); + die->displayed(); + face = die->getFaces(); - if(die->hasChildrenValue()) + if(die->hasChildrenValue()) + { + resulStr+=QStringLiteral(" ["); + for(qint64 i : die->getListValue()) { - resulStr+=QStringLiteral(" ["); - for(qint64 i : die->getListValue()) - { - resulStr+=QStringLiteral("%1 ").arg(i); - } - resulStr.remove(resulStr.size()-1,1); - resulStr+=QStringLiteral("]"); + resulStr+=QStringLiteral("%1 ").arg(i); } - resulStr+=QStringLiteral(", "); + resulStr.remove(resulStr.size()-1,1); + resulStr+=QStringLiteral("]"); } + resulStr+=QStringLiteral(", "); } - resulStr.remove(resulStr.size()-2,2); - - if(!resulStr.isEmpty()) - { - stream << dieValue.arg(face).arg(resulStr); - } + } + resulStr.remove(resulStr.size()-2,2); + if(!resulStr.isEmpty()) + { + stream << dieValue.arg(face).arg(resulStr); } - } - else if(result->hasResultOfType(Result::STRING)) - { - stream << result->getResult(Result::STRING).toString(); - } - result = result->getPrevious(); + } + } + else if(result->hasResultOfType(Result::STRING)) + { + stream << result->getResult(Result::STRING).toString(); } - QTextStream out(stdout); - out << str << "you rolled: " <<m_command << endl; - out << endl; + result = result->getPrevious(); + } + QTextStream out(stdout); + out << str << "you rolled: " <<m_command << endl; + out << endl; - resultList << QStringLiteral("%1, you rolled:%3").arg(str.simplified()).arg(m_command) ; - } - return resultList.join('\n'); + resultList << QStringLiteral("%1, you rolled:%3").arg(str.simplified()).arg(m_command) ; } - QList<qreal> DiceParser::getLastIntegerResults() + + return resultList.join('\n'); +} +QList<qreal> DiceParser::getLastIntegerResults() +{ + QList<qreal> resultValues; + for(auto node : m_startNodes) { - QList<qreal> resultValues; - for(auto node : m_startNodes) + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + bool scalarDone = false; + while((result!=nullptr)&&(!scalarDone)) { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - bool scalarDone = false; - while((result!=nullptr)&&(!scalarDone)) + if(result->hasResultOfType(Result::SCALAR)) { - if(result->hasResultOfType(Result::SCALAR)) - { - resultValues << result->getResult(Result::SCALAR).toReal(); - scalarDone=true; - } - result=result->getPrevious(); + resultValues << result->getResult(Result::SCALAR).toReal(); + scalarDone=true; } + result=result->getPrevious(); } - return resultValues; } - QStringList DiceParser::getStringResult( ) + return resultValues; +} +QStringList DiceParser::getStringResult( ) +{ + QStringList stringListResult; + for(auto node : m_startNodes) { - QStringList stringListResult; - for(auto node : m_startNodes) + ExecutionNode* next = getLeafNode(node); + QString str; + Result* result=next->getResult(); + bool found = false; + while((nullptr!=result) && (!found) ) { - ExecutionNode* next = getLeafNode(node); - QString str; - Result* result=next->getResult(); - bool found = false; - while((nullptr!=result) && (!found) ) + if(result->hasResultOfType(Result::STRING)) { - if(result->hasResultOfType(Result::STRING)) - { - str = result->getResult(Result::STRING).toString(); - found = true; - } - result = result->getPrevious(); + str = result->getResult(Result::STRING).toString(); + found = true; } - stringListResult << str; + result = result->getPrevious(); } - return stringListResult; + stringListResult << str; } - QStringList DiceParser::getAllStringResult(bool& hasAlias) + return stringListResult; +} +QStringList DiceParser::getAllStringResult(bool& hasAlias) +{ + //QStringList allResult; + QStringList stringListResult; + for(auto node : m_startNodes) { - //QStringList allResult; - QStringList stringListResult; - for(auto node : m_startNodes) - { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); - while(nullptr!=result) + while(nullptr!=result) + { + if(result->hasResultOfType(Result::STRING)) { - if(result->hasResultOfType(Result::STRING)) + StringResult* stringResult = dynamic_cast<StringResult*>(result); + if(nullptr!=stringResult) { - StringResult* stringResult = dynamic_cast<StringResult*>(result); - if(nullptr!=stringResult) - { - stringListResult << stringResult->getText(); - hasAlias = stringResult->hasHighLight(); - } + stringListResult << stringResult->getText(); + hasAlias = stringResult->hasHighLight(); } - result = result->getPrevious(); } + result = result->getPrevious(); } - return stringListResult; } - QStringList DiceParser::getAllDiceResult(bool& hasAlias) + return stringListResult; +} +QStringList DiceParser::getAllDiceResult(bool& hasAlias) +{ + QStringList stringListResult; + for(auto node : m_startNodes) { - QStringList stringListResult; - for(auto node : m_startNodes) - { - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - QList<Die*> dieListResult; + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + QList<Die*> dieListResult; - while(nullptr!=result) + while(nullptr!=result) + { + if(result->hasResultOfType(Result::DICE_LIST)) { - if(result->hasResultOfType(Result::DICE_LIST)) + DiceResult* stringResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=stringResult) { - DiceResult* stringResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=stringResult) + for(auto die : stringResult->getResultList()) { - for(auto die : stringResult->getResultList()) + if(!dieListResult.contains(die)) { - if(!dieListResult.contains(die)) - { - dieListResult.removeAll(die); - dieListResult << die; - } + dieListResult.removeAll(die); + dieListResult << die; } - hasAlias = true; } + hasAlias = true; } - result = result->getPrevious(); } - for(Die* die : dieListResult) + result = result->getPrevious(); + } + for(Die* die : dieListResult) + { + if(die->isHighlighted()) { - if(die->isHighlighted()) + for(qint64 value : die->getListValue()) { - for(qint64 value : die->getListValue()) - { - stringListResult << QString::number(value); - } + stringListResult << QString::number(value); } } } - - return stringListResult; } - void DiceParser::getDiceResultFromAllInstruction(QList<ExportedDiceResult>& resultList) + return stringListResult; +} + +void DiceParser::getDiceResultFromAllInstruction(QList<ExportedDiceResult>& resultList) +{ + for(auto start : m_startNodes) { - for(auto start : m_startNodes) + ExecutionNode* next = getLeafNode(start); + Result* result=next->getResult(); + //qDebug() << "result id" << result->toString(true); + ExportedDiceResult nodeResult; + while(nullptr!=result) { - ExecutionNode* next = getLeafNode(start); - Result* result=next->getResult(); - //qDebug() << "result id" << result->toString(true); - ExportedDiceResult nodeResult; - while(nullptr!=result) + if(result->hasResultOfType(Result::DICE_LIST)) { - if(result->hasResultOfType(Result::DICE_LIST)) - { - DiceResult* diceResult = dynamic_cast<DiceResult*>(result); - QList<HighLightDice> list; - quint64 faces = 0; + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + QList<HighLightDice> list; + quint64 faces = 0; - for(Die* die : diceResult->getResultList()) - { - faces = die->getFaces(); - // qDebug() << "face" << faces; - HighLightDice hlDice(die->getListValue(),die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),die->getFaces()); - list.append(hlDice); - } - nodeResult.insert(static_cast<int>(faces),list); + for(Die* die : diceResult->getResultList()) + { + faces = die->getFaces(); + // qDebug() << "face" << faces; + HighLightDice hlDice(die->getListValue(),die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),die->getFaces()); + list.append(hlDice); } - result = result->getPrevious(); + nodeResult.insert(static_cast<int>(faces),list); } - resultList.append(nodeResult); + result = result->getPrevious(); } - //qDebug() << resultList.size(); + resultList.append(nodeResult); } + //qDebug() << resultList.size(); +} - void DiceParser::getLastDiceResult(QList<ExportedDiceResult>& diceValuesList,bool& homogeneous) +void DiceParser::getLastDiceResult(QList<ExportedDiceResult>& diceValuesList,bool& homogeneous) +{ + for(auto start : m_startNodes) { - for(auto start : m_startNodes) + ExportedDiceResult diceValues; + ExecutionNode* next = getLeafNode(start); + Result* result=next->getResult(); + while(nullptr!=result) { - ExportedDiceResult diceValues; - ExecutionNode* next = getLeafNode(start); - Result* result=next->getResult(); - while(nullptr!=result) + if(result->hasResultOfType(Result::DICE_LIST)) { - if(result->hasResultOfType(Result::DICE_LIST)) + DiceResult* diceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=diceResult) { - DiceResult* diceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=diceResult) + if(homogeneous) { - if(homogeneous) - { - homogeneous = diceResult->isHomogeneous(); - } - quint64 face=0; - ListDiceResult listpair; - for(Die* die : diceResult->getResultList()) + homogeneous = diceResult->isHomogeneous(); + } + quint64 face=0; + ListDiceResult listpair; + for(Die* die : diceResult->getResultList()) + { + if(!die->hasBeenDisplayed()) { - if(!die->hasBeenDisplayed()) + QList<qint64> valuesResult; + valuesResult.append(die->getValue()); + die->displayed(); + face = die->getFaces(); + if(die->hasChildrenValue()) { - QList<qint64> valuesResult; - valuesResult.append(die->getValue()); - die->displayed(); - face = die->getFaces(); - if(die->hasChildrenValue()) + for(qint64 i : die->getListValue()) { - for(qint64 i : die->getListValue()) - { - valuesResult.append(i); - } + valuesResult.append(i); } - HighLightDice hlDice(valuesResult,die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),0); - listpair.append(hlDice); } + HighLightDice hlDice(valuesResult,die->isHighlighted(),die->getColor(), die->hasBeenDisplayed(),0); + listpair.append(hlDice); } - if(!listpair.isEmpty()) + } + if(!listpair.isEmpty()) + { + if(!diceValues.contains(face)) { - if(!diceValues.contains(face)) - { - diceValues.insert(face,listpair); - } - else - { - ListDiceResult tmp = diceValues.value(face); - tmp.append(listpair); - diceValues.insert(face,tmp); - } + diceValues.insert(face,listpair); + } + else + { + ListDiceResult tmp = diceValues.value(face); + tmp.append(listpair); + diceValues.insert(face,tmp); } } } - result = result->getPrevious(); - } - if(!diceValues.isEmpty()) - { - diceValuesList.append(diceValues); } + result = result->getPrevious(); + } + if(!diceValues.isEmpty()) + { + diceValuesList.append(diceValues); } } - QString DiceParser::getDiceCommand() const +} +QString DiceParser::getDiceCommand() const +{ + return m_command; +} + +bool DiceParser::hasIntegerResultNotInFirst() +{ + bool result= false; + for(auto node : m_startNodes) { - return m_command; + result |= hasResultOfType(Result::SCALAR,node); } + return result; +} - bool DiceParser::hasIntegerResultNotInFirst() +bool DiceParser::hasDiceResult() +{ + bool result= false; + for(auto node : m_startNodes) { - bool result= false; - for(auto node : m_startNodes) - { - result |= hasResultOfType(Result::SCALAR,node); - } - return result; + result |= hasResultOfType(Result::DICE_LIST,node); } - - bool DiceParser::hasDiceResult() + return result; +} +bool DiceParser::hasStringResult() +{ + bool result = false; + for(auto node : m_startNodes) { - bool result= false; - for(auto node : m_startNodes) - { - result |= hasResultOfType(Result::DICE_LIST,node); - } - return result; + result |= hasResultOfType(Result::STRING,node); } - bool DiceParser::hasStringResult() + return result; +} +bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) +{ + bool scalarDone = false; + ExecutionNode* next = getLeafNode(node); + Result* result=next->getResult(); + while((result!=nullptr)&&(!scalarDone)) { - bool result = false; - for(auto node : m_startNodes) + if(result->hasResultOfType(type) && ((!notthelast)||(notthelast && (nullptr!=result->getPrevious())))) { - result |= hasResultOfType(Result::STRING,node); + scalarDone=true; } - return result; + result=result->getPrevious(); } - bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) + return scalarDone; +} +QList<qreal> DiceParser::getSumOfDiceResult() +{ + QList<qreal> resultValues; + for(auto node : m_startNodes) { - bool scalarDone = false; + qreal resultValue=0; ExecutionNode* next = getLeafNode(node); Result* result=next->getResult(); - while((result!=nullptr)&&(!scalarDone)) + bool found = false; + while((nullptr!=result)&&(!found)) { - if(result->hasResultOfType(type) && ((!notthelast)||(notthelast && (nullptr!=result->getPrevious())))) + if(result->hasResultOfType(Result::DICE_LIST)) { - scalarDone=true; - } - result=result->getPrevious(); - } - return scalarDone; - } - QList<qreal> DiceParser::getSumOfDiceResult() - { - QList<qreal> resultValues; - for(auto node : m_startNodes) - { - qreal resultValue=0; - ExecutionNode* next = getLeafNode(node); - Result* result=next->getResult(); - bool found = false; - while((nullptr!=result)&&(!found)) - { - if(result->hasResultOfType(Result::DICE_LIST)) + DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); + if(nullptr!=myDiceResult) { - DiceResult* myDiceResult = dynamic_cast<DiceResult*>(result); - if(nullptr!=myDiceResult) + for(Die* die : myDiceResult->getResultList()) { - for(Die* die : myDiceResult->getResultList()) - { - resultValue+=die->getValue(); - } - found = true; + resultValue+=die->getValue(); } + found = true; } - result = result->getPrevious(); } - resultValues << resultValue; + result = result->getPrevious(); } - return resultValues; + resultValues << resultValue; } - int DiceParser::getStartNodeCount() const + return resultValues; +} +int DiceParser::getStartNodeCount() const +{ + return m_startNodes.size(); +} +ExecutionNode* DiceParser::getLeafNode(ExecutionNode* start) +{ + ExecutionNode* next = start; + while(nullptr != next->getNextNode() ) { - return m_startNodes.size(); - } - ExecutionNode* DiceParser::getLeafNode(ExecutionNode* start) - { - ExecutionNode* next = start; - while(nullptr != next->getNextNode() ) - { - next = next->getNextNode(); - } - return next; + next = next->getNextNode(); } + return next; +} - bool DiceParser::readDice(QString& str,ExecutionNode* & node) - { - DiceOperator currentOperator; +bool DiceParser::readDice(QString& str,ExecutionNode* & node) +{ + DiceOperator currentOperator; - if(readDiceOperator(str,currentOperator)) + if(readDiceOperator(str,currentOperator)) + { + if(currentOperator==D) { - if(currentOperator==D) + qint64 max; + qint64 min; + bool unique = (ParsingToolBox::UNIQUE == m_parsingToolbox->readListOperator(str)) ? + true : false; + Die::ArithmeticOperator op; + + bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op); + if(m_parsingToolbox->readNumber(str,max)) { - qint64 max; - qint64 min; - bool unique = (ParsingToolBox::UNIQUE == m_parsingToolbox->readListOperator(str)) ? - true : false; - Die::ArithmeticOperator op; - - bool hasOp= m_parsingToolbox->readArithmeticOperator(str,op); - if(m_parsingToolbox->readNumber(str,max)) + if(max<1) { - if(max<1) - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); - return false; - } - DiceRollerNode* drNode = new DiceRollerNode(max); - drNode->setUnique(unique); - if(hasOp) - { - drNode->setOperator(op); - } - node = drNode; - ExecutionNode* current = drNode; - while(readOption(str,current)) - { - current = ParsingToolBox::getLatestNode(current); - } - return true; + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); + return false; } - else if(m_parsingToolbox->readDiceRange(str,min,max)) + DiceRollerNode* drNode = new DiceRollerNode(max); + drNode->setUnique(unique); + if(hasOp) { - DiceRollerNode* drNode = new DiceRollerNode(max,min); - drNode->setUnique(unique); - if(hasOp) - { - drNode->setOperator(op); - } - node = drNode; - ExecutionNode* current = drNode; - while(readOption(str,current)) - { - current = ParsingToolBox::getLatestNode(current); - } - return true; + drNode->setOperator(op); + } + node = drNode; + ExecutionNode* current = drNode; + while(readOption(str,current)) + { + current = ParsingToolBox::getLatestNode(current); } + return true; } - else if(currentOperator ==L) + else if(m_parsingToolbox->readDiceRange(str,min,max)) { - QStringList list; - QList<Range> listRange; - ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); - if(m_parsingToolbox->readList(str,list,listRange)) + DiceRollerNode* drNode = new DiceRollerNode(max,min); + drNode->setUnique(unique); + if(hasOp) { - ListSetRollNode* lsrNode = new ListSetRollNode(); - lsrNode->setRangeList(listRange); - if(op == ParsingToolBox::UNIQUE) - { - lsrNode->setUnique(true); - } - lsrNode->setListValue(list); - node = lsrNode; - return true; + drNode->setOperator(op); } - else + node = drNode; + ExecutionNode* current = drNode; + while(readOption(str,current)) { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); + current = ParsingToolBox::getLatestNode(current); } - } - - } - - return false; - - } - bool DiceParser::readDiceOperator(QString& str,DiceOperator& op) - { - QStringList listKey = m_mapDiceOp->keys(); - for(const QString& key : listKey) - { - if(str.startsWith(key,Qt::CaseInsensitive)) - { - str=str.remove(0,key.size()); - op = m_mapDiceOp->value(key); return true; } } - return false; - } - bool DiceParser::readCommand(QString& str,ExecutionNode* & node) - { - if(m_commandList->contains(str)) + else if(currentOperator ==L) { - if(str== QLatin1String("help")) + QStringList list; + QList<Range> listRange; + ParsingToolBox::LIST_OPERATOR op = m_parsingToolbox->readListOperator(str); + if(m_parsingToolbox->readList(str,list,listRange)) { - HelpNode* help = new HelpNode(); - if(!m_helpPath.isEmpty()) + ListSetRollNode* lsrNode = new ListSetRollNode(); + lsrNode->setRangeList(listRange); + if(op == ParsingToolBox::UNIQUE) { - help->setHelpPath(m_helpPath); + lsrNode->setUnique(true); } - node = help; - + lsrNode->setListValue(list); + node = lsrNode; + return true; } - else if(str== QLatin1String("la")) + else { - node = new ListAliasNode(m_aliasList); + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); } - return true; } - return false; + } - bool DiceParser::readDiceExpression(QString& str,ExecutionNode* & node) - { - bool returnVal=false; + return false; - ExecutionNode* next = nullptr; - if(readDice(str,next)) +} +bool DiceParser::readDiceOperator(QString& str,DiceOperator& op) +{ + QStringList listKey = m_mapDiceOp->keys(); + for(const QString& key : listKey) + { + if(str.startsWith(key,Qt::CaseInsensitive)) + { + str=str.remove(0,key.size()); + op = m_mapDiceOp->value(key); + return true; + } + } + return false; +} +bool DiceParser::readCommand(QString& str,ExecutionNode* & node) +{ + if(m_commandList->contains(str)) + { + if(str== QLatin1String("help")) { - ExecutionNode* latest = next; - while(readOption(str,latest)) + HelpNode* help = new HelpNode(); + if(!m_helpPath.isEmpty()) { - while(nullptr!=latest->getNextNode()) - { - latest = latest->getNextNode(); - } + help->setHelpPath(m_helpPath); } + node = help; - node = next; - returnVal = true; } - else + else if(str== QLatin1String("la")) { - returnVal = false; + node = new ListAliasNode(m_aliasList); } - return returnVal; + return true; } - bool DiceParser::readInstructionOperator(QChar c) + return false; +} + +bool DiceParser::readDiceExpression(QString& str,ExecutionNode* & node) +{ + bool returnVal=false; + + ExecutionNode* next = nullptr; + if(readDice(str,next)) { - if(c == ';') + ExecutionNode* latest = next; + while(readOption(str,latest)) { - return true; + while(nullptr!=latest->getNextNode()) + { + latest = latest->getNextNode(); + } } - return false; + + node = next; + returnVal = true; } - bool DiceParser::readInstructionList(QString& str) + else { - if(str.isEmpty()) - return false; + returnVal = false; + } + return returnVal; +} +bool DiceParser::readInstructionOperator(QChar c) +{ + if(c == ';') + { + return true; + } + return false; +} +bool DiceParser::readInstructionList(QString& str) +{ + if(str.isEmpty()) + return false; - bool hasInstruction = false; - bool readInstruction = true; - while(readInstruction) + bool hasInstruction = false; + bool readInstruction = true; + while(readInstruction) + { + ExecutionNode* startNode = nullptr; + bool keepParsing = readExpression(str,startNode); + if(nullptr != startNode) { - ExecutionNode* startNode = nullptr; - bool keepParsing = readExpression(str,startNode); - if(nullptr != startNode) + hasInstruction = true; + m_startNodes.push_back(startNode); + auto latest = startNode; + if(keepParsing) { - hasInstruction = true; - m_startNodes.push_back(startNode); - auto latest = startNode; - if(keepParsing) + latest = ParsingToolBox::getLatestNode(latest); + keepParsing =!str.isEmpty(); + while(keepParsing) { - latest = ParsingToolBox::getLatestNode(latest); - keepParsing =!str.isEmpty(); - while(keepParsing) + auto before = str; + if(readOperator(str,latest)) { - auto before = str; - if(readOperator(str,latest)) - { - latest = ParsingToolBox::getLatestNode(latest); - } - keepParsing = (!str.isEmpty() & (before!=str)); + latest = ParsingToolBox::getLatestNode(latest); } + keepParsing = (!str.isEmpty() & (before!=str)); } - if( !str.isEmpty() && readInstructionOperator(str[0])) - { - str=str.remove(0,1); - } - else - { - QString result; - QString comment; - if(m_parsingToolbox->readComment(str,result,comment)) - { - m_command.remove(comment); - m_comment = result; - } - readInstruction = false; - } + } + if( !str.isEmpty() && readInstructionOperator(str[0])) + { + str=str.remove(0,1); } else { + QString result; + QString comment; + if(m_parsingToolbox->readComment(str,result,comment)) + { + m_command.remove(comment); + m_comment = result; + } readInstruction = false; } } - return hasInstruction; + else + { + readInstruction = false; + } } + return hasInstruction; +} - bool DiceParser::readOperator(QString& str,ExecutionNode* previous) +bool DiceParser::readOperator(QString& str,ExecutionNode* previous) +{ + if(str.isEmpty() || nullptr == previous) { - if(str.isEmpty() || nullptr == previous) - { - return false; - } + return false; + } - Die::ArithmeticOperator op; - if(m_parsingToolbox->readArithmeticOperator(str,op)) + Die::ArithmeticOperator op; + if(m_parsingToolbox->readArithmeticOperator(str,op)) + { + ScalarOperatorNode* node = new ScalarOperatorNode(); + node->setArithmeticOperator(op); + ExecutionNode* nodeExec = nullptr; + if(readExpression(str,nodeExec)) { - ScalarOperatorNode* node = new ScalarOperatorNode(); - node->setArithmeticOperator(op); - ExecutionNode* nodeExec = nullptr; - if(readExpression(str,nodeExec)) + node->setInternalNode(nodeExec); + if(nullptr==nodeExec) { - node->setInternalNode(nodeExec); - if(nullptr==nodeExec) - { - delete node; - return false; - } - ExecutionNode* nodeExecOrChild = nodeExec; - ExecutionNode* parent = nullptr; + delete node; + return false; + } + ExecutionNode* nodeExecOrChild = nodeExec; + ExecutionNode* parent = nullptr; - while((nullptr!=nodeExecOrChild) && (node->getPriority()<nodeExecOrChild->getPriority())) - { - parent = nodeExecOrChild; - nodeExecOrChild = nodeExecOrChild->getNextNode(); - } - // management of operator priority - if((nullptr != nodeExecOrChild)&&(nodeExec != nodeExecOrChild)) - { - // good 1 1 2 ; bad 1 0 4 - if(nodeExecOrChild->getPriority()>=node->getPriority()) - { - node->setNextNode(nodeExecOrChild); - parent->setNextNode(nullptr); - } - } - else if(node->getPriority()>=nodeExec->getPriority()) + while((nullptr!=nodeExecOrChild) && (node->getPriority()<nodeExecOrChild->getPriority())) + { + parent = nodeExecOrChild; + nodeExecOrChild = nodeExecOrChild->getNextNode(); + } + // management of operator priority + if((nullptr != nodeExecOrChild)&&(nodeExec != nodeExecOrChild)) + { + // good 1 1 2 ; bad 1 0 4 + if(nodeExecOrChild->getPriority()>=node->getPriority()) { - node->setNextNode(nodeExec->getNextNode()); - nodeExec->setNextNode(nullptr); + node->setNextNode(nodeExecOrChild); + parent->setNextNode(nullptr); } - - //nodeResult = node; - previous->setNextNode(node); - - return true; } - else + else if(node->getPriority()>=nodeExec->getPriority()) { - delete node; + node->setNextNode(nodeExec->getNextNode()); + nodeExec->setNextNode(nullptr); } + + //nodeResult = node; + previous->setNextNode(node); + + return true; } else { - while(readOption(str,previous)) - { - previous = ParsingToolBox::getLatestNode(previous); - } + delete node; } - return false; } - bool DiceParser::hasSeparator()const + else { - return m_currentTreeHasSeparator; - } - DiceRollerNode* DiceParser::addRollDiceNode(qint64 faces,ExecutionNode* previous) - { - DiceRollerNode* mydiceRoller= new DiceRollerNode(faces); - previous->setNextNode(mydiceRoller); - return mydiceRoller; + while(readOption(str,previous)) + { + previous = ParsingToolBox::getLatestNode(previous); + } } - ExplodeDiceNode* DiceParser::addExplodeDiceNode(qint64 value,ExecutionNode* previous) + return false; +} +bool DiceParser::hasSeparator()const +{ + return m_currentTreeHasSeparator; +} +DiceRollerNode* DiceParser::addRollDiceNode(qint64 faces,ExecutionNode* previous) +{ + DiceRollerNode* mydiceRoller= new DiceRollerNode(faces); + previous->setNextNode(mydiceRoller); + return mydiceRoller; +} +ExplodeDiceNode* DiceParser::addExplodeDiceNode(qint64 value,ExecutionNode* previous) +{ + ExplodeDiceNode* explodeDiceNode= new ExplodeDiceNode(); + NumberNode* node = new NumberNode(); + node->setNumber(value); + BooleanCondition* condition = new BooleanCondition(); + condition->setValueNode(node); + condition->setOperator(BooleanCondition::Equal); + m_parsingToolbox->isValidValidator(previous,condition); + explodeDiceNode->setValidator(condition); + previous->setNextNode(explodeDiceNode); + return explodeDiceNode; +} +bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, +{ + if(str.isEmpty()) { - ExplodeDiceNode* explodeDiceNode= new ExplodeDiceNode(); - NumberNode* node = new NumberNode(); - node->setNumber(value); - BooleanCondition* condition = new BooleanCondition(); - condition->setValueNode(node); - condition->setOperator(BooleanCondition::Equal); - m_parsingToolbox->isValidValidator(previous,condition); - explodeDiceNode->setValidator(condition); - previous->setNextNode(explodeDiceNode); - return explodeDiceNode; + return false; } - bool DiceParser::readOption(QString& str,ExecutionNode* previous)//, - { - if(str.isEmpty()) - { - return false; - } - ExecutionNode* node = nullptr; - bool found=false; + ExecutionNode* node = nullptr; + bool found=false; - for(int i = 0; ((i<m_OptionOp->keys().size())&&(!found));++i ) + for(int i = 0; ((i<m_OptionOp->keys().size())&&(!found));++i ) + { + QString key =m_OptionOp->keys().at(i); + + if(str.startsWith(key)) { - QString key =m_OptionOp->keys().at(i); - if(str.startsWith(key)) + str=str.remove(0,key.size()); + auto operatorName = m_OptionOp->value(key); + switch(operatorName) + { + case Keep: { + qint64 myNumber=0; + bool ascending = m_parsingToolbox->readAscending(str); - str=str.remove(0,key.size()); - auto operatorName = m_OptionOp->value(key); - switch(operatorName) + if(m_parsingToolbox->readNumber(str,myNumber)) { - case Keep: - { - qint64 myNumber=0; - bool ascending = m_parsingToolbox->readAscending(str); - - if(m_parsingToolbox->readNumber(str,myNumber)) - { - node = m_parsingToolbox->addSort(previous,ascending); - KeepDiceExecNode* nodeK = new KeepDiceExecNode(); - nodeK->setDiceKeepNumber(myNumber); - node->setNextNode(nodeK); - node = nodeK; - found = true; - } + node = m_parsingToolbox->addSort(previous,ascending); + KeepDiceExecNode* nodeK = new KeepDiceExecNode(); + nodeK->setDiceKeepNumber(myNumber); + node->setNextNode(nodeK); + node = nodeK; + found = true; } - break; - case KeepAndExplode: + } + break; + case KeepAndExplode: + { + qint64 myNumber=0; + bool ascending = m_parsingToolbox->readAscending(str); + if(m_parsingToolbox->readNumber(str,myNumber)) { - qint64 myNumber=0; - bool ascending = m_parsingToolbox->readAscending(str); - if(m_parsingToolbox->readNumber(str,myNumber)) - { - /* if(!hasDice) + /* if(!hasDice) { previous = addRollDiceNode(DEFAULT_FACES_NUMBER,previous); }*/ - DiceRollerNode* nodeTmp = dynamic_cast<DiceRollerNode*>(previous); - if(nullptr!=nodeTmp) - { + DiceRollerNode* nodeTmp = dynamic_cast<DiceRollerNode*>(previous); + if(nullptr!=nodeTmp) + { - previous = addExplodeDiceNode(nodeTmp->getFaces(),previous); - } + previous = addExplodeDiceNode(nodeTmp->getFaces(),previous); + } - node = m_parsingToolbox->addSort(previous,ascending); + node = m_parsingToolbox->addSort(previous,ascending); - KeepDiceExecNode* nodeK = new KeepDiceExecNode(); - nodeK->setDiceKeepNumber(myNumber); + KeepDiceExecNode* nodeK = new KeepDiceExecNode(); + nodeK->setDiceKeepNumber(myNumber); - node->setNextNode(nodeK); - node = nodeK; - found = true; - } + node->setNextNode(nodeK); + node = nodeK; + found = true; } - break; - case Filter: + } + break; + case Filter: + { + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) - { - m_parsingToolbox->isValidValidator(previous,validator); + m_parsingToolbox->isValidValidator(previous,validator); - FilterNode* filterNode = new FilterNode(); - filterNode->setValidator(validator); + FilterNode* filterNode = new FilterNode(); + filterNode->setValidator(validator); - previous->setNextNode(filterNode); - node = filterNode; - found = true; - } + previous->setNextNode(filterNode); + node = filterNode; + found = true; } - break; - case Sort: - { - bool ascending = m_parsingToolbox->readAscending(str); - node = m_parsingToolbox->addSort(previous,ascending); - /*if(!hasDice) + } + break; + case Sort: + { + bool ascending = m_parsingToolbox->readAscending(str); + node = m_parsingToolbox->addSort(previous,ascending); + /*if(!hasDice) { m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Sort Operator does not support default dice. You should add dice command before the s")); }*/ - found = true; - } - break; - case Count: + found = true; + } + break; + case Count: + { + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) - { - m_parsingToolbox->isValidValidator(previous,validator); + m_parsingToolbox->isValidValidator(previous,validator); - CountExecuteNode* countNode = new CountExecuteNode(); - countNode->setValidator(validator); + CountExecuteNode* countNode = new CountExecuteNode(); + countNode->setValidator(validator); - previous->setNextNode(countNode); - node = countNode; - found = true; - } - else - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the c operator. Please, change it")); - } + previous->setNextNode(countNode); + node = countNode; + found = true; + } + else + { + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the c operator. Please, change it")); } - break; - case Reroll: - case RerollUntil: - case RerollAndAdd: + } + break; + case Reroll: + case RerollUntil: + case RerollAndAdd: // Todo: I think that Exploding and Rerolling could share the same code + { + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + if(!m_parsingToolbox->isValidValidator(previous,validator)) { - if(!m_parsingToolbox->isValidValidator(previous,validator)) - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the %1 operator. Please, change it") - .arg(operatorName == Reroll ? "r" : "a" )); - } - - auto reroll = (operatorName==RerollAndAdd || operatorName==Reroll); - auto addingMode = (operatorName==RerollAndAdd); - RerollDiceNode* rerollNode = new RerollDiceNode(reroll, addingMode); - ExecutionNode* nodeParam = nullptr; - if(readParameterNode(str,nodeParam)) - { - rerollNode->setInstruction(nodeParam); - } - rerollNode->setValidator(validator); - previous->setNextNode(rerollNode); - node = rerollNode; - found = true; - + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the %1 operator. Please, change it") + .arg(operatorName == Reroll ? "r" : "a" )); } - else + + auto reroll = (operatorName==RerollAndAdd || operatorName==Reroll); + auto addingMode = (operatorName==RerollAndAdd); + RerollDiceNode* rerollNode = new RerollDiceNode(reroll, addingMode); + ExecutionNode* nodeParam = nullptr; + if(readParameterNode(str,nodeParam)) { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, - QObject::tr("Validator is missing after the %1 operator. Please, change it") - .arg(operatorName==Reroll? QStringLiteral("r") - :operatorName==RerollUntil? QStringLiteral("R") - :operatorName==RerollAndAdd? QStringLiteral("a") - :"")); + rerollNode->setInstruction(nodeParam); } + rerollNode->setValidator(validator); + previous->setNextNode(rerollNode); + node = rerollNode; + found = true; } - break; - case Explode: + else { - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) - { - if(!m_parsingToolbox->isValidValidator(previous,validator)) - { - m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR,QObject::tr("This condition %1 introduces an endless loop. Please, change it").arg(validator->toString())); - } - ExplodeDiceNode* explodedNode = new ExplodeDiceNode(); - explodedNode->setValidator(validator); - previous->setNextNode(explodedNode); - node = explodedNode; - found = true; - - } - else - { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the e operator. Please, change it")); - } + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + QObject::tr("Validator is missing after the %1 operator. Please, change it") + .arg(operatorName==Reroll? QStringLiteral("r") + :operatorName==RerollUntil? QStringLiteral("R") + :operatorName==RerollAndAdd? QStringLiteral("a") + :"")); } - break; - case Merge: - { - MergeNode* mergeNode = new MergeNode(); - mergeNode->setStartList(&m_startNodes); - previous->setNextNode(mergeNode); - node = mergeNode; - found = true; - } - break; - case Bind: + } + break; + case Explode: + { + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - BindNode* bindNode = new BindNode(); - bindNode->setStartList(&m_startNodes); - previous->setNextNode(bindNode); - node = bindNode; + if(!m_parsingToolbox->isValidValidator(previous,validator)) + { + m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR,QObject::tr("This condition %1 introduces an endless loop. Please, change it").arg(validator->toString())); + } + ExplodeDiceNode* explodedNode = new ExplodeDiceNode(); + explodedNode->setValidator(validator); + previous->setNextNode(explodedNode); + node = explodedNode; found = true; } - break; - case Painter: + else { - PainterNode* painter = new PainterNode(); - m_parsingToolbox->readPainterParameter(painter,str); - previous->setNextNode(painter); - node = painter; - found = true; + m_errorMap.insert(ExecutionNode::BAD_SYNTAXE,QObject::tr("Validator is missing after the e operator. Please, change it")); } - break; - case ifOperator: + } + break; + case Merge: + { + MergeNode* mergeNode = new MergeNode(); + mergeNode->setStartList(&m_startNodes); + previous->setNextNode(mergeNode); + node = mergeNode; + found = true; + + } + break; + case Bind: + { + BindNode* bindNode = new BindNode(); + bindNode->setStartList(&m_startNodes); + previous->setNextNode(bindNode); + node = bindNode; + found = true; + + } + break; + case Painter: + { + PainterNode* painter = new PainterNode(); + m_parsingToolbox->readPainterParameter(painter,str); + previous->setNextNode(painter); + node = painter; + found = true; + } + break; + case ifOperator: + { + IfNode* nodeif = new IfNode(); + nodeif->setConditionType(m_parsingToolbox->readConditionType(str)); + Validator* validator = m_parsingToolbox->readCompositeValidator(str); + if(nullptr!=validator) { - IfNode* nodeif = new IfNode(); - nodeif->setConditionType(m_parsingToolbox->readConditionType(str)); - Validator* validator = m_parsingToolbox->readCompositeValidator(str); - if(nullptr!=validator) + ExecutionNode* trueNode = nullptr; + ExecutionNode* falseNode = nullptr; + if(readIfInstruction(str,trueNode,falseNode)) { - ExecutionNode* trueNode = nullptr; - ExecutionNode* falseNode = nullptr; - if(readIfInstruction(str,trueNode,falseNode)) - { - nodeif->setInstructionTrue(trueNode); - nodeif->setInstructionFalse(falseNode); - nodeif->setValidator(validator); - previous->setNextNode(nodeif); - node = nodeif; - found = true; - } - else - { - delete nodeif; - } + nodeif->setInstructionTrue(trueNode); + nodeif->setInstructionFalse(falseNode); + nodeif->setValidator(validator); + previous->setNextNode(nodeif); + node = nodeif; + found = true; } else { delete nodeif; } - break; } - case Split: + else { - SplitNode* splitnode = new SplitNode(); - previous->setNextNode(splitnode); - node = splitnode; - found = true; + delete nodeif; } - break; - case Group: + break; + } + case Split: + { + SplitNode* splitnode = new SplitNode(); + previous->setNextNode(splitnode); + node = splitnode; + found = true; + } + break; + case Group: + { + qint64 groupNumber=0; + if(m_parsingToolbox->readNumber(str,groupNumber)) { - qint64 groupNumber=0; - if(m_parsingToolbox->readNumber(str,groupNumber)) - { - GroupNode* groupNode = new GroupNode(); - groupNode->setGroupValue(groupNumber); - previous->setNextNode(groupNode); - node = groupNode; - found = true; - } + GroupNode* groupNode = new GroupNode(); + groupNode->setGroupValue(groupNumber); + previous->setNextNode(groupNode); + node = groupNode; + found = true; } - break; + } + break; - } } } - return found; } - bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,ExecutionNode*& falseNode) + return found; +} +bool DiceParser::readIfInstruction(QString& str,ExecutionNode*& trueNode,ExecutionNode*& falseNode) +{ + if(readBlocInstruction(str,trueNode)) { - if(readBlocInstruction(str,trueNode)) + if(readBlocInstruction(str,falseNode)) { - if(readBlocInstruction(str,falseNode)) - { - return true; - } return true; } - return false; + return true; } + return false; +} - bool DiceParser::readParameterNode(QString& str, ExecutionNode* & node) +bool DiceParser::readParameterNode(QString& str, ExecutionNode* & node) +{ + if(str.startsWith("(")) { - if(str.startsWith("(")) + str=str.remove(0,1); + if(readExpression(str,node)) { - str=str.remove(0,1); - if(readExpression(str,node)) + if(str.startsWith(")")) { - if(str.startsWith(")")) - { - str=str.remove(0,1); - return true; - } + str=str.remove(0,1); + return true; } } - return false; } + return false; +} - bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) +bool DiceParser::readBlocInstruction(QString& str,ExecutionNode*& resultnode) +{ + if(str.startsWith('{')) { - if(str.startsWith('{')) + str=str.remove(0,1); + ExecutionNode* node = nullptr; + Die::ArithmeticOperator op; + ScalarOperatorNode* scalarNode = nullptr; + if(m_parsingToolbox->readArithmeticOperator(str,op)) { - str=str.remove(0,1); - ExecutionNode* node = nullptr; - Die::ArithmeticOperator op; - ScalarOperatorNode* scalarNode = nullptr; - if(m_parsingToolbox->readArithmeticOperator(str,op)) - { - scalarNode = new ScalarOperatorNode(); - scalarNode->setArithmeticOperator(op); - } - if(readExpression(str,node)) + scalarNode = new ScalarOperatorNode(); + scalarNode->setArithmeticOperator(op); + } + if(readExpression(str,node)) + { + if(str.startsWith('}')) { - if(str.startsWith('}')) + if(nullptr==scalarNode) { - if(nullptr==scalarNode) - { - resultnode = node; - } - else - { - resultnode = scalarNode; - scalarNode->setInternalNode(node); - } - str=str.remove(0,1); - return true; + resultnode = node; + } + else + { + resultnode = scalarNode; + scalarNode->setInternalNode(node); } + str=str.remove(0,1); + return true; } } - return false; } + return false; +} - QString DiceParser::getComment() const - { - return m_comment; - } +QString DiceParser::getComment() const +{ + return m_comment; +} - void DiceParser::setComment(const QString &comment) - { - m_comment = comment; - } +void DiceParser::setComment(const QString &comment) +{ + m_comment = comment; +} - QMap<ExecutionNode::DICE_ERROR_CODE,QString> DiceParser::getErrorMap() - { - QMap<ExecutionNode::DICE_ERROR_CODE,QString> map; +QMap<ExecutionNode::DICE_ERROR_CODE,QString> DiceParser::getErrorMap() +{ + QMap<ExecutionNode::DICE_ERROR_CODE,QString> map; - for(auto start : m_startNodes) + for(auto start : m_startNodes) + { + auto mapTmp = start->getExecutionErrorMap(); + for(auto key : mapTmp.keys()) { - auto mapTmp = start->getExecutionErrorMap(); - for(auto key : mapTmp.keys()) - { - map.insertMulti(key,mapTmp[key]); - } + map.insertMulti(key,mapTmp[key]); } - return map; } - QString DiceParser::humanReadableError() + return map; +} +QString DiceParser::humanReadableError() +{ + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_errorMap); + QString str(""); + while (i.hasNext()) { - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_errorMap); - QString str(""); - while (i.hasNext()) - { - i.next(); - str.append(i.value()); - str.append(QStringLiteral("\n")); - } - - ///list - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> j(getErrorMap()); - while(j.hasNext()) - { - j.next(); - str.append(j.value()); - str.append(QStringLiteral("\n")); - } - return str; + i.next(); + str.append(i.value()); + str.append(QStringLiteral("\n")); } - QString DiceParser::humanReadableWarning() + ///list + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> j(getErrorMap()); + while(j.hasNext()) { - QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_warningMap); - QString str(""); - while (i.hasNext()) - { - i.next(); - str.append(i.value()); - str.append(QStringLiteral("\n")); - } - return str; + j.next(); + str.append(j.value()); + str.append(QStringLiteral("\n")); } - - void DiceParser::writeDownDotTree(QString filepath) + return str; +} + +QString DiceParser::humanReadableWarning() +{ + QMapIterator<ExecutionNode::DICE_ERROR_CODE,QString> i(m_warningMap); + QString str(""); + while (i.hasNext()) { - if(m_startNodes.empty()) - return; + i.next(); + str.append(i.value()); + str.append(QStringLiteral("\n")); + } + return str; +} - QString str(QStringLiteral("digraph ExecutionTree {\n")); - for(auto start : m_startNodes) - { - start->generateDotTree(str); - } - str.append(QStringLiteral("}\n")); +void DiceParser::writeDownDotTree(QString filepath) +{ + if(m_startNodes.empty()) + return; - QFile file(filepath); - if(file.open(QIODevice::WriteOnly)) - { - QTextStream in(&file); - in << str; - } - } - void DiceParser::setPathToHelp(QString l) + QString str(QStringLiteral("digraph ExecutionTree {\n")); + for(auto start : m_startNodes) { - m_helpPath = l; + start->generateDotTree(str); } - void DiceParser::setVariableDictionary(const QHash<QString,QString>& variables) + str.append(QStringLiteral("}\n")); + + QFile file(filepath); + if(file.open(QIODevice::WriteOnly)) { - ParsingToolBox::setVariableHash(variables); + QTextStream in(&file); + in << str; } +} +void DiceParser::setPathToHelp(QString l) +{ + m_helpPath = l; +} +void DiceParser::setVariableDictionary(const QHash<QString,QString>& variables) +{ + ParsingToolBox::setVariableHash(variables); +} |