File indexing completed on 2025-01-05 05:09:58

0001 /***************************************************************************
0002  * Copyright (C) 2014 by Renaud Guezennec                                   *
0003  * https://rolisteam.org/contact                      *
0004  *                                                                          *
0005  *  This file is part of DiceParser                                         *
0006  *                                                                          *
0007  * DiceParser is free software; you can redistribute it and/or modify       *
0008  * it under the terms of the GNU General Public License as published by     *
0009  * the Free Software Foundation; either version 2 of the License, or        *
0010  * (at your option) any later version.                                      *
0011  *                                                                          *
0012  * This program is distributed in the hope that it will be useful,          *
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of           *
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the             *
0015  * GNU General Public License for more details.                             *
0016  *                                                                          *
0017  * You should have received a copy of the GNU General Public License        *
0018  * along with this program; if not, write to the                            *
0019  * Free Software Foundation, Inc.,                                          *
0020  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.                 *
0021  ***************************************************************************/
0022 #ifndef DICEPARSER_H
0023 #define DICEPARSER_H
0024 
0025 #include <QJsonObject>
0026 #include <QMap>
0027 #include <QString>
0028 #include <QVariant>
0029 #include <memory>
0030 #include <vector>
0031 
0032 #include <diceparser/diceparser_global.h>
0033 
0034 #include <diceparser/diceparserhelper.h>
0035 #include <diceparser/highlightdice.h>
0036 
0037 class ExplodeDiceNode;
0038 class ParsingToolBox;
0039 class DiceRollerNode;
0040 class DiceAlias;
0041 class ExecutionNode;
0042 /**
0043  * @page DiceParser Dice Parser
0044  *
0045  * @section Intro Introduction
0046  * Diceparser is the software component dedicated to compute dice command in rolisteam.<br/>
0047  *
0048  * @section grammar The Grammar
0049  *
0050  * The grammar is described in Readme.md
0051  */
0052 
0053 /**
0054  * @brief The DiceParser class facade class, it receives a command and return a DiceResult class (not yet implemented).
0055  */
0056 class DICEPARSER_EXPORT DiceParser
0057 {
0058 public:
0059     /**
0060      * @brief DiceParser default constructor
0061      */
0062     DiceParser();
0063     /**
0064      * @brief ~DiceParser
0065      */
0066     virtual ~DiceParser();
0067 
0068     // Command process methods
0069     /**
0070      * @brief parseLine, method to call for starting the dice roll. It will parse the command and run the execution
0071      * tree.
0072      * @param str dice command
0073      * @return bool every thing is fine or not
0074      */
0075     bool parseLine(QString str, bool allowAlias= true);
0076     void start();
0077     void cleanAll();
0078 
0079     // debug
0080     void writeDownDotTree(QString filepath);
0081 
0082     // control methods
0083     bool hasIntegerResultNotInFirst() const;
0084     bool hasDiceResult() const;
0085     bool hasStringResult() const;
0086     bool hasSeparator() const;
0087 
0088     // alias management
0089     const QList<DiceAlias*>& constAliases() const;
0090     QList<DiceAlias*>* aliases() const;
0091     void cleanAliases();
0092     void insertAlias(DiceAlias*, int);
0093     QString convertAlias(const QString& cmd) const;
0094 
0095     QStringList allFirstResultAsString(bool& hasAlias);
0096     QStringList getAllDiceResult(bool& hasAlias);
0097 
0098     // Accessors
0099     int startNodeCount() const;
0100     QList<qreal> scalarResultsFromEachInstruction() const;
0101     QStringList stringResultFromEachInstruction(bool& hasAlias) const;
0102     void diceResultFromEachInstruction(QList<ExportedDiceResult>& resultList) const;
0103     QString finalStringResult(std::function<QString(const QString&, const QString&, bool)> colorize) const;
0104 
0105     QString diceCommand() const;
0106     QMap<Dice::ERROR_CODE, QString> errorMap() const;
0107     QString comment() const;
0108     QString humanReadableWarning() const;
0109     QString humanReadableError() const;
0110     QString resultAsJSon(std::function<QString(const QString&, const QString&, bool)> colorize,
0111                          bool removeUnhighligthed= false) const;
0112 
0113     //    QStringList stringResult() const;
0114     //    QStringList allDiceResult(bool& hasAlias) const;
0115     //    void lastDiceResult(QList<ExportedDiceResult>& diceValues, bool& homogeneous) const;
0116 
0117     // setters
0118     void setPathToHelp(QString l);
0119     void setVariableDictionary(const QHash<QString, QString>& variables);
0120     void setComment(const QString& comment);
0121 
0122 private:
0123     bool readBlocInstruction(QString& str, ExecutionNode*& resultnode);
0124 
0125 private:
0126     std::unique_ptr<ParsingToolBox> m_parsingToolbox;
0127     QString m_command;
0128 };
0129 
0130 #endif // DICEPARSER_H