Sample ANTLR error handling in Intellij

Greetings earthlings,

After more exploration of the source code, I think I have ANTLR error handling integrated properly into intellij. It took me a few hours to figure out that I cannot use the same lexer for both syntax highlighting and parsing. My ANTLR parser wants to ignore bad token sequences which works but then all of the error red squigglies are shifted off target.

You can see a sample input with the attached PSI tree. The input contains a token/lexer error, an extraneous token automatically deleted, a missing token automatically inserted, and then another missing token. Note all of the error messages and recovery are automatically done by ANTLR. All I have done is build adapter objects that make ANTLR play nice with Intellij.  The amazing thing is that I did not have to change ANTLR code generation or libraries in any way. Perhaps this is a testament to the flexibility of both tools.  Woot!

Once they get a satisfactory basic generic plug-in for this little language / antlr grammar, I will try to build an automatic plug-in generator for ANTLR grammars. I'm amazed to how fast this has come together.

Terence



Attachment(s):
Prop.g4.zip
sample-errors.png
2 comments

Nice start!

Could you please also ensure the parser adapter to be able to cancel
parsing process upon request ? E.g. ours PsiBuilderImpl regularly calls
ProgressIndicatorProvider.checkCanceled(); which might end the parsing
by throwing special ProcessCancelledException.

On 9/26/2013 4:45 AM, Terence Parr wrote:

Greetings earthlings,

>

After more exploration of the source code, I think I have ANTLR error handling integrated properly into intellij. It took me a few hours to figure out that I cannot use the same lexer for both syntax highlighting and parsing. My ANTLR parser wants to ignore bad token sequences which works but then all of the error red squigglies are shifted off target.

>

You can see a sample input with the attached PSI tree. The input contains a token/lexer error, an extraneous token automatically deleted, a missing token automatically inserted, and then another missing token. Note all of the error messages and recovery are automatically done by ANTLR. All I have done is build adapter objects that make ANTLR play nice with Intellij.  The amazing thing is that I did not have to change ANTLR code generation or libraries in any way. Perhaps this is a testament to the flexibility of both tools.  Woot!

>

Once they get a satisfactory basic generic plug-in for this little language / antlr grammar, I will try to build an automatic plug-in generator for ANTLR grammars. I'm amazed to how fast this has come together.

>

Terence

>

---
Original message URL: http://devnet.jetbrains.net/message/5499763#5499763

>

0

Hi. Maxim. Sure. So I guess the parser calls build.mark() or advance() or something which can throw ProcessCancelledException? If I ignore the exception won't it just blast out of my parser all the way to whoever called Lexer or Parser start()?

0

Please sign in to leave a comment.