Search in sources :

Example 36 with RecognitionException

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

the class DRL5Parser method annotation.

/* ------------------------------------------------------------------------------------------------
  *                         ANNOTATION
  * ------------------------------------------------------------------------------------------------ */
/**
 * annotation := fullAnnotation | AT ID chunk_(_)?
 */
private 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, DRL5Lexer.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) == DRL5Lexer.LEFT_PAREN) {
                    String value = chunk(DRL5Lexer.LEFT_PAREN, DRL5Lexer.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 37 with RecognitionException

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

the class DRL5Parser method namedConsequence.

/**
 * namedConsequence := THEN LEFT_SQUARE ID RIGHT_SQUARE chunk
 * @param rule
 */
public void namedConsequence(RuleDescrBuilder rule) {
    try {
        match(input, DRL5Lexer.ID, DroolsSoftKeywords.THEN, null, DroolsEditorType.KEYWORD);
        match(input, DRL5Lexer.LEFT_SQUARE, null, null, DroolsEditorType.SYMBOL);
        Token label = match(input, DRL5Lexer.ID, null, null, DroolsEditorType.SYMBOL);
        int first = input.index();
        match(input, DRL5Lexer.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 38 with RecognitionException

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

the class JavaExprAnalyzerTest method testAnalyzeBlock.

@Test
public void testAnalyzeBlock() {
    JavaExprAnalyzer analyzer = new JavaExprAnalyzer();
    String codeBlock = "int x;\n" + "Cheese cheese = new Cheese();\n" + "for( Iterator it = list.iterator(); it.hasNext(); ) {\n" + "    int shouldNotBeIncluded = 1;\n" + "}\n" + "{\n" + "    String anotherNonTopLevelVar = \"test\";\n" + "}\n" + "double thisIsAGoodVar = 0;\n" + "method();\n";
    try {
        JavaAnalysisResult analysis = analyzer.analyzeBlock(codeBlock, new BoundIdentifiers(new HashMap<String, Class<?>>(), null));
        Set<String> vars = analysis.getLocalVariables();
        assertEquals(3, vars.size());
        assertTrue(vars.contains("x"));
        assertTrue(vars.contains("cheese"));
        assertTrue(vars.contains("thisIsAGoodVar"));
    } catch (RecognitionException e) {
        e.printStackTrace();
        fail("Not supposed to raise exception: " + e.getMessage());
    }
}
Also used : HashMap(java.util.HashMap) RecognitionException(org.antlr.runtime.RecognitionException) BoundIdentifiers(org.drools.compiler.compiler.BoundIdentifiers) Test(org.junit.Test)

Example 39 with RecognitionException

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

the class DSLMapParser method statement.

// $ANTLR start "statement"
// src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:111:1: statement : ( entry | EOL !);
public final DSLMapParser.statement_return statement() throws RecognitionException {
    DSLMapParser.statement_return retval = new DSLMapParser.statement_return();
    retval.start = input.LT(1);
    Object root_0 = null;
    Token EOL3 = null;
    ParserRuleReturnScope entry2 = null;
    Object EOL3_tree = null;
    try {
        // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:112:5: ( entry | EOL !)
        int alt2 = 2;
        int LA2_0 = input.LA(1);
        if ((LA2_0 == LEFT_SQUARE)) {
            alt2 = 1;
        } else if ((LA2_0 == EOL)) {
            alt2 = 2;
        } else {
            if (state.backtracking > 0) {
                state.failed = true;
                return retval;
            }
            NoViableAltException nvae = new NoViableAltException("", 2, 0, input);
            throw nvae;
        }
        switch(alt2) {
            case 1:
                // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:112:7: entry
                {
                    root_0 = (Object) adaptor.nil();
                    pushFollow(FOLLOW_entry_in_statement306);
                    entry2 = entry();
                    state._fsp--;
                    if (state.failed)
                        return retval;
                    if (state.backtracking == 0)
                        adaptor.addChild(root_0, entry2.getTree());
                }
                break;
            case 2:
                // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:113:7: EOL !
                {
                    root_0 = (Object) adaptor.nil();
                    EOL3 = (Token) match(input, EOL, FOLLOW_EOL_in_statement314);
                    if (state.failed)
                        return retval;
                }
                break;
        }
        retval.stop = input.LT(-1);
        if (state.backtracking == 0) {
            retval.tree = (Object) adaptor.rulePostProcessing(root_0);
            adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
        retval.tree = (Object) adaptor.errorNode(input, retval.start, input.LT(-1), re);
    } finally {
    // do for sure before leaving
    }
    return retval;
}
Also used : NoViableAltException(org.antlr.runtime.NoViableAltException) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) ParserRuleReturnScope(org.antlr.runtime.ParserRuleReturnScope) RecognitionException(org.antlr.runtime.RecognitionException)

Example 40 with RecognitionException

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

the class DSLMapParser method any_key.

// $ANTLR start "any_key"
// src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:266:1: any_key :{...}?value= LITERAL -> VT_ANY[$value] ;
public final DSLMapParser.any_key_return any_key() throws RecognitionException {
    DSLMapParser.any_key_return retval = new DSLMapParser.any_key_return();
    retval.start = input.LT(1);
    Object root_0 = null;
    Token value = null;
    Object value_tree = null;
    RewriteRuleTokenStream stream_LITERAL = new RewriteRuleTokenStream(adaptor, "token LITERAL");
    try {
        // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:267:5: ({...}?value= LITERAL -> VT_ANY[$value] )
        // src/main/resources/org/drools/compiler/lang/dsl/DSLMap.g:267:7: {...}?value= LITERAL
        {
            if (!((validateIdentifierKey("*")))) {
                if (state.backtracking > 0) {
                    state.failed = true;
                    return retval;
                }
                throw new FailedPredicateException(input, "any_key", "validateIdentifierKey(\"*\")");
            }
            value = (Token) match(input, LITERAL, FOLLOW_LITERAL_in_any_key1695);
            if (state.failed)
                return retval;
            if (state.backtracking == 0)
                stream_LITERAL.add(value);
            // wildcard labels:
            if (state.backtracking == 0) {
                retval.tree = root_0;
                RewriteRuleSubtreeStream stream_retval = new RewriteRuleSubtreeStream(adaptor, "rule retval", retval != null ? retval.getTree() : null);
                root_0 = (Object) adaptor.nil();
                // 268:5: -> VT_ANY[$value]
                {
                    adaptor.addChild(root_0, (Object) adaptor.create(VT_ANY, value));
                }
                retval.tree = root_0;
            }
        }
        retval.stop = input.LT(-1);
        if (state.backtracking == 0) {
            retval.tree = (Object) adaptor.rulePostProcessing(root_0);
            adaptor.setTokenBoundaries(retval.tree, retval.start, retval.stop);
        }
    } catch (RecognitionException re) {
        reportError(re);
        recover(input, re);
        retval.tree = (Object) adaptor.errorNode(input, retval.start, input.LT(-1), re);
    } finally {
    // do for sure before leaving
    }
    return retval;
}
Also used : RewriteRuleTokenStream(org.antlr.runtime.tree.RewriteRuleTokenStream) RewriteRuleSubtreeStream(org.antlr.runtime.tree.RewriteRuleSubtreeStream) Token(org.antlr.runtime.Token) CommonToken(org.antlr.runtime.CommonToken) FailedPredicateException(org.antlr.runtime.FailedPredicateException) 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