Search in sources :

Example 26 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drools by kiegroup.

the class DRL6StrictParser method annotation.

/* ------------------------------------------------------------------------------------------------
    *                         ANNOTATION
    * ------------------------------------------------------------------------------------------------ */
/**
 * annotation := fullAnnotation | AT ID chunk_(_)?
 */
void annotation(AnnotatedDescrBuilder<?> adb) {
    AnnotationDescrBuilder<?> annotation = null;
    try {
        if (speculateFullAnnotation()) {
            boolean buildState = exprParser.isBuildDescr();
            exprParser.setBuildDescr(true);
            exprParser.fullAnnotation(adb);
            exprParser.setBuildDescr(buildState);
        } else {
            // '@'
            Token at = match(input, DRL6Lexer.AT, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
            // identifier
            String fqn = qualifiedIdentifier();
            if (state.failed)
                return;
            if (state.backtracking == 0) {
                annotation = adb.newAnnotation(fqn);
                helper.setStart(annotation, at);
            }
            try {
                if (input.LA(1) == DRL6Lexer.LEFT_PAREN) {
                    String value = chunk(DRL6Lexer.LEFT_PAREN, DRL6Lexer.RIGHT_PAREN, -1).trim();
                    if (state.failed)
                        return;
                    if (state.backtracking == 0) {
                        annotation.value(value);
                    }
                }
            } finally {
                if (state.backtracking == 0) {
                    helper.setEnd(annotation);
                }
            }
        }
    } catch (RecognitionException re) {
        reportError(re);
    }
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 27 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drools by kiegroup.

the class DRL6StrictParser method field.

/**
 * field := annotation* label fieldType (EQUALS_ASSIGN conditionalExpression)? SEMICOLON?
 */
private void field(AbstractClassTypeDeclarationBuilder declare) {
    annotations();
    FieldDescrBuilder field = null;
    String fname = null;
    try {
        fname = label(DroolsEditorType.IDENTIFIER);
        if (state.failed)
            return;
    } catch (RecognitionException re) {
        reportError(re);
    }
    try {
        field = helper.start(declare, FieldDescrBuilder.class, fname);
        setAnnotationsOn(field);
        // type
        String type = type();
        if (state.failed)
            return;
        if (state.backtracking == 0)
            field.type(type);
        if (input.LA(1) == DRL6Lexer.EQUALS_ASSIGN) {
            // EQUALS_ASSIGN
            match(input, DRL6Lexer.EQUALS_ASSIGN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
            int first = input.index();
            exprParser.conditionalExpression();
            if (state.failed)
                return;
            if (state.backtracking == 0 && input.index() > first) {
                // expression consumed something
                String value = input.toString(first, input.LT(-1).getTokenIndex());
                field.initialValue(value);
            }
        }
        if (input.LA(1) == DRL6Lexer.SEMICOLON) {
            match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return;
        }
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(FieldDescrBuilder.class, field);
    }
}
Also used : FieldDescrBuilder(org.drools.compiler.lang.api.FieldDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 28 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drools by kiegroup.

the class DRL6StrictParser method enumDeclaration.

/*
     * typeDeclaration := annotation* [ENUM] qualifiedIdentifier
     *                         enumerative+
     *                         field*
     *                     END
     *
     * @return
     * @throws RecognitionException
     */
public EnumDeclarationDescr enumDeclaration(DeclareDescrBuilder ddb) throws RecognitionException {
    EnumDeclarationDescrBuilder declare = null;
    try {
        declare = helper.start(ddb, EnumDeclarationDescrBuilder.class, null);
        setAnnotationsOn(declare);
        // type may be qualified when adding metadata
        String type = qualifiedIdentifier();
        if (state.failed)
            return null;
        if (state.backtracking == 0)
            declare.name(type);
        while (input.LA(1) == DRL6Lexer.ID) {
            int next = input.LA(2);
            if (next == DRL6Lexer.LEFT_PAREN || next == DRL6Lexer.COMMA || next == DRL6Lexer.SEMICOLON) {
                enumerative(declare);
                if (state.failed)
                    return null;
            }
            if (input.LA(1) == DRL6Lexer.COMMA) {
                match(input, DRL6Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
            } else {
                match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
                break;
            }
        }
        // boolean qualified = type.indexOf( '.' ) >= 0;
        while (// ! qualified &&
        input.LA(1) == DRL6Lexer.ID && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            // field*
            field(declare);
            if (state.failed)
                return null;
        }
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(TypeDeclarationDescrBuilder.class, declare);
    }
    return (declare != null) ? declare.getDescr() : null;
}
Also used : EnumDeclarationDescrBuilder(org.drools.compiler.lang.api.EnumDeclarationDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 29 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drools by kiegroup.

the class DRL6StrictParser method function.

/* ------------------------------------------------------------------------------------------------
     *                         FUNCTION STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * function := FUNCTION type? ID parameters(typed) chunk_{_}
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public FunctionDescr function(PackageDescrBuilder pkg) throws RecognitionException {
    FunctionDescrBuilder function = null;
    try {
        function = helper.start(pkg, FunctionDescrBuilder.class, null);
        // 'function'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.FUNCTION, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        if (input.LA(1) != DRL6Lexer.ID || input.LA(2) != DRL6Lexer.LEFT_PAREN) {
            // type
            String type = type();
            if (state.failed)
                return null;
            if (state.backtracking == 0)
                function.returnType(type);
        }
        // name
        Token id = match(input, DRL6Lexer.ID, null, null, DroolsEditorType.IDENTIFIER);
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            function.name(id.getText());
            helper.setParaphrasesValue(DroolsParaphraseTypes.FUNCTION, "\"" + id.getText() + "\"");
        }
        // arguments
        parameters(function, true);
        if (state.failed)
            return null;
        // body
        String body = chunk(DRL6Lexer.LEFT_CURLY, DRL6Lexer.RIGHT_CURLY, -1);
        if (state.failed)
            return null;
        if (state.backtracking == 0)
            function.body(body);
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(FunctionDescrBuilder.class, function);
    }
    return (function != null) ? function.getDescr() : null;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) FunctionDescrBuilder(org.drools.compiler.lang.api.FunctionDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 30 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drools by kiegroup.

the class DRL6StrictParser method recoverFromMismatchedToken.

/**
 * Attempt to recover from a single missing or extra token.
 *
 *  EXTRA TOKEN
 *
 *  LA(1) is not what we are looking for.  If LA(2) has the right token,
 *  however, then assume LA(1) is some extra spurious token.  Delete it
 *  and LA(2) as if we were doing a normal match(), which advances the
 *  input.
 *
 *  MISSING TOKEN
 *
 *  If current token is consistent with what could come after
 *  ttype then it is ok to "insert" the missing token, else throw
 *  exception For example, Input "i=(3;" is clearly missing the
 *  ')'.  When the parser returns from the nested call to expr, it
 *  will have call chain:
 *
 *    stat -> expr -> atom
 *
 *  and it will be trying to match the ')' at this point in the
 *  derivation:
 *
 *       => ID '=' '(' INT ')' ('+' atom)* ';'
 *                          ^
 *  match() will see that ';' doesn't match ')' and report a
 *  mismatched token error.  To recover, it sees that LA(1)==';'
 *  is in the set of tokens that can follow the ')' token
 *  reference in rule atom.  It can assume that you forgot the ')'.
 */
protected Token recoverFromMismatchedToken(TokenStream input, int ttype, String text, int[] follow) throws RecognitionException {
    RecognitionException e = null;
    // if next token is what we are looking for then "delete" this token
    if (mismatchIsUnwantedToken(input, ttype, text)) {
        e = new UnwantedTokenException(ttype, input);
        // simply delete extra token
        input.consume();
        // report after consuming so AW sees the token in the exception
        reportError(e);
        // we want to return the token we're actually matching
        Token matchedSymbol = input.LT(1);
        // move past ttype token as if all were ok
        input.consume();
        return matchedSymbol;
    }
    // can't recover with single token deletion, try insertion
    if (mismatchIsMissingToken(input, follow)) {
        e = new MissingTokenException(ttype, input, null);
        // report after inserting so AW sees the token in the exception
        reportError(e);
        return null;
    }
    // even that didn't work; must throw the exception
    if (text != null) {
        e = new DroolsMismatchedTokenException(ttype, text, input);
    } else {
        e = new MismatchedTokenException(ttype, input);
    }
    throw e;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) UnwantedTokenException(org.antlr.runtime.UnwantedTokenException) MissingTokenException(org.antlr.runtime.MissingTokenException) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

RecognitionException (org.antlr.runtime.RecognitionException)375 Token (org.antlr.runtime.Token)215 CommonTree (org.antlr.runtime.tree.CommonTree)138 RewriteRuleSubtreeStream (org.antlr.runtime.tree.RewriteRuleSubtreeStream)112 CommonToken (org.antlr.runtime.CommonToken)95 RewriteRuleTokenStream (org.antlr.runtime.tree.RewriteRuleTokenStream)83 NoViableAltException (org.antlr.runtime.NoViableAltException)50 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)34 RewriteEarlyExitException (org.antlr.runtime.tree.RewriteEarlyExitException)27 CommonTokenStream (org.antlr.runtime.CommonTokenStream)22 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)16 EarlyExitException (org.antlr.runtime.EarlyExitException)16 MismatchedSetException (org.antlr.runtime.MismatchedSetException)14 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)12 Test (org.junit.Test)12 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)11 FailedPredicateException (org.antlr.runtime.FailedPredicateException)10 MissingTokenException (org.antlr.runtime.MissingTokenException)10 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)10 BoundIdentifiers (org.drools.compiler.compiler.BoundIdentifiers)8