Skip to content

Commit

Permalink
Pass the current token instead of just the line of the token on Opera…
Browse files Browse the repository at this point in the history
…tor::parse()
  • Loading branch information
fabpot committed Feb 1, 2025
1 parent b9b198c commit 127bffb
Show file tree
Hide file tree
Showing 16 changed files with 41 additions and 36 deletions.
10 changes: 5 additions & 5 deletions src/ExpressionParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public function parseExpression($precedence = 0)
$this->parser->getStream()->next();
$previous = $this->setDeprecationCheck(true);
try {
$expr = $op->parse($this, $expr, $token->getLine());
$expr = $op->parse($this, $expr, $token);
} finally {
$this->setDeprecationCheck($previous);
}
Expand Down Expand Up @@ -160,7 +160,7 @@ public function parsePrimary(): AbstractExpression
$this->parser->getStream()->next();
$previous = $this->setDeprecationCheck(false);
try {
$expr = $operator->parse($this, $token->getLine());
$expr = $operator->parse($this, $token);
} finally {
$this->setDeprecationCheck($previous);
}
Expand Down Expand Up @@ -415,10 +415,10 @@ public function parseSubscriptExpression($node)
trigger_deprecation('twig/twig', '3.20', 'The "%s()" method is deprecated.', __METHOD__);

if ('.' === $this->parser->getStream()->next()->getValue()) {
return $this->operators->getBinary('.')->parse($this, $node, $this->parser->getCurrentToken()->getLine());
return $this->operators->getBinary('.')->parse($this, $node, $this->parser->getCurrentToken());
}

return $this->operators->getBinary('[')->parse($this, $node, $this->parser->getCurrentToken()->getLine());
return $this->operators->getBinary('[')->parse($this, $node, $this->parser->getCurrentToken());
}

/**
Expand All @@ -442,7 +442,7 @@ public function parseFilterExpressionRaw($node)

$op = $this->operators->getBinary('|');
while (true) {
$node = $op->parse($this, $node, $this->parser->getCurrentToken()->getLine());
$node = $op->parse($this, $node, $this->parser->getCurrentToken());
if (!$this->parser->getStream()->test(Token::OPERATOR_TYPE, '|')) {
break;
}
Expand Down
7 changes: 3 additions & 4 deletions src/Operator/Binary/AbstractBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,15 @@
use Twig\Operator\AbstractOperator;
use Twig\Operator\OperatorArity;
use Twig\Operator\OperatorAssociativity;
use Twig\Token;

abstract class AbstractBinaryOperator extends AbstractOperator implements BinaryOperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $left, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $left, Token $token): AbstractExpression
{
$class = $this->getNodeClass();

$right = $parser->parseExpression(OperatorAssociativity::Left === $this->getAssociativity() ? $this->getPrecedence() + 1 : $this->getPrecedence());

return new $class($left, $right, $line);
return new ($this->getNodeClass())($left, $right, $token->getLine());
}

public function getArity(): OperatorArity
Expand Down
5 changes: 3 additions & 2 deletions src/Operator/Binary/ArrowBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,14 @@
use Twig\Operator\AbstractOperator;
use Twig\Operator\OperatorArity;
use Twig\Operator\OperatorAssociativity;
use Twig\Token;

class ArrowBinaryOperator extends AbstractOperator implements BinaryOperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $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, $line);
return new ArrowFunctionExpression($parser->parseExpression(), $expr, $token->getLine());
}

public function getOperator(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Operator/Binary/BinaryOperatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use Twig\Node\Expression\AbstractExpression;
use Twig\Operator\OperatorAssociativity;
use Twig\Operator\OperatorInterface;
use Twig\Token;

interface BinaryOperatorInterface extends OperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $left, int $line): AbstractExpression;
public function parse(ExpressionParser $parser, AbstractExpression $left, Token $token): AbstractExpression;

public function getAssociativity(): OperatorAssociativity;
}
2 changes: 1 addition & 1 deletion src/Operator/Binary/DotBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

class DotBinaryOperator extends AbstractOperator implements BinaryOperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $expr, Token $token): AbstractExpression
{
$stream = $parser->getStream();
$token = $stream->getCurrent();
Expand Down
7 changes: 4 additions & 3 deletions src/Operator/Binary/FilterBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ class FilterBinaryOperator extends AbstractOperator implements BinaryOperatorInt
{
private $readyNodes = [];

public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $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 = $parser->parseNamedArguments();
}

$filter = $parser->getFilter($token->getValue(), $token->getLine());
$filter = $parser->getFilter($token->getValue(), $line);

$ready = true;
if (!isset($this->readyNodes[$class = $filter->getNodeClass()])) {
Expand All @@ -47,7 +48,7 @@ public function parse(ExpressionParser $parser, AbstractExpression $expr, int $l
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(), $token->getLine()), $arguments, $token->getLine());
return new $class($expr, $ready ? $filter : new ConstantExpression($filter->getName(), $line), $arguments, $line);
}

public function getOperator(): string
Expand Down
4 changes: 3 additions & 1 deletion src/Operator/Binary/FunctionBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,15 @@
use Twig\Operator\AbstractOperator;
use Twig\Operator\OperatorArity;
use Twig\Operator\OperatorAssociativity;
use Twig\Token;

class FunctionBinaryOperator extends AbstractOperator implements BinaryOperatorInterface
{
private $readyNodes = [];

public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $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());
}
Expand Down
4 changes: 2 additions & 2 deletions src/Operator/Binary/IsBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class IsBinaryOperator extends AbstractOperator implements BinaryOperatorInterfa
{
private $readyNodes = [];

public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $expr, Token $token): AbstractExpression
{
$stream = $parser->getStream();
$test = $parser->getTest($line);
$test = $parser->getTest($token->getLine());

$arguments = null;
if ($stream->test(Token::OPERATOR_TYPE, '(')) {
Expand Down
5 changes: 3 additions & 2 deletions src/Operator/Binary/IsNotBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@
use Twig\ExpressionParser;
use Twig\Node\Expression\AbstractExpression;
use Twig\Node\Expression\Unary\NotUnary;
use Twig\Token;

class IsNotBinaryOperator extends IsBinaryOperator
{
public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $expr, Token $token): AbstractExpression
{
return new NotUnary(parent::parse($parser, $expr, $line), $line);
return new NotUnary(parent::parse($parser, $expr, $token), $token->getLine());
}

public function getOperator(): string
Expand Down
3 changes: 1 addition & 2 deletions src/Operator/Binary/SquareBracketBinaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@

class SquareBracketBinaryOperator extends AbstractOperator implements BinaryOperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $expr, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $expr, Token $token): AbstractExpression
{
$stream = $parser->getStream();
$token = $stream->getCurrent();
$lineno = $token->getLine();
$arguments = new ArrayExpression([], $lineno);

Expand Down
6 changes: 3 additions & 3 deletions src/Operator/Ternary/ConditionalTernaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,18 @@

class ConditionalTernaryOperator extends AbstractTernaryOperator
{
public function parse(ExpressionParser $parser, AbstractExpression $left, int $line): AbstractExpression
public function parse(ExpressionParser $parser, AbstractExpression $left, Token $token): AbstractExpression
{
$then = $parser->parseExpression($this->getPrecedence());
if ($parser->getStream()->nextIf(Token::PUNCTUATION_TYPE, $this->getElseOperator())) {
// Ternary operator (expr ? expr2 : expr3)
$else = $parser->parseExpression($this->getPrecedence());
} else {
// Ternary without else (expr ? expr2)
$else = new ConstantExpression('', $line);
$else = new ConstantExpression('', $token->getLine());
}

return new ConditionalTernary($left, $then, $else, $line);
return new ConditionalTernary($left, $then, $else, $token->getLine());
}

public function getOperator(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Operator/Ternary/TernaryOperatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
use Twig\Node\Expression\AbstractExpression;
use Twig\Operator\OperatorAssociativity;
use Twig\Operator\OperatorInterface;
use Twig\Token;

interface TernaryOperatorInterface extends OperatorInterface
{
public function parse(ExpressionParser $parser, AbstractExpression $left, int $line): AbstractExpression;
public function parse(ExpressionParser $parser, AbstractExpression $left, Token $token): AbstractExpression;

public function getAssociativity(): OperatorAssociativity;
}
7 changes: 3 additions & 4 deletions src/Operator/Unary/AbstractUnaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
use Twig\Node\Expression\AbstractExpression;
use Twig\Operator\AbstractOperator;
use Twig\Operator\OperatorArity;
use Twig\Token;

abstract class AbstractUnaryOperator extends AbstractOperator implements UnaryOperatorInterface
{
public function parse(ExpressionParser $parser, int $line): AbstractExpression
public function parse(ExpressionParser $parser, Token $token): AbstractExpression
{
$class = $this->getNodeClass();

return new $class($parser->parseExpression($this->getPrecedence()), $line);
return new ($this->getNodeClass())($parser->parseExpression($this->getPrecedence()), $token->getLine());
}

public function getArity(): OperatorArity
Expand Down
6 changes: 3 additions & 3 deletions src/Operator/Unary/ParenthesisUnaryOperator.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

class ParenthesisUnaryOperator extends AbstractOperator implements UnaryOperatorInterface
{
public function parse(ExpressionParser $parser, int $line): AbstractExpression
public function parse(ExpressionParser $parser, Token $token): AbstractExpression
{
$stream = $parser->getStream();
$expr = $parser->parseExpression($this->getPrecedence());
Expand All @@ -32,7 +32,7 @@ public function parse(ExpressionParser $parser, int $line): AbstractExpression
return $expr->setExplicitParentheses();
}

return new ListExpression([$expr], $line);
return new ListExpression([$expr], $token->getLine());
}

// determine if we are parsing an arrow function arguments
Expand All @@ -54,7 +54,7 @@ public function parse(ExpressionParser $parser, int $line): AbstractExpression
throw new SyntaxError('A list of variables must be followed by an arrow.', $stream->getCurrent()->getLine(), $stream->getSourceContext());
}

return new ListExpression($names, $line);
return new ListExpression($names, $token->getLine());
}

public function getOperator(): string
Expand Down
3 changes: 2 additions & 1 deletion src/Operator/Unary/UnaryOperatorInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@
use Twig\ExpressionParser;
use Twig\Node\Expression\AbstractExpression;
use Twig\Operator\OperatorInterface;
use Twig\Token;

interface UnaryOperatorInterface extends OperatorInterface
{
public function parse(ExpressionParser $parser, int $line): AbstractExpression;
public function parse(ExpressionParser $parser, Token $token): AbstractExpression;
}
2 changes: 1 addition & 1 deletion src/TokenParser/ApplyTokenParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function parse(Token $token): Node
$filter = $ref;
$op = $this->parser->getEnvironment()->getOperators()->getBinary('|');
while (true) {
$filter = $op->parse($this->parser->getExpressionParser(), $filter, $this->parser->getCurrentToken()->getLine());
$filter = $op->parse($this->parser->getExpressionParser(), $filter, $this->parser->getCurrentToken());
if (!$this->parser->getStream()->test(Token::OPERATOR_TYPE, '|')) {
break;
}
Expand Down

0 comments on commit 127bffb

Please sign in to comment.