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:
Fabien Potencier
2024-09-30 11:58:51 +02:00
3 changed files with 19 additions and 9 deletions
+2 -7
View File
@@ -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');
}
}
+2 -2
View File
@@ -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(')');
}
}