-
Notifications
You must be signed in to change notification settings - Fork 71
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
Script tags & AST Strructure / Introducing Breaking Changes #170
Comments
@ichiriac I've been working on this exact issue yesterday in the prettier plugin - my approach is to parse the tokens and add new There is one more case for which we're looking at the AST tokens right now: <?php echo 1; ?>
<?php echo 2; ?>
// same AST as
<?php echo 1; ?><?php echo 2; ?> (see prettier/plugin-php#503) |
Here the problem comes from the way interprets newlines after closing tag (another example of AST oriented for execution). The first "\n" is stripped from the output, a sort of authors choice in order to break html output with extra new lines. Actually php-parser is used to :
In principle sticking to the document structure should not be a problem. The reason PHP engine avoids extra information is because the have only one use case, but it's not the case here. What I plan to do is to introduce a script node (with children array nodes) and an output node with raw html output. Here some use cases : <?php
function foo() {
$var = true;
?>OUTPUT <?
for(;;) {
?> HTML<%
}
}
?>
Another output <?= foo(); ?> As nesting script nodes breaks AST struction, only open_tag at the document root would be represented : Program {
children: [
Script : {
short: false,
children: [
Function: {
children: [
Assing: { ... },
Output: { ... nextShortTag: true },
For: { .. Output { nextAspTag: true } .. },
]
}
]
},
Output: {},
EchoScript: {
expr: { Call foo() }
}
]
} Do you see any other kind of missing informations ? (/cc @chris-l) |
This looks really good - I think we'd have it much easier to format files with lots of inline HTML. |
Looks good 👍 |
@ichiriac I thoroughly studied your assumption, it seems to me that it will not solve the problem. New kind of node can introduce new problems. I prefer add option I think it's not difficult to implement. |
I'm agree that creating new nodes does not solves directly your problem, and just fixing the behavior of the inline node would be enough, but here I'm looking for being more close to the document structure, and without a new script tag it would be analog to reducing & simplifying the structure (and in some cases loosing information). So in principle no more implied syntax choices, every structure should have it's own node - keeping the document semantics clean. If the ouput corresponds to the raw HTML output, so the script should have its own node and include inside its childs as it adds a new tree level. In principle at the document root you should only have Here we speak about the script tags but as I will digg into nodes it could maybe then be applied to another structures (I don't see any other right now) I think that's the right moment to introduce this kind of change as the parser is not yet released and I do not want to end up next with a v4 because of changing tags and breaking backwards compatibility. |
#171 related : removal of |
@ichiriac maybe it is good idea introduce Now we print <?php
// Comment
?> |
@ichiriac it think right now it is not high priority, better stabilize parser, then implement better ast for |
related to prettier/plugin-php#517 <!DOCTYPE html>
<html lang="de">
<head>
<?php /* ups */
if (1==1)
{
echo 'FOO';
} ?>
</head>
<body>
</body>
</html> NB : The |
@ichiriac we use own algorithm to attach comment, it is simple comment location should fit in new |
The
A
from AST stands for Abstract, meaning it does not matters about source code formatting. This can result to removing/loosing some extra informations, and that makes impossible to transform AST back to the original source.The reason why AST is like that is because it's purpose is to be an intermediate machine language/structure, it's used by a VM or JIT machine in order to generate atomic machine instructions.
But as
php-parser
is mainly used to edit PHP sources, and the next major release introduce anyway breaking changes that the good time to decide the AST orientations.If I choose to keep on AST the same structure as the document, the script tag nodes may also be included as an AST node in order to be able to scan something like this :
@czosel & @evilebottnawi - do you see any other missing use case where the formatting is lost ?
The text was updated successfully, but these errors were encountered: