Skip to content
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

Closed
chris-l opened this issue Dec 19, 2016 · 2 comments
Closed

Comments in the same line as an expression #38

chris-l opened this issue Dec 19, 2016 · 2 comments
Assignees
Labels

Comments

@chris-l
Copy link
Contributor

chris-l commented Dec 19, 2016

If I try to parse this:

echo $a; // this is a comment
echo $b;

Its get parsed like this:

  ["sys", "echo", [["var","$a"]]],
  ["comment", ["// this is a comment\n"]],
  ["sys", "echo", [["var","$b"]]]

Which is the same AST I get if I parse this instead:

echo $a;

// this is a comment
echo $b;

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 about echo $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?

@ichiriac
Copy link
Member

ichiriac commented Dec 19, 2016

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 : let x = a + 1 and here a possible AST ['let', 'x', ['add', 'a', 1]]

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 :

  • the kind of quotes
  • if the namespaces is under brackets
  • the short form on statements like if/for
  • the output mode under HTML <a href="<?= $var ?>"...
  • extra whitespace informations (up to you to ignore them)

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

@ichiriac ichiriac self-assigned this Dec 19, 2016
@ichiriac ichiriac modified the milestone: 0.2.0 Dec 21, 2016
@ichiriac
Copy link
Member

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": []
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants