summaryrefslogtreecommitdiff
path: root/lib/mu-query-parser.hh
blob: 79c5e46b74e9405a5a1e09d05a61b6eb2d2f3cbe (plain)
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
/*
** Copyright (C) 2023-2024 Dirk-Jan C. Binnema <djcb@djcbsoftware.nl>
**
** This program is free software; you can redistribute it and/or modify it
** under the terms of the GNU General Public License as published by the
** Free Software Foundation; either version 3, or (at your option) any
** later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software Foundation,
** Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
**
*/
#include <string>

#include "mu-xapian-db.hh"

#include "utils/mu-sexp.hh"
#include "utils/mu-result.hh"
#include "mu-store.hh"

namespace Mu {
/*
 * Some useful symbol-sexps
 */
static inline const auto	placeholder_sym = "_"_sym;
static inline const auto	phrase_sym	= "phrase"_sym;
static inline const auto	regex_sym	= "regex"_sym;
static inline const auto	range_sym	= "range"_sym;
static inline const auto	wildcard_sym	= "wildcard"_sym;

static inline const auto	open_sym	= "("_sym;
static inline const auto	close_sym	= ")"_sym;

static inline const auto	and_sym		= "and"_sym;
static inline const auto	or_sym		= "or"_sym;
static inline const auto	xor_sym		= "xor"_sym;
static inline const auto	not_sym		= "not"_sym;
static inline const auto	and_not_sym	= "and-not"_sym;


/*
 * We take a query, then parse it into a human-readable s-expression and then
 * turn that s-expression into a Xapian query
 *
 * some query:
 *   "from:hello or subject:world"
 *
 * 1. tokenize-query
 *   => ((from "hello") or (subject "world"))
 *
 * 2. parse-query
 *   => (or (from "hello") (subject "world"))
 *
 * 3. xapian-query
 *   => Query((Fhello OR Sworld))
 * *
 */

/**
 * Analyze the query expression and express it as a Sexp-list with the sequence
 * of elements.
 *
 * @param expr a search expression
 *
 * @return Sexp with the sequence of elements
 */
Sexp process_query(const std::string& expr);

/**
 * Parse the query expression and create a parse-tree expressed as an Sexp
 * object (tree).
 *
 * Internally, this processes the stream into element (see process_query()) and
 * processes the tokens into a Sexp. This sexp is meant to be human-readable.
 *
 * @param expr a search expression
 * @param expand whether to expand combination-fields (such as '_', 'recip',
 * 'contacts')
 *
 * @return Sexp with the parse tree
 */
Sexp parse_query(const std::string& expr, bool expand=false);

/**
 * Make a Xapian Query for the given string expression.
 *
 * This uses parse_query() and turns the S-expression into a Xapian::Query.
 * Unlike mere parsing, this uses the information in the store to resolve
 * wildcard / regex queries.
 *
 * @param store the message store
 * @param expr a string expression
 * @param flavor type of parser to use
 *
 * @return a Xapian query result or an error.
 */
enum struct ParserFlags {
	None	      = 0 << 0,
	SupportNgrams = 1 << 0, /**< Support Xapian's Ngrams for CJK etc. handling */
	XapianParser  = 1 << 1, /**< For testing only, use Xapian's
				 * built-in QueryParser; this is not
				 * fully compatible with mu, only useful
				 * for debugging. */
};
Result<Xapian::Query> make_xapian_query(const Store& store, const std::string& expr,
					ParserFlags flag=ParserFlags::None) noexcept;

MU_ENABLE_BITOPS(ParserFlags);
} // namespace Mu