[antlr-interest] lexer state ?

Matthew Ford Matthew.Ford at forward.com.au
Thu Apr 18 00:51:36 PDT 2002


Actually the problem is calling skipLine when there is a parsing error.
I use  (where TFLexer is my lexer)

public void reportError(ANTLRException ex) {
    // write out error here
 }

 public void processError(ANTLRException ex) throws TokenStreamException,
    CharStreamException {
  // actually only throws TokenStreamIOException others caught here
  int tokenType=0;
  LexerSharedInputState inputState = TFlexer.getInputState();
  inputState.guessing = 0; // clear guessing mode
    Debug.out("in processError", MAJOR, 0);
    if (!errorFlag) { // first error
     reportError(ex);
     errorFlag=true;  //block new errors until after syncing.
    }

  do {
   try {
    if (ex instanceof TokenStreamRecognitionException) {
     TokenStreamRecognitionException rex =
(TokenStreamRecognitionException)ex;
      // get underlying exception
     ex = null; // have handled this one now
     if ((rex.recog instanceof MismatchedCharException) ||
        (rex.recog instanceof NoViableAltForCharException)) {
      try {
          TFlexer.consume(); // remove current error char;
      } catch (CharStreamException cse) {
       if ( cse instanceof CharStreamIOException ) {
        throw new TokenStreamIOException(((CharStreamIOException)cse).io);
       } else {
        throw new TokenStreamIOException(new IOException(cse.getMessage()));
       }
      }
     }
      }

      tokenType = LA(1);
       if ((tokenType != EOF) && (tokenType != SEMI)) {
        consume(); // remove ;
       }

   } catch (TokenStreamRecognitionException ex1) {
     ex = ex1; // and loop
//      TFlexer.consume(); // remove current error char;
   } catch (TokenStreamRetryException ex1) {
     throw new TokenStreamIOException(new IOException(ex1.getMessage()));
   }
    } while ( tokenType != SEMI && tokenType != EOF && !isEOF());

 }

 private boolean errorFlag = false;

 private boolean eofFlag = false;

 public boolean isEOF() {
  return eofFlag;
  }

----- Original Message -----
From: "Lloyd Dupont" <lloyd at galador.net>
To: <antlr-interest at yahoogroups.com>
Sent: Thursday, April 18, 2002 2:22 PM
Subject: Re: [antlr-interest] lexer state ?


> > is there in ANTLR some kind of Lexer state ?
> > I mean, I want, in case of parsing error, to skip all text up to the
endof
> line and resume
> > my parsing next line.
> > how to do that ?
>
> I solve it using the following rule ?
> is it a good idea (I mean will it stay stable with further evolution ?)
>
> //------------------------------------------------
> skipLine
> {
>   while(LA(1) != NEWLINE)
>     consume();
> }:
>   NEWLINE
>   ;
> //------------------------------------------------
>
>
>
>
>
> Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/
>
>


 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/ 



More information about the antlr-interest mailing list