1 | <?php |
||
15 | class Parser |
||
16 | { |
||
17 | /** |
||
18 | * @var array |
||
19 | */ |
||
20 | public $variables = []; |
||
21 | |||
22 | /** |
||
23 | * @var null|mixed[] |
||
24 | */ |
||
25 | protected $values = null; |
||
26 | |||
27 | /** |
||
28 | * @var null|string |
||
29 | */ |
||
30 | protected $operator = null; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $output = ''; |
||
36 | |||
37 | /** |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $operatorRequired = false; |
||
41 | |||
42 | /** |
||
43 | * @var bool |
||
44 | */ |
||
45 | protected $incompleteCondition = false; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $openParenthesis = 0; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $closedParenthesis = 0; |
||
56 | |||
57 | /** |
||
58 | * @var TokenizerInterface |
||
59 | */ |
||
60 | protected $tokenizer; |
||
61 | |||
62 | /** |
||
63 | * @var Expressions\Factory |
||
64 | */ |
||
65 | protected $expressionFactory; |
||
66 | |||
67 | protected $userDefinedFunctions = []; |
||
68 | |||
69 | 226 | public function __construct(TokenizerInterface $tokenizer, Expressions\Factory $expressionFactory) |
|
70 | { |
||
71 | 226 | $this->tokenizer = $tokenizer; |
|
72 | 226 | $this->expressionFactory = $expressionFactory; |
|
73 | 226 | } |
|
74 | |||
75 | /** |
||
76 | * @throws Exceptions\ParserException |
||
77 | */ |
||
78 | 220 | public function parse(string $rule) : string |
|
79 | { |
||
80 | 220 | $this->output = ''; |
|
81 | 220 | $this->operator = null; |
|
82 | 220 | $this->values = null; |
|
83 | 220 | $this->operatorRequired = false; |
|
84 | |||
85 | 220 | foreach (new AST($this->tokenizer->tokenize($rule), $this) as $token) { |
|
86 | 182 | switch ($token->getGroup()) { |
|
87 | 182 | case Constants::GROUP_VALUE: |
|
88 | 178 | $this->assignVariableValueFromToken($token); |
|
89 | 178 | break; |
|
90 | 182 | case Constants::GROUP_LOGICAL: |
|
91 | 36 | $this->assignLogicalToken($token); |
|
92 | 36 | continue 2; |
|
93 | 182 | case Constants::GROUP_PARENTHESES: |
|
94 | 22 | $this->assignParentheses($token); |
|
95 | 20 | continue 2; |
|
96 | 182 | case Constants::GROUP_OPERATOR: |
|
97 | 178 | $this->assignOperator($token); |
|
98 | 176 | continue 2; |
|
99 | 180 | case Constants::GROUP_COMMENT: |
|
100 | 180 | case Constants::GROUP_SPACE: |
|
101 | 178 | continue 2; |
|
102 | default: |
||
103 | 6 | throw new Exceptions\ParserException(sprintf( |
|
104 | 6 | 'Unknown token "%s" at position %d on line %d', |
|
105 | 6 | $token->getValue(), |
|
106 | 6 | $token->getPosition(), |
|
107 | 6 | $token->getLine() |
|
108 | )); |
||
109 | } |
||
110 | |||
111 | 178 | $this->parseExpression(); |
|
112 | } |
||
113 | |||
114 | 160 | $this->assertSyntaxSeemsOkay(); |
|
115 | 154 | return $this->output; |
|
116 | } |
||
117 | |||
118 | 220 | public function assignVariables(array $variables) |
|
119 | { |
||
120 | 220 | $this->variables = $variables; |
|
121 | 220 | } |
|
122 | |||
123 | /** |
||
124 | * @param Tokens\BaseToken $token |
||
125 | * @throws Exceptions\ParserException |
||
126 | */ |
||
127 | 178 | protected function assignVariableValueFromToken(BaseToken $token) |
|
128 | { |
||
129 | 178 | if ($this->operatorRequired) { |
|
130 | 2 | throw new Exceptions\ParserException(sprintf( |
|
131 | 2 | 'Missing operator at position %d on line %d', |
|
132 | 2 | $token->getPosition(), |
|
133 | 2 | $token->getLine() |
|
134 | )); |
||
135 | } |
||
136 | |||
137 | 178 | $this->operatorRequired = !$this->operatorRequired; |
|
138 | 178 | $this->incompleteCondition = false; |
|
139 | |||
140 | 178 | if (!isset($this->values)) { |
|
141 | 178 | $this->values = [$token->getValue()]; |
|
142 | } else { |
||
143 | 174 | $this->values[] = $token->getValue(); |
|
144 | } |
||
145 | 178 | } |
|
146 | |||
147 | /** |
||
148 | * @throws Exceptions\ParserException |
||
149 | */ |
||
150 | 22 | protected function assignParentheses(BaseToken $token) |
|
178 | |||
179 | /** |
||
180 | * @throws Exceptions\ParserException |
||
181 | */ |
||
182 | 36 | protected function assignLogicalToken(BaseToken $token) |
|
183 | { |
||
184 | 36 | if (!$this->operatorRequired) { |
|
185 | 2 | throw new Exceptions\ParserException(sprintf( |
|
186 | 2 | 'Unexpected "%s" at position %d on line %d', |
|
187 | 2 | $token->getOriginalValue(), |
|
188 | 2 | $token->getPosition(), |
|
189 | 2 | $token->getLine() |
|
190 | )); |
||
191 | } |
||
192 | |||
193 | 36 | $this->output .= $token->getValue(); |
|
194 | 36 | $this->incompleteCondition = true; |
|
195 | 36 | $this->operatorRequired = false; |
|
196 | 36 | } |
|
197 | |||
198 | /** |
||
199 | * @throws Exceptions\ParserException |
||
200 | */ |
||
201 | 178 | protected function assignOperator(BaseToken $token) |
|
202 | { |
||
203 | 178 | if (isset($this->operator)) { |
|
204 | 2 | throw new Exceptions\ParserException(sprintf( |
|
205 | 2 | 'Unexpected "%s" at position %d on line %d', |
|
206 | 2 | $token->getOriginalValue(), |
|
207 | 2 | $token->getPosition(), |
|
208 | 2 | $token->getLine() |
|
209 | )); |
||
210 | 178 | } elseif (!isset($this->values)) { |
|
211 | 2 | throw new Exceptions\ParserException(sprintf( |
|
212 | 2 | 'Incomplete expression for token "%s" at position %d on line %d', |
|
213 | 2 | $token->getOriginalValue(), |
|
214 | 2 | $token->getPosition(), |
|
215 | 2 | $token->getLine() |
|
216 | )); |
||
217 | } |
||
218 | |||
219 | 176 | $this->operator = $token->getValue(); |
|
220 | 176 | $this->operatorRequired = false; |
|
221 | 176 | } |
|
222 | |||
223 | /** |
||
224 | * @throws Exceptions\ExpressionFactoryException |
||
225 | */ |
||
226 | 178 | protected function parseExpression() |
|
227 | { |
||
228 | 178 | if (!isset($this->operator) || count($this->values) <> 2) { |
|
229 | 178 | return; |
|
230 | } |
||
231 | |||
232 | 174 | $this->operatorRequired = true; |
|
233 | 174 | $expression = $this->expressionFactory->createFromOperator($this->operator); |
|
234 | 174 | $this->output .= (int) $expression->evaluate($this->values[0], $this->values[1]); |
|
235 | |||
236 | 172 | unset($this->operator, $this->values); |
|
237 | 172 | } |
|
238 | |||
239 | /** |
||
240 | * @throws Exceptions\ParserException |
||
241 | */ |
||
242 | 160 | protected function assertSyntaxSeemsOkay() |
|
243 | { |
||
244 | 160 | if ($this->incompleteCondition) { |
|
245 | 2 | throw new Exceptions\ParserException( |
|
246 | 2 | 'Incomplete and/or condition' |
|
247 | ); |
||
248 | 158 | } elseif ($this->openParenthesis > $this->closedParenthesis) { |
|
249 | 2 | throw new Exceptions\ParserException( |
|
250 | 2 | 'Missing closing parenthesis' |
|
251 | ); |
||
252 | 156 | } elseif (isset($this->operator) || (isset($this->values) && count($this->values) > 0)) { |
|
253 | 2 | throw new Exceptions\ParserException( |
|
254 | 2 | 'Incomplete expression' |
|
255 | ); |
||
256 | } |
||
257 | 154 | } |
|
258 | |||
259 | 2 | public function registerFunction(string $name, Closure $callback) |
|
260 | { |
||
261 | 2 | $this->userDefinedFunctions[$name] = $callback; |
|
262 | 2 | } |
|
263 | |||
264 | 2 | public function registerToken(string $token, string $regex, int $priority = 10) |
|
268 | |||
269 | /** |
||
270 | * @param string $name |
||
271 | * @return Closure|null |
||
272 | */ |
||
273 | 4 | public function getFunction(string $name) |
|
274 | { |
||
275 | 4 | return isset($this->userDefinedFunctions[$name]) |
|
276 | 2 | ? $this->userDefinedFunctions[$name] |
|
277 | 4 | : null; |
|
279 | } |
||
280 |