sebastienros / esprima-dotnet

Esprima .NET (BSD license) is a .NET port of the esprima.org project. It is a standard-compliant ECMAScript parser (also popularly known as JavaScript).
BSD 3-Clause "New" or "Revised" License
430 stars 75 forks source link

Is there a way to tell the parser to try and continue after an error? #442

Closed sperlis closed 2 months ago

sperlis commented 2 months ago

I had two pieces of code that had error in them:

let a = 10;
lex b = {
    a,
    c: 20
    };

I used lex instead of let And:

let name = 'there';
let x = `hello ${name}`;";

Notice the trailing "; at the end of the second line.

The parser throws an exception when hitting these mistakes.

Is there a way to tell the parser to try and continue? Ignore the errors somehow?

sebastienros commented 2 months ago

I can of two solutions.

The first one would be for the grammar to handle these possibilities and instead of returning Error or not matching anything have a role not generic which can match what you would expect to be an error but just return it as a special node or a flag in the Parser context you provide. Maybe we could add an ErrorAndContinue() kind of parser. I'll provide an example when I have more time.

The second option could be native to Parlot in the form of a parser definition to lookup to recover from and start fresh. For instance ';' or LF such that if an error occurs it does it but then tries to find the next occurrence of this pattern and starts from a specific known parser again. But this could definitely be the ErrorAndContinue parser I was mentioning above.

sperlis commented 2 months ago

Thank you for the response. Gave me some things to think about.