From 2d1998938c0d99b3abe8794e767521e06c050769 Mon Sep 17 00:00:00 2001 From: Nikita Popov Date: Wed, 22 Jul 2020 18:43:55 +0200 Subject: [PATCH] [PHP 8.0] Support trailing comma in closure use list RFC: https://wiki.php.net/rfc/trailing_comma_in_closure_use_list --- CHANGELOG.md | 1 + grammar/php7.y | 2 +- lib/PhpParser/Parser/Php7.php | 168 +++++++++--------- test/code/parser/errorHandling/recovery.test | 68 +------ .../expr/closure_use_trailing_comma.test | 27 +++ 5 files changed, 122 insertions(+), 144 deletions(-) create mode 100644 test/code/parser/expr/closure_use_trailing_comma.test diff --git a/CHANGELOG.md b/CHANGELOG.md index 5d6fa37..0b92b34 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ Version 4.7.0-dev * Add `ParentConnectingVisitor` and `NodeConnectingVisitor` classes. * [PHP 8.0] Added support for match expressions. These are represented using a new `Expr\Match_` containing `MatchArm`s. +* [PHP 8.0] Added support for trailing comma in closure use lists. ### Fixed diff --git a/grammar/php7.y b/grammar/php7.y index adcbf55..0aad147 100644 --- a/grammar/php7.y +++ b/grammar/php7.y @@ -801,7 +801,7 @@ lexical_vars: ; lexical_var_list: - non_empty_lexical_var_list no_comma { $$ = $1; } + non_empty_lexical_var_list optional_comma { $$ = $1; } ; non_empty_lexical_var_list: diff --git a/lib/PhpParser/Parser/Php7.php b/lib/PhpParser/Parser/Php7.php index a841e63..816b389 100644 --- a/lib/PhpParser/Parser/Php7.php +++ b/lib/PhpParser/Parser/Php7.php @@ -330,10 +330,10 @@ class Php7 extends \PhpParser\ParserAbstract 1111, 278, 66, 279, 1080, 312, 298, 361, 786, 591, 363, -445, 1081, 442, -243, -242, 14, 15, 16, 17, 19, 336, 410, 412, 418, -475, 420, 421, 428, 582, - 1060, 1009, 1078, 888, 1040, 1048, -247, -95, -93, -475, - 13, 18, 786, 591, 22, 263, -475, 335, 509, 0, - 513, 534, 445, 446, 584, 1070, 1071, 1072, 1073, 1067, - 1068, 1013, 1055, 1010, 1130, 0, 1026, 1074, 1069, 1077, + 1060, 1009, 1078, 888, 1040, 1048, -247, -95, 13, -475, + 18, 22, 786, 591, 263, 335, -475, 509, 513, 0, + 534, 584, 445, 446, 1013, 1070, 1071, 1072, 1073, 1067, + 1068, 1055, 1010, 1130, 0, 1026, 0, 1074, 1069, 1077, 1102, 1118, 1151, 546, 69, 0, 0, 0, 273, 285 ); @@ -435,7 +435,7 @@ class Php7 extends \PhpParser\ParserAbstract 155, 155, 155, 155, 155, 115, 155, 155, 155, 145, 155, 155, 151, 152, 155, 155, 152, 155, 155, -1, 155, 155, 132, 133, 155, 135, 136, 137, 138, 139, - 140, 155, 155, 155, 155, -1, 156, 147, 148, 157, + 140, 155, 155, 155, -1, 156, -1, 147, 148, 157, 157, 157, 157, 157, 154, -1, -1, -1, 158, 159 ); @@ -455,9 +455,9 @@ class Php7 extends \PhpParser\ParserAbstract 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 384, 332, 332, 214, 167, - 519, 800, 816, 803, 817, 796, 795, 802, 805, 818, - 689, 690, 544, 694, 695, 697, 698, 806, 826, 799, - 809, 567, 567, 567, 567, 567, 567, 567, 567, 567, + 519, 799, 809, 802, 816, 795, 793, 800, 803, 817, + 689, 690, 544, 694, 695, 697, 698, 805, 826, 796, + 806, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 567, 150, 162, 19, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, 156, @@ -470,9 +470,9 @@ class Php7 extends \PhpParser\ParserAbstract 526, 526, 526, 292, 791, 791, 791, 791, 791, 791, 292, 526, 526, 430, 792, 128, -5, 88, 286, 286, 288, 286, 286, 286, 409, 409, 409, 414, 356, 315, - 552, 356, 612, 641, 819, 530, 687, 553, 688, 296, + 552, 356, 612, 641, 818, 530, 687, 553, 688, 296, 306, 571, 342, 573, 342, 342, 342, 416, 415, 420, - 548, 793, 332, 554, 332, 332, 332, 332, 747, 332, + 548, 792, 332, 554, 332, 332, 332, 332, 747, 332, 332, 332, 332, 332, 332, 229, 332, 396, 214, 217, 45, 217, 217, 414, 523, 308, 636, 243, 523, 523, 523, 638, 639, 26, -8, 640, 231, 406, 506, 506, @@ -488,7 +488,7 @@ class Php7 extends \PhpParser\ParserAbstract 536, 555, 531, 705, 568, 512, 512, 512, 701, 512, 512, 512, 512, 512, 512, 512, 512, 524, 822, 560, 501, 751, 539, 528, 550, 540, 619, 521, 570, 570, - 645, 765, 820, 509, 747, 785, 741, 498, 542, 775, + 645, 765, 819, 509, 747, 785, 741, 498, 542, 775, 752, 611, 557, 564, 753, 757, 776, 786, 620, 787, 646, 515, 650, 570, 652, 512, 692, 658, 823, 703, 524, 824, 771, 572, 777, 778, 566, 653, 516, 825, @@ -583,7 +583,7 @@ class Php7 extends \PhpParser\ParserAbstract 180, 92, 195,32767, 193, 193, 92,32767, 94, 94, 32767, 94, 197,32767, 493, 197, 94, 92, 217, 217, 422, 180, 267, 94, 94, 94, 267, 182, 267, 94, - 422, 267, 180, 267, 92, 92,32767, 92, 267,32767, + 422, 267, 180, 267, 92, 92,32767, 94, 267,32767, 32767,32767, 86,32767,32767,32767,32767,32767,32767,32767, 32767,32767,32767,32767,32767,32767,32767,32767, 433,32767, 462,32767, 479, 491,32767, 374, 375, 377, 477, 399, @@ -618,44 +618,44 @@ class Php7 extends \PhpParser\ParserAbstract 204, 205, 206, 207, 208, 209, 210, 211, 163, 212, 164, 180, 181, 182, 216, 183, 165, 166, 167, 184, 180, 168, 217, 148, 185, 213, 169, 186, 214, 187, - 188, 170, 189, 578, 473, 587, 781, 354, 554, 776, - 306, 777, 347, 288, 289, 291, 502, 292, 353, 293, - 356, 536, 507, 339, 1034, 341, 803, 1035, 559, 280, - 342, 478, 478, 478, 256, 302, 478, 524, 551, 467, - 467, 467, 467, 875, 880, 881, 526, 526, 493, 510, + 188, 170, 189, 578, 473, 587, 781, 354, 554, 524, + 551, 306, 347, 288, 289, 291, 502, 292, 353, 293, + 356, 536, 507, 714, 467, 467, 467, 467, 399, 399, + 280, 478, 478, 478, 510, 256, 478, 339, 399, 341, + 1145, 1145, 559, 776, 342, 777, 526, 526, 493, 302, 478, 1079, 1079, 1079, 1079, 1079, 1079, 1079, 1079, 1079, - 1079, 1097, 1097, 815, 337, 586, 1097, 1097, 1097, 1097, - 1097, 1097, 1097, 1097, 1097, 1097, 1095, 1095, 504, 714, - 520, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 1095, + 1079, 1097, 1097, 1121, 1122, 1145, 1097, 1097, 1097, 1097, + 1097, 1097, 1097, 1097, 1097, 1097, 1095, 1095, 875, 880, + 881, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 1095, 506, 922, 590, 715, 478, 478, 572, 920, 1045, 750, 492, 516, 478, 478, 478, 741, 326, 330, 485, 508, 511, 533, 702, 458, 458, 547, 548, 479, 458, 458, 458, 458, 458, 458, 458, 458, 458, 458, 491, - 503, 1121, 1122, 823, 611, 535, 537, 399, 399, 552, - 571, 574, 858, 581, 589, 854, 929, 399, 499, 930, - 741, 304, 741, 713, 515, 529, 531, 532, 583, 1046, - 3, 4, 717, 1134, 847, 821, 821, 819, 821, 610, - 890, 470, 856, 851, 844, 324, 469, 469, 497, 1047, - 1105, 722, 1004, 772, 767, 768, 782, 553, 723, 769, - 720, 770, 771, 721, 885, 775, 1120, 732, 732, 817, - 817, 817, 817, 742, 742, 742, 744, 731, 362, 311, - 866, 811, 818, 307, 308, 544, 1145, 1145, 737, 334, - 303, 745, 733, 894, 734, 895, 898, 328, 471, 615, - 558, 826, 1042, 1160, 484, 861, 861, 1037, 899, 494, - 495, 1145, 816, 1049, 1049, 746, 828, 475, 877, 877, - 877, 939, 1126, 1126, 523, 405, 877, 877, 877, 937, - 749, 417, 1041, 1117, 1117, 423, 1049, 1049, 1049, 1049, - 1107, 0, 11, 0, 1128, 1128, 0, 0, 1049, 1049, - 0, 1144, 1144, 0, 0, 944, 0, 521, 0, 0, - 570, 0, 863, 0, 0, 789, 0, 0, 790, 736, - 557, 842, 1113, 0, 0, 730, 1144, 0, 0, 0, + 503, 815, 337, 586, 611, 535, 537, 1160, 823, 552, + 571, 574, 858, 581, 589, 854, 1034, 499, 803, 1035, + 741, 475, 741, 713, 515, 529, 531, 532, 583, 1046, + 929, 304, 717, 930, 847, 821, 821, 819, 821, 610, + 1134, 470, 856, 851, 1107, 890, 469, 469, 844, 1047, + 1105, 722, 324, 772, 767, 768, 782, 553, 723, 769, + 720, 770, 771, 721, 570, 775, 497, 732, 732, 817, + 817, 817, 817, 742, 742, 742, 744, 731, 745, 733, + 894, 811, 818, 898, 307, 308, 1004, 944, 826, 521, + 861, 861, 885, 504, 863, 520, 475, 328, 471, 816, + 362, 736, 557, 842, 484, 3, 4, 730, 1120, 494, + 495, 311, 866, 1049, 1049, 1041, 937, 749, 877, 877, + 877, 544, 1126, 1126, 303, 737, 877, 877, 877, 1144, + 1144, 417, 334, 1117, 1117, 734, 1049, 1049, 1049, 1049, + 895, 615, 11, 558, 1128, 1128, 1042, 1037, 1049, 1049, + 1044, 746, 828, 899, 1144, 405, 939, 523, 423, 0, + 0, 0, 0, 0, 0, 789, 0, 0, 790, 0, + 0, 0, 1113, 0, 1147, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 475, 490, 0, 0, 1147, 0, 0, 897, + 0, 0, 0, 490, 0, 0, 0, 0, 0, 897, 0, 1115, 1115, 897, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 472, 488, 0, 0, 0, 0, 0, 0, 0, 0, 0, 472, 0, 488, 0, - 0, 327, 0, 0, 476, 392, 1044, 394, 0, 0, + 0, 327, 0, 0, 476, 392, 0, 394, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 740, 0, 0, 1152 ); @@ -673,65 +673,65 @@ class Php7 extends \PhpParser\ParserAbstract 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, 33, - 33, 33, 33, 63, 10, 8, 13, 56, 56, 55, - 81, 55, 56, 56, 56, 56, 56, 56, 56, 56, - 56, 56, 105, 70, 68, 70, 68, 68, 70, 142, - 70, 10, 10, 10, 142, 70, 10, 46, 46, 91, - 91, 91, 91, 98, 98, 98, 92, 92, 10, 91, + 33, 33, 33, 63, 10, 8, 13, 56, 56, 46, + 46, 81, 56, 56, 56, 56, 56, 56, 56, 56, + 56, 56, 105, 17, 91, 91, 91, 91, 124, 124, + 142, 10, 10, 10, 91, 142, 10, 70, 124, 70, + 156, 156, 70, 55, 70, 55, 92, 92, 10, 70, 10, 92, 92, 92, 92, 92, 92, 92, 92, 92, - 92, 143, 143, 77, 77, 77, 143, 143, 143, 143, - 143, 143, 143, 143, 143, 143, 144, 144, 2, 17, - 2, 144, 144, 144, 144, 144, 144, 144, 144, 144, + 92, 143, 143, 151, 151, 156, 143, 143, 143, 143, + 143, 143, 143, 143, 143, 143, 144, 144, 98, 98, + 98, 144, 144, 144, 144, 144, 144, 144, 144, 144, 144, 7, 7, 7, 18, 10, 10, 7, 7, 100, 36, 10, 10, 10, 10, 10, 26, 49, 49, 49, 49, 49, 74, 5, 146, 146, 74, 74, 10, 146, 146, 146, 146, 146, 146, 146, 146, 146, 146, 39, - 10, 151, 151, 40, 39, 39, 39, 124, 124, 39, - 39, 39, 39, 39, 39, 39, 119, 124, 145, 119, - 26, 152, 26, 16, 69, 69, 69, 69, 69, 100, - 37, 37, 13, 154, 16, 16, 16, 16, 16, 16, - 102, 16, 16, 16, 87, 52, 76, 76, 88, 100, - 100, 13, 126, 13, 13, 13, 13, 54, 13, 13, - 13, 13, 13, 13, 97, 13, 149, 26, 26, 76, - 76, 76, 76, 26, 26, 26, 26, 26, 96, 20, - 94, 76, 76, 81, 81, 24, 156, 156, 30, 19, - 11, 14, 14, 14, 28, 104, 14, 66, 66, 83, - 23, 14, 135, 12, 66, 91, 91, 132, 107, 66, - 66, 156, 14, 63, 63, 32, 80, 12, 63, 63, - 63, 122, 8, 8, 73, 72, 63, 63, 63, 14, - 14, 66, 14, 105, 105, 130, 63, 63, 63, 63, - 12, -1, 66, -1, 105, 105, -1, -1, 63, 63, - -1, 155, 155, -1, -1, 15, -1, 15, -1, -1, - 12, -1, 15, -1, -1, 63, -1, -1, 63, 15, - 15, 15, 105, -1, -1, 15, 155, -1, -1, -1, + 10, 77, 77, 77, 39, 39, 39, 12, 40, 39, + 39, 39, 39, 39, 39, 39, 68, 145, 68, 68, + 26, 12, 26, 16, 69, 69, 69, 69, 69, 100, + 119, 152, 13, 119, 16, 16, 16, 16, 16, 16, + 154, 16, 16, 16, 12, 102, 76, 76, 87, 100, + 100, 13, 52, 13, 13, 13, 13, 54, 13, 13, + 13, 13, 13, 13, 12, 13, 88, 26, 26, 76, + 76, 76, 76, 26, 26, 26, 26, 26, 14, 14, + 14, 76, 76, 14, 81, 81, 126, 15, 14, 15, + 91, 91, 97, 2, 15, 2, 12, 66, 66, 14, + 96, 15, 15, 15, 66, 37, 37, 15, 149, 66, + 66, 20, 94, 63, 63, 15, 14, 14, 63, 63, + 63, 24, 8, 8, 11, 30, 63, 63, 63, 155, + 155, 66, 19, 105, 105, 28, 63, 63, 63, 63, + 104, 83, 66, 23, 105, 105, 135, 132, 63, 63, + 12, 32, 80, 107, 155, 72, 122, 73, 130, -1, + -1, -1, -1, -1, -1, 63, -1, -1, 63, -1, + -1, -1, 105, -1, 155, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, 12, 8, -1, -1, 155, -1, -1, 105, + -1, -1, -1, 8, -1, -1, -1, -1, -1, 105, -1, 105, 105, 105, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, -1, 8, -1, - -1, 8, -1, -1, 8, 8, 12, 8, -1, -1, + -1, 8, -1, -1, 8, 8, -1, 8, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 8, -1, -1, 8 ); protected $gotoBase = array( - 0, 0, -395, 0, 0, 223, 0, 206, 115, 0, - -143, 41, 70, -21, -45, 6, 271, 197, 212, 28, - 62, 0, 0, -9, 40, 0, -41, 0, 31, 0, - 42, 0, -6, -23, 0, 0, 211, -335, 0, -343, - 234, 0, 0, 0, 0, 0, 121, 0, 0, 186, - 0, 0, 257, 0, 86, 114, -93, 0, 0, 0, - 0, 0, 0, 117, 0, 0, -30, 0, -195, -1, - -273, 0, -20, -31, -364, 0, 50, -87, 0, 0, - -7, -279, 0, 11, 0, 0, 0, 264, 259, 0, - 0, 135, -54, 0, 46, 0, 75, 63, -96, 0, - -104, 0, 262, 0, 29, 137, 0, -14, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 21, - 0, 0, -3, 0, 228, 0, 55, 0, 0, 0, - -35, 0, -13, 0, 0, -11, 0, 0, 0, 0, - 0, 0, -119, -39, -24, 243, 14, 0, 0, 58, - 0, -63, 245, 0, 256, 146, 81, 0, 0 + 0, 0, -250, 0, 0, 223, 0, 206, 115, 0, + -143, 75, -26, -21, -58, -62, 271, 141, 212, 71, + 94, 0, 0, 34, 76, 0, -41, 0, 72, 0, + 79, 0, 30, -23, 0, 0, 211, -260, 0, -343, + 239, 0, 0, 0, 0, 0, 93, 0, 0, 186, + 0, 0, 264, 0, 86, 148, -93, 0, 0, 0, + 0, 0, 0, 117, 0, 0, -30, 0, -73, -1, + -259, 0, 10, 2, -364, 0, 50, -19, 0, 0, + 29, -278, 0, 53, 0, 0, 0, 268, 277, 0, + 0, 120, -54, 0, 78, 0, 97, 91, -61, 0, + -104, 0, 267, 0, 74, 137, 0, 31, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 35, + 0, 0, 32, 0, 119, 0, 89, 0, 0, 0, + -12, 0, 27, 0, 0, 33, 0, 0, 0, 0, + 0, 0, -118, -39, -24, 242, 14, 0, 0, 100, + 0, -131, 255, 0, 263, 124, -95, 0, 0 ); protected $gotoDefault = array( diff --git a/test/code/parser/errorHandling/recovery.test b/test/code/parser/errorHandling/recovery.test index 9820d55..0042e6f 100644 --- a/test/code/parser/errorHandling/recovery.test +++ b/test/code/parser/errorHandling/recovery.test @@ -594,13 +594,11 @@ isset($x, ); declare(a=42, ); -foo($a, ); global $a, ; static $a, ; echo $a, ; for ($a, ; $b, ; $c, ); -function ($a) use ($b, ) {}; ----- !!php7 A trailing comma is not allowed here from 5:6 to 5:6 @@ -613,13 +611,12 @@ A trailing comma is not allowed here from 13:17 to 13:17 A trailing comma is not allowed here from 14:14 to 14:14 A trailing comma is not allowed here from 16:22 to 16:22 A trailing comma is not allowed here from 21:13 to 21:13 +A trailing comma is not allowed here from 23:10 to 23:10 A trailing comma is not allowed here from 24:10 to 24:10 -A trailing comma is not allowed here from 25:10 to 25:10 -A trailing comma is not allowed here from 26:8 to 26:8 -A trailing comma is not allowed here from 28:8 to 28:8 -A trailing comma is not allowed here from 28:14 to 28:14 -A trailing comma is not allowed here from 28:20 to 28:20 -A trailing comma is not allowed here from 29:22 to 29:22 +A trailing comma is not allowed here from 25:8 to 25:8 +A trailing comma is not allowed here from 27:8 to 27:8 +A trailing comma is not allowed here from 27:14 to 27:14 +A trailing comma is not allowed here from 27:20 to 27:20 array( 0: Stmt_GroupUse( type: TYPE_UNKNOWN (0) @@ -808,32 +805,14 @@ array( ) stmts: null ) - 9: Stmt_Expression( - expr: Expr_FuncCall( - name: Name( - parts: array( - 0: foo - ) - ) - args: array( - 0: Arg( - value: Expr_Variable( - name: a - ) - byRef: false - unpack: false - ) - ) - ) - ) - 10: Stmt_Global( + 9: Stmt_Global( vars: array( 0: Expr_Variable( name: a ) ) ) - 11: Stmt_Static( + 10: Stmt_Static( vars: array( 0: Stmt_StaticVar( var: Expr_Variable( @@ -843,14 +822,14 @@ array( ) ) ) - 12: Stmt_Echo( + 11: Stmt_Echo( exprs: array( 0: Expr_Variable( name: a ) ) ) - 13: Stmt_For( + 12: Stmt_For( init: array( 0: Expr_Variable( name: a @@ -869,35 +848,6 @@ array( stmts: array( ) ) - 14: Stmt_Expression( - expr: Expr_Closure( - static: false - byRef: false - params: array( - 0: Param( - flags: 0 - type: null - byRef: false - variadic: false - var: Expr_Variable( - name: a - ) - default: null - ) - ) - uses: array( - 0: Expr_ClosureUse( - var: Expr_Variable( - name: b - ) - byRef: false - ) - ) - returnType: null - stmts: array( - ) - ) - ) ) -----