Search in sources :

Example 86 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project aic-expresso by aic-sri-international.

the class AntlrGrinderParserWrapper method parse.

@Override
public Expression parse(String string, Parser.ErrorListener parserEerrorListener) {
    Expression result = null;
    try {
        AntlrErrorListener antlrErrorListener = new AntlrErrorListener(parserEerrorListener);
        ANTLRInputStream input = new ANTLRInputStream(string);
        AntlrGrinderLexer lexer = new AntlrGrinderLexer(input);
        CommonTokenStream tokens = new CommonTokenStream(lexer);
        AntlrGrinderParser parser = new AntlrGrinderParser(tokens);
        lexer.removeErrorListeners();
        parser.removeErrorListeners();
        lexer.addErrorListener(antlrErrorListener);
        parser.addErrorListener(antlrErrorListener);
        ParseTree tree = parser.expression();
        boolean eof = parser.getInputStream().LA(1) == Recognizer.EOF;
        if (!antlrErrorListener.errorsDetected) {
            if (!eof) {
                System.err.println("Unable to parse the complete input expression: " + input);
            } else {
                lexer.removeErrorListeners();
                parser.removeErrorListeners();
                ExpressionVisitor expressionVisitor = new ExpressionVisitor(getRandomPredicatesSignatures());
                result = expressionVisitor.visit(tree);
            }
        }
    } catch (RecognitionException re) {
        re.printStackTrace();
    } catch (RuntimeException re) {
        re.printStackTrace();
    }
    return result;
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) Expression(com.sri.ai.expresso.api.Expression) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 87 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project aic-praise by aic-sri-international.

the class HOGMQueryRunner method query.

public List<HOGMQueryResult> query() {
    List<HOGMQueryResult> result = new ArrayList<>();
    Expression queryExpr = null;
    //
    ParsedHOGModel parsedModel = null;
    for (String query : queries) {
        long startQuery = System.currentTimeMillis();
        List<HOGMQueryError> errors = new ArrayList<>();
        try {
            if (model == null || model.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, "Model not specified", 0, 0, 0));
            }
            if (query == null || query.trim().equals("")) {
                errors.add(new HOGMQueryError(HOGMQueryError.Context.QUERY, "Query not specified", 0, 0, 0));
            }
            if (errors.size() == 0) {
                HOGMParserWrapper parser = new HOGMParserWrapper();
                if (parsedModel == null) {
                    parsedModel = parser.parseModel(model, new QueryErrorListener(HOGMQueryError.Context.MODEL, errors));
                }
                queryExpr = parser.parseTerm(query, new QueryErrorListener(HOGMQueryError.Context.QUERY, errors));
                if (errors.size() == 0) {
                    FactorsAndTypes factorsAndTypes = new ExpressionFactorsAndTypes(parsedModel);
                    if (!canceled) {
                        inferencer = new InferenceForFactorGraphAndEvidence(factorsAndTypes, false, null, true, getOptionalTheory());
                        startQuery = System.currentTimeMillis();
                        Expression marginal = inferencer.solve(queryExpr);
                        result.add(new HOGMQueryResult(query, queryExpr, parsedModel, marginal, System.currentTimeMillis() - startQuery));
                    }
                }
            }
        } catch (RecognitionException re) {
            errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, re.getMessage(), re.getOffendingToken().getLine(), re.getOffendingToken().getStartIndex(), re.getOffendingToken().getStopIndex()));
        } catch (UnableToParseAllTheInputError utpai) {
            errors.add(new HOGMQueryError(utpai));
        } catch (HOGModelException me) {
            me.getErrors().forEach(modelError -> {
                String inStatement = modelError.getInStatementInfo().statement.toString();
                String inSource = modelError.getInStatementInfo().sourceText;
                String inSubStatement = modelError.getMessage();
                String inInfo = "";
                if (inSubStatement.equals("") || inSubStatement.equals(inSource)) {
                    inInfo = " in '" + inStatement + "'";
                } else {
                    inInfo = " ('" + inSubStatement + "') in '" + inStatement + "'";
                }
                if (!inSource.replaceAll(" ", "").replaceAll(";", "").equals(inStatement.replaceAll(" ", ""))) {
                    inInfo = inInfo + " derived from '" + inSource + "'";
                }
                errors.add(new HOGMQueryError(HOGMQueryError.Context.MODEL, modelError.getErrorType().formattedMessage() + inInfo, modelError.getInStatementInfo().line, modelError.getInStatementInfo().startIndex, modelError.getInStatementInfo().endIndex));
            });
        } catch (Throwable t) {
            // Unexpected
            errors.add(new HOGMQueryError(t));
        }
        if (errors.size() > 0) {
            result.add(new HOGMQueryResult(query, queryExpr, parsedModel, errors, System.currentTimeMillis() - startQuery));
        }
    }
    return result;
}
Also used : HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Expressions(com.sri.ai.expresso.helper.Expressions) Parser(com.sri.ai.expresso.api.Parser) ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) Expression(com.sri.ai.expresso.api.Expression) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) Context(com.sri.ai.grinder.sgdpllt.api.Context) HOGModelException(com.sri.ai.praise.model.v1.HOGModelException) Theory(com.sri.ai.grinder.sgdpllt.api.Theory) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) ArrayList(java.util.ArrayList) Beta(com.google.common.annotations.Beta) GrinderUtil(com.sri.ai.grinder.helper.GrinderUtil) List(java.util.List) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) RecognitionException(org.antlr.v4.runtime.RecognitionException) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) HOGMSortDeclaration(com.sri.ai.praise.model.v1.HOGMSortDeclaration) Collections(java.util.Collections) ParsedHOGModel(com.sri.ai.praise.model.v1.hogm.antlr.ParsedHOGModel) FactorsAndTypes(com.sri.ai.praise.sgsolver.solver.FactorsAndTypes) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) ArrayList(java.util.ArrayList) ExpressionFactorsAndTypes(com.sri.ai.praise.sgsolver.solver.ExpressionFactorsAndTypes) HOGMParserWrapper(com.sri.ai.praise.model.v1.hogm.antlr.HOGMParserWrapper) InferenceForFactorGraphAndEvidence(com.sri.ai.praise.sgsolver.solver.InferenceForFactorGraphAndEvidence) Expression(com.sri.ai.expresso.api.Expression) UnableToParseAllTheInputError(com.sri.ai.praise.model.v1.hogm.antlr.UnableToParseAllTheInputError) RecognitionException(org.antlr.v4.runtime.RecognitionException)

Example 88 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project presto by prestodb.

the class TypeCalculation method calculateLiteralValue.

public static Long calculateLiteralValue(String calculation, Map<String, Long> inputs) {
    try {
        ParserRuleContext tree = parseTypeCalculation(calculation);
        CalculateTypeVisitor visitor = new CalculateTypeVisitor(inputs);
        BigInteger result = visitor.visit(tree);
        return result.longValueExact();
    } catch (StackOverflowError e) {
        throw new ParsingException("Type calculation is too large (stack overflow while parsing)");
    }
}
Also used : ParserRuleContext(org.antlr.v4.runtime.ParserRuleContext) ParsingException(com.facebook.presto.sql.parser.ParsingException) BigInteger(java.math.BigInteger)

Example 89 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project SSM by Intel-bigdata.

the class TestSmartRuleParser method parseAndExecuteRule.

private void parseAndExecuteRule(String rule) throws Exception {
    System.out.println("--> " + rule);
    InputStream input = new ByteArrayInputStream(rule.getBytes());
    ANTLRInputStream antlrInput = new ANTLRInputStream(input);
    SmartRuleLexer lexer = new SmartRuleLexer(antlrInput);
    CommonTokenStream tokens = new CommonTokenStream(lexer);
    SmartRuleParser parser = new SmartRuleParser(tokens);
    parser.removeErrorListeners();
    parser.addErrorListener(new SSMRuleErrorListener());
    ParseTree tree = parser.ssmrule();
    System.out.println("Parser tree: " + tree.toStringTree(parser));
    System.out.println("Total number of errors: " + parseErrors.size());
    SmartRuleVisitTranslator visitor = new SmartRuleVisitTranslator();
    visitor.visit(tree);
    System.out.println("\nQuery:");
    TranslateResult result = visitor.generateSql();
    int index = 1;
    for (String sql : result.getSqlStatements()) {
        System.out.println("" + index + ". " + sql);
        index++;
    }
    if (parseErrors.size() > 0) {
        throw new IOException("Error while parse rule");
    }
    ExecutionContext ctx = new ExecutionContext();
    ctx.setProperty(ExecutionContext.RULE_ID, 2016);
    DBAdapter dbAdapter = new DBAdapter(TestDBUtil.getTestDBInstance());
    RuleQueryExecutor qe = new RuleQueryExecutor(null, ctx, result, dbAdapter);
    List<String> paths = qe.executeFileRuleQuery();
    index = 1;
    System.out.println("\nFiles:");
    for (String path : paths) {
        System.out.println("" + index + ". " + path);
        index++;
    }
}
Also used : CommonTokenStream(org.antlr.v4.runtime.CommonTokenStream) SmartRuleParser(org.smartdata.server.rule.parser.SmartRuleParser) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) SmartRuleLexer(org.smartdata.server.rule.parser.SmartRuleLexer) SmartRuleVisitTranslator(org.smartdata.server.rule.parser.SmartRuleVisitTranslator) IOException(java.io.IOException) DBAdapter(org.smartdata.server.metastore.DBAdapter) ExecutionContext(org.smartdata.server.metastore.ExecutionContext) ByteArrayInputStream(java.io.ByteArrayInputStream) TranslateResult(org.smartdata.server.rule.parser.TranslateResult) ANTLRInputStream(org.antlr.v4.runtime.ANTLRInputStream) ParseTree(org.antlr.v4.runtime.tree.ParseTree)

Example 90 with Result

use of org.antlr.v4.misc.EscapeSequenceParsing.Result in project oxTrust by GluuFederation.

the class MainScimFilterVisitor method visitEXPR_AND_EXPR.

/**
     * {@InheritDoc}
     * <p>
     * <p>The default implementation returns the result of calling
     * {@link #visitChildren} on {@code ctx}.</p>
     *
     * @param ctx
     */
@Override
public String visitEXPR_AND_EXPR(ScimFilterParser.EXPR_AND_EXPRContext ctx) {
    logger.info(" IN MainScimFilterVisitor.visitEXPR_AND_EXPR()... ");
    boolean isMultivalued = false;
    ParseTree parent = ctx.getParent();
    while (parent != null) {
        if (parent.getClass().getSimpleName().equalsIgnoreCase(ScimFilterParser.LBRAC_EXPR_RBRACContext.class.getSimpleName())) {
            logger.info("********** PARENT = " + parent.getClass().getSimpleName());
            isMultivalued = true;
            break;
        } else {
            parent = parent.getParent();
        }
    }
    if (!isMultivalued) {
        String leftText = FilterUtil.stripScim2Schema(ctx.expression(0).getChild(0).getText());
        String rightText = FilterUtil.stripScim2Schema(ctx.expression(1).getChild(0).getText());
        String[] leftTextTokens = leftText.split("\\.");
        String[] rightTextTokens = rightText.split("\\.");
        if (leftTextTokens[0].equalsIgnoreCase(rightTextTokens[0]) && !leftTextTokens[0].equalsIgnoreCase(Name.class.getSimpleName())) {
            isMultivalued = true;
        }
    }
    String leftExp = visit(ctx.expression(0));
    String rightExp = visit(ctx.expression(1));
    StringBuilder result = new StringBuilder("");
    if (isMultivalued && !leftExp.startsWith("!(") && !rightExp.startsWith("!(")) {
        if (!leftExp.startsWith("&(") || !leftExp.startsWith("|(")) {
            String[] leftExpTokens = leftExp.split("\\=");
            String[] rightExpTokens = rightExp.split("\\=");
            if (leftExpTokens[0].equalsIgnoreCase(rightExpTokens[0])) {
                result.append("&(|(");
                result.append(leftExpTokens[0]);
                result.append("=");
                result.append(leftExpTokens[1]);
                result.append("*");
                result.append(rightExpTokens[1]);
                result.append(")");
                result.append("(");
                result.append(leftExpTokens[0]);
                result.append("=");
                result.append(rightExpTokens[1]);
                result.append("*");
                result.append(leftExpTokens[1]);
                result.append("))");
            } else {
                result.append(transformToLdapAndFilter(leftExp, rightExp));
            }
        } else {
            result.append(transformToLdapAndFilter(leftExp, rightExp));
        }
    } else {
        result.append(transformToLdapAndFilter(leftExp, rightExp));
    }
    logger.info("##### parsed filter = " + result.toString().replaceAll("\\*{2,}", "\\*"));
    logger.info(" LEAVING MainScimFilterVisitor.visitEXPR_AND_EXPR()... ");
    return result.toString().replaceAll("\\*{2,}", "\\*");
}
Also used : ParseTree(org.antlr.v4.runtime.tree.ParseTree) Name(org.gluu.oxtrust.model.scim2.Name)

Aggregations

Test (org.junit.Test)131 LexerGrammar (org.antlr.v4.tool.LexerGrammar)78 ANTLRInputStream (org.antlr.v4.runtime.ANTLRInputStream)52 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)49 LexerInterpreter (org.antlr.v4.runtime.LexerInterpreter)44 ATN (org.antlr.v4.runtime.atn.ATN)44 TokenStreamRewriter (org.antlr.v4.runtime.TokenStreamRewriter)38 IntervalSet (org.antlr.v4.runtime.misc.IntervalSet)38 BaseJavaTest (org.antlr.v4.test.runtime.java.BaseJavaTest)38 ParseTree (org.antlr.v4.runtime.tree.ParseTree)29 ATNState (org.antlr.v4.runtime.atn.ATNState)11 Grammar (org.antlr.v4.tool.Grammar)10 STGroupString (org.stringtemplate.v4.STGroupString)10 ByteBuffer (java.nio.ByteBuffer)8 IntBuffer (java.nio.IntBuffer)8 UTF8CodePointDecoder (org.antlr.v4.runtime.UTF8CodePointDecoder)8 BaseRuntimeTest.antlrOnString (org.antlr.v4.test.runtime.BaseRuntimeTest.antlrOnString)8 DOTGenerator (org.antlr.v4.tool.DOTGenerator)8 ParserATNFactory (org.antlr.v4.automata.ParserATNFactory)7 Rule (org.antlr.v4.tool.Rule)7