~ubuntu-branches/ubuntu/wily/kid3/wily-proposed

« back to all changes in this revision

Viewing changes to kid3/expressionparser.h

  • Committer: Package Import Robot
  • Author(s): Ana Beatriz Guerrero Lopez, Patrick Matthäi, Ana Beatriz Guerrero Lopez
  • Date: 2011-11-13 16:34:13 UTC
  • mfrom: (1.1.13) (2.1.11 sid)
  • Revision ID: package-import@ubuntu.com-20111113163413-5y0anlc4dqf511uh
Tags: 2.0.1-1
* New upstream release.

[ Patrick Matthäi ]
* Adjust build system.
* Add build dependency xsltproc.

[ Ana Beatriz Guerrero Lopez ]
* Some more adjustments to the build system taken from upstream's deb/
* directory.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/**
2
 
 * \file expressionparser.h
3
 
 * Simple parser for expressions.
4
 
 *
5
 
 * \b Project: Kid3
6
 
 * \author Urs Fleisch
7
 
 * \date 23 Jan 2008
8
 
 *
9
 
 * Copyright (C) 2008  Urs Fleisch
10
 
 *
11
 
 * This file is part of Kid3.
12
 
 *
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.
17
 
 *
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.
22
 
 *
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/>.
25
 
 */
26
 
 
27
 
#ifndef EXPRESSIONPARSER_H
28
 
#define EXPRESSIONPARSER_H
29
 
 
30
 
#include <qstring.h>
31
 
#include <qstringlist.h>
32
 
 
33
 
/**
34
 
 * Simple parser for expressions with boolean not, and, or and
35
 
 * other binary operations.
36
 
 */
37
 
class ExpressionParser {
38
 
public:
39
 
        /**
40
 
         * Constructor.
41
 
         *
42
 
         * @param operators additional operators (besides not, and, or),
43
 
         *                  highest priority first
44
 
         */
45
 
        ExpressionParser(QStringList operators);
46
 
 
47
 
        /**
48
 
         * Destructor.
49
 
         */
50
 
        ~ExpressionParser();
51
 
 
52
 
        /**
53
 
         * Tokenize an expression in reverse polish notation.
54
 
         *
55
 
         * @param expr with strings, operators, not, and, or, (, ).
56
 
         */
57
 
        void tokenizeRpn(const QString& expr);
58
 
 
59
 
        /**
60
 
         * Clear the variable stack before restarting an evaluation.
61
 
         */
62
 
        void clearEvaluation();
63
 
 
64
 
        /**
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.
70
 
         *
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
74
 
         *
75
 
         * @return true if the RPN stack has more to evaluate,
76
 
         *         if false, the evaluation is finished.
77
 
         */
78
 
        bool evaluate(QString& op, QString& var1, QString& var2);
79
 
 
80
 
        /**
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.
84
 
         *
85
 
         * @param var boolean to  push
86
 
         */
87
 
        void pushBool(bool var);
88
 
 
89
 
        /**
90
 
         * Check if an error occurred.
91
 
         * @return true if an error occurred.
92
 
         */
93
 
        bool hasError() const { return m_error; }
94
 
 
95
 
        /**
96
 
         * Pop a boolean from the variable stack.
97
 
         * Can be used to get the result after evaluate() returns false and
98
 
         * no error occurred.
99
 
         *
100
 
         * @param var the boolean is returned here
101
 
         *
102
 
         * @return true if ok.
103
 
         */
104
 
        bool popBool(bool& var);
105
 
 
106
 
private:
107
 
        /**
108
 
         * Get the next token from the RPN stack.
109
 
         *
110
 
         * @return token, QString::null if stack is empty.
111
 
         */
112
 
        QString getToken();
113
 
 
114
 
        /**
115
 
         * Compare operator priority.
116
 
         *
117
 
         * @return true if op1 has less priority than op2.
118
 
         */
119
 
        bool lessPriority(const QString& op1, const QString& op2) const;
120
 
 
121
 
        /**
122
 
         * Pop two booleans from the variable stack.
123
 
         *
124
 
         * @param var1 first boolean
125
 
         * @param var2 second boolean
126
 
         *
127
 
         * @return true if ok.
128
 
         */
129
 
        bool popTwoBools(bool& var1, bool& var2);
130
 
 
131
 
        QStringList m_rpnStack;
132
 
        QStringList m_varStack;
133
 
        const QStringList m_operators;
134
 
        QStringList::const_iterator m_rpnIterator;
135
 
        bool m_error;
136
 
};
137
 
 
138
 
#endif