Skip to content

Commit 0fb05e5

Browse files
l46kokcopybara-github
authored andcommitted
Remove dev.cel.parser.Operator, migrate parser package to dev.cel.common.Operator
PiperOrigin-RevId: 826106837
1 parent 2e6da1e commit 0fb05e5

File tree

12 files changed

+115
-331
lines changed

12 files changed

+115
-331
lines changed

common/src/main/java/dev/cel/common/Operator.java

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,14 @@
1515
package dev.cel.common;
1616

1717
import com.google.common.collect.ImmutableMap;
18-
import dev.cel.common.ast.CelExpr;
1918
import java.util.Objects;
2019
import java.util.Optional;
2120

2221
/**
2322
* Package-private enumeration of Common Expression Language operators.
2423
*
25-
* <p>Equivalent to https://pkg.go.dev/github.com/google/cel-go/common/operators.
24+
* <p>Equivalent to <a
25+
* href="https://pkg.go.dev/github.com/google/cel-go/common/operators">operators<a>.
2626
*/
2727
public enum Operator {
2828
CONDITIONAL("_?_:_"),
@@ -96,7 +96,7 @@ String getSymbol() {
9696
.buildOrThrow();
9797

9898
/** Lookup an operator by its unmangled name, as used with the source text of an expression. */
99-
static Optional<Operator> find(String text) {
99+
public static Optional<Operator> find(String text) {
100100
return Optional.ofNullable(OPERATORS.get(text));
101101
}
102102

@@ -193,26 +193,23 @@ static Optional<Operator> findReverseBinaryOperator(String op) {
193193
return Optional.ofNullable(REVERSE_OPERATORS.get(op));
194194
}
195195

196-
static int lookupPrecedence(String op) {
196+
/**
197+
* Returns the precedence of the operator.
198+
*
199+
* @return Integer value describing precedence. Higher value means higher precedence. Returns 0 if
200+
* the operator is not found.
201+
*/
202+
public static int lookupPrecedence(String op) {
197203
return PRECEDENCES.getOrDefault(op, 0);
198204
}
199205

200-
static Optional<String> lookupUnaryOperator(String op) {
206+
/** Looks up the associated unary operator based on its function name (Ex: !_ -> !) */
207+
public static Optional<String> lookupUnaryOperator(String op) {
201208
return Optional.ofNullable(UNARY_OPERATORS.get(op));
202209
}
203210

204-
static Optional<String> lookupBinaryOperator(String op) {
211+
/** Looks up the associated binary operator based on its function name (Ex: _||_ -> ||) */
212+
public static Optional<String> lookupBinaryOperator(String op) {
205213
return Optional.ofNullable(BINARY_OPERATORS.get(op));
206214
}
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-
}
218215
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ java_library(
1717
"//common:cel_source",
1818
"//common:compiler_common",
1919
"//common:container",
20+
"//common:operator",
2021
"//common:options",
2122
"//common:proto_ast",
2223
"//common:proto_json_adapter",

parser/src/test/java/dev/cel/parser/OperatorTest.java renamed to common/src/test/java/dev/cel/common/OperatorTest.java

Lines changed: 1 addition & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package dev.cel.parser;
15+
package dev.cel.common;
1616

1717
import static com.google.common.truth.Truth.assertThat;
1818
import static org.junit.Assert.assertEquals;
19-
import static org.junit.Assert.assertFalse;
20-
import static org.junit.Assert.assertTrue;
2119

2220
import com.google.testing.junit.testparameterinjector.TestParameter;
2321
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
2422
import com.google.testing.junit.testparameterinjector.TestParameters;
25-
import dev.cel.common.ast.CelExpr;
26-
import dev.cel.common.ast.CelExpr.CelCall;
2723
import java.util.Optional;
2824
import org.junit.Test;
2925
import org.junit.runner.RunWith;
@@ -150,70 +146,4 @@ public void lookupBinaryOperator_nonEmpty(String operator, String value) {
150146
public void lookupBinaryOperator_empty(String operator) {
151147
assertEquals(Operator.lookupBinaryOperator(operator), Optional.empty());
152148
}
153-
154-
@Test
155-
@TestParameters({
156-
"{operator1: '_[_]', operator2: '_&&_'}",
157-
"{operator1: '_&&_', operator2: '_||_'}",
158-
"{operator1: '_||_', operator2: '_?_:_'}",
159-
"{operator1: '!_', operator2: '_*_'}",
160-
"{operator1: '_==_', operator2: '_&&_'}",
161-
"{operator1: '_!=_', operator2: '_?_:_'}",
162-
})
163-
public void operatorLowerPrecedence(String operator1, String operator2) {
164-
CelExpr expr =
165-
CelExpr.newBuilder().setCall(CelCall.newBuilder().setFunction(operator2).build()).build();
166-
167-
assertTrue(Operator.isOperatorLowerPrecedence(operator1, expr));
168-
}
169-
170-
@Test
171-
@TestParameters({
172-
"{operator1: '_?_:_', operator2: '_&&_'}",
173-
"{operator1: '_&&_', operator2: '_[_]'}",
174-
"{operator1: '_||_', operator2: '!_'}",
175-
"{operator1: '!_', operator2: '-_'}",
176-
"{operator1: '_==_', operator2: '_!=_'}",
177-
"{operator1: '_!=_', operator2: '_-_'}",
178-
})
179-
public void operatorNotLowerPrecedence(String operator1, String operator2) {
180-
CelExpr expr =
181-
CelExpr.newBuilder().setCall(CelCall.newBuilder().setFunction(operator2).build()).build();
182-
183-
assertFalse(Operator.isOperatorLowerPrecedence(operator1, expr));
184-
}
185-
186-
@Test
187-
@TestParameters({
188-
"{operator: '_[_]'}",
189-
"{operator: '!_'}",
190-
"{operator: '_==_'}",
191-
"{operator: '_?_:_'}",
192-
"{operator: '_!=_'}",
193-
"{operator: '_<_'}",
194-
"{operator: '_<=_'}",
195-
"{operator: '_>_'}",
196-
"{operator: '_>=_'}",
197-
"{operator: '_+_'}",
198-
"{operator: '_-_'}",
199-
"{operator: '_*_'}",
200-
"{operator: '_/_'}",
201-
"{operator: '_%_'}",
202-
"{operator: '-_'}",
203-
"{operator: 'has'}",
204-
"{operator: '_[?_]'}",
205-
"{operator: '@not_strictly_false'}",
206-
})
207-
public void operatorLeftRecursive(String operator) {
208-
assertTrue(Operator.isOperatorLeftRecursive(operator));
209-
}
210-
211-
@Test
212-
@TestParameters({
213-
"{operator: '_&&_'}",
214-
"{operator: '_||_'}",
215-
})
216-
public void operatorNotLeftRecursive(String operator) {
217-
assertFalse(Operator.isOperatorLeftRecursive(operator));
218-
}
219149
}

parser/BUILD.bazel

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,6 @@ java_library(
3131
exports = ["//parser/src/main/java/dev/cel/parser:macro"],
3232
)
3333

34-
java_library(
35-
name = "operator",
36-
exports = ["//parser/src/main/java/dev/cel/parser:operator"],
37-
)
38-
3934
java_library(
4035
name = "cel_g4_visitors",
4136
visibility = ["//:internal"],

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

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,11 +56,11 @@ java_library(
5656
],
5757
deps = [
5858
":macro",
59-
":operator",
6059
":parser_builder",
6160
"//common:cel_ast",
6261
"//common:cel_source",
6362
"//common:compiler_common",
63+
"//common:operator",
6464
"//common:options",
6565
"//common:source_location",
6666
"//common/annotations",
@@ -94,9 +94,9 @@ java_library(
9494
tags = [
9595
],
9696
deps = [
97-
":operator",
9897
"//:auto_value",
9998
"//common:compiler_common",
99+
"//common:operator",
100100
"//common:source_location",
101101
"//common/ast",
102102
"//common/ast:expr_factory",
@@ -105,17 +105,6 @@ java_library(
105105
],
106106
)
107107

108-
java_library(
109-
name = "operator",
110-
srcs = ["Operator.java"],
111-
tags = [
112-
],
113-
deps = [
114-
"//common/ast",
115-
"@maven//:com_google_guava_guava",
116-
],
117-
)
118-
119108
java_library(
120109
name = "unparser",
121110
srcs = UNPARSER_SOURCES,
@@ -134,9 +123,9 @@ java_library(
134123
tags = [
135124
],
136125
deps = [
137-
":operator",
138126
"//common:cel_ast",
139127
"//common:cel_source",
128+
"//common:operator",
140129
"//common/ast",
141130
"//common/ast:cel_expr_visitor",
142131
"//common/values:cel_byte_string",

parser/src/main/java/dev/cel/parser/CelStandardMacro.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.collect.ImmutableList;
2121
import com.google.common.collect.ImmutableSet;
2222
import dev.cel.common.CelIssue;
23+
import dev.cel.common.Operator;
2324
import dev.cel.common.ast.CelExpr;
2425
import java.util.Optional;
2526

parser/src/main/java/dev/cel/parser/CelUnparserVisitor.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,15 @@
1313
// limitations under the License.
1414
package dev.cel.parser;
1515

16+
import static dev.cel.common.Operator.LOGICAL_AND;
17+
import static dev.cel.common.Operator.LOGICAL_OR;
18+
19+
import com.google.common.annotations.VisibleForTesting;
1620
import com.google.common.collect.ImmutableSet;
1721
import com.google.re2j.Pattern;
1822
import dev.cel.common.CelAbstractSyntaxTree;
1923
import dev.cel.common.CelSource;
24+
import dev.cel.common.Operator;
2025
import dev.cel.common.ast.CelConstant;
2126
import dev.cel.common.ast.CelExpr;
2227
import dev.cel.common.ast.CelExpr.CelCall;
@@ -273,7 +278,7 @@ private void visitBinary(CelCall expr, String op) {
273278
// add parens if the current operator is lower precedence than the rhs expr
274279
// operator, or the same precedence and the operator is left recursive.
275280
boolean rhsParen = isComplexOperatorWithRespectTo(rhs, fun);
276-
if (!rhsParen && Operator.isOperatorLeftRecursive(fun)) {
281+
if (!rhsParen && isOperatorLeftRecursive(fun)) {
277282
rhsParen = isOperatorSamePrecedence(fun, rhs);
278283
}
279284

@@ -366,7 +371,7 @@ private boolean isComplexOperatorWithRespectTo(CelExpr expr, String op) {
366371
return false;
367372
}
368373
// Otherwise, return whether the given op has lower precedence than expr
369-
return Operator.isOperatorLowerPrecedence(op, expr);
374+
return isOperatorLowerPrecedence(op, expr);
370375
}
371376

372377
// bytesToOctets converts byte sequences to a string using a three digit octal encoded value
@@ -378,4 +383,17 @@ private String bytesToOctets(CelByteString bytes) {
378383
}
379384
return sb.toString();
380385
}
386+
387+
@VisibleForTesting
388+
static boolean isOperatorLeftRecursive(String op) {
389+
return !op.equals(LOGICAL_AND.getFunction()) && !op.equals(LOGICAL_OR.getFunction());
390+
}
391+
392+
@VisibleForTesting
393+
static boolean isOperatorLowerPrecedence(String op, CelExpr expr) {
394+
if (!expr.exprKind().getKind().equals(CelExpr.ExprKind.Kind.CALL)) {
395+
return false;
396+
}
397+
return Operator.lookupPrecedence(op) < Operator.lookupPrecedence(expr.call().function());
398+
}
381399
}

0 commit comments

Comments
 (0)