1 #include "database-private.h"
5 #include "unicode-util.h"
7 /* _sexp is used for file scope symbols to avoid clashing with
8 * definitions from sexp.h */
12 SEXP_FLAG_FIELD = 1 << 0,
17 Xapian::Query::op xapian_op;
18 Xapian::Query initial;
22 static _sexp_prefix_t prefixes[] =
24 { "and", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
26 { "not", Xapian::Query::OP_AND_NOT, Xapian::Query::MatchAll,
28 { "or", Xapian::Query::OP_OR, Xapian::Query::MatchNothing,
30 { "subject", Xapian::Query::OP_AND, Xapian::Query::MatchAll,
35 static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch,
36 const _sexp_prefix_t *parent,
38 Xapian::Query &output);
40 static notmuch_status_t
41 _sexp_combine_query (notmuch_database_t *notmuch,
42 const _sexp_prefix_t *parent,
43 Xapian::Query::op operation,
46 Xapian::Query &output)
48 Xapian::Query subquery;
50 notmuch_status_t status;
52 /* if we run out elements, return accumulator */
56 return NOTMUCH_STATUS_SUCCESS;
59 status = _sexp_to_xapian_query (notmuch, parent, sx, subquery);
63 return _sexp_combine_query (notmuch,
66 Xapian::Query (operation, left, subquery),
70 static notmuch_status_t
71 _sexp_parse_phrase (std::string term_prefix, const char *phrase, Xapian::Query &output)
73 Xapian::Utf8Iterator p (phrase);
74 Xapian::Utf8Iterator end;
75 std::vector<std::string> terms;
78 Xapian::Utf8Iterator start;
79 while (p != end && ! Xapian::Unicode::is_wordchar (*p))
87 while (p != end && Xapian::Unicode::is_wordchar (*p))
91 std::string word (start, p);
92 word = Xapian::Unicode::tolower (word);
93 terms.push_back (term_prefix + word);
96 output = Xapian::Query (Xapian::Query::OP_PHRASE, terms.begin (), terms.end ());
97 return NOTMUCH_STATUS_SUCCESS;
100 /* Here we expect the s-expression to be a proper list, with first
101 * element defining and operation, or as a special case the empty
104 static notmuch_status_t
105 _sexp_to_xapian_query (notmuch_database_t *notmuch, const _sexp_prefix_t *parent, const sexp_t *sx,
106 Xapian::Query &output)
109 if (sx->ty == SEXP_VALUE) {
110 std::string term = Xapian::Unicode::tolower (sx->val);
111 Xapian::Stem stem = *(notmuch->stemmer);
112 std::string term_prefix = parent ? _find_prefix (parent->name) : "";
113 if (sx->aty == SEXP_BASIC && unicode_word_utf8 (sx->val)) {
114 output = Xapian::Query ("Z" + term_prefix + stem (term));
115 return NOTMUCH_STATUS_SUCCESS;
117 return _sexp_parse_phrase (term_prefix, sx->val, output);
123 output = Xapian::Query::MatchAll;
124 return NOTMUCH_STATUS_SUCCESS;
127 if (sx->list->ty == SEXP_LIST) {
128 _notmuch_database_log (notmuch, "unexpected list in field/operation position\n",
130 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
133 for (_sexp_prefix_t *prefix = prefixes; prefix && prefix->name; prefix++) {
134 if (strcmp (prefix->name, sx->list->val) == 0) {
135 if (prefix->flags & SEXP_FLAG_FIELD) {
137 _notmuch_database_log (notmuch, "nested field: '%s' inside '%s'\n",
138 prefix->name, parent->name);
139 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
144 return _sexp_combine_query (notmuch, parent, prefix->xapian_op, prefix->initial,
145 sx->list->next, output);
149 _notmuch_database_log (notmuch, "unknown prefix '%s'\n", sx->list->val);
151 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
155 _notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
156 Xapian::Query &output)
158 const sexp_t *sx = NULL;
159 char *buf = talloc_strdup (notmuch, querystr);
161 sx = parse_sexp (buf, strlen (querystr));
163 _notmuch_database_log (notmuch, "invalid s-expression: '%s'\n", querystr);
164 return NOTMUCH_STATUS_BAD_QUERY_SYNTAX;
167 return _sexp_to_xapian_query (notmuch, NULL, sx, output);