unofficial mirror of notmuch@notmuchmail.org
 help / color / mirror / code / Atom feed
blob 5241d435661eb20110c9abff1b55d35a3f7e7a02 4476 bytes (raw)
name: lib/parse-sexp.cc 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
 
#include <xapian.h>
#include "notmuch-private.h"
#include "sexp.h"

typedef struct  {
    const char *name;
    Xapian::Query::op xapian_op;
    Xapian::Query initial;
} _sexp_op_t;

static _sexp_op_t operations[] =
{
    { "and",    Xapian::Query::OP_AND,          Xapian::Query::MatchAll },
    { "not",    Xapian::Query::OP_AND_NOT,      Xapian::Query::MatchAll },
    { "or",     Xapian::Query::OP_OR,           Xapian::Query::MatchNothing },
    { }
};

typedef struct  {
    const char *name;
    Xapian::Query::op xapian_op;
} _sexp_field_t;

static _sexp_field_t fields[] =
{
    { "attachment",     Xapian::Query::OP_PHRASE },
    { "body",           Xapian::Query::OP_PHRASE },
    { "from",           Xapian::Query::OP_PHRASE },
    { "folder",         Xapian::Query::OP_OR },
    { "id",             Xapian::Query::OP_OR },
    { "is",             Xapian::Query::OP_AND },
    { "mid",            Xapian::Query::OP_OR },
    { "mimetype",       Xapian::Query::OP_PHRASE },
    { "path",           Xapian::Query::OP_OR },
    { "property",       Xapian::Query::OP_AND },
    { "subject",        Xapian::Query::OP_PHRASE },
    { "tag",            Xapian::Query::OP_AND },
    { "thread",         Xapian::Query::OP_OR },
    { "to",             Xapian::Query::OP_PHRASE },
    { }
};

static notmuch_status_t _sexp_to_xapian_query (notmuch_database_t *notmuch, sexp_t *sx, Xapian::Query &output);

static notmuch_status_t
_sexp_combine_query (notmuch_database_t *notmuch,
		     Xapian::Query::op operation,
		     Xapian::Query left,
		     sexp_t *sx,
		     Xapian::Query &output)
{
    Xapian::Query subquery;

    notmuch_status_t status;

    /* if we run out elements, return accumulator */

    if (! sx) {
	output = left;
	return NOTMUCH_STATUS_SUCCESS;
    }

    status = _sexp_to_xapian_query (notmuch, sx, subquery);
    if (status)
	return status;

    return _sexp_combine_query (notmuch,
				operation,
				Xapian::Query (operation, left, subquery),
				sx->next, output);
}

notmuch_status_t
_notmuch_sexp_string_to_xapian_query (notmuch_database_t *notmuch, const char *querystr,
				      Xapian::Query &output)
{
    sexp_t *sx = NULL;
    char *buf = talloc_strdup (notmuch, querystr);

    sx = parse_sexp (buf, strlen (querystr));
    if (!sx)
	return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;

    return _sexp_to_xapian_query (notmuch, sx, output);
}

static notmuch_status_t
_sexp_combine_field (const char *prefix,
		     Xapian::Query::op operation,
		     sexp_t *sx,
		     Xapian::Query &output)
{
    std::vector<std::string> terms;

    for (sexp_t *cur = sx; cur; cur = cur->next) {
	std::string pref_str = prefix;

	if (operation == Xapian::Query::OP_PHRASE) {
	    Xapian::Utf8Iterator p (cur->val);
	    Xapian::Utf8Iterator end;

	    while (p != end) {
		Xapian::Utf8Iterator start;
		while (p != end && ! Xapian::Unicode::is_wordchar (*p))
		    p++;

		if (p == end)
		    break;

		start = p;

		while (p != end && Xapian::Unicode::is_wordchar (*p))
		    p++;

		if (p != start) {
		    std::string word (start, p);
		    word = Xapian::Unicode::tolower (word);
		    terms.push_back (pref_str + word);
		}
	    }
	} else {
	    terms.push_back (pref_str + cur->val);
	}
    }
    output = Xapian::Query (operation, terms.begin (), terms.end ());
    return NOTMUCH_STATUS_SUCCESS;
}

/* Here we expect the s-expression to be a proper list, with first
 * element defining and operation, or as a special case the empty
 * list */

static notmuch_status_t
_sexp_to_xapian_query (notmuch_database_t *notmuch, sexp_t *sx, Xapian::Query &output)
{

    const _sexp_op_t *op;

    /* Currently we don't understand atoms */
    assert (sx->ty == SEXP_LIST);

    /* Empty list */
    if (! sx->list) {
	output = Xapian::Query::MatchAll;
	return NOTMUCH_STATUS_SUCCESS;
    }

    for (op = operations; op && op->name; op++) {
	if (strcasecmp (op->name, hd_sexp (sx)->val) == 0) {
	    return _sexp_combine_query (notmuch, op->xapian_op, op->initial, sx->list->next, output);
	}

    }

    for (const _sexp_field_t *field = fields; field && field->name; field++) {
	if (strcasecmp (field->name, hd_sexp (sx)->val) == 0) {
	    return _sexp_combine_field (_find_prefix (field->name), field->xapian_op, sx->list->next,
					output);
	}
    }

    _notmuch_database_log_append (notmuch, "unimplemented prefix %s\n", sx->list->val);
    return NOTMUCH_STATUS_ILLEGAL_ARGUMENT;
}

debug log:

solving 4a9e1aeb ...
found 4a9e1aeb in https://yhetil.org/notmuch/20210714000239.804384-10-david@tethera.net/
found 81bf5cee in https://yhetil.org/notmuch/20210714000239.804384-9-david@tethera.net/
found 26d4ee1f in https://yhetil.org/notmuch/20210714000239.804384-8-david@tethera.net/
found 4a2fac8b in https://yhetil.org/notmuch/20210714000239.804384-7-david@tethera.net/
found eded98e7 in https://yhetil.org/notmuch/20210714000239.804384-6-david@tethera.net/

applying [1/5] https://yhetil.org/notmuch/20210714000239.804384-6-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
new file mode 100644
index 00000000..eded98e7


applying [2/5] https://yhetil.org/notmuch/20210714000239.804384-7-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index eded98e7..4a2fac8b 100644\r


applying [3/5] https://yhetil.org/notmuch/20210714000239.804384-8-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index 4a2fac8b..26d4ee1f 100644\r


applying [4/5] https://yhetil.org/notmuch/20210714000239.804384-9-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc\r
index 26d4ee1f..81bf5cee 100644\r


applying [5/5] https://yhetil.org/notmuch/20210714000239.804384-10-david@tethera.net/
diff --git a/lib/parse-sexp.cc b/lib/parse-sexp.cc
index 81bf5cee..4a9e1aeb 100644

Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.
Checking patch lib/parse-sexp.cc...
Applied patch lib/parse-sexp.cc cleanly.

index at:
100644 5241d435661eb20110c9abff1b55d35a3f7e7a02	lib/parse-sexp.cc

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this public inbox

	https://yhetil.org/notmuch.git/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).