ParserAbstract.php 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029
  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. }