From bb6b87a2685c0d71b5c38be33c100f85ac0b9cee Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jul 2019 09:59:29 +0200 Subject: Rework of the component to be a proper lib --- diceparser.cpp | 57 +++++++++++++++++++++++++++++---------------------------- 1 file changed, 29 insertions(+), 28 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index 4ec44ef..da838f1 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -29,6 +29,7 @@ #include "node/bind.h" #include "node/countexecutenode.h" #include "node/dicerollernode.h" +#include "node/executionnode.h" #include "node/explodedicenode.h" #include "node/filternode.h" #include "node/groupnode.h" @@ -180,7 +181,7 @@ bool DiceParser::parseLine(QString str, bool allowAlias) if(!hasInstruction) { m_errorMap.insert( - ExecutionNode::NOTHING_UNDERSTOOD, + Dice::ERROR_CODE::NOTHING_UNDERSTOOD, QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " "https://github.com/" @@ -190,7 +191,7 @@ bool DiceParser::parseLine(QString str, bool allowAlias) { auto i= m_command.size() - str.size(); m_warningMap.insert( - ExecutionNode::UNEXPECTED_CHARACTER, + Dice::ERROR_CODE::UNEXPECTED_CHARACTER, QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); } if(!m_errorMap.isEmpty()) @@ -226,7 +227,7 @@ bool DiceParser::readExpression(QString& str, ExecutionNode*& node) } else { - m_warningMap.insert(ExecutionNode::BAD_SYNTAXE, + m_warningMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Expected closing parenthesis - can't validate the inside.")); } } @@ -392,11 +393,11 @@ QList DiceParser::getLastIntegerResults() bool scalarDone= false; while((result != nullptr) && (!scalarDone)) { - if(result->hasResultOfType(Result::SCALAR)) + if(result->hasResultOfType(Dice::RESULT_TYPE::SCALAR)) { if(!alreadyVisitedNode.contains(result->getId())) { - resultValues << result->getResult(Result::SCALAR).toReal(); + resultValues << result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); alreadyVisitedNode << result->getId(); } scalarDone= true; @@ -417,9 +418,9 @@ QStringList DiceParser::getStringResult() bool found= false; while((nullptr != result) && (!found)) { - if(result->hasResultOfType(Result::STRING)) + if(result->hasResultOfType(Dice::RESULT_TYPE::STRING)) { - str= result->getResult(Result::STRING).toString(); + str= result->getResult(Dice::RESULT_TYPE::STRING).toString(); found= true; } result= result->getPrevious(); @@ -440,7 +441,7 @@ QStringList DiceParser::getAllStringResult(bool& hasAlias) while(nullptr != result) { - if(result->hasResultOfType(Result::STRING)) + if(result->hasResultOfType(Dice::RESULT_TYPE::STRING)) { StringResult* stringResult= dynamic_cast(result); if(nullptr != stringResult) @@ -465,7 +466,7 @@ QStringList DiceParser::getAllDiceResult(bool& hasAlias) while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* stringResult= dynamic_cast(result); if(nullptr != stringResult) @@ -508,7 +509,7 @@ void DiceParser::getDiceResultFromAllInstruction(QList& resu ExportedDiceResult nodeResult; while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* diceResult= dynamic_cast(result); QList list; @@ -540,7 +541,7 @@ void DiceParser::getLastDiceResult(QList& diceValuesList, bo Result* result= next->getResult(); while(nullptr != result) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* diceResult= dynamic_cast(result); if(nullptr != diceResult) @@ -604,7 +605,7 @@ bool DiceParser::hasIntegerResultNotInFirst() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::SCALAR, node); + result|= hasResultOfType(Dice::RESULT_TYPE::SCALAR, node); } return result; } @@ -614,7 +615,7 @@ bool DiceParser::hasDiceResult() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::DICE_LIST, node); + result|= hasResultOfType(Dice::RESULT_TYPE::DICE_LIST, node); } return result; } @@ -623,11 +624,11 @@ bool DiceParser::hasStringResult() bool result= false; for(auto node : m_startNodes) { - result|= hasResultOfType(Result::STRING, node); + result|= hasResultOfType(Dice::RESULT_TYPE::STRING, node); } return result; } -bool DiceParser::hasResultOfType(Result::RESULT_TYPE type, ExecutionNode* node, bool notthelast) +bool DiceParser::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, bool notthelast) { bool scalarDone= false; ExecutionNode* next= getLeafNode(node); @@ -653,7 +654,7 @@ QList DiceParser::getSumOfDiceResult() bool found= false; while((nullptr != result) && (!found)) { - if(result->hasResultOfType(Result::DICE_LIST)) + if(result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST)) { DiceResult* myDiceResult= dynamic_cast(result); if(nullptr != myDiceResult) @@ -704,7 +705,7 @@ bool DiceParser::readDice(QString& str, ExecutionNode*& node) if(max < 1) { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Dice with %1 face(s) does not exist. Please, put a value higher than 0").arg(max)); return false; } @@ -759,7 +760,7 @@ bool DiceParser::readDice(QString& str, ExecutionNode*& node) else { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr( "List is missing after the L operator. Please, add it (e.g : 1L[sword,spear,gun,arrow])")); } @@ -1087,7 +1088,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the c operator. Please, change it")); } } @@ -1103,7 +1104,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, if(!m_parsingToolbox->isValidValidator(previous, validator)) { m_errorMap.insert( - ExecutionNode::BAD_SYNTAXE, + Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the %1 operator. Please, change it") .arg(operatorName == Reroll ? "r" : "a")); } @@ -1123,7 +1124,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the %1 operator. Please, change it") .arg(operatorName == Reroll ? QStringLiteral("r") : @@ -1140,7 +1141,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, { if(!m_parsingToolbox->isValidValidator(previous, validator)) { - m_errorMap.insert(ExecutionNode::ENDLESS_LOOP_ERROR, + m_errorMap.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, QObject::tr("This condition %1 introduces an endless loop. Please, change it") .arg(validator->toString())); } @@ -1152,7 +1153,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(ExecutionNode::BAD_SYNTAXE, + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, QObject::tr("Validator is missing after the e operator. Please, change it")); } } @@ -1345,9 +1346,9 @@ void DiceParser::setComment(const QString& comment) m_comment= comment; } -QMap DiceParser::getErrorMap() +QMap DiceParser::getErrorMap() { - QMap map; + QMap map; for(auto start : m_startNodes) { @@ -1362,7 +1363,7 @@ QMap DiceParser::getErrorMap() } QString DiceParser::humanReadableError() { - QMapIterator i(m_errorMap); + QMapIterator i(m_errorMap); QString str(""); while(i.hasNext()) { @@ -1372,7 +1373,7 @@ QString DiceParser::humanReadableError() } /// list - QMapIterator j(getErrorMap()); + QMapIterator j(getErrorMap()); while(j.hasNext()) { j.next(); @@ -1384,7 +1385,7 @@ QString DiceParser::humanReadableError() QString DiceParser::humanReadableWarning() { - QMapIterator i(m_warningMap); + QMapIterator i(m_warningMap); QString str(""); while(i.hasNext()) { -- cgit v1.2.3-70-g09d2 From ac6d69f33e686275a2a8df89431030197d43378e Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 01:04:05 +0200 Subject: Add cleanAll function --- diceparser.cpp | 15 ++++++++++----- include/diceparser.h | 2 +- 2 files changed, 11 insertions(+), 6 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index da838f1..7623718 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -165,11 +165,7 @@ bool DiceParser::parseLine(QString str, bool allowAlias) { m_errorMap.clear(); m_comment= QString(""); - if(!m_startNodes.empty()) - { - qDeleteAll(m_startNodes); - m_startNodes.clear(); - } + cleanAll(); m_currentTreeHasSeparator= false; if(allowAlias) { @@ -374,6 +370,15 @@ bool DiceParser::readNode(QString& str, ExecutionNode*& node) return false; } +void DiceParser::cleanAll() +{ + if(!m_startNodes.empty()) + { + qDeleteAll(m_startNodes); + m_startNodes.clear(); + } +} + void DiceParser::start() { for(auto start : m_startNodes) diff --git a/include/diceparser.h b/include/diceparser.h index 230d571..32694a3 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -230,7 +230,7 @@ public: QString humanReadableWarning(); bool readValuesList(QString& str, ExecutionNode*& node); - + void cleanAll(); private: /** * @brief readIfInstruction reads the current command to build if node with proper parameters. -- cgit v1.2.3-70-g09d2 From 0fd45ce5b17fa90929800766c0a7b5631541ae2c Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 01:04:44 +0200 Subject: New API for valid dice. --- diceparser.cpp | 63 +++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 18 deletions(-) (limited to 'diceparser.cpp') diff --git a/diceparser.cpp b/diceparser.cpp index 7623718..ea8f251 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -640,7 +640,11 @@ bool DiceParser::hasResultOfType(Dice::RESULT_TYPE type, ExecutionNode* node, bo Result* result= next->getResult(); while((result != nullptr) && (!scalarDone)) { - if(result->hasResultOfType(type) && ((!notthelast) || (nullptr != result->getPrevious()))) + bool lastResult= false; + if(notthelast) + lastResult= (nullptr != result->getPrevious()); + + if(result->hasResultOfType(type) && !lastResult) { scalarDone= true; } @@ -1104,14 +1108,33 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, // Todo: I think that Exploding and Rerolling could share the same code { Validator* validator= m_parsingToolbox->readCompositeValidator(str); + QString symbol= m_OptionOp->key(operatorName); if(nullptr != validator) { - if(!m_parsingToolbox->isValidValidator(previous, validator)) + switch(m_parsingToolbox->isValidValidator(previous, validator)) { - m_errorMap.insert( - Dice::ERROR_CODE::BAD_SYNTAXE, - QObject::tr("Validator is missing after the %1 operator. Please, change it") - .arg(operatorName == Reroll ? "r" : "a")); + case Dice::CONDITION_STATE::ALWAYSTRUE: + if(operatorName == RerollAndAdd) + { + m_errorMap.insert( + Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr( + "Validator is always true missing after the %1 operator. Please, change it") + .arg(symbol)); + } + break; + case Dice::CONDITION_STATE::UNREACHABLE: + if(operatorName == RerollUntil) + { + m_errorMap.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr("Candition can be reached, causing endless loop. Please, " + "change the %1 option condition") + .arg(symbol)); + } + break; + case Dice::CONDITION_STATE::ERROR: + default: + break; } auto reroll= (operatorName == RerollAndAdd || operatorName == Reroll); @@ -1129,13 +1152,9 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, } else { - m_errorMap.insert(Dice::ERROR_CODE::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") : "")); + m_errorMap.insert( + Dice::ERROR_CODE::BAD_SYNTAXE, + QObject::tr("Validator is missing after the %1 operator. Please, change it").arg(symbol)); } } break; @@ -1144,7 +1163,7 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, Validator* validator= m_parsingToolbox->readCompositeValidator(str); if(nullptr != validator) { - if(!m_parsingToolbox->isValidValidator(previous, validator)) + if(Dice::CONDITION_STATE::ALWAYSTRUE == m_parsingToolbox->isValidValidator(previous, validator)) { m_errorMap.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, QObject::tr("This condition %1 introduces an endless loop. Please, change it") @@ -1216,10 +1235,18 @@ bool DiceParser::readOption(QString& str, ExecutionNode* previous) //, case Painter: { PainterNode* painter= new PainterNode(); - m_parsingToolbox->readPainterParameter(painter, str); - previous->setNextNode(painter); - node= painter; - found= true; + if(!m_parsingToolbox->readPainterParameter(painter, str)) + { + m_errorMap.insert(Dice::ERROR_CODE::BAD_SYNTAXE, + QObject::tr("Missing parameter for Painter node (p)")); + delete painter; + } + else + { + previous->setNextNode(painter); + node= painter; + found= true; + } } break; case ifOperator: -- cgit v1.2.3-70-g09d2