Search in sources :

Example 1 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project phoenix by apache.

the class PhoenixParserException method getErrorMessage.

public static String getErrorMessage(Throwable e, String[] tokenNames) {
    String msg;
    if (e instanceof MissingTokenException) {
        MissingTokenException mte = (MissingTokenException) e;
        String tokenName;
        if (mte.expecting == Token.EOF) {
            tokenName = "EOF";
        } else {
            tokenName = tokenNames[mte.expecting];
        }
        msg = "Missing \"" + tokenName + "\" at " + getTokenLocation(mte);
    } else if (e instanceof UnwantedTokenException) {
        UnwantedTokenException ute = (UnwantedTokenException) e;
        String tokenName;
        if (ute.expecting == Token.EOF) {
            tokenName = "EOF";
        } else {
            tokenName = tokenNames[ute.expecting];
        }
        msg = "Unexpected input. Expecting \"" + tokenName + "\", got \"" + ute.getUnexpectedToken().getText() + "\" at " + getTokenLocation(ute);
    } else if (e instanceof MismatchedTokenException) {
        MismatchedTokenException mte = (MismatchedTokenException) e;
        String tokenName;
        if (mte.expecting == Token.EOF) {
            tokenName = "EOF";
        } else {
            tokenName = tokenNames[mte.expecting];
        }
        msg = "Mismatched input. Expecting \"" + tokenName + "\", got \"" + mte.token.getText() + "\" at " + getTokenLocation(mte);
    } else if (e instanceof RecognitionException) {
        RecognitionException re = (RecognitionException) e;
        msg = "Encountered \"" + re.token.getText() + "\" at " + getTokenLocation(re);
    } else if (e instanceof UnknownFunctionException) {
        UnknownFunctionException ufe = (UnknownFunctionException) e;
        msg = "Unknown function: \"" + ufe.getFuncName() + "\".";
    } else {
        msg = e.getMessage();
    }
    return msg;
}
Also used : MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 2 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 3 with MissingTokenException

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

the class DRL5Parser 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 4 with MissingTokenException

use of org.antlr.runtime.MissingTokenException in project n4js by eclipse.

the class AbstractInternalHighlightingAntlrParser method recoverFromMismatchedToken.

/**
 * Same recovery logic as for the production parser.
 */
@Override
protected Object recoverFromMismatchedToken(@SuppressWarnings("hiding") 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 : MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Example 5 with MissingTokenException

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

the class DRL5Parser 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