Search in sources :

Example 1 with MismatchedTokenException

use of antlr.MismatchedTokenException in project hibernate-orm by hibernate.

the class HqlParser method handleIdentifierError.

/**
	 * Overrides the base behavior to retry keywords as identifiers.
	 *
	 * @param token The token.
	 * @param ex The recognition exception.
	 *
	 * @return AST - The new AST.
	 *
	 * @throws antlr.RecognitionException if the substitution was not possible.
	 * @throws antlr.TokenStreamException if the substitution was not possible.
	 */
@Override
public AST handleIdentifierError(Token token, RecognitionException ex) throws RecognitionException, TokenStreamException {
    // If the token can tell us if it could be an identifier...
    if (token instanceof HqlToken) {
        HqlToken hqlToken = (HqlToken) token;
        // a mismatched token error ...
        if (hqlToken.isPossibleID() && (ex instanceof MismatchedTokenException)) {
            MismatchedTokenException mte = (MismatchedTokenException) ex;
            // ... and the expected token type was an identifier, then:
            if (mte.expecting == HqlTokenTypes.IDENT) {
                // Use the token as an identifier.
                reportWarning("Keyword  '" + token.getText() + "' is being interpreted as an identifier due to: " + mte.getMessage());
                // Add the token to the AST.
                ASTPair currentAST = new ASTPair();
                token.setType(HqlTokenTypes.WEIRD_IDENT);
                astFactory.addASTChild(currentAST, astFactory.create(token));
                consume();
                return currentAST.root;
            }
        }
    // if
    }
    // Otherwise, handle the error normally.
    return super.handleIdentifierError(token, ex);
}
Also used : ASTPair(antlr.ASTPair) MismatchedTokenException(antlr.MismatchedTokenException)

Example 2 with MismatchedTokenException

use of antlr.MismatchedTokenException in project groovy-core by groovy.

the class SourceUnit method failedWithUnexpectedEOF.

/**
     * Convenience routine, primarily for use by the InteractiveShell,
     * that returns true if parse() failed with an unexpected EOF.
     */
public boolean failedWithUnexpectedEOF() {
    // If you find another way, please add it.
    if (getErrorCollector().hasErrors()) {
        Message last = (Message) getErrorCollector().getLastError();
        Throwable cause = null;
        if (last instanceof SyntaxErrorMessage) {
            cause = ((SyntaxErrorMessage) last).getCause().getCause();
        }
        if (cause != null) {
            if (cause instanceof NoViableAltException) {
                return isEofToken(((NoViableAltException) cause).token);
            } else if (cause instanceof NoViableAltForCharException) {
                char badChar = ((NoViableAltForCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedCharException) {
                char badChar = (char) ((MismatchedCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedTokenException) {
                return isEofToken(((MismatchedTokenException) cause).token);
            }
        }
    }
    return false;
}
Also used : SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) NoViableAltForCharException(antlr.NoViableAltForCharException) NoViableAltException(antlr.NoViableAltException) MismatchedTokenException(antlr.MismatchedTokenException) MismatchedCharException(antlr.MismatchedCharException)

Example 3 with MismatchedTokenException

use of antlr.MismatchedTokenException in project groovy by apache.

the class SourceUnit method failedWithUnexpectedEOF.

/**
     * Convenience routine, primarily for use by the InteractiveShell,
     * that returns true if parse() failed with an unexpected EOF.
     */
public boolean failedWithUnexpectedEOF() {
    // If you find another way, please add it.
    if (getErrorCollector().hasErrors()) {
        Message last = (Message) getErrorCollector().getLastError();
        Throwable cause = null;
        if (last instanceof SyntaxErrorMessage) {
            cause = ((SyntaxErrorMessage) last).getCause().getCause();
        }
        if (cause != null) {
            if (cause instanceof NoViableAltException) {
                return isEofToken(((NoViableAltException) cause).token);
            } else if (cause instanceof NoViableAltForCharException) {
                char badChar = ((NoViableAltForCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedCharException) {
                char badChar = (char) ((MismatchedCharException) cause).foundChar;
                return badChar == CharScanner.EOF_CHAR;
            } else if (cause instanceof MismatchedTokenException) {
                return isEofToken(((MismatchedTokenException) cause).token);
            }
        }
    }
    return false;
}
Also used : SimpleMessage(org.codehaus.groovy.control.messages.SimpleMessage) Message(org.codehaus.groovy.control.messages.Message) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) SyntaxErrorMessage(org.codehaus.groovy.control.messages.SyntaxErrorMessage) NoViableAltForCharException(antlr.NoViableAltForCharException) NoViableAltException(antlr.NoViableAltException) MismatchedTokenException(antlr.MismatchedTokenException) MismatchedCharException(antlr.MismatchedCharException)

Aggregations

MismatchedTokenException (antlr.MismatchedTokenException)3 MismatchedCharException (antlr.MismatchedCharException)2 NoViableAltException (antlr.NoViableAltException)2 NoViableAltForCharException (antlr.NoViableAltForCharException)2 Message (org.codehaus.groovy.control.messages.Message)2 SimpleMessage (org.codehaus.groovy.control.messages.SimpleMessage)2 SyntaxErrorMessage (org.codehaus.groovy.control.messages.SyntaxErrorMessage)2 ASTPair (antlr.ASTPair)1