diff options
-rw-r--r-- | akonadish/syntaxtree.cpp | 44 |
1 files changed, 42 insertions, 2 deletions
diff --git a/akonadish/syntaxtree.cpp b/akonadish/syntaxtree.cpp index 4188e5f..495ad22 100644 --- a/akonadish/syntaxtree.cpp +++ b/akonadish/syntaxtree.cpp | |||
@@ -170,7 +170,47 @@ Syntax::List SyntaxTree::nearestSyntax(const QStringList &words, const QString & | |||
170 | 170 | ||
171 | QStringList SyntaxTree::tokenize(const QString &text) | 171 | QStringList SyntaxTree::tokenize(const QString &text) |
172 | { | 172 | { |
173 | //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"'] | 173 | //TODO: properly tokenize (e.g. "foo bar" should not become ['"foo', 'bar"']a |
174 | return text.split(" "); | 174 | static const QVector<QChar> quoters = QVector<QChar>() << '"' << '\''; |
175 | QStringList tokens; | ||
176 | QString acc; | ||
177 | QChar closer; | ||
178 | for (int i = 0; i < text.size(); ++i) { | ||
179 | const QChar c = text.at(i); | ||
180 | if (c == '\\') { | ||
181 | ++i; | ||
182 | if (i < text.size()) { | ||
183 | acc.append(text.at(i)); | ||
184 | } | ||
185 | } else if (!closer.isNull()) { | ||
186 | if (c == closer) { | ||
187 | acc = acc.trimmed(); | ||
188 | if (!acc.isEmpty()) { | ||
189 | tokens << acc; | ||
190 | } | ||
191 | acc.clear(); | ||
192 | closer = QChar(); | ||
193 | } else { | ||
194 | acc.append(c); | ||
195 | } | ||
196 | } else if (c.isSpace()) { | ||
197 | acc = acc.trimmed(); | ||
198 | if (!acc.isEmpty()) { | ||
199 | tokens << acc; | ||
200 | } | ||
201 | acc.clear(); | ||
202 | } else if (quoters.contains(c)) { | ||
203 | closer = c; | ||
204 | } else { | ||
205 | acc.append(c); | ||
206 | } | ||
207 | } | ||
208 | |||
209 | acc = acc.trimmed(); | ||
210 | if (!acc.isEmpty()) { | ||
211 | tokens << acc; | ||
212 | } | ||
213 | |||
214 | return tokens; | ||
175 | } | 215 | } |
176 | 216 | ||