| 1 |
/**************************************************************************************** |
| 2 |
* Copyright (c) 2006 Gbor Lehel <illissius@gmail.com> * |
| 3 |
* * |
| 4 |
* This program is free software; you can redistribute it and/or modify it under * |
| 5 |
* the terms of the GNU General Public License as published by the Free Software * |
| 6 |
* Foundation; either version 2 of the License, or (at your option) any later * |
| 7 |
* version. * |
| 8 |
* * |
| 9 |
* This program is distributed in the hope that it will be useful, but WITHOUT ANY * |
| 10 |
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A * |
| 11 |
* PARTICULAR PURPOSE. See the GNU General Public License for more details. * |
| 12 |
* * |
| 13 |
* You should have received a copy of the GNU General Public License along with * |
| 14 |
* this program. If not, see <http://www.gnu.org/licenses/>. * |
| 15 |
****************************************************************************************/ |
| 16 |
|
| 17 |
#ifndef AMAROK_EXPRESSION_H |
| 18 |
#define AMAROK_EXPRESSION_H |
| 19 |
|
| 20 |
#include <QList> |
| 21 |
#include <QString> |
| 22 |
|
| 23 |
struct expression_element |
| 24 |
{ |
| 25 |
QString field; |
| 26 |
QString text; |
| 27 |
bool negate: 1; |
| 28 |
enum { Contains, Less, More } match: 2; |
| 29 |
expression_element(): negate( false ), match( Contains ) { } |
| 30 |
}; |
| 31 |
typedef QList<expression_element> or_list; |
| 32 |
|
| 33 |
typedef QList<or_list> ParsedExpression; |
| 34 |
|
| 35 |
class ExpressionParser |
| 36 |
{ |
| 37 |
public: |
| 38 |
ExpressionParser( const QString &expression ); |
| 39 |
ParsedExpression parse(); |
| 40 |
static ParsedExpression parse( const QString &expression ); |
| 41 |
|
| 42 |
static bool isAdvancedExpression( const QString &expression ); |
| 43 |
|
| 44 |
private: |
| 45 |
void parseChar( const QChar &c ); |
| 46 |
void handleSpace( const QChar &c ); |
| 47 |
void handleMinus( const QChar &c ); |
| 48 |
void handleColon( const QChar &c ); |
| 49 |
void handleMod( const QChar &c ); |
| 50 |
void handleQuote( const QChar &c ); |
| 51 |
void handleChar( const QChar &c ); |
| 52 |
void finishedToken(); |
| 53 |
void finishedElement(); |
| 54 |
void finishedOrGroup(); |
| 55 |
|
| 56 |
const QString &m_expression; |
| 57 |
enum State { ExpectMinus, ExpectField, ExpectMod, ExpectText }; |
| 58 |
int m_state; |
| 59 |
bool m_haveGroup; |
| 60 |
bool m_inQuote; |
| 61 |
bool m_inOrGroup; |
| 62 |
QString m_string; |
| 63 |
expression_element m_element; |
| 64 |
or_list m_or; |
| 65 |
ParsedExpression m_parsed; |
| 66 |
}; |
| 67 |
|
| 68 |
|
| 69 |
#endif |