|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace PhpParser; |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This parser is based on a skeleton written by Moriyoshi Koizumi, which in |
|
7
|
|
|
* turn is based on work by Masato Bito. |
|
8
|
|
|
*/ |
|
9
|
|
|
use PhpParser\Node\Name; |
|
10
|
|
|
|
|
11
|
|
|
abstract class ParserAbstract implements Parser |
|
12
|
|
|
{ |
|
13
|
|
|
const SYMBOL_NONE = -1; |
|
14
|
|
|
|
|
15
|
|
|
/* |
|
16
|
|
|
* The following members will be filled with generated parsing data: |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
/** @var int Size of $tokenToSymbol map */ |
|
20
|
|
|
protected $tokenToSymbolMapSize; |
|
21
|
|
|
/** @var int Size of $action table */ |
|
22
|
|
|
protected $actionTableSize; |
|
23
|
|
|
/** @var int Size of $goto table */ |
|
24
|
|
|
protected $gotoTableSize; |
|
25
|
|
|
|
|
26
|
|
|
/** @var int Symbol number signifying an invalid token */ |
|
27
|
|
|
protected $invalidSymbol; |
|
28
|
|
|
/** @var int Symbol number of error recovery token */ |
|
29
|
|
|
protected $errorSymbol; |
|
30
|
|
|
/** @var int Action number signifying default action */ |
|
31
|
|
|
protected $defaultAction; |
|
32
|
|
|
/** @var int Rule number signifying that an unexpected token was encountered */ |
|
33
|
|
|
protected $unexpectedTokenRule; |
|
34
|
|
|
|
|
35
|
|
|
protected $YY2TBLSTATE; |
|
36
|
|
|
protected $YYNLSTATES; |
|
37
|
|
|
|
|
38
|
|
|
/** @var array Map of lexer tokens to internal symbols */ |
|
39
|
|
|
protected $tokenToSymbol; |
|
40
|
|
|
/** @var array Map of symbols to their names */ |
|
41
|
|
|
protected $symbolToName; |
|
42
|
|
|
/** @var array Names of the production rules (only necessary for debugging) */ |
|
43
|
|
|
protected $productions; |
|
44
|
|
|
|
|
45
|
|
|
/** @var array Map of states to a displacement into the $action table. The corresponding action for this |
|
46
|
|
|
* state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the |
|
47
|
|
|
action is defaulted, i.e. $actionDefault[$state] should be used instead. */ |
|
48
|
|
|
protected $actionBase; |
|
49
|
|
|
/** @var array Table of actions. Indexed according to $actionBase comment. */ |
|
50
|
|
|
protected $action; |
|
51
|
|
|
/** @var array Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol |
|
52
|
|
|
* then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */ |
|
53
|
|
|
protected $actionCheck; |
|
54
|
|
|
/** @var array Map of states to their default action */ |
|
55
|
|
|
protected $actionDefault; |
|
56
|
|
|
|
|
57
|
|
|
/** @var array Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this |
|
58
|
|
|
* non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */ |
|
59
|
|
|
protected $gotoBase; |
|
60
|
|
|
/** @var array Table of states to goto after reduction. Indexed according to $gotoBase comment. */ |
|
61
|
|
|
protected $goto; |
|
62
|
|
|
/** @var array Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal |
|
63
|
|
|
* then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */ |
|
64
|
|
|
protected $gotoCheck; |
|
65
|
|
|
/** @var array Map of non-terminals to the default state to goto after their reduction */ |
|
66
|
|
|
protected $gotoDefault; |
|
67
|
|
|
|
|
68
|
|
|
/** @var array Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for |
|
69
|
|
|
* determining the state to goto after reduction. */ |
|
70
|
|
|
protected $ruleToNonTerminal; |
|
71
|
|
|
/** @var array Map of rules to the length of their right-hand side, which is the number of elements that have to |
|
72
|
|
|
* be popped from the stack(s) on reduction. */ |
|
73
|
|
|
protected $ruleToLength; |
|
74
|
|
|
|
|
75
|
|
|
/* |
|
76
|
|
|
* The following members are part of the parser state: |
|
77
|
|
|
*/ |
|
78
|
|
|
|
|
79
|
|
|
/** @var Lexer Lexer that is used when parsing */ |
|
80
|
|
|
protected $lexer; |
|
81
|
|
|
/** @var mixed Temporary value containing the result of last semantic action (reduction) */ |
|
82
|
|
|
protected $semValue; |
|
83
|
|
|
/** @var int Position in stacks (state stack, semantic value stack, attribute stack) */ |
|
84
|
|
|
protected $stackPos; |
|
85
|
|
|
/** @var array Semantic value stack (contains values of tokens and semantic action results) */ |
|
86
|
|
|
protected $semStack; |
|
87
|
|
|
/** @var array[] Start attribute stack */ |
|
88
|
|
|
protected $startAttributeStack; |
|
89
|
|
|
/** @var array End attributes of last *shifted* token */ |
|
90
|
|
|
protected $endAttributes; |
|
91
|
|
|
|
|
92
|
|
|
/** @var bool Whether to throw on first error */ |
|
93
|
|
|
protected $throwOnError; |
|
94
|
|
|
/** @var Error[] Errors collected during last parse */ |
|
95
|
|
|
protected $errors; |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* Creates a parser instance. |
|
99
|
|
|
* |
|
100
|
|
|
* @param Lexer $lexer A lexer |
|
101
|
|
|
* @param array $options Options array. The boolean 'throwOnError' option determines whether an exception should be |
|
102
|
|
|
* thrown on first error, or if the parser should try to continue parsing the remaining code |
|
103
|
|
|
* and build a partial AST. |
|
104
|
|
|
*/ |
|
105
|
|
|
public function __construct(Lexer $lexer, array $options = array()) { |
|
106
|
|
|
$this->lexer = $lexer; |
|
107
|
|
|
$this->errors = array(); |
|
108
|
|
|
$this->throwOnError = isset($options['throwOnError']) ? $options['throwOnError'] : true; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Get array of errors that occurred during the last parse. |
|
113
|
|
|
* |
|
114
|
|
|
* This method may only return multiple errors if the 'throwOnError' option is disabled. |
|
115
|
|
|
* |
|
116
|
|
|
* @return Error[] |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getErrors() { |
|
119
|
|
|
return $this->errors; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* Parses PHP code into a node tree. |
|
124
|
|
|
* |
|
125
|
|
|
* @param string $code The source code to parse |
|
126
|
|
|
* |
|
127
|
|
|
* @return Node[]|null Array of statements (or null if the 'throwOnError' option is disabled and the parser was |
|
128
|
|
|
* unable to recover from an error). |
|
129
|
|
|
*/ |
|
130
|
|
|
public function parse($code) { |
|
131
|
|
|
$this->lexer->startLexing($code); |
|
132
|
|
|
$this->errors = array(); |
|
133
|
|
|
|
|
134
|
|
|
// We start off with no lookahead-token |
|
135
|
|
|
$symbol = self::SYMBOL_NONE; |
|
136
|
|
|
|
|
137
|
|
|
// The attributes for a node are taken from the first and last token of the node. |
|
138
|
|
|
// From the first token only the startAttributes are taken and from the last only |
|
139
|
|
|
// the endAttributes. Both are merged using the array union operator (+). |
|
140
|
|
|
$startAttributes = '*POISON'; |
|
141
|
|
|
$endAttributes = '*POISON'; |
|
142
|
|
|
$this->endAttributes = $endAttributes; |
|
|
|
|
|
|
143
|
|
|
|
|
144
|
|
|
// In order to figure out the attributes for the starting token, we have to keep |
|
145
|
|
|
// them in a stack |
|
146
|
|
|
$this->startAttributeStack = array(); |
|
147
|
|
|
|
|
148
|
|
|
// Start off in the initial state and keep a stack of previous states |
|
149
|
|
|
$state = 0; |
|
150
|
|
|
$stateStack = array($state); |
|
151
|
|
|
|
|
152
|
|
|
// Semantic value stack (contains values of tokens and semantic action results) |
|
153
|
|
|
$this->semStack = array(); |
|
154
|
|
|
|
|
155
|
|
|
// Current position in the stack(s) |
|
156
|
|
|
$this->stackPos = 0; |
|
157
|
|
|
|
|
158
|
|
|
$errorState = 0; |
|
159
|
|
|
|
|
160
|
|
|
for (;;) { |
|
161
|
|
|
//$this->traceNewState($state, $symbol); |
|
|
|
|
|
|
162
|
|
|
|
|
163
|
|
|
if ($this->actionBase[$state] == 0) { |
|
164
|
|
|
$rule = $this->actionDefault[$state]; |
|
165
|
|
|
} else { |
|
166
|
|
|
if ($symbol === self::SYMBOL_NONE) { |
|
167
|
|
|
// Fetch the next token id from the lexer and fetch additional info by-ref. |
|
168
|
|
|
// The end attributes are fetched into a temporary variable and only set once the token is really |
|
169
|
|
|
// shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is |
|
170
|
|
|
// reduced after a token was read but not yet shifted. |
|
171
|
|
|
$tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes); |
|
172
|
|
|
|
|
173
|
|
|
// map the lexer token id to the internally used symbols |
|
174
|
|
|
$symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize |
|
175
|
|
|
? $this->tokenToSymbol[$tokenId] |
|
176
|
|
|
: $this->invalidSymbol; |
|
177
|
|
|
|
|
178
|
|
|
if ($symbol === $this->invalidSymbol) { |
|
179
|
|
|
throw new \RangeException(sprintf( |
|
180
|
|
|
'The lexer returned an invalid token (id=%d, value=%s)', |
|
181
|
|
|
$tokenId, $tokenValue |
|
182
|
|
|
)); |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
// This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get |
|
186
|
|
|
// the attributes of the next token, even though they don't contain it themselves. |
|
187
|
|
|
$this->startAttributeStack[$this->stackPos+1] = $startAttributes; |
|
188
|
|
|
|
|
189
|
|
|
//$this->traceRead($symbol); |
|
|
|
|
|
|
190
|
|
|
} |
|
191
|
|
|
|
|
192
|
|
|
$idx = $this->actionBase[$state] + $symbol; |
|
193
|
|
|
if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol) |
|
194
|
|
|
|| ($state < $this->YY2TBLSTATE |
|
195
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0 |
|
196
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol)) |
|
197
|
|
|
&& ($action = $this->action[$idx]) != $this->defaultAction) { |
|
198
|
|
|
/* |
|
199
|
|
|
* >= YYNLSTATES: shift and reduce |
|
200
|
|
|
* > 0: shift |
|
201
|
|
|
* = 0: accept |
|
202
|
|
|
* < 0: reduce |
|
203
|
|
|
* = -YYUNEXPECTED: error |
|
204
|
|
|
*/ |
|
205
|
|
|
if ($action > 0) { |
|
206
|
|
|
/* shift */ |
|
207
|
|
|
//$this->traceShift($symbol); |
|
|
|
|
|
|
208
|
|
|
|
|
209
|
|
|
++$this->stackPos; |
|
210
|
|
|
$stateStack[$this->stackPos] = $state = $action; |
|
211
|
|
|
$this->semStack[$this->stackPos] = $tokenValue; |
|
|
|
|
|
|
212
|
|
|
$this->startAttributeStack[$this->stackPos] = $startAttributes; |
|
213
|
|
|
$this->endAttributes = $endAttributes; |
|
|
|
|
|
|
214
|
|
|
$symbol = self::SYMBOL_NONE; |
|
215
|
|
|
|
|
216
|
|
|
if ($errorState) { |
|
217
|
|
|
--$errorState; |
|
218
|
|
|
} |
|
219
|
|
|
|
|
220
|
|
|
if ($action < $this->YYNLSTATES) { |
|
221
|
|
|
continue; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/* $yyn >= YYNLSTATES means shift-and-reduce */ |
|
225
|
|
|
$rule = $action - $this->YYNLSTATES; |
|
226
|
|
|
} else { |
|
227
|
|
|
$rule = -$action; |
|
228
|
|
|
} |
|
229
|
|
|
} else { |
|
230
|
|
|
$rule = $this->actionDefault[$state]; |
|
231
|
|
|
} |
|
232
|
|
|
} |
|
233
|
|
|
|
|
234
|
|
|
for (;;) { |
|
235
|
|
|
if ($rule === 0) { |
|
236
|
|
|
/* accept */ |
|
237
|
|
|
//$this->traceAccept(); |
|
|
|
|
|
|
238
|
|
|
return $this->semValue; |
|
239
|
|
|
} elseif ($rule !== $this->unexpectedTokenRule) { |
|
240
|
|
|
/* reduce */ |
|
241
|
|
|
//$this->traceReduce($rule); |
|
|
|
|
|
|
242
|
|
|
|
|
243
|
|
|
try { |
|
244
|
|
|
$this->{'reduceRule' . $rule}(); |
|
245
|
|
|
} catch (Error $e) { |
|
246
|
|
|
if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) { |
|
247
|
|
|
$e->setStartLine($startAttributes['startLine']); |
|
248
|
|
|
} |
|
249
|
|
|
|
|
250
|
|
|
$this->errors[] = $e; |
|
251
|
|
|
if ($this->throwOnError) { |
|
252
|
|
|
throw $e; |
|
253
|
|
|
} else { |
|
254
|
|
|
// Currently can't recover from "special" errors |
|
255
|
|
|
return null; |
|
256
|
|
|
} |
|
257
|
|
|
} |
|
258
|
|
|
|
|
259
|
|
|
/* Goto - shift nonterminal */ |
|
260
|
|
|
$this->stackPos -= $this->ruleToLength[$rule]; |
|
261
|
|
|
$nonTerminal = $this->ruleToNonTerminal[$rule]; |
|
262
|
|
|
$idx = $this->gotoBase[$nonTerminal] + $stateStack[$this->stackPos]; |
|
263
|
|
|
if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] == $nonTerminal) { |
|
264
|
|
|
$state = $this->goto[$idx]; |
|
265
|
|
|
} else { |
|
266
|
|
|
$state = $this->gotoDefault[$nonTerminal]; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
++$this->stackPos; |
|
270
|
|
|
$stateStack[$this->stackPos] = $state; |
|
271
|
|
|
$this->semStack[$this->stackPos] = $this->semValue; |
|
272
|
|
|
} else { |
|
273
|
|
|
/* error */ |
|
274
|
|
|
switch ($errorState) { |
|
275
|
|
|
case 0: |
|
276
|
|
|
$msg = $this->getErrorMessage($symbol, $state); |
|
277
|
|
|
$error = new Error($msg, $startAttributes + $endAttributes); |
|
278
|
|
|
$this->errors[] = $error; |
|
279
|
|
|
if ($this->throwOnError) { |
|
280
|
|
|
throw $error; |
|
281
|
|
|
} |
|
282
|
|
|
// Break missing intentionally |
|
283
|
|
|
case 1: |
|
284
|
|
|
case 2: |
|
285
|
|
|
$errorState = 3; |
|
|
|
|
|
|
286
|
|
|
|
|
287
|
|
|
// Pop until error-expecting state uncovered |
|
288
|
|
|
while (!( |
|
289
|
|
|
(($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0 |
|
290
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol) |
|
291
|
|
|
|| ($state < $this->YY2TBLSTATE |
|
292
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $this->errorSymbol) >= 0 |
|
293
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol) |
|
294
|
|
|
) || ($action = $this->action[$idx]) == $this->defaultAction) { // Not totally sure about this |
|
295
|
|
|
if ($this->stackPos <= 0) { |
|
296
|
|
|
// Could not recover from error |
|
297
|
|
|
return null; |
|
298
|
|
|
} |
|
299
|
|
|
$state = $stateStack[--$this->stackPos]; |
|
300
|
|
|
//$this->tracePop($state); |
|
|
|
|
|
|
301
|
|
|
} |
|
302
|
|
|
|
|
303
|
|
|
//$this->traceShift($this->errorSymbol); |
|
|
|
|
|
|
304
|
|
|
$stateStack[++$this->stackPos] = $state = $action; |
|
305
|
|
|
break; |
|
306
|
|
|
|
|
307
|
|
|
case 3: |
|
308
|
|
|
if ($symbol === 0) { |
|
309
|
|
|
// Reached EOF without recovering from error |
|
310
|
|
|
return null; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
//$this->traceDiscard($symbol); |
|
|
|
|
|
|
314
|
|
|
$symbol = self::SYMBOL_NONE; |
|
|
|
|
|
|
315
|
|
|
break 2; |
|
316
|
|
|
} |
|
317
|
|
|
} |
|
318
|
|
|
|
|
319
|
|
|
if ($state < $this->YYNLSTATES) { |
|
320
|
|
|
break; |
|
321
|
|
|
} |
|
322
|
|
|
|
|
323
|
|
|
/* >= YYNLSTATES means shift-and-reduce */ |
|
324
|
|
|
$rule = $state - $this->YYNLSTATES; |
|
|
|
|
|
|
325
|
|
|
} |
|
326
|
|
|
} |
|
327
|
|
|
|
|
328
|
|
|
throw new \RuntimeException('Reached end of parser loop'); |
|
329
|
|
|
} |
|
330
|
|
|
|
|
331
|
|
|
protected function getErrorMessage($symbol, $state) { |
|
332
|
|
|
$expectedString = ''; |
|
333
|
|
|
if ($expected = $this->getExpectedTokens($state)) { |
|
334
|
|
|
$expectedString = ', expecting ' . implode(' or ', $expected); |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
protected function getExpectedTokens($state) { |
|
341
|
|
|
$expected = array(); |
|
342
|
|
|
|
|
343
|
|
|
$base = $this->actionBase[$state]; |
|
344
|
|
|
foreach ($this->symbolToName as $symbol => $name) { |
|
345
|
|
|
$idx = $base + $symbol; |
|
346
|
|
|
if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol |
|
347
|
|
|
|| $state < $this->YY2TBLSTATE |
|
348
|
|
|
&& ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0 |
|
349
|
|
|
&& $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol |
|
350
|
|
|
) { |
|
351
|
|
|
if ($this->action[$idx] != $this->unexpectedTokenRule) { |
|
352
|
|
|
if (count($expected) == 4) { |
|
353
|
|
|
/* Too many expected tokens */ |
|
354
|
|
|
return array(); |
|
355
|
|
|
} |
|
356
|
|
|
|
|
357
|
|
|
$expected[] = $name; |
|
358
|
|
|
} |
|
359
|
|
|
} |
|
360
|
|
|
} |
|
361
|
|
|
|
|
362
|
|
|
return $expected; |
|
363
|
|
|
} |
|
364
|
|
|
|
|
365
|
|
|
/* |
|
366
|
|
|
* Tracing functions used for debugging the parser. |
|
367
|
|
|
*/ |
|
368
|
|
|
|
|
369
|
|
|
/* |
|
|
|
|
|
|
370
|
|
|
protected function traceNewState($state, $symbol) { |
|
371
|
|
|
echo '% State ' . $state |
|
372
|
|
|
. ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n"; |
|
373
|
|
|
} |
|
374
|
|
|
|
|
375
|
|
|
protected function traceRead($symbol) { |
|
376
|
|
|
echo '% Reading ' . $this->symbolToName[$symbol] . "\n"; |
|
377
|
|
|
} |
|
378
|
|
|
|
|
379
|
|
|
protected function traceShift($symbol) { |
|
380
|
|
|
echo '% Shift ' . $this->symbolToName[$symbol] . "\n"; |
|
381
|
|
|
} |
|
382
|
|
|
|
|
383
|
|
|
protected function traceAccept() { |
|
384
|
|
|
echo "% Accepted.\n"; |
|
385
|
|
|
} |
|
386
|
|
|
|
|
387
|
|
|
protected function traceReduce($n) { |
|
388
|
|
|
echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n"; |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
protected function tracePop($state) { |
|
392
|
|
|
echo '% Recovering, uncovered state ' . $state . "\n"; |
|
393
|
|
|
} |
|
394
|
|
|
|
|
395
|
|
|
protected function traceDiscard($symbol) { |
|
396
|
|
|
echo '% Discard ' . $this->symbolToName[$symbol] . "\n"; |
|
397
|
|
|
} |
|
398
|
|
|
*/ |
|
399
|
|
|
|
|
400
|
|
|
/* |
|
401
|
|
|
* Helper functions invoked by semantic actions |
|
402
|
|
|
*/ |
|
403
|
|
|
|
|
404
|
|
|
/** |
|
405
|
|
|
* Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions. |
|
406
|
|
|
* |
|
407
|
|
|
* @param Node[] $stmts |
|
408
|
|
|
* @return Node[] |
|
409
|
|
|
*/ |
|
410
|
|
|
protected function handleNamespaces(array $stmts) { |
|
411
|
|
|
$style = $this->getNamespacingStyle($stmts); |
|
412
|
|
|
if (null === $style) { |
|
413
|
|
|
// not namespaced, nothing to do |
|
414
|
|
|
return $stmts; |
|
415
|
|
|
} elseif ('brace' === $style) { |
|
416
|
|
|
// For braced namespaces we only have to check that there are no invalid statements between the namespaces |
|
417
|
|
|
$afterFirstNamespace = false; |
|
418
|
|
|
foreach ($stmts as $stmt) { |
|
419
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) { |
|
420
|
|
|
$afterFirstNamespace = true; |
|
421
|
|
|
} elseif (!$stmt instanceof Node\Stmt\HaltCompiler && $afterFirstNamespace) { |
|
422
|
|
|
throw new Error('No code may exist outside of namespace {}', $stmt->getLine()); |
|
423
|
|
|
} |
|
424
|
|
|
} |
|
425
|
|
|
return $stmts; |
|
426
|
|
|
} else { |
|
427
|
|
|
// For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts |
|
428
|
|
|
$resultStmts = array(); |
|
429
|
|
|
$targetStmts =& $resultStmts; |
|
430
|
|
|
foreach ($stmts as $stmt) { |
|
431
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) { |
|
432
|
|
|
$stmt->stmts = array(); |
|
433
|
|
|
$targetStmts =& $stmt->stmts; |
|
434
|
|
|
$resultStmts[] = $stmt; |
|
435
|
|
|
} elseif ($stmt instanceof Node\Stmt\HaltCompiler) { |
|
436
|
|
|
// __halt_compiler() is not moved into the namespace |
|
437
|
|
|
$resultStmts[] = $stmt; |
|
438
|
|
|
} else { |
|
439
|
|
|
$targetStmts[] = $stmt; |
|
440
|
|
|
} |
|
441
|
|
|
} |
|
442
|
|
|
return $resultStmts; |
|
443
|
|
|
} |
|
444
|
|
|
} |
|
445
|
|
|
|
|
446
|
|
|
private function getNamespacingStyle(array $stmts) { |
|
447
|
|
|
$style = null; |
|
448
|
|
|
$hasNotAllowedStmts = false; |
|
449
|
|
|
foreach ($stmts as $i => $stmt) { |
|
450
|
|
|
if ($stmt instanceof Node\Stmt\Namespace_) { |
|
451
|
|
|
$currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace'; |
|
452
|
|
|
if (null === $style) { |
|
453
|
|
|
$style = $currentStyle; |
|
454
|
|
|
if ($hasNotAllowedStmts) { |
|
455
|
|
|
throw new Error('Namespace declaration statement has to be the very first statement in the script', $stmt->getLine()); |
|
456
|
|
|
} |
|
457
|
|
|
} elseif ($style !== $currentStyle) { |
|
458
|
|
|
throw new Error('Cannot mix bracketed namespace declarations with unbracketed namespace declarations', $stmt->getLine()); |
|
459
|
|
|
} |
|
460
|
|
|
continue; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
/* declare() and __halt_compiler() can be used before a namespace declaration */ |
|
464
|
|
|
if ($stmt instanceof Node\Stmt\Declare_ || $stmt instanceof Node\Stmt\HaltCompiler) { |
|
465
|
|
|
continue; |
|
466
|
|
|
} |
|
467
|
|
|
|
|
468
|
|
|
/* There may be a hashbang line at the very start of the file */ |
|
469
|
|
|
if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) { |
|
470
|
|
|
continue; |
|
471
|
|
|
} |
|
472
|
|
|
|
|
473
|
|
|
/* Everything else if forbidden before namespace declarations */ |
|
474
|
|
|
$hasNotAllowedStmts = true; |
|
475
|
|
|
} |
|
476
|
|
|
return $style; |
|
477
|
|
|
} |
|
478
|
|
|
|
|
479
|
|
|
protected function handleScalarTypes(Name $name) { |
|
480
|
|
|
$scalarTypes = [ |
|
481
|
|
|
'bool' => true, |
|
482
|
|
|
'int' => true, |
|
483
|
|
|
'float' => true, |
|
484
|
|
|
'string' => true, |
|
485
|
|
|
]; |
|
486
|
|
|
|
|
487
|
|
|
if (!$name->isUnqualified()) { |
|
488
|
|
|
return $name; |
|
489
|
|
|
} |
|
490
|
|
|
|
|
491
|
|
|
$lowerName = strtolower($name->toString()); |
|
492
|
|
|
return isset($scalarTypes[$lowerName]) ? $lowerName : $name; |
|
493
|
|
|
} |
|
494
|
|
|
} |
|
495
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..