リカバリ処理。

最新版だと、頑張るの結構キツイかもよ。
既に、公式サイトにサンプルがあるレベルだとRuntimeに実装済み。


ANTLR3.1.1における実装。

  • BaseRecognizer
  protected Object recoverFromMismatchedToken(IntStream input, int ttype, BitSet follow)
    throws RecognitionException
  {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if ( mismatchIsUnwantedToken(input, ttype) ) {
      e = new UnwantedTokenException(ttype, input);
      /*
      System.err.println("recoverFromMismatchedToken deleting "+
                 ((TokenStream)input).LT(1)+
                 " since "+((TokenStream)input).LT(2)+" is what we want");
       */
      beginResync();
      input.consume(); // simply delete extra token
      endResync();
      reportError(e);  // report after consuming so AW sees the token in the exception
      // we want to return the token we're actually matching
      Object matchedSymbol = getCurrentInputSymbol(input);
      input.consume(); // move past ttype token as if all were ok
      return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if ( mismatchIsMissingToken(input, follow) ) {
      Object inserted = getMissingSymbol(input, e, ttype, follow);
      e = new MissingTokenException(ttype, input, inserted);
      reportError(e);  // report after inserting so AW sees the token in the exception
      return inserted;
    }
    // even that didn't work; must throw the exception
    e = new MismatchedTokenException(ttype, input);
    throw e;
  }

single token insertion / deletionの両方を試みる様になっとる。
これは、つまり、汎用的な方法ではなくて、
各ルール単位に特徴的なリカバリ処理を書かないんなら、
Runtimeに任せちゃっておkって事かな。


とりあえず、起きる例外のメッセージを、理解可能な感じに丸めるだけでも、
それはそれで、いい感じになって欲しいな…と思う次第で。