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

Breadcrumb

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

function ExpressionParser::parseMappingExpression

1 call to ExpressionParser::parseMappingExpression()
ExpressionParser::parseHashExpression in vendor/twig/twig/src/ExpressionParser.php

File

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

Class

ExpressionParser
Parses expressions.

Namespace

Twig

Code

public function parseMappingExpression() {
    $stream = $this->parser
        ->getStream();
    $stream->expect(Token::PUNCTUATION_TYPE, '{', 'A mapping element was expected');
    $node = new ArrayExpression([], $stream->getCurrent()
        ->getLine());
    $first = true;
    while (!$stream->test(Token::PUNCTUATION_TYPE, '}')) {
        if (!$first) {
            $stream->expect(Token::PUNCTUATION_TYPE, ',', 'A mapping value must be followed by a comma');
            // trailing ,?
            if ($stream->test(Token::PUNCTUATION_TYPE, '}')) {
                break;
            }
        }
        $first = false;
        if ($stream->nextIf(Token::SPREAD_TYPE)) {
            $value = $this->parseExpression();
            $value->setAttribute('spread', true);
            $node->addElement($value);
            continue;
        }
        // a mapping key can be:
        //
        //  * a number -- 12
        //  * a string -- 'a'
        //  * a name, which is equivalent to a string -- a
        //  * an expression, which must be enclosed in parentheses -- (1 + 2)
        if ($token = $stream->nextIf(Token::NAME_TYPE)) {
            $key = new ConstantExpression($token->getValue(), $token->getLine());
            // {a} is a shortcut for {a:a}
            if ($stream->test(Token::PUNCTUATION_TYPE, [
                ',',
                '}',
            ])) {
                $value = new ContextVariable($key->getAttribute('value'), $key->getTemplateLine());
                $node->addElement($value, $key);
                continue;
            }
        }
        elseif (($token = $stream->nextIf(Token::STRING_TYPE)) || ($token = $stream->nextIf(Token::NUMBER_TYPE))) {
            $key = new ConstantExpression($token->getValue(), $token->getLine());
        }
        elseif ($stream->test(Token::PUNCTUATION_TYPE, '(')) {
            $key = $this->parseExpression();
        }
        else {
            $current = $stream->getCurrent();
            throw new SyntaxError(\sprintf('A mapping key must be a quoted string, a number, a name, or an expression enclosed in parentheses (unexpected token "%s" of value "%s".', Token::typeToEnglish($current->getType()), $current->getValue()), $current->getLine(), $stream->getSourceContext());
        }
        $stream->expect(Token::PUNCTUATION_TYPE, ':', 'A mapping key must be followed by a colon (:)');
        $value = $this->parseExpression();
        $node->addElement($value, $key);
    }
    $stream->expect(Token::PUNCTUATION_TYPE, '}', 'An opened mapping is not properly closed');
    return $node;
}

API Navigation

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