What's the official way to custom annotate error instead of the default parser error message?

Answered

I'm working on a custom language plugin with .bnf file below.

Root ::= RootEntry*
private RootEntry ::= !<<eof>> topLevelEntry { pin = 1 }

private topLevelEntry ::= Field

Field ::= FType IDENTIFIER

FType ::= 'int' | 'string'

For example string foo is completely a correct statement. But if I input a wrong FType intentionally for example wrongType foo,I wish to customize the error message with annotator: “Can not resolve symbol: wrongType”, instead of the default parser message: “<f type> expected, got ‘wrongType’ ” like below. Which looks really weird because users have no idea what <f type> is.

For example, this is how it looks like in Java (Which I want to achieve):

Is there any way to do so? Should I change FType to FType ::= IDENTIFIER so that I can handle this later?

0
2 comments

Hi,

“Cannot resolve symbol” is not a syntax error, but a semantic error, and it is reported on higher level (most likely Annotator).

In your case, consider using nameattribute in FType rule:

FType ::= 'int' | 'string' {
  name = "'int' or 'string'"
}

See: https://github.com/JetBrains/Grammar-Kit#attributes-for-error-recovery-and-reporting

To implement a behavior similar to the mentioned Java example, you can change:

FType ::= 'int' | ‘string’

to

FType ::= TYPE // TYPE should be a token matching letters, I guess

After that, implement an Annotator that will report the error you want to display if the value is not int or string.

0

Thanks, that helped a lot. I think I know what I'm going to do. Thanks again

0

Please sign in to leave a comment.