Giter Site home page Giter Site logo

Comments (6)

nikic avatar nikic commented on May 3, 2024

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.

pscheit avatar pscheit commented on May 3, 2024

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.

nikic avatar nikic commented on May 3, 2024

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.

yasheena avatar yasheena commented on May 3, 2024

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.

Ekstazi avatar Ekstazi commented on May 3, 2024

You shouldn't work with stack directly, think that you need to add a new rule for comments in parser.

from php-parser.

nikic avatar nikic commented on May 3, 2024

Closing this in favor of #41.

from php-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.