Search in sources :

Example 6 with NoViableAltException

use of org.antlr.v4.runtime.NoViableAltException in project claw-compiler by C2SM-RCM.

the class ClawLanguage method analyze.

/**
   * Analyze a raw string input and match it with the CLAW language definition.
   *
   * @param rawPragma A raw pragma statement to be analyzed against the CLAW
   *                  language.
   * @param lineno    Line number of the pragma statement.
   * @param generator Accelerator directive generator.
   * @param target    Target that influences the code transformation.
   * @return A ClawLanguage object with the corresponding extracted information.
   * @throws IllegalDirectiveException If directive does not follow the CLAW
   *                                   language specification.
   */
private static ClawLanguage analyze(String rawPragma, int lineno, AcceleratorGenerator generator, Target target) throws IllegalDirectiveException {
    // Remove additional claw keyword
    rawPragma = nakenize(rawPragma);
    // Discard the ignored code after the claw ignore directive
    if (rawPragma.toLowerCase().contains(IGNORE)) {
        rawPragma = rawPragma.substring(0, rawPragma.toLowerCase().indexOf(IGNORE) + IGNORE.length());
    }
    // Instantiate the lexer with the raw string input
    ClawLexer lexer = new ClawLexer(CharStreams.fromString(rawPragma));
    // Get a list of matched tokens
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    // Pass the tokens to the parser
    ClawParser parser = new ClawParser(tokens);
    parser.setErrorHandler(new BailErrorStrategy());
    parser.removeErrorListeners();
    try {
        // Start the parser analysis from the "analyze" entry point
        ClawParser.AnalyzeContext ctx = parser.analyze();
        // Get the ClawLanguage object return by the parser after analysis.
        ctx.l.setAcceleratorGenerator(generator);
        ctx.l.setTarget(target);
        return ctx.l;
    } catch (ParseCancellationException pcex) {
        if (pcex.getCause() instanceof InputMismatchException) {
            InputMismatchException imex = (InputMismatchException) pcex.getCause();
            throw new IllegalDirectiveException(getTokens(imex.getExpectedTokens(), parser), lineno, imex.getOffendingToken().getCharPositionInLine());
        } else if (pcex.getCause() instanceof NoViableAltException) {
            NoViableAltException nvex = (NoViableAltException) pcex.getCause();
            throw new IllegalDirectiveException(nvex.getOffendingToken(), getTokens(nvex.getExpectedTokens(), parser), lineno, nvex.getOffendingToken().getCharPositionInLine());
        }
        throw new IllegalDirectiveException(rawPragma, "Unsupported construct", lineno, 0);
    }
}
Also used : ClawParser(cx2x.translator.language.parser.ClawParser) IllegalDirectiveException(cx2x.xcodeml.exception.IllegalDirectiveException) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) ClawLexer(cx2x.translator.language.parser.ClawLexer)

Example 7 with NoViableAltException

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

the class ScimFilterParser method criteria.

public final CriteriaContext criteria() throws RecognitionException {
    CriteriaContext _localctx = new CriteriaContext(_ctx, getState());
    enterRule(_localctx, 4, RULE_criteria);
    try {
        int _alt;
        enterOuterAlt(_localctx, 1);
        {
            setState(139);
            match(T__0);
            setState(141);
            _errHandler.sync(this);
            _alt = 1 + 1;
            do {
                switch(_alt) {
                    case 1 + 1:
                        {
                            {
                                setState(140);
                                matchWildcard();
                            }
                        }
                        break;
                    default:
                        throw new NoViableAltException(this);
                }
                setState(143);
                _errHandler.sync(this);
                _alt = getInterpreter().adaptivePredict(_input, 20, _ctx);
            } while (_alt != 1 && _alt != org.antlr.v4.runtime.atn.ATN.INVALID_ALT_NUMBER);
            setState(145);
            match(T__0);
        }
    } catch (RecognitionException re) {
        _localctx.exception = re;
        _errHandler.reportError(this, re);
        _errHandler.recover(this, re);
    } finally {
        exitRule();
    }
    return _localctx;
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 8 with NoViableAltException

use of org.antlr.v4.runtime.NoViableAltException in project elasticsearch by elastic.

the class ParserErrorStrategy method recover.

@Override
public void recover(final Parser recognizer, final RecognitionException re) {
    final Token token = re.getOffendingToken();
    String message;
    if (token == null) {
        message = "no parse token found.";
    } else if (re instanceof InputMismatchException) {
        message = "unexpected token [" + getTokenErrorDisplay(token) + "]" + " was expecting one of [" + re.getExpectedTokens().toString(recognizer.getVocabulary()) + "].";
    } else if (re instanceof NoViableAltException) {
        if (token.getType() == PainlessParser.EOF) {
            message = "unexpected end of script.";
        } else {
            message = "invalid sequence of tokens near [" + getTokenErrorDisplay(token) + "].";
        }
    } else {
        message = "unexpected token near [" + getTokenErrorDisplay(token) + "].";
    }
    Location location = new Location(sourceName, token == null ? -1 : token.getStartIndex());
    throw location.createError(new IllegalArgumentException(message, re));
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Token(org.antlr.v4.runtime.Token) InputMismatchException(org.antlr.v4.runtime.InputMismatchException) Location(org.elasticsearch.painless.Location)

Example 9 with NoViableAltException

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

the class TestATNParserPrediction method checkDFAConstruction.

public void checkDFAConstruction(LexerGrammar lg, Grammar g, int decision, String[] inputString, String[] dfaString) {
    //		Tool.internalOption_ShowATNConfigsInDFA = true;
    ATN lexatn = createATN(lg, true);
    LexerATNSimulator lexInterp = new LexerATNSimulator(lexatn, new DFA[] { new DFA(lexatn.getDecisionState(Lexer.DEFAULT_MODE)) }, new PredictionContextCache());
    semanticProcess(lg);
    g.importVocab(lg);
    semanticProcess(g);
    ParserInterpreterForTesting interp = new ParserInterpreterForTesting(g, null);
    for (int i = 0; i < inputString.length; i++) {
        // Check DFA
        IntegerList types = getTokenTypesViaATN(inputString[i], lexInterp);
        //			System.out.println(types);
        TokenStream input = new IntTokenStream(types);
        try {
            interp.adaptivePredict(input, decision, ParserRuleContext.EMPTY);
        } catch (NoViableAltException nvae) {
            nvae.printStackTrace(System.err);
        }
        DFA dfa = interp.parser.decisionToDFA[decision];
        assertEquals(dfaString[i], dfa.toString(g.getVocabulary()));
    }
}
Also used : TokenStream(org.antlr.v4.runtime.TokenStream) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) LexerATNSimulator(org.antlr.v4.runtime.atn.LexerATNSimulator) IntegerList(org.antlr.v4.runtime.misc.IntegerList) ATN(org.antlr.v4.runtime.atn.ATN) PredictionContextCache(org.antlr.v4.runtime.atn.PredictionContextCache) DFA(org.antlr.v4.runtime.dfa.DFA)

Example 10 with NoViableAltException

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

the class TestATNInterpreter method testMustTrackPreviousGoodAltWithEOF.

@Test(expected = NoViableAltException.class)
public void testMustTrackPreviousGoodAltWithEOF() throws Exception {
    LexerGrammar lg = new LexerGrammar("lexer grammar L;\n" + "A : 'a' ;\n" + "B : 'b' ;\n" + "C : 'c' ;\n");
    Grammar g = new Grammar("parser grammar T;\n" + "a : (A | A B) EOF;");
    checkMatchedAlt(lg, g, "a", 1);
    checkMatchedAlt(lg, g, "ab", 2);
    try {
        checkMatchedAlt(lg, g, "ac", 1);
    } catch (NoViableAltException re) {
        assertEquals(1, re.getOffendingToken().getTokenIndex());
        assertEquals(3, re.getOffendingToken().getType());
        throw re;
    }
}
Also used : NoViableAltException(org.antlr.v4.runtime.NoViableAltException) Grammar(org.antlr.v4.tool.Grammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) LexerGrammar(org.antlr.v4.tool.LexerGrammar) Test(org.junit.Test)

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