Chris@13: lexer = $lexer; Chris@0: Chris@0: if (isset($options['throwOnError'])) { Chris@0: throw new \LogicException( Chris@0: '"throwOnError" is no longer supported, use "errorHandler" instead'); Chris@0: } Chris@13: Chris@13: $this->initReduceCallbacks(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Parses PHP code into a node tree. Chris@0: * Chris@0: * If a non-throwing error handler is used, the parser will continue parsing after an error Chris@0: * occurred and attempt to build a partial AST. Chris@0: * Chris@0: * @param string $code The source code to parse Chris@0: * @param ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults Chris@0: * to ErrorHandler\Throwing. Chris@0: * Chris@13: * @return Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and Chris@13: * the parser was unable to recover from an error). Chris@0: */ Chris@13: public function parse(string $code, ErrorHandler $errorHandler = null) { Chris@0: $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing; Chris@0: Chris@0: $this->lexer->startLexing($code, $this->errorHandler); Chris@13: $result = $this->doParse(); Chris@0: Chris@13: // Clear out some of the interior state, so we don't hold onto unnecessary Chris@13: // memory between uses of the parser Chris@13: $this->startAttributeStack = []; Chris@13: $this->endAttributeStack = []; Chris@13: $this->semStack = []; Chris@13: $this->semValue = null; Chris@13: Chris@13: return $result; Chris@13: } Chris@13: Chris@13: protected function doParse() { Chris@0: // We start off with no lookahead-token Chris@0: $symbol = self::SYMBOL_NONE; Chris@0: Chris@0: // The attributes for a node are taken from the first and last token of the node. Chris@0: // From the first token only the startAttributes are taken and from the last only Chris@0: // the endAttributes. Both are merged using the array union operator (+). Chris@13: $startAttributes = []; Chris@13: $endAttributes = []; Chris@0: $this->endAttributes = $endAttributes; Chris@0: Chris@0: // Keep stack of start and end attributes Chris@13: $this->startAttributeStack = []; Chris@13: $this->endAttributeStack = [$endAttributes]; Chris@0: Chris@0: // Start off in the initial state and keep a stack of previous states Chris@0: $state = 0; Chris@13: $stateStack = [$state]; Chris@0: Chris@0: // Semantic value stack (contains values of tokens and semantic action results) Chris@13: $this->semStack = []; Chris@0: Chris@0: // Current position in the stack(s) Chris@13: $stackPos = 0; Chris@0: Chris@0: $this->errorState = 0; Chris@0: Chris@0: for (;;) { Chris@0: //$this->traceNewState($state, $symbol); Chris@0: Chris@13: if ($this->actionBase[$state] === 0) { Chris@0: $rule = $this->actionDefault[$state]; Chris@0: } else { Chris@0: if ($symbol === self::SYMBOL_NONE) { Chris@0: // Fetch the next token id from the lexer and fetch additional info by-ref. Chris@0: // The end attributes are fetched into a temporary variable and only set once the token is really Chris@0: // shifted (not during read). Otherwise you would sometimes get off-by-one errors, when a rule is Chris@0: // reduced after a token was read but not yet shifted. Chris@0: $tokenId = $this->lexer->getNextToken($tokenValue, $startAttributes, $endAttributes); Chris@0: Chris@0: // map the lexer token id to the internally used symbols Chris@0: $symbol = $tokenId >= 0 && $tokenId < $this->tokenToSymbolMapSize Chris@0: ? $this->tokenToSymbol[$tokenId] Chris@0: : $this->invalidSymbol; Chris@0: Chris@0: if ($symbol === $this->invalidSymbol) { Chris@0: throw new \RangeException(sprintf( Chris@0: 'The lexer returned an invalid token (id=%d, value=%s)', Chris@0: $tokenId, $tokenValue Chris@0: )); Chris@0: } Chris@0: Chris@0: // This is necessary to assign some meaningful attributes to /* empty */ productions. They'll get Chris@0: // the attributes of the next token, even though they don't contain it themselves. Chris@13: $this->startAttributeStack[$stackPos+1] = $startAttributes; Chris@13: $this->endAttributeStack[$stackPos+1] = $endAttributes; Chris@0: $this->lookaheadStartAttributes = $startAttributes; Chris@0: Chris@0: //$this->traceRead($symbol); Chris@0: } Chris@0: Chris@0: $idx = $this->actionBase[$state] + $symbol; Chris@13: if ((($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol) Chris@0: || ($state < $this->YY2TBLSTATE Chris@13: && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0 Chris@13: && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol)) Chris@13: && ($action = $this->action[$idx]) !== $this->defaultAction) { Chris@0: /* Chris@13: * >= numNonLeafStates: shift and reduce Chris@0: * > 0: shift Chris@0: * = 0: accept Chris@0: * < 0: reduce Chris@0: * = -YYUNEXPECTED: error Chris@0: */ Chris@0: if ($action > 0) { Chris@0: /* shift */ Chris@0: //$this->traceShift($symbol); Chris@0: Chris@13: ++$stackPos; Chris@13: $stateStack[$stackPos] = $state = $action; Chris@13: $this->semStack[$stackPos] = $tokenValue; Chris@13: $this->startAttributeStack[$stackPos] = $startAttributes; Chris@13: $this->endAttributeStack[$stackPos] = $endAttributes; Chris@0: $this->endAttributes = $endAttributes; Chris@0: $symbol = self::SYMBOL_NONE; Chris@0: Chris@0: if ($this->errorState) { Chris@0: --$this->errorState; Chris@0: } Chris@0: Chris@13: if ($action < $this->numNonLeafStates) { Chris@0: continue; Chris@0: } Chris@0: Chris@13: /* $yyn >= numNonLeafStates means shift-and-reduce */ Chris@13: $rule = $action - $this->numNonLeafStates; Chris@0: } else { Chris@0: $rule = -$action; Chris@0: } Chris@0: } else { Chris@0: $rule = $this->actionDefault[$state]; Chris@0: } Chris@0: } Chris@0: Chris@0: for (;;) { Chris@0: if ($rule === 0) { Chris@0: /* accept */ Chris@0: //$this->traceAccept(); Chris@0: return $this->semValue; Chris@0: } elseif ($rule !== $this->unexpectedTokenRule) { Chris@0: /* reduce */ Chris@0: //$this->traceReduce($rule); Chris@0: Chris@0: try { Chris@13: $this->reduceCallbacks[$rule]($stackPos); Chris@0: } catch (Error $e) { Chris@0: if (-1 === $e->getStartLine() && isset($startAttributes['startLine'])) { Chris@0: $e->setStartLine($startAttributes['startLine']); Chris@0: } Chris@0: Chris@0: $this->emitError($e); Chris@0: // Can't recover from this type of error Chris@0: return null; Chris@0: } Chris@0: Chris@0: /* Goto - shift nonterminal */ Chris@13: $lastEndAttributes = $this->endAttributeStack[$stackPos]; Chris@13: $stackPos -= $this->ruleToLength[$rule]; Chris@0: $nonTerminal = $this->ruleToNonTerminal[$rule]; Chris@13: $idx = $this->gotoBase[$nonTerminal] + $stateStack[$stackPos]; Chris@13: if ($idx >= 0 && $idx < $this->gotoTableSize && $this->gotoCheck[$idx] === $nonTerminal) { Chris@0: $state = $this->goto[$idx]; Chris@0: } else { Chris@0: $state = $this->gotoDefault[$nonTerminal]; Chris@0: } Chris@0: Chris@13: ++$stackPos; Chris@13: $stateStack[$stackPos] = $state; Chris@13: $this->semStack[$stackPos] = $this->semValue; Chris@13: $this->endAttributeStack[$stackPos] = $lastEndAttributes; Chris@0: } else { Chris@0: /* error */ Chris@0: switch ($this->errorState) { Chris@0: case 0: Chris@0: $msg = $this->getErrorMessage($symbol, $state); Chris@0: $this->emitError(new Error($msg, $startAttributes + $endAttributes)); Chris@0: // Break missing intentionally Chris@0: case 1: Chris@0: case 2: Chris@0: $this->errorState = 3; Chris@0: Chris@0: // Pop until error-expecting state uncovered Chris@0: while (!( Chris@0: (($idx = $this->actionBase[$state] + $this->errorSymbol) >= 0 Chris@13: && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol) Chris@0: || ($state < $this->YY2TBLSTATE Chris@13: && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $this->errorSymbol) >= 0 Chris@13: && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $this->errorSymbol) Chris@13: ) || ($action = $this->action[$idx]) === $this->defaultAction) { // Not totally sure about this Chris@13: if ($stackPos <= 0) { Chris@0: // Could not recover from error Chris@0: return null; Chris@0: } Chris@13: $state = $stateStack[--$stackPos]; Chris@0: //$this->tracePop($state); Chris@0: } Chris@0: Chris@0: //$this->traceShift($this->errorSymbol); Chris@13: ++$stackPos; Chris@13: $stateStack[$stackPos] = $state = $action; Chris@0: Chris@0: // We treat the error symbol as being empty, so we reset the end attributes Chris@0: // to the end attributes of the last non-error symbol Chris@13: $this->endAttributeStack[$stackPos] = $this->endAttributeStack[$stackPos - 1]; Chris@13: $this->endAttributes = $this->endAttributeStack[$stackPos - 1]; Chris@0: break; Chris@0: Chris@0: case 3: Chris@0: if ($symbol === 0) { Chris@0: // Reached EOF without recovering from error Chris@0: return null; Chris@0: } Chris@0: Chris@0: //$this->traceDiscard($symbol); Chris@0: $symbol = self::SYMBOL_NONE; Chris@0: break 2; Chris@0: } Chris@0: } Chris@0: Chris@13: if ($state < $this->numNonLeafStates) { Chris@0: break; Chris@0: } Chris@0: Chris@13: /* >= numNonLeafStates means shift-and-reduce */ Chris@13: $rule = $state - $this->numNonLeafStates; Chris@0: } Chris@0: } Chris@0: Chris@0: throw new \RuntimeException('Reached end of parser loop'); Chris@0: } Chris@0: Chris@0: protected function emitError(Error $error) { Chris@0: $this->errorHandler->handleError($error); Chris@0: } Chris@0: Chris@13: /** Chris@13: * Format error message including expected tokens. Chris@13: * Chris@13: * @param int $symbol Unexpected symbol Chris@13: * @param int $state State at time of error Chris@13: * Chris@13: * @return string Formatted error message Chris@13: */ Chris@13: protected function getErrorMessage(int $symbol, int $state) : string { Chris@0: $expectedString = ''; Chris@0: if ($expected = $this->getExpectedTokens($state)) { Chris@0: $expectedString = ', expecting ' . implode(' or ', $expected); Chris@0: } Chris@0: Chris@0: return 'Syntax error, unexpected ' . $this->symbolToName[$symbol] . $expectedString; Chris@0: } Chris@0: Chris@13: /** Chris@13: * Get limited number of expected tokens in given state. Chris@13: * Chris@13: * @param int $state State Chris@13: * Chris@13: * @return string[] Expected tokens. If too many, an empty array is returned. Chris@13: */ Chris@13: protected function getExpectedTokens(int $state) : array { Chris@13: $expected = []; Chris@0: Chris@0: $base = $this->actionBase[$state]; Chris@0: foreach ($this->symbolToName as $symbol => $name) { Chris@0: $idx = $base + $symbol; Chris@0: if ($idx >= 0 && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol Chris@0: || $state < $this->YY2TBLSTATE Chris@13: && ($idx = $this->actionBase[$state + $this->numNonLeafStates] + $symbol) >= 0 Chris@0: && $idx < $this->actionTableSize && $this->actionCheck[$idx] === $symbol Chris@0: ) { Chris@13: if ($this->action[$idx] !== $this->unexpectedTokenRule Chris@13: && $this->action[$idx] !== $this->defaultAction Chris@13: && $symbol !== $this->errorSymbol Chris@0: ) { Chris@13: if (count($expected) === 4) { Chris@0: /* Too many expected tokens */ Chris@13: return []; Chris@0: } Chris@0: Chris@0: $expected[] = $name; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: return $expected; Chris@0: } Chris@0: Chris@0: /* Chris@0: * Tracing functions used for debugging the parser. Chris@0: */ Chris@0: Chris@0: /* Chris@0: protected function traceNewState($state, $symbol) { Chris@0: echo '% State ' . $state Chris@0: . ', Lookahead ' . ($symbol == self::SYMBOL_NONE ? '--none--' : $this->symbolToName[$symbol]) . "\n"; Chris@0: } Chris@0: Chris@0: protected function traceRead($symbol) { Chris@0: echo '% Reading ' . $this->symbolToName[$symbol] . "\n"; Chris@0: } Chris@0: Chris@0: protected function traceShift($symbol) { Chris@0: echo '% Shift ' . $this->symbolToName[$symbol] . "\n"; Chris@0: } Chris@0: Chris@0: protected function traceAccept() { Chris@0: echo "% Accepted.\n"; Chris@0: } Chris@0: Chris@0: protected function traceReduce($n) { Chris@0: echo '% Reduce by (' . $n . ') ' . $this->productions[$n] . "\n"; Chris@0: } Chris@0: Chris@0: protected function tracePop($state) { Chris@0: echo '% Recovering, uncovered state ' . $state . "\n"; Chris@0: } Chris@0: Chris@0: protected function traceDiscard($symbol) { Chris@0: echo '% Discard ' . $this->symbolToName[$symbol] . "\n"; Chris@0: } Chris@0: */ Chris@0: Chris@0: /* Chris@0: * Helper functions invoked by semantic actions Chris@0: */ Chris@0: Chris@0: /** Chris@0: * Moves statements of semicolon-style namespaces into $ns->stmts and checks various error conditions. Chris@0: * Chris@13: * @param Node\Stmt[] $stmts Chris@13: * @return Node\Stmt[] Chris@0: */ Chris@13: protected function handleNamespaces(array $stmts) : array { Chris@0: $hasErrored = false; Chris@0: $style = $this->getNamespacingStyle($stmts); Chris@0: if (null === $style) { Chris@0: // not namespaced, nothing to do Chris@0: return $stmts; Chris@0: } elseif ('brace' === $style) { Chris@0: // For braced namespaces we only have to check that there are no invalid statements between the namespaces Chris@0: $afterFirstNamespace = false; Chris@0: foreach ($stmts as $stmt) { Chris@0: if ($stmt instanceof Node\Stmt\Namespace_) { Chris@0: $afterFirstNamespace = true; Chris@0: } elseif (!$stmt instanceof Node\Stmt\HaltCompiler Chris@0: && !$stmt instanceof Node\Stmt\Nop Chris@0: && $afterFirstNamespace && !$hasErrored) { Chris@0: $this->emitError(new Error( Chris@0: 'No code may exist outside of namespace {}', $stmt->getAttributes())); Chris@0: $hasErrored = true; // Avoid one error for every statement Chris@0: } Chris@0: } Chris@0: return $stmts; Chris@0: } else { Chris@0: // For semicolon namespaces we have to move the statements after a namespace declaration into ->stmts Chris@13: $resultStmts = []; Chris@0: $targetStmts =& $resultStmts; Chris@13: $lastNs = null; Chris@0: foreach ($stmts as $stmt) { Chris@0: if ($stmt instanceof Node\Stmt\Namespace_) { Chris@13: if ($lastNs !== null) { Chris@13: $this->fixupNamespaceAttributes($lastNs); Chris@13: } Chris@0: if ($stmt->stmts === null) { Chris@13: $stmt->stmts = []; Chris@0: $targetStmts =& $stmt->stmts; Chris@0: $resultStmts[] = $stmt; Chris@0: } else { Chris@0: // This handles the invalid case of mixed style namespaces Chris@0: $resultStmts[] = $stmt; Chris@0: $targetStmts =& $resultStmts; Chris@0: } Chris@13: $lastNs = $stmt; Chris@0: } elseif ($stmt instanceof Node\Stmt\HaltCompiler) { Chris@0: // __halt_compiler() is not moved into the namespace Chris@0: $resultStmts[] = $stmt; Chris@0: } else { Chris@0: $targetStmts[] = $stmt; Chris@0: } Chris@0: } Chris@13: if ($lastNs !== null) { Chris@13: $this->fixupNamespaceAttributes($lastNs); Chris@13: } Chris@0: return $resultStmts; Chris@0: } Chris@0: } Chris@0: Chris@13: private function fixupNamespaceAttributes(Node\Stmt\Namespace_ $stmt) { Chris@13: // We moved the statements into the namespace node, as such the end of the namespace node Chris@13: // needs to be extended to the end of the statements. Chris@13: if (empty($stmt->stmts)) { Chris@13: return; Chris@13: } Chris@13: Chris@13: // We only move the builtin end attributes here. This is the best we can do with the Chris@13: // knowledge we have. Chris@13: $endAttributes = ['endLine', 'endFilePos', 'endTokenPos']; Chris@13: $lastStmt = $stmt->stmts[count($stmt->stmts) - 1]; Chris@13: foreach ($endAttributes as $endAttribute) { Chris@13: if ($lastStmt->hasAttribute($endAttribute)) { Chris@13: $stmt->setAttribute($endAttribute, $lastStmt->getAttribute($endAttribute)); Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@13: /** Chris@13: * Determine namespacing style (semicolon or brace) Chris@13: * Chris@13: * @param Node[] $stmts Top-level statements. Chris@13: * Chris@13: * @return null|string One of "semicolon", "brace" or null (no namespaces) Chris@13: */ Chris@0: private function getNamespacingStyle(array $stmts) { Chris@0: $style = null; Chris@0: $hasNotAllowedStmts = false; Chris@0: foreach ($stmts as $i => $stmt) { Chris@0: if ($stmt instanceof Node\Stmt\Namespace_) { Chris@0: $currentStyle = null === $stmt->stmts ? 'semicolon' : 'brace'; Chris@0: if (null === $style) { Chris@0: $style = $currentStyle; Chris@0: if ($hasNotAllowedStmts) { Chris@0: $this->emitError(new Error( Chris@0: 'Namespace declaration statement has to be the very first statement in the script', Chris@0: $stmt->getLine() // Avoid marking the entire namespace as an error Chris@0: )); Chris@0: } Chris@0: } elseif ($style !== $currentStyle) { Chris@0: $this->emitError(new Error( Chris@0: 'Cannot mix bracketed namespace declarations with unbracketed namespace declarations', Chris@0: $stmt->getLine() // Avoid marking the entire namespace as an error Chris@0: )); Chris@0: // Treat like semicolon style for namespace normalization Chris@0: return 'semicolon'; Chris@0: } Chris@0: continue; Chris@0: } Chris@0: Chris@0: /* declare(), __halt_compiler() and nops can be used before a namespace declaration */ Chris@0: if ($stmt instanceof Node\Stmt\Declare_ Chris@0: || $stmt instanceof Node\Stmt\HaltCompiler Chris@0: || $stmt instanceof Node\Stmt\Nop) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: /* There may be a hashbang line at the very start of the file */ Chris@13: if ($i === 0 && $stmt instanceof Node\Stmt\InlineHTML && preg_match('/\A#!.*\r?\n\z/', $stmt->value)) { Chris@0: continue; Chris@0: } Chris@0: Chris@0: /* Everything else if forbidden before namespace declarations */ Chris@0: $hasNotAllowedStmts = true; Chris@0: } Chris@0: return $style; Chris@0: } Chris@0: Chris@13: /** Chris@13: * Fix up parsing of static property calls in PHP 5. Chris@13: * Chris@13: * In PHP 5 A::$b[c][d] and A::$b[c][d]() have very different interpretation. The former is Chris@13: * interpreted as (A::$b)[c][d], while the latter is the same as A::{$b[c][d]}(). We parse the Chris@13: * latter as the former initially and this method fixes the AST into the correct form when we Chris@13: * encounter the "()". Chris@13: * Chris@13: * @param Node\Expr\StaticPropertyFetch|Node\Expr\ArrayDimFetch $prop Chris@13: * @param Node\Arg[] $args Chris@13: * @param array $attributes Chris@13: * Chris@13: * @return Expr\StaticCall Chris@13: */ Chris@13: protected function fixupPhp5StaticPropCall($prop, array $args, array $attributes) : Expr\StaticCall { Chris@13: if ($prop instanceof Node\Expr\StaticPropertyFetch) { Chris@13: $name = $prop->name instanceof VarLikeIdentifier Chris@13: ? $prop->name->toString() : $prop->name; Chris@13: $var = new Expr\Variable($name, $prop->name->getAttributes()); Chris@13: return new Expr\StaticCall($prop->class, $var, $args, $attributes); Chris@13: } elseif ($prop instanceof Node\Expr\ArrayDimFetch) { Chris@13: $tmp = $prop; Chris@13: while ($tmp->var instanceof Node\Expr\ArrayDimFetch) { Chris@13: $tmp = $tmp->var; Chris@13: } Chris@13: Chris@13: /** @var Expr\StaticPropertyFetch $staticProp */ Chris@13: $staticProp = $tmp->var; Chris@13: Chris@13: // Set start attributes to attributes of innermost node Chris@13: $tmp = $prop; Chris@13: $this->fixupStartAttributes($tmp, $staticProp->name); Chris@13: while ($tmp->var instanceof Node\Expr\ArrayDimFetch) { Chris@13: $tmp = $tmp->var; Chris@13: $this->fixupStartAttributes($tmp, $staticProp->name); Chris@13: } Chris@13: Chris@13: $name = $staticProp->name instanceof VarLikeIdentifier Chris@13: ? $staticProp->name->toString() : $staticProp->name; Chris@13: $tmp->var = new Expr\Variable($name, $staticProp->name->getAttributes()); Chris@13: return new Expr\StaticCall($staticProp->class, $prop, $args, $attributes); Chris@13: } else { Chris@13: throw new \Exception; Chris@13: } Chris@13: } Chris@13: Chris@13: protected function fixupStartAttributes(Node $to, Node $from) { Chris@13: $startAttributes = ['startLine', 'startFilePos', 'startTokenPos']; Chris@13: foreach ($startAttributes as $startAttribute) { Chris@13: if ($from->hasAttribute($startAttribute)) { Chris@13: $to->setAttribute($startAttribute, $from->getAttribute($startAttribute)); Chris@13: } Chris@13: } Chris@13: } Chris@13: Chris@0: protected function handleBuiltinTypes(Name $name) { Chris@0: $scalarTypes = [ Chris@0: 'bool' => true, Chris@0: 'int' => true, Chris@0: 'float' => true, Chris@0: 'string' => true, Chris@0: 'iterable' => true, Chris@0: 'void' => true, Chris@0: 'object' => true, Chris@0: ]; Chris@0: Chris@0: if (!$name->isUnqualified()) { Chris@0: return $name; Chris@0: } Chris@0: Chris@13: $lowerName = $name->toLowerString(); Chris@13: if (!isset($scalarTypes[$lowerName])) { Chris@13: return $name; Chris@13: } Chris@13: Chris@13: return new Node\Identifier($lowerName, $name->getAttributes()); Chris@0: } Chris@0: Chris@13: /** Chris@13: * Get combined start and end attributes at a stack location Chris@13: * Chris@13: * @param int $pos Stack location Chris@13: * Chris@13: * @return array Combined start and end attributes Chris@13: */ Chris@13: protected function getAttributesAt(int $pos) : array { Chris@0: return $this->startAttributeStack[$pos] + $this->endAttributeStack[$pos]; Chris@0: } Chris@0: Chris@17: protected function getFloatCastKind(string $cast): int Chris@17: { Chris@17: $cast = strtolower($cast); Chris@17: if (strpos($cast, 'float') !== false) { Chris@17: return Double::KIND_FLOAT; Chris@17: } Chris@17: Chris@17: if (strpos($cast, 'real') !== false) { Chris@17: return Double::KIND_REAL; Chris@17: } Chris@17: Chris@17: return Double::KIND_DOUBLE; Chris@17: } Chris@17: Chris@0: protected function parseLNumber($str, $attributes, $allowInvalidOctal = false) { Chris@0: try { Chris@0: return LNumber::fromString($str, $attributes, $allowInvalidOctal); Chris@0: } catch (Error $error) { Chris@0: $this->emitError($error); Chris@0: // Use dummy value Chris@0: return new LNumber(0, $attributes); Chris@0: } Chris@0: } Chris@0: Chris@13: /** Chris@13: * Parse a T_NUM_STRING token into either an integer or string node. Chris@13: * Chris@13: * @param string $str Number string Chris@13: * @param array $attributes Attributes Chris@13: * Chris@13: * @return LNumber|String_ Integer or string node. Chris@13: */ Chris@13: protected function parseNumString(string $str, array $attributes) { Chris@0: if (!preg_match('/^(?:0|-?[1-9][0-9]*)$/', $str)) { Chris@0: return new String_($str, $attributes); Chris@0: } Chris@0: Chris@0: $num = +$str; Chris@0: if (!is_int($num)) { Chris@0: return new String_($str, $attributes); Chris@0: } Chris@0: Chris@0: return new LNumber($num, $attributes); Chris@0: } Chris@0: Chris@17: protected function stripIndentation( Chris@17: string $string, int $indentLen, string $indentChar, Chris@17: bool $newlineAtStart, bool $newlineAtEnd, array $attributes Chris@17: ) { Chris@17: if ($indentLen === 0) { Chris@17: return $string; Chris@17: } Chris@17: Chris@17: $start = $newlineAtStart ? '(?:(?<=\n)|\A)' : '(?<=\n)'; Chris@17: $end = $newlineAtEnd ? '(?:(?=[\r\n])|\z)' : '(?=[\r\n])'; Chris@17: $regex = '/' . $start . '([ \t]*)(' . $end . ')?/'; Chris@17: return preg_replace_callback( Chris@17: $regex, Chris@17: function ($matches) use ($indentLen, $indentChar, $attributes) { Chris@17: $prefix = substr($matches[1], 0, $indentLen); Chris@17: if (false !== strpos($prefix, $indentChar === " " ? "\t" : " ")) { Chris@17: $this->emitError(new Error( Chris@17: 'Invalid indentation - tabs and spaces cannot be mixed', $attributes Chris@17: )); Chris@17: } elseif (strlen($prefix) < $indentLen && !isset($matches[2])) { Chris@17: $this->emitError(new Error( Chris@17: 'Invalid body indentation level ' . Chris@17: '(expecting an indentation level of at least ' . $indentLen . ')', Chris@17: $attributes Chris@17: )); Chris@17: } Chris@17: return substr($matches[0], strlen($prefix)); Chris@17: }, Chris@17: $string Chris@17: ); Chris@17: } Chris@17: Chris@17: protected function parseDocString( Chris@17: string $startToken, $contents, string $endToken, Chris@17: array $attributes, array $endTokenAttributes, bool $parseUnicodeEscape Chris@17: ) { Chris@17: $kind = strpos($startToken, "'") === false Chris@17: ? String_::KIND_HEREDOC : String_::KIND_NOWDOC; Chris@17: Chris@17: $regex = '/\A[bB]?<<<[ \t]*[\'"]?([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*)[\'"]?(?:\r\n|\n|\r)\z/'; Chris@17: $result = preg_match($regex, $startToken, $matches); Chris@17: assert($result === 1); Chris@17: $label = $matches[1]; Chris@17: Chris@17: $result = preg_match('/\A[ \t]*/', $endToken, $matches); Chris@17: assert($result === 1); Chris@17: $indentation = $matches[0]; Chris@17: Chris@17: $attributes['kind'] = $kind; Chris@17: $attributes['docLabel'] = $label; Chris@17: $attributes['docIndentation'] = $indentation; Chris@17: Chris@17: $indentHasSpaces = false !== strpos($indentation, " "); Chris@17: $indentHasTabs = false !== strpos($indentation, "\t"); Chris@17: if ($indentHasSpaces && $indentHasTabs) { Chris@17: $this->emitError(new Error( Chris@17: 'Invalid indentation - tabs and spaces cannot be mixed', Chris@17: $endTokenAttributes Chris@17: )); Chris@17: Chris@17: // Proceed processing as if this doc string is not indented Chris@17: $indentation = ''; Chris@17: } Chris@17: Chris@17: $indentLen = \strlen($indentation); Chris@17: $indentChar = $indentHasSpaces ? " " : "\t"; Chris@17: Chris@17: if (\is_string($contents)) { Chris@17: if ($contents === '') { Chris@17: return new String_('', $attributes); Chris@17: } Chris@17: Chris@17: $contents = $this->stripIndentation( Chris@17: $contents, $indentLen, $indentChar, true, true, $attributes Chris@17: ); Chris@17: $contents = preg_replace('~(\r\n|\n|\r)\z~', '', $contents); Chris@17: Chris@17: if ($kind === String_::KIND_HEREDOC) { Chris@17: $contents = String_::parseEscapeSequences($contents, null, $parseUnicodeEscape); Chris@17: } Chris@17: Chris@17: return new String_($contents, $attributes); Chris@17: } else { Chris@17: assert(count($contents) > 0); Chris@17: if (!$contents[0] instanceof Node\Scalar\EncapsedStringPart) { Chris@17: // If there is no leading encapsed string part, pretend there is an empty one Chris@17: $this->stripIndentation( Chris@17: '', $indentLen, $indentChar, true, false, $contents[0]->getAttributes() Chris@17: ); Chris@17: } Chris@17: Chris@17: $newContents = []; Chris@17: foreach ($contents as $i => $part) { Chris@17: if ($part instanceof Node\Scalar\EncapsedStringPart) { Chris@17: $isLast = $i === \count($contents) - 1; Chris@17: $part->value = $this->stripIndentation( Chris@17: $part->value, $indentLen, $indentChar, Chris@17: $i === 0, $isLast, $part->getAttributes() Chris@17: ); Chris@17: $part->value = String_::parseEscapeSequences($part->value, null, $parseUnicodeEscape); Chris@17: if ($isLast) { Chris@17: $part->value = preg_replace('~(\r\n|\n|\r)\z~', '', $part->value); Chris@17: } Chris@17: if ('' === $part->value) { Chris@17: continue; Chris@17: } Chris@17: } Chris@17: $newContents[] = $part; Chris@17: } Chris@17: return new Encapsed($newContents, $attributes); Chris@17: } Chris@17: } Chris@17: Chris@0: protected function checkModifier($a, $b, $modifierPos) { Chris@0: // Jumping through some hoops here because verifyModifier() is also used elsewhere Chris@0: try { Chris@0: Class_::verifyModifier($a, $b); Chris@0: } catch (Error $error) { Chris@0: $error->setAttributes($this->getAttributesAt($modifierPos)); Chris@0: $this->emitError($error); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkParam(Param $node) { Chris@0: if ($node->variadic && null !== $node->default) { Chris@0: $this->emitError(new Error( Chris@0: 'Variadic parameter cannot have a default value', Chris@0: $node->default->getAttributes() Chris@0: )); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkTryCatch(TryCatch $node) { Chris@0: if (empty($node->catches) && null === $node->finally) { Chris@0: $this->emitError(new Error( Chris@0: 'Cannot use try without catch or finally', $node->getAttributes() Chris@0: )); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkNamespace(Namespace_ $node) { Chris@13: if ($node->name && $node->name->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as namespace name', $node->name), Chris@0: $node->name->getAttributes() Chris@0: )); Chris@0: } Chris@0: Chris@0: if (null !== $node->stmts) { Chris@0: foreach ($node->stmts as $stmt) { Chris@0: if ($stmt instanceof Namespace_) { Chris@0: $this->emitError(new Error( Chris@0: 'Namespace declarations cannot be nested', $stmt->getAttributes() Chris@0: )); Chris@0: } Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkClass(Class_ $node, $namePos) { Chris@13: if (null !== $node->name && $node->name->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name), Chris@0: $this->getAttributesAt($namePos) Chris@0: )); Chris@0: } Chris@0: Chris@13: if ($node->extends && $node->extends->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as class name as it is reserved', $node->extends), Chris@0: $node->extends->getAttributes() Chris@0: )); Chris@0: } Chris@0: Chris@0: foreach ($node->implements as $interface) { Chris@13: if ($interface->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), Chris@0: $interface->getAttributes() Chris@0: )); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkInterface(Interface_ $node, $namePos) { Chris@13: if (null !== $node->name && $node->name->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as class name as it is reserved', $node->name), Chris@0: $this->getAttributesAt($namePos) Chris@0: )); Chris@0: } Chris@0: Chris@0: foreach ($node->extends as $interface) { Chris@13: if ($interface->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf('Cannot use \'%s\' as interface name as it is reserved', $interface), Chris@0: $interface->getAttributes() Chris@0: )); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkClassMethod(ClassMethod $node, $modifierPos) { Chris@0: if ($node->flags & Class_::MODIFIER_STATIC) { Chris@13: switch ($node->name->toLowerString()) { Chris@0: case '__construct': Chris@0: $this->emitError(new Error( Chris@0: sprintf('Constructor %s() cannot be static', $node->name), Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: break; Chris@0: case '__destruct': Chris@0: $this->emitError(new Error( Chris@0: sprintf('Destructor %s() cannot be static', $node->name), Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: break; Chris@0: case '__clone': Chris@0: $this->emitError(new Error( Chris@0: sprintf('Clone method %s() cannot be static', $node->name), Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: break; Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkClassConst(ClassConst $node, $modifierPos) { Chris@0: if ($node->flags & Class_::MODIFIER_STATIC) { Chris@0: $this->emitError(new Error( Chris@0: "Cannot use 'static' as constant modifier", Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: } Chris@0: if ($node->flags & Class_::MODIFIER_ABSTRACT) { Chris@0: $this->emitError(new Error( Chris@0: "Cannot use 'abstract' as constant modifier", Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: } Chris@0: if ($node->flags & Class_::MODIFIER_FINAL) { Chris@0: $this->emitError(new Error( Chris@0: "Cannot use 'final' as constant modifier", Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkProperty(Property $node, $modifierPos) { Chris@0: if ($node->flags & Class_::MODIFIER_ABSTRACT) { Chris@0: $this->emitError(new Error('Properties cannot be declared abstract', Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: } Chris@0: Chris@0: if ($node->flags & Class_::MODIFIER_FINAL) { Chris@0: $this->emitError(new Error('Properties cannot be declared final', Chris@0: $this->getAttributesAt($modifierPos))); Chris@0: } Chris@0: } Chris@0: Chris@0: protected function checkUseUse(UseUse $node, $namePos) { Chris@13: if ($node->alias && $node->alias->isSpecialClassName()) { Chris@0: $this->emitError(new Error( Chris@0: sprintf( Chris@0: 'Cannot use %s as %s because \'%2$s\' is a special class name', Chris@0: $node->name, $node->alias Chris@0: ), Chris@0: $this->getAttributesAt($namePos) Chris@0: )); Chris@0: } Chris@0: } Chris@0: }