tolerant-php-parser icon indicating copy to clipboard operation
tolerant-php-parser copied to clipboard

Single word namespaces which are reserved words produce an error incorrectly

Open DeveloperRob opened this issue 3 years ago • 1 comments
trafficstars

PR 331 doesn't support PHP8 valid namespaces which are a single non-string token. For instance, while the first code block will work without issue on PHP8 (as T_NAME_QUALIFIED will be split to T_STRING T_NS_SEPARATOR T_STRING, resulting in Abstract being a T_STRING), the second will fail as token_get_all returns T_ABSTRACT and so the parser thinks that the namespace declaration is incomplete.

Works:

namespace Abstract\Objects;

class Foo{}

Valid PHP but doesn't work:

namespace Abstract;

class Foo{}

(DiagnosticsProvider::getDiagnostics($astNode); returns):

array(2) {
  [0] =>
  class Microsoft\PhpParser\Diagnostic#22 (4) {
    public $kind =>
    int(0)
    public $message =>
    string(13) "';' expected."
    public $start =>
    int(16)
    public $length =>
    int(0)
  }
  [1] =>
  class Microsoft\PhpParser\Diagnostic#23 (4) {
    public $kind =>
    int(0)
    public $message =>
    string(21) "Unexpected 'abstract'"
    public $start =>
    int(17)
    public $length =>
    int(8)
  }
}

DeveloperRob avatar Nov 24 '21 17:11 DeveloperRob

/* Name usable in a namespace declaration. */
namespace_declaration_name:
		identifier								{ $$ = $1; }
	|	T_NAME_QUALIFIED						{ $$ = $1; }
;

namespace declaration names in php 8.0+ seem distinct from any other token - they can be one or more of any reserved word, keyword, or regular string separated by \. Keywords were forbidden before that.

(T_NAME_QUALIFIED is the case of 2 or more without whitespace, identifier is the single case)

TysonAndre avatar Aug 26 '22 00:08 TysonAndre