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

Breadcrumb

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

function ExpressionParser::parseOnlyArguments

4 calls to ExpressionParser::parseOnlyArguments()
ExpressionParser::createArguments in vendor/twig/twig/src/ExpressionParser.php
ExpressionParser::getFunctionNode in vendor/twig/twig/src/ExpressionParser.php
ExpressionParser::parseFilterExpressionRaw in vendor/twig/twig/src/ExpressionParser.php
ExpressionParser::parseTestExpression in vendor/twig/twig/src/ExpressionParser.php

File

vendor/twig/twig/src/ExpressionParser.php, line 880

Class

ExpressionParser
Parses expressions.

Namespace

Twig

Code

public function parseOnlyArguments() {
    $args = [];
    $stream = $this->parser
        ->getStream();
    $stream->expect(Token::PUNCTUATION_TYPE, '(', 'A list of arguments must begin with an opening parenthesis');
    $hasSpread = false;
    while (!$stream->test(Token::PUNCTUATION_TYPE, ')')) {
        if ($args) {
            $stream->expect(Token::PUNCTUATION_TYPE, ',', 'Arguments must be separated by a comma');
            // if the comma above was a trailing comma, early exit the argument parse loop
            if ($stream->test(Token::PUNCTUATION_TYPE, ')')) {
                break;
            }
        }
        if ($stream->nextIf(Token::SPREAD_TYPE)) {
            $hasSpread = true;
            $value = new SpreadUnary($this->parseExpression(), $stream->getCurrent()
                ->getLine());
        }
        elseif ($hasSpread) {
            throw new SyntaxError('Normal arguments must be placed before argument unpacking.', $stream->getCurrent()
                ->getLine(), $stream->getSourceContext());
        }
        else {
            $value = $this->parseExpression();
        }
        $name = null;
        if (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || ($token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':'))) {
            if (!$value instanceof NameExpression) {
                throw new SyntaxError(\sprintf('A parameter name must be a string, "%s" given.', \get_class($value)), $token->getLine(), $stream->getSourceContext());
            }
            $name = $value->getAttribute('name');
            $value = $this->parseExpression();
        }
        if (null === $name) {
            $args[] = $value;
        }
        else {
            $args[$name] = $value;
        }
    }
    $stream->expect(Token::PUNCTUATION_TYPE, ')', 'A list of arguments must be closed by a parenthesis');
    return new Nodes($args);
}

API Navigation

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