Comments (6)
Yes, comments are assigned to the node after them, so if there is nothing following the comment it won't be collected anywhere. There isn't really anything I can do against this, short of switching to a completely different model (where you get a parse tree instead of an abstract syntax tree). If you need to preserve all comments the PHP parser isn't a suitable tool, you'll need to work on the tokens directly.
from php-parser.
is this the same issue?
if (true) {
// body comment
}
body comment is not stored in if? What about a fallback for comments to be attached to its parent (with line number?) when no element is coming afterwards?
Why are inline comments attached to the following element?
$myVar = 8; // 0-based
$otherVar = 9; // 1-based
0-based
is assigned to $otherVar
and // 1-based
is dropped. Would you agree, that this is wrong for inline comments?
Is the parse Tree you name, the one were the comment would be an individual node?
from php-parser.
Yep, those also have the same issue. The comment is assigned to the next node that follows, so for the if
it is just dropped and for your second example it's assigned to the next assignment. I do agree that this is wrong, but I don't see an easy way to change this (with the current approach). Also it's often not really clear what statement a comment "belongs" to. E.g. consider the following code:
<?php
$someCodeIs; // Here is a big block
$writtenInHere; // of documentary text
$otherMultipleLines; // that isn't really related
// to the particular statements
// on the left. It's just a multiline
// comment formatted like that.
Here you can't really say that a comment on the right belongs to the statement left to it.
In any case, I'll try out whether there is some easy way to generate a parse trace which retains all information from the source code. I'm not sure whether this is possible with the data I have available right now.
from php-parser.
I tried to fix this problem by adding the file /node/Stmt/Comment.php to the parser with the following content:
<?php
/**
* @property null|PHPParser_Node_Expr $num Number of loops to break
*/
class PHPParser_Node_Stmt_Comment extends PHPParser_Node_Stmt
{
/**
* Constructs a comment node (if there is no node to attach a comment to).
*
* @param array $attributes Additional attributes
*/
public function __construct(array $attributes = array()) {
parent::__construct(
$attributes
);
}
}
I also added the folowing function to the file /PrettyPrinter/Default.php:
// For printing comments which couldn't be assigned to another node
public function pStmt_Comment($node) {
return '';
}
Now I searched for the right position in the file Parser.php to add a "Comment-Node", if during reducing the stack a still existing (unhandled) comment exists. Do you think this method would be useful to handle this comment problem? If yes, I need a hint: The stack handling of the parser is so complex so I have no idea where I have to insert the additional code.
from php-parser.
You shouldn't work with stack directly, think that you need to add a new rule for comments in parser.
from php-parser.
Closing this in favor of #41.
from php-parser.
Related Issues (20)
- Comment attributes not being repeated for nested nodes is not in UPGRADING guide
- [5.0] Parser crashes on an empty file HOT 1
- [Format-preserving printer] How to get rid of trailing commas in function calls/parameter definitions/closure uses HOT 2
- [5.0] PropertyItem extends Stmt by mistake? HOT 1
- 5.0.0 - Call to undefined method PhpParser\ParserFactory::createForHostVersion() HOT 2
- Declaration of PhpParser\Parser\Multiple::parse HOT 1
- Upgrade Guide to v5.0 -- typo error on Changes to the lexer chapter
- TokenPolyfill tries to construct itself with an id which is a string HOT 5
- getPhpVersion() method has different return type in V5 HOT 2
- Should PhpParser\Node\Name extend PhpParser\Node\Expr ? HOT 2
- Parse arbitrary expressions HOT 4
- Appending new statements HOT 2
- Parser instance is not automatically garbage collected due to self references HOT 4
- TokenPolyfill issue on PHP 7.4 platform HOT 25
- Pretty printing removes leading whitespace from class declaration on first line HOT 6
- php-parser v4.19.0 got deprecated Optional parameter before required parameter on PrintableNewAnonClassNode.php HOT 1
- Is there a way to modify or remove comments? HOT 1
- Error "Syntax error, unexpected EOF on line" when parse valid php file HOT 1
- Is there a way to find places from where specific class/object methods are called HOT 6
- Confusing docs about the usage of NodeTraverser
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from php-parser.