|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.common; |
| 16 | + |
| 17 | +import com.google.common.collect.ImmutableMap; |
| 18 | +import dev.cel.common.ast.CelExpr; |
| 19 | +import java.util.Objects; |
| 20 | +import java.util.Optional; |
| 21 | + |
| 22 | +/** |
| 23 | + * Package-private enumeration of Common Expression Language operators. |
| 24 | + * |
| 25 | + * <p>Equivalent to https://pkg.go.dev/github.com/google/cel-go/common/operators. |
| 26 | + */ |
| 27 | +public enum Operator { |
| 28 | + CONDITIONAL("_?_:_"), |
| 29 | + LOGICAL_AND("_&&_", "&&"), |
| 30 | + LOGICAL_OR("_||_", "||"), |
| 31 | + LOGICAL_NOT("!_", "!"), |
| 32 | + EQUALS("_==_", "=="), |
| 33 | + NOT_EQUALS("_!=_", "!="), |
| 34 | + LESS("_<_", "<"), |
| 35 | + LESS_EQUALS("_<=_", "<="), |
| 36 | + GREATER("_>_", ">"), |
| 37 | + GREATER_EQUALS("_>=_", ">="), |
| 38 | + ADD("_+_", "+"), |
| 39 | + SUBTRACT("_-_", "-"), |
| 40 | + MULTIPLY("_*_", "*"), |
| 41 | + DIVIDE("_/_", "/"), |
| 42 | + MODULO("_%_", "%"), |
| 43 | + NEGATE("-_", "-"), |
| 44 | + INDEX("_[_]"), |
| 45 | + HAS("has"), |
| 46 | + ALL("all"), |
| 47 | + EXISTS("exists"), |
| 48 | + EXISTS_ONE("exists_one"), |
| 49 | + MAP("map"), |
| 50 | + FILTER("filter"), |
| 51 | + NOT_STRICTLY_FALSE("@not_strictly_false"), |
| 52 | + IN("@in", "in"), |
| 53 | + OPTIONAL_INDEX("_[?_]"), |
| 54 | + OPTIONAL_SELECT("_?._"), |
| 55 | + @Deprecated // Prefer NOT_STRICTLY_FALSE. |
| 56 | + OLD_NOT_STRICTLY_FALSE("__not_strictly_false__"), |
| 57 | + @Deprecated // Prefer IN. |
| 58 | + OLD_IN("_in_"); |
| 59 | + |
| 60 | + private final String functionName; |
| 61 | + private final String displayName; |
| 62 | + |
| 63 | + Operator(String functionName) { |
| 64 | + this(functionName, ""); |
| 65 | + } |
| 66 | + |
| 67 | + Operator(String functionName, String displayName) { |
| 68 | + this.functionName = functionName; |
| 69 | + this.displayName = displayName; |
| 70 | + } |
| 71 | + |
| 72 | + /** Returns the mangled operator name, as used within the AST. */ |
| 73 | + public String getFunction() { |
| 74 | + return functionName; |
| 75 | + } |
| 76 | + |
| 77 | + /** Returns the unmangled operator name, as used within the source text of an expression. */ |
| 78 | + String getSymbol() { |
| 79 | + return displayName; |
| 80 | + } |
| 81 | + |
| 82 | + private static final ImmutableMap<String, Operator> OPERATORS = |
| 83 | + ImmutableMap.<String, Operator>builder() |
| 84 | + .put(ADD.getSymbol(), ADD) |
| 85 | + .put(DIVIDE.getSymbol(), DIVIDE) |
| 86 | + .put(EQUALS.getSymbol(), EQUALS) |
| 87 | + .put(GREATER.getSymbol(), GREATER) |
| 88 | + .put(GREATER_EQUALS.getSymbol(), GREATER_EQUALS) |
| 89 | + .put(IN.getSymbol(), IN) |
| 90 | + .put(LESS.getSymbol(), LESS) |
| 91 | + .put(LESS_EQUALS.getSymbol(), LESS_EQUALS) |
| 92 | + .put(MODULO.getSymbol(), MODULO) |
| 93 | + .put(MULTIPLY.getSymbol(), MULTIPLY) |
| 94 | + .put(NOT_EQUALS.getSymbol(), NOT_EQUALS) |
| 95 | + .put(SUBTRACT.getSymbol(), SUBTRACT) |
| 96 | + .buildOrThrow(); |
| 97 | + |
| 98 | + /** Lookup an operator by its unmangled name, as used with the source text of an expression. */ |
| 99 | + static Optional<Operator> find(String text) { |
| 100 | + return Optional.ofNullable(OPERATORS.get(text)); |
| 101 | + } |
| 102 | + |
| 103 | + private static final ImmutableMap<String, Operator> REVERSE_OPERATORS = |
| 104 | + ImmutableMap.<String, Operator>builder() |
| 105 | + .put(ADD.getFunction(), ADD) |
| 106 | + .put(ALL.getFunction(), ALL) |
| 107 | + .put(CONDITIONAL.getFunction(), CONDITIONAL) |
| 108 | + .put(DIVIDE.getFunction(), DIVIDE) |
| 109 | + .put(EQUALS.getFunction(), EQUALS) |
| 110 | + .put(EXISTS.getFunction(), EXISTS) |
| 111 | + .put(EXISTS_ONE.getFunction(), EXISTS_ONE) |
| 112 | + .put(FILTER.getFunction(), FILTER) |
| 113 | + .put(GREATER.getFunction(), GREATER) |
| 114 | + .put(GREATER_EQUALS.getFunction(), GREATER_EQUALS) |
| 115 | + .put(HAS.getFunction(), HAS) |
| 116 | + .put(IN.getFunction(), IN) |
| 117 | + .put(INDEX.getFunction(), INDEX) |
| 118 | + .put(LESS.getFunction(), LESS) |
| 119 | + .put(LESS_EQUALS.getFunction(), LESS_EQUALS) |
| 120 | + .put(LOGICAL_AND.getFunction(), LOGICAL_AND) |
| 121 | + .put(LOGICAL_NOT.getFunction(), LOGICAL_NOT) |
| 122 | + .put(LOGICAL_OR.getFunction(), LOGICAL_OR) |
| 123 | + .put(MAP.getFunction(), MAP) |
| 124 | + .put(MODULO.getFunction(), MODULO) |
| 125 | + .put(MULTIPLY.getFunction(), MULTIPLY) |
| 126 | + .put(NEGATE.getFunction(), NEGATE) |
| 127 | + .put(NOT_EQUALS.getFunction(), NOT_EQUALS) |
| 128 | + .put(NOT_STRICTLY_FALSE.getFunction(), NOT_STRICTLY_FALSE) |
| 129 | + .put(OLD_IN.getFunction(), OLD_IN) |
| 130 | + .put(OLD_NOT_STRICTLY_FALSE.getFunction(), OLD_NOT_STRICTLY_FALSE) |
| 131 | + .put(OPTIONAL_INDEX.getFunction(), OPTIONAL_INDEX) |
| 132 | + .put(OPTIONAL_SELECT.getFunction(), OPTIONAL_SELECT) |
| 133 | + .put(SUBTRACT.getFunction(), SUBTRACT) |
| 134 | + .buildOrThrow(); |
| 135 | + |
| 136 | + // precedence of the operator, where the higher value means higher. |
| 137 | + private static final ImmutableMap<String, Integer> PRECEDENCES = |
| 138 | + ImmutableMap.<String, Integer>builder() |
| 139 | + .put(CONDITIONAL.getFunction(), 8) |
| 140 | + .put(LOGICAL_OR.getFunction(), 7) |
| 141 | + .put(LOGICAL_AND.getFunction(), 6) |
| 142 | + .put(EQUALS.getFunction(), 5) |
| 143 | + .put(GREATER.getFunction(), 5) |
| 144 | + .put(GREATER_EQUALS.getFunction(), 5) |
| 145 | + .put(IN.getFunction(), 5) |
| 146 | + .put(LESS.getFunction(), 5) |
| 147 | + .put(LESS_EQUALS.getFunction(), 5) |
| 148 | + .put(NOT_EQUALS.getFunction(), 5) |
| 149 | + .put(ADD.getFunction(), 4) |
| 150 | + .put(SUBTRACT.getFunction(), 4) |
| 151 | + .put(DIVIDE.getFunction(), 3) |
| 152 | + .put(MODULO.getFunction(), 3) |
| 153 | + .put(MULTIPLY.getFunction(), 3) |
| 154 | + .put(LOGICAL_NOT.getFunction(), 2) |
| 155 | + .put(NEGATE.getFunction(), 2) |
| 156 | + .put(INDEX.getFunction(), 1) |
| 157 | + .buildOrThrow(); |
| 158 | + |
| 159 | + private static final ImmutableMap<String, String> UNARY_OPERATORS = |
| 160 | + ImmutableMap.<String, String>builder() |
| 161 | + .put(NEGATE.getFunction(), "-") |
| 162 | + .put(LOGICAL_NOT.getFunction(), "!") |
| 163 | + .buildOrThrow(); |
| 164 | + |
| 165 | + private static final ImmutableMap<String, String> BINARY_OPERATORS = |
| 166 | + ImmutableMap.<String, String>builder() |
| 167 | + .put(LOGICAL_OR.getFunction(), "||") |
| 168 | + .put(LOGICAL_AND.getFunction(), "&&") |
| 169 | + .put(LESS_EQUALS.getFunction(), "<=") |
| 170 | + .put(LESS.getFunction(), "<") |
| 171 | + .put(GREATER_EQUALS.getFunction(), ">=") |
| 172 | + .put(GREATER.getFunction(), ">") |
| 173 | + .put(EQUALS.getFunction(), "==") |
| 174 | + .put(NOT_EQUALS.getFunction(), "!=") |
| 175 | + .put(IN.getFunction(), "in") |
| 176 | + .put(ADD.getFunction(), "+") |
| 177 | + .put(SUBTRACT.getFunction(), "-") |
| 178 | + .put(MULTIPLY.getFunction(), "*") |
| 179 | + .put(DIVIDE.getFunction(), "/") |
| 180 | + .put(MODULO.getFunction(), "%") |
| 181 | + .buildOrThrow(); |
| 182 | + |
| 183 | + /** Lookup an operator by its mangled name (ex: _&&_), as used within the AST. */ |
| 184 | + public static Optional<Operator> findReverse(String op) { |
| 185 | + return Optional.ofNullable(REVERSE_OPERATORS.get(op)); |
| 186 | + } |
| 187 | + |
| 188 | + /** Lookup a binary operator by its mangled name, as used within the AST. */ |
| 189 | + static Optional<Operator> findReverseBinaryOperator(String op) { |
| 190 | + if (Objects.equals(op, LOGICAL_NOT.getFunction()) || Objects.equals(op, NEGATE.getFunction())) { |
| 191 | + return Optional.empty(); |
| 192 | + } |
| 193 | + return Optional.ofNullable(REVERSE_OPERATORS.get(op)); |
| 194 | + } |
| 195 | + |
| 196 | + static int lookupPrecedence(String op) { |
| 197 | + return PRECEDENCES.getOrDefault(op, 0); |
| 198 | + } |
| 199 | + |
| 200 | + static Optional<String> lookupUnaryOperator(String op) { |
| 201 | + return Optional.ofNullable(UNARY_OPERATORS.get(op)); |
| 202 | + } |
| 203 | + |
| 204 | + static Optional<String> lookupBinaryOperator(String op) { |
| 205 | + return Optional.ofNullable(BINARY_OPERATORS.get(op)); |
| 206 | + } |
| 207 | + |
| 208 | + static boolean isOperatorLowerPrecedence(String op, CelExpr expr) { |
| 209 | + if (!expr.exprKind().getKind().equals(CelExpr.ExprKind.Kind.CALL)) { |
| 210 | + return false; |
| 211 | + } |
| 212 | + return lookupPrecedence(op) < lookupPrecedence(expr.call().function()); |
| 213 | + } |
| 214 | + |
| 215 | + static boolean isOperatorLeftRecursive(String op) { |
| 216 | + return !op.equals(LOGICAL_AND.getFunction()) && !op.equals(LOGICAL_OR.getFunction()); |
| 217 | + } |
| 218 | +} |
0 commit comments