php-src icon indicating copy to clipboard operation
php-src copied to clipboard

token_get_all incorrectly identifies matching named parameters as PHP keywords

Open Naktibalda opened this issue 4 years ago • 3 comments

Description

The following code:

$code ='<?php foo(echo: 4);';
$tokens = token_get_all($code);
var_dump(token_name($tokens[3][0]), $tokens[3][1]);

Resulted in this output:

string(6) "T_ECHO"
string(4) "echo"

But I expected this output instead:

string(8) "T_STRING"
string(4) "echo"

https://3v4l.org/Kr7R5#v8.1.2

PHP Version

PHP 8.1.2

Operating System

No response

Naktibalda avatar Feb 05 '22 17:02 Naktibalda

This is correct. Tokenization (mostly) is context independent. The parser accepts identifiers as named params.

https://github.com/php/php-src/blob/706398fa9fb081c6385c66ac8af1fe952c80f17d/Zend/zend_language_parser.y#L875-L876

Which allow both T_STRINGs and semi_reserved.

https://github.com/php/php-src/blob/706398fa9fb081c6385c66ac8af1fe952c80f17d/Zend/zend_language_parser.y#L318-L325

iluuu1994 avatar Feb 05 '22 17:02 iluuu1994

By passing TOKEN_PARSE to the flags parameter, the parser "Recognizes the ability to use reserved words in specific contexts" https://3v4l.org/DPalK#v8.1.2

iggyvolz avatar Feb 05 '22 18:02 iggyvolz

Thanks, I totally missed that parameter.

Naktibalda avatar Feb 06 '22 10:02 Naktibalda