Parse error on namespace using keywords

Description

Since PHP 8.1 reserved words aka keywords are allowed as namespaces. Our parser currently does not allow this syntax and raises parsing errors. We should allow these reserved words in specific situations like namespace, use, and call expressions.

The following code should not trigger a parse error:

Exception single keywords are not allowed as expressions and types:

Activity

Karim El OuerghemmiJanuary 11, 2021 at 11:47 AM
Edited

The possibility of using keywords in namespaces was introduced in PHP 8: See RFC.

The ticket was initially part of the 3.14 hardening sprint, but was removed from it as it might require more dedicated work and a possible API change of NamespaceNameTree. Reasons:

  • Currently, a namespace such as `Foo\Bar` is lexed as three separate tokens: {IDENTIFIER} {NS_SEPARATOR} {IDENTIFIER}. Our API for NamespaceNameTree offers access to the separator and identifier tokens separately.

  • Simply changing the grammar so that namespaces can be a series of {NAME_IDENTIFIER_OR_KEYWORD} separated by {NS_SEPARATOR}does not work because tokens can be separated by whitespaces which might lead to ambiguities such as `New \Bar()` (is "New" part of the namespace or not?).

  • The PHP developers resolved this by lexing namespaced names as single tokens https://wiki.php.net/rfc/namespaced_names_as_token#proposal (i.e., `Foo\Bar` is no longer three, but one token). Thus, in our API for NamespaceNameTree we would no longer be able to expose the tokens for the separators and and "identifiers" in the same manner.

Done
Pinned fields
Click on the next to a field label to start pinning.

Details

Assignee

Reporter

Components

Fix versions

Priority

Sentry

Created October 27, 2020 at 5:15 PM
Updated October 16, 2024 at 2:37 PM
Resolved January 3, 2023 at 3:26 PM