Search in sources :

Example 6 with FailedPredicateException

use of org.antlr.v4.runtime.FailedPredicateException in project ballerina by ballerina-lang.

the class BallerinaParserErrorStrategy method reportError.

public void reportError(Parser parser, RecognitionException e) {
    if (inErrorRecoveryMode(parser)) {
        return;
    }
    beginErrorCondition(parser);
    if (e instanceof NoViableAltException) {
        reportNoViableAlternative(parser, (NoViableAltException) e);
    } else if (e instanceof InputMismatchException) {
        reportInputMismatch(parser, (InputMismatchException) e);
    } else if (e instanceof FailedPredicateException) {
        reportFailedPredicate(parser, (FailedPredicateException) e);
    } else {
        setContextException(parser);
        DiagnosticPos pos = getPosition(getMissingSymbol(parser));
        dlog.error(pos, DiagnosticCode.INVALID_TOKEN, e.getMessage());
    }
}
Also used : DiagnosticPos(org.wso2.ballerinalang.compiler.util.diagnotic.DiagnosticPos) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) InputMismatchException(org.antlr.v4.runtime.InputMismatchException)

Example 7 with FailedPredicateException

use of org.antlr.v4.runtime.FailedPredicateException in project checkstyle by checkstyle.

the class JavadocDetailNodeParser method parseJavadocAsDetailNode.

/**
 * Parses Javadoc comment as DetailNode tree.
 *
 * @param javadocCommentAst
 *        DetailAST of Javadoc comment
 * @return DetailNode tree of Javadoc comment
 */
public ParseStatus parseJavadocAsDetailNode(DetailAST javadocCommentAst) {
    blockCommentLineNumber = javadocCommentAst.getLineNo();
    final String javadocComment = JavadocUtil.getJavadocCommentContent(javadocCommentAst);
    // Use a new error listener each time to be able to use
    // one check instance for multiple files to be checked
    // without getting side effects.
    final DescriptiveErrorListener errorListener = new DescriptiveErrorListener();
    // Log messages should have line number in scope of file,
    // not in scope of Javadoc comment.
    // Offset is line number of beginning of Javadoc comment.
    errorListener.setOffset(javadocCommentAst.getLineNo() - 1);
    final ParseStatus result = new ParseStatus();
    try {
        final JavadocParser javadocParser = createJavadocParser(javadocComment, errorListener);
        final ParseTree javadocParseTree = javadocParser.javadoc();
        final DetailNode tree = convertParseTreeToDetailNode(javadocParseTree);
        // adjust first line to indent of /**
        adjustFirstLineToJavadocIndent(tree, javadocCommentAst.getColumnNo() + JAVADOC_START.length());
        result.setTree(tree);
        result.firstNonTightHtmlTag = getFirstNonTightHtmlTag(javadocParser, errorListener.offset);
    } catch (ParseCancellationException | IllegalArgumentException ex) {
        ParseErrorMessage parseErrorMessage = null;
        if (ex.getCause() instanceof FailedPredicateException || ex.getCause() instanceof NoViableAltException) {
            final RecognitionException recognitionEx = (RecognitionException) ex.getCause();
            if (recognitionEx.getCtx() instanceof JavadocParser.HtmlTagContext) {
                final Token htmlTagNameStart = getMissedHtmlTag(recognitionEx);
                parseErrorMessage = new ParseErrorMessage(errorListener.offset + htmlTagNameStart.getLine(), MSG_JAVADOC_MISSED_HTML_CLOSE, htmlTagNameStart.getCharPositionInLine(), htmlTagNameStart.getText());
            }
        }
        if (parseErrorMessage == null) {
            // If syntax error occurs then message is printed by error listener
            // and parser throws this runtime exception to stop parsing.
            // Just stop processing current Javadoc comment.
            parseErrorMessage = errorListener.getErrorMessage();
        }
        result.setParseErrorMessage(parseErrorMessage);
    }
    return result;
}
Also used : Token(org.antlr.v4.runtime.Token) CommonToken(org.antlr.v4.runtime.CommonToken) JavadocParser(com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser) ParseCancellationException(org.antlr.v4.runtime.misc.ParseCancellationException) NoViableAltException(org.antlr.v4.runtime.NoViableAltException) DetailNode(com.puppycrawl.tools.checkstyle.api.DetailNode) FailedPredicateException(org.antlr.v4.runtime.FailedPredicateException) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Aggregations

FailedPredicateException (org.antlr.v4.runtime.FailedPredicateException)5 NoViableAltException (org.antlr.v4.runtime.NoViableAltException)4 RecognitionException (org.antlr.v4.runtime.RecognitionException)3 InputMismatchException (org.antlr.v4.runtime.InputMismatchException)2 ParserRuleContext (org.antlr.v4.runtime.ParserRuleContext)2 ActionTransition (org.antlr.v4.runtime.atn.ActionTransition)2 AtomTransition (org.antlr.v4.runtime.atn.AtomTransition)2 DecisionState (org.antlr.v4.runtime.atn.DecisionState)2 LoopEndState (org.antlr.v4.runtime.atn.LoopEndState)2 PrecedencePredicateTransition (org.antlr.v4.runtime.atn.PrecedencePredicateTransition)2 PredicateTransition (org.antlr.v4.runtime.atn.PredicateTransition)2 RuleStartState (org.antlr.v4.runtime.atn.RuleStartState)2 RuleTransition (org.antlr.v4.runtime.atn.RuleTransition)2 Transition (org.antlr.v4.runtime.atn.Transition)2 DetailNode (com.puppycrawl.tools.checkstyle.api.DetailNode)1 JavadocParser (com.puppycrawl.tools.checkstyle.grammar.javadoc.JavadocParser)1 CommonToken (org.antlr.v4.runtime.CommonToken)1 Token (org.antlr.v4.runtime.Token)1 StarLoopEntryState (org.antlr.v4.runtime.atn.StarLoopEntryState)1 ParseCancellationException (org.antlr.v4.runtime.misc.ParseCancellationException)1