Search in sources :

Example 6 with RecognitionException

use of org.antlr.runtime.RecognitionException in project cuba by cuba-platform.

the class QueryTransformerAstBased method addJoinAndWhere.

@Override
public void addJoinAndWhere(String join, String where) {
    EntityReferenceInferer inferer = new EntityReferenceInferer(getMainEntityName());
    EntityReference ref = inferer.infer(getQueryTransformer());
    if (where.contains("{E}")) {
        where = ref.replaceEntries(where, "\\{E\\}");
    }
    if (join.contains("{E}")) {
        join = ref.replaceEntries(join, "\\{E\\}");
    }
    String[] strings = join.split(",");
    join = strings[0];
    try {
        if (StringUtils.isNotBlank(join)) {
            List<JoinVariableNode> joinVariableNodes = Parser.parseJoinClause(join);
            boolean firstJoin = true;
            for (JoinVariableNode joinVariableNode : joinVariableNodes) {
                getQueryTransformer().mixinJoinIntoTree(joinVariableNode, ref, firstJoin);
                firstJoin = false;
            }
        }
        for (int i = 1; i < strings.length; i++) {
            CommonTree selectionSource = Parser.parseSelectionSource(strings[i]);
            getQueryTransformer().addSelectionSource(selectionSource);
        }
        CommonTree whereTree = Parser.parseWhereClause("where " + where);
        addWhere(whereTree, ref, false);
    } catch (RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : CommonTree(org.antlr.runtime.tree.CommonTree) JoinVariableNode(com.haulmont.cuba.core.sys.jpql.tree.JoinVariableNode) RecognitionException(org.antlr.runtime.RecognitionException) JPA2RecognitionException(com.haulmont.cuba.core.sys.jpql.antlr2.JPA2RecognitionException)

Example 7 with RecognitionException

use of org.antlr.runtime.RecognitionException in project drill by apache.

the class TestBuilder method parsePath.

// modified code from SchemaPath.De class. This should be used sparingly and only in tests if absolutely needed.
public static SchemaPath parsePath(String path) {
    try {
        ExprLexer lexer = new ExprLexer(new ANTLRStringStream(path));
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        ExprParser parser = new ExprParser(tokens);
        ExprParser.parse_return ret = parser.parse();
        if (ret.e instanceof SchemaPath) {
            return (SchemaPath) ret.e;
        } else {
            throw new IllegalStateException("Schema path is not a valid format.");
        }
    } catch (RecognitionException e) {
        throw new RuntimeException(e);
    }
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) CommonTokenStream(org.antlr.runtime.CommonTokenStream) SchemaPath(org.apache.drill.common.expression.SchemaPath) ExprLexer(org.apache.drill.common.expression.parser.ExprLexer) ExprParser(org.apache.drill.common.expression.parser.ExprParser) RecognitionException(org.antlr.runtime.RecognitionException)

Example 8 with RecognitionException

use of org.antlr.runtime.RecognitionException in project Palladio-Editors-Sirius by PalladioSimulator.

the class PCMServices method validExpression.

/**
 * Parses an stochastic expression to determine whether it is valid.
 *
 * @param the
 *            expressionString
 * @return the validity
 */
private boolean validExpression(final String expressionString) {
    final MyPCMStoExLexer lexer = new MyPCMStoExLexer(new ANTLRStringStream(expressionString));
    final MyPCMStoExParser parser = new MyPCMStoExParser(new CommonTokenStream(lexer));
    try {
        parser.expression();
    } catch (final RecognitionException e1) {
        return false;
    }
    if (lexer.hasErrors() || parser.hasErrors()) {
        return false;
    }
    return true;
}
Also used : ANTLRStringStream(org.antlr.runtime.ANTLRStringStream) MyPCMStoExParser(org.palladiosimulator.pcm.stochasticexpressions.parser.MyPCMStoExParser) CommonTokenStream(org.antlr.runtime.CommonTokenStream) MyPCMStoExLexer(org.palladiosimulator.pcm.stochasticexpressions.parser.MyPCMStoExLexer) RecognitionException(org.antlr.runtime.RecognitionException)

Example 9 with RecognitionException

use of org.antlr.runtime.RecognitionException in project hive by apache.

the class ParseDriver method parse.

/**
 * Parses a command, optionally assigning the parser's token stream to the
 * given context.
 *
 * @param command
 *          command to parse
 *
 * @param ctx
 *          context with which to associate this parser's token stream, or
 *          null if either no context is available or the context already has
 *          an existing stream
 *
 * @return parsed AST
 */
public ASTNode parse(String command, Context ctx, String viewFullyQualifiedName) throws ParseException {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Parsing command: " + command);
    }
    HiveLexerX lexer = new HiveLexerX(new ANTLRNoCaseStringStream(command));
    TokenRewriteStream tokens = new TokenRewriteStream(lexer);
    if (ctx != null) {
        if (viewFullyQualifiedName == null) {
            // Top level query
            ctx.setTokenRewriteStream(tokens);
        } else {
            // It is a view
            ctx.addViewTokenRewriteStream(viewFullyQualifiedName, tokens);
        }
        lexer.setHiveConf(ctx.getConf());
    }
    HiveParser parser = new HiveParser(tokens);
    if (ctx != null) {
        parser.setHiveConf(ctx.getConf());
    }
    parser.setTreeAdaptor(adaptor);
    HiveParser.statement_return r = null;
    try {
        r = parser.statement();
    } catch (RecognitionException e) {
        e.printStackTrace();
        throw new ParseException(parser.errors);
    }
    if (lexer.getErrors().size() == 0 && parser.errors.size() == 0) {
        LOG.debug("Parse Completed");
    } else if (lexer.getErrors().size() != 0) {
        throw new ParseException(lexer.getErrors());
    } else {
        throw new ParseException(parser.errors);
    }
    ASTNode tree = (ASTNode) r.getTree();
    tree.setUnknownTokenBoundaries();
    return tree;
}
Also used : TokenRewriteStream(org.antlr.runtime.TokenRewriteStream) RecognitionException(org.antlr.runtime.RecognitionException)

Example 10 with RecognitionException

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

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