説明なし

ParserAbstract.php 40KB

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