Chris@13
|
1 <?php declare(strict_types=1);
|
Chris@0
|
2
|
Chris@0
|
3 namespace PhpParser;
|
Chris@0
|
4
|
Chris@0
|
5 /*
|
Chris@0
|
6 * This parser is based on a skeleton written by Moriyoshi Koizumi, which in
|
Chris@0
|
7 * turn is based on work by Masato Bito.
|
Chris@0
|
8 */
|
Chris@13
|
9 use PhpParser\Node\Expr;
|
Chris@17
|
10 use PhpParser\Node\Expr\Cast\Double;
|
Chris@0
|
11 use PhpParser\Node\Name;
|
Chris@0
|
12 use PhpParser\Node\Param;
|
Chris@17
|
13 use PhpParser\Node\Scalar\Encapsed;
|
Chris@0
|
14 use PhpParser\Node\Scalar\LNumber;
|
Chris@0
|
15 use PhpParser\Node\Scalar\String_;
|
Chris@0
|
16 use PhpParser\Node\Stmt\Class_;
|
Chris@0
|
17 use PhpParser\Node\Stmt\ClassConst;
|
Chris@0
|
18 use PhpParser\Node\Stmt\ClassMethod;
|
Chris@0
|
19 use PhpParser\Node\Stmt\Interface_;
|
Chris@0
|
20 use PhpParser\Node\Stmt\Namespace_;
|
Chris@0
|
21 use PhpParser\Node\Stmt\Property;
|
Chris@0
|
22 use PhpParser\Node\Stmt\TryCatch;
|
Chris@0
|
23 use PhpParser\Node\Stmt\UseUse;
|
Chris@13
|
24 use PhpParser\Node\VarLikeIdentifier;
|
Chris@0
|
25
|
Chris@0
|
26 abstract class ParserAbstract implements Parser
|
Chris@0
|
27 {
|
Chris@0
|
28 const SYMBOL_NONE = -1;
|
Chris@0
|
29
|
Chris@0
|
30 /*
|
Chris@0
|
31 * The following members will be filled with generated parsing data:
|
Chris@0
|
32 */
|
Chris@0
|
33
|
Chris@0
|
34 /** @var int Size of $tokenToSymbol map */
|
Chris@0
|
35 protected $tokenToSymbolMapSize;
|
Chris@0
|
36 /** @var int Size of $action table */
|
Chris@0
|
37 protected $actionTableSize;
|
Chris@0
|
38 /** @var int Size of $goto table */
|
Chris@0
|
39 protected $gotoTableSize;
|
Chris@0
|
40
|
Chris@0
|
41 /** @var int Symbol number signifying an invalid token */
|
Chris@0
|
42 protected $invalidSymbol;
|
Chris@0
|
43 /** @var int Symbol number of error recovery token */
|
Chris@0
|
44 protected $errorSymbol;
|
Chris@0
|
45 /** @var int Action number signifying default action */
|
Chris@0
|
46 protected $defaultAction;
|
Chris@0
|
47 /** @var int Rule number signifying that an unexpected token was encountered */
|
Chris@0
|
48 protected $unexpectedTokenRule;
|
Chris@0
|
49
|
Chris@0
|
50 protected $YY2TBLSTATE;
|
Chris@13
|
51 /** @var int Number of non-leaf states */
|
Chris@13
|
52 protected $numNonLeafStates;
|
Chris@0
|
53
|
Chris@13
|
54 /** @var int[] Map of lexer tokens to internal symbols */
|
Chris@0
|
55 protected $tokenToSymbol;
|
Chris@13
|
56 /** @var string[] Map of symbols to their names */
|
Chris@0
|
57 protected $symbolToName;
|
Chris@0
|
58 /** @var array Names of the production rules (only necessary for debugging) */
|
Chris@0
|
59 protected $productions;
|
Chris@0
|
60
|
Chris@13
|
61 /** @var int[] Map of states to a displacement into the $action table. The corresponding action for this
|
Chris@0
|
62 * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the
|
Chris@0
|
63 action is defaulted, i.e. $actionDefault[$state] should be used instead. */
|
Chris@0
|
64 protected $actionBase;
|
Chris@13
|
65 /** @var int[] Table of actions. Indexed according to $actionBase comment. */
|
Chris@0
|
66 protected $action;
|
Chris@13
|
67 /** @var int[] Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol
|
Chris@0
|
68 * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */
|
Chris@0
|
69 protected $actionCheck;
|
Chris@13
|
70 /** @var int[] Map of states to their default action */
|
Chris@0
|
71 protected $actionDefault;
|
Chris@13
|
72 /** @var callable[] Semantic action callbacks */
|
Chris@13
|
73 protected $reduceCallbacks;
|
Chris@0
|
74
|
Chris@13
|
75 /** @var int[] Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this
|
Chris@0
|
76 * non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */
|
Chris@0
|
77 protected $gotoBase;
|
Chris@13
|
78 /** @var int[] Table of states to goto after reduction. Indexed according to $gotoBase comment. */
|
Chris@0
|
79 protected $goto;
|
Chris@13
|
80 /** @var int[] Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal
|
Chris@0
|
81 * then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */
|
Chris@0
|
82 protected $gotoCheck;
|
Chris@13
|
83 /** @var int[] Map of non-terminals to the default state to goto after their reduction */
|
Chris@0
|
84 protected $gotoDefault;
|
Chris@0
|
85
|
Chris@13
|
86 /** @var int[] Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for
|
Chris@0
|
87 * determining the state to goto after reduction. */
|
Chris@0
|
88 protected $ruleToNonTerminal;
|
Chris@13
|
89 /** @var int[] Map of rules to the length of their right-hand side, which is the number of elements that have to
|
Chris@0
|
90 * be popped from the stack(s) on reduction. */
|
Chris@0
|
91 protected $ruleToLength;
|
Chris@0
|
92
|
Chris@0
|
93 /*
|
Chris@0
|
94 * The following members are part of the parser state:
|
Chris@0
|
95 */
|
Chris@0
|
96
|
Chris@0
|
97 /** @var Lexer Lexer that is used when parsing */
|
Chris@0
|
98 protected $lexer;
|
Chris@0
|
99 /** @var mixed Temporary value containing the result of last semantic action (reduction) */
|
Chris@0
|
100 protected $semValue;
|
Chris@0
|
101 /** @var array Semantic value stack (contains values of tokens and semantic action results) */
|
Chris@0
|
102 protected $semStack;
|
Chris@0
|
103 /** @var array[] Start attribute stack */
|
Chris@0
|
104 protected $startAttributeStack;
|
Chris@0
|
105 /** @var array[] End attribute stack */
|
Chris@0
|
106 protected $endAttributeStack;
|
Chris@0
|
107 /** @var array End attributes of last *shifted* token */
|
Chris@0
|
108 protected $endAttributes;
|
Chris@0
|
109 /** @var array Start attributes of last *read* token */
|
Chris@0
|
110 protected $lookaheadStartAttributes;
|
Chris@0
|
111
|
Chris@0
|
112 /** @var ErrorHandler Error handler */
|
Chris@0
|
113 protected $errorHandler;
|
Chris@0
|
114 /** @var int Error state, used to avoid error floods */
|
Chris@0
|
115 protected $errorState;
|
Chris@0
|
116
|
Chris@0
|
117 /**
|
Chris@13
|
118 * Initialize $reduceCallbacks map.
|
Chris@13
|
119 */
|
Chris@13
|
120 abstract protected function initReduceCallbacks();
|
Chris@13
|
121
|
Chris@13
|
122 /**
|
Chris@0
|
123 * Creates a parser instance.
|
Chris@0
|
124 *
|
Chris@13
|
125 * Options: Currently none.
|
Chris@13
|
126 *
|
Chris@0
|
127 * @param Lexer $lexer A lexer
|
Chris@13
|
128 * @param array $options Options array.
|
Chris@0
|
129 */
|
Chris@13
|
130 public function __construct(Lexer $lexer, array $options = []) {
|
Chris@0
|
131 $this->lexer = $lexer;
|
Chris@0
|
132
|
Chris@0
|
133 if (isset($options['throwOnError'])) {
|
Chris@0
|
134 throw new \LogicException(
|
Chris@0
|
135 '"throwOnError" is no longer supported, use "errorHandler" instead');
|
Chris@0
|
136 }
|
Chris@13
|
137
|
Chris@13
|
138 $this->initReduceCallbacks();
|
Chris@0
|
139 }
|
Chris@0
|
140
|
Chris@0
|
141 /**
|
Chris@0
|
142 * Parses PHP code into a node tree.
|
Chris@0
|
143 *
|
Chris@0
|
144 * If a non-throwing error handler is used, the parser will continue parsing after an error
|
Chris@0
|
145 * occurred and attempt to build a partial AST.
|
Chris@0
|
146 *
|
Chris@0
|
147 * @param string $code The source code to parse
|
Chris@0
|
148 * @param ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults
|
Chris@0
|
149 * to ErrorHandler\Throwing.
|
Chris@0
|
150 *
|
Chris@13
|
151 * @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
|
Chris@13
|
152 * the parser was unable to recover from an error).
|
Chris@0
|
153 */
|
Chris@13
|
154 public function parse(string $code, ErrorHandler $errorHandler = null) {
|
Chris@0
|
155 $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;
|
Chris@0
|
156
|
Chris@0
|
157 $this->lexer->startLexing($code, $this->errorHandler);
|
Chris@13
|
158 $result = $this->doParse();
|
Chris@0
|
159
|
Chris@13
|
160 // Clear out some of the interior state, so we don't hold onto unnecessary
|
Chris@13
|
161 // memory between uses of the parser
|
Chris@13
|
162 $this->startAttributeStack = [];
|
Chris@13
|
163 $this->endAttributeStack = [];
|
Chris@13
|
164 $this->semStack = [];
|
Chris@13
|
165 $this->semValue = null;
|
Chris@13
|
166
|
Chris@13
|
167 return $result;
|
Chris@13
|
168 }
|
Chris@13
|
169
|
Chris@13
|
170 protected function doParse() {
|
Chris@0
|
171 // We start off with no lookahead-token
|
Chris@0
|
172 $symbol = self::SYMBOL_NONE;
|
Chris@0
|
173
|
Chris@0
|
174 // The attributes for a node are taken from the first and last token of the node.
|
Chris@0
|
175 // From the first token only the startAttributes are taken and from the last only
|
Chris@0
|
176 // the endAttributes. Both are merged using the array union operator (+).
|
Chris@13
|
177 $startAttributes = [];
|
Chris@13
|
178 $endAttributes = [];
|
Chris@0
|
179 $this->endAttributes = $endAttributes;
|
Chris@0
|
180
|
Chris@0
|
181 // Keep stack of start and end attributes
|
Chris@13
|
182 $this->startAttributeStack = [];
|
Chris@13
|
183 $this->endAttributeStack = [$endAttributes];
|
Chris@0
|
184
|
Chris@0
|
185 // Start off in the initial state and keep a stack of previous states
|
Chris@0
|
186 $state = 0;
|
Chris@13
|
187 $stateStack = [$state];
|
Chris@0
|
188
|
Chris@0
|
189 // Semantic value stack (contains values of tokens and semantic action results)
|
Chris@13
|
190 $this->semStack = [];
|
Chris@0
|
191
|
Chris@0
|
192 // Current position in the stack(s)
|
Chris@13
|
193 $stackPos = 0;
|
Chris@0
|
194
|
Chris@0
|
195 $this->errorState = 0;
|
Chris@0
|
196
|
Chris@0
|
197 for (;;) {
|
Chris@0
|
198 //$this->traceNewState($state, $symbol);
|
Chris@0
|
199
|
Chris@13
|
200 if ($this->actionBase[$state] === 0) {
|
Chris@0
|
201 $rule = $this->actionDefault[$state];
|
Chris@0
|
202 } else {
|
Chris@0
|
203 if ($symbol === self::SYMBOL_NONE) {
|
Chris@0
|
204 // Fetch the next token id from the lexer and fetch additional info by-ref.
|
Chris@0
|
205 // The end attributes are fetched into a temporary variable and only set once the token is really
|
Chris@0
|
206 // shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is
|
Chris@0
|
207 // reduced after a token was read but not yet shifted.
|
Chris@0
|
208 $tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes);
|
Chris@0
|
209
|
Chris@0
|
210 // map the lexer token id to the internally used symbols
|
Chris@0
|
211 $symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize
|
Chris@0
|
212 ? $this->tokenToSymbol[$tokenId]
|
Chris@0
|
213 : $this->invalidSymbol;
|
Chris@0
|
214
|
Chris@0
|
215 if ($symbol === $this->invalidSymbol) {
|
Chris@0
|
216 throw new \RangeException(sprintf(
|
Chris@0
|
217 'The lexer returned an invalid token (id=%d, value=%s)',
|
Chris@0
|
218 $tokenId, $tokenValue
|
Chris@0
|
219 ));
|
Chris@0
|
220 }
|
Chris@0
|
221
|
Chris@0
|
222 // This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get
|
Chris@0
|
223 // the attributes of the next token, even though they don't contain it themselves.
|
Chris@13
|
224 $this->startAttributeStack[$stackPos+1] = $startAttributes;
|
Chris@13
|
225 $this->endAttributeStack[$stackPos+1] = $endAttributes;
|
Chris@0
|
226 $this->lookaheadStartAttributes = $startAttributes;
|
Chris@0
|
227
|
Chris@0
|
228 //$this->traceRead($symbol);
|
Chris@0
|
229 }
|
Chris@0
|
230
|
Chris@0
|
231 $idx = $this->actionBase[$state] + $symbol;
|
Chris@13
|
232 if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)
|
Chris@0
|
233 || ($state < $this->YY2TBLSTATE
|
Chris@13
|
234 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
|
Chris@13
|
235 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol))
|
Chris@13
|
236 && ($action = $this->action[$idx]) !== $this->defaultAction) {
|
Chris@0
|
237 /*
|
Chris@13
|
238 * >= numNonLeafStates: shift and reduce
|
Chris@0
|
239 * > 0: shift
|
Chris@0
|
240 * = 0: accept
|
Chris@0
|
241 * < 0: reduce
|
Chris@0
|
242 * = -YYUNEXPECTED: error
|
Chris@0
|
243 */
|
Chris@0
|
244 if ($action > 0) {
|
Chris@0
|
245 /* shift */
|
Chris@0
|
246 //$this->traceShift($symbol);
|
Chris@0
|
247
|
Chris@13
|
248 ++$stackPos;
|
Chris@13
|
249 $stateStack[$stackPos] = $state = $action;
|
Chris@13
|
250 $this->semStack[$stackPos] = $tokenValue;
|
Chris@13
|
251 $this->startAttributeStack[$stackPos] = $startAttributes;
|
Chris@13
|
252 $this->endAttributeStack[$stackPos] = $endAttributes;
|
Chris@0
|
253 $this->endAttributes = $endAttributes;
|
Chris@0
|
254 $symbol = self::SYMBOL_NONE;
|
Chris@0
|
255
|
Chris@0
|
256 if ($this->errorState) {
|
Chris@0
|
257 --$this->errorState;
|
Chris@0
|
258 }
|
Chris@0
|
259
|
Chris@13
|
260 if ($action < $this->numNonLeafStates) {
|
Chris@0
|
261 continue;
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@13
|
264 /* $yyn >= numNonLeafStates means shift-and-reduce */
|
Chris@13
|
265 $rule = $action - $this->numNonLeafStates;
|
Chris@0
|
266 } else {
|
Chris@0
|
267 $rule = -$action;
|
Chris@0
|
268 }
|
Chris@0
|
269 } else {
|
Chris@0
|
270 $rule = $this->actionDefault[$state];
|
Chris@0
|
271 }
|
Chris@0
|
272 }
|
Chris@0
|
273
|
Chris@0
|
274 for (;;) {
|
Chris@0
|
275 if ($rule === 0) {
|
Chris@0
|
276 /* accept */
|
Chris@0
|
277 //$this->traceAccept();
|
Chris@0
|
278 return $this->semValue;
|
Chris@0
|
279 } elseif ($rule !== $this->unexpectedTokenRule) {
|
Chris@0
|
280 /* reduce */
|
Chris@0
|
281 //$this->traceReduce($rule);
|
Chris@0
|
282
|
Chris@0
|
283 try {
|
Chris@13
|
284 $this->reduceCallbacks[$rule]($stackPos);
|
Chris@0
|
285 } catch (Error $e) {
|
Chris@0
|
286 if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) {
|
Chris@0
|
287 $e->setStartLine($startAttributes['startLine']);
|
Chris@0
|
288 }
|
Chris@0
|
289
|
Chris@0
|
290 $this->emitError($e);
|
Chris@0
|
291 // Can't recover from this type of error
|
Chris@0
|
292 return null;
|
Chris@0
|
293 }
|
Chris@0
|
294
|
Chris@0
|
295 /* Goto - shift nonterminal */
|
Chris@13
|
296 $lastEndAttributes = $this->endAttributeStack[$stackPos];
|
Chris@13
|
297 $stackPos -= $this->ruleToLength[$rule];
|
Chris@0
|
298 $nonTerminal = $this->ruleToNonTerminal[$rule];
|
Chris@13
|
299 $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos];
|
Chris@13
|
300 if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) {
|
Chris@0
|
301 $state = $this->goto[$idx];
|
Chris@0
|
302 } else {
|
Chris@0
|
303 $state = $this->gotoDefault[$nonTerminal];
|
Chris@0
|
304 }
|
Chris@0
|
305
|
Chris@13
|
306 ++$stackPos;
|
Chris@13
|
307 $stateStack[$stackPos] = $state;
|
Chris@13
|
308 $this->semStack[$stackPos] = $this->semValue;
|
Chris@13
|
309 $this->endAttributeStack[$stackPos] = $lastEndAttributes;
|
Chris@0
|
310 } else {
|
Chris@0
|
311 /* error */
|
Chris@0
|
312 switch ($this->errorState) {
|
Chris@0
|
313 case 0:
|
Chris@0
|
314 $msg = $this->getErrorMessage($symbol, $state);
|
Chris@0
|
315 $this->emitError(new Error($msg, $startAttributes + $endAttributes));
|
Chris@0
|
316 // Break missing intentionally
|
Chris@0
|
317 case 1:
|
Chris@0
|
318 case 2:
|
Chris@0
|
319 $this->errorState = 3;
|
Chris@0
|
320
|
Chris@0
|
321 // Pop until error-expecting state uncovered
|
Chris@0
|
322 while (!(
|
Chris@0
|
323 (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0
|
Chris@13
|
324 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
|
Chris@0
|
325 || ($state < $this->YY2TBLSTATE
|
Chris@13
|
326 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0
|
Chris@13
|
327 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
|
Chris@13
|
328 ) || ($action = $this->action[$idx]) === $this->defaultAction) { // Not totally sure about this
|
Chris@13
|
329 if ($stackPos <= 0) {
|
Chris@0
|
330 // Could not recover from error
|
Chris@0
|
331 return null;
|
Chris@0
|
332 }
|
Chris@13
|
333 $state = $stateStack[--$stackPos];
|
Chris@0
|
334 //$this->tracePop($state);
|
Chris@0
|
335 }
|
Chris@0
|
336
|
Chris@0
|
337 //$this->traceShift($this->errorSymbol);
|
Chris@13
|
338 ++$stackPos;
|
Chris@13
|
339 $stateStack[$stackPos] = $state = $action;
|
Chris@0
|
340
|
Chris@0
|
341 // We treat the error symbol as being empty, so we reset the end attributes
|
Chris@0
|
342 // to the end attributes of the last non-error symbol
|
Chris@13
|
343 $this->endAttributeStack[$stackPos] = $this->endAttributeStack[$stackPos - 1];
|
Chris@13
|
344 $this->endAttributes = $this->endAttributeStack[$stackPos - 1];
|
Chris@0
|
345 break;
|
Chris@0
|
346
|
Chris@0
|
347 case 3:
|
Chris@0
|
348 if ($symbol === 0) {
|
Chris@0
|
349 // Reached EOF without recovering from error
|
Chris@0
|
350 return null;
|
Chris@0
|
351 }
|
Chris@0
|
352
|
Chris@0
|
353 //$this->traceDiscard($symbol);
|
Chris@0
|
354 $symbol = self::SYMBOL_NONE;
|
Chris@0
|
355 break 2;
|
Chris@0
|
356 }
|
Chris@0
|
357 }
|
Chris@0
|
358
|
Chris@13
|
359 if ($state < $this->numNonLeafStates) {
|
Chris@0
|
360 break;
|
Chris@0
|
361 }
|
Chris@0
|
362
|
Chris@13
|
363 /* >= numNonLeafStates means shift-and-reduce */
|
Chris@13
|
364 $rule = $state - $this->numNonLeafStates;
|
Chris@0
|
365 }
|
Chris@0
|
366 }
|
Chris@0
|
367
|
Chris@0
|
368 throw new \RuntimeException('Reached end of parser loop');
|
Chris@0
|
369 }
|
Chris@0
|
370
|
Chris@0
|
371 protected function emitError(Error $error) {
|
Chris@0
|
372 $this->errorHandler->handleError($error);
|
Chris@0
|
373 }
|
Chris@0
|
374
|
Chris@13
|
375 /**
|
Chris@13
|
376 * Format error message including expected tokens.
|
Chris@13
|
377 *
|
Chris@13
|
378 * @param int $symbol Unexpected symbol
|
Chris@13
|
379 * @param int $state State at time of error
|
Chris@13
|
380 *
|
Chris@13
|
381 * @return string Formatted error message
|
Chris@13
|
382 */
|
Chris@13
|
383 protected function getErrorMessage(int $symbol, int $state) : string {
|
Chris@0
|
384 $expectedString = '';
|
Chris@0
|
385 if ($expected = $this->getExpectedTokens($state)) {
|
Chris@0
|
386 $expectedString = ', expecting ' . implode(' or ', $expected);
|
Chris@0
|
387 }
|
Chris@0
|
388
|
Chris@0
|
389 return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
|
Chris@0
|
390 }
|
Chris@0
|
391
|
Chris@13
|
392 /**
|
Chris@13
|
393 * Get limited number of expected tokens in given state.
|
Chris@13
|
394 *
|
Chris@13
|
395 * @param int $state State
|
Chris@13
|
396 *
|
Chris@13
|
397 * @return string[] Expected tokens. If too many, an empty array is returned.
|
Chris@13
|
398 */
|
Chris@13
|
399 protected function getExpectedTokens(int $state) : array {
|
Chris@13
|
400 $expected = [];
|
Chris@0
|
401
|
Chris@0
|
402 $base = $this->actionBase[$state];
|
Chris@0
|
403 foreach ($this->symbolToName as $symbol => $name) {
|
Chris@0
|
404 $idx = $base + $symbol;
|
Chris@0
|
405 if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
|
Chris@0
|
406 || $state < $this->YY2TBLSTATE
|
Chris@13
|
407 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
|
Chris@0
|
408 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
|
Chris@0
|
409 ) {
|
Chris@13
|
410 if ($this->action[$idx] !== $this->unexpectedTokenRule
|
Chris@13
|
411 && $this->action[$idx] !== $this->defaultAction
|
Chris@13
|
412 && $symbol !== $this->errorSymbol
|
Chris@0
|
413 ) {
|
Chris@13
|
414 if (count($expected) === 4) {
|
Chris@0
|
415 /* Too many expected tokens */
|
Chris@13
|
416 return [];
|
Chris@0
|
417 }
|
Chris@0
|
418
|
Chris@0
|
419 $expected[] = $name;
|
Chris@0
|
420 }
|
Chris@0
|
421 }
|
Chris@0
|
422 }
|
Chris@0
|
423
|
Chris@0
|
424 return $expected;
|
Chris@0
|
425 }
|
Chris@0
|
426
|
Chris@0
|
427 /*
|
Chris@0
|
428 * Tracing functions used for debugging the parser.
|
Chris@0
|
429 */
|
Chris@0
|
430
|
Chris@0
|
431 /*
|
Chris@0
|
432 protected function traceNewState($state, $symbol) {
|
Chris@0
|
433 echo '% State ' . $state
|
Chris@0
|
434 . ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n";
|
Chris@0
|
435 }
|
Chris@0
|
436
|
Chris@0
|
437 protected function traceRead($symbol) {
|
Chris@0
|
438 echo '% Reading ' . $this->symbolToName[$symbol] . "\n";
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 protected function traceShift($symbol) {
|
Chris@0
|
442 echo '% Shift ' . $this->symbolToName[$symbol] . "\n";
|
Chris@0
|
443 }
|
Chris@0
|
444
|
Chris@0
|
445 protected function traceAccept() {
|
Chris@0
|
446 echo "% Accepted.\n";
|
Chris@0
|
447 }
|
Chris@0
|
448
|
Chris@0
|
449 protected function traceReduce($n) {
|
Chris@0
|
450 echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n";
|
Chris@0
|
451 }
|
Chris@0
|
452
|
Chris@0
|
453 protected function tracePop($state) {
|
Chris@0
|
454 echo '% Recovering, uncovered state ' . $state . "\n";
|
Chris@0
|
455 }
|
Chris@0
|
456
|
Chris@0
|
457 protected function traceDiscard($symbol) {
|
Chris@0
|
458 echo '% Discard ' . $this->symbolToName[$symbol] . "\n";
|
Chris@0
|
459 }
|
Chris@0
|
460 */
|
Chris@0
|
461
|
Chris@0
|
462 /*
|
Chris@0
|
463 * Helper functions invoked by semantic actions
|
Chris@0
|
464 */
|
Chris@0
|
465
|
Chris@0
|
466 /**
|
Chris@0
|
467 * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
|
Chris@0
|
468 *
|
Chris@13
|
469 * @param Node\Stmt[] $stmts
|
Chris@13
|
470 * @return Node\Stmt[]
|
Chris@0
|
471 */
|
Chris@13
|
472 protected function handleNamespaces(array $stmts) : array {
|
Chris@0
|
473 $hasErrored = false;
|
Chris@0
|
474 $style = $this->getNamespacingStyle($stmts);
|
Chris@0
|
475 if (null === $style) {
|
Chris@0
|
476 // not namespaced, nothing to do
|
Chris@0
|
477 return $stmts;
|
Chris@0
|
478 } elseif ('brace' === $style) {
|
Chris@0
|
479 // For braced namespaces we only have to check that there are no invalid statements between the namespaces
|
Chris@0
|
480 $afterFirstNamespace = false;
|
Chris@0
|
481 foreach ($stmts as $stmt) {
|
Chris@0
|
482 if ($stmt instanceof Node\Stmt\Namespace_) {
|
Chris@0
|
483 $afterFirstNamespace = true;
|
Chris@0
|
484 } elseif (!$stmt instanceof Node\Stmt\HaltCompiler
|
Chris@0
|
485 && !$stmt instanceof Node\Stmt\Nop
|
Chris@0
|
486 && $afterFirstNamespace && !$hasErrored) {
|
Chris@0
|
487 $this->emitError(new Error(
|
Chris@0
|
488 'No code may exist outside of namespace {}', $stmt->getAttributes()));
|
Chris@0
|
489 $hasErrored = true; // Avoid one error for every statement
|
Chris@0
|
490 }
|
Chris@0
|
491 }
|
Chris@0
|
492 return $stmts;
|
Chris@0
|
493 } else {
|
Chris@0
|
494 // For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts
|
Chris@13
|
495 $resultStmts = [];
|
Chris@0
|
496 $targetStmts =& $resultStmts;
|
Chris@13
|
497 $lastNs = null;
|
Chris@0
|
498 foreach ($stmts as $stmt) {
|
Chris@0
|
499 if ($stmt instanceof Node\Stmt\Namespace_) {
|
Chris@13
|
500 if ($lastNs !== null) {
|
Chris@13
|
501 $this->fixupNamespaceAttributes($lastNs);
|
Chris@13
|
502 }
|
Chris@0
|
503 if ($stmt->stmts === null) {
|
Chris@13
|
504 $stmt->stmts = [];
|
Chris@0
|
505 $targetStmts =& $stmt->stmts;
|
Chris@0
|
506 $resultStmts[] = $stmt;
|
Chris@0
|
507 } else {
|
Chris@0
|
508 // This handles the invalid case of mixed style namespaces
|
Chris@0
|
509 $resultStmts[] = $stmt;
|
Chris@0
|
510 $targetStmts =& $resultStmts;
|
Chris@0
|
511 }
|
Chris@13
|
512 $lastNs = $stmt;
|
Chris@0
|
513 } elseif ($stmt instanceof Node\Stmt\HaltCompiler) {
|
Chris@0
|
514 // __halt_compiler() is not moved into the namespace
|
Chris@0
|
515 $resultStmts[] = $stmt;
|
Chris@0
|
516 } else {
|
Chris@0
|
517 $targetStmts[] = $stmt;
|
Chris@0
|
518 }
|
Chris@0
|
519 }
|
Chris@13
|
520 if ($lastNs !== null) {
|
Chris@13
|
521 $this->fixupNamespaceAttributes($lastNs);
|
Chris@13
|
522 }
|
Chris@0
|
523 return $resultStmts;
|
Chris@0
|
524 }
|
Chris@0
|
525 }
|
Chris@0
|
526
|
Chris@13
|
527 private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt) {
|
Chris@13
|
528 // We moved the statements into the namespace node, as such the end of the namespace node
|
Chris@13
|
529 // needs to be extended to the end of the statements.
|
Chris@13
|
530 if (empty($stmt->stmts)) {
|
Chris@13
|
531 return;
|
Chris@13
|
532 }
|
Chris@13
|
533
|
Chris@13
|
534 // We only move the builtin end attributes here. This is the best we can do with the
|
Chris@13
|
535 // knowledge we have.
|
Chris@13
|
536 $endAttributes = ['endLine', 'endFilePos', 'endTokenPos'];
|
Chris@13
|
537 $lastStmt = $stmt->stmts[count($stmt->stmts) - 1];
|
Chris@13
|
538 foreach ($endAttributes as $endAttribute) {
|
Chris@13
|
539 if ($lastStmt->hasAttribute($endAttribute)) {
|
Chris@13
|
540 $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute));
|
Chris@13
|
541 }
|
Chris@13
|
542 }
|
Chris@13
|
543 }
|
Chris@13
|
544
|
Chris@13
|
545 /**
|
Chris@13
|
546 * Determine namespacing style (semicolon or brace)
|
Chris@13
|
547 *
|
Chris@13
|
548 * @param Node[] $stmts Top-level statements.
|
Chris@13
|
549 *
|
Chris@13
|
550 * @return null|string One of "semicolon", "brace" or null (no namespaces)
|
Chris@13
|
551 */
|
Chris@0
|
552 private function getNamespacingStyle(array $stmts) {
|
Chris@0
|
553 $style = null;
|
Chris@0
|
554 $hasNotAllowedStmts = false;
|
Chris@0
|
555 foreach ($stmts as $i => $stmt) {
|
Chris@0
|
556 if ($stmt instanceof Node\Stmt\Namespace_) {
|
Chris@0
|
557 $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace';
|
Chris@0
|
558 if (null === $style) {
|
Chris@0
|
559 $style = $currentStyle;
|
Chris@0
|
560 if ($hasNotAllowedStmts) {
|
Chris@0
|
561 $this->emitError(new Error(
|
Chris@0
|
562 'Namespace declaration statement has to be the very first statement in the script',
|
Chris@0
|
563 $stmt->getLine() // Avoid marking the entire namespace as an error
|
Chris@0
|
564 ));
|
Chris@0
|
565 }
|
Chris@0
|
566 } elseif ($style !== $currentStyle) {
|
Chris@0
|
567 $this->emitError(new Error(
|
Chris@0
|
568 'Cannot mix bracketed namespace declarations with unbracketed namespace declarations',
|
Chris@0
|
569 $stmt->getLine() // Avoid marking the entire namespace as an error
|
Chris@0
|
570 ));
|
Chris@0
|
571 // Treat like semicolon style for namespace normalization
|
Chris@0
|
572 return 'semicolon';
|
Chris@0
|
573 }
|
Chris@0
|
574 continue;
|
Chris@0
|
575 }
|
Chris@0
|
576
|
Chris@0
|
577 /* declare(), __halt_compiler() and nops can be used before a namespace declaration */
|
Chris@0
|
578 if ($stmt instanceof Node\Stmt\Declare_
|
Chris@0
|
579 || $stmt instanceof Node\Stmt\HaltCompiler
|
Chris@0
|
580 || $stmt instanceof Node\Stmt\Nop) {
|
Chris@0
|
581 continue;
|
Chris@0
|
582 }
|
Chris@0
|
583
|
Chris@0
|
584 /* There may be a hashbang line at the very start of the file */
|
Chris@13
|
585 if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
|
Chris@0
|
586 continue;
|
Chris@0
|
587 }
|
Chris@0
|
588
|
Chris@0
|
589 /* Everything else if forbidden before namespace declarations */
|
Chris@0
|
590 $hasNotAllowedStmts = true;
|
Chris@0
|
591 }
|
Chris@0
|
592 return $style;
|
Chris@0
|
593 }
|
Chris@0
|
594
|
Chris@13
|
595 /**
|
Chris@13
|
596 * Fix up parsing of static property calls in PHP 5.
|
Chris@13
|
597 *
|
Chris@13
|
598 * In PHP 5 A::$b[c][d] and A::$b[c][d]() have very different interpretation. The former is
|
Chris@13
|
599 * interpreted as (A::$b)[c][d], while the latter is the same as A::{$b[c][d]}(). We parse the
|
Chris@13
|
600 * latter as the former initially and this method fixes the AST into the correct form when we
|
Chris@13
|
601 * encounter the "()".
|
Chris@13
|
602 *
|
Chris@13
|
603 * @param Node\Expr\StaticPropertyFetch|Node\Expr\ArrayDimFetch $prop
|
Chris@13
|
604 * @param Node\Arg[] $args
|
Chris@13
|
605 * @param array $attributes
|
Chris@13
|
606 *
|
Chris@13
|
607 * @return Expr\StaticCall
|
Chris@13
|
608 */
|
Chris@13
|
609 protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) : Expr\StaticCall {
|
Chris@13
|
610 if ($prop instanceof Node\Expr\StaticPropertyFetch) {
|
Chris@13
|
611 $name = $prop->name instanceof VarLikeIdentifier
|
Chris@13
|
612 ? $prop->name->toString() : $prop->name;
|
Chris@13
|
613 $var = new Expr\Variable($name, $prop->name->getAttributes());
|
Chris@13
|
614 return new Expr\StaticCall($prop->class, $var, $args, $attributes);
|
Chris@13
|
615 } elseif ($prop instanceof Node\Expr\ArrayDimFetch) {
|
Chris@13
|
616 $tmp = $prop;
|
Chris@13
|
617 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
|
Chris@13
|
618 $tmp = $tmp->var;
|
Chris@13
|
619 }
|
Chris@13
|
620
|
Chris@13
|
621 /** @var Expr\StaticPropertyFetch $staticProp */
|
Chris@13
|
622 $staticProp = $tmp->var;
|
Chris@13
|
623
|
Chris@13
|
624 // Set start attributes to attributes of innermost node
|
Chris@13
|
625 $tmp = $prop;
|
Chris@13
|
626 $this->fixupStartAttributes($tmp, $staticProp->name);
|
Chris@13
|
627 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
|
Chris@13
|
628 $tmp = $tmp->var;
|
Chris@13
|
629 $this->fixupStartAttributes($tmp, $staticProp->name);
|
Chris@13
|
630 }
|
Chris@13
|
631
|
Chris@13
|
632 $name = $staticProp->name instanceof VarLikeIdentifier
|
Chris@13
|
633 ? $staticProp->name->toString() : $staticProp->name;
|
Chris@13
|
634 $tmp->var = new Expr\Variable($name, $staticProp->name->getAttributes());
|
Chris@13
|
635 return new Expr\StaticCall($staticProp->class, $prop, $args, $attributes);
|
Chris@13
|
636 } else {
|
Chris@13
|
637 throw new \Exception;
|
Chris@13
|
638 }
|
Chris@13
|
639 }
|
Chris@13
|
640
|
Chris@13
|
641 protected function fixupStartAttributes(Node $to, Node $from) {
|
Chris@13
|
642 $startAttributes = ['startLine', 'startFilePos', 'startTokenPos'];
|
Chris@13
|
643 foreach ($startAttributes as $startAttribute) {
|
Chris@13
|
644 if ($from->hasAttribute($startAttribute)) {
|
Chris@13
|
645 $to->setAttribute($startAttribute, $from->getAttribute($startAttribute));
|
Chris@13
|
646 }
|
Chris@13
|
647 }
|
Chris@13
|
648 }
|
Chris@13
|
649
|
Chris@0
|
650 protected function handleBuiltinTypes(Name $name) {
|
Chris@0
|
651 $scalarTypes = [
|
Chris@0
|
652 'bool' => true,
|
Chris@0
|
653 'int' => true,
|
Chris@0
|
654 'float' => true,
|
Chris@0
|
655 'string' => true,
|
Chris@0
|
656 'iterable' => true,
|
Chris@0
|
657 'void' => true,
|
Chris@0
|
658 'object' => true,
|
Chris@0
|
659 ];
|
Chris@0
|
660
|
Chris@0
|
661 if (!$name->isUnqualified()) {
|
Chris@0
|
662 return $name;
|
Chris@0
|
663 }
|
Chris@0
|
664
|
Chris@13
|
665 $lowerName = $name->toLowerString();
|
Chris@13
|
666 if (!isset($scalarTypes[$lowerName])) {
|
Chris@13
|
667 return $name;
|
Chris@13
|
668 }
|
Chris@13
|
669
|
Chris@13
|
670 return new Node\Identifier($lowerName, $name->getAttributes());
|
Chris@0
|
671 }
|
Chris@0
|
672
|
Chris@13
|
673 /**
|
Chris@13
|
674 * Get combined start and end attributes at a stack location
|
Chris@13
|
675 *
|
Chris@13
|
676 * @param int $pos Stack location
|
Chris@13
|
677 *
|
Chris@13
|
678 * @return array Combined start and end attributes
|
Chris@13
|
679 */
|
Chris@13
|
680 protected function getAttributesAt(int $pos) : array {
|
Chris@0
|
681 return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
|
Chris@0
|
682 }
|
Chris@0
|
683
|
Chris@17
|
684 protected function getFloatCastKind(string $cast): int
|
Chris@17
|
685 {
|
Chris@17
|
686 $cast = strtolower($cast);
|
Chris@17
|
687 if (strpos($cast, 'float') !== false) {
|
Chris@17
|
688 return Double::KIND_FLOAT;
|
Chris@17
|
689 }
|
Chris@17
|
690
|
Chris@17
|
691 if (strpos($cast, 'real') !== false) {
|
Chris@17
|
692 return Double::KIND_REAL;
|
Chris@17
|
693 }
|
Chris@17
|
694
|
Chris@17
|
695 return Double::KIND_DOUBLE;
|
Chris@17
|
696 }
|
Chris@17
|
697
|
Chris@0
|
698 protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
|
Chris@0
|
699 try {
|
Chris@0
|
700 return LNumber::fromString($str, $attributes, $allowInvalidOctal);
|
Chris@0
|
701 } catch (Error $error) {
|
Chris@0
|
702 $this->emitError($error);
|
Chris@0
|
703 // Use dummy value
|
Chris@0
|
704 return new LNumber(0, $attributes);
|
Chris@0
|
705 }
|
Chris@0
|
706 }
|
Chris@0
|
707
|
Chris@13
|
708 /**
|
Chris@13
|
709 * Parse a T_NUM_STRING token into either an integer or string node.
|
Chris@13
|
710 *
|
Chris@13
|
711 * @param string $str Number string
|
Chris@13
|
712 * @param array $attributes Attributes
|
Chris@13
|
713 *
|
Chris@13
|
714 * @return LNumber|String_ Integer or string node.
|
Chris@13
|
715 */
|
Chris@13
|
716 protected function parseNumString(string $str, array $attributes) {
|
Chris@0
|
717 if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
|
Chris@0
|
718 return new String_($str, $attributes);
|
Chris@0
|
719 }
|
Chris@0
|
720
|
Chris@0
|
721 $num = +$str;
|
Chris@0
|
722 if (!is_int($num)) {
|
Chris@0
|
723 return new String_($str, $attributes);
|
Chris@0
|
724 }
|
Chris@0
|
725
|
Chris@0
|
726 return new LNumber($num, $attributes);
|
Chris@0
|
727 }
|
Chris@0
|
728
|
Chris@17
|
729 protected function stripIndentation(
|
Chris@17
|
730 string $string, int $indentLen, string $indentChar,
|
Chris@17
|
731 bool $newlineAtStart, bool $newlineAtEnd, array $attributes
|
Chris@17
|
732 ) {
|
Chris@17
|
733 if ($indentLen === 0) {
|
Chris@17
|
734 return $string;
|
Chris@17
|
735 }
|
Chris@17
|
736
|
Chris@17
|
737 $start = $newlineAtStart ? '(?:(?<=\n)|\A)' : '(?<=\n)';
|
Chris@17
|
738 $end = $newlineAtEnd ? '(?:(?=[\r\n])|\z)' : '(?=[\r\n])';
|
Chris@17
|
739 $regex = '/' . $start . '([ \t]*)(' . $end . ')?/';
|
Chris@17
|
740 return preg_replace_callback(
|
Chris@17
|
741 $regex,
|
Chris@17
|
742 function ($matches) use ($indentLen, $indentChar, $attributes) {
|
Chris@17
|
743 $prefix = substr($matches[1], 0, $indentLen);
|
Chris@17
|
744 if (false !== strpos($prefix, $indentChar === " " ? "\t" : " ")) {
|
Chris@17
|
745 $this->emitError(new Error(
|
Chris@17
|
746 'Invalid indentation - tabs and spaces cannot be mixed', $attributes
|
Chris@17
|
747 ));
|
Chris@17
|
748 } elseif (strlen($prefix) < $indentLen && !isset($matches[2])) {
|
Chris@17
|
749 $this->emitError(new Error(
|
Chris@17
|
750 'Invalid body indentation level ' .
|
Chris@17
|
751 '(expecting an indentation level of at least ' . $indentLen . ')',
|
Chris@17
|
752 $attributes
|
Chris@17
|
753 ));
|
Chris@17
|
754 }
|
Chris@17
|
755 return substr($matches[0], strlen($prefix));
|
Chris@17
|
756 },
|
Chris@17
|
757 $string
|
Chris@17
|
758 );
|
Chris@17
|
759 }
|
Chris@17
|
760
|
Chris@17
|
761 protected function parseDocString(
|
Chris@17
|
762 string $startToken, $contents, string $endToken,
|
Chris@17
|
763 array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape
|
Chris@17
|
764 ) {
|
Chris@17
|
765 $kind = strpos($startToken, "'") === false
|
Chris@17
|
766 ? String_::KIND_HEREDOC : String_::KIND_NOWDOC;
|
Chris@17
|
767
|
Chris@17
|
768 $regex = '/\A[bB]?<<<[ \t]*[\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\'"]?(?:\r\n|\n|\r)\z/';
|
Chris@17
|
769 $result = preg_match($regex, $startToken, $matches);
|
Chris@17
|
770 assert($result === 1);
|
Chris@17
|
771 $label = $matches[1];
|
Chris@17
|
772
|
Chris@17
|
773 $result = preg_match('/\A[ \t]*/', $endToken, $matches);
|
Chris@17
|
774 assert($result === 1);
|
Chris@17
|
775 $indentation = $matches[0];
|
Chris@17
|
776
|
Chris@17
|
777 $attributes['kind'] = $kind;
|
Chris@17
|
778 $attributes['docLabel'] = $label;
|
Chris@17
|
779 $attributes['docIndentation'] = $indentation;
|
Chris@17
|
780
|
Chris@17
|
781 $indentHasSpaces = false !== strpos($indentation, " ");
|
Chris@17
|
782 $indentHasTabs = false !== strpos($indentation, "\t");
|
Chris@17
|
783 if ($indentHasSpaces && $indentHasTabs) {
|
Chris@17
|
784 $this->emitError(new Error(
|
Chris@17
|
785 'Invalid indentation - tabs and spaces cannot be mixed',
|
Chris@17
|
786 $endTokenAttributes
|
Chris@17
|
787 ));
|
Chris@17
|
788
|
Chris@17
|
789 // Proceed processing as if this doc string is not indented
|
Chris@17
|
790 $indentation = '';
|
Chris@17
|
791 }
|
Chris@17
|
792
|
Chris@17
|
793 $indentLen = \strlen($indentation);
|
Chris@17
|
794 $indentChar = $indentHasSpaces ? " " : "\t";
|
Chris@17
|
795
|
Chris@17
|
796 if (\is_string($contents)) {
|
Chris@17
|
797 if ($contents === '') {
|
Chris@17
|
798 return new String_('', $attributes);
|
Chris@17
|
799 }
|
Chris@17
|
800
|
Chris@17
|
801 $contents = $this->stripIndentation(
|
Chris@17
|
802 $contents, $indentLen, $indentChar, true, true, $attributes
|
Chris@17
|
803 );
|
Chris@17
|
804 $contents = preg_replace('~(\r\n|\n|\r)\z~', '', $contents);
|
Chris@17
|
805
|
Chris@17
|
806 if ($kind === String_::KIND_HEREDOC) {
|
Chris@17
|
807 $contents = String_::parseEscapeSequences($contents, null, $parseUnicodeEscape);
|
Chris@17
|
808 }
|
Chris@17
|
809
|
Chris@17
|
810 return new String_($contents, $attributes);
|
Chris@17
|
811 } else {
|
Chris@17
|
812 assert(count($contents) > 0);
|
Chris@17
|
813 if (!$contents[0] instanceof Node\Scalar\EncapsedStringPart) {
|
Chris@17
|
814 // If there is no leading encapsed string part, pretend there is an empty one
|
Chris@17
|
815 $this->stripIndentation(
|
Chris@17
|
816 '', $indentLen, $indentChar, true, false, $contents[0]->getAttributes()
|
Chris@17
|
817 );
|
Chris@17
|
818 }
|
Chris@17
|
819
|
Chris@17
|
820 $newContents = [];
|
Chris@17
|
821 foreach ($contents as $i => $part) {
|
Chris@17
|
822 if ($part instanceof Node\Scalar\EncapsedStringPart) {
|
Chris@17
|
823 $isLast = $i === \count($contents) - 1;
|
Chris@17
|
824 $part->value = $this->stripIndentation(
|
Chris@17
|
825 $part->value, $indentLen, $indentChar,
|
Chris@17
|
826 $i === 0, $isLast, $part->getAttributes()
|
Chris@17
|
827 );
|
Chris@17
|
828 $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape);
|
Chris@17
|
829 if ($isLast) {
|
Chris@17
|
830 $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value);
|
Chris@17
|
831 }
|
Chris@17
|
832 if ('' === $part->value) {
|
Chris@17
|
833 continue;
|
Chris@17
|
834 }
|
Chris@17
|
835 }
|
Chris@17
|
836 $newContents[] = $part;
|
Chris@17
|
837 }
|
Chris@17
|
838 return new Encapsed($newContents, $attributes);
|
Chris@17
|
839 }
|
Chris@17
|
840 }
|
Chris@17
|
841
|
Chris@0
|
842 protected function checkModifier($a, $b, $modifierPos) {
|
Chris@0
|
843 // Jumping through some hoops here because verifyModifier() is also used elsewhere
|
Chris@0
|
844 try {
|
Chris@0
|
845 Class_::verifyModifier($a, $b);
|
Chris@0
|
846 } catch (Error $error) {
|
Chris@0
|
847 $error->setAttributes($this->getAttributesAt($modifierPos));
|
Chris@0
|
848 $this->emitError($error);
|
Chris@0
|
849 }
|
Chris@0
|
850 }
|
Chris@0
|
851
|
Chris@0
|
852 protected function checkParam(Param $node) {
|
Chris@0
|
853 if ($node->variadic && null !== $node->default) {
|
Chris@0
|
854 $this->emitError(new Error(
|
Chris@0
|
855 'Variadic parameter cannot have a default value',
|
Chris@0
|
856 $node->default->getAttributes()
|
Chris@0
|
857 ));
|
Chris@0
|
858 }
|
Chris@0
|
859 }
|
Chris@0
|
860
|
Chris@0
|
861 protected function checkTryCatch(TryCatch $node) {
|
Chris@0
|
862 if (empty($node->catches) && null === $node->finally) {
|
Chris@0
|
863 $this->emitError(new Error(
|
Chris@0
|
864 'Cannot use try without catch or finally', $node->getAttributes()
|
Chris@0
|
865 ));
|
Chris@0
|
866 }
|
Chris@0
|
867 }
|
Chris@0
|
868
|
Chris@0
|
869 protected function checkNamespace(Namespace_ $node) {
|
Chris@13
|
870 if ($node->name && $node->name->isSpecialClassName()) {
|
Chris@0
|
871 $this->emitError(new Error(
|
Chris@0
|
872 sprintf('Cannot use \'%s\' as namespace name', $node->name),
|
Chris@0
|
873 $node->name->getAttributes()
|
Chris@0
|
874 ));
|
Chris@0
|
875 }
|
Chris@0
|
876
|
Chris@0
|
877 if (null !== $node->stmts) {
|
Chris@0
|
878 foreach ($node->stmts as $stmt) {
|
Chris@0
|
879 if ($stmt instanceof Namespace_) {
|
Chris@0
|
880 $this->emitError(new Error(
|
Chris@0
|
881 'Namespace declarations cannot be nested', $stmt->getAttributes()
|
Chris@0
|
882 ));
|
Chris@0
|
883 }
|
Chris@0
|
884 }
|
Chris@0
|
885 }
|
Chris@0
|
886 }
|
Chris@0
|
887
|
Chris@0
|
888 protected function checkClass(Class_ $node, $namePos) {
|
Chris@13
|
889 if (null !== $node->name && $node->name->isSpecialClassName()) {
|
Chris@0
|
890 $this->emitError(new Error(
|
Chris@0
|
891 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
|
Chris@0
|
892 $this->getAttributesAt($namePos)
|
Chris@0
|
893 ));
|
Chris@0
|
894 }
|
Chris@0
|
895
|
Chris@13
|
896 if ($node->extends && $node->extends->isSpecialClassName()) {
|
Chris@0
|
897 $this->emitError(new Error(
|
Chris@0
|
898 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends),
|
Chris@0
|
899 $node->extends->getAttributes()
|
Chris@0
|
900 ));
|
Chris@0
|
901 }
|
Chris@0
|
902
|
Chris@0
|
903 foreach ($node->implements as $interface) {
|
Chris@13
|
904 if ($interface->isSpecialClassName()) {
|
Chris@0
|
905 $this->emitError(new Error(
|
Chris@0
|
906 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
|
Chris@0
|
907 $interface->getAttributes()
|
Chris@0
|
908 ));
|
Chris@0
|
909 }
|
Chris@0
|
910 }
|
Chris@0
|
911 }
|
Chris@0
|
912
|
Chris@0
|
913 protected function checkInterface(Interface_ $node, $namePos) {
|
Chris@13
|
914 if (null !== $node->name && $node->name->isSpecialClassName()) {
|
Chris@0
|
915 $this->emitError(new Error(
|
Chris@0
|
916 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
|
Chris@0
|
917 $this->getAttributesAt($namePos)
|
Chris@0
|
918 ));
|
Chris@0
|
919 }
|
Chris@0
|
920
|
Chris@0
|
921 foreach ($node->extends as $interface) {
|
Chris@13
|
922 if ($interface->isSpecialClassName()) {
|
Chris@0
|
923 $this->emitError(new Error(
|
Chris@0
|
924 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
|
Chris@0
|
925 $interface->getAttributes()
|
Chris@0
|
926 ));
|
Chris@0
|
927 }
|
Chris@0
|
928 }
|
Chris@0
|
929 }
|
Chris@0
|
930
|
Chris@0
|
931 protected function checkClassMethod(ClassMethod $node, $modifierPos) {
|
Chris@0
|
932 if ($node->flags & Class_::MODIFIER_STATIC) {
|
Chris@13
|
933 switch ($node->name->toLowerString()) {
|
Chris@0
|
934 case '__construct':
|
Chris@0
|
935 $this->emitError(new Error(
|
Chris@0
|
936 sprintf('Constructor %s() cannot be static', $node->name),
|
Chris@0
|
937 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
938 break;
|
Chris@0
|
939 case '__destruct':
|
Chris@0
|
940 $this->emitError(new Error(
|
Chris@0
|
941 sprintf('Destructor %s() cannot be static', $node->name),
|
Chris@0
|
942 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
943 break;
|
Chris@0
|
944 case '__clone':
|
Chris@0
|
945 $this->emitError(new Error(
|
Chris@0
|
946 sprintf('Clone method %s() cannot be static', $node->name),
|
Chris@0
|
947 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
948 break;
|
Chris@0
|
949 }
|
Chris@0
|
950 }
|
Chris@0
|
951 }
|
Chris@0
|
952
|
Chris@0
|
953 protected function checkClassConst(ClassConst $node, $modifierPos) {
|
Chris@0
|
954 if ($node->flags & Class_::MODIFIER_STATIC) {
|
Chris@0
|
955 $this->emitError(new Error(
|
Chris@0
|
956 "Cannot use 'static' as constant modifier",
|
Chris@0
|
957 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
958 }
|
Chris@0
|
959 if ($node->flags & Class_::MODIFIER_ABSTRACT) {
|
Chris@0
|
960 $this->emitError(new Error(
|
Chris@0
|
961 "Cannot use 'abstract' as constant modifier",
|
Chris@0
|
962 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
963 }
|
Chris@0
|
964 if ($node->flags & Class_::MODIFIER_FINAL) {
|
Chris@0
|
965 $this->emitError(new Error(
|
Chris@0
|
966 "Cannot use 'final' as constant modifier",
|
Chris@0
|
967 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
968 }
|
Chris@0
|
969 }
|
Chris@0
|
970
|
Chris@0
|
971 protected function checkProperty(Property $node, $modifierPos) {
|
Chris@0
|
972 if ($node->flags & Class_::MODIFIER_ABSTRACT) {
|
Chris@0
|
973 $this->emitError(new Error('Properties cannot be declared abstract',
|
Chris@0
|
974 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
975 }
|
Chris@0
|
976
|
Chris@0
|
977 if ($node->flags & Class_::MODIFIER_FINAL) {
|
Chris@0
|
978 $this->emitError(new Error('Properties cannot be declared final',
|
Chris@0
|
979 $this->getAttributesAt($modifierPos)));
|
Chris@0
|
980 }
|
Chris@0
|
981 }
|
Chris@0
|
982
|
Chris@0
|
983 protected function checkUseUse(UseUse $node, $namePos) {
|
Chris@13
|
984 if ($node->alias && $node->alias->isSpecialClassName()) {
|
Chris@0
|
985 $this->emitError(new Error(
|
Chris@0
|
986 sprintf(
|
Chris@0
|
987 'Cannot use %s as %s because \'%2$s\' is a special class name',
|
Chris@0
|
988 $node->name, $node->alias
|
Chris@0
|
989 ),
|
Chris@0
|
990 $this->getAttributesAt($namePos)
|
Chris@0
|
991 ));
|
Chris@0
|
992 }
|
Chris@0
|
993 }
|
Chris@0
|
994 }
|