]> git.notmuchmail.org Git - notmuch/blob - lib/parse-sexp.cc
9f6e0b77f268c19e63e3cb026a5ace2453dccf0f
[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_t;
21
22 /*
23  * define bitwise operators to hide casts */
24
25 inline _sexp_flag_t
26 operator| (_sexp_flag_t a, _sexp_flag_t b)
27 {
28     return static_cast<_sexp_flag_t>(
29         static_cast<unsigned>(a) | static_cast<unsigned>(b));
30 }
31
32 inline _sexp_flag_t
33 operator& (_sexp_flag_t a, _sexp_flag_t b)
34 {
35     return static_cast<_sexp_flag_t>(
36         static_cast<unsigned>(a) & static_cast<unsigned>(b));
37 }
38
39 typedef struct  {
40     const char *name;
41     Xapian::Query::op xapian_op;
42     Xapian::Query initial;
43     _sexp_flag_t flags;
44 } _sexp_prefix_t;
45
46 static _sexp_prefix_t prefixes[] =
47 {
48     { "and",            Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
49       SEXP_FLAG_NONE },
50     { "attachment",     Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
51       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
52     { "body",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
53       SEXP_FLAG_FIELD },
54     { "from",           Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
55       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
56     { "folder",         Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
57       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
58     { "id",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
59       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
60     { "is",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
61       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
62     { "matching",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
63       SEXP_FLAG_DO_EXPAND },
64     { "mid",            Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
65       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
66     { "mimetype",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
67       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
68     { "not",            Xapian::Query::OP_AND_NOT,      Xapian::Query::MatchAll,
69       SEXP_FLAG_NONE },
70     { "of",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
71       SEXP_FLAG_DO_EXPAND },
72     { "or",             Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
73       SEXP_FLAG_NONE },
74     { "path",           Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
75       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX },
76     { "property",       Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
77       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
78     { "regex",          Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
79       SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
80     { "rx",             Xapian::Query::OP_INVALID,      Xapian::Query::MatchAll,
81       SEXP_FLAG_SINGLE | SEXP_FLAG_DO_REGEX },
82     { "starts-with",    Xapian::Query::OP_WILDCARD,     Xapian::Query::MatchAll,
83       SEXP_FLAG_SINGLE },
84     { "subject",        Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
85       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
86     { "tag",            Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
87       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
88     { "thread",         Xapian::Query::OP_OR,           Xapian::Query::MatchNothing,
89       SEXP_FLAG_FIELD | SEXP_FLAG_BOOLEAN | SEXP_FLAG_WILDCARD | SEXP_FLAG_REGEX | SEXP_FLAG_EXPAND },
90     { "to",             Xapian::Query::OP_AND,          Xapian::Query::MatchAll,
91       SEXP_FLAG_FIELD | SEXP_FLAG_WILDCARD | SEXP_FLAG_EXPAND },
92     { }
93 };
94
95 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
96                                                const _sexp_prefix_t *parent,
97                                                const sexp_t *sx,
98                                                Xapian::Query &output);
99
100 static notmuch_status_t
101 _sexp_combine_query (notmuch_database_t *notmuch,
102                      const _sexp_prefix_t *parent,
103                      Xapian::Query::op operation,
104                      Xapian::Query left,
105                      const sexp_t *sx,
106                      Xapian::Query &output)
107 {
108     Xapian::Query subquery;
109
110     notmuch_status_t status;
111
112     /* if we run out elements, return accumulator */
113
114     if (! sx) {
115         output = left;
116         return NOTMUCH_STATUS_SUCCESS;
117     }
118
119     status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
120     if (status)
121         return status;
122
123     return _sexp_combine_query (notmuch,
124                                 parent,
125                                 operation,
126                                 Xapian::Query (operation, left, subquery),
127                                 sx->next, output);
128 }
129
130 static notmuch_status_t
131 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
132 {
133     Xapian::Utf8Iterator p (phrase);
134     Xapian::Utf8Iterator end;
135     std::vector<std::string> terms;
136
137     while (p != end) {
138         Xapian::Utf8Iterator start;
139         while (p != end && ! Xapian::Unicode::is_wordchar (*p))
140             p++;
141
142         if (p == end)
143             break;
144
145         start = p;
146
147         while (p != end && Xapian::Unicode::is_wordchar (*p))
148             p++;
149
150         if (p != start) {
151             std::string word (start, p);
152             word = Xapian::Unicode::tolower (word);
153             terms.push_back (term_prefix + word);
154         }
155     }
156     output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
157     return NOTMUCH_STATUS_SUCCESS;
158 }
159
160 static notmuch_status_t
161 _sexp_parse_wildcard (notmuch_database_t *notmuch,
162                       const _sexp_prefix_t *parent,
163                       std::string match,
164                       Xapian::Query &output)
165 {
166
167     std::string term_prefix = parent ? _find_prefix (parent->name) : "";
168
169     if (parent && ! (parent->flags & SEXP_FLAG_WILDCARD)) {
170         _notmuch_database_log (notmuch, "'%s' does not support wildcard queries\n", parent->name);
171         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
172     }
173
174     output = Xapian::Query (Xapian::Query::OP_WILDCARD,
175                             term_prefix + Xapian::Unicode::tolower (match));
176     return NOTMUCH_STATUS_SUCCESS;
177 }
178
179 static notmuch_status_t
180 _sexp_parse_one_term (notmuch_database_t *notmuch, std::string term_prefix, const sexp_t *sx,
181                       Xapian::Query &output)
182 {
183     Xapian::Stem stem = *(notmuch->stemmer);
184
185     if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
186         std::string term = Xapian::Unicode::tolower (sx->val);
187
188         output = Xapian::Query ("Z" + term_prefix + stem (term));
189         return NOTMUCH_STATUS_SUCCESS;
190     } else {
191         return _sexp_parse_phrase (term_prefix, sx->val, output);
192     }
193
194 }
195
196 notmuch_status_t
197 _sexp_parse_regex (notmuch_database_t *notmuch,
198                    const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
199                    std::string val, Xapian::Query &output)
200 {
201     if (! parent) {
202         _notmuch_database_log (notmuch, "illegal '%s' outside field\n",
203                                prefix->name);
204         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
205     }
206
207     if (! (parent->flags & SEXP_FLAG_REGEX)) {
208         _notmuch_database_log (notmuch, "'%s' not supported in field '%s'\n",
209                                prefix->name, parent->name);
210         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
211     }
212
213     std::string msg; /* ignored */
214
215     return _notmuch_regexp_to_query (notmuch, Xapian::BAD_VALUENO, parent->name,
216                                      val, output, msg);
217 }
218
219
220 static notmuch_status_t
221 _sexp_expand_query (notmuch_database_t *notmuch,
222                     const _sexp_prefix_t *prefix, const _sexp_prefix_t *parent,
223                     const sexp_t *sx, Xapian::Query &output)
224 {
225     Xapian::Query subquery;
226     notmuch_status_t status;
227     std::string msg;
228
229     if (! (parent->flags & SEXP_FLAG_EXPAND)) {
230         _notmuch_database_log (notmuch, "'%s' unsupported inside '%s'\n", prefix->name, parent->name);
231         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
232     }
233
234     status = _sexp_combine_query (notmuch, NULL, prefix->xapian_op, prefix->initial, sx, subquery);
235     if (status)
236         return status;
237
238     status = _notmuch_query_expand (notmuch, parent->name, subquery, output, msg);
239     if (status) {
240         _notmuch_database_log (notmuch, "error expanding query %s\n", msg.c_str ());
241     }
242     return status;
243 }
244
245 /* Here we expect the s-expression to be a proper list, with first
246  * element defining and operation, or as a special case the empty
247  * list */
248
249 static notmuch_status_t
250 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
251                        Xapian::Query &output)
252 {
253     if (sx->ty == SEXP_VALUE) {
254         std::string term_prefix = parent ? _find_prefix (parent->name) : "";
255
256         if (sx->aty == SEXP_BASIC && strcmp (sx->val, "*") == 0) {
257             return _sexp_parse_wildcard (notmuch, parent, "", output);
258         }
259
260         if (parent && (parent->flags & SEXP_FLAG_BOOLEAN)) {
261             output = Xapian::Query (term_prefix + sx->val);
262             return NOTMUCH_STATUS_SUCCESS;
263         }
264         if (parent) {
265             return _sexp_parse_one_term (notmuch, term_prefix, sx, output);
266         } else {
267             Xapian::Query accumulator;
268             for (_sexp_prefix_t *prefix = prefixes; prefix->name; prefix++) {
269                 if (prefix->flags & SEXP_FLAG_FIELD) {
270                     notmuch_status_t status;
271                     Xapian::Query subquery;
272                     term_prefix = _find_prefix (prefix->name);
273                     status = _sexp_parse_one_term (notmuch, term_prefix, sx, subquery);
274                     if (status)
275                         return status;
276                     accumulator = Xapian::Query (Xapian::Query::OP_OR, accumulator, subquery);
277                 }
278             }
279             output = accumulator;
280             return NOTMUCH_STATUS_SUCCESS;
281         }
282     }
283
284     /* Empty list */
285     if (! sx->list) {
286         output = Xapian::Query::MatchAll;
287         return NOTMUCH_STATUS_SUCCESS;
288     }
289
290     if (sx->list->ty == SEXP_LIST) {
291         _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
292                                sx->list->val);
293         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
294     }
295
296     for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
297         if (strcmp (prefix->name, sx->list->val) == 0) {
298             if (prefix->flags & SEXP_FLAG_FIELD) {
299                 if (parent) {
300                     _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
301                                            prefix->name, parent->name);
302                     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
303                 }
304                 parent = prefix;
305             }
306
307             if ((prefix->flags & SEXP_FLAG_SINGLE) &&
308                 (! sx->list->next || sx->list->next->next || sx->list->next->ty != SEXP_VALUE)) {
309                 _notmuch_database_log (notmuch, "'%s' expects single atom as argument\n",
310                                        prefix->name);
311                 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
312             }
313
314             if (prefix->xapian_op == Xapian::Query::OP_WILDCARD)
315                 return _sexp_parse_wildcard (notmuch, parent, sx->list->next->val, output);
316
317             if (prefix->flags & SEXP_FLAG_DO_REGEX) {
318                 return _sexp_parse_regex (notmuch, prefix, parent, sx->list->next->val, output);
319             }
320
321             if (prefix->flags & SEXP_FLAG_DO_EXPAND) {
322                 return _sexp_expand_query (notmuch, prefix, parent, sx->list->next, output);
323             }
324
325             return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
326                                         sx->list->next, output);
327         }
328     }
329
330     _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
331
332     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
333 }
334
335 notmuch_status_t
336 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
337                                       Xapian::Query &output)
338 {
339     const sexp_t *sx = NULL;
340     char *buf = talloc_strdup (notmuch, querystr);
341
342     sx = parse_sexp (buf, strlen (querystr));
343     if (! sx) {
344         _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
345         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
346     }
347
348     return _sexp_to_xapian_query (notmuch, NULL, sx, output);
349 }
350 #endif