-
Notifications
You must be signed in to change notification settings - Fork 68
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
Added option to suppress parse errors #12
Conversation
@@ -34,12 +34,13 @@ module.exports = function(engine) { | |||
* The basic parser api | |||
*/ | |||
var api = { | |||
// le lexer |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
french touch :)
Great functionnality, I really appreciate your help ! I've merged it but there is 2 mistakes : --> The parser is not stopped by the throw - so the parser should be instable and make some inconsistent AST. To fix this you have 2 options :
Also I would prefer to avoid sending things to console as this is a library. It would be more usefull to store the last error directly a a variable with the message, the line, etc ... and it's up to the developper to check if the was errors during the parsing. |
'\nat line ' + this.lexer.yylloc.first_line | ||
); | ||
var errorMessage = 'Parse Error : unexpected ' + token + msgExpect + ' at line ' + this.lexer.yylloc.first_line; | ||
if (suppressErrors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be if (this.suppressErrors) {
For my use case of the parser, ideally it should be able to recover from a parse error and continue parsing the rest of the file. But for now, putting the token at the EOF would be a good compromise. Is this correct? For errors, should we have an additional sub-array with a list of the errors thrown while parsing? Something like I'll be submitting a new pull request to fix the bug that I introduced (forgot to use |
Already done the changes and commit them (the same as you have done). I'm interested into your proposal to add a support for gracefull parsing (with another option for example) and put into the ast the error. The tricky part is to recover a clean state when you will want to continue the parsing, so you will have to eat every bad token... but avoid eating good ones, and leave the function as early as possible ... It should be a trick for inserting automatically the error node by decorating the each eater function (maybe automatically) and it could be a solution to keep the exception system so catching the error and avoiding the eater to be too greedy. I'll open an issue for that |
I've added a flag to allow you to suppress errors when parsing a PHP file. This means that if a parse error is enountered, the AST generated up to that point will be returned instead of throwing an exception.
Default behaviour is to not suppress errors (how it used to be).
Usage:
This also fixes a bug where the line number is not being returned in the exception thrown.