File indexing completed on 2024-05-19 04:49:16

0001 /****************************************************************************************
0002  * Copyright (c) 2006 Gbor Lehel <illissius@gmail.com>                                  *
0003  *                                                                                      *
0004  * This program is free software; you can redistribute it and/or modify it under        *
0005  * the terms of the GNU General Public License as published by the Free Software        *
0006  * Foundation; either version 2 of the License, or (at your option) any later           *
0007  * version.                                                                             *
0008  *                                                                                      *
0009  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0010  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0011  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0012  *                                                                                      *
0013  * You should have received a copy of the GNU General Public License along with         *
0014  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0015  ****************************************************************************************/
0016 
0017 #ifndef AMAROK_EXPRESSION_H
0018 #define AMAROK_EXPRESSION_H
0019 
0020 #include <QList>
0021 #include <QString>
0022 
0023 #include <utility>
0024 
0025 struct expression_element
0026 {
0027     QString field;
0028     QString text;
0029     bool negate: 1;
0030     enum { Contains, Equals, Less, More } match: 2;
0031     expression_element(): negate( false ), match( Contains ) { }
0032 
0033     // workaround for gcc bug 56235
0034     expression_element(const expression_element&) = default;
0035     expression_element& operator=(const expression_element&) = default;
0036     expression_element(expression_element&& o)
0037         : field(std::move(o.field))
0038         , text(std::move(o.text))
0039         , negate(o.negate)
0040         , match(o.match)
0041         {}
0042     expression_element& operator=(expression_element&& o)
0043     {
0044         field = std::move(o.field);
0045         text = std::move(o.text);
0046         negate = o.negate;
0047         match = o.match;
0048         return *this;
0049     }
0050 };
0051 typedef QList<expression_element> or_list;
0052 
0053 typedef QList<or_list> ParsedExpression;
0054 
0055 class ExpressionParser
0056 {
0057     public:
0058         explicit ExpressionParser( const QString &expression );
0059         ParsedExpression parse();
0060         static ParsedExpression parse( const QString &expression );
0061 
0062         static bool isAdvancedExpression( const QString &expression );
0063 
0064     private:
0065         void parseChar(   const QChar &c );
0066         void handleSpace( const QChar &c );
0067         void handleMinus( const QChar &c );
0068         void handleColon( const QChar &c );
0069         void handleMod(   const QChar &c );
0070         void handleQuote( const QChar &c );
0071         void handleChar(  const QChar &c );
0072         void finishedToken();
0073         void finishedElement();
0074         void finishedOrGroup();
0075 
0076         const QString &m_expression;
0077         enum State { ExpectMinus, ExpectField, ExpectMod, ExpectText };
0078         int m_state;
0079         bool m_haveGroup;
0080         bool m_inQuote;
0081         bool m_inOrGroup;
0082         QString m_string;
0083         expression_element m_element;
0084         or_list m_or;
0085         ParsedExpression m_parsed;
0086 };
0087 
0088 
0089 #endif