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

Breadcrumb

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

function ExpressionParser::parsePrimaryExpression

1 call to ExpressionParser::parsePrimaryExpression()
ExpressionParser::getPrimary in vendor/twig/twig/src/ExpressionParser.php

File

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

Class

ExpressionParser
Parses expressions.

Namespace

Twig

Code

public function parsePrimaryExpression() {
    $token = $this->parser
        ->getCurrentToken();
    switch ($token->getType()) {
        case Token::NAME_TYPE:
            $this->parser
                ->getStream()
                ->next();
            switch ($token->getValue()) {
                case 'true':
                case 'TRUE':
                    $node = new ConstantExpression(true, $token->getLine());
                    break;
                case 'false':
                case 'FALSE':
                    $node = new ConstantExpression(false, $token->getLine());
                    break;
                case 'none':
                case 'NONE':
                case 'null':
                case 'NULL':
                    $node = new ConstantExpression(null, $token->getLine());
                    break;
                default:
                    if ('(' === $this->parser
                        ->getCurrentToken()
                        ->getValue()) {
                        $node = $this->getFunctionNode($token->getValue(), $token->getLine());
                    }
                    else {
                        $node = new ContextVariable($token->getValue(), $token->getLine());
                    }
            }
            break;
        case Token::NUMBER_TYPE:
            $this->parser
                ->getStream()
                ->next();
            $node = new ConstantExpression($token->getValue(), $token->getLine());
            break;
        case Token::STRING_TYPE:
        case Token::INTERPOLATION_START_TYPE:
            $node = $this->parseStringExpression();
            break;
        case Token::PUNCTUATION_TYPE:
            $node = match ($token->getValue()) {    '[' => $this->parseSequenceExpression(),
                '{' => $this->parseMappingExpression(),
                default => throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser
                    ->getStream()
                    ->getSourceContext()),
            
            };
            break;
        case Token::OPERATOR_TYPE:
            if (preg_match(Lexer::REGEX_NAME, $token->getValue(), $matches) && $matches[0] == $token->getValue()) {
                // in this context, string operators are variable names
                $this->parser
                    ->getStream()
                    ->next();
                $node = new ContextVariable($token->getValue(), $token->getLine());
                break;
            }
            if ('=' === $token->getValue() && ('==' === $this->parser
                ->getStream()
                ->look(-1)
                ->getValue() || '!=' === $this->parser
                ->getStream()
                ->look(-1)
                ->getValue())) {
                throw new SyntaxError(\sprintf('Unexpected operator of value "%s". Did you try to use "===" or "!==" for strict comparison? Use "is same as(value)" instead.', $token->getValue()), $token->getLine(), $this->parser
                    ->getStream()
                    ->getSourceContext());
            }
        // no break
        default:
            throw new SyntaxError(\sprintf('Unexpected token "%s" of value "%s".', Token::typeToEnglish($token->getType()), $token->getValue()), $token->getLine(), $this->parser
                ->getStream()
                ->getSourceContext());
    }
    return $this->parsePostfixExpression($node);
}

API Navigation

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