mirror of
https://github.com/twigphp/Twig.git
synced 2026-09-15 11:56:50 +00:00
minor #4365 Add support for detecting if an expression had explicit parentheses (fabpot)
This PR was merged into the 3.x branch.
Discussion
----------
Add support for detecting if an expression had explicit parentheses
It's going to help with operator precedence changes and their related deprecation notices.
Commits
-------
d133ab7e70 Add support for detecting if an expression had explicit parentheses
This commit is contained in:
@@ -169,15 +169,10 @@ class ExpressionParser
|
||||
return $this->parsePostfixExpression(new $class($expr, $token->getLine()));
|
||||
} elseif ($token->test(Token::PUNCTUATION_TYPE, '(')) {
|
||||
$this->parser->getStream()->next();
|
||||
$expr = $this->parseExpression();
|
||||
$expr = $this->parseExpression()->setExplicitParentheses();
|
||||
$this->parser->getStream()->expect(Token::PUNCTUATION_TYPE, ')', 'An opened parenthesis is not properly closed');
|
||||
|
||||
$expr = $this->parsePostfixExpression($expr);
|
||||
if ($expr instanceof NegUnary) {
|
||||
$expr->wrapInParentheses();
|
||||
}
|
||||
|
||||
return $expr;
|
||||
return $this->parsePostfixExpression($expr);
|
||||
}
|
||||
|
||||
return $this->parsePrimaryExpression();
|
||||
|
||||
@@ -25,4 +25,19 @@ abstract class AbstractExpression extends Node
|
||||
{
|
||||
return $this->hasAttribute('is_generator') && $this->getAttribute('is_generator');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return static
|
||||
*/
|
||||
public function setExplicitParentheses(): self
|
||||
{
|
||||
$this->setAttribute('with_parentheses', true);
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function hasExplicitParentheses(): bool
|
||||
{
|
||||
return $this->hasAttribute('with_parentheses') && $this->getAttribute('with_parentheses');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -30,14 +30,14 @@ abstract class AbstractUnary extends AbstractExpression
|
||||
|
||||
public function compile(Compiler $compiler): void
|
||||
{
|
||||
if ($this->getAttribute('with_parentheses')) {
|
||||
if ($this->hasExplicitParentheses()) {
|
||||
$compiler->raw('(');
|
||||
} else {
|
||||
$compiler->raw(' ');
|
||||
}
|
||||
$this->operator($compiler);
|
||||
$compiler->subcompile($this->getNode('node'));
|
||||
if ($this->getAttribute('with_parentheses')) {
|
||||
if ($this->hasExplicitParentheses()) {
|
||||
$compiler->raw(')');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user