Skip to content

Commit 141dda8

Browse files
l46kokcopybara-github
authored andcommitted
Add Mutable versions of NavigableExpr and AST
PiperOrigin-RevId: 625488362
1 parent 473e439 commit 141dda8

File tree

9 files changed

+403
-3
lines changed

9 files changed

+403
-3
lines changed

common/navigation/BUILD.bazel

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,17 @@ package(
33
default_visibility = ["//visibility:public"],
44
)
55

6+
java_library(
7+
name = "common",
8+
exports = ["//common/src/main/java/dev/cel/common/navigation:common"],
9+
)
10+
611
java_library(
712
name = "navigation",
813
exports = ["//common/src/main/java/dev/cel/common/navigation"],
914
)
15+
16+
java_library(
17+
name = "mutable_navigation",
18+
exports = ["//common/src/main/java/dev/cel/common/navigation:mutable_navigation"],
19+
)

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

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,57 @@ package(
88
)
99

1010
java_library(
11-
name = "navigation",
11+
name = "common",
1212
srcs = [
1313
"BaseNavigableExpr.java",
14-
"CelNavigableAst.java",
15-
"CelNavigableExpr.java",
1614
"CelNavigableExprVisitor.java",
1715
"ExprPropertyCalculator.java",
1816
"TraversalOrder.java",
1917
],
2018
tags = [
2119
],
2220
deps = [
21+
"//:auto_value",
22+
"//common/ast",
23+
"@maven//:com_google_errorprone_error_prone_annotations",
24+
"@maven//:com_google_guava_guava",
25+
"@maven//:org_jspecify_jspecify",
26+
],
27+
)
28+
29+
java_library(
30+
name = "navigation",
31+
srcs = [
32+
"CelNavigableAst.java",
33+
"CelNavigableExpr.java",
34+
],
35+
tags = [
36+
],
37+
deps = [
38+
":common",
2339
"//:auto_value",
2440
"//common",
2541
"//common/ast",
2642
"@maven//:com_google_errorprone_error_prone_annotations",
2743
"@maven//:com_google_guava_guava",
44+
"@maven//:org_jspecify_jspecify",
45+
],
46+
)
47+
48+
java_library(
49+
name = "mutable_navigation",
50+
srcs = [
51+
"CelNavigableMutableAst.java",
52+
"CelNavigableMutableExpr.java",
53+
],
54+
tags = [
55+
],
56+
deps = [
57+
":common",
58+
"//:auto_value",
59+
"//common/ast:mutable_ast",
60+
"@maven//:com_google_errorprone_error_prone_annotations",
61+
"@maven//:com_google_guava_guava",
62+
"@maven//:org_jspecify_jspecify",
2863
],
2964
)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// Copyright 2024 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.navigation;
16+
17+
import dev.cel.common.ast.CelMutableAst;
18+
19+
/**
20+
* Decorates a {@link CelMutableAst} with navigational properties. This allows us to visit a node's
21+
* children, descendants or its parent with ease.
22+
*/
23+
public final class CelNavigableMutableAst {
24+
25+
private final CelMutableAst ast;
26+
private final CelNavigableMutableExpr root;
27+
28+
private CelNavigableMutableAst(CelMutableAst mutableAst) {
29+
this.ast = mutableAst;
30+
this.root = CelNavigableMutableExpr.fromExpr(mutableAst.expr());
31+
}
32+
33+
/** Constructs a new instance of {@link CelNavigableMutableAst} from {@link CelMutableAst}. */
34+
public static CelNavigableMutableAst fromAst(CelMutableAst ast) {
35+
return new CelNavigableMutableAst(ast);
36+
}
37+
38+
/** Returns the root of the AST. */
39+
public CelNavigableMutableExpr getRoot() {
40+
return root;
41+
}
42+
43+
/** Returns the underlying {@link CelMutableAst}. */
44+
public CelMutableAst getAst() {
45+
return ast;
46+
}
47+
}
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright 2024 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.navigation;
16+
17+
import com.google.auto.value.AutoValue;
18+
import dev.cel.common.ast.CelMutableExpr;
19+
import dev.cel.common.navigation.ExprPropertyCalculator.ExprProperty;
20+
import java.util.Optional;
21+
import java.util.stream.Stream;
22+
23+
/**
24+
* CelNavigableMutableExpr decorates {@link CelMutableExpr} with capabilities to inspect the parent
25+
* and its descendants with ease.
26+
*/
27+
@AutoValue
28+
// unchecked: Generic types are properly bound to BaseNavigableExpr
29+
// redundant override: Overriding is required to specify the return type to a concrete type.
30+
@SuppressWarnings({"unchecked", "RedundantOverride"})
31+
public abstract class CelNavigableMutableExpr extends BaseNavigableExpr<CelMutableExpr> {
32+
33+
/** Constructs a new instance of {@link CelNavigableMutableExpr} from {@link CelMutableExpr}. */
34+
public static CelNavigableMutableExpr fromExpr(CelMutableExpr expr) {
35+
ExprPropertyCalculator<CelMutableExpr> exprHeightCalculator =
36+
new ExprPropertyCalculator<>(expr);
37+
ExprProperty exprProperty = exprHeightCalculator.getProperty(expr.id());
38+
39+
return builder()
40+
.setExpr(expr)
41+
.setHeight(exprProperty.height())
42+
.setMaxId(exprProperty.maxId())
43+
.build();
44+
}
45+
46+
@Override
47+
public Stream<CelNavigableMutableExpr> allNodes() {
48+
return super.allNodes();
49+
}
50+
51+
@Override
52+
public Stream<CelNavigableMutableExpr> allNodes(TraversalOrder traversalOrder) {
53+
return super.allNodes(traversalOrder);
54+
}
55+
56+
@Override
57+
public abstract Optional<CelNavigableMutableExpr> parent();
58+
59+
@Override
60+
public Stream<CelNavigableMutableExpr> descendants() {
61+
return super.descendants();
62+
}
63+
64+
@Override
65+
public Stream<CelNavigableMutableExpr> descendants(TraversalOrder traversalOrder) {
66+
return super.descendants(traversalOrder);
67+
}
68+
69+
@Override
70+
public Stream<CelNavigableMutableExpr> children() {
71+
return super.children();
72+
}
73+
74+
@Override
75+
public Stream<CelNavigableMutableExpr> children(TraversalOrder traversalOrder) {
76+
return super.children(traversalOrder);
77+
}
78+
79+
@Override
80+
public Builder builderFromInstance() {
81+
return builder();
82+
}
83+
84+
/** Create a new builder to construct a {@link CelNavigableExpr} instance. */
85+
public static CelNavigableMutableExpr.Builder builder() {
86+
return new AutoValue_CelNavigableMutableExpr.Builder().setDepth(0).setHeight(0).setMaxId(0);
87+
}
88+
89+
/** Builder to configure {@link CelNavigableExpr}. */
90+
@AutoValue.Builder
91+
public abstract static class Builder
92+
implements BaseNavigableExpr.Builder<CelMutableExpr, CelNavigableMutableExpr> {
93+
94+
@Override
95+
public abstract Builder setParent(CelNavigableMutableExpr value);
96+
97+
@Override
98+
public abstract Builder setExpr(CelMutableExpr value);
99+
100+
@Override
101+
public abstract Builder setDepth(int value);
102+
103+
@Override
104+
public abstract Builder setMaxId(long value);
105+
106+
@Override
107+
public abstract Builder setHeight(int value);
108+
}
109+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ java_library(
1212
"//common:compiler_common",
1313
"//common:options",
1414
"//common/ast",
15+
"//common/ast:mutable_ast",
1516
"//common/navigation",
17+
"//common/navigation:common",
18+
"//common/navigation:mutable_navigation",
1619
"//common/resources/testdata/proto3:test_all_types_java_proto",
1720
"//common/types",
1821
"//compiler",
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright 2024 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.navigation;
16+
17+
import static com.google.common.truth.Truth.assertThat;
18+
19+
import dev.cel.common.ast.CelConstant;
20+
import dev.cel.common.ast.CelMutableAst;
21+
import dev.cel.common.ast.CelMutableExpr;
22+
import dev.cel.compiler.CelCompiler;
23+
import dev.cel.compiler.CelCompilerFactory;
24+
import org.junit.Test;
25+
import org.junit.runner.RunWith;
26+
import org.junit.runners.JUnit4;
27+
28+
@RunWith(JUnit4.class)
29+
public class CelNavigableMutableAstTest {
30+
31+
@Test
32+
public void construct_success() throws Exception {
33+
CelCompiler celCompiler = CelCompilerFactory.standardCelCompilerBuilder().build();
34+
CelMutableAst ast = CelMutableAst.fromCelAst(celCompiler.compile("'Hello World'").getAst());
35+
36+
CelNavigableMutableAst navigableAst = CelNavigableMutableAst.fromAst(ast);
37+
38+
assertThat(navigableAst.getAst()).isEqualTo(ast);
39+
assertThat(navigableAst.getRoot().expr())
40+
.isEqualTo(CelMutableExpr.ofConstant(1, CelConstant.ofValue("Hello World")));
41+
assertThat(navigableAst.getRoot().parent()).isEmpty();
42+
assertThat(navigableAst.getRoot().depth()).isEqualTo(0);
43+
}
44+
}

0 commit comments

Comments
 (0)