annotate vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents a9cd425dd02b
children
rev   line source
Chris@0 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@0 9 use PhpParser\Node\Expr;
Chris@4 10 use PhpParser\Node\Expr\Cast\Double;
Chris@0 11 use PhpParser\Node\Name;
Chris@0 12 use PhpParser\Node\Param;
Chris@4 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@0 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@0 51 /** @var int Number of non-leaf states */
Chris@0 52 protected $numNonLeafStates;
Chris@0 53
Chris@0 54 /** @var int[] Map of lexer tokens to internal symbols */
Chris@0 55 protected $tokenToSymbol;
Chris@0 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@0 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@0 65 /** @var int[] Table of actions. Indexed according to $actionBase comment. */
Chris@0 66 protected $action;
Chris@0 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@0 70 /** @var int[] Map of states to their default action */
Chris@0 71 protected $actionDefault;
Chris@0 72 /** @var callable[] Semantic action callbacks */
Chris@0 73 protected $reduceCallbacks;
Chris@0 74
Chris@0 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@0 78 /** @var int[] Table of states to goto after reduction. Indexed according to $gotoBase comment. */
Chris@0 79 protected $goto;
Chris@0 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@0 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@0 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@0 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@0 118 * Initialize $reduceCallbacks map.
Chris@0 119 */
Chris@0 120 abstract protected function initReduceCallbacks();
Chris@0 121
Chris@0 122 /**
Chris@0 123 * Creates a parser instance.
Chris@0 124 *
Chris@0 125 * Options: Currently none.
Chris@0 126 *
Chris@0 127 * @param Lexer $lexer A lexer
Chris@0 128 * @param array $options Options array.
Chris@0 129 */
Chris@0 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@0 137
Chris@0 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@0 151 * @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and
Chris@0 152 * the parser was unable to recover from an error).
Chris@0 153 */
Chris@0 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@0 158 $result = $this->doParse();
Chris@0 159
Chris@0 160 // Clear out some of the interior state, so we don't hold onto unnecessary
Chris@0 161 // memory between uses of the parser
Chris@0 162 $this->startAttributeStack = [];
Chris@0 163 $this->endAttributeStack = [];
Chris@0 164 $this->semStack = [];
Chris@0 165 $this->semValue = null;
Chris@0 166
Chris@0 167 return $result;
Chris@0 168 }
Chris@0 169
Chris@0 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@0 177 $startAttributes = [];
Chris@0 178 $endAttributes = [];
Chris@0 179 $this->endAttributes = $endAttributes;
Chris@0 180
Chris@0 181 // Keep stack of start and end attributes
Chris@0 182 $this->startAttributeStack = [];
Chris@0 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@0 187 $stateStack = [$state];
Chris@0 188
Chris@0 189 // Semantic value stack (contains values of tokens and semantic action results)
Chris@0 190 $this->semStack = [];
Chris@0 191
Chris@0 192 // Current position in the stack(s)
Chris@0 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@0 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@0 224 $this->startAttributeStack[$stackPos+1] = $startAttributes;
Chris@0 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@0 232 if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)
Chris@0 233 || ($state < $this->YY2TBLSTATE
Chris@0 234 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
Chris@0 235 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol))
Chris@0 236 && ($action = $this->action[$idx]) !== $this->defaultAction) {
Chris@0 237 /*
Chris@0 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@0 248 ++$stackPos;
Chris@0 249 $stateStack[$stackPos] = $state = $action;
Chris@0 250 $this->semStack[$stackPos] = $tokenValue;
Chris@0 251 $this->startAttributeStack[$stackPos] = $startAttributes;
Chris@0 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@0 260 if ($action < $this->numNonLeafStates) {
Chris@0 261 continue;
Chris@0 262 }
Chris@0 263
Chris@0 264 /* $yyn >= numNonLeafStates means shift-and-reduce */
Chris@0 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@0 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@0 296 $lastEndAttributes = $this->endAttributeStack[$stackPos];
Chris@0 297 $stackPos -= $this->ruleToLength[$rule];
Chris@0 298 $nonTerminal = $this->ruleToNonTerminal[$rule];
Chris@0 299 $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos];
Chris@0 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@0 306 ++$stackPos;
Chris@0 307 $stateStack[$stackPos] = $state;
Chris@0 308 $this->semStack[$stackPos] = $this->semValue;
Chris@0 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@0 324 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
Chris@0 325 || ($state < $this->YY2TBLSTATE
Chris@0 326 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0
Chris@0 327 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol)
Chris@0 328 ) || ($action = $this->action[$idx]) === $this->defaultAction) { // Not totally sure about this
Chris@0 329 if ($stackPos <= 0) {
Chris@0 330 // Could not recover from error
Chris@0 331 return null;
Chris@0 332 }
Chris@0 333 $state = $stateStack[--$stackPos];
Chris@0 334 //$this->tracePop($state);
Chris@0 335 }
Chris@0 336
Chris@0 337 //$this->traceShift($this->errorSymbol);
Chris@0 338 ++$stackPos;
Chris@0 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@0 343 $this->endAttributeStack[$stackPos] = $this->endAttributeStack[$stackPos - 1];
Chris@0 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@0 359 if ($state < $this->numNonLeafStates) {
Chris@0 360 break;
Chris@0 361 }
Chris@0 362
Chris@0 363 /* >= numNonLeafStates means shift-and-reduce */
Chris@0 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@0 375 /**
Chris@0 376 * Format error message including expected tokens.
Chris@0 377 *
Chris@0 378 * @param int $symbol Unexpected symbol
Chris@0 379 * @param int $state State at time of error
Chris@0 380 *
Chris@0 381 * @return string Formatted error message
Chris@0 382 */
Chris@0 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@0 392 /**
Chris@0 393 * Get limited number of expected tokens in given state.
Chris@0 394 *
Chris@0 395 * @param int $state State
Chris@0 396 *
Chris@0 397 * @return string[] Expected tokens. If too many, an empty array is returned.
Chris@0 398 */
Chris@0 399 protected function getExpectedTokens(int $state) : array {
Chris@0 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@0 407 && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0
Chris@0 408 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol
Chris@0 409 ) {
Chris@0 410 if ($this->action[$idx] !== $this->unexpectedTokenRule
Chris@0 411 && $this->action[$idx] !== $this->defaultAction
Chris@0 412 && $symbol !== $this->errorSymbol
Chris@0 413 ) {
Chris@0 414 if (count($expected) === 4) {
Chris@0 415 /* Too many expected tokens */
Chris@0 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@0 469 * @param Node\Stmt[] $stmts
Chris@0 470 * @return Node\Stmt[]
Chris@0 471 */
Chris@0 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@0 495 $resultStmts = [];
Chris@0 496 $targetStmts =& $resultStmts;
Chris@0 497 $lastNs = null;
Chris@0 498 foreach ($stmts as $stmt) {
Chris@0 499 if ($stmt instanceof Node\Stmt\Namespace_) {
Chris@0 500 if ($lastNs !== null) {
Chris@0 501 $this->fixupNamespaceAttributes($lastNs);
Chris@0 502 }
Chris@0 503 if ($stmt->stmts === null) {
Chris@0 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@0 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@0 520 if ($lastNs !== null) {
Chris@0 521 $this->fixupNamespaceAttributes($lastNs);
Chris@0 522 }
Chris@0 523 return $resultStmts;
Chris@0 524 }
Chris@0 525 }
Chris@0 526
Chris@0 527 private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt) {
Chris@0 528 // We moved the statements into the namespace node, as such the end of the namespace node
Chris@0 529 // needs to be extended to the end of the statements.
Chris@0 530 if (empty($stmt->stmts)) {
Chris@0 531 return;
Chris@0 532 }
Chris@0 533
Chris@0 534 // We only move the builtin end attributes here. This is the best we can do with the
Chris@0 535 // knowledge we have.
Chris@0 536 $endAttributes = ['endLine', 'endFilePos', 'endTokenPos'];
Chris@0 537 $lastStmt = $stmt->stmts[count($stmt->stmts) - 1];
Chris@0 538 foreach ($endAttributes as $endAttribute) {
Chris@0 539 if ($lastStmt->hasAttribute($endAttribute)) {
Chris@0 540 $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute));
Chris@0 541 }
Chris@0 542 }
Chris@0 543 }
Chris@0 544
Chris@0 545 /**
Chris@0 546 * Determine namespacing style (semicolon or brace)
Chris@0 547 *
Chris@0 548 * @param Node[] $stmts Top-level statements.
Chris@0 549 *
Chris@0 550 * @return null|string One of "semicolon", "brace" or null (no namespaces)
Chris@0 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@0 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@0 595 /**
Chris@0 596 * Fix up parsing of static property calls in PHP 5.
Chris@0 597 *
Chris@0 598 * In PHP 5 A::$b[c][d] and A::$b[c][d]() have very different interpretation. The former is
Chris@0 599 * interpreted as (A::$b)[c][d], while the latter is the same as A::{$b[c][d]}(). We parse the
Chris@0 600 * latter as the former initially and this method fixes the AST into the correct form when we
Chris@0 601 * encounter the "()".
Chris@0 602 *
Chris@0 603 * @param Node\Expr\StaticPropertyFetch|Node\Expr\ArrayDimFetch $prop
Chris@0 604 * @param Node\Arg[] $args
Chris@0 605 * @param array $attributes
Chris@0 606 *
Chris@0 607 * @return Expr\StaticCall
Chris@0 608 */
Chris@0 609 protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) : Expr\StaticCall {
Chris@0 610 if ($prop instanceof Node\Expr\StaticPropertyFetch) {
Chris@0 611 $name = $prop->name instanceof VarLikeIdentifier
Chris@0 612 ? $prop->name->toString() : $prop->name;
Chris@0 613 $var = new Expr\Variable($name, $prop->name->getAttributes());
Chris@0 614 return new Expr\StaticCall($prop->class, $var, $args, $attributes);
Chris@0 615 } elseif ($prop instanceof Node\Expr\ArrayDimFetch) {
Chris@0 616 $tmp = $prop;
Chris@0 617 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
Chris@0 618 $tmp = $tmp->var;
Chris@0 619 }
Chris@0 620
Chris@0 621 /** @var Expr\StaticPropertyFetch $staticProp */
Chris@0 622 $staticProp = $tmp->var;
Chris@0 623
Chris@0 624 // Set start attributes to attributes of innermost node
Chris@0 625 $tmp = $prop;
Chris@0 626 $this->fixupStartAttributes($tmp, $staticProp->name);
Chris@0 627 while ($tmp->var instanceof Node\Expr\ArrayDimFetch) {
Chris@0 628 $tmp = $tmp->var;
Chris@0 629 $this->fixupStartAttributes($tmp, $staticProp->name);
Chris@0 630 }
Chris@0 631
Chris@0 632 $name = $staticProp->name instanceof VarLikeIdentifier
Chris@0 633 ? $staticProp->name->toString() : $staticProp->name;
Chris@0 634 $tmp->var = new Expr\Variable($name, $staticProp->name->getAttributes());
Chris@0 635 return new Expr\StaticCall($staticProp->class, $prop, $args, $attributes);
Chris@0 636 } else {
Chris@0 637 throw new \Exception;
Chris@0 638 }
Chris@0 639 }
Chris@0 640
Chris@0 641 protected function fixupStartAttributes(Node $to, Node $from) {
Chris@0 642 $startAttributes = ['startLine', 'startFilePos', 'startTokenPos'];
Chris@0 643 foreach ($startAttributes as $startAttribute) {
Chris@0 644 if ($from->hasAttribute($startAttribute)) {
Chris@0 645 $to->setAttribute($startAttribute, $from->getAttribute($startAttribute));
Chris@0 646 }
Chris@0 647 }
Chris@0 648 }
Chris@0 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@0 665 $lowerName = $name->toLowerString();
Chris@0 666 if (!isset($scalarTypes[$lowerName])) {
Chris@0 667 return $name;
Chris@0 668 }
Chris@0 669
Chris@0 670 return new Node\Identifier($lowerName, $name->getAttributes());
Chris@0 671 }
Chris@0 672
Chris@0 673 /**
Chris@0 674 * Get combined start and end attributes at a stack location
Chris@0 675 *
Chris@0 676 * @param int $pos Stack location
Chris@0 677 *
Chris@0 678 * @return array Combined start and end attributes
Chris@0 679 */
Chris@0 680 protected function getAttributesAt(int $pos) : array {
Chris@0 681 return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos];
Chris@0 682 }
Chris@0 683
Chris@4 684 protected function getFloatCastKind(string $cast): int
Chris@4 685 {
Chris@4 686 $cast = strtolower($cast);
Chris@4 687 if (strpos($cast, 'float') !== false) {
Chris@4 688 return Double::KIND_FLOAT;
Chris@4 689 }
Chris@4 690
Chris@4 691 if (strpos($cast, 'real') !== false) {
Chris@4 692 return Double::KIND_REAL;
Chris@4 693 }
Chris@4 694
Chris@4 695 return Double::KIND_DOUBLE;
Chris@4 696 }
Chris@4 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@0 708 /**
Chris@0 709 * Parse a T_NUM_STRING token into either an integer or string node.
Chris@0 710 *
Chris@0 711 * @param string $str Number string
Chris@0 712 * @param array $attributes Attributes
Chris@0 713 *
Chris@0 714 * @return LNumber|String_ Integer or string node.
Chris@0 715 */
Chris@0 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@4 729 protected function stripIndentation(
Chris@4 730 string $string, int $indentLen, string $indentChar,
Chris@4 731 bool $newlineAtStart, bool $newlineAtEnd, array $attributes
Chris@4 732 ) {
Chris@4 733 if ($indentLen === 0) {
Chris@4 734 return $string;
Chris@4 735 }
Chris@4 736
Chris@4 737 $start = $newlineAtStart ? '(?:(?<=\n)|\A)' : '(?<=\n)';
Chris@4 738 $end = $newlineAtEnd ? '(?:(?=[\r\n])|\z)' : '(?=[\r\n])';
Chris@4 739 $regex = '/' . $start . '([ \t]*)(' . $end . ')?/';
Chris@4 740 return preg_replace_callback(
Chris@4 741 $regex,
Chris@4 742 function ($matches) use ($indentLen, $indentChar, $attributes) {
Chris@4 743 $prefix = substr($matches[1], 0, $indentLen);
Chris@4 744 if (false !== strpos($prefix, $indentChar === " " ? "\t" : " ")) {
Chris@4 745 $this->emitError(new Error(
Chris@4 746 'Invalid indentation - tabs and spaces cannot be mixed', $attributes
Chris@4 747 ));
Chris@4 748 } elseif (strlen($prefix) < $indentLen && !isset($matches[2])) {
Chris@4 749 $this->emitError(new Error(
Chris@4 750 'Invalid body indentation level ' .
Chris@4 751 '(expecting an indentation level of at least ' . $indentLen . ')',
Chris@4 752 $attributes
Chris@4 753 ));
Chris@4 754 }
Chris@4 755 return substr($matches[0], strlen($prefix));
Chris@4 756 },
Chris@4 757 $string
Chris@4 758 );
Chris@4 759 }
Chris@4 760
Chris@4 761 protected function parseDocString(
Chris@4 762 string $startToken, $contents, string $endToken,
Chris@4 763 array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape
Chris@4 764 ) {
Chris@4 765 $kind = strpos($startToken, "'") === false
Chris@4 766 ? String_::KIND_HEREDOC : String_::KIND_NOWDOC;
Chris@4 767
Chris@4 768 $regex = '/\A[bB]?<<<[ \t]*[\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\'"]?(?:\r\n|\n|\r)\z/';
Chris@4 769 $result = preg_match($regex, $startToken, $matches);
Chris@4 770 assert($result === 1);
Chris@4 771 $label = $matches[1];
Chris@4 772
Chris@4 773 $result = preg_match('/\A[ \t]*/', $endToken, $matches);
Chris@4 774 assert($result === 1);
Chris@4 775 $indentation = $matches[0];
Chris@4 776
Chris@4 777 $attributes['kind'] = $kind;
Chris@4 778 $attributes['docLabel'] = $label;
Chris@4 779 $attributes['docIndentation'] = $indentation;
Chris@4 780
Chris@4 781 $indentHasSpaces = false !== strpos($indentation, " ");
Chris@4 782 $indentHasTabs = false !== strpos($indentation, "\t");
Chris@4 783 if ($indentHasSpaces && $indentHasTabs) {
Chris@4 784 $this->emitError(new Error(
Chris@4 785 'Invalid indentation - tabs and spaces cannot be mixed',
Chris@4 786 $endTokenAttributes
Chris@4 787 ));
Chris@4 788
Chris@4 789 // Proceed processing as if this doc string is not indented
Chris@4 790 $indentation = '';
Chris@4 791 }
Chris@4 792
Chris@4 793 $indentLen = \strlen($indentation);
Chris@4 794 $indentChar = $indentHasSpaces ? " " : "\t";
Chris@4 795
Chris@4 796 if (\is_string($contents)) {
Chris@4 797 if ($contents === '') {
Chris@4 798 return new String_('', $attributes);
Chris@4 799 }
Chris@4 800
Chris@4 801 $contents = $this->stripIndentation(
Chris@4 802 $contents, $indentLen, $indentChar, true, true, $attributes
Chris@4 803 );
Chris@4 804 $contents = preg_replace('~(\r\n|\n|\r)\z~', '', $contents);
Chris@4 805
Chris@4 806 if ($kind === String_::KIND_HEREDOC) {
Chris@4 807 $contents = String_::parseEscapeSequences($contents, null, $parseUnicodeEscape);
Chris@4 808 }
Chris@4 809
Chris@4 810 return new String_($contents, $attributes);
Chris@4 811 } else {
Chris@4 812 assert(count($contents) > 0);
Chris@4 813 if (!$contents[0] instanceof Node\Scalar\EncapsedStringPart) {
Chris@4 814 // If there is no leading encapsed string part, pretend there is an empty one
Chris@4 815 $this->stripIndentation(
Chris@4 816 '', $indentLen, $indentChar, true, false, $contents[0]->getAttributes()
Chris@4 817 );
Chris@4 818 }
Chris@4 819
Chris@4 820 $newContents = [];
Chris@4 821 foreach ($contents as $i => $part) {
Chris@4 822 if ($part instanceof Node\Scalar\EncapsedStringPart) {
Chris@4 823 $isLast = $i === \count($contents) - 1;
Chris@4 824 $part->value = $this->stripIndentation(
Chris@4 825 $part->value, $indentLen, $indentChar,
Chris@4 826 $i === 0, $isLast, $part->getAttributes()
Chris@4 827 );
Chris@4 828 $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape);
Chris@4 829 if ($isLast) {
Chris@4 830 $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value);
Chris@4 831 }
Chris@4 832 if ('' === $part->value) {
Chris@4 833 continue;
Chris@4 834 }
Chris@4 835 }
Chris@4 836 $newContents[] = $part;
Chris@4 837 }
Chris@4 838 return new Encapsed($newContents, $attributes);
Chris@4 839 }
Chris@4 840 }
Chris@4 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@0 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@0 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@0 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@0 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@0 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@0 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@0 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@0 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 }