Search in sources :

Example 1 with Location

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;
}
Also used : ParserRuleContext(org.projectnessie.cel.shaded.org.antlr.v4.runtime.ParserRuleContext) Token(org.projectnessie.cel.shaded.org.antlr.v4.runtime.Token) Location(org.projectnessie.cel.common.Location)

Example 2 with Location

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);
}
Also used : ErrorWithLocation(org.projectnessie.cel.common.ErrorWithLocation) Expr(com.google.api.expr.v1alpha1.Expr) Location(org.projectnessie.cel.common.Location) ErrorWithLocation(org.projectnessie.cel.common.ErrorWithLocation)

Aggregations

Location (org.projectnessie.cel.common.Location)2 Expr (com.google.api.expr.v1alpha1.Expr)1 ErrorWithLocation (org.projectnessie.cel.common.ErrorWithLocation)1 ParserRuleContext (org.projectnessie.cel.shaded.org.antlr.v4.runtime.ParserRuleContext)1 Token (org.projectnessie.cel.shaded.org.antlr.v4.runtime.Token)1