mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-01-17 15:18:17 +01:00
Add possibility to pretty print expressions to PrettyPrinter
This commit is contained in:
parent
29bac2a120
commit
684a638f46
@ -153,4 +153,6 @@ For the code mentioned in the above section this should create the output:
|
|||||||
{
|
{
|
||||||
echo $msg, "\n";
|
echo $msg, "\n";
|
||||||
}
|
}
|
||||||
printLine('Hallo World!!!');
|
printLine('Hallo World!!!');
|
||||||
|
|
||||||
|
You can also pretty print only a single expression using the `prettyPrintExpr()` method.
|
@ -59,25 +59,30 @@ abstract class PHPParser_PrettyPrinterAbstract
|
|||||||
'Expr_LogicalOr' => 18,
|
'Expr_LogicalOr' => 18,
|
||||||
);
|
);
|
||||||
protected $stmtsWithoutSemicolon = array(
|
protected $stmtsWithoutSemicolon = array(
|
||||||
'Stmt_Func' => true,
|
'Stmt_Func' => true,
|
||||||
'Stmt_Interface' => true,
|
'Stmt_Interface' => true,
|
||||||
'Stmt_Class' => true,
|
'Stmt_Class' => true,
|
||||||
'Stmt_ClassMethod' => true,
|
'Stmt_ClassMethod' => true,
|
||||||
'Stmt_For' => true,
|
'Stmt_For' => true,
|
||||||
'Stmt_Foreach' => true,
|
'Stmt_Foreach' => true,
|
||||||
'Stmt_If' => true,
|
'Stmt_If' => true,
|
||||||
'Stmt_Switch' => true,
|
'Stmt_Switch' => true,
|
||||||
'Stmt_While' => true,
|
'Stmt_While' => true,
|
||||||
'Stmt_TryCatch' => true,
|
'Stmt_TryCatch' => true,
|
||||||
'Stmt_Label' => true,
|
'Stmt_Label' => true,
|
||||||
'Stmt_HaltCompiler' => true,
|
'Stmt_HaltCompiler' => true,
|
||||||
'Stmt_Namespace' => true,
|
'Stmt_Namespace' => true,
|
||||||
);
|
);
|
||||||
|
|
||||||
protected $precedenceStack;
|
protected $precedenceStack;
|
||||||
protected $precedenceStackPos;
|
protected $precedenceStackPos;
|
||||||
protected $noIndentToken;
|
protected $noIndentToken;
|
||||||
|
|
||||||
|
public function __construct() {
|
||||||
|
$this->precedenceStack = array($this->precedenceStackPos = 0 => 19);
|
||||||
|
$this->noIndentToken = uniqid('_NO_INDENT_');
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pretty prints an array of nodes (statements).
|
* Pretty prints an array of nodes (statements).
|
||||||
*
|
*
|
||||||
@ -86,12 +91,20 @@ abstract class PHPParser_PrettyPrinterAbstract
|
|||||||
* @return string Pretty printed nodes
|
* @return string Pretty printed nodes
|
||||||
*/
|
*/
|
||||||
public function prettyPrint(array $nodes) {
|
public function prettyPrint(array $nodes) {
|
||||||
$this->precedenceStack = array($this->precedenceStackPos = 0 => 19);
|
|
||||||
$this->noIndentToken = uniqid('_NO_INDENT_');
|
|
||||||
|
|
||||||
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
|
return str_replace("\n" . $this->noIndentToken, "\n", $this->pStmts($nodes, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pretty prints an array of nodes (statements) and indents them optionally.
|
* Pretty prints an array of nodes (statements) and indents them optionally.
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user