fstirlitz / luaparse

A Lua parser written in JavaScript
https://fstirlitz.github.io/luaparse/
MIT License
459 stars 91 forks source link

Allow preserving parenthesis. #113

Open ravener opened 2 years ago

ravener commented 2 years ago

This is very useful for when converting the tree back to source code to know where parenthesis have been used by the user.

This is inspired by acornjs and is exactly how they did it.

Example: print((5 + 2) * 3)

{
  "type": "Chunk",
  "body": [
    {
      "type": "CallStatement",
      "expression": {
        "type": "CallExpression",
        "base": {
          "type": "Identifier",
          "name": "print"
        },
        "arguments": [
          {
            "type": "BinaryExpression",
            "operator": "*",
            "left": {
              "type": "ParenthesizedExpression",
              "expression": {
                "type": "BinaryExpression",
                "operator": "+",
                "left": {
                  "type": "NumericLiteral",
                  "value": 5,
                  "raw": "5"
                },
                "right": {
                  "type": "NumericLiteral",
                  "value": 2,
                  "raw": "2"
                }
              }
            },
            "right": {
              "type": "NumericLiteral",
              "value": 3,
              "raw": "3"
            }
          }
        ]
      }
    }
  ],
  "comments": []
}
fstirlitz commented 2 years ago

First of all, the test suite is failing. I will not be merging anything without that fixed.

Second, this may conflict with #98 when it is finally merged.

And finally, I am not necessarily sold on preserving parentheses where they are not semantically meaningful. This is an abstract syntax tree after all.

ravener commented 2 years ago

@fstirlitz

1) not sure why tests failed, it wasn't really a major change, i'll take a look. 2) not sure what to say about this, i think this change is still compatible with it. 3) well as I said it helps when transforming the tree back to source code, it's hidden behind an option anyway.

Edit: the tests was due to a typo, just fixed it.

ravener commented 2 years ago

Now CI is failing due to coverage, I have no idea what to do from here on but I hope you will consider the idea, it might be really useful for some.

If not then there must atleast be some way to achieve this from the user side.