public function setInput($code, \PHPParser_Node $ast = null) { $this->astStream->setAst($ast ?: PhpParser\ParseUtils::parse($code)); $this->tokenStream->setCode($code); $lastNode = null; while ($this->moveNext()) { if ($lastNode !== $this->node) { $this->node->setAttribute('start_token', $this->token); if (null !== $lastNode) { $lastNode->setAttribute('end_token', $this->token->getPreviousToken()->get()); } } $lastNode = $this->node; } $this->reset(); }
/** * @param TokenStream\AbstractToken $token * @param array $tokens the format returned by ``token_get_all`` */ private function insertAllTokensBefore(AbstractToken $token, array $tokens) { $normalizedTokens = $this->normalizeTokens($tokens); $lineOffset = 0; foreach ($normalizedTokens as $newToken) { $lineOffset += substr_count($newToken->getValue(), "\n"); } foreach ($this->tokens as $i => $cToken) { if ($token !== $cToken) { continue; } // Compensate for the token that is being inserted, so that we do not visit a token twice. if ($i <= $this->i) { $this->i += count($normalizedTokens); } $this->tokens = array_merge(array_slice($this->tokens, 0, $i), $normalizedTokens, array_slice($this->tokens, $i)); for ($k = $i, $c = count($this->tokens); $k < $c; $k++) { $this->tokens[$i]->setAttribute('position', $k); } for ($k = 0, $c = count($normalizedTokens); $k < $c; $k++) { if (0 === $k) { $token->getPreviousToken()->get()->setNextToken($normalizedTokens[$k]); $normalizedTokens[$k]->setPreviousToken($token->getPreviousToken()->get()); } if ($k + 1 === $c) { $token->setPreviousToken($normalizedTokens[$k]); $normalizedTokens[$k]->setNextToken($token); } if ($k > 0 && $k + 1 < $c) { $normalizedTokens[$k]->setPreviousToken($normalizedTokens[$k - 1]); $normalizedTokens[$k - 1]->setNextToken($normalizedTokens[$k]); $normalizedTokens[$k]->setNextToken($normalizedTokens[$k + 1]); $normalizedTokens[$k + 1]->setPreviousToken($normalizedTokens[$k - 1]); } } if ($lineOffset > 0) { $nextToken = $token; do { $nextToken->setLine($nextToken->getLine() + $lineOffset); } while (null !== ($nextToken = $nextToken->getNextToken()->getOrElse(null))); } break; } }