nikic / PHP-Parser

A PHP parser written in PHP
BSD 3-Clause "New" or "Revised" License
16.98k stars 1.09k forks source link

parse `$result = &($context->getPageData());` not ok,have any idea ? #996

Closed dengyl2018 closed 4 months ago

dengyl2018 commented 4 months ago

parse $result = &($context->getPageData()); not ok,error:

Fatal error: Uncaught PhpParser\Error: Syntax error, unexpected ';', expecting '[' or T_OBJECT_OPERATOR or T_NULLSAFE_OBJECT_OPERATOR or '{' on line 36 in /PhpParser/ParserAbstract.php:318 Stack trace:

0 /PhpParser/ParserAbstract.php(159): PhpParser\ParserAbstract->doParse()

1 /PhpParser/Parser/Multiple.php(51): PhpParser\ParserAbstract->parse('<?php\n/****...', Object(PhpParser\ErrorHandler\Throwing))

2 PhpParser/Parser/Multiple.php(32): PhpParser\Parser\Multiple->tryParse(Object(PhpParser\Parser\Php7), Object(PhpParser\ErrorHandler\Throwing), '<?php\n/****...')

3 php_parser.php(23): PhpParser\Parser\Multiple->parse('< in /PhpParser/ParserAbstract.php on line 318

for compare, parse $a = $context->getPageData(); $result = &$a; is ok

dryabov commented 4 months ago

Simply because it is not valid PHP code. Try to run it in PHP and you get almost the same parsing error syntax error, unexpected token ";", expecting "->" or "?->" or "{" or "[": https://3v4l.org/vpiSi

nikic commented 4 months ago

That's right -- the right hand side of & needs to be a "variable" (in a very specific sense that cannot be briefly explained) and (...) is not a variable.