mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-19 05:46:41 +00:00
Move Operators to ExpressionParsers, deprecate ExpressionParser
This commit is contained in:
@@ -0,0 +1,81 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\Unary\SpreadUnary;
|
||||
use Twig\Node\Expression\Variable\ContextVariable;
|
||||
use Twig\Node\Expression\Variable\LocalVariable;
|
||||
use Twig\Node\Nodes;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
trait ArgumentsTrait
|
||||
{
|
||||
private function parseCallableArguments(Parser $parser, int $line, bool $parseOpenParenthesis = true): ArrayExpression
|
||||
{
|
||||
$arguments = new ArrayExpression([], $line);
|
||||
foreach ($this->parseNamedArguments($parser, $parseOpenParenthesis) as $k => $n) {
|
||||
$arguments->addElement($n, new LocalVariable($k, $line));
|
||||
}
|
||||
|
||||
return $arguments;
|
||||
}
|
||||
|
||||
private function parseNamedArguments(Parser $parser, bool $parseOpenParenthesis = true): Nodes
|
||||
{
|
||||
$args = [];
|
||||
$stream = $parser->getStream();
|
||||
if ($parseOpenParenthesis) {
|
||||
$stream->expect(Token::OPERATOR_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($parser->parseExpression(), $stream->getCurrent()->getLine());
|
||||
} elseif ($hasSpread) {
|
||||
throw new SyntaxError('Normal arguments must be placed before argument unpacking.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
|
||||
} else {
|
||||
$value = $parser->parseExpression();
|
||||
}
|
||||
|
||||
$name = null;
|
||||
if (($token = $stream->nextIf(Token::OPERATOR_TYPE, '=')) || ($token = $stream->nextIf(Token::PUNCTUATION_TYPE, ':'))) {
|
||||
if (!$value instanceof ContextVariable) {
|
||||
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 = $parser->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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ArrowFunctionExpression;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ArrowExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
// As the expression of the arrow function is independent from the current precedence, we want a precedence of 0
|
||||
return new ArrowFunctionExpression($parser->parseExpression(), $expr, $token->getLine());
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '=>';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 250;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\ExpressionParser\PrecedenceChange;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\Binary\AbstractBinary;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class BinaryOperatorExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
public function __construct(
|
||||
/** @var class-string<AbstractBinary> */
|
||||
private string $nodeClass,
|
||||
private string $name,
|
||||
private int $precedence,
|
||||
private InfixAssociativity $associativity = InfixAssociativity::Left,
|
||||
private ?PrecedenceChange $precedenceChange = null,
|
||||
private array $aliases = [],
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* @return AbstractBinary
|
||||
*/
|
||||
public function parse(Parser $parser, AbstractExpression $left, Token $token): AbstractExpression
|
||||
{
|
||||
$right = $parser->parseExpression(InfixAssociativity::Left === $this->getAssociativity() ? $this->getPrecedence() + 1 : $this->getPrecedence());
|
||||
|
||||
return new ($this->nodeClass)($left, $right, $token->getLine());
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return $this->associativity;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return $this->precedence;
|
||||
}
|
||||
|
||||
public function getPrecedenceChange(): ?PrecedenceChange
|
||||
{
|
||||
return $this->precedenceChange;
|
||||
}
|
||||
|
||||
public function getAliases(): array
|
||||
{
|
||||
return $this->aliases;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\Ternary\ConditionalTernary;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class ConditionalTernaryExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
public function parse(Parser $parser, AbstractExpression $left, Token $token): AbstractExpression
|
||||
{
|
||||
$then = $parser->parseExpression($this->getPrecedence());
|
||||
if ($parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, ':')) {
|
||||
// Ternary operator (expr ? expr2 : expr3)
|
||||
$else = $parser->parseExpression($this->getPrecedence());
|
||||
} else {
|
||||
// Ternary without else (expr ? expr2)
|
||||
$else = new ConstantExpression('', $token->getLine());
|
||||
}
|
||||
|
||||
return new ConditionalTernary($left, $then, $else, $token->getLine());
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '?';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Lexer;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\GetAttrExpression;
|
||||
use Twig\Node\Expression\MacroReferenceExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Expression\Variable\TemplateVariable;
|
||||
use Twig\Parser;
|
||||
use Twig\Template;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class DotExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
use ArgumentsTrait;
|
||||
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
$stream = $parser->getStream();
|
||||
$token = $stream->getCurrent();
|
||||
$lineno = $token->getLine();
|
||||
$arguments = new ArrayExpression([], $lineno);
|
||||
$type = Template::ANY_CALL;
|
||||
|
||||
if ($stream->nextIf(Token::OPERATOR_TYPE, '(')) {
|
||||
$attribute = $parser->parseExpression();
|
||||
$stream->expect(Token::PUNCTUATION_TYPE, ')');
|
||||
} else {
|
||||
$token = $stream->next();
|
||||
if (
|
||||
$token->test(Token::NAME_TYPE)
|
||||
|| $token->test(Token::NUMBER_TYPE)
|
||||
|| ($token->test(Token::OPERATOR_TYPE) && preg_match(Lexer::REGEX_NAME, $token->getValue()))
|
||||
) {
|
||||
$attribute = new ConstantExpression($token->getValue(), $token->getLine());
|
||||
} else {
|
||||
throw new SyntaxError(\sprintf('Expected name or number, got value "%s" of type %s.', $token->getValue(), $token->toEnglish()), $token->getLine(), $stream->getSourceContext());
|
||||
}
|
||||
}
|
||||
|
||||
if ($stream->test(Token::OPERATOR_TYPE, '(')) {
|
||||
$type = Template::METHOD_CALL;
|
||||
$arguments = $this->parseCallableArguments($parser, $token->getLine());
|
||||
}
|
||||
|
||||
if (
|
||||
$expr instanceof NameExpression
|
||||
&& (
|
||||
null !== $parser->getImportedSymbol('template', $expr->getAttribute('name'))
|
||||
|| '_self' === $expr->getAttribute('name') && $attribute instanceof ConstantExpression
|
||||
)
|
||||
) {
|
||||
return new MacroReferenceExpression(new TemplateVariable($expr->getAttribute('name'), $expr->getTemplateLine()), 'macro_'.$attribute->getAttribute('value'), $arguments, $expr->getTemplateLine());
|
||||
}
|
||||
|
||||
return new GetAttrExpression($expr, $attribute, $arguments, $type, $lineno);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '.';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 300;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,73 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Attribute\FirstClassTwigCallableReady;
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\EmptyNode;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class FilterExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
use ArgumentsTrait;
|
||||
|
||||
private $readyNodes = [];
|
||||
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
$stream = $parser->getStream();
|
||||
$token = $stream->expect(Token::NAME_TYPE);
|
||||
$line = $token->getLine();
|
||||
|
||||
if (!$stream->test(Token::OPERATOR_TYPE, '(')) {
|
||||
$arguments = new EmptyNode();
|
||||
} else {
|
||||
$arguments = $this->parseNamedArguments($parser);
|
||||
}
|
||||
|
||||
$filter = $parser->getFilter($token->getValue(), $line);
|
||||
|
||||
$ready = true;
|
||||
if (!isset($this->readyNodes[$class = $filter->getNodeClass()])) {
|
||||
$this->readyNodes[$class] = (bool) (new \ReflectionClass($class))->getConstructor()->getAttributes(FirstClassTwigCallableReady::class);
|
||||
}
|
||||
|
||||
if (!$ready = $this->readyNodes[$class]) {
|
||||
trigger_deprecation('twig/twig', '3.12', 'Twig node "%s" is not marked as ready for passing a "TwigFilter" in the constructor instead of its name; please update your code and then add #[FirstClassTwigCallableReady] attribute to the constructor.', $class);
|
||||
}
|
||||
|
||||
return new $class($expr, $ready ? $filter : new ConstantExpression($filter->getName(), $line), $arguments, $line);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '|';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 300;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,84 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Attribute\FirstClassTwigCallableReady;
|
||||
use Twig\Error\SyntaxError;
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\EmptyNode;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\MacroReferenceExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class FunctionExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
use ArgumentsTrait;
|
||||
|
||||
private $readyNodes = [];
|
||||
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
$line = $token->getLine();
|
||||
if (!$expr instanceof NameExpression) {
|
||||
throw new SyntaxError('Function name must be an identifier.', $line, $parser->getStream()->getSourceContext());
|
||||
}
|
||||
|
||||
$name = $expr->getAttribute('name');
|
||||
|
||||
if (null !== $alias = $parser->getImportedSymbol('function', $name)) {
|
||||
return new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], $this->parseCallableArguments($parser, $line, false), $line);
|
||||
}
|
||||
|
||||
$args = $this->parseNamedArguments($parser, false);
|
||||
|
||||
$function = $parser->getFunction($name, $line);
|
||||
|
||||
if ($function->getParserCallable()) {
|
||||
$fakeNode = new EmptyNode($line);
|
||||
$fakeNode->setSourceContext($parser->getStream()->getSourceContext());
|
||||
|
||||
return ($function->getParserCallable())($parser, $fakeNode, $args, $line);
|
||||
}
|
||||
|
||||
if (!isset($this->readyNodes[$class = $function->getNodeClass()])) {
|
||||
$this->readyNodes[$class] = (bool) (new \ReflectionClass($class))->getConstructor()->getAttributes(FirstClassTwigCallableReady::class);
|
||||
}
|
||||
|
||||
if (!$ready = $this->readyNodes[$class]) {
|
||||
trigger_deprecation('twig/twig', '3.12', 'Twig node "%s" is not marked as ready for passing a "TwigFunction" in the constructor instead of its name; please update your code and then add #[FirstClassTwigCallableReady] attribute to the constructor.', $class);
|
||||
}
|
||||
|
||||
return new $class($ready ? $function : $function->getName(), $args, $line);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '(';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 300;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,78 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Attribute\FirstClassTwigCallableReady;
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\MacroReferenceExpression;
|
||||
use Twig\Node\Expression\NameExpression;
|
||||
use Twig\Node\Nodes;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
use Twig\TwigTest;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
class IsExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
use ArgumentsTrait;
|
||||
|
||||
private $readyNodes = [];
|
||||
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
$stream = $parser->getStream();
|
||||
$test = $parser->getTest($token->getLine());
|
||||
|
||||
$arguments = null;
|
||||
if ($stream->test(Token::OPERATOR_TYPE, '(')) {
|
||||
$arguments = $this->parseNamedArguments($parser);
|
||||
} elseif ($test->hasOneMandatoryArgument()) {
|
||||
$arguments = new Nodes([0 => $parser->parseExpression($this->getPrecedence())]);
|
||||
}
|
||||
|
||||
if ('defined' === $test->getName() && $expr instanceof NameExpression && null !== $alias = $parser->getImportedSymbol('function', $expr->getAttribute('name'))) {
|
||||
$expr = new MacroReferenceExpression($alias['node']->getNode('var'), $alias['name'], new ArrayExpression([], $expr->getTemplateLine()), $expr->getTemplateLine());
|
||||
}
|
||||
|
||||
$ready = $test instanceof TwigTest;
|
||||
if (!isset($this->readyNodes[$class = $test->getNodeClass()])) {
|
||||
$this->readyNodes[$class] = (bool) (new \ReflectionClass($class))->getConstructor()->getAttributes(FirstClassTwigCallableReady::class);
|
||||
}
|
||||
|
||||
if (!$ready = $this->readyNodes[$class]) {
|
||||
trigger_deprecation('twig/twig', '3.12', 'Twig node "%s" is not marked as ready for passing a "TwigTest" in the constructor instead of its name; please update your code and then add #[FirstClassTwigCallableReady] attribute to the constructor.', $class);
|
||||
}
|
||||
|
||||
return new $class($expr, $ready ? $test : $test->getName(), $arguments, $stream->getCurrent()->getLine());
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 100;
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'is';
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\Unary\NotUnary;
|
||||
use Twig\Parser;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class IsNotExpressionParser extends IsExpressionParser
|
||||
{
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
return new NotUnary(parent::parse($parser, $expr, $token), $token->getLine());
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return 'is not';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
<?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 Twig\ExpressionParser\Infix;
|
||||
|
||||
use Twig\ExpressionParser\AbstractExpressionParser;
|
||||
use Twig\ExpressionParser\InfixAssociativity;
|
||||
use Twig\ExpressionParser\InfixExpressionParserInterface;
|
||||
use Twig\Node\Expression\AbstractExpression;
|
||||
use Twig\Node\Expression\ArrayExpression;
|
||||
use Twig\Node\Expression\ConstantExpression;
|
||||
use Twig\Node\Expression\GetAttrExpression;
|
||||
use Twig\Node\Nodes;
|
||||
use Twig\Parser;
|
||||
use Twig\Template;
|
||||
use Twig\Token;
|
||||
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
final class SquareBracketExpressionParser extends AbstractExpressionParser implements InfixExpressionParserInterface
|
||||
{
|
||||
public function parse(Parser $parser, AbstractExpression $expr, Token $token): AbstractExpression
|
||||
{
|
||||
$stream = $parser->getStream();
|
||||
$lineno = $token->getLine();
|
||||
$arguments = new ArrayExpression([], $lineno);
|
||||
|
||||
// slice?
|
||||
$slice = false;
|
||||
if ($stream->test(Token::PUNCTUATION_TYPE, ':')) {
|
||||
$slice = true;
|
||||
$attribute = new ConstantExpression(0, $token->getLine());
|
||||
} else {
|
||||
$attribute = $parser->parseExpression();
|
||||
}
|
||||
|
||||
if ($stream->nextIf(Token::PUNCTUATION_TYPE, ':')) {
|
||||
$slice = true;
|
||||
}
|
||||
|
||||
if ($slice) {
|
||||
if ($stream->test(Token::PUNCTUATION_TYPE, ']')) {
|
||||
$length = new ConstantExpression(null, $token->getLine());
|
||||
} else {
|
||||
$length = $parser->parseExpression();
|
||||
}
|
||||
|
||||
$filter = $parser->getFilter('slice', $token->getLine());
|
||||
$arguments = new Nodes([$attribute, $length]);
|
||||
$filter = new ($filter->getNodeClass())($expr, $filter, $arguments, $token->getLine());
|
||||
|
||||
$stream->expect(Token::PUNCTUATION_TYPE, ']');
|
||||
|
||||
return $filter;
|
||||
}
|
||||
|
||||
$stream->expect(Token::PUNCTUATION_TYPE, ']');
|
||||
|
||||
return new GetAttrExpression($expr, $attribute, $arguments, Template::ARRAY_CALL, $lineno);
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return '[';
|
||||
}
|
||||
|
||||
public function getPrecedence(): int
|
||||
{
|
||||
return 300;
|
||||
}
|
||||
|
||||
public function getAssociativity(): InfixAssociativity
|
||||
{
|
||||
return InfixAssociativity::Left;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user