Search in sources :

Example 76 with Recognizer

use of org.antlr.v4.runtime.Recognizer in project drools by kiegroup.

the class FEELParserErrorListener method syntaxError.

@Override
public void syntaxError(final Recognizer<?, ?> recognizer, final Object offendingSymbol, final int line, final int charPositionInLine, final String msg, final RecognitionException e) {
    final SyntaxErrorEvent error;
    final CommonToken token = (CommonToken) offendingSymbol;
    final int tokenIndex = token.getTokenIndex();
    final Parser parser = (Parser) recognizer;
    if (parser.getRuleInvocationStack().contains("nameDefinitionWithEOF")) {
        error = generateInvalidVariableError(offendingSymbol, line, charPositionInLine, e, token);
    } else if ("}".equals(token.getText()) && tokenIndex > 1 && ":".equals(parser.getTokenStream().get(tokenIndex - 1).getText())) {
        error = new SyntaxErrorEvent(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.MISSING_EXPRESSION, parser.getTokenStream().get(tokenIndex - 2).getText()), e, line, charPositionInLine, offendingSymbol);
    } else if (e != null && parser.getRuleInvocationStack().get(0).equals("ifExpression")) {
        final List<String> expected = toList(e.getExpectedTokens(), e.getRecognizer().getVocabulary());
        if (expected.contains("ELSE")) {
            error = new SyntaxErrorEvent(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.IF_MISSING_ELSE, token.getText(), msg), e, line, charPositionInLine, offendingSymbol);
        } else if (expected.contains("THEN")) {
            error = new SyntaxErrorEvent(FEELEvent.Severity.ERROR, Msg.createMessage(Msg.IF_MISSING_THEN, token.getText(), msg), e, line, charPositionInLine, offendingSymbol);
        } else {
            // fallback.
            error = new SyntaxErrorEvent(FEELEvent.Severity.ERROR, msg, e, line, charPositionInLine, offendingSymbol);
        }
    } else {
        error = new SyntaxErrorEvent(FEELEvent.Severity.ERROR, msg, e, line, charPositionInLine, offendingSymbol);
    }
    // if the event manager is set, notify listeners, otherwise store the error in the errors list
    if (eventsManager != null) {
        FEELEventListenersManager.notifyListeners(eventsManager, () -> error);
    } else {
        if (errors == null) {
            errors = new ArrayList<>();
        }
        errors.add(error);
    }
}
Also used : SyntaxErrorEvent(org.kie.dmn.feel.runtime.events.SyntaxErrorEvent) CommonToken(org.antlr.v4.runtime.CommonToken) Parser(org.antlr.v4.runtime.Parser)

Aggregations

IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)24 Token (org.antlr.v4.runtime.Token)22 RecognitionException (org.antlr.v4.runtime.RecognitionException)19 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)15 File (java.io.File)11 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)10 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)10 ATNState (org.antlr.v4.runtime.atn.ATNState)9 IOException (java.io.IOException)8 BaseErrorListener (org.antlr.v4.runtime.BaseErrorListener)8 Parser (org.antlr.v4.runtime.Parser)8 BaseRuntimeTest.writeFile (org.antlr.v4.test.runtime.BaseRuntimeTest.writeFile)8 ArrayList (java.util.ArrayList)7 ATN (org.antlr.v4.runtime.atn.ATN)6 Pair (com.abubusoft.kripton.common.Pair)5 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)5 TokenStream (org.antlr.v4.runtime.TokenStream)5 BeetlException (org.beetl.core.exception.BeetlException)5 STGroupString (org.stringtemplate.v4.STGroupString)5 CommonToken (org.antlr.v4.runtime.CommonToken)4