]> granicus.if.org Git - php/commitdiff
Add trailing comma syntax support for mixed and unmixed group use lists
authorSammy Kaye Powers <sammyk@sammykmedia.com>
Thu, 20 Apr 2017 18:08:11 +0000 (13:08 -0500)
committerNikita Popov <nikita.ppv@gmail.com>
Mon, 1 May 2017 10:19:47 +0000 (12:19 +0200)
RFC: https://wiki.php.net/rfc/list-syntax-trailing-commas

15 files changed:
NEWS
UPGRADING
Zend/tests/ns_088.phpt
Zend/tests/ns_094.phpt
Zend/tests/ns_trailing_comma_01.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_02.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_01.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_02.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_03.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_04.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_05.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_06.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_07.phpt [new file with mode: 0644]
Zend/tests/ns_trailing_comma_error_08.phpt [new file with mode: 0644]
Zend/zend_language_parser.y

diff --git a/NEWS b/NEWS
index 963fff27032783b2255e2a2f34b2c17cc99ca414..a69981536aaf27311472e72238d02449245a2729 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -39,6 +39,8 @@ PHP                                                                        NEWS
   . Fixed bug #74149 (static embed SAPI linkage error). (krakjoe)
   . Fixed bug #72359, bug #72451, bug #73706, bug #71115 and others related
     to interned strings handling in TS builds. (Anatol, Dmitry)
+  . Implemented "Trailing Commas In List Syntax" RFC for group use lists only.
+    (Sammy Kaye Powers)
 
 - BCMath:
   . Fixed bug #46564 (bcmod truncates fractionals). (liborm85)
index baead9de0c95dbb25f9b2047d49b5f5cb8afc281..e1877667bad544c3d05bcbf65bfe68e8940e6728 100644 (file)
--- a/UPGRADING
+++ b/UPGRADING
@@ -99,6 +99,8 @@ PHP 7.2 UPGRADE NOTES
     inherited method. This complies with contravariance of method argument types
     under the Liskov Substitution Principle.
     (https://wiki.php.net/rfc/parameter-no-type-variance)
+  . A trailing comma in group use statements is now allowed.
+    (https://wiki.php.net/rfc/list-syntax-trailing-commas)
 
 - PCRE:
   . Added `J` modifier for setting PCRE_DUPNAMES.
index 7af8dcc8d3d252460721cefbe7740d06be521b78..18210da6df817375cca58f054872733b7f8dbce4 100644 (file)
@@ -14,4 +14,4 @@ namespace Fiz\Biz\Buz {
 }
 ?>
 --EXPECTF--
-Parse error: syntax error, unexpected '{', expecting ',' or '}' in %sns_088.php on line 5
+Parse error: syntax error, unexpected '{', expecting '}' in %sns_088.php on line 5
index 16001da67e30ebd9a2aac8fac91a91af44c02995..f33bf560a91b015f7215d834079babcca8fbac39 100644 (file)
@@ -12,4 +12,4 @@ use const Foo\Bar\{
 };
 
 --EXPECTF--
-Parse error: syntax error, unexpected 'const' (T_CONST), expecting identifier (T_STRING) in %s on line 7
+Parse error: syntax error, unexpected 'const' (T_CONST), expecting '}' in %s on line 7
diff --git a/Zend/tests/ns_trailing_comma_01.phpt b/Zend/tests/ns_trailing_comma_01.phpt
new file mode 100644 (file)
index 0000000..cbd443f
--- /dev/null
@@ -0,0 +1,30 @@
+--TEST--
+Mixed group use declaration can contain trailing comma
+--FILE--
+<?php
+namespace Foo {
+  const FOO_CONST = "Foo const\n";
+  function foo_func() {
+    echo "Foo func\n";
+  }
+  class FooClass {
+    function __construct() {
+      echo "Foo class\n";
+    }
+  }
+}
+namespace {
+  use Foo\{
+    const FOO_CONST,
+    function foo_func,
+    FooClass as BarClass,
+  };
+  echo FOO_CONST;
+  foo_func();
+  new BarClass;
+}
+?>
+--EXPECT--
+Foo const
+Foo func
+Foo class
diff --git a/Zend/tests/ns_trailing_comma_02.phpt b/Zend/tests/ns_trailing_comma_02.phpt
new file mode 100644 (file)
index 0000000..658a173
--- /dev/null
@@ -0,0 +1,52 @@
+--TEST--
+Unmixed group use declaration can contain trailing comma
+--FILE--
+<?php
+namespace Foo {
+  const FOO_CONST_1 = "Foo const 1\n";
+  const FOO_CONST_2 = "Foo const 2\n";
+}
+namespace Bar {
+  function foo_func_1() {
+    echo "Bar func 1\n";
+  }
+  function foo_func_2() {
+    echo "Bar func 2\n";
+  }
+}
+namespace Baz {
+  class BazFooClass {
+    function __construct() { echo "BazFoo class\n"; }
+  }
+  class BazBarClass {
+    function __construct() { echo "BazBar class\n"; }
+  }
+}
+namespace {
+  use const Foo\{
+    FOO_CONST_1,
+    FOO_CONST_2,
+  };
+  use function Bar\{
+    foo_func_1,
+    foo_func_2,
+  };
+  use Baz\{
+    BazFooClass,
+    BazBarClass,
+  };
+  echo FOO_CONST_1;
+  echo FOO_CONST_2;
+  foo_func_1();
+  foo_func_2();
+  new BazFooClass;
+  new BazBarClass;
+}
+?>
+--EXPECT--
+Foo const 1
+Foo const 2
+Bar func 1
+Bar func 2
+BazFoo class
+BazBar class
diff --git a/Zend/tests/ns_trailing_comma_error_01.phpt b/Zend/tests/ns_trailing_comma_error_01.phpt
new file mode 100644 (file)
index 0000000..6ada1f7
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Group use declarations mustn't be empty
+--FILE--
+<?php
+use Baz\{};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected '}', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_02.phpt b/Zend/tests/ns_trailing_comma_error_02.phpt
new file mode 100644 (file)
index 0000000..c9d0d92
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Group use declarations mustn't contain just a comma
+--FILE--
+<?php
+use Baz\{,};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_03.phpt b/Zend/tests/ns_trailing_comma_error_03.phpt
new file mode 100644 (file)
index 0000000..9d10c47
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Group use declarations mustn't allow more than one comma
+--FILE--
+<?php
+use Baz\{Foo,,};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting '}' in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_04.phpt b/Zend/tests/ns_trailing_comma_error_04.phpt
new file mode 100644 (file)
index 0000000..d04b1af
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Group use declarations mustn't begin with a comma
+--FILE--
+<?php
+use Baz\{,Foo};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting identifier (T_STRING) or function (T_FUNCTION) or const (T_CONST) in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_05.phpt b/Zend/tests/ns_trailing_comma_error_05.phpt
new file mode 100644 (file)
index 0000000..1d43e8f
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Group use declarations mustn't contain two commas mid-list
+--FILE--
+<?php
+use Baz\{Foo,,Bar};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting '}' in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_06.phpt b/Zend/tests/ns_trailing_comma_error_06.phpt
new file mode 100644 (file)
index 0000000..2f2738e
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Unmixed group use declarations mustn't allow more than one comma
+--FILE--
+<?php
+use const Baz\{Foo,,};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting '}' in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_07.phpt b/Zend/tests/ns_trailing_comma_error_07.phpt
new file mode 100644 (file)
index 0000000..c60dd6a
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Unmixed group use declarations mustn't begin with a comma
+--FILE--
+<?php
+use function Baz\{,Foo};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting identifier (T_STRING) in %s on line %d
\ No newline at end of file
diff --git a/Zend/tests/ns_trailing_comma_error_08.phpt b/Zend/tests/ns_trailing_comma_error_08.phpt
new file mode 100644 (file)
index 0000000..b234463
--- /dev/null
@@ -0,0 +1,8 @@
+--TEST--
+Unmixed group use declarations mustn't contain two commas mid-list
+--FILE--
+<?php
+use const Baz\{Foo,,Bar};
+?>
+--EXPECTF--
+Parse error: syntax error, unexpected ',', expecting '}' in %s on line %d
\ No newline at end of file
index 9970ce3a0724f9e938ec903150aca429efac747b..f1a7e3889360234a162da0735fed6e7527627cf7 100644 (file)
@@ -338,19 +338,24 @@ use_type:
 ;
 
 group_use_declaration:
-               namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
+               namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
                        { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4); }
-       |       T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations '}'
+       |       T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' unprefixed_use_declarations possible_comma '}'
                        { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $2, $5); }
 ;
 
 mixed_group_use_declaration:
-               namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
+               namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
                        { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $1, $4);}
-       |       T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' inline_use_declarations '}'
+       |       T_NS_SEPARATOR namespace_name T_NS_SEPARATOR '{' inline_use_declarations possible_comma '}'
                        { $$ = zend_ast_create(ZEND_AST_GROUP_USE, $2, $5); }
 ;
 
+possible_comma:
+               /* empty */
+       |       ','
+;
+
 inline_use_declarations:
                inline_use_declarations ',' inline_use_declaration
                        { $$ = zend_ast_list_add($1, $3); }