Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. ParserAbstract.php

function ParserAbstract::parse

Same name in this branch
  1. 11.1.x vendor/mck89/peast/lib/Peast/Syntax/ParserAbstract.php \Peast\Syntax\ParserAbstract::parse()

Parses PHP code into a node tree.

If a non-throwing error handler is used, the parser will continue parsing after an error occurred and attempt to build a partial AST.

Parameters

string $code The source code to parse:

ErrorHandler|null $errorHandler Error handler to use for lexer/parser errors, defaults: to ErrorHandler\Throwing.

Return value

Node\Stmt[]|null Array of statements (or null non-throwing error handler is used and the parser was unable to recover from an error).

Overrides Parser::parse

File

vendor/nikic/php-parser/lib/PhpParser/ParserAbstract.php, line 180

Class

ParserAbstract

Namespace

PhpParser

Code

public function parse(string $code, ?ErrorHandler $errorHandler = null) : ?array {
    $this->errorHandler = $errorHandler ?: new ErrorHandler\Throwing();
    $this->createdArrays = new \SplObjectStorage();
    $this->tokens = $this->lexer
        ->tokenize($code, $this->errorHandler);
    $result = $this->doParse();
    // Report errors for any empty elements used inside arrays. This is delayed until after the main parse,
    // because we don't know a priori whether a given array expression will be used in a destructuring context
    // or not.
    foreach ($this->createdArrays as $node) {
        foreach ($node->items as $item) {
            if ($item->value instanceof Expr\Error) {
                $this->errorHandler
                    ->handleError(new Error('Cannot use empty array elements in arrays', $item->getAttributes()));
            }
        }
    }
    // Clear out some of the interior state, so we don't hold onto unnecessary
    // memory between uses of the parser
    $this->tokenStartStack = [];
    $this->tokenEndStack = [];
    $this->semStack = [];
    $this->semValue = null;
    $this->createdArrays = null;
    if ($result !== null) {
        $traverser = new NodeTraverser(new CommentAnnotatingVisitor($this->tokens));
        $traverser->traverse($result);
    }
    return $result;
}

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal