This repository was archived by the owner on May 29, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpressionParser.php
More file actions
245 lines (188 loc) · 6.69 KB
/
ExpressionParser.php
File metadata and controls
245 lines (188 loc) · 6.69 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<?php
namespace Icecave\Dialekt\Parser;
use Icecave\Dialekt\AST\ExpressionInterface;
use Icecave\Dialekt\AST\LogicalAnd;
use Icecave\Dialekt\AST\LogicalNot;
use Icecave\Dialekt\AST\LogicalOr;
use Icecave\Dialekt\AST\Pattern;
use Icecave\Dialekt\AST\PatternLiteral;
use Icecave\Dialekt\AST\PatternWildcard;
use Icecave\Dialekt\AST\Tag;
class ExpressionParser extends AbstractParser
{
public function __construct()
{
parent::__construct();
$this->setLogicalOrByDefault(false);
}
/**
* Indicates whether or not the the default operator should be OR, rather
* than AND.
*
* @return boolean True if the default operator should be OR, rather than AND.
*/
public function logicalOrByDefault()
{
return $this->logicalOrByDefault;
}
/**
* Set whether or not the the default operator should be OR, rather than
* AND.
*
* @param boolean $logicalOrByDefault True if the default operator should be OR, rather than AND.
*/
public function setLogicalOrByDefault($logicalOrByDefault)
{
$this->logicalOrByDefault = $logicalOrByDefault;
}
protected function parseExpression()
{
$this->startExpression();
$expression = $this->parseUnaryExpression();
$expression = $this->parseCompoundExpression($expression);
$this->endExpression($expression);
return $expression;
}
private function parseUnaryExpression()
{
$this->expectToken(
Token::STRING,
Token::LOGICAL_NOT,
Token::OPEN_BRACKET
);
if (Token::LOGICAL_NOT === $this->currentToken->type) {
return $this->parseLogicalNot();
} elseif (Token::OPEN_BRACKET === $this->currentToken->type) {
return $this->parseNestedExpression();
} elseif (false === strpos($this->currentToken->value, $this->wildcardString())) {
return $this->parseTag();
} else {
return $this->parsePattern();
}
}
private function parseTag()
{
$this->startExpression();
$expression = new Tag(
$this->currentToken->value
);
$this->nextToken();
$this->endExpression($expression);
return $expression;
}
private function parsePattern()
{
$this->startExpression();
$parts = preg_split(
'/(' . preg_quote($this->wildcardString(), '/') . ')/',
$this->currentToken->value,
-1,
PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY
);
$expression = new Pattern();
foreach ($parts as $value) {
if ($this->wildcardString() === $value) {
$expression->add(new PatternWildcard());
} else {
$expression->add(new PatternLiteral($value));
}
}
$this->nextToken();
$this->endExpression($expression);
return $expression;
}
private function parseNestedExpression()
{
$this->startExpression();
$this->nextToken();
$expression = $this->parseExpression();
$this->expectToken(Token::CLOSE_BRACKET);
$this->nextToken();
$this->endExpression($expression);
return $expression;
}
private function parseLogicalNot()
{
$this->startExpression();
$this->nextToken();
$expression = new LogicalNot(
$this->parseUnaryExpression()
);
$this->endExpression($expression);
return $expression;
}
private function parseCompoundExpression(ExpressionInterface $leftExpression, $minimumPrecedence = 0)
{
$allowCollapse = false;
while (true) {
// Parse the operator and determine whether or not it's explicit ...
list($operator, $isExplicit) = $this->parseOperator();
$precedence = self::$operatorPrecedence[$operator];
// Abort if the operator's precedence is less than what we're looking for ...
if ($precedence < $minimumPrecedence) {
break;
}
// Advance the token pointer if an explicit operator token was found ...
if ($isExplicit) {
$this->nextToken();
}
// Parse the expression to the right of the operator ...
$rightExpression = $this->parseUnaryExpression();
// Only parse additional compound expressions if their precedence is greater than the
// expression already being parsed ...
list($nextOperator) = $this->parseOperator();
if ($precedence < self::$operatorPrecedence[$nextOperator]) {
$rightExpression = $this->parseCompoundExpression(
$rightExpression,
$precedence + 1
);
}
// Combine the parsed expression with the existing expression ...
$operatorClass = self::$operatorClasses[$operator];
// Collapse the expression into an existing expression of the same type ...
if ($allowCollapse && $leftExpression instanceof $operatorClass) {
$leftExpression->add($rightExpression);
} else {
$leftExpression = new $operatorClass(
$leftExpression,
$rightExpression
);
$allowCollapse = true;
}
}
return $leftExpression;
}
private function parseOperator()
{
// End of input ...
if (!$this->currentToken) {
return array(null, false);
// Closing bracket ...
} elseif (Token::CLOSE_BRACKET === $this->currentToken->type) {
return array(null, false);
// Explicit logical OR ...
} elseif (Token::LOGICAL_OR === $this->currentToken->type) {
return array(Token::LOGICAL_OR, true);
// Explicit logical AND ...
} elseif (Token::LOGICAL_AND === $this->currentToken->type) {
return array(Token::LOGICAL_AND, true);
// Implicit logical OR ...
} elseif ($this->logicalOrByDefault()) {
return array(Token::LOGICAL_OR, false);
// Implicit logical AND ...
} else {
return array(Token::LOGICAL_AND, false);
}
return array(null, false);
}
private static $operatorClasses = array(
Token::LOGICAL_AND => 'Icecave\Dialekt\AST\LogicalAnd',
Token::LOGICAL_OR => 'Icecave\Dialekt\AST\LogicalOr',
);
private static $operatorPrecedence = array(
Token::LOGICAL_AND => 1,
Token::LOGICAL_OR => 0,
null => -1,
);
private $logicalOrByDefault;
}