comparison vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php @ 13:5fb285c0d0e3

Update Drupal core to 8.4.7 via Composer. Security update; I *think* we've been lucky to get away with this so far, as we don't support self-registration which seems to be used by the so-called "drupalgeddon 2" attack that 8.4.5 was vulnerable to.
author Chris Cannam
date Mon, 23 Apr 2018 09:33:26 +0100
parents 4c8ae668cc8c
children c2387f117808
comparison
equal deleted inserted replaced
12:7a779792577d 13:5fb285c0d0e3
1 <?php 1 <?php declare(strict_types=1);
2 2
3 namespace PhpParser; 3 namespace PhpParser;
4 4
5 /* 5 /*
6 * This parser is based on a skeleton written by Moriyoshi Koizumi, which in 6 * This parser is based on a skeleton written by Moriyoshi Koizumi, which in
7 * turn is based on work by Masato Bito. 7 * turn is based on work by Masato Bito.
8 */ 8 */
9 use PhpParser\Node\Expr;
9 use PhpParser\Node\Name; 10 use PhpParser\Node\Name;
10 use PhpParser\Node\Param; 11 use PhpParser\Node\Param;
11 use PhpParser\Node\Scalar\LNumber; 12 use PhpParser\Node\Scalar\LNumber;
12 use PhpParser\Node\Scalar\String_; 13 use PhpParser\Node\Scalar\String_;
13 use PhpParser\Node\Stmt\Class_; 14 use PhpParser\Node\Stmt\Class_;
16 use PhpParser\Node\Stmt\Interface_; 17 use PhpParser\Node\Stmt\Interface_;
17 use PhpParser\Node\Stmt\Namespace_; 18 use PhpParser\Node\Stmt\Namespace_;
18 use PhpParser\Node\Stmt\Property; 19 use PhpParser\Node\Stmt\Property;
19 use PhpParser\Node\Stmt\TryCatch; 20 use PhpParser\Node\Stmt\TryCatch;
20 use PhpParser\Node\Stmt\UseUse; 21 use PhpParser\Node\Stmt\UseUse;
22 use PhpParser\Node\VarLikeIdentifier;
21 23
22 abstract class ParserAbstract implements Parser 24 abstract class ParserAbstract implements Parser
23 { 25 {
24 const SYMBOL_NONE = -1; 26 const SYMBOL_NONE = -1;
25 27
42 protected $defaultAction; 44 protected $defaultAction;
43 /** @var int Rule number signifying that an unexpected token was encountered */ 45 /** @var int Rule number signifying that an unexpected token was encountered */
44 protected $unexpectedTokenRule; 46 protected $unexpectedTokenRule;
45 47
46 protected $YY2TBLSTATE; 48 protected $YY2TBLSTATE;
47 protected $YYNLSTATES; 49 /** @var int Number of non-leaf states */
48 50 protected $numNonLeafStates;
49 /** @var array Map of lexer tokens to internal symbols */ 51
52 /** @var int[] Map of lexer tokens to internal symbols */
50 protected $tokenToSymbol; 53 protected $tokenToSymbol;
51 /** @var array Map of symbols to their names */ 54 /** @var string[] Map of symbols to their names */
52 protected $symbolToName; 55 protected $symbolToName;
53 /** @var array Names of the production rules (only necessary for debugging) */ 56 /** @var array Names of the production rules (only necessary for debugging) */
54 protected $productions; 57 protected $productions;
55 58
56 /** @var array Map of states to a displacement into the $action table. The corresponding action for this 59 /** @var int[] Map of states to a displacement into the $action table. The corresponding action for this
57 * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the 60 * state/symbol pair is $action[$actionBase[$state] + $symbol]. If $actionBase[$state] is 0, the
58 action is defaulted, i.e. $actionDefault[$state] should be used instead. */ 61 action is defaulted, i.e. $actionDefault[$state] should be used instead. */
59 protected $actionBase; 62 protected $actionBase;
60 /** @var array Table of actions. Indexed according to $actionBase comment. */ 63 /** @var int[] Table of actions. Indexed according to $actionBase comment. */
61 protected $action; 64 protected $action;
62 /** @var array Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol 65 /** @var int[] Table indexed analogously to $action. If $actionCheck[$actionBase[$state] + $symbol] != $symbol
63 * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */ 66 * then the action is defaulted, i.e. $actionDefault[$state] should be used instead. */
64 protected $actionCheck; 67 protected $actionCheck;
65 /** @var array Map of states to their default action */ 68 /** @var int[] Map of states to their default action */
66 protected $actionDefault; 69 protected $actionDefault;
67 70 /** @var callable[] Semantic action callbacks */
68 /** @var array Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this 71 protected $reduceCallbacks;
72
73 /** @var int[] Map of non-terminals to a displacement into the $goto table. The corresponding goto state for this
69 * non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */ 74 * non-terminal/state pair is $goto[$gotoBase[$nonTerminal] + $state] (unless defaulted) */
70 protected $gotoBase; 75 protected $gotoBase;
71 /** @var array Table of states to goto after reduction. Indexed according to $gotoBase comment. */ 76 /** @var int[] Table of states to goto after reduction. Indexed according to $gotoBase comment. */
72 protected $goto; 77 protected $goto;
73 /** @var array Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal 78 /** @var int[] Table indexed analogously to $goto. If $gotoCheck[$gotoBase[$nonTerminal] + $state] != $nonTerminal
74 * then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */ 79 * then the goto state is defaulted, i.e. $gotoDefault[$nonTerminal] should be used. */
75 protected $gotoCheck; 80 protected $gotoCheck;
76 /** @var array Map of non-terminals to the default state to goto after their reduction */ 81 /** @var int[] Map of non-terminals to the default state to goto after their reduction */
77 protected $gotoDefault; 82 protected $gotoDefault;
78 83
79 /** @var array Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for 84 /** @var int[] Map of rules to the non-terminal on their left-hand side, i.e. the non-terminal to use for
80 * determining the state to goto after reduction. */ 85 * determining the state to goto after reduction. */
81 protected $ruleToNonTerminal; 86 protected $ruleToNonTerminal;
82 /** @var array Map of rules to the length of their right-hand side, which is the number of elements that have to 87 /** @var int[] Map of rules to the length of their right-hand side, which is the number of elements that have to
83 * be popped from the stack(s) on reduction. */ 88 * be popped from the stack(s) on reduction. */
84 protected $ruleToLength; 89 protected $ruleToLength;
85 90
86 /* 91 /*
87 * The following members are part of the parser state: 92 * The following members are part of the parser state:
89 94
90 /** @var Lexer Lexer that is used when parsing */ 95 /** @var Lexer Lexer that is used when parsing */
91 protected $lexer; 96 protected $lexer;
92 /** @var mixed Temporary value containing the result of last semantic action (reduction) */ 97 /** @var mixed Temporary value containing the result of last semantic action (reduction) */
93 protected $semValue; 98 protected $semValue;
94 /** @var int Position in stacks (state stack, semantic value stack, attribute stack) */
95 protected $stackPos;
96 /** @var array Semantic value stack (contains values of tokens and semantic action results) */ 99 /** @var array Semantic value stack (contains values of tokens and semantic action results) */
97 protected $semStack; 100 protected $semStack;
98 /** @var array[] Start attribute stack */ 101 /** @var array[] Start attribute stack */
99 protected $startAttributeStack; 102 protected $startAttributeStack;
100 /** @var array[] End attribute stack */ 103 /** @var array[] End attribute stack */
110 protected $errors; 113 protected $errors;
111 /** @var int Error state, used to avoid error floods */ 114 /** @var int Error state, used to avoid error floods */
112 protected $errorState; 115 protected $errorState;
113 116
114 /** 117 /**
118 * Initialize $reduceCallbacks map.
119 */
120 abstract protected function initReduceCallbacks();
121
122 /**
115 * Creates a parser instance. 123 * Creates a parser instance.
116 * 124 *
125 * Options: Currently none.
126 *
117 * @param Lexer $lexer A lexer 127 * @param Lexer $lexer A lexer
118 * @param array $options Options array. Currently no options are supported. 128 * @param array $options Options array.
119 */ 129 */
120 public function __construct(Lexer $lexer, array $options = array()) { 130 public function __construct(Lexer $lexer, array $options = []) {
121 $this->lexer = $lexer; 131 $this->lexer = $lexer;
122 $this->errors = array(); 132 $this->errors = [];
123 133
124 if (isset($options['throwOnError'])) { 134 if (isset($options['throwOnError'])) {
125 throw new \LogicException( 135 throw new \LogicException(
126 '"throwOnError" is no longer supported, use "errorHandler" instead'); 136 '"throwOnError" is no longer supported, use "errorHandler" instead');
127 } 137 }
138
139 $this->initReduceCallbacks();
128 } 140 }
129 141
130 /** 142 /**
131 * Parses PHP code into a node tree. 143 * Parses PHP code into a node tree.
132 * 144 *
135 * 147 *
136 * @param string $code The source code to parse 148 * @param string $code The source code to parse
137 * @param ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults 149 * @param ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults
138 * to ErrorHandler\Throwing. 150 * to ErrorHandler\Throwing.
139 * 151 *
140 * @return Node[]|null Array of statements (or null if the 'throwOnError' option is disabled and the parser was 152 * @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
141 * unable to recover from an error). 153 * the parser was unable to recover from an error).
142 */ 154 */
143 public function parse($code, ErrorHandler $errorHandler = null) { 155 public function parse(string $code, ErrorHandler $errorHandler = null) {
144 $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing; 156 $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing;
145 157
146 // Initialize the lexer
147 $this->lexer->startLexing($code, $this->errorHandler); 158 $this->lexer->startLexing($code, $this->errorHandler);
148 159 $result = $this->doParse();
160
161 // Clear out some of the interior state, so we don't hold onto unnecessary
162 // memory between uses of the parser
163 $this->startAttributeStack = [];
164 $this->endAttributeStack = [];
165 $this->semStack = [];
166 $this->semValue = null;
167
168 return $result;
169 }
170
171 protected function doParse() {
149 // We start off with no lookahead-token 172 // We start off with no lookahead-token
150 $symbol = self::SYMBOL_NONE; 173 $symbol = self::SYMBOL_NONE;
151 174
152 // The attributes for a node are taken from the first and last token of the node. 175 // The attributes for a node are taken from the first and last token of the node.
153 // From the first token only the startAttributes are taken and from the last only 176 // From the first token only the startAttributes are taken and from the last only
154 // the endAttributes. Both are merged using the array union operator (+). 177 // the endAttributes. Both are merged using the array union operator (+).
155 $startAttributes = '*POISON'; 178 $startAttributes = [];
156 $endAttributes = '*POISON'; 179 $endAttributes = [];
157 $this->endAttributes = $endAttributes; 180 $this->endAttributes = $endAttributes;
158 181
159 // Keep stack of start and end attributes 182 // Keep stack of start and end attributes
160 $this->startAttributeStack = array(); 183 $this->startAttributeStack = [];
161 $this->endAttributeStack = array($endAttributes); 184 $this->endAttributeStack = [$endAttributes];
162 185
163 // Start off in the initial state and keep a stack of previous states 186 // Start off in the initial state and keep a stack of previous states
164 $state = 0; 187 $state = 0;
165 $stateStack = array($state); 188 $stateStack = [$state];
166 189
167 // Semantic value stack (contains values of tokens and semantic action results) 190 // Semantic value stack (contains values of tokens and semantic action results)
168 $this->semStack = array(); 191 $this->semStack = [];
169 192
170 // Current position in the stack(s) 193 // Current position in the stack(s)
171 $this->stackPos = 0; 194 $stackPos = 0;
172 195
173 $this->errorState = 0; 196 $this->errorState = 0;
174 197
175 for (;;) { 198 for (;;) {
176 //$this->traceNewState($state, $symbol); 199 //$this->traceNewState($state, $symbol);
177 200
178 if ($this->actionBase[$state] == 0) { 201 if ($this->actionBase[$state] === 0) {
179 $rule = $this->actionDefault[$state]; 202 $rule = $this->actionDefault[$state];
180 } else { 203 } else {
181 if ($symbol === self::SYMBOL_NONE) { 204 if ($symbol === self::SYMBOL_NONE) {
182 // Fetch the next token id from the lexer and fetch additional info by-ref. 205 // Fetch the next token id from the lexer and fetch additional info by-ref.
183 // The end attributes are fetched into a temporary variable and only set once the token is really 206 // The end attributes are fetched into a temporary variable and only set once the token is really
197 )); 220 ));
198 } 221 }
199 222
200 // This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get 223 // This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get
201 // the attributes of the next token, even though they don't contain it themselves. 224 // the attributes of the next token, even though they don't contain it themselves.
202 $this->startAttributeStack[$this->stackPos+1] = $startAttributes; 225 $this->startAttributeStack[$stackPos+1] = $startAttributes;
203 $this->endAttributeStack[$this->stackPos+1] = $endAttributes; 226 $this->endAttributeStack[$stackPos+1] = $endAttributes;
204 $this->lookaheadStartAttributes = $startAttributes; 227 $this->lookaheadStartAttributes = $startAttributes;
205 228
206 //$this->traceRead($symbol); 229 //$this->traceRead($symbol);
207 } 230 }
208 231
209 $idx = $this->actionBase[$state] + $symbol; 232 $idx = $this->actionBase[$state] + $symbol;
210 if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol) 233 if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)
211 || ($state < $this->YY2TBLSTATE 234 || ($state < $this->YY2TBLSTATE
212 && ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0 235 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
213 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $symbol)) 236 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol))
214 && ($action = $this->action[$idx]) != $this->defaultAction) { 237 && ($action = $this->action[$idx]) !== $this->defaultAction) {
215 /* 238 /*
216 * >= YYNLSTATES: shift and reduce 239 * >= numNonLeafStates: shift and reduce
217 * > 0: shift 240 * > 0: shift
218 * = 0: accept 241 * = 0: accept
219 * < 0: reduce 242 * < 0: reduce
220 * = -YYUNEXPECTED: error 243 * = -YYUNEXPECTED: error
221 */ 244 */
222 if ($action > 0) { 245 if ($action > 0) {
223 /* shift */ 246 /* shift */
224 //$this->traceShift($symbol); 247 //$this->traceShift($symbol);
225 248
226 ++$this->stackPos; 249 ++$stackPos;
227 $stateStack[$this->stackPos] = $state = $action; 250 $stateStack[$stackPos] = $state = $action;
228 $this->semStack[$this->stackPos] = $tokenValue; 251 $this->semStack[$stackPos] = $tokenValue;
229 $this->startAttributeStack[$this->stackPos] = $startAttributes; 252 $this->startAttributeStack[$stackPos] = $startAttributes;
230 $this->endAttributeStack[$this->stackPos] = $endAttributes; 253 $this->endAttributeStack[$stackPos] = $endAttributes;
231 $this->endAttributes = $endAttributes; 254 $this->endAttributes = $endAttributes;
232 $symbol = self::SYMBOL_NONE; 255 $symbol = self::SYMBOL_NONE;
233 256
234 if ($this->errorState) { 257 if ($this->errorState) {
235 --$this->errorState; 258 --$this->errorState;
236 } 259 }
237 260
238 if ($action < $this->YYNLSTATES) { 261 if ($action < $this->numNonLeafStates) {
239 continue; 262 continue;
240 } 263 }
241 264
242 /* $yyn >= YYNLSTATES means shift-and-reduce */ 265 /* $yyn >= numNonLeafStates means shift-and-reduce */
243 $rule = $action - $this->YYNLSTATES; 266 $rule = $action - $this->numNonLeafStates;
244 } else { 267 } else {
245 $rule = -$action; 268 $rule = -$action;
246 } 269 }
247 } else { 270 } else {
248 $rule = $this->actionDefault[$state]; 271 $rule = $this->actionDefault[$state];
257 } elseif ($rule !== $this->unexpectedTokenRule) { 280 } elseif ($rule !== $this->unexpectedTokenRule) {
258 /* reduce */ 281 /* reduce */
259 //$this->traceReduce($rule); 282 //$this->traceReduce($rule);
260 283
261 try { 284 try {
262 $this->{'reduceRule' . $rule}(); 285 $this->reduceCallbacks[$rule]($stackPos);
263 } catch (Error $e) { 286 } catch (Error $e) {
264 if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) { 287 if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) {
265 $e->setStartLine($startAttributes['startLine']); 288 $e->setStartLine($startAttributes['startLine']);
266 } 289 }
267 290
269 // Can't recover from this type of error 292 // Can't recover from this type of error
270 return null; 293 return null;
271 } 294 }
272 295
273 /* Goto - shift nonterminal */ 296 /* Goto - shift nonterminal */
274 $lastEndAttributes = $this->endAttributeStack[$this->stackPos]; 297 $lastEndAttributes = $this->endAttributeStack[$stackPos];
275 $this->stackPos -= $this->ruleToLength[$rule]; 298 $stackPos -= $this->ruleToLength[$rule];
276 $nonTerminal = $this->ruleToNonTerminal[$rule]; 299 $nonTerminal = $this->ruleToNonTerminal[$rule];
277 $idx = $this->gotoBase[$nonTerminal] + $stateStack[$this->stackPos]; 300 $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos];
278 if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] == $nonTerminal) { 301 if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) {
279 $state = $this->goto[$idx]; 302 $state = $this->goto[$idx];
280 } else { 303 } else {
281 $state = $this->gotoDefault[$nonTerminal]; 304 $state = $this->gotoDefault[$nonTerminal];
282 } 305 }
283 306
284 ++$this->stackPos; 307 ++$stackPos;
285 $stateStack[$this->stackPos] = $state; 308 $stateStack[$stackPos] = $state;
286 $this->semStack[$this->stackPos] = $this->semValue; 309 $this->semStack[$stackPos] = $this->semValue;
287 $this->endAttributeStack[$this->stackPos] = $lastEndAttributes; 310 $this->endAttributeStack[$stackPos] = $lastEndAttributes;
288 } else { 311 } else {
289 /* error */ 312 /* error */
290 switch ($this->errorState) { 313 switch ($this->errorState) {
291 case 0: 314 case 0:
292 $msg = $this->getErrorMessage($symbol, $state); 315 $msg = $this->getErrorMessage($symbol, $state);
297 $this->errorState = 3; 320 $this->errorState = 3;
298 321
299 // Pop until error-expecting state uncovered 322 // Pop until error-expecting state uncovered
300 while (!( 323 while (!(
301 (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0 324 (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0
302 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol) 325 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
303 || ($state < $this->YY2TBLSTATE 326 || ($state < $this->YY2TBLSTATE
304 && ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $this->errorSymbol) >= 0 327 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0
305 && $idx < $this->actionTableSize && $this->actionCheck[$idx] == $this->errorSymbol) 328 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
306 ) || ($action = $this->action[$idx]) == $this->defaultAction) { // Not totally sure about this 329 ) || ($action = $this->action[$idx]) === $this->defaultAction) { // Not totally sure about this
307 if ($this->stackPos <= 0) { 330 if ($stackPos <= 0) {
308 // Could not recover from error 331 // Could not recover from error
309 return null; 332 return null;
310 } 333 }
311 $state = $stateStack[--$this->stackPos]; 334 $state = $stateStack[--$stackPos];
312 //$this->tracePop($state); 335 //$this->tracePop($state);
313 } 336 }
314 337
315 //$this->traceShift($this->errorSymbol); 338 //$this->traceShift($this->errorSymbol);
316 ++$this->stackPos; 339 ++$stackPos;
317 $stateStack[$this->stackPos] = $state = $action; 340 $stateStack[$stackPos] = $state = $action;
318 341
319 // We treat the error symbol as being empty, so we reset the end attributes 342 // We treat the error symbol as being empty, so we reset the end attributes
320 // to the end attributes of the last non-error symbol 343 // to the end attributes of the last non-error symbol
321 $this->endAttributeStack[$this->stackPos] = $this->endAttributeStack[$this->stackPos - 1]; 344 $this->endAttributeStack[$stackPos] = $this->endAttributeStack[$stackPos - 1];
322 $this->endAttributes = $this->endAttributeStack[$this->stackPos - 1]; 345 $this->endAttributes = $this->endAttributeStack[$stackPos - 1];
323 break; 346 break;
324 347
325 case 3: 348 case 3:
326 if ($symbol === 0) { 349 if ($symbol === 0) {
327 // Reached EOF without recovering from error 350 // Reached EOF without recovering from error
332 $symbol = self::SYMBOL_NONE; 355 $symbol = self::SYMBOL_NONE;
333 break 2; 356 break 2;
334 } 357 }
335 } 358 }
336 359
337 if ($state < $this->YYNLSTATES) { 360 if ($state < $this->numNonLeafStates) {
338 break; 361 break;
339 } 362 }
340 363
341 /* >= YYNLSTATES means shift-and-reduce */ 364 /* >= numNonLeafStates means shift-and-reduce */
342 $rule = $state - $this->YYNLSTATES; 365 $rule = $state - $this->numNonLeafStates;
343 } 366 }
344 } 367 }
345 368
346 throw new \RuntimeException('Reached end of parser loop'); 369 throw new \RuntimeException('Reached end of parser loop');
347 } 370 }
348 371
349 protected function emitError(Error $error) { 372 protected function emitError(Error $error) {
350 $this->errorHandler->handleError($error); 373 $this->errorHandler->handleError($error);
351 } 374 }
352 375
353 protected function getErrorMessage($symbol, $state) { 376 /**
377 * Format error message including expected tokens.
378 *
379 * @param int $symbol Unexpected symbol
380 * @param int $state State at time of error
381 *
382 * @return string Formatted error message
383 */
384 protected function getErrorMessage(int $symbol, int $state) : string {
354 $expectedString = ''; 385 $expectedString = '';
355 if ($expected = $this->getExpectedTokens($state)) { 386 if ($expected = $this->getExpectedTokens($state)) {
356 $expectedString = ', expecting ' . implode(' or ', $expected); 387 $expectedString = ', expecting ' . implode(' or ', $expected);
357 } 388 }
358 389
359 return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString; 390 return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString;
360 } 391 }
361 392
362 protected function getExpectedTokens($state) { 393 /**
363 $expected = array(); 394 * Get limited number of expected tokens in given state.
395 *
396 * @param int $state State
397 *
398 * @return string[] Expected tokens. If too many, an empty array is returned.
399 */
400 protected function getExpectedTokens(int $state) : array {
401 $expected = [];
364 402
365 $base = $this->actionBase[$state]; 403 $base = $this->actionBase[$state];
366 foreach ($this->symbolToName as $symbol => $name) { 404 foreach ($this->symbolToName as $symbol => $name) {
367 $idx = $base + $symbol; 405 $idx = $base + $symbol;
368 if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol 406 if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
369 || $state < $this->YY2TBLSTATE 407 || $state < $this->YY2TBLSTATE
370 && ($idx = $this->actionBase[$state + $this->YYNLSTATES] + $symbol) >= 0 408 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
371 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol 409 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
372 ) { 410 ) {
373 if ($this->action[$idx] != $this->unexpectedTokenRule 411 if ($this->action[$idx] !== $this->unexpectedTokenRule
374 && $this->action[$idx] != $this->defaultAction 412 && $this->action[$idx] !== $this->defaultAction
375 && $symbol != $this->errorSymbol 413 && $symbol !== $this->errorSymbol
376 ) { 414 ) {
377 if (count($expected) == 4) { 415 if (count($expected) === 4) {
378 /* Too many expected tokens */ 416 /* Too many expected tokens */
379 return array(); 417 return [];
380 } 418 }
381 419
382 $expected[] = $name; 420 $expected[] = $name;
383 } 421 }
384 } 422 }
427 */ 465 */
428 466
429 /** 467 /**
430 * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions. 468 * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions.
431 * 469 *
432 * @param Node[] $stmts 470 * @param Node\Stmt[] $stmts
433 * @return Node[] 471 * @return Node\Stmt[]
434 */ 472 */
435 protected function handleNamespaces(array $stmts) { 473 protected function handleNamespaces(array $stmts) : array {
436 $hasErrored = false; 474 $hasErrored = false;
437 $style = $this->getNamespacingStyle($stmts); 475 $style = $this->getNamespacingStyle($stmts);
438 if (null === $style) { 476 if (null === $style) {
439 // not namespaced, nothing to do 477 // not namespaced, nothing to do
440 return $stmts; 478 return $stmts;
453 } 491 }
454 } 492 }
455 return $stmts; 493 return $stmts;
456 } else { 494 } else {
457 // For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts 495 // For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts
458 $resultStmts = array(); 496 $resultStmts = [];
459 $targetStmts =& $resultStmts; 497 $targetStmts =& $resultStmts;
498 $lastNs = null;
460 foreach ($stmts as $stmt) { 499 foreach ($stmts as $stmt) {
461 if ($stmt instanceof Node\Stmt\Namespace_) { 500 if ($stmt instanceof Node\Stmt\Namespace_) {
501 if ($lastNs !== null) {
502 $this->fixupNamespaceAttributes($lastNs);
503 }
462 if ($stmt->stmts === null) { 504 if ($stmt->stmts === null) {
463 $stmt->stmts = array(); 505 $stmt->stmts = [];
464 $targetStmts =& $stmt->stmts; 506 $targetStmts =& $stmt->stmts;
465 $resultStmts[] = $stmt; 507 $resultStmts[] = $stmt;
466 } else { 508 } else {
467 // This handles the invalid case of mixed style namespaces 509 // This handles the invalid case of mixed style namespaces
468 $resultStmts[] = $stmt; 510 $resultStmts[] = $stmt;
469 $targetStmts =& $resultStmts; 511 $targetStmts =& $resultStmts;
470 } 512 }
513 $lastNs = $stmt;
471 } elseif ($stmt instanceof Node\Stmt\HaltCompiler) { 514 } elseif ($stmt instanceof Node\Stmt\HaltCompiler) {
472 // __halt_compiler() is not moved into the namespace 515 // __halt_compiler() is not moved into the namespace
473 $resultStmts[] = $stmt; 516 $resultStmts[] = $stmt;
474 } else { 517 } else {
475 $targetStmts[] = $stmt; 518 $targetStmts[] = $stmt;
476 } 519 }
477 } 520 }
521 if ($lastNs !== null) {
522 $this->fixupNamespaceAttributes($lastNs);
523 }
478 return $resultStmts; 524 return $resultStmts;
479 } 525 }
480 } 526 }
481 527
528 private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt) {
529 // We moved the statements into the namespace node, as such the end of the namespace node
530 // needs to be extended to the end of the statements.
531 if (empty($stmt->stmts)) {
532 return;
533 }
534
535 // We only move the builtin end attributes here. This is the best we can do with the
536 // knowledge we have.
537 $endAttributes = ['endLine', 'endFilePos', 'endTokenPos'];
538 $lastStmt = $stmt->stmts[count($stmt->stmts) - 1];
539 foreach ($endAttributes as $endAttribute) {
540 if ($lastStmt->hasAttribute($endAttribute)) {
541 $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute));
542 }
543 }
544 }
545
546 /**
547 * Determine namespacing style (semicolon or brace)
548 *
549 * @param Node[] $stmts Top-level statements.
550 *
551 * @return null|string One of "semicolon", "brace" or null (no namespaces)
552 */
482 private function getNamespacingStyle(array $stmts) { 553 private function getNamespacingStyle(array $stmts) {
483 $style = null; 554 $style = null;
484 $hasNotAllowedStmts = false; 555 $hasNotAllowedStmts = false;
485 foreach ($stmts as $i => $stmt) { 556 foreach ($stmts as $i => $stmt) {
486 if ($stmt instanceof Node\Stmt\Namespace_) { 557 if ($stmt instanceof Node\Stmt\Namespace_) {
510 || $stmt instanceof Node\Stmt\Nop) { 581 || $stmt instanceof Node\Stmt\Nop) {
511 continue; 582 continue;
512 } 583 }
513 584
514 /* There may be a hashbang line at the very start of the file */ 585 /* There may be a hashbang line at the very start of the file */
515 if ($i == 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) { 586 if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) {
516 continue; 587 continue;
517 } 588 }
518 589
519 /* Everything else if forbidden before namespace declarations */ 590 /* Everything else if forbidden before namespace declarations */
520 $hasNotAllowedStmts = true; 591 $hasNotAllowedStmts = true;
521 } 592 }
522 return $style; 593 return $style;
594 }
595
596 /**
597 * Fix up parsing of static property calls in PHP 5.
598 *
599 * In PHP 5 A::$b[c][d] and A::$b[c][d]() have very different interpretation. The former is
600 * interpreted as (A::$b)[c][d], while the latter is the same as A::{$b[c][d]}(). We parse the
601 * latter as the former initially and this method fixes the AST into the correct form when we
602 * encounter the "()".
603 *
604 * @param Node\Expr\StaticPropertyFetch|Node\Expr\ArrayDimFetch $prop
605 * @param Node\Arg[] $args
606 * @param array $attributes
607 *
608 * @return Expr\StaticCall
609 */
610 protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) : Expr\StaticCall {
611 if ($prop instanceof Node\Expr\StaticPropertyFetch) {
612 $name = $prop->name instanceof VarLikeIdentifier
613 ? $prop->name->toString() : $prop->name;
614 $var = new Expr\Variable($name, $prop->name->getAttributes());
615 return new Expr\StaticCall($prop->class, $var, $args, $attributes);
616 } elseif ($prop instanceof Node\Expr\ArrayDimFetch) {
617 $tmp = $prop;
618 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
619 $tmp = $tmp->var;
620 }
621
622 /** @var Expr\StaticPropertyFetch $staticProp */
623 $staticProp = $tmp->var;
624
625 // Set start attributes to attributes of innermost node
626 $tmp = $prop;
627 $this->fixupStartAttributes($tmp, $staticProp->name);
628 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
629 $tmp = $tmp->var;
630 $this->fixupStartAttributes($tmp, $staticProp->name);
631 }
632
633 $name = $staticProp->name instanceof VarLikeIdentifier
634 ? $staticProp->name->toString() : $staticProp->name;
635 $tmp->var = new Expr\Variable($name, $staticProp->name->getAttributes());
636 return new Expr\StaticCall($staticProp->class, $prop, $args, $attributes);
637 } else {
638 throw new \Exception;
639 }
640 }
641
642 protected function fixupStartAttributes(Node $to, Node $from) {
643 $startAttributes = ['startLine', 'startFilePos', 'startTokenPos'];
644 foreach ($startAttributes as $startAttribute) {
645 if ($from->hasAttribute($startAttribute)) {
646 $to->setAttribute($startAttribute, $from->getAttribute($startAttribute));
647 }
648 }
523 } 649 }
524 650
525 protected function handleBuiltinTypes(Name $name) { 651 protected function handleBuiltinTypes(Name $name) {
526 $scalarTypes = [ 652 $scalarTypes = [
527 'bool' => true, 653 'bool' => true,
535 661
536 if (!$name->isUnqualified()) { 662 if (!$name->isUnqualified()) {
537 return $name; 663 return $name;
538 } 664 }
539 665
540 $lowerName = strtolower($name->toString()); 666 $lowerName = $name->toLowerString();
541 return isset($scalarTypes[$lowerName]) ? $lowerName : $name; 667 if (!isset($scalarTypes[$lowerName])) {
542 } 668 return $name;
543 669 }
544 protected static $specialNames = array( 670
545 'self' => true, 671 return new Node\Identifier($lowerName, $name->getAttributes());
546 'parent' => true, 672 }
547 'static' => true, 673
548 ); 674 /**
549 675 * Get combined start and end attributes at a stack location
550 protected function getAttributesAt($pos) { 676 *
677 * @param int $pos Stack location
678 *
679 * @return array Combined start and end attributes
680 */
681 protected function getAttributesAt(int $pos) : array {
551 return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos]; 682 return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
552 } 683 }
553 684
554 protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) { 685 protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) {
555 try { 686 try {
559 // Use dummy value 690 // Use dummy value
560 return new LNumber(0, $attributes); 691 return new LNumber(0, $attributes);
561 } 692 }
562 } 693 }
563 694
564 protected function parseNumString($str, $attributes) { 695 /**
696 * Parse a T_NUM_STRING token into either an integer or string node.
697 *
698 * @param string $str Number string
699 * @param array $attributes Attributes
700 *
701 * @return LNumber|String_ Integer or string node.
702 */
703 protected function parseNumString(string $str, array $attributes) {
565 if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) { 704 if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) {
566 return new String_($str, $attributes); 705 return new String_($str, $attributes);
567 } 706 }
568 707
569 $num = +$str; 708 $num = +$str;
600 )); 739 ));
601 } 740 }
602 } 741 }
603 742
604 protected function checkNamespace(Namespace_ $node) { 743 protected function checkNamespace(Namespace_ $node) {
605 if (isset(self::$specialNames[strtolower($node->name)])) { 744 if ($node->name && $node->name->isSpecialClassName()) {
606 $this->emitError(new Error( 745 $this->emitError(new Error(
607 sprintf('Cannot use \'%s\' as namespace name', $node->name), 746 sprintf('Cannot use \'%s\' as namespace name', $node->name),
608 $node->name->getAttributes() 747 $node->name->getAttributes()
609 )); 748 ));
610 } 749 }
619 } 758 }
620 } 759 }
621 } 760 }
622 761
623 protected function checkClass(Class_ $node, $namePos) { 762 protected function checkClass(Class_ $node, $namePos) {
624 if (null !== $node->name && isset(self::$specialNames[strtolower($node->name)])) { 763 if (null !== $node->name && $node->name->isSpecialClassName()) {
625 $this->emitError(new Error( 764 $this->emitError(new Error(
626 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name), 765 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
627 $this->getAttributesAt($namePos) 766 $this->getAttributesAt($namePos)
628 )); 767 ));
629 } 768 }
630 769
631 if (isset(self::$specialNames[strtolower($node->extends)])) { 770 if ($node->extends && $node->extends->isSpecialClassName()) {
632 $this->emitError(new Error( 771 $this->emitError(new Error(
633 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends), 772 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends),
634 $node->extends->getAttributes() 773 $node->extends->getAttributes()
635 )); 774 ));
636 } 775 }
637 776
638 foreach ($node->implements as $interface) { 777 foreach ($node->implements as $interface) {
639 if (isset(self::$specialNames[strtolower($interface)])) { 778 if ($interface->isSpecialClassName()) {
640 $this->emitError(new Error( 779 $this->emitError(new Error(
641 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), 780 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
642 $interface->getAttributes() 781 $interface->getAttributes()
643 )); 782 ));
644 } 783 }
645 } 784 }
646 } 785 }
647 786
648 protected function checkInterface(Interface_ $node, $namePos) { 787 protected function checkInterface(Interface_ $node, $namePos) {
649 if (null !== $node->name && isset(self::$specialNames[strtolower($node->name)])) { 788 if (null !== $node->name && $node->name->isSpecialClassName()) {
650 $this->emitError(new Error( 789 $this->emitError(new Error(
651 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name), 790 sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name),
652 $this->getAttributesAt($namePos) 791 $this->getAttributesAt($namePos)
653 )); 792 ));
654 } 793 }
655 794
656 foreach ($node->extends as $interface) { 795 foreach ($node->extends as $interface) {
657 if (isset(self::$specialNames[strtolower($interface)])) { 796 if ($interface->isSpecialClassName()) {
658 $this->emitError(new Error( 797 $this->emitError(new Error(
659 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), 798 sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface),
660 $interface->getAttributes() 799 $interface->getAttributes()
661 )); 800 ));
662 } 801 }
663 } 802 }
664 } 803 }
665 804
666 protected function checkClassMethod(ClassMethod $node, $modifierPos) { 805 protected function checkClassMethod(ClassMethod $node, $modifierPos) {
667 if ($node->flags & Class_::MODIFIER_STATIC) { 806 if ($node->flags & Class_::MODIFIER_STATIC) {
668 switch (strtolower($node->name)) { 807 switch ($node->name->toLowerString()) {
669 case '__construct': 808 case '__construct':
670 $this->emitError(new Error( 809 $this->emitError(new Error(
671 sprintf('Constructor %s() cannot be static', $node->name), 810 sprintf('Constructor %s() cannot be static', $node->name),
672 $this->getAttributesAt($modifierPos))); 811 $this->getAttributesAt($modifierPos)));
673 break; 812 break;
714 $this->getAttributesAt($modifierPos))); 853 $this->getAttributesAt($modifierPos)));
715 } 854 }
716 } 855 }
717 856
718 protected function checkUseUse(UseUse $node, $namePos) { 857 protected function checkUseUse(UseUse $node, $namePos) {
719 if ('self' == strtolower($node->alias) || 'parent' == strtolower($node->alias)) { 858 if ($node->alias && $node->alias->isSpecialClassName()) {
720 $this->emitError(new Error( 859 $this->emitError(new Error(
721 sprintf( 860 sprintf(
722 'Cannot use %s as %s because \'%2$s\' is a special class name', 861 'Cannot use %s as %s because \'%2$s\' is a special class name',
723 $node->name, $node->alias 862 $node->name, $node->alias
724 ), 863 ),