ParserAbstract.php 40 KB

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