From 06aac01fdcdb0b320b5c931b6b28f8a3cb2e74ed Mon Sep 17 00:00:00 2001 From: Renaud G Date: Sun, 9 Dec 2018 22:51:15 +0100 Subject: Display warning message and fix error. --- cli/main.cpp | 32 +++++++++++++++++++++++--------- diceparser.cpp | 37 +++++++++++++++++++++++++++---------- diceparser.h | 1 + node/executionnode.h | 2 +- 4 files changed, 52 insertions(+), 20 deletions(-) diff --git a/cli/main.cpp b/cli/main.cpp index 1324a3d..0f984c1 100644 --- a/cli/main.cpp +++ b/cli/main.cpp @@ -53,6 +53,7 @@ */ QTextStream out(stdout, QIODevice::WriteOnly); +QTextStream err(stderr, QIODevice::WriteOnly); bool markdown = false; #ifdef PAINTER_OP enum EXPORTFORMAT {TERMINAL, SVG, IMAGE, MARKDOWN, JSON, BOT}; @@ -104,7 +105,7 @@ void displayImage(QString scalarText, QString resultStr,QJsonArray array, bool w out << DisplayToolBox::makeImage( scalarText, resultStr, array, withColor, cmd, comment, allSameFaceCount, allSameColor); } #endif -void displayJSon(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor) +void displayJSon(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount,bool allSameColor) { Q_UNUSED(withColor); QJsonDocument doc; @@ -116,11 +117,12 @@ void displayJSon(QString scalarText, QString resultStr,QJsonArray array, bool wi obj["string"]=resultStr; obj["allSameFace"]=allSameFaceCount; obj["allSameColor"]=allSameColor; + obj["warning"]=warning; obj["command"]=cmd; doc.setObject(obj); out << doc.toJson() << "\n"; } -void displayMarkdown(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor) +void displayMarkdown(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount,bool allSameColor) { Q_UNUSED(withColor); QString str("```Markdown\n"); @@ -130,6 +132,9 @@ void displayMarkdown(QString scalarText, QString resultStr,QJsonArray array, boo } else { + if(!warning.isEmpty()) + str.append(QStringLiteral("Warning: %1\n").arg(warning)); + if(!comment.isEmpty()) { str.prepend(QStringLiteral("%1\n").arg(comment)); @@ -148,7 +153,7 @@ void displayMarkdown(QString scalarText, QString resultStr,QJsonArray array, boo str.append(QStringLiteral("```")); out << str; } -void displaySVG(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor) +void displaySVG(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount,bool allSameColor) { QString str("\n\n"); @@ -158,6 +163,9 @@ void displaySVG(QString scalarText, QString resultStr,QJsonArray array, bool wit } else { + if(!warning.isEmpty()) + str.append(QStringLiteral("%1").arg(warning)); + int y = 20; if(!comment.isEmpty()) { @@ -182,13 +190,18 @@ void displaySVG(QString scalarText, QString resultStr,QJsonArray array, bool wit out << str << "\n"; } -void displayCommandResult(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString comment, bool allSameFaceCount,bool allSameColor) +void displayCommandResult(QString scalarText, QString resultStr,QJsonArray array, bool withColor, QString cmd, QString error, QString warning, QString comment, bool allSameFaceCount,bool allSameColor) { + // TODO display warning if(!error.isEmpty()) { - out << "Error" << error << "\n"; + err << "Error" << error << "\n"; return; } + + if(!warning.isEmpty()) + err << "Warning: "<< warning << "\n"; + QString str; auto diceList = DisplayToolBox::diceToText(array,withColor,allSameFaceCount,allSameColor); @@ -243,6 +256,7 @@ int startDiceParsing(QStringList& cmds,QString& treeFile,bool withColor, EXPORTF QString lastScalarText; QString comment = parser.getComment(); QString error = parser.humanReadableError(); + QString warnings = parser.humanReadableWarning(); QStringList strLst; QStringList listOfDiceResult; @@ -343,17 +357,17 @@ int startDiceParsing(QStringList& cmds,QString& treeFile,bool withColor, EXPORTF switch(format) { case TERMINAL: - displayCommandResult(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor); + displayCommandResult(scalarText, resultStr, array, withColor, cmd, error,warnings, comment, allSameFaceCount, allSameColor); break; case SVG: - displaySVG(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor); + displaySVG(scalarText, resultStr, array, withColor, cmd, error,warnings, comment, allSameFaceCount, allSameColor); break; case BOT: case MARKDOWN: - displayMarkdown(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor); + displayMarkdown(scalarText, resultStr, array, withColor, cmd, error,warnings, comment, allSameFaceCount, allSameColor); break; case JSON: - displayJSon(scalarText, resultStr, array, withColor, cmd, error, comment, allSameFaceCount, allSameColor); + displayJSon(scalarText, resultStr, array, withColor, cmd, error,warnings, comment, allSameFaceCount, allSameColor); break; #ifdef PAINTER_OP case IMAGE: diff --git a/diceparser.cpp b/diceparser.cpp index 2508f41..337efd0 100644 --- a/diceparser.cpp +++ b/diceparser.cpp @@ -169,16 +169,19 @@ bool DiceParser::parseLine(QString str, bool allowAlias) m_command = str; bool hasInstruction = readInstructionList(str); - if((m_errorMap.isEmpty())&&(hasInstruction)) + bool value = hasInstruction; + if(!hasInstruction) { - return true; + m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " + "https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md")); } - else + else if(hasInstruction && !str.isEmpty()) { - m_errorMap.insert(ExecutionNode::NOTHING_UNDERSTOOD,QObject::tr("Nothing was understood. To roll dice: !1d6 - full documation: " - "https://github.com/Rolisteam/DiceParser/blob/master/HelpMe.md")); + auto i = m_command.size()-str.size(); + m_warningMap.insert(ExecutionNode::UNEXPECTED_CHARACTER,QObject::tr("Unexpected character at %1 - end of command was ignored \"%2\"").arg(i).arg(str)); } - return false; + + return value; } bool DiceParser::readExpression(QString& str,ExecutionNode* & node) @@ -253,8 +256,6 @@ bool DiceParser::readExpression(QString& str,ExecutionNode* & node) { NumberNode* numberNode=new NumberNode(); numberNode->setNumber(1); - ExecutionNode* previous = diceNode->getPreviousNode(); - numberNode->setPreviousNode(previous); numberNode->setNextNode(diceNode); node = numberNode; return true; @@ -860,12 +861,16 @@ bool DiceParser::readInstructionList(QString& str) { latest = ParsingToolBox::getLatestNode(latest); keepParsing =!str.isEmpty(); - if(keepParsing) + while(keepParsing) { + auto before = str; ExecutionNode* operatorNode = nullptr; if(readOperator(str,operatorNode)) + { latest->setNextNode(operatorNode); - latest = ParsingToolBox::getLatestNode(latest); + latest = ParsingToolBox::getLatestNode(latest); + } + keepParsing = (!str.isEmpty() & (before!=str)); } } if( !str.isEmpty() && readInstructionOperator(str[0])) @@ -1348,6 +1353,18 @@ QString DiceParser::humanReadableError() return str; } +QString DiceParser::humanReadableWarning() +{ + QMapIterator i(m_warningMap); + QString str(""); + while (i.hasNext()) + { + i.next(); + str.append(i.value()); + str.append(QStringLiteral("\n")); + } + return str; +} void DiceParser::writeDownDotTree(QString filepath) { diff --git a/diceparser.h b/diceparser.h index be0dacd..6197e6b 100644 --- a/diceparser.h +++ b/diceparser.h @@ -220,6 +220,7 @@ public: bool readOptionFromNull(QString &str, ExecutionNode *&node); bool readInstructionList(QString &str); void getDiceResultFromAllInstruction(QList &resultList); + QString humanReadableWarning(); protected: bool readParameterNode(QString &str, ExecutionNode *&node); private: diff --git a/node/executionnode.h b/node/executionnode.h index 796831d..53ea83b 100644 --- a/node/executionnode.h +++ b/node/executionnode.h @@ -18,7 +18,7 @@ public: NOTHING_UNDERSTOOD, NO_DICE_TO_ROLL, TOO_MANY_DICE,NO_VARIBALE, - INVALID_INDEX}; + INVALID_INDEX,UNEXPECTED_CHARACTER}; /** * @brief ExecutionNode */ -- cgit v1.2.3-70-g09d2