Search in sources :

Example 96 with RecognitionException

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

the class DRL6StrictParser method namedConsequence.

/**
 * namedConsequence := THEN LEFT_SQUARE ID RIGHT_SQUARE chunk
 * @param rule
 */
public void namedConsequence(RuleDescrBuilder rule) {
    try {
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.THEN, null, DroolsEditorType.KEYWORD);
        match(input, DRL6Lexer.LEFT_SQUARE, null, null, DroolsEditorType.SYMBOL);
        Token label = match(input, DRL6Lexer.ID, null, null, DroolsEditorType.SYMBOL);
        int first = input.index();
        match(input, DRL6Lexer.RIGHT_SQUARE, null, null, DroolsEditorType.SYMBOL);
        if (state.failed)
            return;
        String name = label.getText();
        String chunk = getConsequenceCode(first);
        // remove the closing squqre bracket "]" and any subsequent spaces and line breaks
        // keep indentation of 1st non-blank line
        chunk = chunk.replaceFirst("^\\]\\s*\\r?\\n?", "");
        rule.namedRhs(name, chunk);
    } catch (RecognitionException re) {
        reportError(re);
    }
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 97 with RecognitionException

use of org.antlr.runtime.RecognitionException 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 98 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 99 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 100 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)

Aggregations

RecognitionException (org.antlr.runtime.RecognitionException)257 Token (org.antlr.runtime.Token)169 CommonTree (org.antlr.runtime.tree.CommonTree)132 RewriteRuleSubtreeStream (org.antlr.runtime.tree.RewriteRuleSubtreeStream)98 RewriteRuleTokenStream (org.antlr.runtime.tree.RewriteRuleTokenStream)74 CommonToken (org.antlr.runtime.CommonToken)48 NoViableAltException (org.antlr.runtime.NoViableAltException)43 RewriteEarlyExitException (org.antlr.runtime.tree.RewriteEarlyExitException)21 ParserRuleReturnScope (org.antlr.runtime.ParserRuleReturnScope)14 CommonTokenStream (org.antlr.runtime.CommonTokenStream)11 EarlyExitException (org.antlr.runtime.EarlyExitException)10 MismatchedSetException (org.antlr.runtime.MismatchedSetException)10 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)8 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)7 TokenRewriteStream (org.antlr.runtime.TokenRewriteStream)7 FailedPredicateException (org.antlr.runtime.FailedPredicateException)6 MissingTokenException (org.antlr.runtime.MissingTokenException)6 UnwantedTokenException (org.antlr.runtime.UnwantedTokenException)6 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)5 HashMap (java.util.HashMap)4