1 <?php declare(strict_types=1);
5 use PhpParser\Parser\Tokens;
14 protected $prevCloseTagHasNewline;
17 protected $dropTokens;
19 protected $usedAttributes;
24 * @param array $options Options array. Currently only the 'usedAttributes' option is supported,
25 * which is an array of attributes to add to the AST nodes. Possible
26 * attributes are: 'comments', 'startLine', 'endLine', 'startTokenPos',
27 * 'endTokenPos', 'startFilePos', 'endFilePos'. The option defaults to the
28 * first three. For more info see getNextToken() docs.
30 public function __construct(array $options = []) {
31 // map from internal tokens to PhpParser tokens
32 $this->tokenMap = $this->createTokenMap();
34 // map of tokens to drop while lexing (the map is only used for isset lookup,
35 // that's why the value is simply set to 1; the value is never actually used.)
36 $this->dropTokens = array_fill_keys(
37 [\T_WHITESPACE, \T_OPEN_TAG, \T_COMMENT, \T_DOC_COMMENT], 1
40 // the usedAttributes member is a map of the used attribute names to a dummy
41 // value (here "true")
43 'usedAttributes' => ['comments', 'startLine', 'endLine'],
45 $this->usedAttributes = array_fill_keys($options['usedAttributes'], true);
49 * Initializes the lexer for lexing the provided source code.
51 * This function does not throw if lexing errors occur. Instead, errors may be retrieved using
52 * the getErrors() method.
54 * @param string $code The source code to lex
55 * @param ErrorHandler|null $errorHandler Error handler to use for lexing errors. Defaults to
56 * ErrorHandler\Throwing
58 public function startLexing(string $code, ErrorHandler $errorHandler = null) {
59 if (null === $errorHandler) {
60 $errorHandler = new ErrorHandler\Throwing();
63 $this->code = $code; // keep the code around for __halt_compiler() handling
68 // If inline HTML occurs without preceding code, treat it as if it had a leading newline.
69 // This ensures proper composability, because having a newline is the "safe" assumption.
70 $this->prevCloseTagHasNewline = true;
72 $scream = ini_set('xdebug.scream', '0');
75 $this->tokens = @token_get_all($code);
76 $this->handleErrors($errorHandler);
78 if (false !== $scream) {
79 ini_set('xdebug.scream', $scream);
83 private function handleInvalidCharacterRange($start, $end, $line, ErrorHandler $errorHandler) {
84 for ($i = $start; $i < $end; $i++) {
85 $chr = $this->code[$i];
86 if ($chr === 'b' || $chr === 'B') {
87 // HHVM does not treat b" tokens correctly, so ignore these
92 // PHP cuts error message after null byte, so need special case
93 $errorMsg = 'Unexpected null byte';
96 'Unexpected character "%s" (ASCII %d)', $chr, ord($chr)
100 $errorHandler->handleError(new Error($errorMsg, [
101 'startLine' => $line,
103 'startFilePos' => $i,
110 * Check whether comment token is unterminated.
114 private function isUnterminatedComment($token) : bool {
115 return ($token[0] === \T_COMMENT || $token[0] === \T_DOC_COMMENT)
116 && substr($token[1], 0, 2) === '/*'
117 && substr($token[1], -2) !== '*/';
121 * Check whether an error *may* have occurred during tokenization.
125 private function errorMayHaveOccurred() : bool {
126 if (defined('HHVM_VERSION')) {
127 // In HHVM token_get_all() does not throw warnings, so we need to conservatively
128 // assume that an error occurred
132 return null !== error_get_last();
135 protected function handleErrors(ErrorHandler $errorHandler) {
136 if (!$this->errorMayHaveOccurred()) {
140 // PHP's error handling for token_get_all() is rather bad, so if we want detailed
141 // error information we need to compute it ourselves. Invalid character errors are
142 // detected by finding "gaps" in the token array. Unterminated comments are detected
143 // by checking if a trailing comment has a "*/" at the end.
147 foreach ($this->tokens as $token) {
148 $tokenValue = \is_string($token) ? $token : $token[1];
149 $tokenLen = \strlen($tokenValue);
151 if (substr($this->code, $filePos, $tokenLen) !== $tokenValue) {
152 // Something is missing, must be an invalid character
153 $nextFilePos = strpos($this->code, $tokenValue, $filePos);
154 $this->handleInvalidCharacterRange(
155 $filePos, $nextFilePos, $line, $errorHandler);
156 $filePos = (int) $nextFilePos;
159 $filePos += $tokenLen;
160 $line += substr_count($tokenValue, "\n");
163 if ($filePos !== \strlen($this->code)) {
164 if (substr($this->code, $filePos, 2) === '/*') {
165 // Unlike PHP, HHVM will drop unterminated comments entirely
166 $comment = substr($this->code, $filePos);
167 $errorHandler->handleError(new Error('Unterminated comment', [
168 'startLine' => $line,
169 'endLine' => $line + substr_count($comment, "\n"),
170 'startFilePos' => $filePos,
171 'endFilePos' => $filePos + \strlen($comment),
174 // Emulate the PHP behavior
175 $isDocComment = isset($comment[3]) && $comment[3] === '*';
176 $this->tokens[] = [$isDocComment ? \T_DOC_COMMENT : \T_COMMENT, $comment, $line];
178 // Invalid characters at the end of the input
179 $this->handleInvalidCharacterRange(
180 $filePos, \strlen($this->code), $line, $errorHandler);
185 if (count($this->tokens) > 0) {
186 // Check for unterminated comment
187 $lastToken = $this->tokens[count($this->tokens) - 1];
188 if ($this->isUnterminatedComment($lastToken)) {
189 $errorHandler->handleError(new Error('Unterminated comment', [
190 'startLine' => $line - substr_count($lastToken[1], "\n"),
192 'startFilePos' => $filePos - \strlen($lastToken[1]),
193 'endFilePos' => $filePos,
200 * Fetches the next token.
202 * The available attributes are determined by the 'usedAttributes' option, which can
203 * be specified in the constructor. The following attributes are supported:
205 * * 'comments' => Array of PhpParser\Comment or PhpParser\Comment\Doc instances,
206 * representing all comments that occurred between the previous
207 * non-discarded token and the current one.
208 * * 'startLine' => Line in which the node starts.
209 * * 'endLine' => Line in which the node ends.
210 * * 'startTokenPos' => Offset into the token array of the first token in the node.
211 * * 'endTokenPos' => Offset into the token array of the last token in the node.
212 * * 'startFilePos' => Offset into the code string of the first character that is part of the node.
213 * * 'endFilePos' => Offset into the code string of the last character that is part of the node.
215 * @param mixed $value Variable to store token content in
216 * @param mixed $startAttributes Variable to store start attributes in
217 * @param mixed $endAttributes Variable to store end attributes in
219 * @return int Token id
221 public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null) : int {
222 $startAttributes = [];
226 if (isset($this->tokens[++$this->pos])) {
227 $token = $this->tokens[$this->pos];
229 // EOF token with ID 0
233 if (isset($this->usedAttributes['startLine'])) {
234 $startAttributes['startLine'] = $this->line;
236 if (isset($this->usedAttributes['startTokenPos'])) {
237 $startAttributes['startTokenPos'] = $this->pos;
239 if (isset($this->usedAttributes['startFilePos'])) {
240 $startAttributes['startFilePos'] = $this->filePos;
243 if (\is_string($token)) {
245 if (isset($token[1])) {
246 // bug in token_get_all
253 } elseif (!isset($this->dropTokens[$token[0]])) {
255 $id = $this->tokenMap[$token[0]];
256 if (\T_CLOSE_TAG === $token[0]) {
257 $this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
258 } elseif (\T_INLINE_HTML === $token[0]) {
259 $startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
262 $this->line += substr_count($value, "\n");
263 $this->filePos += \strlen($value);
265 if (\T_COMMENT === $token[0] || \T_DOC_COMMENT === $token[0]) {
266 if (isset($this->usedAttributes['comments'])) {
267 $comment = \T_DOC_COMMENT === $token[0]
268 ? new Comment\Doc($token[1], $this->line, $this->filePos, $this->pos)
269 : new Comment($token[1], $this->line, $this->filePos, $this->pos);
270 $startAttributes['comments'][] = $comment;
274 $this->line += substr_count($token[1], "\n");
275 $this->filePos += \strlen($token[1]);
279 if (isset($this->usedAttributes['endLine'])) {
280 $endAttributes['endLine'] = $this->line;
282 if (isset($this->usedAttributes['endTokenPos'])) {
283 $endAttributes['endTokenPos'] = $this->pos;
285 if (isset($this->usedAttributes['endFilePos'])) {
286 $endAttributes['endFilePos'] = $this->filePos - 1;
292 throw new \RuntimeException('Reached end of lexer loop');
296 * Returns the token array for current code.
298 * The token array is in the same format as provided by the
299 * token_get_all() function and does not discard tokens (i.e.
300 * whitespace and comments are included). The token position
301 * attributes are against this token array.
303 * @return array Array of tokens in token_get_all() format
305 public function getTokens() : array {
306 return $this->tokens;
310 * Handles __halt_compiler() by returning the text after it.
312 * @return string Remaining text
314 public function handleHaltCompiler() : string {
315 // text after T_HALT_COMPILER, still including ();
316 $textAfter = substr($this->code, $this->filePos);
318 // ensure that it is followed by ();
319 // this simplifies the situation, by not allowing any comments
320 // in between of the tokens.
321 if (!preg_match('~^\s*\(\s*\)\s*(?:;|\?>\r?\n?)~', $textAfter, $matches)) {
322 throw new Error('__HALT_COMPILER must be followed by "();"');
325 // prevent the lexer from returning any further tokens
326 $this->pos = count($this->tokens);
328 // return with (); removed
329 return substr($textAfter, strlen($matches[0]));
333 * Creates the token map.
335 * The token map maps the PHP internal token identifiers
336 * to the identifiers used by the Parser. Additionally it
337 * maps T_OPEN_TAG_WITH_ECHO to T_ECHO and T_CLOSE_TAG to ';'.
339 * @return array The token map
341 protected function createTokenMap() : array {
344 // 256 is the minimum possible token number, as everything below
345 // it is an ASCII value
346 for ($i = 256; $i < 1000; ++$i) {
347 if (\T_DOUBLE_COLON === $i) {
348 // T_DOUBLE_COLON is equivalent to T_PAAMAYIM_NEKUDOTAYIM
349 $tokenMap[$i] = Tokens::T_PAAMAYIM_NEKUDOTAYIM;
350 } elseif(\T_OPEN_TAG_WITH_ECHO === $i) {
351 // T_OPEN_TAG_WITH_ECHO with dropped T_OPEN_TAG results in T_ECHO
352 $tokenMap[$i] = Tokens::T_ECHO;
353 } elseif(\T_CLOSE_TAG === $i) {
354 // T_CLOSE_TAG is equivalent to ';'
355 $tokenMap[$i] = ord(';');
356 } elseif ('UNKNOWN' !== $name = token_name($i)) {
357 if ('T_HASHBANG' === $name) {
358 // HHVM uses a special token for #! hashbang lines
359 $tokenMap[$i] = Tokens::T_INLINE_HTML;
360 } elseif (defined($name = Tokens::class . '::' . $name)) {
361 // Other tokens can be mapped directly
362 $tokenMap[$i] = constant($name);
367 // HHVM uses a special token for numbers that overflow to double
368 if (defined('T_ONUMBER')) {
369 $tokenMap[\T_ONUMBER] = Tokens::T_DNUMBER;
371 // HHVM also has a separate token for the __COMPILER_HALT_OFFSET__ constant
372 if (defined('T_COMPILER_HALT_OFFSET')) {
373 $tokenMap[\T_COMPILER_HALT_OFFSET] = Tokens::T_STRING;