PhpParser\Lexer::getNextToken PHP Method

getNextToken() public method

The available attributes are determined by the 'usedAttributes' option, which can be specified in the constructor. The following attributes are supported: * '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. * '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. * 'endFilePos' => Offset into the code string of the last character that is part of the node.
public getNextToken ( mixed &$value = null, mixed &$startAttributes = null, mixed &$endAttributes = null ) : integer
$value mixed Variable to store token content in
$startAttributes mixed Variable to store start attributes in
$endAttributes mixed Variable to store end attributes in
return integer Token id
    public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null)
    {
        $startAttributes = array();
        $endAttributes = array();
        while (1) {
            if (isset($this->tokens[++$this->pos])) {
                $token = $this->tokens[$this->pos];
            } else {
                // EOF token with ID 0
                $token = "";
            }
            if (isset($this->usedAttributes['startLine'])) {
                $startAttributes['startLine'] = $this->line;
            }
            if (isset($this->usedAttributes['startTokenPos'])) {
                $startAttributes['startTokenPos'] = $this->pos;
            }
            if (isset($this->usedAttributes['startFilePos'])) {
                $startAttributes['startFilePos'] = $this->filePos;
            }
            if (\is_string($token)) {
                $value = $token;
                if (isset($token[1])) {
                    // bug in token_get_all
                    $this->filePos += 2;
                    $id = ord('"');
                } else {
                    $this->filePos += 1;
                    $id = ord($token);
                }
            } elseif (!isset($this->dropTokens[$token[0]])) {
                $value = $token[1];
                $id = $this->tokenMap[$token[0]];
                if (T_CLOSE_TAG === $token[0]) {
                    $this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
                } else {
                    if (T_INLINE_HTML === $token[0]) {
                        $startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
                    }
                }
                $this->line += substr_count($value, "\n");
                $this->filePos += \strlen($value);
            } else {
                if (T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0]) {
                    if (isset($this->usedAttributes['comments'])) {
                        $comment = T_DOC_COMMENT === $token[0] ? new Comment\Doc($token[1], $this->line, $this->filePos) : new Comment($token[1], $this->line, $this->filePos);
                        $startAttributes['comments'][] = $comment;
                    }
                }
                $this->line += substr_count($token[1], "\n");
                $this->filePos += \strlen($token[1]);
                continue;
            }
            if (isset($this->usedAttributes['endLine'])) {
                $endAttributes['endLine'] = $this->line;
            }
            if (isset($this->usedAttributes['endTokenPos'])) {
                $endAttributes['endTokenPos'] = $this->pos;
            }
            if (isset($this->usedAttributes['endFilePos'])) {
                $endAttributes['endFilePos'] = $this->filePos - 1;
            }
            return $id;
        }
        throw new \RuntimeException('Reached end of lexer loop');
    }

Usage Example

Example #1
0
 /**
  * Retrieves the next token and determines the associated attributes and
  * returns the token id.
  *
  * @param string   $value
  * @param string[] $startAttributes
  * @param string[] $endAttributes
  *
  * @return int
  */
 public function getNextToken(&$value = null, &$startAttributes = null, &$endAttributes = null)
 {
     $tokenId = parent::getNextToken($value, $startAttributes, $endAttributes);
     if ($this->isTokenScalar($tokenId)) {
         // store original value because the value itself will be interpreted
         // by PHP_Parser and we want the unformatted value
         $endAttributes['originalValue'] = $value;
     }
     return $tokenId;
 }
All Usage Examples Of PhpParser\Lexer::getNextToken