Search in sources :

Example 1 with MismatchedTokenException

use of org.antlr.runtime.MismatchedTokenException 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 MismatchedTokenException

use of org.antlr.runtime.MismatchedTokenException in project binnavi by google.

the class DebuggerMemoryExpressionParser method parse.

/**
   * Parses a single memory expression string.
   *
   * @param memoryExpression The memory expression string to parse.
   *
   * @return The parsed memory expression tree.
   *
   * @throws RecognitionException Thrown if parsing failed.
   */
public static MemoryExpressionElement parse(final String memoryExpression) throws RecognitionException {
    final CharStream charStream = new ANTLRStringStream(memoryExpression);
    final MemoryExpressionLexer lexer = new MemoryExpressionLexer(charStream);
    final CommonTokenStream tokens = new CommonTokenStream();
    tokens.setTokenSource(lexer);
    final MemoryExpressionParser parser = new MemoryExpressionParser(tokens) {

        @Override
        public void recover(final IntStream input, final RecognitionException exception) {
        // Nothing to do
        }

        @Override
        public Object recoverFromMismatchedToken(final IntStream input, final int ttype, final org.antlr.runtime.BitSet follow) throws RecognitionException {
            throw new MismatchedTokenException(ttype, input);
        }

        @Override
        public void reportError(final RecognitionException exception) {
        // Nothing to do
        }
    };
    parser.setTreeAdaptor(adaptor);
    final MemoryExpressionParser.prog_return parserResult = parser.prog();
    final CommonTree ast = (CommonTree) parserResult.getTree();
    return convert(ast);
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MemoryExpressionParser(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MemoryExpressionLexer(com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer) CommonTree(org.antlr.runtime.tree.CommonTree) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) IntStream(org.antlr.runtime.IntStream) CharStream(org.antlr.runtime.CharStream) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)2 RecognitionException (org.antlr.runtime.RecognitionException)2 MemoryExpressionLexer (com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionLexer)1 MemoryExpressionParser (com.google.security.zynamics.binnavi.parsers.gotomem.MemoryExpressionParser)1 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)1 CharStream (org.antlr.runtime.CharStream)1 CommonTokenStream (org.antlr.runtime.CommonTokenStream)1 IntStream (org.antlr.runtime.IntStream)1 MissingTokenException (org.antlr.runtime.MissingTokenException)1 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)1 CommonTree (org.antlr.runtime.tree.CommonTree)1