1
0
mirror of https://github.com/danog/PHP-Parser.git synced 2024-11-27 04:14:44 +01:00

Furthre work on PrettyPrinter

This commit is contained in:
nikic 2011-05-30 17:29:10 +02:00
parent 9c8651120f
commit 196d892090
20 changed files with 135 additions and 11 deletions

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_BinaryNot extends Node_Expr
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_Clone extends Node_Expr
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_ErrorSupress extends Node_Expr
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_Eval extends Node_Expr
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_Print extends Node_Expr
{
}

View File

@ -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_ShiftLeft extends Node_Expr
{
}

View File

@ -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_ShiftRight extends Node_Expr
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property Node_Expr $expr Expression
*/
class Node_Expr_UnaryPlus extends Node_Expr
{
}

View File

@ -1,7 +1,7 @@
<?php
/**
* @property null|int $num Number of loops to break
* @property null|Node_Expr $num Number of loops to break
*/
class Node_Stmt_Break extends Node_Stmt
{

View File

@ -1,5 +1,10 @@
<?php
/**
* @property Node_Name $type Class of exception
* @property string $var Variable for exception
* @property array $stmts Statements
*/
class Node_Stmt_Catch extends Node_Stmt
{
}

View File

@ -1,7 +1,7 @@
<?php
/**
* @property null|int $num Number of loops to break
* @property null|Node_Expr $num Number of loops to break
*/
class Node_Stmt_Continue extends Node_Stmt
{

View File

@ -1,5 +1,9 @@
<?php
/**
* @property array $stmts Statements
* @property Node_Expr $cond Condition
*/
class Node_Stmt_Do extends Node_Stmt
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property array $vars Variables
*/
class Node_Stmt_Global extends Node_Stmt
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property string $value String
*/
class Node_Stmt_InlineHTML extends Node_Stmt
{
}

View File

@ -1,5 +1,10 @@
<?php
/**
* @property string $name Name
* @property array $extends Extended interfaces
* @property array $stmts Statements
*/
class Node_Stmt_Interface extends Node_Stmt
{
}

View File

@ -1,5 +1,9 @@
<?php
/**
* @property array $stmts Statements
* @property array $catches Catches
*/
class Node_Stmt_TryCatch extends Node_Stmt
{
}

View File

@ -1,5 +1,8 @@
<?php
/**
* @property array $vars Variables to unset
*/
class Node_Stmt_Unset extends Node_Stmt
{
}

View File

@ -183,6 +183,14 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return $this->p($node->left) . ' ^ ' . $this->p($node->right);
}
public function pExpr_ShiftLeft(Node_Expr_ShiftLeft $node) {
return $this->p($node->left) . ' << ' . $this->p($node->right);
}
public function pExpr_ShiftRight(Node_Expr_ShiftRight $node) {
return $this->p($node->left) . ' >> ' . $this->p($node->right);
}
public function pExpr_LogicalAnd(Node_Expr_LogicalAnd $node) {
return $this->p($node->left) . ' and ' . $this->p($node->right);
}
@ -237,10 +245,18 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return '!' . $this->p($node->expr);
}
public function pExpr_BinaryNot(Node_Expr_BinaryNot $node) {
return '~' . $this->p($node->expr);
}
public function pExpr_UnaryMinus(Node_Expr_UnaryMinus $node) {
return '-' . $this->p($node->expr);
}
public function pExpr_UnaryPlus(Node_Expr_UnaryPlus $node) {
return '+' . $this->p($node->expr);
}
public function pExpr_PreInc(Node_Expr_PreInc $node) {
return '++' . $this->p($node->var);
}
@ -257,6 +273,10 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return $this->p($node->var) . '--';
}
public function pExpr_ErrorSupress(Node_Expr_ErrorSupress $node) {
return '@' . $this->p($node->expr);
}
// Casts
public function pExpr_IntCast(Node_Expr_IntCast $node) {
@ -304,7 +324,7 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
public function pExpr_StaticCall(Node_Expr_StaticCall $node) {
return $this->pClassName($node->class) . '::'
. ($node->func instanceof Node_Variable ? $this->p($node->func) : $node->func)
. ($node->func instanceof NodeAbstract ? $this->p($node->func) : $node->func)
. '(' . $this->pCommaSeparated($node->args) . ')';
}
@ -316,6 +336,14 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return 'isset(' . $this->pCommaSeparated($node->vars) . ')';
}
public function pExpr_Print(Node_Expr_Print $node) {
return 'print ' . $this->p($node->expr);
}
public function pExpr_Eval(Node_Expr_Eval $node) {
return 'eval(' . $this->p($node->expr) . ')';
}
public function pExpr_Include(Node_Expr_Include $node) {
static $map = array(
Node_Expr_Include::TYPE_INCLUDE => 'include',
@ -377,6 +405,10 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return 'new ' . $this->pClassName($node->class) . '(' . $this->pCommaSeparated($node->args) . ')';
}
public function pExpr_Clone(Node_Expr_Clone $node) {
return 'clone ' . $this->p($node->expr);
}
public function pExpr_Ternary(Node_Expr_Ternary $node) {
return $this->p($node->cond) . ' ?'
. (null !== $node->if ? ' ' . $this->p($node->if) . ' ' : '')
@ -401,6 +433,12 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return $this->p($node->ns) . (null !== $node->alias ? ' as ' . $node->alias : '');
}
public function pStmt_Interface(Node_Stmt_Interface $node) {
return 'interface ' . $node->name
. (!empty($node->extends) ? ' extends ' . $this->pCommaSeparated($node->extends) : '')
. "\n" . '{' . "\n" . $this->pIndent($this->pStmts($node->stmts)) . '}';
}
public function pStmt_Class(Node_Stmt_Class $node) {
return $this->pModifiers($node->type)
. 'class ' . $node->name
@ -495,22 +533,37 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
. "\n" . $this->pIndent($this->pStmts($node->stmts)) . '}';
}
public function pStmt_Do(Node_Stmt_Do $node) {
return 'do {' . "\n" . $this->pIndent($this->pStmts($node->stmts))
. '} while (' . $this->p($node->cond) . ')';
}
public function pStmt_Switch(Node_Stmt_Switch $node) {
return 'switch (' . $this->p($node->cond) . ') {'
. "\n" . $this->pIndent($this->pImplode($node->caseList)) . '}';
}
public function pStmt_TryCatch(Node_Stmt_TryCatch $node) {
return 'try {' . "\n" . $this->pIndent($this->pStmts($node->stmts)) . '}'
. $this->pImplode($node->catches);
}
public function pStmt_Catch(Node_Stmt_Catch $node) {
return ' catch (' . $this->p($node->type) . ' $' . $node->var . ') {'
. "\n" . $this->pIndent($this->pStmts($node->stmts)) . '}';
}
public function pStmt_Case(Node_Stmt_Case $node) {
return (null !== $node->cond ? 'case ' . $this->p($node->cond) : 'default') . ':'
. "\n" . $this->pIndent($this->pStmts($node->stmts));
}
public function pStmt_Break(Node_Stmt_Break $node) {
return 'break' . ($node->num !== null ? ' ' . $node->num : '');
return 'break' . ($node->num !== null ? ' ' . $this->p($node->num) : '');
}
public function pStmt_Continue(Node_Stmt_Continue $node) {
return 'continue' . ($node->num !== null ? ' ' . $node->num : '');
return 'continue' . ($node->num !== null ? ' ' . $this->p($node->num) : '');
}
public function pStmt_Return(Node_Stmt_Return $node) {
@ -535,18 +588,30 @@ class PrettyPrinter_Zend extends PrettyPrinterAbstract
return 'static ' . $this->pCommaSeparated($node->vars);
}
public function pStmt_Global(Node_Stmt_Global $node) {
return 'global ' . $this->pCommaSeparated($node->vars);
}
public function pStmt_StaticVar(Node_Stmt_StaticVar $node) {
return '$' . $node->name
. (null !== $node->default ? ' = ' . $this->p($node->default) : '');
}
public function pStmt_Unset(Node_Stmt_Unset $node) {
return 'unset(' . $this->pCommaSeparated($node->vars) . ')';
}
public function pStmt_InlineHTML(Node_Stmt_InlineHTML $node) {
return '?>' . $node->value . '<?php ';
}
// Helpers
public function pObjectProperty($node) {
if ($node instanceof Node_Expr) {
if ($node instanceof Node_Variable || $node instanceof Node_Expr_ArrayDimFetch) {
return $this->p($node);
} elseif ($node instanceof NodeAbstract) {
return '{' . $this->p($node) . '}';
} elseif ($node instanceof Node_Variable) {
return $this->pVariable($node);
} else {
return $node;
}

View File

@ -79,6 +79,7 @@ abstract class PrettyPrinterAbstract
$return .= $this->p($node);
if ( $node instanceof Node_Stmt_Func
|| $node instanceof Node_Stmt_Interface
|| $node instanceof Node_Stmt_Class
|| $node instanceof Node_Stmt_ClassMethod
|| $node instanceof Node_Stmt_For
@ -86,6 +87,7 @@ abstract class PrettyPrinterAbstract
|| $node instanceof Node_Stmt_If
|| $node instanceof Node_Stmt_Switch
|| $node instanceof Node_Stmt_While
|| $node instanceof Node_Stmt_TryCatch
) {
$return .= "\n";
} else {

View File

@ -32,11 +32,15 @@ if (false !== $stmts) {
echo "\n\n";
$prettyPrinter = new PrettyPrinter_Zend;
echo htmlspecialchars($prettyPrinter->pStmts(
$code = $prettyPrinter->pStmts(
$parser->yyparse(
new Lexer(file_get_contents('./grammar/rebuildParser.php')),
new Lexer(file_get_contents(
__FILE__
)),
function ($msg) {
echo $msg;
}
)
));
);
echo htmlspecialchars($code);