|
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
|
|
|
|