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 #include <utility>
24 
25 struct expression_element
26 {
27     QString field;
28     QString text;
29     bool negate: 1;
30     enum { Contains, Equals, Less, More } match: 2;
expression_elementexpression_element31     expression_element(): negate( false ), match( Contains ) { }
32 
33     // workaround for gcc bug 56235
34     expression_element(const expression_element&) = default;
35     expression_element& operator=(const expression_element&) = default;
expression_elementexpression_element36     expression_element(expression_element&& o)
37         : field(std::move(o.field))
38         , text(std::move(o.text))
39         , negate(o.negate)
40         , match(o.match)
41         {}
42     expression_element& operator=(expression_element&& o)
43     {
44         field = std::move(o.field);
45         text = std::move(o.text);
46         negate = o.negate;
47         match = o.match;
48         return *this;
49     }
50 };
51 typedef QList<expression_element> or_list;
52 
53 typedef QList<or_list> ParsedExpression;
54 
55 class ExpressionParser
56 {
57     public:
58         explicit ExpressionParser( const QString &expression );
59         ParsedExpression parse();
60         static ParsedExpression parse( const QString &expression );
61 
62         static bool isAdvancedExpression( const QString &expression );
63 
64     private:
65         void parseChar(   const QChar &c );
66         void handleSpace( const QChar &c );
67         void handleMinus( const QChar &c );
68         void handleColon( const QChar &c );
69         void handleMod(   const QChar &c );
70         void handleQuote( const QChar &c );
71         void handleChar(  const QChar &c );
72         void finishedToken();
73         void finishedElement();
74         void finishedOrGroup();
75 
76         const QString &m_expression;
77         enum State { ExpectMinus, ExpectField, ExpectMod, ExpectText };
78         int m_state;
79         bool m_haveGroup;
80         bool m_inQuote;
81         bool m_inOrGroup;
82         QString m_string;
83         expression_element m_element;
84         or_list m_or;
85         ParsedExpression m_parsed;
86 };
87 
88 
89 #endif
90