コード例 #1
0
ファイル: BaseRecognizer.java プロジェクト: jjchaverra/antlr3
 /**
  * Attempt to recover from a single missing or extra token.
  *
  * <p>EXTRA TOKEN
  *
  * <p>LA(1) is not what we are looking for. If LA(2) has the right token, however, then assume
  * LA(1) is some extra spurious token. Delete it and LA(2) as if we were doing a normal match(),
  * which advances the input.
  *
  * <p>MISSING TOKEN
  *
  * <p>If current token is consistent with what could come after ttype then it is ok to "insert"
  * the missing token, else throw exception For example, Input "i=(3;" is clearly missing the ')'.
  * When the parser returns from the nested call to expr, it will have call chain:
  *
  * <p>stat &rarr; expr &rarr; atom
  *
  * <p>and it will be trying to match the ')' at this point in the derivation:
  *
  * <p>=&gt; ID '=' '(' INT ')' ('+' atom)* ';' ^ match() will see that ';' doesn't match ')' and
  * report a mismatched token error. To recover, it sees that LA(1)==';' is in the set of tokens
  * that can follow the ')' token reference in rule atom. It can assume that you forgot the ')'.
  */
 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;
 }
コード例 #2
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 public void consumeUntil(IntStream input, int tokenType) {
   // System.out.println("consumeUntil "+tokenType);
   int ttype = input.LA(1);
   while (ttype != Token.EOF && ttype != tokenType) {
     input.consume();
     ttype = input.LA(1);
   }
 }
コード例 #3
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 /** Consume tokens until one matches the given token set */
 public void consumeUntil(IntStream input, BitSet set) {
   // System.out.println("consumeUntil("+set.toString(getTokenNames())+")");
   int ttype = input.LA(1);
   while (ttype != Token.EOF && !set.member(ttype)) {
     // System.out.println("consume during recover LA(1)="+getTokenNames()[input.LA(1)]);
     input.consume();
     ttype = input.LA(1);
   }
 }
コード例 #4
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 /**
  * Attempt to recover from a single missing or extra token.
  *
  * <p>EXTRA TOKEN
  *
  * <p>LA(1) is not what we are looking for. If LA(2) has the right token, however, then assume
  * LA(1) is some extra spurious token. Delete it and LA(2) as if we were doing a normal match(),
  * which advances the input.
  *
  * <p>MISSING TOKEN
  *
  * <p>If current token is consistent with what could come after ttype then it is ok to "insert"
  * the missing token, else throw exception For example, Input "i=(3;" is clearly missing the ')'.
  * When the parser returns from the nested call to expr, it will have call chain:
  *
  * <p>stat -> expr -> atom
  *
  * <p>and it will be trying to match the ')' at this point in the derivation:
  *
  * <p>=> ID '=' '(' INT ')' ('+' atom)* ';' ^ match() will see that ';' doesn't match ')' and
  * report a mismatched token error. To recover, it sees that LA(1)==';' is in the set of tokens
  * that can follow the ')' token reference in rule atom. It can assume that you forgot the ')'.
  */
 public void recoverFromMismatchedToken(
     IntStream input, RecognitionException e, int ttype, BitSet follow)
     throws RecognitionException {
   // if next token is what we are looking for then "delete" this token
   if (input.LA(2) == ttype) {
     reportError(e);
     /*
      * System.err.println("recoverFromMismatchedToken deleting "+input.LT
      * (1)+ " since "+input.LT(2)+" is what we want");
      */
     beginResync();
     input.consume(); // simply delete extra token
     endResync();
     input.consume(); // move past ttype token as if all were ok
     return;
   }
   if (!recoverFromMismatchedElement(input, e, follow)) {
     throw e;
   }
 }
コード例 #5
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 /**
  * Recover from an error found on the input stream. Mostly this is NoViableAlt exceptions, but
  * could be a mismatched token that the match() routine could not recover from.
  */
 public void recover(IntStream input, RecognitionException re) {
   if (lastErrorIndex == input.index()) {
     // uh oh, another error at same token index; must be a case
     // where LT(1) is in the recovery token set so nothing is
     // consumed; consume a single token so at least to prevent
     // an infinite loop; this is a failsafe.
     input.consume();
   }
   lastErrorIndex = input.index();
   BitSet followSet = computeErrorRecoverySet();
   beginResync();
   consumeUntil(input, followSet);
   endResync();
 }
コード例 #6
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 /**
  * Match current input symbol against ttype. Upon error, do one token insertion or deletion if
  * possible. You can override to not recover here and bail out of the current production to the
  * normal error exception catch (at the end of the method) by just throwing
  * MismatchedTokenException upon input.LA(1)!=ttype.
  */
 public void match(IntStream input, int ttype, BitSet follow) throws RecognitionException {
   if (input.LA(1) == ttype) {
     input.consume();
     errorRecovery = false;
     failed = false;
     return;
   }
   if (backtracking > 0) {
     failed = true;
     return;
   }
   mismatch(input, ttype, follow);
   return;
 }
コード例 #7
0
ファイル: BaseRecognizer.java プロジェクト: jjchaverra/antlr3
 /**
  * Match current input symbol against ttype. Attempt single token insertion or deletion error
  * recovery. If that fails, throw MismatchedTokenException.
  *
  * <p>To turn off single token insertion or deletion error recovery, override
  * recoverFromMismatchedToken() and have it throw an exception. See
  * TreeParser.recoverFromMismatchedToken(). This way any error in a rule will cause an exception
  * and immediate exit from rule. Rule would recover by resynchronizing to the set of symbols that
  * can follow rule ref.
  */
 public Object match(IntStream input, int ttype, BitSet follow) throws RecognitionException {
   // System.out.println("match "+((TokenStream)input).LT(1));
   Object matchedSymbol = getCurrentInputSymbol(input);
   if (input.LA(1) == ttype) {
     input.consume();
     state.errorRecovery = false;
     state.failed = false;
     return matchedSymbol;
   }
   if (state.backtracking > 0) {
     state.failed = true;
     return matchedSymbol;
   }
   matchedSymbol = recoverFromMismatchedToken(input, ttype, follow);
   return matchedSymbol;
 }
コード例 #8
0
ファイル: BaseRecognizer.java プロジェクト: hanhvo/ecl
 public void matchAny(IntStream input) {
   errorRecovery = false;
   failed = false;
   input.consume();
 }