2011-05-27 22:57:55 +02:00
|
|
|
<?php
|
|
|
|
|
2011-06-05 18:40:04 +02:00
|
|
|
abstract class PHPParser_PrettyPrinterAbstract
|
2011-05-27 22:57:55 +02:00
|
|
|
{
|
2011-05-29 12:20:47 +02:00
|
|
|
protected $precedanceMap = array(
|
|
|
|
'Expr_BinaryNot' => 1,
|
|
|
|
'Expr_PreInc' => 1,
|
|
|
|
'Expr_PreDec' => 1,
|
|
|
|
'Expr_PostInc' => 1,
|
|
|
|
'Expr_PostDec' => 1,
|
2011-06-02 23:15:56 +02:00
|
|
|
'Expr_UnaryPlus' => 1,
|
|
|
|
'Expr_UnaryMinus' => 1,
|
2011-05-29 12:20:47 +02:00
|
|
|
'Expr_IntCast' => 1,
|
|
|
|
'Expr_DoubleCast' => 1,
|
|
|
|
'Expr_StringCast' => 1,
|
|
|
|
'Expr_ArrayCast' => 1,
|
|
|
|
'Expr_ObjectCast' => 1,
|
|
|
|
'Expr_BoolCast' => 1,
|
|
|
|
'Expr_UnsetCast' => 1,
|
|
|
|
'Expr_ErrorSuppress' => 1,
|
|
|
|
'Expr_Instanceof' => 2,
|
|
|
|
'Expr_BooleanNot' => 3,
|
|
|
|
'Expr_Mul' => 4,
|
|
|
|
'Expr_Div' => 4,
|
|
|
|
'Expr_Mod' => 4,
|
|
|
|
'Expr_Plus' => 5,
|
|
|
|
'Expr_Minus' => 5,
|
|
|
|
'Expr_Concat' => 5,
|
|
|
|
'Expr_ShiftLeft' => 6,
|
|
|
|
'Expr_ShiftRight' => 6,
|
|
|
|
'Expr_Smaller' => 7,
|
|
|
|
'Expr_SmallerOrEqual' => 7,
|
|
|
|
'Expr_Greater' => 7,
|
|
|
|
'Expr_GreaterOrEqual' => 7,
|
|
|
|
'Expr_Equal' => 8,
|
|
|
|
'Expr_NotEqual' => 8,
|
|
|
|
'Expr_Identical' => 8,
|
|
|
|
'Expr_NotIdentical' => 8,
|
|
|
|
'Expr_BinaryAnd' => 9,
|
|
|
|
'Expr_BinaryXor' => 10,
|
|
|
|
'Expr_BinaryOr' => 11,
|
|
|
|
'Expr_BooleanAnd' => 12,
|
|
|
|
'Expr_BooleanOr' => 13,
|
|
|
|
'Expr_Ternary' => 14,
|
|
|
|
'Expr_Assign' => 15,
|
|
|
|
'Expr_AssignPlus' => 15,
|
|
|
|
'Expr_AssignMinus' => 15,
|
|
|
|
'Expr_AssignMul' => 15,
|
|
|
|
'Expr_AssignDiv' => 15,
|
|
|
|
'Expr_AssignConcat' => 15,
|
|
|
|
'Expr_AssignMod' => 15,
|
|
|
|
'Expr_AssignBinaryAnd' => 15,
|
|
|
|
'Expr_AssignBinaryOr' => 15,
|
|
|
|
'Expr_AssignBinaryXor' => 15,
|
|
|
|
'Expr_AssignShiftLeft' => 15,
|
|
|
|
'Expr_AssignShiftRight' => 15,
|
|
|
|
'Expr_LogicalAnd' => 16,
|
|
|
|
'Expr_LogicalXor' => 17,
|
|
|
|
'Expr_LogicalOr' => 18,
|
|
|
|
);
|
2011-06-02 22:52:24 +02:00
|
|
|
protected $stmtsWithoutSemicolon = array(
|
2011-08-04 18:19:45 +02:00
|
|
|
'Stmt_Func' => true,
|
|
|
|
'Stmt_Interface' => true,
|
|
|
|
'Stmt_Class' => true,
|
|
|
|
'Stmt_ClassMethod' => true,
|
|
|
|
'Stmt_For' => true,
|
|
|
|
'Stmt_Foreach' => true,
|
|
|
|
'Stmt_If' => true,
|
|
|
|
'Stmt_Switch' => true,
|
|
|
|
'Stmt_While' => true,
|
|
|
|
'Stmt_TryCatch' => true,
|
|
|
|
'Stmt_Label' => true,
|
2011-06-03 22:02:02 +02:00
|
|
|
'Stmt_HaltCompiler' => true,
|
2011-08-04 18:19:45 +02:00
|
|
|
'Stmt_Namespace' => true,
|
2011-06-02 22:52:24 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
protected $precedenceStack;
|
|
|
|
protected $precedenceStackPos;
|
|
|
|
protected $noIndentToken;
|
|
|
|
|
2011-08-04 18:19:45 +02:00
|
|
|
public function __construct() {
|
|
|
|
$this->precedenceStack = array($this->precedenceStackPos = 0 => 19);
|
|
|
|
$this->noIndentToken = uniqid('_NO_INDENT_');
|
|
|
|
}
|
|
|
|
|
2011-06-02 22:52:24 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints an array of nodes (statements).
|
|
|
|
*
|
|
|
|
* @param array $nodes Array of nodes
|
|
|
|
*
|
|
|
|
* @return string Pretty printed nodes
|
|
|
|
*/
|
|
|
|
public function prettyPrint(array $nodes) {
|
|
|
|
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
|
|
|
|
}
|
2011-05-29 12:20:47 +02:00
|
|
|
|
2011-08-04 18:19:45 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints an expression.
|
|
|
|
*
|
|
|
|
* @param PHPParser_Node_Expr $node Expression node
|
|
|
|
*
|
|
|
|
* @return string Pretty printed node
|
|
|
|
*/
|
|
|
|
public function prettyPrintExpr(PHPParser_Node_Expr $node) {
|
|
|
|
return str_replace("\n" . $this->noIndentToken, "\n", $this->p($node));
|
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2011-06-02 22:52:24 +02:00
|
|
|
* @param array $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) {
|
|
|
|
$pNodes = array();
|
2011-05-27 22:57:55 +02:00
|
|
|
foreach ($nodes as $node) {
|
2011-07-03 16:35:45 +02:00
|
|
|
$pNodes[] = ((null !== $docComment = $node->getDocComment())
|
|
|
|
? preg_replace('~^\s+\*~m', ' *', $docComment) . "\n"
|
|
|
|
: '')
|
|
|
|
. $this->p($node)
|
2011-06-02 22:52:24 +02:00
|
|
|
. (isset($this->stmtsWithoutSemicolon[$node->getType()]) ? '' : ';');
|
|
|
|
}
|
2011-05-27 22:57:55 +02:00
|
|
|
|
2011-06-02 22:52:24 +02:00
|
|
|
if ($indent) {
|
|
|
|
return ' ' . preg_replace(
|
|
|
|
'~\n(?!$|' . $this->noIndentToken . ')~',
|
|
|
|
"\n" . ' ',
|
|
|
|
implode("\n", $pNodes)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return implode("\n", $pNodes);
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-05-31 16:33:11 +02:00
|
|
|
/**
|
|
|
|
* Pretty prints a node.
|
|
|
|
*
|
2011-06-05 18:40:04 +02:00
|
|
|
* @param PHPParser_NodeAbstract $node Node to be pretty printed
|
2011-05-31 16:33:11 +02:00
|
|
|
*
|
|
|
|
* @return string Pretty printed node
|
|
|
|
*/
|
2011-06-05 18:40:04 +02:00
|
|
|
protected function p(PHPParser_NodeAbstract $node) {
|
2011-05-29 12:20:47 +02:00
|
|
|
$type = $node->getType();
|
|
|
|
|
|
|
|
if (isset($this->precedanceMap[$type])) {
|
|
|
|
$precedence = $this->precedanceMap[$type];
|
|
|
|
|
2011-05-30 22:11:11 +02:00
|
|
|
if ($precedence >= $this->precedenceStack[$this->precedenceStackPos]) {
|
2011-05-29 12:20:47 +02:00
|
|
|
$this->precedenceStack[++$this->precedenceStackPos] = $precedence;
|
|
|
|
$return = '(' . $this->{'p' . $type}($node) . ')';
|
|
|
|
--$this->precedenceStackPos;
|
|
|
|
} else {
|
|
|
|
$this->precedenceStack[++$this->precedenceStackPos] = $precedence;
|
|
|
|
$return = $this->{'p' . $type}($node);
|
|
|
|
--$this->precedenceStackPos;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $return;
|
|
|
|
} else {
|
|
|
|
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.
|
|
|
|
*
|
|
|
|
* @param array $nodes Array of Nodes to be printed
|
|
|
|
* @param string $glue Character to implode with
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*
|
|
|
|
* @param array $nodes Array of Nodes to be printed
|
|
|
|
*
|
|
|
|
* @return string Comma separated pretty printed nodes
|
|
|
|
*/
|
|
|
|
protected function pCommaSeparated(array $nodes) {
|
|
|
|
return $this->pImplode($nodes, ', ');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2011-06-02 22:52:24 +02:00
|
|
|
* Signifies 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
|
|
|
*/
|
2011-06-02 22:52:24 +02:00
|
|
|
protected function pSafe($string) {
|
|
|
|
return str_replace("\n", "\n" . $this->noIndentToken, $string);
|
2011-05-31 16:33:11 +02:00
|
|
|
}
|
2011-05-27 22:57:55 +02:00
|
|
|
}
|