Search in sources :

Example 16 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class DRL6StrictParser method constraint.

/**
 * constraint := nestedConstraint | conditionalOrExpression
 * @param pattern
 * @throws org.antlr.runtime.RecognitionException
 */
private void constraint(PatternDescrBuilder<?> pattern, boolean positional, String prefix) throws RecognitionException {
    if (speculateNestedConstraint()) {
        nestedConstraint(pattern, prefix);
        return;
    }
    if (state.backtracking == 0) {
        helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
    }
    int first = input.index();
    // resetting
    exprParser.getHelper().setHasOperator(false);
    try {
        exprParser.conditionalOrExpression();
    } finally {
        if (state.backtracking == 0) {
            if (input.LA(1) == DRL6Lexer.ID && input.LA(2) == DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
            } else if (input.LA(1) != DRL6Lexer.EOF) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_END);
            } else if (!lastTokenWasWhiteSpace()) {
                int location = getCurrentLocation();
                if (location == Location.LOCATION_LHS_INSIDE_CONDITION_END) {
                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_ARGUMENT);
                } else if (input.get(input.index()).getType() != DRL6Lexer.EOF) {
                    helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_START);
                }
            } else if (getCurrentLocation() == Location.LOCATION_LHS_INSIDE_CONDITION_START && !exprParser.getHelper().getHasOperator() && lastTokenWasWhiteSpace() && input.LA(1) == DRL6Lexer.EOF && input.LA(-1) == DRL6Lexer.ID) {
                helper.emit(Location.LOCATION_LHS_INSIDE_CONDITION_OPERATOR);
            }
        }
    }
    if (state.failed)
        return;
    if (state.backtracking == 0 && input.index() > first) {
        // expression consumed something
        int last = input.LT(-1).getTokenIndex();
        String expr = toExpression(prefix, first, last);
        pattern.constraint(expr, positional);
        BaseDescr constrDescr = pattern.getDescr().getDescrs().get(pattern.getDescr().getDescrs().size() - 1);
        constrDescr.setLocation(input.get(first).getLine(), input.get(first).getCharPositionInLine());
        constrDescr.setEndLocation(input.get(last).getLine(), input.get(last).getCharPositionInLine());
        constrDescr.setStartCharacter(((CommonToken) input.get(first)).getStartIndex());
        constrDescr.setEndCharacter(((CommonToken) input.get(last)).getStopIndex());
    }
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr)

Example 17 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class DRL6StrictParser method declare.

/* ------------------------------------------------------------------------------------------------
     *                         DECLARE STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * declare := DECLARE
 *               | (ENTRY-POINT) => entryPointDeclaration
 *               | (WINDOW) => windowDeclaration
 *               | (TRAIT) => typeDeclaration (trait)
 *               | (ENUM) => enumDeclaration
 *               | typeDeclaration (class)
 *            END
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public BaseDescr declare(PackageDescrBuilder pkg) throws RecognitionException {
    BaseDescr declaration = null;
    try {
        DeclareDescrBuilder declare = helper.start(pkg, DeclareDescrBuilder.class, null);
        // 'declare'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.DECLARE, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        if (helper.validateIdentifierKey(DroolsSoftKeywords.ENTRY)) {
            // entry point declaration
            declaration = entryPointDeclaration(declare);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.WINDOW)) {
            // window declaration
            declaration = windowDeclaration(declare);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.TRAIT)) {
            // trait type declaration
            // 'trait'
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.TRAIT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            declaration = typeDeclaration(declare, true);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENUM)) {
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.ENUM, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            declaration = enumDeclaration(declare);
        } else {
            // class type declaration
            declaration = typeDeclaration(declare, false);
        }
    } catch (RecognitionException re) {
        reportError(re);
    }
    return declaration;
}
Also used : DeclareDescrBuilder(org.drools.compiler.lang.api.DeclareDescrBuilder) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 18 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class DRL6StrictParser method lhsUnary.

/**
 * lhsUnary :=
 *           annotation*
 *           ( lhsExists namedConsequence?
 *           | lhsNot namedConsequence?
 *           | lhsEval consequenceInvocation*
 *           | lhsForall
 *           | lhsAccumulate
 *           | LEFT_PAREN lhsOr RIGHT_PAREN namedConsequence?
 *           | lhsPatternBind consequenceInvocation*
 *           )
 *           SEMICOLON?
 *
 * @param ce
 * @return
 */
private BaseDescr lhsUnary(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
    annotations();
    BaseDescr result = null;
    if (helper.validateIdentifierKey(DroolsSoftKeywords.EXISTS)) {
        result = lhsExists(ce, allowOr);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.DO)) {
            namedConsequence(ce, null);
        }
    } else if (helper.validateIdentifierKey(DroolsSoftKeywords.NOT)) {
        result = lhsNot(ce, allowOr);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.DO)) {
            namedConsequence(ce, null);
        }
    } else if (helper.validateIdentifierKey(DroolsSoftKeywords.EVAL)) {
        result = lhsEval(ce);
        for (BaseDescr i = consequenceInvocation(ce); i != null; i = consequenceInvocation(ce)) ;
    } else if (helper.validateIdentifierKey(DroolsSoftKeywords.FORALL)) {
        result = lhsForall(ce);
    } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ACCUMULATE) || helper.validateIdentifierKey(DroolsSoftKeywords.ACC)) {
        result = lhsAccumulate(ce);
    } else if (input.LA(1) == DRL6Lexer.LEFT_PAREN) {
        // the order here is very important: this if branch must come before the lhsPatternBind below
        result = lhsParen(ce, allowOr);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.DO)) {
            namedConsequence(ce, null);
        }
    } else if (input.LA(1) == DRL6Lexer.ID || input.LA(1) == DRL6Lexer.QUESTION) {
        result = lhsPatternBind(ce, allowOr);
        for (BaseDescr i = consequenceInvocation(ce); i != null; i = consequenceInvocation(ce)) ;
    } else {
        failMismatchedTokenException();
    }
    if (input.LA(1) == DRL6Lexer.SEMICOLON) {
        match(input, DRL6Lexer.SEMICOLON, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return null;
    }
    setAnnotationsOn(result);
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr)

Example 19 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class DRL6StrictParser method lhsOr.

/**
 * lhsOr := LEFT_PAREN OR lhsAnd+ RIGHT_PAREN
 *        | lhsAnd (OR lhsAnd)*
 *
 * @param ce
 * @param allowOr
 * @throws org.antlr.runtime.RecognitionException
 */
private BaseDescr lhsOr(final CEDescrBuilder<?, ?> ce, boolean allowOr) throws RecognitionException {
    BaseDescr result = null;
    if (allowOr && input.LA(1) == DRL6Lexer.LEFT_PAREN && helper.validateLT(2, DroolsSoftKeywords.OR)) {
        // prefixed OR
        CEDescrBuilder<?, OrDescr> or = null;
        if (state.backtracking == 0) {
            or = ce.or();
            result = or.getDescr();
            helper.start(or, CEDescrBuilder.class, null);
        }
        try {
            match(input, DRL6Lexer.LEFT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
            }
            while (input.LA(1) != DRL6Lexer.RIGHT_PAREN) {
                lhsAnd(or, allowOr);
                if (state.failed)
                    return null;
            }
            match(input, DRL6Lexer.RIGHT_PAREN, null, null, DroolsEditorType.SYMBOL);
            if (state.failed)
                return null;
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, or);
            }
        }
    } else {
        // infix OR
        // create an OR anyway, as if it is not an OR we remove it later
        CEDescrBuilder<?, OrDescr> or = null;
        if (state.backtracking == 0) {
            or = ce.or();
            result = or.getDescr();
            helper.start(or, CEDescrBuilder.class, null);
        }
        try {
            lhsAnd(or, allowOr);
            if (state.failed)
                return null;
            if (allowOr && (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL6Lexer.DOUBLE_PIPE)) {
                while (helper.validateIdentifierKey(DroolsSoftKeywords.OR) || input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
                    if (input.LA(1) == DRL6Lexer.DOUBLE_PIPE) {
                        match(input, DRL6Lexer.DOUBLE_PIPE, null, null, DroolsEditorType.SYMBOL);
                    } else {
                        match(input, DRL6Lexer.ID, DroolsSoftKeywords.OR, null, DroolsEditorType.KEYWORD);
                    }
                    if (state.failed)
                        return null;
                    if (state.backtracking == 0) {
                        helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION_AND_OR);
                    }
                    lhsAnd(or, allowOr);
                    if (state.failed)
                        return null;
                }
            } else if (allowOr) {
                if (state.backtracking == 0) {
                    // if no OR present, then remove it and add children to parent
                    ((ConditionalElementDescr) ce.getDescr()).getDescrs().remove(or.getDescr());
                    for (BaseDescr base : or.getDescr().getDescrs()) {
                        ((ConditionalElementDescr) ce.getDescr()).addDescr(base);
                    }
                    result = ce.getDescr();
                }
            }
        } finally {
            if (state.backtracking == 0) {
                helper.end(CEDescrBuilder.class, or);
            }
        }
    }
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr) OrDescr(org.drools.compiler.lang.descr.OrDescr) ConditionalElementDescr(org.drools.compiler.lang.descr.ConditionalElementDescr)

Example 20 with BaseDescr

use of org.drools.compiler.lang.descr.BaseDescr in project drools by kiegroup.

the class DRL5Expressions method conditionalExpression.

// $ANTLR end "expression"
// $ANTLR start "conditionalExpression"
// src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:168:1: conditionalExpression returns [BaseDescr result] : left= conditionalOrExpression ( ternaryExpression )? ;
public final BaseDescr conditionalExpression() throws RecognitionException {
    BaseDescr result = null;
    BaseDescr left = null;
    try {
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:169:5: (left= conditionalOrExpression ( ternaryExpression )? )
        // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:169:9: left= conditionalOrExpression ( ternaryExpression )?
        {
            pushFollow(FOLLOW_conditionalOrExpression_in_conditionalExpression820);
            left = conditionalOrExpression();
            state._fsp--;
            if (state.failed)
                return result;
            if (state.backtracking == 0) {
                if (buildDescr) {
                    result = left;
                }
            }
            // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:170:9: ( ternaryExpression )?
            int alt17 = 2;
            int LA17_0 = input.LA(1);
            if ((LA17_0 == QUESTION)) {
                alt17 = 1;
            }
            switch(alt17) {
                case 1:
                    // src/main/resources/org/drools/compiler/lang/DRL5Expressions.g:170:9: ternaryExpression
                    {
                        pushFollow(FOLLOW_ternaryExpression_in_conditionalExpression832);
                        ternaryExpression();
                        state._fsp--;
                        if (state.failed)
                            return result;
                    }
                    break;
            }
        }
    } catch (RecognitionException re) {
        throw re;
    } finally {
    // do for sure before leaving
    }
    return result;
}
Also used : BaseDescr(org.drools.compiler.lang.descr.BaseDescr) AnnotatedBaseDescr(org.drools.compiler.lang.descr.AnnotatedBaseDescr)

Aggregations

BaseDescr (org.drools.compiler.lang.descr.BaseDescr)109 AnnotatedBaseDescr (org.drools.compiler.lang.descr.AnnotatedBaseDescr)60 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)23 AtomicExprDescr (org.drools.compiler.lang.descr.AtomicExprDescr)17 ConditionalElementDescr (org.drools.compiler.lang.descr.ConditionalElementDescr)16 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)12 AndDescr (org.drools.compiler.lang.descr.AndDescr)11 RelationalExprDescr (org.drools.compiler.lang.descr.RelationalExprDescr)11 DeclareDescrBuilder (org.drools.compiler.lang.api.DeclareDescrBuilder)9 PatternDescr (org.drools.compiler.lang.descr.PatternDescr)8 OrDescr (org.drools.compiler.lang.descr.OrDescr)7 ArrayList (java.util.ArrayList)6 AccumulateDescrBuilder (org.drools.compiler.lang.api.AccumulateDescrBuilder)6 AnnotatedDescrBuilder (org.drools.compiler.lang.api.AnnotatedDescrBuilder)6 AnnotationDescrBuilder (org.drools.compiler.lang.api.AnnotationDescrBuilder)6 AttributeDescrBuilder (org.drools.compiler.lang.api.AttributeDescrBuilder)6 BehaviorDescrBuilder (org.drools.compiler.lang.api.BehaviorDescrBuilder)6 CEDescrBuilder (org.drools.compiler.lang.api.CEDescrBuilder)6 CollectDescrBuilder (org.drools.compiler.lang.api.CollectDescrBuilder)6 ConditionalBranchDescrBuilder (org.drools.compiler.lang.api.ConditionalBranchDescrBuilder)6