Search in sources :

Example 71 with RecognitionException

use of org.antlr.runtime.RecognitionException in project freud by LMAX-Exchange.

the class JavaParser method arguments.

// $ANTLR start "arguments"
// /home/langera/dev/freud/trunk/src/grammar/Java.g:1151:1: arguments : LPAREN ( expressionList )? RPAREN -> ^( ARGUMENT_LIST[$LPAREN, \"ARGUMENT_LIST\"] ( expressionList )? ) ;
public final JavaParser.arguments_return arguments() throws RecognitionException {
    JavaParser.arguments_return retval = new JavaParser.arguments_return();
    retval.start = input.LT(1);
    int arguments_StartIndex = input.index();
    CommonTree root_0 = null;
    Token LPAREN531 = null;
    Token RPAREN533 = null;
    JavaParser.expressionList_return expressionList532 = null;
    CommonTree LPAREN531_tree = null;
    CommonTree RPAREN533_tree = null;
    RewriteRuleTokenStream stream_RPAREN = new RewriteRuleTokenStream(adaptor, "token RPAREN");
    RewriteRuleTokenStream stream_LPAREN = new RewriteRuleTokenStream(adaptor, "token LPAREN");
    RewriteRuleSubtreeStream stream_expressionList = new RewriteRuleSubtreeStream(adaptor, "rule expressionList");
    try {
        if (state.backtracking > 0 && alreadyParsedRule(input, 110)) {
            return retval;
        }
        // /home/langera/dev/freud/trunk/src/grammar/Java.g:1152:5: ( LPAREN ( expressionList )? RPAREN -> ^( ARGUMENT_LIST[$LPAREN, \"ARGUMENT_LIST\"] ( expressionList )? ) )
        // /home/langera/dev/freud/trunk/src/grammar/Java.g:1152:9: LPAREN ( expressionList )? RPAREN
        {
            LPAREN531 = (Token) match(input, LPAREN, FOLLOW_LPAREN_in_arguments14389);
            if (state.failed) {
                return retval;
            }
            if (state.backtracking == 0) {
                stream_LPAREN.add(LPAREN531);
            }
            // /home/langera/dev/freud/trunk/src/grammar/Java.g:1152:16: ( expressionList )?
            int alt154 = 2;
            int LA154_0 = input.LA(1);
            if ((LA154_0 == DEC || LA154_0 == INC || LA154_0 == LESS_THAN || LA154_0 == LOGICAL_NOT || (LA154_0 >= LPAREN && LA154_0 <= MINUS) || LA154_0 == NOT || LA154_0 == PLUS || LA154_0 == BOOLEAN || LA154_0 == BYTE || LA154_0 == CHAR || LA154_0 == DOUBLE || LA154_0 == FALSE || LA154_0 == FLOAT || (LA154_0 >= INT && LA154_0 <= LONG) || (LA154_0 >= NEW && LA154_0 <= NULL) || LA154_0 == SHORT || LA154_0 == SUPER || LA154_0 == THIS || LA154_0 == TRUE || LA154_0 == VOID || (LA154_0 >= IDENT && LA154_0 <= STRING_LITERAL))) {
                alt154 = 1;
            }
            switch(alt154) {
                case 1:
                    // /home/langera/dev/freud/trunk/src/grammar/Java.g:0:0: expressionList
                    {
                        pushFollow(FOLLOW_expressionList_in_arguments14391);
                        expressionList532 = expressionList();
                        state._fsp--;
                        if (state.failed) {
                            return retval;
                        }
                        if (state.backtracking == 0) {
                            stream_expressionList.add(expressionList532.getTree());
                        }
                    }
                    break;
            }
            RPAREN533 = (Token) match(input, RPAREN, FOLLOW_RPAREN_in_arguments14394);
            if (state.failed) {
                return retval;
            }
            if (state.backtracking == 0) {
                stream_RPAREN.add(RPAREN533);
            }
            // wildcard labels:
            if (state.backtracking == 0) {
                retval.tree = root_0;
                RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream(adaptor, "rule retval", retval != null ? retval.tree : null);
                root_0 = (CommonTree) adaptor.nil();
                // 1153:9: -> ^( ARGUMENT_LIST[$LPAREN, \"ARGUMENT_LIST\"] ( expressionList )? )
                {
                    // /home/langera/dev/freud/trunk/src/grammar/Java.g:1153:13: ^( ARGUMENT_LIST[$LPAREN, \"ARGUMENT_LIST\"] ( expressionList )? )
                    {
                        CommonTree root_1 = (CommonTree) adaptor.nil();
                        root_1 = (CommonTree) adaptor.becomeRoot((CommonTree) adaptor.create(ARGUMENT_LIST, LPAREN531, "ARGUMENT_LIST"), root_1);
                        // /home/langera/dev/freud/trunk/src/grammar/Java.g:1153:55: ( expressionList )?
                        if (stream_expressionList.hasNext()) {
                            adaptor.addChild(root_1, stream_expressionList.nextTree());
                        }
                        stream_expressionList.reset();
                        adaptor.addChild(root_0, root_1);
                    }
                }
                retval.tree = root_0;
            }
        }
        retval.stop = input.LT(-1);
        if (state.backtracking == 0) {
            retval.tree = (CommonTree) adaptor.rulePostProcessing(root_0);
            adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
        retval.tree = (CommonTree) adaptor.errorNode(input, retval.start, input.LT(-1), re);
    } finally {
        if (state.backtracking > 0) {
            memoize(input, 110, arguments_StartIndex);
        }
    }
    return retval;
}
Also used : RewriteRuleTokenStream(org.antlr.runtime.tree.RewriteRuleTokenStream) CommonTree(org.antlr.runtime.tree.CommonTree) RewriteRuleSubtreeStream(org.antlr.runtime.tree.RewriteRuleSubtreeStream) Token(org.antlr.runtime.Token) RecognitionException(org.antlr.runtime.RecognitionException)

Example 72 with RecognitionException

use of org.antlr.runtime.RecognitionException in project freud by LMAX-Exchange.

the class JavaParser method switchBlockLabels.

// $ANTLR start "switchBlockLabels"
// /home/langera/dev/freud/trunk/src/grammar/Java.g:885:1: switchBlockLabels : preCases= switchCaseLabels ( switchDefaultLabel )? postCases= switchCaseLabels -> ^( SWITCH_BLOCK_LABEL_LIST ( $preCases)? ( switchDefaultLabel )? ( $postCases)? ) ;
public final JavaParser.switchBlockLabels_return switchBlockLabels() throws RecognitionException {
    JavaParser.switchBlockLabels_return retval = new JavaParser.switchBlockLabels_return();
    retval.start = input.LT(1);
    int switchBlockLabels_StartIndex = input.index();
    CommonTree root_0 = null;
    JavaParser.switchCaseLabels_return preCases = null;
    JavaParser.switchCaseLabels_return postCases = null;
    JavaParser.switchDefaultLabel_return switchDefaultLabel346 = null;
    RewriteRuleSubtreeStream stream_switchDefaultLabel = new RewriteRuleSubtreeStream(adaptor, "rule switchDefaultLabel");
    RewriteRuleSubtreeStream stream_switchCaseLabels = new RewriteRuleSubtreeStream(adaptor, "rule switchCaseLabels");
    try {
        if (state.backtracking > 0 && alreadyParsedRule(input, 79)) {
            return retval;
        }
        // /home/langera/dev/freud/trunk/src/grammar/Java.g:886:5: (preCases= switchCaseLabels ( switchDefaultLabel )? postCases= switchCaseLabels -> ^( SWITCH_BLOCK_LABEL_LIST ( $preCases)? ( switchDefaultLabel )? ( $postCases)? ) )
        // /home/langera/dev/freud/trunk/src/grammar/Java.g:886:9: preCases= switchCaseLabels ( switchDefaultLabel )? postCases= switchCaseLabels
        {
            pushFollow(FOLLOW_switchCaseLabels_in_switchBlockLabels9838);
            preCases = switchCaseLabels();
            state._fsp--;
            if (state.failed) {
                return retval;
            }
            if (state.backtracking == 0) {
                stream_switchCaseLabels.add(preCases.getTree());
            }
            // /home/langera/dev/freud/trunk/src/grammar/Java.g:886:35: ( switchDefaultLabel )?
            int alt100 = 2;
            int LA100_0 = input.LA(1);
            if ((LA100_0 == DEFAULT)) {
                alt100 = 1;
            }
            switch(alt100) {
                case 1:
                    // /home/langera/dev/freud/trunk/src/grammar/Java.g:0:0: switchDefaultLabel
                    {
                        pushFollow(FOLLOW_switchDefaultLabel_in_switchBlockLabels9840);
                        switchDefaultLabel346 = switchDefaultLabel();
                        state._fsp--;
                        if (state.failed) {
                            return retval;
                        }
                        if (state.backtracking == 0) {
                            stream_switchDefaultLabel.add(switchDefaultLabel346.getTree());
                        }
                    }
                    break;
            }
            pushFollow(FOLLOW_switchCaseLabels_in_switchBlockLabels9845);
            postCases = switchCaseLabels();
            state._fsp--;
            if (state.failed) {
                return retval;
            }
            if (state.backtracking == 0) {
                stream_switchCaseLabels.add(postCases.getTree());
            }
            // wildcard labels:
            if (state.backtracking == 0) {
                retval.tree = root_0;
                RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream(adaptor, "rule retval", retval != null ? retval.tree : null);
                RewriteRuleSubtreeStream stream_preCases = new RewriteRuleSubtreeStream(adaptor, "rule preCases", preCases != null ? preCases.tree : null);
                RewriteRuleSubtreeStream stream_postCases = new RewriteRuleSubtreeStream(adaptor, "rule postCases", postCases != null ? postCases.tree : null);
                root_0 = (CommonTree) adaptor.nil();
                // 887:9: -> ^( SWITCH_BLOCK_LABEL_LIST ( $preCases)? ( switchDefaultLabel )? ( $postCases)? )
                {
                    // /home/langera/dev/freud/trunk/src/grammar/Java.g:887:13: ^( SWITCH_BLOCK_LABEL_LIST ( $preCases)? ( switchDefaultLabel )? ( $postCases)? )
                    {
                        CommonTree root_1 = (CommonTree) adaptor.nil();
                        root_1 = (CommonTree) adaptor.becomeRoot((CommonTree) adaptor.create(SWITCH_BLOCK_LABEL_LIST, "SWITCH_BLOCK_LABEL_LIST"), root_1);
                        // /home/langera/dev/freud/trunk/src/grammar/Java.g:887:39: ( $preCases)?
                        if (stream_preCases.hasNext()) {
                            adaptor.addChild(root_1, stream_preCases.nextTree());
                        }
                        stream_preCases.reset();
                        // /home/langera/dev/freud/trunk/src/grammar/Java.g:887:50: ( switchDefaultLabel )?
                        if (stream_switchDefaultLabel.hasNext()) {
                            adaptor.addChild(root_1, stream_switchDefaultLabel.nextTree());
                        }
                        stream_switchDefaultLabel.reset();
                        // /home/langera/dev/freud/trunk/src/grammar/Java.g:887:70: ( $postCases)?
                        if (stream_postCases.hasNext()) {
                            adaptor.addChild(root_1, stream_postCases.nextTree());
                        }
                        stream_postCases.reset();
                        adaptor.addChild(root_0, root_1);
                    }
                }
                retval.tree = root_0;
            }
        }
        retval.stop = input.LT(-1);
        if (state.backtracking == 0) {
            retval.tree = (CommonTree) adaptor.rulePostProcessing(root_0);
            adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
        retval.tree = (CommonTree) adaptor.errorNode(input, retval.start, input.LT(-1), re);
    } finally {
        if (state.backtracking > 0) {
            memoize(input, 79, switchBlockLabels_StartIndex);
        }
    }
    return retval;
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) RewriteRuleSubtreeStream(org.antlr.runtime.tree.RewriteRuleSubtreeStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 73 with RecognitionException

use of org.antlr.runtime.RecognitionException in project smali by JesusFreke.

the class LexerTest method runTest.

public void runTest(String test, boolean discardHiddenTokens) {
    String smaliFile = String.format("LexerTest%s%s.smali", File.separatorChar, test);
    String tokensFile = String.format("LexerTest%s%s.tokens", File.separatorChar, test);
    org.jf.smali.expectedTokensTestGrammarLexer expectedTokensLexer = null;
    try {
        expectedTokensLexer = new org.jf.smali.expectedTokensTestGrammarLexer(new ANTLRInputStream(LexerTest.class.getClassLoader().getResourceAsStream(tokensFile)));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
    CommonTokenStream expectedTokensStream = new CommonTokenStream(expectedTokensLexer);
    org.jf.smali.expectedTokensTestGrammarParser expectedTokensParser = new org.jf.smali.expectedTokensTestGrammarParser(expectedTokensStream);
    try {
        expectedTokensParser.top();
    } catch (RecognitionException ex) {
        throw new RuntimeException(ex);
    }
    List<ExpectedToken> expectedTokens = expectedTokensParser.getExpectedTokens();
    InputStream smaliStream = LexerTest.class.getClassLoader().getResourceAsStream(smaliFile);
    if (smaliStream == null) {
        Assert.fail("Could not load " + smaliFile);
    }
    smaliFlexLexer lexer = new smaliFlexLexer(new InputStreamReader(smaliStream));
    lexer.setSourceFile(new File(test + ".smali"));
    lexer.setSuppressErrors(true);
    CommonTokenStream tokenStream = new CommonTokenStream(lexer);
    tokenStream.fill();
    List tokens = tokenStream.getTokens();
    int expectedTokenIndex = 0;
    CommonToken token;
    for (int i = 0; i < tokens.size() - 1; i++) {
        token = (CommonToken) tokens.get(i);
        if (discardHiddenTokens && token.getChannel() == smaliParser.HIDDEN) {
            continue;
        }
        if (expectedTokenIndex >= expectedTokens.size()) {
            Assert.fail("Too many tokens");
        }
        if (token.getType() == smaliParser.INVALID_TOKEN) {
            Assert.assertTrue("Encountered an INVALID_TOKEN not on the error channel", token.getChannel() == smaliParser.ERROR_CHANNEL);
        }
        ExpectedToken expectedToken = expectedTokens.get(expectedTokenIndex++);
        if (!tokenTypesByName.containsKey(expectedToken.tokenName)) {
            Assert.fail("Unknown token: " + expectedToken.tokenName);
        }
        int expectedTokenType = tokenTypesByName.get(expectedToken.tokenName);
        if (token.getType() != expectedTokenType) {
            Assert.fail(String.format("Invalid token at index %d. Expecting %s, got %s(%s)", expectedTokenIndex - 1, expectedToken.tokenName, getTokenName(token.getType()), token.getText()));
        }
        if (expectedToken.tokenText != null) {
            if (!expectedToken.tokenText.equals(token.getText())) {
                Assert.fail(String.format("Invalid token text at index %d. Expecting text \"%s\", got \"%s\"", expectedTokenIndex - 1, expectedToken.tokenText, token.getText()));
            }
        }
    }
    if (expectedTokenIndex < expectedTokens.size()) {
        Assert.fail(String.format("Not enough tokens. Expecting %d tokens, but got %d", expectedTokens.size(), expectedTokenIndex));
    }
}
Also used : CommonTokenStream(org.antlr.runtime.CommonTokenStream) InputStreamReader(java.io.InputStreamReader) ANTLRInputStream(org.antlr.runtime.ANTLRInputStream) InputStream(java.io.InputStream) ExpectedToken(org.jf.smali.expectedTokensTestGrammarParser.ExpectedToken) IOException(java.io.IOException) List(java.util.List) CommonToken(org.antlr.runtime.CommonToken) File(java.io.File) ANTLRInputStream(org.antlr.runtime.ANTLRInputStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 74 with RecognitionException

use of org.antlr.runtime.RecognitionException 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 75 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drill by apache.

the class TestBuilder method parsePath.

// modified code from SchemaPath.De class. This should be used sparingly and only in tests if absolutely needed.
public static SchemaPath parsePath(String path) {
    try {
        ExprLexer lexer = new ExprLexer(new ANTLRStringStream(path));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExprParser parser = new ExprParser(tokens);
        ExprParser.parse_return ret = parser.parse();
        if (ret.e instanceof SchemaPath) {
            return (SchemaPath) ret.e;
        } else {
            throw new IllegalStateException("Schema path is not a valid format.");
        }
    } catch (RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) SchemaPath(org.apache.drill.common.expression.SchemaPath) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

RecognitionException (org.antlr.runtime.RecognitionException)257 Token (org.antlr.runtime.Token)169 CommonTree (org.antlr.runtime.tree.CommonTree)132 RewriteRuleSubtreeStream (org.antlr.runtime.tree.RewriteRuleSubtreeStream)98 RewriteRuleTokenStream (org.antlr.runtime.tree.RewriteRuleTokenStream)74 CommonToken (org.antlr.runtime.CommonToken)48 NoViableAltException (org.antlr.runtime.NoViableAltException)43 RewriteEarlyExitException (org.antlr.runtime.tree.RewriteEarlyExitException)21 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)14 CommonTokenStream (org.antlr.runtime.CommonTokenStream)11 EarlyExitException (org.antlr.runtime.EarlyExitException)10 MismatchedSetException (org.antlr.runtime.MismatchedSetException)10 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)8 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)7 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)7 FailedPredicateException (org.antlr.runtime.FailedPredicateException)6 MissingTokenException (org.antlr.runtime.MissingTokenException)6 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)6 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)5 HashMap (java.util.HashMap)4