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;
}
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;
}
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)");
}
}
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++;
}
}
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,}", "\\*");
}
Aggregations