use of org.projectnessie.cel.common.Location in project cel-java by projectnessie.
the class Helper method id.
long id(Object ctx) {
Location location;
if (ctx instanceof ParserRuleContext) {
Token token = ((ParserRuleContext) ctx).start;
location = source.newLocation(token.getLine(), token.getCharPositionInLine());
} else if (ctx instanceof Token) {
Token token = (Token) ctx;
location = source.newLocation(token.getLine(), token.getCharPositionInLine());
} else if (ctx instanceof Location) {
location = (Location) ctx;
} else {
// This should only happen if the ctx is nil
return -1L;
}
long id = nextID;
positions.put(id, source.locationOffset(location));
nextID++;
return id;
}
use of org.projectnessie.cel.common.Location in project cel-java by projectnessie.
the class Macro method makeQuantifier.
static Expr makeQuantifier(QuantifierKind kind, ExprHelper eh, Expr target, List<Expr> args) {
String v = extractIdent(args.get(0));
if (v == null) {
Location location = eh.offsetLocation(args.get(0).getId());
throw new ErrorWithLocation(location, "argument must be a simple name");
}
Supplier<Expr> accuIdent = () -> eh.ident(AccumulatorName);
Expr init;
Expr condition;
Expr step;
Expr result;
switch(kind) {
case quantifierAll:
init = eh.literalBool(true);
condition = eh.globalCall(Operator.NotStrictlyFalse.id, accuIdent.get());
step = eh.globalCall(Operator.LogicalAnd.id, accuIdent.get(), args.get(1));
result = accuIdent.get();
break;
case quantifierExists:
init = eh.literalBool(false);
condition = eh.globalCall(Operator.NotStrictlyFalse.id, eh.globalCall(Operator.LogicalNot.id, accuIdent.get()));
step = eh.globalCall(Operator.LogicalOr.id, accuIdent.get(), args.get(1));
result = accuIdent.get();
break;
case quantifierExistsOne:
Expr zeroExpr = eh.literalInt(0);
Expr oneExpr = eh.literalInt(1);
init = zeroExpr;
condition = eh.literalBool(true);
step = eh.globalCall(Operator.Conditional.id, args.get(1), eh.globalCall(Operator.Add.id, accuIdent.get(), oneExpr), accuIdent.get());
result = eh.globalCall(Operator.Equals.id, accuIdent.get(), oneExpr);
break;
default:
throw new ErrorWithLocation(null, String.format("unrecognized quantifier '%s'", kind));
}
return eh.fold(v, target, AccumulatorName, init, condition, step, result);
}
Aggregations