Public Member Functions

SimpleParser Class Reference

A simple parser supporting main arithmetic operations and parentheses. More...

#include <simpleparser.h>

Inheritance diagram for SimpleParser:
Inheritance graph
[legend]
Collaboration diagram for SimpleParser:
Collaboration graph
[legend]

List of all members.

Public Member Functions

 SimpleParser ()
QVariant operator() (QString expression) const

Detailed Description

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.


Constructor & Destructor Documentation

SimpleParser::SimpleParser (  ) 

{};


Member Function Documentation

QVariant SimpleParser::operator() ( QString  expression  )  const [virtual]

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;
}


The documentation for this class was generated from the following files: