1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* File containing the {@see Mailcode_Parser_Statement_Tokenizer} class. |
4
|
|
|
* |
5
|
|
|
* @package Mailcode |
6
|
|
|
* @subpackage Parser |
7
|
|
|
* @see Mailcode_Parser_Statement_Tokenizer |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Mailcode; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Mailcode statement tokenizer: parses a mailcode statement |
16
|
|
|
* into its logical parts. |
17
|
|
|
* |
18
|
|
|
* @package Mailcode |
19
|
|
|
* @subpackage Parser |
20
|
|
|
* @author Sebastian Mordziol <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Mailcode_Parser_Statement_Info |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer |
26
|
|
|
*/ |
27
|
|
|
protected $tokenizer; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var Mailcode_Parser_Statement_Tokenizer_Token[] |
31
|
|
|
*/ |
32
|
|
|
protected $tokens = array(); |
33
|
|
|
|
34
|
|
|
public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer) |
35
|
|
|
{ |
36
|
|
|
$this->tokenizer = $tokenizer; |
37
|
|
|
$this->tokens = $this->tokenizer->getTokens(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Whether the whole statement is a variable being assigned a value. |
42
|
|
|
* |
43
|
|
|
* @return bool |
44
|
|
|
*/ |
45
|
|
|
public function isVariableAssignment() : bool |
46
|
|
|
{ |
47
|
|
|
$variable = $this->getVariableByIndex(0); |
48
|
|
|
$operand = $this->getOperandByIndex(1); |
49
|
|
|
$value = $this->getTokenByIndex(2); |
50
|
|
|
|
51
|
|
|
if($variable && $operand && $value && $operand->isAssignment()) |
52
|
|
|
{ |
53
|
|
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Whether the whole statement is a variable being compared to something. |
61
|
|
|
* |
62
|
|
|
* @return bool |
63
|
|
|
*/ |
64
|
|
|
public function isVariableComparison() : bool |
65
|
|
|
{ |
66
|
|
|
$variable = $this->getVariableByIndex(0); |
67
|
|
|
$operand = $this->getOperandByIndex(1); |
68
|
|
|
$value = $this->getTokenByIndex(2); |
69
|
|
|
|
70
|
|
|
if($variable && $operand && $value && $operand->isComparator()) |
71
|
|
|
{ |
72
|
|
|
return true; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return false; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieves all variables used in the statement. |
80
|
|
|
* |
81
|
|
|
* @return \Mailcode\Mailcode_Variables_Variable[] |
82
|
|
|
*/ |
83
|
|
|
public function getVariables() |
84
|
|
|
{ |
85
|
|
|
$result = array(); |
86
|
|
|
|
87
|
|
|
foreach($this->tokens as $token) |
88
|
|
|
{ |
89
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) |
90
|
|
|
{ |
91
|
|
|
$result[] = $token->getVariable(); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
return $result; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieves a variable by its position in the command's parameters. |
100
|
|
|
* Returns null if there is no parameter at the specified index, or |
101
|
|
|
* if it is of another type. |
102
|
|
|
* |
103
|
|
|
* @param int $index Zero-based index. |
104
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
105
|
|
|
*/ |
106
|
|
|
public function getVariableByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable |
107
|
|
|
{ |
108
|
|
|
$token = $this->getTokenByIndex($index); |
109
|
|
|
|
110
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Variable) |
111
|
|
|
{ |
112
|
|
|
return $token; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return null; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retrieves a string literal by its position in the command's parameters. |
120
|
|
|
* Returns null if there is no parameter at the specified index, or |
121
|
|
|
* if it is of another type. |
122
|
|
|
* |
123
|
|
|
* @param int $index Zero-based index. |
124
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL |
125
|
|
|
*/ |
126
|
|
|
public function getStringLiteralByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
127
|
|
|
{ |
128
|
|
|
$token = $this->getTokenByIndex($index); |
129
|
|
|
|
130
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) |
131
|
|
|
{ |
132
|
|
|
return $token; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Retrieves a keyword by its position in the command's parameters. |
140
|
|
|
* Returns null if there is no parameter at the specified index, or |
141
|
|
|
* if it is of another type. |
142
|
|
|
* |
143
|
|
|
* @param int $index Zero-based index. |
144
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL |
145
|
|
|
*/ |
146
|
|
|
public function getKeywordByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword |
147
|
|
|
{ |
148
|
|
|
$token = $this->getTokenByIndex($index); |
149
|
|
|
|
150
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) |
151
|
|
|
{ |
152
|
|
|
return $token; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
return null; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* Retrieves an operand by its position in the command's parameters. |
160
|
|
|
* Returns null if there is no parameter at the specified index, or |
161
|
|
|
* if it is of another type. |
162
|
|
|
* |
163
|
|
|
* @param int $index Zero-based index. |
164
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL |
165
|
|
|
*/ |
166
|
|
|
public function getOperandByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Operand |
167
|
|
|
{ |
168
|
|
|
$token = $this->getTokenByIndex($index); |
169
|
|
|
|
170
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) |
171
|
|
|
{ |
172
|
|
|
return $token; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
return null; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Retrieves a parameter token by its position in the command's parameters, |
180
|
|
|
* regardless of its type. Returns null if there is no parameter at the |
181
|
|
|
* specified index. |
182
|
|
|
* |
183
|
|
|
* @param int $index Zero-based index. |
184
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token|NULL |
185
|
|
|
*/ |
186
|
|
|
public function getTokenByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token |
187
|
|
|
{ |
188
|
|
|
if(isset($this->tokens[$index])) |
189
|
|
|
{ |
190
|
|
|
return $this->tokens[$index]; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
return null; |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
public function hasTokenAtIndex(int $index) : bool |
197
|
|
|
{ |
198
|
|
|
return isset($this->tokens[$index]); |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
/** |
202
|
|
|
* Retrieves all tokens. |
203
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token[] |
204
|
|
|
*/ |
205
|
|
|
public function getTokens() : array |
206
|
|
|
{ |
207
|
|
|
return $this->tokens; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Retrieves all string literals that were found in the command. |
212
|
|
|
* @return \Mailcode\Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] |
213
|
|
|
*/ |
214
|
|
|
public function getStringLiterals() |
215
|
|
|
{ |
216
|
|
|
$result = array(); |
217
|
|
|
|
218
|
|
|
foreach($this->tokens as $token) |
219
|
|
|
{ |
220
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) |
221
|
|
|
{ |
222
|
|
|
$result[] = $token; |
223
|
|
|
} |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
return $result; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
public function createPruner() : Mailcode_Parser_Statement_Info_Pruner |
230
|
|
|
{ |
231
|
|
|
return new Mailcode_Parser_Statement_Info_Pruner($this); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[] |
236
|
|
|
*/ |
237
|
|
|
public function getKeywords() : array |
238
|
|
|
{ |
239
|
|
|
$result = array(); |
240
|
|
|
|
241
|
|
|
foreach($this->tokens as $token) |
242
|
|
|
{ |
243
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Keyword) |
244
|
|
|
{ |
245
|
|
|
$result[] = $token; |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
return $result; |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* Adds or removes a keyword dependin on whether it should be enabled. |
254
|
|
|
* |
255
|
|
|
* @param string $keyword The keyword name, with or without : |
256
|
|
|
* @param bool $enabled |
257
|
|
|
* @return Mailcode_Parser_Statement_Info |
258
|
|
|
* @throws Mailcode_Exception |
259
|
|
|
*/ |
260
|
|
|
public function setKeywordEnabled(string $keyword, bool $enabled) : Mailcode_Parser_Statement_Info |
261
|
|
|
{ |
262
|
|
|
if($enabled) |
263
|
|
|
{ |
264
|
|
|
return $this->addKeyword($keyword); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
return $this->removeKeyword($keyword); |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
/** |
271
|
|
|
* Adds a keyword to the command. |
272
|
|
|
* |
273
|
|
|
* @param string $keyword Keyword name, with or without : |
274
|
|
|
* @return $this |
275
|
|
|
* @throws Mailcode_Exception |
276
|
|
|
*/ |
277
|
|
|
protected function addKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
278
|
|
|
{ |
279
|
|
|
$keyword = rtrim($keyword, ':').':'; |
280
|
|
|
|
281
|
|
|
if(!$this->hasKeyword($keyword)) |
282
|
|
|
{ |
283
|
|
|
$this->tokenizer->appendKeyword($keyword); |
284
|
|
|
$this->tokens = $this->tokenizer->getTokens(); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
return $this; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Removes a keyword from the command, if it has one. |
292
|
|
|
* Has no effect otherwise. |
293
|
|
|
* |
294
|
|
|
* @param string $keyword Keyword name, with or without : |
295
|
|
|
* @return $this |
296
|
|
|
*/ |
297
|
|
|
public function removeKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
298
|
|
|
{ |
299
|
|
|
$keyword = rtrim($keyword, ':').':'; |
300
|
|
|
$keywords = $this->getKeywords(); |
301
|
|
|
|
302
|
|
|
foreach ($keywords as $kw) |
303
|
|
|
{ |
304
|
|
|
if ($kw->getKeyword() !== $keyword) { |
305
|
|
|
continue; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
$this->tokenizer->removeToken($kw); |
309
|
|
|
$this->tokens = $this->tokenizer->getTokens(); |
310
|
|
|
} |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
/** |
316
|
|
|
* Whether the command has the specified keyword. |
317
|
|
|
* |
318
|
|
|
* @param string $keyword Keyword name, with or without : |
319
|
|
|
* @return bool |
320
|
|
|
*/ |
321
|
|
|
public function hasKeyword(string $keyword) : bool |
322
|
|
|
{ |
323
|
|
|
$keyword = rtrim($keyword, ':').':'; |
324
|
|
|
$keywords = $this->getKeywords(); |
325
|
|
|
|
326
|
|
|
foreach ($keywords as $kw) |
327
|
|
|
{ |
328
|
|
|
if($kw->getKeyword() === $keyword) |
329
|
|
|
{ |
330
|
|
|
return true; |
331
|
|
|
} |
332
|
|
|
} |
333
|
|
|
|
334
|
|
|
return false; |
335
|
|
|
} |
336
|
|
|
} |
337
|
|
|
|