fstirlitz / luaparse

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

Multiple independent parsers / reentrancy #62

Open fstirlitz opened 5 years ago

fstirlitz commented 5 years ago

Currently, this code works:

luaparse.parse({ wait: true }).write('foo = "');
console.info(luaparse.parse('bar"'));

It prints out:

{
    "type": "Chunk",
    "body": [
        {
            "type": "AssignmentStatement",
            "variables": [
                {
                    "type": "Identifier",
                    "name": "foo"
                }
            ],
            "init": [
                {
                    "type": "StringLiteral",
                    "value": "bar",
                    "raw": "\"bar\""
                }
            ]
        }
    ],
    "comments": []
}

This is because the library maintains a single lexer and parser state shared between invocations of the parse function; there is no way to concurrently parse multiple Lua scripts. Code that expects each .parse({ wait: true }) to create a new parser independent of any previously created one is in for a nasty surprise.

There should be a way to create multiple isolated parser states. This will probably necessitate a quite invasive re-write, and may break some backwards compatibility unless this is done through separate API calls. Then though, the sort of code that relies on non-reentrancy is not one I wish to personally support.

fstirlitz commented 3 years ago

I’ll think I’ll add a re-entrant API in 0.3.2, while leaving the current one in place, with semantics preserved. Not sure what I’ll do in 0.4. I may remove the stand-alone parse call entirely, or I may change it to construct a temporary parser state, so that each parse call is independent from the next. Either will be technically a breaking change.