]> git.cworth.org Git - notmuch/blob - lib/parse-sexp.cc
lib/parse-sexp: stem unquoted atoms
[notmuch] / lib / parse-sexp.cc
1 #include "database-private.h"
2
3 #if HAVE_SFSEXP
4 #include "sexp.h"
5
6
7 /* _sexp is used for file scope symbols to avoid clashing with
8  * definitions from sexp.h */
9
10 /* Here we expect the s-expression to be a proper list, with first
11  * element defining and operation, or as a special case the empty
12  * list */
13
14 static notmuch_status_t
15 _sexp_to_xapian_query (notmuch_database_t *notmuch, const sexp_t *sx,
16                        Xapian::Query &output)
17 {
18
19     if (sx->ty == SEXP_VALUE) {
20         std::string term = Xapian::Unicode::tolower (sx->val);
21         Xapian::Stem stem = *(notmuch->stemmer);
22         if (sx->aty == SEXP_BASIC)
23             term = "Z" + stem (term);
24
25         output = Xapian::Query (term);
26         return NOTMUCH_STATUS_SUCCESS;
27     }
28
29     /* Empty list */
30     if (! sx->list) {
31         output = Xapian::Query::MatchAll;
32         return NOTMUCH_STATUS_SUCCESS;
33     }
34
35     if (sx->list->ty == SEXP_VALUE)
36         _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
37     else
38         _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
39                                sx->list->val);
40
41     return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
42 }
43
44 notmuch_status_t
45 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
46                                       Xapian::Query &output)
47 {
48     const sexp_t *sx = NULL;
49     char *buf = talloc_strdup (notmuch, querystr);
50
51     sx = parse_sexp (buf, strlen (querystr));
52     if (! sx) {
53         _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
54         return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
55     }
56
57     return _sexp_to_xapian_query (notmuch, sx, output);
58 }
59 #endif