com.sap.cds.impl.parser.ExpressionParser Maven / Gradle / Ivy
/*******************************************************************
* © 2019 SAP SE or an SAP affiliate company. All rights reserved. *
*******************************************************************/
package com.sap.cds.impl.parser;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.google.common.collect.Lists;
import com.sap.cds.impl.parser.builder.ExpressionBuilder;
import com.sap.cds.ql.cqn.CqnExpression;
import com.sap.cds.ql.cqn.CqnPredicate;
import com.sap.cds.ql.cqn.CqnSyntaxException;
import com.sap.cds.ql.cqn.CqnToken;
public class ExpressionParser {
private ExpressionParser() {
}
public static CqnExpression parseExpression(ArrayNode xpr) {
if (xpr != null) {
return ExpressionBuilder.create(parseTokens(xpr)).xpr();
}
return null;
}
public static CqnPredicate parsePredicate(String cqnExpr) {
try {
ArrayNode expr = CqnParser.getDocumentContext(cqnExpr).read("$");
return parsePredicate(expr);
} catch (Exception ex) {
throw new CqnSyntaxException("Error parsing " + cqnExpr, ex);
}
}
public static List parseTokens(String cqnExpr) {
ArrayNode expr = CqnParser.getDocumentContext(cqnExpr).read("$");
return parseTokens(expr);
}
public static CqnPredicate parsePredicate(JsonNode expr) {
if (expr == null) {
return null;
}
if (expr.isArray()) {
List tokens = parseTokens((ArrayNode) expr);
return new ExprParser().parsePredicate(tokens);
}
return ExpressionBuilder.create(TokenParser.parse(expr)).predicate();
}
public static List parseTokens(ArrayNode arrayNode) {
List tokens = Lists.newArrayList();
if (arrayNode != null) {
arrayNode.forEach(n -> tokens.add(TokenParser.parse(n)));
}
return tokens;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy