Search in sources :

Example 31 with RecognitionException

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

the class DRL5Parser 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)

Example 32 with RecognitionException

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

the class DRL5Parser method typeDeclaration.

/**
 * typeDeclaration := [TYPE] qualifiedIdentifier (EXTENDS qualifiedIdentifier)?
 *                         annotation*
 *                         field*
 *                     END
 *
 * @return
 * @throws RecognitionException
 */
public TypeDeclarationDescr typeDeclaration(DeclareDescrBuilder ddb, boolean isTrait) throws RecognitionException {
    TypeDeclarationDescrBuilder declare = null;
    try {
        declare = helper.start(ddb, TypeDeclarationDescrBuilder.class, null);
        declare.setTrait(isTrait);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.TYPE)) {
            // 'type'
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.TYPE, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
        }
        // type may be qualified when adding metadata
        String type = qualifiedIdentifier();
        if (state.failed)
            return null;
        if (state.backtracking == 0)
            declare.name(type);
        if (helper.validateIdentifierKey(DroolsSoftKeywords.EXTENDS)) {
            match(input, DRL5Lexer.ID, DroolsSoftKeywords.EXTENDS, null, DroolsEditorType.KEYWORD);
            if (!state.failed) {
                // Going for type includes generics, which is a no-no (JIRA-3040)
                String superType = qualifiedIdentifier();
                declare.superType(superType);
                while (input.LA(1) == DRL5Lexer.COMMA) {
                    match(input, DRL5Lexer.COMMA, null, null, DroolsEditorType.SYMBOL);
                    superType = qualifiedIdentifier();
                    declare.superType(superType);
                }
            }
        }
        while (input.LA(1) == DRL5Lexer.AT) {
            // annotation*
            annotation(declare);
            if (state.failed)
                return null;
        }
        // boolean qualified = type.indexOf( '.' ) >= 0;
        while (// ! qualified &&
        input.LA(1) == DRL5Lexer.ID && !helper.validateIdentifierKey(DroolsSoftKeywords.END)) {
            // field*
            field(declare);
            if (state.failed)
                return null;
        }
        match(input, DRL5Lexer.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 : TypeDeclarationDescrBuilder(org.drools.compiler.lang.api.TypeDeclarationDescrBuilder) RecognitionException(org.antlr.runtime.RecognitionException)

Example 33 with RecognitionException

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

the class DRL5Parser method qualifiedIdentifier.

/**
 * Matches a qualified identifier
 *
 * qualifiedIdentifier := ID ( DOT ID )*
 *
 * @return
 * @throws RecognitionException
 */
public String qualifiedIdentifier() throws RecognitionException {
    String qi = "";
    try {
        Token first = match(input, DRL5Lexer.ID, null, new int[] { DRL5Lexer.DOT }, DroolsEditorType.IDENTIFIER);
        if (state.failed)
            return qi;
        Token last = first;
        while (input.LA(1) == DRL5Lexer.DOT && input.LA(2) == DRL5Lexer.ID) {
            last = match(input, DRL5Lexer.DOT, null, new int[] { DRL5Lexer.ID }, DroolsEditorType.IDENTIFIER);
            if (state.failed)
                return qi;
            last = match(input, DRL5Lexer.ID, null, new int[] { DRL5Lexer.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 34 with RecognitionException

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

the class DRL5Parser method declare.

/* ------------------------------------------------------------------------------------------------
     *                         DECLARE STATEMENT
     * ------------------------------------------------------------------------------------------------ */
/**
 * declare := DECLARE
 *               | (ENTRY-POINT) => entryPointDeclaration
 *               | (WINDOW) => windowDeclaration
 *               | (TRAIT) => typeDeclaration (trait)
 *               | (ENUM) => enumDeclaration
 *               | typeDeclaration (class)
 *            END
 *
 * @return
 * @throws RecognitionException
 */
public BaseDescr declare(PackageDescrBuilder pkg) throws RecognitionException {
    BaseDescr declaration = null;
    try {
        DeclareDescrBuilder declare = helper.start(pkg, DeclareDescrBuilder.class, null);
        // 'declare'
        match(input, DRL5Lexer.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, DRL5Lexer.ID, DroolsSoftKeywords.TRAIT, null, DroolsEditorType.KEYWORD);
            if (state.failed)
                return null;
            declaration = typeDeclaration(declare, true);
        } else if (helper.validateIdentifierKey(DroolsSoftKeywords.ENUM)) {
            match(input, DRL5Lexer.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) RecognitionException(org.antlr.runtime.RecognitionException)

Example 35 with RecognitionException

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

the class DRL5Parser method entryPointDeclaration.

/**
 * entryPointDeclaration := ENTRY-POINT stringId annotation* END
 *
 * @return
 * @throws RecognitionException
 */
public EntryPointDeclarationDescr entryPointDeclaration(DeclareDescrBuilder ddb) throws RecognitionException {
    EntryPointDeclarationDescrBuilder declare = null;
    try {
        declare = helper.start(ddb, EntryPointDeclarationDescrBuilder.class, null);
        match(input, DRL5Lexer.ID, DroolsSoftKeywords.ENTRY, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        match(input, DRL5Lexer.MINUS, null, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        match(input, DRL5Lexer.ID, DroolsSoftKeywords.POINT, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
        String ep = stringId();
        if (state.failed)
            return null;
        if (state.backtracking == 0) {
            declare.entryPointId(ep);
        }
        while (input.LA(1) == DRL5Lexer.AT) {
            // annotation*
            annotation(declare);
            if (state.failed)
                return null;
        }
        match(input, DRL5Lexer.ID, DroolsSoftKeywords.END, null, DroolsEditorType.KEYWORD);
        if (state.failed)
            return null;
    } catch (RecognitionException re) {
        reportError(re);
    } finally {
        helper.end(EntryPointDeclarationDescrBuilder.class, declare);
    }
    return (declare != null) ? declare.getDescr() : null;
}
Also used : EntryPointDeclarationDescrBuilder(org.drools.compiler.lang.api.EntryPointDeclarationDescrBuilder) 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