-
Notifications
You must be signed in to change notification settings - Fork 79
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fixes #220 : Properly parse expression lists passed to <?=
#245
Conversation
a156e91
to
9e356eb
Compare
This looks good at first look, but I will take a closer look soon, merge it, and cut a new release. |
I think this can only be a top-level statement or placed within E.g. Are you fine with taking it back out of CHILD_NODES? |
I think you're right about the places where this can occur. I'm ok with removing the new property from the AST. But I'm not clear what this will look like exactly, will you just have |
It will only if |
9e356eb
to
77a21aa
Compare
I was planning to continue creating that property, but just to exclude it from CHILD_NODES. Other approaches that could also be used:
See https://3v4l.org/ELbqZ for an example |
77a21aa
to
e0e22e5
Compare
// FIXME the tolerant-php-parser has a bug, this code always echoes if executed. | ||
// (i.e. same as `if (false); echo "hello world"` with an implicit semicolon) | ||
// NOTE: If the inline HTML is surrounded by brackets, then this would never echo. | ||
if (false)?>hello world<?php |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This bug wasn't introduced by my PR, so I consider it out of scope. I filed #246 for followup work.
- The tree should change once the bug is fixed
@TysonAndre I can confirm that I also wanted to note that internally PHP treats |
That isn't what this change does. It's also completely normal for me to see InlineHtml (tolerant-php-parser is an accurate representation of the tokens in the PHP code) What this change does: Instead of representing the fragment |
I am aware of what this change does, and yes the parser can do whatever it wants with regard to the nodes it creates. I was simply noting that reusing an However, my original point was that Just glancing at the code, I believe that you may have also introduced a new bug: <?php echo $a // ERROR: ';' expected <?= $a // NO ERROR Edit: Formatting. Bug confirmed. |
It definitely introduces a new bug. To fix this, I'll need to do the same thing as the parser would when parsing the rest of an ExpressionStatement (check for |
731863e
to
73606f5
Compare
Before, tolerant-php-parser parsed the tokens after `<?=` and `<?php` the same way. Fixes microsoft#220 (This commit and subsequent commits in this PR) This adds `TokenKind::ScriptSectionStartWithEchoTag` and handles the subsequent tokens differently if that token kind is seen instead of `ScriptSectionStartTag`. This reuses `ExpressionStatement` with an `EchoExpression` for simplicity. The resulting expression will have no `echoKeyword` because `<?=` was part of the preceding `InlineHTML` Node. - In all cases that I'm aware of, the ExpressionStatement with the EchoExpression will be moved into the outer statement list. NOTE: echo is a statement, not an expression. It must be followed by `?>` or `;` - `echo exprList` can't be used as an expression. - Contrast with `print expr`, (`PrintIntrinsicExpression`), which is correctly treated as an expression. Update documentation Add a test that the Parser will warn about `<?= 'expr'<EOF>`. - Fixing that bug got rid of an incorrect EmptyExpression in another test (programStructure5.php.tree)
73606f5
to
80071b4
Compare
@@ -0,0 +1 @@ | |||
<?= "expression" // properly warns about missing ';' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This tests the regression that was mentioned
This is ready for review. Tests pass, and the bug was fixed I changed InlineHtml->echoExpression to InlineHtml->echoStatement. It didn't make sense to add an EchoExpression to the list of statements (The new behavior matches what a regular |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks good to me, thanks!
Also, treat the literal
echo
as a statement instead of an expression,while preserving the generated ASTs.
print expr
, (PrintIntrinsicExpression
),which is correctly treated as an expression.
Before, tolerant-php-parser parsed the tokens after
<?=
and<?php
the same way.
This PR adds
TokenKind::ScriptSectionStartWithEchoTag
and handles thesubsequent tokens differently (as an expressionList) if that token kind is seen instead of
ScriptSectionStartTag
.This reuses
EchoExpression
to make it easier for applications to reuse code handling printstatements. The resulting expression will have no
echoKeyword
because
<?=
was part of the precedingInlineHTML
Node.the EchoExpression will be moved into the outer statement list.
php-language-server
or other projects.Virtually all of the AST trees were changed due to the
addition of a new property to InlineHTML.
I'm not familiar enough with the language spec to be sure that I
will be able to catch and normalize everywhere that InlineHTML
<?=
can occur,so I gave up and made it permissible to be part of the final AST.
The addition of the new property to CHILD_NODES can be undone if you're confident.
(loops? other statements? (etc.).)