| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace PhpParser; |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | use PhpParser\Node\Arg; |
| 11: | use PhpParser\Node\Expr; |
| 12: | use PhpParser\Node\Expr\Array_; |
| 13: | use PhpParser\Node\Expr\Cast\Double; |
| 14: | use PhpParser\Node\Identifier; |
| 15: | use PhpParser\Node\InterpolatedStringPart; |
| 16: | use PhpParser\Node\Name; |
| 17: | use PhpParser\Node\Param; |
| 18: | use PhpParser\Node\PropertyHook; |
| 19: | use PhpParser\Node\Scalar\InterpolatedString; |
| 20: | use PhpParser\Node\Scalar\Int_; |
| 21: | use PhpParser\Node\Scalar\String_; |
| 22: | use PhpParser\Node\Stmt; |
| 23: | use PhpParser\Node\Stmt\Class_; |
| 24: | use PhpParser\Node\Stmt\ClassConst; |
| 25: | use PhpParser\Node\Stmt\ClassMethod; |
| 26: | use PhpParser\Node\Stmt\Const_; |
| 27: | use PhpParser\Node\Stmt\Else_; |
| 28: | use PhpParser\Node\Stmt\ElseIf_; |
| 29: | use PhpParser\Node\Stmt\Enum_; |
| 30: | use PhpParser\Node\Stmt\Interface_; |
| 31: | use PhpParser\Node\Stmt\Namespace_; |
| 32: | use PhpParser\Node\Stmt\Nop; |
| 33: | use PhpParser\Node\Stmt\Property; |
| 34: | use PhpParser\Node\Stmt\TryCatch; |
| 35: | use PhpParser\Node\UseItem; |
| 36: | use PhpParser\Node\VarLikeIdentifier; |
| 37: | use PhpParser\NodeVisitor\CommentAnnotatingVisitor; |
| 38: | |
| 39: | abstract class ParserAbstract implements Parser { |
| 40: | private const SYMBOL_NONE = -1; |
| 41: | |
| 42: | |
| 43: | protected Lexer $lexer; |
| 44: | |
| 45: | protected PhpVersion $phpVersion; |
| 46: | |
| 47: | |
| 48: | |
| 49: | |
| 50: | |
| 51: | |
| 52: | protected int $tokenToSymbolMapSize; |
| 53: | |
| 54: | protected int $actionTableSize; |
| 55: | |
| 56: | protected int $gotoTableSize; |
| 57: | |
| 58: | |
| 59: | protected int $invalidSymbol; |
| 60: | |
| 61: | protected int $errorSymbol; |
| 62: | |
| 63: | protected int $defaultAction; |
| 64: | |
| 65: | protected int $unexpectedTokenRule; |
| 66: | |
| 67: | protected int $YY2TBLSTATE; |
| 68: | |
| 69: | protected int $numNonLeafStates; |
| 70: | |
| 71: | |
| 72: | protected array $phpTokenToSymbol; |
| 73: | |
| 74: | protected array $dropTokens; |
| 75: | |
| 76: | protected array $tokenToSymbol; |
| 77: | |
| 78: | protected array $symbolToName; |
| 79: | |
| 80: | protected array $productions; |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | protected array $actionBase; |
| 86: | |
| 87: | protected array $action; |
| 88: | |
| 89: | |
| 90: | protected array $actionCheck; |
| 91: | |
| 92: | protected array $actionDefault; |
| 93: | |
| 94: | protected array $reduceCallbacks; |
| 95: | |
| 96: | |
| 97: | |
| 98: | protected array $gotoBase; |
| 99: | |
| 100: | protected array $goto; |
| 101: | |
| 102: | |
| 103: | protected array $gotoCheck; |
| 104: | |
| 105: | protected array $gotoDefault; |
| 106: | |
| 107: | |
| 108: | |
| 109: | protected array $ruleToNonTerminal; |
| 110: | |
| 111: | |
| 112: | protected array $ruleToLength; |
| 113: | |
| 114: | |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | protected $semValue; |
| 120: | |
| 121: | protected array $semStack; |
| 122: | |
| 123: | protected array $tokenStartStack; |
| 124: | |
| 125: | protected array $tokenEndStack; |
| 126: | |
| 127: | |
| 128: | protected ErrorHandler $errorHandler; |
| 129: | |
| 130: | protected int $errorState; |
| 131: | |
| 132: | |
| 133: | protected ?\SplObjectStorage $createdArrays; |
| 134: | |
| 135: | |
| 136: | protected array $tokens; |
| 137: | |
| 138: | protected int $tokenPos; |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | abstract protected function initReduceCallbacks(): void; |
| 144: | |
| 145: | |
| 146: | |
| 147: | |
| 148: | |
| 149: | |
| 150: | |
| 151: | |
| 152: | |
| 153: | |
| 154: | |
| 155: | |
| 156: | |
| 157: | |
| 158: | public function __construct(Lexer $lexer, ?PhpVersion $phpVersion = null) { |
| 159: | $this->lexer = $lexer; |
| 160: | $this->phpVersion = $phpVersion ?? PhpVersion::getNewestSupported(); |
| 161: | |
| 162: | $this->initReduceCallbacks(); |
| 163: | $this->phpTokenToSymbol = $this->createTokenMap(); |
| 164: | $this->dropTokens = array_fill_keys( |
| 165: | [\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT, \T_BAD_CHARACTER], true |
| 166: | ); |
| 167: | } |
| 168: | |
| 169: | |
| 170: | |
| 171: | |
| 172: | |
| 173: | |
| 174: | |
| 175: | |
| 176: | |
| 177: | |
| 178: | |
| 179: | |
| 180: | |
| 181: | |
| 182: | public function parse(string $code, ?ErrorHandler $errorHandler = null): ?array { |
| 183: | $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing(); |
| 184: | $this->createdArrays = new \SplObjectStorage(); |
| 185: | |
| 186: | $this->tokens = $this->lexer->tokenize($code, $this->errorHandler); |
| 187: | $result = $this->doParse(); |
| 188: | |
| 189: | |
| 190: | |
| 191: | |
| 192: | foreach ($this->createdArrays as $node) { |
| 193: | foreach ($node->items as $item) { |
| 194: | if ($item->value instanceof Expr\Error) { |
| 195: | $this->errorHandler->handleError( |
| 196: | new Error('Cannot use empty array elements in arrays', $item->getAttributes())); |
| 197: | } |
| 198: | } |
| 199: | } |
| 200: | |
| 201: | |
| 202: | |
| 203: | $this->tokenStartStack = []; |
| 204: | $this->tokenEndStack = []; |
| 205: | $this->semStack = []; |
| 206: | $this->semValue = null; |
| 207: | $this->createdArrays = null; |
| 208: | |
| 209: | if ($result !== null) { |
| 210: | $traverser = new NodeTraverser(new CommentAnnotatingVisitor($this->tokens)); |
| 211: | $traverser->traverse($result); |
| 212: | } |
| 213: | |
| 214: | return $result; |
| 215: | } |
| 216: | |
| 217: | public function getTokens(): array { |
| 218: | return $this->tokens; |
| 219: | } |
| 220: | |
| 221: | |
| 222: | protected function doParse(): ?array { |
| 223: | |
| 224: | $symbol = self::SYMBOL_NONE; |
| 225: | $tokenValue = null; |
| 226: | $this->tokenPos = -1; |
| 227: | |
| 228: | |
| 229: | $this->tokenStartStack = []; |
| 230: | $this->tokenEndStack = [0]; |
| 231: | |
| 232: | |
| 233: | $state = 0; |
| 234: | $stateStack = [$state]; |
| 235: | |
| 236: | |
| 237: | $this->semStack = []; |
| 238: | |
| 239: | |
| 240: | $stackPos = 0; |
| 241: | |
| 242: | $this->errorState = 0; |
| 243: | |
| 244: | for (;;) { |
| 245: | |
| 246: | |
| 247: | if ($this->actionBase[$state] === 0) { |
| 248: | $rule = $this->actionDefault[$state]; |
| 249: | } else { |
| 250: | if ($symbol === self::SYMBOL_NONE) { |
| 251: | do { |
| 252: | $token = $this->tokens[++$this->tokenPos]; |
| 253: | $tokenId = $token->id; |
| 254: | } while (isset($this->dropTokens[$tokenId])); |
| 255: | |
| 256: | |
| 257: | $tokenValue = $token->text; |
| 258: | if (!isset($this->phpTokenToSymbol[$tokenId])) { |
| 259: | throw new \RangeException(sprintf( |
| 260: | 'The lexer returned an invalid token (id=%d, value=%s)', |
| 261: | $tokenId, $tokenValue |
| 262: | )); |
| 263: | } |
| 264: | $symbol = $this->phpTokenToSymbol[$tokenId]; |
| 265: | |
| 266: | |
| 267: | } |
| 268: | |
| 269: | $idx = $this->actionBase[$state] + $symbol; |
| 270: | if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol) |
| 271: | || ($state < $this->YY2TBLSTATE |
| 272: | && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0 |
| 273: | && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)) |
| 274: | && ($action = $this->action[$idx]) !== $this->defaultAction) { |
| 275: | |
| 276: | |
| 277: | |
| 278: | |
| 279: | |
| 280: | |
| 281: | |
| 282: | if ($action > 0) { |
| 283: | |
| 284: | |
| 285: | |
| 286: | ++$stackPos; |
| 287: | $stateStack[$stackPos] = $state = $action; |
| 288: | $this->semStack[$stackPos] = $tokenValue; |
| 289: | $this->tokenStartStack[$stackPos] = $this->tokenPos; |
| 290: | $this->tokenEndStack[$stackPos] = $this->tokenPos; |
| 291: | $symbol = self::SYMBOL_NONE; |
| 292: | |
| 293: | if ($this->errorState) { |
| 294: | --$this->errorState; |
| 295: | } |
| 296: | |
| 297: | if ($action < $this->numNonLeafStates) { |
| 298: | continue; |
| 299: | } |
| 300: | |
| 301: | |
| 302: | $rule = $action - $this->numNonLeafStates; |
| 303: | } else { |
| 304: | $rule = -$action; |
| 305: | } |
| 306: | } else { |
| 307: | $rule = $this->actionDefault[$state]; |
| 308: | } |
| 309: | } |
| 310: | |
| 311: | for (;;) { |
| 312: | if ($rule === 0) { |
| 313: | |
| 314: | |
| 315: | return $this->semValue; |
| 316: | } |
| 317: | if ($rule !== $this->unexpectedTokenRule) { |
| 318: | |
| 319: | |
| 320: | |
| 321: | $ruleLength = $this->ruleToLength[$rule]; |
| 322: | try { |
| 323: | $callback = $this->reduceCallbacks[$rule]; |
| 324: | if ($callback !== null) { |
| 325: | $callback($this, $stackPos); |
| 326: | } elseif ($ruleLength > 0) { |
| 327: | $this->semValue = $this->semStack[$stackPos - $ruleLength + 1]; |
| 328: | } |
| 329: | } catch (Error $e) { |
| 330: | if (-1 === $e->getStartLine()) { |
| 331: | $e->setStartLine($this->tokens[$this->tokenPos]->line); |
| 332: | } |
| 333: | |
| 334: | $this->emitError($e); |
| 335: | |
| 336: | return null; |
| 337: | } |
| 338: | |
| 339: | |
| 340: | $lastTokenEnd = $this->tokenEndStack[$stackPos]; |
| 341: | $stackPos -= $ruleLength; |
| 342: | $nonTerminal = $this->ruleToNonTerminal[$rule]; |
| 343: | $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos]; |
| 344: | if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) { |
| 345: | $state = $this->goto[$idx]; |
| 346: | } else { |
| 347: | $state = $this->gotoDefault[$nonTerminal]; |
| 348: | } |
| 349: | |
| 350: | ++$stackPos; |
| 351: | $stateStack[$stackPos] = $state; |
| 352: | $this->semStack[$stackPos] = $this->semValue; |
| 353: | $this->tokenEndStack[$stackPos] = $lastTokenEnd; |
| 354: | if ($ruleLength === 0) { |
| 355: | |
| 356: | $this->tokenStartStack[$stackPos] = $this->tokenPos; |
| 357: | } |
| 358: | } else { |
| 359: | |
| 360: | switch ($this->errorState) { |
| 361: | case 0: |
| 362: | $msg = $this->getErrorMessage($symbol, $state); |
| 363: | $this->emitError(new Error($msg, $this->getAttributesForToken($this->tokenPos))); |
| 364: | |
| 365: | |
| 366: | case 1: |
| 367: | case 2: |
| 368: | $this->errorState = 3; |
| 369: | |
| 370: | |
| 371: | while (!( |
| 372: | (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0 |
| 373: | && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol) |
| 374: | || ($state < $this->YY2TBLSTATE |
| 375: | && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0 |
| 376: | && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol) |
| 377: | ) || ($action = $this->action[$idx]) === $this->defaultAction) { |
| 378: | if ($stackPos <= 0) { |
| 379: | |
| 380: | return null; |
| 381: | } |
| 382: | $state = $stateStack[--$stackPos]; |
| 383: | |
| 384: | } |
| 385: | |
| 386: | |
| 387: | ++$stackPos; |
| 388: | $stateStack[$stackPos] = $state = $action; |
| 389: | |
| 390: | |
| 391: | |
| 392: | $this->tokenStartStack[$stackPos] = $this->tokenPos; |
| 393: | $this->tokenEndStack[$stackPos] = $this->tokenEndStack[$stackPos - 1]; |
| 394: | break; |
| 395: | |
| 396: | case 3: |
| 397: | if ($symbol === 0) { |
| 398: | |
| 399: | return null; |
| 400: | } |
| 401: | |
| 402: | |
| 403: | $symbol = self::SYMBOL_NONE; |
| 404: | break 2; |
| 405: | } |
| 406: | } |
| 407: | |
| 408: | if ($state < $this->numNonLeafStates) { |
| 409: | break; |
| 410: | } |
| 411: | |
| 412: | |
| 413: | $rule = $state - $this->numNonLeafStates; |
| 414: | } |
| 415: | } |
| 416: | } |
| 417: | |
| 418: | protected function emitError(Error $error): void { |
| 419: | $this->errorHandler->handleError($error); |
| 420: | } |
| 421: | |
| 422: | |
| 423: | |
| 424: | |
| 425: | |
| 426: | |
| 427: | |
| 428: | |
| 429: | |
| 430: | protected function getErrorMessage(int $symbol, int $state): string { |
| 431: | $expectedString = ''; |
| 432: | if ($expected = $this->getExpectedTokens($state)) { |
| 433: | $expectedString = ', expecting ' . implode(' or ', $expected); |
| 434: | } |
| 435: | |
| 436: | return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString; |
| 437: | } |
| 438: | |
| 439: | |
| 440: | |
| 441: | |
| 442: | |
| 443: | |
| 444: | |
| 445: | |
| 446: | protected function getExpectedTokens(int $state): array { |
| 447: | $expected = []; |
| 448: | |
| 449: | $base = $this->actionBase[$state]; |
| 450: | foreach ($this->symbolToName as $symbol => $name) { |
| 451: | $idx = $base + $symbol; |
| 452: | if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol |
| 453: | || $state < $this->YY2TBLSTATE |
| 454: | && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0 |
| 455: | && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol |
| 456: | ) { |
| 457: | if ($this->action[$idx] !== $this->unexpectedTokenRule |
| 458: | && $this->action[$idx] !== $this->defaultAction |
| 459: | && $symbol !== $this->errorSymbol |
| 460: | ) { |
| 461: | if (count($expected) === 4) { |
| 462: | |
| 463: | return []; |
| 464: | } |
| 465: | |
| 466: | $expected[] = $name; |
| 467: | } |
| 468: | } |
| 469: | } |
| 470: | |
| 471: | return $expected; |
| 472: | } |
| 473: | |
| 474: | |
| 475: | |
| 476: | |
| 477: | |
| 478: | |
| 479: | |
| 480: | |
| 481: | protected function getAttributes(int $tokenStartPos, int $tokenEndPos): array { |
| 482: | $startToken = $this->tokens[$tokenStartPos]; |
| 483: | $afterEndToken = $this->tokens[$tokenEndPos + 1]; |
| 484: | return [ |
| 485: | 'startLine' => $startToken->line, |
| 486: | 'startTokenPos' => $tokenStartPos, |
| 487: | 'startFilePos' => $startToken->pos, |
| 488: | 'endLine' => $afterEndToken->line, |
| 489: | 'endTokenPos' => $tokenEndPos, |
| 490: | 'endFilePos' => $afterEndToken->pos - 1, |
| 491: | ]; |
| 492: | } |
| 493: | |
| 494: | |
| 495: | |
| 496: | |
| 497: | |
| 498: | |
| 499: | protected function getAttributesForToken(int $tokenPos): array { |
| 500: | if ($tokenPos < \count($this->tokens) - 1) { |
| 501: | return $this->getAttributes($tokenPos, $tokenPos); |
| 502: | } |
| 503: | |
| 504: | |
| 505: | $token = $this->tokens[$tokenPos]; |
| 506: | return [ |
| 507: | 'startLine' => $token->line, |
| 508: | 'startTokenPos' => $tokenPos, |
| 509: | 'startFilePos' => $token->pos, |
| 510: | 'endLine' => $token->line, |
| 511: | 'endTokenPos' => $tokenPos, |
| 512: | 'endFilePos' => $token->pos, |
| 513: | ]; |
| 514: | } |
| 515: | |
| 516: | |
| 517: | |
| 518: | |
| 519: | |
| 520: | |
| 521: | |
| 522: | |
| 523: | |
| 524: | |
| 525: | |
| 526: | |
| 527: | |
| 528: | |
| 529: | |
| 530: | |
| 531: | |
| 532: | |
| 533: | |
| 534: | |
| 535: | |
| 536: | |
| 537: | |
| 538: | |
| 539: | |
| 540: | |
| 541: | |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | |
| 550: | |
| 551: | |
| 552: | |
| 553: | |
| 554: | |
| 555: | |
| 556: | |
| 557: | |
| 558: | |
| 559: | |
| 560: | |
| 561: | protected function handleNamespaces(array $stmts): array { |
| 562: | $hasErrored = false; |
| 563: | $style = $this->getNamespacingStyle($stmts); |
| 564: | if (null === $style) { |
| 565: | |
| 566: | return $stmts; |
| 567: | } |
| 568: | if ('brace' === $style) { |
| 569: | |
| 570: | $afterFirstNamespace = false; |
| 571: | foreach ($stmts as $stmt) { |
| 572: | if ($stmt instanceof Node\Stmt\Namespace_) { |
| 573: | $afterFirstNamespace = true; |
| 574: | } elseif (!$stmt instanceof Node\Stmt\HaltCompiler |
| 575: | && !$stmt instanceof Node\Stmt\Nop |
| 576: | && $afterFirstNamespace && !$hasErrored) { |
| 577: | $this->emitError(new Error( |
| 578: | 'No code may exist outside of namespace {}', $stmt->getAttributes())); |
| 579: | $hasErrored = true; |
| 580: | } |
| 581: | } |
| 582: | return $stmts; |
| 583: | } else { |
| 584: | |
| 585: | $resultStmts = []; |
| 586: | $targetStmts = &$resultStmts; |
| 587: | $lastNs = null; |
| 588: | foreach ($stmts as $stmt) { |
| 589: | if ($stmt instanceof Node\Stmt\Namespace_) { |
| 590: | if ($lastNs !== null) { |
| 591: | $this->fixupNamespaceAttributes($lastNs); |
| 592: | } |
| 593: | if ($stmt->stmts === null) { |
| 594: | $stmt->stmts = []; |
| 595: | $targetStmts = &$stmt->stmts; |
| 596: | $resultStmts[] = $stmt; |
| 597: | } else { |
| 598: | |
| 599: | $resultStmts[] = $stmt; |
| 600: | $targetStmts = &$resultStmts; |
| 601: | } |
| 602: | $lastNs = $stmt; |
| 603: | } elseif ($stmt instanceof Node\Stmt\HaltCompiler) { |
| 604: | |
| 605: | $resultStmts[] = $stmt; |
| 606: | } else { |
| 607: | $targetStmts[] = $stmt; |
| 608: | } |
| 609: | } |
| 610: | if ($lastNs !== null) { |
| 611: | $this->fixupNamespaceAttributes($lastNs); |
| 612: | } |
| 613: | return $resultStmts; |
| 614: | } |
| 615: | } |
| 616: | |
| 617: | private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt): void { |
| 618: | |
| 619: | |
| 620: | if (empty($stmt->stmts)) { |
| 621: | return; |
| 622: | } |
| 623: | |
| 624: | |
| 625: | |
| 626: | $endAttributes = ['endLine', 'endFilePos', 'endTokenPos']; |
| 627: | $lastStmt = $stmt->stmts[count($stmt->stmts) - 1]; |
| 628: | foreach ($endAttributes as $endAttribute) { |
| 629: | if ($lastStmt->hasAttribute($endAttribute)) { |
| 630: | $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute)); |
| 631: | } |
| 632: | } |
| 633: | } |
| 634: | |
| 635: | |
| 636: | private function getNamespaceErrorAttributes(Namespace_ $node): array { |
| 637: | $attrs = $node->getAttributes(); |
| 638: | |
| 639: | if (isset($attrs['startLine'])) { |
| 640: | $attrs['endLine'] = $attrs['startLine']; |
| 641: | } |
| 642: | if (isset($attrs['startTokenPos'])) { |
| 643: | $attrs['endTokenPos'] = $attrs['startTokenPos']; |
| 644: | } |
| 645: | if (isset($attrs['startFilePos'])) { |
| 646: | $attrs['endFilePos'] = $attrs['startFilePos'] + \strlen('namespace') - 1; |
| 647: | } |
| 648: | return $attrs; |
| 649: | } |
| 650: | |
| 651: | |
| 652: | |
| 653: | |
| 654: | |
| 655: | |
| 656: | |
| 657: | |
| 658: | private function getNamespacingStyle(array $stmts): ?string { |
| 659: | $style = null; |
| 660: | $hasNotAllowedStmts = false; |
| 661: | foreach ($stmts as $i => $stmt) { |
| 662: | if ($stmt instanceof Node\Stmt\Namespace_) { |
| 663: | $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace'; |
| 664: | if (null === $style) { |
| 665: | $style = $currentStyle; |
| 666: | if ($hasNotAllowedStmts) { |
| 667: | $this->emitError(new Error( |
| 668: | 'Namespace declaration statement has to be the very first statement in the script', |
| 669: | $this->getNamespaceErrorAttributes($stmt) |
| 670: | )); |
| 671: | } |
| 672: | } elseif ($style !== $currentStyle) { |
| 673: | $this->emitError(new Error( |
| 674: | 'Cannot mix bracketed namespace declarations with unbracketed namespace declarations', |
| 675: | $this->getNamespaceErrorAttributes($stmt) |
| 676: | )); |
| 677: | |
| 678: | return 'semicolon'; |
| 679: | } |
| 680: | continue; |
| 681: | } |
| 682: | |
| 683: | |
| 684: | if ($stmt instanceof Node\Stmt\Declare_ |
| 685: | || $stmt instanceof Node\Stmt\HaltCompiler |
| 686: | || $stmt instanceof Node\Stmt\Nop) { |
| 687: | continue; |
| 688: | } |
| 689: | |
| 690: | |
| 691: | if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) { |
| 692: | continue; |
| 693: | } |
| 694: | |
| 695: | |
| 696: | $hasNotAllowedStmts = true; |
| 697: | } |
| 698: | return $style; |
| 699: | } |
| 700: | |
| 701: | |
| 702: | protected function handleBuiltinTypes(Name $name) { |
| 703: | if (!$name->isUnqualified()) { |
| 704: | return $name; |
| 705: | } |
| 706: | |
| 707: | $lowerName = $name->toLowerString(); |
| 708: | if (!$this->phpVersion->supportsBuiltinType($lowerName)) { |
| 709: | return $name; |
| 710: | } |
| 711: | |
| 712: | return new Node\Identifier($lowerName, $name->getAttributes()); |
| 713: | } |
| 714: | |
| 715: | |
| 716: | |
| 717: | |
| 718: | |
| 719: | |
| 720: | |
| 721: | |
| 722: | protected function getAttributesAt(int $stackPos): array { |
| 723: | return $this->getAttributes($this->tokenStartStack[$stackPos], $this->tokenEndStack[$stackPos]); |
| 724: | } |
| 725: | |
| 726: | protected function getFloatCastKind(string $cast): int { |
| 727: | $cast = strtolower($cast); |
| 728: | if (strpos($cast, 'float') !== false) { |
| 729: | return Double::KIND_FLOAT; |
| 730: | } |
| 731: | |
| 732: | if (strpos($cast, 'real') !== false) { |
| 733: | return Double::KIND_REAL; |
| 734: | } |
| 735: | |
| 736: | return Double::KIND_DOUBLE; |
| 737: | } |
| 738: | |
| 739: | protected function getIntCastKind(string $cast): int { |
| 740: | $cast = strtolower($cast); |
| 741: | if (strpos($cast, 'integer') !== false) { |
| 742: | return Expr\Cast\Int_::KIND_INTEGER; |
| 743: | } |
| 744: | |
| 745: | return Expr\Cast\Int_::KIND_INT; |
| 746: | } |
| 747: | |
| 748: | protected function getBoolCastKind(string $cast): int { |
| 749: | $cast = strtolower($cast); |
| 750: | if (strpos($cast, 'boolean') !== false) { |
| 751: | return Expr\Cast\Bool_::KIND_BOOLEAN; |
| 752: | } |
| 753: | |
| 754: | return Expr\Cast\Bool_::KIND_BOOL; |
| 755: | } |
| 756: | |
| 757: | protected function getStringCastKind(string $cast): int { |
| 758: | $cast = strtolower($cast); |
| 759: | if (strpos($cast, 'binary') !== false) { |
| 760: | return Expr\Cast\String_::KIND_BINARY; |
| 761: | } |
| 762: | |
| 763: | return Expr\Cast\String_::KIND_STRING; |
| 764: | } |
| 765: | |
| 766: | |
| 767: | protected function parseLNumber(string $str, array $attributes, bool $allowInvalidOctal = false): Int_ { |
| 768: | try { |
| 769: | return Int_::fromString($str, $attributes, $allowInvalidOctal); |
| 770: | } catch (Error $error) { |
| 771: | $this->emitError($error); |
| 772: | |
| 773: | return new Int_(0, $attributes); |
| 774: | } |
| 775: | } |
| 776: | |
| 777: | |
| 778: | |
| 779: | |
| 780: | |
| 781: | |
| 782: | |
| 783: | |
| 784: | |
| 785: | protected function parseNumString(string $str, array $attributes) { |
| 786: | if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) { |
| 787: | return new String_($str, $attributes); |
| 788: | } |
| 789: | |
| 790: | $num = +$str; |
| 791: | if (!is_int($num)) { |
| 792: | return new String_($str, $attributes); |
| 793: | } |
| 794: | |
| 795: | return new Int_($num, $attributes); |
| 796: | } |
| 797: | |
| 798: | |
| 799: | protected function stripIndentation( |
| 800: | string $string, int $indentLen, string $indentChar, |
| 801: | bool $newlineAtStart, bool $newlineAtEnd, array $attributes |
| 802: | ): string { |
| 803: | if ($indentLen === 0) { |
| 804: | return $string; |
| 805: | } |
| 806: | |
| 807: | $start = $newlineAtStart ? '(?:(?<=\n)|\A)' : '(?<=\n)'; |
| 808: | $end = $newlineAtEnd ? '(?:(?=[\r\n])|\z)' : '(?=[\r\n])'; |
| 809: | $regex = '/' . $start . '([ \t]*)(' . $end . ')?/'; |
| 810: | return preg_replace_callback( |
| 811: | $regex, |
| 812: | function ($matches) use ($indentLen, $indentChar, $attributes) { |
| 813: | $prefix = substr($matches[1], 0, $indentLen); |
| 814: | if (false !== strpos($prefix, $indentChar === " " ? "\t" : " ")) { |
| 815: | $this->emitError(new Error( |
| 816: | 'Invalid indentation - tabs and spaces cannot be mixed', $attributes |
| 817: | )); |
| 818: | } elseif (strlen($prefix) < $indentLen && !isset($matches[2])) { |
| 819: | $this->emitError(new Error( |
| 820: | 'Invalid body indentation level ' . |
| 821: | '(expecting an indentation level of at least ' . $indentLen . ')', |
| 822: | $attributes |
| 823: | )); |
| 824: | } |
| 825: | return substr($matches[0], strlen($prefix)); |
| 826: | }, |
| 827: | $string |
| 828: | ); |
| 829: | } |
| 830: | |
| 831: | |
| 832: | |
| 833: | |
| 834: | |
| 835: | |
| 836: | protected function parseDocString( |
| 837: | string $startToken, $contents, string $endToken, |
| 838: | array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape |
| 839: | ): Expr { |
| 840: | $kind = strpos($startToken, "'") === false |
| 841: | ? String_::KIND_HEREDOC : String_::KIND_NOWDOC; |
| 842: | |
| 843: | $regex = '/\A[bB]?<<<[ \t]*[\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\'"]?(?:\r\n|\n|\r)\z/'; |
| 844: | $result = preg_match($regex, $startToken, $matches); |
| 845: | assert($result === 1); |
| 846: | $label = $matches[1]; |
| 847: | |
| 848: | $result = preg_match('/\A[ \t]*/', $endToken, $matches); |
| 849: | assert($result === 1); |
| 850: | $indentation = $matches[0]; |
| 851: | |
| 852: | $attributes['kind'] = $kind; |
| 853: | $attributes['docLabel'] = $label; |
| 854: | $attributes['docIndentation'] = $indentation; |
| 855: | |
| 856: | $indentHasSpaces = false !== strpos($indentation, " "); |
| 857: | $indentHasTabs = false !== strpos($indentation, "\t"); |
| 858: | if ($indentHasSpaces && $indentHasTabs) { |
| 859: | $this->emitError(new Error( |
| 860: | 'Invalid indentation - tabs and spaces cannot be mixed', |
| 861: | $endTokenAttributes |
| 862: | )); |
| 863: | |
| 864: | |
| 865: | $indentation = ''; |
| 866: | } |
| 867: | |
| 868: | $indentLen = \strlen($indentation); |
| 869: | $indentChar = $indentHasSpaces ? " " : "\t"; |
| 870: | |
| 871: | if (\is_string($contents)) { |
| 872: | if ($contents === '') { |
| 873: | $attributes['rawValue'] = $contents; |
| 874: | return new String_('', $attributes); |
| 875: | } |
| 876: | |
| 877: | $contents = $this->stripIndentation( |
| 878: | $contents, $indentLen, $indentChar, true, true, $attributes |
| 879: | ); |
| 880: | $contents = preg_replace('~(\r\n|\n|\r)\z~', '', $contents); |
| 881: | $attributes['rawValue'] = $contents; |
| 882: | |
| 883: | if ($kind === String_::KIND_HEREDOC) { |
| 884: | $contents = String_::parseEscapeSequences($contents, null, $parseUnicodeEscape); |
| 885: | } |
| 886: | |
| 887: | return new String_($contents, $attributes); |
| 888: | } else { |
| 889: | assert(count($contents) > 0); |
| 890: | if (!$contents[0] instanceof Node\InterpolatedStringPart) { |
| 891: | |
| 892: | $this->stripIndentation( |
| 893: | '', $indentLen, $indentChar, true, false, $contents[0]->getAttributes() |
| 894: | ); |
| 895: | } |
| 896: | |
| 897: | $newContents = []; |
| 898: | foreach ($contents as $i => $part) { |
| 899: | if ($part instanceof Node\InterpolatedStringPart) { |
| 900: | $isLast = $i === \count($contents) - 1; |
| 901: | $part->value = $this->stripIndentation( |
| 902: | $part->value, $indentLen, $indentChar, |
| 903: | $i === 0, $isLast, $part->getAttributes() |
| 904: | ); |
| 905: | if ($isLast) { |
| 906: | $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value); |
| 907: | } |
| 908: | $part->setAttribute('rawValue', $part->value); |
| 909: | $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape); |
| 910: | if ('' === $part->value) { |
| 911: | continue; |
| 912: | } |
| 913: | } |
| 914: | $newContents[] = $part; |
| 915: | } |
| 916: | return new InterpolatedString($newContents, $attributes); |
| 917: | } |
| 918: | } |
| 919: | |
| 920: | protected function createCommentFromToken(Token $token, int $tokenPos): Comment { |
| 921: | assert($token->id === \T_COMMENT || $token->id == \T_DOC_COMMENT); |
| 922: | return \T_DOC_COMMENT === $token->id |
| 923: | ? new Comment\Doc($token->text, $token->line, $token->pos, $tokenPos, |
| 924: | $token->getEndLine(), $token->getEndPos() - 1, $tokenPos) |
| 925: | : new Comment($token->text, $token->line, $token->pos, $tokenPos, |
| 926: | $token->getEndLine(), $token->getEndPos() - 1, $tokenPos); |
| 927: | } |
| 928: | |
| 929: | |
| 930: | |
| 931: | |
| 932: | protected function getCommentBeforeToken(int $tokenPos): ?Comment { |
| 933: | while (--$tokenPos >= 0) { |
| 934: | $token = $this->tokens[$tokenPos]; |
| 935: | if (!isset($this->dropTokens[$token->id])) { |
| 936: | break; |
| 937: | } |
| 938: | |
| 939: | if ($token->id === \T_COMMENT || $token->id === \T_DOC_COMMENT) { |
| 940: | return $this->createCommentFromToken($token, $tokenPos); |
| 941: | } |
| 942: | } |
| 943: | return null; |
| 944: | } |
| 945: | |
| 946: | |
| 947: | |
| 948: | |
| 949: | protected function maybeCreateZeroLengthNop(int $tokenPos): ?Nop { |
| 950: | $comment = $this->getCommentBeforeToken($tokenPos); |
| 951: | if ($comment === null) { |
| 952: | return null; |
| 953: | } |
| 954: | |
| 955: | $commentEndLine = $comment->getEndLine(); |
| 956: | $commentEndFilePos = $comment->getEndFilePos(); |
| 957: | $commentEndTokenPos = $comment->getEndTokenPos(); |
| 958: | $attributes = [ |
| 959: | 'startLine' => $commentEndLine, |
| 960: | 'endLine' => $commentEndLine, |
| 961: | 'startFilePos' => $commentEndFilePos + 1, |
| 962: | 'endFilePos' => $commentEndFilePos, |
| 963: | 'startTokenPos' => $commentEndTokenPos + 1, |
| 964: | 'endTokenPos' => $commentEndTokenPos, |
| 965: | ]; |
| 966: | return new Nop($attributes); |
| 967: | } |
| 968: | |
| 969: | protected function maybeCreateNop(int $tokenStartPos, int $tokenEndPos): ?Nop { |
| 970: | if ($this->getCommentBeforeToken($tokenStartPos) === null) { |
| 971: | return null; |
| 972: | } |
| 973: | return new Nop($this->getAttributes($tokenStartPos, $tokenEndPos)); |
| 974: | } |
| 975: | |
| 976: | protected function handleHaltCompiler(): string { |
| 977: | |
| 978: | $nextToken = $this->tokens[$this->tokenPos + 1]; |
| 979: | $this->tokenPos = \count($this->tokens) - 2; |
| 980: | |
| 981: | |
| 982: | return $nextToken->id === \T_INLINE_HTML ? $nextToken->text : ''; |
| 983: | } |
| 984: | |
| 985: | protected function inlineHtmlHasLeadingNewline(int $stackPos): bool { |
| 986: | $tokenPos = $this->tokenStartStack[$stackPos]; |
| 987: | $token = $this->tokens[$tokenPos]; |
| 988: | assert($token->id == \T_INLINE_HTML); |
| 989: | if ($tokenPos > 0) { |
| 990: | $prevToken = $this->tokens[$tokenPos - 1]; |
| 991: | assert($prevToken->id == \T_CLOSE_TAG); |
| 992: | return false !== strpos($prevToken->text, "\n") |
| 993: | || false !== strpos($prevToken->text, "\r"); |
| 994: | } |
| 995: | return true; |
| 996: | } |
| 997: | |
| 998: | |
| 999: | |
| 1000: | |
| 1001: | protected function createEmptyElemAttributes(int $tokenPos): array { |
| 1002: | return $this->getAttributesForToken($tokenPos); |
| 1003: | } |
| 1004: | |
| 1005: | protected function fixupArrayDestructuring(Array_ $node): Expr\List_ { |
| 1006: | $this->createdArrays->offsetUnset($node); |
| 1007: | return new Expr\List_(array_map(function (Node\ArrayItem $item) { |
| 1008: | if ($item->value instanceof Expr\Error) { |
| 1009: | |
| 1010: | return null; |
| 1011: | } |
| 1012: | if ($item->value instanceof Array_) { |
| 1013: | return new Node\ArrayItem( |
| 1014: | $this->fixupArrayDestructuring($item->value), |
| 1015: | $item->key, $item->byRef, $item->getAttributes()); |
| 1016: | } |
| 1017: | return $item; |
| 1018: | }, $node->items), ['kind' => Expr\List_::KIND_ARRAY] + $node->getAttributes()); |
| 1019: | } |
| 1020: | |
| 1021: | protected function postprocessList(Expr\List_ $node): void { |
| 1022: | foreach ($node->items as $i => $item) { |
| 1023: | if ($item->value instanceof Expr\Error) { |
| 1024: | |
| 1025: | $node->items[$i] = null; |
| 1026: | } |
| 1027: | } |
| 1028: | } |
| 1029: | |
| 1030: | |
| 1031: | protected function fixupAlternativeElse($node): void { |
| 1032: | |
| 1033: | $numStmts = \count($node->stmts); |
| 1034: | if ($numStmts !== 0 && $node->stmts[$numStmts - 1] instanceof Nop) { |
| 1035: | $nopAttrs = $node->stmts[$numStmts - 1]->getAttributes(); |
| 1036: | if (isset($nopAttrs['endLine'])) { |
| 1037: | $node->setAttribute('endLine', $nopAttrs['endLine']); |
| 1038: | } |
| 1039: | if (isset($nopAttrs['endFilePos'])) { |
| 1040: | $node->setAttribute('endFilePos', $nopAttrs['endFilePos']); |
| 1041: | } |
| 1042: | if (isset($nopAttrs['endTokenPos'])) { |
| 1043: | $node->setAttribute('endTokenPos', $nopAttrs['endTokenPos']); |
| 1044: | } |
| 1045: | } |
| 1046: | } |
| 1047: | |
| 1048: | protected function checkClassModifier(int $a, int $b, int $modifierPos): void { |
| 1049: | try { |
| 1050: | Modifiers::verifyClassModifier($a, $b); |
| 1051: | } catch (Error $error) { |
| 1052: | $error->setAttributes($this->getAttributesAt($modifierPos)); |
| 1053: | $this->emitError($error); |
| 1054: | } |
| 1055: | } |
| 1056: | |
| 1057: | protected function checkModifier(int $a, int $b, int $modifierPos): void { |
| 1058: | |
| 1059: | try { |
| 1060: | Modifiers::verifyModifier($a, $b); |
| 1061: | } catch (Error $error) { |
| 1062: | $error->setAttributes($this->getAttributesAt($modifierPos)); |
| 1063: | $this->emitError($error); |
| 1064: | } |
| 1065: | } |
| 1066: | |
| 1067: | protected function checkParam(Param $node): void { |
| 1068: | if ($node->variadic && null !== $node->default) { |
| 1069: | $this->emitError(new Error( |
| 1070: | 'Variadic parameter cannot have a default value', |
| 1071: | $node->default->getAttributes() |
| 1072: | )); |
| 1073: | } |
| 1074: | } |
| 1075: | |
| 1076: | protected function checkTryCatch(TryCatch $node): void { |
| 1077: | if (empty($node->catches) && null === $node->finally) { |
| 1078: | $this->emitError(new Error( |
| 1079: | 'Cannot use try without catch or finally', $node->getAttributes() |
| 1080: | )); |
| 1081: | } |
| 1082: | } |
| 1083: | |
| 1084: | protected function checkNamespace(Namespace_ $node): void { |
| 1085: | if (null !== $node->stmts) { |
| 1086: | foreach ($node->stmts as $stmt) { |
| 1087: | if ($stmt instanceof Namespace_) { |
| 1088: | $this->emitError(new Error( |
| 1089: | 'Namespace declarations cannot be nested', $stmt->getAttributes() |
| 1090: | )); |
| 1091: | } |
| 1092: | } |
| 1093: | } |
| 1094: | } |
| 1095: | |
| 1096: | private function checkClassName(?Identifier $name, int $namePos): void { |
| 1097: | if (null !== $name && $name->isSpecialClassName()) { |
| 1098: | $this->emitError(new Error( |
| 1099: | sprintf('Cannot use \'%s\' as class name as it is reserved', $name), |
| 1100: | $this->getAttributesAt($namePos) |
| 1101: | )); |
| 1102: | } |
| 1103: | } |
| 1104: | |
| 1105: | |
| 1106: | private function checkImplementedInterfaces(array $interfaces): void { |
| 1107: | foreach ($interfaces as $interface) { |
| 1108: | if ($interface->isSpecialClassName()) { |
| 1109: | $this->emitError(new Error( |
| 1110: | sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), |
| 1111: | $interface->getAttributes() |
| 1112: | )); |
| 1113: | } |
| 1114: | } |
| 1115: | } |
| 1116: | |
| 1117: | protected function checkClass(Class_ $node, int $namePos): void { |
| 1118: | $this->checkClassName($node->name, $namePos); |
| 1119: | |
| 1120: | if ($node->extends && $node->extends->isSpecialClassName()) { |
| 1121: | $this->emitError(new Error( |
| 1122: | sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends), |
| 1123: | $node->extends->getAttributes() |
| 1124: | )); |
| 1125: | } |
| 1126: | |
| 1127: | $this->checkImplementedInterfaces($node->implements); |
| 1128: | } |
| 1129: | |
| 1130: | protected function checkInterface(Interface_ $node, int $namePos): void { |
| 1131: | $this->checkClassName($node->name, $namePos); |
| 1132: | $this->checkImplementedInterfaces($node->extends); |
| 1133: | } |
| 1134: | |
| 1135: | protected function checkEnum(Enum_ $node, int $namePos): void { |
| 1136: | $this->checkClassName($node->name, $namePos); |
| 1137: | $this->checkImplementedInterfaces($node->implements); |
| 1138: | } |
| 1139: | |
| 1140: | protected function checkClassMethod(ClassMethod $node, int $modifierPos): void { |
| 1141: | if ($node->flags & Modifiers::STATIC) { |
| 1142: | switch ($node->name->toLowerString()) { |
| 1143: | case '__construct': |
| 1144: | $this->emitError(new Error( |
| 1145: | sprintf('Constructor %s() cannot be static', $node->name), |
| 1146: | $this->getAttributesAt($modifierPos))); |
| 1147: | break; |
| 1148: | case '__destruct': |
| 1149: | $this->emitError(new Error( |
| 1150: | sprintf('Destructor %s() cannot be static', $node->name), |
| 1151: | $this->getAttributesAt($modifierPos))); |
| 1152: | break; |
| 1153: | case '__clone': |
| 1154: | $this->emitError(new Error( |
| 1155: | sprintf('Clone method %s() cannot be static', $node->name), |
| 1156: | $this->getAttributesAt($modifierPos))); |
| 1157: | break; |
| 1158: | } |
| 1159: | } |
| 1160: | |
| 1161: | if ($node->flags & Modifiers::READONLY) { |
| 1162: | $this->emitError(new Error( |
| 1163: | sprintf('Method %s() cannot be readonly', $node->name), |
| 1164: | $this->getAttributesAt($modifierPos))); |
| 1165: | } |
| 1166: | } |
| 1167: | |
| 1168: | protected function checkClassConst(ClassConst $node, int $modifierPos): void { |
| 1169: | foreach ([Modifiers::STATIC, Modifiers::ABSTRACT, Modifiers::READONLY] as $modifier) { |
| 1170: | if ($node->flags & $modifier) { |
| 1171: | $this->emitError(new Error( |
| 1172: | "Cannot use '" . Modifiers::toString($modifier) . "' as constant modifier", |
| 1173: | $this->getAttributesAt($modifierPos))); |
| 1174: | } |
| 1175: | } |
| 1176: | } |
| 1177: | |
| 1178: | protected function checkUseUse(UseItem $node, int $namePos): void { |
| 1179: | if ($node->alias && $node->alias->isSpecialClassName()) { |
| 1180: | $this->emitError(new Error( |
| 1181: | sprintf( |
| 1182: | 'Cannot use %s as %s because \'%2$s\' is a special class name', |
| 1183: | $node->name, $node->alias |
| 1184: | ), |
| 1185: | $this->getAttributesAt($namePos) |
| 1186: | )); |
| 1187: | } |
| 1188: | } |
| 1189: | |
| 1190: | protected function checkPropertyHooksForMultiProperty(Property $property, int $hookPos): void { |
| 1191: | if (count($property->props) > 1) { |
| 1192: | $this->emitError(new Error( |
| 1193: | 'Cannot use hooks when declaring multiple properties', $this->getAttributesAt($hookPos))); |
| 1194: | } |
| 1195: | } |
| 1196: | |
| 1197: | |
| 1198: | protected function checkEmptyPropertyHookList(array $hooks, int $hookPos): void { |
| 1199: | if (empty($hooks)) { |
| 1200: | $this->emitError(new Error( |
| 1201: | 'Property hook list cannot be empty', $this->getAttributesAt($hookPos))); |
| 1202: | } |
| 1203: | } |
| 1204: | |
| 1205: | protected function checkPropertyHook(PropertyHook $hook, ?int $paramListPos): void { |
| 1206: | $name = $hook->name->toLowerString(); |
| 1207: | if ($name !== 'get' && $name !== 'set') { |
| 1208: | $this->emitError(new Error( |
| 1209: | 'Unknown hook "' . $hook->name . '", expected "get" or "set"', |
| 1210: | $hook->name->getAttributes())); |
| 1211: | } |
| 1212: | if ($name === 'get' && $paramListPos !== null) { |
| 1213: | $this->emitError(new Error( |
| 1214: | 'get hook must not have a parameter list', $this->getAttributesAt($paramListPos))); |
| 1215: | } |
| 1216: | } |
| 1217: | |
| 1218: | protected function checkPropertyHookModifiers(int $a, int $b, int $modifierPos): void { |
| 1219: | try { |
| 1220: | Modifiers::verifyModifier($a, $b); |
| 1221: | } catch (Error $error) { |
| 1222: | $error->setAttributes($this->getAttributesAt($modifierPos)); |
| 1223: | $this->emitError($error); |
| 1224: | } |
| 1225: | |
| 1226: | if ($b != Modifiers::FINAL) { |
| 1227: | $this->emitError(new Error( |
| 1228: | 'Cannot use the ' . Modifiers::toString($b) . ' modifier on a property hook', |
| 1229: | $this->getAttributesAt($modifierPos))); |
| 1230: | } |
| 1231: | } |
| 1232: | |
| 1233: | protected function checkConstantAttributes(Const_ $node): void { |
| 1234: | if ($node->attrGroups !== [] && count($node->consts) > 1) { |
| 1235: | $this->emitError(new Error( |
| 1236: | 'Cannot use attributes on multiple constants at once', $node->getAttributes())); |
| 1237: | } |
| 1238: | } |
| 1239: | |
| 1240: | |
| 1241: | |
| 1242: | |
| 1243: | protected function addPropertyNameToHooks(Node $node): void { |
| 1244: | if ($node instanceof Property) { |
| 1245: | $name = $node->props[0]->name->toString(); |
| 1246: | } else { |
| 1247: | $name = $node->var->name; |
| 1248: | } |
| 1249: | foreach ($node->hooks as $hook) { |
| 1250: | $hook->setAttribute('propertyName', $name); |
| 1251: | } |
| 1252: | } |
| 1253: | |
| 1254: | |
| 1255: | private function isSimpleExit(array $args): bool { |
| 1256: | if (\count($args) === 0) { |
| 1257: | return true; |
| 1258: | } |
| 1259: | if (\count($args) === 1) { |
| 1260: | $arg = $args[0]; |
| 1261: | return $arg instanceof Arg && $arg->name === null && |
| 1262: | $arg->byRef === false && $arg->unpack === false; |
| 1263: | } |
| 1264: | return false; |
| 1265: | } |
| 1266: | |
| 1267: | |
| 1268: | |
| 1269: | |
| 1270: | |
| 1271: | protected function createExitExpr(string $name, int $namePos, array $args, array $attrs): Expr { |
| 1272: | if ($this->isSimpleExit($args)) { |
| 1273: | |
| 1274: | $attrs['kind'] = strtolower($name) === 'exit' ? Expr\Exit_::KIND_EXIT : Expr\Exit_::KIND_DIE; |
| 1275: | return new Expr\Exit_(\count($args) === 1 ? $args[0]->value : null, $attrs); |
| 1276: | } |
| 1277: | return new Expr\FuncCall(new Name($name, $this->getAttributesAt($namePos)), $args, $attrs); |
| 1278: | } |
| 1279: | |
| 1280: | |
| 1281: | |
| 1282: | |
| 1283: | |
| 1284: | |
| 1285: | |
| 1286: | |
| 1287: | |
| 1288: | |
| 1289: | protected function createTokenMap(): array { |
| 1290: | $tokenMap = []; |
| 1291: | |
| 1292: | |
| 1293: | for ($i = 0; $i < 256; ++$i) { |
| 1294: | $tokenMap[$i] = $i; |
| 1295: | } |
| 1296: | |
| 1297: | foreach ($this->symbolToName as $name) { |
| 1298: | if ($name[0] === 'T') { |
| 1299: | $tokenMap[\constant($name)] = constant(static::class . '::' . $name); |
| 1300: | } |
| 1301: | } |
| 1302: | |
| 1303: | |
| 1304: | $tokenMap[\T_OPEN_TAG_WITH_ECHO] = static::T_ECHO; |
| 1305: | |
| 1306: | $tokenMap[\T_CLOSE_TAG] = ord(';'); |
| 1307: | |
| 1308: | |
| 1309: | |
| 1310: | $fullTokenMap = []; |
| 1311: | foreach ($tokenMap as $phpToken => $extSymbol) { |
| 1312: | $intSymbol = $this->tokenToSymbol[$extSymbol]; |
| 1313: | if ($intSymbol === $this->invalidSymbol) { |
| 1314: | continue; |
| 1315: | } |
| 1316: | $fullTokenMap[$phpToken] = $intSymbol; |
| 1317: | } |
| 1318: | |
| 1319: | return $fullTokenMap; |
| 1320: | } |
| 1321: | } |
| 1322: | |