From 6f499de159eeac37fe473f945042e13359dc2d40 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jul 2019 09:59:03 +0200 Subject: move files --- include/parsingtoolbox.h | 247 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 247 insertions(+) create mode 100644 include/parsingtoolbox.h (limited to 'include/parsingtoolbox.h') diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h new file mode 100644 index 0000000..b91db1a --- /dev/null +++ b/include/parsingtoolbox.h @@ -0,0 +1,247 @@ +/*************************************************************************** + * Copyright (C) 2014 by Renaud Guezennec * + * http://www.rolisteam.org/contact * + * * + * This file is part of DiceParser * + * * + * DiceParser is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#ifndef PARSINGTOOLBOX_H +#define PARSINGTOOLBOX_H + +#include + +#include "booleancondition.h" +#include "compositevalidator.h" +#include "node/dicerollernode.h" +#include "node/executionnode.h" +#include "node/ifnode.h" +#include "node/paintnode.h" +#include "node/scalaroperatornode.h" +#include "operationcondition.h" +#include "range.h" + +class SubtituteInfo +{ +public: + SubtituteInfo(); + + bool isValid() const; + + int length() const; + void setLength(int length); + + int resultIndex() const; + void setResultIndex(int valueIndex); + + int position() const; + void setPosition(int position); + + int digitNumber() const; + void setDigitNumber(int digitNumber); + +private: + int m_length= 2; + int m_digitNumber= 0; + int m_resultIndex= -1; + int m_position= -1; +}; + +/** + * @brief The ParsingToolBox is gathering many useful methods for dice parsing. + * Its goal is to make the diceparser a bit lighter. + */ +class ParsingToolBox +{ +public: + enum LIST_OPERATOR + { + NONE, + UNIQUE + }; + + /** + * @brief ParsingToolBox + */ + ParsingToolBox(); + /** + * @brief ParsingToolBox + * @param data + */ + ParsingToolBox(const ParsingToolBox& data); + /** + * @brief ~ParsingToolBox + */ + virtual ~ParsingToolBox(); + /** + * @brief addSort + * @param e + * @param b + * @return + */ + ExecutionNode* addSort(ExecutionNode* e, bool b); + /** + * @brief readAscending + * @param str + * @return + */ + static bool readAscending(QString& str); + /** + * @brief readLogicOperator + * @param str + * @param op + * @return + */ + bool readLogicOperator(QString& str, BooleanCondition::LogicOperator& op); + /** + * @brief readValidator + * @param str + * @return + */ + Validator* readValidator(QString& str, bool hasSquare=false); + /** + * @brief readCompositeValidator + * @param str + * @return + */ + Validator* readCompositeValidator(QString& str); + + /** + * @brief readNumber read number in the given str and remove from the string the read character. + * @param str the command line + * @param myNumber reference to the found number + * @return true, succeed to read number, false otherwise. + */ + 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 + * @return + */ + static bool readVariable(QString& str, qint64& myNumber, QString& reasonFail); + /** + * @brief readOpenParentheses + * @param str + * @return + */ + static bool readOpenParentheses(QString& str); + /** + * @brief readCloseParentheses + * @param str + * @return + */ + static bool readCloseParentheses(QString& str); + + /** + * @brief readNumber read number in the given str and remove from the string the read character. + * @param str the command line + * @param myNumber reference to the found number + * @return true, succeed to read number, false otherwise. + */ + static bool readDynamicVariable(QString& str, qint64& index); + + /** + * @brief readList + * @param str + * @param list + * @return + */ + bool readList(QString& str, QStringList& list, QList& ranges); + /** + * @brief isValidValidator + * @param previous + * @param val + * @return + */ + bool isValidValidator(ExecutionNode* previous, Validator* val); + /** + * @brief getDiceRollerNode + * @param previous + * @return + */ + DiceRollerNode* getDiceRollerNode(ExecutionNode* previous); + + /** + * @brief readDiceRange + * @param str + * @param start + * @param end + * @return + */ + bool readDiceRange(QString& str, qint64& start, qint64& end); + /** + * @brief readListOperator + * @param str + * @return + */ + static LIST_OPERATOR readListOperator(QString& str); + + void readProbability(QStringList& str, QList& ranges); + + bool readLogicOperation(QString& str, CompositeValidator::LogicOperation& op); + + bool readDiceLogicOperator(QString& str, OperationCondition::ConditionOperator& op); + + bool readArithmeticOperator(QString& str, Die::ArithmeticOperator& op); + + static void readPainterParameter(PainterNode* painter, QString& str); + + static QHash getVariableHash(); + static void setVariableHash(const QHash& variableHash); + /** + * @brief readConditionType + * @param str + * @return + */ + static IfNode::ConditionType readConditionType(QString& str); + + bool readComment(QString& str, QString&, QString&); + static ExecutionNode* getLatestNode(ExecutionNode* node); + + static std::vector* getStartNodes(); + static void setStartNodes(std::vector* startNodes); + + static bool readOperand(QString& str, ExecutionNode*& node); + static int findClosingCharacterIndexOf(QChar open, QChar closing, const QString& str, int offset); + + static QString replaceVariableToValue(const QString& source, QStringList values); + + static SubtituteInfo readVariableFromString(const QString& source, int& start); + + static void readSubtitutionParameters(SubtituteInfo& info, QString& rest); + + static bool readComma(QString& str); + +private: + QMap* m_logicOp; + QMap* m_logicOperation; + QMap* m_conditionOperation; + QHash* m_arithmeticOperation; + + static QHash m_variableHash; + static std::vector* m_startNodes; +}; + +#endif // PARSINGTOOLBOX_H -- cgit v1.2.3-70-g09d2 From 7f535260bbc2210bf8d605bac88546e9f18b2b05 Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 01:35:59 +0200 Subject: New API for isValidRangeSize --- include/diceparserhelper.h | 8 ++++++++ include/parsingtoolbox.h | 2 +- node/explodedicenode.cpp | 14 ++++++++++++-- operationcondition.cpp | 21 ++++++++++++++------- operationcondition.h | 2 +- parsingtoolbox.cpp | 16 +++++----------- range.cpp | 16 ++++++++++++---- range.h | 1 + 8 files changed, 54 insertions(+), 26 deletions(-) (limited to 'include/parsingtoolbox.h') diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h index 5013ebe..00b9362 100644 --- a/include/diceparserhelper.h +++ b/include/diceparserhelper.h @@ -4,6 +4,14 @@ namespace Dice { +enum class CONDITION_STATE : int +{ + ERROR, + ALWAYSTRUE, + UNREACHABLE, + REACHABLE +}; + enum class ERROR_CODE : int { NO_DICE_ERROR, diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h index b91db1a..9e246d6 100644 --- a/include/parsingtoolbox.h +++ b/include/parsingtoolbox.h @@ -175,7 +175,7 @@ public: * @param val * @return */ - bool isValidValidator(ExecutionNode* previous, Validator* val); + Dice::CONDITION_STATE isValidValidator(ExecutionNode* previous, Validator* val); /** * @brief getDiceRollerNode * @param previous diff --git a/node/explodedicenode.cpp b/node/explodedicenode.cpp index 704ac8a..2292a05 100644 --- a/node/explodedicenode.cpp +++ b/node/explodedicenode.cpp @@ -16,7 +16,6 @@ void ExplodeDiceNode::run(ExecutionNode* previous) for(auto& die : previous_result->getResultList()) { Die* tmpdie= new Die(*die); -// *tmpdie= *die; m_diceResult->insertResult(tmpdie); die->displayed(); } @@ -25,12 +24,23 @@ void ExplodeDiceNode::run(ExecutionNode* previous) for(auto& die : list) { + if(Dice::CONDITION_STATE::ALWAYSTRUE + == m_validator->isValidRangeSize(std::make_pair(die->getBase(), die->getMaxValue()))) + { + m_errors.insert(Dice::ERROR_CODE::ENDLESS_LOOP_ERROR, + QObject::tr("Condition (%1) cause an endless loop with this dice: %2") + .arg(toString(true)) + .arg(QStringLiteral("d[%1,%2]") + .arg(static_cast(die->getBase())) + .arg(static_cast(die->getMaxValue())))); + continue; + } + while(m_validator->hasValid(die, false)) { die->roll(true); } } - // m_diceResult->setResultList(list); if(nullptr != m_nextNode) { diff --git a/operationcondition.cpp b/operationcondition.cpp index 507416d..97c7bce 100644 --- a/operationcondition.cpp +++ b/operationcondition.cpp @@ -106,15 +106,22 @@ QString OperationCondition::toString() } return QStringLiteral("[%1%2%3]").arg(str).arg(valueToScalar()).arg(m_boolean->toString()); } -bool OperationCondition::isValidRangeSize(std::pair) const +Dice::CONDITION_STATE OperationCondition::isValidRangeSize(const std::pair& range) const { - auto value= valueToScalar(); - bool valid= true; + Dice::CONDITION_STATE valid= Dice::CONDITION_STATE::REACHABLE; - if(value == 0) - valid= false; - /* else if(nullptr != m_boolean) - valid = m_boolean->isValidRangeSize(range);*/ + auto rangeIsClose= (range.first == range.second); + + Die die; + die.insertRollValue(range.first); + + if(nullptr == m_boolean) + return Dice::CONDITION_STATE::ERROR; + + if(rangeIsClose && m_boolean->hasValid(&die, false, false)) + valid= Dice::CONDITION_STATE::ALWAYSTRUE; + else if(rangeIsClose && !m_boolean->hasValid(&die, false, false)) + valid= Dice::CONDITION_STATE::UNREACHABLE; return valid; } diff --git a/operationcondition.h b/operationcondition.h index 86562e6..8b5a411 100644 --- a/operationcondition.h +++ b/operationcondition.h @@ -43,7 +43,7 @@ public: void setValueNode(ExecutionNode* node); QString toString(); - virtual bool isValidRangeSize(std::pair range) const; + virtual Dice::CONDITION_STATE isValidRangeSize(const std::pair& range) const override; BooleanCondition* getBoolean() const; void setBoolean(BooleanCondition* boolean); diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 2453eb7..4084825 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -601,19 +601,13 @@ bool ParsingToolBox::readAscending(QString& str) } return false; } -bool ParsingToolBox::isValidValidator(ExecutionNode* previous, Validator* val) +Dice::CONDITION_STATE ParsingToolBox::isValidValidator(ExecutionNode* previous, Validator* val) { DiceRollerNode* node= getDiceRollerNode(previous); - bool valid= false; - if(nullptr != node) - { - valid= val->isValidRangeSize(node->getRange()); - } - else - { - valid= true; - } - return valid; + if(nullptr == node) + return Dice::CONDITION_STATE::ERROR; + + return val->isValidRangeSize(node->getRange()); } DiceRollerNode* ParsingToolBox::getDiceRollerNode(ExecutionNode* previous) { diff --git a/range.cpp b/range.cpp index 471784c..8eeff4e 100644 --- a/range.cpp +++ b/range.cpp @@ -58,12 +58,20 @@ QString Range::toString() { return QStringLiteral("[%1-%2]").arg(m_start).arg(m_end); } -bool Range::isValidRangeSize(std::pair range) const +Dice::CONDITION_STATE Range::isValidRangeSize(const std::pair& range) const { - auto newStart= qBound(range.first, m_start, range.second); - auto newEnd= qBound(range.first, m_end, range.second); + auto minRange= std::min(m_start, m_end); + auto minPossibleValue= std::min(range.first, range.second); - return (newStart == m_start && newEnd == m_end && m_end >= m_start); + auto maxRange= std::max(m_start, m_end); + auto maxPossibleValue= std::max(range.first, range.second); + + if(minRange == minPossibleValue && maxRange == maxPossibleValue) + return Dice::CONDITION_STATE::ALWAYSTRUE; + else if(maxRange < minPossibleValue || minRange > maxPossibleValue) + return Dice::CONDITION_STATE::UNREACHABLE; + else + return Dice::CONDITION_STATE::UNREACHABLE; } void Range::setStart(qint64 start) { diff --git a/range.h b/range.h index 17cc9ad..596d8f3 100644 --- a/range.h +++ b/range.h @@ -39,6 +39,7 @@ public: virtual QString toString(); virtual bool isValidRangeSize(std::pair range) const; + virtual Dice::CONDITION_STATE isValidRangeSize(const std::pair& range) const override; bool isFullyDefined() const; qint64 getStart() const; -- cgit v1.2.3-70-g09d2 From a817fcbaa3350a59790d9b264d8eb613df80703d Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 28 Jul 2019 01:41:34 +0200 Subject: Add bool return to manage no parameter on painter node --- include/parsingtoolbox.h | 2 +- parsingtoolbox.cpp | 38 +++++++++++++++++++++----------------- 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'include/parsingtoolbox.h') diff --git a/include/parsingtoolbox.h b/include/parsingtoolbox.h index 9e246d6..8d5f62b 100644 --- a/include/parsingtoolbox.h +++ b/include/parsingtoolbox.h @@ -206,7 +206,7 @@ public: bool readArithmeticOperator(QString& str, Die::ArithmeticOperator& op); - static void readPainterParameter(PainterNode* painter, QString& str); + static bool readPainterParameter(PainterNode* painter, QString& str); static QHash getVariableHash(); static void setVariableHash(const QHash& variableHash); diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp index 4084825..7caeefe 100644 --- a/parsingtoolbox.cpp +++ b/parsingtoolbox.cpp @@ -661,28 +661,32 @@ ParsingToolBox::LIST_OPERATOR ParsingToolBox::readListOperator(QString& str) return NONE; } -void ParsingToolBox::readPainterParameter(PainterNode* painter, QString& str) +bool ParsingToolBox::readPainterParameter(PainterNode* painter, QString& str) { - if(str.startsWith('[')) - { - str= str.remove(0, 1); - int pos= str.indexOf(']'); + if(!str.startsWith('[')) + return false; + + str= str.remove(0, 1); + int pos= str.indexOf(']'); - if(pos > -1) + if(pos == -1) + return false; + + QString data= str.left(pos); + str= str.remove(0, pos + 1); + QStringList duos= data.split(','); + bool result= false; + for(QString& duoStr : duos) + { + QStringList keyValu= duoStr.split(':'); + if(keyValu.size() == 2) { - QString data= str.left(pos); - str= str.remove(0, pos + 1); - QStringList duos= data.split(','); - for(QString& duoStr : duos) - { - QStringList keyValu= duoStr.split(':'); - if(keyValu.size() == 2) - { - painter->insertColorItem(keyValu[1], keyValu[0].toInt()); - } - } + painter->insertColorItem(keyValu[1], keyValu[0].toInt()); + result= true; } } + + return result; } QHash ParsingToolBox::getVariableHash() -- cgit v1.2.3-70-g09d2