-
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
Comments in the same line as an expression #38
Comments
Hi @chris-l, Here you can find another edge cases with comments : #6 (comment) In my opinion comments should not be parts of the AST because the aim of the syntax tree is to be interpreted by a VM or a compiler, so each node contains a structural logic. Take this pseudo code addition sample : As you can see AST should define operations, so you can't introduce inside comments tags. Actually comments are standalone tags in order to be ignored by the processing engine, except in classes (& traits, interfaces), because their body is an object, and comments decorates nodes. But as you stated, if the comment is on the same line with the expression, it should be attached on it. You may have a way to implement this on activating node positions : https://github.com/glayzzle/php-parser/wiki/Nodes-positions After playing with your project https://github.com/chris-l/php-unparser, I'm actually thinking that the parser misses an option to extract document presentation (more than just comments) , for by example :
This mode is usefull only when you want to rebuild the original document from AST, I'll put it on the todo list - more informations here : #39 |
Hi @chris-l, By including the location, you are now able on a comment node to figure out if it's on the same line as the previous node. Here the result of your original code : PHP Script : <?php
echo $a; // this is a comment
echo $b; AST Structure : {
"kind": "program",
"loc": {
"source": null,
"start": {
"line": 1,
"column": 0,
"offset": 0
},
"end": {
"line": 3,
"column": 8,
"offset": 44
}
},
"children": [
{
"kind": "echo",
"loc": {
"source": null,
"start": {
"line": 2,
"column": 0,
"offset": 6
},
"end": {
"line": 2,
"column": 8,
"offset": 14
}
},
"arguments": [
{
"kind": "variable",
"loc": {
"source": null,
"start": {
"line": 2,
"column": 5,
"offset": 11
},
"end": {
"line": 2,
"column": 7,
"offset": 13
}
},
"name": "a",
"byref": false,
"curly": false
}
]
},
{
"kind": "doc",
"loc": {
"source": null,
"start": {
"line": 2,
"column": 9,
"offset": 15
},
"end": {
"line": 3,
"column": 0,
"offset": 36
}
},
"isDoc": false,
"lines": [
"this is a comment"
]
},
{
"kind": "echo",
"loc": {
"source": null,
"start": {
"line": 3,
"column": 0,
"offset": 36
},
"end": {
"line": 3,
"column": 8,
"offset": 44
}
},
"arguments": [
{
"kind": "variable",
"loc": {
"source": null,
"start": {
"line": 3,
"column": 5,
"offset": 41
},
"end": {
"line": 3,
"column": 7,
"offset": 43
}
},
"name": "b",
"byref": false,
"curly": false
}
]
}
],
"errors": []
} |
If I try to parse this:
Its get parsed like this:
Which is the same AST I get if I parse this instead:
That is a problem, because in the first case the comment belongs to
echo $a
, while the second case is more likely to be a comment aboutecho $b
.Perhaps it should be a decorator, with a property saying its in the same line? I'm not sure what would be the right thing to do. What do you think about this?
The text was updated successfully, but these errors were encountered: