2
* \file expressionparser.h
3
* Simple parser for expressions.
9
* Copyright (C) 2008 Urs Fleisch
11
* This file is part of Kid3.
13
* Kid3 is free software; you can redistribute it and/or modify
14
* it under the terms of the GNU General Public License as published by
15
* the Free Software Foundation; either version 2 of the License, or
16
* (at your option) any later version.
18
* Kid3 is distributed in the hope that it will be useful,
19
* but WITHOUT ANY WARRANTY; without even the implied warranty of
20
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21
* GNU General Public License for more details.
23
* You should have received a copy of the GNU General Public License
24
* along with this program. If not, see <http://www.gnu.org/licenses/>.
27
#ifndef EXPRESSIONPARSER_H
28
#define EXPRESSIONPARSER_H
31
#include <qstringlist.h>
34
* Simple parser for expressions with boolean not, and, or and
35
* other binary operations.
37
class ExpressionParser {
42
* @param operators additional operators (besides not, and, or),
43
* highest priority first
45
ExpressionParser(QStringList operators);
53
* Tokenize an expression in reverse polish notation.
55
* @param expr with strings, operators, not, and, or, (, ).
57
void tokenizeRpn(const QString& expr);
60
* Clear the variable stack before restarting an evaluation.
62
void clearEvaluation();
65
* Evaluate the RPN stack.
66
* Boolean operations and, or, not are performed automatically. If another
67
* operation has to be performed, the method stops and returns operator
68
* and variables. The result can then be pushed onto the stack using
69
* pushBool() and then the method can be called again.
71
* @param op the operator is returned here
72
* @param var1 the first variable is returned here
73
* @param var2 the second variable is returned here
75
* @return true if the RPN stack has more to evaluate,
76
* if false, the evaluation is finished.
78
bool evaluate(QString& op, QString& var1, QString& var2);
81
* Push a boolean to the variable stack.
82
* Can be used to push the result of the operation returned by
83
* evaluate() back onto the variable stack.
85
* @param var boolean to push
87
void pushBool(bool var);
90
* Check if an error occurred.
91
* @return true if an error occurred.
93
bool hasError() const { return m_error; }
96
* Pop a boolean from the variable stack.
97
* Can be used to get the result after evaluate() returns false and
100
* @param var the boolean is returned here
102
* @return true if ok.
104
bool popBool(bool& var);
108
* Get the next token from the RPN stack.
110
* @return token, QString::null if stack is empty.
115
* Compare operator priority.
117
* @return true if op1 has less priority than op2.
119
bool lessPriority(const QString& op1, const QString& op2) const;
122
* Pop two booleans from the variable stack.
124
* @param var1 first boolean
125
* @param var2 second boolean
127
* @return true if ok.
129
bool popTwoBools(bool& var1, bool& var2);
131
QStringList m_rpnStack;
132
QStringList m_varStack;
133
const QStringList m_operators;
134
QStringList::const_iterator m_rpnIterator;