2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2011-04-18 19:02:30 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2022-06-19 17:29:24 +02:00
|
|
|
require __DIR__ . '/compatibility_tokens.php';
|
|
|
|
|
2022-08-28 22:57:06 +02:00
|
|
|
class Lexer {
|
2022-06-04 17:34:48 +02:00
|
|
|
/** @var string Code being tokenized */
|
2011-06-03 22:02:02 +02:00
|
|
|
protected $code;
|
2022-12-14 22:59:53 +01:00
|
|
|
/** @var list<Token> List of tokens */
|
2011-04-18 19:02:30 +02:00
|
|
|
protected $tokens;
|
2022-06-04 17:34:48 +02:00
|
|
|
/** @var int Current position in the token array */
|
2011-04-18 19:02:30 +02:00
|
|
|
protected $pos;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether the preceding closing PHP tag has a trailing newline */
|
2016-07-25 16:42:42 +02:00
|
|
|
protected $prevCloseTagHasNewline;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var array<int, int> Map of tokens that should be dropped (like T_WHITESPACE) */
|
2012-04-25 20:04:46 +02:00
|
|
|
protected $dropTokens;
|
2011-04-18 19:02:30 +02:00
|
|
|
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the startLine attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeStartLineUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the endLine attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeEndLineUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the startTokenPos attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeStartTokenPosUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the endTokenPos attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeEndTokenPosUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the startFilePos attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeStartFilePosUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the endFilePos attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeEndFilePosUsed;
|
2022-09-11 22:33:18 +02:00
|
|
|
/** @var bool Whether to use the comments attribute */
|
2019-05-12 15:26:26 +02:00
|
|
|
private $attributeCommentsUsed;
|
2014-11-27 20:38:14 +01:00
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Creates a Lexer.
|
2014-11-27 20:38:14 +01:00
|
|
|
*
|
2022-09-17 17:12:55 +02:00
|
|
|
* @param array{usedAttributes?: string[]} $options Options array. Currently only the
|
|
|
|
* 'usedAttributes' option is supported, which is an array of attributes to add to the
|
|
|
|
* AST nodes. Possible attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
|
|
|
|
* 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the first three.
|
|
|
|
* For more info see getNextToken() docs.
|
2012-04-25 20:04:46 +02:00
|
|
|
*/
|
2017-08-13 14:06:08 +02:00
|
|
|
public function __construct(array $options = []) {
|
2012-04-25 20:04:46 +02:00
|
|
|
// map of tokens to drop while lexing (the map is only used for isset lookup,
|
|
|
|
// that's why the value is simply set to 1; the value is never actually used.)
|
2016-04-02 07:54:01 +09:00
|
|
|
$this->dropTokens = array_fill_keys(
|
2019-08-12 22:10:02 +02:00
|
|
|
[\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT, \T_BAD_CHARACTER], 1
|
2016-04-02 07:54:01 +09:00
|
|
|
);
|
2014-11-27 20:38:14 +01:00
|
|
|
|
2019-05-12 15:26:26 +02:00
|
|
|
$defaultAttributes = ['comments', 'startLine', 'endLine'];
|
|
|
|
$usedAttributes = array_fill_keys($options['usedAttributes'] ?? $defaultAttributes, true);
|
|
|
|
|
|
|
|
// Create individual boolean properties to make these checks faster.
|
|
|
|
$this->attributeStartLineUsed = isset($usedAttributes['startLine']);
|
|
|
|
$this->attributeEndLineUsed = isset($usedAttributes['endLine']);
|
|
|
|
$this->attributeStartTokenPosUsed = isset($usedAttributes['startTokenPos']);
|
|
|
|
$this->attributeEndTokenPosUsed = isset($usedAttributes['endTokenPos']);
|
|
|
|
$this->attributeStartFilePosUsed = isset($usedAttributes['startFilePos']);
|
|
|
|
$this->attributeEndFilePosUsed = isset($usedAttributes['endFilePos']);
|
|
|
|
$this->attributeCommentsUsed = isset($usedAttributes['comments']);
|
2012-04-25 20:04:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the lexer for lexing the provided source code.
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
2016-09-30 18:28:35 +02:00
|
|
|
* This function does not throw if lexing errors occur. Instead, errors may be retrieved using
|
|
|
|
* the getErrors() method.
|
2011-07-13 23:07:05 +02:00
|
|
|
*
|
2016-09-30 18:28:35 +02:00
|
|
|
* @param string $code The source code to lex
|
2016-10-09 13:15:24 +02:00
|
|
|
* @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
|
|
|
|
* ErrorHandler\Throwing
|
2011-05-31 16:33:11 +02:00
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
public function startLexing(string $code, ?ErrorHandler $errorHandler = null): void {
|
2016-10-09 13:15:24 +02:00
|
|
|
if (null === $errorHandler) {
|
|
|
|
$errorHandler = new ErrorHandler\Throwing();
|
|
|
|
}
|
|
|
|
|
2016-09-30 18:28:35 +02:00
|
|
|
$this->code = $code; // keep the code around for __halt_compiler() handling
|
2022-06-04 17:34:48 +02:00
|
|
|
$this->pos = -1;
|
2016-09-30 18:28:35 +02:00
|
|
|
|
|
|
|
// If inline HTML occurs without preceding code, treat it as if it had a leading newline.
|
|
|
|
// This ensures proper composability, because having a newline is the "safe" assumption.
|
|
|
|
$this->prevCloseTagHasNewline = true;
|
|
|
|
|
2015-03-23 11:43:22 +01:00
|
|
|
$scream = ini_set('xdebug.scream', '0');
|
2014-04-19 22:26:05 +02:00
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
$this->tokens = @Token::tokenize($code);
|
2020-06-27 18:53:09 +02:00
|
|
|
$this->postprocessTokens($errorHandler);
|
2012-02-21 17:00:49 +01:00
|
|
|
|
2015-03-23 11:43:22 +01:00
|
|
|
if (false !== $scream) {
|
|
|
|
ini_set('xdebug.scream', $scream);
|
|
|
|
}
|
2012-02-21 17:00:49 +01:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
private function handleInvalidCharacter(Token $token, ErrorHandler $errorHandler): void {
|
|
|
|
$chr = $token->text;
|
|
|
|
if ($chr === "\0") {
|
|
|
|
// PHP cuts error message after null byte, so need special case
|
|
|
|
$errorMsg = 'Unexpected null byte';
|
|
|
|
} else {
|
|
|
|
$errorMsg = sprintf(
|
|
|
|
'Unexpected character "%s" (ASCII %d)', $chr, ord($chr)
|
|
|
|
);
|
2016-09-30 18:28:35 +02:00
|
|
|
}
|
2022-06-04 17:34:48 +02:00
|
|
|
|
|
|
|
$errorHandler->handleError(new Error($errorMsg, [
|
|
|
|
'startLine' => $token->line,
|
|
|
|
'endLine' => $token->line,
|
|
|
|
'startFilePos' => $token->pos,
|
|
|
|
'endFilePos' => $token->pos,
|
|
|
|
]));
|
2016-09-30 18:28:35 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
private function isUnterminatedComment(Token $token): bool {
|
|
|
|
return $token->is([\T_COMMENT, \T_DOC_COMMENT])
|
|
|
|
&& substr($token->text, 0, 2) === '/*'
|
|
|
|
&& substr($token->text, -2) !== '*/';
|
2016-09-30 18:28:35 +02:00
|
|
|
}
|
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function postprocessTokens(ErrorHandler $errorHandler): void {
|
2022-06-04 17:34:48 +02:00
|
|
|
// This function reports errors (bad characters and unterminated comments) in the token
|
|
|
|
// array, and performs certain canonicalizations:
|
2021-07-09 16:08:46 +02:00
|
|
|
// * Use PHP 8.1 T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG and
|
|
|
|
// T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG tokens used to disambiguate intersection types.
|
2022-06-04 17:34:48 +02:00
|
|
|
// * Add a sentinel token with ID 0.
|
2016-09-30 18:28:35 +02:00
|
|
|
|
2019-06-30 11:43:48 +02:00
|
|
|
$numTokens = \count($this->tokens);
|
2022-06-04 17:34:48 +02:00
|
|
|
if ($numTokens === 0) {
|
|
|
|
// Empty input edge case: Just add the sentinel token.
|
|
|
|
$this->tokens[] = new Token(0, "\0", 1, 0);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-06-30 11:43:48 +02:00
|
|
|
for ($i = 0; $i < $numTokens; $i++) {
|
|
|
|
$token = $this->tokens[$i];
|
2022-06-04 17:34:48 +02:00
|
|
|
if ($token->id === \T_BAD_CHARACTER) {
|
|
|
|
$this->handleInvalidCharacter($token, $errorHandler);
|
2020-07-23 12:28:13 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
if ($token->id === \ord('&')) {
|
2021-07-09 16:08:46 +02:00
|
|
|
$next = $i + 1;
|
2022-06-04 17:34:48 +02:00
|
|
|
while (isset($this->tokens[$next]) && $this->tokens[$next]->id === \T_WHITESPACE) {
|
2021-07-09 16:08:46 +02:00
|
|
|
$next++;
|
|
|
|
}
|
|
|
|
$followedByVarOrVarArg = isset($this->tokens[$next]) &&
|
2022-06-04 17:34:48 +02:00
|
|
|
$this->tokens[$next]->is([\T_VARIABLE, \T_ELLIPSIS]);
|
|
|
|
$token->id = $followedByVarOrVarArg
|
|
|
|
? \T_AMPERSAND_FOLLOWED_BY_VAR_OR_VARARG
|
|
|
|
: \T_AMPERSAND_NOT_FOLLOWED_BY_VAR_OR_VARARG;
|
2021-07-09 16:08:46 +02:00
|
|
|
}
|
2011-06-03 17:44:23 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
// Check for unterminated comment
|
|
|
|
$lastToken = $this->tokens[$numTokens - 1];
|
|
|
|
if ($this->isUnterminatedComment($lastToken)) {
|
|
|
|
$errorHandler->handleError(new Error('Unterminated comment', [
|
|
|
|
'startLine' => $lastToken->line,
|
|
|
|
'endLine' => $lastToken->getEndLine(),
|
|
|
|
'startFilePos' => $lastToken->pos,
|
|
|
|
'endFilePos' => $lastToken->getEndPos(),
|
|
|
|
]));
|
2011-07-13 12:24:10 +02:00
|
|
|
}
|
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
// Add sentinel token.
|
|
|
|
$this->tokens[] = new Token(0, "\0", $lastToken->getEndLine(), $lastToken->getEndPos());
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
2012-05-05 17:34:27 +02:00
|
|
|
* Fetches the next token.
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
2014-11-27 20:38:14 +01:00
|
|
|
* The available attributes are determined by the 'usedAttributes' option, which can
|
|
|
|
* be specified in the constructor. The following attributes are supported:
|
|
|
|
*
|
2014-12-18 23:26:17 +01:00
|
|
|
* * 'comments' => Array of PhpParser\Comment or PhpParser\Comment\Doc instances,
|
|
|
|
* representing all comments that occurred between the previous
|
|
|
|
* non-discarded token and the current one.
|
2014-12-19 00:06:09 +01:00
|
|
|
* * 'startLine' => Line in which the node starts.
|
|
|
|
* * 'endLine' => Line in which the node ends.
|
|
|
|
* * 'startTokenPos' => Offset into the token array of the first token in the node.
|
|
|
|
* * 'endTokenPos' => Offset into the token array of the last token in the node.
|
|
|
|
* * 'startFilePos' => Offset into the code string of the first character that is part of the node.
|
2016-03-09 21:10:55 +01:00
|
|
|
* * 'endFilePos' => Offset into the code string of the last character that is part of the node.
|
2014-11-27 20:38:14 +01:00
|
|
|
*
|
2012-05-05 17:34:27 +02:00
|
|
|
* @param mixed $value Variable to store token content in
|
|
|
|
* @param mixed $startAttributes Variable to store start attributes in
|
|
|
|
* @param mixed $endAttributes Variable to store end attributes in
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return int Token id
|
|
|
|
*/
|
2022-06-04 17:34:48 +02:00
|
|
|
public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null): int {
|
2017-08-13 14:06:08 +02:00
|
|
|
$startAttributes = [];
|
|
|
|
$endAttributes = [];
|
2011-07-03 16:35:45 +02:00
|
|
|
|
2015-04-27 15:33:51 +02:00
|
|
|
while (1) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$token = $this->tokens[++$this->pos];
|
2011-06-03 22:02:02 +02:00
|
|
|
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeStartLineUsed) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$startAttributes['startLine'] = $token->line;
|
2016-04-02 07:58:29 +09:00
|
|
|
}
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeStartTokenPosUsed) {
|
2014-12-18 23:26:17 +01:00
|
|
|
$startAttributes['startTokenPos'] = $this->pos;
|
|
|
|
}
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeStartFilePosUsed) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$startAttributes['startFilePos'] = $token->pos;
|
2014-11-27 20:38:14 +01:00
|
|
|
}
|
2011-10-19 18:09:13 +02:00
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
$id = $token->id;
|
|
|
|
if (isset($this->dropTokens[$id])) {
|
|
|
|
if (\T_COMMENT === $id || \T_DOC_COMMENT === $id) {
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeCommentsUsed) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$comment = \T_DOC_COMMENT === $id
|
|
|
|
? new Comment\Doc($token->text, $token->line, $token->pos, $this->pos,
|
|
|
|
$token->getEndLine(), $token->getEndPos() - 1, $this->pos)
|
|
|
|
: new Comment($token->text, $token->line, $token->pos, $this->pos,
|
|
|
|
$token->getEndLine(), $token->getEndPos() - 1, $this->pos);
|
2016-04-02 07:54:01 +09:00
|
|
|
$startAttributes['comments'][] = $comment;
|
|
|
|
}
|
2011-07-13 13:27:14 +02:00
|
|
|
}
|
2016-04-02 07:58:29 +09:00
|
|
|
continue;
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
2016-04-02 07:58:29 +09:00
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
$value = $token->text;
|
|
|
|
if (\T_CLOSE_TAG === $token->id) {
|
|
|
|
$this->prevCloseTagHasNewline = false !== strpos($value, "\n")
|
|
|
|
|| false !== strpos($value, "\r");
|
|
|
|
} elseif (\T_INLINE_HTML === $token->id) {
|
|
|
|
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch the end line/pos from the next token (if available) instead of recomputing it.
|
|
|
|
$nextToken = $this->tokens[$this->pos + 1] ?? null;
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeEndLineUsed) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$endAttributes['endLine'] = $nextToken ? $nextToken->line : $token->getEndLine();
|
2016-04-02 07:58:29 +09:00
|
|
|
}
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeEndTokenPosUsed) {
|
2016-04-02 07:58:29 +09:00
|
|
|
$endAttributes['endTokenPos'] = $this->pos;
|
|
|
|
}
|
2019-05-12 15:26:26 +02:00
|
|
|
if ($this->attributeEndFilePosUsed) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$endAttributes['endFilePos'] = ($nextToken ? $nextToken->pos : $token->getEndPos()) - 1;
|
2016-04-02 07:58:29 +09:00
|
|
|
}
|
|
|
|
|
2022-06-19 18:05:52 +02:00
|
|
|
return $id;
|
2011-04-18 19:02:30 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 23:26:17 +01:00
|
|
|
/**
|
|
|
|
* Returns the token array for current code.
|
|
|
|
*
|
2022-06-04 17:34:48 +02:00
|
|
|
* The token array is in the same format as provided by the PhpToken::tokenize() method in
|
|
|
|
* PHP 8.0. The tokens are instances of PhpParser\Token, to abstract over a polyfill
|
|
|
|
* implementation in earlier PHP version.
|
2014-12-18 23:26:17 +01:00
|
|
|
*
|
2022-06-04 17:34:48 +02:00
|
|
|
* The token array is terminated by a sentinel token with token ID 0.
|
|
|
|
* The token array does not discard any tokens (i.e. whitespace and comments are included).
|
|
|
|
* The token position attributes are against this token array.
|
|
|
|
*
|
|
|
|
* @return Token[] Array of tokens
|
2014-12-18 23:26:17 +01:00
|
|
|
*/
|
2022-06-04 17:34:48 +02:00
|
|
|
public function getTokens(): array {
|
2014-12-18 23:26:17 +01:00
|
|
|
return $this->tokens;
|
|
|
|
}
|
|
|
|
|
2011-06-03 22:02:02 +02:00
|
|
|
/**
|
|
|
|
* Handles __halt_compiler() by returning the text after it.
|
|
|
|
*
|
|
|
|
* @return string Remaining text
|
|
|
|
*/
|
2022-06-04 17:34:48 +02:00
|
|
|
public function handleHaltCompiler(): string {
|
|
|
|
// Prevent the lexer from returning any further tokens.
|
|
|
|
$nextToken = $this->tokens[$this->pos + 1];
|
|
|
|
$this->pos = \count($this->tokens) - 2;
|
2011-06-03 22:02:02 +02:00
|
|
|
|
2022-06-04 17:34:48 +02:00
|
|
|
// Return text after __halt_compiler.
|
|
|
|
return $nextToken->id === \T_INLINE_HTML ? $nextToken->text : '';
|
2011-06-03 22:02:02 +02:00
|
|
|
}
|
2014-01-23 13:33:02 +01:00
|
|
|
}
|