forked from Probesys/bileto
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParser.php
More file actions
209 lines (175 loc) · 6.25 KB
/
Parser.php
File metadata and controls
209 lines (175 loc) · 6.25 KB
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
<?php
// This file is part of Bileto.
// Copyright 2022-2026 Probesys
// SPDX-License-Identifier: AGPL-3.0-or-later
namespace App\SearchEngine\Query;
use App\SearchEngine\Query;
/**
* The LL grammar is defined by the following rules:
*
* S -> QUERY
*
* QUERY -> CONDITIONAL_QUERY
* QUERY -> CONDITIONAL_QUERY QUERY
* QUERY -> CONDITIONAL_QUERY end_of_query
*
* CONDITIONAL_QUERY -> or CONDITION
* CONDITIONAL_QUERY -> and CONDITION
*
* CONDITION -> CRITERIA
* CONDITION -> not CRITERIA
*
* CRITERIA -> TEXT_OR_LIST
* CRITERIA -> qualifier TEXT_OR_LIST
* CRITERIA -> open_bracket QUERY close_bracket
*
* TEXT_OR_LIST -> text
* TEXT_OR_LIST -> text comma TEXT_OR_LIST
*
* Each rule of the grammar is implemented by a method in the Parser class to
* make the code as clear as possible.
*
* @phpstan-import-type Token from Tokenizer
*/
class Parser
{
/** @var Token[] */
private array $tokens;
/** @param Token[] $tokens */
public function parse(array $tokens): Query
{
if (empty($tokens)) {
throw new \LogicException('The parser cannot be called with an empty list of tokens.');
}
if ($tokens[0]['type'] != TokenType::And) {
// We want to be sure that the first token is an AND (and not an
// OR), otherwise the query is invalid. If it has been an OR, it
// would be grammatically valid. Thus, we could improve the
// grammar, but I think that would make it more complicated for
// nothing.
// Also, the tokenizer makes sure to insert an AND at the
// beginning of the tokens, so this code should never be reached.
// But we're never too sure.
throw new \LogicException('An AND keyword is expected char 1');
}
$this->tokens = $tokens;
$query = new Query();
$this->ruleQuery($query);
return $query;
}
private function ruleQuery(Query $query): void
{
$this->ruleConditionalQuery($query);
$currentToken = $this->readToken();
if (
$currentToken['type'] === TokenType::Or ||
$currentToken['type'] === TokenType::And
) {
$this->ruleQuery($query);
} elseif ($currentToken['type'] === TokenType::EndOfQuery) {
$this->consumeToken(TokenType::EndOfQuery);
}
}
private function ruleConditionalQuery(Query $query): void
{
$currentToken = $this->readToken();
if ($currentToken['type'] === TokenType::Or) {
$this->consumeToken(TokenType::Or);
$this->ruleCondition($query, 'or');
} elseif ($currentToken['type'] === TokenType::And) {
$this->consumeToken(TokenType::And);
$this->ruleCondition($query, 'and');
} else {
// This is a LogicException (not SyntaxError) because the Tokenizer
// should insert AND keywords when expected.
$position = $currentToken['position'];
throw new \LogicException("An AND keyword is expected char {$position}");
}
}
/**
* @param value-of<Query\Condition::OPERATORS> $operator
*/
private function ruleCondition(Query $query, string $operator): void
{
$currentToken = $this->readToken();
if ($currentToken['type'] === TokenType::Not) {
$this->consumeToken(TokenType::Not);
$this->ruleCriteria($query, $operator, true);
} else {
$this->ruleCriteria($query, $operator, false);
}
}
/**
* @param value-of<Query\Condition::OPERATORS> $operator
*/
private function ruleCriteria(Query $query, string $operator, bool $not): void
{
$currentToken = $this->readToken();
if ($currentToken['type'] === TokenType::Text) {
$value = $this->ruleTextOrList();
$condition = Query\Condition::textCondition($operator, $value, $not);
$query->addCondition($condition);
} elseif ($currentToken['type'] === TokenType::Qualifier) {
$this->consumeToken(TokenType::Qualifier);
assert(isset($currentToken['value']));
$value = $this->ruleTextOrList();
$condition = Query\Condition::qualifierCondition($operator, $currentToken['value'], $value, $not);
$query->addCondition($condition);
} elseif ($currentToken['type'] === TokenType::OpenBracket) {
$this->consumeToken(TokenType::OpenBracket);
$subQuery = new Query();
$this->ruleQuery($subQuery);
$condition = Query\Condition::queryCondition($operator, $subQuery, $not);
$query->addCondition($condition);
$this->consumeToken(TokenType::CloseBracket);
} else {
throw SyntaxError::tokenUnexpected(
$currentToken['position'],
$currentToken,
);
}
}
/**
* @return non-empty-string|non-empty-string[]
*/
private function ruleTextOrList(): mixed
{
$currentToken = $this->readToken();
$this->consumeToken(TokenType::Text);
assert(isset($currentToken['value']));
$nextToken = $this->readToken();
if ($nextToken['type'] === TokenType::Comma) {
$this->consumeToken(TokenType::Comma);
$textOrList = $this->ruleTextOrList();
if (is_array($textOrList)) {
return array_merge([$currentToken['value']], $textOrList);
} else {
return [$currentToken['value'], $textOrList];
}
} else {
return $currentToken['value'];
}
}
/**
* @return Token
*/
private function readToken(): array
{
$token = reset($this->tokens);
if ($token === false) {
throw new \LogicException('The parser expected a token to be present but the list is empty.');
}
return $token;
}
private function consumeToken(TokenType $expectedTokenType): void
{
$currentToken = $this->readToken();
if ($currentToken['type'] !== $expectedTokenType) {
throw SyntaxError::tokenUnexpected(
$currentToken['position'],
$currentToken,
);
}
array_shift($this->tokens);
}
}