fstirlitz / luaparse

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

Tolerant error handling #9

Open oxyc opened 11 years ago

oxyc commented 11 years ago

Implement a mode where errors are stored instead of thrown. Necessary recovery functions should be exposed so that users can hook into it.

I don't think the parser itself should include recovery functionality but should definitely have an example available.

pablomayobre commented 7 years ago

This would be great for syntax highlighting, autocompletion, getting a list of definitions and even better static analysis (letting you get many errors in a single run)...

What would need to be changed in order to support this?

pablomayobre commented 7 years ago

I'm sorry, are there any news on this? Somewhere where help is needed or a way to start working on this?

fstirlitz commented 7 years ago

To my knowledge, unless @oxyc is sitting in a cave and secretly crafting a solution, this isn't being worked on currently. As the cliché goes, patches are welcome.

I think simply slapping some try-catch blocks in a few strategic places (where the catch block puts the syntax error in some array and then just swallows the exception) could give us a rudimentary solution. But that would hardcode some policy decisions about interpreting non-well-formed code into the parser. I'm not sure what design @oxyc had in mind when he filed this issue.

(And while we're doing this, I'd change the SyntaxError to a dedicated exception type.)

oxyc commented 7 years ago

Yeah I've given a a few shots at implementations but couldn't really come up with anything that was useful. At the moment I don't have access to the code I experimented with and it was a while ago so not certain what I tried.

I at least wrapped lex() within next() in a try-catch and then attempted to move on from errors. Also had several recovery conditions in the individual parse functions but it just turned into a mess. I think I would have to read up on strategies if I were to implement it. But yes, patches are definitely welcome hehe.

Acorns loose parser would be a something to look at. https://github.com/ternjs/acorn/tree/master/src/loose

fstirlitz commented 5 years ago

When parse errors are made recoverable, it seems it shouldn't be too hard to go the whole way and introduce warning lints as well, or at least a mechanism for such lints to be easily plugged in.

Some warnings to consider: