From 196d892090618d4f249b3d224b0744fbe12306b6 Mon Sep 17 00:00:00 2001 From: nikic <+@ni-po.com> Date: Mon, 30 May 2011 17:29:10 +0200 Subject: [PATCH] Furthre work on PrettyPrinter --- lib/Node/Expr/BinaryNot.php | 3 ++ lib/Node/Expr/Clone.php | 3 ++ lib/Node/Expr/ErrorSupress.php | 3 ++ lib/Node/Expr/Eval.php | 3 ++ lib/Node/Expr/Print.php | 3 ++ lib/Node/Expr/ShiftLeft.php | 4 ++ lib/Node/Expr/ShiftRight.php | 4 ++ lib/Node/Expr/UnaryPlus.php | 3 ++ lib/Node/Stmt/Break.php | 2 +- lib/Node/Stmt/Catch.php | 5 +++ lib/Node/Stmt/Continue.php | 2 +- lib/Node/Stmt/Do.php | 4 ++ lib/Node/Stmt/Global.php | 3 ++ lib/Node/Stmt/InlineHTML.php | 3 ++ lib/Node/Stmt/Interface.php | 5 +++ lib/Node/Stmt/TryCatch.php | 4 ++ lib/Node/Stmt/Unset.php | 3 ++ lib/PrettyPrinter/Zend.php | 77 +++++++++++++++++++++++++++++++--- lib/PrettyPrinterAbstract.php | 2 + test.php | 10 +++-- 20 files changed, 135 insertions(+), 11 deletions(-) diff --git a/lib/Node/Expr/BinaryNot.php b/lib/Node/Expr/BinaryNot.php index 0a9e419..464466c 100644 --- a/lib/Node/Expr/BinaryNot.php +++ b/lib/Node/Expr/BinaryNot.php @@ -1,5 +1,8 @@ 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 . 'p($node); + } elseif ($node instanceof NodeAbstract) { return '{' . $this->p($node) . '}'; - } elseif ($node instanceof Node_Variable) { - return $this->pVariable($node); } else { return $node; } diff --git a/lib/PrettyPrinterAbstract.php b/lib/PrettyPrinterAbstract.php index a1cdf07..dfdb85e 100644 --- a/lib/PrettyPrinterAbstract.php +++ b/lib/PrettyPrinterAbstract.php @@ -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 { diff --git a/test.php b/test.php index 823e28c..ad980bd 100644 --- a/test.php +++ b/test.php @@ -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; } ) -)); \ No newline at end of file +); + +echo htmlspecialchars($code); \ No newline at end of file