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
|
|
|
/** |
35
|
|
|
* @var Mailcode_Parser_Statement_Info_Keywords |
36
|
|
|
*/ |
37
|
|
|
protected $keywords; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var Mailcode_Parser_Statement_Info_Variables |
41
|
|
|
*/ |
42
|
|
|
protected $variables; |
43
|
|
|
|
44
|
|
|
public function __construct(Mailcode_Parser_Statement_Tokenizer $tokenizer) |
45
|
|
|
{ |
46
|
|
|
$this->tokenizer = $tokenizer; |
47
|
|
|
$this->keywords = new Mailcode_Parser_Statement_Info_Keywords($this, $this->tokenizer); |
48
|
|
|
$this->variables = new Mailcode_Parser_Statement_Info_Variables($this, $this->tokenizer); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Whether the whole statement is a variable being assigned a value. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
|
|
public function isVariableAssignment() : bool |
57
|
|
|
{ |
58
|
|
|
return $this->variables->isAssignment(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Whether the whole statement is a variable being compared to something. |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
|
|
public function isVariableComparison() : bool |
67
|
|
|
{ |
68
|
|
|
return $this->variables->isComparison(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Retrieves all variables used in the statement. |
73
|
|
|
* |
74
|
|
|
* @return Mailcode_Variables_Variable[] |
75
|
|
|
* @throws Mailcode_Parser_Exception |
76
|
|
|
*/ |
77
|
|
|
public function getVariables() : array |
78
|
|
|
{ |
79
|
|
|
return $this->variables->getAll(); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieves a variable by its position in the command's parameters. |
84
|
|
|
* Returns null if there is no parameter at the specified index, or |
85
|
|
|
* if it is of another type. |
86
|
|
|
* |
87
|
|
|
* @param int $index Zero-based index. |
88
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Variable|NULL |
89
|
|
|
*/ |
90
|
|
|
public function getVariableByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Variable |
91
|
|
|
{ |
92
|
|
|
return $this->variables->getByIndex($index); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Retrieves a string literal by its position in the command's parameters. |
97
|
|
|
* Returns null if there is no parameter at the specified index, or |
98
|
|
|
* if it is of another type. |
99
|
|
|
* |
100
|
|
|
* @param int $index Zero-based index. |
101
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral|NULL |
102
|
|
|
*/ |
103
|
|
|
public function getStringLiteralByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
104
|
|
|
{ |
105
|
|
|
$token = $this->getTokenByIndex($index); |
106
|
|
|
|
107
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) |
108
|
|
|
{ |
109
|
|
|
return $token; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
return null; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Retrieves a keyword by its position in the command's parameters. |
117
|
|
|
* Returns null if there is no parameter at the specified index, or |
118
|
|
|
* if it is of another type. |
119
|
|
|
* |
120
|
|
|
* @param int $index Zero-based index. |
121
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword|NULL |
122
|
|
|
*/ |
123
|
|
|
public function getKeywordByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Keyword |
124
|
|
|
{ |
125
|
|
|
return $this->keywords->getByIndex($index); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Retrieves an operand by its position in the command's parameters. |
130
|
|
|
* Returns null if there is no parameter at the specified index, or |
131
|
|
|
* if it is of another type. |
132
|
|
|
* |
133
|
|
|
* @param int $index Zero-based index. |
134
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Operand|NULL |
135
|
|
|
*/ |
136
|
|
|
public function getOperandByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token_Operand |
137
|
|
|
{ |
138
|
|
|
$token = $this->getTokenByIndex($index); |
139
|
|
|
|
140
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_Operand) |
141
|
|
|
{ |
142
|
|
|
return $token; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return null; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Retrieves a parameter token by its position in the command's parameters, |
150
|
|
|
* regardless of its type. Returns null if there is no parameter at the |
151
|
|
|
* specified index. |
152
|
|
|
* |
153
|
|
|
* @param int $index Zero-based index. |
154
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token|NULL |
155
|
|
|
*/ |
156
|
|
|
public function getTokenByIndex(int $index) : ?Mailcode_Parser_Statement_Tokenizer_Token |
157
|
|
|
{ |
158
|
|
|
$tokens = $this->tokenizer->getTokens(); |
159
|
|
|
|
160
|
|
|
if(isset($tokens[$index])) |
161
|
|
|
{ |
162
|
|
|
return $tokens[$index]; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
return null; |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
public function hasTokenAtIndex(int $index) : bool |
169
|
|
|
{ |
170
|
|
|
$tokens = $this->tokenizer->getTokens(); |
171
|
|
|
|
172
|
|
|
return isset($tokens[$index]); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Retrieves all tokens. |
177
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token[] |
178
|
|
|
*/ |
179
|
|
|
public function getTokens() : array |
180
|
|
|
{ |
181
|
|
|
return $this->tokenizer->getTokens(); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Retrieves all string literals that were found in the command. |
186
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral[] |
187
|
|
|
*/ |
188
|
|
|
public function getStringLiterals() : array |
189
|
|
|
{ |
190
|
|
|
$result = array(); |
191
|
|
|
$tokens = $this->tokenizer->getTokens(); |
192
|
|
|
|
193
|
|
|
foreach($tokens as $token) |
194
|
|
|
{ |
195
|
|
|
if($token instanceof Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral) |
196
|
|
|
{ |
197
|
|
|
$result[] = $token; |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $result; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function createPruner() : Mailcode_Parser_Statement_Info_Pruner |
205
|
|
|
{ |
206
|
|
|
return new Mailcode_Parser_Statement_Info_Pruner($this); |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* @return Mailcode_Parser_Statement_Tokenizer_Token_Keyword[] |
211
|
|
|
*/ |
212
|
|
|
public function getKeywords() : array |
213
|
|
|
{ |
214
|
|
|
return $this->keywords->getAll(); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
public function getKeywordsCollection() : Mailcode_Parser_Statement_Info_Keywords |
218
|
|
|
{ |
219
|
|
|
return $this->keywords; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Adds or removes a keyword depending on whether it should be enabled. |
224
|
|
|
* |
225
|
|
|
* @param string $keyword The keyword name, with or without : |
226
|
|
|
* @param bool $enabled |
227
|
|
|
* @return Mailcode_Parser_Statement_Info |
228
|
|
|
* @throws Mailcode_Parser_Exception |
229
|
|
|
*/ |
230
|
|
|
public function setKeywordEnabled(string $keyword, bool $enabled) : Mailcode_Parser_Statement_Info |
231
|
|
|
{ |
232
|
|
|
$this->keywords->setEnabled($keyword, $enabled); |
233
|
|
|
|
234
|
|
|
return $this; |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
/** |
238
|
|
|
* Adds a keyword to the command. |
239
|
|
|
* |
240
|
|
|
* @param string $keyword Keyword name, with or without : |
241
|
|
|
* @return $this |
242
|
|
|
* @throws Mailcode_Parser_Exception |
243
|
|
|
*/ |
244
|
|
|
public function addKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
245
|
|
|
{ |
246
|
|
|
$this->keywords->add($keyword); |
247
|
|
|
|
248
|
|
|
return $this; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* Removes a keyword from the command, if it has one. |
253
|
|
|
* Has no effect otherwise. |
254
|
|
|
* |
255
|
|
|
* @param string $keyword Keyword name, with or without : |
256
|
|
|
* @return $this |
257
|
|
|
*/ |
258
|
|
|
public function removeKeyword(string $keyword) : Mailcode_Parser_Statement_Info |
259
|
|
|
{ |
260
|
|
|
$this->keywords->remove($keyword); |
261
|
|
|
|
262
|
|
|
return $this; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
/** |
266
|
|
|
* Whether the command has the specified keyword. |
267
|
|
|
* |
268
|
|
|
* @param string $keyword Keyword name, with or without : |
269
|
|
|
* @return bool |
270
|
|
|
*/ |
271
|
|
|
public function hasKeyword(string $keyword) : bool |
272
|
|
|
{ |
273
|
|
|
return $this->keywords->hasKeyword($keyword); |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
public function removeToken(Mailcode_Parser_Statement_Tokenizer_Token $token) : void |
277
|
|
|
{ |
278
|
|
|
$this->tokenizer->removeToken($token); |
279
|
|
|
} |
280
|
|
|
|
281
|
|
|
public function addStringLiteral(string $text) : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
282
|
|
|
{ |
283
|
|
|
return $this->tokenizer->appendStringLiteral($text); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function prependStringLiteral(string $text) : Mailcode_Parser_Statement_Tokenizer_Token_StringLiteral |
287
|
|
|
{ |
288
|
|
|
return $this->tokenizer->prependStringLiteral($text); |
289
|
|
|
} |
290
|
|
|
} |
291
|
|
|
|