mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-27 04:14:44 +01:00
Add initial implementation of pretty printer
This commit is contained in:
parent
d4f3cdd695
commit
acab6f2826
@ -1,5 +1,9 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property Node_Expr $left The left hand side expression
|
||||
* @property Node_Expr $right The right hand side expression
|
||||
*/
|
||||
class Node_Expr_Minus extends Node_Expr
|
||||
{
|
||||
}
|
@ -1,8 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property Node_Variable $var Variable holding object
|
||||
* @property string|Node_Expr $name Name
|
||||
* @property Node_Variable $var Variable holding object
|
||||
* @property string|Node_Expr|Node_Variable $name Name
|
||||
*/
|
||||
class Node_Expr_PropertyFetch extends Node_Expr
|
||||
{
|
||||
|
@ -1,5 +1,8 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @property string $value String value
|
||||
*/
|
||||
class Node_Scalar_String extends Node_Scalar
|
||||
{
|
||||
}
|
96
lib/PrettyPrinter/Zend.php
Normal file
96
lib/PrettyPrinter/Zend.php
Normal file
@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
class PrettyPrinter_Zend extends PrettyPrinterAbstract
|
||||
{
|
||||
public function pName(Node_Name $node) {
|
||||
return implode('\\', $node->parts);
|
||||
}
|
||||
|
||||
public function pVariable(Node_Variable $node) {
|
||||
if ($node->name instanceof Node_Expr) {
|
||||
return '${' . $this->p($node->name) . '}';
|
||||
} elseif ($node->name instanceof Node_Variable) {
|
||||
return '$' . $this->p($node->name);
|
||||
} else {
|
||||
return '$' . $node->name;
|
||||
}
|
||||
}
|
||||
|
||||
public function pScalar_FileConst(Node_Scalar_FileConst $node) {
|
||||
return '__FILE__';
|
||||
}
|
||||
|
||||
public function pScalar_String(Node_Scalar_String $node) {
|
||||
return '\'' . addslashes($node->value) . '\'';
|
||||
}
|
||||
|
||||
public function pExpr_Assign(Node_Expr_Assign $node) {
|
||||
return $this->p($node->var) . ' = ' . $this->p($node->expr);
|
||||
}
|
||||
|
||||
public function pExpr_BooleanAnd(Node_Expr_BooleanAnd $node) {
|
||||
return $this->p($node->left) . ' && ' . $this->p($node->right);
|
||||
}
|
||||
|
||||
public function pExpr_Concat(Node_Expr_Concat $node) {
|
||||
return $this->p($node->left) . ' . ' . $this->p($node->right);
|
||||
}
|
||||
|
||||
public function pExpr_ConstFetch(Node_Expr_ConstFetch $node) {
|
||||
return $this->p($node->name);
|
||||
}
|
||||
|
||||
public function pExpr_FuncCall(Node_Expr_FuncCall $node) {
|
||||
return $this->p($node->func) . '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
}
|
||||
|
||||
public function pExpr_FuncCallArg(Node_Expr_FuncCallArg $node) {
|
||||
return ($node->byRef ? '&' : '') . $this->p($node->value);
|
||||
}
|
||||
|
||||
public function pExpr_MethodCall(Node_Expr_MethodCall $node) {
|
||||
return $this->p($node->var) . '->' . $this->pObjectProperty($node->name)
|
||||
. '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
}
|
||||
|
||||
public function pExpr_Minus(Node_Expr_Minus $node) {
|
||||
return $this->p($node->left) . ' - ' . $this->p($node->right);
|
||||
}
|
||||
|
||||
public function pExpr_New(Node_Expr_New $node) {
|
||||
return 'new ' . $this->p($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
|
||||
}
|
||||
|
||||
public function pExpr_PropertyFetch(Node_Expr_PropertyFetch $node) {
|
||||
return $this->p($node->var) . '->' . $this->pObjectProperty($node->name);
|
||||
}
|
||||
|
||||
public function pStmt_Echo(Node_Stmt_Echo $node) {
|
||||
return 'echo ' . $this->pCommaSeparated($node->exprs);
|
||||
}
|
||||
|
||||
public function pStmt_Func(Node_Stmt_Func $node) {
|
||||
return 'function ' . ($node->byRef ? '&' : '') . $node->name
|
||||
. '(' . $this->pCommaSeparated($node->params) . ')' . "\n" . '{' . "\n"
|
||||
. $this->pIndent($this->pStmts($node->stmts)) . '}';
|
||||
}
|
||||
|
||||
public function pStmt_FuncParam(Node_Stmt_FuncParam $node) {
|
||||
return ($node->type ? ('array' == $node->type ? 'array' : $this->p($node->type)) . ' ' : '')
|
||||
. ($node->byRef ? '&' : '')
|
||||
. '$' . $node->name
|
||||
. ($node->default ? ' = ' . $this->p($node->default) : '');
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
public function pObjectProperty($node) {
|
||||
if ($node instanceof Node_Expr) {
|
||||
return '{' . $this->p($node) . '}';
|
||||
} elseif ($node instanceof Node_Variable) {
|
||||
return $this->pVariable($node);
|
||||
} else {
|
||||
return $node;
|
||||
}
|
||||
}
|
||||
}
|
47
lib/PrettyPrinterAbstract.php
Normal file
47
lib/PrettyPrinterAbstract.php
Normal file
@ -0,0 +1,47 @@
|
||||
<?php
|
||||
|
||||
abstract class PrettyPrinterAbstract
|
||||
{
|
||||
public function pCommaSeparated(array $nodes) {
|
||||
$pNodes = array();
|
||||
foreach ($nodes as $node) {
|
||||
$pNodes[] = $this->p($node);
|
||||
}
|
||||
return implode(', ', $pNodes);
|
||||
}
|
||||
|
||||
public function pStmts(array $nodes) {
|
||||
$return = '';
|
||||
foreach ($nodes as $node) {
|
||||
$return .= $this->p($node);
|
||||
|
||||
if ($node instanceof Node_Stmt_Func) {
|
||||
$return .= "\n";
|
||||
} else {
|
||||
$return .= ';' . "\n";
|
||||
}
|
||||
}
|
||||
return $return;
|
||||
}
|
||||
|
||||
public function pIndent($string) {
|
||||
$lines = explode("\n", $string);
|
||||
foreach ($lines as &$line) {
|
||||
if ('' !== $line) {
|
||||
$line = ' ' . $line;
|
||||
}
|
||||
}
|
||||
|
||||
return implode("\n", $lines);
|
||||
}
|
||||
|
||||
public function p(NodeAbstract $node) {
|
||||
if (method_exists($this, 'p' . $node->getType())) {
|
||||
return $this->{'p' . $node->getType()}($node);
|
||||
} else {
|
||||
echo 'Missing: ' . 'p' . $node->getType() . "\n";
|
||||
|
||||
return '';
|
||||
}
|
||||
}
|
||||
}
|
9
test.php
9
test.php
@ -19,7 +19,7 @@ $stmts = $parser->yyparse(new Lexer(
|
||||
$x->$y[z]();
|
||||
$x->$$y[z]();'
|
||||
),
|
||||
function($msg) {
|
||||
function ($msg) {
|
||||
echo $msg;
|
||||
}
|
||||
);
|
||||
@ -31,6 +31,13 @@ if (false !== $stmts) {
|
||||
|
||||
echo "\n\n";
|
||||
|
||||
$prettyPrinter = new PrettyPrinter_Zend;
|
||||
echo $prettyPrinter->pStmts($parser->yyparse(new Lexer(file_get_contents(__FILE__)), function ($msg) {
|
||||
echo $msg;
|
||||
}));
|
||||
|
||||
echo "\n\n";
|
||||
|
||||
// Correctness Demo
|
||||
$GST = microtime(true);
|
||||
foreach (new RecursiveIteratorIterator(
|
||||
|
Loading…
Reference in New Issue
Block a user