PHP-Parser
PHP-Parser copied to clipboard
Inline comment before semicolon not in AST
Steps to reproduce:
Create a simple PHP file like this:
<?php
echo 'foo' /* bar */ ;
The comment between the end of the function and the ; is crucial. It's also reproducible with code like $x = str_contains('abcde', 'a') /* test */; or $x = 7 /* seven */;.
Parse it:
$parser = (new ParserFactory())->create(ParserFactory::PREFER_PHP7);
$ast = $parser->parse(file_get_contents('sample.php');
print_r($ast);
Actual Result
array(1) {
[0]=>
object(PhpParser\Node\Stmt\Echo_)#9202 (2) {
["exprs"]=>
array(1) {
[0]=>
object(PhpParser\Node\Scalar\String_)#9206 (2) {
["value"]=>
string(3) "foo"
["attributes":protected]=>
array(3) {
["startLine"]=>
int(2)
["endLine"]=>
int(2)
["kind"]=>
int(1)
}
}
}
["attributes":protected]=>
array(2) {
["startLine"]=>
int(2)
["endLine"]=>
int(2)
}
}
}
Expected Result
I'd like to see to "bar" comment to show up somewhere in the AST. As an attribute of the Stmt\Echo_ most likely.
Use Case
We use this in our internal translation system to annotate calls to our translation engine.