8 #include "advancedqueryparser.h"
10 #include <QStringList>
14 using namespace Baloo;
16 AdvancedQueryParser::AdvancedQueryParser()
24 bool inQuotes =
false;
26 for (
int i = 0, end = text.
size(); i != end; ++i) {
36 }
else if (inQuotes) {
59 if (((i + 1) < end) && (text.
at(i + 1) ==
QLatin1Char(
'='))) {
78 static void addTermToStack(
QStack<Term>& stack,
const Term& termInConstruction, Term::Operation op)
80 Term &tos = stack.
top();
82 tos = Term(tos, op, termInConstruction);
85 Term AdvancedQueryParser::parse(
const QString& text)
90 Term termInConstruction;
91 bool valueExpected =
false;
98 for (
const QString &token : tokens) {
103 auto property = termInConstruction.value().toString();
104 if (property.isEmpty()) {
105 qDebug() <<
"Binary operator without first argument encountered:" << text;
108 termInConstruction.setProperty(property);
110 termInConstruction.setValue(token);
111 valueExpected =
false;
117 if (!termInConstruction.isEmpty()) {
118 addTermToStack(stack, termInConstruction, ops.
top());
119 termInConstruction = Term();
121 ops.
top() = Term::And;
124 if (!termInConstruction.isEmpty()) {
125 addTermToStack(stack, termInConstruction, ops.
top());
126 termInConstruction = Term();
128 ops.
top() = Term::Or;
133 Term::Comparator comparator = Term::Auto;
137 comparator = Term::Contains;
140 comparator = Term::Equal;
143 if (token.
size() == 1) {
144 comparator = Term::Less;
146 comparator = Term::LessEqual;
151 if (token.
size() == 1) {
152 comparator = Term::Greater;
154 comparator = Term::GreaterEqual;
159 if (!termInConstruction.isEmpty()) {
160 addTermToStack(stack, termInConstruction, ops.
top());
161 ops.
top() = Term::And;
166 termInConstruction = Term();
171 if (stack.
size() > 1) {
173 if (termInConstruction.value().isValid()) {
174 addTermToStack(stack, termInConstruction, ops.
top());
180 addTermToStack(stack, stack.
pop(), ops.
top());
181 ops.
top() = Term::And;
182 termInConstruction = Term();
190 if (comparator != Term::Auto) {
192 termInConstruction.setComparator(comparator);
193 valueExpected =
true;
197 if (!termInConstruction.isEmpty()) {
198 addTermToStack(stack, termInConstruction, ops.
top());
199 ops.
top() = Term::And;
202 termInConstruction = Term(
QString(), token);
207 termInConstruction.setProperty(termInConstruction.value().toString());
208 termInConstruction.setValue(
QString());
209 termInConstruction.setComparator(Term::Contains);
212 if (termInConstruction.value().isValid()) {
213 addTermToStack(stack, termInConstruction, ops.
top());
218 while (stack.
size() > 1) {
221 addTermToStack(stack, stack.
pop(), ops.
top());