Skip to content
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

Customizable error messages. #500

Closed
srinivasmk opened this issue Jun 19, 2017 · 6 comments · Fixed by #505
Closed

Customizable error messages. #500

srinivasmk opened this issue Jun 19, 2017 · 6 comments · Fixed by #505

Comments

@srinivasmk
Copy link

No description provided.

@bd82
Copy link
Member

bd82 commented Jun 19, 2017

Can you provide a code snippet demonstrating what you are trying to achieve?
I.E a little rule grammar to show the current error message and separately describe the custom
message you want to use instead?

@srinivasmk
Copy link
Author

srinivasmk commented Jun 19, 2017

Thanks for the quick reply. I am trying to implement a parser using Chevrotain. I like to add my own custom error message for each rule.
For example,

$.RULE("statement", function () {
      $.CONSUME(variable);
      $.CONSUME(equals);
      $.CONSUME(StringLiteral);
      $.CONSUME(terminal);
    });

This is the sample rule for defining a variable (without space)
Input :
variable="testStr";

If semicolon gets missed at the end the statement, then it is throwing error as
Expecting token of type --> terminal <-- but found --> '' <--

Instead of using above message, can i implement my own custom message ?
Example : Missing ";" at the end of the statement

Thanks in advance.

@bd82
Copy link
Member

bd82 commented Jun 19, 2017

Current Status

There is some ability to customize error messages.

Requested Feature

The capability you are requesting is not currently supported.
However It is something I would very much like Chevrotain to have.

A naive implementation would just support providing the desired string by the end users.
However that would quickly become verbose.

$.RULE("statement", function () {
      $.CONSUME(variable, "missing variable name in assignment statement");
      $.CONSUME(equals, "missing operator of assignment statement");
      $.CONSUME(StringLiteral, "missing RHS of assignment statement");
      $.CONSUME(terminal, "Missing ";" at the end of the statement");
    });

A better solution would probably be to provide the parser with an "ErrorMessageCustomizer"
function during parser construction.

Given an Error type (MismatchedTokenException/NoViableAltException/...) and the position in the grammar this customize should produce the appropriate error message.

  • A user's messageCustomizer should be able to fallback to the default implementation.

@bd82
Copy link
Member

bd82 commented Jun 19, 2017

Basically the existing hardcoded error message building need to be refactored to use some sort of
customizer (strategy) pattern and thus allow any end-user to build his/hers own error messages.

@bd82 bd82 changed the title How to give custom error message for each rule ? customizable error messages. Jun 19, 2017
@bd82 bd82 changed the title customizable error messages. Customizable error messages. Jun 19, 2017
@srinivasmk
Copy link
Author

Thank you for considering this.

@bd82
Copy link
Member

bd82 commented Jun 20, 2017

I'm playing around with implementing this.
So far it seems pretty simple...

If you don't want to wait for the official API
You could just override the existing method that produce the error message in that specific example you provided.

Unfortunately this seems like the only easy one to override.

bd82 added a commit that referenced this issue Jun 21, 2017
bd82 added a commit that referenced this issue Jun 21, 2017
bd82 added a commit that referenced this issue Jun 21, 2017
bd82 added a commit that referenced this issue Jun 21, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 22, 2017
bd82 added a commit that referenced this issue Jun 23, 2017
bd82 added a commit that referenced this issue Jun 23, 2017
bd82 added a commit that referenced this issue Jun 23, 2017
bd82 added a commit that referenced this issue Jun 23, 2017
@bd82 bd82 closed this as completed in #505 Jun 23, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants