[antlr-interest] Recovery with consumeUntil()

Ivan Veselov veselov at gmail.com
Thu May 31 10:43:39 PDT 2007


Hi all!

I have some problems with recovery after error in my grammar. It seems
that standard recovery process doesn't do what I need and I don't know
yet how to fix it manually.

Here is part of my grammar:

=============================================
importDefs
 :  importDef (importDef)*
 ;

importDef
options {defaultErrorHandler=true;}
 :  "import" identifierStar (SEMI)?
 ;

identifierStar
 :  IDENT
  (
      options { greedy=true; }
       :   DOT IDENT
  )*
  (DOT STAR)?
 ;

classDef
 :   "class"
    .... //other class parts

compilationUnit
  : importDefs
    classDef

=============================================

I need to provide code completion for my imports. So what I am parsing
is a bit incomplete source code, something like this:

import a.b.   //incomplete package, I need to get child packages or
classes for package "a.b"
class MyClass
{}

So, parser fails on incomplete import and then I have added options
"{defaultErrorHandler=true;}" to import definition. It adds try/catch
block to source of that rule with reportError() and recover() methods
in 'catch' clause. But it seems that it skips not to next import or to
"class" keyword, which can be start of classDef, but to the end of
file :(
After matching "a.b", parser sees that "class" is not ID and tries to
match it as STAR (something like a.b.*), but certainly it fails. Then
it tries to skip until some tokens from bitset are apeeared and skip
all the tokens up to EOF.

It seems that it is incorrect behaviour in my case.

So, the question is: do you know any way to generate these BitSets
(follow sets) for recovery() method to correct recovering process
manually? Or maybe another solution of my problem?

I have seen some messages related to this topic in antlr-interest, but
I haven't found answer yet.
Could you please help me with this problem? Any help would be appreciated.
Thank you!

With best regards,
Ivan Veselov.


More information about the antlr-interest mailing list