From b591c329ee3129adbdc35141bb1542d119f7a2a1 Mon Sep 17 00:00:00 2001 From: Sammy Kaye Powers Date: Sat, 7 Oct 2017 12:57:07 -0500 Subject: [PATCH] Allow trailing commas in function and method calls --- .../call_with_leading_comma_error.phpt | 8 ++ .../call_with_multi_inner_comma_error.phpt | 8 ++ .../call_with_multi_trailing_comma_error.phpt | 8 ++ .../call_with_only_comma_error.phpt | 8 ++ .../call_with_trailing_comma_basic.phpt | 97 +++++++++++++++++++ Zend/zend_language_parser.y | 6 +- 6 files changed, 132 insertions(+), 3 deletions(-) create mode 100644 Zend/tests/function_arguments/call_with_leading_comma_error.phpt create mode 100644 Zend/tests/function_arguments/call_with_multi_inner_comma_error.phpt create mode 100644 Zend/tests/function_arguments/call_with_multi_trailing_comma_error.phpt create mode 100644 Zend/tests/function_arguments/call_with_only_comma_error.phpt create mode 100644 Zend/tests/function_arguments/call_with_trailing_comma_basic.phpt diff --git a/Zend/tests/function_arguments/call_with_leading_comma_error.phpt b/Zend/tests/function_arguments/call_with_leading_comma_error.phpt new file mode 100644 index 0000000000..1f587dd8f5 --- /dev/null +++ b/Zend/tests/function_arguments/call_with_leading_comma_error.phpt @@ -0,0 +1,8 @@ +--TEST-- +Leading commas in function calls is not allowed +--FILE-- + +--EXPECTF-- +Parse error: syntax error, unexpected ',' in %s on line %d diff --git a/Zend/tests/function_arguments/call_with_multi_inner_comma_error.phpt b/Zend/tests/function_arguments/call_with_multi_inner_comma_error.phpt new file mode 100644 index 0000000000..d8250536da --- /dev/null +++ b/Zend/tests/function_arguments/call_with_multi_inner_comma_error.phpt @@ -0,0 +1,8 @@ +--TEST-- +Multiple inner commas in function calls is not allowed +--FILE-- + +--EXPECTF-- +Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d diff --git a/Zend/tests/function_arguments/call_with_multi_trailing_comma_error.phpt b/Zend/tests/function_arguments/call_with_multi_trailing_comma_error.phpt new file mode 100644 index 0000000000..a38a01644b --- /dev/null +++ b/Zend/tests/function_arguments/call_with_multi_trailing_comma_error.phpt @@ -0,0 +1,8 @@ +--TEST-- +Multiple trailing commas in function calls is not allowed +--FILE-- + +--EXPECTF-- +Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d diff --git a/Zend/tests/function_arguments/call_with_only_comma_error.phpt b/Zend/tests/function_arguments/call_with_only_comma_error.phpt new file mode 100644 index 0000000000..8a0ce6810d --- /dev/null +++ b/Zend/tests/function_arguments/call_with_only_comma_error.phpt @@ -0,0 +1,8 @@ +--TEST-- +Single comma in function calls is not allowed +--FILE-- + +--EXPECTF-- +Parse error: syntax error, unexpected ',' in %s on line %d diff --git a/Zend/tests/function_arguments/call_with_trailing_comma_basic.phpt b/Zend/tests/function_arguments/call_with_trailing_comma_basic.phpt new file mode 100644 index 0000000000..f6a7603790 --- /dev/null +++ b/Zend/tests/function_arguments/call_with_trailing_comma_basic.phpt @@ -0,0 +1,97 @@ +--TEST-- +Allow trailing commas in function and method calls +--FILE-- +bar( + 'method', + 'bar', +); + +$foo( + 'invoke', + 'bar', +); + +$bar = function(...$args) { + echo __FUNCTION__ . "\n"; + var_dump($args); +}; + +$bar( + 'closure', + 'bar', +); + +# Make sure to hit the "not really a function" language constructs +unset($foo, $bar,); +var_dump(isset($foo, $bar,)); +?> +--EXPECT-- +foo +array(2) { + [0]=> + string(8) "function" + [1]=> + string(3) "bar" +} +__construct +array(2) { + [0]=> + string(11) "constructor" + [1]=> + string(3) "bar" +} +bar +array(2) { + [0]=> + string(6) "method" + [1]=> + string(3) "bar" +} +__invoke +array(2) { + [0]=> + string(6) "invoke" + [1]=> + string(3) "bar" +} +{closure} +array(2) { + [0]=> + string(7) "closure" + [1]=> + string(3) "bar" +} +bool(false) diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index 2c508a59fe..6d11afc8ba 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -439,7 +439,7 @@ statement: | T_ECHO echo_expr_list ';' { $$ = $2; } | T_INLINE_HTML { $$ = zend_ast_create(ZEND_AST_ECHO, $1); } | expr ';' { $$ = $1; } - | T_UNSET '(' unset_variables ')' ';' { $$ = $3; } + | T_UNSET '(' unset_variables possible_comma ')' ';' { $$ = $3; } | T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement { $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); } | T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')' @@ -670,7 +670,7 @@ return_type: argument_list: '(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); } - | '(' non_empty_argument_list ')' { $$ = $2; } + | '(' non_empty_argument_list possible_comma ')' { $$ = $2; } ; non_empty_argument_list: @@ -1260,7 +1260,7 @@ encaps_var_offset: internal_functions_in_yacc: - T_ISSET '(' isset_variables ')' { $$ = $3; } + T_ISSET '(' isset_variables possible_comma ')' { $$ = $3; } | T_EMPTY '(' expr ')' { $$ = zend_ast_create(ZEND_AST_EMPTY, $3); } | T_INCLUDE expr { $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE, $2); } -- 2.40.0