Search in sources :

Example 11 with RecognitionException

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

the class DRL6Parser method attribute.

/**
 * attribute :=
 *       salience
 *   |   enabled
 *   |   ( NO-LOOP
 *       | AUTO-FOCUS
 *       | LOCK-ON-ACTIVE
 *       | REFRACT
 *       | DIRECT
 *       ) BOOLEAN?
 *   |   ( AGENDA-GROUP
 *       | ACTIVATION-GROUP
 *       | RULEFLOW-GROUP
 *       | DATE-EFFECTIVE
 *       | DATE-EXPIRES
 *       | DIALECT
 *       ) STRING
 *   |   CALENDARS STRING (COMMA STRING)*
 *   |   TIMER ( DECIMAL | chunk_(_) )
 *   |   DURATION ( DECIMAL | chunk_(_) )
 *
 * The above syntax is not quite how this is parsed, because the soft keyword
 * is determined by look-ahead and passed on to one of the x-Attribute methods
 * (booleanAttribute, stringAttribute, stringListAttribute, intOrChunkAttribute)
 * which will actually gobble the tokens.
 *
 * @return
 */
public AttributeDescr attribute(AttributeSupportBuilder<?> as) {
    AttributeDescr attribute = null;
    try {
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_RULE_HEADER_KEYWORD);
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.SALIENCE)) {
            attribute = salience(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENABLED)) {
            attribute = enabled(as);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.NO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.LOOP)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.NO, "-", DroolsSoftKeywords.LOOP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AUTO) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.FOCUS)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.AUTO, "-", DroolsSoftKeywords.FOCUS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.LOCK) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.ON) && helper.validateLT(4, "-") && helper.validateLT(5, DroolsSoftKeywords.ACTIVE)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.LOCK, "-", DroolsSoftKeywords.ON, "-", DroolsSoftKeywords.ACTIVE });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.REFRACT)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.REFRACT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIRECT)) {
            attribute = booleanAttribute(as, new String[] { DroolsSoftKeywords.DIRECT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.AGENDA) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.AGENDA, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ACTIVATION) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.ACTIVATION, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.RULEFLOW) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.GROUP)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.RULEFLOW, "-", DroolsSoftKeywords.GROUP });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EFFECTIVE)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EFFECTIVE });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DATE) && helper.validateLT(2, "-") && helper.validateLT(3, DroolsSoftKeywords.EXPIRES)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DATE, "-", DroolsSoftKeywords.EXPIRES });
            attribute.setType(AttributeDescr.Type.DATE);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DIALECT)) {
            attribute = stringAttribute(as, new String[] { DroolsSoftKeywords.DIALECT });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.CALENDARS)) {
            attribute = stringListAttribute(as, new String[] { DroolsSoftKeywords.CALENDARS });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.TIMER)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.TIMER });
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.DURATION)) {
            attribute = intOrChunkAttribute(as, new String[] { DroolsSoftKeywords.DURATION });
        }
        if (state.backtracking == 0) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
    } catch (RecognitionException re) {
        reportError(re);
    }
    return attribute;
}
Also used : AttributeDescr(org.drools.compiler.lang.descr.AttributeDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 12 with RecognitionException

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

the class DRL6Parser 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 13 with RecognitionException

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

the class DRL6Parser method qualifiedIdentifier.

/**
 * Matches a qualified identifier
 *
 * qualifiedIdentifier := ID ( DOT ID )*
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public String qualifiedIdentifier() throws RecognitionException {
    String qi = "";
    try {
        Token first = match(input, DRL6Lexer.ID, null, new int[] { DRL6Lexer.DOT }, DroolsEditorType.IDENTIFIER);
        if (state.failed)
            return qi;
        Token last = first;
        while (input.LA(1) == DRL6Lexer.DOT && input.LA(2) == DRL6Lexer.ID) {
            last = match(input, DRL6Lexer.DOT, null, new int[] { DRL6Lexer.ID }, DroolsEditorType.IDENTIFIER);
            if (state.failed)
                return qi;
            last = match(input, DRL6Lexer.ID, null, new int[] { DRL6Lexer.DOT }, DroolsEditorType.IDENTIFIER);
            if (state.failed)
                return qi;
        }
        qi = input.toString(first, last);
        qi = qi.replace(" ", "");
    } catch (RecognitionException re) {
        reportError(re);
    }
    return qi;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 14 with RecognitionException

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

the class DRL6Parser method typeArguments.

/**
 * Matches type arguments
 *
 * typeArguments := LESS typeArgument (COMMA typeArgument)* GREATER
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public String typeArguments() throws RecognitionException {
    String typeArguments = "";
    try {
        int first = input.index();
        Token token = match(input, DRL6Lexer.LESS, null, new int[] { DRL6Lexer.QUESTION, DRL6Lexer.ID }, DroolsEditorType.SYMBOL);
        if (state.failed)
            return typeArguments;
        typeArgument();
        if (state.failed)
            return typeArguments;
        while (input.LA(1) == DRL6Lexer.COMMA) {
            token = match(input, DRL6Lexer.COMMA, null, new int[] { DRL6Lexer.QUESTION, DRL6Lexer.ID }, DroolsEditorType.IDENTIFIER);
            if (state.failed)
                return typeArguments;
            typeArgument();
            if (state.failed)
                return typeArguments;
        }
        token = match(input, DRL6Lexer.GREATER, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return typeArguments;
        typeArguments = input.toString(first, token.getTokenIndex());
    } catch (RecognitionException re) {
        reportError(re);
    }
    return typeArguments;
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 15 with RecognitionException

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

the class DRL6Parser method rule.

/* ------------------------------------------------------------------------------------------------
     *                         RULE STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * rule := RULE stringId (EXTENDS stringId)? annotation* attributes? lhs? rhs END
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public RuleDescr rule(PackageDescrBuilder pkg) throws RecognitionException {
    RuleDescrBuilder rule = null;
    try {
        rule = helper.start(pkg, RuleDescrBuilder.class, null);
        // 'rule'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.RULE, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        if (helper.validateIdentifierKey(DroolsSoftKeywords.WHEN) || helper.validateIdentifierKey(DroolsSoftKeywords.THEN) || helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            failMissingTokenException();
            // in case it is backtracking
            return null;
        }
        String name = stringId();
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            rule.name(name);
            helper.setParaphrasesValue(DroolsParaphraseTypes.RULE, "\"" + name + "\"");
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
        if (helper.validateIdentifierKey(DroolsSoftKeywords.EXTENDS)) {
            // 'extends'
            match(input, DRL6Lexer.ID, DroolsSoftKeywords.EXTENDS, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            String parent = stringId();
            if (state.backtracking == 0)
                rule.extendsRule(parent);
            if (state.failed)
                return null;
        }
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
        while (input.LA(1) == DRL6Lexer.AT) {
            // annotation*
            annotation(rule);
            if (state.failed)
                return null;
        }
        attributes(rule);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.WHEN)) {
            lhs(rule);
        } else {
            // creates an empty LHS
            rule.lhs();
        }
        rhs(rule);
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(RuleDescrBuilder.class, rule);
    }
    return (rule != null) ? rule.getDescr() : null;
}
Also used : RuleDescrBuilder(org.drools.compiler.lang.api.RuleDescrBuilder) 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