2011-05-27 22:57:55 +02:00
|
|
|
<?php
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
namespace PhpParser;
|
|
|
|
|
|
|
|
use PhpParser\Node\Expr;
|
|
|
|
use PhpParser\Node\Stmt;
|
|
|
|
|
|
|
|
abstract class PrettyPrinterAbstract
|
2011-05-27 22:57:55 +02:00
|
|
|
{
|
2012-05-11 16:18:14 +02:00
|
|
|
protected $precedenceMap = array(
|
2012-10-31 17:34:06 +01:00
|
|
|
// [precedence, associativity] where for the latter -1 is %left, 0 is %nonassoc and 1 is %right
|
2014-03-26 19:18:16 +01:00
|
|
|
'Expr_BinaryOp_Pow' => array( 0, 1),
|
2014-02-06 14:44:16 +01:00
|
|
|
'Expr_BitwiseNot' => array( 1, 1),
|
|
|
|
'Expr_PreInc' => array( 1, 1),
|
|
|
|
'Expr_PreDec' => array( 1, 1),
|
|
|
|
'Expr_PostInc' => array( 1, -1),
|
|
|
|
'Expr_PostDec' => array( 1, -1),
|
|
|
|
'Expr_UnaryPlus' => array( 1, 1),
|
|
|
|
'Expr_UnaryMinus' => array( 1, 1),
|
|
|
|
'Expr_Cast_Int' => array( 1, 1),
|
|
|
|
'Expr_Cast_Double' => array( 1, 1),
|
|
|
|
'Expr_Cast_String' => array( 1, 1),
|
|
|
|
'Expr_Cast_Array' => array( 1, 1),
|
|
|
|
'Expr_Cast_Object' => array( 1, 1),
|
|
|
|
'Expr_Cast_Bool' => array( 1, 1),
|
|
|
|
'Expr_Cast_Unset' => array( 1, 1),
|
|
|
|
'Expr_ErrorSuppress' => array( 1, 1),
|
|
|
|
'Expr_Instanceof' => array( 2, 0),
|
|
|
|
'Expr_BooleanNot' => array( 3, 1),
|
|
|
|
'Expr_BinaryOp_Mul' => array( 4, -1),
|
|
|
|
'Expr_BinaryOp_Div' => array( 4, -1),
|
|
|
|
'Expr_BinaryOp_Mod' => array( 4, -1),
|
|
|
|
'Expr_BinaryOp_Plus' => array( 5, -1),
|
|
|
|
'Expr_BinaryOp_Minus' => array( 5, -1),
|
|
|
|
'Expr_BinaryOp_Concat' => array( 5, -1),
|
|
|
|
'Expr_BinaryOp_ShiftLeft' => array( 6, -1),
|
|
|
|
'Expr_BinaryOp_ShiftRight' => array( 6, -1),
|
|
|
|
'Expr_BinaryOp_Smaller' => array( 7, 0),
|
|
|
|
'Expr_BinaryOp_SmallerOrEqual' => array( 7, 0),
|
|
|
|
'Expr_BinaryOp_Greater' => array( 7, 0),
|
|
|
|
'Expr_BinaryOp_GreaterOrEqual' => array( 7, 0),
|
|
|
|
'Expr_BinaryOp_Equal' => array( 8, 0),
|
|
|
|
'Expr_BinaryOp_NotEqual' => array( 8, 0),
|
|
|
|
'Expr_BinaryOp_Identical' => array( 8, 0),
|
|
|
|
'Expr_BinaryOp_NotIdentical' => array( 8, 0),
|
|
|
|
'Expr_BinaryOp_BitwiseAnd' => array( 9, -1),
|
|
|
|
'Expr_BinaryOp_BitwiseXor' => array(10, -1),
|
|
|
|
'Expr_BinaryOp_BitwiseOr' => array(11, -1),
|
|
|
|
'Expr_BinaryOp_BooleanAnd' => array(12, -1),
|
|
|
|
'Expr_BinaryOp_BooleanOr' => array(13, -1),
|
|
|
|
'Expr_Ternary' => array(14, -1),
|
2012-10-31 17:34:06 +01:00
|
|
|
// parser uses %left for assignments, but they really behave as %right
|
2014-02-06 14:44:16 +01:00
|
|
|
'Expr_Assign' => array(15, 1),
|
|
|
|
'Expr_AssignRef' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Plus' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Minus' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Mul' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Div' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Concat' => array(15, 1),
|
|
|
|
'Expr_AssignOp_Mod' => array(15, 1),
|
|
|
|
'Expr_AssignOp_BitwiseAnd' => array(15, 1),
|
|
|
|
'Expr_AssignOp_BitwiseOr' => array(15, 1),
|
|
|
|
'Expr_AssignOp_BitwiseXor' => array(15, 1),
|
|
|
|
'Expr_AssignOp_ShiftLeft' => array(15, 1),
|
|
|
|
'Expr_AssignOp_ShiftRight' => array(15, 1),
|
2014-03-26 19:18:16 +01:00
|
|
|
'Expr_AssignOp_Pow' => array(15, 1),
|
2014-02-06 14:44:16 +01:00
|
|
|
'Expr_BinaryOp_LogicalAnd' => array(16, -1),
|
|
|
|
'Expr_BinaryOp_LogicalXor' => array(17, -1),
|
|
|
|
'Expr_BinaryOp_LogicalOr' => array(18, -1),
|
|
|
|
'Expr_Include' => array(19, -1),
|
2011-05-29 12:20:47 +02:00
|
|
|
);
|
2011-06-02 22:52:24 +02:00
|
|
|
|
|
|
|
protected $noIndentToken;
|
2013-01-15 18:21:42 +01:00
|
|
|
protected $canUseSemicolonNamespaces;
|
2011-06-02 22:52:24 +02:00
|
|
|
|
2011-08-04 18:19:45 +02:00
|
|
|
public function __construct() {
|
2013-07-13 01:03:57 +02:00
|
|
|
$this->noIndentToken = '_NO_INDENT_' . mt_rand();
|
2011-08-04 18:19:45 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*/
|
2013-04-15 20:53:23 +02:00
|
|
|
public function prettyPrint(array $stmts) {
|
|
|
|
$this->preprocessNodes($stmts);
|
2013-01-15 18:21:42 +01:00
|
|
|
|
2014-03-27 12:30:03 +01:00
|
|
|
return trim(str_replace("\n" . $this->noIndentToken, "\n", $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
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
public function prettyPrintExpr(Expr $node) {
|
2011-08-04 18:19:45 +02:00
|
|
|
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
public function prettyPrintFile(array $stmts) {
|
2014-03-27 12:30:03 +01:00
|
|
|
$p = $this->prettyPrint($stmts);
|
2013-04-15 20:53:23 +02:00
|
|
|
|
|
|
|
$p = preg_replace('/^\?>\n?/', '', $p, -1, $count);
|
|
|
|
$p = preg_replace('/<\?php$/', '', $p);
|
|
|
|
|
|
|
|
if (!$count) {
|
|
|
|
$p = "<?php\n\n" . $p;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
*/
|
|
|
|
protected function preprocessNodes(array $nodes) {
|
|
|
|
/* 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
2011-06-02 22:52:24 +02:00
|
|
|
protected function pStmts(array $nodes, $indent = true) {
|
2014-03-27 12:30:03 +01:00
|
|
|
$result = '';
|
2011-05-27 22:57:55 +02:00
|
|
|
foreach ($nodes as $node) {
|
2014-03-27 12:30:03 +01:00
|
|
|
$result .= "\n"
|
|
|
|
. $this->pComments($node->getAttribute('comments', array()))
|
|
|
|
. $this->p($node)
|
|
|
|
. ($node instanceof Expr ? ';' : '');
|
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) {
|
2014-03-27 12:30:03 +01:00
|
|
|
return preg_replace('~\n(?!$|' . $this->noIndentToken . ')~', "\n ", $result);
|
2011-06-02 22:52:24 +02:00
|
|
|
} else {
|
2014-03-27 12:30:03 +01:00
|
|
|
return $result;
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints a node.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node $node Node to be pretty printed
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return string Pretty printed node
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function p(Node $node) {
|
2012-10-31 17:34:06 +01:00
|
|
|
return $this->{'p' . $node->getType()}($node);
|
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function pInfixOp($type, Node $leftNode, $operatorString, Node $rightNode) {
|
2012-10-31 17:34:06 +01:00
|
|
|
list($precedence, $associativity) = $this->precedenceMap[$type];
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2012-10-31 17:34:06 +01:00
|
|
|
return $this->pPrec($leftNode, $precedence, $associativity, -1)
|
|
|
|
. $operatorString
|
|
|
|
. $this->pPrec($rightNode, $precedence, $associativity, 1);
|
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function pPrefixOp($type, $operatorString, Node $node) {
|
2012-10-31 17:34:06 +01:00
|
|
|
list($precedence, $associativity) = $this->precedenceMap[$type];
|
|
|
|
return $operatorString . $this->pPrec($node, $precedence, $associativity, 1);
|
|
|
|
}
|
|
|
|
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function pPostfixOp($type, Node $node, $operatorString) {
|
2012-10-31 17:34:06 +01:00
|
|
|
list($precedence, $associativity) = $this->precedenceMap[$type];
|
|
|
|
return $this->pPrec($node, $precedence, $associativity, -1) . $operatorString;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Prints an expression node with the least amount of parentheses necessary to preserve the meaning.
|
|
|
|
*
|
2014-02-06 14:44:16 +01:00
|
|
|
* @param Node $node Node to pretty print
|
|
|
|
* @param int $parentPrecedence Precedence of the parent operator
|
|
|
|
* @param int $parentAssociativity Associativity of parent operator
|
|
|
|
* (-1 is left, 0 is nonassoc, 1 is right)
|
|
|
|
* @param int $childPosition Position of the node relative to the operator
|
|
|
|
* (-1 is left, 1 is right)
|
2012-10-31 17:34:06 +01:00
|
|
|
*
|
|
|
|
* @return string The pretty printed node
|
|
|
|
*/
|
2014-02-06 14:44:16 +01:00
|
|
|
protected function pPrec(Node $node, $parentPrecedence, $parentAssociativity, $childPosition) {
|
2012-10-31 17:34:06 +01:00
|
|
|
$type = $node->getType();
|
|
|
|
if (isset($this->precedenceMap[$type])) {
|
|
|
|
$childPrecedence = $this->precedenceMap[$type][0];
|
|
|
|
if ($childPrecedence > $parentPrecedence
|
|
|
|
|| ($parentPrecedence == $childPrecedence && $parentAssociativity != $childPosition)
|
|
|
|
) {
|
|
|
|
return '(' . $this->{'p' . $type}($node) . ')';
|
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
}
|
2012-10-31 17:34:06 +01:00
|
|
|
|
|
|
|
return $this->{'p' . $type}($node);
|
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
|
|
|
*
|
|
|
|
* @return string Imploded pretty printed nodes
|
|
|
|
*/
|
|
|
|
protected function pImplode(array $nodes, $glue = '') {
|
|
|
|
$pNodes = array();
|
|
|
|
foreach ($nodes as $node) {
|
|
|
|
$pNodes[] = $this->p($node);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
protected function pCommaSeparated(array $nodes) {
|
|
|
|
return $this->pImplode($nodes, ', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-01-15 18:21:42 +01:00
|
|
|
* Signals the pretty printer that a string shall not be indented.
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
2011-06-02 22:52:24 +02:00
|
|
|
* @param string $string Not to be indented string
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
2011-06-02 22:52:24 +02:00
|
|
|
* @return mixed String marked with $this->noIndentToken's.
|
2011-05-31 16:33:11 +02:00
|
|
|
*/
|
2012-10-31 17:50:54 +01:00
|
|
|
protected function pNoIndent($string) {
|
2011-06-02 22:52:24 +02:00
|
|
|
return str_replace("\n", "\n" . $this->noIndentToken, $string);
|
2011-05-31 16:33:11 +02:00
|
|
|
}
|
2012-05-11 16:18:14 +02:00
|
|
|
|
|
|
|
protected function pComments(array $comments) {
|
|
|
|
$result = '';
|
|
|
|
|
|
|
|
foreach ($comments as $comment) {
|
|
|
|
$result .= $comment->getReformattedText() . "\n";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
}
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|