| 1 |
from lunr.query_lexer import QueryLexer |
| 2 |
from lunr.query import Clause, QueryPresence |
| 3 |
from lunr.exceptions import QueryParseError |
| 4 |
|
| 5 |
|
| 6 |
class QueryParser: |
| 7 |
def __init__(self, string, query): |
| 8 |
self.lexer = QueryLexer(string) |
| 9 |
self.query = query |
| 10 |
self.current_clause = Clause() |
| 11 |
self.lexeme_idx = 0 |
| 12 |
|
| 13 |
def parse(self): |
| 14 |
self.lexer.run() |
| 15 |
self.lexemes = self.lexer.lexemes |
| 16 |
|
| 17 |
state = self.__class__.parse_clause |
| 18 |
|
| 19 |
while state: |
| 20 |
state = state(self) |
| 21 |
|
| 22 |
return self.query |
| 23 |
|
| 24 |
def peek_lexeme(self): |
| 25 |
try: |
| 26 |
return self.lexemes[self.lexeme_idx] |
| 27 |
except IndexError: |
| 28 |
return None |
| 29 |
|
| 30 |
def consume_lexeme(self): |
| 31 |
lexeme = self.peek_lexeme() |
| 32 |
self.lexeme_idx += 1 |
| 33 |
return lexeme |
| 34 |
|
| 35 |
def next_clause(self): |
| 36 |
self.query.clause(self.current_clause) |
| 37 |
self.current_clause = Clause() |
| 38 |
|
| 39 |
@classmethod |
| 40 |
def parse_clause(cls, parser): |
| 41 |
lexeme = parser.peek_lexeme() |
| 42 |
if lexeme is None: |
| 43 |
return |
| 44 |
|
| 45 |
if lexeme["type"] == QueryLexer.FIELD: |
| 46 |
return cls.parse_field |
| 47 |
elif lexeme["type"] == QueryLexer.TERM: |
| 48 |
return cls.parse_term |
| 49 |
elif lexeme["type"] == QueryLexer.PRESENCE: |
| 50 |
return cls.parse_presence |
| 51 |
else: |
| 52 |
raise QueryParseError( |
| 53 |
"Expected either a field or a term, found {}{}".format( |
| 54 |
lexeme["type"], |
| 55 |
( |
| 56 |
'with value "' + lexeme["string"] + '"' |
| 57 |
if len(lexeme["string"]) |
| 58 |
else "" |
| 59 |
), |
| 60 |
) |
| 61 |
) |
| 62 |
|
| 63 |
@classmethod |
| 64 |
def parse_field(cls, parser): |
| 65 |
lexeme = parser.consume_lexeme() |
| 66 |
|
| 67 |
if lexeme["string"] not in parser.query.all_fields: |
| 68 |
raise QueryParseError( |
| 69 |
'Unrecognized field "{}", possible fields {}'.format( |
| 70 |
lexeme["string"], ", ".join(parser.query.all_fields) |
| 71 |
) |
| 72 |
) |
| 73 |
|
| 74 |
parser.current_clause.fields = [lexeme["string"]] |
| 75 |
|
| 76 |
next_lexeme = parser.peek_lexeme() |
| 77 |
if next_lexeme is None: |
| 78 |
raise QueryParseError("Expected term, found nothing") |
| 79 |
|
| 80 |
if next_lexeme["type"] == QueryLexer.TERM: |
| 81 |
return cls.parse_term |
| 82 |
else: |
| 83 |
raise QueryParseError("Expected term, found {}".format(next_lexeme["type"])) |
| 84 |
|
| 85 |
@classmethod |
| 86 |
def parse_term(cls, parser): |
| 87 |
lexeme = parser.consume_lexeme() |
| 88 |
|
| 89 |
parser.current_clause.term = lexeme["string"].lower() |
| 90 |
if "*" in lexeme["string"]: |
| 91 |
parser.current_clause.use_pipeline = False |
| 92 |
|
| 93 |
return cls._peek_next_lexeme(parser) |
| 94 |
|
| 95 |
@classmethod |
| 96 |
def parse_presence(cls, parser): |
| 97 |
lexeme = parser.consume_lexeme() |
| 98 |
|
| 99 |
if lexeme is None: |
| 100 |
return |
| 101 |
|
| 102 |
if lexeme["string"] == "-": |
| 103 |
parser.current_clause.presence = QueryPresence.PROHIBITED |
| 104 |
elif lexeme["string"] == "+": |
| 105 |
parser.current_clause.presence = QueryPresence.REQUIRED |
| 106 |
else: |
| 107 |
raise QueryParseError( |
| 108 |
"Unrecognized parser operator: {}, expected `+` or `-`".format( |
| 109 |
lexeme.str |
| 110 |
) |
| 111 |
) |
| 112 |
|
| 113 |
next_lexeme = parser.peek_lexeme() |
| 114 |
if next_lexeme is None: |
| 115 |
raise QueryParseError("Expected either a field or a term, found nothing") |
| 116 |
|
| 117 |
if next_lexeme["type"] == QueryLexer.FIELD: |
| 118 |
return cls.parse_field |
| 119 |
elif next_lexeme["type"] == QueryLexer.TERM: |
| 120 |
return cls.parse_term |
| 121 |
else: |
| 122 |
raise QueryParseError( |
| 123 |
"Expected either a field or a term, found {}".format(lexeme["type"]) |
| 124 |
) |
| 125 |
|
| 126 |
@classmethod |
| 127 |
def parse_edit_distance(cls, parser): |
| 128 |
lexeme = parser.consume_lexeme() |
| 129 |
|
| 130 |
try: |
| 131 |
edit_distance = int(lexeme["string"]) |
| 132 |
except ValueError as e: |
| 133 |
raise QueryParseError("Edit distance must be numeric") from e |
| 134 |
|
| 135 |
parser.current_clause.edit_distance = edit_distance |
| 136 |
|
| 137 |
return cls._peek_next_lexeme(parser) |
| 138 |
|
| 139 |
@classmethod |
| 140 |
def parse_boost(cls, parser): |
| 141 |
lexeme = parser.consume_lexeme() |
| 142 |
|
| 143 |
try: |
| 144 |
boost = int(lexeme["string"]) |
| 145 |
except ValueError as e: |
| 146 |
raise QueryParseError("Boost must be numeric") from e |
| 147 |
|
| 148 |
parser.current_clause.boost = boost |
| 149 |
|
| 150 |
return cls._peek_next_lexeme(parser) |
| 151 |
|
| 152 |
@classmethod |
| 153 |
def _peek_next_lexeme(cls, parser): |
| 154 |
next_lexeme = parser.peek_lexeme() |
| 155 |
if next_lexeme is None: |
| 156 |
parser.next_clause() |
| 157 |
return |
| 158 |
|
| 159 |
if next_lexeme["type"] == QueryLexer.TERM: |
| 160 |
parser.next_clause() |
| 161 |
return cls.parse_term |
| 162 |
elif next_lexeme["type"] == QueryLexer.FIELD: |
| 163 |
parser.next_clause() |
| 164 |
return cls.parse_field |
| 165 |
elif next_lexeme["type"] == QueryLexer.EDIT_DISTANCE: |
| 166 |
return cls.parse_edit_distance |
| 167 |
elif next_lexeme["type"] == QueryLexer.BOOST: |
| 168 |
return cls.parse_boost |
| 169 |
elif next_lexeme["type"] == QueryLexer.PRESENCE: |
| 170 |
parser.next_clause() |
| 171 |
return cls.parse_presence |
| 172 |
else: |
| 173 |
raise QueryParseError( |
| 174 |
"Unexpected lexeme type {}".format(next_lexeme["type"]) |
| 175 |
) |