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/dicealias.h | 119 +++++++++++++++ include/diceparser.h | 362 +++++++++++++++++++++++++++++++++++++++++++++ include/diceparserhelper.h | 4 + include/highlightdice.h | 55 +++++++ include/parsingtoolbox.h | 247 +++++++++++++++++++++++++++++++ 5 files changed, 787 insertions(+) create mode 100644 include/dicealias.h create mode 100644 include/diceparser.h create mode 100644 include/diceparserhelper.h create mode 100644 include/highlightdice.h create mode 100644 include/parsingtoolbox.h (limited to 'include') diff --git a/include/dicealias.h b/include/dicealias.h new file mode 100644 index 0000000..463a654 --- /dev/null +++ b/include/dicealias.h @@ -0,0 +1,119 @@ +/*************************************************************************** + * 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 DICEALIAS_H +#define DICEALIAS_H + +#include +/** + * @brief The DiceAlias class is dedicated to store aliases, alias is mainly two QString. The Alias and its replacement. + * The replacement can be a simple QString or a RegExp. + */ +class DiceAlias +{ +public: + enum RESOLUTION_TYPE + { + REPLACE, + REGEXP + }; + /** + * @brief DiceAlias + * @param cmd + * @param key + * @param isReplace + */ + DiceAlias(QString pattern, QString remplacement, bool isReplace= true, bool isEnable= true); + /** + * @brief ~DiceAlias + */ + virtual ~DiceAlias(); + /** + * @brief resolved + * @param str + * @return + */ + bool resolved(QString& str); + /** + * @brief setCommand + * @param key + */ + void setCommand(QString key); + /** + * @brief setValue + * @param value + */ + void setValue(QString value); + /** + * @brief setType + */ + void setType(RESOLUTION_TYPE); + + /** + * @brief getCommand + * @return + */ + QString getCommand() const; + /** + * @brief getValue + * @return + */ + QString getValue() const; + /** + * @brief isReplace + * @return + */ + bool isReplace() const; + /** + * @brief setReplace + */ + void setReplace(bool); + + /** + * @brief isEnable + * @return + */ + bool isEnable() const; + /** + * @brief setEnable + * @param b + */ + void setEnable(bool b); + /** + * @brief getComment + * @return + */ + QString getComment() const; + /** + * @brief setComment + * @param comment + */ + void setComment(const QString& comment); + +private: + QString m_command; + QString m_value; + QString m_comment; + RESOLUTION_TYPE m_type; + bool m_isEnable; +}; + +#endif // DICEALIAS_H diff --git a/include/diceparser.h b/include/diceparser.h new file mode 100644 index 0000000..8ecfb50 --- /dev/null +++ b/include/diceparser.h @@ -0,0 +1,362 @@ +/*************************************************************************** + * 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 DICEPARSER_H +#define DICEPARSER_H + +#include +#include + +#include "dicealias.h" +#include "highlightdice.h" +#include "node/executionnode.h" + +typedef QList ListDiceResult; +typedef QMap ExportedDiceResult; + +class ExplodeDiceNode; +class ParsingToolBox; +class DiceRollerNode; +/** + * @page DiceParser Dice Parser + * + * @section Intro Introduction + * Diceparser is the software component dedicated to compute dice command in rolisteam.
+ * + * @section grammar The Grammar + * + * The grammar is described in Readme.md + */ + +/** + * @brief The DiceParser class facade class, it receives a command and return a DiceResult class (not yet implemented). + */ +class DiceParser +{ +public: + /** + * @brief The DiceOperator enum gathering all dice operators + */ + enum DiceOperator + { + D, + L + }; + /** + * @brief The DiceSymbol enum + */ + enum NodeAction + { + JumpBackward + }; + /** + * @brief The OptionOperator enum gathering all options availables for result. + */ + enum OptionOperator + { + KeepAndExplode, + Keep, + Reroll, + RerollUntil, + Explode, + Sort, + Count, + RerollAndAdd, + Merge, + ifOperator, + Painter, + Filter, + Split, + Group, + Occurences, + Unique, + Bind + }; + /** + * @brief The CommandOperator enum + */ + enum CommandOperator + { + }; + + /** + * @brief DiceParser default constructor + */ + DiceParser(); + /** + * @brief ~DiceParser + */ + virtual ~DiceParser(); + + /** + * @brief parseLine, method to call for starting the dice roll. It will parse the command and run the execution + * tree. + * @param str dice command + * @return bool every thing is fine or not + */ + bool parseLine(QString str, bool allowAlias= true); + /** + * @brief getStartNodeCount + * @return + */ + int getStartNodeCount() const; + /** + * @brief Start running the execution tree + * + */ + void start(); + + /** + * @brief displayResult + */ + QString displayResult(); + /** + * @brief readExpression + * @param str + * @param node + * @return + */ + bool readExpression(QString& str, ExecutionNode*& node); + /** + * @brief displayDotTree - Write the execution tree into file using dot format. + * @param filepath absolute or relative path to the tree file. + */ + void writeDownDotTree(QString filepath); + /** + * @brief getLastIntegerResults + * @return + */ + QList getLastIntegerResults(); + /** + * @brief getSumOfDiceResult + * @return + */ + QList getSumOfDiceResult(); + /** + * @brief getLastDiceResult + * @return + */ + void getLastDiceResult(QList& diceValues, bool& homogeneous); + /** + * @brief hasIntegerResultNotInFirst + * @return + */ + bool hasIntegerResultNotInFirst(); + /** + * @brief hasDiceResult + * @return + */ + bool hasDiceResult(); + /** + * @brief getDiceCommand + * @return + */ + QString getDiceCommand() const; + /** + * @brief hasStringResult + * @return + */ + bool hasStringResult(); + /** + * @brief getStringResult + * @return + */ + QStringList getStringResult(); + /** + * @brief humanReadableError + * @return + */ + QString humanReadableError(); + /** + * @brief getAliases + * @return + */ + QList* getAliases(); + /** + * @brief insertAlias + */ + void insertAlias(DiceAlias*, int); + /** + * @brief DiceParser::convertAlias + * @param str + * @return + */ + QString convertAlias(QString str); + /** + * @brief getErrorList + * @return + */ + QMap getErrorMap(); + /** + * @brief setPathToHelp set the path to the documentation, this path must be adatped to the lang of application etc… + * @param l the path. + */ + void setPathToHelp(QString l); + /** + * @brief getAllStringResult + * @return + */ + QStringList getAllStringResult(bool& hasAlias); + /** + * @brief getAllDiceResult + * @param hasAlias + * @return + */ + QStringList getAllDiceResult(bool& hasAlias); + /** + * @brief hasSeparator allows to know if the current command has separator. + * @return true when the command has separator, false otherwise. + */ + bool hasSeparator() const; + /** + * @brief readIfInstruction reads the current command to build if node with proper parameters. + * @param str is the command string, if IF istruction is found, the str will be changed, in other case the string is + * unmodified + * @param trueNode is the branch's beginning to be executed if the IfNode is true. + * @param falseNode is the branch's beginning to be executed if the IfNode is false. + * @return true, ifNode has been found, false otherwise + */ + bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); + /** + * @brief setVariableDictionary + * @param variables + */ + void setVariableDictionary(const QHash& variables); + QString getComment() const; + void setComment(const QString& comment); + + bool readOptionFromNull(QString& str, ExecutionNode*& node); + bool readOperatorFromNull(QString& str, ExecutionNode*& node); + + bool readInstructionList(QString& str); + void getDiceResultFromAllInstruction(QList& resultList); + QString humanReadableWarning(); + + bool readValuesList(QString& str, ExecutionNode*& node); + +protected: + bool readParameterNode(QString& str, ExecutionNode*& node); + +private: + /** + * @brief readDice + * @param str + * @return + */ + bool readDice(QString& str, ExecutionNode*& node); + /** + * @brief readDiceOperator + * @return + */ + bool readDiceOperator(QString&, DiceOperator&); + /** + * @brief readDiceExpression + * @param node + * @return + */ + bool readDiceExpression(QString&, ExecutionNode*& node); + /** + * @brief readOperator + * @return + */ + bool readOperator(QString&, ExecutionNode* previous); + /** + * @brief DiceParser::readCommand + * @param str + * @param node + * @return + */ + bool readCommand(QString& str, ExecutionNode*& node); + + /** + * @brief readOption + */ + bool readOption(QString&, ExecutionNode* node); // OptionOperator& option, + + /** + * @brief addRollDiceNode + * @param faces + * @return + */ + DiceRollerNode* addRollDiceNode(qint64 faces, ExecutionNode*); + /** + * @brief addExplodeDiceNode + * @param faces + * @param previous + * @return + */ + ExplodeDiceNode* addExplodeDiceNode(qint64 faces, ExecutionNode* previous); + /** + * @brief readOperand + * @param node + * @return + */ + bool readOperand(QString&, ExecutionNode*& node); + + /** + * @brief readInstructionOperator + * @param c + * @return + */ + bool readInstructionOperator(QChar c); + /** + * @brief readNode + * @param str + * @param node + * @return + */ + bool readNode(QString& str, ExecutionNode*& node); + + /** + * @brief getLeafNode + * @return + */ + ExecutionNode* getLeafNode(ExecutionNode* node); + + /** + * @brief hasResultOfType + * @param notthelast + * @return + */ + bool hasResultOfType(Result::RESULT_TYPE, ExecutionNode* node, bool notthelast= false); + +private: + QMap* m_mapDiceOp; + QMap* m_OptionOp; + QMap* m_nodeActionMap; + QList* m_aliasList; + QStringList* m_commandList; + + QMap m_errorMap; + QMap m_warningMap; + + ExecutionNode* m_start= nullptr; + std::vector m_startNodes; + // ExecutionNode* m_current; + QString m_command; + ParsingToolBox* m_parsingToolbox; + QString m_helpPath; + bool m_currentTreeHasSeparator; + bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); + QString m_comment; +}; + +#endif // DICEPARSER_H diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h new file mode 100644 index 0000000..4f53293 --- /dev/null +++ b/include/diceparserhelper.h @@ -0,0 +1,4 @@ +#ifndef DICEPARSERHELPER_H +#define DICEPARSERHELPER_H + +#endif // DICEPARSERHELPER_H diff --git a/include/highlightdice.h b/include/highlightdice.h new file mode 100644 index 0000000..2e8f08a --- /dev/null +++ b/include/highlightdice.h @@ -0,0 +1,55 @@ +/*************************************************************************** + * Copyright (C) 2016 by Renaud Guezennec * + * http:://www.rolisteam.org/contact * + * * + * rolisteam 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 HighLightDice_H +#define HighLightDice_H + +#include +#include + +class HighLightDice +{ +public: + HighLightDice(QList result, bool isHighlighted, QString color, bool displayed, quint64 faces); + virtual ~HighLightDice(); + + QList getResult() const; + void setResult(const QList& result); + + bool isHighlighted() const; + void setHighlight(bool hasHighlight); + + QString getColor() const; + void setColor(const QString& color); + + bool getDisplayed() const; + void setDisplayed(bool displayed); + + quint64 getFaces() const; + void setFaces(const quint64& faces); + +private: + QList m_result; + bool m_hasHighlight= true; + QString m_color; + bool m_displayed= false; + quint64 m_faces; +}; + +#endif // HighLightDice_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 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 --- CMakeLists.txt | 37 +++++++++++++++++++++++-- booleancondition.cpp | 2 +- cli/CMakeLists.txt | 2 ++ cli/main.cpp | 48 ++++++++++++++++---------------- diceparser.cpp | 57 +++++++++++++++++++------------------- highlightdice.cpp | 2 +- include/diceparser.h | 67 +++++++++++++++++++++------------------------ include/diceparserhelper.h | 30 ++++++++++++++++++++ node/dicerollernode.cpp | 8 +++--- node/executionnode.cpp | 6 ++-- node/executionnode.h | 21 ++------------ node/forloopnode.cpp | 2 +- node/ifnode.cpp | 2 +- node/jumpbackwardnode.cpp | 5 ++-- node/keepdiceexecnode.cpp | 7 +++-- node/listsetrollnode.cpp | 6 ++-- node/mergenode.cpp | 4 +-- node/occurencecountnode.cpp | 1 + node/paintnode.cpp | 2 +- node/rerolldicenode.cpp | 3 +- node/scalaroperatornode.cpp | 33 +++++++++++----------- node/scalaroperatornode.h | 2 +- node/valueslistnode.cpp | 2 +- node/variablenode.cpp | 2 +- operationcondition.cpp | 2 +- result/diceresult.cpp | 8 +++--- result/diceresult.h | 2 +- result/result.cpp | 9 ++++-- result/result.h | 16 ++--------- result/scalarresult.cpp | 9 ++---- result/scalarresult.h | 2 +- result/stringresult.cpp | 15 +++++----- result/stringresult.h | 4 +-- tests/dice/CMakeLists.txt | 3 ++ tests/dice/tst_dice.cpp | 4 +-- 35 files changed, 235 insertions(+), 190 deletions(-) (limited to 'include') diff --git a/CMakeLists.txt b/CMakeLists.txt index 9a9a340..e06bdac 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,6 @@ cmake_minimum_required(VERSION 3.5) -project(diceparser) +project(diceparser VERSION 1.9.0 DESCRIPTION "Parser of dice command") set(QT_REQUIRED_VERSION "5.12.0") find_package(Qt5 ${QT_REQUIRED_VERSION} CONFIG REQUIRED COMPONENTS Core Test Gui Svg) @@ -53,11 +53,42 @@ SET( dice_sources node/valueslistnode.cpp ) -add_library(diceparser SHARED ${dice_sources} ) -target_link_libraries(diceparser PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) +add_library(diceparser_shared SHARED ${dice_sources} ) +add_library(diceparser_static STATIC ${dice_sources} ) + +target_include_directories(diceparser_shared PRIVATE include) +target_include_directories(diceparser_static PRIVATE include) + +SET_TARGET_PROPERTIES(diceparser_static PROPERTIES OUTPUT_NAME diceparser CLEAN_DIRECT_OUTPUT 1) +SET_TARGET_PROPERTIES(diceparser_shared PROPERTIES OUTPUT_NAME diceparser CLEAN_DIRECT_OUTPUT 1) + +target_link_libraries(diceparser_shared PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) +target_link_libraries(diceparser_static PUBLIC Qt5::Core Qt5::Gui Qt5::Svg) + +set_target_properties(diceparser_shared PROPERTIES VERSION ${PROJECT_VERSION}) +set_target_properties(diceparser_shared PROPERTIES SOVERSION 1) +#target_link_libraries(diceparsersta PROPERTIES VERSION ${PROJECT_VERSION}) + +set_target_properties(diceparser_shared PROPERTIES PUBLIC_HEADER "include/diceparser.h;include/highlightdice.h;include/parsingtoolbox.h;include/dicealias.h;include/diceparserhelper.h") +#set_target_properties(diceparsersta PROPERTIES PUBLIC_HEADER "include/diceparser.h;include/highlightdice.h;include/parsingtoolbox.h;include/dicealias.h;include/diceparserhelper.h") add_subdirectory(cli) add_subdirectory( tests ) #add_subdirectory(irc) #add_subdirectory(mobile) #add_subdirectory(webserver) + + +include(GNUInstallDirs) + +install(TARGETS diceparser_shared + LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} + PUBLIC_HEADER DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + +install(TARGETS diceparser_static + ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}) + +configure_file(diceparser.pc.in diceparser.pc @ONLY) + +install(FILES ${CMAKE_BINARY_DIR}/diceparser.pc DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/pkgconfig) + diff --git a/booleancondition.cpp b/booleancondition.cpp index f62c900..c81f5ab 100644 --- a/booleancondition.cpp +++ b/booleancondition.cpp @@ -159,5 +159,5 @@ qint64 BooleanCondition::valueToScalar() const m_value->run(nullptr); auto result= m_value->getResult(); - return result->getResult(Result::SCALAR).toInt(); + return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 215287d..5d79b3c 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -74,6 +74,8 @@ SET( cli_sources set(documentation_files ../HelpMe.md ../README.md) add_executable( dice ${cli_sources} ${dice_QM} ${documentation_files}) +target_include_directories(dice PRIVATE ../include) + target_link_libraries(dice diceparser ${Qt5Core_LIBRARIES} ${Qt5Gui_LIBRARIES} ${Qt5Svg_LIBRARIES}) INSTALL_TARGETS(/bin dice) diff --git a/cli/main.cpp b/cli/main.cpp index d6f6398..958d96f 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -35,6 +35,7 @@ #include #endif +#include "dicealias.h" #include "diceparser.h" #include "displaytoolbox.h" #include "highlightdice.h" @@ -115,7 +116,7 @@ QString diceToMarkdown(QJsonArray array, bool withColor, bool allSameColor, bool } void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { Q_UNUSED(withColor); QJsonDocument doc; @@ -133,7 +134,7 @@ void displayJSon(QString scalarText, QString resultStr, QJsonArray array, bool w out << doc.toJson() << "\n"; } void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, - QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString error, QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { Q_UNUSED(withColor); QString str("```Markdown\n"); @@ -165,7 +166,7 @@ void displayMarkdown(QString scalarText, QString resultStr, QJsonArray array, bo out << str; } QString displaySVG(QString scalarText, QString resultStr, QJsonArray array, bool withColor, QString cmd, QString error, - QString warning, QString comment, bool allSameFaceCount, bool allSameColor) + QString warning, QString comment, bool allSameFaceCount, bool allSameColor) { QString str( "\n", "aliasfile"); + "path to alias json files: ", "aliasfile"); QCommandLineOption character(QStringList() << "s" << "charactersheet", - "set Parameters to simulate character sheet: ", "sheetfile"); + "set Parameters to simulate character sheet: ", "sheetfile"); QCommandLineOption markdown(QStringList() << "m" << "markdown", - "The output is formatted in markdown."); + "The output is formatted in markdown."); QCommandLineOption bot(QStringList() << "b" << "bot", - "Discord bot."); + "Discord bot."); QCommandLineOption svg(QStringList() << "g" << "svg", - "The output is formatted in svg."); + "The output is formatted in svg."); QCommandLineOption json(QStringList() << "j" << "json", - "The output is formatted in json."); + "The output is formatted in json."); QCommandLineOption dotFile(QStringList() << "d" << "dot-file", - "Instead of rolling dice, generate the execution tree and write it in ", "dotfile"); + "Instead of rolling dice, generate the execution tree and write it in ", + "dotfile"); QCommandLineOption translation(QStringList() << "t" << "translation", - "path to the translation file: ", "translationfile"); + "path to the translation file: ", "translationfile"); QCommandLineOption help(QStringList() << "h" << "help", - "Display this help"); + "Display this help"); optionParser.addOption(color); optionParser.addOption(version); 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()) { diff --git a/highlightdice.cpp b/highlightdice.cpp index 02941d5..c2abdb6 100644 --- a/highlightdice.cpp +++ b/highlightdice.cpp @@ -17,7 +17,7 @@ * Free Software Foundation, Inc., * * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ -#include "highlightdice.h" +#include "include/highlightdice.h" HighLightDice::HighLightDice(QList result, bool isHighlighted, QString color, bool displayed, quint64 faces) : m_result(result), m_hasHighlight(isHighlighted), m_color(color), m_displayed(displayed), m_faces(faces) diff --git a/include/diceparser.h b/include/diceparser.h index 8ecfb50..230d571 100644 --- a/include/diceparser.h +++ b/include/diceparser.h @@ -24,10 +24,11 @@ #include #include +#include -#include "dicealias.h" #include "highlightdice.h" -#include "node/executionnode.h" +#include "diceparserhelper.h" +//#include "node/executionnode.h" typedef QList ListDiceResult; typedef QMap ExportedDiceResult; @@ -35,6 +36,8 @@ typedef QMap ExportedDiceResult; class ExplodeDiceNode; class ParsingToolBox; class DiceRollerNode; +class DiceAlias; +class ExecutionNode; /** * @page DiceParser Dice Parser * @@ -123,18 +126,6 @@ public: * */ void start(); - - /** - * @brief displayResult - */ - QString displayResult(); - /** - * @brief readExpression - * @param str - * @param node - * @return - */ - bool readExpression(QString& str, ExecutionNode*& node); /** * @brief displayDotTree - Write the execution tree into file using dot format. * @param filepath absolute or relative path to the tree file. @@ -204,7 +195,7 @@ public: * @brief getErrorList * @return */ - QMap getErrorMap(); + QMap getErrorMap(); /** * @brief setPathToHelp set the path to the documentation, this path must be adatped to the lang of application etc… * @param l the path. @@ -226,15 +217,7 @@ public: * @return true when the command has separator, false otherwise. */ bool hasSeparator() const; - /** - * @brief readIfInstruction reads the current command to build if node with proper parameters. - * @param str is the command string, if IF istruction is found, the str will be changed, in other case the string is - * unmodified - * @param trueNode is the branch's beginning to be executed if the IfNode is true. - * @param falseNode is the branch's beginning to be executed if the IfNode is false. - * @return true, ifNode has been found, false otherwise - */ - bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); + /** * @brief setVariableDictionary * @param variables @@ -243,19 +226,32 @@ public: QString getComment() const; void setComment(const QString& comment); - bool readOptionFromNull(QString& str, ExecutionNode*& node); - bool readOperatorFromNull(QString& str, ExecutionNode*& node); - - bool readInstructionList(QString& str); void getDiceResultFromAllInstruction(QList& resultList); QString humanReadableWarning(); bool readValuesList(QString& str, ExecutionNode*& node); -protected: - bool readParameterNode(QString& str, ExecutionNode*& node); - private: + /** + * @brief readIfInstruction reads the current command to build if node with proper parameters. + * @param str is the command string, if IF istruction is found, the str will be changed, in other case the string is + * unmodified + * @param trueNode is the branch's beginning to be executed if the IfNode is true. + * @param falseNode is the branch's beginning to be executed if the IfNode is false. + * @return true, ifNode has been found, false otherwise + */ + bool readIfInstruction(QString& str, ExecutionNode*& trueNode, ExecutionNode*& falseNode); + bool readInstructionList(QString& str); + bool readOptionFromNull(QString& str, ExecutionNode*& node); + bool readOperatorFromNull(QString& str, ExecutionNode*& node); + bool readParameterNode(QString& str, ExecutionNode*& node); + /** + * @brief readExpression + * @param str + * @param node + * @return + */ + bool readExpression(QString& str, ExecutionNode*& node); /** * @brief readDice * @param str @@ -336,7 +332,8 @@ private: * @param notthelast * @return */ - bool hasResultOfType(Result::RESULT_TYPE, ExecutionNode* node, bool notthelast= false); + bool hasResultOfType(Dice::RESULT_TYPE, ExecutionNode* node, bool notthelast= false); + bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); private: QMap* m_mapDiceOp; @@ -345,17 +342,15 @@ private: QList* m_aliasList; QStringList* m_commandList; - QMap m_errorMap; - QMap m_warningMap; + QMap m_errorMap; + QMap m_warningMap; ExecutionNode* m_start= nullptr; std::vector m_startNodes; - // ExecutionNode* m_current; QString m_command; ParsingToolBox* m_parsingToolbox; QString m_helpPath; bool m_currentTreeHasSeparator; - bool readBlocInstruction(QString& str, ExecutionNode*& resultnode); QString m_comment; }; diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h index 4f53293..3ca4f15 100644 --- a/include/diceparserhelper.h +++ b/include/diceparserhelper.h @@ -1,4 +1,34 @@ #ifndef DICEPARSERHELPER_H #define DICEPARSERHELPER_H +namespace Dice +{ + +enum class ERROR_CODE : int +{ + NO_DICE_ERROR, + DIE_RESULT_EXPECTED, + BAD_SYNTAXE, + ENDLESS_LOOP_ERROR, + DIVIDE_BY_ZERO, + NOTHING_UNDERSTOOD, + NO_DICE_TO_ROLL, + TOO_MANY_DICE, + NO_VARIBALE, + INVALID_INDEX, + UNEXPECTED_CHARACTER, + NO_PREVIOUS_ERROR +}; + +/** + * @brief The RESULT_TYPE enum or combinaison + */ +enum class RESULT_TYPE : int +{ + NONE= 0, + SCALAR= 1, + STRING= 2, + DICE_LIST= 4 +}; +} #endif // DICEPARSERHELPER_H diff --git a/node/dicerollernode.cpp b/node/dicerollernode.cpp index f57d3e3..f87908f 100644 --- a/node/dicerollernode.cpp +++ b/node/dicerollernode.cpp @@ -19,10 +19,10 @@ void DiceRollerNode::run(ExecutionNode* previous) Result* result= previous->getResult(); if(nullptr != result) { - auto num= result->getResult(Result::SCALAR).toReal(); + auto num= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(num <= 0) { - m_errors.insert(NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); + m_errors.insert(Dice::ERROR_CODE::NO_DICE_TO_ROLL, QObject::tr("No dice to roll")); } m_diceCount= num > 0 ? static_cast(num) : 0; m_result->setPrevious(result); @@ -30,7 +30,7 @@ void DiceRollerNode::run(ExecutionNode* previous) auto possibleValue= static_cast(std::abs((m_max - m_min) + 1)); if(possibleValue < m_diceCount && m_unique) { - m_errors.insert(TOO_MANY_DICE, + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, QObject::tr("More unique values asked than possible values (D operator)")); return; } @@ -62,7 +62,7 @@ void DiceRollerNode::run(ExecutionNode* previous) quint64 DiceRollerNode::getFaces() const { - return std::abs(m_max - m_min) + 1; + return static_cast(std::abs(m_max - m_min) + 1); } std::pair DiceRollerNode::getRange() const diff --git a/node/executionnode.cpp b/node/executionnode.cpp index 5bee091..4545934 100644 --- a/node/executionnode.cpp +++ b/node/executionnode.cpp @@ -6,7 +6,7 @@ ExecutionNode::ExecutionNode() : m_previousNode(nullptr) , m_result(nullptr) , m_nextNode(nullptr) - , m_errors(QMap()) + , m_errors(QMap()) , m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) { } @@ -40,7 +40,7 @@ ExecutionNode* ExecutionNode::getNextNode() { return m_nextNode; } -QMap ExecutionNode::getExecutionErrorMap() +QMap ExecutionNode::getExecutionErrorMap() { if(nullptr != m_nextNode) { @@ -97,5 +97,5 @@ qint64 ExecutionNode::getScalarResult() { if(m_result == nullptr) return 0; - return m_result->getResult(Result::SCALAR).toInt(); + return m_result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/node/executionnode.h b/node/executionnode.h index e8eca1f..62bc5a5 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -1,8 +1,8 @@ #ifndef EXECUTIONNODE_H #define EXECUTIONNODE_H +#include "diceparserhelper.h" #include "result/result.h" -#include /** * @brief The ExecutionNode class @@ -10,21 +10,6 @@ class ExecutionNode { public: - enum DICE_ERROR_CODE - { - NO_DICE_ERROR, - DIE_RESULT_EXPECTED, - BAD_SYNTAXE, - ENDLESS_LOOP_ERROR, - DIVIDE_BY_ZERO, - NOTHING_UNDERSTOOD, - NO_DICE_TO_ROLL, - TOO_MANY_DICE, - NO_VARIBALE, - INVALID_INDEX, - UNEXPECTED_CHARACTER, - NO_PREVIOUS_ERROR - }; /** * @brief ExecutionNode */ @@ -72,7 +57,7 @@ public: * @brief getErrorList * @return */ - virtual QMap getExecutionErrorMap(); + virtual QMap getExecutionErrorMap(); /** * @brief generateDotTree @@ -109,7 +94,7 @@ protected: /** * @brief m_errors */ - QMap m_errors; + QMap m_errors; QString m_id; }; diff --git a/node/forloopnode.cpp b/node/forloopnode.cpp index f65a389..aba6ff4 100644 --- a/node/forloopnode.cpp +++ b/node/forloopnode.cpp @@ -58,7 +58,7 @@ void ForLoopNode::run(ExecutionNode* previous) tmp= tmp->getNextNode(); } Result* internalResult= tmp->getResult(); - auto value= internalResult->getResult(Result::SCALAR).toInt(); + auto value= internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); Die* neodie= new Die(); *neodie= *dice; diff --git a/node/ifnode.cpp b/node/ifnode.cpp index 5470e15..10b2f79 100644 --- a/node/ifnode.cpp +++ b/node/ifnode.cpp @@ -45,7 +45,7 @@ void IfNode::run(ExecutionNode* previous) if(nullptr != m_result) { - qreal value= previousResult->getResult(Result::SCALAR).toReal(); + qreal value= previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(nullptr != m_validator) { diff --git a/node/jumpbackwardnode.cpp b/node/jumpbackwardnode.cpp index 15c7063..2870854 100644 --- a/node/jumpbackwardnode.cpp +++ b/node/jumpbackwardnode.cpp @@ -96,7 +96,7 @@ void JumpBackwardNode::run(ExecutionNode* previous) if(nullptr != result) { //--i; - if(/*(i==0)&&*/ (result->hasResultOfType(Result::DICE_LIST))) + if(/*(i==0)&&*/ (result->hasResultOfType(Dice::RESULT_TYPE::DICE_LIST))) { found= true; m_backwardNode= parent; @@ -118,7 +118,8 @@ void JumpBackwardNode::run(ExecutionNode* previous) } if(nullptr == result) { - m_errors.insert(DIE_RESULT_EXPECTED, + m_errors.insert( + Dice::ERROR_CODE::DIE_RESULT_EXPECTED, QObject::tr(" The @ operator expects dice result. Please check the documentation to fix your command.")); } else diff --git a/node/keepdiceexecnode.cpp b/node/keepdiceexecnode.cpp index 42b4c40..688c27b 100644 --- a/node/keepdiceexecnode.cpp +++ b/node/keepdiceexecnode.cpp @@ -58,9 +58,10 @@ void KeepDiceExecNode::run(ExecutionNode* previous) if(m_numberOfDice > static_cast(diceList.size())) { - m_errors.insert(TOO_MANY_DICE, QObject::tr(" You ask to keep %1 dice but the result only has %2") - .arg(m_numberOfDice) - .arg(diceList.size())); + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr(" You ask to keep %1 dice but the result only has %2") + .arg(m_numberOfDice) + .arg(diceList.size())); } for(auto& tmp : diceList.mid(static_cast(m_numberOfDice), -1)) diff --git a/node/listsetrollnode.cpp b/node/listsetrollnode.cpp index ffa0f03..565a609 100644 --- a/node/listsetrollnode.cpp +++ b/node/listsetrollnode.cpp @@ -62,11 +62,11 @@ void ListSetRollNode::run(ExecutionNode* previous) Result* result= previous->getResult(); if(nullptr != result) { - quint64 diceCount= result->getResult(Result::SCALAR).toReal(); + quint64 diceCount= result->getResult(Dice::RESULT_TYPE::SCALAR).toReal(); if(diceCount > static_cast(m_values.size()) && m_unique) { - m_errors.insert( - TOO_MANY_DICE, QObject::tr("More unique values asked than possible values (L operator)")); + m_errors.insert(Dice::ERROR_CODE::TOO_MANY_DICE, + QObject::tr("More unique values asked than possible values (L operator)")); } else { diff --git a/node/mergenode.cpp b/node/mergenode.cpp index 4a11a76..c4c1543 100644 --- a/node/mergenode.cpp +++ b/node/mergenode.cpp @@ -29,7 +29,7 @@ void MergeNode::run(ExecutionNode* previous) { if(nullptr == previous) { - m_errors.insert(ExecutionNode::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Merge operator")); + m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Merge operator")); return; } @@ -69,7 +69,7 @@ void MergeNode::run(ExecutionNode* previous) } } auto it= std::find_if(pastResult.begin(), pastResult.end(), - [tmpResult](const Result* a) { return (a == tmpResult->getPrevious()); }); + [tmpResult](const Result* a) { return (a == tmpResult->getPrevious()); }); if(it == pastResult.end()) { pastResult.push_back(previousLast->getResult()); diff --git a/node/occurencecountnode.cpp b/node/occurencecountnode.cpp index e0117ef..0d69d2a 100644 --- a/node/occurencecountnode.cpp +++ b/node/occurencecountnode.cpp @@ -20,6 +20,7 @@ #include "occurencecountnode.h" #include "result/diceresult.h" #include "result/stringresult.h" +#include OccurenceCountNode::OccurenceCountNode() : ExecutionNode() {} diff --git a/node/paintnode.cpp b/node/paintnode.cpp index 02c7230..fe5a324 100644 --- a/node/paintnode.cpp +++ b/node/paintnode.cpp @@ -61,7 +61,7 @@ void PainterNode::run(ExecutionNode* previous) m_previousNode= previous; if(nullptr == previous) { - m_errors.insert(ExecutionNode::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); + m_errors.insert(Dice::ERROR_CODE::NO_PREVIOUS_ERROR, QObject::tr("No previous node before Paint operator")); return; } Result* previousResult= previous->getResult(); diff --git a/node/rerolldicenode.cpp b/node/rerolldicenode.cpp index 56ee6d9..18e8bba 100644 --- a/node/rerolldicenode.cpp +++ b/node/rerolldicenode.cpp @@ -79,7 +79,8 @@ void RerollDiceNode::run(ExecutionNode* previous) } else { - m_errors.insert(ExecutionNode::DIE_RESULT_EXPECTED, + m_errors.insert( + Dice::ERROR_CODE::DIE_RESULT_EXPECTED, QObject::tr( " The a operator expects dice result. Please check the documentation and fix your command.")); } diff --git a/node/scalaroperatornode.cpp b/node/scalaroperatornode.cpp index 3a26fc7..9edbcdc 100644 --- a/node/scalaroperatornode.cpp +++ b/node/scalaroperatornode.cpp @@ -69,28 +69,29 @@ void ScalarOperatorNode::run(ExecutionNode* previous) switch(m_arithmeticOperator) { case Die::PLUS: - m_scalarResult->setValue(add(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(add(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::MINUS: - m_scalarResult->setValue(substract(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(substract(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::MULTIPLICATION: - m_scalarResult->setValue(multiple(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(multiple(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::DIVIDE: - m_scalarResult->setValue(divide(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; case Die::INTEGER_DIVIDE: - m_scalarResult->setValue(static_cast(divide(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal()))); + m_scalarResult->setValue( + static_cast(divide(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal()))); break; case Die::POW: - m_scalarResult->setValue(pow(previousResult->getResult(Result::SCALAR).toReal(), - internalResult->getResult(Result::SCALAR).toReal())); + m_scalarResult->setValue(pow(previousResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal(), + internalResult->getResult(Dice::RESULT_TYPE::SCALAR).toReal())); break; } } @@ -128,7 +129,7 @@ qreal ScalarOperatorNode::divide(qreal a, qreal b) { if(qFuzzyCompare(b, 0)) { - m_errors.insert(DIVIDE_BY_ZERO, QObject::tr("Division by zero")); + m_errors.insert(Dice::ERROR_CODE::DIVIDE_BY_ZERO, QObject::tr("Division by zero")); return 0; } return static_cast(a / b); @@ -240,12 +241,12 @@ void ScalarOperatorNode::generateDotTree(QString& s) } s.append(str); } -QMap ScalarOperatorNode::getExecutionErrorMap() +QMap ScalarOperatorNode::getExecutionErrorMap() { if(nullptr != m_internalNode) { auto keys= m_internalNode->getExecutionErrorMap().keys(); - for(ExecutionNode::DICE_ERROR_CODE& key : keys) + for(const auto& key : keys) { m_errors.insert(key, m_internalNode->getExecutionErrorMap().value(key)); } @@ -253,7 +254,7 @@ QMap ScalarOperatorNode::getExecutionEr if(nullptr != m_nextNode) { auto keys= m_nextNode->getExecutionErrorMap().keys(); - for(ExecutionNode::DICE_ERROR_CODE& key : keys) + for(auto const& key : keys) { m_errors.insert(key, m_nextNode->getExecutionErrorMap().value(key)); } diff --git a/node/scalaroperatornode.h b/node/scalaroperatornode.h index 2ac89b0..a58a8d3 100644 --- a/node/scalaroperatornode.h +++ b/node/scalaroperatornode.h @@ -76,7 +76,7 @@ public: * @brief getErrorList * @return */ - virtual QMap getExecutionErrorMap(); + virtual QMap getExecutionErrorMap(); /** * @brief getArithmeticOperator * @return diff --git a/node/valueslistnode.cpp b/node/valueslistnode.cpp index b31ee84..d378350 100644 --- a/node/valueslistnode.cpp +++ b/node/valueslistnode.cpp @@ -16,7 +16,7 @@ void ValuesListNode::run(ExecutionNode* previous) auto result= node->getResult(); if(!result) continue; - auto val= result->getResult(Result::SCALAR).toInt(); + auto val= result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); Die* die= new Die(); auto dyna= dynamic_cast(node); if(nullptr != dyna) diff --git a/node/variablenode.cpp b/node/variablenode.cpp index 1d13a2c..e45214d 100644 --- a/node/variablenode.cpp +++ b/node/variablenode.cpp @@ -24,7 +24,7 @@ void VariableNode::run(ExecutionNode* previous) } else { - m_errors.insert(NO_VARIBALE, QObject::tr("No variable at index:%1").arg(m_index + 1)); + m_errors.insert(Dice::ERROR_CODE::NO_VARIBALE, QObject::tr("No variable at index:%1").arg(m_index + 1)); } } diff --git a/operationcondition.cpp b/operationcondition.cpp index 53a994d..67b10fc 100644 --- a/operationcondition.cpp +++ b/operationcondition.cpp @@ -131,5 +131,5 @@ qint64 OperationCondition::valueToScalar() const m_value->run(nullptr); auto result= m_value->getResult(); - return result->getResult(Result::SCALAR).toInt(); + return result->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); } diff --git a/result/diceresult.cpp b/result/diceresult.cpp index e66fc4f..23b925e 100644 --- a/result/diceresult.cpp +++ b/result/diceresult.cpp @@ -25,7 +25,7 @@ DiceResult::DiceResult() : m_operator(Die::PLUS) { - m_resultTypes= (DICE_LIST | SCALAR); + m_resultTypes= (static_cast(Dice::RESULT_TYPE::DICE_LIST) | static_cast(Dice::RESULT_TYPE::SCALAR)); m_homogeneous= true; } void DiceResult::insertResult(Die* die) @@ -63,15 +63,15 @@ DiceResult::~DiceResult() m_diceValues.clear(); } } -QVariant DiceResult::getResult(RESULT_TYPE type) +QVariant DiceResult::getResult(Dice::RESULT_TYPE type) { switch(type) { - case SCALAR: + case Dice::RESULT_TYPE::SCALAR: { return getScalarResult(); } - case DICE_LIST: + case Dice::RESULT_TYPE::DICE_LIST: { return QVariant(); } diff --git a/result/diceresult.h b/result/diceresult.h index 54e0b57..ce8ffb7 100644 --- a/result/diceresult.h +++ b/result/diceresult.h @@ -61,7 +61,7 @@ public: * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief toString * @return diff --git a/result/result.cpp b/result/result.cpp index edd00e4..983f1f4 100644 --- a/result/result.cpp +++ b/result/result.cpp @@ -22,7 +22,10 @@ #include "result.h" #include -Result::Result() : m_resultTypes(NONE), m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())), m_previous(nullptr) +Result::Result() + : m_resultTypes(static_cast(Dice::RESULT_TYPE::NONE)) + , m_id(QString("\"%1\"").arg(QUuid::createUuid().toString())) + , m_previous(nullptr) { } Result::~Result() {} @@ -43,9 +46,9 @@ bool Result::isStringResult() const return false; } void Result::clear() {} -bool Result::hasResultOfType(RESULT_TYPE type) const +bool Result::hasResultOfType(Dice::RESULT_TYPE type) const { - return (m_resultTypes & type); + return (m_resultTypes & static_cast(type)); } void Result::generateDotTree(QString& s) { diff --git a/result/result.h b/result/result.h index 8f31c05..a34c74f 100644 --- a/result/result.h +++ b/result/result.h @@ -22,7 +22,7 @@ #ifndef RESULT_H #define RESULT_H -//#include +#include "diceparserhelper.h" #include #include /** @@ -31,16 +31,6 @@ class Result { public: - /** - * @brief The RESULT_TYPE enum or combinaison - */ - enum RESULT_TYPE - { - NONE= 0, - SCALAR= 1, - STRING= 2, - DICE_LIST= 4 - }; /** * @brief Result */ @@ -54,12 +44,12 @@ public: * @brief isScalar * @return */ - virtual bool hasResultOfType(RESULT_TYPE) const; + virtual bool hasResultOfType(Dice::RESULT_TYPE) const; /** * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE)= 0; + virtual QVariant getResult(Dice::RESULT_TYPE)= 0; /** * @brief getPrevious * @return diff --git a/result/scalarresult.cpp b/result/scalarresult.cpp index 089c4b8..ec2b5f0 100644 --- a/result/scalarresult.cpp +++ b/result/scalarresult.cpp @@ -21,18 +21,15 @@ ***************************************************************************/ #include "scalarresult.h" -ScalarResult::ScalarResult() -{ - m_resultTypes= Result::SCALAR; -} +ScalarResult::ScalarResult() {} void ScalarResult::setValue(qreal i) { m_value= i; } -QVariant ScalarResult::getResult(Result::RESULT_TYPE type) +QVariant ScalarResult::getResult(Dice::RESULT_TYPE type) { - if(SCALAR == type) + if(Dice::RESULT_TYPE::SCALAR == type) { return m_value; } diff --git a/result/scalarresult.h b/result/scalarresult.h index c20de0f..1eac73c 100644 --- a/result/scalarresult.h +++ b/result/scalarresult.h @@ -39,7 +39,7 @@ public: * @brief getResult * @return */ - virtual QVariant getResult(Result::RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief setValue * @param i diff --git a/result/stringresult.cpp b/result/stringresult.cpp index a44e24e..d5a7880 100644 --- a/result/stringresult.cpp +++ b/result/stringresult.cpp @@ -3,20 +3,21 @@ StringResult::StringResult() { m_highlight= true; - m_resultTypes= Result::STRING; + m_resultTypes= static_cast(Dice::RESULT_TYPE::STRING); } void StringResult::setText(QString text) { m_value= text; } StringResult::~StringResult() {} -bool StringResult::hasResultOfType(RESULT_TYPE resultType) const +bool StringResult::hasResultOfType(Dice::RESULT_TYPE resultType) const { - if(resultType & Result::STRING) + + if(resultType == Dice::RESULT_TYPE::STRING) { return true; } - else if(resultType & Result::SCALAR) + else if(resultType == Dice::RESULT_TYPE::SCALAR) { bool ok= false; getText().toInt(&ok); @@ -28,13 +29,13 @@ QString StringResult::getText() const { return m_value; } -QVariant StringResult::getResult(RESULT_TYPE type) +QVariant StringResult::getResult(Dice::RESULT_TYPE type) { switch(type) { - case STRING: + case Dice::RESULT_TYPE::STRING: return getText(); - case SCALAR: + case Dice::RESULT_TYPE::SCALAR: return getText().toInt(); default: return QVariant(); diff --git a/result/stringresult.h b/result/stringresult.h index ec6c3ce..0e469fe 100644 --- a/result/stringresult.h +++ b/result/stringresult.h @@ -31,7 +31,7 @@ public: * @brief getScalar * @return */ - virtual QVariant getResult(RESULT_TYPE); + virtual QVariant getResult(Dice::RESULT_TYPE); /** * @brief toString * @return @@ -40,7 +40,7 @@ public: virtual void setHighLight(bool); virtual bool hasHighLight() const; - virtual bool hasResultOfType(RESULT_TYPE resultType) const; + virtual bool hasResultOfType(Dice::RESULT_TYPE resultType) const; virtual Result* getCopy() const; private: diff --git a/tests/dice/CMakeLists.txt b/tests/dice/CMakeLists.txt index 4065cc5..fef1a7b 100644 --- a/tests/dice/CMakeLists.txt +++ b/tests/dice/CMakeLists.txt @@ -5,5 +5,8 @@ find_package(Qt5Test REQUIRED) set(test_source testnode.cpp tst_dice.cpp) add_executable(test_dice ${test_source} ) + +target_include_directories(test_dice PRIVATE ../../include) + target_link_libraries(test_dice diceparser Qt5::Test) add_test(tst_diceparser test_dice) diff --git a/tests/dice/tst_dice.cpp b/tests/dice/tst_dice.cpp index 1fe2641..4da3474 100644 --- a/tests/dice/tst_dice.cpp +++ b/tests/dice/tst_dice.cpp @@ -524,7 +524,7 @@ void TestDice::keepTest() if(error) return; - auto resultScore= keepN.getResult()->getResult(Result::SCALAR).toInt(); + auto resultScore= keepN.getResult()->getResult(Dice::RESULT_TYPE::SCALAR).toInt(); QCOMPARE(score, resultScore); } @@ -609,7 +609,7 @@ void TestDice::countTest() node.run(nullptr); - QCOMPARE(score, countN.getResult()->getResult(Result::SCALAR).toInt()); + QCOMPARE(score, countN.getResult()->getResult(Dice::RESULT_TYPE::SCALAR).toInt()); countN.setValidator(nullptr); } -- cgit v1.2.3-70-g09d2 From 3fd2bd47e6581760c2129bcd2fccf532a32ef7ba Mon Sep 17 00:00:00 2001 From: Renaud G Date: Thu, 25 Jul 2019 22:34:20 +0200 Subject: Put together all important enums inside diceparserhelper.h --- cli/CMakeLists.txt | 60 +++++++++++++++++++++++++++++++++++----------- include/diceparserhelper.h | 3 ++- 2 files changed, 48 insertions(+), 15 deletions(-) (limited to 'include') diff --git a/cli/CMakeLists.txt b/cli/CMakeLists.txt index 5d79b3c..c697899 100644 --- a/cli/CMakeLists.txt +++ b/cli/CMakeLists.txt @@ -3,15 +3,57 @@ cmake_minimum_required(VERSION 3.5) option(UPDATE_TRANSLATIONS "update Translation" OFF) MESSAGE(STATUS "UPDATE TRANSLATIONS: ${UPDATE_TRANSLATIONS}") - project(dice) +SET( dice_sources + ../diceparser.cpp + ../range.cpp + ../booleancondition.cpp + ../validator.cpp + ../compositevalidator.cpp + ../operationcondition.cpp + ../die.cpp + ../parsingtoolbox.cpp + ../dicealias.cpp + ../result/result.cpp + ../result/scalarresult.cpp + ../result/stringresult.cpp + ../result/diceresult.cpp + ../node/countexecutenode.cpp + ../node/dicerollernode.cpp + ../node/executionnode.cpp + ../node/explodedicenode.cpp + ../node/helpnode.cpp + ../node/mergenode.cpp + ../node/jumpbackwardnode.cpp + ../node/keepdiceexecnode.cpp + ../node/listaliasnode.cpp + ../node/listsetrollnode.cpp + ../node/numbernode.cpp + ../node/parenthesesnode.cpp + # ../ node/forloopnode.cpp + ../node/paintnode.cpp + ../node/rerolldicenode.cpp + ../node/scalaroperatornode.cpp + ../node/sortresult.cpp + ../node/startingnode.cpp + ../node/filternode.cpp + ../node/stringnode.cpp + ../node/ifnode.cpp + ../node/splitnode.cpp + ../node/groupnode.cpp + ../node/bind.cpp + ../node/occurencecountnode.cpp + ../node/uniquenode.cpp + ../highlightdice.cpp + ../node/variablenode.cpp + ../node/valueslistnode.cpp +) # Find includes in corresponding build directories set(CMAKE_INCLUDE_CURRENT_DIR ON) # Instruct CMake to run moc automatically when needed. set(CMAKE_AUTOMOC ON) - set(EXECUTABLE_OUTPUT_PATH bin/) # Find the QtWidgets library @@ -38,7 +80,6 @@ set(dice_RESOURCES diceparser.qrc) find_package(Qt5LinguistTools) find_package(Qt5Svg) - IF(UPDATE_TRANSLATIONS) MESSAGE( update Translation ) FILE(GLOB_RECURSE translate_dice_SRCS ../*.cpp ../*.h) @@ -50,17 +91,8 @@ ELSE() ENDIF(UPDATE_TRANSLATIONS) if(Qt5Core_FOUND) - - #IF(UPDATE_TRANSLATIONS) - MESSAGE(status "find" ${dice_TS} ${translate_SRCS} ) - #QT5_CREATE_TRANSLATION(dice_QM ${translate_SRCS} ${dice_TS}) - #ELSE() - #QT5_ADD_TRANSLATION(dice_QM ${dice_TS}) - #ENDIF() - + MESSAGE(status "find" ${dice_TS} ${translate_SRCS} ) QT5_ADD_RESOURCES(dice_RESOURCES_RCC ${dice_RESOURCES}) - - # guess plugins and libraries directory set(QT_PLUGINS_DIR "${Qt5Core_DIR}/../../../plugins") get_target_property(QT_LIBRARY_DIR Qt5::Core LOCATION) get_filename_component(QT_LIBRARY_DIR ${QT_LIBRARY_DIR} PATH) @@ -73,7 +105,7 @@ SET( cli_sources set(documentation_files ../HelpMe.md ../README.md) -add_executable( dice ${cli_sources} ${dice_QM} ${documentation_files}) +add_executable( dice ${cli_sources} ${dice_QM} ${dice_sources} ${documentation_files}) target_include_directories(dice PRIVATE ../include) diff --git a/include/diceparserhelper.h b/include/diceparserhelper.h index 3ca4f15..5013ebe 100644 --- a/include/diceparserhelper.h +++ b/include/diceparserhelper.h @@ -17,7 +17,8 @@ enum class ERROR_CODE : int NO_VARIBALE, INVALID_INDEX, UNEXPECTED_CHARACTER, - NO_PREVIOUS_ERROR + NO_PREVIOUS_ERROR, + NO_VALID_RESULT }; /** -- 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 'include') 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 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') 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') 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