2 /* queryparser.lemony: build a Xapian::Query object from a user query string.
4 * Copyright (C) 2004,2005,2006,2007,2008,2009,2010,2011,2012,2013,2015,2016,2018 Olly Betts
5 * Copyright (C) 2007,2008,2009 Lemur Consulting Ltd
6 * Copyright (C) 2010 Adam Sjøgren
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of the
11 * License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
26 #include "queryparser_internal.h"
28 #include "api/queryinternal.h"
31 #include "stringutils.h"
32 #include "xapian/error.h"
33 #include "xapian/unicode.h"
35 // Include the list of token values lemon generates.
36 #include "queryparser_token.h"
38 #include "cjk-tokenizer.h"
47 // We create the yyParser on the stack.
48 #define Parse_ENGINEALWAYSONSTACK
52 using namespace Xapian;
55 U_isupper(unsigned ch) {
56 return (ch < 128 && C_isupper(static_cast<unsigned char>(ch)));
60 U_isdigit(unsigned ch) {
61 return (ch < 128 && C_isdigit(static_cast<unsigned char>(ch)));
65 U_isalpha(unsigned ch) {
66 return (ch < 128 && C_isalpha(static_cast<unsigned char>(ch)));
69 using Xapian::Unicode::is_whitespace;
72 is_not_whitespace(unsigned ch) {
73 return !is_whitespace(ch);
76 using Xapian::Unicode::is_wordchar;
79 is_not_wordchar(unsigned ch) {
80 return !is_wordchar(ch);
84 is_digit(unsigned ch) {
85 return (Unicode::get_category(ch) == Unicode::DECIMAL_DIGIT_NUMBER);
88 // FIXME: we used to keep trailing "-" (e.g. Cl-) but it's of dubious utility
89 // and there's the risk of hyphens getting stuck onto the end of terms...
91 is_suffix(unsigned ch) {
92 return ch == '+' || ch == '#';
96 is_double_quote(unsigned ch) {
97 // We simply treat all double quotes as equivalent, which is a bit crude,
98 // but it isn't clear that it would actually better to require them to
101 // 0x201c is Unicode opening double quote.
102 // 0x201d is Unicode closing double quote.
103 return ch == '"' || ch == 0x201c || ch == 0x201d;
107 prefix_needs_colon(const string & prefix, unsigned ch)
109 if (!U_isupper(ch) && ch != ':') return false;
110 string::size_type len = prefix.length();
111 return (len > 1 && prefix[len - 1] != ':');
114 using Unicode::is_currency;
117 is_positional(Xapian::Query::op op)
119 return (op == Xapian::Query::OP_PHRASE || op == Xapian::Query::OP_NEAR);
124 /** Class used to pass information about a token from lexer to parser.
126 * Generally an instance of this class carries term information, but it can be
127 * used for a range query, and with some operators (e.g. the distance in
128 * NEAR/3 or ADJ/3, etc).
135 const FieldInfo * field_info;
137 QueryParser::stem_strategy stem;
141 Term(const string &name_, termpos pos_)
142 : name(name_), stem(QueryParser::STEM_NONE), pos(pos_) { }
143 explicit Term(const string &name_)
144 : name(name_), stem(QueryParser::STEM_NONE), pos(0) { }
145 Term(const string &name_, const FieldInfo * field_info_)
146 : name(name_), field_info(field_info_),
147 stem(QueryParser::STEM_NONE), pos(0) { }
148 explicit Term(termpos pos_) : stem(QueryParser::STEM_NONE), pos(pos_) { }
149 Term(State * state_, const string &name_, const FieldInfo * field_info_,
150 const string &unstemmed_,
151 QueryParser::stem_strategy stem_ = QueryParser::STEM_NONE,
153 : state(state_), name(name_), field_info(field_info_),
154 unstemmed(unstemmed_), stem(stem_), pos(pos_) { }
156 Term(const Xapian::Query & q, const string & grouping)
157 : name(grouping), query(q) { }
159 string make_term(const string & prefix) const;
161 void need_positions() {
162 if (stem == QueryParser::STEM_SOME) stem = QueryParser::STEM_NONE;
165 termpos get_termpos() const { return pos; }
167 string get_grouping() const {
168 return field_info->grouping;
171 Query * as_wildcarded_query(State * state) const;
173 /** Build a query for a term at the very end of the query string when
174 * FLAG_PARTIAL is in use.
176 * This query should match documents containing any terms which start with
177 * the characters specified, but should give a higher score to exact
178 * matches (since the user might have finished typing - we simply don't
181 Query * as_partial_query(State * state_) const;
183 /** Build a query for a string of CJK characters. */
184 Query * as_cjk_query() const;
186 /** Handle a CJK character string in a positional context. */
187 void as_positional_cjk_term(Terms * terms) const;
190 Query as_range_query() const;
192 Query get_query() const;
194 Query get_query_with_synonyms() const;
196 Query get_query_with_auto_synonyms() const;
199 /// Parser State shared between the lexer and the parser.
201 QueryParser::Internal * qpi;
208 State(QueryParser::Internal * qpi_, unsigned flags_)
209 : qpi(qpi_), error(NULL), flags(flags_) { }
211 string stem_term(const string &term) {
212 return qpi->stemmer(term);
215 void add_to_stoplist(const Term * term) {
216 qpi->stoplist.push_back(term->name);
219 void add_to_unstem(const string & term, const string & unstemmed) {
220 qpi->unstem.insert(make_pair(term, unstemmed));
223 Term * range(const string &a, const string &b) {
224 for (auto i : qpi->rangeprocs) {
225 Xapian::Query range_query = (i.proc)->check_range(a, b);
226 Xapian::Query::op op = range_query.get_type();
228 case Xapian::Query::OP_INVALID:
230 case Xapian::Query::OP_VALUE_RANGE:
231 case Xapian::Query::OP_VALUE_GE:
232 case Xapian::Query::OP_VALUE_LE:
233 if (i.default_grouping) {
234 Xapian::Internal::QueryValueBase * base =
235 static_cast<Xapian::Internal::QueryValueBase*>(
236 range_query.internal.get());
237 Xapian::valueno slot = base->get_slot();
238 return new Term(range_query, str(slot));
241 case Xapian::Query::LEAF_TERM:
242 return new Term(range_query, i.grouping);
244 return new Term(range_query, string());
250 Query::op default_op() const { return qpi->default_op; }
252 bool is_stopword(const Term *term) const {
253 return qpi->stopper.get() && (*qpi->stopper)(term->name);
256 Database get_database() const {
260 const Stopper * get_stopper() const {
261 return qpi->stopper.get();
264 size_t stoplist_size() const {
265 return qpi->stoplist.size();
268 void stoplist_resize(size_t s) {
269 qpi->stoplist.resize(s);
272 Xapian::termcount get_max_wildcard_expansion() const {
273 return qpi->max_wildcard_expansion;
276 int get_max_wildcard_type() const {
277 return qpi->max_wildcard_type;
280 Xapian::termcount get_max_partial_expansion() const {
281 return qpi->max_partial_expansion;
284 int get_max_partial_type() const {
285 return qpi->max_partial_type;
290 Term::make_term(const string & prefix) const
293 if (stem != QueryParser::STEM_NONE && stem != QueryParser::STEM_ALL)
295 if (!prefix.empty()) {
297 if (prefix_needs_colon(prefix, name[0])) term += ':';
299 if (stem != QueryParser::STEM_NONE) {
300 term += state->stem_term(name);
305 if (!unstemmed.empty())
306 state->add_to_unstem(term, unstemmed);
310 // Iterator shim to allow building a synonym query from a TermIterator pair.
311 class SynonymIterator {
312 Xapian::TermIterator i;
316 const Xapian::Query * first;
319 SynonymIterator(const Xapian::TermIterator & i_,
320 Xapian::termpos pos_ = 0,
321 const Xapian::Query * first_ = NULL)
322 : i(i_), pos(pos_), first(first_) { }
324 SynonymIterator & operator++() {
332 const Xapian::Query operator*() const {
333 if (first) return *first;
334 return Xapian::Query(*i, 1, pos);
337 bool operator==(const SynonymIterator & o) const {
338 return i == o.i && first == o.first;
341 bool operator!=(const SynonymIterator & o) const {
342 return !(*this == o);
345 typedef std::input_iterator_tag iterator_category;
346 typedef Xapian::Query value_type;
347 typedef Xapian::termcount_diff difference_type;
348 typedef Xapian::Query * pointer;
349 typedef Xapian::Query & reference;
353 Term::get_query_with_synonyms() const
355 // Handle single-word synonyms with each prefix.
356 const list<string> & prefixes = field_info->prefixes;
357 if (prefixes.empty()) {
358 // FIXME: handle multiple here
359 Assert(!field_info->procs.empty());
360 return (**field_info->procs.begin())(name);
363 Query q = get_query();
365 list<string>::const_iterator piter;
366 for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
367 // First try the unstemmed term:
369 if (!piter->empty()) {
371 if (prefix_needs_colon(*piter, name[0])) term += ':';
375 Xapian::Database db = state->get_database();
376 Xapian::TermIterator syn = db.synonyms_begin(term);
377 Xapian::TermIterator end = db.synonyms_end(term);
378 if (syn == end && stem != QueryParser::STEM_NONE) {
379 // If that has no synonyms, try the stemmed form:
381 if (!piter->empty()) {
383 if (prefix_needs_colon(*piter, name[0])) term += ':';
385 term += state->stem_term(name);
386 syn = db.synonyms_begin(term);
387 end = db.synonyms_end(term);
389 q = Query(q.OP_SYNONYM,
390 SynonymIterator(syn, pos, &q),
391 SynonymIterator(end));
397 Term::get_query_with_auto_synonyms() const
399 const unsigned MASK_ENABLE_AUTO_SYNONYMS =
400 QueryParser::FLAG_AUTO_SYNONYMS |
401 QueryParser::FLAG_AUTO_MULTIWORD_SYNONYMS;
402 if (state->flags & MASK_ENABLE_AUTO_SYNONYMS)
403 return get_query_with_synonyms();
409 add_to_query(Query *& q, Query::op op, Query * term)
413 *q = Query(op, *q, *term);
421 add_to_query(Query *& q, Query::op op, const Query & term)
424 *q = Query(op, *q, term);
431 Term::get_query() const
433 const list<string> & prefixes = field_info->prefixes;
434 if (prefixes.empty()) {
435 // FIXME: handle multiple here
436 Assert(!field_info->procs.empty());
437 return (**field_info->procs.begin())(name);
439 list<string>::const_iterator piter = prefixes.begin();
440 Query q(make_term(*piter), 1, pos);
441 while (++piter != prefixes.end()) {
442 q = Query(Query::OP_OR, q, Query(make_term(*piter), 1, pos));
448 Term::as_wildcarded_query(State * state_) const
450 const list<string> & prefixes = field_info->prefixes;
451 list<string>::const_iterator piter;
452 Xapian::termcount max = state_->get_max_wildcard_expansion();
453 int max_type = state_->get_max_wildcard_type();
455 subqs.reserve(prefixes.size());
456 for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
457 string root = *piter;
459 // Combine with OP_OR, and apply OP_SYNONYM afterwards.
460 subqs.push_back(Query(Query::OP_WILDCARD, root, max, max_type,
463 Query * q = new Query(Query::OP_SYNONYM, subqs.begin(), subqs.end());
469 Term::as_partial_query(State * state_) const
471 Xapian::termcount max = state_->get_max_partial_expansion();
472 int max_type = state_->get_max_partial_type();
473 vector<Query> subqs_partial; // A synonym of all the partial terms.
474 vector<Query> subqs_full; // A synonym of all the full terms.
476 const list<string> & prefixes = field_info->prefixes;
477 list<string>::const_iterator piter;
478 for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
479 string root = *piter;
481 // Combine with OP_OR, and apply OP_SYNONYM afterwards.
482 subqs_partial.push_back(Query(Query::OP_WILDCARD, root, max, max_type,
484 // Add the term, as it would normally be handled, as an alternative.
485 subqs_full.push_back(Query(make_term(*piter), 1, pos));
487 Query * q = new Query(Query::OP_OR,
488 Query(Query::OP_SYNONYM,
489 subqs_partial.begin(), subqs_partial.end()),
490 Query(Query::OP_SYNONYM,
491 subqs_full.begin(), subqs_full.end()));
497 Term::as_cjk_query() const
499 vector<Query> prefix_subqs;
500 vector<Query> cjk_subqs;
501 const list<string> & prefixes = field_info->prefixes;
502 list<string>::const_iterator piter;
503 for (piter = prefixes.begin(); piter != prefixes.end(); ++piter) {
504 const string& prefix = *piter;
505 for (CJKTokenIterator tk(name); tk != CJKTokenIterator(); ++tk) {
506 cjk_subqs.push_back(Query(prefix + *tk, 1, pos));
508 prefix_subqs.push_back(Query(Query::OP_AND,
509 cjk_subqs.begin(), cjk_subqs.end()));
512 Query * q = new Query(Query::OP_OR,
513 prefix_subqs.begin(), prefix_subqs.end());
519 Term::as_range_query() const
527 is_phrase_generator(unsigned ch)
529 // These characters generate a phrase search.
530 // Ordered mostly by frequency of calls to this function done when
531 // running the testcases in api_queryparser.cc.
532 return (ch && ch < 128 && strchr(".-/:\\@", ch) != NULL);
536 is_stem_preventer(unsigned ch)
538 return (ch && ch < 128 && strchr("(/\\@<>=*[{\"", ch) != NULL);
542 should_stem(const string & term)
544 const unsigned int SHOULD_STEM_MASK =
545 (1 << Unicode::LOWERCASE_LETTER) |
546 (1 << Unicode::TITLECASE_LETTER) |
547 (1 << Unicode::MODIFIER_LETTER) |
548 (1 << Unicode::OTHER_LETTER);
549 Utf8Iterator u(term);
550 return ((SHOULD_STEM_MASK >> Unicode::get_category(*u)) & 1);
553 /** Value representing "ignore this" when returned by check_infix() or
554 * check_infix_digit().
556 const unsigned UNICODE_IGNORE = numeric_limits<unsigned>::max();
558 inline unsigned check_infix(unsigned ch) {
559 if (ch == '\'' || ch == '&' || ch == 0xb7 || ch == 0x5f4 || ch == 0x2027) {
560 // Unicode includes all these except '&' in its word boundary rules,
561 // as well as 0x2019 (which we handle below) and ':' (for Swedish
562 // apparently, but we ignore this for now as it's problematic in
563 // real world cases).
567 // 0x2019 is Unicode apostrophe and single closing quote.
568 // 0x201b is Unicode single opening quote with the tail rising.
569 if (ch == 0x2019 || ch == 0x201b)
571 if (ch <= 0x200d || ch == 0x2060 || ch == 0xfeff)
572 return UNICODE_IGNORE;
577 inline unsigned check_infix_digit(unsigned ch) {
578 // This list of characters comes from Unicode's word identifying algorithm.
583 case 0x037e: // GREEK QUESTION MARK
584 case 0x0589: // ARMENIAN FULL STOP
585 case 0x060D: // ARABIC DATE SEPARATOR
586 case 0x07F8: // NKO COMMA
587 case 0x2044: // FRACTION SLASH
588 case 0xFE10: // PRESENTATION FORM FOR VERTICAL COMMA
589 case 0xFE13: // PRESENTATION FORM FOR VERTICAL COLON
590 case 0xFE14: // PRESENTATION FORM FOR VERTICAL SEMICOLON
593 if (ch >= 0x200b && (ch <= 0x200d || ch == 0x2060 || ch == 0xfeff))
594 return UNICODE_IGNORE;
598 // Prototype a function lemon generates, but which we want to call before that
599 // in the generated source code file.
601 static void yy_parse_failed(yyParser *);
604 QueryParser::Internal::add_prefix(const string &field, const string &prefix)
606 map<string, FieldInfo>::iterator p = field_map.find(field);
607 if (p == field_map.end()) {
608 field_map.insert(make_pair(field, FieldInfo(NON_BOOLEAN, prefix)));
610 // Check that this is the same type of filter as the existing one(s).
611 if (p->second.type != NON_BOOLEAN) {
612 throw Xapian::InvalidOperationError("Can't use add_prefix() and add_boolean_prefix() on the same field name, or add_boolean_prefix() with different values of the 'exclusive' parameter");
614 if (!p->second.procs.empty())
615 throw Xapian::FeatureUnavailableError("Mixing FieldProcessor objects and string prefixes currently not supported");
616 p->second.prefixes.push_back(prefix);
621 QueryParser::Internal::add_prefix(const string &field, FieldProcessor *proc)
623 map<string, FieldInfo>::iterator p = field_map.find(field);
624 if (p == field_map.end()) {
625 field_map.insert(make_pair(field, FieldInfo(NON_BOOLEAN, proc)));
627 // Check that this is the same type of filter as the existing one(s).
628 if (p->second.type != NON_BOOLEAN) {
629 throw Xapian::InvalidOperationError("Can't use add_prefix() and add_boolean_prefix() on the same field name, or add_boolean_prefix() with different values of the 'exclusive' parameter");
631 if (!p->second.prefixes.empty())
632 throw Xapian::FeatureUnavailableError("Mixing FieldProcessor objects and string prefixes currently not supported");
633 throw Xapian::FeatureUnavailableError("Multiple FieldProcessor objects for the same prefix currently not supported");
634 // p->second.procs.push_back(proc);
639 QueryParser::Internal::add_boolean_prefix(const string &field,
640 const string &prefix,
641 const string* grouping)
643 // Don't allow the empty prefix to be set as boolean as it doesn't
644 // really make sense.
646 throw Xapian::UnimplementedError("Can't set the empty prefix to be a boolean filter");
647 if (!grouping) grouping = &field;
648 filter_type type = grouping->empty() ? BOOLEAN : BOOLEAN_EXCLUSIVE;
649 map<string, FieldInfo>::iterator p = field_map.find(field);
650 if (p == field_map.end()) {
651 field_map.insert(make_pair(field, FieldInfo(type, prefix, *grouping)));
653 // Check that this is the same type of filter as the existing one(s).
654 if (p->second.type != type) {
655 throw Xapian::InvalidOperationError("Can't use add_prefix() and add_boolean_prefix() on the same field name, or add_boolean_prefix() with different values of the 'exclusive' parameter"); // FIXME
657 if (!p->second.procs.empty())
658 throw Xapian::FeatureUnavailableError("Mixing FieldProcessor objects and string prefixes currently not supported");
659 p->second.prefixes.push_back(prefix); // FIXME grouping
664 QueryParser::Internal::add_boolean_prefix(const string &field,
665 FieldProcessor *proc,
666 const string* grouping)
668 // Don't allow the empty prefix to be set as boolean as it doesn't
669 // really make sense.
671 throw Xapian::UnimplementedError("Can't set the empty prefix to be a boolean filter");
672 if (!grouping) grouping = &field;
673 filter_type type = grouping->empty() ? BOOLEAN : BOOLEAN_EXCLUSIVE;
674 map<string, FieldInfo>::iterator p = field_map.find(field);
675 if (p == field_map.end()) {
676 field_map.insert(make_pair(field, FieldInfo(type, proc, *grouping)));
678 // Check that this is the same type of filter as the existing one(s).
679 if (p->second.type != type) {
680 throw Xapian::InvalidOperationError("Can't use add_prefix() and add_boolean_prefix() on the same field name, or add_boolean_prefix() with different values of the 'exclusive' parameter"); // FIXME
682 if (!p->second.prefixes.empty())
683 throw Xapian::FeatureUnavailableError("Mixing FieldProcessor objects and string prefixes currently not supported");
684 throw Xapian::FeatureUnavailableError("Multiple FieldProcessor objects for the same prefix currently not supported");
685 // p->second.procs.push_back(proc);
690 QueryParser::Internal::parse_term(Utf8Iterator &it, const Utf8Iterator &end,
691 bool cjk_ngram, bool & is_cjk_term,
695 // Look for initials separated by '.' (e.g. P.T.O., U.N.C.L.E).
696 // Don't worry if there's a trailing '.' or not.
697 if (U_isupper(*it)) {
701 Unicode::append_utf8(t, *p++);
702 } while (p != end && *p == '.' && ++p != end && U_isupper(*p));
703 // One letter does not make an acronym! If we handled a single
704 // uppercase letter here, we wouldn't catch M&S below.
705 if (t.length() > 1) {
706 // Check there's not a (lower case) letter or digit
707 // immediately after it.
708 // FIXME: should I.B.M..P.T.O be a range search?
709 if (p == end || !is_wordchar(*p)) {
715 was_acronym = !term.empty();
717 if (cjk_ngram && term.empty() && CJK::codepoint_is_cjk(*it)) {
718 term = CJK::get_cjk(it);
723 unsigned prevch = *it;
724 Unicode::append_utf8(term, prevch);
725 while (++it != end) {
726 if (cjk_ngram && CJK::codepoint_is_cjk(*it)) break;
728 if (!is_wordchar(ch)) {
729 // Treat a single embedded '&' or "'" or similar as a word
730 // character (e.g. AT&T, Fred's). Also, normalise
731 // apostrophes to ASCII apostrophe.
734 if (p == end || !is_wordchar(*p)) break;
735 unsigned nextch = *p;
736 if (is_digit(prevch) && is_digit(nextch)) {
737 ch = check_infix_digit(ch);
739 ch = check_infix(ch);
742 if (ch == UNICODE_IGNORE)
745 Unicode::append_utf8(term, ch);
748 if (it != end && is_suffix(*it)) {
749 string suff_term = term;
751 // Keep trailing + (e.g. C++, Na+) or # (e.g. C#).
753 if (suff_term.size() - term.size() == 3) {
758 } while (is_suffix(*++p));
759 if (!suff_term.empty() && (p == end || !is_wordchar(*p))) {
760 // If the suffixed term doesn't exist, check that the
761 // non-suffixed term does. This also takes care of
762 // the case when QueryParser::set_database() hasn't
764 bool use_suff_term = false;
765 string lc = Unicode::tolower(suff_term);
766 if (db.term_exists(lc)) {
767 use_suff_term = true;
769 lc = Unicode::tolower(term);
770 if (!db.term_exists(lc)) use_suff_term = true;
783 // Switch to %code to insert at the end of the file so struct yyParser has been
788 QueryParser::Internal::parse_query(const string &qs, unsigned flags,
789 const string &default_prefix)
791 bool cjk_ngram = (flags & FLAG_CJK_NGRAM) || CJK::is_cjk_enabled();
793 // Set ranges if we may have to handle ranges in the query.
794 bool ranges = !rangeprocs.empty() && (qs.find("..") != string::npos);
796 termpos term_pos = 1;
797 Utf8Iterator it(qs), end;
799 State state(this, flags);
801 // To successfully apply more than one spelling correction to a query
802 // string, we must keep track of the offset due to previous corrections.
803 int correction_offset = 0;
804 corrected_query.resize(0);
806 // Stack of prefixes, used for phrases and subexpressions.
807 list<const FieldInfo *> prefix_stack;
809 // If default_prefix is specified, use it. Otherwise, use any list
810 // that has been set for the empty prefix.
811 const FieldInfo def_pfx(NON_BOOLEAN, default_prefix);
813 const FieldInfo * default_field_info = &def_pfx;
814 if (default_prefix.empty()) {
815 auto f = field_map.find(string());
816 if (f != field_map.end()) default_field_info = &(f->second);
819 // We always have the current prefix on the top of the stack.
820 prefix_stack.push_back(default_field_info);
825 unsigned newprev = ' ';
828 DEFAULT, IN_QUOTES, IN_PREFIXED_QUOTES, IN_PHRASED_TERM, IN_GROUP,
829 IN_GROUP2, EXPLICIT_SYNONYM
831 while (it != end && !state.error) {
832 bool last_was_operator = false;
833 bool last_was_operator_needing_term = false;
834 if (mode == EXPLICIT_SYNONYM) mode = DEFAULT;
837 if (it == end) break;
839 last_was_operator_needing_term = false;
840 last_was_operator = true;
843 just_had_operator_needing_term:
844 last_was_operator_needing_term = true;
845 last_was_operator = true;
847 if (mode == IN_PHRASED_TERM) mode = DEFAULT;
848 if (is_whitespace(*it)) {
851 it = find_if(it, end, is_not_whitespace);
852 if (it == end) break;
856 (mode == DEFAULT || mode == IN_GROUP || mode == IN_GROUP2)) {
857 // Scan forward to see if this could be the "start of range"
858 // token. Sadly this has O(n^2) tendencies, though at least
859 // "n" is the number of words in a query which is likely to
860 // remain fairly small. FIXME: can we tokenise more elegantly?
861 Utf8Iterator it_initial = it;
865 if (ch == '.' && *p == '.') {
868 Unicode::append_utf8(a, *it++);
870 // Trim off the trailing ".".
871 a.resize(a.size() - 1);
873 // Either end of the range can be empty (for an open-ended
874 // range) but both can't be empty.
875 if (!a.empty() || (p != end && *p > ' ' && *p != ')')) {
877 // Allow any character except whitespace and ')' in the
879 while (p != end && *p > ' ' && *p != ')') {
880 Unicode::append_utf8(b, *p++);
882 Term * range = state.range(a, b);
884 state.error = "Unknown range operation";
885 if (a.find(':', 1) == string::npos) {
888 // Might be a boolean filter with ".." in. Leave
889 // state.error in case it isn't.
893 Parse(&parser, RANGE, range, &state);
899 // Allow any character except whitespace and '(' in the lower
901 if (ch <= ' ' || ch == '(') break;
906 if (!is_wordchar(*it)) {
907 unsigned prev = newprev;
910 // Drop out of IN_GROUP mode.
911 if (mode == IN_GROUP || mode == IN_GROUP2)
915 case 0x201c: // Left curly double quote.
916 case 0x201d: // Right curly double quote.
918 if (mode == DEFAULT) {
920 it = find_if(it, end, is_not_whitespace);
922 // Ignore an unmatched " at the end of the query to
923 // avoid generating an empty pair of QUOTEs which will
924 // cause a parse error.
927 if (is_double_quote(*it)) {
928 // Ignore empty "" (but only if we're not already
929 // IN_QUOTES as we don't merge two adjacent quoted
935 if (flags & QueryParser::FLAG_PHRASE) {
936 Parse(&parser, QUOTE, NULL, &state);
937 if (mode == DEFAULT) {
940 // Remove the prefix we pushed for this phrase.
941 if (mode == IN_PREFIXED_QUOTES)
942 prefix_stack.pop_back();
948 case '+': case '-': // Loved or hated term/phrase/subexpression.
949 // Ignore + or - at the end of the query string.
950 if (it == end) goto done;
951 if (prev > ' ' && prev != '(') {
952 // Or if not after whitespace or an open bracket.
955 if (is_whitespace(*it) || *it == '+' || *it == '-') {
956 // Ignore + or - followed by a space, or further + or -.
957 // Postfix + (such as in C++ and H+) is handled as part of
958 // the term lexing code in parse_term().
962 if (mode == DEFAULT && (flags & FLAG_LOVEHATE)) {
966 } else if (last_was_operator) {
967 token = HATE_AFTER_AND;
971 Parse(&parser, token, NULL, &state);
972 goto just_had_operator_needing_term;
974 // Need to prevent the term after a LOVE or HATE starting a
978 case '(': // Bracketed subexpression.
980 it = find_if(it, end, is_not_whitespace);
981 // Ignore ( at the end of the query string.
982 if (it == end) goto done;
983 if (prev > ' ' && strchr("()+-", prev) == NULL) {
984 // Or if not after whitespace or a bracket or '+' or '-'.
992 if (mode == DEFAULT && (flags & FLAG_BOOLEAN)) {
993 prefix_stack.push_back(prefix_stack.back());
994 Parse(&parser, BRA, NULL, &state);
998 case ')': // End of bracketed subexpression.
999 if (mode == DEFAULT && (flags & FLAG_BOOLEAN)) {
1000 // Remove the prefix we pushed for the corresponding BRA.
1001 // If brackets are unmatched, it's a syntax error, but
1002 // that's no excuse to SEGV!
1003 if (prefix_stack.size() > 1) prefix_stack.pop_back();
1004 Parse(&parser, KET, NULL, &state);
1008 case '~': // Synonym expansion.
1009 // Ignore at the end of the query string.
1010 if (it == end) goto done;
1011 if (mode == DEFAULT && (flags & FLAG_SYNONYM)) {
1012 if (prev > ' ' && strchr("+-(", prev) == NULL) {
1013 // Or if not after whitespace, +, -, or an open bracket.
1016 if (!is_wordchar(*it)) {
1017 // Ignore if not followed by a word character.
1020 Parse(&parser, SYNONYM, NULL, &state);
1021 mode = EXPLICIT_SYNONYM;
1022 goto just_had_operator_needing_term;
1026 // Skip any other characters.
1030 Assert(is_wordchar(*it));
1032 size_t term_start_index = it.raw() - qs.data();
1034 newprev = 'A'; // Any letter will do...
1036 // A term, a prefix, or a boolean operator.
1037 const FieldInfo * field_info = NULL;
1038 if ((mode == DEFAULT || mode == IN_GROUP || mode == IN_GROUP2 || mode == EXPLICIT_SYNONYM) &&
1039 !field_map.empty()) {
1040 // Check for a fieldname prefix (e.g. title:historical).
1041 Utf8Iterator p = find_if(it, end, is_not_wordchar);
1042 if (p != end && *p == ':' && ++p != end && *p > ' ' && *p != ')') {
1046 Unicode::append_utf8(field, *p++);
1047 map<string, FieldInfo>::const_iterator f;
1048 f = field_map.find(field);
1049 if (f != field_map.end()) {
1050 // Special handling for prefixed fields, depending on the
1051 // type of the prefix.
1053 field_info = &(f->second);
1055 if (field_info->type != NON_BOOLEAN) {
1056 // Drop out of IN_GROUP if we're in it.
1057 if (mode == IN_GROUP || mode == IN_GROUP2)
1061 if (it != end && is_double_quote(*it)) {
1062 // Quoted boolean term (can contain any character).
1063 bool fancy = (*it != '"');
1067 // Interpret "" as an escaped ".
1068 if (++it == end || *it != '"')
1070 } else if (fancy && is_double_quote(*it)) {
1071 // If the opening quote was ASCII, then the
1072 // closing one must be too - otherwise
1073 // the user can't protect non-ASCII double
1074 // quote characters by quoting or escaping.
1078 Unicode::append_utf8(name, *it++);
1081 // Can't boolean filter prefix a subexpression, so
1082 // just use anything following the prefix until the
1083 // next space or ')' as part of the boolean filter
1085 while (it != end && *it > ' ' && *it != ')')
1086 Unicode::append_utf8(name, *it++);
1088 // Build the unstemmed form in field.
1091 // Clear any pending range error.
1093 Term * token = new Term(&state, name, field_info, field);
1094 Parse(&parser, BOOLEAN_FILTER, token, &state);
1098 if ((flags & FLAG_PHRASE) && is_double_quote(ch)) {
1099 // Prefixed phrase, e.g.: subject:"space flight"
1100 mode = IN_PREFIXED_QUOTES;
1101 Parse(&parser, QUOTE, NULL, &state);
1105 prefix_stack.push_back(field_info);
1109 if (ch == '(' && (flags & FLAG_BOOLEAN)) {
1110 // Prefixed subexpression, e.g.: title:(fast NEAR food)
1112 Parse(&parser, BRA, NULL, &state);
1116 prefix_stack.push_back(field_info);
1121 // Allow 'path:/usr/local' but not 'foo::bar::baz'.
1122 while (is_phrase_generator(ch)) {
1129 if (is_wordchar(ch)) {
1134 // It looks like a prefix but isn't, so parse it as
1144 bool is_cjk_term = false;
1145 string term = parse_term(it, end, cjk_ngram, is_cjk_term, was_acronym);
1147 // Boolean operators.
1148 if ((mode == DEFAULT || mode == IN_GROUP || mode == IN_GROUP2) &&
1149 (flags & FLAG_BOOLEAN) &&
1150 // Don't want to interpret A.N.D. as an AND operator.
1153 term.size() >= 2 && term.size() <= 4 && U_isalpha(term[0])) {
1156 if (flags & FLAG_BOOLEAN_ANY_CASE) {
1157 for (string::iterator i = op.begin(); i != op.end(); ++i) {
1161 if (op.size() == 3) {
1163 Parse(&parser, AND, NULL, &state);
1164 goto just_had_operator;
1167 Parse(&parser, NOT, NULL, &state);
1168 goto just_had_operator;
1171 Parse(&parser, XOR, NULL, &state);
1172 goto just_had_operator;
1175 if (it != end && *it == '/') {
1177 Utf8Iterator p = it;
1178 while (++p != end && U_isdigit(*p)) {
1179 width = (width * 10) + (*p - '0');
1181 if (width && (p == end || is_whitespace(*p))) {
1183 Parse(&parser, ADJ, new Term(width), &state);
1184 goto just_had_operator;
1187 Parse(&parser, ADJ, NULL, &state);
1188 goto just_had_operator;
1191 } else if (op.size() == 2) {
1193 Parse(&parser, OR, NULL, &state);
1194 goto just_had_operator;
1196 } else if (op.size() == 4) {
1198 if (it != end && *it == '/') {
1200 Utf8Iterator p = it;
1201 while (++p != end && U_isdigit(*p)) {
1202 width = (width * 10) + (*p - '0');
1204 if (width && (p == end || is_whitespace(*p))) {
1206 Parse(&parser, NEAR, new Term(width), &state);
1207 goto just_had_operator;
1210 Parse(&parser, NEAR, NULL, &state);
1211 goto just_had_operator;
1217 // If no prefix is set, use the default one.
1218 if (!field_info) field_info = prefix_stack.back();
1220 Assert(field_info->type == NON_BOOLEAN);
1223 string unstemmed_term(term);
1224 term = Unicode::tolower(term);
1226 // Reuse stem_strategy - STEM_SOME here means "stem terms except
1227 // when used with positional operators".
1228 stem_strategy stem_term = stem_action;
1229 if (stem_term != STEM_NONE) {
1230 if (!stemmer.internal.get()) {
1231 // No stemmer is set.
1232 stem_term = STEM_NONE;
1233 } else if (stem_term == STEM_SOME) {
1234 if (!should_stem(unstemmed_term) ||
1235 (it != end && is_stem_preventer(*it))) {
1236 // Don't stem this particular term.
1237 stem_term = STEM_NONE;
1242 Term * term_obj = new Term(&state, term, field_info,
1243 unstemmed_term, stem_term, term_pos++);
1246 Parse(&parser, CJKTERM, term_obj, &state);
1247 if (it == end) break;
1251 if (mode == DEFAULT || mode == IN_GROUP || mode == IN_GROUP2) {
1253 if ((flags & FLAG_WILDCARD) && *it == '*') {
1256 if (p == end || !is_wordchar(*p)) {
1258 if (mode == IN_GROUP || mode == IN_GROUP2) {
1259 // Drop out of IN_GROUP and flag that the group
1260 // can be empty if all members are stopwords.
1261 if (mode == IN_GROUP2)
1262 Parse(&parser, EMPTY_GROUP_OK, NULL, &state);
1265 // Wildcard at end of term (also known as
1266 // "right truncation").
1267 Parse(&parser, WILD_TERM, term_obj, &state);
1272 if (flags & FLAG_PARTIAL) {
1273 if (mode == IN_GROUP || mode == IN_GROUP2) {
1274 // Drop out of IN_GROUP and flag that the group
1275 // can be empty if all members are stopwords.
1276 if (mode == IN_GROUP2)
1277 Parse(&parser, EMPTY_GROUP_OK, NULL, &state);
1280 // Final term of a partial match query, with no
1281 // following characters - treat as a wildcard.
1282 Parse(&parser, PARTIAL_TERM, term_obj, &state);
1288 // Check spelling, if we're a normal term, and any of the prefixes
1290 if ((flags & FLAG_SPELLING_CORRECTION) && !was_acronym) {
1291 const list<string> & pfxes = field_info->prefixes;
1292 list<string>::const_iterator pfx_it;
1293 for (pfx_it = pfxes.begin(); pfx_it != pfxes.end(); ++pfx_it) {
1294 if (!pfx_it->empty())
1296 const string & suggest = db.get_spelling_suggestion(term);
1297 if (!suggest.empty()) {
1298 if (corrected_query.empty()) corrected_query = qs;
1299 size_t term_end_index = it.raw() - qs.data();
1300 size_t n = term_end_index - term_start_index;
1301 size_t pos = term_start_index + correction_offset;
1302 corrected_query.replace(pos, n, suggest);
1303 correction_offset += suggest.size();
1304 correction_offset -= n;
1310 if (mode == IN_PHRASED_TERM) {
1311 Parse(&parser, PHR_TERM, term_obj, &state);
1313 // See if the next token will be PHR_TERM - if so, this one
1314 // needs to be TERM not GROUP_TERM.
1315 if ((mode == IN_GROUP || mode == IN_GROUP2) &&
1316 is_phrase_generator(*it)) {
1317 // FIXME: can we clean this up?
1318 Utf8Iterator p = it;
1321 } while (p != end && is_phrase_generator(*p));
1322 // Don't generate a phrase unless the phrase generators are
1323 // immediately followed by another term.
1324 if (p != end && is_wordchar(*p)) {
1330 if (mode == IN_GROUP || mode == IN_GROUP2) {
1334 Parse(&parser, token, term_obj, &state);
1335 if (token == TERM && mode != DEFAULT)
1340 if (it == end) break;
1342 if (is_phrase_generator(*it)) {
1343 // Skip multiple phrase generators.
1346 } while (it != end && is_phrase_generator(*it));
1347 // Don't generate a phrase unless the phrase generators are
1348 // immediately followed by another term.
1349 if (it != end && is_wordchar(*it)) {
1350 mode = IN_PHRASED_TERM;
1351 term_start_index = it.raw() - qs.data();
1354 } else if (mode == DEFAULT || mode == IN_GROUP || mode == IN_GROUP2) {
1355 int old_mode = mode;
1357 if (!last_was_operator_needing_term && is_whitespace(*it)) {
1359 // Skip multiple whitespace.
1362 } while (it != end && is_whitespace(*it));
1363 // Don't generate a group unless the terms are only separated
1365 if (it != end && is_wordchar(*it)) {
1366 if (old_mode == IN_GROUP || old_mode == IN_GROUP2) {
1377 // Implicitly close any unclosed quotes.
1378 if (mode == IN_QUOTES || mode == IN_PREFIXED_QUOTES)
1379 Parse(&parser, QUOTE, NULL, &state);
1381 // Implicitly close all unclosed brackets.
1382 while (prefix_stack.size() > 1) {
1383 Parse(&parser, KET, NULL, &state);
1384 prefix_stack.pop_back();
1386 Parse(&parser, 0, NULL, &state);
1389 errmsg = state.error;
1397 Query* query = NULL;
1400 // filter is a map from prefix to a query for that prefix. Queries with
1401 // the same prefix are combined with OR, and the results of this are
1402 // combined with AND to get the full filter.
1403 map<string, Query> filter;
1408 ProbQuery(Query* query_) : query(query_) {}
1416 void add_filter(const string& grouping, const Query & q) {
1417 filter[grouping] = q;
1420 void append_filter(const string& grouping, const Query & qnew) {
1421 auto it = filter.find(grouping);
1422 if (it == filter.end()) {
1423 filter.insert(make_pair(grouping, qnew));
1425 Query & q = it->second;
1426 // We OR multiple filters with the same prefix if they're
1427 // exclusive, otherwise we AND them.
1428 bool exclusive = !grouping.empty();
1429 Query::op op = exclusive ? Query::OP_OR : Query::OP_AND;
1430 q = Query(op, q, qnew);
1434 void add_filter_range(const string& grouping, const Query & range) {
1435 filter[grouping] = range;
1438 void append_filter_range(const string& grouping, const Query & range) {
1439 Query & q = filter[grouping];
1440 q = Query(Query::OP_OR, q, range);
1443 Query merge_filters() const {
1444 auto i = filter.begin();
1445 Assert(i != filter.end());
1446 Query q = i->second;
1447 while (++i != filter.end()) {
1448 q = Query(Query::OP_AND, q, i->second);
1454 /// A group of terms separated only by whitespace.
1456 vector<Term *> terms;
1458 /** Controls how to handle a group where all terms are stopwords.
1460 * If true, then as_group() returns NULL. If false, then the
1461 * stopword status of the terms is ignored.
1465 TermGroup(Term* t1, Term* t2) : empty_ok(false) {
1471 /// Factory function - ensures heap allocation.
1472 static TermGroup* create(Term* t1, Term* t2) {
1473 return new TermGroup(t1, t2);
1477 for (auto&& t : terms) {
1482 /// Add a Term object to this TermGroup object.
1483 void add_term(Term * term) {
1484 terms.push_back(term);
1487 /// Set the empty_ok flag.
1488 void set_empty_ok() { empty_ok = true; }
1490 /// Convert to a Xapian::Query * using default_op.
1491 Query * as_group(State *state) const;
1495 TermGroup::as_group(State *state) const
1497 const Xapian::Stopper * stopper = state->get_stopper();
1498 size_t stoplist_size = state->stoplist_size();
1499 bool default_op_is_positional = is_positional(state->default_op());
1501 Query::op default_op = state->default_op();
1502 vector<Query> subqs;
1503 subqs.reserve(terms.size());
1504 if (state->flags & QueryParser::FLAG_AUTO_MULTIWORD_SYNONYMS) {
1505 // Check for multi-word synonyms.
1506 Database db = state->get_database();
1509 vector<Term*>::const_iterator begin = terms.begin();
1510 vector<Term*>::const_iterator i = begin;
1511 while (i != terms.end()) {
1512 TermIterator synkey(db.synonym_keys_begin((*i)->name));
1513 TermIterator synend(db.synonym_keys_end((*i)->name));
1514 if (synkey == synend) {
1515 // No multi-synonym matches.
1516 if (stopper && (*stopper)((*i)->name)) {
1517 state->add_to_stoplist(*i);
1519 if (default_op_is_positional)
1520 (*i)->need_positions();
1521 subqs.push_back((*i)->get_query_with_auto_synonyms());
1527 while (i != terms.end()) {
1528 if (!key.empty()) key += ' ';
1531 synkey.skip_to(key);
1532 if (synkey == synend || !startswith(*synkey, key)) break;
1534 // Greedily try to match as many consecutive words as possible.
1535 TermIterator syn, end;
1537 syn = db.synonyms_begin(key);
1538 end = db.synonyms_end(key);
1539 if (syn != end) break;
1540 if (--i == begin) break;
1541 key.resize(key.size() - (*i)->name.size() - 1);
1544 // No multi-synonym matches.
1545 if (stopper && (*stopper)((*i)->name)) {
1546 state->add_to_stoplist(*i);
1548 if (default_op_is_positional)
1549 (*i)->need_positions();
1550 subqs.push_back((*i)->get_query_with_auto_synonyms());
1556 vector<Query> subqs2;
1557 vector<Term*>::const_iterator j;
1558 for (j = begin; j != i; ++j) {
1559 if (stopper && (*stopper)((*j)->name)) {
1560 state->add_to_stoplist(*j);
1562 if (default_op_is_positional)
1563 (*i)->need_positions();
1564 subqs2.push_back((*j)->get_query());
1567 Query q_original_terms;
1568 if (default_op_is_positional) {
1569 q_original_terms = Query(default_op,
1570 subqs2.begin(), subqs2.end(),
1573 q_original_terms = Query(default_op,
1574 subqs2.begin(), subqs2.end());
1578 // Use the position of the first term for the synonyms.
1579 Query q(Query::OP_SYNONYM,
1580 SynonymIterator(syn, (*begin)->pos, &q_original_terms),
1581 SynonymIterator(end));
1587 vector<Term*>::const_iterator i;
1588 for (i = terms.begin(); i != terms.end(); ++i) {
1589 if (stopper && (*stopper)((*i)->name)) {
1590 state->add_to_stoplist(*i);
1592 if (default_op_is_positional)
1593 (*i)->need_positions();
1594 subqs.push_back((*i)->get_query_with_auto_synonyms());
1599 if (!empty_ok && stopper && subqs.empty() &&
1600 stoplist_size < state->stoplist_size()) {
1601 // This group is all stopwords, so roll-back, disable stopper
1602 // temporarily, and reprocess this group.
1603 state->stoplist_resize(stoplist_size);
1609 if (!subqs.empty()) {
1610 if (default_op_is_positional) {
1611 q = new Query(default_op, subqs.begin(), subqs.end(),
1614 q = new Query(default_op, subqs.begin(), subqs.end());
1621 /// Some terms which form a positional sub-query.
1623 vector<Term *> terms;
1626 /** Keep track of whether the terms added all have the same list of
1627 * prefixes. If so, we'll build a set of phrases, one using each prefix.
1628 * This works around the limitation that a phrase cannot have multiple
1629 * components which are "OR" combinations of terms, but is also probably
1630 * what users expect: i.e., if a user specifies a phrase in a field, and
1631 * that field maps to multiple prefixes, the user probably wants a phrase
1632 * returned with all terms having one of those prefixes, rather than a
1633 * phrase comprised of terms with differing prefixes.
1635 bool uniform_prefixes;
1637 /** The list of prefixes of the terms added.
1638 * This will be NULL if the terms have different prefixes.
1640 const list<string> * prefixes;
1642 /// Convert to a query using the given operator and window size.
1643 Query * as_opwindow_query(Query::op op, Xapian::termcount w_delta) const {
1645 size_t n_terms = terms.size();
1646 Xapian::termcount w = w_delta + terms.size();
1647 if (uniform_prefixes) {
1649 list<string>::const_iterator piter;
1650 for (piter = prefixes->begin(); piter != prefixes->end(); ++piter) {
1651 vector<Query> subqs;
1652 subqs.reserve(n_terms);
1653 vector<Term *>::const_iterator titer;
1654 for (titer = terms.begin(); titer != terms.end(); ++titer) {
1656 subqs.push_back(Query(t->make_term(*piter), 1, t->pos));
1658 add_to_query(q, Query::OP_OR,
1659 Query(op, subqs.begin(), subqs.end(), w));
1663 vector<Query> subqs;
1664 subqs.reserve(n_terms);
1665 vector<Term *>::const_iterator titer;
1666 for (titer = terms.begin(); titer != terms.end(); ++titer) {
1667 subqs.push_back((*titer)->get_query());
1669 q = new Query(op, subqs.begin(), subqs.end(), w);
1676 Terms() : window(0), uniform_prefixes(true), prefixes(NULL) { }
1679 /// Factory function - ensures heap allocation.
1680 static Terms* create() {
1685 for (auto&& t : terms) {
1690 /// Add an unstemmed Term object to this Terms object.
1691 void add_positional_term(Term * term) {
1692 const list<string> & term_prefixes = term->field_info->prefixes;
1693 if (terms.empty()) {
1694 prefixes = &term_prefixes;
1695 } else if (uniform_prefixes && prefixes != &term_prefixes) {
1696 if (*prefixes != term_prefixes) {
1698 uniform_prefixes = false;
1701 term->need_positions();
1702 terms.push_back(term);
1705 void adjust_window(size_t alternative_window) {
1706 if (alternative_window > window) window = alternative_window;
1709 /// Convert to a Xapian::Query * using adjacent OP_PHRASE.
1710 Query * as_phrase_query() const {
1711 return as_opwindow_query(Query::OP_PHRASE, 0);
1714 /// Convert to a Xapian::Query * using OP_NEAR.
1715 Query * as_near_query() const {
1716 // The common meaning of 'a NEAR b' is "a within 10 terms of b", which
1717 // means a window size of 11. For more than 2 terms, we just add one
1718 // to the window size for each extra term.
1721 return as_opwindow_query(Query::OP_NEAR, w - 1);
1724 /// Convert to a Xapian::Query * using OP_PHRASE to implement ADJ.
1725 Query * as_adj_query() const {
1726 // The common meaning of 'a ADJ b' is "a at most 10 terms before b",
1727 // which means a window size of 11. For more than 2 terms, we just add
1728 // one to the window size for each extra term.
1731 return as_opwindow_query(Query::OP_PHRASE, w - 1);
1736 Term::as_positional_cjk_term(Terms * terms) const
1738 // Add each individual CJK character to the phrase.
1740 for (Utf8Iterator it(name); it != Utf8Iterator(); ++it) {
1741 Unicode::append_utf8(t, *it);
1742 Term * c = new Term(state, t, field_info, unstemmed, stem, pos);
1743 terms->add_positional_term(c);
1747 // FIXME: we want to add the n-grams as filters too for efficiency.
1752 // Helper macro for converting a boolean operation into a Xapian::Query.
1753 #define BOOL_OP_TO_QUERY(E, A, OP, B, OP_TXT) \
1756 state->error = "Syntax: <expression> " OP_TXT " <expression>";\
1757 yy_parse_failed(yypParser);\
1760 E = new Query(OP, *A, *B);\
1767 %token_type {Term *}
1768 %token_destructor {delete $$;}
1770 %extra_argument {State * state}
1773 // If we've not already set an error message, set a default one.
1774 if (!state->error) state->error = "parse error";
1778 yy_parse_failed(yypParser);
1781 // Operators, grouped in order of increasing precedence:
1787 %left LOVE HATE HATE_AFTER_AND SYNONYM.
1789 // Destructors for terminal symbols:
1791 // TERM is a query term, including prefix (if any).
1792 %destructor TERM {delete $$;}
1794 // GROUP_TERM is a query term which follows a TERM or another GROUP_TERM and
1795 // is only separated by whitespace characters.
1796 %destructor GROUP_TERM {delete $$;}
1798 // PHR_TERM is a query term which follows a TERM or another PHR_TERM and is
1799 // separated only by one or more phrase generator characters (hyphen and
1800 // apostrophe are common examples - see is_phrase_generator() for the list
1801 // of all punctuation which does this).
1802 %destructor PHR_TERM {delete $$;}
1804 // WILD_TERM is like a TERM, but has a trailing wildcard which needs to be
1806 %destructor WILD_TERM {delete $$;}
1808 // PARTIAL_TERM is like a TERM, but it's at the end of the query string and
1809 // we're doing "search as you type". It expands to something like WILD_TERM
1811 %destructor PARTIAL_TERM {delete $$;}
1813 // BOOLEAN_FILTER is a query term with a prefix registered using
1814 // add_boolean_prefix(). It's added to the query using an OP_FILTER operator,
1815 // (or OP_AND_NOT if it's negated) e.g. site:xapian.org or -site:xapian.org
1816 %destructor BOOLEAN_FILTER {delete $$;}
1820 // query - The whole query - just an expr or nothing.
1822 // query non-terminal doesn't need a type, so just give a dummy one.
1825 query ::= expr(E). {
1826 // Save the parsed query in the State structure so we can return it.
1831 state->query = Query();
1836 // Handle a query string with no terms in.
1837 state->query = Query();
1840 // expr - A query expression.
1842 %type expr {Query *}
1843 %destructor expr {delete $$;}
1845 expr(E) ::= prob_expr(E).
1847 expr(E) ::= bool_arg(A) AND bool_arg(B).
1848 { BOOL_OP_TO_QUERY(E, A, Query::OP_AND, B, "AND"); }
1850 expr(E) ::= bool_arg(A) NOT bool_arg(B). {
1851 // 'NOT foo' -> '<alldocuments> NOT foo'
1852 if (!A && (state->flags & QueryParser::FLAG_PURE_NOT)) {
1853 A = new Query("", 1, 0);
1855 BOOL_OP_TO_QUERY(E, A, Query::OP_AND_NOT, B, "NOT");
1858 expr(E) ::= bool_arg(A) AND NOT bool_arg(B). [NOT]
1859 { BOOL_OP_TO_QUERY(E, A, Query::OP_AND_NOT, B, "AND NOT"); }
1861 expr(E) ::= bool_arg(A) AND HATE_AFTER_AND bool_arg(B). [AND]
1862 { BOOL_OP_TO_QUERY(E, A, Query::OP_AND_NOT, B, "AND"); }
1864 expr(E) ::= bool_arg(A) OR bool_arg(B).
1865 { BOOL_OP_TO_QUERY(E, A, Query::OP_OR, B, "OR"); }
1867 expr(E) ::= bool_arg(A) XOR bool_arg(B).
1868 { BOOL_OP_TO_QUERY(E, A, Query::OP_XOR, B, "XOR"); }
1870 // bool_arg - an argument to a boolean operator such as AND or OR.
1872 %type bool_arg {Query *}
1873 %destructor bool_arg {delete $$;}
1875 bool_arg(A) ::= expr(A).
1877 bool_arg(A) ::= . [ERROR] {
1878 // Set the argument to NULL, which enables the bool_arg-using rules in
1879 // expr above to report uses of AND, OR, etc which don't have two
1884 // prob_expr - a single compound term, or a prob.
1886 %type prob_expr {Query *}
1887 %destructor prob_expr {delete $$;}
1889 prob_expr(E) ::= prob(P). {
1892 // Handle any "+ terms".
1894 if (P->love->empty()) {
1900 add_to_query(E, Query::OP_AND_MAYBE, P->love);
1906 // Handle any boolean filters.
1907 if (!P->filter.empty()) {
1909 add_to_query(E, Query::OP_FILTER, P->merge_filters());
1911 // Make the query a boolean one.
1912 E = new Query(Query::OP_SCALE_WEIGHT, P->merge_filters(), 0.0);
1915 // Handle any "- terms".
1916 if (P->hate && !P->hate->empty()) {
1919 yy_parse_failed(yypParser);
1922 *E = Query(Query::OP_AND_NOT, *E, *P->hate);
1927 prob_expr(E) ::= term(E).
1929 // prob - a sub-expression consisting of stop_terms, "+" terms, "-" terms,
1930 // boolean filters, and/or ranges.
1932 // Note: stop_term can also be several other things other than a simple term!
1934 %type prob {ProbQuery *}
1935 %destructor prob {delete $$;}
1937 prob(P) ::= RANGE(R). {
1938 string grouping = R->name;
1939 const Query & range = R->as_range_query();
1940 P = new ProbQuery; /*P-overwrites-R*/
1941 P->add_filter_range(grouping, range);
1944 prob(P) ::= stop_prob(P) RANGE(R). {
1945 string grouping = R->name;
1946 const Query & range = R->as_range_query();
1947 P->append_filter_range(grouping, range);
1950 prob(P) ::= stop_term(T) stop_term(U). {
1951 P = new ProbQuery(T); /*P-overwrites-T*/
1953 Query::op op = state->default_op();
1954 if (P->query && is_positional(op)) {
1955 // If default_op is OP_NEAR or OP_PHRASE, set the window size to
1956 // 11 for the first pair of terms and it will automatically grow
1957 // by one for each subsequent term.
1958 Query * subqs[2] = { P->query, U };
1959 *(P->query) = Query(op, subqs, subqs + 2, 11);
1962 add_to_query(P->query, op, U);
1967 prob(P) ::= prob(P) stop_term(T). {
1968 // If T is a stopword, there's nothing to do here.
1969 if (T) add_to_query(P->query, state->default_op(), T);
1972 prob(P) ::= LOVE term(T). {
1974 if (state->default_op() == Query::OP_AND) {
1981 prob(P) ::= stop_prob(P) LOVE term(T). {
1982 if (state->default_op() == Query::OP_AND) {
1983 /* The default op is AND, so we just put loved terms into the query
1984 * (in this case the only effect of love is to ignore the stopword
1986 add_to_query(P->query, Query::OP_AND, T);
1988 add_to_query(P->love, Query::OP_AND, T);
1992 prob(P) ::= HATE term(T). {
1997 prob(P) ::= stop_prob(P) HATE term(T). {
1998 add_to_query(P->hate, Query::OP_OR, T);
2001 prob(P) ::= HATE BOOLEAN_FILTER(T). {
2003 P->hate = new Query(T->get_query());
2007 prob(P) ::= stop_prob(P) HATE BOOLEAN_FILTER(T). {
2008 add_to_query(P->hate, Query::OP_OR, T->get_query());
2012 prob(P) ::= BOOLEAN_FILTER(T). {
2014 P->add_filter(T->get_grouping(), T->get_query());
2018 prob(P) ::= stop_prob(P) BOOLEAN_FILTER(T). {
2019 P->append_filter(T->get_grouping(), T->get_query());
2023 prob(P) ::= LOVE BOOLEAN_FILTER(T). {
2024 // LOVE BOOLEAN_FILTER(T) is just the same as BOOLEAN_FILTER
2026 P->filter[T->get_grouping()] = T->get_query();
2030 prob(P) ::= stop_prob(P) LOVE BOOLEAN_FILTER(T). {
2031 // LOVE BOOLEAN_FILTER(T) is just the same as BOOLEAN_FILTER
2032 // We OR filters with the same prefix...
2033 Query & q = P->filter[T->get_grouping()];
2034 q = Query(Query::OP_OR, q, T->get_query());
2038 // stop_prob - A prob or a stop_term.
2040 %type stop_prob {ProbQuery *}
2041 %destructor stop_prob {delete $$;}
2043 stop_prob(P) ::= prob(P).
2045 stop_prob(P) ::= stop_term(T). {
2046 P = new ProbQuery(T); /*P-overwrites-T*/
2049 // stop_term - A term which should be checked against the stopword list,
2050 // or a compound_term.
2052 // If a term is loved, hated, or in a phrase, we don't want to consult the
2053 // stopword list, so stop_term isn't used there (instead term is).
2055 %type stop_term {Query *}
2056 %destructor stop_term {delete $$;}
2058 stop_term(T) ::= TERM(U). {
2059 if (state->is_stopword(U)) {
2061 state->add_to_stoplist(U);
2063 T = new Query(U->get_query_with_auto_synonyms());
2068 stop_term(T) ::= compound_term(T).
2070 // term - A term or a compound_term.
2072 %type term {Query *}
2073 %destructor term {delete $$;}
2075 term(T) ::= TERM(U). {
2076 T = new Query(U->get_query_with_auto_synonyms());
2080 term(T) ::= compound_term(T).
2082 // compound_term - A WILD_TERM, a quoted phrase (with or without prefix), a
2083 // phrased_term, group, near_expr, adj_expr, or a bracketed subexpression (with
2084 // or without prefix).
2086 %type compound_term {Query *}
2087 %destructor compound_term {delete $$;}
2089 compound_term(T) ::= WILD_TERM(U).
2090 { T = U->as_wildcarded_query(state); /*T-overwrites-U*/ }
2092 compound_term(T) ::= PARTIAL_TERM(U).
2093 { T = U->as_partial_query(state); /*T-overwrites-U*/ }
2095 compound_term(T) ::= QUOTE phrase(P) QUOTE.
2096 { T = P->as_phrase_query(); }
2098 compound_term(T) ::= phrased_term(P).
2099 { T = P->as_phrase_query(); /*T-overwrites-P*/ }
2101 compound_term(T) ::= group(P).
2102 { T = P->as_group(state); /*T-overwrites-P*/ }
2104 compound_term(T) ::= near_expr(P).
2105 { T = P->as_near_query(); /*T-overwrites-P*/ }
2107 compound_term(T) ::= adj_expr(P).
2108 { T = P->as_adj_query(); /*T-overwrites-P*/ }
2110 compound_term(T) ::= BRA expr(E) KET.
2113 compound_term(T) ::= SYNONYM TERM(U). {
2114 T = new Query(U->get_query_with_synonyms());
2118 compound_term(T) ::= CJKTERM(U). {
2119 { T = U->as_cjk_query(); /*T-overwrites-U*/ }
2122 // phrase - The "inside the quotes" part of a double-quoted phrase.
2124 %type phrase {Terms *}
2126 %destructor phrase {delete $$;}
2128 phrase(P) ::= TERM(T). {
2129 P = Terms::create();
2130 P->add_positional_term(T);
2133 phrase(P) ::= CJKTERM(T). {
2134 P = Terms::create();
2135 T->as_positional_cjk_term(P);
2138 phrase(P) ::= phrase(P) TERM(T). {
2139 P->add_positional_term(T);
2142 phrase(P) ::= phrase(P) CJKTERM(T). {
2143 T->as_positional_cjk_term(P);
2146 // phrased_term - A phrased term works like a single term, but is actually
2147 // 2 or more terms linked together into a phrase by punctuation. There must be
2148 // at least 2 terms in order to be able to have punctuation between the terms!
2150 %type phrased_term {Terms *}
2151 %destructor phrased_term {delete $$;}
2153 phrased_term(P) ::= TERM(T) PHR_TERM(U). {
2154 P = Terms::create();
2155 P->add_positional_term(T);
2156 P->add_positional_term(U);
2159 phrased_term(P) ::= phrased_term(P) PHR_TERM(T). {
2160 P->add_positional_term(T);
2163 // group - A group of terms separated only by whitespace - candidates for
2164 // multi-term synonyms.
2166 %type group {TermGroup *}
2167 %destructor group {delete $$;}
2169 group(P) ::= TERM(T) GROUP_TERM(U). {
2170 P = TermGroup::create(T, U); /*P-overwrites-T*/
2173 group(P) ::= group(P) GROUP_TERM(T). {
2177 group(P) ::= group(P) EMPTY_GROUP_OK. {
2181 // near_expr - 2 or more terms with NEAR in between. There must be at least 2
2182 // terms in order for there to be any NEAR operators!
2184 %type near_expr {Terms *}
2185 %destructor near_expr {delete $$;}
2187 near_expr(P) ::= TERM(T) NEAR(N) TERM(U). {
2188 P = Terms::create();
2189 P->add_positional_term(T);
2190 P->add_positional_term(U);
2192 P->adjust_window(N->get_termpos());
2197 near_expr(P) ::= near_expr(P) NEAR(N) TERM(T). {
2198 P->add_positional_term(T);
2200 P->adjust_window(N->get_termpos());
2205 // adj_expr - 2 or more terms with ADJ in between. There must be at least 2
2206 // terms in order for there to be any ADJ operators!
2208 %type adj_expr {Terms *}
2209 %destructor adj_expr {delete $$;}
2211 adj_expr(P) ::= TERM(T) ADJ(N) TERM(U). {
2212 P = Terms::create();
2213 P->add_positional_term(T);
2214 P->add_positional_term(U);
2216 P->adjust_window(N->get_termpos());
2221 adj_expr(P) ::= adj_expr(P) ADJ(N) TERM(T). {
2222 P->add_positional_term(T);
2224 P->adjust_window(N->get_termpos());
2229 // Select yacc syntax highlighting in vim editor: vim: syntax=yacc
2230 // (lemon syntax colouring isn't supplied by default; yacc does an OK job).