Вход Регистрация
Файл: upload/core/vendor/twig/twig/src/ExpressionParser/Infix/AssignmentExpressionParser.php
Строк: 118
<?php

/*
 * This file is part of Twig.
 *
 * (c) Fabien Potencier
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace TwigExpressionParserInfix;

use 
TwigErrorSyntaxError;
use 
TwigExpressionParserInfixAssociativity;
use 
TwigNodeExpressionAbstractExpression;
use 
TwigNodeExpressionArrayExpression;
use 
TwigNodeExpressionBinaryAbstractBinary;
use 
TwigNodeExpressionBinaryObjectDestructuringSetBinary;
use 
TwigNodeExpressionBinarySequenceDestructuringSetBinary;
use 
TwigNodeExpressionBinarySetBinary;
use 
TwigNodeExpressionVariableContextVariable;
use 
TwigParser;
use 
TwigToken;

/**
 * @internal
 */
class AssignmentExpressionParser extends BinaryOperatorExpressionParser
{
    public function 
__construct(
        
string $name,
    ) {
        
parent::__construct(SetBinary::class, $name0InfixAssociativity::Right);
    }

    
/**
     * @return AbstractBinary
     */
    
public function parse(Parser $parserAbstractExpression $leftToken $token): AbstractExpression
    
{
        if (!
$left instanceof ContextVariable && !$left instanceof ArrayExpression) {
            throw new 
SyntaxError(sprintf('Cannot assign to "%s", only variables can be assigned.'$left::class), $token->getLine(), $parser->getStream()->getSourceContext());
        }
        
$right $parser->parseExpression(InfixAssociativity::Left === $this->getAssociativity() ? $this->getPrecedence() + $this->getPrecedence());
        
$right match ($this->getName()) {
            
'=' => $right,
            default => throw new 
LogicException(sprintf('Unknown operator: %s.'$this->getName())),
        };

        if (
$left instanceof ArrayExpression) {
            if (
$left->isSequence()) {
                return new 
SequenceDestructuringSetBinary($left$right$token->getLine());
            } else {
                return new 
ObjectDestructuringSetBinary($left$right$token->getLine());
            }
        } else {
            return new 
SetBinary($left$right$token->getLine());
        }
    }

    public function 
getDescription(): string
    
{
        return 
'Assignment operator';
    }
}
Онлайн: 1
Реклама