2017-08-18 22:57:27 +02:00
|
|
|
<?php declare(strict_types=1);
|
2011-05-27 22:57:55 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
2017-10-05 18:44:45 +02:00
|
|
|
use PhpParser\Internal\DiffElem;
|
2017-10-29 12:25:13 +01:00
|
|
|
use PhpParser\Internal\PrintableNewAnonClassNode;
|
2018-01-27 13:40:20 +01:00
|
|
|
use PhpParser\Internal\TokenStream;
|
2022-09-03 20:56:06 +02:00
|
|
|
use PhpParser\Node\AttributeGroup;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Expr;
|
2017-11-13 00:11:19 +01:00
|
|
|
use PhpParser\Node\Expr\AssignOp;
|
|
|
|
use PhpParser\Node\Expr\BinaryOp;
|
|
|
|
use PhpParser\Node\Expr\Cast;
|
2022-09-03 20:56:06 +02:00
|
|
|
use PhpParser\Node\IntersectionType;
|
|
|
|
use PhpParser\Node\MatchArm;
|
2022-09-03 19:11:35 +02:00
|
|
|
use PhpParser\Node\Param;
|
2016-12-23 14:11:31 +01:00
|
|
|
use PhpParser\Node\Scalar;
|
2014-02-06 14:44:16 +01:00
|
|
|
use PhpParser\Node\Stmt;
|
2022-09-03 20:56:06 +02:00
|
|
|
use PhpParser\Node\UnionType;
|
2014-02-06 14:44:16 +01:00
|
|
|
|
2022-08-28 22:57:06 +02:00
|
|
|
abstract class PrettyPrinterAbstract {
|
2023-02-26 15:57:37 +01:00
|
|
|
protected const FIXUP_PREC_LEFT = 0; // LHS operand affected by precedence
|
|
|
|
protected const FIXUP_PREC_RIGHT = 1; // RHS operand affected by precedence
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected const FIXUP_PREC_UNARY = 2; // Only operand affected by precedence
|
|
|
|
protected const FIXUP_CALL_LHS = 3; // LHS of call
|
|
|
|
protected const FIXUP_DEREF_LHS = 4; // LHS of dereferencing operation
|
|
|
|
protected const FIXUP_STATIC_DEREF_LHS = 5; // LHS of static dereferencing operation
|
|
|
|
protected const FIXUP_BRACED_NAME = 6; // Name operand that may require bracing
|
|
|
|
protected const FIXUP_VAR_BRACED_NAME = 7; // Name operand that may require ${} bracing
|
|
|
|
protected const FIXUP_ENCAPSED = 8; // Encapsed string part
|
|
|
|
protected const FIXUP_NEW = 9; // New/instanceof operand
|
|
|
|
|
2023-02-26 18:44:40 +01:00
|
|
|
protected const MAX_PRECEDENCE = 1000;
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
|
|
|
|
/** @var array<class-string, array{int, int, int}> */
|
2017-08-13 14:06:08 +02:00
|
|
|
protected $precedenceMap = [
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
// [precedence, precedenceLHS, precedenceRHS]
|
|
|
|
// Where the latter two are the precedences to use for the LHS and RHS of a binary operator,
|
|
|
|
// where 1 is added to one of the sides depending on associativity. This information is not
|
|
|
|
// used for unary operators and set to -1.
|
|
|
|
Expr\Clone_::class => [-10, 0, 1],
|
|
|
|
BinaryOp\Pow::class => [ 0, 0, 1],
|
|
|
|
Expr\BitwiseNot::class => [ 10, -1, -1],
|
|
|
|
Expr\UnaryPlus::class => [ 10, -1, -1],
|
|
|
|
Expr\UnaryMinus::class => [ 10, -1, -1],
|
|
|
|
Cast\Int_::class => [ 10, -1, -1],
|
|
|
|
Cast\Double::class => [ 10, -1, -1],
|
|
|
|
Cast\String_::class => [ 10, -1, -1],
|
|
|
|
Cast\Array_::class => [ 10, -1, -1],
|
|
|
|
Cast\Object_::class => [ 10, -1, -1],
|
|
|
|
Cast\Bool_::class => [ 10, -1, -1],
|
|
|
|
Cast\Unset_::class => [ 10, -1, -1],
|
|
|
|
Expr\ErrorSuppress::class => [ 10, -1, -1],
|
|
|
|
Expr\Instanceof_::class => [ 20, -1, -1],
|
|
|
|
Expr\BooleanNot::class => [ 30, -1, -1],
|
|
|
|
BinaryOp\Mul::class => [ 40, 41, 40],
|
|
|
|
BinaryOp\Div::class => [ 40, 41, 40],
|
|
|
|
BinaryOp\Mod::class => [ 40, 41, 40],
|
|
|
|
BinaryOp\Plus::class => [ 50, 51, 50],
|
|
|
|
BinaryOp\Minus::class => [ 50, 51, 50],
|
|
|
|
BinaryOp\Concat::class => [ 50, 51, 50],
|
|
|
|
BinaryOp\ShiftLeft::class => [ 60, 61, 60],
|
|
|
|
BinaryOp\ShiftRight::class => [ 60, 61, 60],
|
|
|
|
BinaryOp\Smaller::class => [ 70, 70, 70],
|
|
|
|
BinaryOp\SmallerOrEqual::class => [ 70, 70, 70],
|
|
|
|
BinaryOp\Greater::class => [ 70, 70, 70],
|
|
|
|
BinaryOp\GreaterOrEqual::class => [ 70, 70, 70],
|
|
|
|
BinaryOp\Equal::class => [ 80, 80, 80],
|
|
|
|
BinaryOp\NotEqual::class => [ 80, 80, 80],
|
|
|
|
BinaryOp\Identical::class => [ 80, 80, 80],
|
|
|
|
BinaryOp\NotIdentical::class => [ 80, 80, 80],
|
|
|
|
BinaryOp\Spaceship::class => [ 80, 80, 80],
|
|
|
|
BinaryOp\BitwiseAnd::class => [ 90, 91, 90],
|
|
|
|
BinaryOp\BitwiseXor::class => [100, 101, 100],
|
|
|
|
BinaryOp\BitwiseOr::class => [110, 111, 110],
|
|
|
|
BinaryOp\BooleanAnd::class => [120, 121, 120],
|
|
|
|
BinaryOp\BooleanOr::class => [130, 131, 130],
|
|
|
|
BinaryOp\Coalesce::class => [140, 140, 141],
|
|
|
|
Expr\Ternary::class => [150, -1, -1],
|
|
|
|
Expr\Assign::class => [160, -1, -1],
|
|
|
|
Expr\AssignRef::class => [160, -1, -1],
|
|
|
|
AssignOp\Plus::class => [160, -1, -1],
|
|
|
|
AssignOp\Minus::class => [160, -1, -1],
|
|
|
|
AssignOp\Mul::class => [160, -1, -1],
|
|
|
|
AssignOp\Div::class => [160, -1, -1],
|
|
|
|
AssignOp\Concat::class => [160, -1, -1],
|
|
|
|
AssignOp\Mod::class => [160, -1, -1],
|
|
|
|
AssignOp\BitwiseAnd::class => [160, -1, -1],
|
|
|
|
AssignOp\BitwiseOr::class => [160, -1, -1],
|
|
|
|
AssignOp\BitwiseXor::class => [160, -1, -1],
|
|
|
|
AssignOp\ShiftLeft::class => [160, -1, -1],
|
|
|
|
AssignOp\ShiftRight::class => [160, -1, -1],
|
|
|
|
AssignOp\Pow::class => [160, -1, -1],
|
|
|
|
AssignOp\Coalesce::class => [160, -1, -1],
|
|
|
|
Expr\YieldFrom::class => [170, -1, -1],
|
2023-02-26 18:44:40 +01:00
|
|
|
Expr\Yield_::class => [175, -1, -1],
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
Expr\Print_::class => [180, -1, -1],
|
|
|
|
BinaryOp\LogicalAnd::class => [190, 191, 190],
|
|
|
|
BinaryOp\LogicalXor::class => [200, 201, 200],
|
|
|
|
BinaryOp\LogicalOr::class => [210, 211, 210],
|
|
|
|
Expr\Include_::class => [220, -1, -1],
|
|
|
|
Expr\ArrowFunction::class => [230, -1, -1],
|
2023-02-26 18:33:24 +01:00
|
|
|
Expr\Throw_::class => [240, -1, -1],
|
2017-08-13 14:06:08 +02:00
|
|
|
];
|
2011-06-02 22:52:24 +02:00
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
/** @var int Current indentation level. */
|
|
|
|
protected $indentLevel;
|
|
|
|
/** @var string Newline including current indentation. */
|
|
|
|
protected $nl;
|
2022-07-24 22:15:42 +02:00
|
|
|
/** @var string|null Token placed at end of doc string to ensure it is followed by a newline.
|
|
|
|
* Null if flexible doc strings are used. */
|
2016-04-02 22:22:24 +09:00
|
|
|
protected $docStringEndToken;
|
2016-12-23 14:11:31 +01:00
|
|
|
/** @var bool Whether semicolon namespaces can be used (i.e. no global namespace is used) */
|
2013-01-15 18:21:42 +01:00
|
|
|
protected $canUseSemicolonNamespaces;
|
2022-07-23 18:23:39 +02:00
|
|
|
/** @var bool Whether to use short array syntax if the node specifies no preference */
|
|
|
|
protected $shortArraySyntax;
|
|
|
|
/** @var PhpVersion PHP version to target */
|
|
|
|
protected $phpVersion;
|
2011-06-02 22:52:24 +02:00
|
|
|
|
2022-09-11 12:31:50 +02:00
|
|
|
/** @var TokenStream|null Original tokens for use in format-preserving pretty print */
|
2016-12-23 14:11:31 +01:00
|
|
|
protected $origTokens;
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @var Internal\Differ<Node>|null Differ for node lists */
|
2017-10-05 18:44:45 +02:00
|
|
|
protected $nodeListDiffer;
|
2022-09-17 17:12:55 +02:00
|
|
|
/** @var array<string, bool> Map determining whether a certain character is a label character */
|
2016-12-23 14:11:31 +01:00
|
|
|
protected $labelCharMap;
|
|
|
|
/**
|
2022-09-17 17:12:55 +02:00
|
|
|
* @var array<string, array<string, int>> Map from token classes and subnode names to FIXUP_* constants.
|
|
|
|
* This is used during format-preserving prints to place additional parens/braces if necessary.
|
2016-12-23 14:11:31 +01:00
|
|
|
*/
|
|
|
|
protected $fixupMap;
|
2017-01-21 17:21:01 +01:00
|
|
|
/**
|
2022-09-17 17:12:55 +02:00
|
|
|
* @var array<string, array{left?: int|string, right?: int|string}> Map from "{$node->getType()}->{$subNode}"
|
|
|
|
* to ['left' => $l, 'right' => $r], where $l and $r specify the token type that needs to be stripped
|
|
|
|
* when removing this node.
|
2017-01-21 17:21:01 +01:00
|
|
|
*/
|
|
|
|
protected $removalMap;
|
2017-10-06 13:18:58 +02:00
|
|
|
/**
|
2022-09-17 17:12:55 +02:00
|
|
|
* @var array<string, array{int|string|null, bool, string|null, string|null}> Map from
|
|
|
|
* "{$node->getType()}->{$subNode}" to [$find, $beforeToken, $extraLeft, $extraRight].
|
|
|
|
* $find is an optional token after which the insertion occurs. $extraLeft/Right
|
|
|
|
* are optionally added before/after the main insertions.
|
2017-10-06 13:18:58 +02:00
|
|
|
*/
|
2017-01-21 20:40:05 +01:00
|
|
|
protected $insertionMap;
|
2017-10-06 13:18:58 +02:00
|
|
|
/**
|
2022-09-17 17:12:55 +02:00
|
|
|
* @var array<string, string> Map From "{$class}->{$subNode}" to string that should be inserted
|
|
|
|
* between elements of this list subnode.
|
2017-10-06 13:18:58 +02:00
|
|
|
*/
|
|
|
|
protected $listInsertionMap;
|
2022-09-03 20:56:06 +02:00
|
|
|
|
2022-09-17 17:12:55 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, array{int|string|null, string, string}>
|
|
|
|
*/
|
2019-05-11 22:12:02 +02:00
|
|
|
protected $emptyListInsertionMap;
|
2022-09-21 18:56:09 +02:00
|
|
|
/** @var array<string, array{string, int}> Map from "{$class}->{$subNode}" to [$printFn, $token]
|
|
|
|
* where $printFn is the function to print the modifiers and $token is the token before which
|
|
|
|
* the modifiers should be reprinted. */
|
2017-10-25 22:27:29 +02:00
|
|
|
protected $modifierChangeMap;
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2015-09-21 15:12:59 +08:00
|
|
|
/**
|
|
|
|
* Creates a pretty printer instance using the given options.
|
|
|
|
*
|
|
|
|
* Supported options:
|
2022-07-23 18:23:39 +02:00
|
|
|
* * PhpVersion $phpVersion: The PHP version to target (default to PHP 7.0). This option
|
|
|
|
* controls compatibility of the generated code with older PHP
|
|
|
|
* versions in cases where a simple stylistic choice exists (e.g.
|
|
|
|
* array() vs []). It is safe to pretty-print an AST for a newer
|
|
|
|
* PHP version while specifying an older target (but the result will
|
|
|
|
* of course not be compatible with the older version in that case).
|
|
|
|
* * bool $shortArraySyntax: Whether to use [] instead of array() as the default array
|
|
|
|
* syntax, if the node does not specify a format. Defaults to whether
|
|
|
|
* the phpVersion support short array syntax.
|
2015-09-21 15:12:59 +08:00
|
|
|
*
|
2022-09-17 17:12:55 +02:00
|
|
|
* @param array{phpVersion?: PhpVersion, shortArraySyntax?: bool} $options Dictionary of formatting options
|
2015-09-21 15:12:59 +08:00
|
|
|
*/
|
|
|
|
public function __construct(array $options = []) {
|
2022-07-23 18:23:39 +02:00
|
|
|
$this->phpVersion = $options['phpVersion'] ?? PhpVersion::fromComponents(7, 0);
|
|
|
|
$this->shortArraySyntax =
|
|
|
|
$options['shortArraySyntax'] ?? $this->phpVersion->supportsShortArraySyntax();
|
2022-07-24 22:15:42 +02:00
|
|
|
$this->docStringEndToken =
|
|
|
|
$this->phpVersion->supportsFlexibleHeredoc() ? null : '_DOC_STRING_END_' . mt_rand();
|
2011-08-04 18:19:45 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
/**
|
|
|
|
* Reset pretty printing state.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function resetState(): void {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->indentLevel = 0;
|
|
|
|
$this->nl = "\n";
|
2017-10-29 13:28:46 +01:00
|
|
|
$this->origTokens = null;
|
2017-09-03 17:05:35 +02:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
/**
|
|
|
|
* Set indentation level
|
|
|
|
*
|
|
|
|
* @param int $level Level in number of spaces
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function setIndentLevel(int $level): void {
|
2017-10-06 18:21:08 +02:00
|
|
|
$this->indentLevel = $level;
|
|
|
|
$this->nl = "\n" . \str_repeat(' ', $level);
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
/**
|
|
|
|
* Increase indentation level.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function indent(): void {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->indentLevel += 4;
|
|
|
|
$this->nl .= ' ';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Decrease indentation level.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function outdent(): void {
|
2017-10-06 18:21:08 +02:00
|
|
|
assert($this->indentLevel >= 4);
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->indentLevel -= 4;
|
|
|
|
$this->nl = "\n" . str_repeat(' ', $this->indentLevel);
|
|
|
|
}
|
|
|
|
|
2011-06-02 22:52:24 +02:00
|
|
|
/**
|
2013-04-15 20:53:23 +02:00
|
|
|
* Pretty prints an array of statements.
|
2011-06-02 22:52:24 +02:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $stmts Array of statements
|
2011-06-02 22:52:24 +02:00
|
|
|
*
|
2013-04-15 20:53:23 +02:00
|
|
|
* @return string Pretty printed statements
|
2011-06-02 22:52:24 +02:00
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function prettyPrint(array $stmts): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->resetState();
|
2013-04-15 20:53:23 +02:00
|
|
|
$this->preprocessNodes($stmts);
|
2013-01-15 18:21:42 +01:00
|
|
|
|
2016-04-02 22:22:24 +09:00
|
|
|
return ltrim($this->handleMagicTokens($this->pStmts($stmts, false)));
|
2011-06-02 22:52:24 +02:00
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2011-08-04 18:19:45 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints an expression.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Expr $node Expression node
|
2011-08-04 18:19:45 +02:00
|
|
|
*
|
|
|
|
* @return string Pretty printed node
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function prettyPrintExpr(Expr $node): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->resetState();
|
2016-04-02 22:22:24 +09:00
|
|
|
return $this->handleMagicTokens($this->p($node));
|
2011-08-04 18:19:45 +02:00
|
|
|
}
|
|
|
|
|
2013-04-15 20:53:23 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints a file of statements (includes the opening <?php tag if it is required).
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $stmts Array of statements
|
2013-04-15 20:53:23 +02:00
|
|
|
*
|
|
|
|
* @return string Pretty printed statements
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function prettyPrintFile(array $stmts): string {
|
2016-02-20 18:23:52 +01:00
|
|
|
if (!$stmts) {
|
|
|
|
return "<?php\n\n";
|
|
|
|
}
|
2013-04-15 20:53:23 +02:00
|
|
|
|
2016-02-28 20:28:32 +01:00
|
|
|
$p = "<?php\n\n" . $this->prettyPrint($stmts);
|
2013-04-15 20:53:23 +02:00
|
|
|
|
2016-02-20 18:23:52 +01:00
|
|
|
if ($stmts[0] instanceof Stmt\InlineHTML) {
|
2016-02-28 20:28:32 +01:00
|
|
|
$p = preg_replace('/^<\?php\s+\?>\n?/', '', $p);
|
2013-04-15 20:53:23 +02:00
|
|
|
}
|
2016-02-20 18:23:52 +01:00
|
|
|
if ($stmts[count($stmts) - 1] instanceof Stmt\InlineHTML) {
|
|
|
|
$p = preg_replace('/<\?php$/', '', rtrim($p));
|
|
|
|
}
|
|
|
|
|
2013-04-15 20:53:23 +02:00
|
|
|
return $p;
|
|
|
|
}
|
|
|
|
|
2013-01-15 18:21:42 +01:00
|
|
|
/**
|
|
|
|
* Preprocesses the top-level nodes to initialize pretty printer state.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $nodes Array of nodes
|
2013-01-15 18:21:42 +01:00
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function preprocessNodes(array $nodes): void {
|
2013-01-15 18:21:42 +01:00
|
|
|
/* We can use semicolon-namespaces unless there is a global namespace declaration */
|
|
|
|
$this->canUseSemicolonNamespaces = true;
|
|
|
|
foreach ($nodes as $node) {
|
2014-02-06 14:44:16 +01:00
|
|
|
if ($node instanceof Stmt\Namespace_ && null === $node->name) {
|
2013-01-15 18:21:42 +01:00
|
|
|
$this->canUseSemicolonNamespaces = false;
|
2017-09-15 10:43:25 +03:00
|
|
|
break;
|
2013-01-15 18:21:42 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
/**
|
|
|
|
* Handles (and removes) no-indent and doc-string-end tokens.
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-07-24 22:15:42 +02:00
|
|
|
protected function handleMagicTokens(string $str): string {
|
|
|
|
if ($this->docStringEndToken !== null) {
|
|
|
|
// Replace doc-string-end tokens with nothing or a newline
|
|
|
|
$str = str_replace($this->docStringEndToken . ";\n", ";\n", $str);
|
|
|
|
$str = str_replace($this->docStringEndToken, "\n", $str);
|
|
|
|
}
|
2016-04-02 22:22:24 +09:00
|
|
|
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
2011-06-02 22:52:24 +02:00
|
|
|
* Pretty prints an array of nodes (statements) and indents them optionally.
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $nodes Array of nodes
|
|
|
|
* @param bool $indent Whether to indent the printed nodes
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return string Pretty printed statements
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pStmts(array $nodes, bool $indent = true): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
if ($indent) {
|
|
|
|
$this->indent();
|
|
|
|
}
|
|
|
|
|
2014-03-27 12:30:03 +01:00
|
|
|
$result = '';
|
2011-05-27 22:57:55 +02:00
|
|
|
foreach ($nodes as $node) {
|
2017-09-29 17:09:16 +02:00
|
|
|
$comments = $node->getComments();
|
2016-04-19 15:10:51 +02:00
|
|
|
if ($comments) {
|
2017-09-03 17:05:35 +02:00
|
|
|
$result .= $this->nl . $this->pComments($comments);
|
2016-04-19 15:10:51 +02:00
|
|
|
if ($node instanceof Stmt\Nop) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
$result .= $this->nl . $this->p($node);
|
2011-06-02 22:52:24 +02:00
|
|
|
}
|
2011-05-27 22:57:55 +02:00
|
|
|
|
2011-06-02 22:52:24 +02:00
|
|
|
if ($indent) {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->outdent();
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|
2017-09-03 17:05:35 +02:00
|
|
|
|
|
|
|
return $result;
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|
|
|
|
|
2017-01-24 00:38:55 -07:00
|
|
|
/**
|
2017-01-26 00:16:54 +01:00
|
|
|
* Pretty-print an infix operation while taking precedence into account.
|
|
|
|
*
|
2017-11-13 00:11:19 +01:00
|
|
|
* @param string $class Node class of operator
|
2017-01-26 00:16:54 +01:00
|
|
|
* @param Node $leftNode Left-hand side node
|
|
|
|
* @param string $operatorString String representation of the operator
|
|
|
|
* @param Node $rightNode Right-hand side node
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @param int $precedence Precedence of parent operator
|
|
|
|
* @param int $lhsPrecedence Precedence for unary operator on LHS of binary operator
|
2017-01-26 00:16:54 +01:00
|
|
|
*
|
|
|
|
* @return string Pretty printed infix operation
|
2017-01-24 00:38:55 -07:00
|
|
|
*/
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pInfixOp(
|
|
|
|
string $class, Node $leftNode, string $operatorString, Node $rightNode,
|
|
|
|
int $precedence, int $lhsPrecedence
|
|
|
|
): string {
|
|
|
|
list($opPrecedence, $newPrecedenceLHS, $newPrecedenceRHS) = $this->precedenceMap[$class];
|
|
|
|
$prefix = '';
|
|
|
|
$suffix = '';
|
|
|
|
if ($opPrecedence >= $precedence) {
|
|
|
|
$prefix = '(';
|
|
|
|
$suffix = ')';
|
|
|
|
$lhsPrecedence = self::MAX_PRECEDENCE;
|
|
|
|
}
|
|
|
|
return $prefix . $this->p($leftNode, $newPrecedenceLHS, $newPrecedenceLHS)
|
|
|
|
. $operatorString . $this->p($rightNode, $newPrecedenceRHS, $lhsPrecedence) . $suffix;
|
2012-10-31 17:34:06 +01:00
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2017-01-24 00:38:55 -07:00
|
|
|
/**
|
2017-01-26 00:16:54 +01:00
|
|
|
* Pretty-print a prefix operation while taking precedence into account.
|
|
|
|
*
|
2017-11-13 00:11:19 +01:00
|
|
|
* @param string $class Node class of operator
|
2017-01-26 00:16:54 +01:00
|
|
|
* @param string $operatorString String representation of the operator
|
|
|
|
* @param Node $node Node
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @param int $precedence Precedence of parent operator
|
|
|
|
* @param int $lhsPrecedence Precedence for unary operator on LHS of binary operator
|
2017-01-26 00:16:54 +01:00
|
|
|
*
|
|
|
|
* @return string Pretty printed prefix operation
|
2017-01-24 00:38:55 -07:00
|
|
|
*/
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pPrefixOp(string $class, string $operatorString, Node $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
$opPrecedence = $this->precedenceMap[$class][0];
|
|
|
|
$prefix = '';
|
|
|
|
$suffix = '';
|
2023-02-26 18:44:40 +01:00
|
|
|
if ($opPrecedence >= $lhsPrecedence) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$prefix = '(';
|
|
|
|
$suffix = ')';
|
|
|
|
$lhsPrecedence = self::MAX_PRECEDENCE;
|
|
|
|
}
|
|
|
|
return $prefix . $operatorString . $this->p($node, $opPrecedence, $lhsPrecedence) . $suffix;
|
2012-10-31 17:34:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* Pretty-print a postfix operation while taking precedence into account.
|
2012-10-31 17:34:06 +01:00
|
|
|
*
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @param string $class Node class of operator
|
|
|
|
* @param string $operatorString String representation of the operator
|
|
|
|
* @param Node $node Node
|
|
|
|
* @param int $precedence Precedence of parent operator
|
|
|
|
* @param int $lhsPrecedence Precedence for unary operator on LHS of binary operator
|
2012-10-31 17:34:06 +01:00
|
|
|
*
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @return string Pretty printed postfix operation
|
2012-10-31 17:34:06 +01:00
|
|
|
*/
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pPostfixOp(string $class, Node $node, string $operatorString, int $precedence, int $lhsPrecedence): string {
|
|
|
|
$opPrecedence = $this->precedenceMap[$class][0];
|
|
|
|
$prefix = '';
|
|
|
|
$suffix = '';
|
2023-02-26 18:44:40 +01:00
|
|
|
if ($opPrecedence >= $precedence) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$prefix = '(';
|
|
|
|
$suffix = ')';
|
|
|
|
$lhsPrecedence = self::MAX_PRECEDENCE;
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
if ($opPrecedence < $lhsPrecedence) {
|
|
|
|
$lhsPrecedence = $opPrecedence;
|
|
|
|
}
|
|
|
|
return $prefix . $this->p($node, $opPrecedence, $lhsPrecedence) . $operatorString . $suffix;
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|
2011-05-31 16:33:11 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Pretty prints an array of nodes and implodes the printed values.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $nodes Array of Nodes to be printed
|
|
|
|
* @param string $glue Character to implode with
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @return string Imploded pretty printed nodes> $pre
|
2011-05-31 16:33:11 +02:00
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pImplode(array $nodes, string $glue = ''): string {
|
2017-08-13 14:06:08 +02:00
|
|
|
$pNodes = [];
|
2011-05-31 16:33:11 +02:00
|
|
|
foreach ($nodes as $node) {
|
2016-07-06 23:21:53 +02:00
|
|
|
if (null === $node) {
|
2016-07-09 21:54:15 +02:00
|
|
|
$pNodes[] = '';
|
2016-07-06 23:21:53 +02:00
|
|
|
} else {
|
|
|
|
$pNodes[] = $this->p($node);
|
|
|
|
}
|
2011-05-31 16:33:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return implode($glue, $pNodes);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pretty prints an array of nodes and implodes the printed values with commas.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node[] $nodes Array of Nodes to be printed
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return string Comma separated pretty printed nodes
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pCommaSeparated(array $nodes): string {
|
2011-05-31 16:33:11 +02:00
|
|
|
return $this->pImplode($nodes, ', ');
|
|
|
|
}
|
|
|
|
|
2017-04-09 19:49:47 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints a comma-separated list of nodes in multiline style, including comments.
|
|
|
|
*
|
|
|
|
* The result includes a leading newline and one level of indentation (same as pStmts).
|
|
|
|
*
|
|
|
|
* @param Node[] $nodes Array of Nodes to be printed
|
|
|
|
* @param bool $trailingComma Whether to use a trailing comma
|
|
|
|
*
|
|
|
|
* @return string Comma separated pretty printed nodes in multiline style
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pCommaSeparatedMultiline(array $nodes, bool $trailingComma): string {
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->indent();
|
|
|
|
|
2017-04-09 19:49:47 +02:00
|
|
|
$result = '';
|
|
|
|
$lastIdx = count($nodes) - 1;
|
|
|
|
foreach ($nodes as $idx => $node) {
|
|
|
|
if ($node !== null) {
|
2017-09-29 17:09:16 +02:00
|
|
|
$comments = $node->getComments();
|
2017-04-09 19:49:47 +02:00
|
|
|
if ($comments) {
|
2017-09-03 17:05:35 +02:00
|
|
|
$result .= $this->nl . $this->pComments($comments);
|
2017-04-09 19:49:47 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
$result .= $this->nl . $this->p($node);
|
2017-04-09 19:49:47 +02:00
|
|
|
} else {
|
2017-09-03 17:05:35 +02:00
|
|
|
$result .= $this->nl;
|
2017-04-09 19:49:47 +02:00
|
|
|
}
|
|
|
|
if ($trailingComma || $idx !== $lastIdx) {
|
|
|
|
$result .= ',';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->outdent();
|
|
|
|
return $result;
|
2011-05-31 16:33:11 +02:00
|
|
|
}
|
2012-05-11 16:18:14 +02:00
|
|
|
|
2016-02-20 17:06:09 +01:00
|
|
|
/**
|
|
|
|
* Prints reformatted text of the passed comments.
|
|
|
|
*
|
|
|
|
* @param Comment[] $comments List of comments
|
|
|
|
*
|
|
|
|
* @return string Reformatted text of comments
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pComments(array $comments): string {
|
2016-04-19 15:10:51 +02:00
|
|
|
$formattedComments = [];
|
2012-05-11 16:18:14 +02:00
|
|
|
|
|
|
|
foreach ($comments as $comment) {
|
2017-09-03 17:05:35 +02:00
|
|
|
$formattedComments[] = str_replace("\n", $this->nl, $comment->getReformattedText());
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
return implode($this->nl, $formattedComments);
|
2012-05-11 16:18:14 +02:00
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform a format-preserving pretty print of an AST.
|
|
|
|
*
|
|
|
|
* The format preservation is best effort. For some changes to the AST the formatting will not
|
|
|
|
* be preserved (at least not locally).
|
|
|
|
*
|
|
|
|
* In order to use this method a number of prerequisites must be satisfied:
|
|
|
|
* * The startTokenPos and endTokenPos attributes in the lexer must be enabled.
|
|
|
|
* * The CloningVisitor must be run on the AST prior to modification.
|
|
|
|
* * The original tokens must be provided, using the getTokens() method on the lexer.
|
|
|
|
*
|
|
|
|
* @param Node[] $stmts Modified AST with links to original AST
|
|
|
|
* @param Node[] $origStmts Original AST with token offset information
|
2022-09-17 17:12:55 +02:00
|
|
|
* @param Token[] $origTokens Tokens of the original code
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
public function printFormatPreserving(array $stmts, array $origStmts, array $origTokens): string {
|
2017-10-05 18:44:45 +02:00
|
|
|
$this->initializeNodeListDiffer();
|
2016-12-23 14:11:31 +01:00
|
|
|
$this->initializeLabelCharMap();
|
|
|
|
$this->initializeFixupMap();
|
2017-01-21 17:21:01 +01:00
|
|
|
$this->initializeRemovalMap();
|
2017-01-21 20:40:05 +01:00
|
|
|
$this->initializeInsertionMap();
|
2017-10-06 13:18:58 +02:00
|
|
|
$this->initializeListInsertionMap();
|
2019-05-11 22:12:02 +02:00
|
|
|
$this->initializeEmptyListInsertionMap();
|
2017-10-25 22:27:29 +02:00
|
|
|
$this->initializeModifierChangeMap();
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-09-03 17:05:35 +02:00
|
|
|
$this->resetState();
|
2017-09-03 18:54:22 +02:00
|
|
|
$this->origTokens = new TokenStream($origTokens);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$this->preprocessNodes($stmts);
|
|
|
|
|
|
|
|
$pos = 0;
|
2019-05-11 22:12:02 +02:00
|
|
|
$result = $this->pArray($stmts, $origStmts, $pos, 0, 'File', 'stmts', null);
|
2016-12-23 14:11:31 +01:00
|
|
|
if (null !== $result) {
|
2022-06-04 17:34:48 +02:00
|
|
|
$result .= $this->origTokens->getTokenCode($pos, count($origTokens) - 1, 0);
|
2016-12-23 14:11:31 +01:00
|
|
|
} else {
|
|
|
|
// Fallback
|
|
|
|
// TODO Add <?php properly
|
|
|
|
$result = "<?php\n" . $this->pStmts($stmts, false);
|
|
|
|
}
|
|
|
|
|
|
|
|
return ltrim($this->handleMagicTokens($result));
|
|
|
|
}
|
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function pFallback(Node $node, int $precedence, int $lhsPrecedence): string {
|
|
|
|
return $this->{'p' . $node->getType()}($node, $precedence, $lhsPrecedence);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Pretty prints a node.
|
|
|
|
*
|
|
|
|
* This method also handles formatting preservation for nodes.
|
|
|
|
*
|
|
|
|
* @param Node $node Node to be pretty printed
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
* @param int $precedence Precedence of parent operator
|
|
|
|
* @param int $lhsPrecedence Precedence for unary operator on LHS of binary operator
|
2017-12-24 18:04:51 +01:00
|
|
|
* @param bool $parentFormatPreserved Whether parent node has preserved formatting
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
|
|
|
* @return string Pretty printed node
|
|
|
|
*/
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
protected function p(
|
|
|
|
Node $node, int $precedence = self::MAX_PRECEDENCE, int $lhsPrecedence = self::MAX_PRECEDENCE,
|
|
|
|
bool $parentFormatPreserved = false
|
|
|
|
): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
// No orig tokens means this is a normal pretty print without preservation of formatting
|
|
|
|
if (!$this->origTokens) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->{'p' . $node->getType()}($node, $precedence, $lhsPrecedence);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2022-09-11 12:38:46 +02:00
|
|
|
/** @var Node|null $origNode */
|
2016-12-23 14:11:31 +01:00
|
|
|
$origNode = $node->getAttribute('origNode');
|
|
|
|
if (null === $origNode) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($node, $precedence, $lhsPrecedence);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-11-13 00:11:19 +01:00
|
|
|
$class = \get_class($node);
|
|
|
|
\assert($class === \get_class($origNode));
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-09-29 17:34:15 +02:00
|
|
|
$startPos = $origNode->getStartTokenPos();
|
|
|
|
$endPos = $origNode->getEndTokenPos();
|
2017-11-04 18:14:12 +01:00
|
|
|
\assert($startPos >= 0 && $endPos >= 0);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-10-29 12:25:13 +01:00
|
|
|
$fallbackNode = $node;
|
2016-12-23 14:11:31 +01:00
|
|
|
if ($node instanceof Expr\New_ && $node->class instanceof Stmt\Class_) {
|
2017-10-29 12:25:13 +01:00
|
|
|
// Normalize node structure of anonymous classes
|
2022-09-11 12:38:46 +02:00
|
|
|
assert($origNode instanceof Expr\New_);
|
2017-10-29 12:25:13 +01:00
|
|
|
$node = PrintableNewAnonClassNode::fromNewNode($node);
|
|
|
|
$origNode = PrintableNewAnonClassNode::fromNewNode($origNode);
|
2022-09-03 20:56:06 +02:00
|
|
|
$class = PrintableNewAnonClassNode::class;
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-12-24 18:04:51 +01:00
|
|
|
// InlineHTML node does not contain closing and opening PHP tags. If the parent formatting
|
|
|
|
// is not preserved, then we need to use the fallback code to make sure the tags are
|
|
|
|
// printed.
|
|
|
|
if ($node instanceof Stmt\InlineHTML && !$parentFormatPreserved) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
2017-12-24 18:04:51 +01:00
|
|
|
}
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$indentAdjustment = $this->indentLevel - $this->origTokens->getIndentationBefore($startPos);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$type = $node->getType();
|
2017-11-13 00:11:19 +01:00
|
|
|
$fixupInfo = $this->fixupMap[$class] ?? null;
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$result = '';
|
|
|
|
$pos = $startPos;
|
2017-02-11 05:05:36 -05:00
|
|
|
foreach ($node->getSubNodeNames() as $subNodeName) {
|
2016-12-23 14:11:31 +01:00
|
|
|
$subNode = $node->$subNodeName;
|
|
|
|
$origSubNode = $origNode->$subNodeName;
|
|
|
|
|
2017-01-21 20:40:05 +01:00
|
|
|
if ((!$subNode instanceof Node && $subNode !== null)
|
|
|
|
|| (!$origSubNode instanceof Node && $origSubNode !== null)
|
|
|
|
) {
|
2016-12-23 14:11:31 +01:00
|
|
|
if ($subNode === $origSubNode) {
|
|
|
|
// Unchanged, can reuse old code
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
if (is_array($subNode) && is_array($origSubNode)) {
|
|
|
|
// Array subnode changed, we might be able to reconstruct it
|
|
|
|
$listResult = $this->pArray(
|
2022-09-03 20:56:06 +02:00
|
|
|
$subNode, $origSubNode, $pos, $indentAdjustment, $class, $subNodeName,
|
2019-05-11 22:12:02 +02:00
|
|
|
$fixupInfo[$subNodeName] ?? null
|
2017-01-21 17:21:01 +01:00
|
|
|
);
|
|
|
|
if (null === $listResult) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
$result .= $listResult;
|
|
|
|
continue;
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:56:09 +02:00
|
|
|
// Check if this is a modifier change
|
|
|
|
$key = $class . '->' . $subNodeName;
|
|
|
|
if (!isset($this->modifierChangeMap[$key])) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
2017-10-25 22:27:29 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:56:09 +02:00
|
|
|
[$printFn, $findToken] = $this->modifierChangeMap[$key];
|
|
|
|
$result .= $this->$printFn($subNode);
|
|
|
|
$pos = $this->origTokens->findRight($pos, $findToken);
|
|
|
|
continue;
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 20:40:05 +01:00
|
|
|
$extraLeft = '';
|
|
|
|
$extraRight = '';
|
|
|
|
if ($origSubNode !== null) {
|
2017-09-29 17:34:15 +02:00
|
|
|
$subStartPos = $origSubNode->getStartTokenPos();
|
|
|
|
$subEndPos = $origSubNode->getEndTokenPos();
|
2017-11-04 18:14:12 +01:00
|
|
|
\assert($subStartPos >= 0 && $subEndPos >= 0);
|
2017-01-21 20:40:05 +01:00
|
|
|
} else {
|
|
|
|
if ($subNode === null) {
|
|
|
|
// Both null, nothing to do
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// A node has been inserted, check if we have insertion information for it
|
|
|
|
$key = $type . '->' . $subNodeName;
|
|
|
|
if (!isset($this->insertionMap[$key])) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
2017-01-21 20:40:05 +01:00
|
|
|
}
|
|
|
|
|
2019-01-05 12:06:18 +01:00
|
|
|
list($findToken, $beforeToken, $extraLeft, $extraRight) = $this->insertionMap[$key];
|
2017-01-21 20:40:05 +01:00
|
|
|
if (null !== $findToken) {
|
2019-01-05 12:06:18 +01:00
|
|
|
$subStartPos = $this->origTokens->findRight($pos, $findToken)
|
|
|
|
+ (int) !$beforeToken;
|
2017-01-21 20:40:05 +01:00
|
|
|
} else {
|
|
|
|
$subStartPos = $pos;
|
|
|
|
}
|
2019-01-05 12:06:18 +01:00
|
|
|
|
2017-03-25 21:21:10 +01:00
|
|
|
if (null === $extraLeft && null !== $extraRight) {
|
|
|
|
// If inserting on the right only, skipping whitespace looks better
|
2017-09-03 18:54:22 +02:00
|
|
|
$subStartPos = $this->origTokens->skipRightWhitespace($subStartPos);
|
2017-03-25 21:21:10 +01:00
|
|
|
}
|
2017-01-21 20:40:05 +01:00
|
|
|
$subEndPos = $subStartPos - 1;
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
if (null === $subNode) {
|
|
|
|
// A node has been removed, check if we have removal information for it
|
|
|
|
$key = $type . '->' . $subNodeName;
|
|
|
|
if (!isset($this->removalMap[$key])) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
return $this->pFallback($fallbackNode, $precedence, $lhsPrecedence);
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adjust positions to account for additional tokens that must be skipped
|
|
|
|
$removalInfo = $this->removalMap[$key];
|
|
|
|
if (isset($removalInfo['left'])) {
|
2017-09-03 18:54:22 +02:00
|
|
|
$subStartPos = $this->origTokens->skipLeft($subStartPos - 1, $removalInfo['left']) + 1;
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
|
|
|
if (isset($removalInfo['right'])) {
|
2017-09-03 18:54:22 +02:00
|
|
|
$subEndPos = $this->origTokens->skipRight($subEndPos + 1, $removalInfo['right']) - 1;
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$result .= $this->origTokens->getTokenCode($pos, $subStartPos, $indentAdjustment);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
if (null !== $subNode) {
|
2017-01-21 20:40:05 +01:00
|
|
|
$result .= $extraLeft;
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$origIndentLevel = $this->indentLevel;
|
|
|
|
$this->setIndentLevel($this->origTokens->getIndentationBefore($subStartPos) + $indentAdjustment);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
// If it's the same node that was previously in this position, it certainly doesn't
|
|
|
|
// need fixup. It's important to check this here, because our fixup checks are more
|
|
|
|
// conservative than strictly necessary.
|
|
|
|
if (isset($fixupInfo[$subNodeName])
|
|
|
|
&& $subNode->getAttribute('origNode') !== $origSubNode
|
|
|
|
) {
|
|
|
|
$fixup = $fixupInfo[$subNodeName];
|
2017-11-13 00:11:19 +01:00
|
|
|
$res = $this->pFixup($fixup, $subNode, $class, $subStartPos, $subEndPos);
|
2017-01-21 17:21:01 +01:00
|
|
|
} else {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$res = $this->p($subNode, self::MAX_PRECEDENCE, self::MAX_PRECEDENCE, true);
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-01-21 17:21:01 +01:00
|
|
|
$this->safeAppend($result, $res);
|
2017-10-06 18:21:08 +02:00
|
|
|
$this->setIndentLevel($origIndentLevel);
|
2017-01-21 20:40:05 +01:00
|
|
|
|
|
|
|
$result .= $extraRight;
|
2017-01-21 17:21:01 +01:00
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$pos = $subEndPos + 1;
|
|
|
|
}
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$result .= $this->origTokens->getTokenCode($pos, $endPos + 1, $indentAdjustment);
|
2016-12-23 14:11:31 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-26 15:40:34 +01:00
|
|
|
* Perform a format-preserving pretty print of an array.
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
2022-09-17 17:12:55 +02:00
|
|
|
* @param Node[] $nodes New nodes
|
|
|
|
* @param Node[] $origNodes Original nodes
|
2017-10-06 13:18:58 +02:00
|
|
|
* @param int $pos Current token position (updated by reference)
|
|
|
|
* @param int $indentAdjustment Adjustment for indentation
|
2022-09-03 20:56:06 +02:00
|
|
|
* @param string $parentNodeClass Class of the containing node.
|
2017-12-26 15:40:34 +01:00
|
|
|
* @param string $subNodeName Name of array subnode.
|
2017-10-06 13:18:58 +02:00
|
|
|
* @param null|int $fixup Fixup information for array item nodes
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
|
|
|
* @return null|string Result of pretty print or null if cannot preserve formatting
|
|
|
|
*/
|
2017-10-06 13:18:58 +02:00
|
|
|
protected function pArray(
|
2022-09-03 20:56:06 +02:00
|
|
|
array $nodes, array $origNodes, int &$pos, int $indentAdjustment,
|
|
|
|
string $parentNodeClass, string $subNodeName, ?int $fixup
|
2022-06-04 12:48:12 +02:00
|
|
|
): ?string {
|
2017-10-05 18:44:45 +02:00
|
|
|
$diff = $this->nodeListDiffer->diffWithReplacements($origNodes, $nodes);
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2022-09-03 20:56:06 +02:00
|
|
|
$mapKey = $parentNodeClass . '->' . $subNodeName;
|
2019-05-11 22:12:02 +02:00
|
|
|
$insertStr = $this->listInsertionMap[$mapKey] ?? null;
|
2020-08-23 20:57:00 +02:00
|
|
|
$isStmtList = $subNodeName === 'stmts';
|
2019-05-11 22:12:02 +02:00
|
|
|
|
2017-12-02 15:10:15 +01:00
|
|
|
$beforeFirstKeepOrReplace = true;
|
2020-08-23 17:33:02 +02:00
|
|
|
$skipRemovedNode = false;
|
2017-12-02 15:10:15 +01:00
|
|
|
$delayedAdd = [];
|
2018-01-25 22:08:40 +01:00
|
|
|
$lastElemIndentLevel = $this->indentLevel;
|
2017-12-01 22:09:51 +01:00
|
|
|
|
2017-12-26 21:13:56 +01:00
|
|
|
$insertNewline = false;
|
|
|
|
if ($insertStr === "\n") {
|
|
|
|
$insertStr = '';
|
|
|
|
$insertNewline = true;
|
|
|
|
}
|
|
|
|
|
2020-08-23 20:57:00 +02:00
|
|
|
if ($isStmtList && \count($origNodes) === 1 && \count($nodes) !== 1) {
|
2017-12-26 15:40:34 +01:00
|
|
|
$startPos = $origNodes[0]->getStartTokenPos();
|
|
|
|
$endPos = $origNodes[0]->getEndTokenPos();
|
|
|
|
\assert($startPos >= 0 && $endPos >= 0);
|
|
|
|
if (!$this->origTokens->haveBraces($startPos, $endPos)) {
|
|
|
|
// This was a single statement without braces, but either additional statements
|
|
|
|
// have been added, or the single statement has been removed. This requires the
|
|
|
|
// addition of braces. For now fall back.
|
|
|
|
// TODO: Try to preserve formatting
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
$result = '';
|
2017-10-06 13:18:58 +02:00
|
|
|
foreach ($diff as $i => $diffElem) {
|
2017-10-05 18:44:45 +02:00
|
|
|
$diffType = $diffElem->type;
|
2022-09-11 12:38:46 +02:00
|
|
|
/** @var Node|string|null $arrItem */
|
2017-10-05 18:44:45 +02:00
|
|
|
$arrItem = $diffElem->new;
|
2022-09-11 12:38:46 +02:00
|
|
|
/** @var Node|string|null $origArrItem */
|
2017-10-05 18:44:45 +02:00
|
|
|
$origArrItem = $diffElem->old;
|
|
|
|
|
2017-10-06 13:18:58 +02:00
|
|
|
if ($diffType === DiffElem::TYPE_KEEP || $diffType === DiffElem::TYPE_REPLACE) {
|
2017-12-02 15:10:15 +01:00
|
|
|
$beforeFirstKeepOrReplace = false;
|
|
|
|
|
2017-10-06 13:18:58 +02:00
|
|
|
if ($origArrItem === null || $arrItem === null) {
|
|
|
|
// We can only handle the case where both are null
|
|
|
|
if ($origArrItem === $arrItem) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-18 15:42:01 +02:00
|
|
|
if (!$arrItem instanceof Node || !$origArrItem instanceof Node) {
|
|
|
|
// We can only deal with nodes. This can occur for Names, which use string arrays.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-10-06 13:18:58 +02:00
|
|
|
$itemStartPos = $origArrItem->getStartTokenPos();
|
|
|
|
$itemEndPos = $origArrItem->getEndTokenPos();
|
2020-09-22 22:41:02 +02:00
|
|
|
\assert($itemStartPos >= 0 && $itemEndPos >= 0 && $itemStartPos >= $pos);
|
2017-10-06 13:18:58 +02:00
|
|
|
|
2017-11-13 13:27:27 +01:00
|
|
|
$origIndentLevel = $this->indentLevel;
|
2018-01-25 22:08:40 +01:00
|
|
|
$lastElemIndentLevel = $this->origTokens->getIndentationBefore($itemStartPos) + $indentAdjustment;
|
|
|
|
$this->setIndentLevel($lastElemIndentLevel);
|
2017-11-13 13:27:27 +01:00
|
|
|
|
2017-11-04 17:56:11 +01:00
|
|
|
$comments = $arrItem->getComments();
|
|
|
|
$origComments = $origArrItem->getComments();
|
2020-02-09 16:53:46 +01:00
|
|
|
$commentStartPos = $origComments ? $origComments[0]->getStartTokenPos() : $itemStartPos;
|
2017-12-01 22:09:51 +01:00
|
|
|
\assert($commentStartPos >= 0);
|
|
|
|
|
2020-09-22 22:41:02 +02:00
|
|
|
if ($commentStartPos < $pos) {
|
|
|
|
// Comments may be assigned to multiple nodes if they start at the same position.
|
|
|
|
// Make sure we don't try to print them multiple times.
|
|
|
|
$commentStartPos = $itemStartPos;
|
2017-12-01 22:09:51 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 22:41:02 +02:00
|
|
|
if ($skipRemovedNode) {
|
2022-09-21 20:38:21 +02:00
|
|
|
if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) ||
|
|
|
|
$this->origTokens->haveTagInRange($pos, $itemStartPos))) {
|
2020-09-22 22:41:02 +02:00
|
|
|
// We'd remove the brace of a code block.
|
|
|
|
// TODO: Preserve formatting.
|
|
|
|
$this->setIndentLevel($origIndentLevel);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
} else {
|
2017-12-01 22:09:51 +01:00
|
|
|
$result .= $this->origTokens->getTokenCode(
|
|
|
|
$pos, $commentStartPos, $indentAdjustment);
|
2020-09-22 22:41:02 +02:00
|
|
|
}
|
2017-11-04 17:56:11 +01:00
|
|
|
|
2020-09-22 22:41:02 +02:00
|
|
|
if (!empty($delayedAdd)) {
|
2017-12-02 15:10:15 +01:00
|
|
|
/** @var Node $delayedAddNode */
|
2017-12-26 17:53:36 +01:00
|
|
|
foreach ($delayedAdd as $delayedAddNode) {
|
2017-12-26 21:13:56 +01:00
|
|
|
if ($insertNewline) {
|
2017-12-02 15:10:15 +01:00
|
|
|
$delayedAddComments = $delayedAddNode->getComments();
|
|
|
|
if ($delayedAddComments) {
|
|
|
|
$result .= $this->pComments($delayedAddComments) . $this->nl;
|
|
|
|
}
|
2017-12-01 22:09:51 +01:00
|
|
|
}
|
2017-11-04 17:56:11 +01:00
|
|
|
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$this->safeAppend($result, $this->p($delayedAddNode, self::MAX_PRECEDENCE, self::MAX_PRECEDENCE, true));
|
2017-11-04 17:56:11 +01:00
|
|
|
|
2017-12-26 21:13:56 +01:00
|
|
|
if ($insertNewline) {
|
|
|
|
$result .= $insertStr . $this->nl;
|
2017-12-02 15:10:15 +01:00
|
|
|
} else {
|
2017-12-26 17:53:36 +01:00
|
|
|
$result .= $insertStr;
|
2017-12-02 15:10:15 +01:00
|
|
|
}
|
2017-11-04 17:56:11 +01:00
|
|
|
}
|
2017-12-01 22:09:51 +01:00
|
|
|
|
2017-12-02 15:10:15 +01:00
|
|
|
$delayedAdd = [];
|
2017-12-01 22:09:51 +01:00
|
|
|
}
|
|
|
|
|
2020-09-22 22:41:02 +02:00
|
|
|
if ($comments !== $origComments) {
|
|
|
|
if ($comments) {
|
|
|
|
$result .= $this->pComments($comments) . $this->nl;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$result .= $this->origTokens->getTokenCode(
|
|
|
|
$commentStartPos, $itemStartPos, $indentAdjustment);
|
2017-11-04 17:56:11 +01:00
|
|
|
}
|
2020-08-23 17:33:02 +02:00
|
|
|
|
|
|
|
// If we had to remove anything, we have done so now.
|
|
|
|
$skipRemovedNode = false;
|
2018-01-13 16:03:36 +01:00
|
|
|
} elseif ($diffType === DiffElem::TYPE_ADD) {
|
2017-10-06 13:18:58 +02:00
|
|
|
if (null === $insertStr) {
|
|
|
|
// We don't have insertion information for this list type
|
|
|
|
return null;
|
|
|
|
}
|
2017-10-02 21:17:41 +02:00
|
|
|
|
2022-09-17 21:14:31 +02:00
|
|
|
if (!$arrItem instanceof Node) {
|
|
|
|
// We only support list insertion of nodes.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2021-09-12 20:51:25 +01:00
|
|
|
// We go multiline if the original code was multiline,
|
|
|
|
// or if it's an array item with a comment above it.
|
2022-09-03 21:02:34 +02:00
|
|
|
// Match always uses multiline formatting.
|
2021-09-12 21:53:48 +02:00
|
|
|
if ($insertStr === ', ' &&
|
2022-09-03 21:02:34 +02:00
|
|
|
($this->isMultiline($origNodes) || $arrItem->getComments() ||
|
|
|
|
$parentNodeClass === Expr\Match_::class)
|
2021-09-12 20:51:25 +01:00
|
|
|
) {
|
2017-12-26 21:13:56 +01:00
|
|
|
$insertStr = ',';
|
|
|
|
$insertNewline = true;
|
|
|
|
}
|
|
|
|
|
2017-12-02 15:10:15 +01:00
|
|
|
if ($beforeFirstKeepOrReplace) {
|
2017-12-26 17:53:36 +01:00
|
|
|
// Will be inserted at the next "replace" or "keep" element
|
|
|
|
$delayedAdd[] = $arrItem;
|
2017-12-01 22:09:51 +01:00
|
|
|
continue;
|
2017-10-06 13:18:58 +02:00
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-10-06 13:18:58 +02:00
|
|
|
$itemStartPos = $pos;
|
|
|
|
$itemEndPos = $pos - 1;
|
2017-01-20 22:27:51 +01:00
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$origIndentLevel = $this->indentLevel;
|
2018-01-25 22:08:40 +01:00
|
|
|
$this->setIndentLevel($lastElemIndentLevel);
|
2017-10-06 17:58:56 +02:00
|
|
|
|
2017-12-26 21:13:56 +01:00
|
|
|
if ($insertNewline) {
|
2021-09-12 20:51:25 +01:00
|
|
|
$result .= $insertStr . $this->nl;
|
2017-10-06 17:58:56 +02:00
|
|
|
$comments = $arrItem->getComments();
|
|
|
|
if ($comments) {
|
2021-09-12 20:51:25 +01:00
|
|
|
$result .= $this->pComments($comments) . $this->nl;
|
2017-10-06 17:58:56 +02:00
|
|
|
}
|
2017-10-06 13:18:58 +02:00
|
|
|
} else {
|
|
|
|
$result .= $insertStr;
|
|
|
|
}
|
2018-01-13 16:03:36 +01:00
|
|
|
} elseif ($diffType === DiffElem::TYPE_REMOVE) {
|
2017-11-04 17:23:17 +01:00
|
|
|
if (!$origArrItem instanceof Node) {
|
|
|
|
// We only support removal for nodes
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-23 17:33:02 +02:00
|
|
|
$itemStartPos = $origArrItem->getStartTokenPos();
|
2017-11-04 17:23:17 +01:00
|
|
|
$itemEndPos = $origArrItem->getEndTokenPos();
|
2020-08-23 17:33:02 +02:00
|
|
|
\assert($itemStartPos >= 0 && $itemEndPos >= 0);
|
|
|
|
|
|
|
|
// Consider comments part of the node.
|
|
|
|
$origComments = $origArrItem->getComments();
|
|
|
|
if ($origComments) {
|
|
|
|
$itemStartPos = $origComments[0]->getStartTokenPos();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($i === 0) {
|
|
|
|
// If we're removing from the start, keep the tokens before the node and drop those after it,
|
|
|
|
// instead of the other way around.
|
|
|
|
$result .= $this->origTokens->getTokenCode(
|
|
|
|
$pos, $itemStartPos, $indentAdjustment);
|
|
|
|
$skipRemovedNode = true;
|
|
|
|
} else {
|
2022-09-21 20:38:21 +02:00
|
|
|
if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) ||
|
|
|
|
$this->origTokens->haveTagInRange($pos, $itemStartPos))) {
|
2020-08-23 17:33:02 +02:00
|
|
|
// We'd remove the brace of a code block.
|
|
|
|
// TODO: Preserve formatting.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 17:23:17 +01:00
|
|
|
|
|
|
|
$pos = $itemEndPos + 1;
|
|
|
|
continue;
|
2017-10-06 13:18:58 +02:00
|
|
|
} else {
|
|
|
|
throw new \Exception("Shouldn't happen");
|
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
if (null !== $fixup && $arrItem->getAttribute('origNode') !== $origArrItem) {
|
|
|
|
$res = $this->pFixup($fixup, $arrItem, null, $itemStartPos, $itemEndPos);
|
|
|
|
} else {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$res = $this->p($arrItem, self::MAX_PRECEDENCE, self::MAX_PRECEDENCE, true);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
$this->safeAppend($result, $res);
|
|
|
|
|
2017-10-06 18:21:08 +02:00
|
|
|
$this->setIndentLevel($origIndentLevel);
|
2016-12-23 14:11:31 +01:00
|
|
|
$pos = $itemEndPos + 1;
|
|
|
|
}
|
2017-12-02 15:10:15 +01:00
|
|
|
|
2020-08-23 17:33:02 +02:00
|
|
|
if ($skipRemovedNode) {
|
|
|
|
// TODO: Support removing single node.
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-12-02 15:10:15 +01:00
|
|
|
if (!empty($delayedAdd)) {
|
2019-05-11 22:12:02 +02:00
|
|
|
if (!isset($this->emptyListInsertionMap[$mapKey])) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
list($findToken, $extraLeft, $extraRight) = $this->emptyListInsertionMap[$mapKey];
|
|
|
|
if (null !== $findToken) {
|
|
|
|
$insertPos = $this->origTokens->findRight($pos, $findToken) + 1;
|
|
|
|
$result .= $this->origTokens->getTokenCode($pos, $insertPos, $indentAdjustment);
|
|
|
|
$pos = $insertPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
$first = true;
|
|
|
|
$result .= $extraLeft;
|
|
|
|
foreach ($delayedAdd as $delayedAddNode) {
|
|
|
|
if (!$first) {
|
|
|
|
$result .= $insertStr;
|
2022-09-04 09:25:36 +02:00
|
|
|
if ($insertNewline) {
|
|
|
|
$result .= $this->nl;
|
|
|
|
}
|
2019-05-11 22:12:02 +02:00
|
|
|
}
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$result .= $this->p($delayedAddNode, self::MAX_PRECEDENCE, self::MAX_PRECEDENCE, true);
|
2019-05-11 22:12:02 +02:00
|
|
|
$first = false;
|
|
|
|
}
|
2022-09-03 10:52:01 +02:00
|
|
|
$result .= $extraRight === "\n" ? $this->nl : $extraRight;
|
2017-12-02 15:10:15 +01:00
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
return $result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print node with fixups.
|
|
|
|
*
|
|
|
|
* Fixups here refer to the addition of extra parentheses, braces or other characters, that
|
|
|
|
* are required to preserve program semantics in a certain context (e.g. to maintain precedence
|
|
|
|
* or because only certain expressions are allowed in certain places).
|
|
|
|
*
|
2017-04-26 21:49:22 +02:00
|
|
|
* @param int $fixup Fixup type
|
|
|
|
* @param Node $subNode Subnode to print
|
2017-11-13 00:11:19 +01:00
|
|
|
* @param string|null $parentClass Class of parent node
|
2017-04-26 21:49:22 +02:00
|
|
|
* @param int $subStartPos Original start pos of subnode
|
|
|
|
* @param int $subEndPos Original end pos of subnode
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
|
|
|
* @return string Result of fixed-up print of subnode
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function pFixup(int $fixup, Node $subNode, ?string $parentClass, int $subStartPos, int $subEndPos): string {
|
2016-12-23 14:11:31 +01:00
|
|
|
switch ($fixup) {
|
|
|
|
case self::FIXUP_PREC_LEFT:
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
// We use a conservative approximation where lhsPrecedence == precedence.
|
|
|
|
if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) {
|
|
|
|
$precedence = $this->precedenceMap[$parentClass][1];
|
|
|
|
return $this->p($subNode, $precedence, $precedence);
|
|
|
|
}
|
|
|
|
break;
|
2016-12-23 14:11:31 +01:00
|
|
|
case self::FIXUP_PREC_RIGHT:
|
2017-09-03 18:54:22 +02:00
|
|
|
if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$precedence = $this->precedenceMap[$parentClass][2];
|
|
|
|
return $this->p($subNode, $precedence, $precedence);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::FIXUP_PREC_UNARY:
|
|
|
|
if (!$this->origTokens->haveParens($subStartPos, $subEndPos)) {
|
|
|
|
$precedence = $this->precedenceMap[$parentClass][0];
|
|
|
|
return $this->p($subNode, $precedence, $precedence);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::FIXUP_CALL_LHS:
|
|
|
|
if ($this->callLhsRequiresParens($subNode)
|
2017-09-03 18:54:22 +02:00
|
|
|
&& !$this->origTokens->haveParens($subStartPos, $subEndPos)
|
2016-12-23 14:11:31 +01:00
|
|
|
) {
|
|
|
|
return '(' . $this->p($subNode) . ')';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::FIXUP_DEREF_LHS:
|
|
|
|
if ($this->dereferenceLhsRequiresParens($subNode)
|
2017-09-03 18:54:22 +02:00
|
|
|
&& !$this->origTokens->haveParens($subStartPos, $subEndPos)
|
2016-12-23 14:11:31 +01:00
|
|
|
) {
|
|
|
|
return '(' . $this->p($subNode) . ')';
|
|
|
|
}
|
|
|
|
break;
|
2023-02-26 15:57:37 +01:00
|
|
|
case self::FIXUP_STATIC_DEREF_LHS:
|
|
|
|
if ($this->staticDereferenceLhsRequiresParens($subNode)
|
|
|
|
&& !$this->origTokens->haveParens($subStartPos, $subEndPos)
|
|
|
|
) {
|
|
|
|
return '(' . $this->p($subNode) . ')';
|
|
|
|
}
|
|
|
|
break;
|
2023-02-26 15:17:07 +01:00
|
|
|
case self::FIXUP_NEW:
|
|
|
|
if ($this->newOperandRequiresParens($subNode)
|
|
|
|
&& !$this->origTokens->haveParens($subStartPos, $subEndPos)) {
|
|
|
|
return '(' . $this->p($subNode) . ')';
|
|
|
|
}
|
|
|
|
break;
|
2016-12-23 14:11:31 +01:00
|
|
|
case self::FIXUP_BRACED_NAME:
|
|
|
|
case self::FIXUP_VAR_BRACED_NAME:
|
|
|
|
if ($subNode instanceof Expr
|
2017-09-03 18:54:22 +02:00
|
|
|
&& !$this->origTokens->haveBraces($subStartPos, $subEndPos)
|
2016-12-23 14:11:31 +01:00
|
|
|
) {
|
|
|
|
return ($fixup === self::FIXUP_VAR_BRACED_NAME ? '$' : '')
|
|
|
|
. '{' . $this->p($subNode) . '}';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case self::FIXUP_ENCAPSED:
|
2022-09-03 13:25:23 +02:00
|
|
|
if (!$subNode instanceof Node\InterpolatedStringPart
|
2017-09-03 18:54:22 +02:00
|
|
|
&& !$this->origTokens->haveBraces($subStartPos, $subEndPos)
|
2016-12-23 14:11:31 +01:00
|
|
|
) {
|
|
|
|
return '{' . $this->p($subNode) . '}';
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
throw new \Exception('Cannot happen');
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nothing special to do
|
|
|
|
return $this->p($subNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends to a string, ensuring whitespace between label characters.
|
|
|
|
*
|
|
|
|
* Example: "echo" and "$x" result in "echo$x", but "echo" and "x" result in "echo x".
|
|
|
|
* Without safeAppend the result would be "echox", which does not preserve semantics.
|
|
|
|
*
|
|
|
|
* @param string $str
|
|
|
|
* @param string $append
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function safeAppend(string &$str, string $append): void {
|
2016-12-23 14:11:31 +01:00
|
|
|
if ($str === "") {
|
|
|
|
$str = $append;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-07-06 22:19:43 +08:00
|
|
|
if ($append === "") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
if (!$this->labelCharMap[$append[0]]
|
|
|
|
|| !$this->labelCharMap[$str[\strlen($str) - 1]]) {
|
|
|
|
$str .= $append;
|
|
|
|
} else {
|
|
|
|
$str .= " " . $append;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the LHS of a call must be wrapped in parenthesis.
|
|
|
|
*
|
|
|
|
* @param Node $node LHS of a call
|
|
|
|
*
|
|
|
|
* @return bool Whether parentheses are required
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function callLhsRequiresParens(Node $node): bool {
|
2016-12-23 14:11:31 +01:00
|
|
|
return !($node instanceof Node\Name
|
|
|
|
|| $node instanceof Expr\Variable
|
|
|
|
|| $node instanceof Expr\ArrayDimFetch
|
|
|
|
|| $node instanceof Expr\FuncCall
|
|
|
|
|| $node instanceof Expr\MethodCall
|
2020-08-02 10:30:44 +02:00
|
|
|
|| $node instanceof Expr\NullsafeMethodCall
|
2016-12-23 14:11:31 +01:00
|
|
|
|| $node instanceof Expr\StaticCall
|
|
|
|
|| $node instanceof Expr\Array_);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-02-26 15:57:37 +01:00
|
|
|
* Determines whether the LHS of an array/object operation must be wrapped in parentheses.
|
2016-12-23 14:11:31 +01:00
|
|
|
*
|
|
|
|
* @param Node $node LHS of dereferencing operation
|
|
|
|
*
|
|
|
|
* @return bool Whether parentheses are required
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function dereferenceLhsRequiresParens(Node $node): bool {
|
2023-02-26 15:57:37 +01:00
|
|
|
// A constant can occur on the LHS of an array/object deref, but not a static deref.
|
|
|
|
return $this->staticDereferenceLhsRequiresParens($node)
|
|
|
|
&& !$node instanceof Expr\ConstFetch;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Determines whether the LHS of a static operation must be wrapped in parentheses.
|
|
|
|
*
|
|
|
|
* @param Node $node LHS of dereferencing operation
|
|
|
|
*
|
|
|
|
* @return bool Whether parentheses are required
|
|
|
|
*/
|
|
|
|
protected function staticDereferenceLhsRequiresParens(Node $node): bool {
|
2016-12-23 14:11:31 +01:00
|
|
|
return !($node instanceof Expr\Variable
|
|
|
|
|| $node instanceof Node\Name
|
|
|
|
|| $node instanceof Expr\ArrayDimFetch
|
|
|
|
|| $node instanceof Expr\PropertyFetch
|
2020-08-02 10:30:44 +02:00
|
|
|
|| $node instanceof Expr\NullsafePropertyFetch
|
2016-12-23 14:11:31 +01:00
|
|
|
|| $node instanceof Expr\StaticPropertyFetch
|
|
|
|
|| $node instanceof Expr\FuncCall
|
|
|
|
|| $node instanceof Expr\MethodCall
|
2020-08-02 10:30:44 +02:00
|
|
|
|| $node instanceof Expr\NullsafeMethodCall
|
2016-12-23 14:11:31 +01:00
|
|
|
|| $node instanceof Expr\StaticCall
|
|
|
|
|| $node instanceof Expr\Array_
|
|
|
|
|| $node instanceof Scalar\String_
|
|
|
|
|| $node instanceof Expr\ClassConstFetch);
|
|
|
|
}
|
|
|
|
|
2023-02-26 15:11:36 +01:00
|
|
|
/**
|
|
|
|
* Determines whether an expression used in "new" or "instanceof" requires parentheses.
|
|
|
|
*
|
|
|
|
* @param Node $node New or instanceof operand
|
|
|
|
*
|
|
|
|
* @return bool Whether parentheses are required
|
|
|
|
*/
|
|
|
|
protected function newOperandRequiresParens(Node $node): bool {
|
2023-02-27 19:00:33 +01:00
|
|
|
if ($node instanceof Node\Name || $node instanceof Expr\Variable) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if ($node instanceof Expr\ArrayDimFetch || $node instanceof Expr\PropertyFetch ||
|
|
|
|
$node instanceof Expr\NullsafePropertyFetch
|
|
|
|
) {
|
|
|
|
return $this->newOperandRequiresParens($node->var);
|
|
|
|
}
|
|
|
|
if ($node instanceof Expr\StaticPropertyFetch) {
|
|
|
|
return $this->newOperandRequiresParens($node->class);
|
|
|
|
}
|
|
|
|
return true;
|
2023-02-26 15:11:36 +01:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:27:29 +02:00
|
|
|
/**
|
|
|
|
* Print modifiers, including trailing whitespace.
|
|
|
|
*
|
|
|
|
* @param int $modifiers Modifier mask to print
|
|
|
|
*
|
|
|
|
* @return string Printed modifiers
|
|
|
|
*/
|
2022-06-04 12:48:12 +02:00
|
|
|
protected function pModifiers(int $modifiers): string {
|
2022-08-28 22:57:06 +02:00
|
|
|
return ($modifiers & Modifiers::FINAL ? 'final ' : '')
|
|
|
|
. ($modifiers & Modifiers::ABSTRACT ? 'abstract ' : '')
|
|
|
|
. ($modifiers & Modifiers::PUBLIC ? 'public ' : '')
|
2022-08-28 21:09:00 +02:00
|
|
|
. ($modifiers & Modifiers::PROTECTED ? 'protected ' : '')
|
2022-08-28 22:57:06 +02:00
|
|
|
. ($modifiers & Modifiers::PRIVATE ? 'private ' : '')
|
|
|
|
. ($modifiers & Modifiers::STATIC ? 'static ' : '')
|
|
|
|
. ($modifiers & Modifiers::READONLY ? 'readonly ' : '');
|
2017-10-25 22:27:29 +02:00
|
|
|
}
|
|
|
|
|
2022-09-21 18:56:09 +02:00
|
|
|
protected function pStatic(bool $static): string {
|
|
|
|
return $static ? 'static ' : '';
|
|
|
|
}
|
|
|
|
|
2017-12-26 21:13:56 +01:00
|
|
|
/**
|
|
|
|
* Determine whether a list of nodes uses multiline formatting.
|
|
|
|
*
|
|
|
|
* @param (Node|null)[] $nodes Node list
|
|
|
|
*
|
|
|
|
* @return bool Whether multiline formatting is used
|
|
|
|
*/
|
2022-08-28 22:57:06 +02:00
|
|
|
protected function isMultiline(array $nodes): bool {
|
2017-12-26 21:13:56 +01:00
|
|
|
if (\count($nodes) < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$pos = -1;
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
if (null === $node) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
$endPos = $node->getEndTokenPos() + 1;
|
|
|
|
if ($pos >= 0) {
|
|
|
|
$text = $this->origTokens->getTokenCode($pos, $endPos, 0);
|
|
|
|
if (false === strpos($text, "\n")) {
|
|
|
|
// We require that a newline is present between *every* item. If the formatting
|
|
|
|
// is inconsistent, with only some items having newlines, we don't consider it
|
|
|
|
// as multiline
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$pos = $endPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
/**
|
|
|
|
* Lazily initializes label char map.
|
|
|
|
*
|
|
|
|
* The label char map determines whether a certain character may occur in a label.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeLabelCharMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->labelCharMap) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$this->labelCharMap = [];
|
|
|
|
for ($i = 0; $i < 256; $i++) {
|
|
|
|
// Since PHP 7.1 The lower range is 0x80. However, we also want to support code for
|
|
|
|
// older versions.
|
2021-07-09 15:46:50 +02:00
|
|
|
$chr = chr($i);
|
|
|
|
$this->labelCharMap[$chr] = $i >= 0x7f || ctype_alnum($chr);
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-05 18:44:45 +02:00
|
|
|
/**
|
|
|
|
* Lazily initializes node list differ.
|
|
|
|
*
|
|
|
|
* The node list differ is used to determine differences between two array subnodes.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeNodeListDiffer(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->nodeListDiffer) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-05 18:44:45 +02:00
|
|
|
|
|
|
|
$this->nodeListDiffer = new Internal\Differ(function ($a, $b) {
|
|
|
|
if ($a instanceof Node && $b instanceof Node) {
|
|
|
|
return $a === $b->getAttribute('origNode');
|
|
|
|
}
|
|
|
|
// Can happen for array destructuring
|
|
|
|
return $a === null && $b === null;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2016-12-23 14:11:31 +01:00
|
|
|
/**
|
|
|
|
* Lazily initializes fixup map.
|
|
|
|
*
|
|
|
|
* The fixup map is used to determine whether a certain subnode of a certain node may require
|
|
|
|
* some kind of "fixup" operation, e.g. the addition of parenthesis or braces.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeFixupMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->fixupMap) {
|
|
|
|
return;
|
|
|
|
}
|
2016-12-23 14:11:31 +01:00
|
|
|
|
|
|
|
$this->fixupMap = [
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\Instanceof_::class => [
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
'expr' => self::FIXUP_PREC_UNARY,
|
2023-02-26 15:17:07 +01:00
|
|
|
'class' => self::FIXUP_NEW,
|
2016-12-23 14:11:31 +01:00
|
|
|
],
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\Ternary::class => [
|
2016-12-23 14:11:31 +01:00
|
|
|
'cond' => self::FIXUP_PREC_LEFT,
|
|
|
|
'else' => self::FIXUP_PREC_RIGHT,
|
|
|
|
],
|
2023-02-26 18:44:40 +01:00
|
|
|
Expr\Yield_::class => ['value' => self::FIXUP_PREC_UNARY],
|
2016-12-23 14:11:31 +01:00
|
|
|
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\FuncCall::class => ['name' => self::FIXUP_CALL_LHS],
|
2023-02-26 15:57:37 +01:00
|
|
|
Expr\StaticCall::class => ['class' => self::FIXUP_STATIC_DEREF_LHS],
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\ArrayDimFetch::class => ['var' => self::FIXUP_DEREF_LHS],
|
2023-02-26 16:00:35 +01:00
|
|
|
Expr\ClassConstFetch::class => [
|
|
|
|
'class' => self::FIXUP_STATIC_DEREF_LHS,
|
|
|
|
'name' => self::FIXUP_BRACED_NAME,
|
|
|
|
],
|
2023-02-26 15:17:07 +01:00
|
|
|
Expr\New_::class => ['class' => self::FIXUP_NEW],
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\MethodCall::class => [
|
2016-12-23 14:11:31 +01:00
|
|
|
'var' => self::FIXUP_DEREF_LHS,
|
|
|
|
'name' => self::FIXUP_BRACED_NAME,
|
|
|
|
],
|
2020-08-02 10:30:44 +02:00
|
|
|
Expr\NullsafeMethodCall::class => [
|
|
|
|
'var' => self::FIXUP_DEREF_LHS,
|
|
|
|
'name' => self::FIXUP_BRACED_NAME,
|
|
|
|
],
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\StaticPropertyFetch::class => [
|
2023-02-26 15:57:37 +01:00
|
|
|
'class' => self::FIXUP_STATIC_DEREF_LHS,
|
2016-12-23 14:11:31 +01:00
|
|
|
'name' => self::FIXUP_VAR_BRACED_NAME,
|
|
|
|
],
|
2017-11-13 00:11:19 +01:00
|
|
|
Expr\PropertyFetch::class => [
|
2016-12-23 14:11:31 +01:00
|
|
|
'var' => self::FIXUP_DEREF_LHS,
|
|
|
|
'name' => self::FIXUP_BRACED_NAME,
|
|
|
|
],
|
2020-08-02 10:30:44 +02:00
|
|
|
Expr\NullsafePropertyFetch::class => [
|
|
|
|
'var' => self::FIXUP_DEREF_LHS,
|
|
|
|
'name' => self::FIXUP_BRACED_NAME,
|
|
|
|
],
|
2022-09-03 15:13:42 +02:00
|
|
|
Scalar\InterpolatedString::class => [
|
2016-12-23 14:11:31 +01:00
|
|
|
'parts' => self::FIXUP_ENCAPSED,
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$binaryOps = [
|
2017-11-13 00:11:19 +01:00
|
|
|
BinaryOp\Pow::class, BinaryOp\Mul::class, BinaryOp\Div::class, BinaryOp\Mod::class,
|
|
|
|
BinaryOp\Plus::class, BinaryOp\Minus::class, BinaryOp\Concat::class,
|
|
|
|
BinaryOp\ShiftLeft::class, BinaryOp\ShiftRight::class, BinaryOp\Smaller::class,
|
|
|
|
BinaryOp\SmallerOrEqual::class, BinaryOp\Greater::class, BinaryOp\GreaterOrEqual::class,
|
|
|
|
BinaryOp\Equal::class, BinaryOp\NotEqual::class, BinaryOp\Identical::class,
|
|
|
|
BinaryOp\NotIdentical::class, BinaryOp\Spaceship::class, BinaryOp\BitwiseAnd::class,
|
|
|
|
BinaryOp\BitwiseXor::class, BinaryOp\BitwiseOr::class, BinaryOp\BooleanAnd::class,
|
|
|
|
BinaryOp\BooleanOr::class, BinaryOp\Coalesce::class, BinaryOp\LogicalAnd::class,
|
|
|
|
BinaryOp\LogicalXor::class, BinaryOp\LogicalOr::class,
|
2016-12-23 14:11:31 +01:00
|
|
|
];
|
|
|
|
foreach ($binaryOps as $binaryOp) {
|
|
|
|
$this->fixupMap[$binaryOp] = [
|
|
|
|
'left' => self::FIXUP_PREC_LEFT,
|
|
|
|
'right' => self::FIXUP_PREC_RIGHT
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$prefixOps = [
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
Expr\Clone_::class, Expr\BitwiseNot::class, Expr\BooleanNot::class, Expr\UnaryPlus::class, Expr\UnaryMinus::class,
|
2017-11-13 00:11:19 +01:00
|
|
|
Cast\Int_::class, Cast\Double::class, Cast\String_::class, Cast\Array_::class,
|
|
|
|
Cast\Object_::class, Cast\Bool_::class, Cast\Unset_::class, Expr\ErrorSuppress::class,
|
|
|
|
Expr\YieldFrom::class, Expr\Print_::class, Expr\Include_::class,
|
2022-07-23 21:53:48 +02:00
|
|
|
Expr\Assign::class, Expr\AssignRef::class, AssignOp\Plus::class, AssignOp\Minus::class,
|
|
|
|
AssignOp\Mul::class, AssignOp\Div::class, AssignOp\Concat::class, AssignOp\Mod::class,
|
|
|
|
AssignOp\BitwiseAnd::class, AssignOp\BitwiseOr::class, AssignOp\BitwiseXor::class,
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
AssignOp\ShiftLeft::class, AssignOp\ShiftRight::class, AssignOp\Pow::class, AssignOp\Coalesce::class,
|
2023-02-26 18:33:24 +01:00
|
|
|
Expr\ArrowFunction::class, Expr\Throw_::class,
|
2016-12-23 14:11:31 +01:00
|
|
|
];
|
|
|
|
foreach ($prefixOps as $prefixOp) {
|
Properly support prefix operator precedence printing
The pretty printer is supposed to produce a minimal-parentheses
printing for expressions. However, for prefix operators, we were
failing to do so in ways that are practically meaningful, e.g.
$a = yield from $b produced redundant parentheses around the
yield from.
For prefix operators, the precedence of the direct parent operator
is not actually relevant: What matters is the precedence of any
infix operator whose LHS it appears on. For example, $a . include $b
does not require parentheses, but (include $a) . $b does.
To handle this, keep separate track of the parent operator
precedence, and a special LHS precedence which is used for unary
operator printing only. Because the LHS precedence may not come
from the direct parent, we have to pass it down the call stack.
And if we do that, we may as well do the same for the parent
precedence.
This also fixes an integration test failure with php-src, because
arrow function precedence was previously not respected (and would
be ugly to respect without this change).
2023-02-25 21:05:08 +01:00
|
|
|
$this->fixupMap[$prefixOp] = ['expr' => self::FIXUP_PREC_UNARY];
|
2016-12-23 14:11:31 +01:00
|
|
|
}
|
|
|
|
}
|
2017-01-21 17:21:01 +01:00
|
|
|
|
2017-01-21 20:40:05 +01:00
|
|
|
/**
|
|
|
|
* Lazily initializes the removal map.
|
|
|
|
*
|
2021-04-25 21:11:36 +02:00
|
|
|
* The removal map is used to determine which additional tokens should be removed when a
|
2017-01-21 20:40:05 +01:00
|
|
|
* certain node is replaced by null.
|
|
|
|
*/
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeRemovalMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->removalMap) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-21 17:21:01 +01:00
|
|
|
|
2017-11-10 23:33:12 +01:00
|
|
|
$stripBoth = ['left' => \T_WHITESPACE, 'right' => \T_WHITESPACE];
|
|
|
|
$stripLeft = ['left' => \T_WHITESPACE];
|
|
|
|
$stripRight = ['right' => \T_WHITESPACE];
|
|
|
|
$stripDoubleArrow = ['right' => \T_DOUBLE_ARROW];
|
2017-01-21 17:21:01 +01:00
|
|
|
$stripColon = ['left' => ':'];
|
|
|
|
$stripEquals = ['left' => '='];
|
|
|
|
$this->removalMap = [
|
|
|
|
'Expr_ArrayDimFetch->dim' => $stripBoth,
|
2022-09-03 19:04:22 +02:00
|
|
|
'ArrayItem->key' => $stripDoubleArrow,
|
2019-05-09 14:17:28 +02:00
|
|
|
'Expr_ArrowFunction->returnType' => $stripColon,
|
2017-01-21 17:21:01 +01:00
|
|
|
'Expr_Closure->returnType' => $stripColon,
|
|
|
|
'Expr_Exit->expr' => $stripBoth,
|
|
|
|
'Expr_Ternary->if' => $stripBoth,
|
|
|
|
'Expr_Yield->key' => $stripDoubleArrow,
|
|
|
|
'Expr_Yield->value' => $stripBoth,
|
|
|
|
'Param->type' => $stripRight,
|
|
|
|
'Param->default' => $stripEquals,
|
|
|
|
'Stmt_Break->num' => $stripBoth,
|
2020-06-27 17:42:46 +02:00
|
|
|
'Stmt_Catch->var' => $stripLeft,
|
2017-01-21 17:21:01 +01:00
|
|
|
'Stmt_ClassMethod->returnType' => $stripColon,
|
2017-11-10 23:33:12 +01:00
|
|
|
'Stmt_Class->extends' => ['left' => \T_EXTENDS],
|
2021-04-25 21:11:36 +02:00
|
|
|
'Stmt_Enum->scalarType' => $stripColon,
|
|
|
|
'Stmt_EnumCase->expr' => $stripEquals,
|
2017-11-10 23:33:12 +01:00
|
|
|
'Expr_PrintableNewAnonClass->extends' => ['left' => \T_EXTENDS],
|
2017-01-21 17:21:01 +01:00
|
|
|
'Stmt_Continue->num' => $stripBoth,
|
|
|
|
'Stmt_Foreach->keyVar' => $stripDoubleArrow,
|
|
|
|
'Stmt_Function->returnType' => $stripColon,
|
|
|
|
'Stmt_If->else' => $stripLeft,
|
|
|
|
'Stmt_Namespace->name' => $stripLeft,
|
2019-01-05 12:06:18 +01:00
|
|
|
'Stmt_Property->type' => $stripRight,
|
2022-09-03 18:55:22 +02:00
|
|
|
'PropertyItem->default' => $stripEquals,
|
2017-01-21 17:21:01 +01:00
|
|
|
'Stmt_Return->expr' => $stripBoth,
|
|
|
|
'Stmt_StaticVar->default' => $stripEquals,
|
|
|
|
'Stmt_TraitUseAdaptation_Alias->newName' => $stripLeft,
|
|
|
|
'Stmt_TryCatch->finally' => $stripLeft,
|
|
|
|
// 'Stmt_Case->cond': Replace with "default"
|
|
|
|
// 'Stmt_Class->name': Unclear what to do
|
|
|
|
// 'Stmt_Declare->stmts': Not a plain node
|
|
|
|
// 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a plain node
|
|
|
|
];
|
|
|
|
}
|
2017-01-21 20:40:05 +01:00
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeInsertionMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->insertionMap) {
|
|
|
|
return;
|
|
|
|
}
|
2017-01-21 20:40:05 +01:00
|
|
|
|
|
|
|
// TODO: "yield" where both key and value are inserted doesn't work
|
2019-05-11 22:12:02 +02:00
|
|
|
// [$find, $beforeToken, $extraLeft, $extraRight]
|
2017-01-21 20:40:05 +01:00
|
|
|
$this->insertionMap = [
|
2019-01-05 12:06:18 +01:00
|
|
|
'Expr_ArrayDimFetch->dim' => ['[', false, null, null],
|
2022-09-03 19:04:22 +02:00
|
|
|
'ArrayItem->key' => [null, false, null, ' => '],
|
2022-06-04 23:23:07 +02:00
|
|
|
'Expr_ArrowFunction->returnType' => [')', false, ': ', null],
|
|
|
|
'Expr_Closure->returnType' => [')', false, ': ', null],
|
2019-01-05 12:06:18 +01:00
|
|
|
'Expr_Ternary->if' => ['?', false, ' ', ' '],
|
|
|
|
'Expr_Yield->key' => [\T_YIELD, false, null, ' => '],
|
|
|
|
'Expr_Yield->value' => [\T_YIELD, false, ' ', null],
|
|
|
|
'Param->type' => [null, false, null, ' '],
|
|
|
|
'Param->default' => [null, false, ' = ', null],
|
|
|
|
'Stmt_Break->num' => [\T_BREAK, false, ' ', null],
|
2020-06-27 17:42:46 +02:00
|
|
|
'Stmt_Catch->var' => [null, false, ' ', null],
|
2022-06-04 23:23:07 +02:00
|
|
|
'Stmt_ClassMethod->returnType' => [')', false, ': ', null],
|
2019-01-05 12:06:18 +01:00
|
|
|
'Stmt_Class->extends' => [null, false, ' extends ', null],
|
2021-04-25 21:11:36 +02:00
|
|
|
'Stmt_Enum->scalarType' => [null, false, ' : ', null],
|
|
|
|
'Stmt_EnumCase->expr' => [null, false, ' = ', null],
|
2022-09-17 17:12:55 +02:00
|
|
|
'Expr_PrintableNewAnonClass->extends' => [null, false, ' extends ', null],
|
2019-01-05 12:06:18 +01:00
|
|
|
'Stmt_Continue->num' => [\T_CONTINUE, false, ' ', null],
|
|
|
|
'Stmt_Foreach->keyVar' => [\T_AS, false, null, ' => '],
|
2022-06-04 23:23:07 +02:00
|
|
|
'Stmt_Function->returnType' => [')', false, ': ', null],
|
2019-01-05 12:06:18 +01:00
|
|
|
'Stmt_If->else' => [null, false, ' ', null],
|
|
|
|
'Stmt_Namespace->name' => [\T_NAMESPACE, false, ' ', null],
|
|
|
|
'Stmt_Property->type' => [\T_VARIABLE, true, null, ' '],
|
2022-09-03 18:55:22 +02:00
|
|
|
'PropertyItem->default' => [null, false, ' = ', null],
|
2019-01-05 12:06:18 +01:00
|
|
|
'Stmt_Return->expr' => [\T_RETURN, false, ' ', null],
|
|
|
|
'Stmt_StaticVar->default' => [null, false, ' = ', null],
|
|
|
|
//'Stmt_TraitUseAdaptation_Alias->newName' => [T_AS, false, ' ', null], // TODO
|
|
|
|
'Stmt_TryCatch->finally' => [null, false, ' ', null],
|
2017-01-21 20:40:05 +01:00
|
|
|
|
|
|
|
// 'Expr_Exit->expr': Complicated due to optional ()
|
|
|
|
// 'Stmt_Case->cond': Conversion from default to case
|
|
|
|
// 'Stmt_Class->name': Unclear
|
|
|
|
// 'Stmt_Declare->stmts': Not a proper node
|
|
|
|
// 'Stmt_TraitUseAdaptation_Alias->newModifier': Not a proper node
|
|
|
|
];
|
|
|
|
}
|
2017-10-06 13:18:58 +02:00
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeListInsertionMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->listInsertionMap) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-06 13:18:58 +02:00
|
|
|
|
|
|
|
$this->listInsertionMap = [
|
|
|
|
// special
|
|
|
|
//'Expr_ShellExec->parts' => '', // TODO These need to be treated more carefully
|
2022-09-03 15:13:42 +02:00
|
|
|
//'Scalar_InterpolatedString->parts' => '',
|
2022-09-03 20:56:06 +02:00
|
|
|
Stmt\Catch_::class . '->types' => '|',
|
|
|
|
UnionType::class . '->types' => '|',
|
|
|
|
IntersectionType::class . '->types' => '&',
|
|
|
|
Stmt\If_::class . '->elseifs' => ' ',
|
|
|
|
Stmt\TryCatch::class . '->catches' => ' ',
|
2017-10-06 13:18:58 +02:00
|
|
|
|
|
|
|
// comma-separated lists
|
2022-09-03 20:56:06 +02:00
|
|
|
Expr\Array_::class . '->items' => ', ',
|
|
|
|
Expr\ArrowFunction::class . '->params' => ', ',
|
|
|
|
Expr\Closure::class . '->params' => ', ',
|
|
|
|
Expr\Closure::class . '->uses' => ', ',
|
|
|
|
Expr\FuncCall::class . '->args' => ', ',
|
|
|
|
Expr\Isset_::class . '->vars' => ', ',
|
|
|
|
Expr\List_::class . '->items' => ', ',
|
|
|
|
Expr\MethodCall::class . '->args' => ', ',
|
|
|
|
Expr\NullsafeMethodCall::class . '->args' => ', ',
|
|
|
|
Expr\New_::class . '->args' => ', ',
|
|
|
|
PrintableNewAnonClassNode::class . '->args' => ', ',
|
|
|
|
Expr\StaticCall::class . '->args' => ', ',
|
|
|
|
Stmt\ClassConst::class . '->consts' => ', ',
|
|
|
|
Stmt\ClassMethod::class . '->params' => ', ',
|
|
|
|
Stmt\Class_::class . '->implements' => ', ',
|
|
|
|
Stmt\Enum_::class . '->implements' => ', ',
|
|
|
|
PrintableNewAnonClassNode::class . '->implements' => ', ',
|
|
|
|
Stmt\Const_::class . '->consts' => ', ',
|
|
|
|
Stmt\Declare_::class . '->declares' => ', ',
|
|
|
|
Stmt\Echo_::class . '->exprs' => ', ',
|
|
|
|
Stmt\For_::class . '->init' => ', ',
|
|
|
|
Stmt\For_::class . '->cond' => ', ',
|
|
|
|
Stmt\For_::class . '->loop' => ', ',
|
|
|
|
Stmt\Function_::class . '->params' => ', ',
|
|
|
|
Stmt\Global_::class . '->vars' => ', ',
|
|
|
|
Stmt\GroupUse::class . '->uses' => ', ',
|
|
|
|
Stmt\Interface_::class . '->extends' => ', ',
|
2022-09-03 21:02:34 +02:00
|
|
|
Expr\Match_::class . '->arms' => ', ',
|
2022-09-03 20:56:06 +02:00
|
|
|
Stmt\Property::class . '->props' => ', ',
|
|
|
|
Stmt\StaticVar::class . '->vars' => ', ',
|
|
|
|
Stmt\TraitUse::class . '->traits' => ', ',
|
|
|
|
Stmt\TraitUseAdaptation\Precedence::class . '->insteadof' => ', ',
|
|
|
|
Stmt\Unset_::class . '->vars' => ', ',
|
|
|
|
Stmt\UseUse::class . '->uses' => ', ',
|
|
|
|
MatchArm::class . '->conds' => ', ',
|
|
|
|
AttributeGroup::class . '->attrs' => ', ',
|
2017-10-06 13:18:58 +02:00
|
|
|
|
|
|
|
// statement lists
|
2022-09-03 20:56:06 +02:00
|
|
|
Expr\Closure::class . '->stmts' => "\n",
|
|
|
|
Stmt\Case_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Catch_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Class_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Enum_::class . '->stmts' => "\n",
|
|
|
|
PrintableNewAnonClassNode::class . '->stmts' => "\n",
|
|
|
|
Stmt\Interface_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Trait_::class . '->stmts' => "\n",
|
|
|
|
Stmt\ClassMethod::class . '->stmts' => "\n",
|
|
|
|
Stmt\Declare_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Do_::class . '->stmts' => "\n",
|
|
|
|
Stmt\ElseIf_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Else_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Finally_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Foreach_::class . '->stmts' => "\n",
|
|
|
|
Stmt\For_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Function_::class . '->stmts' => "\n",
|
|
|
|
Stmt\If_::class . '->stmts' => "\n",
|
|
|
|
Stmt\Namespace_::class . '->stmts' => "\n",
|
|
|
|
|
|
|
|
// Attribute groups
|
|
|
|
Stmt\Class_::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\Enum_::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\EnumCase::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\Interface_::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\Trait_::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\Function_::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\ClassMethod::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\ClassConst::class . '->attrGroups' => "\n",
|
|
|
|
Stmt\Property::class . '->attrGroups' => "\n",
|
|
|
|
PrintableNewAnonClassNode::class . '->attrGroups' => ' ',
|
|
|
|
Expr\Closure::class . '->attrGroups' => ' ',
|
|
|
|
Expr\ArrowFunction::class . '->attrGroups' => ' ',
|
|
|
|
Param::class . '->attrGroups' => ' ',
|
|
|
|
Stmt\Switch_::class . '->cases' => "\n",
|
|
|
|
Stmt\TraitUse::class . '->adaptations' => "\n",
|
|
|
|
Stmt\TryCatch::class . '->stmts' => "\n",
|
|
|
|
Stmt\While_::class . '->stmts' => "\n",
|
2019-05-11 22:12:02 +02:00
|
|
|
|
|
|
|
// dummy for top-level context
|
|
|
|
'File->stmts' => "\n",
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeEmptyListInsertionMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->emptyListInsertionMap) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-11 22:12:02 +02:00
|
|
|
|
|
|
|
// TODO Insertion into empty statement lists.
|
|
|
|
|
|
|
|
// [$find, $extraLeft, $extraRight]
|
|
|
|
$this->emptyListInsertionMap = [
|
2022-09-03 20:56:06 +02:00
|
|
|
Expr\ArrowFunction::class . '->params' => ['(', '', ''],
|
|
|
|
Expr\Closure::class . '->uses' => [')', ' use (', ')'],
|
|
|
|
Expr\Closure::class . '->params' => ['(', '', ''],
|
|
|
|
Expr\FuncCall::class . '->args' => ['(', '', ''],
|
|
|
|
Expr\MethodCall::class . '->args' => ['(', '', ''],
|
|
|
|
Expr\NullsafeMethodCall::class . '->args' => ['(', '', ''],
|
|
|
|
Expr\New_::class . '->args' => ['(', '', ''],
|
|
|
|
PrintableNewAnonClassNode::class . '->args' => ['(', '', ''],
|
|
|
|
PrintableNewAnonClassNode::class . '->implements' => [null, ' implements ', ''],
|
|
|
|
Expr\StaticCall::class . '->args' => ['(', '', ''],
|
|
|
|
Stmt\Class_::class . '->implements' => [null, ' implements ', ''],
|
|
|
|
Stmt\Enum_::class . '->implements' => [null, ' implements ', ''],
|
|
|
|
Stmt\ClassMethod::class . '->params' => ['(', '', ''],
|
|
|
|
Stmt\Interface_::class . '->extends' => [null, ' extends ', ''],
|
|
|
|
Stmt\Function_::class . '->params' => ['(', '', ''],
|
|
|
|
Stmt\Interface_::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\Class_::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\ClassConst::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\ClassMethod::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\Function_::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\Property::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Stmt\Trait_::class . '->attrGroups' => [null, '', "\n"],
|
|
|
|
Expr\ArrowFunction::class . '->attrGroups' => [null, '', ' '],
|
|
|
|
Expr\Closure::class . '->attrGroups' => [null, '', ' '],
|
|
|
|
PrintableNewAnonClassNode::class . '->attrGroups' => [\T_NEW, ' ', ''],
|
2019-05-11 22:12:02 +02:00
|
|
|
|
|
|
|
/* These cannot be empty to start with:
|
|
|
|
* Expr_Isset->vars
|
|
|
|
* Stmt_Catch->types
|
|
|
|
* Stmt_Const->consts
|
|
|
|
* Stmt_ClassConst->consts
|
|
|
|
* Stmt_Declare->declares
|
|
|
|
* Stmt_Echo->exprs
|
|
|
|
* Stmt_Global->vars
|
|
|
|
* Stmt_GroupUse->uses
|
|
|
|
* Stmt_Property->props
|
|
|
|
* Stmt_StaticVar->vars
|
|
|
|
* Stmt_TraitUse->traits
|
|
|
|
* Stmt_TraitUseAdaptation_Precedence->insteadof
|
|
|
|
* Stmt_Unset->vars
|
|
|
|
* Stmt_Use->uses
|
2019-10-25 21:35:43 +02:00
|
|
|
* UnionType->types
|
2019-05-11 22:12:02 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* TODO
|
|
|
|
* Stmt_If->elseifs
|
|
|
|
* Stmt_TryCatch->catches
|
|
|
|
* Expr_Array->items
|
|
|
|
* Expr_List->items
|
|
|
|
* Stmt_For->init
|
|
|
|
* Stmt_For->cond
|
|
|
|
* Stmt_For->loop
|
|
|
|
*/
|
2017-10-06 13:18:58 +02:00
|
|
|
];
|
|
|
|
}
|
2017-10-25 22:27:29 +02:00
|
|
|
|
2022-09-11 17:51:59 +02:00
|
|
|
protected function initializeModifierChangeMap(): void {
|
2022-08-28 22:57:06 +02:00
|
|
|
if ($this->modifierChangeMap) {
|
|
|
|
return;
|
|
|
|
}
|
2017-10-25 22:27:29 +02:00
|
|
|
|
|
|
|
$this->modifierChangeMap = [
|
2022-09-21 18:56:09 +02:00
|
|
|
Stmt\ClassConst::class . '->flags' => ['pModifiers', \T_CONST],
|
|
|
|
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_FUNCTION],
|
|
|
|
Stmt\Class_::class . '->flags' => ['pModifiers', \T_CLASS],
|
|
|
|
Stmt\Property::class . '->flags' => ['pModifiers', \T_VARIABLE],
|
|
|
|
Param::class . '->flags' => ['pModifiers', \T_VARIABLE],
|
|
|
|
Expr\Closure::class . '->static' => ['pStatic', \T_FUNCTION],
|
|
|
|
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_FN],
|
2022-09-03 19:11:35 +02:00
|
|
|
//Stmt\TraitUseAdaptation\Alias::class . '->newModifier' => 0, // TODO
|
2017-10-25 22:27:29 +02:00
|
|
|
];
|
2017-11-04 17:23:17 +01:00
|
|
|
|
|
|
|
// List of integer subnodes that are not modifiers:
|
|
|
|
// Expr_Include->type
|
|
|
|
// Stmt_GroupUse->type
|
|
|
|
// Stmt_Use->type
|
2022-09-03 18:59:48 +02:00
|
|
|
// UseItem->type
|
2017-10-25 22:27:29 +02:00
|
|
|
}
|
2015-03-12 22:45:38 +01:00
|
|
|
}
|