From 7d4ed8af0371c0bfae949431354b06119bb4e7fb Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Mon, 17 Mar 2025 23:01:22 +0100 Subject: [PATCH] Fix "expecting token" error for ampersand Currently, this is only handled for the "unexpected token" part of the message, but not the "expected" part. Fixes GH-18026 Closes GH-18101 --- NEWS | 1 + Zend/tests/gh18026.phpt | 12 ++++++++++++ Zend/zend_language_parser.y | 8 ++++++++ 3 files changed, 21 insertions(+) create mode 100644 Zend/tests/gh18026.phpt diff --git a/NEWS b/NEWS index e2c67a4a24d..fcb849ef98a 100644 --- a/NEWS +++ b/NEWS @@ -37,6 +37,7 @@ PHP NEWS (ilutov) . Fixed bug GH-18033 (NULL-ptr dereference when using register_tick_function in destructor). (nielsdos) + . Fixed bug GH-18026 (Improve "expecting token" error for ampersand). (ilutov) - Curl: . Added curl_multi_get_handles(). (timwolla) diff --git a/Zend/tests/gh18026.phpt b/Zend/tests/gh18026.phpt new file mode 100644 index 00000000000..0e33e349b12 --- /dev/null +++ b/Zend/tests/gh18026.phpt @@ -0,0 +1,12 @@ +--TEST-- +GH-18026: Confusing "amp" reference in parser error +--FILE-- + +--EXPECTF-- +Parse error: syntax error, unexpected token ")", expecting token "&" in %s on line %d diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y index c671b3a295e..1f117d142c1 100644 --- a/Zend/zend_language_parser.y +++ b/Zend/zend_language_parser.y @@ -1819,6 +1819,14 @@ static YYSIZE_T zend_yytnamerr(char *yyres, const char *yystr) return sizeof("\"\\\"")-1; } + /* We used "amp" as a dummy label to avoid a duplicate token literal warning. */ + if (strcmp(toktype, "\"amp\"") == 0) { + if (yyres) { + yystpcpy(yyres, "token \"&\""); + } + return sizeof("token \"&\"")-1; + } + /* Strip off the outer quote marks */ if (toktype_len >= 2 && *toktype == '"') { toktype++;