Search in sources :

Example 6 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project drools by kiegroup.

the class DRL6StrictParser method recoverFromMismatchedToken.

/**
 * Attempt to recover from a single missing or extra token.
 *
 *  EXTRA TOKEN
 *
 *  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.
 *
 *  MISSING TOKEN
 *
 *  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:
 *
 *    stat -> expr -> atom
 *
 *  and it will be trying to match the ')' at this point in the
 *  derivation:
 *
 *       => 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 Token recoverFromMismatchedToken(TokenStream input, int ttype, String text, int[] follow) throws RecognitionException {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if (mismatchIsUnwantedToken(input, ttype, text)) {
        e = new UnwantedTokenException(ttype, input);
        // simply delete extra token
        input.consume();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Token matchedSymbol = input.LT(1);
        // move past ttype token as if all were ok
        input.consume();
        return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if (mismatchIsMissingToken(input, follow)) {
        e = new MissingTokenException(ttype, input, null);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    }
    // even that didn't work; must throw the exception
    if (text != null) {
        e = new DroolsMismatchedTokenException(ttype, text, input);
    } else {
        e = new MismatchedTokenException(ttype, input);
    }
    throw e;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 7 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project drools by kiegroup.

the class DRL6Parser method recoverFromMismatchedToken.

/**
 * Attempt to recover from a single missing or extra token.
 *
 *  EXTRA TOKEN
 *
 *  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.
 *
 *  MISSING TOKEN
 *
 *  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:
 *
 *    stat -> expr -> atom
 *
 *  and it will be trying to match the ')' at this point in the
 *  derivation:
 *
 *       => 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 Token recoverFromMismatchedToken(TokenStream input, int ttype, String text, int[] follow) throws RecognitionException {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if (mismatchIsUnwantedToken(input, ttype, text)) {
        e = new UnwantedTokenException(ttype, input);
        // simply delete extra token
        input.consume();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Token matchedSymbol = input.LT(1);
        // move past ttype token as if all were ok
        input.consume();
        return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if (mismatchIsMissingToken(input, follow)) {
        e = new MissingTokenException(ttype, input, null);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    }
    // even that didn't work; must throw the exception
    if (text != null) {
        e = new DroolsMismatchedTokenException(ttype, text, input);
    } else {
        e = new MismatchedTokenException(ttype, input);
    }
    throw e;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 8 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project xtext-core by eclipse.

the class AbstractInternalAntlrParser method recoverFromMismatchedToken.

@Override
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();
        // simply delete extra token
        input.consume();
        endResync();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Object matchedSymbol = getCurrentInputSymbol(input);
        // move past ttype token as if all were ok
        input.consume();
        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);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    // throw e;
    }
    // even that didn't work; must throw the exception
    e = new MismatchedTokenException(ttype, input);
    throw e;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 9 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project drools by kiegroup.

the class DRL6Parser method recoverFromMismatchedToken.

/**
 * Attempt to recover from a single missing or extra token.
 *
 *  EXTRA TOKEN
 *
 *  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.
 *
 *  MISSING TOKEN
 *
 *  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:
 *
 *    stat -> expr -> atom
 *
 *  and it will be trying to match the ')' at this point in the
 *  derivation:
 *
 *       => 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 Token recoverFromMismatchedToken(TokenStream input, int ttype, String text, int[] follow) throws RecognitionException {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if (mismatchIsUnwantedToken(input, ttype, text)) {
        e = new UnwantedTokenException(ttype, input);
        // simply delete extra token
        input.consume();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Token matchedSymbol = input.LT(1);
        // move past ttype token as if all were ok
        input.consume();
        return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if (mismatchIsMissingToken(input, follow)) {
        e = new MissingTokenException(ttype, input, null);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    }
    // even that didn't work; must throw the exception
    if (text != null) {
        e = new DroolsMismatchedTokenException(ttype, text, input);
    } else {
        e = new MismatchedTokenException(ttype, input);
    }
    throw e;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)9 MissingTokenException (org.antlr.runtime.MissingTokenException)9 RecognitionException (org.antlr.runtime.RecognitionException)9 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)9 CommonToken (org.antlr.runtime.CommonToken)6 Token (org.antlr.runtime.Token)6 EObject (org.eclipse.emf.ecore.EObject)1