aboutsummaryrefslogtreecommitdiffstatshomepage
diff options
context:
space:
mode:
authorRenaud G <renaud@rolisteam.org>2018-08-27 01:03:42 +0200
committerRenaud G <renaud@rolisteam.org>2018-08-27 01:03:42 +0200
commitcdaac5c82433b97ae17d0576b185a7fd689c89a9 (patch)
tree39f48a45f74a4142dcd2f9533842bf25b5faf2f6
parentf238c80409b345482d4a292084b3813dcdbbb37f (diff)
parent52343ff414f7b41e8f38e28bec3de39500b5fed2 (diff)
downloadOneRoll-cdaac5c82433b97ae17d0576b185a7fd689c89a9.tar.gz
OneRoll-cdaac5c82433b97ae17d0576b185a7fd689c89a9.zip
Merge branch 'master' of github.com:Rolisteam/DiceParser
-rw-r--r--HelpMe.md15
-rw-r--r--dicealias.cpp82
-rw-r--r--dicealias.h2
-rw-r--r--diceparser.cpp3
-rw-r--r--diceparser.h2
-rw-r--r--diceparser.pri2
-rw-r--r--parsingtoolbox.cpp12
-rw-r--r--parsingtoolbox.h6
8 files changed, 109 insertions, 15 deletions
diff --git a/HelpMe.md b/HelpMe.md
index 4e59a78..8dbd121 100644
--- a/HelpMe.md
+++ b/HelpMe.md
@@ -19,6 +19,7 @@
* [Merge](#merge)
* [if](#if)
* [Group](#group)
+ * [Unique](#Unique)
* [Comment (\#)](#comment-)
* [Arithmetic](#arithmetic)
* [Arithmetic and Dice](#arithmetic-and-dice)
@@ -75,7 +76,7 @@ Rolling 3 dice with 10 faces starting at 0.
> 3d[-20--9]
-Rolling 3 dice, values ars between -20 and -9.
+Rolling 3 dice, values are between -20 and -9.
### Instruction: Roll two (or more) kinds of dice at once
@@ -96,7 +97,7 @@ It is possible to merge every instruction inside a huge one.
The operator merge is dedicated to that.
It is useful when you need to manage all diceresult as the same result.
-For example, if you need to keep the high dice between a d6 and d8.
+For example, if you need to keep the higher dice between a d6 and d8.
> d6;d8mk1
@@ -252,6 +253,16 @@ Merge operator is used for gathering several dice rolls from different die type
This command merges together the result from the d6 and the d8. Then, it applied the k operator on both result to keep the best.
Be careful, this operator merges the instruction list. Instruction reference (such as $1 etc..) won't work after merge operator.
+### Unique
+
+It makes exploded dice as new dice.
+
+> 4d6e6u6k3
+
+Result: 6 4 3 3 2
+Final result: 6+4+3 = 13
+
+
### Bind
Bind works exactly as merge but one thing.
diff --git a/dicealias.cpp b/dicealias.cpp
index a2683e3..f514e9e 100644
--- a/dicealias.cpp
+++ b/dicealias.cpp
@@ -24,6 +24,85 @@
#include <QDebug>
+QString makeReplament(const QString& pattern, const QString& replacement, QString cmd)
+{
+
+ // FIXME try to do the same with RegularExpression
+ auto hasPattern = cmd.contains(pattern);
+ if(hasPattern)
+ {
+ auto hasVariable = cmd.contains("${");
+ auto hasQuote = cmd.contains("\"");
+
+ if(!hasQuote && !hasVariable)
+ {
+ cmd.replace(pattern, replacement);
+ }
+ else
+ {
+ std::vector<int> patternPosList;
+ std::vector<std::pair<int,int>> variablePos;
+
+ int pos= 0;
+ QRegularExpressionMatch match;
+ while(pos!=-1)
+ {
+ auto start = cmd.indexOf(QRegularExpression("\\${\\N+}"),pos,&match);
+ if(start >=0)
+ {
+ auto end = start+match.captured().length();
+ variablePos.push_back(std::make_pair(start,end));
+ pos = end+1;
+ }
+ else
+ {
+ pos = start;
+ }
+ }
+
+ pos = 0;
+ while(pos!=-1)
+ {
+ auto start = cmd.indexOf("\"",pos);
+ if(start >= 0)
+ {
+ auto end = cmd.indexOf("\"",start+1);
+ variablePos.push_back(std::make_pair(start,end));
+ pos = end+1;
+ }
+ else
+ {
+ pos = start;
+ }
+ }
+
+ pos= 0;
+ while((pos = cmd.indexOf(pattern,pos)) && pos!=-1)
+ {
+ bool isInsidePair = false;
+ for(auto pair : variablePos)
+ {
+ if(!isInsidePair)
+ isInsidePair = (pos > pair.first && pos < pair.second);
+ }
+ if(!isInsidePair)
+ patternPosList.push_back(pos);
+ pos+=1;
+ }
+
+ // TODO to be replace by C++14 when it is ready
+ for (auto i = patternPosList.rbegin(); i != patternPosList.rend(); ++i)
+ {
+ cmd.replace(*i,1,replacement);
+ }
+ }
+ }
+ return cmd;
+}
+
+
+
+
DiceAlias::DiceAlias(QString cmd, QString key, bool isReplace,bool isEnable)
: m_command(cmd),m_value(key),m_isEnable(isEnable)
{
@@ -49,7 +128,8 @@ bool DiceAlias::resolved(QString & str)
if((m_type == REPLACE)&&(str.contains(m_command)))
{
- str.replace(m_command,m_value);
+ str = makeReplament(m_command,m_value,str);
+ //str.replace(m_command,m_value);
return true;
}
else if(m_type == REGEXP)
diff --git a/dicealias.h b/dicealias.h
index a85c9a0..8320445 100644
--- a/dicealias.h
+++ b/dicealias.h
@@ -37,7 +37,7 @@ public:
* @param key
* @param isReplace
*/
- DiceAlias(QString cmd, QString key, bool isReplace = true, bool isEnable = true);
+ DiceAlias(QString pattern, QString remplacement, bool isReplace = true, bool isEnable = true);
/**
* @brief ~DiceAlias
*/
diff --git a/diceparser.cpp b/diceparser.cpp
index 22ff52f..41a91e5 100644
--- a/diceparser.cpp
+++ b/diceparser.cpp
@@ -155,6 +155,7 @@ void DiceParser::insertAlias(DiceAlias* dice, int i)
bool DiceParser::parseLine(QString str, bool allowAlias)
{
m_errorMap.clear();
+ m_comment = QStringLiteral("");
if(!m_startNodes.empty())
{
qDeleteAll(m_startNodes);
@@ -1329,7 +1330,7 @@ void DiceParser::setPathToHelp(QString l)
{
m_helpPath = l;
}
-void DiceParser::setVariableDictionary(QHash<QString,QString>* variables)
+void DiceParser::setVariableDictionary(const QHash<QString,QString>& variables)
{
ParsingToolBox::setVariableHash(variables);
}
diff --git a/diceparser.h b/diceparser.h
index ad831cf..2ede553 100644
--- a/diceparser.h
+++ b/diceparser.h
@@ -213,7 +213,7 @@ public:
* @brief setVariableDictionary
* @param variables
*/
- void setVariableDictionary(QHash<QString,QString>* variables);
+ void setVariableDictionary(const QHash<QString, QString> &variables);
QString getComment() const;
void setComment(const QString &comment);
diff --git a/diceparser.pri b/diceparser.pri
index 22bcb30..b554d8c 100644
--- a/diceparser.pri
+++ b/diceparser.pri
@@ -40,6 +40,7 @@ SOURCES += $$PWD/diceparser.cpp \
$$PWD/node/splitnode.cpp \
$$PWD/node/listsetrollnode.cpp\
$$PWD/node/variablenode.cpp\
+ $$PWD/node/bind.cpp\
$$PWD/diceroller.cpp\
$$PWD/qmltypesregister.cpp
@@ -79,6 +80,7 @@ HEADERS += \
$$PWD/node/mergenode.h \
$$PWD/node/listaliasnode.h \
$$PWD/node/ifnode.h \
+ $$PWD/node/bind.h\
$$PWD/node/splitnode.h \
$$PWD/node/paintnode.h \
$$PWD/node/listsetrollnode.h \
diff --git a/parsingtoolbox.cpp b/parsingtoolbox.cpp
index a56c098..63ae406 100644
--- a/parsingtoolbox.cpp
+++ b/parsingtoolbox.cpp
@@ -28,7 +28,7 @@
#include "node/stringnode.h"
-QHash<QString,QString>* ParsingToolBox::m_variableHash = nullptr;
+QHash<QString,QString> ParsingToolBox::m_variableHash;
std::vector<ExecutionNode*>* ParsingToolBox::m_startNodes = nullptr;
ParsingToolBox::ParsingToolBox()
@@ -491,11 +491,11 @@ bool ParsingToolBox::readVariable(QString &str, qint64 &myNumber, QString& reaso
int post = str.indexOf('}');
key = str.left(post);
- if(nullptr!=m_variableHash)
+ if(!m_variableHash.isEmpty())
{
- if(m_variableHash->contains(key))
+ if(m_variableHash.contains(key))
{
- QString value = m_variableHash->value(key);
+ QString value = m_variableHash.value(key);
bool ok;
int valueInt = value.toInt(&ok);
if(ok)
@@ -664,12 +664,12 @@ void ParsingToolBox::readPainterParameter(PainterNode* painter,QString& str)
}
}
-QHash<QString, QString> *ParsingToolBox::getVariableHash()
+QHash<QString, QString> ParsingToolBox::getVariableHash()
{
return m_variableHash;
}
-void ParsingToolBox::setVariableHash(QHash<QString, QString> *variableHash)
+void ParsingToolBox::setVariableHash(const QHash<QString, QString>& variableHash)
{
m_variableHash = variableHash;
}
diff --git a/parsingtoolbox.h b/parsingtoolbox.h
index 4a88732..be0b0eb 100644
--- a/parsingtoolbox.h
+++ b/parsingtoolbox.h
@@ -178,8 +178,8 @@ public:
static void readPainterParameter(PainterNode *painter, QString &str);
- static QHash<QString, QString> *getVariableHash();
- static void setVariableHash(QHash<QString, QString> *variableHash);
+ static QHash<QString, QString> getVariableHash();
+ static void setVariableHash(const QHash<QString, QString> &variableHash);
/**
* @brief readConditionType
* @param str
@@ -202,7 +202,7 @@ private:
QHash<QString,Die::ArithmeticOperator>* m_arithmeticOperation;
- static QHash<QString,QString>* m_variableHash;
+ static QHash<QString,QString> m_variableHash;
static std::vector<ExecutionNode*>* m_startNodes;
};