Search in sources :

Example 1 with RecognizerSharedState

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

the class DrlExprParser method parse.

/**
 * Parse an expression from text
 */
public ConstraintConnectiveDescr parse(final String text) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream(lexer);
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper(input, state, languageLevel);
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr(true);
        // setting initial value just in case
        parser.setLeftMostExpr(null);
        BaseDescr expr = parser.conditionalOrExpression();
        if (expr != null && !parser.hasErrors()) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge(expr);
        }
    } catch (RecognitionException e) {
        helper.reportError(e);
    }
    return constraint;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) DRLExpressions(org.drools.compiler.lang.DRLExpressions) DRLLexer(org.drools.compiler.lang.DRLLexer) ParserHelper(org.drools.compiler.lang.ParserHelper) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) BaseDescr(org.drools.compiler.lang.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.compiler.lang.descr.ConstraintConnectiveDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Example 2 with RecognizerSharedState

use of org.antlr.runtime.RecognizerSharedState in project n4js by eclipse.

the class SemicolonInjectionHelper method promoteEOL.

/**
 * <p>
 * Promotes EOL which may lead to an automatically inserted semicolon. This is probably the most important method
 * for automatic semicolon insertion, as it is only possible to insert a semicolon in case of line breaks (even if
 * they are hidden in a multi-line comment!).
 * </p>
 */
public static void promoteEOL(Callback callback) {
    RecognizerSharedState state = callback.getState();
    TokenStream input = callback.getInput();
    // Don't promote EOL if there was a syntax error at EOF
    if (state.lastErrorIndex == input.size()) {
        return;
    }
    // Get current token and its type (the possibly offending token).
    Token prev = input.LT(-1);
    Token next = input.LT(1);
    int la = next.getType();
    // A ML_COMMENT gets promoted when it contains an EOL.
    for (int idx = prev == null ? 0 : prev.getTokenIndex() + 1, max = la == Token.EOF ? input.size() : next.getTokenIndex(); idx < max; idx++) {
        Token lt = input.get(idx);
        if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
            // On channel token found: stop scanning (previously promoted)
            break;
        } else if (isSemicolonEquivalent(lt)) {
            // We found our EOL: promote the token to on channel, position the input on it and reset the rule
            // start.
            lt.setChannel(Token.DEFAULT_CHANNEL);
            input.seek(idx);
            break;
        }
    }
}
Also used : TokenStream(org.antlr.runtime.TokenStream) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) Token(org.antlr.runtime.Token)

Example 3 with RecognizerSharedState

use of org.antlr.runtime.RecognizerSharedState in project n4js by eclipse.

the class SemicolonInjectionHelper method recover.

/**
 * Recover from an error found on the input stream. This is for {@link NoViableAltException} and
 * {@link MismatchedTokenException}. If you enable single token insertion and deletion, this will usually not handle
 * mismatched symbol exceptions but there could be a mismatched token that the
 * {@link Parser#match(IntStream, int, BitSet) match} routine could not recover from.
 */
public static void recover(IntStream inputStream, RecognitionException re, Callback callback) {
    RecognizerSharedState state = callback.getState();
    if (re instanceof MismatchedTokenException) {
        // We expected a specific token
        // if that is not a semicolon, ASI is pointless, perform normal recovery
        int expecting = ((MismatchedTokenException) re).expecting;
        if (expecting != InternalN4JSParser.Semicolon) {
            // delete ASI message, a previously added ASI may fix too much! cf.
            callback.discardError();
            // IDEBUG-215
            callback.recoverBase(inputStream, re);
            return;
        }
    }
    // System.out.println("Line: " + re.line + ":" + re.index);
    int unexpectedTokenType = re.token.getType();
    if (!followedBySemicolon(state, callback.getRecoverySets(), re.index) || isOffendingToken(unexpectedTokenType)) {
        callback.recoverBase(inputStream, re);
    } else {
        int la = inputStream.LA(1);
        TokenStream casted = (TokenStream) inputStream;
        if (!isOffendingToken(la)) {
            // previous on channel token.
            for (int ix = re.token.getTokenIndex() - 1; ix > 0; ix--) {
                Token lt = casted.get(ix);
                if (lt.getChannel() == Token.DEFAULT_CHANNEL) {
                    // On channel token found: stop scanning.
                    callback.recoverBase(inputStream, re);
                    return;
                } else if (lt.getType() == InternalN4JSParser.RULE_EOL) {
                    // rule start.
                    if (!callback.allowASI(re)) {
                        callback.recoverBase(inputStream, re);
                        return;
                    }
                    if (!findCommaBeforeEOL(casted, ix)) {
                        callback.addASIMessage();
                        return;
                    }
                } else if (lt.getType() == InternalN4JSParser.RULE_ML_COMMENT) {
                    String tokenText = lt.getText();
                    if (!findCommaBeforeEOL(casted, ix) && (tokenText.indexOf('\n', 2) >= 2 || tokenText.indexOf('\r', 2) >= 2)) {
                        callback.addASIMessage();
                        return;
                    }
                }
            }
            callback.recoverBase(inputStream, re);
        }
    }
}
Also used : TokenStream(org.antlr.runtime.TokenStream) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) MismatchedTokenException(org.antlr.runtime.MismatchedTokenException) Token(org.antlr.runtime.Token)

Example 4 with RecognizerSharedState

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

the class DrlExprParser method parse.

/**
 * Parse an expression from text
 */
public ConstraintConnectiveDescr parse(final String text) {
    ConstraintConnectiveDescr constraint = null;
    try {
        DRLLexer lexer = DRLFactory.getDRLLexer(new ANTLRStringStream(text), languageLevel);
        CommonTokenStream input = new CommonTokenStream(lexer);
        RecognizerSharedState state = new RecognizerSharedState();
        helper = new ParserHelper(input, state, languageLevel);
        DRLExpressions parser = DRLFactory.getDRLExpressions(input, state, helper, languageLevel);
        parser.setBuildDescr(true);
        // setting initial value just in case
        parser.setLeftMostExpr(null);
        BaseDescr expr = parser.conditionalOrExpression();
        if (expr != null && !parser.hasErrors()) {
            constraint = ConstraintConnectiveDescr.newAnd();
            constraint.addOrMerge(expr);
        }
    } catch (RecognitionException e) {
        helper.reportError(e);
    }
    return constraint;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) DRLExpressions(org.drools.drl.parser.lang.DRLExpressions) DRLLexer(org.drools.drl.parser.lang.DRLLexer) ParserHelper(org.drools.drl.parser.lang.ParserHelper) RecognizerSharedState(org.antlr.runtime.RecognizerSharedState) BaseDescr(org.drools.drl.ast.descr.BaseDescr) ConstraintConnectiveDescr(org.drools.drl.ast.descr.ConstraintConnectiveDescr) RecognitionException(org.antlr.runtime.RecognitionException)

Aggregations

RecognizerSharedState (org.antlr.runtime.RecognizerSharedState)4 ANTLRStringStream (org.antlr.runtime.ANTLRStringStream)2 CommonTokenStream (org.antlr.runtime.CommonTokenStream)2 RecognitionException (org.antlr.runtime.RecognitionException)2 Token (org.antlr.runtime.Token)2 TokenStream (org.antlr.runtime.TokenStream)2 MismatchedTokenException (org.antlr.runtime.MismatchedTokenException)1 DRLExpressions (org.drools.compiler.lang.DRLExpressions)1 DRLLexer (org.drools.compiler.lang.DRLLexer)1 ParserHelper (org.drools.compiler.lang.ParserHelper)1 BaseDescr (org.drools.compiler.lang.descr.BaseDescr)1 ConstraintConnectiveDescr (org.drools.compiler.lang.descr.ConstraintConnectiveDescr)1 BaseDescr (org.drools.drl.ast.descr.BaseDescr)1 ConstraintConnectiveDescr (org.drools.drl.ast.descr.ConstraintConnectiveDescr)1 DRLExpressions (org.drools.drl.parser.lang.DRLExpressions)1 DRLLexer (org.drools.drl.parser.lang.DRLLexer)1 ParserHelper (org.drools.drl.parser.lang.ParserHelper)1