Search in sources :

Example 21 with RecognitionException

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

the class DRL6StrictParser method defaultConsequence.

/**
 * defaultConsequence := THEN chunk
 * @param rule
 */
public void defaultConsequence(RuleDescrBuilder rule) {
    try {
        int first = input.index();
        Token t = match(input, DRL6Lexer.ID, DroolsSoftKeywords.THEN, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return;
        if (state.backtracking == 0) {
            rule.getDescr().setConsequenceLocation(t.getLine(), t.getCharPositionInLine());
            helper.emit(Location.LOCATION_RHS);
        }
        String chunk = getConsequenceCode(first);
        // remove the "then" keyword and any subsequent spaces and line breaks
        // keep indentation of 1st non-blank line
        chunk = chunk.replaceFirst("^then\\s*\\r?\\n?", "");
        rule.rhs(chunk);
    } catch (RecognitionException re) {
        reportError(re);
    }
}
Also used : Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 22 with RecognitionException

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

the class DRL6StrictParser method query.

/* ------------------------------------------------------------------------------------------------
     *                         QUERY STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * query := annotation* QUERY stringId parameters? lhsExpression END
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public RuleDescr query(PackageDescrBuilder pkg) throws RecognitionException {
    QueryDescrBuilder query = null;
    try {
        query = helper.start(pkg, QueryDescrBuilder.class, null);
        setAnnotationsOn(query);
        // 'query'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.QUERY, 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.backtracking == 0)
            query.name(name);
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            helper.emit(Location.LOCATION_RULE_HEADER);
        }
        if (speculateParameters(true)) {
            // parameters
            parameters(query, true);
            if (state.failed)
                return null;
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
        } else if (speculateParameters(false)) {
            // parameters
            parameters(query, false);
            if (state.failed)
                return null;
            if (state.backtracking == 0) {
                helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
            }
        }
        if (state.backtracking == 0 && input.LA(1) != DRL6Lexer.EOF) {
            helper.emit(Location.LOCATION_LHS_BEGIN_OF_CONDITION);
        }
        if (input.LA(1) != DRL6Lexer.EOF) {
            lhsExpression(query != null ? query.lhs() : null);
        }
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        helper.emit(Location.LOCATION_RHS);
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(QueryDescrBuilder.class, query);
    }
    return (query != null) ? query.getDescr() : null;
}
Also used : QueryDescrBuilder(org.drools.compiler.lang.api.QueryDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 23 with RecognitionException

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

the class DRL6StrictParser method globalStatement.

/* ------------------------------------------------------------------------------------------------
     *                         GLOBAL STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * globalStatement := GLOBAL type ID
 *
 * @return
 * @throws org.antlr.runtime.RecognitionException
 */
public GlobalDescr globalStatement(PackageDescrBuilder pkg) throws RecognitionException {
    GlobalDescrBuilder global = null;
    try {
        global = helper.start(pkg, GlobalDescrBuilder.class, null);
        // 'global'
        match(input, DRL6Lexer.ID, DroolsSoftKeywords.GLOBAL, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        // type
        String type = type();
        if (state.backtracking == 0)
            global.type(type);
        if (state.failed)
            return null;
        // identifier
        Token id = match(input, DRL6Lexer.ID, null, null, DroolsEditorType.IDENTIFIER_TYPE);
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            global.identifier(id.getText());
            helper.setParaphrasesValue(DroolsParaphraseTypes.GLOBAL, id.getText());
        }
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(GlobalDescrBuilder.class, global);
    }
    return (global != null) ? global.getDescr() : null;
}
Also used : GlobalDescrBuilder(org.drools.compiler.lang.api.GlobalDescrBuilder) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) RecognitionException(org.antlr.runtime.RecognitionException)

Example 24 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 25 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)

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