This function returns a parser. The grammar should be like most full text search engines (Google, Tsearch, Lucene). Grammar: - a query consists of alphanumeric words, with an optional '*' wildcard at the end of a word - a sequence of words b
(self)
| 84 | self._parser = self.parser() |
| 85 | |
| 86 | def parser(self): |
| 87 | """ |
| 88 | This function returns a parser. |
| 89 | The grammar should be like most full text search engines (Google, Tsearch, Lucene). |
| 90 | |
| 91 | Grammar: |
| 92 | - a query consists of alphanumeric words, with an optional '*' wildcard |
| 93 | at the end of a word |
| 94 | - a sequence of words between quotes is a literal string |
| 95 | - words can be used together by using operators ('and' or 'or') |
| 96 | - words with operators can be grouped with parenthesis |
| 97 | - a word or group of words can be preceded by a 'not' operator |
| 98 | - the 'and' operator precedes an 'or' operator |
| 99 | - if an operator is missing, use an 'and' operator |
| 100 | """ |
| 101 | operatorOr = Forward() |
| 102 | |
| 103 | operatorWord = Group(Combine(Word(alphanums) + Suppress("*"))).set_results_name( |
| 104 | "wordwildcard" |
| 105 | ) | Group(Word(alphanums)).set_results_name("word") |
| 106 | |
| 107 | operatorQuotesContent = Forward() |
| 108 | operatorQuotesContent << ((operatorWord + operatorQuotesContent) | operatorWord) |
| 109 | |
| 110 | operatorQuotes = ( |
| 111 | Group(Suppress('"') + operatorQuotesContent + Suppress('"')).set_results_name( |
| 112 | "quotes" |
| 113 | ) |
| 114 | | operatorWord |
| 115 | ) |
| 116 | |
| 117 | operatorParenthesis = ( |
| 118 | Group(Suppress("(") + operatorOr + Suppress(")")).set_results_name( |
| 119 | "parenthesis" |
| 120 | ) |
| 121 | | operatorQuotes |
| 122 | ) |
| 123 | |
| 124 | operatorNot = Forward() |
| 125 | operatorNot << ( |
| 126 | Group(Suppress(Keyword("not", caseless=True)) + operatorNot).set_results_name( |
| 127 | "not" |
| 128 | ) |
| 129 | | operatorParenthesis |
| 130 | ) |
| 131 | |
| 132 | operatorAnd = Forward() |
| 133 | operatorAnd << ( |
| 134 | Group( |
| 135 | operatorNot + Suppress(Keyword("and", caseless=True)) + operatorAnd |
| 136 | ).set_results_name("and") |
| 137 | | Group( |
| 138 | operatorNot + OneOrMore(~one_of("and or") + operatorAnd) |
| 139 | ).set_results_name("and") |
| 140 | | operatorNot |
| 141 | ) |
| 142 | |
| 143 | operatorOr << ( |