Search in sources :

Example 11 with NoViableAltException

use of org.antlr.v4.runtime.NoViableAltException in project antlr4 by antlr.

the class ParserATNSimulator method execATNWithFullContext.

// comes back with reach.uniqueAlt set to a valid alt
protected int execATNWithFullContext(DFA dfa, // how far we got in SLL DFA before failing over
DFAState D, ATNConfigSet s0, TokenStream input, int startIndex, ParserRuleContext outerContext) {
    if (debug || debug_list_atn_decisions) {
        System.out.println("execATNWithFullContext " + s0);
    }
    boolean fullCtx = true;
    boolean foundExactAmbig = false;
    ATNConfigSet reach = null;
    ATNConfigSet previous = s0;
    input.seek(startIndex);
    int t = input.LA(1);
    int predictedAlt;
    while (true) {
        // while more work
        //			System.out.println("LL REACH "+getLookaheadName(input)+
        //							   " from configs.size="+previous.size()+
        //							   " line "+input.LT(1).getLine()+":"+input.LT(1).getCharPositionInLine());
        reach = computeReachSet(previous, t, fullCtx);
        if (reach == null) {
            // if any configs in previous dipped into outer context, that
            // means that input up to t actually finished entry rule
            // at least for LL decision. Full LL doesn't dip into outer
            // so don't need special case.
            // We will get an error no matter what so delay until after
            // decision; better error message. Also, no reachable target
            // ATN states in SLL implies LL will also get nowhere.
            // If conflict in states that dip out, choose min since we
            // will get error no matter what.
            NoViableAltException e = noViableAlt(input, outerContext, previous, startIndex);
            input.seek(startIndex);
            int alt = getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext);
            if (alt != ATN.INVALID_ALT_NUMBER) {
                return alt;
            }
            throw e;
        }
        Collection<BitSet> altSubSets = PredictionMode.getConflictingAltSubsets(reach);
        if (debug) {
            System.out.println("LL altSubSets=" + altSubSets + ", predict=" + PredictionMode.getUniqueAlt(altSubSets) + ", resolvesToJustOneViableAlt=" + PredictionMode.resolvesToJustOneViableAlt(altSubSets));
        }
        //			System.out.println("altSubSets: "+altSubSets);
        //			System.err.println("reach="+reach+", "+reach.conflictingAlts);
        reach.uniqueAlt = getUniqueAlt(reach);
        // unique prediction?
        if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
            predictedAlt = reach.uniqueAlt;
            break;
        }
        if (mode != PredictionMode.LL_EXACT_AMBIG_DETECTION) {
            predictedAlt = PredictionMode.resolvesToJustOneViableAlt(altSubSets);
            if (predictedAlt != ATN.INVALID_ALT_NUMBER) {
                break;
            }
        } else {
            // Just keeps scarfing until we know what the conflict is
            if (PredictionMode.allSubsetsConflict(altSubSets) && PredictionMode.allSubsetsEqual(altSubSets)) {
                foundExactAmbig = true;
                predictedAlt = PredictionMode.getSingleViableAlt(altSubSets);
                break;
            }
        // else there are multiple non-conflicting subsets or
        // we're not sure what the ambiguity is yet.
        // So, keep going.
        }
        previous = reach;
        if (t != IntStream.EOF) {
            input.consume();
            t = input.LA(1);
        }
    }
    // not SLL.
    if (reach.uniqueAlt != ATN.INVALID_ALT_NUMBER) {
        reportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.index());
        return predictedAlt;
    }
    // We do not check predicates here because we have checked them
    // on-the-fly when doing full context prediction.
    /*
		In non-exact ambiguity detection mode, we might	actually be able to
		detect an exact ambiguity, but I'm not going to spend the cycles
		needed to check. We only emit ambiguity warnings in exact ambiguity
		mode.

		For example, we might know that we have conflicting configurations.
		But, that does not mean that there is no way forward without a
		conflict. It's possible to have nonconflicting alt subsets as in:

		   LL altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]

		from

		   [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
			(13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]

		In this case, (17,1,[5 $]) indicates there is some next sequence that
		would resolve this without conflict to alternative 1. Any other viable
		next sequence, however, is associated with a conflict.  We stop
		looking for input because no amount of further lookahead will alter
		the fact that we should predict alternative 1.  We just can't say for
		sure that there is an ambiguity without looking further.
		*/
    reportAmbiguity(dfa, D, startIndex, input.index(), foundExactAmbig, reach.getAlts(), reach);
    return predictedAlt;
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) BitSet(java.util.BitSet)

Example 12 with NoViableAltException

use of org.antlr.v4.runtime.NoViableAltException in project oxTrust by GluuFederation.

the class ScimFilterParser method expression.

private ExpressionContext expression(int _p) throws RecognitionException {
    ParserRuleContext _parentctx = _ctx;
    int _parentState = getState();
    ExpressionContext _localctx = new ExpressionContext(_ctx, _parentState);
    ExpressionContext _prevctx = _localctx;
    int _startState = 2;
    enterRecursionRule(_localctx, 2, RULE_expression, _p);
    int _la;
    try {
        int _alt;
        enterOuterAlt(_localctx, 1);
        {
            setState(92);
            _errHandler.sync(this);
            switch(getInterpreter().adaptivePredict(_input, 11, _ctx)) {
                case 1:
                    {
                        _localctx = new NOT_EXPRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(17);
                        match(NOT);
                        setState(19);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(18);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(21);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 1, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(23);
                        expression(9);
                    }
                    break;
                case 2:
                    {
                        _localctx = new ATTR_OPER_EXPRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(24);
                        match(ATTRNAME);
                        setState(26);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(25);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(28);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 2, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(30);
                        operator();
                        setState(32);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(31);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(34);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 3, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(36);
                        expression(4);
                    }
                    break;
                case 3:
                    {
                        _localctx = new ATTR_PRContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(38);
                        match(ATTRNAME);
                        setState(40);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(39);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(42);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 4, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(44);
                        match(PR);
                    }
                    break;
                case 4:
                    {
                        _localctx = new ATTR_OPER_CRITERIAContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(45);
                        match(ATTRNAME);
                        setState(47);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(46);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(49);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 5, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(51);
                        operator();
                        setState(53);
                        _errHandler.sync(this);
                        _alt = 1 + 1;
                        do {
                            switch(_alt) {
                                case 1 + 1:
                                    {
                                        {
                                            setState(52);
                                            match(WS);
                                        }
                                    }
                                    break;
                                default:
                                    throw new NoViableAltException(this);
                            }
                            setState(55);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 6, _ctx);
                        } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                        setState(57);
                        criteria();
                    }
                    break;
                case 5:
                    {
                        _localctx = new LPAREN_EXPR_RPARENContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(59);
                        match(LPAREN);
                        setState(63);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(60);
                                        match(WS);
                                    }
                                }
                            }
                            setState(65);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 7, _ctx);
                        }
                        setState(66);
                        expression(0);
                        setState(70);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(67);
                                        match(WS);
                                    }
                                }
                            }
                            setState(72);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 8, _ctx);
                        }
                        setState(73);
                        match(RPAREN);
                    }
                    break;
                case 6:
                    {
                        _localctx = new LBRAC_EXPR_RBRACContext(_localctx);
                        _ctx = _localctx;
                        _prevctx = _localctx;
                        setState(75);
                        match(ATTRNAME);
                        setState(76);
                        match(LBRAC);
                        setState(80);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(77);
                                        match(WS);
                                    }
                                }
                            }
                            setState(82);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 9, _ctx);
                        }
                        setState(83);
                        expression(0);
                        setState(87);
                        _errHandler.sync(this);
                        _alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
                        while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                            if (_alt == 1 + 1) {
                                {
                                    {
                                        setState(84);
                                        match(WS);
                                    }
                                }
                            }
                            setState(89);
                            _errHandler.sync(this);
                            _alt = getInterpreter().adaptivePredict(_input, 10, _ctx);
                        }
                        setState(90);
                        match(RBRAC);
                    }
                    break;
            }
            _ctx.stop = _input.LT(-1);
            setState(136);
            _errHandler.sync(this);
            _alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
            while (_alt != 2 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER) {
                if (_alt == 1) {
                    if (_parseListeners != null)
                        triggerExitRuleEvent();
                    _prevctx = _localctx;
                    {
                        setState(134);
                        _errHandler.sync(this);
                        switch(getInterpreter().adaptivePredict(_input, 18, _ctx)) {
                            case 1:
                                {
                                    _localctx = new EXPR_AND_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(94);
                                    if (!(precpred(_ctx, 8)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 8)");
                                    setState(96);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(95);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(98);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 12, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(100);
                                    match(AND);
                                    setState(102);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(101);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(104);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 13, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(106);
                                    expression(9);
                                }
                                break;
                            case 2:
                                {
                                    _localctx = new EXPR_OR_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(107);
                                    if (!(precpred(_ctx, 7)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 7)");
                                    setState(109);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(108);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(111);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 14, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(113);
                                    match(OR);
                                    setState(115);
                                    _errHandler.sync(this);
                                    _la = _input.LA(1);
                                    do {
                                        {
                                            {
                                                setState(114);
                                                match(WS);
                                            }
                                        }
                                        setState(117);
                                        _errHandler.sync(this);
                                        _la = _input.LA(1);
                                    } while (_la == WS);
                                    setState(119);
                                    expression(8);
                                }
                                break;
                            case 3:
                                {
                                    _localctx = new EXPR_OPER_EXPRContext(new ExpressionContext(_parentctx, _parentState));
                                    pushNewRecursionContext(_localctx, _startState, RULE_expression);
                                    setState(120);
                                    if (!(precpred(_ctx, 6)))
                                        throw new FailedPredicateException(this, "precpred(_ctx, 6)");
                                    setState(122);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(121);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(124);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 16, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(126);
                                    operator();
                                    setState(128);
                                    _errHandler.sync(this);
                                    _alt = 1 + 1;
                                    do {
                                        switch(_alt) {
                                            case 1 + 1:
                                                {
                                                    {
                                                        setState(127);
                                                        match(WS);
                                                    }
                                                }
                                                break;
                                            default:
                                                throw new NoViableAltException(this);
                                        }
                                        setState(130);
                                        _errHandler.sync(this);
                                        _alt = getInterpreter().adaptivePredict(_input, 17, _ctx);
                                    } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
                                    setState(132);
                                    expression(7);
                                }
                                break;
                        }
                    }
                }
                setState(138);
                _errHandler.sync(this);
                _alt = getInterpreter().adaptivePredict(_input, 19, _ctx);
            }
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        unrollRecursionContexts(_parentctx);
    }
    return _localctx;
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 13 with NoViableAltException

use of org.antlr.v4.runtime.NoViableAltException in project lucene-solr by apache.

the class JavascriptParserErrorStrategy method recover.

/**
   * Ensures the ANTLR parser will throw an exception after the first error
   *
   * @param recognizer the parser being used
   * @param re the original exception from the parser
   */
@Override
public void recover(Parser recognizer, RecognitionException re) {
    Token token = re.getOffendingToken();
    String message;
    if (token == null) {
        message = "error " + getTokenErrorDisplay(token);
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")" + " was expecting one of " + re.getExpectedTokens().toString(recognizer.getVocabulary());
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == JavascriptParser.EOF) {
            message = "unexpected end of expression";
        } else {
            message = "invalid sequence of tokens near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
        }
    } else {
        message = " unexpected token near " + getTokenErrorDisplay(token) + " on line (" + token.getLine() + ") position (" + token.getCharPositionInLine() + ")";
    }
    ParseException parseException = new ParseException(message, token.getStartIndex());
    parseException.initCause(re);
    throw new RuntimeException(parseException);
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Token(org.antlr.v4.runtime.Token) ParseException(java.text.ParseException) InputMismatchException(org.antlr.v4.runtime.InputMismatchException)

Aggregations

NoViableAltException (org.antlr.v4.runtime.NoViableAltException)12 Test (org.junit.Test)5 Grammar (org.antlr.v4.tool.Grammar)4 LexerGrammar (org.antlr.v4.tool.LexerGrammar)4 BitSet (java.util.BitSet)2 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)2 RecognitionException (org.antlr.v4.runtime.RecognitionException)2 Token (org.antlr.v4.runtime.Token)2 ClawLexer (cx2x.translator.language.parser.ClawLexer)1 ClawParser (cx2x.translator.language.parser.ClawParser)1 IllegalDirectiveException (cx2x.xcodeml.exception.IllegalDirectiveException)1 ParseException (java.text.ParseException)1 FailedPredicateException (org.antlr.v4.runtime.FailedPredicateException)1 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)1 TokenStream (org.antlr.v4.runtime.TokenStream)1 ATN (org.antlr.v4.runtime.atn.ATN)1 LexerATNSimulator (org.antlr.v4.runtime.atn.LexerATNSimulator)1 PredictionContextCache (org.antlr.v4.runtime.atn.PredictionContextCache)1 DFA (org.antlr.v4.runtime.dfa.DFA)1 DFAState (org.antlr.v4.runtime.dfa.DFAState)1