]> git.cworth.org Git - notmuch/blob - lib/parse-sexp.cc
lib/parse-sexp: handle saved queries
[notmuch] / lib / parse-sexp.cc
1 #include "database-private.h"
2
3 #if HAVE_SFSEXP
4 #include "sexp.h"
5 #include "unicode-util.h"
6
7 /* _sexp is used for file scope symbols to avoid clashing with
8  * definitions from sexp.h */
9
10 typedef enum {
11     SEXP_FLAG_NONE      = 0,
12     SEXP_FLAG_FIELD     = 1 << 0,
13     SEXP_FLAG_BOOLEAN   = 1 << 1,
14     SEXP_FLAG_SINGLE    = 1 << 2,
15     SEXP_FLAG_WILDCARD  = 1 << 3,
16     SEXP_FLAG_REGEX     = 1 << 4,
17     SEXP_FLAG_DO_REGEX  = 1 << 5,
18     SEXP_FLAG_EXPAND    = 1 << 6,
19     SEXP_FLAG_DO_EXPAND = 1 << 7,
20     SEXP_FLAG_ORPHAN    = 1 << 8,
21 } _sexp_flag_t;
22
23 /*
24  * define bitwise operators to hide casts */
25
26 inline _sexp_flag_t
27 operator| (_sexp_flag_t a, _sexp_flag_t b)
28 {
29     return static_cast<_sexp_flag_t>(
30         static_cast<unsigned>(a) | static_cast<unsigned>(b));
31 }
32
33 inline _sexp_flag_t
34 operator& (_sexp_flag_t a, _sexp_flag_t b)
35 {
36     return static_cast<_sexp_flag_t>(
37         static_cast<unsigned>(a) & static_cast<unsigned>(b));
38 }
39
40 typedef struct  {
41     const char *name;
42     Xapian::Query::op xapian_op;
43     Xapian::Query initial;
44     _sexp_flag_t flags;
45 } _sexp_prefix_t;
46
47 static _sexp_prefix_t prefixes[] =
48 {
49     { "and",            Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
50       SEXP_FLAG_NONE },
51     { "attachment",     Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
52       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
53     { "body",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
54       SEXP_FLAG_FIELD },
55     { "from",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
56       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
57     { "folder",         Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
58       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
59     { "id",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
60       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
61     { "infix",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
62       SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
63     { "is",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
64       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
65     { "matching",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
66       SEXP_FLAG_DO_EXPAND },
67     { "mid",            Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
68       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
69     { "mimetype",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
70       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
71     { "not",            Xapian::Query::OP_AND_NOT,      Xapian::Query::MatchAll,
72       SEXP_FLAG_NONE },
73     { "of",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
74       SEXP_FLAG_DO_EXPAND },
75     { "or",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
76       SEXP_FLAG_NONE },
77     { "path",           Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
78       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
79     { "property",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
80       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
81     { "query",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchNothing,
82       SEXP_FLAG_SINGLE | SEXP_FLAG_ORPHAN },
83     { "regex",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
84       SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
85     { "rx",             Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
86       SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
87     { "starts-with",    Xapian::Query::OP_WILDCARD,     Xapian::Query::MatchAll,
88       SEXP_FLAG_SINGLE },
89     { "subject",        Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
90       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
91     { "tag",            Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
92       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
93     { "thread",         Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
94       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
95     { "to",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
96       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
97     { }
98 };
99
100 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
101                                                const _sexp_prefix_t *parent,
102                                                const sexp_t *sx,
103                                                Xapian::Query &output);
104
105 static notmuch_status_t
106 _sexp_combine_query (notmuch_database_t *notmuch,
107                      const _sexp_prefix_t *parent,
108                      Xapian::Query::op operation,
109                      Xapian::Query left,
110                      const sexp_t *sx,
111                      Xapian::Query &output)
112 {
113     Xapian::Query subquery;
114
115     notmuch_status_t status;
116
117     /* if we run out elements, return accumulator */
118
119     if (! sx) {
120         output = left;
121         return NOTMUCH_STATUS_SUCCESS;
122     }
123
124     status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
125     if (status)
126         return status;
127
128     return _sexp_combine_query (notmuch,
129                                 parent,
130                                 operation,
131                                 Xapian::Query (operation, left, subquery),
132                                 sx->next, output);
133 }
134
135 static notmuch_status_t
136 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
137 {
138     Xapian::Utf8Iterator p (phrase);
139     Xapian::Utf8Iterator end;
140     std::vector<std::string> terms;
141
142     while (p != end) {
143         Xapian::Utf8Iterator start;
144         while (p != end && ! Xapian::Unicode::is_wordchar (*p))
145             p++;
146
147         if (p == end)
148             break;
149
150         start = p;
151
152         while (p != end && Xapian::Unicode::is_wordchar (*p))
153             p++;
154
155         if (p != start) {
156             std::string word (start, p);
157             word = Xapian::Unicode::tolower (word);
158             terms.push_back (term_prefix + word);
159         }
160     }
161     output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
162     return NOTMUCH_STATUS_SUCCESS;
163 }
164
165 static notmuch_status_t
166 _sexp_parse_wildcard (notmuch_database_t *notmuch,
167                       const _sexp_prefix_t *parent,
168                       std::string match,
169                       Xapian::Query &output)
170 {
171
172     std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
173
174     if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
175         _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
176         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
177     }
178
179     output = Xapian::Query (Xapian::Query::OP_WILDCARD,
180                             term_prefix + Xapian::Unicode::tolower (match));
181     return NOTMUCH_STATUS_SUCCESS;
182 }
183
184 static notmuch_status_t
185 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
186                       Xapian::Query &output)
187 {
188     Xapian::Stem stem = *(notmuch->stemmer);
189
190     if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
191         std::string term = Xapian::Unicode::tolower (sx->val);
192
193         output = Xapian::Query ("Z" + term_prefix + stem (term));
194         return NOTMUCH_STATUS_SUCCESS;
195     } else {
196         return _sexp_parse_phrase (term_prefix, sx->val, output);
197     }
198
199 }
200
201 notmuch_status_t
202 _sexp_parse_regex (notmuch_database_t *notmuch,
203                    const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
204                    std::string val, Xapian::Query &output)
205 {
206     if (! parent) {
207         _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
208                                prefix->name);
209         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
210     }
211
212     if (! (parent->flags & SEXP_FLAG_REGEX)) {
213         _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
214                                prefix->name, parent->name);
215         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
216     }
217
218     std::string msg; /* ignored */
219
220     return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
221                                      val, output, msg);
222 }
223
224
225 static notmuch_status_t
226 _sexp_expand_query (notmuch_database_t *notmuch,
227                     const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
228                     const sexp_t *sx, Xapian::Query &output)
229 {
230     Xapian::Query subquery;
231     notmuch_status_t status;
232     std::string msg;
233
234     if (! (parent->flags & SEXP_FLAG_EXPAND)) {
235         _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
236         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
237     }
238
239     status = _sexp_combine_query (notmuch, NULL, prefix->xapian_op, prefix->initial, sx, subquery);
240     if (status)
241         return status;
242
243     status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
244     if (status) {
245         _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
246     }
247     return status;
248 }
249
250 static notmuch_status_t
251 _sexp_parse_infix (notmuch_database_t *notmuch, const sexp_t *sx, Xapian::Query &output)
252 {
253     try {
254         output = notmuch->query_parser->parse_query (sx->val, NOTMUCH_QUERY_PARSER_FLAGS);
255     } catch (const Xapian::QueryParserError &error) {
256         _notmuch_database_log (notmuch, "Syntax error in infix query: %s\n", sx->val);
257         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
258     } catch (const Xapian::Error &error) {
259         if (! notmuch->exception_reported) {
260             _notmuch_database_log (notmuch,
261                                    "A Xapian exception occurred parsing query: %s\n",
262                                    error.get_msg ().c_str ());
263             _notmuch_database_log_append (notmuch,
264                                           "Query string was: %s\n",
265                                           sx->val);
266             notmuch->exception_reported = true;
267             return NOTMUCH_STATUS_XAPIAN_EXCEPTION;
268         }
269     }
270     return NOTMUCH_STATUS_SUCCESS;
271 }
272
273 static notmuch_status_t
274 _sexp_parse_header (notmuch_database_t *notmuch, const _sexp_prefix_t *parent,
275                     const sexp_t *sx, Xapian::Query &output)
276 {
277     _sexp_prefix_t user_prefix;
278
279     user_prefix.name = sx->list->val;
280     user_prefix.flags = SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD;
281
282     if (parent) {
283         _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
284                                sx->list->val, parent->name);
285         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
286     }
287
288     parent = &user_prefix;
289
290     return _sexp_combine_query (notmuch, parent, Xapian::Query::OP_AND, Xapian::Query::MatchAll,
291                                 sx->list->next, output);
292 }
293
294 /* Here we expect the s-expression to be a proper list, with first
295  * element defining and operation, or as a special case the empty
296  * list */
297
298 static notmuch_status_t
299 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
300                        Xapian::Query &output)
301 {
302     if (sx->ty == SEXP_VALUE) {
303         std::string term_prefix = parent ? _notmuch_database_prefix (notmuch, parent->name) : "";
304
305         if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
306             return _sexp_parse_wildcard (notmuch, parent, "", output);
307         }
308
309         if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
310             output = Xapian::Query (term_prefix + sx->val);
311             return NOTMUCH_STATUS_SUCCESS;
312         }
313
314         if (parent) {
315             return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
316         } else {
317             Xapian::Query accumulator;
318             for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
319                 if (prefix->flags & SEXP_FLAG_FIELD) {
320                     notmuch_status_t status;
321                     Xapian::Query subquery;
322                     term_prefix = _notmuch_database_prefix (notmuch, prefix->name);
323                     status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
324                     if (status)
325                         return status;
326                     accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
327                 }
328             }
329             output = accumulator;
330             return NOTMUCH_STATUS_SUCCESS;
331         }
332     }
333
334     /* Empty list */
335     if (! sx->list) {
336         output = Xapian::Query::MatchAll;
337         return NOTMUCH_STATUS_SUCCESS;
338     }
339
340     if (sx->list->ty == SEXP_LIST) {
341         _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
342                                sx->list->val);
343         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
344     }
345
346     /* Check for user defined field */
347     if (_notmuch_string_map_get (notmuch->user_prefix, sx->list->val)) {
348         return _sexp_parse_header (notmuch, parent, sx, output);
349     }
350
351     for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
352         if (strcmp (prefix->name, sx->list->val) == 0) {
353             if (prefix->flags & SEXP_FLAG_FIELD) {
354                 if (parent) {
355                     _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
356                                            prefix->name, parent->name);
357                     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
358                 }
359                 parent = prefix;
360             }
361
362             if (parent && (prefix->flags & SEXP_FLAG_ORPHAN)) {
363                 _notmuch_database_log (notmuch, "'%s' not supported inside '%s'\n",
364                                        prefix->name, parent->name);
365                 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
366             }
367
368             if ((prefix->flags & SEXP_FLAG_SINGLE) &&
369                 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
370                 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
371                                        prefix->name);
372                 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
373             }
374
375             if (strcmp (prefix->name, "infix") == 0) {
376                 return _sexp_parse_infix (notmuch, sx->list->next, output);
377             }
378
379             if (strcmp (prefix->name, "query") == 0) {
380                 return _notmuch_query_name_to_query (notmuch, sx->list->next->val, output);
381             }
382
383             if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
384                 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
385
386             if (prefix->flags & SEXP_FLAG_DO_REGEX) {
387                 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
388             }
389
390             if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
391                 return _sexp_expand_query (notmuch, prefix, parent, sx->list->next, output);
392             }
393
394             return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
395                                         sx->list->next, output);
396         }
397     }
398
399     _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
400     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
401 }
402
403 notmuch_status_t
404 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
405                                       Xapian::Query &output)
406 {
407     const sexp_t *sx = NULL;
408     char *buf = talloc_strdup (notmuch, querystr);
409
410     sx = parse_sexp (buf, strlen (querystr));
411     if (! sx) {
412         _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
413         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
414     }
415
416     return _sexp_to_xapian_query (notmuch, NULL, sx, output);
417 }
418 #endif