From 07b1f92ed662f6fa9309e679b83aff328362c98b Mon Sep 17 00:00:00 2001 From: Dmitry Stogov Date: Wed, 18 Feb 2015 23:06:32 +0300 Subject: [PATCH] fixed pretty-printer (support for "elseif") --- Zend/tests/assert/expect_015.phpt | 6 ++++ Zend/zend_ast.c | 57 +++++++++++++++++++++++++------ 2 files changed, 52 insertions(+), 11 deletions(-) diff --git a/Zend/tests/assert/expect_015.phpt b/Zend/tests/assert/expect_015.phpt index d4f3769de73..942c480a7c3 100644 --- a/Zend/tests/assert/expect_015.phpt +++ b/Zend/tests/assert/expect_015.phpt @@ -134,6 +134,9 @@ assert(0 && ($a = function () { $x = new foo(); $x = new \foo(); $x = new namespace\foo(); + if ($a) { + } elseif ($b) { + } })); ?> @@ -286,4 +289,7 @@ Warning: assert(): assert(0 && ($a = function () { $x = new foo(); $x = new \foo(); $x = new namespace\foo(); + if ($a) { + } elseif ($b) { + } })) failed in %sexpect_015.php on line %d diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c index becb00b2269..b0b97c17766 100644 --- a/Zend/zend_ast.c +++ b/Zend/zend_ast.c @@ -747,6 +747,43 @@ static void zend_ast_export_stmt(smart_str *str, zend_ast *ast, int indent) } } +static void zend_ast_export_if_stmt(smart_str *str, zend_ast_list *list, int indent) +{ + uint32_t i; + zend_ast *ast; + +tail_call: + i = 0; + while (i < list->children) { + ast = list->child[i]; + ZEND_ASSERT(ast->kind == ZEND_AST_IF_ELEM); + if (ast->child[0]) { + if (i == 0) { + smart_str_appends(str, "if ("); + } else { + zend_ast_export_indent(str, indent); + smart_str_appends(str, "} elseif ("); + } + zend_ast_export_ex(str, ast->child[0], 0, indent); + smart_str_appends(str, ") {\n"); + zend_ast_export_stmt(str, ast->child[1], indent + 1); + } else { + zend_ast_export_indent(str, indent); + smart_str_appends(str, "} else "); + if (ast->child[1]->kind == ZEND_AST_IF) { + list = (zend_ast_list*)ast->child[1]; + goto tail_call; + } else { + smart_str_appends(str, "{\n"); + zend_ast_export_stmt(str, ast->child[1], indent + 1); + } + } + i++; + } + zend_ast_export_indent(str, indent); + smart_str_appendc(str, '}'); +} + static void zend_ast_export_zval(smart_str *str, zval *zv, int priority, int indent) { zend_long idx; @@ -974,6 +1011,8 @@ simple_list: zend_ast_export_stmt(str, ast, indent); break; case ZEND_AST_IF: + zend_ast_export_if_stmt(str, (zend_ast_list*)ast, indent); + break; case ZEND_AST_SWITCH_LIST: case ZEND_AST_CATCH_LIST: zend_ast_export_list(str, (zend_ast_list*)ast, 0, 0, indent); @@ -1268,23 +1307,19 @@ simple_list: zend_ast_export_ex(str, ast->child[1], 0, indent); smart_str_appendc(str, ')'); break; + case ZEND_AST_IF_ELEM: if (ast->child[0]) { smart_str_appends(str, "if ("); zend_ast_export_ex(str, ast->child[0], 0, indent); - smart_str_appends(str, ") "); - } else { - smart_str_appends(str, " else "); - } - if (ast->child[1]->kind == ZEND_AST_IF) { - zend_ast_export_list(str, (zend_ast_list*)ast->child[1], 0, 0, indent); - } else { - smart_str_appends(str, "{\n"); + smart_str_appends(str, ") {\n"); + zend_ast_export_stmt(str, ast->child[1], indent + 1); + } else { + smart_str_appends(str, "else {\n"); zend_ast_export_stmt(str, ast->child[1], indent + 1); - zend_ast_export_indent(str, indent); - smart_str_appendc(str, '}'); - break; } + zend_ast_export_indent(str, indent); + smart_str_appendc(str, '}'); break; case ZEND_AST_SWITCH: smart_str_appends(str, "switch (");