A simple parser supporting main arithmetic operations and parentheses. More...
#include <simpleparser.h>
Public Member Functions | |
SimpleParser () | |
QVariant | operator() (QString expression) const |
A simple parser supporting main arithmetic operations and parentheses.
This is an extension of the simple parser found in Blanchette, J., C++ GUI Programming with Qt 4, Second Edition. Currently it is extended with the int() function which removes the fractional part.
SimpleParser::SimpleParser | ( | ) |
{};
Implements Parser.
{ static const QRegExp regExp("\\s||\\f||\\n"); QRegExp intReg("int\\(([\\d,\\.,\\,,\\+,\\-,\\/,\\*]+)\\)"); //probleem is dat je hiermee binnen int() geen haken kan gebruiken! // If there are unequal number of parentheses, then the parser might crash, // so here we check that first. if (expression.count("(") != expression.count(")")) return invalid(); // int conversion implementation while (expression.contains(intReg)) { bool ok; QVariant eval = (*this)(intReg.cap(1)); if (eval == invalid()) return QVariant(); expression.replace(intReg.cap(0), QString::number((int) eval.toDouble(&ok))); Q_ASSERT(ok); } int pos = 0; expression.replace(regExp, ""); expression.append(QChar::Null); QVariant r = evalExpression(expression, pos); if (expression[pos] != QChar::Null) return invalid(); return r; }