Skip to content

Commit 2e6da1e

Browse files
l46kokcopybara-github
authored andcommitted
Copy dev.cel.parser.Operator enum into dev.cel.common package
This enum is being promoted into common package, as it's already in use across multiple packages. PiperOrigin-RevId: 825268198
1 parent 4aeba25 commit 2e6da1e

File tree

26 files changed

+292
-62
lines changed

26 files changed

+292
-62
lines changed

checker/src/main/java/dev/cel/checker/BUILD.bazel

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ java_library(
179179
"//common:cel_ast",
180180
"//common:compiler_common",
181181
"//common:container",
182+
"//common:operator",
182183
"//common:options",
183184
"//common:proto_ast",
184185
"//common/annotations",
@@ -191,7 +192,6 @@ java_library(
191192
"//common/types:cel_types",
192193
"//common/types:type_providers",
193194
"//parser:macro",
194-
"//parser:operator",
195195
"@cel_spec//proto/cel/expr:checked_java_proto",
196196
"@cel_spec//proto/cel/expr:syntax_java_proto",
197197
"@maven//:com_google_errorprone_error_prone_annotations",
@@ -239,10 +239,10 @@ java_library(
239239
deps = [
240240
":cel_ident_decl",
241241
"//common:compiler_common",
242+
"//common:operator",
242243
"//common/types",
243244
"//common/types:cel_types",
244245
"//common/types:type_providers",
245-
"//parser:operator",
246246
"@maven//:com_google_errorprone_error_prone_annotations",
247247
"@maven//:com_google_guava_guava",
248248
],

checker/src/main/java/dev/cel/checker/CelStandardDeclarations.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@
2323
import com.google.errorprone.annotations.Immutable;
2424
import dev.cel.common.CelFunctionDecl;
2525
import dev.cel.common.CelOverloadDecl;
26+
import dev.cel.common.Operator;
2627
import dev.cel.common.types.CelType;
2728
import dev.cel.common.types.CelTypes;
2829
import dev.cel.common.types.ListType;
2930
import dev.cel.common.types.MapType;
3031
import dev.cel.common.types.SimpleType;
3132
import dev.cel.common.types.TypeParamType;
3233
import dev.cel.common.types.TypeType;
33-
import dev.cel.parser.Operator;
3434

3535
/**
3636
* Standard declarations for CEL.

checker/src/main/java/dev/cel/checker/ExprChecker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import dev.cel.common.CelFunctionDecl;
3232
import dev.cel.common.CelOverloadDecl;
3333
import dev.cel.common.CelProtoAbstractSyntaxTree;
34+
import dev.cel.common.Operator;
3435
import dev.cel.common.annotations.Internal;
3536
import dev.cel.common.ast.CelConstant;
3637
import dev.cel.common.ast.CelExpr;
@@ -44,7 +45,6 @@
4445
import dev.cel.common.types.OptionalType;
4546
import dev.cel.common.types.SimpleType;
4647
import dev.cel.common.types.TypeType;
47-
import dev.cel.parser.Operator;
4848
import java.util.ArrayList;
4949
import java.util.HashSet;
5050
import java.util.List;

checker/src/test/java/dev/cel/checker/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ java_library(
2727
"//common:compiler_common",
2828
"//common:container",
2929
"//common:mutable_ast",
30+
"//common:operator",
3031
"//common:options",
3132
"//common:proto_ast",
3233
"//common:source_location",
@@ -42,7 +43,6 @@ java_library(
4243
"//compiler",
4344
"//compiler:compiler_builder",
4445
"//parser:macro",
45-
"//parser:operator",
4646
"//testing:adorner",
4747
"//testing:cel_baseline_test_case",
4848
"@maven//:junit_junit",

checker/src/test/java/dev/cel/checker/CelProtoExprVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
import com.google.auto.value.AutoValue;
2323
import dev.cel.common.CelAbstractSyntaxTree;
2424
import dev.cel.common.CelContainer;
25+
import dev.cel.common.Operator;
2526
import dev.cel.common.types.SimpleType;
2627
import dev.cel.compiler.CelCompiler;
2728
import dev.cel.compiler.CelCompilerFactory;
2829
import dev.cel.expr.conformance.proto3.TestAllTypes;
2930
import dev.cel.parser.CelStandardMacro;
30-
import dev.cel.parser.Operator;
3131
import java.util.Optional;
3232
import org.junit.Before;
3333
import org.junit.Test;

common/BUILD.bazel

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,8 @@ java_library(
121121
],
122122
exports = ["//common/src/main/java/dev/cel/common:cel_descriptor_util"],
123123
)
124+
125+
java_library(
126+
name = "operator",
127+
exports = ["//common/src/main/java/dev/cel/common:operator"],
128+
)

common/src/main/java/dev/cel/common/BUILD.bazel

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,3 +354,14 @@ java_library(
354354
"@maven//:com_google_guava_guava",
355355
],
356356
)
357+
358+
java_library(
359+
name = "operator",
360+
srcs = ["Operator.java"],
361+
tags = [
362+
],
363+
deps = [
364+
"//common/ast",
365+
"@maven//:com_google_guava_guava",
366+
],
367+
)
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
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+
}

common/src/test/java/dev/cel/common/ast/BUILD.bazel

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ java_library(
1717
"//common:container",
1818
"//common:mutable_ast",
1919
"//common:mutable_source",
20+
"//common:operator",
2021
"//common:options",
2122
"//common/ast",
2223
"//common/ast:cel_expr_visitor",
@@ -31,7 +32,6 @@ java_library(
3132
"//compiler:compiler_builder",
3233
"//extensions:optional_library",
3334
"//parser:macro",
34-
"//parser:operator",
3535
"@cel_spec//proto/cel/expr:checked_java_proto",
3636
"@cel_spec//proto/cel/expr:syntax_java_proto",
3737
"@cel_spec//proto/cel/expr/conformance/proto3:test_all_types_java_proto",

common/src/test/java/dev/cel/common/ast/CelExprVisitorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import dev.cel.common.CelAbstractSyntaxTree;
2323
import dev.cel.common.CelContainer;
2424
import dev.cel.common.CelOptions;
25+
import dev.cel.common.Operator;
2526
import dev.cel.common.ast.CelExpr.CelCall;
2627
import dev.cel.common.ast.CelExpr.CelComprehension;
2728
import dev.cel.common.ast.CelExpr.CelIdent;
@@ -35,7 +36,6 @@
3536
import dev.cel.compiler.CelCompilerFactory;
3637
import dev.cel.expr.conformance.proto3.TestAllTypes;
3738
import dev.cel.parser.CelStandardMacro;
38-
import dev.cel.parser.Operator;
3939
import java.util.Optional;
4040
import org.junit.Before;
4141
import org.junit.Test;

0 commit comments

Comments
 (0)